Skip to content

Commit a63684d

Browse files
committed
Lambdify as a module level function
1 parent c9dce78 commit a63684d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/symengine.rb

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ def Function(n)
3333
def evalf(operand, prec=53, real=false)
3434
return _evalf(operand, prec, real)
3535
end
36+
def lambdify(exp)
37+
sym_map = exp.free_symbols.map { |sym| sym.to_s}.join(",")
38+
str = exp.to_s
39+
str.gsub!(/[\d\.]+/, 'Rational(\0,1)')
40+
replacements = [
41+
["asin", "Math.asin"], ["acos", "Math.acos"], ["atan","Math.atan"],
42+
["sin", "Math.sin"], ["cos", "Math.cos"], ["tan","Math.tan"],
43+
["asinh", "Math.asinh"], ["acosh", "Math.acosh"], ["atanh","Math.atanh"],
44+
["sinh", "Math.sinh"], ["cosh", "Math.cosh"], ["tanh","Math.tanh"],
45+
["pi", "Math::PI"], ["E", "Math::E"] ]
46+
replacements.each {|replacement| str.gsub!(/(\b#{replacement[0]}\b)/, replacement[1])}
47+
return eval("lambda { |"+ sym_map +"| "+ str +" }")
48+
end
3649
end
3750
end
3851

0 commit comments

Comments
 (0)