Sympy 简明教程
SymPy - Derivative
函数的导数是指它相对于其一个变量的瞬时变化率。这相当于找到该函数在某一点的切线斜率。我们能够使用 SymPy 包中的 diff() 函数找到以变量形式表示的数学表达式的微分。
The derivative of a function is its instantaneous rate of change with respect to one of its variables. This is equivalent to finding the slope of the tangent line to the function at a point.we can find the differentiation of mathematical expressions in the form of variables by using diff() function in SymPy package.
diff(expr, variable)
>>> from sympy import diff, sin, exp
>>> from sympy.abc import x,y
>>> expr=x*sin(x*x)+1 >>> expr
上面的代码片段给出的输出等同于以下表达式 −
The above code snippet gives an output equivalent to the below expression −
$x\sin(x^2) + 1$
$x\sin(x^2) + 1$
>>> diff(expr,x)
上面的代码片段给出的输出等同于以下表达式 −
The above code snippet gives an output equivalent to the below expression −
$2x2\cos(x2) + \sin(x^2)$
$2x2\cos(x2) + \sin(x^2)$
>>> diff(exp(x**2),x)
上面的代码片段给出的输出等同于以下表达式 −
The above code snippet gives an output equivalent to the below expression −
2xex2
2xex2
若要获取多个导数,按照需要求导的次数传递变量,或在变量后传递一个数字。
To take multiple derivatives, pass the variable as many times as you wish to differentiate, or pass a number after the variable.
>>> diff(x**4,x,3)
上面的代码片段给出的输出等同于以下表达式 −
The above code snippet gives an output equivalent to the below expression −
$24x$
$24x$
>>> for i in range(1,4): print (diff(x**4,x,i))
上面的代码段给出了以下表达式:
The above code snippet gives the below expression −
4*x 3
4*x*3*
12*x 2
12*x*2*
24*x
24*x
也可以调用表达式的 diff() 方法。它的工作方式与 diff() 函数类似。
It is also possible to call diff() method of an expression. It works similarly as diff() function.
>>> expr=x*sin(x*x)+1
>>> expr.diff(x)
上面的代码片段给出的输出等同于以下表达式 −
The above code snippet gives an output equivalent to the below expression −
$2x2\cos(x2) + \sin(x^2)$
$2x2\cos(x2) + \sin(x^2)$
使用 Derivative 类可以创建一个未求值的导数。它的语法与 diff() 函数相同。若要求一个未求值的导数,请使用 doit 方法。
An unevaluated derivative is created by using the Derivative class. It has the same syntax as diff() function. To evaluate an unevaluated derivative, use the doit method.
>>> from sympy import Derivative
>>> d=Derivative(expr)
>>> d
上面的代码片段给出的输出等同于以下表达式 −
The above code snippet gives an output equivalent to the below expression −
$\frac{d}{dx}(x\sin(x^2)+1)$
$\frac{d}{dx}(x\sin(x^2)+1)$
>>> d.doit()
上面的代码片段给出的输出等同于以下表达式 −
The above code snippet gives an output equivalent to the below expression −
$2x2\cos(x2) + \sin(x^2)$
$2x2\cos(x2) + \sin(x^2)$