matrixlib-js
v0.3.1
Published
MatrixLib.js is a JavaScript simple matrix calculation library.
Downloads
40
Maintainers
Readme
matrixlib-js
matrixlib-js is a JavaScript simple matrix calculation library.
Description
There is a single-threaded normal version matrixlib.js and a multi-threaded version of matrixlib-multithread.js.
Both APIs are the same, but multithreaded versions will return Promise objects, so you should cook and burn or love it.
It seems that it can be used for machine learning in recent times.
DEMO:
Test matrixlib-js(single thread version) in your browser.
Features
- You do not have to worry about the types of matrixes, arrays, and scalar values.
Usage
Browser
for single thread version
<script src="./js/matrixlib.js"></script>
const mt = matrixlibJs();
for multi thread version
<script src="./js/matrixlib-multithread.js"></script>
const mt = matrixLibMultiThreadJs({
libUrl: `${window.location.protocol}//${window.location.hostname}${(() => {return window.location.port ? ':' + window.location.port : '';})()}${window.location.pathname}js/matrixlib.js`
});
Browserify/Webpack
for single thread version
const mt = require('matrixlib-js/dist/matrixlib.js');
for multi thread version
const mt = require('matrixlib-js/dist/matrixlib-multithread.js')({
libUrl: 'https://webryone.github.io/matrixlib.js/js/matrixlib.js' // Please change as appropriate.
});
Sample code
// for single thread version.
//It is similar to the following code, but since the promise is not returned in the single thread version, the "async, await" keyword is not necessary.
// for multi thread version.
(async () => {
let m1 = await mt.math.matrix.util.create(3, 3, 7);
let m2 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
let m3 = [m1, m2];
console.log('m1', m1);
console.log('m2', m2);
console.log('m3', m3);
let dot = await mt.math.dot(m1, m2);
console.log('dot', dot);
let plus = await mt.math.plus(m1, m2);
console.log('plus', plus);
let minus = await mt.math.minus(m1, m2);
console.log('minus', minus);
let multi = await mt.math.multi(m1, m2);
console.log('multi', multi);
let div = await mt.math.div(m1, m2);
console.log('div', div);
let more = await mt.math.more(m1, m2);
console.log('more', more);
let less = await mt.math.less(m1, m2);
console.log('less', less);
let moreEq = await mt.math.moreEq(m1, m2);
console.log('moreEq', moreEq);
let lessEq = await mt.math.lessEq(m1, m2);
console.log('lessEq', lessEq);
let equal = await mt.math.equal(m1, m2);
console.log('equal', equal);
let notEqual = await mt.math.notEqual(m1, m2);
console.log('notEqual', notEqual);
let boolToInt = await mt.math.boolToInt(more);
console.log('boolToInt', boolToInt);
let valToAnyVal = await mt.math.valToAnyVal(more, (x) => { return !x; });
console.log('valToAnyVal', valToAnyVal);
let valToAnyVal2 = await mt.math.valToAnyVal(more, (_more, _less, _idx, _arr) => { return !_more ? 0 : _less; }, less);
console.log('valToAnyVal2', valToAnyVal2);
let sum = await mt.math.sum(m2);
console.log('sum', sum);
let sumAxis0 = await mt.math.sum(m2, 0);
console.log('sumAxis0', sumAxis0);
let sumAxis1 = await mt.math.sum(m2, 1);
console.log('sumAxis1', sumAxis1);
let sumAxis2 = await mt.math.sum(m3, 2);
console.log('sumAxis2', sumAxis2);
let pow = await mt.math.pow(m1, 2);
console.log('pow', pow);
let exp = await mt.math.exp(m2);
console.log('exp', exp);
let log = await mt.math.log(m2);
console.log('log', log);
let max = await mt.math.max(m2);
console.log('max', max);
let maxAxis0 = await mt.math.max(m2, 0);
console.log('maxAxis0', maxAxis0);
let maxAxis1 = await mt.math.max(m2, 1);
console.log('maxAxis1', maxAxis1);
let maxAxis2 = await mt.math.max(m3, 2);
console.log('maxAxis2', maxAxis2);
let maximum = await mt.math.maximum(m1, m2);
console.log('maximum', maximum);
let maxIdx = await mt.math.maxIdx(m2);
console.log('maxIdx', maxIdx);
let maxIdxAxis0 = await mt.math.maxIdx(m2, 0);
console.log('maxIdxAxis0', maxIdxAxis0);
let maxIdxAxis1 = await mt.math.maxIdx(m2, 1);
console.log('maxIdxAxis1', maxIdxAxis1);
let maxIdxAxis2 = await mt.math.maxIdx(m3, 2);
console.log('maxIdxAxis2', maxIdxAxis2);
let matchCount = await mt.math.matchCount(m1, m2);
console.log('matchCount', matchCount);
let flatten = await mt.math.flatten(m2);
console.log('flatten', flatten);
console.log(
'shape',
mt.math.util.shape(m2)
);
console.log(
'rnorm',
mt.util.rnorm(1, 0.1)
);
console.log(
'create(arr)',
mt.math.arr.util.create(10, () => { return mt.util.rnorm(1, 0.1); })
);
console.log(
'create(matrix)',
mt.math.matrix.util.create(10, 5, () => { return mt.util.rnorm(1, 0.1); })
);
console.log(
'arange',
mt.math.arr.util.arange(-5, 5, 0.1, (x) => { return Math.round(x * Math.pow(10, 1)) / Math.pow(10, 1); })
);
console.log(
'colToRow(matrix)',
mt.math.matrix.util.colToRow(m1)
);
})();
Installation
$ npm install --save matrixlib-js
or
$ yarn add matrixlib-js