go get github.com/BaseMax/go-math-optimport "github.com/BaseMax/go-math-opt"expr, err := mathopt.Parse("x ^ 2 + 2 * x + 1")
if err != nil {
log.Fatal(err)
}result, err := expr.Eval(map[string]float64{"x": 5})
// result = 36simplified := expr.Simplify()
fmt.Println(simplified.String())derivative := expr.Diff("x")
fmt.Println(derivative.Simplify().String())
// Output: 2 * x + 2e1, _ := mathopt.Parse("x + 0")
e2, _ := mathopt.Parse("x")
equal := mathopt.ExpressionEquivalence(e1, e2)
// equal = trueexpr, _ := mathopt.Parse("(2 + 3) * (4 + 1)")
folded := mathopt.ConstantFold(expr)
fmt.Println(folded.String())
// Output: 25- Integers:
42,0,-5 - Decimals:
3.14,0.5,-2.718
- Single letters:
x,y,z - Multi-character:
theta,alpha,var1
| Operator | Description | Example |
|---|---|---|
+ |
Addition | x + 5 |
- |
Subtraction | x - 3 |
* |
Multiplication | 2 * x |
/ |
Division | x / 2 |
^ |
Exponentiation | x ^ 2 |
| Operator | Description | Example |
|---|---|---|
- |
Negation | -x |
sin() |
Sine | sin(x) |
cos() |
Cosine | cos(x) |
exp() |
Exponential | exp(x) |
ln() |
Natural log | ln(x) |
Use parentheses to group operations: (x + 1) * (x - 1)
- Highest: Parentheses
() - Functions:
sin(),cos(), etc. - Exponentiation:
^ - Multiplication/Division:
*,/ - Lowest: Addition/Subtraction:
+,-
// f(x) = x^3 + 2x^2 + x + 1
expr, _ := mathopt.Parse("x ^ 3 + 2 * x ^ 2 + x + 1")
derivative := expr.Diff("x").Simplify()
// f'(x) = 3x^2 + 4x + 1// f(x) = sin(x^2)
expr, _ := mathopt.Parse("sin(x ^ 2)")
derivative := expr.Diff("x").Simplify()
// f'(x) = cos(x^2) * 2x// f(x) = x * sin(x)
expr, _ := mathopt.Parse("x * sin(x)")
derivative := expr.Diff("x").Simplify()
// f'(x) = sin(x) + x*cos(x)// f(x) = x / (x + 1)
expr, _ := mathopt.Parse("x / (x + 1)")
derivative := expr.Diff("x").Simplify()
// f'(x) = 1 / (x + 1)^2expr, _ := mathopt.Parse("x * y + z")
// Partial derivative with respect to x
dfdx := expr.Diff("x").Simplify() // y
// Partial derivative with respect to y
dfdy := expr.Diff("y").Simplify() // x
// Partial derivative with respect to z
dfdz := expr.Diff("z").Simplify() // 1Returns a human-readable string representation.
expr, _ := mathopt.Parse("x + 5")
fmt.Println(expr.String()) // "x + 5"Evaluates the expression numerically with given variable values.
expr, _ := mathopt.Parse("x ^ 2 + 1")
result, err := expr.Eval(map[string]float64{"x": 3})
// result = 10Computes the symbolic derivative with respect to a variable.
expr, _ := mathopt.Parse("x ^ 2")
derivative := expr.Diff("x") // 2 * xSimplifies the expression using algebraic rules.
expr, _ := mathopt.Parse("1 * x + 0")
simplified := expr.Simplify() // xChecks if two expressions are structurally identical.
e1, _ := mathopt.Parse("x + 1")
e2, _ := mathopt.Parse("x + 1")
equal := e1.Equals(e2) // trueexpr, err := mathopt.Parse("x +")
if err != nil {
fmt.Println("Parse error:", err)
// Parse error: unexpected end of expression
}expr, _ := mathopt.Parse("x + y")
result, err := expr.Eval(map[string]float64{"x": 5})
if err != nil {
fmt.Println("Eval error:", err)
// Eval error: undefined variable: y
}expr, _ := mathopt.Parse("1 / 0")
result, err := expr.Eval(nil)
if err != nil {
fmt.Println("Math error:", err)
// Math error: division by zero
}- Always check errors from
Parse()andEval() - Simplify after differentiation for cleaner results
- Use MustParse() only in tests where panic is acceptable
- Variables are case-sensitive:
x≠X - Spaces are optional but improve readability
- Constant fold early to optimize performance
- Cache parsed expressions if used repeatedly
See the examples/demo.go file for comprehensive usage examples.
The simplifier has limited capabilities. For advanced simplification:
- Apply multiple simplification passes
- Manually restructure the expression
- Consider implementing custom simplification rules
This is expected for complex expressions. The result is mathematically correct but may need manual simplification.
For high-performance scenarios:
- Cache parsed expressions
- Pre-simplify constant parts
- Consider expression compilation to Go functions
- README - Full documentation
- ARCHITECTURE - Design documentation
- Examples - Code examples
- Tests - Usage examples in tests