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
bd350265
Commit
bd350265
authored
13 years ago
by
Andreas Klöckner
Browse files
Options
Downloads
Patches
Plain Diff
Fix sum/product unification.
parent
8f0ae973
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pymbolic/mapper/unifier.py
+92
-37
92 additions, 37 deletions
pymbolic/mapper/unifier.py
with
92 additions
and
37 deletions
pymbolic/mapper/unifier.py
+
92
−
37
View file @
bd350265
...
...
@@ -101,41 +101,41 @@ class UnifierBase(RecursiveMapper):
return
UnificationRecord
([(
lhs
,
rhs
)])
def
map_constant
(
self
,
expr
,
other
,
u
ni
s
):
def
map_constant
(
self
,
expr
,
other
,
u
rec
s
):
if
expr
==
other
:
return
u
ni
s
return
u
rec
s
else
:
return
[]
def
map_variable
(
self
,
expr
,
other
,
u
ni
s
):
def
map_variable
(
self
,
expr
,
other
,
u
rec
s
):
new_uni_record
=
self
.
unification_record_from_equation
(
expr
,
other
)
if
new_uni_record
is
None
:
if
(
isinstance
(
other
,
Variable
)
and
other
.
name
==
expr
.
name
and
expr
.
name
not
in
self
.
lhs_mapping_candidates
):
return
u
ni
s
return
u
rec
s
else
:
return
[]
else
:
return
unify_many
(
u
ni
s
,
new_uni_record
)
return
unify_many
(
u
rec
s
,
new_uni_record
)
def
map_subscript
(
self
,
expr
,
other
,
u
ni
s
):
def
map_subscript
(
self
,
expr
,
other
,
u
rec
s
):
if
not
isinstance
(
other
,
type
(
expr
)):
return
self
.
treat_mismatch
(
expr
,
other
,
u
ni
s
)
return
self
.
treat_mismatch
(
expr
,
other
,
u
rec
s
)
return
self
.
rec
(
expr
.
aggregate
,
other
.
aggregate
,
self
.
rec
(
expr
.
index
,
other
.
index
,
u
ni
s
))
self
.
rec
(
expr
.
index
,
other
.
index
,
u
rec
s
))
def
map_lookup
(
self
,
expr
,
other
,
u
ni
s
):
def
map_lookup
(
self
,
expr
,
other
,
u
rec
s
):
if
not
isinstance
(
other
,
type
(
expr
)):
return
self
.
treat_mismatch
(
expr
,
other
,
u
ni
s
)
return
self
.
treat_mismatch
(
expr
,
other
,
u
rec
s
)
if
expr
.
name
!=
other
.
name
:
return
[]
return
self
.
rec
(
expr
.
aggregate
,
other
.
aggregate
,
u
ni
s
)
return
self
.
rec
(
expr
.
aggregate
,
other
.
aggregate
,
u
rec
s
)
def
map_sum
(
self
,
expr
,
other
,
u
ni
s
):
def
map_sum
(
self
,
expr
,
other
,
u
rec
s
):
if
(
not
isinstance
(
other
,
type
(
expr
))
or
len
(
expr
.
children
)
!=
len
(
other
.
children
)):
return
[]
...
...
@@ -145,7 +145,7 @@ class UnifierBase(RecursiveMapper):
from
pytools
import
generate_permutations
had_structural_match
=
False
for
perm
in
generate_permutations
(
range
(
len
(
expr
.
children
))):
it_assignments
=
u
ni
s
it_assignments
=
u
rec
s
for
my_child
,
other_child
in
zip
(
expr
.
children
,
...
...
@@ -159,52 +159,52 @@ class UnifierBase(RecursiveMapper):
result
.
extend
(
it_assignments
)
if
not
had_structural_match
:
return
self
.
treat_mismatch
(
expr
,
other
,
u
ni
s
)
return
self
.
treat_mismatch
(
expr
,
other
,
u
rec
s
)
return
result
map_product
=
map_sum
def
map_negation
(
self
,
expr
,
other
,
u
ni
s
):
def
map_negation
(
self
,
expr
,
other
,
u
rec
s
):
if
not
isinstance
(
other
,
type
(
expr
)):
return
self
.
treat_mismatch
(
expr
,
other
,
u
ni
s
)
return
self
.
rec
(
expr
.
child
,
other
.
child
,
u
ni
s
)
return
self
.
treat_mismatch
(
expr
,
other
,
u
rec
s
)
return
self
.
rec
(
expr
.
child
,
other
.
child
,
u
rec
s
)
def
map_quotient
(
self
,
expr
,
other
,
u
ni
s
):
def
map_quotient
(
self
,
expr
,
other
,
u
rec
s
):
if
not
isinstance
(
other
,
type
(
expr
)):
return
self
.
treat_mismatch
(
expr
,
other
,
u
ni
s
)
return
self
.
treat_mismatch
(
expr
,
other
,
u
rec
s
)
return
self
.
rec
(
expr
.
numerator
,
other
.
numerator
,
self
.
rec
(
expr
.
denominator
,
other
.
denominator
,
u
ni
s
))
self
.
rec
(
expr
.
denominator
,
other
.
denominator
,
u
rec
s
))
map_floor_div
=
map_quotient
map_remainder
=
map_quotient
def
map_power
(
self
,
expr
,
other
,
u
ni
s
):
def
map_power
(
self
,
expr
,
other
,
u
rec
s
):
if
not
isinstance
(
other
,
type
(
expr
)):
return
self
.
treat_mismatch
(
expr
,
other
,
u
ni
s
)
return
self
.
treat_mismatch
(
expr
,
other
,
u
rec
s
)
return
self
.
rec
(
expr
.
base
,
other
.
base
,
self
.
rec
(
expr
.
exponent
,
other
.
exponent
,
u
ni
s
))
self
.
rec
(
expr
.
exponent
,
other
.
exponent
,
u
rec
s
))
def
map_list
(
self
,
expr
,
other
,
u
ni
s
):
def
map_list
(
self
,
expr
,
other
,
u
rec
s
):
if
(
not
isinstance
(
other
,
type
(
expr
))
or
len
(
expr
)
!=
len
(
other
)):
return
[]
for
my_child
,
other_child
in
zip
(
expr
,
other
):
u
ni
s
=
self
.
rec
(
my_child
,
other_child
,
u
ni
s
)
if
not
u
ni
s
:
u
rec
s
=
self
.
rec
(
my_child
,
other_child
,
u
rec
s
)
if
not
u
rec
s
:
break
return
u
ni
s
return
u
rec
s
map_tuple
=
map_list
def
__call__
(
self
,
expr
,
other
,
u
ni
s
=
None
):
if
u
ni
s
is
None
:
u
ni
s
=
[
UnificationRecord
([])]
return
self
.
rec
(
expr
,
other
,
u
ni
s
)
def
__call__
(
self
,
expr
,
other
,
u
rec
s
=
None
):
if
u
rec
s
is
None
:
u
rec
s
=
[
UnificationRecord
([])]
return
self
.
rec
(
expr
,
other
,
u
rec
s
)
...
...
@@ -214,14 +214,69 @@ class UnidirectionalUnifier(UnifierBase):
subexpression of the second.
"""
def
treat_mismatch
(
self
,
expr
,
other
,
u
ni
s
):
def
treat_mismatch
(
self
,
expr
,
other
,
u
rec
s
):
return
[]
def
map_commut_assoc
(
self
,
expr
,
other
,
urecs
,
factory
):
if
not
isinstance
(
other
,
type
(
expr
)):
return
plain_cand_variables
=
[]
non_var_children
=
[]
for
child
in
expr
.
children
:
if
(
isinstance
(
child
,
Variable
)
and
child
.
name
in
self
.
lhs_mapping_candidates
):
plain_cand_variables
.
append
(
child
)
else
:
non_var_children
.
append
(
child
)
# list (with indices matching non_var_children) of
# list of tuples (other_index, unifiers)
unification_candidates
=
[]
class
BidirectionalUnifier
(
UnifierBase
):
"""
Only assigns variables encountered in the first expression to
subexpression of the second.
"""
for
i
,
my_child
in
enumerate
(
non_var_children
):
i_matches
=
[]
for
j
,
other_child
in
enumerate
(
other
.
children
):
result
=
self
.
rec
(
my_child
,
other_child
,
urecs
)
if
result
:
i_matches
.
append
((
j
,
result
))
unification_candidates
.
append
(
i_matches
)
def
match_children
(
urec
,
next_my_idx
,
other_leftovers
):
if
next_my_idx
>=
len
(
non_var_children
):
if
not
plain_cand_variables
and
other_leftovers
:
return
eqns
=
[]
for
pv
in
plain_cand_variables
:
eqns
.
append
((
pv
,
factory
(
other
.
children
[
i
]
for
i
in
other_leftovers
)))
other_leftovers
=
[]
yield
urec
.
unify
(
UnificationRecord
(
eqns
))
return
for
other_idx
,
pair_urecs
in
unification_candidates
[
next_my_idx
]:
if
other_idx
not
in
other_leftovers
:
continue
new_urecs
=
unify_many
(
pair_urecs
,
urec
)
new_rhs_leftovers
=
other_leftovers
-
set
([
other_idx
])
for
cand_urec
in
new_urecs
:
for
result_urec
in
match_children
(
cand_urec
,
next_my_idx
+
1
,
new_rhs_leftovers
):
yield
result_urec
for
urec
in
match_children
(
UnificationRecord
([]),
0
,
set
(
range
(
len
(
other
.
children
)))):
yield
urec
def
map_sum
(
self
,
expr
,
other
,
unis
):
from
pymbolic.primitives
import
flattened_sum
return
list
(
self
.
map_commut_assoc
(
expr
,
other
,
unis
,
flattened_sum
))
treat_mismatch
=
UnifierBase
.
map_variable
def
map_product
(
self
,
expr
,
other
,
unis
):
from
pymbolic.primitives
import
flattened_product
return
list
(
self
.
map_commut_assoc
(
expr
,
other
,
unis
,
flattened_product
))
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