Home Reference Source Test
public class | source

Matrix

Extends:

Array → Matrix

A class for storing values in rows and columns and for doing common matrix operations. As the Vector class, this extends the js Array class.

Values are stores columns-first internally, but is normally instantiated by supplying values in rows-first order.

Static Method Summary

Static Public Methods
public static

fromArray(arr: number[], columns: number, rowsFirst: boolean, mutateArgs: boolean): Matrix

Instantiate a matrix from an array.

public static

fromVectors(vectors: ...number[]): Matrix

Create a matrix from vectors.

public static

Creates an identity matrix with rows and columns equal to size.

public static

traverse(arr: number[], cols: number, callback: function, rowsFirst: boolean)

Traverse an array as a matrix, either rows-first or columns first.

Constructor Summary

Public Constructor
public

constructor(rows: number, cols: number, values: number[], rowsFirst: boolean, mutateArgs: boolean)

Constructs a new instance of Matrix.

Member Summary

Public Members
public

[i]: *

assign value

public

[idx]: *

assign value

public get

Get the number of columns in this matrix.

public get

isSquare: boolan: *

This function returns true if this matrix has the same number of rows as columns, otherwise false.

public get

rows: number: *

Get the number of rows in this matrix.

Private Members
private

_c: *

internal property to hold number of columns of this matrix

private

_r: *

internal property to hold number of rows of this matrix

Method Summary

Public Methods
public

Clone/copy this matrix

public

col(j: number, target: Vector | number[]): number[]

Get the values from a specific column.

public

Returns a native javascript Array representation of this matrix, in rows-first order

public

copyFrom(values: number[], rowsFirst: boolean, mutateArgs: number): *

Set values of this matrix from an array

public

Calculate this matrix's determinant (where applicable)

public

dot(other: number[] | Vector | Matrix, target: Vector | number[]): Matrix | Vector | number[]

Calculate the matrix-matrix or matrix-vector product, depending on the type of the argument passed to the other-parameter.

public

dotMat(matrices: ...Matrix): Matrix

Calculate the matrix matrix product between this matrix and the passed matrices.

public

dotVec(vec: number[], target: Vector | number[]): number[]

Calculate the matrix vector product between this matrix and the passed arrays/vectors.

public

get(row: number, col: number): number

Get a matrix element value by row and column

public

index(row: number, col: number): *

Calculate the internal 1d array index

public

inverse(target: Matrix): Matrix

Invert this matrix (where applicable)

public

remove(row: number, col: number, target: Matrix): Matrix

Remove a row and/or column from this matrix

public

row(i: number, target: Vector | number[]): number[]

Get the values from a specific row.

public

Returns a native javascript Array representation of this matrix, in column-first order

public

scale(factor: number, target: Matrix | number[]): Matrix

Scale this vector by a factor

public

set(row: number, col: number, value: number): Matrix

Set a value at the specified row and column

public

submatrix(row: number, col: number, rows: number, cols: number, target: Matrix | number[]): Matrix

Create a submatrix from this matrix.

public

toArray(rowsFirst: boolean): number[]

Return a native javascript Array representation of this matrix.

public

toArray2d(rowsFirst: boolean): Array<Array>

Returns a 2d native javascript Array representation of this matrix.

public

transpose(target: Matrix | number[]): Matrix

Transpose this matrix (switch rows and columns)

public

traverse(callback: function, rowsFirst: boolean): Matrix

Traverse this matrix

Static Public Methods

public static fromArray(arr: number[], columns: number, rowsFirst: boolean, mutateArgs: boolean): Matrix source

Instantiate a matrix from an array.

Params:

NameTypeAttributeDescription
arr number[]

array of numbers

columns number

number of columns to split the array into

rowsFirst boolean

traverse order

mutateArgs boolean

set if the input array should be mutated or not

Return:

Matrix

public static fromVectors(vectors: ...number[]): Matrix source

Create a matrix from vectors. Each vector will be a column in the matrix and the number of rows are determined by the vector dimension.

Params:

NameTypeAttributeDescription
vectors ...number[]

Return:

Matrix

public static identity(size: number): Matrix source

Creates an identity matrix with rows and columns equal to size.

Params:

NameTypeAttributeDescription
size number

dimension

Return:

Matrix

public static traverse(arr: number[], cols: number, callback: function, rowsFirst: boolean) source

Traverse an array as a matrix, either rows-first or columns first.

Params:

NameTypeAttributeDescription
arr number[]

array to traverse as a matrix

cols number

number of columns to split the array into

callback function

function to be executed for each element

rowsFirst boolean

traverse order

Public Constructors

public constructor(rows: number, cols: number, values: number[], rowsFirst: boolean, mutateArgs: boolean) source

Constructs a new instance of Matrix. If cols is omitted, it will mirror the rows parameter, making a square matrix.

Params:

NameTypeAttributeDescription
rows number

number of rows

cols number

number of columns

values number[]

array of numbers to assign to the matrix elements

rowsFirst boolean

if values should be read rows-first or columns-first

mutateArgs boolean

set if the input array should be mutated or not

Public Members

public [i]: * source

assign value

public [idx]: * source

assign value

public get columns: number: * source

Get the number of columns in this matrix.

Return:

number

public get isSquare: boolan: * source

This function returns true if this matrix has the same number of rows as columns, otherwise false.

Return:

boolan

public get rows: number: * source

Get the number of rows in this matrix.

Return:

number

Private Members

private _c: * source

internal property to hold number of columns of this matrix

private _r: * source

