Skip to main content

Matrix

data-structure-typed


data-structure-typed / Matrix

Class: Matrix

Defined in: data-structures/matrix/matrix.ts:97

Matrix — a numeric matrix with standard linear algebra operations.

Examples

// Basic matrix arithmetic
const a = new Matrix([
[1, 2],
[3, 4]
]);
const b = new Matrix([
[5, 6],
[7, 8]
]);

const sum = a.add(b);
console.log(sum?.data); // [
// [6, 8],
// [10, 12]
// ];

const diff = b.subtract(a);
console.log(diff?.data); // [
// [4, 4],
// [4, 4]
// ];
// Matrix multiplication for transformations
// 2x3 matrix * 3x2 matrix = 2x2 matrix
const a = new Matrix([
[1, 2, 3],
[4, 5, 6]
]);
const b = new Matrix([
[7, 8],
[9, 10],
[11, 12]
]);

const product = a.multiply(b);
console.log(product?.rows); // 2;
console.log(product?.cols); // 2;
// Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
// Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
console.log(product?.data); // [
// [58, 64],
// [139, 154]
// ];
// Matrix transpose (square matrix)
const m = new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);

const transposed = m.transpose();
console.log(transposed.rows); // 3;
console.log(transposed.cols); // 3;
console.log(transposed.data); // [
// [1, 4, 7],
// [2, 5, 8],
// [3, 6, 9]
// ];

// Transpose of transpose = original
console.log(transposed.transpose().data); // m.data;
// Get and set individual cells
const m = new Matrix([
[0, 0, 0],
[0, 0, 0]
]);

m.set(0, 1, 42);
m.set(1, 2, 99);

console.log(m.get(0, 1)); // 42;
console.log(m.get(1, 2)); // 99;
console.log(m.get(0, 0)); // 0;

// Out of bounds returns undefined
console.log(m.get(5, 5)); // undefined;

Constructors

Constructor

new Matrix(data, options?): Matrix;

Defined in: data-structures/matrix/matrix.ts:105

The constructor function initializes a matrix object with the provided data and options, or with default values if no options are provided.

Parameters

data

number[][]

A 2D array of numbers representing the data for the matrix.

options?

MatrixOptions

The options parameter is an optional object that can contain the following properties:

Returns

Matrix

Accessors

addFn

Get Signature

get addFn(): (a, b) => number | undefined;

Defined in: data-structures/matrix/matrix.ts:164

The above function returns the value of the _addFn property.

Returns

The value of the property _addFn is being returned.

(a, b) => number | undefined


cols

Get Signature

get cols(): number;

Defined in: data-structures/matrix/matrix.ts:146

The function returns the value of the protected variable _cols.

Returns

number

The number of columns.


data

Get Signature

get data(): number[][];

Defined in: data-structures/matrix/matrix.ts:156

The function returns a two-dimensional array of numbers.

Returns

number[][]

The data property, which is a two-dimensional array of numbers.


multiplyFn

Get Signature

get multiplyFn(): (a, b) => number;

Defined in: data-structures/matrix/matrix.ts:180

The function returns the value of the _multiplyFn property.

Returns

The _multiplyFn property is being returned.

(a, b) => number


rows

Get Signature

get rows(): number;

Defined in: data-structures/matrix/matrix.ts:136

The function returns the number of rows.

Returns

number

The number of rows.


size

Get Signature

get size(): [number, number];

Defined in: data-structures/matrix/matrix.ts:896

Returns [rows, cols] dimensions tuple.

Returns

[number, number]


subtractFn

Get Signature

get subtractFn(): (a, b) => number;

Defined in: data-structures/matrix/matrix.ts:172

The function returns the value of the _subtractFn property.

Returns

The _subtractFn property is being returned.

(a, b) => number

Methods

[iterator]()

iterator: IterableIterator<number[]>;

Defined in: data-structures/matrix/matrix.ts:925

Iterates over rows.

Returns

IterableIterator<number[]>


add()

add(matrix): Matrix | undefined;

Defined in: data-structures/matrix/matrix.ts:382

The add function adds two matrices together, returning a new matrix with the result.

Parameters

matrix

Matrix

The matrix parameter is an instance of the Matrix class.

Returns

Matrix | undefined

The add method returns a new Matrix object that represents the result of adding the current matrix with the provided matrix parameter.

Example

// Basic matrix arithmetic
const a = new Matrix([
[1, 2],
[3, 4]
]);
const b = new Matrix([
[5, 6],
[7, 8]
]);

const sum = a.add(b);
console.log(sum?.data); // [
// [6, 8],
// [10, 12]
// ];

