Matrix Class

A way to describe matrices and perform operations with them.

Methods


Constructor

Matrix
(
  • rows
  • cols
)

Parameters:

  • rows Number

    the number of rows.

  • cols Number

    the number of columns.

Example:


const m = new Matrix(3,4);
m.log();

Methods

add
(
  • n
)
chainable

Provided by the Layer module.

Defined in src\classes\matrix\methods\add.js:1

Add a value or Matrix to a Matrix object.

Parameters:

  • n Number | Matrix

    Number value or Matrix to add to the Matrix object.

Example:


const m = new Matrix(3,3);
m.set([
   [1, 1, 1],
   [5, 3, 2],
   [2, 4, 4]
]);
m.add(1);
m.log();
//   [2, 2, 2]
//   [6, 4, 3]
//   [2, 4, 4]

const a = new Matrix(3,3);
a.set([
   [1, 1, 1],
   [5, 3, 2],
   [2, 4, 4]
]);
const b = new Matrix(3,3);
b.set([
   [1, 4, 1],
   [2, 2, 1],
   [2, 1, 0]
]);
a.add(b);
a.log();
//   [2, 5, 2]
//   [7, 7, 3]
//   [4, 5, 4]

add
(
  • a
  • b
)
Matrix static

Provided by the Layer module.

Defined in src\classes\matrix\methods\add.js:65

Add two Matrix objects together.

Parameters:

  • a Matrix

    The first Matrix object in the operation.

  • b Matrix

    The second Matrix object in the operation.

Returns:

Matrix :

the result Matrix.

Example:


const a = new Matrix(3,3);
a.set([
   [4, 2, 1],
   [2, 3, 2],
   [1, 1, 4]
]);
const b = new Matrix(3,3);
b.set([
   [1, 4, 1],
   [2, 2, 1],
   [2, 1, 0]
]);
const c = Matrix.add(a, b);
c.log();
//   [5, 6, 1]
//   [4, 5, 3]
//   [3, 2, 4]

fromArray
(
  • array
)
Matrix static

Provided by the Layer module.

Defined in src\classes\matrix\methods\fromArray.js:1

Convert an Array into a Matrix Object

Parameters:

  • array Array

    The array to convert into a Matrix.

Returns:

Matrix :

1 row, n col Matrix Object

Example:


let a = [1, 0, 1, 1];
let m = Matrix.fromArray(a);
m.log();

initiate
(
  • [value]
)
chainable

Provided by the Layer module.

Defined in src\classes\matrix\methods\initiate.js:1

Initiate a matrix with a certain value.

Parameters:

  • [value] Number optional

    The value to initiate te Matrix with, set to 0 by default.

Example:


const m = new Matrix(2, 2);
m.initiate(100);
m.log();
// [100, 100]
// [100, 100]

insert
(
  • value
  • x
  • y
)
chainable

Provided by the Layer module.

Defined in src\classes\matrix\methods\insert.js:1

Set a specific value at a coordinate in the matrix.

Parameters:

  • value Number

    The value to be inserted into the specified coordinates in the matrix

  • x Number

    Row index

  • y Number

    Column index

Example:


const m1 = new Matrix(3,4);
m1.log();
// [0, 0, 0, 0]
// [0, 0, 0, 0]
// [0, 0, 0, 0]
let value = 1;
m1.insert(value, 2, 3);
m1.log();
// [0, 0, 0, 0]
// [0, 0, 0, 0]
// [0, 0, 0, 1]

log
(
  • [options]
)

Provided by the Layer module.

Defined in src\classes\matrix\methods\log.js:1

Logs information about the matrix.

Parameters:

  • [options] Object optional

    Object including specific properties.

Example:


const m = new Matrix(3, 6);
// log
m.log();
// log as a table
m.log({table:true});
// log with rounded values
m.log({decimals:3});

make
(
  • x
  • y
)
Number[] static

Provided by the Layer module.

Defined in src\classes\matrix\methods\make.js:1

Create a Number[][] matrix, all values set to 0.

Parameters:

  • x Number

    the x component of the Number[][] matrix.

  • y Number

    the y component of the Number[][] matrix.

Returns:

Number[] :

Example:


let rawMatrix = Matrix.make(3, 4);
console.log(rawMatrix);

map
(
  • f
)
chainable

Provided by the Layer module.

Defined in src\classes\matrix\methods\map.js:1

Map matrix values to a function

Parameters:

  • f Function

    The function with which to map the matrix values.

Example:


const m = new Matrix(2, 2);
m.initiate(4);
m.log();
// [4, 4]
// [4, 4]
m.map(x => x*x);
m.log();
// [16, 16]
// [16, 16]

map
(
  • m
)
Matrix static

Provided by the Layer module.

Defined in src\classes\matrix\methods\map.js:29

Map matrix values to a function

Parameters:

  • m Function

    The Matrix with which to apply the operation.

Returns:

Matrix :

Example:


