Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
trs
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
trs
Commits
6c6d35a6
Commit
6c6d35a6
authored
Dec 12, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Restructured rules and added match_expand.
parent
49c547c8
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
85 additions
and
51 deletions
+85
-51
src/rules/__init__.py
src/rules/__init__.py
+3
-3
src/rules/poly.py
src/rules/poly.py
+77
-43
tests/test_b1_ch08.py
tests/test_b1_ch08.py
+1
-1
tests/test_rules.py
tests/test_rules.py
+4
-4
No files found.
src/rules/__init__.py
View file @
6c6d35a6
from
..node
import
ExpressionNode
as
Node
,
OP_ADD
from
.poly
import
match_combine_factors
#, match_combine_parentheses
from
..node
import
ExpressionNode
as
Node
,
OP_ADD
,
OP_MUL
from
.poly
import
match_combine_factors
,
match_expand
RULES
=
{
OP_ADD
:
[
match_combine_factors
],
#OP_MUL: [match_combine_parentheses
],
OP_MUL
:
[
match_expand
],
}
src/rules/poly.py
View file @
6c6d35a6
from
itertools
import
combinations
from
..node
import
ExpressionLeaf
as
Leaf
,
TYPE_OPERATOR
from
..node
import
ExpressionLeaf
as
Leaf
,
TYPE_OPERATOR
,
OP_ADD
from
..possibilities
import
Possibility
as
P
def
match_expand
(
node
):
"""
a * (b + c) -> ab + ac
"""
if
node
.
type
!=
TYPE_OPERATOR
or
not
node
.
op
&
OP_MUL
:
return
[]
p
=
[]
# 'a' parts
left
=
[]
# '(b + c)' parts
right
=
[]
for
n
in
node
.
get_scope
():
if
node
.
type
==
TYPE_OPERATOR
:
if
n
.
op
&
OP_ADD
:
right
.
append
(
n
)
else
:
left
.
append
(
n
)
if
len
(
left
)
and
len
(
right
):
for
l
in
left
:
for
r
in
right
:
p
.
append
(
P
(
node
,
expand_single
,
l
,
r
))
return
p
def
expand_single
(
root
,
args
):
"""
Combine a leaf (left) multiplied with an addition of two expressions
(right) to an addition of two multiplications.
>>> a * (b + c) -> a * b + a * c
"""
left
,
right
=
args
others
=
list
(
set
(
root
.
get_scope
())
-
{
left
,
right
})
def
match_combine_factors
(
node
):
"""
n + exp + m -> exp + (n + m)
k0 * v ^ n + exp + k1 * v ^ n -> exp + (k0 + k1) * v ^ n
"""
if
node
.
type
!=
TYPE_OPERATOR
:
if
node
.
type
!=
TYPE_OPERATOR
or
not
node
.
op
&
OP_ADD
:
return
[]
p
=
[]
if
node
.
is_nary
():
# Collect all nodes that can be combined
# Numeric leaves
numerics
=
[]
...
...
@@ -22,26 +61,21 @@ def match_combine_factors(node):
# (identifier, exponent, coefficient)
orders
=
[]
# Nodes that cannot be combined
others
=
[]
for
n
in
node
.
get_scope
():
if
isinstance
(
n
,
Leaf
):
if
n
.
is_numeric
():
numerics
.
append
(
n
)
elif
n
.
is_identifier
():
orders
.
append
((
n
.
value
,
1
,
1
))
else
:
if
node
.
type
==
TYPE_OPERATOR
:
order
=
n
.
get_order
()
if
order
:
orders
+=
order
else
:
others
.
append
(
n
)
if
n
.
is_numeric
():
numerics
.
append
(
n
)
elif
n
.
is_identifier
():
orders
.
append
((
n
.
value
,
1
,
1
))
if
len
(
numerics
)
>
1
:
for
num0
,
num1
in
combinations
(
numerics
,
2
):
p
.
append
(
P
(
node
,
combine_numerics
,
(
num0
,
num1
,
others
)))
p
.
append
(
P
(
node
,
combine_numerics
,
(
num0
,
num1
)))
if
len
(
orders
)
>
1
:
for
order0
,
order1
in
combinations
(
orders
,
2
):
...
...
@@ -50,7 +84,7 @@ def match_combine_factors(node):
if
id0
==
id1
and
exponent0
==
exponent1
:
# Same identifier and exponent -> combine coefficients
args
=
order0
+
(
coeff1
,)
+
(
others
,)
args
=
order0
+
(
coeff1
,)
p
.
append
(
P
(
node
,
combine_orders
,
args
))
return
p
...
...
@@ -63,8 +97,8 @@ def combine_numerics(root, args):
Example:
>>> 3 + 4 -> 7
"""
numerics
,
others
=
args
value
=
sum
([
n
.
value
for
n
in
numeric
s
])
others
=
list
(
set
(
root
.
get_scope
())
-
set
(
args
))
value
=
sum
([
n
.
value
for
n
in
arg
s
])
return
nary_node
(
'+'
,
others
+
[
Leaf
(
value
)])
...
...
tests/test_b1_ch8.py
→
tests/test_b1_ch
0
8.py
View file @
6c6d35a6
...
...
@@ -5,7 +5,7 @@ from src.node import ExpressionNode as N, ExpressionLeaf as L
from
tests.parser
import
run_expressions
class
TestB1Ch8
(
unittest
.
TestCase
):
class
TestB1Ch
0
8
(
unittest
.
TestCase
):
def
test_diagnostic_test
(
self
):
run_expressions
(
Parser
,
[
...
...
tests/test_rules.py
View file @
6c6d35a6
...
...
@@ -23,15 +23,15 @@ class TestRules(unittest.TestCase):
l0
,
l1
=
L
(
1
),
L
(
2
)
plus
=
N
(
'+'
,
l0
,
l1
)
p
=
match_combine_factors
(
plus
)
self
.
assertEqualPos
(
p
,
[
P
(
plus
,
combine_numerics
,
(
l0
,
l1
,
[]
))])
self
.
assertEqualPos
(
p
,
[
P
(
plus
,
combine_numerics
,
(
l0
,
l1
))])
def
test_match_combine_factors_numeric_combinations
(
self
):
l0
,
l1
,
l2
=
L
(
1
),
L
(
2
),
L
(
2
)
plus
=
N
(
'+'
,
N
(
'+'
,
l0
,
l1
),
l2
)
p
=
match_combine_factors
(
plus
)
self
.
assertEqualPos
(
p
,
[
P
(
plus
,
combine_numerics
,
(
l0
,
l1
,
[]
)),
P
(
plus
,
combine_numerics
,
(
l0
,
l2
,
[]
)),
P
(
plus
,
combine_numerics
,
(
l1
,
l2
,
[]
))])
self
.
assertEqualPos
(
p
,
[
P
(
plus
,
combine_numerics
,
(
l0
,
l1
)),
P
(
plus
,
combine_numerics
,
(
l0
,
l2
)),
P
(
plus
,
combine_numerics
,
(
l1
,
l2
))])
def
assertEqualPos
(
self
,
possibilities
,
expected
):
for
p
,
e
in
zip
(
possibilities
,
expected
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment