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
99832583
Commit
99832583
authored
Jan 24, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added negated division rewrite rules.
parent
271a591b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
94 additions
and
3 deletions
+94
-3
src/rules/__init__.py
src/rules/__init__.py
+2
-2
src/rules/negation.py
src/rules/negation.py
+48
-1
tests/test_rules_negation.py
tests/test_rules_negation.py
+44
-0
No files found.
src/rules/__init__.py
View file @
99832583
...
...
@@ -9,7 +9,7 @@ from .powers import match_add_exponents, match_subtract_exponents, \
from
.numerics
import
match_divide_numerics
,
match_multiply_numerics
from
.fractions
import
match_constant_division
,
match_add_constant_fractions
,
\
match_expand_and_add_fractions
from
.negation
import
match_negate_group
from
.negation
import
match_negate_group
,
match_negated_division
RULES
=
{
OP_ADD
:
[
match_add_constant_fractions
,
match_combine_polynomes
,
\
...
...
@@ -17,7 +17,7 @@ RULES = {
OP_MUL
:
[
match_multiply_numerics
,
match_expand
,
match_add_exponents
,
\
match_expand_and_add_fractions
],
OP_DIV
:
[
match_subtract_exponents
,
match_divide_numerics
,
\
match_constant_division
],
match_constant_division
,
match_negated_division
],
OP_POW
:
[
match_multiply_exponents
,
match_duplicate_exponent
,
\
match_remove_negative_exponent
,
match_exponent_to_root
,
\
match_extend_exponent
],
...
...
src/rules/negation.py
View file @
99832583
from
..node
import
OP_NEG
,
OP_ADD
,
OP_MUL
,
get_scope
,
nary_node
from
..node
import
get_scope
,
nary_node
,
OP_NEG
,
OP_ADD
,
OP_MUL
,
OP_DIV
from
..possibilities
import
Possibility
as
P
,
MESSAGES
from
..translate
import
_
...
...
@@ -85,4 +85,51 @@ def double_negation(root, args):
return
node
[
0
][
0
]
def
match_negated_division
(
node
):
"""
-a / -b -> a / b
"""
assert
node
.
is_op
(
OP_DIV
)
a
,
b
=
node
a_neg
=
a
.
is_op
(
OP_NEG
)
b_neg
=
b
.
is_op
(
OP_NEG
)
if
a_neg
and
b_neg
:
return
[
P
(
node
,
double_negated_division
,
(
node
,))]
elif
a_neg
:
return
[
P
(
node
,
single_negated_division
,
(
a
[
0
],
b
))]
elif
b_neg
:
return
[
P
(
node
,
single_negated_division
,
(
a
,
b
[
0
]))]
return
[]
def
single_negated_division
(
root
,
args
):
"""
-a / b -> -(a / b)
a / -b -> -(a / b)
"""
a
,
b
=
args
return
-
(
a
/
b
)
MESSAGES
[
single_negated_division
]
=
\
_
(
'Bring negation outside of the division: -({1} / {2}).'
)
def
double_negated_division
(
root
,
args
):
"""
-a / -b -> a / b
"""
a
,
b
=
root
return
a
[
0
]
/
b
[
0
]
MESSAGES
[
double_negated_division
]
=
\
_
(
'Eliminate top and bottom negation in {1}.'
)
MESSAGES
[
double_negation
]
=
_
(
'Remove double negation in {1}.'
)
tests/test_rules_negation.py
0 → 100644
View file @
99832583
from
src.rules.negation
import
match_negated_division
,
\
single_negated_division
,
double_negated_division
from
src.possibilities
import
Possibility
as
P
from
tests.rulestestcase
import
RulesTestCase
,
tree
class
TestRulesNegation
(
RulesTestCase
):
def
test_match_negated_division_none
(
self
):
self
.
assertEqual
(
match_negated_division
(
tree
(
'1 / 2'
)),
[])
def
test_match_negated_division_single
(
self
):
l1
,
l2
=
root
=
tree
(
'-1 / 2'
)
possibilities
=
match_negated_division
(
root
)
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
single_negated_division
,
(
l1
[
0
],
l2
))])
l1
,
l2
=
root
=
tree
(
'1 / -2'
)
possibilities
=
match_negated_division
(
root
)
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
single_negated_division
,
(
l1
,
l2
[
0
]))])
def
test_match_negated_division_double
(
self
):
root
=
tree
(
'-1 / -2'
)
possibilities
=
match_negated_division
(
root
)
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
double_negated_division
,
(
root
,))])
def
test_single_negated_division
(
self
):
l1
,
l2
=
root
=
tree
(
'-1 / 2'
)
self
.
assertEqualNodes
(
single_negated_division
(
root
,
(
l1
[
0
],
l2
)),
-
(
l1
[
0
]
/
l2
))
l1
,
l2
=
root
=
tree
(
'1 / -2'
)
self
.
assertEqualNodes
(
single_negated_division
(
root
,
(
l1
,
l2
[
0
])),
-
(
l1
/
l2
[
0
]))
def
test_double_negated_division
(
self
):
l1
,
l2
=
root
=
tree
(
'-1 / -2'
)
self
.
assertEqualNodes
(
double_negated_division
(
root
,
(
root
,)),
l1
[
0
]
/
l2
[
0
])
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