npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

matrix-core

v1.0.4

Published

A Small and Efficient Matrix library

Downloads

2

Readme

Matrix Library {small and Efficient}

CircleCI Build Coverage Status

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:

<script src="/node_modules/matrix-core/dist/matrix-core.min.js"></script>

<!-- now you have access to Matrix obj -->

<!-- Example -->
<script>

 Matrix.add([1,2],[3,4]); // [4,6]
 
</script>

In Node

  const matrix = require('matrix-core');

  // or 

  const {add , mul , identity} = require('matrix-core');

If you want tree shaking:

const add = require('matrix-core/add');
const identity = require('matrix-core/identity');
const transpose = require('matrix-core/transpose');

If you want to get all the speed possible

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 = require('matrix-core/add'); // with Error handling

// Or

const add = require('matrix-core/_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 = [[1,2,3]]; // a matrix with (1 row and 3 columns)

matrix = [1,2,3]; // this is the same as above (1 row and 3 columns)

matrix = [[1,2] , [3,4]]; // (2 rows and 2 columns)


matrix = [[1,2] , [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 = require('matrix-core/invalidityReasoner');

const isValid = require('matrix-core/isValid');

const matrix = [1,[2]];

console.log(invalidityReasoner(matrix)); // logs : rows in matrix do not match

// or 


console.log(isValid(matrix));// 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 [Not pure] 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} = require('matrix-core');
// Or
const add = require('matrix-core/add');
// Or for fast speed
const add = require('matrix-core/_add');

const m1 = [[1,2]];
const m2 = [[3,4]];

const result = add(m1 , m2); // [[4 , 6]]

Sub

const {sub} = require('matrix-core');

const m1 = [1 , 2];
const m2 = [3 , 4];

const result = sub(m1 , m2); // [-2,-2]

Scale

const {scale} = require('matrix-core');

const matrix = [[1,2] , [3,4]];

const result = scale(m , 2); // [[2,4] ,[6,8]]

Mul

const {mul} = require('matrix-core');

const m1 = [[1, 2], [3, 4]];
const m2 = [[5, 6], [7, 8]];
const result = mul(m1, m2); // [[19, 22], [43, 50]]

Transpose

const {transpose} = require('matrix-core');

const matrix = [[1, 2], [3, 4]];

const result = transpose(m); // [[1 , 3], [2 , 4]]

Det

[Not pure]

const {det} = require('matrix-core');

const matrix = [[-1, 1, 1], [1, -2, 2], [1, -3, 3]];
const result = det(m); // result = -2

Div

const {div} = require('matrix-core');

const m1 = [[1 , 2]];
const m2 = [[2 ,2]];
const result = div(m1 , m2); // [[0.5 , 1]]

Identity

const {identity} = require('matrix-core');

const result = identity(3); 

/* 
result = 
[
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1]
]

*/

Random

const {random} = require('matrix-core');

// returns a matrix 1 x 2 
// elements are between 1 and 0
const result = random(1,2);// returns [[0 <= x1 < 1,0 <= x2 < 1]]

const result = random(2);// returns 2 x 2 matrix

// you can provide a function to generate random numbers
const rand = (i , j) => 1
const result = random(1,2,rand);// returns [[1,1]]

Stringify

const {stringify} = require('matrix-core');

const matrix = [[1, 2],[3,4]];

const strRepr = stringify(m1);
// you can give the stringify function a precision as the second arg
// default is 2

console.log(strRepr);
/*
 __        __
| 1.00  2.00 |
| 3.00  4.00 |
|__........__|

*/

Clone

const {clone} = require('matrix-core');

const matrix = [[1, 2],[3,4]];

const result = clone(m); // [[1, 2], [3, 4]]

LU

It returns the Lower and Upper triangular of matrix.

[Not pure]

const {LU} = require('matrix-core');

const matrix = [
  [3, 1],
  [4, 2]
];

[lt, ut] = LU(m);

/*
 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} = require('matrix-core');

const matrix = [[1, 2], [3, 4]];

let inv_m = inv(matrix); // [[-2, 1], [1.5, -0.5]];
 


const matrix1 = [[1, 4], [1, 4]]; // determinant = 0 --> so does not have an inverse

const result = inv(matrix1); // result = 0; means there is no inverse


MergeR

const {mergeR} = require('matrix-core');

const m1 = [[1, 2], [3, 4]];
const m2 = [[5, 5], [5, 5]];

const result = mergeR(m1, m2); 
/*
[
  [1, 2, 5, 5],
  [3, 4, 5, 5]
]
*/

MergeL

const {mergeL} = require('matrix-core');

const m1 = [[1, 2], [3, 4]];
const m2 = [[5, 5], [5, 5]];

const result = mergeL(m1, m2); 
/*
[
  [5, 5, 1, 2],
  [5, 5, 3, 4]
]
*/

Ones

const {ones} = require('matrix-core');

const result = ones(3);
/*
[
  [1, 1, 1],
  [1, 1, 1],
  [1, 1, 1]
]

*/

Zeros

const {zeros} = require('matrix-core');

const result = zeros(3);
/*
[
  [0, 0, 0],
  [0, 0, 0],
  [0, 0, 0]
]

*/

Columns

returns the number of columns

const {columns} = require('matrix-core');

// Or for tree shaking use bellow
// const {columns} = require('matrix-core/utils');


const matrix =[[1,2],[3,4]]

const result = columns(matrix) ; // 2

Column

returns the column sepcified

const {column} = require('matrix-core');

// Or for tree shaking use bellow
// const {column} = require('matrix-core/utils');


const matrix =[[1,2],[3,4]]

const result = column(matrix , 0) ; // [1,3]

Rows

returns the number of rows

const {rows} = require('matrix-core');

// Or for tree shaking use bellow
// const {rows} = require('matrix-core/utils');


const matrix =[[1,2],[3,4]]

const result = rows(matrix) ; // 2

Row

returns the row sepcified

const {row} = require('matrix-core');

// Or for tree shaking use bellow
// const {row} = require('matrix-core/utils');


const matrix =[[1,2],[3,4]]

const result = row(matrix , 0) ; // [1,2]

Feedback

Please send Bug Reports, Comments, or Questions to SC0d3r. Contributions and extensions with new algorithms are welcome.

License

The MIT License