Members
-
n :integer
The dimension of the ambient
R^n. -
Description
All vectors in this subspace have
nentries.Examples
new Subspace([[1, 2, 3], [4, 5, 6]]).n; // 3Details
-
basis :Matrix
The columns of this matrix form a basis for this subspace.
-
Description
A nonzero subspace has infinitely many bases. This is the basis created in the constructor from the passed generators.
Examples
let A = Matrix.create([ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7]); let V = new Subspace(A); V.basis.toString(1); // "[ 0.0 -3.0 4.0] // [-1.0 -2.0 3.0] // [-2.0 -3.0 3.0] // [ 1.0 4.0 -9.0]"Details
-
dim :integer
The dimension of the subspace.
-
Description
The dimension of a subspace is by definition the number of vectors in any basis of the subspace. Hence this is an alias for
this.basis.n.Examples
let A = Matrix.create([ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7]); let V = new Subspace(A); V.dim; // 3Details
Methods
-
<static> Rn( n ) → {Subspace}
The subspace
R^n. -
Description
This creates the subspace of
R^nwith basis equal to thenxnidentity matrix.Parameters
Name Type Description ninteger The dimension of the space.
Returns
Examples
Subspace.Rn(3).toString(); // "The full subspace R^3"Details
-
<static> zero( n ) → {Subspace}
The zero subspace of
R^n. -
Description
This creates the subspace of
R^nwith no generators.Parameters
Name Type Description ninteger The ambient dimension.
Returns
Examples
Subspace.zero(3).toString(); // "The zero subspace of R^3"Details
-
toString( [ precision ] ) → {string}
Return a string representation of the subspace.
-
Parameters
Name Type Attributes Default Description precisioninteger <optional> 4 The number of decimal places to include.
Returns
Examples
let A = Matrix.create([ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7]); let V = new Subspace(A); V.toString(1); // "Subspace of R^4 of dimension 3 with basis // [ 0.0] [-3.0] [ 4.0] // [-1.0] [-2.0] [ 3.0] // [-2.0] [-3.0] [ 3.0] // [ 1.0] [ 4.0] [-9.0]"Details
-
isMaximal() → {boolean}
Test whether the subspace is all of
R^n. -
Description
The only
n-dimensional subspace ofR^nisR^nitself, so this is a shortcut forthis.dim === this.n.Returns
Examples
Subspace.Rn(3).isMaximal(); // truelet A = Matrix.create([ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7]); new Subspace(A).isMaximal(); // falseDetails
-
isZero() → {boolean}
Test whether the subspace only contains the zero vector.
-
Description
The only zero-dimensional subspace of
R^nis{0}, so this is a shortcut forthis.dim === 0.Returns
Examples
Subspace.zero(3).isZero(); // truelet A = Matrix.create([ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7]); new Subspace(A).isZero(); // falseDetails
-
add( other [, ε ] ) → {Subspace}
Return the sum of two subspaces.
-
Description
The sum is the subspace generated by the bases of
thisandother. It is the smallest subspace containing boththisandother.Parameters
Name Type Attributes Default Description otherSubspace The subspace to add.
εnumber <optional> 1e-10 Entries smaller than this value are taken to be zero for the purposes of pivoting.
Returns
Examples
let V = new Subspace([[ 0, -1, -2, 1], [-3, -2, -3, 4]]); let W = new Subspace([[-6, -1, 0, 5], [ 4, 3, 3, -9]]); V.add(W).toString(1); // "Subspace of R^4 of dimension 3 spanned by // [ 0.0] [-3.0] [ 4.0] // [-1.0] [-2.0] [ 3.0] // [-2.0] [-3.0] [ 3.0] // [ 1.0] [ 4.0] [-9.0]"Throws
Details
-
intersect( other [, ε ] ) → {Subspace}
Take the intersection of two subspaces.
-
Description
This is the subspace consisting of all vectors contained both in
thisand inother. It is computed by taking the orthogonal complement of the sum of the orthogonal complements.Parameters
Name Type Attributes Default Description otherSubspace The subspace to intersect.
εnumber <optional> 1e-10 Entries smaller than this value are taken to be zero for the purposes of pivoting.
Returns
Examples
let V = new Subspace([[ 0, -1, -2, 1], [-3, -2, -3, 4]]); let W = new Subspace([[-6, -1, 0, 5], [ 4, 3, 3, -9]]); V.intersect(W).toString(); // "Subspace of R^4 of dimension 1 spanned by // [ 1.0000] // [ 0.1667] // [ 0.0000] // [-0.8333]"Throws
Details
-
projectionMatrix( [ ε ] ) → {Matrix}
Compute the projection matrix onto
this. -
Description
The projection matrix is the
nxnmatrixPsuch thatPvis the projection ofvonto this subspace. This method computesPusing the formulaA(A^TA)^(-1)A^T, whereAis the matrixthis.basis, unless an orthonormal basis has been computed, in which case it returnsQQ^T, where the columns ofQform an orthonormal basis.Parameters
Name Type Attributes Default Description εnumber <optional> 1e-10 Entries smaller than this value are taken to be zero for the purposes of pivoting.
Returns
Examples
let V = new Subspace(Matrix.create([ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7])); let P = V.projectionMatrix(); P.toString(); // "[1.0000 0.0000 0.0000 0.0000] // [0.0000 0.1667 0.3333 -0.1667] // [0.0000 0.3333 0.8667 0.0667] // [0.0000 -0.1667 0.0667 0.9667]" P.mult(P).equals(P, 1e-10); // true P.colSpace().equals(V); // true P.nullSpace().equals(V.perp()); // trueDetails
-
project( v [, ε ] ) → {Vector}
Compute the orthogonal projection of a vector onto
this. -
Description
The orthogonal projection
v1ofvis the closest vector tovin the subspace. It is defined by the property thatv-v1is orthogonal toV.If the projection matrix
Phas been cached,v1is computed asPv. Otherwisev1is computed usingbasis.projectColSpace(). If you want to compute many orthogonal projections, runthis.projectionMatrix()first.Parameters
Name Type Attributes Default Description vVector The vector to project.
εnumber <optional> 1e-10 Entries smaller than this value are taken to be zero for the purposes of pivoting.
Returns
Examples
let V = new Subspace(Matrix.create([ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7])); let u = Vector.create(1, 2, 3, 4); let u1 = V.project(u); u1.toString(); // "[1.0000 0.6667 3.5333 3.7333]" V.contains(u1); // true V.isOrthogonalTo(u.sub(u1)); // true let v = Vector.create(0, -1, -2, 1); // in V V.project(v).toString(0); // "[0 -1 -2 1]" let w = Vector.create(0, 5, -2, 1); // in Vperp V.project(w).toString(0); // "[0 0 0 0]"Throws
Details
-
orthoDecomp( v [, ε ] ) → {Array.<Vector>}
Compute the orthogonal decomposition of a vector with respect to
this. -
Description
This returns the unique pair of vectors
[v1, v2]such thatv1is inthis,v2is orthogonal tothis, andv1 + v2 = v. The vectorv1is the orthogonal projection ofv, andv2is justv-v1.Parameters
Name Type Attributes Default Description vVector The vector to decompose.
εnumber <optional> 1e-10 Entries smaller than this value are taken to be zero for the purposes of pivoting.
Returns
Examples
let V = new Subspace(Matrix.create([ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7])); let u = Vector.create(1, 2, 3, 4); let [u1, u2] = V.orthoDecomp(u); u1.toString(); // "[1.0000 0.6667 3.5333 3.7333]" u2.toString(); // "[0.0000 1.3333 -0.5333 0.2667]" u1.clone().add(u2).equals(u); // true V.contains(u1); // true V.isOrthogonalTo(u2); // true let v = Vector.create(0, -1, -2, 1); // in V let [v1, v2] = V.orthoDecomp(v); v1.toString(0); // "[0 -1 -2 1]" v2.toString(0); // "[0 0 0 0]" let w = Vector.create(0, 5, -2, 1); // in Vperp let [w1, w2] = V.orthoDecomp(w); w1.toString(0); // "[0 0 0 0]" w2.toString(0); // "[0 5 -2 1]"Throws
Details
-
complement( v [, ε ] ) → {Vector}
This is an alias for
this.orthoDecomp(v, ε)[1]. -
Description
The complement
v2ofvwith respect tothisis the shortest vector fromthistov. It is defined by the property thatv-v2is the orthogonal projection ofvontothis. Equivalently,v2is the orthogonal projection ofvonto the orthogonal complement.Parameters
Name Type Attributes Default Description vVector The vector to project.
εnumber <optional> 1e-10 Entries smaller than this value are taken to be zero for the purposes of pivoting.
Returns
Examples
let V = new Subspace(Matrix.create([ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7])); let u = Vector.create(1, 2, 3, 4); let u2 = V.complement(u); u2.toString(); // "[0.0000 1.3333 -0.5333 0.2667]" V.isOrthogonalTo(u2); // true V.contains(u2.sub(u)); // true let v = Vector.create(0, -1, -2, 1); // in V let v2 = V.complement(v); v2.toString(0); // "[0 0 0 0]" let w = Vector.create(0, 5, -2, 1); // in Vperp let w2 = V.complement(w); w2.toString(0); // "[0 5 -2 1]"Throws
Details
-
distanceTo( v [, ε ] ) → {number}
Compute the distance of
vfromthis. -
Description
This is an alias for
this.complement(v, ε).size.Parameters
Name Type Attributes Default Description vVector The vector to measure.
εnumber <optional> 1e-10 Entries smaller than this value are taken to be zero for the purposes of pivoting.
Returns
Throws
Details
-
contains( v [, ε ] ) → {boolean}
Test if this subspace contains a vector.
-
Description
To say that
thiscontainsvmeans thatvis a linear combination of the basis vectors.If
v.size <= εthen this method returns true. Otherwise this method measures the distance fromv.normalize()tothis, and returnstrueif the distance is at mostε.Parameters
Name Type Attributes Default Description vVector The vector to test.
εnumber <optional> 1e-10 Vectors shorter than this value are taken to be zero. Also used for pivoting if no projection matrix has been cached.
Returns
Examples
let V = new Subspace(Matrix.create([ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7])); let v = Vector.create(-9, -4, -5, 10); // Sum of the first three columns V.contains(v); // true let w = Vector.create(-9, -4, -5, 9); V.contains(w); // false V.distanceTo(w); // 0.1825 (approximately)Throws
Details
-
isOrthogonalTo( v [, ε ] ) → {boolean}
Test if a vector is orthogonal to
this. -
Description
To say that
vis orthogonal tothismeans that the dot product ofvwith the basis vectors is equal to zero.If
v.size <= εthen this method returns true. Otherwise this method measures the size of the projection ofv.normalize()ontothis, and returnstrueif the size is at mostε. This is (mathematically if not numerically) equivalent tothis.perp().contains(v, ε).Parameters
Name Type Attributes Default Description vVector The vector to test.
εnumber <optional> 1e-10 Vectors shorter than this value are taken to be zero. Also used for pivoting if no projection matrix has been cached.
Returns
Examples
let V = new Subspace(Matrix.create([ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7])); let v = Vector.create(0, 5, -2, 1); V.isOrthogonalTo(v); // true V.basis.transpose.apply(v).isZero(); // true let w = Vector.create(0, 5, -2, 2); V.isOrthogonalTo(w); // false V.project(w).size; // 0.9831 (approximately)Throws
Details
-
isSubspaceOf( other [, ε ] ) → {boolean}
Test if this subspace is contained in another subspace.
-
Description
This subspace
Vis contained in another subspaceWwhen all of the basis vectors ofVare contained inW.Parameters
Name Type Attributes Default Description otherSubspace The subspace to test.
εnumber <optional> 1e-10 Parameter passed to Subspace#contains.
Returns
Examples
let V = new Subspace([[ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7]]); let W = new Subspace([[ 1, 0, -3, 0, 5], [ 0, 0, 0, 1, 0]]); W.isSubspaceOf(V); // true W.perp().isSubspaceOf(V); // falseThrows
Details
-
equals( other [, ε ] ) → {boolean}
Test if this Subspace is equal to
other. -
Description
Two subspaces are equal if and only if they have the same dimension and one contains the other.
Parameters
Name Type Attributes Default Description otherSubspace The subspace to compare.
εnumber <optional> 1e-10 Parameter passed to Subspace#contains.
Returns
Examples
let V = new Subspace([[ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7]]); let W = new Subspace([[-1, -5, -7, 7, 10], [-3, -5, -1, 6, 0], [-1, 1, 5, -6, -8]]); let U = new Subspace([[ 1, 0, 0, 0, 0], [ 0, 1, 0, 0, 0], [ 0, 0, 1, 0, 0]]); V.equals(W); // true V.equals(U); // falseThrows
Details
-
perp( [ ε ] ) → {Subspace}
Return the orthogonal complement of
this. -
Description
The orthogonal complement of a subspace
Vis the set of all vectorswsuch thatwis orthogonal to every vector inV. Its dimension isnminus the dimension ofV.If
Vis the column space of a matrixA, then its orthogonal complement is the left null space ofA.Parameters
Name Type Attributes Default Description εnumber <optional> 1e-10 Entries smaller than this value are taken to be zero for the purposes of pivoting.
Returns
Examples
let V = new Subspace([[ 0, -3, -6, 4, 9], [-1, -2, -1, 3, 1], [-2, -3, 0, 3, -1], [ 1, 4, 5, -9, -7]]); V.toString(1); // "Subspace of R^5 of dimension 3 with basis // [ 0.0] [-1.0] [-2.0] // [-3.0] [-2.0] [-3.0] // [-6.0] [-1.0] [ 0.0] // [ 4.0] [ 3.0] [ 3.0] // [ 9.0] [ 1.0] [-1.0]" V.perp().toString(1); // "Subspace of R^5 of dimension 2 with basis // [ 0.0] [ 1.0] // [-0.2] [-0.6] // [ 1.0] [ 0.0] // [ 0.0] [-0.0] // [ 0.6] [-0.2]" Array.from(V.perp().basis.cols()).every(col => V.isOrthogonalTo(col)); // trueDetails
-
ONbasis( [ ε ] ) → {Matrix}
Compute an orthonormal basis for the subspace.
-
Description
This is a shortcut for
this.basis.QR(ε).Q.Parameters
Name Type Attributes Default Description εnumber <optional> 1e-10 Vectors shorter than this value are taken to be zero.
Returns
Examples
let V = new Subspace([[ 3, 1, -1, 3], [-5, 1, 5, -7], [ 1, 1, -2, 8]]); let Q = V.ONbasis(); Q.toString(); // "[ 0.6708 0.2236 -0.6708] // [ 0.2236 0.6708 0.2236] // [-0.2236 0.6708 0.2236] // [ 0.6708 -0.2236 0.6708]" Q.colSpace().equals(V); // true Q.transpose.mult(Q).toString(1); // "[1.0 0.0 0.0] // [0.0 1.0 0.0] // [0.0 0.0 1.0]"Details