11# -*- coding: utf-8 -*-
2- import numpy
32from typing import Callable , List , Optional , Tuple
43
4+ import numpy
55
6+ from mathics .core .convert .lambdify import (
7+ CompileError as LambdifyCompileError ,
8+ lambdify_compile ,
9+ )
610from mathics .core .evaluation import Evaluation
711from mathics .core .expression import Expression , from_python
812from mathics .core .symbols import Symbol , SymbolFalse , SymbolTrue
9- from mathics .core .systemsymbols import SymbolBlank , SymbolInteger , SymbolReal , SymbolComplex , SymbolOr
13+ from mathics .core .systemsymbols import (
14+ SymbolBlank ,
15+ SymbolComplex ,
16+ SymbolInteger ,
17+ SymbolOr ,
18+ SymbolReal ,
19+ )
1020from mathics .eval .nevaluator import eval_N
11- from mathics .core .convert .lambdify import lambdify_compile , CompileError as LambdifyCompileError
12-
1321
1422PERMITTED_TYPES = {
15- Expression (SymbolBlank , SymbolInteger ): int ,
16- Expression (SymbolBlank , SymbolReal ): float ,
17- Expression (SymbolBlank , SymbolComplex ): complex ,
18- Expression (SymbolOr , SymbolTrue , SymbolFalse ): bool ,
19- Expression (SymbolOr , SymbolFalse , SymbolTrue ): bool ,
23+ Expression (SymbolBlank , SymbolInteger ): int ,
24+ Expression (SymbolBlank , SymbolReal ): float ,
25+ Expression (SymbolBlank , SymbolComplex ): complex ,
26+ Expression (SymbolOr , SymbolTrue , SymbolFalse ): bool ,
27+ Expression (SymbolOr , SymbolFalse , SymbolTrue ): bool ,
2028}
2129
2230
3543 USE_LLVM = False
3644
3745
38-
3946class CompileDuplicateArgName (Exception ):
4047 def __init__ (self , symb ):
4148 self .symb = symb
@@ -46,8 +53,11 @@ def __init__(self, var):
4653 self .var = var
4754
4855
49-
50- def expression_to_llvm (expr : Expression , args :Optional [list ]= None , evaluation : Optional [Evaluation ]= None ):
56+ def expression_to_llvm (
57+ expr : Expression ,
58+ args : Optional [list ] = None ,
59+ evaluation : Optional [Evaluation ] = None ,
60+ ):
5161 """
5262 Convert an expression to LLVM code. None if it fails.
5363 expr: Expression
@@ -61,17 +71,18 @@ def expression_to_llvm(expr: Expression, args:Optional[list]=None, evaluation: O
6171
6272
6373def expression_to_python_function (
64- expr : Expression ,
65- args : Optional [list ] = None ,
66- evaluation : Optional [Evaluation ] = None ,
74+ expr : Expression ,
75+ args : Optional [list ] = None ,
76+ evaluation : Optional [Evaluation ] = None ,
6777) -> Optional [Callable ]:
6878 """
69- Return a Python function from an expression.
79+ Return a Python function from an expression.
7080 expr: Expression
7181 args: a list of CompileArg elements
7282 evaluation: an Evaluation object used if the llvm compilation fails
7383 """
7484 try :
85+
7586 def _pythonized_mathics_expr (* x ):
7687 inner_evaluation = Evaluation (definitions = evaluation .definitions )
7788 x_mathics = (from_python (u ) for u in x [: len (args )])
@@ -87,7 +98,7 @@ def _pythonized_mathics_expr(*x):
8798 return None
8899
89100
90- def collect_args (vars )-> Optional [List [CompileArg ]]:
101+ def collect_args (vars ) -> Optional [List [CompileArg ]]:
91102 """
92103 Convert a List expression into a list of CompileArg objects.
93104 """
@@ -116,15 +127,14 @@ def collect_args(vars)->Optional[List[CompileArg]]:
116127 names .append (name )
117128 args .append (CompileArg (name , typ ))
118129 return args
119-
120130
121131
122132def expression_to_callable_and_args (
123133 expr : Expression ,
124134 vars : Optional [list ] = None ,
125135 evaluation : Optional [Evaluation ] = None ,
126- debug :int = 0 ,
127- vectorize = False
136+ debug : int = 0 ,
137+ vectorize = False ,
128138) -> Tuple [Callable , Optional [list ]]:
129139 """
130140 Return a tuple of Python callable and a list of CompileArgs.
@@ -143,18 +153,24 @@ def expression_to_callable_and_args(
143153
144154 # Then, try with llvm if available
145155 if USE_LLVM :
146- try :
147- llvm_args = None if args is None else [CompileArg (compile_arg .name , LLVM_TYPE_TRANSLATION [compile_arg .typ ]) for compile_arg in args ]
156+ try :
157+ llvm_args = (
158+ None
159+ if args is None
160+ else [
161+ CompileArg (compile_arg .name , LLVM_TYPE_TRANSLATION [compile_arg .typ ])
162+ for compile_arg in args
163+ ]
164+ )
148165 cfunc = expression_to_llvm (expr , llvm_args , evaluation )
149166 if vectorize :
150167 cfunc = numpy .vectorize (cfunc )
151168 return cfunc , llvm_args
152169 except KeyError :
153170 pass
154-
171+
155172 # Last resource
156173 cfunc = expression_to_python_function (expr , args , evaluation )
157174 if vectorize :
158175 cfunc = numpy .vectorize (cfunc )
159- return cfunc , args
160-
176+ return cfunc , args
0 commit comments