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
6b59d5de
Commit
6b59d5de
authored
Mar 21, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Derivative now supports multiple variables without an error.
parent
19b91caf
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
15 deletions
+48
-15
src/rules/derivatives.py
src/rules/derivatives.py
+4
-9
src/rules/utils.py
src/rules/utils.py
+33
-1
tests/test_rules_derivatives.py
tests/test_rules_derivatives.py
+3
-4
tests/test_rules_utils.py
tests/test_rules_utils.py
+8
-1
No files found.
src/rules/derivatives.py
View file @
6b59d5de
from
.utils
import
find_variables
from
.utils
import
find_variables
,
first_sorted_variable
from
.logarithmic
import
ln
from
.goniometry
import
sin
,
cos
from
..node
import
ExpressionNode
as
N
,
ExpressionLeaf
as
L
,
Scope
,
OP_DER
,
\
...
...
@@ -32,16 +32,10 @@ def get_derivation_variable(node, variables=None):
if
not
variables
:
variables
=
find_variables
(
node
)
if
len
(
variables
)
>
1
:
# FIXME: Use first variable, sorted alphabetically?
#return sorted(variables)[0]
raise
ValueError
(
'More than 1 variable in implicit derivative: '
+
', '
.
join
(
variables
))
if
not
len
(
variables
):
return
None
return
list
(
variables
)[
0
]
return
first_sorted_variable
(
variables
)
def
chain_rule
(
root
,
args
):
...
...
@@ -168,7 +162,8 @@ def match_variable_power(node):
return
[
P
(
node
,
variable_root
)]
return
[
P
(
node
,
chain_rule
,
(
root
,
variable_root
,
()))]
elif
not
x
in
rvars
and
x
in
evars
:
if
not
x
in
rvars
and
x
in
evars
:
if
exponent
.
is_variable
():
return
[
P
(
node
,
variable_exponent
)]
...
...
src/rules/utils.py
View file @
6b59d5de
from
..node
import
ExpressionLeaf
as
L
,
OP_MUL
,
OP_DIV
from
..node
import
ExpressionLeaf
as
L
,
OP_MUL
,
OP_DIV
,
INFINITY
def
greatest_common_divisor
(
a
,
b
):
...
...
@@ -84,3 +84,35 @@ def find_variables(node):
return
reduce
(
lambda
a
,
b
:
a
|
b
,
map
(
find_variables
,
node
))
return
set
()
def
first_sorted_variable
(
variables
):
"""
In a set of variables, find the main variable to be used in a derivation or
integral. The prioritized order is x, y, z, a, b, c, d, ...
"""
for
x
in
'xyz'
:
if
x
in
variables
:
return
x
return
sorted
(
variables
)[
0
]
def
infinity
():
return
L
(
INFINITY
)
def
replace_variable
(
f
,
x
,
replacement
):
"""
Replace all occurences of variable x in function f with the specified
replacement.
"""
if
f
==
x
:
return
replacement
.
clone
()
if
f
.
is_leaf
:
return
f
children
=
map
(
lambda
c
:
replace_variable
(
c
,
x
,
replacement
),
f
)
return
N
(
f
,
*
children
)
tests/test_rules_derivatives.py
View file @
6b59d5de
...
...
@@ -16,13 +16,12 @@ from tests.rulestestcase import RulesTestCase, tree
class
TestRulesDerivatives
(
RulesTestCase
):
def
test_get_derivation_variable
(
self
):
xy
,
x
,
l1
=
tree
(
'der(xy, x), der(x), der(1)'
)
self
.
assertEqual
(
get_derivation_variable
(
xy
),
'x'
)
xy0
,
xy1
,
x
,
l1
=
tree
(
'der(xy, x), der(xy), der(x), der(1)'
)
self
.
assertEqual
(
get_derivation_variable
(
xy0
),
'x'
)
self
.
assertEqual
(
get_derivation_variable
(
xy1
),
'x'
)
self
.
assertEqual
(
get_derivation_variable
(
x
),
'x'
)
self
.
assertIsNone
(
get_derivation_variable
(
l1
))
self
.
assertRaises
(
ValueError
,
tree
,
'der(xy)'
)
def
test_match_zero_derivative
(
self
):
root
=
tree
(
'der(x, y)'
)
self
.
assertEqualPos
(
match_zero_derivative
(
root
),
...
...
tests/test_rules_utils.py
View file @
6b59d5de
import
unittest
from
src.rules.utils
import
least_common_multiple
,
is_fraction
,
partition
,
\
find_variables
find_variables
,
first_sorted_variable
from
tests.rulestestcase
import
tree
...
...
@@ -31,3 +31,10 @@ class TestRulesUtils(unittest.TestCase):
self
.
assertSetEqual
(
find_variables
(
add
),
set
([
'x'
]))
self
.
assertSetEqual
(
find_variables
(
mul0
),
set
([
'x'
]))
self
.
assertSetEqual
(
find_variables
(
mul1
),
set
([
'x'
,
'y'
]))
def
test_first_sorted_variable
(
self
):
self
.
assertEqual
(
first_sorted_variable
(
set
(
'ax'
)),
'x'
)
self
.
assertEqual
(
first_sorted_variable
(
set
(
'ay'
)),
'y'
)
self
.
assertEqual
(
first_sorted_variable
(
set
(
'az'
)),
'z'
)
self
.
assertEqual
(
first_sorted_variable
(
set
(
'xz'
)),
'x'
)
self
.
assertEqual
(
first_sorted_variable
(
set
(
'bac'
)),
'a'
)
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