Skip to content

Math.js: Matrices

JS Calc offers extensive support for matrix operations through Math.js. You can create, manipulate, and perform a wide range of calculations with matrices.

Creating Matrices

Matrices are defined using square brackets []. * Elements in a row are separated by commas ,. * Rows are separated by semicolons ;.

Examples:

  1. A 2x2 matrix:
> A = [1, 2; 3, 4]
    [[1, 2], [3, 4]]

(Assigning a matrix to a variable will show the matrix as the result if not part of a larger expression, and it will update @.)

  1. A row vector (1x3 matrix):
> rowVec = [5, 6, 7]
 [5, 6, 7]

(Math.js often treats 1D arrays as matrices with one row or one column depending on context.)

  1. A column vector (3x1 matrix):
> colVec = [8; 9; 10]
 [[8], [9], [10]]

Helper Functions for Matrix Creation

Math.js also provides functions to create common matrices:

  • eye(n) or identity(n): Creates an n x n identity matrix.
> I = identity(3)
 [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
  • zeros(m, n): Creates an m x n matrix of zeros.
> Z = zeros(2, 3)
 [[0, 0, 0], [0, 0, 0]]
  • ones(m, n): Creates an m x n matrix of ones.
> O = ones(2) // Creates a 2x2 matrix of ones
 [[1, 1], [1, 1]]
  • diag(array): Creates a diagonal matrix from an array.
> D = diag([1, 2, 3])
 [[1, 0, 0], [0, 2, 0], [0, 0, 3]]

Accessing Matrix Elements

You can access elements using 1-based indexing (unlike JavaScript's 0-based arrays): matrixName(row, col)

> M = [10, 20; 30, 40]
> M(1, 2) // Element in 1st row, 2nd column
 20
> M(2, 1)
 30

Matrix Operations

Arithmetic

  • Addition/Subtraction: Performed element-wise. Matrices must have the same dimensions.
> A = [1,2;3,4]; B = [5,6;7,8]
> A + B
 [[6, 8], [10, 12]]
  • Scalar Multiplication/Division: Multiplies/divides each element by the scalar.
> A = [1,2;3,4]
> A * 3
 [[3, 6], [9, 12]]

> A / 2
 [[0.5, 1], [1.5, 2]]
  • Element-wise Operations (Dot Operations):
    • A .* B (Element-wise multiplication)
    • A ./ B (Element-wise division)
    • A .^ p (Element-wise power)
> A = [1,2;3,4]; B = [2,2;3,3]
> A .* B
 [[2, 4], [9, 12]]

> A .^ 2
 [[1, 4], [9, 16]]
  • Matrix Multiplication: Standard matrix multiplication (* operator). The number of columns in the first matrix must equal the number of rows in the second.
> A = [1,2;3,4]; B = [5,6;7,8]
> A * B
 [[19, 22], [43, 50]]

Common Matrix Functions

  • det(A): Calculates the determinant of a square matrix.
> M = [3, 1; 2, 5]
> det(M)
 13
  • inv(A): Computes the inverse of a square, invertible matrix.
> A = [1,2;3,5] // det(A) = 5-6 = -1
> inv(A)
 [[-5, 2], [3, -1]]
  • transpose(A) or A' : Transposes a matrix (swaps rows and columns).
> M = [1,2,3;4,5,6]
> transpose(M)
 [[1, 4], [2, 5], [3, 6]]

> M'
 [[1, 4], [2, 5], [3, 6]]
  • size(A): Returns the dimensions of a matrix.
> M = zeros(3,4)
> size(M)
 [3, 4]
  • trace(A): Sum of the diagonal elements of a square matrix.
  • multiply(A, B, ...): Multiplies matrices or scalars.
  • add(A, B, ...): Adds matrices or scalars element-wise.
  • ...and many more (e.g., kron, expm, sqrtm, solving linear systems like lusolve(A, b)).

Persistence

Matrices assigned to variables are saved and will be available in subsequent sessions until the calculator state is cleared.

For more advanced matrix functions and details, consult the Math.js Matrix Documentation.