您如何评估python中的导数? [英] How do you evaluate a derivative in python?

查看:658
本文介绍了您如何评估python中的导数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的初学者.我最近了解了Sympy及其符号操作功能,尤其是差异化.我正在尝试以最简单的方式执行以下操作:

I'm a beginner in python. I've recently learned about Sympy and its symbolic manipulation capabilities, in particular, differentiation. I am trying to do the following in the easiest way possible:

  1. 定义f(x,y)= x ^ 2 + xy ^ 2.
  2. 相对于x区分f.所以f'(x,y)= 2x + xy ^ 2.
  3. 计算导数,例如f'(1,1)= 2 +1 = 3.

我知道如何做1和2.问题是,当我尝试在步骤3中评估导数时,出现一个错误,指出python无法计算导数.这是一个最小的工作示例:

I know how to do 1 and 2. The problem is, when I try to evaluate the derivative in step 3, I get an error that python can't calculate the derivative. Here is a minimal working example:

import sympy as sym
import math


def f(x,y):
    return x**2 + x*y**2


x, y = sym.symbols('x y')

def fprime(x,y):
    return sym.diff(f(x,y),x)

print(fprime(x,y)) #This works.

print(fprime(1,1)) 

我希望最后一行显示3.它什么也不会打印,并说无法计算1的一阶导数".

I expect the last line to print 3. It doesn't print anything, and says "can't calculate the 1st derivative wrt 1".

推荐答案

这个问题的答案非常简单.当然,另一个答案中给出的subs选项可用于评估某个数字的导数,但是如果要绘制导数,则该选项不起作用.有一种解决方法:lambdify,如下所述.

The answer to this question is pretty simple. Sure, the subs option given in the other answer works for evaluating the derivative at a number, but it doesn't work if you want to plot the derivative. There is a way to fix this: lambdify, as explained below.

使用lambdify将所有sympy函数(可以区分但不能求值)转换为numpy对应函数(可以对其进行评估,绘制等,但不能求差).例如,sym.sin(x)将被替换为np.sin(x).这个想法是:使用sympy函数定义函数,根据需要进行区分,然后定义一个新函数,即原始函数的lambdified版本.

Use lambdify to convert all of the sympy functions (which can be differentiated but not evaluated) to their numpy counterparts (which can be evaluated, plotted, etc., but not differentiated). For example, sym.sin(x) will be replaced with np.sin(x). The idea is: define the function using sympy functions, differentiate as needed, and then define a new function which is the lambdified version of the original function.

如下面的代码所示,sym.lambdify接受以下输入:

As in the code below, sym.lambdify takes the following inputs:

sym.lambdify(variable, function(variable), "numpy")

第三个输入"numpy"是用其numpy对应项替换sympy函数的函数.一个例子是:

The third input, "numpy", is what replaces sympy functions with their numpy counterparts. An example is:

def f(x):
    return sym.cos(x)

def fprime(x):
    return sym.diff(f(x),x)

fprimeLambdified = sym.lambdify(x,f(x),"numpy")

然后,函数fprime(x)返回-sym.sin(x),函数fprimeLambdified(x)返回-np.sin(x).现在,我们可以在特定的输入值上调用"/评估fprimeLambdified,而我们不能调用"/评估fprime,因为前者由numpy表达式和后者的sympy表达式组成.换句话说,输入fprimelambdified(math.pi)是有意义的,这将返回输出,而fprime(math.pi)将返回错误.

Then the function fprime(x) returns -sym.sin(x), and the function fprimeLambdified(x) returns -np.sin(x). We can "call"/evaluate fprimeLambdified at specific input values now, whereas we cannot "call"/evaluate fprime, since the former is composed of numpy expressions and the latter sympy expressions. In other words, it makes sense to input fprimelambdified(math.pi), and this will return an output, while fprime(math.pi) will return an error.

下面是在多个变量中使用sym.lambdify的示例.

An example of using sym.lambdify in more than one variable is seen below.

import sympy as sym
import math


def f(x,y):
    return x**2 + x*y**2


x, y = sym.symbols('x y')

def fprime(x,y):
    return sym.diff(f(x,y),x)

print(fprime(x,y)) #This works.

DerivativeOfF = sym.lambdify((x,y),fprime(x,y),"numpy")

print(DerivativeOfF(1,1))

这篇关于您如何评估python中的导数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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