简体   繁体   English

Python中的部分符号派生词

[英]Partial symbolic derivative in Python

I need to partially derivate my equation and form a matrix out of the derivatives. 我需要部分推导出我的方程并从衍生物中形成一个矩阵。 My equation is: 我的等式是: 在此输入图像描述 While this conditions must be met: 虽然必须满足这些条件: 在此输入图像描述 For doing this I've used the sympy module and its diff() function. 为此,我使用了sympy模块及其diff()函数。 My code so far is: 到目前为止我的代码是:

from sympy import*
import numpy as np
init_printing() #delete if you dont have LaTeX installed

logt_r, logt_a, T, T_a, a_0, a_1, a_2, logS, Taa_0, Taa_1, Taa_2  = symbols('logt_r, logt_a, T, T_a, a_0, a_1, a_2, logS, Taa_0, Taa_1, Taa_2')

A = (logt_r - logt_a - (T - T_a) * (a_0 + a_1 * logS + a_2 * logS**2) )**2
parametri = [logt_a, a_0, Taa_0, a_1, Taa_1, a_2, Taa_2]

M = expand(A)
M = M.subs(T_a*a_0, Taa_0)
M = M.subs(T_a*a_1, Taa_1)
M = M.subs(T_a*a_2, Taa_2)

K = zeros(len(parametri), len(parametri))
O = []

def odv(par):
    for j in range(len(par)):
        for i in range(len(par)):
            P = diff(M, par[i])/2
            B = P.coeff(par[j])
            K[i,j] = B
    return K 

odv(parametri)

My result: 我的结果: 在此输入图像描述

My problem 我的问题

The problem that I'm having is in the partial derivatives of products (T_a a_0, T_a a_1 and T_a*a_2), because by using the diff() function, you cannot derivate a function with a product (obviously), else you get an error: 我遇到的问题是产品的偏导数(T_a a_0,T_a a_1和T_a * a_2),因为通过使用diff()函数,你不能用产品(显然)推导出一个函数,否则你得到一个错误:

ValueError: 
Can't calculate 1-th derivative wrt T_a*a_0.

To solve this I substitued this products with coefficients, like: 为了解决这个问题,我将这些产品替换为系数,例如:

M = M.subs(T_a*a_0, Taa_0)
M = M.subs(T_a*a_1, Taa_1)
M = M.subs(T_a*a_2, Taa_2)

But as you can see in the final result, this works only in some cases. 但正如您在最终结果中所看到的,这仅适用于某些情况。 I would like to know if there is a better way of doing this where I wouldn't need to substitude the products and that it would work in all cases. 我想知道是否有更好的方法来做到这一点,我不需要对产品进行替换,并且它可以在所有情况下使用。

ADDITIONAL INFORMATION 附加信息

Let me rephrase my question. 让我重新解释一下我的问题。 Is it possible to symbolically derive an equation with a function by using python or in that matter, to use the sympy module? 是否有可能通过使用python象征性地导出具有函数的方程式,或者就此而言,使用sympy模块?

So I've managed to solve my problem on my own. 所以我已经设法自己解决了我的问题。 The main question was how to symbolically derive a function or equation with another function. 主要问题是如何用另一个函数象征性地推导出一个函数或方程。 As I've gone again slowly over the sympy documentation, I saw a little detail, that I've missed before. 当我慢慢地再次通过交流文档时,我看到了一些细节,我以前错过了。 In order to derive a function with a function you need to change the settings of the function, that will be used to derive. 为了使用函数派生函数,您需要更改函数的设置,这将用于派生。 For example: 例如:

x, y, z = symbols('x, y, z')
A = x*y*z
B = x*y

# This is the detail:
type(B)._diff_wrt = True
diff(A, B)

Or in my case, the code looks like: 或者就我而言,代码如下:

koef = [logt_a, a_0, T_a*a_0, a_1, T_a*a_1, a_2, T_a*a_2]
M = expand(A)
K = zeros(len(koef), len(koef))
def odvod_mat(par):
    for j in range(len(par)):
        for i in range(len(par)):
            type(par[i])._diff_wrt = True
            P = diff(M, par[i])/2
            B = P.coeff(par[j])
            K[i,j] = B

            #Removal of T_a
            K[i,j] = K[i,j].subs(T_a, 0)
    return K  
odvod_mat(koef)

Thanks again to all that were taking their time to read this. 再次感谢所有花时间阅读本文的人。 I hope this helps to anyone, who will have the same problem as I did. 我希望这对任何人都有帮助,他们会遇到和我一样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM