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
5d0be51e
Commit
5d0be51e
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added rewrite rules for negated sin/cos parameters.
parent
a37df98b
No related branches found
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
+5
-2
5 additions, 2 deletions
src/rules/__init__.py
src/rules/goniometry.py
+42
-2
42 additions, 2 deletions
src/rules/goniometry.py
tests/test_rules_goniometry.py
+25
-2
25 additions, 2 deletions
tests/test_rules_goniometry.py
with
72 additions
and
6 deletions
src/rules/__init__.py
+
5
−
2
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
],
}
This diff is collapsed.
Click to expand it.
src/rules/goniometry.py
+
42
−
2
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}.
'
)
This diff is collapsed.
Click to expand it.
tests/test_rules_goniometry.py
+
25
−
2
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
))
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