Skip to content

Latest commit

 

History

History
50 lines (43 loc) · 959 Bytes

creational.md

File metadata and controls

50 lines (43 loc) · 959 Bytes

Creational examples and features

constructors

// creates a 2x3 matrix
const A = new Matrix([
    [1, 2, 3], 
    [4, 5, 6], 
]);

// creates a 2x3 matrix
const B = new Matrix([1, 2, 3, 4, 5, 6], 2, 3);

// creates a 3x2 matrix
const B = new Matrix([1, 2, 3, 4, 5, 6], 3, 2);

// creates an 1x6 matrix
const B = new Matrix([1, 2, 3, 4, 5, 6], 1, 6);

zeros and ones

// results [[0, 0, 0, 0], [0, 0, 0, 0]];
const matrix = zeros(2, 4);

// results [[1], [1], [1]];
const matrix = zeros(3, 1);

identity (aka eye)

// [[0, 0, 0], [0, 1, 0], [0, 0, 1]];
const matrix = identity(3);

clone

Make a full copy of source matrix

const A = new Matrix([
    [1, 2, 3], 
    [4, 5, 6], 
]);
const B = A.clone();

diagonal

Creates a diagonal matrix using elements from a 1D array

const v = [-1, 4, 2);
const B = diagonal(v);
// B is [[-1, 0, 0], [0, 4, 0], [0, 0, 2]];