Symbolic math for ruby.

Installation:

gem install symbolic

Introduction:

Symbolic math can be really helpful if you want to simplify some giant equation or if you don’t want to get a performance hit re-evaluating big math expressions every time when few variables change.

Currently I’ve implemented only naive optimizations to simplify math expressions, but it’s pretty simple to add your own - see Symbolic::Optimizations module for examples.

Examples:

x = var :name => 'x'
y = var :name => 'y'

f = symbolic { (4 - 2*(-x) + y*(-1.0))*x - 0*y + 0 - 2 }
puts f # => (4+2*x-y)*x-2

p f.undefined_variables.map &:name # => ["x", "y"]
x.value = 2
y.value = 4

puts f.value # => 6

g = symbolic { Math.cos(y) }
puts g # => cos(y)
y.value = 0
puts g.value # => 1.0