internal property to hold number of rows of this matrix

Public Methods

public clone(): Matrix source

Clone/copy this matrix

Return:

Matrix

public col(j: number, target: Vector | number[]): number[] source

Get the values from a specific column.

Params:

NameTypeAttributeDescription
j number

zero-based column index

target Vector | number[]

optional array/vector to write values to

Return:

number[]

column values

public columnsFirst(): number[] source

Returns a native javascript Array representation of this matrix, in rows-first order

Return:

number[]

public copyFrom(values: number[], rowsFirst: boolean, mutateArgs: number): * source

Set values of this matrix from an array

Params:

NameTypeAttributeDescription
values number[]

array of numbers

rowsFirst boolean

if values should be read rows-first or columns-first

mutateArgs number

Return:

*

public determinant(): number source

Calculate this matrix's determinant (where applicable)

Return:

number

the determinant

public dot(other: number[] | Vector | Matrix, target: Vector | number[]): Matrix | Vector | number[] source

Calculate the matrix-matrix or matrix-vector product, depending on the type of the argument passed to the other-parameter.

Params:

NameTypeAttributeDescription
other number[] | Vector | Matrix

{Array/Vector/Matrix}

target Vector | number[]

optional array/vector to avoid mutating input argument

Return:

Matrix | Vector | number[]

depending on input.

public dotMat(matrices: ...Matrix): Matrix source

Calculate the matrix matrix product between this matrix and the passed matrices. in the order they appear.

The left hand matrix number of columns must always match the right hand matrix number of rows.

Params:

NameTypeAttributeDescription
matrices ...Matrix

Return:

Matrix

public dotVec(vec: number[], target: Vector | number[]): number[] source

Calculate the matrix vector product between this matrix and the passed arrays/vectors.

Since it is common to use homogeneous coordinates when doing matrix-vector multiplication, i.e. use 4x4 matrix for transforming 3d vectors, the vectors will be treated as if they had the same number of components as the matrix has columns. For vectors with a lower dimension than the number of columns in the matrix, the last component will be iterpreted as 1 and any in between as 0. In the opposite case, it will simply neglect excessive components.

Params:

NameTypeAttributeDescription
vec number[]

vector (as Array/Vector)

target Vector | number[]

optional array/vector to avoid mutating the vec argument

Return:

number[]

resulting vector (type depending on vec/target argument)

public get(row: number, col: number): number source

Get a matrix element value by row and column

Params:

NameTypeAttributeDescription
row number

row index (zero-based)

col number

column index (zero-based)

Return:

number

public index(row: number, col: number): * source

Calculate the internal 1d array index

Params:

NameTypeAttributeDescription
row number

row index (zero-based)

col number

column index (zero-based)

Return:

*

public inverse(target: Matrix): Matrix source

Invert this matrix (where applicable)

Params:

NameTypeAttributeDescription
target Matrix

optional matrix to avoid mutating this matrix

Return:

Matrix

transposed matrix

public remove(row: number, col: number, target: Matrix): Matrix source

Remove a row and/or column from this matrix

Params:

NameTypeAttributeDescription
row number

zero-based row index (-1 or null to omit)

col number

zero-based column index (-1 or null to omit)

target Matrix

optional matrix to avoid mutating this matrix

Return:

Matrix

reduced matrix

public row(i: number, target: Vector | number[]): number[] source

Get the values from a specific row.

Params:

NameTypeAttributeDescription
i number

zero-based row index

target Vector | number[]

optional array/vector to write values to

Return:

number[]

row values

public rowsFirst(): number[] source

Returns a native javascript Array representation of this matrix, in column-first order

Return:

number[]

public scale(factor: number, target: Matrix | number[]): Matrix source

Scale this vector by a factor

Params:

NameTypeAttributeDescription
factor number

scaling factor

target Matrix | number[]

optional array/matrix to avoid mutating this matrix

Return:

Matrix

public set(row: number, col: number, value: number): Matrix source

Set a value at the specified row and column

Params:

NameTypeAttributeDescription
row number

row index (zero-based)

col number

column index (zero-based)

value number

value to set

Return:

Matrix

returns itself for chaining

public submatrix(row: number, col: number, rows: number, cols: number, target: Matrix | number[]): Matrix source

Create a submatrix from this matrix.

Params:

NameTypeAttributeDescription
row number

zero-based row index

col number

zero-based column index

rows number

number of rows to include

cols number

number of columns to include

target Matrix | number[]

optional array/matrix to write values to

Return:

Matrix

submatrix

public toArray(rowsFirst: boolean): number[] source

Return a native javascript Array representation of this matrix.

Params:

NameTypeAttributeDescription
rowsFirst boolean

if the output should be rows-first or columns-first

Return:

number[]

public toArray2d(rowsFirst: boolean): Array<Array> source

Returns a 2d native javascript Array representation of this matrix.

Params:

NameTypeAttributeDescription
rowsFirst boolean

if the output should be rows-first or columns-first

Return:

Array<Array>

public transpose(target: Matrix | number[]): Matrix source

Transpose this matrix (switch rows and columns)

Params:

NameTypeAttributeDescription
target Matrix | number[]

optional array/matrix to avoid mutating this matrix

Return:

Matrix

transposed matrix

public traverse(callback: function, rowsFirst: boolean): Matrix source

Traverse this matrix

Params:

NameTypeAttributeDescription
callback function

function to be executed for each element

rowsFirst boolean

if values should be read rows-first or columns-first

Return:

Matrix

returns itself for chaining