A Go-based symbolic algebra engine for parsing, simplifying, and manipulating mathematical expressions. Designed for compilers, research tooling, and symbolic computation.
- 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
go get github.com/BaseMax/go-math-optpackage 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
}// 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 + 2expr, _ := mathopt.Parse("(2 + 3) * (4 + 1)")
folded := mathopt.ConstantFold(expr)
fmt.Println(folded.String()) // Output: 25expr, _ := mathopt.Parse("x ^ 2 + 2 * x + 1")
vars := map[string]float64{"x": 3}
result, _ := expr.Eval(vars)
fmt.Println(result) // Output: 16e1, _ := mathopt.Parse("x + 0")
e2, _ := mathopt.Parse("x")
equivalent := mathopt.ExpressionEquivalence(e1, e2)
fmt.Println(equivalent) // Output: true- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/ - Exponentiation:
^
- Negation:
-x - Sine:
sin(x) - Cosine:
cos(x) - Exponential:
exp(x) - Natural logarithm:
ln(x)
The engine automatically applies these simplification rules:
0 + x = xx + 0 = x1 * x = xx * 1 = x0 * x = 0x * 0 = 0x ^ 0 = 1x ^ 1 = xx - 0 = xx / 1 = x0 / x = 0x - x = 0x / x = 1- Constant folding for all numeric operations
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
See the examples directory for more comprehensive examples.
Run the demo:
cd examples
go run demo.goAll expressions implement this interface:
String() string- Returns string representationEval(vars map[string]float64) (float64, error)- Evaluates the expressionDiff(variable string) Expr- Computes the derivativeSimplify() Expr- Simplifies the expressionEquals(other Expr) bool- Checks structural equivalence
Constant- Numeric constantVariable- Symbolic variableBinaryOp- Binary operation (+, -, *, /, ^)UnaryOp- Unary operation (-, sin, cos, exp, ln)
Parses a mathematical expression from a string.
Like Parse but panics on error (useful for tests).
Checks if two expressions are equivalent after simplification.
Performs aggressive constant folding until a fixpoint is reached.
Normalizes an expression by applying simplifications.
- 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
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
go test -vAll core functionality is thoroughly tested with comprehensive test coverage.
MIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit issues or pull requests.
Max Base (@BaseMax)