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
60155fdc
Commit
60155fdc
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Moved division negation to nominator.
parent
dd369030
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/parser.py
+7
-6
7 additions, 6 deletions
src/parser.py
src/rules/fractions.py
+4
-6
4 additions, 6 deletions
src/rules/fractions.py
tests/test_parser.py
+8
-0
8 additions, 0 deletions
tests/test_parser.py
with
19 additions
and
12 deletions
src/parser.py
+
7
−
6
View file @
60155fdc
...
...
@@ -15,7 +15,7 @@ from pybison import BisonParser, BisonSyntaxError
from
graph_drawing.graph
import
generate_graph
from
node
import
ExpressionNode
as
Node
,
ExpressionLeaf
as
Leaf
,
OP_MAP
,
\
TOKEN_MAP
,
TYPE_OPERATOR
,
OP_COMMA
,
OP_NEG
,
OP_MUL
,
Scope
,
PI
TOKEN_MAP
,
TYPE_OPERATOR
,
OP_COMMA
,
OP_NEG
,
OP_MUL
,
OP_DIV
,
Scope
,
PI
from
rules
import
RULES
from
strategy
import
pick_suggestion
from
possibilities
import
filter_duplicates
,
apply_suggestion
...
...
@@ -358,14 +358,15 @@ class Parser(BisonParser):
"""
if
option
==
0
:
# rule: NEG exp
node
=
values
[
1
]
# Add negation to the left-most child
if
values
[
1
]
.
is_leaf
or
values
[
1
]
.
op
!=
OP_MUL
:
values
[
1
]
.
negated
+=
1
if
node
.
is_leaf
or
(
node
.
op
!=
OP_MUL
and
node
.
op
!=
OP_DIV
)
:
node
.
negated
+=
1
else
:
child
=
Scope
(
values
[
1
]
)[
0
]
child
=
Scope
(
node
)[
0
]
child
.
negated
+=
1
return
values
[
1
]
return
node
if
option
==
1
:
# rule: FUNCTION exp
if
values
[
1
].
is_op
(
OP_COMMA
):
...
...
@@ -393,7 +394,7 @@ class Parser(BisonParser):
node
=
values
[
2
]
# Add negation to the left-most child
if
node
.
is_leaf
or
node
.
op
!=
OP_MUL
:
if
node
.
is_leaf
or
(
node
.
op
!=
OP_MUL
and
node
.
op
!=
OP_DIV
)
:
node
.
negated
+=
1
else
:
node
=
Scope
(
node
)[
0
]
...
...
This diff is collapsed.
Click to expand it.
src/rules/fractions.py
+
4
−
6
View file @
60155fdc
...
...
@@ -111,6 +111,7 @@ def match_add_constant_fractions(node):
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
"""
scope
,
denom
=
args
[::
3
]
...
...
@@ -121,14 +122,11 @@ def equalize_denominators(root, args):
if
mult
!=
1
:
if
n
.
is_numeric
():
n
=
L
(
n
.
value
*
mult
)
n
om
=
L
(
n
.
value
*
mult
)
else
:
n
=
L
(
mult
)
*
n
n
om
=
L
(
mult
)
*
n
#n = L(n.value * mult) if n.is_numeric() else L(mult) * n
scope
.
replace
(
fraction
,
negate
(
n
/
L
(
d
.
value
*
mult
),
fraction
.
negated
))
scope
.
replace
(
fraction
,
negate
(
nom
/
L
(
d
.
value
*
mult
),
n
.
negated
))
return
scope
.
as_nary_node
()
...
...
This diff is collapsed.
Click to expand it.
tests/test_parser.py
+
8
−
0
View file @
60155fdc
...
...
@@ -38,6 +38,14 @@ class TestParser(unittest.TestCase):
self
.
assertNotEqual
(
possibilities1
,
possibilities2
)
def
test_moved_negation
(
self
):
a
,
b
=
tree
(
'
a,b
'
)
self
.
assertEqual
(
tree
(
'
-ab
'
),
(
-
a
)
*
b
)
self
.
assertEqual
(
tree
(
'
-(ab)
'
),
(
-
a
)
*
b
)
self
.
assertEqual
(
tree
(
'
-a / b
'
),
(
-
a
)
/
b
)
self
.
assertEqual
(
tree
(
'
-(a / b)
'
),
(
-
a
)
/
b
)
def
test_functions
(
self
):
root
,
x
=
tree
(
'
sin x, x
'
)
...
...
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