const diff = b.subtract(a);
console.log(diff?.data); // [
// [4, 4],
// [4, 4]
// ];

clone()

clone(): Matrix;

Defined in: data-structures/matrix/matrix.ts:878

The clone function returns a new instance of the Matrix class with the same data and properties as the original instance.

Returns

Matrix

The clone() method is returning a new instance of the Matrix class with the same data and properties as the current instance.


dot()

dot(matrix): Matrix | undefined;

Defined in: data-structures/matrix/matrix.ts:827

The dot function calculates the dot product of two matrices and returns a new matrix.

Parameters

matrix

Matrix

The matrix parameter is an instance of the Matrix class.

Returns

Matrix | undefined

a new Matrix object.

Example

// Dot product of two matrices
const a = Matrix.from([[1, 2], [3, 4]]);
const b = Matrix.from([[5, 6], [7, 8]]);
const result = a.dot(b);
console.log(result?.toArray()); // [[19, 22], [43, 50]];

flatten()

flatten(): number[];

Defined in: data-structures/matrix/matrix.ts:914

Returns a flat row-major array.

Returns

number[]


forEach()

forEach(callback): void;

Defined in: data-structures/matrix/matrix.ts:944

Visits each element with its row and column index.

Parameters

callback

(value, row, col) => void

Returns

void


get()

get(row, col): number | undefined;

Defined in: data-structures/matrix/matrix.ts:243

The get function returns the value at the specified row and column index if it is a valid index.

Parameters

row

number

The row parameter represents the row index of the element you want to retrieve from the data array.

col

number

The parameter "col" represents the column number of the element you want to retrieve from the data array.

Returns

number | undefined

The get function returns a number if the provided row and column indices are valid. Otherwise, it returns undefined.

Example

// Get and set individual cells
const m = new Matrix([
[0, 0, 0],
[0, 0, 0]
]);

m.set(0, 1, 42);
m.set(1, 2, 99);

console.log(m.get(0, 1)); // 42;
console.log(m.get(1, 2)); // 99;
console.log(m.get(0, 0)); // 0;

// Out of bounds returns undefined
console.log(m.get(5, 5)); // undefined;

inverse()

inverse(): Matrix | undefined;

Defined in: data-structures/matrix/matrix.ts:707

The inverse function calculates the inverse of a square matrix using Gaussian elimination.

Returns

Matrix | undefined

a Matrix object, which represents the inverse of the original matrix.

Example

// Compute the inverse of a 2x2 matrix
const m = Matrix.from([[4, 7], [2, 6]]);
const inv = m.inverse();
console.log(inv); // defined;
// A * A^-1 should ≈ Identity
const product = m.multiply(inv!);
console.log(product?.get(0, 0)); // toBeCloseTo;
console.log(product?.get(0, 1)); // toBeCloseTo;
console.log(product?.get(1, 0)); // toBeCloseTo;
console.log(product?.get(1, 1)); // toBeCloseTo;

isMatchForCalculate()

isMatchForCalculate(matrix): boolean;

Defined in: data-structures/matrix/matrix.ts:316

The function checks if the dimensions of the given matrix match the dimensions of the current matrix.

Parameters

matrix

Matrix

The parameter matrix is of type Matrix.

Returns

boolean

a boolean value.


isValidIndex()

isValidIndex(row, col): boolean;

Defined in: data-structures/matrix/matrix.ts:868

The function checks if a given row and column index is valid within a specified range.

Parameters

row

number

The row parameter represents the row index of a two-dimensional array or matrix. It is a number that indicates the specific row in the matrix.

col

number

The "col" parameter represents the column index in a two-dimensional array or grid. It is used to check if the given column index is valid within the bounds of the grid.

Returns

boolean

A boolean value is being returned.


map()

map(callback): Matrix;

Defined in: data-structures/matrix/matrix.ts:955

Maps each element (number → number) and returns a new Matrix.

Parameters

callback

(value, row, col) => number

Returns

Matrix


multiply()

multiply(matrix): Matrix | undefined;

Defined in: data-structures/matrix/matrix.ts:543

The multiply function performs matrix multiplication between two matrices and returns the result as a new matrix.

Parameters

matrix

Matrix

The matrix parameter is an instance of the Matrix class.

Returns

Matrix | undefined

a new Matrix object.

Example

// Matrix multiplication for transformations
// 2x3 matrix * 3x2 matrix = 2x2 matrix
const a = new Matrix([
[1, 2, 3],
[4, 5, 6]
]);
const b = new Matrix([
[7, 8],
[9, 10],
[11, 12]
]);

