Dannjs
2.3.13Deep Neural Network Library for JavaScript.
new Matrix
(
rows
cols
)
Matrix component.
rows
Number
the number of rows.
cols
Number
the number of columns.
Example
const Dannjs = require('dannjs'); const Matrix = Dannjs.matrix;
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.
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
)
Add two Matrix objects together.
a
Matrix
The first Matrix object in the operation.
b
Matrix
The second Matrix object in the operation.
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]
fillCol
(
col
num
)
Fill a culmn of a matrix with a value.
col
Number
The column to fill
num
Number
The value to fill the column with
Example
let m = new Matrix(3, 4);
// Fill column 3 with values of 100
m.fillCol(3, 100);
fillRow
(
row
num
)
Fill a specific row in a matrix.
row
Number
The row index to fill
num
Number
The value to fill the row with
Example
let m = new Matrix(3, 4);
// Fill row 2 with values of 100
m.fillRow(2, 100);
fromArray
(
array
)
Convert an Array into a Matrix Object
array
Array
The array to convert into a Matrix.
Example
let a = [1, 0, 1, 1];
let m = Matrix.fromArray(a);
m.log();
initiate
(
[value]
)
Initiate a matrix with a certain value.
[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
)
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
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]
)
Logs information about the matrix.
[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
)
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.
Example
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.
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
)
Map matrix values to a function
m
Function
The Matrix with which to apply the operation.
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
)
Multiply a Matrix object by an other matrix or a scalar
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 Multiplication, also commonly refered as Matrix dot product. The rows of B must match the columns of A.
a
Matrix
The first matrix in the operation.
b
Matrix
The second matrix in the operation.
[options]
Object
optional
Optional parameters.
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
)
Randomize a Matrix Object's values
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
)
Set a Matrix object.
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
)
Subtract a value or Matrix to a matrix object.
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
)
Subtract two Matrix objects together.
a
Matrix
The first Matrix object in the operation.
b
Matrix
The second Matrix object in the operation.
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
( )
Convert a (1 by n) or (n by 1) Matrix object to an array.
Example
let m = new Matrix(4, 1);
let a = m.toArray();
console.log(a);
toArray
( )
Convert a (1 by n) or (n by 1) Matrix object to an array.
Example
let m = new Matrix(4, 1);
let a = Matrix.toArray(m);
console.log(a);
transpose
(
m
)
Transpose operation of a matrix. Invert row coordinates with column coordinates
m
Matrix
The matrix to be transposed.
Example
const m1 = new Matrix(2,4);
m1.log({table:true});
const m2 = Matrix.transpose(m1);
m2.log({table:true});