This library is not intended to be taken seriously, as it is made just for fun and learning purposes. One may find it useful but any use in production environments, research studies, etc. is discouraged.
This javascript matrix library comes from the need to perform various numeric matrix operations during the course of some assignments at college. The lack of one of those fancy graphic calculators, my reluctance in spending money buying one and the growing interest in javascript took me to start writing most of the code found here. After six months or so I have managed to put this sort of library and learned some more linear algebra and algorithms during the process.
In case you find this useful, or end up ignoring my first advice and use it somewhere, I will be delighted to know it so feel free to mail me at the email address found in the source code. Moreover, if you happen to find any mistakes (I am pretty sure there are a ton of them) in the source code or in this document, you can issue them at its github repository at https://github.com/dhuertas/mat.js.
Happy coding!
(float) - Sets the error resolution for the iterative algorithms (e.g. eigenValues, eigenVectors).
0.000001
var A = new MAT(2, 2, [1, 2, 3, 4]); A.error = 0.000001;or
var A = new MAT({ rows: 2, columns: 2, values: [1, 2, 3, 4], error: 0.000001});
(integer) - Sets the number of maximum rounds for the iterative algorithms (e.g. eigenValues, eigenVectors).
1000
var A = new MAT(2, 2, [1, 2, 3, 4]); A.maxrounds = 1000;or
var A = new MAT({ rows: 2, columns: 2, values: [1, 2, 3, 4], maxrounds: 1000});
When set to true
the result values will be stored in the same matrix instance, overwriting the previous ones (not to be confused with in-place algorithms). The aim is to reduce the amount of memory and time used to perform iterative operations.
false
var A = new MAT(2,2,[1, 2, 3, 4]); A.overwrite = true;or
var A = new MAT({ rows: 2, columns: 2, values: [1, 2, 3, 4], overwrite: true});
Creates a new matrix using array notation.
.fromArray(array);
This example creates a 2x3 matrix object:
var A = new MAT({ maxrounds: 1000, overwrite: true }).fromArray([ [1, 2, 3], [4, 5, 6] ]);
Returns an array containing all the matrix values, ordered from left to right and from top to bottom.
.toArray()
None.
var values = A.toArray();
Returns a string containing all the matrix values, using the toString method of the javascript Array
object.
.toString()
None.
document.write(A.toString());
Returns the matrix converted into a LaTeX formatted string.
.toLatex([closure])
The following example uses the MathJax library, which enables the browser to format text using LaTeX:
var A = new MAT(2, 2, [1, 2, -1, 2]); document.getElementById("output").innerHTML += "The determinant $$" + A.toLatex('v') + "$$ equals " + A.det() + "."; // Typeset the element: MathJax.Hub.Queue(["Typeset",MathJax.Hub,"output"]);
If mathjax loeaded correctly from the CDN, should look something like this:
Returns a column (or column vector) from the matrix.
.getColumn(column);
0
, to columns-1
).The following example will print the column vector [0, 0, 1]
:
var A = new MAT().fromArray([ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]); var column = A.getColumn(2); document.write(column.toString());
Returns the number of columns.
.getColumns()
None.
var A = new MAT().fromArray([ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]); document.write(A.getColumns());
Will print the number 3.
Returns the number of elements (or length) of the matrix array.
.getLength()
None.
var A = new MAT().fromArray([ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]); document.write(A.getLength());
Will print the number 9.
Returns a row (row vector) from the matrix.
.getRow(row)
0
, to rows-1
).The following example will print the row vector [0, 1, 0]
.
var A = new MAT().fromArray([ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]); var row = A.getRow(1); document.write(row.toString());
Returns the number of rows.
.getRows()
None.
var A = new MAT().fromArray([ [1, 0, 0], [0, 1, 0], [0, 0, 1] ]); document.write(A.getRows());
Will print the number 3.
Returns the shape (or the dimensions) of the matrix.
.getShape()
None.
[rows, columns]
.For a 3x4 matrix the following code will print the array [3, 4]
:
var A = new MAT(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); document.write(A.getShape());
Returns the value at the specified row
and column
values.
.getValue(row, column)
0
to rows-1
).0
to columns-1
).[real,imag]
).var A = new MAT(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); document.write(A.getValue(2, 3));
The previous code will print the number 12
.
Returns a copy of the matrix values, ordered from left to right and from top to bottom. This is the same as the toString
method.
.getValues()
None
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]); document.write(A.getValues());
The previous code will print the array [1, 2, 3, 4, 5, 6, 7, 8, 9]
.
Sets the number of rows of the matrix. This method should be used in conjuction with the setColumns method in order to avoid undesired behaviours.
.setRows(rows)
Let's say we want to transform a 3x2 matrix to a 1x6 matrix (or row vector):
var A = new MAT(3, 2, [1, 2, 3, 4, 5, 6]); // do some stuff here A.setRows(1).setColumns(6); document.write(A.getShape());
The previous code will print the array [1, 6]
.
Sets the number of columns of the matrix. This method should be used in conjuction with the setRows method in order to avoid undesired behaviours.
.setColumns(columns)
Let's say we want to transform a 3x2 matrix to a 6x1 matrix (or column vector):
var A = new MAT(3, 2,[1, 2, 3, 4, 5, 6]); // do some stuff here A.setColumns(1).setRows(6); document.write(A.getShape());
The previous code will print the array [6, 1]
.
Sets the value at the specified position of the matrix.
.setValue(row, column, value)
0
to rows-1
).0
to columns-1
).The following example sets the center value of a 3x3 matrix to 0.
matrix.setValue(1, 1, 0).
Tells whether the matrix is a square matrix or not.
.isSquare()
None.
true
if the matrix is square, false
otherwise.if (matrix.isSquare()) { // execute this code when the matrix is square } else { // :( }
Tells whether the matrix is a column vector or not.
.isColumnVector()
None.
true
when the matrix is a column vector, false
otherwise.if (matrix.isColumnVector()) { // matrix is a column vector } else { // other code }
Tells whether the matrix is a row vector or not.
.isRowVector()
None.
true
when the matrix is a row vector, false
otherwise.if (matrix.isColumnVector()) { // matrix is a row vector } else { // other code }
Tells whether the matrix is a vector (1 row or 1 column) or not.
.isVector()
None.
true
when the matrix is a vector, false
otherwise.if (matrix.isVector()) { // the matrix is a vector } else { // other code }
Compares the shape of the matrix against the one in the argument.
.isSameSize(matrix)
true
when both matrices have the same shape (same number of rows and columns).The following example compares two matrices that have the same number of rows and columns and outputs the string same size:
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]); var B = new MAT(3, 3, [9, 8, 7, 6, 5, 4, 3, 2, 1]); if (A.isSameSize(B)) { document.write('same size'); } else { documenet.write('different size'); }
Performs the addition of two matrices (A + B).
.add(matrix)
Note: when the overwrite
attribute is set to true
the result values will overwrite the ones of the first matrix, returning a reference to itself instead of a new matrix.
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]); var B = new MAT(3, 3, [9, 8, 7, 6, 5, 4, 3, 2, 1]); var C = A.add(B); // [1, 2, 3] [9, 8, 7] // C = [4, 5, 6] + [6, 5, 4] // [7, 8, 9] [3, 2, 1] document.write(C.toString()); // outputs "10, 10, 10, 10, 10, 10, 10, 10, 10" document.write(A.toString()); // outputs "1, 2, 3, 4, 5, 6, 7, 8, 9" // Now with the overwrite attribute set to true A.overwrite = true; A.add(B); document.write(A.toString()); // outputs "10, 10, 10, 10, 10, 10, 10, 10, 10"
Performs the subtraction of two matrices from left to right (A - B).
.subtract(matrix)
Note: when the overwrite
attribute is set to true
the result values will overwrite the ones of the first matrix, returning a reference to itself instead of a new matrix.
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]); var B = new MAT(3, 3, [9, 8, 7, 6, 5, 4, 3, 2, 1]); var C = A.subtract(B); // [1, 2, 3] [9, 8, 7] // C = [4, 5, 6] - [6, 5, 4] // [7, 8, 9] [3, 2, 1] document.write(C.toString()); // outputs "-8, -6, -4, -2, 0, 2, 4, 6, 8" document.write(A.toString()); // outputs "1, 2, 3, 4, 5, 6, 7, 8, 9" // Now with the overwrite attribute set to true A.overwrite = true; A.subtract(B); document.write(A.toString()); // outputs "-8, -6, -4, -2, 0, 2, 4, 6, 8"
Performs the product of two matrices (from left to right AB). The number of columns of the first matrix must match the number of rows of the second one.
This is the naive product algorithm. The strassenProduct is asymptotically faster for large matrices.
.product(matrix)
Note: when the overwrite
attribute is set to true
the result values will overwrite the ones of the first matrix, returning a reference to itself instead of a new matrix.
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]); var B = new MAT(3, 3, [1, 0, 1, 0, 1, 0, 0, 1, 1]); var C = A.product(B); // [1, 2, 3] [1, 0, 1] // C = [4, 5, 6] * [0, 1, 0] // [7, 8, 9] [0, 1, 1] document.write(C.toString()); // outputs "1, 5, 4, 4, 11, 10, 7, 17, 16" // Now with the overwrite attribute set to true A.overwrite = true; A.product(B); document.write(A.toString()); // outputs "1, 5, 4, 4, 11, 10, 7, 17, 16"
Performs the product of two matrices (from left to right AB) using the Strassen's algorithm. This algorithm is asymptotically faster than the naive method for large matrices.
.strassenProduct(matrix)
Note: when the overwrite
attribute is set to true
the result values will overwrite the ones of the first matrix, returning a reference to itself instead of a new matrix.
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]); var B = new MAT(3, 3, [9, 8, 7, 6, 5, 4, 3, 2, 1]); var C = A.strassenProduct(B); document.write(C.toString()); // outputs "30,24,18,84,69,54,138,114,90" // [ 30, 24, 18] // [ 84, 69, 54] // [138, 114, 90]
The next table shows some average running times for different matrix sizes:
Size | Naive | Strassen |
---|---|---|
4x4 | - ms | 1 ms |
8x8 | - ms | 1 ms |
16x16 | 2 ms | 9.5 ms |
32x32 | 9.5 ms | 14.5 ms |
64x64 | 75 ms | 93.5 ms |
128x128 | 587 ms | 661 ms |
256x256 | 4835.5 ms | 4670 ms |
512x512 | 39319 ms | 34827.5 ms |
1024x1024 | 426434.5 ms | 243014 ms |
It seems that at a size between 128 and 256 Strassen's algorithm starts to perform better than the naive algorithm, although the memory required for the Strassen's algorithm should be taken into account.
Performs the Hadamard product of two matrices (from left to right AB).
.hadamardProduct(matrix)
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]); var B = new MAT(3, 3, [1, 0, 1, 0, 1, 0, 0, 1, 1]); var C = A.hadamardProduct(B); // [1, 2, 3] [1, 0, 1] // C = [4, 5, 6] o [0, 1, 0] // [7, 8, 9] [0, 1, 1] document.write(C.toString()); // outputs "1,0,3,0,5,0,0,8,9" // [1, 0, 3] // [0, 5, 0] // [0, 8, 9]
Performs the scalar product between the matrix and a scalar.
.scalarProduct(value)
Note: when the overwrite
attribute is set to true
the result values will overwrite the ones of the first matrix, returning a reference to itself instead of a new matrix.
var A = new MAT(2, 2, [1, 2, 3, 4]); document.write(A.scalarProduct(2).toString()); // outputs "1,4,6,8"
Returns the transpose matrix.
.transpose()
None.
Note: when the overwrite
attribute is set to true
the result values will overwrite the previous ones, returning a reference to itself instead of a new matrix.
var A = new MAT(3, 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); // [1, 2, 3, 4] // A = [5, 6, 7, 8] // [9,10,11,12] var C = A.transpose(); // [1, 5, 9] // C = [2, 6,10] // [3, 7,11] // [4, 8,12] document.write(C.toString()); // outputs "1,5,9,2,6,10,3,7,11,4,8,12"
Returns the hermitian matrix (conjugate transpose).
.hermitian()
None.
Note: when the overwrite
attribute is set to true
the result values will overwrite the previous ones, returning a reference to itself instead of a new matrix.
var A = new MAT(2, 2, [3, [2,3], [2,-1], 1]); document.write(A.hermitian().toString()); // outputs "3,[2,1],[2,-3],1"
Returns the trace of the matrix. That is, the sum of all the elements in the main diagonal.
.trace()
None.
var A = new MAT(2, 2, [3, [2,3], [2,-1], 1]); document.write(A.trace());
The previous code will print 4.
Returns the trace of the matrix upper diagonals.
.upperTrace(diagonal)
0
for the main diagonal to columns-1
).var A = new MAT(4, 4, [ 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12, 13,14,15,16 ]); document.write(A.upperTrace(1)); // outputs 21 (2 + 7 + 12) document.write(A.upperTrace(2)); // outputs 11 (3 + 8)
Returns the trace of the matrix lower diagonals.
.lowerTrace(diagonal)
0
for the main diagonal to rows-1
).var A = new MAT({ rows: 4, columns: 4, values: [ 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12, 13,14,15,16] }); document.write(A.lowerTrace(1)); // outputs 30 (= 5 + 10 + 15) document.write(A.lowerTrace(2)); // outputs 23 (= 9 + 14)
Returns a smaller matrix obtained by removing the row and the column at a specified position.
.minor(row, column)
Note: when the overwrite
attribute is set to true
the result values will overwrite the previous ones, returning a reference to itself instead of a new matrix.
var A = new MAT(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9]); document.write(A.minor(2, 2).toString()); // outputs "1,2,4,5"
Returns the determinant of the matrix.
The algorithm uses the Laplace's formula by default, which is useful for small matrices. In order to speed things up, the user is able to select one of the following decomposition methods: lu, qr.
.determinant([method])
Note: when using the QR decomposition method, the result is the absolute value of the matrix determinant.
Following the example extracted from the wikipedia article:
var A = new MAT(3, 3, [-2, 2, 3, -1, 1, 3, 2, 0, -1]); document.write(A.determinant()); // outputs 6 document.write(A.determinant('lu')); // throws "division by 0" document.write(A.determinant('qr')); // outputs 5.999999999999997
Note that the previous matrix cannot be LU decomposed. LUP decomposition (LU with partial or full Pivoting), which overcomes this limitation, is not implemented yet.
This other example, extracted from another wikipedia article, demonstrates the diferent values returned when using each method:
var A = new MAT(3, 3, [12, -51, 4, 6, 167, -68, -4, 24, -41]); document.write(A.determinant()); // outputs -85750 document.write(A.determinant('lu')); // outputs -85749.99999999999 document.write(A.determinant('qr')); // outputs 85750.00000000009
This is an alias for the determinant method.
.det([method])
View the previous determinant examples.
Performs the Gaussian elimination algorithm. This is the method teached at school to solve systems of linear equations.
.gaussElimination()
None.
Note: when the overwrite
attribute is set to true
the result values will overwrite the previous ones, returning a reference to itself instead of a new matrix.
The following example is extracted from the wikipedia article:
var A = new MAT(3, 3, [2, 1, -1, -3, -1, 2, -2, 1, 2]); // [ 2, 1, -1] // A = [-3, -1, 2] // [-2, 1, 2] var Extended = new MAT(3, 4, [2, 1, -1, 8, -3, -1, 2, -11, -2, 1, 2, -3]); // [ 2, 1, -1| 8] // Extended = [-3, -1, 2|-11] // [-2, 1, 2| -3] document.write(A.gaussElimination().toString()); // outputs "2,1,-1,0,0.5,0.5,0,0,-1" // [2, 1, -1] // [0, 0.5, 0.5] // [0, 0, -1] document.write(Extended.gaussElimination().toString()); // outputs "2,1,-1,8,0,0.5,0.5,1,0,0,-1,1" // [2, 1, -1| 8] // [0, 0.5, 0.5| 1] // [0, 0, -1| 1]
Performs the Gauss elimination method and then solves the system of linear equations using backward substitution.
.solve(array)
var A = new MAT({ rows: 3, columns: 3, values: [2, 1, -1, -3, -1, 2, -2, 1, 2] }); var result = A.solve([8, -11, -3]); document.write(result.toString()); // outputs "2,3,-1"
Performs the Gram-Schmidt process (orthonormalize a set of vectors).
.gramSchmidt()
None.
Note: when the overwrite
attribute is set to true
the result values will overwrite the previous ones, returning a reference to itself instead of a new matrix.
var A = new MAT({ rows: 3, columns: 3, values: [2, 1, -1, -3, -1, 2, -2, 1, 2] }); var result = A.gramSchmidt(); document.write(result.toString()); //outputs: // [ 0.48507125007266594, 0.41166465371081584, 0.7715167498104589] // [ -0.7276068751089989, -0.2993924754260479, 0.6172133998483683] // [-0.48507125007266594, 0.8607533668498878, -0.15430334996209222] // We can check that they are normalized ... document.write(result.getColumn(0).norm()); // outputs "1" document.write(result.getColumn(1).norm()); // outputs "1" document.write(result.getColumn(2).norm()); // outputs "1" // ... and orthogonal result.getColumn(0).transpose().product(result.getColumn(1)); // outputs "0" result.getColumn(0).transpose().product(result.getColumn(2)); // outputs "-6.522560269672795e-16" result.getColumn(1).transpose().product(result.getColumn(2)); // outputs "-7.494005416219807e-16"
Note that since javascript performs floating point operations we may not obtain an exact number, but a really close one.
Performs the QR decomposition, that is, obtains an orthogonal matrix Q and an upper triangular matrix R (A = QR).
.qrDecomposition()
None.
var A = new MAT({ rows: 3, columns: 3, values: [2, 1, -1, -3, -1, 2, -2, 1, 2] }); var QR = A.qrDecomposition(); document.write(QR[0].toString()); // [ 0.48507125007266594, 0.41166465371081584, 0.7715167498104589] // [ -0.7276068751089989, -0.2993924754260479, 0.6172133998483683] // [-0.48507125007266594, 0.8607533668498878, -0.15430334996209222] document.write(QR[1].toString()); // [ 4.123105625617661, 0.727606875108999, -2.9104275004359956] // [ -2.220446049250313e-16, 1.5718104959867514, 0.7110571291368639] // [-2.7755575615628914e-15, -1.609823385706477e-15, 0.15430334996209327]
This is an alias for the qrDecomposition method.
.qr()
None.
View the previous qrDecomposition example.
Performs the LU decomposition, that is, obtains a lower triangular matrix L and an upper triangular matrix U (A = LU)
.luDecomposition()
None.
var A = new MAT({ rows: 3, columns: 3, values: [2, 1, -1, -3, -1, 2, -2, 1, 2] }); var LU = A.luDecomposition(); document.write(LU[0].toString()); // outputs "1,0,0,-1.5,1,0,-1,4,1" // [ 1, 0, 0] // [-1.5, 1, 0] // [ -1, 4, 1] document.write(LU[1].toString()); // outputs "2,1,-1,0,0.5,0.5,0,0,-1" // [2, 1, -1] // [0, 0.5, 0.5] // [0, 0, -1] // Then we can perform the product to obtain the original matrix document.write(LU[0].product(LU[1]).toString()); // outputs "2,1,-1,-3,-1,2,-2,1,2"
This is an alias for the luDecomposition method.
.lu()
None.
View the previous luDecomposition example.
Obtains the eigenvalues of a square matrix using the QR algorithm. This method uses the attributes maxrounds
and error
to make sure the algorithm finishes in case the process does not converge.
.eigenValues()
None.
null
otherwise.var A = new MAT(2, 2, [2, -4, -1, -1]); var eigenvalues = A.eigenValues(); document.write(eigenvalues.toString()); // outputs "2.9999995960226222,-1.9999995960225925"
Note: the previous example takes 32 rounds to reach a lower trace with a lower value than 0.000001.
Obtains the eigenvectors of a square matrix using the QR algorithm. This method uses the attributes maxrounds
and error
to make sure the algorithm finishes in case the process does not converge.
Note: this method is not fully tested yet! Use at your own risk ;)
.eigenVectors()
None.
null
otherwise.var A = new MAT(2, 2, [2, -4, -1, -1]); var eigenvectors = A.eigenVectors(); document.write(eigenvectors.toString()); // [0.970142532804957, -0.2425354943978082] // [0.24253549439781444, 0.970142532804954]
Returns the identity matrix I
, a square matrix with ones in the main diagonal and zeros elsewhere.
.identity(integer)
var A = new MAT(2, 2, [2, -4, -1, -1]); var I = new MAT().identity(2); document.write(A.product(I).toString()); // outputs "2,-4,-1,-1"
Returns a squared, diagonal matrix with different values in the main diagonal and zeros elsewhere.
.diagonal(array)
var D = new MAT().diagonal([1,2,3,4]); document.write(D.toString()); // [1,0,0,0] // [0,2,0,0] // [0,0,3,0] // [0,0,0,4]
Returns the p-norm over the set of values of the matrix. To obtain the p-norm of a column vector from a matrix the getColumn method should precede, or getRow for a row vector.
.pNorm(float)
var v = new MAT(3, 1, [1, 1, 1]); document.write(v.pNorm(2)); // outputs "1.7320508075688772" document.write(v.pNorm(1)); // outputs "3"
Note that for p=1
we get the Taxicab norm (or Manhattan norm) and for p=2
we get the Euclidean norm.
var A = new MAT(2, 2, [2, 1, 1, 2]); document.write(A.getColumn(0).pNorm(2)); // outputs 2.23606797749979
Returns the Euclidean norm over the set of values of the matrix. To obtain the Euclidean norm of a column vector from a matrix the getColumn method should precede, or getRow method for a row vector.
Note that this is the same as using the pNorm with 2 as argument.
.norm()
None.
var v = new MAT(3, 1, [1, 1, 1]); document.write(v.norm()); // outputs "1.7320508075688772" var A = new MAT(2, 2, [2, 1, 1, 2]); document.write(A.getColumn(0).norm()); // outputs 2.23606797749979
Turns a column or row vector to a Toeplitz matrix (each descending diagonal from left to right is constant).
.toToeplitz(integer)
var A = new MAT(3, 1, [1, 2, 3]); document.write(A.toToeplitz(5).toString()); // [1,0,0,0,0] // [2,1,0,0,0] // [3,2,1,0,0] // [0,3,2,1,0] // [0,0,3,2,1] // [0,0,0,3,2] // [0,0,0,0,3] document.write(A.toToeplitz(2).toString()); // [1,0] // [2,1] // [3,2] // [0,3]
Returns the inverse matrix of a non-singular matrix. The process uses the Cramer's rule, which is useful to find the inverse of small matrices. For larger matrices the use of this method is not recommended.
.inverse()
None.
var A = new MAT(3, 3, [2, -1, 0, -1, 2, -1, 0, -1, 2]); var Ai = A.inverse(); document.write(Ai.toString()); // [0.75, 0.5, 0.25] // [ 0.5, 1, 0.5] // [0.25, 0.5, 0.75] document.write(Ai.product(A).toString()); // [1,0,0] // [0,1,0] // [0,0,1]
Returns the Moore-Penrose pseudoinverse (generalized inverse matrix).
.pseudoInverse()
None.
var A = new MAT(3, 3, [2, -1, 0, -1, 2, -1, 0, -1, 2]); var Api = A.pseudoInverse(); document.write(Api.toString()); // [0.75, 0.5, 0.25] // [ 0.5, 1, 0.5] // [0.25, 0.5, 0.75]
In the previous example we can see that the pseudoinverse produces the same inverse matrix. This other example shows how to get the pseudoinverse of a nxm matrix:
var A = new MAT(3, 2, [1, 2, 3, -1, -2, 1]); var Api = A.pseudoInverse(); document.write(Api.toString()); // [ 0.16, 0.19999999999999998, -0.12] // [0.41333333333333333, -0.06666666666666668, 0.10666666666666667] document.write(Api.product(A).toString()); // [ 1, 2.7755575615628914e-17] // [-5.551115123125783e-17, 1]
We can see that the pseudoinverse is a left inverse.
Produces a Vandermonde matrix from a given set of values.
.toVandermonde(integer)
var V = new MAT({values: [1,2,3,4,5,6], overwrite: true}).toVandermonde(5); document.write(V.toString()); // [1, 1, 1, 1, 1] // [1, 2, 4, 8, 16] // [1, 3, 9, 27, 81] // [1, 4, 16, 64, 256] // [1, 5, 25, 125, 625] // [1, 6, 36, 216, 1296]
Returns a matrix filled with zeros.
.zero([array])
var Z = new MAT().zero([3, 4]); document.write(Z.toString()); // [0,0,0,0] // [0,0,0,0] // [0,0,0,0]