Skip to content

A Go-based symbolic algebra engine for parsing, simplifying, and manipulating mathematical expressions. Designed for compilers, research tooling, and symbolic computation. A symbolic math expression simplifier and optimizer.

License

Notifications You must be signed in to change notification settings

BaseMax/go-math-opt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-math-opt

A Go-based symbolic algebra engine for parsing, simplifying, and manipulating mathematical expressions. Designed for compilers, research tooling, and symbolic computation.

Features

  • Expression Parsing: Parse mathematical expressions from strings into Abstract Syntax Trees (AST)
  • Symbolic Differentiation: Compute derivatives symbolically using calculus rules
  • Expression Simplification: Simplify expressions using algebraic rules
  • Constant Folding: Evaluate constant subexpressions at compile time
  • Expression Equivalence: Check if two expressions are mathematically equivalent
  • Algebraic Optimizations: Apply rules like identity, associativity, and distributivity
  • Multiple Operations: Support for +, -, *, /, ^ (power)
  • Functions: Support for sin, cos, exp, ln, and unary minus

Installation

go get github.com/BaseMax/go-math-opt

Usage

Basic Parsing and Simplification

package main

import (
    "fmt"
    "github.com/BaseMax/go-math-opt"
)

func main() {
    // Parse an expression
    expr, err := mathopt.Parse("x + 0")
    if err != nil {
        panic(err)
    }
    
    // Simplify it
    simplified := expr.Simplify()
    fmt.Println(simplified.String()) // Output: x
}

Symbolic Differentiation

// Parse a polynomial
expr, _ := mathopt.Parse("x ^ 2 + 2 * x + 1")

// Compute derivative with respect to x
derivative := expr.Diff("x")

// Simplify the result
simplified := derivative.Simplify()
fmt.Println(simplified.String()) // Output: 2 * x + 2

Constant Folding

expr, _ := mathopt.Parse("(2 + 3) * (4 + 1)")
folded := mathopt.ConstantFold(expr)
fmt.Println(folded.String()) // Output: 25

Expression Evaluation

expr, _ := mathopt.Parse("x ^ 2 + 2 * x + 1")
vars := map[string]float64{"x": 3}
result, _ := expr.Eval(vars)
fmt.Println(result) // Output: 16

Expression Equivalence

e1, _ := mathopt.Parse("x + 0")
e2, _ := mathopt.Parse("x")
equivalent := mathopt.ExpressionEquivalence(e1, e2)
fmt.Println(equivalent) // Output: true

Supported Operations

Binary Operations

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Exponentiation: ^

Unary Operations

  • Negation: -x
  • Sine: sin(x)
  • Cosine: cos(x)
  • Exponential: exp(x)
  • Natural logarithm: ln(x)

Algebraic Simplifications

The engine automatically applies these simplification rules:

  • 0 + x = x
  • x + 0 = x
  • 1 * x = x
  • x * 1 = x
  • 0 * x = 0
  • x * 0 = 0
  • x ^ 0 = 1
  • x ^ 1 = x
  • x - 0 = x
  • x / 1 = x
  • 0 / x = 0
  • x - x = 0
  • x / x = 1
  • Constant folding for all numeric operations

Differentiation Rules

The engine implements standard calculus differentiation rules:

  • Constant Rule: d/dx(c) = 0
  • Power Rule: d/dx(x^n) = n * x^(n-1)
  • Sum Rule: d/dx(f + g) = df/dx + dg/dx
  • Product Rule: d/dx(f * g) = f * dg/dx + g * df/dx
  • Quotient Rule: d/dx(f / g) = (g * df/dx - f * dg/dx) / g^2
  • Chain Rule: Applied for composite functions
  • Trigonometric: d/dx(sin(x)) = cos(x), d/dx(cos(x)) = -sin(x)
  • Exponential: d/dx(exp(x)) = exp(x)
  • Logarithmic: d/dx(ln(x)) = 1/x

Examples

See the examples directory for more comprehensive examples.

Run the demo:

cd examples
go run demo.go

API Reference

Core Types

Expr Interface

All expressions implement this interface:

  • String() string - Returns string representation
  • Eval(vars map[string]float64) (float64, error) - Evaluates the expression
  • Diff(variable string) Expr - Computes the derivative
  • Simplify() Expr - Simplifies the expression
  • Equals(other Expr) bool - Checks structural equivalence

Concrete Types

  • Constant - Numeric constant
  • Variable - Symbolic variable
  • BinaryOp - Binary operation (+, -, *, /, ^)
  • UnaryOp - Unary operation (-, sin, cos, exp, ln)

Functions

Parse(input string) (Expr, error)

Parses a mathematical expression from a string.

MustParse(input string) Expr

Like Parse but panics on error (useful for tests).

ExpressionEquivalence(e1, e2 Expr) bool

Checks if two expressions are equivalent after simplification.

ConstantFold(expr Expr) Expr

Performs aggressive constant folding until a fixpoint is reached.

NormalizeExpression(expr Expr) Expr

Normalizes an expression by applying simplifications.

Use Cases

  • Compiler Optimization: Simplify expressions in intermediate representations
  • Symbolic Mathematics: Perform symbolic computations without numeric solving
  • Research Tools: Build prototypes for mathematical software
  • Education: Demonstrate calculus and algebra rules programmatically
  • Code Generation: Generate optimized code from mathematical expressions

Design Philosophy

This library focuses on pure symbolic manipulation without numeric solving. It's designed for:

  • Compiler writers who need expression optimization
  • Researchers building mathematical tools
  • Educational purposes to demonstrate algebraic transformations
  • Systems that need to manipulate mathematical expressions symbolically

Testing

go test -v

All core functionality is thoroughly tested with comprehensive test coverage.

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

Author

Max Base (@BaseMax)

About

A Go-based symbolic algebra engine for parsing, simplifying, and manipulating mathematical expressions. Designed for compilers, research tooling, and symbolic computation. A symbolic math expression simplifier and optimizer.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages