Skip to content

mat

Since v8.0.74

Dense()

A dense matrix implementation.

Creation
Dense(r, c, data)
Parameters
  • r Number rows
  • c Number columns
  • data Number[] matrix elements

Creates a new Dense matrix of size r × c.

Usage example
1
2
3
4
5
6
7
const mat = require('mathx/mat');
const a = mat.Dense(3, 3, [11,12,13,21,22,23,31,32,33]);
console.println(mat.format(a, {format:"a = %v",  prefix: "    "}))

// a = ⎡11  12  13⎤
//     ⎢21  22  23⎥
//     ⎣31  32  33⎦

dims()

Returns the matrix dimensions (rows, columns).

Usage example
1
2
3
4
5
const mat = require('mathx/mat');
const a = mat.Dense(3, 3, [11,12,13,21,22,23,31,32,33]);
console.println("dims:", a.dims());

// dims: [3, 3]

at()

Returns the value at the specified (row, col).

Usage example
1
2
3
4
5
const mat = require('mathx/mat');
const a = mat.Dense(3, 3, [11,12,13,21,22,23,31,32,33]);
console.println("at(1,2):", a.at(1,2));

// at(1,2): 23

set()

Sets the value at the specified position.

Usage example
1
2
3
4
5
6
7
8
const mat = require('mathx/mat');
const a = mat.Dense(3, 3, [11,12,13,21,22,23,31,32,33]);
a.set(2,2,55)
console.println(mat.format(a, {format:"a = %v",  prefix: "    "}))

// a = ⎡11  12  13⎤
//     ⎢21  22  23⎥
//     ⎣31  32  55⎦

t()

Creates a transposed matrix.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const mat = require('mathx/mat');
A = mat.Dense(2, 2, [
    1, 2,
    3, 4,
]);
console.println(mat.format(A, {format:"A=%v", prefix:"  "}));
B = A.t();
console.println(mat.format(B, {format:"B=%v", prefix:"  "}));

// A=⎡1  2⎤
//   ⎣3  4⎦
// B=⎡1  3⎤
//   ⎣2  4⎦

add()

Performs matrix addition.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
const mat = require('mathx/mat');
A = mat.Dense(2, 2, [
    1, 2,
    3, 4,
]);
B = mat.Dense(2, 2, [
    10, 20,
    30, 40,
]);
C = mat.Dense();
C.add(A, B); // C = A + B
console.println(mat.format(C));

// ⎡11 22⎤ 
// ⎣33 44⎦

sub()

Performs matrix subtraction.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
const mat = require('mathx/mat');
A = mat.Dense(2, 2, [
    1, 2,
    3, 4,
]);
B = mat.Dense(2, 2, [
    10, 20,
    30, 40,
]);
C = mat.Dense();
C.sub(B, A); // C = B - A
console.println(mat.format(C));

// ⎡ 9 18⎤ 
// ⎣27 36⎦

mul()

Performs matrix multiplication.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
const mat = require('mathx/mat');
A = mat.Dense(2, 2, [
    1, 2,
    3, 4,
]);
B = mat.Dense(2, 2, [
    10, 20,
    30, 40,
]);
C = mat.Dense();
C.mul(A, B); // C = A * B
console.println(mat.format(C));

// ⎡ 70 100⎤ 
// ⎣150 220⎦

mulElem()

Performs element-wise multiplication.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
const mat = require('mathx/mat');
A = mat.Dense(2, 2, [
    1, 2,
    3, 4,
]);
B = mat.Dense(2, 2, [
    10, 20,
    30, 40,
]);
C = mat.Dense();
C.mulElem(A, B);
console.println(mat.format(C));

// ⎡ 10 40⎤ 
// ⎣ 90 160⎦

divElem()

Performs element-wise division.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
const mat = require('mathx/mat');
A = mat.Dense(2, 2, [
    1, 2,
    3, 4,
]);
B = mat.Dense(2, 2, [
    10, 20,
    30, 40,
]);
C = mat.Dense();
C.divElem(A, B);
console.println(mat.format(C));

// ⎡0.1  0.1⎤
// ⎣0.1  0.1⎦

inverse()

Calculates the inverse matrix.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
const mat = require('mathx/mat');
A = mat.Dense(2, 2, [
    1, 2,
    3, 4,
]);
B = mat.Dense();
B.inverse(A);
C = mat.Dense();
C.mul(A, B);
console.println(mat.format(B, {format:"B=%.f", prefix:"  "}));
console.println(mat.format(C, {format:"C=%.f", prefix:"  "}));

//B=⎡-2  1⎤
//  ⎣ 1 -0⎦
//C=⎡1 0⎤
//  ⎣0 1⎦

solve()

Solves the linear equation A * X = B and returns the solution.

exp()

Computes the matrix exponential.

pow()

Performs matrix exponentiation.

scale()

Scales the matrix by a scalar value.

VecDense

A dense vector type.

Creation
VecDense(n, data)
Parameters
  • n Number vector length (must be greater than 0)
  • data Number[] element array (filled with zeros if omitted)

cap()

Returns the internal buffer capacity.

len()

Returns the vector length.

atVec()

Returns the value at the specified index.

setVec()

Sets the value at the specified index.

addVec()

Adds two vectors and stores the result.

subVec()

Performs vector subtraction.

mulVec()

Computes matrix-vector multiplication.

mulElemVec()

Computes element-wise multiplication.

scaleVec()

Scales the vector by a scalar.

solveVec()

Solves a linear system and returns the vector solution.

QR

QR factorization is a decomposition of a matrix A into a product A = QR of an orthonormal matrix Q and an upper triangular matrix R. QR decomposition is often used to solve the linear least squares (LLS) problem and is the basis for a particular eigenvalue algorithm, the QR algorithm.

Any real matrix A may be decomposed as

A = QR

where Q is an orthogonal matrix and R is an upper triangular matrix. If A is invertible, then the factorization is unique if we require the diagonal elements of R to be positive.

Usage example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const m = require('mathx/mat');
A = m.Dense(4, 2, [
    0, 1,
    1, 1,
    1, 1,
    2, 1,
]);

qr = m.QR();
qr.factorize(A);

Q = m.Dense();
qr.qTo(Q);

R = m.Dense();
qr.rTo(R);

B = m.Dense(4, 1, [1, 0, 2, 1]);
x = m.Dense();
qr.solveTo(x, false, B);
console.println(m.format(x, { format: "x = %.2f", prefix: "    " }));

// x = ⎡0.00⎤
//     ⎣1.00⎦

format()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const m = require("mathx/mat");
A = m.Dense(100, 100);
for (let i = 0; i < 100; i++) {
    for (let j = 0; j < 100; j++) {
        A.set(i, j, i + j);
    }
}
console.println(m.format(A, {
    format: "A = %v",
    prefix: "    ",
    squeeze: true,
    excerpt: 3,
}));

// A = Dims(100, 100)
//     ⎡ 0    1    2  ...  ...   97   98   99⎤
//     ⎢ 1    2    3             98   99  100⎥
//     ⎢ 2    3    4             99  100  101⎥
//      .
//      .
//      .
//     ⎢97   98   99            194  195  196⎥
//     ⎢98   99  100            195  196  197⎥
//     ⎣99  100  101  ...  ...  196  197  198⎦
Last updated on