Matrix
Library {small and Efficient
}
Matrix-core is a small library for handling matrices , At its core it is made only from functions there is no object involved , So this gives more speed and less unnessary complexity and most importantly by combining it with a bundler like Webpack , ... which does Tree Shaking you can get only the functions you need not the entire jungle!
Installation
npm
> npm i matrix-core
In browser
Then add a <script>
to your index.html:
<!-- now you have access to Matrix obj --> <!-- Example -->
In Node
const matrix = ; // or const add mul identity = ;
If you want tree shaking:
const add = ;const identity = ;const transpose = ;
speed
possible
If you want to get all the every method in this library has a cousin with the same name following an Underscore
. Functions with Underscores
do not have any Error Handling Codes
which makes them much much Faster
than their counterparts , But make sure you pass valid matrices to them or you can use invalidityReasoner to check a matrix if it is valid or not and then pass that matrix to the function with underscore.
For example :
const add = ; // with Error handling // Or const add = ; // this is way faster but without Error handling
A Short Explanation
every inner array
= A row
in matrix
every ,
= A column
in matrix
For example :
let matrix = 123; // a matrix with (1 row and 3 columns) matrix = 123; // this is the same as above (1 row and 3 columns) matrix = 12 34; // (2 rows and 2 columns) matrix = 12 3 ; // Error : this matrix is not valid // Reason : (first row has 2 columns but second row has only 1 column)
Error Handling
you can use invalidityReasoner
to ensure if a matrix is valid or not it returns a text message containing the reasons for invalidity or an empty string if it is valid.
or you can use isValid
function to get a boolean true
or false
Example:
const invalidityReasoner = ; const isValid = ; const matrix = 12; console; // logs : rows in matrix do not match // or console;// logs : false
Methods
[ add ] [ sub ] [ scale ] [ mul ] [ transpose ] [ det ] [ div ] [ identity ] [ random ] [ stringify ] [ clone ] [ LU ] [ inv ] [ mergeR ] [ mergeL ] [ ones ] [ zeros ] [ columns ] [ column ] [ rows ] [ row ]
Note :
inv
,det
,LU
functions have [Notpure
] tag this means that these function change their parameter(=matrix
) for getting the most speed, These functions have a pure version which you can use instead :pInv
,pDet
,PLU
Add
const add = ;// Orconst add = ;// Or for fast speedconst add = ; const m1 = 12;const m2 = 34; const result = ; // [[4 , 6]]
Sub
const sub = ; const m1 = 1 2;const m2 = 3 4; const result = ; // [-2,-2]
Scale
const scale = ; const matrix = 12 34; const result = ; // [[2,4] ,[6,8]]
Mul
const mul = ; const m1 = 1 2 3 4;const m2 = 5 6 7 8;const result = ; // [[19, 22], [43, 50]]
Transpose
const transpose = ; const matrix = 1 2 3 4; const result = ; // [[1 , 3], [2 , 4]]
Det
[Not pure
]
const det = ; const matrix = -1 1 1 1 -2 2 1 -3 3;const result = ; // result = -2
Div
const div = ; const m1 = 1 2;const m2 = 2 2;const result = ; // [[0.5 , 1]]
Identity
const identity = ; const result = ; /* result = [ [1, 0, 0], [0, 1, 0], [0, 0, 1]] */
Random
const random = ; // returns a matrix 1 x 2 // elements are between 1 and 0const result = ;// returns [[0 <= x1 < 1,0 <= x2 < 1]] const result = ;// returns 2 x 2 matrix // you can provide a function to generate random numbersconst rand = 1const result = ;// returns [[1,1]]
Stringify
const stringify = ; const matrix = 1 234; const strRepr = ;// you can give the stringify function a precision as the second arg// default is 2 console;/* __ __| 1.00 2.00 || 3.00 4.00 ||__........__| */
Clone
const clone = ; const matrix = 1 234; const result = ; // [[1, 2], [3, 4]]
LU
It returns the Lower
and Upper
triangular of matrix.
[Not pure
]
const LU = ; const matrix = 3 1 4 2; lt ut = ; /* lt (or lower triangular) = [ [1, 0], [1.3333333333333333, 1] ]; ut (or upper triangular) = [ [3, 1], [0, 0.6666666666666667] ]; Note : if matrix has no LU form it returns lt = 0 , ut = 0*/
Inv
[Not pure
]
const inv = ; const matrix = 1 2 3 4; let inv_m = ; // [[-2, 1], [1.5, -0.5]]; const matrix1 = 1 4 1 4; // determinant = 0 --> so does not have an inverse const result = ; // result = 0; means there is no inverse
MergeR
const mergeR = ; const m1 = 1 2 3 4;const m2 = 5 5 5 5; const result = ; /*[ [1, 2, 5, 5], [3, 4, 5, 5]]*/
MergeL
const mergeL = ; const m1 = 1 2 3 4;const m2 = 5 5 5 5; const result = ; /*[ [5, 5, 1, 2], [5, 5, 3, 4]]*/
Ones
const ones = ; const result = ;/*[ [1, 1, 1], [1, 1, 1], [1, 1, 1]] */
Zeros
const zeros = ; const result = ;/*[ [0, 0, 0], [0, 0, 0], [0, 0, 0]] */
Columns
returns the number
of columns
const columns = ; // Or for tree shaking use bellow// const {columns} = require('matrix-core/utils'); const matrix =1234 const result = ; // 2
Column
returns the column
sepcified
const column = ; // Or for tree shaking use bellow// const {column} = require('matrix-core/utils'); const matrix =1234 const result = ; // [1,3]
Rows
returns the number
of rows
const rows = ; // Or for tree shaking use bellow// const {rows} = require('matrix-core/utils'); const matrix =1234 const result = ; // 2
Row
returns the row
sepcified
const row = ; // Or for tree shaking use bellow// const {row} = require('matrix-core/utils'); const matrix =1234 const result = ; // [1,2]
Feedback
Please send Bug Reports
, Comments
, or Questions
to SC0d3r. Contributions and extensions with new algorithms are welcome.