Matrix
Matrix computation on JSON arrays of rows: create, add, multiply, transpose and invert matrices, determinants, decompositions and linear systems. Typical uses: solve a system of equations, scientific and statistical calculations.
matrix addmatrix colmatrix detmatrix diagmatrix getmatrix identitymatrix inversematrix mulmatrix onesmatrix randommatrix rowmatrix scalar_mulmatrix setmatrix sizematrix solvematrix submatrix tracematrix transposematrix zerosmatrix choleskymatrix condmatrix eigenmatrix lumatrix normmatrix norm_frobeniusmatrix pinvmatrix powmatrix qrmatrix rankmatrix rrefmatrix svd

matrix add <json_matrix> <json_matrix2>


Description

    Add two matrices of the same size

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
    json_matrix2:   A matrix: JSON array of rows - String - required
admin
matrix add "[[1, 2], [3, 4]]" "[[5, 6], [7, 8]]"
mentdb
[[6,8],[10,12]]
3 x 3
admin
matrix add "[[1, 0, 2], [0, 1, 0], [2, 0, 1]]" "[[1, 1, 1], [1, 1, 1], [1, 1, 1]]"
mentdb
[[2,1,3],[1,2,1],[3,1,2]]
Negative values
admin
matrix add "[[1, -1], [0, 2]]" "[[-1, 1], [0, -2]]"
mentdb
[[0,0],[0,0]]
Different sizes: error
admin
matrix add "[[1, 2]]" "[[1], [2]]"
mentdb
Sorry, an error appears (the matrices must have the same size (1x2 and 2x1)).
Used in use cases (3 recipes)

    Control engineer › State feedback by pole placement (Ackermann)
    Data scientist › Least squares by QR decomposition, then ridge regression
    Control engineer › Lyapunov stability by solving At P + P A = -Q
Often used with

    matrix mul  ·  matrix det  ·  matrix identity  ·  matrix inverse  ·  matrix transpose  ·  cas expand  ·  json load select  ·  matrix cholesky

matrix col <json_matrix> <j>


Description

    Get a column of a matrix (index starts at 1), as a vector

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
    j:   The column (1 to n) - Number - required
admin
matrix col "[[1, 2], [3, 4]]" 2
mentdb
[2,4]
Third column
admin
matrix col "[[1, 2, 3], [4, 5, 6]]" 3
mentdb
[3,6]
First column
admin
matrix col "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]" 1
mentdb
[1,4,7]
Index out of the matrix: error
admin
matrix col "[[1, 2], [3, 4]]" 3
mentdb
Sorry, an error appears (the column 3 is out of the matrix (1 to 2)).
Used in use cases (16 recipes)

    Acoustics engineer › Spectrum and THD of a tone
    Mathematical physicist › Legendre polynomials from a QR decomposition
    Crystallographer & materials physicist › Lattice parameters from an orientation matrix by QR
    Chemist › Equivalence point of a weak acid titration
    Electrochemist & battery engineer › OCV-SOC curve of a Li-ion cell
    Process & reactor engineer › Residence time distribution and tanks-in-series model
    and 10 more in the Use cases
Often used with

    num table  ·  dsp peaks  ·  dsp periodogram  ·  dsp spectrum  ·  json load select  ·  matrix get  ·  matrix qr  ·  num fft

matrix det <json_matrix>


Description

    Get the determinant of a square matrix (an integer matrix gives an integer determinant)

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix det "[[1, 2], [3, 4]]"
mentdb
-2
admin
matrix det "[[2, -3, 1], [2, 0, -1], [1, 4, 5]]"
mentdb
49
Singular matrix: 0
admin
matrix det "[[1, 2], [2, 4]]"
mentdb
0
Identity: 1
admin
matrix det (matrix identity 4)
mentdb
1
Triangular matrix: product of the diagonal
admin
matrix det "[[2, 5, 1], [0, 3, 7], [0, 0, 4]]"
mentdb
24
Not square: error
admin
matrix det "[[1, 2, 3], [4, 5, 6]]"
mentdb
Sorry, an error appears (the matrix must be square (2x3)).
Used in use cases (15 recipes)

    Mathematician › Spectrum and invariants of a symmetric matrix
    Cryptographer & number theorist › Hill cipher with a 2 x 2 key matrix
    Operations researcher › Leontief input-output model of three sectors
    Crystallographer & materials physicist › Miller indices, d-spacing and Bragg angle in a monoclinic cell
    Medical physicist › Beam quality (HVL) and dual-energy separation
    Space mission engineer › Attitude matrix, Euler angle and inertia tensor
    and 9 more in the Use cases
Often used with

    matrix mul  ·  matrix get  ·  matrix identity  ·  matrix inverse  ·  json load select  ·  matrix sub  ·  matrix trace  ·  matrix transpose

matrix diag <json_vector>


Description

    Create a diagonal matrix from a vector

Parameters

    json_vector:   A vector: JSON array of numbers, ex: [1, 2, 3] - String - required
admin
matrix diag "[1, 2, 3]"
mentdb
[[1,0,0],[0,2,0],[0,0,3]]
Diagonal matrix of 4 values
admin
matrix diag "[2, 3, 5, 7]"
mentdb
[[2,0,0,0],[0,3,0,0],[0,0,5,0],[0,0,0,7]]
Diagonal with zeros
admin
matrix diag "[1, 0, -1]"
mentdb
[[1,0,0],[0,0,0],[0,0,-1]]
One value
admin
matrix diag "[5]"
mentdb
[[5]]
Used in use cases (9 recipes)

    Advanced physical chemist › Consecutive reactions A to B to C by eigen-decomposition
    Epidemiologist › Direct age standardization of mortality
    Space mission engineer › Attitude matrix, Euler angle and inertia tensor
    Geologist & seismologist › Decomposition of an earthquake moment tensor
    Control engineer › State transition matrix by modal decomposition
    Robotics engineer › Re-orthonormalize a drifting rotation matrix with QR
    and 3 more in the Use cases
