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
f057cada
Commit
f057cada
authored
Jan 15, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented and tested numerics multiplication rewriting.
parent
9f4f5029
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
111 additions
and
49 deletions
+111
-49
src/rules/numerics.py
src/rules/numerics.py
+67
-35
tests/test_rules_numerics.py
tests/test_rules_numerics.py
+44
-14
No files found.
src/rules/numerics.py
View file @
f057cada
from
itertools
import
combinations
from
.utils
import
nary_node
from
..node
import
ExpressionLeaf
as
Leaf
,
OP_DIV
,
OP_MUL
from
..possibilities
import
Possibility
as
P
,
MESSAGES
from
.utils
import
nary_node
def
add_numerics
(
root
,
args
):
"""
Combine two constants to a single constant in an n-ary addition.
Example:
2 + 3 -> 5
"""
n0
,
n1
,
c0
,
c1
=
args
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 match_subtract_numerics(node):
# """
# 3 - 2 -> 2.0
# 3.0 - 2 -> 1.0
# 3 - 2.0 -> 1.0
# 3.0 - 2.0 -> 1.0
# """
# # TODO: This should be handled by match_combine_polynomes
# assert node.is_op(OP_MUL)
def
match_divide_numerics
(
node
):
...
...
@@ -40,28 +73,6 @@ def match_divide_numerics(node):
return
[
P
(
node
,
divide_numerics
,
(
n
.
value
,
dv
))]
if
divide
else
[]
def
match_multiply_numerics
(
node
):
"""
3 * 2 -> 6
3.0 * 2 -> 6.0 # FIXME: is this correct?
3 * 2.0 -> 6.0 # FIXME: is this correct?
3.0 * 2.0 -> 6.0
"""
# TODO: Finish
assert
node
.
is_op
(
OP_MUL
)
def
match_subtract_numerics
(
node
):
"""
3 - 2 -> 2.0
3.0 - 2 -> 1.0 # FIXME: is this correct?
3 - 2.0 -> 1.0 # FIXME: is this correct?
3.0 - 2.0 -> 1.0
"""
# TODO: Finish
assert
node
.
is_op
(
OP_MUL
)
def
divide_numerics
(
root
,
args
):
"""
Combine two constants to a single constant in a division.
...
...
@@ -78,21 +89,42 @@ def divide_numerics(root, args):
return
Leaf
(
n
/
d
)
def
add_numerics
(
root
,
args
):
def
match_multiply_numerics
(
node
):
"""
3 * 2 -> 6
3.0 * 2 -> 6.0
3 * 2.0 -> 6.0
3.0 * 2.0 -> 6.0
"""
assert
node
.
is_op
(
OP_MUL
)
p
=
[]
scope
=
node
.
get_scope
()
numerics
=
filter
(
lambda
n
:
n
.
is_numeric
(),
scope
)
for
args
in
combinations
(
numerics
,
2
):
p
.
append
(
P
(
node
,
multiply_numerics
,
args
))
return
p
def
multiply_numerics
(
root
,
args
):
"""
Combine two constants to a single constant in an n-ary
plus
.
Combine two constants to a single constant in an n-ary
multiplication
.
Example:
2
+ 3 -> 5
2
* 3 -> 6
"""
n0
,
n1
,
c0
,
c1
=
args
scope
=
root
.
get_scope
()
n0
,
n1
=
args
scope
=
[]
for
n
in
root
.
get_scope
():
if
hash
(
n
)
==
hash
(
n0
):
# Replace the left node with the new expression
scope
[
scope
.
index
(
n0
)]
=
Leaf
(
c0
+
c1
)
scope
.
append
(
Leaf
(
n0
.
value
*
n1
.
value
))
#scope.append(n)
elif
hash
(
n
)
!=
hash
(
n1
):
# Remove the right node
scope
.
remove
(
n1
)
scope
.
append
(
n
)
return
nary_node
(
'
+
'
,
scope
)
return
nary_node
(
'
*
'
,
scope
)
tests/test_rules_numerics.py
View file @
f057cada
from
src.rules.numerics
import
match_divide_numerics
,
divide_numerics
,
\
add
_numerics
from
src.rules.numerics
import
add_numerics
,
match_
divide_numerics
,
\
divide_numerics
,
match_multiply_numerics
,
multiply
_numerics
from
src.possibilities
import
Possibility
as
P
from
src.node
import
ExpressionLeaf
as
L
from
tests.rulestestcase
import
RulesTestCase
...
...
@@ -8,11 +8,20 @@ from tests.test_rules_poly import tree
class
TestRulesNumerics
(
RulesTestCase
):
def
test_add_numerics
(
self
):
l0
,
a
,
l1
=
tree
(
'1,a,2'
)
self
.
assertEqual
(
add_numerics
(
l0
+
l1
,
(
l0
,
l1
,
1
,
2
)),
3
)
self
.
assertEqual
(
add_numerics
(
l0
+
a
+
l1
,
(
l0
,
l1
,
1
,
2
)),
L
(
3
)
+
a
)
def
test_add_numerics
(
self
):
l0
,
a
,
l1
=
tree
(
'1,a,2'
)
self
.
assertEqual
(
add_numerics
(
l0
+
l1
,
(
l0
,
l1
,
1
,
2
)),
3
)
self
.
assertEqual
(
add_numerics
(
l0
+
a
+
l1
,
(
l0
,
l1
,
1
,
2
)),
L
(
3
)
+
a
)
def
test_match_divide_numerics
(
self
):
# FIXME: Parser does not recognize floats
#a, b, i2, i3, i6, f1, f2, f3 = tree('a,b,2,3,6,1.0,2.0,3.0')
a
,
b
,
i2
,
i3
,
i6
=
tree
(
'a,b,2,3,6'
)
f1
,
f2
,
f3
=
L
(
1.0
),
L
(
2.0
),
L
(
3.0
)
a
,
b
,
i2
,
i3
,
i6
,
f1
,
f2
,
f3
=
tree
(
'a,b,2,3,6,1.0,2.0,3.0'
)
root
=
i6
/
i2
possibilities
=
match_divide_numerics
(
root
)
...
...
@@ -48,18 +57,39 @@ class TestRulesNumerics(RulesTestCase):
self
.
assertEqualPos
(
possibilities
,
[])
def
test_divide_numerics
(
self
):
# FIXME: Parser does not recognize floats
#i2, i3, i6, f2, f3 = tree('2,3,6,2.0,3.0')
i2
,
i3
,
i6
=
tree
(
'2,3,6'
)
f2
,
f3
=
L
(
2.0
),
L
(
3.0
)
i2
,
i3
,
i6
,
f2
,
f3
=
tree
(
'2,3,6,2.0,3.0'
)
self
.
assertEqual
(
divide_numerics
(
i6
/
i2
,
(
6
,
2
)),
3
)
self
.
assertEqual
(
divide_numerics
(
f3
/
i2
,
(
3.0
,
2
)),
1.5
)
self
.
assertEqual
(
divide_numerics
(
i3
/
f2
,
(
3
,
2.0
)),
1.5
)
self
.
assertEqual
(
divide_numerics
(
f3
/
f2
,
(
3.0
,
2.0
)),
1.5
)
def
test_
add
_numerics
(
self
):
l0
,
a
,
l1
=
tree
(
'1,a,2
'
)
def
test_
match_multiply
_numerics
(
self
):
i2
,
i3
,
i6
,
f2
,
f3
,
f6
=
tree
(
'2,3,6,2.0,3.0,6.0
'
)
self
.
assertEqual
(
add_numerics
(
l0
+
l1
,
(
l0
,
l1
,
1
,
2
)),
3
)
self
.
assertEqual
(
add_numerics
(
l0
+
a
+
l1
,
(
l0
,
l1
,
1
,
2
)),
L
(
3
)
+
a
)
root
=
i3
*
i2
self
.
assertEqual
(
match_multiply_numerics
(
root
),
[
P
(
root
,
multiply_numerics
,
(
i3
,
i2
))])
root
=
f3
*
i2
self
.
assertEqual
(
match_multiply_numerics
(
root
),
[
P
(
root
,
multiply_numerics
,
(
f3
,
i2
))])
root
=
i3
*
f2
self
.
assertEqual
(
match_multiply_numerics
(
root
),
[
P
(
root
,
multiply_numerics
,
(
i3
,
f2
))])
root
=
f3
*
f2
self
.
assertEqual
(
match_multiply_numerics
(
root
),
[
P
(
root
,
multiply_numerics
,
(
f3
,
f2
))])
def
test_multiply_numerics
(
self
):
a
,
b
,
i2
,
i3
,
i6
,
f2
,
f3
,
f6
=
tree
(
'a,b,2,3,6,2.0,3.0,6.0'
)
self
.
assertEqual
(
multiply_numerics
(
i3
*
i2
,
(
i3
,
i2
)),
6
)
self
.
assertEqual
(
multiply_numerics
(
f3
*
i2
,
(
f3
,
i2
)),
6.0
)
self
.
assertEqual
(
multiply_numerics
(
i3
*
f2
,
(
i3
,
f2
)),
6.0
)
self
.
assertEqual
(
multiply_numerics
(
f3
*
f2
,
(
f3
,
f2
)),
6.0
)
self
.
assertEqualNodes
(
multiply_numerics
(
a
*
i3
*
i2
*
b
,
(
i3
,
i2
)),
a
*
6
*
b
)
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