Skip to content

This is a generalized math package with clean and transparent API for the javascript.

License

Notifications You must be signed in to change notification settings

lovesaroha/lmath.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

lmath.js

This is a generalized math library with clean and transparent API for the javascript. Also available for golang github/lovesaroha/lmath

Installation

    <script type="text/javascript" src="lmath.js"></script>

Usage

Random

  // Random value between given range.
  let random = lmath.Random(-1, 1);

Map Given Value Between Given Range

  // Return value between given range of (200, 1000).
  let value = lmath.Map(20 , 0, 100, 200, 1000);

Apply Math Functions

  // 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

  // Create a matrix (rows, cols).
  let matrix = lmath.Matrix(4, 3);

  // Print matrix shape.
  matrix.Shape();
  // Print matrix values.
  matrix.Print();

image

Create Random Matrix

  // 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();

image

Convert Array Into Matrix

    // 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();

image

Matrix Element Wise Operations (Add, Subtract, Multiply, Divide)

  // 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();

image

Matrix Element Wise Operations With Scalar Value (Add, Subtract, Multiply, Divide)

  // 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();

image

Matrix Dot Product

  // 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();

image

Matrix Transpose

  // 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();

image

Add All Column Values

  // 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();

image

Change Matrix Values (Map)

  // 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();

image

Apply Function In Matrix

  // 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();

image

Examples

Flappy-Bird-Game-AI

A flappy bird game AI github.com/lovesaroha/Flappy-Bird-Game-AI trained with this library.

game

lovesaroha/p/Flappy-Bird-Game-AI

Logistic Regression (OR Gate)

    // 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();

image

About

This is a generalized math package with clean and transparent API for the javascript.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published