const product = a.multiply(b);
console.log(product?.rows); // 2;
console.log(product?.cols); // 2;
// Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
// Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
console.log(product?.data); // [
// [58, 64],
// [139, 154]
// ];

set()

set(
row,
col,
value): boolean;

Defined in: data-structures/matrix/matrix.ts:302

The set function updates the value at a specified row and column in a two-dimensional array.

Parameters

row

number

The "row" parameter represents the row index of the element in a two-dimensional array or matrix. It specifies the row where the value will be set.

col

number

The "col" parameter represents the column index of the element in a two-dimensional array.

value

number

The value parameter represents the number that you want to set at the specified row and column in the data array.

Returns

boolean

a boolean value. It returns true if the index (row, col) is valid and the value is successfully set in the data array. It returns false if the index is invalid and the value is not set.

Example

// Modify individual cells
const m = Matrix.zeros(2, 2);
console.log(m.set(0, 0, 5)); // true;
console.log(m.set(1, 1, 10)); // true;
console.log(m.get(0, 0)); // 5;
console.log(m.get(1, 1)); // 10;

subtract()

subtract(matrix): Matrix | undefined;

Defined in: data-structures/matrix/matrix.ts:455

The subtract function performs element-wise subtraction between two matrices and returns a new matrix with the result.

Parameters

matrix

Matrix

The matrix parameter is an instance of the Matrix class. It represents the matrix that you want to subtract from the current matrix.

Returns

Matrix | undefined

a new Matrix object with the result of the subtraction operation.

Example

// Element-wise subtraction
const a = Matrix.from([[5, 6], [7, 8]]);
const b = Matrix.from([[1, 2], [3, 4]]);
const result = a.subtract(b);
console.log(result?.toArray()); // [[4, 4], [4, 4]];

toArray()

toArray(): number[][];

Defined in: data-structures/matrix/matrix.ts:907

Returns a deep copy of the data as a plain 2D array.

Returns

number[][]


transpose()

transpose(): Matrix;

Defined in: data-structures/matrix/matrix.ts:634

The transpose function takes a matrix and returns a new matrix that is the transpose of the original matrix.

Returns

Matrix

The transpose() function returns a new Matrix object with the transposed data.

Example

// Matrix transpose (square matrix)
const m = new Matrix([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);

const transposed = m.transpose();
console.log(transposed.rows); // 3;
console.log(transposed.cols); // 3;
console.log(transposed.data); // [
// [1, 4, 7],
// [2, 5, 8],
// [3, 6, 9]
// ];

// Transpose of transpose = original
console.log(transposed.transpose().data); // m.data;

from()

static from(data): Matrix;

Defined in: data-structures/matrix/matrix.ts:1014

Creates a Matrix from a plain 2D array (deep copy).

Parameters

data

number[][]

Returns

Matrix

Example

const m = Matrix.from([[1, 2], [3, 4]]);
m.get(0, 1); // 2

identity()

static identity(n): Matrix;

Defined in: data-structures/matrix/matrix.ts:999

Creates an n×n identity matrix.

Parameters

n

number

Returns

Matrix

Example

const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]

zeros()

static zeros(rows, cols): Matrix;

Defined in: data-structures/matrix/matrix.ts:987

Creates a rows×cols zero matrix.

Parameters

rows

number

cols

number

Returns

Matrix

Example

const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]

Protected Members

_addScaledRow()

protected _addScaledRow(
targetRow,
sourceRow,
scalar): void;

Defined in: data-structures/matrix/matrix.ts:1068

The function _addScaledRow multiplies a row in a matrix by a scalar value and adds it to another row.

Parameters

targetRow

number

The targetRow parameter represents the index of the row in which the scaled values will be added.

sourceRow

number

The sourceRow parameter represents the index of the row from which the values will be scaled and added to the targetRow.

scalar

number

The scalar parameter is a number that is used to scale the values in the source row before adding them to the target row.

Returns

void


_scaleRow()

protected _scaleRow(row, scalar): void;

Defined in: data-structures/matrix/matrix.ts:1050

The function scales a specific row in a matrix by a given scalar value.

Parameters

row

number

The row parameter represents the index of the row in the matrix that you want to scale. It is a number that indicates the position of the row within the matrix.

scalar

number

The scalar parameter is a number that is used to multiply each element in a specific row of a matrix.

Returns

void


_swapRows()

protected _swapRows(row1, row2): void;

Defined in: data-structures/matrix/matrix.ts:1037

The function _swapRows swaps the positions of two rows in an array.

Parameters

row1

number

The row1 parameter is the index of the first row that you want to swap.

row2

number

The row2 parameter is the index of the second row that you want to swap with the first row.

Returns

void