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
4e576a94
Commit
4e576a94
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added some unit tests.
parent
34e6d703
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/rules/derivatives.py
+11
-4
11 additions, 4 deletions
src/rules/derivatives.py
tests/test_rules_derivatives.py
+11
-2
11 additions, 2 deletions
tests/test_rules_derivatives.py
with
22 additions
and
6 deletions
src/rules/derivatives.py
+
11
−
4
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.
'
)
This diff is collapsed.
Click to expand it.
tests/test_rules_derivatives.py
+
11
−
2
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
'
)
...
...
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