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
76158313
Commit
76158313
authored
Jan 05, 2012
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added tests for polynome combinations.
parent
0065eb9e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
28 deletions
+80
-28
src/rules/poly.py
src/rules/poly.py
+17
-8
tests/test_rules_poly.py
tests/test_rules_poly.py
+63
-20
No files found.
src/rules/poly.py
View file @
76158313
...
...
@@ -90,23 +90,24 @@ def match_combine_polynomes(node, verbose=False):
# Each combination of powers of the same value and polynome can be added
if
len
(
polys
)
>=
2
:
for
left
,
right
in
combinations
(
polys
,
2
):
c0
,
r0
,
e0
=
left
[
1
]
c1
,
r1
,
e1
=
right
[
1
]
n0
,
p0
=
left
n1
,
p1
=
right
c0
,
r0
,
e0
=
p0
c1
,
r1
,
e1
=
p1
# Both numeric root and same exponent -> combine coefficients and
# roots, or: same root and exponent -> combine coefficients.
# TODO: Addition with zero, e.g. a + 0 -> a
if
c0
==
1
and
c1
==
1
and
e0
==
1
and
e1
==
1
\
and
r0
.
is_numeric
()
and
r1
.
is_numeric
():
# 2 + 3 -> 5
p
.
append
(
P
(
node
,
combine_numerics
,
\
(
left
[
0
],
right
[
0
],
r0
,
r1
)))
p
.
append
(
P
(
node
,
combine_numerics
,
(
n0
,
n1
,
r0
.
value
,
r1
.
value
)))
elif
c0
.
is_numeric
()
and
c1
.
is_numeric
()
and
r0
==
r1
and
e0
==
e1
:
# 2a + 2a -> 4a
# a + 2a -> 3a
# 2a + a -> 3a
# a + a -> 2a
p
.
append
(
P
(
node
,
combine_polynomes
,
\
(
left
[
0
],
right
[
0
],
c0
,
c1
,
r0
,
e0
)))
p
.
append
(
P
(
node
,
combine_polynomes
,
(
n0
,
n1
,
c0
,
c1
,
r0
,
e0
)))
return
p
...
...
@@ -118,9 +119,17 @@ def combine_numerics(root, args):
Synopsis:
c0 + c1 -> eval(c1 + c2)
"""
c0
,
c1
=
args
n0
,
n1
,
c0
,
c1
=
args
return
Leaf
(
c0
.
value
+
c1
.
value
)
scope
=
root
.
get_scope
()
# Replace the left node with the new expression
scope
[
scope
.
index
(
n0
)]
=
Leaf
(
c0
+
c1
)
# Remove the right node
scope
.
remove
(
n1
)
return
nary_node
(
'+'
,
scope
)
def
combine_polynomes
(
root
,
args
):
...
...
tests/test_rules_poly.py
View file @
76158313
...
...
@@ -3,6 +3,7 @@ import unittest
from
src.rules.poly
import
match_combine_polynomes
,
combine_polynomes
,
\
combine_numerics
from
src.possibilities
import
Possibility
as
P
from
src.node
import
ExpressionNode
,
ExpressionLeaf
as
L
from
src.parser
import
Parser
from
tests.parser
import
ParserWrapper
...
...
@@ -24,15 +25,6 @@ class TestRulesPoly(unittest.TestCase):
self
.
assertEqual
(
p
,
e
)
def
test_numbers
(
self
):
return
# TODO: Move to combine numeric test
l1
,
l2
=
root
=
tree
(
'1+2'
)
possibilities
=
match_combine_polynomes
(
root
)
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
combine_numerics
,
((
l1
,
(
l1
,
l1
,
l1
)),
(
l2
,
(
l1
,
l2
,
l1
))))])
def
test_identifiers_basic
(
self
):
a1
,
a2
=
root
=
tree
(
'a+a'
)
possibilities
=
match_combine_polynomes
(
root
)
...
...
@@ -96,16 +88,67 @@ class TestRulesPoly(unittest.TestCase):
#self.assertEqualPos(possibilities,
# [P(root, combine_polynomes, (left, right, c, c, a_b, d))])
def
test_match_combine_polynomes_numeric_combinations
(
self
):
return
root
=
tree
(
'0+1+2'
)
# TODO: this test fails with this code: l0, l1, l2 = tree('0,1,2')
l0
,
l1
,
l2
=
root
[
0
][
0
],
root
[
0
][
1
],
root
[
1
]
def
test_match_combine_numerics
(
self
):
l0
,
l1
,
l2
=
tree
(
'0,1,2'
)
root
=
l0
+
l1
+
l2
possibilities
=
match_combine_polynomes
(
root
)
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
combine_numerics
,
(
l0
,
l1
,
l0
,
l1
)),
P
(
root
,
combine_numerics
,
(
l0
,
l2
,
l0
,
l2
)),
P
(
root
,
combine_numerics
,
(
l1
,
l2
,
l1
,
l2
))])
def
test_match_combine_numerics_explicit_powers
(
self
):
l0
,
l1
,
l2
=
tree
(
'0^1,1*1,1*2^1'
)
root
=
l0
+
l1
+
l2
possibilities
=
match_combine_polynomes
(
root
)
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
combine_polynomes
,
((
l0
,
(
l1
,
l0
,
l1
)),
(
l1
,
(
l1
,
l1
,
l1
)))),
P
(
root
,
combine_polynomes
,
((
l0
,
(
l1
,
l0
,
l1
)),
(
l2
,
(
l1
,
l2
,
l1
)))),
P
(
root
,
combine_polynomes
,
((
l1
,
(
l1
,
l1
,
l1
)),
(
l2
,
(
l1
,
l2
,
l1
))))])
[
P
(
root
,
combine_numerics
,
(
l0
,
l1
,
l0
[
0
],
l1
[
1
])),
P
(
root
,
combine_numerics
,
(
l0
,
l2
,
l0
[
0
],
l2
[
1
][
0
])),
P
(
root
,
combine_numerics
,
(
l1
,
l2
,
l1
[
1
],
l2
[
1
][
0
]))])
def
test_combine_numerics
(
self
):
l0
,
l1
=
tree
(
'1,2'
)
self
.
assertEqual
(
combine_numerics
(
l0
+
l1
,
(
l0
,
l1
,
1
,
2
)),
3
)
def
test_combine_numerics_nary
(
self
):
l0
,
a
,
l1
=
tree
(
'1,a,2'
)
self
.
assertEqual
(
combine_numerics
(
l0
+
a
+
l1
,
(
l0
,
l1
,
1
,
2
)),
L
(
3
)
+
a
)
def
test_combine_polynomes
(
self
):
# 2a + 3a -> (2 + 3) * a
l0
,
a
,
l1
,
l2
=
tree
(
'2,a,3,1'
)
root
=
l0
*
a
+
l1
*
a
left
,
right
=
root
replacement
=
combine_polynomes
(
root
,
(
left
,
right
,
l0
,
l1
,
a
,
1
))
self
.
assertEqualNodes
(
replacement
,
(
l0
+
l1
)
*
a
)
# a + 3a -> (1 + 3) * a
root
=
a
+
l1
*
a
left
,
right
=
root
replacement
=
combine_polynomes
(
root
,
(
left
,
right
,
l2
,
l1
,
a
,
1
))
self
.
assertEqualNodes
(
replacement
,
(
l2
+
l1
)
*
a
)
# 2a + a -> (2 + 1) * a
root
=
l0
*
a
+
a
left
,
right
=
root
replacement
=
combine_polynomes
(
root
,
(
left
,
right
,
l0
,
l2
,
a
,
1
))
self
.
assertEqualNodes
(
replacement
,
(
l0
+
1
)
*
a
)
# a + a -> (1 + 1) * a
root
=
a
+
a
left
,
right
=
root
replacement
=
combine_polynomes
(
root
,
(
left
,
right
,
l2
,
l2
,
a
,
1
))
self
.
assertEqualNodes
(
replacement
,
(
l2
+
1
)
*
a
)
def
assertEqualNodes
(
self
,
a
,
b
):
if
not
isinstance
(
a
,
ExpressionNode
):
return
self
.
assertEqual
(
a
,
b
)
self
.
assertIsInstance
(
b
,
ExpressionNode
)
self
.
assertEqual
(
a
.
op
,
b
.
op
)
for
ca
,
cb
in
zip
(
a
,
b
):
self
.
assertEqualNodes
(
ca
,
cb
)
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