https://github.com/lovesaroha/lmath.js
This is a generalized math package with clean and transparent API for the javascript.
https://github.com/lovesaroha/lmath.js
javascript javascript-library logistic-regression math matrix
Last synced: 6 months ago
JSON representation
This is a generalized math package with clean and transparent API for the javascript.
- Host: GitHub
- URL: https://github.com/lovesaroha/lmath.js
- Owner: lovesaroha
- License: gpl-3.0
- Created: 2021-09-17T13:11:50.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-09-20T08:21:29.000Z (over 4 years ago)
- Last Synced: 2025-07-03T03:04:12.752Z (6 months ago)
- Topics: javascript, javascript-library, logistic-regression, math, matrix
- Language: JavaScript
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## lmath.js
This is a generalized math library with clean and transparent API for the javascript. Also available for golang [github/lovesaroha/lmath](https://github.com/lovesaroha/lmath)
## Installation
```html
```
## Usage
### Random
```js
// Random value between given range.
let random = lmath.Random(-1, 1);
```
### Map Given Value Between Given Range
```js
// Return value between given range of (200, 1000).
let value = lmath.Map(20 , 0, 100, 200, 1000);
```
## Apply Math Functions
```js
// Sigmoid.
let result = lmath.Sigmoid(1.5);
// Diff of sigmoid.
let dresult = lmath.Dsigmoid(result);
// Relu.
let result = lmath.Relu(2);
// Diff of relu.
let dresult = lmath.Drelu(result);
```
### Create Matrix
```js
// Create a matrix (rows, cols).
let matrix = lmath.Matrix(4, 3);
// Print matrix shape.
matrix.Shape();
// Print matrix values.
matrix.Print();
```

### Create Random Matrix
```js
// Create a matrix (rows, cols, minimum , maximum).
let matrix = lmath.Matrix(3, 4, -1 , 1);
// Print matrix shape.
matrix.Shape();
// Print matrix values.
matrix.Print();
```

### Convert Array Into Matrix
```js
// Array to matrix and print values.
lmath.ToMatrix([1, 2, 3]).Print();
lmath.ToMatrix([[1, 2], [3, 4]]).Print();
lmath.ToMatrix([4, 5, 6]).Print();
lmath.ToMatrix([[7, 8], [9, 0]]).Print();
```

### Matrix Element Wise Operations (Add, Subtract, Multiply, Divide)
```js
// Create a matrix (rows, cols, minimum , maximum).
let matrix = lmath.Matrix(3, 4, 10, 20);
let matrixB = lmath.Matrix(3, 4, 0, 10);
// Add and print values.
matrix.Add(matrixB).Print();
// Subtract and print values.
matrix.Sub(matrixB).Print();
// Multiply and print values.
matrix.Mul(matrixB).Print();
// Divide and print values.
matrix.Sub(matrixB).Print();
```

### Matrix Element Wise Operations With Scalar Value (Add, Subtract, Multiply, Divide)
```js
// Create a matrix (rows, cols, minimum , maximum).
let matrix = lmath.Matrix(3, 4, 10, 20);
// Add and print values.
matrix.Add(2).Print();
// Subtract and print values.
matrix.Sub(2).Print();
// Multiply and print values.
matrix.Mul(2).Print();
// Divide and print values.
matrix.Sub(2).Print();
```

### Matrix Dot Product
```js
// Create a matrix (rows, cols, minimum , maximum).
let matrix = lmath.Matrix(3, 4, 10, 20);
let matrixB = lmath.Matrix(4, 3, 0, 10);
// Dot product and print values.
matrix.Dot(matrixB).Print();
```

### Matrix Transpose
```js
// Create a matrix (rows, cols, minimum , maximum).
let matrix = lmath.Matrix(3, 4, 10, 20);
// Print values.
matrix.Print();
// Transpose and print values.
matrix.Transpose().Print();
```

### Add All Column Values
```js
// Create a matrix (rows, cols, minimum , maximum).
let matrix = lmath.Matrix(3, 4, 10, 20);
// Print values.
matrix.Print();
// Add columns and print values.
matrix.AddCols().Print();
```

### Change Matrix Values (Map)
```js
// Create a matrix (rows, cols, minimum , maximum).
let matrix = lmath.Matrix(3, 4, 10, 20);
// Print values.
matrix.Print();
// Square and print values.
matrix.Map(function(x) {
return x * x;
}).Print();
```

### Apply Function In Matrix
```js
// Create a matrix (rows, cols, minimum , maximum).
let matrix = lmath.Matrix(3, 4, -1, 1);
// Print values.
matrix.Print();
// Apply sigmoid and print values.
matrix.Map(lmath.Sigmoid).Print();
```

## Examples
### Flappy-Bird-Game-AI
A flappy bird game AI [github.com/lovesaroha/Flappy-Bird-Game-AI](https://github.com/lovesaroha/Flappy-Bird-Game-AI) trained with this library.

[lovesaroha/p/Flappy-Bird-Game-AI](https://lovesaroha.com/p/Flappy-Bird-Game-AI)
### Logistic Regression (OR Gate)
```js
// Learning rate.
let learningRate = 0.2;
// Inputs and outputs.
let inputs = lmath.ToMatrix([[1, 1, 0, 0] , [0, 1, 0, 1]]);
let outputs = lmath.ToMatrix([[1, 1, 0, 1]]);
// Weights and bias.
let weights = lmath.Matrix(2, 1, -1, 1);
let bias = lmath.Random(-1, 1);
// Train weights and bias (epochs 1000).
for(let i = 0; i < 1000; i++) {
// Sigmoid(wx + b).
let prediction = weights.Transpose().Dot(inputs).Add(bias).Map(lmath.Sigmoid);
let dZ = prediction.Sub(outputs);
weights = weights.Sub(inputs.Dot(dZ.Transpose()).Divide(4).Mul(learningRate));
bias -= dZ.Sum() / 4;
}
// Show prediction.
weights.Transpose().Dot(inputs).Add(bias).Map(lmath.Sigmoid).Print();
```
