Python中的部分符号导数 [英] Partial symbolic derivative in Python

查看:53
本文介绍了Python中的部分符号导数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要部分推导我的方程并从导数中形成一个矩阵.我的等式是:虽然必须满足此条件:为此,我使用了 sympy 模块及其 diff() 函数.到目前为止,我的代码是:

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. 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)

我的结果:

我的问题

我遇到的问题是产品的偏导数(T_aa_0、T_aa_1 和 T_a*a_2),因为通过使用 diff() 函数,你不能推导函数使用产品(显然),否则您会收到错误:

The problem that I'm having is in the partial derivatives of products (T_aa_0, T_aa_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:

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.

让我重新表述我的问题.是否可以通过使用 python 或在这种情况下使用 sympy 模块以符号方式推导出带有函数的方程?

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?

推荐答案

所以我已经设法自己解决了我的问题.主要问题是如何用另一个函数象征性地推导出一个函数或方程.当我再次慢慢浏览 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.

这篇关于Python中的部分符号导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