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
1724ba2f
Commit
1724ba2f
authored
Feb 27, 2012
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added raise_numerics rule and added leiden oefenopgaven 1 and 2 as test cases.
parent
a7505db7
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
3 deletions
+79
-3
src/rules/__init__.py
src/rules/__init__.py
+4
-2
src/rules/numerics.py
src/rules/numerics.py
+31
-1
tests/test_leiden_oefenopgave_v12.py
tests/test_leiden_oefenopgave_v12.py
+44
-0
No files found.
src/rules/__init__.py
View file @
1724ba2f
...
@@ -7,7 +7,8 @@ from .powers import match_add_exponents, match_subtract_exponents, \
...
@@ -7,7 +7,8 @@ from .powers import match_add_exponents, match_subtract_exponents, \
match_remove_negative_exponent
,
match_exponent_to_root
,
\
match_remove_negative_exponent
,
match_exponent_to_root
,
\
match_extend_exponent
,
match_constant_exponent
match_extend_exponent
,
match_constant_exponent
from
.numerics
import
match_add_numerics
,
match_divide_numerics
,
\
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
,
\
from
.fractions
import
match_constant_division
,
match_add_constant_fractions
,
\
match_expand_and_add_fractions
match_expand_and_add_fractions
from
.negation
import
match_negated_factor
,
match_negate_polynome
,
\
from
.negation
import
match_negated_factor
,
match_negate_polynome
,
\
...
@@ -27,7 +28,8 @@ RULES = {
...
@@ -27,7 +28,8 @@ RULES = {
match_constant_division
,
match_negated_division
],
match_constant_division
,
match_negated_division
],
OP_POW
:
[
match_multiply_exponents
,
match_duplicate_exponent
,
OP_POW
:
[
match_multiply_exponents
,
match_duplicate_exponent
,
match_remove_negative_exponent
,
match_exponent_to_root
,
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_NEG
:
[
match_negate_polynome
],
OP_SIN
:
[
match_negated_parameter
,
match_half_pi_subtraction
,
OP_SIN
:
[
match_negated_parameter
,
match_half_pi_subtraction
,
match_standard_radian
],
match_standard_radian
],
...
...
src/rules/numerics.py
View file @
1724ba2f
from
itertools
import
combinations
from
itertools
import
combinations
from
..node
import
ExpressionLeaf
as
Leaf
,
Scope
,
negate
,
OP_ADD
,
OP_DIV
,
\
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
..possibilities
import
Possibility
as
P
,
MESSAGES
from
..translate
import
_
from
..translate
import
_
...
@@ -243,3 +243,33 @@ def multiply_numerics(root, args):
...
@@ -243,3 +243,33 @@ def multiply_numerics(root, args):
MESSAGES
[
multiply_numerics
]
=
_
(
'Multiply constant {2} with {3}.'
)
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}.'
)
tests/test_leiden_oefenopgave_v12.py
View file @
1724ba2f
...
@@ -50,3 +50,47 @@ class TestLeidenOefenopgaveV12(TestCase):
...
@@ -50,3 +50,47 @@ class TestLeidenOefenopgaveV12(TestCase):
'-72x ^ 3 + 96x ^ 2 + 32 * -x'
,
'-72x ^ 3 + 96x ^ 2 + 32 * -x'
,
'-72x ^ 3 + 96x ^ 2 - 32x'
,
'-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)'
,
])
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