new Vector()

Class representing a vector.

Description

A vector is a sequence of numbers of a fixed length, called the "length" of the vector.

Note that Vector uses the Array constructor, so new Vector(3) will return an "empty" vector of length 3. Instead use the convenience routine Vector.create.

Examples
Vector.create(1, 2, 3).toString(1);  // "[1.0 2.0 3.0]"
Details

Array

Members


sizesq :number

The square of the geometric length of the vector.

Description

This is the sum of the squares of the entries, which is the dot product of this with itself.

Examples

			Vector.create(3, 4).sizesq;   // 25
Details
number

size :number

The geometric length of the vector.

Description

This is the square root of this.sizesq.

Examples

			Vector.create(3, 4).size;   // 5
Details
number

Methods


<static> create( ...entries ) → {Vector}

Create a Vector with the given entries.

Description

This is an alias for Vector.of.

Parameters
Name Type Attributes Description
entries number <repeatable>

The entries of the resulting Vector.

Returns

The vector with the given entries.

Examples

			Vector.create(1).toString(1);    // "[1.0]"
			Vector.create(1, 2).toString(1); // "[1.0 2.0]"
Details

<static> constant( n, c ) → {Vector}

Create a Vector with n entries equal to c.

Parameters
Name Type Description
n integer

The size of the resulting Vector.

c number

The value of the entries.

Returns

The vector [c, c, ..., c].

Examples

			Vector.constant(3, 1).toString(1);  // "[1.0 1.0 1.0]"
Details

<static> zero( n ) → {Vector}

Create a Vector with n entries equal to zero.

Parameters
Name Type Description
n integer

The size of the resulting Vector.

Returns

The zero vector of size n.

Examples

			Vector.zero(3).toString(1);  // "[0.0 0.0 0.0]"
Details

<static> e( i, n [, λ ] ) → {Vector}

Create a unit coordinate vector.

Description

This is the Vector with all entries equal to 0, except the ith equal to 1.

Parameters
Name Type Attributes Default Description
i integer

The nonzero entry.

n integer

The size of the resulting Vector.

λ number <optional>
1

The nonzero entry is set to this.

Returns

The ith unit coordinate vector in R^n.

Examples

			Vector.e(1, 3).toString(1); // "[0.0 1.0 0.0]"
Details

<static> isLinearlyIndependent( vecs [, ε ] ) → {boolean}

Check if an iterable of vectors is linearly independent.

Description

This means that the vector equation c1 v1 + c2 v2 + ... + cn vn = 0 has only the solution c1 = c2 = ... = cn = 0. Equivalently, the matrix with columns v1, v2, ..., vn has full column rank.

Parameters
Name Type Attributes Default Description
vecs Array.<Vector>

The vectors to check.

ε number <optional>
1e-10

Entries smaller than this value are taken to be zero for the purposes of pivoting.

Returns

True if the vectors are linearly independent.

Examples

			Vector.isLinearlyIndependent(
			    [Vector.create(1, 0, 0),
			     Vector.create(0, 1, 0),
			     Vector.create(0, 0, 1)]);  // true
			Vector.isLinearlyIndependent(
			    [Vector.create(1, 0, 0),
			     Vector.create(0, 1, 0),
			     Vector.create(1, 1, 0)]);  // false
Throws

Will throw an error if the vectors do not have the same length.

Details

<static> isLinearlyDependent( vecs [, ε ] ) → {boolean}

Check if an iterable of vectors is linearly dependent.

Description

This is an alias for !Vector.isLinearlyIndependent(vecs).

Parameters
Name Type Attributes Default Description
vecs Array.<Vector>

The vectors to check.

ε number <optional>
1e-10

Entries smaller than this value are taken to be zero for the purposes of pivoting.

Returns

True if the vectors are linearly dependent.

Examples

			Vector.isLinearlyDependent(
			    [Vector.create(1, 0, 0),
			     Vector.create(0, 1, 0),
			     Vector.create(0, 0, 1)]);  // false
			Vector.isLinearlyDependent(
			    [Vector.create(1, 0, 0),
			     Vector.create(0, 1, 0),
			     Vector.create(1, 1, 0)]);  // true
Throws

Will throw an error if the vectors do not have the same length.

Details

<static> linearlyIndependentSubset( vecs [, ε ] ) → {Array.<Vector>}

Return a linearly independent subset.

Description

This returns an Array containing a maximal linearly independent subset of vectors from vecs.

Parameters
Name Type Attributes Default Description
vecs Array.<Vector>

The vectors to use.

ε number <optional>
1e-10

Entries smaller than this value are taken to be zero for the purposes of pivoting.

Returns

A linearly independent subset of vecs.

Examples

			let v1 = Vector.create(1, 0, 0);
			let v2 = Vector.create(0, 1, 0);
			let v3 = Vector.create(1, 1, 0);
			Vector.linearlyIndependentSubset([v1, v2, v3]);  // [v1, v2]
Throws

Will throw an error if the vectors do not have the same length.

Details

<static> linearCombination( coeffs, vecs )

Compute a linear combination of vectors.

Description

The linear combination of the vectors v1, v2, ..., vn with coefficients c1, c2, ..., cn is the vector c1 v1 + c2 v2 + ... + cn vn.

Parameters
Name Type Description
coeffs Array.<number>

A non-empty array of coefficients.

vecs Array.<Vector>

