Vectorious is a generalized n-dimensional matrix and vector library written in JavaScript, which can be used both in node.js and the browser.
Install with npm
$ npm install vectorious
Test with
$ npm test
Run benchmarks with
$ npm run benchmark
Download a release and use it like this:
<script src="vectorious-2.x.x.js"></script>
<script>
// e.g. var vector = new vectorious.Vector()
</script>
- Solve — Solves matrix equations of the form Ax = B.
- Plot — Generates a two-dimensional SVG plot from two input vectors.
The constructors of both Matrix
and Vector
are designed to be flexible, so they can be initialized using several different arguments.
Since 2.1.0 Vector
implements JavaScript typed arrays for increased performance. The default Vector
type is Float64Array
, but this can be specified upon creation.
var vectorious = require('vectorious'),
Vector = vectorious.Vector,
Matrix = vectorious.Matrix;
var vector,
matrix;
// Create an empty vector of default type Float64Array
vector = new Vector();
// Create an empty vector of type Uint8Array
vector = new Vector(Uint8Array);
matrix = new Matrix();
// { rows: [] }
vector = Vector.zeros(5);
// { values: [0, 0, 0, 0, 0], length: 5 }
vector = new Vector(1, 2, 3, 4, 5);
// { values: [1, 2, 3, 4, 5], length: 5 }
matrix = new Matrix(vector);
// { rows: [ { values: [1, 2, 3, 4, 5], length: 5 } ] }
matrix = Matrix.zeros(2, 2);
/* {
rows: [
{ values: [0, 0], length: 2 },
{ values: [0, 0], length: 2 }
]
} */
var input = [
[1, 2],
[3, 4]
];
matrix = new Matrix(input);
/* {
rows: [
{ values: [1, 2], length: 2 },
{ values: [3, 4], length: 2 }
]
} */
Now that you've got a hang of the basics, let me show you a useful application example.
var time = Vector.range(0, Math.PI / 12, Math.PI);
/* {
values:
[ 0,
0.2617993877991494,
0.5235987755982988,
0.7853981633974483,
1.0471975511965976,
1.308996938995747,
1.5707963267948963,
1.8325957145940457,
2.0943951023931953,
2.356194490192345,
2.6179938779914944,
2.879793265790644 ],
length: 12 } */
var sine = time.map(Math.sin);
/* {
values:
[ 0,
0.25881904510252074,
0.49999999999999994,
0.7071067811865475,
0.8660254037844386,
0.9659258262890682,
1,
0.9659258262890684,
0.8660254037844387,
0.7071067811865476,
0.49999999999999994,
0.2588190451025206 ],
length: 12 } */
For more advanced uses, check out the extensions solve and plot.
The following vector operations and methods are implemented in vector.js
.
add
subtract
scale
normalize
dot
magnitude
angle
project
zeros
ones
range
equals
get
min
max
set
combine
push
map
each
toString
toArray
// (Vector, Vector) => (Vector)
Vector.add = function(a, b)
Vector.prototype.add = function(vector)
Add two vectors together.
// (Vector, Vector) => (Vector)
Vector.subtract = function(a, b)
Vector.prototype.subtract = function(vector)
Subtract two vectors.
// (Vector, Number) => (Vector)
Vector.prototype.scale = function(scalar)
Multiply a vector by a scalar.
// (Vector) => (Vector)
Vector.prototype.normalize = function()
Normalize a vector.
// (Vector, Vector) => (Number)
Vector.dot = function(a, b)
Vector.prototype.dot = function(vector)
Get dot product of two vectors.
// (Vector) => (Number)
Vector.prototype.magnitude = function()
Get magnitude (norm) of vector.
// (Vector, Vector) => (Angle)
Vector.angle = function(a, b)
Vector.prototype.angle = function(vector)
Get the angle (in radians) between two vectors.
// (Vector, Vector) => (Vector)
Vector.project = function(a, b)
Vector.prototype.project = function(vector)
Project a vector onto another vector.
// (Number) => (Vector)
Vector.zeros = function(count, [type])
Create a vector of count
zeros. type
is optional and specifies the type of TypedArray
used in computations.
// (Number) => (Vector)
Vector.ones = function(count, [type])
Create a vector of count
ones. type
is optional and specifies the type of TypedArray
used in computations.
// (Number, [Number], Number) => (Vector)
Vector.range = function(start, [step], end, [type])
Create a vector containing the range from start
to end
in steps of step
(optional). type
is optional and specifies the type of TypedArray
used in computations.
// (Vector, Vector) => (Boolean)
Vector.equals = function(a, b)
Vector.prototype.equals = function(vector)
Compare two vectors.
// (Vector, Number) => (Number)
Vector.prototype.get = function(index)
Get value of an element at index
.
// (Vector, Number) => (Number)
Vector.prototype.min = function()
Get the minimum value of a vector.
// (Vector, Number) => (Number)
Vector.prototype.max = function()
Get the maximum value of a vector.
// (Vector, Number, Number) => (Vector)
Vector.prototype.set = function(index, value)
Set value of an element at index
.
// (Vector, Vector) => (Vector)
Vector.combine = function(a, b)
Vector.prototype.combine = function(vector)
Combines two vectors.
// (Vector, Number) => (Vector)
Vector.prototype.push = function(value)
Pushes value
into the vector.
// (Vector, Function) => (Vector)
Vector.prototype.map = function(callback)
Maps a function callback
to all elements of the vector.
// (Vector, Function) => (Vector)
Vector.prototype.each = function(callback)
Calls callback(value, index)
for each element in the vector.
// (Vector) => (String)
Vector.prototype.toString = function()
Convert vector to string.
// (Vector) => (Array)
Vector.prototype.toArray = function()
Convert vector to array.
The following matrix operations and methods are implemented in matrix.js
.
add
subtract
scale
multiply
transpose
inverse
gauss
diag
augment
trace
identity
zeros
ones
equals
get
set
swap
map
each
toString
toArray
// (Matrix, Matrix) => (Matrix)
Matrix.add = function(a, b)
Matrix.prototype.add = function(matrix)
Add two matrices together.
// (Matrix, Matrix) => (Matrix)
Matrix.subtract = function(a, b)
Matrix.prototype.subtract = function(matrix)
Subtract two matrices.
// (Matrix, Number) => (Matrix)
Matrix.prototype.scale = function(scalar)
Multiply all elements in matrix with a scalar.
// (Matrix, Matrix) => (Matrix)
Matrix.multiply = function(a, b)
Matrix.prototype.multiply = function(matrix)
Multiply two matrices together.
// (Matrix) => (Matrix)
Matrix.prototype.transpose = function()
Transpose a matrix.
// (Matrix, Boolean) => (Matrix)
Matrix.prototype.gauss = function()
Convert a matrix to reduced row echelon (RREF) form using Gauss-Jordan eliminiation.
// (Matrix) => (Matrix)
Matrix.prototype.inverse = function()
Get the inverse of any invertible square matrix using Gauss-Jordan elimination.
// (Matrix) => (Vector)
Matrix.prototype.diag = function()
Get matrix diagonal as a Vector
.
// (Matrix, Matrix) => (Matrix)
Matrix.augment = function(a, b)
Matrix.prototype.augment = function(matrix)
Create an augmented matrix.
// (Matrix) => (Number)
Matrix.prototype.trace = function()
Get matrix trace (the sum of the diagonal).
// (Number) => (Matrix)
Matrix.identity = function(size, [type])
Create an identity matrix. type
is optional and specifies the type of TypedArray
used in computations.
// (Number, Number) => (Matrix)
Matrix.zeros = function(i, j, [type])
Create an i x j
matrix of zeros. type
is optional and specifies the type of TypedArray
used in computations.
// (Number, Number) => (Matrix)
Matrix.ones = function(i, j, [type])
Create an i x j
matrix of ones. type
is optional and specifies the type of TypedArray
used in computations.
// (Matrix, Matrix) => (Boolean)
Matrix.equals = function(a, b)
Matrix.prototype.equals = function(matrix)
Compare two matrices.
// (Matrix, Number, Number) => (Number)
Matrix.prototype.get = function(i, j)
Get element at row i
, column j
.
// (Matrix, Number, Number, Number) => (Matrix)
Matrix.prototype.set = function(i, j, value)
Set the value of an element at row i
, column j
.
// (Matrix, Number, Number) => (Matrix)
Matrix.prototype.swap = function(i, j)
Swaps the position of rows i
and j
.
// (Matrix, Function) => (Matrix)
Matrix.prototype.map = function(callback)
Maps a function callback
to all elements of the matrix.
// (Matrix, Function) => (Matrix)
Matrix.prototype.each = function(callback)
Calls callback(row, index)
for each row in the matrix.
// (Matrix) => (String)
Matrix.prototype.toString = function()
Convert matrix to string.
// (Matrix) => (Array)
Matrix.prototype.toArray = function()
Convert matrix to array.