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
1724ba2f
Commit
1724ba2f
authored
13 years ago
by
Sander Mathijs van Veen
Browse files
Options
Downloads
Patches
Plain Diff
Added raise_numerics rule and added leiden oefenopgaven 1 and 2 as test cases.
parent
a7505db7
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/rules/__init__.py
+4
-2
4 additions, 2 deletions
src/rules/__init__.py
src/rules/numerics.py
+31
-1
31 additions, 1 deletion
src/rules/numerics.py
tests/test_leiden_oefenopgave_v12.py
+44
-0
44 additions, 0 deletions
tests/test_leiden_oefenopgave_v12.py
with
79 additions
and
3 deletions
src/rules/__init__.py
+
4
−
2
View file @
1724ba2f
...
...
@@ -7,7 +7,8 @@ from .powers import match_add_exponents, match_subtract_exponents, \
match_remove_negative_exponent
,
match_exponent_to_root
,
\
match_extend_exponent
,
match_constant_exponent
from
.numerics
import
match_add_numerics
,
match_divide_numerics
,
\
match_multiply_numerics
,
match_multiply_zero
,
match_multiply_one
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
from
.negation
import
match_negated_factor
,
match_negate_polynome
,
\
...
...
@@ -27,7 +28,8 @@ RULES = {
match_constant_division
,
match_negated_division
],
OP_POW
:
[
match_multiply_exponents
,
match_duplicate_exponent
,
match_remove_negative_exponent
,
match_exponent_to_root
,
match_extend_exponent
,
match_constant_exponent
],
match_extend_exponent
,
match_constant_exponent
,
match_raise_numerics
],
OP_NEG
:
[
match_negate_polynome
],
OP_SIN
:
[
match_negated_parameter
,
match_half_pi_subtraction
,
match_standard_radian
],
...
...
This diff is collapsed.
Click to expand it.
src/rules/numerics.py
+
31
−
1
View file @
1724ba2f
from
itertools
import
combinations
from
..node
import
ExpressionLeaf
as
Leaf
,
Scope
,
negate
,
OP_ADD
,
OP_DIV
,
\
OP_MUL
OP_MUL
,
OP_POW
from
..possibilities
import
Possibility
as
P
,
MESSAGES
from
..translate
import
_
...
...
@@ -243,3 +243,33 @@ def multiply_numerics(root, args):
MESSAGES
[
multiply_numerics
]
=
_
(
'
Multiply constant {2} with {3}.
'
)
def
match_raise_numerics
(
node
):
"""
2 ^ 3 -> 8
(-2) ^ 3 -> -8
(-2) ^ 2 -> 4
"""
assert
node
.
is_op
(
OP_POW
)
r
,
e
=
node
if
r
.
is_numeric
()
and
e
.
is_numeric
()
and
not
e
.
negated
:
return
[
P
(
node
,
raise_numerics
,
(
r
,
e
))]
return
[]
def
raise_numerics
(
root
,
args
):
"""
2 ^ 3 -> 8
(-2) ^ 3 -> -8
(-2) ^ 2 -> 4
"""
r
,
e
=
args
return
Leaf
(
r
.
value
**
e
.
value
).
negate
(
r
.
negated
*
e
.
value
)
MESSAGES
[
raise_numerics
]
=
_
(
'
Raise constant {1} with {2}.
'
)
This diff is collapsed.
Click to expand it.
tests/test_leiden_oefenopgave_v12.py
+
44
−
0
View file @
1724ba2f
...
...
@@ -50,3 +50,47 @@ class TestLeidenOefenopgaveV12(TestCase):
'
-72x ^ 3 + 96x ^ 2 + 32 * -x
'
,
'
-72x ^ 3 + 96x ^ 2 - 32x
'
,
])
def
test_2_a
(
self
):
self
.
assertRewrite
([
'
(a2b^-1)^3(ab2)
'
,
'
(a ^ 2 * (1 / b ^ 1)) ^ 3 * ab ^ 2
'
,
'
(a ^ 2 * (1 / b)) ^ 3 * ab ^ 2
'
,
'
(a ^ 2) ^ 3 * (1 / b) ^ 3 * ab ^ 2
'
,
'
a ^ (2 * 3)(1 / b) ^ 3 * ab ^ 2
'
,
'
a ^ 6 * (1 / b) ^ 3 * ab ^ 2
'
,
'
a ^ (6 + 1)(1 / b) ^ 3 * b ^ 2
'
,
'
a ^ 7 * (1 / b) ^ 3 * b ^ 2
'
,
])
def
test_2_b
(
self
):
self
.
assertRewrite
([
'
a3b2a3
'
,
'
a ^ (3 + 3)b ^ 2
'
,
'
a ^ 6 * b ^ 2
'
])
def
test_2_c
(
self
):
self
.
assertRewrite
([
'
a5+a3
'
,
'
a ^ 5 + a ^ 3
'
])
def
test_2_d
(
self
):
self
.
assertRewrite
([
'
a2+a2
'
,
'
(1 + 1)a ^ 2
'
,
'
2a ^ 2
'
])
def
test_2_e
(
self
):
self
.
assertRewrite
([
'
4b^-2
'
,
'
4(1 / b ^ 2)
'
,
# FIXME: '4 * 1/b ^ 2',
])
def
test_2_f
(
self
):
self
.
assertRewrite
([
'
(4b) ^ -2
'
,
'
4 ^ -2 * b ^ -2
'
,
'
1 / 4 ^ 2 * b ^ -2
'
,
'
1 / 16 * b ^ -2
'
,
'
1 / 16 * (1 / b ^ 2)
'
,
'
1 / 16 * (1 / b ^ 2)
'
,
'
1 * 1 / (16b ^ 2)
'
,
'
1 / (16b ^ 2)
'
,
])
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