Skip to content

Commit a462ad7

Browse files
author
mmatera
committed
black
1 parent 26bcaf1 commit a462ad7

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

mathics/core/convert/function.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
# -*- coding: utf-8 -*-
2-
import numpy
32
from 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+
)
610
from mathics.core.evaluation import Evaluation
711
from mathics.core.expression import Expression, from_python
812
from 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+
)
1020
from mathics.eval.nevaluator import eval_N
11-
from mathics.core.convert.lambdify import lambdify_compile, CompileError as LambdifyCompileError
12-
1321

1422
PERMITTED_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

@@ -35,7 +43,6 @@
3543
USE_LLVM = False
3644

3745

38-
3946
class 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

6373
def 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

122132
def 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

mathics/eval/drawing/plot3d_vectorized.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import numpy as np
99

1010
from mathics.builtin.colors.color_internals import convert_color
11+
from mathics.core.convert.lambdify import lambdify_compile as plot_compile
1112
from mathics.core.evaluation import Evaluation
1213
from mathics.core.symbols import strip_context
1314
from mathics.core.systemsymbols import SymbolNone, SymbolRGBColor
1415
from mathics.timing import Timer
1516

16-
from mathics.core.convert.lambdify import lambdify_compile as plot_compile
1717
from .util import GraphicsGenerator
1818

1919

test/core/convert/test_lambdify_compile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
import numpy as np
1919

20+
from mathics.core.convert.lambdify import lambdify_compile
2021
from mathics.core.convert.python import from_python
2122
from mathics.core.expression import Expression
22-
from mathics.core.convert.lambdify import lambdify_compile
2323

2424
#
2525
# Each test specifies:

0 commit comments

Comments
 (0)