Dannjs
2.2.10Deep Neural Network Library for JavaScript.
A way to describe matrices and perform operations with them.
Matrix
rows
cols
rows
Number
the number of rows.
cols
Number
the number of columns.
const m = new Matrix(3,4);
m.log();
add
n
Add a value or Matrix to a Matrix object.
n
Number | Matrix
Number value or Matrix to add to the Matrix object.
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
Add two Matrix objects together.
the result Matrix.
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]
initiate
[value]
Initiate a matrix with a certain value.
[value]
Number
optional
The value to initiate te Matrix with, set to 0 by default.
const m = new Matrix(2, 2);
m.initiate(100);
m.log();
// [100, 100]
// [100, 100]
insert
value
x
y
Set a specific value at a coordinate in the matrix.
value
Number
The value to be inserted into the specified coordinates in the matrix
x
Number
Row index
y
Number
Column index
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]
Logs information about the matrix.
[options]
Object
optional
Object including specific properties.
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
Create a Number[][] matrix, all values set to 0.
x
Number
the x component of the Number[][] matrix.
y
Number
the y component of the Number[][] matrix.
let rawMatrix = Matrix.make(3, 4);
console.log(rawMatrix);
map
f
Map matrix values to a function
f
Function
The function with which to map the matrix values.
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]
mult
n
Multiply a Matrix object by an other matrix or a scalar
n
Matrix | Number
Scalar or Matrix to multiply to the Matrix object.
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 Multiplication, also commonly refered as Matrix dot product. The rows of B must match the columns of A.
The resultant Matrix Object.
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
Randomize a Matrix Object's values
min
Number
the minimum possible random value.
max
Number
the maximum possible random value.
const a = new Matrix(3,3);
a.randomize(-1, 1);
a.log();
set
matrix
Set a Matrix object.
matrix
Number[]
A matrix with which to set the current Matrix object with.
const a = new Matrix(0,0);
const rawMatrix = [
[1, 0],
[0, 1]
];
a.set(rawMatrix);
a.log();
sub
n
Subtract a value or Matrix to a matrix object.
n
Number | Matrix
Number to subtract to the matrix.
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
Subtract two Matrix objects together.
the result Matrix.
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
Convert a (1 by n) or (n by 1) Matrix object to an array.
let m = new Matrix(4, 1);
let a = m.toArray();
console.log(a);