@@ -13,10 +13,10 @@ class << self
13
13
# x, y = SymEngine.symbols(['x', 'y'])
14
14
# x, y = SymEngine.symbols('x', 'y')
15
15
# x, y = SymEngine.symbols('x y')
16
- #
17
- def symbols ary_or_string , *params
16
+ #
17
+ def symbols ( ary_or_string , *params )
18
18
# Want to make sure we can accept an array or a bunch of splatted arguments
19
- if params . size > 0
19
+ if ! params . empty?
20
20
ary_or_string = ( ary_or_string . is_a? ( String ) ? [ ary_or_string ] : ary_or_string ) . concat ( params )
21
21
elsif ary_or_string . is_a? ( String )
22
22
# or accept a string
@@ -25,45 +25,56 @@ def symbols ary_or_string, *params
25
25
26
26
# Make an Array of SymEngine::Symbols from the parameters we received,
27
27
# now that they're normalized.
28
- ary_or_string . map do |symbol_or_string |
29
- SymEngine ::Symbol . new ( symbol_or_string )
30
- end
31
- end
32
- def Function ( n )
28
+ ary_or_string . map ( &SymEngine ::Symbol . method ( :new ) )
29
+ end
30
+
31
+ def Function ( n ) # rubocop:disable Style/MethodName
33
32
SymEngine ::UndefFunction . new ( n )
34
33
end
35
- def evalf ( operand , prec = 53 , real = false )
34
+
35
+ def evalf ( operand , prec = 53 , real = false )
36
36
_evalf ( operand , prec , real )
37
37
end
38
+
38
39
def lambdify ( exp , *syms )
39
40
syms . flatten!
40
41
if exp . free_symbols . count > syms . length
41
42
raise ArgumentError , "Formula contains #{ exp . free_symbols . count } free " \
42
- " symbols. You should provide at least this number " \
43
+ ' symbols. You should provide at least this number ' \
43
44
"of arguments (only #{ syms . length } given)."
44
45
end
45
- eval ( SymEngine ::Utils ::lambdify_code ( exp , syms ) )
46
+ # TODO: Implement one of the two options below.
47
+ # 1. check that all the symbol names and function names are valid.
48
+ # 2. wrap symengine's LambdaRealDouble into C and then to ruby
49
+ eval ( SymEngine ::Utils . lambdify_code ( exp , syms ) )
46
50
end
47
51
end
48
52
module Utils
49
53
class << self
50
- REPLACEMENTS = { sin : 'Math.sin' , cos : 'Math.cos' , tan : 'Math.tan' ,
51
- asin : 'Math.asin' , acos : 'Math.acos' , atan : 'Math.atan' ,
52
- sinh : 'Math.sinh' , cosh : 'Math.cosh' , tanh : 'Math.tanh' ,
53
- asinh : 'Math.asinh' , acosh : 'Math.acosh' , atanh : 'Math.atanh' ,
54
- pi : 'Math::PI' , E : 'Math::E' , I : '::Complex::I' ,
55
- dirichlet_eta : 'SymEngine::Utils::evalf_dirichlet_eta' ,
56
- zeta : 'SymEngine::Utils::evalf_zeta' , gamma : 'Math.gamma' } . map { |from , to | [ /(\b #{ from } \b )/ , to ] } . to_h . freeze
54
+ REPLACEMENTS = {
55
+ sin : 'Math.sin' , cos : 'Math.cos' , tan : 'Math.tan' ,
56
+ asin : 'Math.asin' , acos : 'Math.acos' , atan : 'Math.atan' ,
57
+ sinh : 'Math.sinh' , cosh : 'Math.cosh' , tanh : 'Math.tanh' ,
58
+ asinh : 'Math.asinh' , acosh : 'Math.acosh' , atanh : 'Math.atanh' ,
59
+ pi : 'Math::PI' , E : 'Math::E' , I : '::Complex::I' ,
60
+ dirichlet_eta : 'SymEngine::Utils::evalf_dirichlet_eta' ,
61
+ zeta : 'SymEngine::Utils::evalf_zeta' , gamma : 'Math.gamma'
62
+ } . map { |from , to |
63
+ [ /(\b #{ from } \b )/ , to ]
64
+ } . to_h . freeze
65
+
57
66
def evalf_dirichlet_eta ( exp )
58
- SymEngine :: evalf ( SymEngine :: dirichlet_eta ( exp ) )
67
+ SymEngine . evalf ( SymEngine . dirichlet_eta ( exp ) )
59
68
end
69
+
60
70
def evalf_zeta ( exp )
61
- SymEngine :: evalf ( SymEngine :: zeta ( exp ) )
71
+ SymEngine . evalf ( SymEngine . zeta ( exp ) )
62
72
end
73
+
63
74
def lambdify_code ( exp , syms )
64
75
body = exp . to_s . gsub ( /[\d \. ]+/ , 'Rational(\0,1)' )
65
- sym_map = syms . join ( "," )
66
- rubified_body = REPLACEMENTS . inject ( body ) { |res , ( from , to ) | res . gsub ( from , to ) }
76
+ sym_map = syms . join ( ',' )
77
+ rubified_body = REPLACEMENTS . inject ( body ) { |res , ( from , to ) | res . gsub ( from , to ) }
67
78
"proc { | #{ sym_map } | #{ rubified_body } }"
68
79
end
69
80
end
0 commit comments