Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
trs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
Taddeüs Kroes
trs
Commits
1a74b48d
Commit
1a74b48d
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added 0*a -> 0 rule.
parent
076f6aae
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/rules/__init__.py
+3
-2
3 additions, 2 deletions
src/rules/__init__.py
src/rules/numerics.py
+46
-0
46 additions, 0 deletions
src/rules/numerics.py
tests/test_leiden_oefenopgave.py
+4
-2
4 additions, 2 deletions
tests/test_leiden_oefenopgave.py
with
53 additions
and
4 deletions
src/rules/__init__.py
+
3
−
2
View file @
1a74b48d
...
...
@@ -6,7 +6,8 @@ from .powers import match_add_exponents, match_subtract_exponents, \
match_multiply_exponents
,
match_duplicate_exponent
,
\
match_remove_negative_exponent
,
match_exponent_to_root
,
\
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
,
\
match_expand_and_add_fractions
from
.negation
import
match_negate_group
,
match_negated_division
...
...
@@ -15,7 +16,7 @@ RULES = {
OP_ADD
:
[
match_add_constant_fractions
,
match_combine_polynomes
,
\
match_combine_groups
],
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
,
\
match_constant_division
,
match_negated_division
],
OP_POW
:
[
match_multiply_exponents
,
match_duplicate_exponent
,
\
...
...
This diff is collapsed.
Click to expand it.
src/rules/numerics.py
+
46
−
0
View file @
1a74b48d
...
...
@@ -106,6 +106,52 @@ def divide_numerics(root, args):
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
):
"""
3 * 2 -> 6
...
...
This diff is collapsed.
Click to expand it.
tests/test_leiden_oefenopgave.py
+
4
−
2
View file @
1a74b48d
...
...
@@ -102,8 +102,10 @@ class TestLeidenOefenopgave(TestCase):
'
-20x + 16 * x ^ 2 - 25 + 5 * 4x
'
,
'
-20x + 16 * x ^ 2 - 25 + 20x
'
,
'
(-20 + 20)x + 16 * x ^ 2 - 25
'
,
'
0x + 16 * x ^ 2 - 25
'
,])
# FIXME: '16x^2 - 25'])
'
0x + 16 * x ^ 2 - 25
'
,
'
0 + 16 * x ^ 2 - 25
'
,
'
-25 + 16 * x ^ 2
'
])
# FIXME: '16 * x ^ 2 - 25'])
def
test_2
(
self
):
pass
...
...
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