Often used with

    matrix mul  ·  matrix transpose  ·  matrix eigen  ·  matrix get  ·  matrix inverse  ·  matrix norm_frobenius  ·  matrix sub  ·  matrix trace

matrix get <json_matrix> <i> <j>


Description

    Get a value of a matrix (indexes start at 1)

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
    i:   The row (1 to n) - Number - required
    j:   The column (1 to n) - Number - required
admin
matrix get "[[1, 2], [3, 4]]" 2 1
mentdb
3
Element (1, 3)
admin
matrix get "[[1, 2, 3], [4, 5, 6]]" 1 3
mentdb
3
The indices start at 1
admin
matrix get "[[1, 2], [3, 4]]" 1 1
mentdb
1
Out of the matrix: error
admin
matrix get "[[1, 2], [3, 4]]" 3 1
mentdb
Sorry, an error appears (the row 3 is out of the matrix (1 to 2)).
Used in use cases (26 recipes)

    Algorithms in MQL › Matrix product: triple loop against matrix mul
    Cryptographer & number theorist › Hill cipher with a 2 x 2 key matrix
    Operations researcher › Availability of a machine with a Markov chain
    Mathematical physicist › Legendre polynomials from a QR decomposition
    Crystallographer & materials physicist › Elastic tensor of silicon: stability and moduli
    Chemist › Balancing a redox equation with linear algebra
    and 20 more in the Use cases
Often used with

    matrix mul  ·  matrix inverse  ·  matrix solve  ·  matrix det  ·  matrix transpose  ·  json load select  ·  math sin  ·  matrix lu

matrix identity <n>


Description

    Create the identity matrix n x n

Parameters

    n:   The size - Number - required
