Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pymbolic
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lawrence Mitchell
pymbolic
Commits
b370c2e4
Commit
b370c2e4
authored
13 years ago
by
Andreas Klöckner
Browse files
Options
Downloads
Patches
Plain Diff
Support tuples in expressions in a few places, to be able to compile tuple generation.
parent
1b5bc881
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pymbolic/compiler.py
+14
-7
14 additions, 7 deletions
pymbolic/compiler.py
pymbolic/mapper/__init__.py
+5
-0
5 additions, 0 deletions
pymbolic/mapper/__init__.py
with
19 additions
and
7 deletions
pymbolic/compiler.py
+
14
−
7
View file @
b370c2e4
...
@@ -24,7 +24,7 @@ def _constant_mapper(c):
...
@@ -24,7 +24,7 @@ def _constant_mapper(c):
class
CompileMapper
(
StringifyMapper
):
class
CompileMapper
(
StringifyMapper
):
def
__init__
(
self
):
def
__init__
(
self
):
StringifyMapper
.
__init__
(
self
,
StringifyMapper
.
__init__
(
self
,
constant_mapper
=
_constant_mapper
)
constant_mapper
=
_constant_mapper
)
def
map_polynomial
(
self
,
expr
,
enclosing_prec
):
def
map_polynomial
(
self
,
expr
,
enclosing_prec
):
...
@@ -46,7 +46,7 @@ class CompileMapper(StringifyMapper):
...
@@ -46,7 +46,7 @@ class CompileMapper(StringifyMapper):
next_exp
=
rev_data
[
i
+
1
][
0
]
next_exp
=
rev_data
[
i
+
1
][
0
]
else
:
else
:
next_exp
=
0
next_exp
=
0
result
=
"
(%s+%s)%s
"
%
(
result
,
self
(
coeff
,
PREC_SUM
),
result
=
"
(%s+%s)%s
"
%
(
result
,
self
(
coeff
,
PREC_SUM
),
stringify_exp
(
exp
-
next_exp
))
stringify_exp
(
exp
-
next_exp
))
#print "A", result
#print "A", result
#print "B", expr
#print "B", expr
...
@@ -68,16 +68,23 @@ class CompileMapper(StringifyMapper):
...
@@ -68,16 +68,23 @@ class CompileMapper(StringifyMapper):
return
"
numpy.array(%s)
"
%
stringify_leading_dimension
(
expr
)
return
"
numpy.array(%s)
"
%
stringify_leading_dimension
(
expr
)
def
map_foreign
(
self
,
expr
,
enclosing_prec
):
if
isinstance
(
expr
,
tuple
):
return
"
(%s)
"
%
(
"
,
"
.
join
(
self
.
rec
(
child
,
PREC_NONE
)
for
child
in
expr
))
else
:
return
StringifyMapper
.
map_foreign
(
self
,
expr
,
enclosing_prec
)
class
CompiledExpression
:
class
CompiledExpression
:
"""
This class encapsulates a compiled expression.
"""
This class encapsulates a compiled expression.
The main reason for its existence is the fact that a dynamically-constructed
The main reason for its existence is the fact that a dynamically-constructed
lambda function is not picklable.
lambda function is not picklable.
"""
"""
def
__init__
(
self
,
expression
,
variables
=
[]):
def
__init__
(
self
,
expression
,
variables
=
[]):
import
pymbolic.primitives
as
primi
import
pymbolic.primitives
as
primi
...
@@ -105,13 +112,13 @@ class CompiledExpression:
...
@@ -105,13 +112,13 @@ class CompiledExpression:
all_variables
=
self
.
_Variables
+
used_variables
all_variables
=
self
.
_Variables
+
used_variables
expr_s
=
CompileMapper
()(
self
.
_Expression
,
PREC_NONE
)
expr_s
=
CompileMapper
()(
self
.
_Expression
,
PREC_NONE
)
func_s
=
"
lambda %s:%s
"
%
(
"
,
"
.
join
(
str
(
v
)
for
v
in
all_variables
),
func_s
=
"
lambda %s:
%s
"
%
(
"
,
"
.
join
(
str
(
v
)
for
v
in
all_variables
),
expr_s
)
expr_s
)
self
.
__call__
=
eval
(
func_s
,
ctx
)
self
.
__call__
=
eval
(
func_s
,
ctx
)
def
__getinitargs__
(
self
):
def
__getinitargs__
(
self
):
return
self
.
_Expression
,
self
.
_Variables
return
self
.
_Expression
,
self
.
_Variables
def
__getstate__
(
self
):
def
__getstate__
(
self
):
return
None
return
None
...
...
This diff is collapsed.
Click to expand it.
pymbolic/mapper/__init__.py
+
5
−
0
View file @
b370c2e4
...
@@ -142,6 +142,11 @@ class CombineMapper(RecursiveMapper):
...
@@ -142,6 +142,11 @@ class CombineMapper(RecursiveMapper):
self
.
rec
(
expr
.
then
),
self
.
rec
(
expr
.
then
),
self
.
rec
(
expr
.
else_
)])
self
.
rec
(
expr
.
else_
)])
def
map_foreign
(
self
,
expr
,
*
args
):
if
isinstance
(
expr
,
tuple
):
return
self
.
combine
([
self
.
rec
(
child
)
for
child
in
expr
])
else
:
return
RecursiveMapper
.
map_foreign
(
self
,
expr
,
*
args
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment