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
5d0be51e
Commit
5d0be51e
authored
Feb 25, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added rewrite rules for negated sin/cos parameters.
parent
a37df98b
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
6 deletions
+72
-6
src/rules/__init__.py
src/rules/__init__.py
+5
-2
src/rules/goniometry.py
src/rules/goniometry.py
+42
-2
tests/test_rules_goniometry.py
tests/test_rules_goniometry.py
+25
-2
No files found.
src/rules/__init__.py
View file @
5d0be51e
from
..node
import
OP_ADD
,
OP_MUL
,
OP_DIV
,
OP_POW
,
OP_NEG
from
..node
import
OP_ADD
,
OP_MUL
,
OP_DIV
,
OP_POW
,
OP_NEG
,
OP_SIN
,
OP_COS
from
.groups
import
match_combine_groups
from
.factors
import
match_expand
from
.powers
import
match_add_exponents
,
match_subtract_exponents
,
\
...
...
@@ -12,10 +12,11 @@ from .fractions import match_constant_division, match_add_constant_fractions, \
from
.negation
import
match_negated_factor
,
match_negate_polynome
,
\
match_negated_division
from
.sort
import
match_sort_multiplicants
from
.goniometry
import
match_add_quadrants
,
match_negated_parameter
RULES
=
{
OP_ADD
:
[
match_add_numerics
,
match_add_constant_fractions
,
match_combine_groups
],
match_combine_groups
,
match_add_quadrants
],
OP_MUL
:
[
match_multiply_numerics
,
match_expand
,
match_add_exponents
,
match_expand_and_add_fractions
,
match_multiply_zero
,
match_negated_factor
,
match_multiply_one
,
...
...
@@ -26,4 +27,6 @@ RULES = {
match_remove_negative_exponent
,
match_exponent_to_root
,
match_extend_exponent
,
match_constant_exponent
],
OP_NEG
:
[
match_negate_polynome
],
OP_SIN
:
[
match_negated_parameter
],
OP_COS
:
[
match_negated_parameter
],
}
src/rules/goniometry.py
View file @
5d0be51e
...
...
@@ -18,7 +18,7 @@ def tan(*args):
def
match_add_quadrants
(
node
):
"""
sin(
x) ^ 2 + cos(x
) ^ 2 -> 1
sin(
t) ^ 2 + cos(t
) ^ 2 -> 1
"""
assert
node
.
is_op
(
OP_ADD
)
...
...
@@ -36,9 +36,49 @@ def match_add_quadrants(node):
def
add_quadrants
(
root
,
args
):
"""
sin(
x) ^ 2 + cos(x
) ^ 2 -> 1
sin(
t) ^ 2 + cos(t
) ^ 2 -> 1
"""
return
L
(
1
)
MESSAGES
[
add_quadrants
]
=
_
(
'Add the sinus and cosinus quadrants to 1.'
)
def
match_negated_parameter
(
node
):
"""
sin(-t) -> -sin(t)
cos(-t) -> cos(t)
"""
assert
node
.
is_op
(
OP_SIN
)
or
node
.
is_op
(
OP_COS
)
t
=
node
[
0
]
if
t
.
negated
:
if
node
.
op
==
OP_SIN
:
return
[
P
(
node
,
negated_sinus_parameter
,
(
t
,))]
return
[
P
(
node
,
negated_cosinus_parameter
,
(
t
,))]
return
[]
def
negated_sinus_parameter
(
root
,
args
):
"""
sin(-t) -> -sin(t)
"""
return
-
sin
(
+
args
[
0
])
MESSAGES
[
negated_sinus_parameter
]
=
\
_
(
'Bring the negation from the sinus parameter {1} to the outside.'
)
def
negated_cosinus_parameter
(
root
,
args
):
"""
cos(-t) -> cos(t)
"""
return
cos
(
+
args
[
0
])
MESSAGES
[
negated_cosinus_parameter
]
=
\
_
(
'Remove the negation from the cosinus parameter {1}.'
)
tests/test_rules_goniometry.py
View file @
5d0be51e
from
src.rules.goniometry
import
match_add_quadrants
,
add_quadrants
# vim: set fileencoding=utf-8 :
from
src.rules.goniometry
import
match_add_quadrants
,
add_quadrants
,
\
match_negated_parameter
,
negated_sinus_parameter
,
\
negated_cosinus_parameter
,
sin
,
cos
from
src.possibilities
import
Possibility
as
P
from
tests.rulestestcase
import
RulesTestCase
,
tree
...
...
@@ -6,9 +9,29 @@ from tests.rulestestcase import RulesTestCase, tree
class
TestRulesGoniometry
(
RulesTestCase
):
def
test_match_add_quadrants
(
self
):
root
=
tree
(
'sin
x ^ 2 + cos x
^ 2'
)
root
=
tree
(
'sin
t ^ 2 + cos t
^ 2'
)
possibilities
=
match_add_quadrants
(
root
)
self
.
assertEqualPos
(
possibilities
,
[
P
(
root
,
add_quadrants
,
())])
def
test_add_quadrants
(
self
):
self
.
assertEqual
(
add_quadrants
(
None
,
()),
1
)
def
test_match_negated_parameter
(
self
):
s
,
c
=
tree
(
'(sin -t), cos -t'
)
t
=
s
[
0
]
self
.
assertEqualPos
(
match_negated_parameter
(
s
),
\
[
P
(
s
,
negated_sinus_parameter
,
(
t
,))])
self
.
assertEqualPos
(
match_negated_parameter
(
c
),
\
[
P
(
c
,
negated_cosinus_parameter
,
(
t
,))])
def
test_negated_sinus_parameter
(
self
):
s
=
tree
(
'sin -t'
)
t
=
s
[
0
]
self
.
assertEqual
(
negated_sinus_parameter
(
s
,
(
t
,)),
-
sin
(
+
t
))
def
test_negated_cosinus_parameter
(
self
):
c
=
tree
(
'cos -t'
)
t
=
c
[
0
]
self
.
assertEqual
(
negated_cosinus_parameter
(
c
,
(
t
,)),
cos
(
+
t
))
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