Members
-
n :integer
The dimension of the ambient
R^n
. -
Description
All vectors in this subspace have
n
entries.Examples
new Subspace([[1, 2, 3], [4, 5, 6]]).n; // 3
Details
-
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; // 3
Details
Methods
-
<static> Rn( n ) → {Subspace}
The subspace
R^n
. -
Description
This creates the subspace of
R^n
with basis equal to then
xn
identity matrix.Parameters
Name Type Description n
integer 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^n
with no generators.Parameters
Name Type Description n
integer 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 precision
integer <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^n
isR^n
itself, so this is a shortcut forthis.dim === this.n
.Returns
Examples
Subspace.Rn(3).isMaximal(); // true
let 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(); // false
Details
-
isZero() → {boolean}
Test whether the subspace only contains the zero vector.
-
Description
The only zero-dimensional subspace of
R^n
is{0}
, so this is a shortcut forthis.dim === 0
.Returns
Examples
Subspace.zero(3).isZero(); // true
let 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(); // false
Details
-
add( other [, ε ] ) → {Subspace}
Return the sum of two subspaces.
-
Description
The sum is the subspace generated by the bases of
this
andother
. It is the smallest subspace containing boththis
andother
.Parameters
Name Type Attributes Default Description other
Subspace 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
this
and inother
. It is computed by taking the orthogonal complement of the sum of the orthogonal complements.Parameters
Name Type Attributes Default Description other
Subspace 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
n
xn
matrixP
such thatPv
is the projection ofv
onto this subspace. This method computesP
using the formulaA(A^TA)^(-1)A^T
, whereA
is the matrixthis.basis
, unless an orthonormal basis has been computed, in which case it returnsQQ^T
, where the columns ofQ
form 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()); // true
Details
-
project( v [, ε ] ) → {Vector}
Compute the orthogonal projection of a vector onto
this
. -
Description
The orthogonal projection
v1
ofv
is the closest vector tov
in the subspace. It is defined by the property thatv-v1
is orthogonal toV
.If the projection matrix
P
has been cached,v1
is computed asPv
. Otherwisev1
is computed usingbasis.projectColSpace()
. If you want to compute many orthogonal projections, runthis.projectionMatrix()
first.Parameters
Name Type Attributes Default Description v
Vector 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 thatv1
is inthis
,v2
is orthogonal tothis
, andv1 + v2 = v
. The vectorv1
is the orthogonal projection ofv
, andv2
is justv-v1
.Parameters
Name Type Attributes Default Description v
Vector 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
v2
ofv
with respect tothis
is the shortest vector fromthis
tov
. It is defined by the property thatv-v2
is the orthogonal projection ofv
ontothis
. Equivalently,v2
is the orthogonal projection ofv
onto the orthogonal complement.Parameters
Name Type Attributes Default Description v
Vector 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
v
fromthis
. -
Description
This is an alias for
this.complement(v, ε).size
.Parameters
Name Type Attributes Default Description v
Vector 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
this
containsv
means thatv
is 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 returnstrue
if the distance is at mostε
.Parameters
Name Type Attributes Default Description v
Vector 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
v
is orthogonal tothis
means that the dot product ofv
with 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 returnstrue
if the size is at mostε
. This is (mathematically if not numerically) equivalent tothis.perp().contains(v, ε)
.Parameters
Name Type Attributes Default Description v
Vector 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
V
is contained in another subspaceW
when all of the basis vectors ofV
are contained inW
.Parameters
Name Type Attributes Default Description other
Subspace 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); // false
Throws
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 other
Subspace 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); // false
Throws
Details
-
perp( [ ε ] ) → {Subspace}
Return the orthogonal complement of
this
. -
Description
The orthogonal complement of a subspace
V
is the set of all vectorsw
such thatw
is orthogonal to every vector inV
. Its dimension isn
minus the dimension ofV
.If
V
is 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)); // true
Details
-
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