Navigation: Docs/Reference | Demo Playground | Github Source
Math-Script
A math library for JS with an emphasis on matrix operations, non linear solvers, visualizations, and overload support!.
var M = require('math-script');
M.execute(function(M){
var X = M.range(0,0.1,10);
var Y = X.pow(3).multiply(-1.3).add(X.pow(2).multiply(13)).add(X.multiply(-5)).add(30);
Y = -1.3*X**3 + 13*X**2 + -5*X + 30;
console.log(Y.print());
});
Core features:
- Matrix operations operator support in execute environment
*, +, **, -
- Feature rich [M,N] matrix operation support, see reference
- SVD support / Nonlinear solvers
Install
NPM Installation:
npm install math-script
Browser minimal:
See dist folder for files
<script type="text/javascript" src="math-script.js"></script>
Math-Script with Plotting
<script type="text/javascript" src="plotly.min1.19.2.js"></script>
<script type="text/javascript" src="math-script-full.js"></script>
Library Application Sample
Linear Regression Example:
This example uses linear least squares to identify a best fitting polynomial to arbitrary data. This example demonstrates the matrix and plotting functionality of libary.
var X = Matrixs.range(-1.2, 0.01, 1.2);
var Y = X.pow(3).subtract(X).addNoise(0.9);
var A = X.pow(3).catH(X.pow(2)).catH(X).catH(Matrixs.ones(X.length(),1));
var x = A.lsq(Y);
var yFit = A.multiply(x);
plotyLayout.showlegend= false;
plotyLayout.title= 'Least Square Example';
Plots.create([X,Y],{type:'scatter'});
Plots.add([X,yFit]);
Basic Matrix Examples
Maths.js supports numerous matrixs opperations. Below is small sampling and see here for the matrixs reference.
Matrix Creation:
var A = Matrixs.make([[1,2,3],[4,5,6],[7,8,5]);
A.print();
Matrix Opperations (Addition, Multiplicaiton ...ect):
One can add or perform any basic matrix opperation with scalars or other matricies. These commands can be sequentially chained.
var A = Matrixs.make([[1,2,3],[4,5,6],[7,8,5]);
A.add(1).add([[1,2,3],[0,0,0],[0,0,0]]).print();
Matrix Inversion:
Maths.js can invert arbitrary sized(e.g. 100x100) singular and non singular(with SVD) matricies.
var A = new Matrixs([[1,2,3],[4,5,6],[7,8,9]]);
A.invert().print();
Basic Plot Example
Simple Line and Scatter Plot :
Maths.js can easily create plots for visualization. Please see here for the plot reference. . The backbone of this
library is plotly whos backbone is D3 meaning the plots are SVG based.
var X = Matrixs.range(100);
var Y = X.addNoise(0.9);
Plots.create([Y],{type:'scatter'});
Plots.add([X]);
License: MIT
https://opensource.org/licenses/MIT