class-matrix

matrix, class for two dimension arrays


Keywords
2d, array, math, matrix, two dimension, two dimensional, class, prototype, 2darrays, array-manipulations, node-js, pure-javascript
License
ISC
Install
npm install class-matrix@1.2.0

Documentation

Matrix - class for two dimension array

V1.20 : methods for math added. Refer to the function list below.

  • Easy as original Javascript prototype functions
  • All the same prototype functions of array for two dimensional arrays with adjusted arguments
  • Added functions only for this class

< Install >

needed only for node.js

$ npm i class-matrix    

< Use - node.js >

  • common JS
const Matrix = require('class-matrix');
  • ESM
import Matrix from 'class-matrix';

< Use - browser >

  • Link in <head> tag
<script src="./matrix-js-link.min.js"></script>
  • import in a module file
import { Matrix } from './matrix-js-module.min.js';

< Methods >

  • INIT & VALUE
new Matrix
.row
.column
getter value
setter value
getValueOf()
setValueOf()   
  • CHECK & FIND
Matrix.isMatrix()
at()
find()
findLast()
findIndex()
findLastIndex()
indexOf()
lastIndexOf()
includes()
  • NEW STRUCTURE
concat()
flat()
join()
toString()
slice()
  • ITERATE
forEach()
map()
reduce()
reduceRight()
filter()
every()
some()
  • CHANGE IN PLACE
fill()
pop()
push()
shift()
unshift()
splice()
sort()
reverse()
copyWithin()
  • CHANGE IN PLACE
fill()
pop()
push()
shift()
unshift()
splice()
sort()
reverse()
copyWithin()
  • GENERATOR
keys()
values()
entries()
  • MATH
det()
cofactors()
transpose()
adjoint()
inverse()
add()
subtract()
multiply()
divide()

All the prototype functions are rebuilt for this class.
In most cases, an index in callback function is seperated into a row index and a column index
Some prototype functions are added only for matrix class


< EXAMPLE >

let matrixA = new Matrix(3,3).fill((el,[i,j])=>i+j);
0 1 2
1 2 3
2 3 4
console.log(matrixA.getValueOf([2,2]));

4

matrixA.setValueOf([2,2],9);
console.log(matrixA.value);
0 1 2
1 2 3
2 3 9
matrixA.value = [[0,1],[2,3],[4,5]];
console.log(matrixA.value);
console.log(`row = ${matrixA.row}, column = ${matrixA.column}`);

// if you set value directly, it would change the structure of the matrix
0 1
2 3
4 5

row = 2, column = 2

matrixA.forEach((el, [i,j])=>{
    console.log(`[${i},${j}] = ${el}`);
});

[0,0] = 0
[0,1] = 1
[0,2] = 2
[1,0] = 1
[1,1] = 2
[1,2] = 3
[2,0] = 2
[2,1] = 3
[2,2] = 4

Click here for more details of each methods!