Skip to content

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

License

Notifications You must be signed in to change notification settings

lovesaroha/lmath

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

lmath

This is a generalized math package with clean and transparent API for the Go language. Also available for javascript github/lovesaroha/lmath.js

Features

  • Lightweight and Fast.
  • Native Go implementation.

Requirements

  • Go 1.9 or higher. We aim to support the 3 latest versions of Go.

Installation

Simple install the package to your $GOPATH with the go tool from shell:

go get -u github.com/lovesaroha/lmath

Make sure Git is installed on your machine and in your system's PATH.

Usage

Random

  // Random float64 between given range.
  random := lmath.Random(-1, 1)

Map Given Value Between Given Range

  // Return float64 between given range of (200, 1000).
  result := lmath.Map(20 , 0, 100, 200, 1000)

Apply Math Functions

  // Sigmoid.
  result := lmath.Sigmoid(1.5)
  // Diff of sigmoid.
  dresult := lmath.Dsigmoid(result)
  // Relu.
  result := lmath.Relu(2)
  // Diff of relu.
  dresult := lmath.Drelu(result)

Create Matrix

  // Create a matrix (rows, cols).
  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).
  matrix := lmath.Matrix(3, 4, -1 , 1)

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

image

Convert Slice Into Matrix

    // Slice of int to matrix and print values.
    lmath.ToMatrix([]int{1, 2, 3}).Print()
    // 2d slice of int to matrix and print values.
    lmath.ToMatrix([][]int{[]int{1, 2},[]int{3, 4}}).Print()
    // Slice of float64 to matrix and print values.
     lmath.ToMatrix([]float64{4, 5, 6}).Print()
    // 2d slice of float64 to matrix and print values.
    lmath.ToMatrix([][]float64{[]float64{7, 8},[]float64{9, 0}}).Print()

image

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

  // Create a matrix (rows, cols, minimum , maximum).
  matrix := lmath.Matrix(3, 4, 10, 20)
  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).
  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).
  matrix := lmath.Matrix(3, 4, 10, 20)
  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).
  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).
  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).
  matrix := lmath.Matrix(3, 4, 10, 20)

  // Print values.
  matrix.Print()
  // Square and print values.
  matrix.Map(func (value float64) float64 {
    return value * value
  }).Print()

image

Apply Function In Matrix

  // Create a matrix (rows, cols, minimum , maximum).
  matrix := lmath.Matrix(3, 4, -1, 1)

  // Print values.
  matrix.Print()
  // Apply sigmoid and print values.
  matrix.Map(lmath.Sigmoid).Print()

image

Examples

Logistic Regression (OR Gate)

    // Learning rate.
    var learningRate = 0.2

    // Inputs and outputs.
    inputs := lmath.ToMatrix([][]float64{[]float64{1, 1, 0, 0},[]float64{0, 1, 0, 1}})
    outputs := lmath.ToMatrix([][]float64{[]float64{1, 1, 0, 1}})

    // Weights and bias.
    weights := lmath.Matrix(2, 1, -1, 1)
    bias := lmath.Random(-1, 1)

    // Train weights and bias (epochs 1000).
    for i := 0; i < 1000; i++ {
      // Sigmoid(wx + b).
      prediction := weights.Transpose().Dot(inputs).Add(bias).Map(lmath.Sigmoid)
      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 Go language.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages