Introduction

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!

top

error

(float) - Sets the error resolution for the iterative algorithms (e.g. eigenValues, eigenVectors).

Default:

0.000001

Example:

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});
top

maxrounds

(integer) - Sets the number of maximum rounds for the iterative algorithms (e.g. eigenValues, eigenVectors).

Default:

1000

Example:

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});
top

overwrite

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.

Default:

false

Example:

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});
top

fromArray

Creates a new matrix using array notation.

Syntax:

.fromArray(array);

Arguments:

  1. (array) - A 2D array containing the matrix values.

Returns:

  • (object) - A new matrix.

Example:

This example creates a 2x3 matrix object:

var A = new MAT({
	maxrounds: 1000,
	overwrite: true
}).fromArray([
	[1, 2, 3],
	[4, 5, 6]
]);
top

toArray

Returns an array containing all the matrix values, ordered from left to right and from top to bottom.

Syntax:

.toArray()

Arguments:

None.

Returns:

  • (array) - A copy of the array containing the matrix values.

Example:

var values = A.toArray();
top

toString

Returns a string containing all the matrix values, using the toString method of the javascript Array object.

Syntax:

.toString()

Arguments:

None.

Returns:

  • (string) - The toString method applied to the matrix array.

Example:

document.write(A.toString());
top

toLatex

Returns the matrix converted into a LaTeX formatted string.

Syntax:

.toLatex([closure])

Arguments:

  1. (string) - The closure format. Options are: b, B, v, V, p ([ ], { }, | |, || ||, ( ) respectively). Default closure is p ( ).

Returns:

  • (string) - A LaTeX formatted string.

Example:

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:

top

getColumn

Returns a column (or column vector) from the matrix.

Syntax:

.getColumn(column);

Arguments:

  1. (integer) - The column position (starting from 0, to columns-1).

Returns:

  • (object) - A new matrix containing the specified column.

Example:

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());
top

getColumns

Returns the number of columns.

Syntax:

.getColumns()

Arguments:

None.

Returns:

  • (integer) - The number of columns.

Example:

var A = new MAT().fromArray([
	[1, 0, 0],
	[0, 1, 0],
	[0, 0, 1]
]);
document.write(A.getColumns());

Will print the number 3.

top

getLength

Returns the number of elements (or length) of the matrix array.

Syntax:

.getLength()

Arguments:

None.

Returns:

  • (integer) - The number of elements of the matrix array.

Example:

var A = new MAT().fromArray([
	[1, 0, 0],
	[0, 1, 0],
	[0, 0, 1]
]);
document.write(A.getLength());

Will print the number 9.

top

getRow

Returns a row (row vector) from the matrix.

Syntax:

.getRow(row)

Arguments:

  1. (integer) - The row position (starting from 0, to rows-1).

Returns:

  • (object) - A new matrix containing the specified row.

Example:

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());
top

getRows

Returns the number of rows.

Syntax:

.getRows()

Arguments:

None.

Returns:

  • (integer) - The number of rows.

Example:

var A = new MAT().fromArray([
	[1, 0, 0],
	[0, 1, 0],
	[0, 0, 1]
]);
document.write(A.getRows());

Will print the number 3.

top

getShape

Returns the shape (or the dimensions) of the matrix.

Syntax:

.getShape()

Arguments:

None.

Returns:

  • (array) - An array of the form [rows, columns].

Example:

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());
top

getValue

Returns the value at the specified row and column values.

Syntax:

.getValue(row, column)

Arguments:

  1. (integer) - The row position (from 0 to rows-1).
  2. (integer) - The column position (from 0 to columns-1).

Returns:

  • (float|array) - The value at the specified position (complex numbers are treated as duplex of the form [real,imag]).

Example:

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.

top

getValues

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.

Syntax:

.getValues()

Arguments:

None

Returns:

  • (array) - A copy of the array containing the matrix values.

Example:

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].

top

setRows

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.

Syntax:

.setRows(rows)

Arguments:

  1. (integer) - The number of rows.

Returns:

  • (object) - The matrix object.

Example:

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].

top

setColumns

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.

Syntax:

.setColumns(columns)

Arguments:

  1. (integer) - The number of columns.

Returns:

  • (object) - The matrix object.

Example:

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].

top

setValue

Sets the value at the specified position of the matrix.

Syntax:

.setValue(row, column, value)

Arguments:

  1. (integer) - The row position (from 0 to rows-1).
  2. (integer) - The column position (from 0 to columns-1).
  3. (float|array) - The desired value.

Returns:

  • (object) - The matrix object.

Example:

The following example sets the center value of a 3x3 matrix to 0.

matrix.setValue(1, 1, 0).
top

isSquare

Tells whether the matrix is a square matrix or not.

Syntax:

.isSquare()

Arguments:

None.

Returns:

  • (boolean) - true if the matrix is square, false otherwise.

Example:

if (matrix.isSquare()) {
	// execute this code when the matrix is square
} else {
	// :(
}
top

isColumnVector

Tells whether the matrix is a column vector or not.

Syntax:

.isColumnVector()

Arguments:

None.

Returns:

  • (boolean) - true when the matrix is a column vector, false otherwise.

Example:

if (matrix.isColumnVector()) {
	// matrix is a column vector
} else {
	// other code
}
top

isRowVector

Tells whether the matrix is a row vector or not.

Syntax:

.isRowVector()

Arguments:

None.

Returns:

  • (boolean) - true when the matrix is a row vector, false otherwise.

Example:

if (matrix.isColumnVector()) {
	// matrix is a row vector
} else {
	// other code
}
top

isVector

Tells whether the matrix is a vector (1 row or 1 column) or not.

Syntax:

.isVector()

Arguments:

None.

Returns:

  • (boolean) - true when the matrix is a vector, false otherwise.

Example:

if (matrix.isVector()) {
	// the matrix is a vector
} else {
	// other code
}
top

isSameSize

Compares the shape of the matrix against the one in the argument.

Syntax:

.isSameSize(matrix)

Arguments:

  1. (object) - Another matrix

Returns:

  • (boolean) - true when both matrices have the same shape (same number of rows and columns).

Example:

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');
}
top

add

Performs the addition of two matrices (A + B).

Syntax:

.add(matrix)

Arguments:

  1. (object) - A matrix object.

Returns:

  • (object) - A new matrix with the result.

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.

Example:

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"
top

subtract

Performs the subtraction of two matrices from left to right (A - B).

Syntax:

.subtract(matrix)

Arguments:

  1. (object) - A matrix object.

Returns:

  • (object) - A new matrix with the result.

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.

Example:

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"
top

product

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.

Syntax:

.product(matrix)

Arguments:

  1. (object) - A matrix object.

Returns:

  • (object) - A new matrix with the result.

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.

Example:

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"
top

strassenProduct

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.

Syntax:

.strassenProduct(matrix)

Arguments:

  1. (object) - A matrix object.

Returns:

  • (object) - A new matrix with the result.

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.

Example:

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.

top

hadamardProduct

Performs the Hadamard product of two matrices (from left to right AB).

Syntax:

.hadamardProduct(matrix)

Arguments:

  1. (object) - A matrix object.

Returns:

  • (object) - A new matrix with the result.

Example:

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]
top

scalarProduct

Performs the scalar product between the matrix and a scalar.

Syntax:

.scalarProduct(value)

Arguments:

  1. (float|array) - A scalar value.

Returns:

  • (object) - A new matrix with the result.

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.

Example:

var A = new MAT(2, 2, [1, 2, 3, 4]);

document.write(A.scalarProduct(2).toString()); // outputs "1,4,6,8"
top

transpose

Returns the transpose matrix.

Syntax:

.transpose()

Arguments:

None.

Returns:

  • (object) - The matrix transposed.

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.

Example:

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"
top

hermitian

Returns the hermitian matrix (conjugate transpose).

Syntax:

.hermitian()

Arguments:

None.

Returns:

  • (object) - The hermitian matrix.

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.

Example:

var A = new MAT(2, 2, [3, [2,3], [2,-1], 1]);

document.write(A.hermitian().toString()); // outputs "3,[2,1],[2,-3],1"
top

trace

Returns the trace of the matrix. That is, the sum of all the elements in the main diagonal.

Syntax:

.trace()

Arguments:

None.

Returns:

  • (float|array) - The trace of the matrix.

Example:

var A = new MAT(2, 2, [3, [2,3], [2,-1], 1]);
document.write(A.trace());

The previous code will print 4.

top

upperTrace

Returns the trace of the matrix upper diagonals.

Syntax:

.upperTrace(diagonal)

Arguments:

  1. (integer) - The position of the upper diagonal (starting from 0 for the main diagonal to columns-1).

Returns:

  • (float|array) - The trace of the upper diagonal.

Example:

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)
top

lowerTrace

Returns the trace of the matrix lower diagonals.

Syntax:

.lowerTrace(diagonal)

Arguments:

  1. (integer) - The position of the lower diagonal (starting from 0 for the main diagonal to rows-1).

Returns:

  • (float|array) - The trace of the lower diagonal.

Example:

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)
top

minor

Returns a smaller matrix obtained by removing the row and the column at a specified position.

Syntax:

.minor(row, column)

Arguments:

  1. (integer) - The row to remove from the matrix.
  2. (integer) - The column to remove from the matrix.

Returns:

  • (object) - The minor matrix.

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.

Example:

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"
top

determinant

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.

Syntax:

.determinant([method])

Arguments:

  1. (string) - Optional, the method to be used (default is laplace. Options are: lu, qr).

Returns:

  • (float|array) - The determinant.

Note: when using the QR decomposition method, the result is the absolute value of the matrix determinant.