A non-empty array of vectors, of the same length as coeffs.

Returns

The sum of the vectors scaled by the coefficients.

Examples

			let v1 = Vector.create(1, 0, 0);
			let v2 = Vector.create(0, 1, 0);
			let v3 = Vector.create(1, 1, 0);
			Vector.linearCombination([1, 2, 3], [v1, v2, v3]).toString(1);
			   // "[4.0 5.0 0.0]"
Throws

Will throw an error if the vectors do not have the same length, or if coeffs is empty.

Details

equals( other [, ε ] ) → {boolean}

Check if this vector is equal to other.

Description

Two vectors are equal if they have the same number of entries, and all entries are equal.

Parameters
Name Type Attributes Default Description
other Vector

The vector to compare.

ε number <optional>
0

Entries will test as equal if they are within ε of each other. This is provided in order to account for rounding errors.

Returns

True if the vectors are equal.

Examples

			let v = Vector.create(0.01, -0.01, 0);
			let w = Vector.zero(3);
			v.equals(w);              // false
			v.equals(w, 0.05);        // true
			w.equals(Vector.zero(2)); // false
Details

clone() → {Vector}

Create a new Vector with the same entries.

Returns

The new vector.

Details

toString( [ precision ] ) → {string}

Return a string representation of the vector.

Parameters
Name Type Attributes Default Description
precision integer <optional>
4

The number of decimal places to include.

Returns

A string representation of the vector.

Examples

			Vector.create(1, 2, 3).toString(2); // "[1.00 2.00 3.00]"
Details

toLaTeX( [ precision [, opts ] ] ) → {string}

Return a LaTeX representation of the vector.

Parameters
Name Type Attributes Default Description
precision integer <optional>
4

The number of decimal places to include.

opts Object <optional>
{}

Options.

Name Type Attributes Default Description
env string <optional>
"bmatrix"

The matrix environment to use.

row boolean <optional>
false

Print as a row vector instead of a column vector.

Returns

A string representation of the vector.

Examples

			Vector.create(1, 2, 3).toLaTeX(2);
			   // "\begin{bmatrix} 1.00 \\ 2.00 \\ 3.00 \end{bmatrix}"
Details

isZero( [ ε ] ) → {boolean}

Decide if a vector is zero.

Description

This is functionally equivalent to this.equals(Vector.zero(this.length), ε).

Parameters
Name Type Attributes Default Description
ε number <optional>
0

Entries smaller than this in absolute value will be considered zero.

Returns

True if the vector has all zero entries.

Details

normalize() → {Vector}

Scale the vector by the reciprocal of its length.

Description

This modifies the vector in-place to have size 1.

Returns

this

Examples

			let v = Vector.create(3, 4);
			v.normalize();
			v.toString(2); // "[0.60 0.80]"
Throws

Will throw an error if this is the zero vector.

Details

set( ...entries ) → {Vector}

Set multiple components of a Vector.

Description

This modifies the vector by setting the components to the passed values.

Parameters
Name Type Attributes Description
entries number <repeatable>

The new entries of the vector.

Returns

this

Examples

			let v = Vector.zero(2);
			v.set(3, 4);
			v.toString(1);  // "[3.0, 4.0]"
Throws

Will throw an error if the number of passed entries is incorrect.

Details

add( other [, factor [, start ] ] ) → {Vector}

Add a Vector in-place.

Description

This modifies the vector in-place by adding the entries of other.

Parameters
Name Type Attributes Default Description
other Vector

The vector to add.

factor number <optional>
1

Add factor times other instead of just adding other.

start integer <optional>
0

Only add the entries start...this.length. Provided for optimizations when the entries of other before start are known to be zero.

Returns

this

Examples

			let v = Vector.create(1, 2), w = Vector.create(3, 4);
			v.add(w);
			v.toString(1);  // "[4.0 6.0]"
Throws

Will throw an error if the vectors have different lengths.

Details

sub( other [, start ] ) → {Vector}

Subtract a Vector in-place.

Description

This modifies the vector in-place by subtracting the entries of other.

Parameters
Name Type Attributes Default Description
other Vector

The vector to subtract.

start integer <optional>
0

Only subtract the entries start...this.length. Provided for optimizations when the entries of other before start are known to be zero.

Returns

this

Examples

			let v = Vector.create(1, 2), w = Vector.create(3, 4);
			v.sub(w);
			v.toString(1);  // "[-2.0 -2.0]"
Throws

Will throw an error if the vectors have different lengths.

Details

scale( c [, start ] ) → {Vector}

Multiply a Vector by a scalar in-place.

Description

This modifies the vector in-place by multiplying all entries by c.

Parameters
Name Type Attributes Default Description
c number

The scaling factor.

start integer <optional>
0

Only scale the entries start...this.length. Provided for optimizations when the entries before start are known to be zero.

Returns

this

Examples

			let v = Vector.create(1, 2);
			v.scale(2);
			v.toString(1);  // "[2.0 4.0]"
Details

dot( other ) → {number}

Compute the dot product with another vector.

Description

This is the sum of the pairwise products of the entries of this and other.

Parameters
Name Type Description
other Vector

The vector to dot.

Returns

The dot product.

Examples

			let v = Vector.create(1, 2), w = Vector.create(3, 4);
			v.dot(w);  // 1*3 + 2*4
Throws

Will throw an error if the vectors have different lengths.

Details