admin
matrix identity 3
mentdb
[[1,0,0],[0,1,0],[0,0,1]]
2 x 2
admin
matrix identity 2
mentdb
[[1,0],[0,1]]
Size 4
admin
matrix identity 4
mentdb
[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
Size 1
admin
matrix identity 1
mentdb
[[1]]
Used in use cases (6 recipes)

    Operations researcher › Availability of a machine with a Markov chain
    Control engineer › State feedback by pole placement (Ackermann)
    Robotics engineer › Rotation matrix of a tool from ZYX Euler angles
    Operations researcher › Leontief input-output model of three sectors
    Control engineer › Lyapunov stability by solving At P + P A = -Q
    Robotics engineer › Re-orthonormalize a drifting rotation matrix with QR
Often used with

    matrix mul  ·  matrix det  ·  matrix sub  ·  matrix get  ·  matrix transpose  ·  matrix inverse  ·  matrix norm_frobenius  ·  matrix row

matrix inverse <json_matrix>


Description

    Get the inverse of a square matrix (error if the matrix is singular)

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix inverse "[[1, 2], [3, 4]]"
mentdb
[[-2,1],[1.5,-0.5]]
admin
matrix inverse "[[1, 2, 3], [0, 1, 4], [5, 6, 0]]"
mentdb
[[-24,18,5],[20,-15,-4],[-5,4,1]]
3 x 3
admin
matrix inverse "[[2, 0, 0], [0, 4, 0], [0, 0, 8]]"
mentdb
[[0.5,0,0],[0,0.25,0],[0,0,0.125]]
Check: A * inverse(A) = I
admin
matrix mul "[[1, 2], [3, 4]]" (matrix inverse "[[1, 2], [3, 4]]")
mentdb
[[1,0],[0,1]]
Rotation of 90 degrees: the inverse is the transpose
admin
matrix inverse "[[0, -1], [1, 0]]"
mentdb
[[0,1],[-1,0]]
Singular matrix: error
admin
matrix inverse "[[1, 2], [2, 4]]"
mentdb
Sorry, an error appears (the matrix is singular (determinant = 0)).
Used in use cases (13 recipes)

    Operations researcher › Availability of a machine with a Markov chain
    Mathematical physicist › Legendre polynomials from a QR decomposition
    Crystallographer & materials physicist › Miller indices, d-spacing and Bragg angle in a monoclinic cell
    Advanced physical chemist › Consecutive reactions A to B to C by eigen-decomposition
    Materials scientist › Cross-ply laminate stiffness by classical lamination theory
    Control engineer › State feedback by pole placement (Ackermann)
    and 7 more in the Use cases
Often used with

    matrix mul  ·  matrix get  ·  matrix det  ·  matrix row  ·  matrix eigen  ·  matrix lu  ·  matrix transpose  ·  vector dot

matrix mul <json_matrix> <json_matrix_or_vector>


Description

    Multiply two matrices, or a matrix by a vector (column): the result is then a vector

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
    json_matrix_or_vector:   A matrix or a vector - String - required
admin
matrix mul "[[1, 2], [3, 4]]" "[[5, 6], [7, 8]]"
mentdb
[[19,22],[43,50]]
admin
matrix mul "[[1, 2], [3, 4]]" "[1, 1]"
mentdb
[3,7]
Rotation of 90 degrees of the vector (1, 0)
admin
matrix mul "[[0, -1], [1, 0]]" "[1, 0]"
mentdb
[0,1]
Rectangular: (2 x 3) x (3 x 2)
admin
matrix mul "[[1, 2, 3], [4, 5, 6]]" "[[1, 0], [0, 1], [1, 1]]"
mentdb
[[4,5],[10,11]]
Rotation of a point by 90 degrees
admin
matrix mul "[[0, -1], [1, 0]]" "[2, 1]"
mentdb
[-1,2]
Wrong sizes: error
admin
matrix mul "[[1, 2], [3, 4]]" "[1, 2, 3]"
mentdb
Sorry, an error appears (the number of columns of the matrix (2) must be equal to the size of the vector (3)).
Used in use cases (31 recipes)

    Algorithms in MQL › Matrix product: triple loop against matrix mul
    Cryptographer & number theorist › Hill cipher with a 2 x 2 key matrix
    Operations researcher › Availability of a machine with a Markov chain
    Crystallographer & materials physicist › Miller indices, d-spacing and Bragg angle in a monoclinic cell
    Chemist › Two dyes in one drink by least squares (QR)
    Advanced physical chemist › Consecutive reactions A to B to C by eigen-decomposition
    and 25 more in the Use cases
Often used with

    matrix transpose  ·  matrix get  ·  matrix solve  ·  matrix det  ·  matrix inverse  ·  matrix qr  ·  matrix diag  ·  matrix sub

matrix ones <rows> <cols>


Description

    Create a matrix filled with 1

Parameters

    rows:   The number of rows - Number - required
    cols:   The number of columns - Number - required
admin
matrix ones 2 2
mentdb
[[1,1],[1,1]]
3 x 1
admin
matrix ones 3 1
mentdb
[[1],[1],[1]]
Row vector
admin
matrix ones 1 3
mentdb
[[1,1,1]]
Matrix of 2 x 3
admin
matrix ones 2 3
mentdb
[[1,1,1],[1,1,1]]
Used in use cases (3 recipes)

    Agronomist › Growing degree days and frost risk for maize
    Data scientist › Glorot uniform initialisation of a dense layer
    Deep learning engineer › Xavier initialisation of a dense layer
Often used with

    math abs  ·  math sqrt  ·  matrix random  ·  matrix scalar_mul  ·  matrix size  ·  matrix sub  ·  stat max  ·  stat mean

matrix random <rows> <cols>


Description

    Create a matrix of random values in [0, 1[

Parameters

    rows:   The number of rows - Number - required
    cols:   The number of columns - Number - required
admin
matrix random 2 2
mentdb
[[0.7038372016186624,0.11553578291392803],[0.2846512312577406,0.9122283401418925]]
3 x 2 (the result changes each time)
admin
matrix random 3 2
mentdb
[[0.7336323096664192,0.9588240596867678],[0.40548742962834683,0.443526902901052],[0.1160726913934782,0.18012846601976895]]
Determinant of a random matrix (the result changes each time)
admin
matrix det (matrix random 3 3)
mentdb
-0.23265845744408647
Used in use cases (2 recipes)

    Data scientist › Glorot uniform initialisation of a dense layer
    Deep learning engineer › Xavier initialisation of a dense layer
Often used with

    math abs  ·  math sqrt  ·  matrix ones  ·  matrix scalar_mul  ·  matrix size  ·  matrix sub  ·  stat max  ·  stat min

matrix row <json_matrix> <i>


Description

    Get a row of a matrix (index starts at 1), as a vector

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
    i:   The row (1 to n) - Number - required
admin
matrix row "[[1, 2], [3, 4]]" 2
mentdb
[3,4]
First row
admin
matrix row "[[1, 2, 3], [4, 5, 6]]" 1
mentdb
[1,2,3]
Last row
admin
matrix row "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]" 3
mentdb
[7,8,9]
Out of the matrix: error
admin
matrix row "[[1, 2], [3, 4]]" 5
mentdb
Sorry, an error appears (the row 5 is out of the matrix (1 to 2)).
Used in use cases (9 recipes)

    Operations researcher › Availability of a machine with a Markov chain
    Mathematical physicist › Legendre polynomials from a QR decomposition
    Crystallographer & materials physicist › Twinning in titanium: the 10-12 twin
    Epidemiologist › Direct age standardization of mortality
    Agronomist › Growing degree days and frost risk for maize
    Control engineer › State feedback by pole placement (Ackermann)
    and 3 more in the Use cases
Often used with

    matrix mul  ·  matrix get  ·  matrix inverse  ·  json load select  ·  matrix solve  ·  matrix identity  ·  matrix qr  ·  matrix sub

matrix scalar_mul <json_matrix> <k>


Description

    Multiply a matrix by a number

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
    k:   The number - Number - required
admin
matrix scalar_mul "[[1, 2], [3, 4]]" 2.5
mentdb
[[2.5,5],[7.5,10]]
Opposite matrix
admin
matrix scalar_mul "[[1, -2], [3, 0]]" -1
mentdb
[[-1,2],[-3,0]]
Divide by 2
admin
matrix scalar_mul "[[2, 4], [6, 8]]" 0.5
mentdb
[[1,2],[3,4]]
By 0
admin
matrix scalar_mul "[[1, 2], [3, 4]]" 0
mentdb
[[0,0],[0,0]]
Used in use cases (6 recipes)

    Cryptographer & number theorist › Hill cipher with a 2 x 2 key matrix
    Materials scientist › Cross-ply laminate stiffness by classical lamination theory
    Control engineer › State feedback by pole placement (Ackermann)
    Data scientist › Glorot uniform initialisation of a dense layer
    Deep learning engineer › Scaled dot-product attention of a transformer
    Deep learning engineer › Xavier initialisation of a dense layer
Often used with

    matrix mul  ·  math sqrt  ·  matrix get  ·  matrix sub  ·  stat max  ·  math abs  ·  matrix det  ·  matrix inverse

matrix set <json_matrix> <i> <j> <value>


Description

    Set a value of a matrix (indexes start at 1): returns the new matrix

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
    i:   The row (1 to n) - Number - required
    j:   The column (1 to n) - Number - required
    value:   The new value - Number - required
admin
matrix set "[[1, 2], [3, 4]]" 1 2 9
mentdb
[[1,9],[3,4]]
Change the element (2, 2)
admin
matrix set (matrix identity 2) 2 2 5
mentdb
[[1,0],[0,5]]
Change a value
admin
matrix set "[[1, 2, 3], [4, 5, 6]]" 2 3 0
mentdb
[[1,2,3],[4,5,0]]
Out of the matrix: error
admin
matrix set "[[1, 2], [3, 4]]" 3 3 1
mentdb
Sorry, an error appears (the row 3 is out of the matrix (1 to 2)).
Used in use cases (2 recipes)

    Operations researcher › Availability of a machine with a Markov chain
    Mathematical physicist › Tight-binding chain and Chebyshev polynomials
Often used with

    json load select  ·  math pow  ·  matrix eigen  ·  matrix get  ·  matrix identity  ·  matrix inverse  ·  matrix mul  ·  matrix norm_frobenius

matrix size <json_matrix>


Description

    Get the size of a matrix [rows, columns]

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix size "[[1, 2, 3], [4, 5, 6]]"
mentdb
[2,3]
Column vector
admin
matrix size "[[1], [2], [3]]"
mentdb
[3,1]
Square matrix
admin
matrix size (matrix identity 4)
mentdb
[4,4]
One row
admin
matrix size "[[1, 2, 3, 4]]"
mentdb
[1,4]
Used in use cases (3 recipes)

    Mathematical physicist › Tight-binding chain and Chebyshev polynomials
    Data scientist › Glorot uniform initialisation of a dense layer
    Deep learning engineer › Xavier initialisation of a dense layer
Often used with

    math abs  ·  math sqrt  ·  matrix ones  ·  matrix random  ·  matrix scalar_mul  ·  matrix sub  ·  stat max  ·  stat min

matrix solve <json_matrix_a> <json_b>


Description

    Solve the linear system A.X = B.
- A square: exact solution (LU), error if A is singular.
- A with more rows than columns: least squares solution (QR).
B can be a vector (the result is a vector) or a matrix (several systems at once).

Parameters

    json_matrix_a:   The matrix A - String - required
    json_b:   The vector or the matrix B - String - required
admin
matrix solve "[[1, 1, 1], [0, 2, 5], [2, 5, -1]]" "[6, -4, 27]"
mentdb
[5,3,-2]
admin
matrix solve "[[2, 1], [1, 3]]" "[3, 5]"
mentdb
[0.8,1.4]
Electrical circuit (Kirchhoff): 3 currents
admin
matrix solve "[[1, -1, -1], [10, 5, 0], [0, 5, -20]]" "[0, 12, 0]"
mentdb
[0.8571428571428571,0.6857142857142858,0.17142857142857146]
2 equations: 2x + y = 5, x - y = 1
admin
matrix solve "[[2, 1], [1, -1]]" "[5, 1]"
mentdb
[2,1]
Mixture: 2 products at 10 % and 30 % to make 100 L at 25 %
admin
matrix solve "[[1, 1], [0.10, 0.30]]" "[100, 25]"
mentdb
[25,75]
Singular system: error
admin
matrix solve "[[1, 2], [2, 4]]" "[3, 6]"
mentdb
Sorry, an error appears (the matrix is singular: no solution or an infinity of solutions).
Worked example: Linear algebra: the currents of an electrical network (Kirchhoff laws for a network with 3 loops give a linear system A I = b (resistances in ohm, voltages in V). matrix solve gives the currents; the determinant and the condition number tell if the system is well posed.)
admin
#Worked example: Linear algebra: the currents of an electrical network; #The matrix of the system; -> "[A]" "[[10, -2, -3], [-2, 8, -1], [-3, -1, 12]]"; -> "[b]" "[12, 0, 5]"; matrix det [A]; #Condition number (close to 1: well conditioned); matrix cond [A]; #The currents I1, I2, I3 in A; matrix solve [A] [b]; #Check: A I gives b again; matrix mul [A] (matrix solve [A] [b]); #The same with the inverse matrix; matrix mul (matrix inverse [A]) [b]; #The matrix is symmetric positive definite: Cholesky; matrix cholesky [A]; #Eigenvalues (all positive); matrix eigen [A];
mentdb
{"values": [14.185954093606897,10.11118747624417,5.702858430148934], "vectors": [[-0.6018828494372924,0.06594106779198469,0.7958572806299498],[-0.5269374463893727,0.7160473362423064,-0.45783527589401024],[0.60006163278941,0.694930203573532,0.39622979319500545]]}
Used in use cases (16 recipes)

    Operations researcher › Availability of a machine with a Markov chain
    Metrologist › Calibration curve of a Pt100 sensor
    Crystallographer & materials physicist › Crystallite size and microstrain: Williamson-Hall plot
    Chemist › Two dyes in one drink by least squares (QR)
    Process & reactor engineer › Mass balance of a two-column distillation train
    Geneticist & bioinformatician › Heritability by parent-offspring regression (QR)
    and 10 more in the Use cases
Often used with

    matrix mul  ·  matrix get  ·  matrix transpose  ·  matrix qr  ·  matrix lu  ·  matrix row  ·  atom get  ·  matrix cond

matrix sub <json_matrix> <json_matrix2>


Description

    Subtract two matrices of the same size

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
    json_matrix2:   A matrix: JSON array of rows - String - required
admin
matrix sub "[[1, 2], [3, 4]]" "[[5, 6], [7, 8]]"
mentdb
[[-4,-4],[-4,-4]]
A - A = 0
admin
matrix sub "[[1, 2], [3, 4]]" "[[1, 2], [3, 4]]"
mentdb
[[0,0],[0,0]]
Difference with the identity
admin
matrix sub "[[2, 1], [1, 2]]" (matrix identity 2)
mentdb
[[1,1],[1,1]]
Different sizes: error
admin
matrix sub "[[1, 2]]" "[[1, 2], [3, 4]]"
mentdb
Sorry, an error appears (the matrices must have the same size (1x2 and 2x2)).
Used in use cases (11 recipes)

    Algorithms in MQL › Matrix product: triple loop against matrix mul
    Operations researcher › Availability of a machine with a Markov chain
    Geologist & seismologist › Decomposition of an earthquake moment tensor
    Control engineer › State feedback by pole placement (Ackermann)
    Robotics engineer › Rotation matrix of a tool from ZYX Euler angles
    Data scientist › Glorot uniform initialisation of a dense layer
    and 5 more in the Use cases
Often used with

    matrix mul  ·  matrix get  ·  matrix identity  ·  matrix norm_frobenius  ·  matrix det  ·  matrix transpose  ·  math abs  ·  math round_sig

matrix trace <json_matrix>


Description

    Get the trace (sum of the diagonal) of a square matrix

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix trace "[[1, 2], [3, 4]]"
mentdb
5
3 x 3
admin
matrix trace "[[2, 0, 0], [0, 3, 4], [0, 4, 9]]"
mentdb
14
Trace of the identity = n
admin
matrix trace (matrix identity 5)
mentdb
5
Trace = sum of the eigenvalues
admin
matrix trace "[[2, -1], [-1, 2]]"
mentdb
4
Used in use cases (10 recipes)

    Algorithms in MQL › Matrix product: triple loop against matrix mul
    Mathematician › Spectrum and invariants of a symmetric matrix
    Mathematical physicist › Tight-binding chain and Chebyshev polynomials
    Crystallographer & materials physicist › Elastic tensor of silicon: stability and moduli
    Space mission engineer › Attitude matrix, Euler angle and inertia tensor
    Geologist & seismologist › Decomposition of an earthquake moment tensor
    and 4 more in the Use cases
Often used with

    matrix eigen  ·  matrix get  ·  json load select  ·  matrix det  ·  matrix mul  ·  math acos  ·  matrix diag  ·  matrix inverse

matrix transpose <json_matrix>


Description

    Get the transpose of a matrix

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix transpose "[[1, 2, 3], [4, 5, 6]]"
mentdb
[[1,4],[2,5],[3,6]]
Column vector to row
admin
matrix transpose "[[1], [2], [3]]"
mentdb
[[1,2,3]]
Symmetric matrix: unchanged
admin
matrix transpose "[[1, 2], [2, 3]]"
mentdb
[[1,2],[2,3]]
Row to column
admin
matrix transpose "[[1, 2, 3]]"
mentdb
[[1],[2],[3]]
Used in use cases (18 recipes)

    Operations researcher › Availability of a machine with a Markov chain
    Chemist › Two dyes in one drink by least squares (QR)
    Advanced physical chemist › Consecutive reactions A to B to C by eigen-decomposition
    Electrochemist & battery engineer › Diffusion coefficient from a Cottrell plot
    Geneticist & bioinformatician › Heritability by parent-offspring regression (QR)
    Space mission engineer › Attitude matrix, Euler angle and inertia tensor
    and 12 more in the Use cases
Often used with

    matrix mul  ·  matrix qr  ·  matrix solve  ·  matrix get  ·  matrix diag  ·  matrix det  ·  matrix identity  ·  matrix inverse

matrix zeros <rows> <cols>


Description

    Create a matrix filled with 0

Parameters

    rows:   The number of rows - Number - required
    cols:   The number of columns - Number - required
admin
matrix zeros 2 3
mentdb
[[0,0,0],[0,0,0]]
1 x 4
admin
matrix zeros 1 4
mentdb
[[0,0,0,0]]
Square 3 x 3
admin
matrix zeros 3 3
mentdb
[[0,0,0],[0,0,0],[0,0,0]]
Column
admin
matrix zeros 3 1
mentdb
[[0],[0],[0]]
Used in use cases (1 recipe)

    Mathematical physicist › Tight-binding chain and Chebyshev polynomials
Often used with

    json load select  ·  math pow  ·  matrix eigen  ·  matrix norm_frobenius  ·  matrix set  ·  matrix size  ·  matrix trace  ·  num solve

matrix cholesky <json_matrix>


Description

    Get the Cholesky decomposition A = L.Lt of a symmetric positive definite matrix.
Returns {L}.

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix cholesky "[[4, 1], [1, 3]]"
mentdb
{"L": [[2,0],[0.5,1.6583123951777]]}
3 x 3 symmetric positive
admin
matrix cholesky "[[4, 12, -16], [12, 37, -43], [-16, -43, 98]]"
mentdb
{"L": [[2,0,0],[6,1,0],[-8,5,3]]}
Identity
admin
matrix cholesky (matrix identity 2)
mentdb
{"L": [[1,0],[0,1]]}
Covariance matrix
admin
matrix cholesky "[[2, 0.6], [0.6, 1]]"
mentdb
{"L": [[1.4142135623730951,0],[0.42426406871192845,0.9055385138137417]]}
Not positive definite: error
admin
matrix cholesky "[[1, 2], [2, 1]]"
mentdb
Sorry, an error appears (the matrix is not positive definite).
Used in use cases (2 recipes)

    Crystallographer & materials physicist › Elastic tensor of silicon: stability and moduli
    Control engineer › Lyapunov stability by solving At P + P A = -Q
Often used with

    json load select  ·  matrix add  ·  matrix cond  ·  matrix det  ·  matrix eigen  ·  matrix get  ·  matrix identity  ·  matrix inverse

matrix cond <json_matrix>


Description

    Get the condition number (2-norm: largest / smallest singular value). Infinity for a singular matrix.

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix cond "[[1, 2], [3, 4]]"
mentdb
14.93303437365927
Ill-conditioned matrix (Hilbert 3 x 3)
admin
matrix cond "[[1, 0.5, 0.3333333333333333], [0.5, 0.3333333333333333, 0.25], [0.3333333333333333, 0.25, 0.2]]"
mentdb
524.0567775860604
Identity: 1
admin
matrix cond (matrix identity 3)
mentdb
1
Almost singular: very large
admin
matrix cond "[[1, 1], [1, 1.0001]]"
mentdb
40002.00007491188
Used in use cases (5 recipes)

    Mathematician › Spectrum and invariants of a symmetric matrix
    Crystallographer & materials physicist › Elastic tensor of silicon: stability and moduli
    Process & reactor engineer › Mass balance of a two-column distillation train
    Medical physicist › Beam quality (HVL) and dual-energy separation
    Machine learning engineer › Linear regression solved by QR decomposition
Often used with

    json load select  ·  matrix solve  ·  matrix det  ·  matrix eigen  ·  matrix get  ·  matrix trace  ·  stat sum  ·  math round_sig

matrix eigen <json_matrix>


Description

    Get the eigenvalues and the eigenvectors of a square matrix.
Returns {values, vectors}: values sorted by decreasing real part; vectors = one unit vector per row (in the order of the values), its largest component positive.
When some eigenvalues are complex, they are given in the format a+bi and vectors is null.

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix eigen "[[2, 0, 0], [0, 3, 4], [0, 4, 9]]"
mentdb
{"values": [11,2,1], "vectors": [[0,0.4472135954999579,0.8944271909999159],[1,0,0],[0,0.8944271909999159,-0.4472135954999579]]}
admin
matrix eigen "[[0, -1], [1, 0]]"
mentdb
{"values": ["i","-i"], "vectors": null}
Symmetric 2 x 2: normal modes of two coupled oscillators
admin
matrix eigen "[[2, -1], [-1, 2]]"
mentdb
{"values": [3,1], "vectors": [[0.7071067811865476,-0.7071067811865476],[0.7071067811865476,0.7071067811865476]]}
Diagonal matrix: the values of the diagonal
admin
matrix eigen "[[3, 0], [0, 5]]"
mentdb
{"values": [5,3], "vectors": [[0,1],[1,0]]}
Markov chain: stationary state (eigenvalue 1)
admin
matrix eigen "[[0.9, 0.5], [0.1, 0.5]]"
mentdb
{"values": [1,0.4], "vectors": [[0.9805806756909202,0.19611613513818404],[0.7071067811865476,-0.7071067811865476]]}
Inertia tensor
admin
matrix eigen "[[2, -1, 0], [-1, 2, -1], [0, -1, 2]]"
mentdb
{"values": [3.414213562373095,2,0.5857864376269051], "vectors": [[-0.5,0.7071067811865474,-0.5],[0.7071067811865477,0,-0.7071067811865474],[0.5,0.7071067811865475,0.5]]}
Used in use cases (9 recipes)

    Mathematician › Spectrum and invariants of a symmetric matrix
    Mathematical physicist › Tight-binding chain and Chebyshev polynomials
    Crystallographer & materials physicist › Elastic tensor of silicon: stability and moduli
    Advanced physical chemist › Huckel theory of butadiene: energies, bond orders and beta
    Geologist & seismologist › Decomposition of an earthquake moment tensor
    Control engineer › State feedback by pole placement (Ackermann)
    and 3 more in the Use cases
Often used with

    matrix trace  ·  json load select  ·  matrix diag  ·  matrix inverse  ·  matrix mul  ·  matrix get  ·  matrix norm_frobenius  ·  matrix sub

matrix lu <json_matrix>


Description

    Get the LU decomposition with partial pivoting: P.A = L.U (error if the matrix is singular).
Returns {L, U, P}.

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix lu "[[1, 2], [3, 4]]"
mentdb
{"L": [[1,0],[0.3333333333333333,1]], "U": [[3,4],[0,0.6666666666666667]], "P": [[0,1],[1,0]]}
3 x 3
admin
matrix lu "[[2, 1, 1], [4, -6, 0], [-2, 7, 2]]"
mentdb
{"L": [[1,0,0],[0.5,1,0],[-0.5,1,1]], "U": [[4,-6,0],[0,4,1],[0,0,1]], "P": [[0,1,0],[1,0,0],[0,0,1]]}
Identity
admin
matrix lu (matrix identity 2)
mentdb
{"L": [[1,0],[0,1]], "U": [[1,0],[0,1]], "P": [[1,0],[0,1]]}
Singular matrix: error
admin
matrix lu "[[1, 2], [2, 4]]"
mentdb
Sorry, an error appears (the matrix is singular: no LU decomposition).
Used in use cases (7 recipes)

    Operations researcher › Leontief input-output model of three sectors
    Materials scientist › Cross-ply laminate stiffness by classical lamination theory
    Medical physicist › Triple-isotope SPECT: crosstalk correction by LU
    Renewable energy engineer › DC power flow of a 4-bus grid with an LU factorisation
    Control engineer › Relative gain array of a distillation column
    Data scientist › Multivariate Gaussian log-likelihood with an LU decomposition
    and 1 more in the Use cases
Often used with

    matrix mul  ·  matrix get  ·  matrix inverse  ·  matrix solve  ·  matrix det  ·  math abs  ·  constant math_two_pi  ·  math exp

matrix norm <json_matrix>


Description

    Get the 2-norm (spectral norm: largest singular value)

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix norm "[[1, 2], [3, 4]]"
mentdb
5.464985704219043
Column 1-norm of 3 x 3
admin
matrix norm "[[1, -2, 3], [0, 4, -1], [2, 0, 1]]"
mentdb
5.229409617400718
Identity: 1
admin
matrix norm (matrix identity 3)
mentdb
1
Diagonal matrix
admin
matrix norm "[[3, 0], [0, -5]]"
mentdb
5
Used in use cases (1 recipe)

    Crystallographer & materials physicist › Elastic tensor of silicon: stability and moduli
Often used with

    json load select  ·  matrix cholesky  ·  matrix cond  ·  matrix eigen  ·  matrix get  ·  matrix inverse  ·  matrix rank  ·  matrix trace

matrix norm_frobenius <json_matrix>


Description

    Get the Frobenius norm (square root of the sum of the squares)

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix norm_frobenius "[[1, 2], [3, 4]]"
mentdb
5.477225575051661
Identity 3 x 3: sqrt(3)
admin
matrix norm_frobenius (matrix identity 3)
mentdb
1.7320508075688772
Diagonal matrix
admin
matrix norm_frobenius "[[3, 0], [0, 4]]"
mentdb
5
Distance between two matrices
admin
matrix norm_frobenius (matrix sub "[[1, 2], [3, 4]]" "[[1, 2], [3, 5]]")
mentdb
1
Used in use cases (7 recipes)

    Algorithms in MQL › Matrix product: triple loop against matrix mul
    Mathematical physicist › Tight-binding chain and Chebyshev polynomials
    Geologist & seismologist › Decomposition of an earthquake moment tensor
    Control engineer › Lyapunov stability by solving At P + P A = -Q
    Robotics engineer › Rotation matrix of a tool from ZYX Euler angles
    Machine learning engineer › PCA: how much variance does the first component keep?
    and 1 more in the Use cases
Often used with

    matrix mul  ·  matrix sub  ·  matrix transpose  ·  math round_sig  ·  matrix det  ·  matrix diag  ·  matrix eigen  ·  matrix identity

matrix pinv <json_matrix>


Description

    Get the Moore-Penrose pseudo-inverse (any size, singular matrices included)

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix pinv "[[1, 2], [2, 4]]"
mentdb
[[0.04,0.08],[0.08,0.16]]
Least squares: line through 3 points (pseudo-inverse times y)
admin
matrix mul (matrix pinv "[[1, 0], [1, 1], [1, 2]]") "[1, 2, 2]"
mentdb
[1.1666666666666665,0.5]
Invertible matrix: the inverse
admin
matrix pinv "[[1, 2], [3, 4]]"
mentdb
[[-2,1],[1.5,-0.5]]
Rectangular matrix
admin
matrix pinv "[[1, 2, 3]]"
mentdb
[[0.07142857142857141],[0.14285714285714288],[0.2142857142857143]]
Used in use cases (1 recipe)

    Robotics engineer › Forward kinematics and Jacobian of a planar 3R arm
Often used with

    matrix det  ·  matrix get  ·  matrix mul  ·  matrix svd

matrix pow <json_matrix> <n>


Description

    Get A^n (n integer; negative n: power of the inverse)

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
    n:   An integer - Number - required
admin
matrix pow "[[1, 2], [3, 4]]" 3
mentdb
[[37,54],[81,118]]
admin
matrix pow "[[1, 2], [3, 4]]" -1
mentdb
[[-2,1],[1.5,-0.5]]
Fibonacci numbers: [[1, 1], [1, 0]]^10
admin
matrix pow "[[1, 1], [1, 0]]" 10
mentdb
[[89,55],[55,34]]
Power 0: identity
admin
matrix pow "[[1, 2], [3, 4]]" 0
mentdb
[[1,0],[0,1]]
Markov chain after 10 steps
admin
matrix pow "[[0.9, 0.1], [0.5, 0.5]]" 10
mentdb
[[0.8333508096,0.1666491904],[0.833245952,0.166754048]]
Not square: error
admin
matrix pow "[[1, 2, 3]]" 2
mentdb
Sorry, an error appears (the matrix must be square (1x3)).
Used in use cases (3 recipes)

    Operations researcher › Availability of a machine with a Markov chain
    Crystallographer & materials physicist › Symmetry operations: rotation angle, order and the crystallographic restriction
    Control engineer › State feedback by pole placement (Ackermann)
Often used with

    matrix det  ·  matrix get  ·  matrix identity  ·  matrix inverse  ·  matrix mul  ·  matrix row  ·  matrix sub  ·  matrix trace

matrix qr <json_matrix>


Description

    Get the QR decomposition A = Q.R (Q orthogonal, R upper triangular, Householder method).
Returns {Q, R}.

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix qr "[[1, 2], [3, 4]]"
mentdb
{"Q": [[-0.3162277660168382,0.9486832980505142],[-0.9486832980505138,-0.3162277660168382]], "R": [[-3.1622776601683795,-4.427188724235732],[0,0.632455532033676]]}
3 x 2
admin
matrix qr "[[1, 1], [1, 2], [1, 3]]"
mentdb
{"Q": [[-0.577350269189626,0.7071067811865476,0.4082482904638629],[-0.5773502691896258,0,-0.816496580927726],[-0.5773502691896258,-0.7071067811865475,0.4082482904638631]], "R": [[-1.7320508075688772,-3.4641016151377544],[0,-1.4142135623730951],[0,0]]}
Square 3 x 3
admin
matrix qr "[[12, -51, 4], [6, 167, -68], [-4, 24, -41]]"
mentdb
{"Q": [[-0.857142857142857,0.3942857142857143,-0.3314285714285714],[-0.42857142857142855,-0.9028571428571428,0.03428571428571431],[0.2857142857142857,-0.17142857142857143,-0.9428571428571428]], "R": [[-14,-21,14],[0,-175,70],[0,0,35]]}
Used in use cases (10 recipes)

    Operations researcher › Trend and Q4 peak forecast by QR least squares
    Mathematical physicist › Legendre polynomials from a QR decomposition
    Crystallographer & materials physicist › Lattice parameters from an orientation matrix by QR
    Chemist › Two dyes in one drink by least squares (QR)
    Geneticist & bioinformatician › Heritability by parent-offspring regression (QR)
    Climate & environment scientist › Acceleration of the sea level rise by QR least squares
    and 4 more in the Use cases
Often used with

    matrix mul  ·  matrix transpose  ·  matrix solve  ·  json load select  ·  matrix get  ·  matrix row  ·  vector dot  ·  vector norm

matrix rank <json_matrix>


Description

    Get the rank (number of non negligible singular values)

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix rank "[[1, 2, 3], [2, 4, 6], [1, 0, 1]]"
mentdb
2
Full rank
admin
matrix rank "[[1, 0], [0, 1]]"
mentdb
2
Zero matrix: 0
admin
matrix rank (matrix zeros 2 2)
mentdb
0
Rectangular
admin
matrix rank "[[1, 2, 3, 4], [2, 4, 6, 8]]"
mentdb
1
Used in use cases (3 recipes)

    Crystallographer & materials physicist › Elastic tensor of silicon: stability and moduli
    Chemist › Balancing a redox equation with linear algebra
    Control engineer › State feedback by pole placement (Ackermann)
Often used with

    matrix get  ·  json load select  ·  matrix eigen  ·  matrix inverse  ·  matrix trace  ·  arith frac_mul  ·  arith lcm  ·  arith to_fraction

matrix rref <json_matrix>


Description

    Get the reduced row echelon form (Gauss-Jordan with partial pivoting). Solving a system: rref of the augmented matrix [A|b].

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix rref "[[1, 2, -1, -4], [2, 3, -1, -11], [-2, 0, -3, 22]]"
mentdb
[[1,0,0,-8],[0,1,0,1],[0,0,1,-2]]
Reduced row echelon form of a 2 x 3 system
admin
matrix rref "[[1, 2, 5], [3, 4, 6]]"
mentdb
[[1,0,-4],[0,1,4.5]]
Singular system: a row of zeros
admin
matrix rref "[[1, 2, 3], [2, 4, 6]]"
mentdb
[[1,2,3],[0,0,0]]
Inverse with the augmented matrix [A | I]
admin
matrix rref "[[2, 1, 1, 0], [1, 1, 0, 1]]"
mentdb
[[1,0,1,-1],[0,1,-1,2]]
Used in use cases (1 recipe)

    Chemist › Balancing a redox equation with linear algebra
Often used with

    arith frac_mul  ·  arith lcm  ·  arith to_fraction  ·  matrix get  ·  matrix rank  ·  string regexp_extract  ·  string split

matrix svd <json_matrix>


Description

    Get the singular value decomposition A = U.diag(S).Vt (compact form).
Returns {U, S, V}: S = the singular values in decreasing order.

Parameters

    json_matrix:   A matrix: JSON array of rows, ex: [[1, 2], [3, 4]] - String - required
admin
matrix svd "[[1, 2], [3, 4]]"
mentdb
{"U": [[0.4045535848337569,0.9145142956773045],[0.9145142956773045,-0.4045535848337569]], "S": [5.464985704219043,0.3659661906262574], "V": [[0.5760484367663208,-0.8174155604703632],[0.8174155604703632,0.5760484367663208]]}
Singular values of a 3 x 2 matrix
admin
matrix svd "[[3, 2], [2, 3], [2, -2]]"
mentdb
{"U": [[0.7071067811865475,-0.23570226039551587],[0.7071067811865477,0.235702260395516],[0,-0.9428090415820634]], "S": [5,3], "V": [[0.7071067811865475,-0.7071067811865475],[0.7071067811865475,0.7071067811865475]]}
Diagonal matrix
admin
matrix svd "[[3, 0], [0, 2]]"
mentdb
{"U": [[-1,0],[0,1]], "S": [3,2], "V": [[-1,0],[0,1]]}
Matrix of rank 1
admin
matrix svd "[[1, 2], [2, 4]]"
mentdb
{"U": [[-0.44721359549995787,-0.894427190999916],[-0.8944271909999159,0.44721359549995787]], "S": [5,0], "V": [[-0.447213595499958,-0.894427190999916],[-0.894427190999916,0.447213595499958]]}
Used in use cases (1 recipe)

    Robotics engineer › Forward kinematics and Jacobian of a planar 3R arm
Often used with

    matrix det  ·  matrix get  ·  matrix mul  ·  matrix pinv