Skip to content

Commit f871359

Browse files
committed
Changed structure of lambdify, added exceptions, altered tests
1 parent a2ac802 commit f871359

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

lib/symengine.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def Function(n)
3333
def evalf(operand, prec=53, real=false)
3434
return _evalf(operand, prec, real)
3535
end
36-
def lambdify(exp, syms=nil)
36+
def lambdify(exp, syms)
3737
eval(SymEngine::Utils::lambdify_code(exp, syms))
3838
end
3939
end
@@ -45,13 +45,9 @@ def evalf_dirichlet_eta(exp)
4545
def evalf_zeta(exp)
4646
SymEngine::evalf(SymEngine::zeta(exp))
4747
end
48-
def lambdify_code(exp, syms=nil)
48+
def lambdify_code(exp, syms)
4949
str = exp.to_s
50-
if syms == nil
51-
sym_map = exp.free_symbols.map {|sym| sym.to_s}.map {|s| { index: str.index(s), sym: s} }.sort_by{ |item| item[:index] }.map {|item| item[:sym] }.join(',')
52-
else
53-
sym_map = syms.join(",")
54-
end
50+
sym_map = syms.join(",")
5551
str.gsub!(/[\d\.]+/, 'Rational(\0,1)')
5652
replacements = { sin:"Math.sin", cos: "Math.cos", tan: "Math.tan",
5753
asin:"Math.asin", acos: "Math.acos", atan: "Math.atan",

lib/symengine/basic.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ def free_symbols
1111
end
1212

1313
def abs
14-
SymEngine::abs(self)
14+
SymEngine::abs(self)
1515
end
16-
def to_proc
17-
SymEngine::lambdify(self)
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}."
19+
end
20+
SymEngine::lambdify(self, self.free_symbols.map {|s| s.to_s})
1821
end
1922
end
2023
end

spec/lambdify_spec.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
let(:z) { SymEngine::Symbol.new('z') }
66

77
def l(code)
8-
SymEngine::Utils::lambdify_code(code)
8+
SymEngine::Utils::lambdify_code(code, [x])
99
end
1010

1111
it 'creates lambda codes' do
12-
expect(l( x + y + z )).to eq("lambda { | x,y,z | x + y + z }")
12+
expect(SymEngine::Utils::lambdify_code( x + y + z, [x, y, z])).to eq("lambda { | x,y,z | x + y + z }")
1313
expect(l( x + 5 )).to eq("lambda { | x | Rational(5,1) + x }")
1414
expect(l( SymEngine::sin(x) )).to eq("lambda { | x | Math.sin(x) }")
1515
expect(l( SymEngine::cos(x) )).to eq("lambda { | x | Math.cos(x) }")
@@ -41,7 +41,7 @@ def l(code)
4141

4242
describe 'lambda for Addition' do
4343
let(:func) { x + y + z }
44-
let(:lamb) { SymEngine::lambdify(func) }
44+
let(:lamb) { SymEngine::lambdify(func, [x, y, z]) }
4545
it 'performs addition with a lambda function' do
4646
expect(lamb.call(1, 1, 1)).to eq(3)
4747
expect(lamb.call(1, -1, 1)).to eq(1)
@@ -51,7 +51,7 @@ def l(code)
5151

5252
describe 'lambda for Addition with FixNums' do
5353
let(:func) { x + 5}
54-
let(:lamb) { SymEngine::lambdify(func) }
54+
let(:lamb) { SymEngine::lambdify(func, [x]) }
5555
it 'performs addition with a lambda function' do
5656
expect(lamb.call(1)).to eq(6)
5757
expect(lamb.call(0)).to eq(5)
@@ -62,12 +62,21 @@ def l(code)
6262

6363
describe 'lambda for sin' do
6464
let(:func) { SymEngine::sin(x) }
65-
let(:lamb) { SymEngine::lambdify(func) }
65+
let(:lamb) { SymEngine::lambdify(func, [x]) }
6666
it 'performs sin calculation with a lambda function' do
6767
expect(lamb.call(0)).to be_within(1e-15).of(0.0)
6868
expect(lamb.call(Math::PI)).to be_within(1e-15).of(0.0)
6969
expect(lamb.call(Math::PI/2)).to be_within(1e-15).of(1.0)
7070
end
7171
end
7272

73+
describe 'to_proc' do
74+
let(:func) { x * 10 }
75+
let(:func2) { x + y + 10}
76+
it 'creates procs' do
77+
expect([1, 2, 3, 4, 5].map(&func)).to eq([10, 20, 30, 40, 50])
78+
expect { [[1, 2], [3, 4]].map(&func2) }.to raise_error(ArgumentError)
79+
end
80+
end
81+
7382
end

0 commit comments

Comments
 (0)