Element-wise operations

Every element-wise operator comes in two forms:

  • Instance (in-place): m.add(value) mutates m and returns this.
  • Static (new matrix): Matrix.add(m, value) returns a new matrix.

value may be a scalar (number) or another matrix / 2D array of the same shape. With a matrix operand, shapes must match (else RangeError).

import { Matrix } from "@teakit/matrix";

const a = new Matrix([[1, 1], [2, 2]]);
const b = new Matrix([[3, 3], [1, 1]]);

Matrix.add(a, b); // new: [[4, 4], [3, 3]]
Matrix.sub(a, b); // new
Matrix.mul(a, 10); // new: scalar multiply
Matrix.div(a, 10); // new
Matrix.mod(b, 2); // new

a.add(b); // in place: a becomes a + b
a.mul(10); // in place

Arithmetic

add, sub (alias subtract), mul (alias multiply), div (alias divide), mod (alias modulus), pow.

pow accepts a scalar or a same-shape matrix (element-wise exponent). For the matrix power Aⁿ use mpow (see linear-algebra).

Bitwise

and, or, xor, leftShift, signPropagatingRightShift, rightShift (alias zeroFillRightShift), not.

Math functions (unary)

Apply the corresponding Math.* function to every element:

abs, acos, acosh, asin, asinh, atan, atanh, cbrt, ceil, clz32, cos, cosh,
exp, expm1, floor, fround, log, log1p, log10, log2, round, sign, sin, sinh,
sqrt, tan, tanh, trunc
Matrix.abs(a); // new matrix of |aᵢⱼ|
a.sqrt(); // in place
Matrix.cos(a); // new

Negate, fill, scale-by-vector

m.neg(); // alias: negate — in place, ×(-1)
m.fill(0); // in place
m.addRowVector(vec); // also sub/mul/div RowVector and ColumnVector
m.mulRow(i, value); // scale one row / column in place
m.mulColumn(j, value);