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
Snippets
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
Alexandru Fikl
pymbolic
Commits
60aae072
Commit
60aae072
authored
17 years ago
by
Andreas Klöckner
Browse files
Options
Downloads
Patches
Plain Diff
Remove unnecessary property functions. Incremental improvements.
Remove is_constant from differentiator. Trivial fixes.
parent
b24dac1f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/mapper/dependency.py
+2
-2
2 additions, 2 deletions
src/mapper/dependency.py
src/mapper/differentiator.py
+21
-31
21 additions, 31 deletions
src/mapper/differentiator.py
src/mapper/evaluator.py
+6
-0
6 additions, 0 deletions
src/mapper/evaluator.py
src/primitives.py
+81
-96
81 additions, 96 deletions
src/primitives.py
with
110 additions
and
129 deletions
src/mapper/dependency.py
+
2
−
2
View file @
60aae072
...
...
@@ -24,13 +24,13 @@ class DependencyMapper(CombineMapper):
def
map_variable
(
self
,
expr
):
return
set
([
expr
])
def
map_call
(
self
,
expr
,
*
args
,
**
kwargs
):
def
map_call
(
self
,
expr
):
if
self
.
IncludeCalls
:
return
set
([
expr
])
else
:
return
CombineMapper
.
map_call
(
self
,
expr
)
def
map_lookup
(
self
,
expr
,
*
args
,
**
kwargs
):
def
map_lookup
(
self
,
expr
):
if
self
.
IncludeLookups
:
return
set
([
expr
])
else
:
...
...
This diff is collapsed.
Click to expand it.
src/mapper/differentiator.py
+
21
−
31
View file @
60aae072
...
...
@@ -34,25 +34,25 @@ def map_math_functions_by_name(i, func, pars):
class
DifferentiationMapper
(
pymbolic
.
mapper
.
RecursiveMapper
):
def
__init__
(
self
,
variable
,
func_map
):
self
.
V
ariable
=
variable
self
.
F
unction
M
ap
=
func_map
def
__init__
(
self
,
variable
,
func_map
=
map_math_functions_by_name
):
self
.
v
ariable
=
variable
self
.
f
unction
_m
ap
=
func_map
def
map_constant
(
self
,
expr
):
return
0
def
map_variable
(
self
,
expr
):
if
expr
==
self
.
V
ariable
:
if
expr
==
self
.
v
ariable
:
return
1
else
:
return
0
def
map_call
(
self
,
expr
):
return
pymbolic
.
sum
(
self
.
F
unction
M
ap
(
i
,
expr
.
function
,
expr
.
parameters
)
return
pymbolic
.
flattened_
sum
(
self
.
f
unction
_m
ap
(
i
,
expr
.
function
,
expr
.
parameters
)
*
self
.
rec
(
par
)
for
i
,
par
in
enumerate
(
expr
.
parameters
)
if
not
self
.
_isc
(
par
)
)
)
map_subscript
=
map_variable
...
...
@@ -60,29 +60,27 @@ class DifferentiationMapper(pymbolic.mapper.RecursiveMapper):
return
-
self
.
rec
(
expr
.
child
)
def
map_sum
(
self
,
expr
):
return
pymbolic
.
sum
(
self
.
rec
(
child
)
for
child
in
expr
.
children
if
not
self
.
_isc
(
child
))
return
pymbolic
.
flattened_sum
(
self
.
rec
(
child
)
for
child
in
expr
.
children
)
def
map_product
(
self
,
expr
):
return
pymbolic
.
sum
(
pymbolic
.
product
(
return
pymbolic
.
flattened_
sum
(
pymbolic
.
flattened_
product
(
expr
.
children
[
0
:
i
]
+
(
self
.
rec
(
child
),)
+
expr
.
children
[
i
+
1
:])
for
i
,
child
in
enumerate
(
expr
.
children
)
if
not
self
.
_isc
(
child
))
for
i
,
child
in
enumerate
(
expr
.
children
))
def
map_quotient
(
self
,
expr
):
f
=
expr
.
numerator
g
=
expr
.
denominator
f
_const
=
self
.
_is
c
(
f
)
g
_const
=
self
.
_is
c
(
g
)
d
f
=
self
.
re
c
(
f
)
d
g
=
self
.
re
c
(
g
)
if
f_const
and
g_const
:
if
(
not
df
)
and
(
not
dg
)
:
return
0
elif
f_const
:
elif
(
not
df
)
:
return
-
f
*
self
.
rec
(
g
)
/
g
**
2
elif
g_const
:
elif
(
not
dg
)
:
return
self
.
rec
(
f
)
/
g
else
:
return
(
self
.
rec
(
f
)
*
g
-
self
.
rec
(
g
)
*
f
)
/
g
**
2
...
...
@@ -90,16 +88,16 @@ class DifferentiationMapper(pymbolic.mapper.RecursiveMapper):
def
map_power
(
self
,
expr
):
f
=
expr
.
base
g
=
expr
.
exponent
f
_const
=
self
.
_is
c
(
f
)
g
_const
=
self
.
_is
c
(
g
)
d
f
=
self
.
re
c
(
f
)
d
g
=
self
.
re
c
(
g
)
log
=
pymbolic
.
var
(
"
log
"
)
if
f_const
and
g_const
:
if
(
not
df
)
and
(
not
dg
)
:
return
0
elif
f_const
:
elif
(
not
df
)
:
return
log
(
f
)
*
f
**
g
*
self
.
rec
(
g
)
elif
g_const
:
elif
(
not
dg
)
:
return
g
*
f
**
(
g
-
1
)
*
self
.
rec
(
f
)
else
:
return
log
(
f
)
*
f
**
g
*
self
.
rec
(
g
)
+
\
...
...
@@ -124,14 +122,6 @@ class DifferentiationMapper(pymbolic.mapper.RecursiveMapper):
return
\
Polynomial
(
expr
.
base
,
tuple
(
deriv_coeff
),
expr
.
unit
)
+
\
Polynomial
(
expr
.
base
,
tuple
(
deriv_base
),
expr
.
unit
)
def
_isc
(
self
,
subexp
):
return
pymbolic
.
is_constant
(
subexp
,
[
self
.
Variable
],
include_lookups
=
True
,
include_subscripts
=
True
)
...
...
This diff is collapsed.
Click to expand it.
src/mapper/evaluator.py
+
6
−
0
View file @
60aae072
...
...
@@ -74,6 +74,12 @@ class EvaluationMapper(RecursiveMapper):
class
FloatEvaluationMapper
(
EvaluationMapper
):
def
handle_unsupported_expression
(
self
,
expr
):
try
:
return
float
(
expr
)
except
:
raise
TypeError
,
"
cannot convert %s to float
"
%
type
(
expr
)
def
map_constant
(
self
,
expr
):
return
float
(
expr
)
...
...
This diff is collapsed.
Click to expand it.
src/primitives.py
+
81
−
96
View file @
60aae072
...
...
@@ -154,31 +154,38 @@ class Expression(object):
return
"
%s(%s)
"
%
(
self
.
__class__
.
__name__
,
initargs_str
)
class
AlgebraicLeaf
(
Expression
):
pass
class
Leaf
(
AlgebraicLeaf
):
pass
class
Variable
(
Leaf
):
def
__init__
(
self
,
name
):
self
.
_N
ame
=
name
self
.
n
ame
=
name
def
__getinitargs__
(
self
):
return
self
.
_Name
,
def
_name
(
self
):
return
self
.
_Name
name
=
property
(
_name
)
def
__lt__
(
self
,
other
):
if
isinstance
(
other
,
Variable
):
return
self
.
_N
ame
.
__lt__
(
other
.
_N
ame
)
return
self
.
n
ame
.
__lt__
(
other
.
n
ame
)
else
:
return
NotImplemented
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
Variable
)
and
self
.
_Name
==
other
.
_Name
return
(
isinstance
(
other
,
Variable
)
and
self
.
name
==
other
.
name
)
def
__hash__
(
self
):
return
0x111
^
hash
(
self
.
name
)
...
...
@@ -188,24 +195,16 @@ class Variable(Leaf):
class
Call
(
AlgebraicLeaf
):
def
__init__
(
self
,
function
,
parameters
):
self
.
_F
unction
=
function
self
.
_P
arameters
=
parameters
self
.
f
unction
=
function
self
.
p
arameters
=
parameters
def
__getinitargs__
(
self
):
return
self
.
_Function
,
self
.
_Parameters
def
_function
(
self
):
return
self
.
_Function
function
=
property
(
_function
)
def
_parameters
(
self
):
return
self
.
_Parameters
parameters
=
property
(
_parameters
)
return
self
.
function
,
self
.
parameters
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
Call
)
\
and
(
self
.
_F
unction
==
other
.
_F
unction
)
\
and
(
self
.
_P
arameters
==
other
.
_P
arameters
)
and
(
self
.
f
unction
==
other
.
f
unction
)
\
and
(
self
.
p
arameters
==
other
.
p
arameters
)
def
__hash__
(
self
):
return
hash
(
self
.
function
)
^
hash
(
self
.
parameters
)
...
...
@@ -213,26 +212,21 @@ class Call(AlgebraicLeaf):
def
get_mapper_method
(
self
,
mapper
):
return
mapper
.
map_call
class
Subscript
(
AlgebraicLeaf
):
def
__init__
(
self
,
aggregate
,
index
):
self
.
_A
ggregate
=
aggregate
self
.
_I
ndex
=
index
self
.
a
ggregate
=
aggregate
self
.
i
ndex
=
index
def
__getinitargs__
(
self
):
return
self
.
_Aggregate
,
self
.
_Index
def
_aggregate
(
self
):
return
self
.
_Aggregate
aggregate
=
property
(
_aggregate
)
def
_index
(
self
):
return
self
.
_Index
index
=
property
(
_index
)
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
Subscript
)
\
and
(
self
.
_A
ggregate
==
other
.
_A
ggregate
)
\
and
(
self
.
_I
ndex
==
other
.
_I
ndex
)
and
(
self
.
a
ggregate
==
other
.
a
ggregate
)
\
and
(
self
.
i
ndex
==
other
.
i
ndex
)
def
__hash__
(
self
):
return
0x123
^
hash
(
self
.
aggregate
)
^
hash
(
self
.
index
)
...
...
@@ -242,26 +236,19 @@ class Subscript(AlgebraicLeaf):
class
ElementLookup
(
AlgebraicLeaf
):
def
__init__
(
self
,
aggregate
,
name
):
self
.
_A
ggregate
=
aggregate
self
.
_N
ame
=
name
self
.
a
ggregate
=
aggregate
self
.
n
ame
=
name
def
__getinitargs__
(
self
):
return
self
.
_Aggregate
,
self
.
_Name
def
_aggregate
(
self
):
return
self
.
_Aggregate
aggregate
=
property
(
_aggregate
)
def
_name
(
self
):
return
self
.
_Name
name
=
property
(
_name
)
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
ElementLookup
)
\
and
(
self
.
_A
ggregate
==
other
.
_A
ggregate
)
\
and
(
self
.
_N
ame
==
other
.
_N
ame
)
and
(
self
.
a
ggregate
==
other
.
a
ggregate
)
\
and
(
self
.
n
ame
==
other
.
n
ame
)
def
__hash__
(
self
):
return
0x183
^
hash
(
self
.
aggregate
)
^
hash
(
self
.
name
)
...
...
@@ -269,41 +256,41 @@ class ElementLookup(AlgebraicLeaf):
def
get_mapper_method
(
self
,
mapper
):
return
mapper
.
map_lookup
class
Sum
(
Expression
):
def
__init__
(
self
,
children
):
assert
isinstance
(
children
,
tuple
)
self
.
_C
hildren
=
children
self
.
c
hildren
=
children
def
__getinitargs__
(
self
):
return
self
.
_Children
def
_children
(
self
):
return
self
.
_Children
children
=
property
(
_children
)
return
self
.
children
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
Sum
)
and
(
self
.
_Children
==
other
.
_Children
)
return
(
isinstance
(
other
,
Sum
)
and
(
set
(
self
.
children
)
==
set
(
other
.
children
)))
def
__add__
(
self
,
other
):
if
not
is_valid_operand
(
other
):
return
NotImplemented
if
isinstance
(
other
,
Sum
):
return
Sum
(
self
.
_C
hildren
+
other
.
_C
hildren
)
return
Sum
(
self
.
c
hildren
+
other
.
c
hildren
)
if
not
other
:
return
self
return
Sum
(
self
.
_C
hildren
+
(
other
,))
return
Sum
(
self
.
c
hildren
+
(
other
,))
def
__radd__
(
self
,
other
):
if
not
is_constant
(
other
):
return
NotImplemented
if
isinstance
(
other
,
Sum
):
return
Sum
(
other
.
_C
hildren
+
self
.
_C
hildren
)
return
Sum
(
other
.
c
hildren
+
self
.
c
hildren
)
if
not
other
:
return
self
return
Sum
((
other
,)
+
self
.
_C
hildren
)
return
Sum
((
other
,)
+
self
.
c
hildren
)
def
__sub__
(
self
,
other
):
if
not
is_valid_operand
(
other
):
...
...
@@ -311,13 +298,13 @@ class Sum(Expression):
if
not
other
:
return
self
return
Sum
(
self
.
_C
hildren
+
(
-
other
,))
return
Sum
(
self
.
c
hildren
+
(
-
other
,))
def
__nonzero__
(
self
):
if
len
(
self
.
_C
hildren
)
==
0
:
if
len
(
self
.
c
hildren
)
==
0
:
return
True
elif
len
(
self
.
_C
hildren
)
==
1
:
return
bool
(
self
.
_C
hildren
[
0
])
elif
len
(
self
.
c
hildren
)
==
1
:
return
bool
(
self
.
c
hildren
[
0
])
else
:
# FIXME: Right semantics?
return
True
...
...
@@ -328,45 +315,45 @@ class Sum(Expression):
def
get_mapper_method
(
self
,
mapper
):
return
mapper
.
map_sum
class
Product
(
Expression
):
def
__init__
(
self
,
children
):
assert
isinstance
(
children
,
tuple
)
self
.
_C
hildren
=
children
self
.
c
hildren
=
children
def
__getinitargs__
(
self
):
return
self
.
_Children
def
_children
(
self
):
return
self
.
_Children
children
=
property
(
_children
)
return
self
.
children
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
Product
)
and
(
self
.
_Children
==
other
.
_Children
)
return
(
isinstance
(
other
,
Product
)
and
(
set
(
self
.
children
)
==
set
(
other
.
children
)))
def
__mul__
(
self
,
other
):
if
not
is_valid_operand
(
other
):
return
NotImplemented
if
isinstance
(
other
,
Product
):
return
Product
(
self
.
_C
hildren
+
other
.
_C
hildren
)
return
Product
(
self
.
c
hildren
+
other
.
c
hildren
)
if
not
other
:
return
0
if
not
other
-
1
:
return
self
return
Product
(
self
.
_C
hildren
+
(
other
,))
return
Product
(
self
.
c
hildren
+
(
other
,))
def
__rmul__
(
self
,
other
):
if
not
is_constant
(
other
):
return
NotImplemented
if
isinstance
(
other
,
Product
):
return
Product
(
other
.
_C
hildren
+
self
.
_C
hildren
)
return
Product
(
other
.
c
hildren
+
self
.
c
hildren
)
if
not
other
:
return
0
if
not
other
-
1
:
return
self
return
Product
((
other
,)
+
self
.
_C
hildren
)
return
Product
((
other
,)
+
self
.
c
hildren
)
def
__nonzero__
(
self
):
for
i
in
self
.
_C
hildren
:
for
i
in
self
.
c
hildren
:
if
not
i
:
return
False
return
True
...
...
@@ -377,30 +364,33 @@ class Product(Expression):
def
get_mapper_method
(
self
,
mapper
):
return
mapper
.
map_product
class
Quotient
(
Expression
):
def
__init__
(
self
,
numerator
,
denominator
=
1
):
self
.
_N
umerator
=
numerator
self
.
_D
enominator
=
denominator
self
.
n
umerator
=
numerator
self
.
d
enominator
=
denominator
def
__getinitargs__
(
self
):
return
self
.
_N
umerator
,
self
.
_D
enominator
return
self
.
n
umerator
,
self
.
d
enominator
def
_num
(
self
):
return
self
.
_Numerator
numerator
=
property
(
_num
)
@property
def
num
(
self
):
return
self
.
numerator
def
_den
(
self
):
return
self
.
_Denominator
denominator
=
property
(
_den
)
@property
def
den
(
self
):
return
self
.
denominator
def
__eq__
(
self
,
other
):
from
pymbolic.rational
import
Rational
return
isinstance
(
other
,
(
Rational
,
Quotient
))
\
and
(
self
.
_N
umerator
==
other
.
numerator
)
\
and
(
self
.
_D
enominator
==
other
.
denominator
)
and
(
self
.
n
umerator
==
other
.
numerator
)
\
and
(
self
.
d
enominator
==
other
.
denominator
)
def
__nonzero__
(
self
):
return
bool
(
self
.
_N
umerator
)
return
bool
(
self
.
n
umerator
)
def
__hash__
(
self
):
return
0xabc
^
hash
(
self
.
numerator
)
^
hash
(
self
.
denominator
)
...
...
@@ -408,26 +398,21 @@ class Quotient(Expression):
def
get_mapper_method
(
self
,
mapper
):
return
mapper
.
map_quotient
class
Power
(
Expression
):
def
__init__
(
self
,
base
,
exponent
):
self
.
_B
ase
=
base
self
.
_E
xponent
=
exponent
self
.
b
ase
=
base
self
.
e
xponent
=
exponent
def
__getinitargs__
(
self
):
return
self
.
_Base
,
self
.
_Exponent
def
_base
(
self
):
return
self
.
_Base
base
=
property
(
_base
)
def
_exponent
(
self
):
return
self
.
_Exponent
exponent
=
property
(
_exponent
)
return
self
.
base
,
self
.
exponent
def
__eq__
(
self
,
other
):
return
isinstance
(
other
,
Power
)
\
and
(
self
.
_B
ase
==
other
.
_B
ase
)
\
and
(
self
.
_E
xponent
==
other
.
_E
xponent
)
and
(
self
.
b
ase
==
other
.
b
ase
)
\
and
(
self
.
e
xponent
==
other
.
e
xponent
)
def
__hash__
(
self
):
return
0xdef
^
hash
(
self
.
base
)
^
hash
(
self
.
exponent
)
...
...
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