Skip to content
Calculus_is_fun edited this page Jan 25, 2025 · 18 revisions

Matrix

Constructor

Columns

the number of columns this matrix will have.

indices

The values in the matrix in reading order of LTR. can be Numbers (goes through Math.floor()), BigInts, or Rationals. They all get converted to Rationals.

let A = new Matrix(2n, [1, 2n, new Rational(1n, 4n), 4]);

A =

If indices is the string "identity", creates an identity matrix of given size.

let A = new Matrix(3, "identity");

A =

Methods

most of these mutate this, exceptions will be noted

add

adds argument to this element-wise

let A = new Matrix(2n, [1n, 3n, 4n, new Rational(5n, 2n)]);
let B = new Matrix(2n, [new Rational(1n, 2n), 1n, 4n, -1n]);
A.add(B);

A =

addRow

adds a scaled copy of one row to another

arguments

  1. source (BigInt): the row to add, where 0 is the top row
  2. destination (BigInt): the row added to, where 0 is the top row
  3. scale (BigInt/Rational): a row multiplier before adding (defaults to 1)
let A = new Matrix(3n, [1n, 2n, 3n, 2n, 5n, 1n, 2n, 3n, 0n]);
A.addRow(0n, 1n, -2n);
A.addRow(0n, 2n, new Rational(1n, 3n));

A =

augment

appends the colums of argument to the right of this

let A = new Matrix(2n, [1n, 2n, 4n, 5n, 7n, 8n]);
let B = new Matrix(1n, [3n, 6n, 9n]);
A.augment(B);

A =

clone

does not mutate this

returns a copy of this, potential better than structuredClone

let A = new Matrix(2n, [1n, 1n, 1n, 0n]);
let clone = A.clone();
A.scale(2n);
clone.scale(new Rational(1n, 2n));

A =

clone =

dotProduct

does not mutate this

returns the dot product of this and the argument, which both must be column vectors

let A = new Matrix(1n, [1n, 2n, 3n]);
let B = new Matrix(1n, [3n, 6n, new Rational(3n, 5n)]);
console.log(A.dotProduct(B)); // 84/5

gaussianElimination

does Gaussian elimination to row echelon form

let A = new Matrix(4n, [1n, 0n, 4n, 2n, 1n, 2n, 6n, 2n, 2n, 0n, 8n, 8n, 2n, 1n, 9n, 4n]);
A.gaussianElimination();

A =

hadamardProduct

does the naïve product of element-wise multiplication

let A = new Matrix(2n, [1n, 2n, 3n, 4n]);
let B = new Matrix(2n, [1n, new Rational(1n, 3n), new Rational(1n, 9n), new Rational(1n, 27n)]);
A.hadamardProduct(B);

A =

inverse

finds the inverse of this

let A = new Matrix(3n, [4n, 6n, new Rational(4n, 3n), 2n, 5n, 9n, 3n, new Rational(1n, 2n), 4n]);
let I = A.clone();
I.inverse();
A.product(I);

A =

isSquare

this does not mutate this

returns true if this is square

let square = new Matrix(3n, [1n, 2n, 3n, 4n, 5n, 6n, 7n, 8n, 9n]);
let nonsquare = new Matrix(2n, [1n, 2n, 3n, 4n, 5n, 6n, 7n, 8n]);
console.log(square.isSquare()); // true
console.log(nonsquare.isSquare()); // false

kroneckerProduct

does every pairwise product in a specific arrangement

let A = new Matrix(2n, [1n, 2n, 4n, 8n]);
let B = new Matrix(2n, [1n, new Rational(1n, 3n), new Rational(1n, 9n), new Rational(1n, 27n)]);
A.kroneckerProduct(B);

A =

outerProduct

let A = new Matrix(1n, [1n, 2n, 4n, 8n]);
let B = new Matrix(1n, [1n, new Rational(1n, 3n), new Rational(1n, 9n), new Rational(1n, 27n)]);
let O = A.clone();
O.outerProduct(B);
B.transpose();
A.product(B); // A and O are the same

O =

product

the standard matrix product

let A = new Matrix(2n, [1n, 1n, 0n, 1n]);
let B = new Matrix(1n, [3n, 6n]);
A.product(B);

A =

resetFlags

If you directly set the indices of the matrix after calling determinate, gaussianElimination, or inverse; call this function before calling them again.

swapRows

swaps 2 rows, where 0 is the top row

let A = new Matrix(2n, [1n, 2n, 3n, 4n, 5n, 6n, 7n, 8n]);
A.swapRows(0n, 2n);
A.swapRows(1n, 2n);

A =

toLatex

does not mutate this

returns the LaTeX code corresponding to this

let A = new Matrix(2n, [1, 2, new Rational(2n,3n), 10]);
console.log(A.toLatex()); // "\begin{bmatrix}1&2\\\frac{2}{3}&10\end{bmatrix}"

transpose

flips the entries of this across the main diagonal

let A = new Matrix(4n, [1n, 2n, 3n, 4n, 5n, 6n, 7n, 8n]);
let T = A.clone();
T.transpose();

A =

T =

Clone this wiki locally