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
697592af
Commit
697592af
authored
Feb 27, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added possibility to multiply denominators of division addition.
parent
01acdf18
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
21 deletions
+33
-21
src/rules/fractions.py
src/rules/fractions.py
+17
-10
tests/test_rules_fractions.py
tests/test_rules_fractions.py
+16
-11
No files found.
src/rules/fractions.py
View file @
697592af
...
@@ -70,17 +70,20 @@ MESSAGES[division_by_self] = _('Division of {1} by {1} reduces to 1.')
...
@@ -70,17 +70,20 @@ MESSAGES[division_by_self] = _('Division of {1} by {1} reduces to 1.')
def
match_add_constant_fractions
(
node
):
def
match_add_constant_fractions
(
node
):
"""
"""
1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4 # Equalize denominators
1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4 # Equalize denominators
2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4
2 / 4 + 3 / 4 -> 5 / 4 # Equal denominators, so nominators can
2 / 4 + 3 / 4 -> 5 / 4 # Equal denominators, so nominators can
# be added
# be added
2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
2 / 4 - 3 / 4 -> -1 / 4
2 / 4 - 3 / 4 -> -1 / 4 # Equal denominators, so nominators can
1 / 2 + 3 / 4 -> 4 / 8 + 6 / 8 # Equalize denominators by multiplying
# be subtracted
# them with eachother
"""
"""
assert
node
.
is_op
(
OP_ADD
)
assert
node
.
is_op
(
OP_ADD
)
p
=
[]
p
=
[]
scope
=
Scope
(
node
)
fractions
=
filter
(
lambda
node
:
node
.
is_op
(
OP_DIV
),
Scope
(
node
)
)
fractions
=
filter
(
lambda
node
:
node
.
is_op
(
OP_DIV
),
scope
)
for
a
,
b
in
combinations
(
fractions
,
2
):
for
a
,
b
in
combinations
(
fractions
,
2
):
na
,
da
=
a
na
,
da
=
a
...
@@ -94,7 +97,12 @@ def match_add_constant_fractions(node):
...
@@ -94,7 +97,12 @@ def match_add_constant_fractions(node):
# least common multiple of their denominators. Later, the
# least common multiple of their denominators. Later, the
# nominators will be added
# nominators will be added
denom
=
least_common_multiple
(
da
.
value
,
db
.
value
)
denom
=
least_common_multiple
(
da
.
value
,
db
.
value
)
p
.
append
(
P
(
node
,
equalize_denominators
,
(
a
,
b
,
denom
)))
p
.
append
(
P
(
node
,
equalize_denominators
,
(
scope
,
a
,
b
,
denom
)))
# Also, add the (non-recommended) possibility to multiply the
# denominators
p
.
append
(
P
(
node
,
equalize_denominators
,
(
scope
,
a
,
b
,
da
.
value
*
db
.
value
)))
return
p
return
p
...
@@ -104,10 +112,9 @@ def equalize_denominators(root, args):
...
@@ -104,10 +112,9 @@ def equalize_denominators(root, args):
1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4
1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4
a / 2 + b / 4 -> 2a / 4 + b / 4
a / 2 + b / 4 -> 2a / 4 + b / 4
"""
"""
denom
=
args
[
2
]
scope
,
denom
=
args
[::
3
]
scope
=
Scope
(
root
)
for
fraction
in
args
[
:
2
]:
for
fraction
in
args
[
1
:
3
]:
n
,
d
=
fraction
n
,
d
=
fraction
mult
=
denom
/
d
.
value
mult
=
denom
/
d
.
value
...
@@ -125,8 +132,8 @@ def equalize_denominators(root, args):
...
@@ -125,8 +132,8 @@ def equalize_denominators(root, args):
return
scope
.
as_nary_node
()
return
scope
.
as_nary_node
()
MESSAGES
[
equalize_denominators
]
=
_
(
'Equalize the denominators of division'
MESSAGES
[
equalize_denominators
]
=
_
(
'Equalize the denominators of division
s
'
'
of {1} by {2
}.'
)
'
{2} and {3} to {4
}.'
)
def
add_nominators
(
root
,
args
):
def
add_nominators
(
root
,
args
):
...
...
tests/test_rules_fractions.py
View file @
697592af
from
src.rules.fractions
import
match_constant_division
,
division_by_one
,
\
from
src.rules.fractions
import
match_constant_division
,
division_by_one
,
\
division_of_zero
,
division_by_self
,
match_add_constant_fractions
,
\
division_of_zero
,
division_by_self
,
match_add_constant_fractions
,
\
equalize_denominators
,
add_nominators
equalize_denominators
,
add_nominators
from
src.node
import
Scope
from
src.possibilities
import
Possibility
as
P
from
src.possibilities
import
Possibility
as
P
from
tests.rulestestcase
import
RulesTestCase
,
tree
from
tests.rulestestcase
import
RulesTestCase
,
tree
...
@@ -51,12 +52,14 @@ class TestRulesFractions(RulesTestCase):
...
@@ -51,12 +52,14 @@ class TestRulesFractions(RulesTestCase):
n0
,
n1
=
root
=
l1
/
l2
+
l3
/
l4
n0
,
n1
=
root
=
l1
/
l2
+
l3
/
l4
possibilities
=
match_add_constant_fractions
(
root
)
possibilities
=
match_add_constant_fractions
(
root
)
self
.
assertEqualPos
(
possibilities
,
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
equalize_denominators
,
(
n0
,
n1
,
4
))])
[
P
(
root
,
equalize_denominators
,
(
Scope
(
root
),
n0
,
n1
,
4
)),
P
(
root
,
equalize_denominators
,
(
Scope
(
root
),
n0
,
n1
,
8
))])
(((
n0
,
n1
),
n2
),
n3
),
n4
=
root
=
a
+
l1
/
l2
+
b
+
l3
/
l4
+
c
(((
n0
,
n1
),
n2
),
n3
),
n4
=
root
=
a
+
l1
/
l2
+
b
+
l3
/
l4
+
c
possibilities
=
match_add_constant_fractions
(
root
)
possibilities
=
match_add_constant_fractions
(
root
)
self
.
assertEqualPos
(
possibilities
,
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
equalize_denominators
,
(
n1
,
n3
,
4
))])
[
P
(
root
,
equalize_denominators
,
(
Scope
(
root
),
n1
,
n3
,
4
)),
P
(
root
,
equalize_denominators
,
(
Scope
(
root
),
n1
,
n3
,
8
))])
n0
,
n1
=
root
=
l2
/
l4
+
l3
/
l4
n0
,
n1
=
root
=
l2
/
l4
+
l3
/
l4
possibilities
=
match_add_constant_fractions
(
root
)
possibilities
=
match_add_constant_fractions
(
root
)
...
@@ -74,7 +77,8 @@ class TestRulesFractions(RulesTestCase):
...
@@ -74,7 +77,8 @@ class TestRulesFractions(RulesTestCase):
(((
n0
,
n1
),
n2
),
n3
),
n4
=
root
=
a
+
l2
/
l2
+
b
+
(
-
l3
/
l4
)
+
c
(((
n0
,
n1
),
n2
),
n3
),
n4
=
root
=
a
+
l2
/
l2
+
b
+
(
-
l3
/
l4
)
+
c
possibilities
=
match_add_constant_fractions
(
root
)
possibilities
=
match_add_constant_fractions
(
root
)
self
.
assertEqualPos
(
possibilities
,
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
equalize_denominators
,
(
n1
,
n3
,
4
))])
[
P
(
root
,
equalize_denominators
,
(
Scope
(
root
),
n1
,
n3
,
4
)),
P
(
root
,
equalize_denominators
,
(
Scope
(
root
),
n1
,
n3
,
8
))])
(((
n0
,
n1
),
n2
),
n3
),
n4
=
root
=
a
+
l2
/
l4
+
b
+
(
-
l3
/
l4
)
+
c
(((
n0
,
n1
),
n2
),
n3
),
n4
=
root
=
a
+
l2
/
l4
+
b
+
(
-
l3
/
l4
)
+
c
possibilities
=
match_add_constant_fractions
(
root
)
possibilities
=
match_add_constant_fractions
(
root
)
...
@@ -85,22 +89,23 @@ class TestRulesFractions(RulesTestCase):
...
@@ -85,22 +89,23 @@ class TestRulesFractions(RulesTestCase):
a
,
b
,
l1
,
l2
,
l3
,
l4
=
tree
(
'a,b,1,2,3,4'
)
a
,
b
,
l1
,
l2
,
l3
,
l4
=
tree
(
'a,b,1,2,3,4'
)
n0
,
n1
=
root
=
l1
/
l2
+
l3
/
l4
n0
,
n1
=
root
=
l1
/
l2
+
l3
/
l4
self
.
assertEqualNodes
(
equalize_denominators
(
root
,
(
n0
,
n1
,
4
)),
self
.
assertEqualNodes
(
equalize_denominators
(
root
,
l2
/
l4
+
l3
/
l4
)
(
Scope
(
root
),
n0
,
n1
,
4
)),
l2
/
l4
+
l3
/
l4
)
n0
,
n1
=
root
=
a
/
l2
+
b
/
l4
n0
,
n1
=
root
=
a
/
l2
+
b
/
l4
self
.
assertEqualNodes
(
equalize_denominators
(
root
,
(
n0
,
n1
,
4
)),
self
.
assertEqualNodes
(
equalize_denominators
(
root
,
(
l2
*
a
)
/
l4
+
b
/
l4
)
(
Scope
(
root
),
n0
,
n1
,
4
)),
(
l2
*
a
)
/
l4
+
b
/
l4
)
#2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
#2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
n0
,
n1
=
root
=
l1
/
l2
+
(
-
l3
/
l4
)
n0
,
n1
=
root
=
l1
/
l2
+
(
-
l3
/
l4
)
self
.
assertEqualNodes
(
equalize_denominators
(
root
,
(
n0
,
n1
,
4
)),
self
.
assertEqualNodes
(
equalize_denominators
(
root
,
l2
/
l4
+
(
-
l3
/
l4
))
(
Scope
(
root
),
n0
,
n1
,
4
)),
l2
/
l4
+
(
-
l3
/
l4
))
#2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
#2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
n0
,
n1
=
root
=
a
/
l2
+
(
-
b
/
l4
)
n0
,
n1
=
root
=
a
/
l2
+
(
-
b
/
l4
)
self
.
assertEqualNodes
(
equalize_denominators
(
root
,
(
n0
,
n1
,
4
)),
self
.
assertEqualNodes
(
equalize_denominators
(
root
,
(
l2
*
a
)
/
l4
+
(
-
b
/
l4
))
(
Scope
(
root
),
n0
,
n1
,
4
)),
(
l2
*
a
)
/
l4
+
(
-
b
/
l4
))
def
test_add_nominators
(
self
):
def
test_add_nominators
(
self
):
a
,
b
,
c
=
tree
(
'a,b,c'
)
a
,
b
,
c
=
tree
(
'a,b,c'
)
...
...
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