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
4e576a94
Commit
4e576a94
authored
Mar 13, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added some unit tests.
parent
34e6d703
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
6 deletions
+22
-6
src/rules/derivatives.py
src/rules/derivatives.py
+11
-4
tests/test_rules_derivatives.py
tests/test_rules_derivatives.py
+11
-2
No files found.
src/rules/derivatives.py
View file @
4e576a94
...
...
@@ -108,7 +108,7 @@ MESSAGES[zero_derivative] = _('Constant {0[0]} has derivative 0.')
def
match_const_deriv_multiplication
(
node
):
"""
[f(c * x)]' -> c * [f(x)]'
der(c * f(x), x) -> c * der(f(x), x)
"""
assert
node
.
is_op
(
OP_DERIV
)
...
...
@@ -126,14 +126,13 @@ def match_const_deriv_multiplication(node):
def
const_deriv_multiplication
(
root
,
args
):
"""
[f(c * x)]' -> c * [f(x)]'
der(c * f(x), x) -> c * der(f(x), x)
"""
scope
,
c
=
args
scope
.
remove
(
c
)
x
=
L
(
get_derivation_variable
(
root
))
# FIXME: is the explicit 'x' parameter necessary?
return
c
*
der
(
scope
.
as_nary_node
(),
x
)
...
...
@@ -167,7 +166,7 @@ def match_variable_power(node):
if
exponent
.
is_variable
():
return
[
P
(
node
,
variable_exponent
)]
return
[
P
(
node
,
chain_rule
,
(
roo
t
,
variable_exponent
,
()))]
return
[
P
(
node
,
chain_rule
,
(
exponen
t
,
variable_exponent
,
()))]
return
[]
...
...
@@ -181,6 +180,10 @@ def variable_root(root, args):
return
n
*
x
**
(
n
-
1
)
MESSAGES
[
variable_root
]
=
\
_
(
'Apply standard derivative d/dx x ^ n = n * x ^ (n - 1) on {0}.'
)
def
variable_exponent
(
root
,
args
):
"""
der(g ^ x, x) -> g ^ x * ln(g)
...
...
@@ -192,3 +195,7 @@ def variable_exponent(root, args):
g
,
x
=
root
[
0
]
return
g
**
x
*
ln
(
g
)
MESSAGES
[
variable_exponent
]
=
\
_
(
'Apply standard derivative d/dx g ^ x = g ^ x * ln g.'
)
tests/test_rules_derivatives.py
View file @
4e576a94
from
src.rules.derivatives
import
der
,
get_derivation_variable
,
\
match_zero_derivative
,
match_one_derivative
,
one_derivative
,
\
zero_derivative
,
match_variable_power
,
variable_root
,
\
match_const_deriv_multiplication
,
const_deriv_multiplication
,
\
chain_rule
variable_exponent
,
match_const_deriv_multiplication
,
\
const_deriv_multiplication
,
chain_rule
from
src.rules.logarithmic
import
ln
from
src.node
import
Scope
from
src.possibilities
import
Possibility
as
P
from
tests.rulestestcase
import
RulesTestCase
,
tree
...
...
@@ -70,11 +71,19 @@ class TestRulesDerivatives(RulesTestCase):
self
.
assertEqualPos
(
match_variable_power
(
root
),
[
P
(
root
,
variable_root
)])
root
=
tree
(
'der(2 ^ x)'
)
self
.
assertEqualPos
(
match_variable_power
(
root
),
[
P
(
root
,
variable_exponent
)])
def
test_match_variable_power_chain_rule
(
self
):
root
,
x
,
l2
,
x3
=
tree
(
'der((x ^ 3) ^ 2), x, 2, x ^ 3'
)
self
.
assertEqualPos
(
match_variable_power
(
root
),
[
P
(
root
,
chain_rule
,
(
x3
,
variable_root
,
()))])
root
=
tree
(
'der(2 ^ x ^ 3)'
)
self
.
assertEqualPos
(
match_variable_power
(
root
),
[
P
(
root
,
chain_rule
,
(
x3
,
variable_exponent
,
()))])
# Below is not mathematically underivable, it's just not within the
# scope of our program
root
,
x
=
tree
(
'der(x ^ x), x'
)
...
...
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