lnagb.js Documentation

Matrix

Contains the Matrix class, which encodes basic matrices of any size in linear algebra.

Author: Zach / cszach@proton.me


Matrix~Matrix

Low-level, speed-prioritized class that encodes matrices and their operations. A matrix is encoded by storing its elements - in row-major order for lnagb.js - and its dimensions (see instance properties elements and size).

This class should be used to work with large matrices of arbitrary dimensions. For small matrices, see module:Matrix1~Matrix1, Matrix2, Matrix3, Matrix4, module:Matrix2x3~Matrix2x3, module:Matrix3x2~Matrix3x2.

Kind: inner class of Matrix


new Matrix(nRows, nColumns, entries)

Constructs a new Matrix instance, which encodes a matrix. To create a zero matrix, leave entries undefined. To create an identity matrix, see IdentityMatrix.

Param Type Description
nRows number Number of rows in the new matrix.
nColumns number Number of columns in the matrix.
entries Array.<number> Entries of the matrix in row-major order.

matrix.size : object

Contains the dimensions of this matrix as an object in the form { rows, columns }.

Kind: instance property of Matrix


matrix.numberOfEntries : number

The number of entries in this matrix.

Kind: instance property of Matrix


matrix.elements : Array.<number>

Stores the elements of this matrix in row-major order.

Kind: instance property of Matrix


matrix.rows ⇒ Array.<Array>

Returns the rows of this matrix in an array.

Kind: instance property of Matrix
Returns: Array.<Array> - The rows in this matrix


matrix.columns ⇒ Array.<Array>

Returns the columns of this matrix in an array.

Kind: instance property of Matrix
Returns: Array.<Array> - The columns in this matrix


matrix.mainDiagonal ⇒ Array.<number>

Returns the main diagonal of this matrix.

Kind: instance property of Matrix
Returns: Array.<number> - The entries in the main diagonal of this matrix


matrix.clone() ⇒ Matrix

Creates and returns a clone of this matrix instance.

Kind: instance method of Matrix
Returns: Matrix - A clone of this instance


matrix.equals(matrix) ⇒ boolean

Checks if this matrix and another matrix are equal.

Kind: instance method of Matrix
Returns: boolean - true if the two matrices are equal, false otherwise

Param Type Description
matrix Matrix The matrix to compare this matrix to.

matrix.entry(i, j) ⇒ number

Returns the entry in the specified row and column in this matrix.

Kind: instance method of Matrix
Returns: number - The entry

Param Type Description
i number The row that contains the entry (1-indexed).
j number The column that contains the entry (1-indexed).

matrix.row(r) ⇒ Array.<number>

Returns a row in this matrix as a JavaScript array.

Kind: instance method of Matrix
Returns: Array.<number> - The row’s entries

Param Type Description
r number Row number (1-indexed).

matrix.column(c) ⇒ Array.<number>

Returns a column in this matrix as a JavaScript array.

Kind: instance method of Matrix
Returns: Array.<number> - The column’s entries

Param Type Description
c number Column number (1-indexed).

matrix.leadingCoefficient(r) ⇒ number

Returns the leading coefficient of a row, or undefined if the row does not have a leading coefficient.

Kind: instance method of Matrix
Returns: number - The leading coefficient of the row

Param Type Description
r number Row number (1-indexed).

matrix.forEach(callback, thisArg)

Executes a function for each entry in this matrix. Entries are iterated in row-major order.

Kind: instance method of Matrix

Param Type Description
callback forEach The function to execute per iteration.
thisArg object The argument to use as this in the function.

matrix.forEachRow(callback, thisArg)

Executes a function for each row in this matrix.

Kind: instance method of Matrix

Param Type Description
callback forEachRow The function to execute per iteration.
thisArg object The argument to use as this in the function.

matrix.forEachColumn(callback, thisArg)

Executes a function for each column in this matrix.

Kind: instance method of Matrix

Param Type Description
callback forEachColumn The function to execute per iteration.
thisArg object The argument to use as this in the function.

matrix.interchargeRows(r, s) ⇒ Matrix

Intercharges two rows in this matrix.

Kind: instance method of Matrix
Returns: Matrix - This matrix

Param Type Description
r number First row number (1-indexed).
s number Second row number (1-indexed).

matrix.multiplyRowByScalar(r, k) ⇒ Matrix

Multiplies a row in this matrix by a nonzero scalar.

Kind: instance method of Matrix
Returns: Matrix - This matrix

Param Type Description
r number Row number (1-indexed).
k number The nonzero scalar to multiply the row by.

matrix.addRowTimesScalarToRow(r, s, k) ⇒ Matrix

Adds multiples of a row to another row in this matrix.

Kind: instance method of Matrix
Returns: Matrix - This matrix

Param Type Default Description
r number   The row that gets added (1-indexed position).
s number   The row to multiply the scalar by and then add to row r (1-indexed position).
k number 1 The scalar to multiply row s by.

matrix.transpose() ⇒ Matrix

Transposes this matrix in place.

Kind: instance method of Matrix
Returns: Matrix - This matrix
Todo


matrix.multiplyScalar(k) ⇒ Matrix

Multiplies this matrix by a scalar.

Kind: instance method of Matrix
Returns: Matrix - This matrix

Param Type Description
k number The scalar to multiply this matrix by.

matrix.negate() ⇒ Matrix

Multiplies this matrix by -1.

Kind: instance method of Matrix
Returns: Matrix - This matrix


matrix.add(matrix) ⇒ Matrix

Adds a matrix to this matrix.

Kind: instance method of Matrix
Returns: Matrix - This matrix

Param Type Description
matrix Matrix The matrix to add to this matrix.

matrix.sub(matrix) ⇒ Matrix

Subtracts a matrix from this matrix.

Kind: instance method of Matrix
Returns: Matrix - This matrix

Param Type Description
matrix Matrix The matrix to subtract this matrix to.

matrix.multiply(matrix) ⇒ Matrix

Multiplies this matrix by another matrix. If the input matrix is not compatible for multiplication, return this matrix unchanged.

Kind: instance method of Matrix
Returns: Matrix - This matrix

Param Type Description
matrix Matrix The matrix to post-multiply this matrix to.

Matrix~forEach : function

Kind: inner typedef of Matrix

Param Type Description
entry number The current entry being processed.
i number The entry’s row number (1-indexed).
j number The entry’s column number (1-indexed).
index number The index of the entry in this.elements (0-indexed).
matrix Matrix The instance that this method was called upon.

Matrix~forEachRow : function

Kind: inner typedef of Matrix

Param Type Description
row Array.<number> The row being processed (with its entries).
r number The row’s number (1-indexed).
matrix Matrix The instance that this method was called upon.

Matrix~forEachColumn : function

Kind: inner typedef of Matrix

Param Type Description
column Array.<number> The column being processed.
c number The’s column number (1-indexed).
matrix Matrix The instance that this method was called upon.