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
eb087f2e
Commit
eb087f2e
authored
Feb 27, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added rule to multiply fractions.
parent
6029187f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
3 deletions
+53
-3
src/rules/__init__.py
src/rules/__init__.py
+2
-2
src/rules/fractions.py
src/rules/fractions.py
+34
-0
tests/test_rules_fractions.py
tests/test_rules_fractions.py
+17
-1
No files found.
src/rules/__init__.py
View file @
eb087f2e
...
...
@@ -10,7 +10,7 @@ from .numerics import match_add_numerics, match_divide_numerics, \
match_multiply_numerics
,
match_multiply_zero
,
match_multiply_one
,
\
match_raise_numerics
from
.fractions
import
match_constant_division
,
match_add_constant_fractions
,
\
match_expand_and_add_fractions
match_expand_and_add_fractions
,
match_multiply_fractions
from
.negation
import
match_negated_factor
,
match_negate_polynome
,
\
match_negated_division
from
.sort
import
match_sort_multiplicants
...
...
@@ -23,7 +23,7 @@ RULES = {
OP_MUL
:
[
match_multiply_numerics
,
match_expand
,
match_add_exponents
,
match_expand_and_add_fractions
,
match_multiply_zero
,
match_negated_factor
,
match_multiply_one
,
match_sort_multiplicants
],
match_sort_multiplicants
,
match_multiply_fractions
],
OP_DIV
:
[
match_subtract_exponents
,
match_divide_numerics
,
match_constant_division
,
match_negated_division
],
OP_POW
:
[
match_multiply_exponents
,
match_duplicate_exponent
,
...
...
src/rules/fractions.py
View file @
eb087f2e
...
...
@@ -173,3 +173,37 @@ def match_expand_and_add_fractions(node):
p
=
[]
return
p
def
match_multiply_fractions
(
node
):
"""
a / b * (c / d) -> ac / (bd)
"""
# TODO: is 'add' Appropriate when rewriting to "(a + (-d)) / * (b / c)"?
assert
node
.
is_op
(
OP_MUL
)
p
=
[]
scope
=
Scope
(
node
)
fractions
=
filter
(
lambda
n
:
n
.
is_op
(
OP_DIV
),
scope
)
for
ab
,
cd
in
combinations
(
fractions
,
2
):
p
.
append
(
P
(
node
,
multiply_fractions
,
(
scope
,
ab
,
cd
)))
return
p
def
multiply_fractions
(
root
,
args
):
"""
a / b * (c / d) -> ac / (bd)
"""
scope
,
ab
,
cd
=
args
a
,
b
=
ab
c
,
d
=
cd
scope
.
replace
(
ab
,
a
*
c
/
(
b
*
d
))
scope
.
remove
(
cd
)
return
scope
.
as_nary_node
()
MESSAGES
[
multiply_fractions
]
=
_
(
'Multiply fractions {2} and {3}.'
)
tests/test_rules_fractions.py
View file @
eb087f2e
from
src.rules.fractions
import
match_constant_division
,
division_by_one
,
\
division_of_zero
,
division_by_self
,
match_add_constant_fractions
,
\
equalize_denominators
,
add_nominators
equalize_denominators
,
add_nominators
,
match_multiply_fractions
,
\
multiply_fractions
from
src.node
import
Scope
from
src.possibilities
import
Possibility
as
P
from
tests.rulestestcase
import
RulesTestCase
,
tree
...
...
@@ -123,3 +124,18 @@ class TestRulesFractions(RulesTestCase):
n0
,
n1
=
root
=
a
/
-
b
+
-
c
/
-
b
self
.
assertEqualNodes
(
add_nominators
(
root
,
(
n0
,
n1
)),
(
a
+
-
c
)
/
-
b
)
def
test_match_multiply_fractions
(
self
):
(
a
,
b
),
(
c
,
d
)
=
ab
,
cd
=
root
=
tree
(
'a / b * (c / d)'
)
self
.
assertEqualPos
(
match_multiply_fractions
(
root
),
[
P
(
root
,
multiply_fractions
,
(
Scope
(
root
),
ab
,
cd
))])
def
test_multiply_fractions
(
self
):
(
a
,
b
),
(
c
,
d
)
=
ab
,
cd
=
root
=
tree
(
'a / b * (c / d)'
)
self
.
assertEqual
(
multiply_fractions
(
root
,
(
Scope
(
root
),
ab
,
cd
)),
a
*
c
/
(
b
*
d
))
(
ab
,
e
),
cd
=
root
=
tree
(
'a / b * e * (c / d)'
)
self
.
assertEqual
(
multiply_fractions
(
root
,
(
Scope
(
root
),
ab
,
cd
)),
a
*
c
/
(
b
*
d
)
*
e
)
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