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
0551ec1b
Commit
0551ec1b
authored
Jan 24, 2012
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of kompiler.org:trs
parents
94269a3f
87d0f49c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
4 deletions
+53
-4
src/rules/__init__.py
src/rules/__init__.py
+3
-2
src/rules/numerics.py
src/rules/numerics.py
+46
-0
tests/test_leiden_oefenopgave.py
tests/test_leiden_oefenopgave.py
+4
-2
No files found.
src/rules/__init__.py
View file @
0551ec1b
...
@@ -6,7 +6,8 @@ from .powers import match_add_exponents, match_subtract_exponents, \
...
@@ -6,7 +6,8 @@ from .powers import match_add_exponents, match_subtract_exponents, \
match_multiply_exponents
,
match_duplicate_exponent
,
\
match_multiply_exponents
,
match_duplicate_exponent
,
\
match_remove_negative_exponent
,
match_exponent_to_root
,
\
match_remove_negative_exponent
,
match_exponent_to_root
,
\
match_extend_exponent
match_extend_exponent
from
.numerics
import
match_divide_numerics
,
match_multiply_numerics
from
.numerics
import
match_divide_numerics
,
match_multiply_numerics
,
\
match_multiply_zero
from
.fractions
import
match_constant_division
,
match_add_constant_fractions
,
\
from
.fractions
import
match_constant_division
,
match_add_constant_fractions
,
\
match_expand_and_add_fractions
match_expand_and_add_fractions
from
.negation
import
match_negate_group
,
match_negated_division
from
.negation
import
match_negate_group
,
match_negated_division
...
@@ -15,7 +16,7 @@ RULES = {
...
@@ -15,7 +16,7 @@ RULES = {
OP_ADD
:
[
match_add_constant_fractions
,
match_combine_polynomes
,
\
OP_ADD
:
[
match_add_constant_fractions
,
match_combine_polynomes
,
\
match_combine_groups
],
match_combine_groups
],
OP_MUL
:
[
match_multiply_numerics
,
match_expand
,
match_add_exponents
,
\
OP_MUL
:
[
match_multiply_numerics
,
match_expand
,
match_add_exponents
,
\
match_expand_and_add_fractions
],
match_expand_and_add_fractions
,
match_multiply_zero
],
OP_DIV
:
[
match_subtract_exponents
,
match_divide_numerics
,
\
OP_DIV
:
[
match_subtract_exponents
,
match_divide_numerics
,
\
match_constant_division
,
match_negated_division
],
match_constant_division
,
match_negated_division
],
OP_POW
:
[
match_multiply_exponents
,
match_duplicate_exponent
,
\
OP_POW
:
[
match_multiply_exponents
,
match_duplicate_exponent
,
\
...
...
src/rules/numerics.py
View file @
0551ec1b
...
@@ -106,6 +106,52 @@ def divide_numerics(root, args):
...
@@ -106,6 +106,52 @@ def divide_numerics(root, args):
return
Leaf
(
n
/
d
)
return
Leaf
(
n
/
d
)
def
match_multiply_zero
(
node
):
"""
a * 0 -> 0
0 * a -> 0
-0 * a -> -0
0 * -a -> -0
-0 * -a -> 0
"""
assert
node
.
is_op
(
OP_MUL
)
left
,
right
=
node
is_zero
=
lambda
n
:
n
.
is_leaf
()
and
n
.
value
==
0
if
is_zero
(
left
):
negated
=
right
.
is_op
(
OP_NEG
)
elif
is_zero
(
right
):
negated
=
left
.
is_op
(
OP_NEG
)
elif
left
.
is_op
(
OP_NEG
)
and
is_zero
(
left
[
0
]):
negated
=
not
right
.
is_op
(
OP_NEG
)
elif
right
.
is_op
(
OP_NEG
)
and
is_zero
(
right
[
0
]):
negated
=
not
left
.
is_op
(
OP_NEG
)
else
:
return
[]
return
[
P
(
node
,
multiply_zero
,
(
negated
,))]
def
multiply_zero
(
root
,
args
):
"""
a * 0 -> 0
0 * a -> 0
-0 * a -> -0
0 * -a -> -0
-0 * -a -> 0
"""
negated
=
args
[
0
]
if
negated
:
return
-
Leaf
(
0
)
else
:
return
Leaf
(
0
)
MESSAGES
[
multiply_zero
]
=
_
(
'Multiplication with zero yields zero.'
)
def
match_multiply_numerics
(
node
):
def
match_multiply_numerics
(
node
):
"""
"""
3 * 2 -> 6
3 * 2 -> 6
...
...
tests/test_leiden_oefenopgave.py
View file @
0551ec1b
...
@@ -102,8 +102,10 @@ class TestLeidenOefenopgave(TestCase):
...
@@ -102,8 +102,10 @@ class TestLeidenOefenopgave(TestCase):
'-20x + 16 * x ^ 2 - 25 + 5 * 4x'
,
'-20x + 16 * x ^ 2 - 25 + 5 * 4x'
,
'-20x + 16 * x ^ 2 - 25 + 20x'
,
'-20x + 16 * x ^ 2 - 25 + 20x'
,
'(-20 + 20)x + 16 * x ^ 2 - 25'
,
'(-20 + 20)x + 16 * x ^ 2 - 25'
,
'0x + 16 * x ^ 2 - 25'
,])
'0x + 16 * x ^ 2 - 25'
,
# FIXME: '16x^2 - 25'])
'0 + 16 * x ^ 2 - 25'
,
'-25 + 16 * x ^ 2'
])
# FIXME: '16 * x ^ 2 - 25'])
def
test_2
(
self
):
def
test_2
(
self
):
pass
pass
...
...
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