const m = new Matrix(2, 2);
m.initiate(4);
m.log();
// [4, 4]
// [4, 4]
let m1 = Matrix.map(m, x => x*x*x);
m1.log();
// [64, 64]
// [64, 64]

mult
(
  • n
)
chainable

Provided by the Layer module.

Defined in src\classes\matrix\methods\mult.js:1

Multiply a Matrix object by an other matrix or a scalar

Parameters:

  • n Matrix | Number

    Scalar or Matrix to multiply to the Matrix object.

Example:


const m = new Matrix(2, 2);
m.set([
   [1, 2],
   [2, 3]
]);
m.mult(2);
m.log();
//  [2, 4],
//  [4, 6]

const a = new Matrix(2, 2);
a.set([
   [1, 2],
   [2, 3]
]);
const b = new Matrix(2, 2);
b.set([
   [2, 2],
   [2, 0.5]
]);
a.mult(b);
a.log();
//  [2, 4],
//  [4, 1.5]

mult
(
  • a
  • b
  • [options]
)
Matrix static

Provided by the Layer module.

Defined in src\classes\matrix\methods\mult.js:62

Matrix Multiplication, also commonly refered as Matrix dot product. The rows of B must match the columns of A.

Parameters:

  • a Matrix

    The first matrix in the operation.

  • b Matrix

    The second matrix in the operation.

  • [options] Object optional

    Optional parameters.

Returns:

Matrix :

The resultant Matrix Object.

Example:


let a = new Matrix(4, 3);
a.set([
  [1, 0, 1, 0],
  [0, 1, 0, 0],
  [0, 1, 1, 1]
]);
let b = new Matrix(3, 4);
b.set([
   [1, 0, 1],
   [0, 1, 0],
   [0, 1, 1],
   [1, 0, 0]
]);
let c = Matrix.mult(a,b);
c.log();
// c.matrix is
// [1, 1, 2]
// [0, 1, 0]
// [1, 2, 1]

randomize
(
  • min
  • max
)
chainable

Provided by the Layer module.

Defined in src\classes\matrix\methods\randomize.js:1

Randomize a Matrix Object's values

Parameters:

  • min Number

    the minimum possible random value.

  • max Number

    the maximum possible random value.

Example:


const a = new Matrix(3,3);
a.randomize(-1, 1);
a.log();

set
(
  • matrix
)

Provided by the Layer module.

Defined in src\classes\matrix\methods\set.js:1

Set a Matrix object.

Parameters:

  • matrix Number[]

    A matrix with which to set the current Matrix object with.

Example:


const a = new Matrix(0,0);
const rawMatrix = [
   [1, 0],
   [0, 1]
];
a.set(rawMatrix);
a.log();

sub
(
  • n
)
chainable

Provided by the Layer module.

Defined in src\classes\matrix\methods\sub.js:1

Subtract a value or Matrix to a matrix object.

Parameters:

  • n Number | Matrix

    Number to subtract to the matrix.

Example:


const m = new Matrix(3,3);
m.set([
   [1, 2, 1],
   [2, 2, 3],
   [2, 1, 4]
]);
m.sub(1);
m.log();
//   [0, 1, 0]
//   [1, 1, 2]
//   [1, 0, 3]

sub
(
  • a
  • b
)
Matrix static

Provided by the Layer module.

Defined in src\classes\matrix\methods\sub.js:43

Subtract two Matrix objects together.

Parameters:

  • a Matrix

    The first Matrix object in the operation.

  • b Matrix

    The second Matrix object in the operation.

Returns:

Matrix :

the result Matrix.

Example:


const a = new Matrix(3,3);
a.set([
   [4, 2, 1],
   [2, 3, 2],
   [1, 1, 4]
]);
const b = new Matrix(3,3);
b.set([
   [1, 4, 1],
   [2, 2, 1],
   [2, 1, 0]
]);
const c = Matrix.sub(a, b);
c.log();
//   [3, -2, 0]
//   [0,  1, 1]
//   [-1, 0, 4]

toArray ( ) Array

Provided by the Layer module.

Defined in src\classes\matrix\methods\toArray.js:1

Convert a (1 by n) or (n by 1) Matrix object to an array.

Returns:

Array :

Example:


let m = new Matrix(4, 1);
let a = m.toArray();
console.log(a);

toArray ( ) Array static

Provided by the Layer module.

Defined in src\classes\matrix\methods\toArray.js:31

Convert a (1 by n) or (n by 1) Matrix object to an array.

Returns:

Array :

Example:


let m = new Matrix(4, 1);
let a = Matrix.toArray(m);
console.log(a);

transpose
(
  • m
)
Matrix

Provided by the Layer module.

Defined in src\classes\matrix\methods\transpose.js:1

Transpose operation of a matrix. Invert row coordinates with column coordinates

Parameters:

  • m Matrix

    The matrix to be transposed.

Returns:

Matrix :

Example:


const m1 = new Matrix(2,4);
m1.log({table:true});
const m2 = Matrix.transpose(m1);
m2.log({table:true});