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
99832583
Commit
99832583
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added negated division rewrite rules.
parent
271a591b
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
+2
-2
2 additions, 2 deletions
src/rules/__init__.py
src/rules/negation.py
+48
-1
48 additions, 1 deletion
src/rules/negation.py
tests/test_rules_negation.py
+44
-0
44 additions, 0 deletions
tests/test_rules_negation.py
with
94 additions
and
3 deletions
src/rules/__init__.py
+
2
−
2
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
],
...
...
This diff is collapsed.
Click to expand it.
src/rules/negation.py
+
48
−
1
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}.
'
)
This diff is collapsed.
Click to expand it.
tests/test_rules_negation.py
0 → 100644
+
44
−
0
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
])
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