[英]Differentiation of a list of functions with respect to a list of variables
Suppose I have a list of functions 假设我有一个功能列表
f = ['m','n', 'z']
and a list of variables 和变量列表
v = ['x', 'y', 'z']
Now 现在
m = x**2
n = y**2
z = x*y
Two important things here: 这里有两件重要的事情:
Now I want to differentiate all the elements of the function list with respect to all the elements of the variable list and the output will be an multidimensional array. 现在,我要相对于变量列表的所有元素来区分函数列表的所有元素,并且输出将是多维数组。 I am trying this without same variable and function name(here only printing one derivative, but ultimately I need a full array):
我在没有相同的变量和函数名称的情况下尝试了此操作(这里仅打印一个派生变量,但最终我需要一个完整的数组):
from sympy import *
f = ['m', 'n']
v = ['x', 'y']
variable = [var(i) for i in v]
Funct = [Function(i) for i in f]
m = x**2
n = y**2
print(diff(Funct[0], variable[0]))
it is giving this error: 它给出了这个错误:
print(diff(Funct[0], variable[0]))
File "/usr/local/lib/python2.7/site-packages/sympy/core/function.py", line 1837, in diff
return Derivative(f, *symbols, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sympy/core/function.py", line 1136, in __new__
if symbol_set.difference(expr.free_symbols):
TypeError: 'property' object is not iterable
What you're looking for is called the Jacobian, and can be computed with SymPy like this: 您正在寻找的被称为Jacobian,可以用SymPy计算如下:
>>> x, y, z = symbols('x y z')
>>> Matrix([x**2, y**2, x*y]).jacobian([x, y, z])
Matrix([
[2*x, 0, 0],
[ 0, 2*y, 0],
[ y, x, 0]])
One thing to realize is that the strings we use when creating symbols have nothing to do with names of those symbols. 要意识到的一件事是,我们在创建符号时使用的字符串与这些符号的名称无关。 You can have
你可以有
a = Symbol('b')
and now a
is a symbol whose string representation is 'b'. 现在
a
是一个符号,其字符串表示形式为'b'。 In your code, this symbol is referred to as a
, not as b
. 在您的代码中,此符号称为
a
,而不是b
。 In particular, creating Funct = [Function('m'), Function('n')]
and then assigning m = x**2
does nothing for the functions you created: there is no connection between m
and Function('m')
. 特别是,创建
Funct = [Function('m'), Function('n')]
,然后分配m = x**2
对创建的函数没有任何作用: m
和Function('m')
之间没有连接Function('m')
。
Also, SymPy does not really differentiate functions , it differentiates expressions (functions in which something is plugged in). 另外,SymPy并没有真正区分函数 ,而是区分表达式 (插入了某些东西的函数)。 The following creates a double list of derivatives, given expressions and variables:
下面创建给定表达式和变量的导数的双重列表:
variables = symbols("x y z")
expressions = [x**2, y**2, x*y]
print([[diff(e, v) for v in variables] for e in expressions])
Output: 输出:
[[2*x, 0, 0], [0, 2*y, 0], [y, x, 0]]
You can try derive_by_array
: 您可以尝试
derive_by_array
:
In [3]: f = [i(x, y, z) for i in symbols(['m','n', 'z'], cls=Function)]
In [4]: f
Out[4]: [m(x, y, z), n(x, y, z), z(x, y, z)]
In [5]: derive_by_array(f, [x, y, z])
Out[5]:
⎡∂ ∂ ∂ ⎤
⎢──(m(x, y, z)) ──(n(x, y, z)) ──(z(x, y, z))⎥
⎢∂x ∂x ∂x ⎥
⎢ ⎥
⎢∂ ∂ ∂ ⎥
⎢──(m(x, y, z)) ──(n(x, y, z)) ──(z(x, y, z))⎥
⎢∂y ∂y ∂y ⎥
⎢ ⎥
⎢∂ ∂ ∂ ⎥
⎢──(m(x, y, z)) ──(n(x, y, z)) ──(z(x, y, z))⎥
⎣∂z ∂z ∂z ⎦
The next version of SymPy will support an even more compact syntax: SymPy的下一版本将支持更紧凑的语法:
diff(f, [[x, y, z]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.