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
90b24c3c
Commit
90b24c3c
authored
Jan 14, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added basic fraction rewrite rules.
parent
3909b46f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
1 deletion
+80
-1
src/rules/__init__.py
src/rules/__init__.py
+3
-1
src/rules/fractions.py
src/rules/fractions.py
+43
-0
tests/test_rules_fractions.py
tests/test_rules_fractions.py
+34
-0
No files found.
src/rules/__init__.py
View file @
90b24c3c
...
@@ -4,12 +4,14 @@ from .powers import match_add_exponents, match_subtract_exponents, \
...
@@ -4,12 +4,14 @@ 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
from
.numerics
import
match_divide_numerics
from
.numerics
import
match_divide_numerics
from
.fractions
import
match_constant_division
RULES
=
{
RULES
=
{
OP_ADD
:
[
match_combine_polynomes
],
OP_ADD
:
[
match_combine_polynomes
],
OP_MUL
:
[
match_expand
,
match_add_exponents
],
OP_MUL
:
[
match_expand
,
match_add_exponents
],
OP_DIV
:
[
match_subtract_exponents
,
match_divide_numerics
],
OP_DIV
:
[
match_subtract_exponents
,
match_divide_numerics
,
\
match_constant_division
],
OP_POW
:
[
match_multiply_exponents
,
match_duplicate_exponent
,
\
OP_POW
:
[
match_multiply_exponents
,
match_duplicate_exponent
,
\
match_remove_negative_exponent
,
match_exponent_to_root
],
match_remove_negative_exponent
,
match_exponent_to_root
],
}
}
src/rules/fractions.py
0 → 100644
View file @
90b24c3c
from
..node
import
ExpressionLeaf
as
L
,
OP_DIV
from
..possibilities
import
Possibility
as
P
,
MESSAGES
from
..translate
import
_
def
match_constant_division
(
node
):
"""
a / 0 -> Division by zero
a / 1 -> a
0 / a -> 0
"""
assert
node
.
is_op
(
OP_DIV
)
p
=
[]
nominator
,
denominator
=
node
# a / 0
if
denominator
==
0
:
raise
ZeroDivisionError
()
# a / 1
if
denominator
==
1
:
p
.
append
(
P
(
node
,
division_by_one
,
(
nominator
,)))
# 0 / a
if
nominator
==
0
:
p
.
append
(
P
(
node
,
division_of_zero
))
return
p
def
division_by_one
(
root
,
args
):
"""
a / 1 -> a
"""
return
args
[
0
]
def
division_of_zero
(
root
,
args
):
"""
0 / a -> 0
"""
return
L
(
0
)
tests/test_rules_fractions.py
0 → 100644
View file @
90b24c3c
from
src.rules.fractions
import
match_constant_division
,
division_by_one
,
\
division_of_zero
from
src.possibilities
import
Possibility
as
P
from
tests.test_rules_poly
import
tree
from
tests.rulestestcase
import
RulesTestCase
class
TestRulesFractions
(
RulesTestCase
):
def
test_match_constant_division
(
self
):
a
,
zero
=
tree
(
'a,0'
)
root
=
a
/
zero
self
.
assertRaises
(
ZeroDivisionError
,
match_constant_division
,
root
)
root
=
a
/
1
possibilities
=
match_constant_division
(
root
)
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
division_by_one
,
(
a
,))])
root
=
zero
/
a
possibilities
=
match_constant_division
(
root
)
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
division_of_zero
)])
def
test_division_by_one
(
self
):
a
=
tree
(
'a'
)
root
=
a
/
1
self
.
assertEqualNodes
(
division_by_one
(
root
,
(
a
,)),
a
)
def
test_division_of_zero
(
self
):
a
,
zero
=
tree
(
'a,0'
)
root
=
zero
/
a
self
.
assertEqualNodes
(
division_of_zero
(
root
,
()),
zero
)
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