@@ -13,10 +13,10 @@ class << self
1313 # x, y = SymEngine.symbols(['x', 'y'])
1414 # x, y = SymEngine.symbols('x', 'y')
1515 # x, y = SymEngine.symbols('x y')
16- #
17- def symbols ary_or_string , *params
16+ #
17+ def symbols ( ary_or_string , *params )
1818 # Want to make sure we can accept an array or a bunch of splatted arguments
19- if params . size > 0
19+ if ! params . empty?
2020 ary_or_string = ( ary_or_string . is_a? ( String ) ? [ ary_or_string ] : ary_or_string ) . concat ( params )
2121 elsif ary_or_string . is_a? ( String )
2222 # or accept a string
@@ -25,45 +25,56 @@ def symbols ary_or_string, *params
2525
2626 # Make an Array of SymEngine::Symbols from the parameters we received,
2727 # 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
3332 SymEngine ::UndefFunction . new ( n )
3433 end
35- def evalf ( operand , prec = 53 , real = false )
34+
35+ def evalf ( operand , prec = 53 , real = false )
3636 _evalf ( operand , prec , real )
3737 end
38+
3839 def lambdify ( exp , *syms )
3940 syms . flatten!
4041 if exp . free_symbols . count > syms . length
4142 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 ' \
4344 "of arguments (only #{ syms . length } given)."
4445 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 ) )
4650 end
4751 end
4852 module Utils
4953 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+
5766 def evalf_dirichlet_eta ( exp )
58- SymEngine :: evalf ( SymEngine :: dirichlet_eta ( exp ) )
67+ SymEngine . evalf ( SymEngine . dirichlet_eta ( exp ) )
5968 end
69+
6070 def evalf_zeta ( exp )
61- SymEngine :: evalf ( SymEngine :: zeta ( exp ) )
71+ SymEngine . evalf ( SymEngine . zeta ( exp ) )
6272 end
73+
6374 def lambdify_code ( exp , syms )
6475 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 ) }
6778 "proc { | #{ sym_map } | #{ rubified_body } }"
6879 end
6980 end
0 commit comments