Vector operations.
These operations are very lax about the dimensions of vectors - they stick zeros in when pairs of vectors don't line up exactly.
Methods
(static) abs(a) → {number}
Length of a vector.
Parameters:
Name | Type | Description |
---|---|---|
a |
vector |
Returns:
Type | Description |
---|---|
number |
(static) abs_squared(a) → {number}
Length of a vector, squared.
Parameters:
Name | Type | Description |
---|---|---|
a |
vector |
Returns:
Type | Description |
---|---|
number |
(static) add(a, b) → {vector}
Add two vectors.
Parameters:
Name | Type | Description |
---|---|---|
a |
vector | |
b |
vector |
Returns:
Type | Description |
---|---|
vector |
(static) angle(a, b) → {number}
Angle between vectors a and b, in radians, or 0 if either vector has length 0.
Parameters:
Name | Type | Description |
---|---|---|
a |
vector | |
b |
vector |
Returns:
Type | Description |
---|---|
number |
(static) cross(a, b) → {vector}
Vector cross product - each argument can be a vector, or a matrix with one row, which is converted to a vector.
Parameters:
Name | Type | Description |
---|---|---|
a |
vector | matrix | |
b |
vector | matrix |
Throws:
-
-
"vectormaths.cross.matrix too big" if either of
a
orb
is bigger than1xN
orNx1
. - Type
- Numbas.Error
-
-
-
"vectormath.cross.not 3d" if either of the vectors is not 3D.
- Type
- Numbas.Error
-
Returns:
Type | Description |
---|---|
vector |
(static) div(v, k) → {vector}
Divide by a scalar.
Parameters:
Name | Type | Description |
---|---|---|
v |
vector | |
k |
number |
Returns:
Type | Description |
---|---|
vector |
(static) dot(a, b) → {number}
Vector dot product - each argument can be a vector, or a matrix with one row or one column, which is converted to a vector.
Parameters:
Name | Type | Description |
---|---|---|
a |
vector | matrix | |
b |
vector | matrix |
Throws:
-
"vectormaths.dot.matrix too big" if either of
a
orb
is bigger than1xN
orNx1
. - Type
- Numbas.Error
Returns:
Type | Description |
---|---|
number |
(static) eq(a, b) → {boolean}
Are two vectors equal? True if each pair of corresponding components is equal.
Parameters:
Name | Type | Description |
---|---|---|
a |
vector | |
b |
vector |
Returns:
Type | Description |
---|---|
boolean |
(static) is_zero(v) → {boolean}
Is every component of this vector zero?
Parameters:
Name | Type | Description |
---|---|---|
v |
vector |
Returns:
Type | Description |
---|---|
boolean |
(static) map(v, fn) → {vector}
Apply given function to each element.
Parameters:
Name | Type | Description |
---|---|---|
v |
vector | |
fn |
function |
Returns:
Type | Description |
---|---|
vector |
(static) matrixmul(m, v) → {vector}
Multiply a vector on the left by a matrix.
Parameters:
Name | Type | Description |
---|---|---|
m |
matrix | |
v |
vector |
Returns:
Type | Description |
---|---|
vector |
(static) mul(k, v) → {vector}
Multiply by a scalar.
Parameters:
Name | Type | Description |
---|---|---|
k |
number | |
v |
vector |
Returns:
Type | Description |
---|---|
vector |
(static) negate(v) → {vector}
Negate a vector - negate each of its components.
Parameters:
Name | Type | Description |
---|---|---|
v |
vector |
Returns:
Type | Description |
---|---|
vector |
(static) neq(a, b) → {boolean}
Are two vectors unequal?
Parameters:
Name | Type | Description |
---|---|---|
a |
vector | |
b |
vector |
Returns:
Type | Description |
---|---|
boolean |
- Source:
- See:
(static) precround(v, dp) → {vector}
Round each element to given number of decimal places.
Parameters:
Name | Type | Description |
---|---|---|
v |
vector | |
dp |
number | Number of decimal places. |
Returns:
Type | Description |
---|---|
vector |
(static) siground(v, sf) → {vector}
Round each element to given number of significant figures.
Parameters:
Name | Type | Description |
---|---|---|
v |
vector | |
sf |
number | Number of decimal places. |
Returns:
Type | Description |
---|---|
vector |
(static) sub(a, b) → {vector}
Subtract one vector from another.
Parameters:
Name | Type | Description |
---|---|---|
a |
vector | |
b |
vector |
Returns:
Type | Description |
---|---|
vector |
(static) toMatrix(v) → {matrix}
Convert a vector to a 1-column matrix.
Parameters:
Name | Type | Description |
---|---|---|
v |
vector |
Returns:
Type | Description |
---|---|
matrix |
(static) transpose(v) → {matrix}
Transpose of a vector.
Parameters:
Name | Type | Description |
---|---|---|
v |
vector |
Returns:
Type | Description |
---|---|
matrix |