Example:

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
top

det

This is an alias for the determinant method.

Syntax:

.det([method])

Arguments:

  1. (string) - Optional, the method to be used (default is laplace. Options are: lu, qr).

Returns:

  • (float|array) - The determinant.

Example:

View the previous determinant examples.

top

gaussElimination

Performs the Gaussian elimination algorithm. This is the method teached at school to solve systems of linear equations.

Syntax:

.gaussElimination()

Arguments:

None.

Returns:

  • (object) - The matrix in a triangular (or row echelon) form.

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.

Example:

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]
top

solve

Performs the Gauss elimination method and then solves the system of linear equations using backward substitution.

Syntax:

.solve(array)

Arguments:

  1. (array) - An array with the constant terms.

Returns:

  • (object) - A column vector with the result values.

Example:

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"
top

gramSchmidt

Performs the Gram-Schmidt process (orthonormalize a set of vectors).

Syntax:

.gramSchmidt()

Arguments:

None.

Returns:

  • (object) - The set of vectors orthonormalized.

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.

Example:

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.

top

qrDecomposition

Performs the QR decomposition, that is, obtains an orthogonal matrix Q and an upper triangular matrix R (A = QR).

Syntax:

.qrDecomposition()

Arguments:

None.

Returns:

  • (array) - A duplex containing both Q and R matrices. Q matrix is at position 0, R matrix is at position 1.

Example:

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]
top

qr

This is an alias for the qrDecomposition method.

Syntax:

.qr()

Arguments:

None.

Returns:

  • (array) - A duplex containing both QR matrices. Q matrix is at position 0, R matrix is at position 1.

Example:

View the previous qrDecomposition example.

top

luDecomposition

Performs the LU decomposition, that is, obtains a lower triangular matrix L and an upper triangular matrix U (A = LU)

Syntax:

.luDecomposition()

Arguments:

None.

Returns:

  • (array) - A duplex containing both LU matrices. L matrix is at position 0, U matrix is at position 1.

Example:

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"
top

lu

This is an alias for the luDecomposition method.

Syntax:

.lu()

Arguments:

None.

Returns:

  • (array) - A duplex containing both LU matrices. L matrix is at position 0, U matrix is at position 1.

Example:

View the previous luDecomposition example.

top

eigenValues

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.

Syntax:

.eigenValues()

Arguments:

None.

Returns:

  • (object) - On convergence returns a column vector containing the eigenvalues. It returns null otherwise.

Example:

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.

top

eigenVectors

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 ;)

Syntax:

.eigenVectors()

Arguments:

None.

Returns:

  • (object) - On convergence returns a matrix containing the eigenvectors (column vectors). It returns null otherwise.

Example:

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]
top

identity

Returns the identity matrix I, a square matrix with ones in the main diagonal and zeros elsewhere.

Syntax:

.identity(integer)

Arguments:

  1. (integer) - The size of the matrix.

Returns:

  • (object) - A new identity matrix.

Example:

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"
top

diagonal

Returns a squared, diagonal matrix with different values in the main diagonal and zeros elsewhere.

Syntax:

.diagonal(array)

Arguments:

  1. (array) - An array containing the values for the main diagonal.

Returns:

  • (object) - A squared, diagonal matrix.

Example:

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]
top

pNorm

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.

Syntax:

.pNorm(float)

Arguments:

  1. (float) - The value for p.

Returns:

  • (float) - The p-norm.

Example:

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
top

norm

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.

Syntax:

.norm()

Arguments:

None.

Returns:

  • (float) - The Euclidean norm.

Example:

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
top

toToeplitz

Turns a column or row vector to a Toeplitz matrix (each descending diagonal from left to right is constant).

Syntax:

.toToeplitz(integer)

Arguments:

  1. (integer) - The number of columns.

Returns:

  • (object) - A Toeplitz matrix.

Example:

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]
top

inverse

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.

Syntax:

.inverse()

Arguments:

None.

Returns:

  • (object) - The inverse matrix.

Example:

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]
top

pseudoInverse

Returns the Moore-Penrose pseudoinverse (generalized inverse matrix).

Syntax:

.pseudoInverse()

Arguments:

None.

Returns:

  • (object) - The pseudoinverse matrix.

Example:

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.

top

toVandermonde

Produces a Vandermonde matrix from a given set of values.

Syntax:

.toVandermonde(integer)

Arguments:

  1. (integer) - The number of columns.

Returns:

  • (object) - A Vandermonde matrix.

Example:

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]
top

zero

Returns a matrix filled with zeros.

Syntax:

.zero([array])

Arguments:

  1. (array) - Optional. The shape of the zero matrix.

Returns:

  • (object) - A new matrix filled with zeros.

Example:

var Z = new MAT().zero([3, 4]);
document.write(Z.toString());

// [0,0,0,0]
// [0,0,0,0]
// [0,0,0,0]