Class Matrix
represents a 2D matrix of real (floating point) numbers
Defined in: jsmatrix.src.js.
Constructor Attributes | Constructor Name and Description |
---|---|
Matrix(nRows, nCols)
Matrix implementation
|
Field Attributes | Field Name and Description |
---|---|
Number of columns of receiver
|
|
Number of rows of receiver
|
|
Array (2D) containing receiver's elements
|
Method Attributes | Method Name and Description |
---|---|
add(mat)
Returns sum of receiver and matrix ( ret = this + mat )
|
|
appendCols(rows)
Appends vector(s)/matrix as column(s) to receiver
|
|
appendRows(rows)
Appends vector(s)/matrix as row(s) to receiver
|
|
backwardSubstitution(vec, saveOrig)
Returns vector x as a solution of this*ret = vec, receiver is assumed to be upper triangular matrix
|
|
canMultiplyMat(mat)
Testing if receiver can multiply given matrix ( this * mat )
|
|
canMultiplyVec(vec)
Testing if receiver can multiply given vector ( this * vec )
|
|
chol()
Alias for Cholesky decomposition, see Matrix#choleskyDecomposition
|
|
Returns lower triangular matrix of Cholesky's (LLT) decomposition of receiver.
|
|
copy()
Returns copy of receiver
|
|
<static> |
Matrix.create(arry)
Constructs new Matrix from given 2D array
|
decrsm(rows, cols, mat)
Decrement submatrix at defined positions of receiver
|
|
det(method, precompDecomps)
Alias for determinant, see Matrix#determinant
|
|
determinant(method, precompDecomps)
Returns determinant of receiver.
|
|
diag()
Alias for diagonalToVector(), see Matrix#diagonalToVector
|
|
<static> |
Matrix.Diagonal(arry)
Constructs new diagonal matrix from given 1D array
|
Returns vector containing receiver's diagonal elements
|
|
forwardSubstitution(vec, saveOrig)
Returns vector x as a solution of this*ret = vec, receiver is assumed to be lower triangular matrix
|
|
fromArray(arry)
Sets elements of receiver form given array
|
|
gaussianElimination(rhs, pivoting, saveOrig)
Returns vector as a solution of system of linear equations using gaussian elimination method (this * ret = rhs --> ret )
|
|
gaussJordanElimination(rhs, pivoting, saveOrig)
Returns matrix, whose columns are solutions of system of equations this*ret = rhs
|
|
get(r, c)
Returns one element of receiver ( ret = this[r,c] ) - numbering from 0
|
|
getCol(c)
Returns one column of receiver - numbering from 0
|
|
getRow(r)
Returns one row of receiver - numbering from 0
|
|
getsm(rows, cols)
Returns submatrix of receiver - numbering from 0
|
|
<static> |
Matrix.I(nRows)
Alias for Matrix.Identity(), see Matrix#Identity
|
iadd(mat)
Add given matrix to receiver ( this += mat )
|
|
<static> |
Matrix.Identity(size)
Constructs new identity matrix of given size
|
implicitPartialPivotPermutation(saveOrig)
Returns row permutation matrix using implicit partial pivoting
|
|
imulf(f)
Multiply receiver by float f ( this *= f )
|
|
incr(r, c, val)
Increment one element of receiver ( this[r,c] += val ) - numbering from 0
|
|
incrsm(rows, cols, mat)
Increment submatrix at defined positions of receiver
|
|
ineg()
Alias for neagete(), see Matrix#negate
|
|
inv(method, precompDecomps)
Alias for inversion, see Matrix#inversed
|
|
inversed(method, precompDecomps)
Returns inversion of receiver
|
|
isEqualTo(mat)
Testing matrix equality
|
|
Testing if receiver is lower triangular matrix
|
|
isSameSizeAs(mat)
Testing matrix size equality
|
|
Test receiver's singularity
|
|
isSquare()
Testing if receiver is square
|
|
Testing if receiver is symmetric matrix
|
|
isub(mat)
Substract given matrix to receiver ( this -= mat )
|
|
Testing if receiver is upper triangular matrix
|
|
lduDecomposition(pivoting, retDiagAsVector)
Returns lower triangular (L), diagonal (D) and upper triangular (U) matrix of the receiver such that L*D*U=this.
|
|
Alias for Cholesky decomposition, see Matrix#choleskyDecomposition
|
|
luDecomposition(pivoting)
Returns lower (L) and upper (U) triangular matrix of the receiver such that L*U=this.
|
|
mul(what)
Returns product of receiver and given mltiplier (float or Matrix)
|
|
mulf(f)
Returns receiver multiplied by float f ( ret = this * f )
|
|
mulm(mat)
Returns product of receiver and given matrix ( ret = this * mat )
|
|
mulv(vec)
Returns product of receiver and given vector ( ret = this * vec )
|
|
neg()
Alias for neageted(), see Matrix#negated
|
|
negate()
Negate receiver ( ret *= -1.
|
|
negated()
Returns negative of receiver ( ret = -this )
|
|
<static> |
Matrix.Ones(nRows, nCols)
Creates a matrix of given size full of ones
|
permuteRows(indx, backward)
Permute rows of receiver according to given indices
|
|
qrDecomposition(saveOrig)
Returns orthogonal matrix Q^T and upper triangular matrix R of QR decomposition of receiver such that Q*R=this.
|
|
resize(nRows, nCols)
Resize receiver according to given size (delete extra elements or add zero elements)
|
|
resized(nRows, nCols)
Returns resized copy of receiver according to given size (delete extra elements or add zero elements)
|
|
rowPermutation(indx, backward)
Returns copy of receiver with rows permutated according to given indices
|
|
set(r, c, val)
Set one element of receiver ( this[r,c] = val ) - numbering from 0
|
|
setCol(c, vec)
Sets one column of receiver - numbering from 0
|
|
setRow(r, vec)
Sets one row of receiver - numbering from 0
|
|
setsm(rows, cols, mat)
Set submatrix to defined positions of receiver
|
|
size()
Returns size of receiver as [nRows,nCols]
|
|
solveForRhs(rhs, method, saveOrig, precompDecomps)
Returns vector/array of vectors/matrix as a solution of system of linear equations (this*ret=rhs --> ret).
|
|
sub(mat)
Returns difference of receiver and matrix ( ret = this - mat )
|
|
swapCols(c1, c2)
Swaps two columns of receiver - numbering from 0
|
|
swapRows(r1, r2)
Swaps two rows of receiver - numbering from 0
|
|
T()
Alias for transposed(), see Matrix#transposed
|
|
toArray()
Returns elements of receiver as an array
|
|
toString()
Returns string representation of receiver
|
|
Returns transposition of receiver ( ret = this^T )
|
|
x(what)
Alias for mul(), see Matrix#mul
|
|
zero()
Zeros all elements of receiver
|
|
<static> |
Matrix.Zeros(nRows, nCols)
Creates a matrix of given size full of zeros
|
Field Detail
{int}
nCols
Number of columns of receiver
- Default Value:
- 0
{int}
nRows
Number of rows of receiver
- Default Value:
- 0
{[floats]}
theArray
Array (2D) containing receiver's elements
- Default Value:
- []
Method Detail
{Matrix}
add(mat)
Returns sum of receiver and matrix ( ret = this + mat )
m1 = mat([[1,2],[3,4]]); m2 = mat([[2,5],[3,2]]); m3 = m1.add(m2); // m3 = Matrix([[3,7],[6,6]])
- Parameters:
- {Matrix} mat
- vector to be added
- Returns:
- {Matrix} sum of receiver and matrix
appendCols(rows)
Appends vector(s)/matrix as column(s) to receiver
m = mat([[1,2],[3,4]]); m.appendCols(vec([7,6])); // m = Matrix([[1,2,7],[3,4,6]]) m.appendCols(mat([[1,3],[2,4]])); // m = Matrix([[1,2,7,1,3],[3,4,6,2,4]]) m.appendCols([vec([2,1]),vec([33,44])]); // m = Matrix([[1,2,7,1,3,2,33],[3,4,6,2,4,1,44]]) m.appendCols([mat([[2,1],[4,3]]),mat([[99,88],[77,66]])]); // m = Matrix([[1,2,7,1,3,2,33,2,1,99,88],[3,4,6,2,4,1,44,4,3,77,66]])
appendRows(rows)
Appends vector(s)/matrix as row(s) to receiver
m = mat([[1,2],[3,4]]); m.appendRows(vec([7,6])); // m = Matrix([[1,2],[3,4],[7,6]]) m.appendRows(mat([[1,3],[2,4]])); // m = Matrix([[1,2],[3,4],[7,6],[1,3],[2,4]]) m.appendRows([vec([2,1]),vec([33,44])]); // m = Matrix([[1,2],[3,4],[7,6],[1,3],[2,4],[2,1],[33,44]]) m.appendRows([mat([[2,1],[4,3]]),mat([[99,88],[77,66]])]); // m = Matrix([[1,2],[3,4],[7,6],[1,3],[2,4],[2,1],[33,44],[2,1],[4,3],[99,88],[77,66]])
{Vector}
backwardSubstitution(vec, saveOrig)
Returns vector x as a solution of this*ret = vec, receiver is assumed to be upper triangular matrix
a = mat([[1,2,3,4],[0,5,6,7],[0,0,8,9],[0,0,0,10]]); y = vec([20,34,25,10]); x = a.forwardSubstitution(y); // x = Vector([4,3,2,1]) // y = Vector([20,34,25,10]) check = a.mul(x); // check = Vector([20,34,25,10]) // x = Vector([4,3,2,1]) // y = Vector([20,34,25,10]) yy - y.copy(); x = a.forwardSubstitution(yy,false); // x = Vector([4,3,2,1]) // yy = Vector([4,3,2,1])
- Parameters:
- {Vector} vec
- right hand side
- {bool} saveOrig Optional, Default: true
- if true, returns vec will be unchanged and new vector will be returned. If false, solution will be saved to vec and vec will be returned
- Returns:
- {Vector} solution of this*ret = vec
{bool}
canMultiplyMat(mat)
Testing if receiver can multiply given matrix ( this * mat )
m1 = mat([[1,2,3],[4,5,6]]); m2 = mat([[7,8,9],[10,11,12]]); m3 = mat([[11,12],[21,22],[31,32]]); t1 = m1.canMultiplyMat(m2); // t1 = false t2 = m1.canMultiplyMat(m3); // t2 = true
- Parameters:
- {Matrix} mat
- matrix to be tested
- Returns:
- {bool} true if the multiplication is possible, false otherwise
{bool}
canMultiplyVec(vec)
Testing if receiver can multiply given vector ( this * vec )
m = mat([[1,2,3],[4,5,6]]); v1 = vec([1,2,3]); v2 = vec([4,5]); t1 = m.canMultiplyVec(v1); // t1 = true t2 = m.canMultiplyVec(v2); // t2 = false
- Parameters:
- {Vector} vec
- vector to be tested
- Returns:
- {bool} true if the multiplication is possible, false otherwise
chol()
Alias for Cholesky decomposition, see Matrix#choleskyDecomposition
{Matrix}
choleskyDecomposition()
Returns lower triangular matrix of Cholesky's (LLT) decomposition of receiver. Receiver must be square, symmetric and positive definite. ( ret * ret^T = this )
a = mat([[1,2,4],[2,13,23],[4,23,77]]); l = a.choleskyDecomposition(); // l = Matrix([[1,0,0],[2,3,0],[4,5,6]]) check = l.x(l.T()); // check = Matrix([[1,2,4],[2,13,23],[4,23,77]]);
- Returns:
- {Matrix} lower triangular matrix
{Matrix}
copy()
Returns copy of receiver
m1 = mat([[11,12],[21,22]]); m2 = m1; m3 = m1.copy(); m1.set(1,0,6); // m1 = Matrix([[11,12],[6,22]]) // m2 = Matrix([[11,12],[6,22]]) // m3 = Matrix([[11,12],[21,22]])
- Returns:
- {Matrix} copy of receiver
<static>
{Matrix}
Matrix.create(arry)
Constructs new Matrix from given 2D array
m = Matrix.create([[1,2,3],[4,5,6],[7,8,9]]) // m = Matrix([[1,2,3],[4,5,6],[7,8,9]])
- Parameters:
- {[floats](2D)} arry Optional, Default: []
- array containing elements of new matrix
- Returns:
- {Matrix} new Matrix object
decrsm(rows, cols, mat)
Decrement submatrix at defined positions of receiver
m = mat([[11,12,13,14],[21,22,23,24],[31,32,33,34],[41,42,43,44]]); m.decrsm([1,2],[0,2],mat([[66,77],[88,99]])); // m = Matrix([11,12,13,14],[-45,22,-54,24],[-57,32,-66,34],[41,42,43,44]])
- Parameters:
- {[floats]|Vector} rows
- array containing rows indices to be decrementes - numbering from 0
- {[floats]|Vector} cols
- array containing columns indices to be decremented - numbering from 0
- {Matrix} mat
- matrix to be decremented on desired positions
det(method, precompDecomps)
Alias for determinant, see Matrix#determinant
- Parameters:
- method
- precompDecomps
{float}
determinant(method, precompDecomps)
Returns determinant of receiver.
a = mat([[4,2,1],[2,5,3],[1,3,6]]); d = a.determinant(); // d = 67
- Parameters:
- {int} method Optional, Default: JSM_LU
- used method. Options are JSM_LU for using LU decomposition
- {[Matrices]} precompDecomps
- precomputed matrix decompositions if we want to use some
- Returns:
- {float} determinant of receiver
diag()
Alias for diagonalToVector(), see Matrix#diagonalToVector
<static>
{Matrix}
Matrix.Diagonal(arry)
Constructs new diagonal matrix from given 1D array
m1 = Matrix.Diagonal([1,2,3]); // m1 = Matrix([[1,0,0],[0,2,0],[0,0,3]]) m2 = Matrix.Diagonal(vec([1,2,3])); // m2 = Matrix([[1,0,0],[0,2,0],[0,0,3]])
- Parameters:
- {[floats]|Vector} arry
- array or vector containing elements of new matrix
- Returns:
- {Matrix} new Matrix object
{Vector}
diagonalToVector()
Returns vector containing receiver's diagonal elements
m = mat([[1,2,3],[4,5,6],[7,8,9]]); v = m.diag(); // v = Vector([1,5,9])
- Returns:
- {Vector} vector containing receiver's diagonal elements
{Vector}
forwardSubstitution(vec, saveOrig)
Returns vector x as a solution of this*ret = vec, receiver is assumed to be lower triangular matrix
a = mat([[1,0,0,0],[2,3,0,0],[4,5,6,0],[7,8,9,10]]); y = vec([4,17,43,80]); x = a.forwardSubstitution(y); // x = Vector([4,3,2,1]) // y = Vector([4,17,43,61]) check = a.mul(x); // check = Vector([4,17,43,61]) // x = Vector([4,3,2,1]) // y = Vector([4,17,43,61]) yy - y.copy(); x = a.forwardSubstitution(yy,false); // x = Vector([4,3,2,1]) // yy = Vector([4,3,2,1])
- Parameters:
- {Vector} vec
- right hand side
- {bool} saveOrig Optional, Default: true
- if true, returns vec will be unchanged and new vector will be returned. If false, solution will be saved to vec and vec will be returned
- Returns:
- {Vector} solution of this*ret = vec
fromArray(arry)
Sets elements of receiver form given array
m.fromArray([[1,2,3],[4,5,6],[7,8,9]]); // m = Matrix([[1,2,3],[4,5,6],[7,8,9]])
- Parameters:
- {[floats](2D)} arry
- array containing new receiver elements
{Vector}
gaussianElimination(rhs, pivoting, saveOrig)
Returns vector as a solution of system of linear equations using gaussian elimination method (this * ret = rhs --> ret )
a = mat([[1,2,9],[8,3,2],[3,7,3]]) b = vec([32,20,26]); x = a.gaussianElimination(b) // x = Vector([1,2,3]) x = a.gaussianElimination(b,JSM_PARTPIVOT); // x = Vector([1,2,3]) a.gaussianElimination(b,JSM_NOPIVOT,false); // a = Matrix([[1,2,9],[8,-13,-70],[3,1,-29.384615384615387]]) // b = Vector([1,2,3])
- Parameters:
- {Vector} rhs
- vector of right hand sides
- {int} pivoting Optional, Default: JSM_PARTPIVOT
- what type of pivoting to use. JSM_NOPIVOT stands for no pivoting, JSM_PARTPIVOT for implicit partial pivoting (only row interchanges)
- saveOrig
- Returns:
- {Vector} vector of solution
{Matrix}
gaussJordanElimination(rhs, pivoting, saveOrig)
Returns matrix, whose columns are solutions of system of equations this*ret = rhs
a = mat([[1,2,9],[8,3,2],[3,7,3]]); b1 = vec([32,20,26]); b2 = vec([16,32,26]); b3 = [b1.copy(),b2.copy()]; b4 = mat([b1.toArray(),b2.toArray()]); x1 = a.gaussJordanElimination(b1); // x1 = Vector([ 1, 2, 3 ]) x2 = a.gaussJordanElimination(b2); // x2 = Vector([ 3.0000000000000018, 2, 1 ]) x3 = a.gaussJordanElimination(b3); // x3 = Vector([ 1, 2, 3 ]) ,Vector([ 3.0000000000000018, 2, 1 ]) x4 = a.gaussJordanElimination(b4); // x4 = Matrix([[ 1, 3.0000000000000018 ], [ 2, 2 ], [ 3, 1 ]]) x5 = a.gaussJordanElimination(b1,JSM_PARTPIVOT);; // x5 = Vector([ 1, 2, 3 ]) x6 = a.gaussJordanElimination(b2,JSM_PARTPIVOT); // x6 = Vector([ 3, 2, 1 ]) x7 = a.gaussJordanElimination(b3,JSM_PARTPIVOT); // x7 = Vector([ 1, 2, 3 ]) ,Vector([ 3, 2, 1 ]) x8 = a.gaussJordanElimination(b4,JSM_PARTPIVOT); // x8 = Matrix([[ 1, 3 ], [ 2, 2 ], [ 3, 1 ]]) a.gaussJordanElimination(b4,JSM_NOPIVOT,false); // b4 = Matrix([[ 1, 3.0000000000000018 ], [ 2, 2 ], [ 3, 1 ]])
- Parameters:
- {Matrix|[Vectors]|Vector} rhs
- matrix/vector/array of vectors representing right hand sides
- {int} pivoting Optional, Default: JSM_PARTPIVOT
- what type of pivoting to use. JSM_NOPIVOT stands for no pivoting, JSM_PARTPIVOT for implicit partial pivoting (only row interchanges)
- {bool} saveOrig Optional, Default: true
- if true, receiver is not changed. If false, solution will be saved to rhs and rhs will be returned
- Returns:
- {Matrix} matrix, whose columns are solution for particular right hand sides
{float}
get(r, c)
Returns one element of receiver ( ret = this[r,c] ) - numbering from 0
m = mat([[11,12,13],[21,22,23],[31,32,33]]); g = m.get(1,2); // g = 23
- Parameters:
- {int} r
- index of row to be get (numbering from 0)
- {int} c
- index of column to be get (numbering from 0)
- Returns:
- {float} value of the element at r-th row and c-th column
{Vector}
getCol(c)
Returns one column of receiver - numbering from 0
m = m([[1,2,3],[4,5,6],[7,8,9]]); v = mat.getCol(1); // v = Vector([2,5,8])
- Parameters:
- {int} c
- index of column to return - numbering from 0
- Returns:
- {Vector} c-th column of receiver as vector
{Vector}
getRow(r)
Returns one row of receiver - numbering from 0
m = m([[1,2,3],[4,5,6],[7,8,9]]); v = mat.getRow(1); // v = Vector([4,5,6])
- Parameters:
- {int} r
- index of row to return - numbering from 0
- Returns:
- {Vector} r-th row of receiver as vector
{Matrix}
getsm(rows, cols)
Returns submatrix of receiver - numbering from 0
m1 = mat([[11,12,13,14],[21,22,23,24],[31,32,33,34],[41,42,43,44]]); m2 = m1.getsm([1,2],[0,2]); // m2 = Matrix([[21,23],[31,33]])
- Parameters:
- {[floats]|Vector} rows
- array containing rowa indices of desired submatrix - numbering from 0
- {[floats]|Vector} cols
- array containing columns indices of desired submatrix - numbering from 0
- Returns:
- {Matrix} desired submatrix
<static>
Matrix.I(nRows)
Alias for Matrix.Identity(), see Matrix#Identity
- Parameters:
- nRows
iadd(mat)
Add given matrix to receiver ( this += mat )
m1 = mat([[1,2],[3,4]]); m2 = mat([[2,5],[3,2]]); m1.iadd(m2); // m1 = Matrix([[3,7],[6,6]])
- Parameters:
- {Matrix} mat
- vector to be added
<static>
{Matrix}
Matrix.Identity(size)
Constructs new identity matrix of given size
m = Matrix.Identity(4) // m = Matrix([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]])
- Parameters:
- {int} size
- size of returned matrix
- Returns:
- {Matrix} identity matri
{[Matrix|[ints]]}
implicitPartialPivotPermutation(saveOrig)
Returns row permutation matrix using implicit partial pivoting
m1 = mat([[1,2,4,9],[1,8,1,1],[9,4,5,3],[1,2,6,2]]); m2 = m1.implicitPartialPivotPermutation(); // m2[0] = Matrix([[9,4,5,3],[1,8,1,1],[1,2,6,2],[1,2,4,9]]) // m2[1] = [2,1,3,0] // m1 = Matrix([[1,2,4,9],[1,8,1,1],[9,4,5,3],[1,2,6,2]]) m3 = m1.rowPermutation(m2[1]); // m3 = Matrix([[9,4,5,3],[1,8,1,1],[1,2,6,2],[1,2,4,9]]) m4 = m2[0].rowPermutation(m2[1],true); // m4 = Matrix([[1,2,4,9],[1,8,1,1],[9,4,5,3],[1,2,6,2]]) m1 = m1.implicitPartialPivotPermutation(false)[0]; // m1 = Matrix([[9,4,5,3],[1,8,1,1],[1,2,6,2],[1,2,4,9]])
- Parameters:
- {bool} saveOrig Optional, Default: true
- if true, returns permutated copy of receiver. If false, permutate receiver and return this
- Returns:
- {[Matrix|[ints]]} permutated matrix and array containing indices of original matrix according to position in the permutated one ( such that this.rowPermutation(indx) = ret )
imulf(f)
Multiply receiver by float f ( this *= f )
m = mat([[1,2,3],[4,5,6]]); m.imulf(3); // m = Matrix([[3,6,9],[12,15,18]])
- Parameters:
- {float} f
- float multiplier
incr(r, c, val)
Increment one element of receiver ( this[r,c] += val ) - numbering from 0
m = mat([[11,12,13],[21,22,23],[31,32,33]]); m.incr(1,2,3.45); // m = Matrix([[11,12,13],[21,22,26.45],[31,32,33]])
- Parameters:
- {int} r
- index of row to be incremented (numbering from 0)
- {int} c
- index of column to be incremented (numbering from 0)
- {float} val
- value to be add
incrsm(rows, cols, mat)
Increment submatrix at defined positions of receiver
m = mat([[11,12,13,14],[21,22,23,24],[31,32,33,34],[41,42,43,44]]); m.incrsm([1,2],[0,2],mat([[66,77],[88,99]])); // m = Matrix([11,12,13,14],[87,22,110,24],[119,32,132,34],[41,42,43,44]])
- Parameters:
- {[floats]|Vector} rows
- array containing rows indices to be incrementes - numbering from 0
- {[floats]|Vector} cols
- array containing columns indices to be incremented - numbering from 0
- {Matrix} mat
- matrix to be incremented on desired positions
ineg()
Alias for neagete(), see Matrix#negate
inv(method, precompDecomps)
Alias for inversion, see Matrix#inversed
- Parameters:
- method
- precompDecomps
{Matrix}
inversed(method, precompDecomps)
Returns inversion of receiver
a = mat([[2,2,1],[2,3,0.5],[1,0.5,2]]); b = a.inversed(); // b = Matrix([[2.3,-1.4,-0.8],[-1.4,1.2,0.4],[-0.8,0.4,0.8]]) check = a.x(b); // check = Matrix([[1,0,0],[0,1,0],[0,0,1]])
- Parameters:
- {int} method Optional, Default: JSM_GAUSSJORDAN
- method used for solution. Options are JSM_GAUSSJORDAN for Gauss-Jordan method, JSM_LU for solution with LU decomposition
- {[Matrices]} precompDecomps Optional, Default: undefined
- precomputed matrix decompositions if we want to se some
- Returns:
- {Matrix} inversion of receiver
{bool}
isEqualTo(mat)
Testing matrix equality
a = mat([[1,2,3],[4,5,6],[7,8,9]]); b = mat([[1,2,2.999999999],[4.0000000000002,5,6],[7,8,8.9999999999]]); c = mat([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]); t1 = a.isEqualTo(b); // t1 = true t2 = a.isEqualTo(c); // t2 = false
- Parameters:
- {Matrix} mat
- matrix to be compared with receiver
- Returns:
- {bool} true if this==mat, false otherwise
{bool}
isLowerTriangular()
Testing if receiver is lower triangular matrix
m1 = mat([[1,2,3],[4,5,6],[7,8,9]]); m2 = mat([[1,0,0],[4,5,0],[7,8,9]]); t1 = m1.isLowerTriangular(); // t1 = false t2 = m2.isLowerTriangular(); // t2 = true
- Returns:
- {bool} true if receiver is lower triangular, false otherwise
{bool}
isSameSizeAs(mat)
Testing matrix size equality
a = mat([[1,2,3],[4,5,6]]); b = mat([[5,6,7],[8,9,10],[11,12,13]]); c = mat([[14,15,16],[17,18,19]]); t1 = a.isSameSizeAs(b); // t1 = false t2 = a.isSameSizeAs(c); // t1 = true
- Parameters:
- {Matrix} mat
- matrix to be tested
- Returns:
- {bool} true if mat and receiver has same size, false otherwise
{bool}
isSingular()
Test receiver's singularity
a = mat([[1,2,3],[2,4,6],[4,5,6]]); b = mat([[4,2,1],[2,5,3],[1,3,6]]); s1 = a.isSingular(); // s1 = true s2 = b.isSingular(); // s1 = false
- Returns:
- {bool} true if receiver is singular (if abs(this.det()) < JSM_TOL), false otherwise
{bool}
isSquare()
Testing if receiver is square
m1 = mat([[1,2,3],[4,5,6]]); m2 = mat([[1,2,3],[4,5,6],[7,8,9]]); t1 = m1.isSquare(); // t1 = false t2 = m2.isSquare(); // t2 = true
- Returns:
- {bool} true if receiver is square matrix, false otherwise
{bool}
isSymmetric()
Testing if receiver is symmetric matrix
m1 = mat([[11,12,13],[21,22,23],[31,32,33]]); m2 = mat([[11,12,13],[12,22,23],[13,23,33]]); t1 = m1.isSymmetric(); // t1 = false t2 = m2.isSymmetric(); // t2 = true
- Returns:
- {bool} true if receiver is symmetric, false otherwise
isub(mat)
Substract given matrix to receiver ( this -= mat )
m1 = mat([[1,2],[3,4]]); m2 = mat([[2,5],[3,2]]); m1.isub(m2); // m1 = Matrix([[-1,-3],[0,2]])
- Parameters:
- {Matrix} mat
- vector to be added
{bool}
isUpperTriangular()
Testing if receiver is upper triangular matrix
m1 = mat([[1,2,3],[4,5,6],[7,8,9]]); m2 = mat([[1,2,3],[0,5,6],[0,0,9]]); t1 = m1.isUpperTriangular(); // t1 = false t2 = m2.isUpperTriangular(); // t2 = true
- Returns:
- {bool} true if receiver is upper triangular, false otherwise
Returns lower triangular (L), diagonal (D) and upper triangular (U) matrix of the receiver such that L*D*U=this. Diagonal elements of both L and U are all 1. Receiver must be square.
a = mat([[12,10,8],[24,29,22],[36,66,52]]); ldu = a.lduDecomposition(); l = ldu[0]; // l = Matrix([[1,0,0],[2,1,0],[3,4,1]]) d = ldu[1]; // d = Matrix([[2,0,0],[0,3,0],[0,0,4]]) u = ldu[2]; // u = Matrix([[6,5,4],[0,3,2],[0,0,1]]) check = l.x(d).x(u) // check = Matrix([[12,10,8],[24,29,22],[36,66,52]])
- Parameters:
- {int} pivoting Optional, Default: JSM_NOPIVOT
- what type of pivoting to use. JSM_NOPIVOT stands for no pivoting, JSM_PARTPIVOT for implicit partial pivoting (only row interchanges)
- {bool} retDiagAsVector Optional, Default: false
- if true, d is returned as vector. If false, d is returned as diagonal matrix
lltDecomposition()
Alias for Cholesky decomposition, see Matrix#choleskyDecomposition
Returns lower (L) and upper (U) triangular matrix of the receiver such that L*U=this. Diagonal elements of L are all 1. Receiver must be square.
a = mat([[6,5,4],[12,13,10],[18,27,21]]); lu = a.luDecomposition(); l = lu[0]; // l = Matrix([[1,0,0],[2,1,0],[3,4,1]]) u = lu[1]; // u = Matrix([[6,5,4],[0,3,2],[0,0,1]]) check = l.x(u); // check = Matrix([[6,5,4],[12,13,10],[18,27,21]])
- Parameters:
- {int} pivoting Optional, Default: JSM_NOPIVOT
- what type of pivoting to use. JSM_NOPIVOT stands for no pivoting, JSM_PARTPIVOT for implicit partial pivoting (only row interchanges)
Returns product of receiver and given mltiplier (float or Matrix)
m1 = mat([[1,2,3],[4,5,6]]); m2 = m1.mulf(3); // m2 = Matrix([[3,6,9],[12,15,18]]) m3 = mat([[11,12,13],[21,22,23],[31,32,33]]); v1 = vec([1,2,3]); v2 = m3.mulv(v1) // v2 = Vector([74,134,194]); m4 = mat([[11,12],[21,22]]); m5 = mat([[1,2],[3,4]]); m6 = m4.mulm(m5); // m6 = Matrix([[47,70],[87,130]])
{Matrix}
mulf(f)
Returns receiver multiplied by float f ( ret = this * f )
m1 = mat([[1,2,3],[4,5,6]]); m2 = m1.mulf(3); // m2 = Matrix([[3,6,9],[12,15,18]])
- Parameters:
- {float} f
- float multiplier
- Returns:
- {Matrix} copy of receiver multiplied by f
{Matrix}
mulm(mat)
Returns product of receiver and given matrix ( ret = this * mat )
m1 = mat([[11,12],[21,22]]); m2 = mat([[1,2],[3,4]]); m3 = m1.mulm(m2); // m3 = Matrix([[47,70],[87,130]])
- Parameters:
- {Matrix} mat
- matrix to multiply
- Returns:
- {Matrix} copy of receiver multiplied by mat
{Vector}
mulv(vec)
Returns product of receiver and given vector ( ret = this * vec )
m = mat([[11,12,13],[21,22,23],[31,32,33]]); v1 = vec([1,2,3]); v2 = m.mulv(v1); // v2 = Vector([74,134,194])
- Parameters:
- {Vector} vec
- vector to multiply
- Returns:
- {Vector} copy of receiver multiplied by vec
neg()
Alias for neageted(), see Matrix#negated
negate()
Negate receiver ( ret *= -1., ret = -ret )
m1 = mat([[1,2,3],[4,5,6]]); m1.negate(); // m1 = Matrix([[-1,-2,-3],[-4,-5,-6]])
{Matrix}
negated()
Returns negative of receiver ( ret = -this )
m1 = mat([[1,2,3],[4,5,6]]); m2 = m1.negated(); // m2 = Matrix([[-1,-2,-3],[-4,-5,-6]])
- Returns:
- {Matrix} negative of receiver
<static>
{Matrix}
Matrix.Ones(nRows, nCols)
Creates a matrix of given size full of ones
m = Matrix.Ones(2,3); // m = Matrix([[1,1,1],[1,1,1]])
- Parameters:
- {int} nRows Optional, Default: 0
- number of rows
- {int} nCols Optional, Default: 0
- number of columns
- Returns:
- {Matrix} new vector full of zero
permuteRows(indx, backward)
Permute rows of receiver according to given indices
m = mat([[11,12,13],[21,22,23],[31,32,33]]); m.permuteRows([1,2,0]); // m = Matrix([[21,22,23],[31,32,33],[11,12,13]]) m.permuteRows([1,2,0],true); // m = mat([[11,12,13],[21,22,23],[31,32,33]])
- Parameters:
- {[ints]|Vector} indx
- indices of row permutation
- {bool} backward Optional, Default: false
- if false, receiver's rows is permutated to given indices (forward). If true, from given indices (backward)
Returns orthogonal matrix Q^T and upper triangular matrix R of QR decomposition of receiver such that Q*R=this.
a = mat([[1,2,9],[8,3,4],[3,7,1]]); qr = a.qrDecomposition(); qt = qr[0]; // qt = Matrix([[ -0.1162, -0.9299, -0.3487 ], [ 0.2407, -0.3670, 0.8985 ], [ -0.9636, 0.0205, 0.2665 ]]) r = qr[1]; // r = Matrix([[ -8.6023, -5.4636, -5.1148 ], [ 0, 5.6699, 1.5968 ], [ 0, 0, -8.3239 ]]) check = q.T().x(r); // check = Matrix([[1,2,9],[8,3,4],[3,7,1]]) o = q.T().x(q); // o = Matrix([[1,0,0],[0,1,0],[0,0,1]])
- Parameters:
- {bool} saveOrig Optional, Default: true
- if true, receiver is not changed. If false, solution (R matrix) will be saved to this and this will be returned
resize(nRows, nCols)
Resize receiver according to given size (delete extra elements or add zero elements)
m1 = mat([[11,12,13,14],[21,22,23,24],[31,32,33,34],[41,42,43,44]]); m2 = mat([[11,12],[21,22]]); m1.resize(3,3); // m1 = Matrix([[11,12,13],[21,22,23],[31,32,33]]) m2.resize(3,3); // m2 = Matrix([[11,12,0],[21,22,0],[0,0,0]])
- Parameters:
- {int} nRows
- new number of rows
- {int} nCols
- new number of columns
{Matrix}
resized(nRows, nCols)
Returns resized copy of receiver according to given size (delete extra elements or add zero elements)
m1 = mat([[11,12,13],[21,22,23],[31,32,33]]); m2 = m1.resized(2,4); // m2 = Matrix([[11,12,13,0],[21,22,23,0]]) m3 = m1.resized(4,2); // m3 = Matrix([[11,12],[21,22],[31,32],[0,0]])
- Parameters:
- {int} nRows
- new number of rows
- {int} nCols
- new number of columns
- Returns:
- {Matrix} resized copy of receiver
{Matrix}
rowPermutation(indx, backward)
Returns copy of receiver with rows permutated according to given indices
m1 = mat([[11,12,13],[21,22,23],[31,32,33]]); m2 = m1.permuteRows([1,2,0]); // m2 = Matrix([[21,22,23],[31,32,33],[11,12,13]]) m3 = m1.permuteRows([1,2,0],true); // m3 = mat([[31,32,33],[11,12,13],[21,22,23]])
- Parameters:
- {[ints]|Vector} indx
- indices of permutation
- {bool} backward Optional, Default: false
- if false, receiver is permutated to given indices (forward). If true, from given indices (backward)
- Returns:
- {Matrix} copy of receiver with permutated rows
set(r, c, val)
Set one element of receiver ( this[r,c] = val ) - numbering from 0
m = mat([[11,12,13],[21,22,23],[31,32,33]]); m.set(1,2,3.45); // m = Matrix([[11,12,13],[21,22,3.45],[31,32,33]])
- Parameters:
- {int} r
- index of row to be set (numbering from 0)
- {int} c
- index of column to be set (numbering from 0)
- {float} val
- value to be set
setCol(c, vec)
Sets one column of receiver - numbering from 0
m = mat([[1,2,3],[4,5,6],[7,8,9]]); m.setCol(1,vec([11,12,13])); // m = Matrix([[1,11,3],[4,12,6],[7,13,9]])
- Parameters:
- {int} c
- index of column to set - numbering from 0
- {Vector} vec
- vector to be set as new r-th row
setRow(r, vec)
Sets one row of receiver - numbering from 0
mat([[1,2,3],[4,5,6],[7,8,9]]); m.setRow(1,vec([11,12,13])); // m = Matrix([[1,2,3],[11,12,13],[7,8,9]])
- Parameters:
- {int} r
- index of row to set - numbering from 0
- {Vector} vec
- vector to be set as new r-th row
setsm(rows, cols, mat)
Set submatrix to defined positions of receiver
m = mat([[11,12,13,14],[21,22,23,24],[31,32,33,34],[41,42,43,44]]); m.setsm([1,2],[0,2],mat([[66,77],[88,99]])); // m = Matrix([[11,12,13,14],[66,22,77,24],[88,32,99,34],[41,42,43,44]])
- Parameters:
- {[floats]|Vector} rows
- array containing rows indices to be set - numbering from 0
- {[floats]|Vector} cols
- array containing columns indices to be set - numbering from 0
- {Matrix} mat
- matrix to be set on desired positions
{[int|int]}
size()
Returns size of receiver as [nRows,nCols]
m = mat([[1,2,3],[4,5,6]]); s = m.size(); // s = [2,3]
- Returns:
- {[int|int]} array as [number of rows,number of columns]
{Vector|[Vectors]|Matrix}
solveForRhs(rhs, method, saveOrig, precompDecomps)
Returns vector/array of vectors/matrix as a solution of system of linear equations (this*ret=rhs --> ret). Receiver must be square. TODO: solution with LU decomposition with permutation
a = mat([[1,2,9],[8,3,2],[3,7,3]]); b = vec([32,20,26]); x1 = a.solveForRhs(b); // x1 = Vector([1,2,3]) x2 = a.solveForRhs(b,JSM_GAUSS); // x2 = Vector([1,2,3]) x3 = a.solveForRhs(b,JSM_GAUSSJORDAN); // x3 = Vector([1,2,3]) x4 = a.solveForRhs(b,JSM_LU); // x4 = Vector([1,2,3]) x5 = a.solveForRhs(b,JSM_LDU); // x5 = Vector([1,2,3]) x6 = a.solveForRhs(b,JSM_CHOLESKY); // x6 = Vector([1,2,3]) x7 = a.solveForRhs(b,JSM_QR); // x7 = Vector([1,2,3])
- Parameters:
- {Vector|[Vectors]|Matrix} rhs
- vector of right hand sides. Array of vectors or Matrix as rhs is supported only with JSM_GAUSSJORDAN method
- {int} method Optional, Default: JSM_GAUSS
- method of the solution. Implemented methods are JSM_GAUSS for gaussian elimination, JSM_GAUSSJORDAN for Gauss-Jordan elimination (supporting more than one right hand sides in form of array of vectors or matrix with columns as rhs), JSM_LU for using LU decomposition, JSM_LDU for using LDU decomposition, JSM_CHOLESKY for using Cholesky decomposition
- {bool} saveOrig Optional, Default: true
- if true, receiver and rhs is not changed. If false, solution will be saved to rhs and rhs will be returned and receiver will be changed
- {[Matrices]} precompDecomps Optional, Default: undefined
- array of Matrices objects, used for solution using matrix decomposition as precomputed values (the decomposition is performed within solveForRhs function if args parameter is not specified)
- Returns:
- {Vector|[Vectors]|Matrix} vector/array of vectors/matrix of solution
{Matrix}
sub(mat)
Returns difference of receiver and matrix ( ret = this - mat )
m1 = mat([[1,2],[3,4]]); m2 = mat([[2,5],[3,2]]); m3 = m1.sub(m2); // m3 = Matrix([[-1,-3],[0,2]])
- Parameters:
- {Matrix} mat
- vector to be added
- Returns:
- {Matrix} sum of receiver and matrix
swapCols(c1, c2)
Swaps two columns of receiver - numbering from 0
m = mat([[11,12,13],[21,22,23],[31,32,33]]); m.swapCols(0,2); // m = Matrix([[13,12,11],[23,22,21],[33,32,31]])
- Parameters:
- {int} c1
- index of first row to swap - numbering from 0
- {int} c2
- index of second row to swap - numbering from 0
swapRows(r1, r2)
Swaps two rows of receiver - numbering from 0
m = mat([[11,12,13],[21,22,23],[31,32,33]]); m.swapRows(0,2); // m = Matrix([[31,32,33],[21,22,23],[11,12,13]])
- Parameters:
- {int} r1
- index of first row to swap - numbering from 0
- {int} r2
- index of second row to swap - numbering from 0
T()
Alias for transposed(), see Matrix#transposed
{[floats](2D)}
toArray()
Returns elements of receiver as an array
m =mat([[11,12],[21,22]]); a = m.toArray(); // a = [[11,12],[21,22]]
- Returns:
- {[floats](2D)} array containing receiver's elements
{string}
toString()
Returns string representation of receiver
m = mat([[1,2,3],[4,5,6],[7,8,9]]); alert(m); // alerts "Matrix([[1,2,3],[4,5,6],[7,8,9]])"
- Returns:
- {string} string representation of receiver
{Matrix}
transposed()
Returns transposition of receiver ( ret = this^T )
m1 = mat([[1,2,3],[4,5,6]]); m2 = m1.transposed(); // m2 = Matrix([[1,4],[2,5],[3,6]])
- Returns:
- {Matrix} transposed copy of receiver
x(what)
Alias for mul(), see Matrix#mul
- Parameters:
- what
zero()
Zeros all elements of receiver
m = mat([[1,2,3],[4,5,6],[7,8,9]]); m.zero(); // m = Matrix([[0,0,0],[0,0,0],[0,0,0]])
<static>
{Matrix}
Matrix.Zeros(nRows, nCols)
Creates a matrix of given size full of zeros
m = Matrix.Zeros(2,3); // m = Matrix([[0,0,0],[0,0,0]])
- Parameters:
- {int} nRows Optional, Default: 0
- number of rows
- {int} nCols Optional, Default: 0
- number of columns
- Returns:
- {Matrix} new matrix full of zero