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
e20e1936
Commit
e20e1936
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added some numeric division rules.
parent
1a15deb5
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/rules/numerics.py
+60
-10
60 additions, 10 deletions
src/rules/numerics.py
src/strategy.py
+2
-3
2 additions, 3 deletions
src/strategy.py
with
62 additions
and
13 deletions
src/rules/numerics.py
+
60
−
10
View file @
e20e1936
from
itertools
import
combinations
from
.utils
import
greatest_common_divisor
from
..node
import
ExpressionLeaf
as
Leaf
,
Scope
,
negate
,
OP_ADD
,
OP_DIV
,
\
OP_MUL
,
OP_POW
from
..possibilities
import
Possibility
as
P
,
MESSAGES
...
...
@@ -86,23 +87,39 @@ def match_divide_numerics(node):
Example:
6 / 2 -> 3
3 / 2 -> 3 / 2 # 1.5 would mean a decrease in precision
3 / 2 -> 3 / 2
# 1.5 would mean a decrease in precision
3.0 / 2 -> 1.5
3 / 2.0 -> 1.5
3.0 / 2.0 -> 1.5
3 / 1.0 -> 3 # Exceptional case: division of integer by 1.0 keeps
# integer precision
3 / 1.0 -> 3 # Exceptional case: division of integer by 1.0
# keeps integer precision
2 / 4 -> 1 / 2 # denominator % nominator == 0
4 / 3 -> 1 + 1 / 3 # nominator > denominator
"""
assert
node
.
is_op
(
OP_DIV
)
n
,
d
=
node
divide
=
False
dv
=
d
.
value
nv
,
dv
=
n
.
value
,
d
.
value
if
n
.
is_int
()
and
d
.
is_int
():
# 6 / 2 -> 3
# 3 / 2 -> 3 / 2
divide
=
not
divmod
(
n
.
value
,
dv
)[
1
]
mod
=
nv
%
dv
if
not
mod
:
# 6 / 2 -> 3
# 3 / 2 -> 3 / 2
return
[
P
(
node
,
divide_numerics
,
(
nv
,
dv
))]
gcd
=
greatest_common_divisor
(
nv
,
dv
)
if
1
<
gcd
<=
nv
:
# 2 / 4 -> 1 / 2
return
[
P
(
node
,
reduce_fraction_constants
,
(
gcd
,))]
if
nv
>
dv
:
# 4 / 3 -> 1 + 1 / 3
return
[
P
(
node
,
fraction_to_int_fraction
,
((
nv
-
mod
)
/
dv
,
mod
,
dv
))]
elif
n
.
is_numeric
()
and
d
.
is_numeric
():
if
d
==
1.0
:
# 3 / 1.0 -> 3
...
...
@@ -111,14 +128,14 @@ def match_divide_numerics(node):
# 3.0 / 2 -> 1.5
# 3 / 2.0 -> 1.5
# 3.0 / 2.0 -> 1.5
divide
=
True
return
[
P
(
node
,
divide_numerics
,
(
nv
,
dv
))]
return
[
P
(
node
,
divide_numerics
,
(
n
.
value
,
dv
))]
if
divide
else
[]
return
[]
def
divide_numerics
(
root
,
args
):
"""
Combine two constants to a single constant
in a division
.
Combine two
divided
constants
in
to a single constant.
Examples:
6 / 2 -> 3
...
...
@@ -135,6 +152,39 @@ def divide_numerics(root, args):
MESSAGES
[
divide_numerics
]
=
_
(
'
Divide constant {1} by constant {2}.
'
)
def
reduce_fraction_constants
(
root
,
args
):
"""
Reduce the nominator and denominator of a fraction with a given greatest
common divisor.
Example:
2 / 4 -> 1 / 2
"""
gcd
=
args
[
0
]
a
,
b
=
root
return
Leaf
(
a
.
value
/
gcd
).
negate
(
a
.
negated
)
\
/
Leaf
(
b
.
value
/
gcd
).
negate
(
b
.
negated
)
MESSAGES
[
reduce_fraction_constants
]
=
_
(
'
Simplify fraction {0}.
'
)
def
fraction_to_int_fraction
(
root
,
args
):
"""
Combine two divided integer into an integer with a fraction.
Examples:
4 / 3 -> 1 + 1 / 3
"""
integer
,
nominator
,
denominator
=
map
(
Leaf
,
args
)
return
integer
+
nominator
/
denominator
MESSAGES
[
divide_numerics
]
=
_
(
'
Divide constant {1} by constant {2}.
'
)
def
match_multiply_zero
(
node
):
"""
a * 0 -> 0
...
...
This diff is collapsed.
Click to expand it.
src/strategy.py
+
2
−
3
View file @
e20e1936
from
rules.sort
import
move_constant
from
rules.numerics
import
reduce_fraction_constants
def
pick_suggestion
(
possibilities
):
...
...
@@ -7,12 +8,10 @@ def pick_suggestion(possibilities):
# TODO: pick the best suggestion.
for
suggestion
,
p
in
enumerate
(
possibilities
+
[
None
]):
if
p
and
p
.
handler
not
in
[
move_constant
]:
if
p
and
p
.
handler
not
in
[
move_constant
,
reduce_fraction_constants
]:
break
if
not
p
:
return
possibilities
[
0
]
return
possibilities
[
suggestion
]
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