Skip to content

Commit a5221d8

Browse files
committed
Improvements to lambdify, to_proc
1 parent f871359 commit a5221d8

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

lib/symengine.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +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, syms)
36+
def lambdify(exp, *syms)
3737
eval(SymEngine::Utils::lambdify_code(exp, syms))
3838
end
3939
end
4040
module Utils
4141
class << self
42+
REPLACEMENTS = { sin: 'Math.sin', cos: 'Math.cos', tan: 'Math.tan',
43+
asin: 'Math.asin', acos: 'Math.acos', atan: 'Math.atan',
44+
sinh: 'Math.sinh', cosh: 'Math.cosh', tanh: 'Math.tanh',
45+
asinh: 'Math.asinh', acosh: 'Math.acosh', atanh: 'Math.atanh',
46+
pi: 'Math::PI', E: 'Math::E',
47+
dirichlet_eta: 'SymEngine::Utils::evalf_dirichlet_eta',
48+
zeta: 'SymEngine::Utils::evalf_zeta', gamma: 'Math.gamma' }.map { |from, to| [/(\b#{from}\b)/, to] }.to_h.freeze
4249
def evalf_dirichlet_eta(exp)
4350
SymEngine::evalf(SymEngine::dirichlet_eta(exp))
4451
end
@@ -49,14 +56,7 @@ def lambdify_code(exp, syms)
4956
str = exp.to_s
5057
sym_map = syms.join(",")
5158
str.gsub!(/[\d\.]+/, 'Rational(\0,1)')
52-
replacements = { sin:"Math.sin", cos: "Math.cos", tan: "Math.tan",
53-
asin:"Math.asin", acos: "Math.acos", atan: "Math.atan",
54-
sinh:"Math.sinh", cosh: "Math.cosh", tanh: "Math.tanh",
55-
asinh:"Math.asinh", acosh: "Math.acosh", atanh: "Math.atanh",
56-
pi: "Math::PI", E: "Math::E",
57-
dirichlet_eta: "SymEngine::Utils::evalf_dirichlet_eta",
58-
zeta: "SymEngine::Utils::evalf_zeta", gamma: "Math.gamma" }
59-
replacements.each {|key, value| str.gsub!(/(\b#{key}\b)/, value)}
59+
str = REPLACEMENTS.inject(str) { |res, (from, to)| res.gsub(from, to)}
6060
"lambda { | #{sym_map} | #{str} }"
6161
end
6262
end

lib/symengine/basic.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ def free_symbols
1313
def abs
1414
SymEngine::abs(self)
1515
end
16-
def to_proc()
17-
if self.free_symbols.count > 1
18-
raise ArgumentError, "Too many free symbols! Only 1 allowed. Found #{self.free_symbols.count}."
16+
def to_proc(*args)
17+
if(args.length == 0)
18+
if self.free_symbols.count > 1
19+
raise ArgumentError, "Too many free symbols! Only 1 allowed. Found #{self.free_symbols.count}."
20+
end
21+
SymEngine::lambdify(self, self.free_symbols.map {|s| s})
22+
else
23+
if self.free_symbols.count > args.length
24+
raise ArgumentError, "#{self.free_symbols.count} Free Symbols. Only #{args.length} given."
25+
end
26+
SymEngine::lambdify(self, args)
1927
end
20-
SymEngine::lambdify(self, self.free_symbols.map {|s| s.to_s})
2128
end
2229
end
2330
end

spec/lambdify_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def l(code)
7676
it 'creates procs' do
7777
expect([1, 2, 3, 4, 5].map(&func)).to eq([10, 20, 30, 40, 50])
7878
expect { [[1, 2], [3, 4]].map(&func2) }.to raise_error(ArgumentError)
79+
expect([[1, 2], [3, 4]].map(&func2.to_proc(x, y))).to eq([13, 17])
7980
end
8081
end
8182

0 commit comments

Comments
 (0)