This section covers how to do basic calculus tasks such as derivatives,
integrals, limits, and series expansions in SymPy. If you are not familiar
with the math of any part of this section, you may safely skip it.
>>> from sympy import * >>> x, y, z = symbols('x y z') >>> init_printing(use_unicode=True)
Derivatives#
To take derivatives, use the diff
function.
>>> diff(cos(x), x) -sin(x) >>> diff(exp(x**2), x) ⎛ 2⎞ ⎝x ⎠ 2⋅x⋅ℯ
diff
can take multiple derivatives at once. To take multiple derivatives,
pass the variable as many times as you wish to differentiate, or pass a number
after the variable. For example, both of the following find the third
derivative of (x^4).
>>> diff(x**4, x, x, x) 24⋅x >>> diff(x**4, x, 3) 24⋅x
You can also take derivatives with respect to many variables at once. Just
pass each derivative in order, using the same syntax as for single variable
derivatives. For example, each of the following will compute
(frac{partial^7}{partial xpartial y^2partial z^4} e^{x y z}).
>>> expr = exp(x*y*z) >>> diff(expr, x, y, y, z, z, z, z) 3 2 ⎛ 3 3 3 2 2 2 ⎞ x⋅y⋅z x ⋅y ⋅⎝x ⋅y ⋅z + 14⋅x ⋅y ⋅z + 52⋅x⋅y⋅z + 48⎠⋅ℯ >>> diff(expr, x, y, 2, z, 4) 3 2 ⎛ 3 3 3 2 2 2 ⎞ x⋅y⋅z x ⋅y ⋅⎝x ⋅y ⋅z + 14⋅x ⋅y ⋅z + 52⋅x⋅y⋅z + 48⎠⋅ℯ >>> diff(expr, x, y, y, z, 4) 3 2 ⎛ 3 3 3 2 2 2 ⎞ x⋅y⋅z x ⋅y ⋅⎝x ⋅y ⋅z + 14⋅x ⋅y ⋅z + 52⋅x⋅y⋅z + 48⎠⋅ℯ
diff
can also be called as a method. The two ways of calling diff
are
exactly the same, and are provided only for convenience.
>>> expr.diff(x, y, y, z, 4) 3 2 ⎛ 3 3 3 2 2 2 ⎞ x⋅y⋅z x ⋅y ⋅⎝x ⋅y ⋅z + 14⋅x ⋅y ⋅z + 52⋅x⋅y⋅z + 48⎠⋅ℯ
To create an unevaluated derivative, use the Derivative
class. It has the
same syntax as diff
.
>>> deriv = Derivative(expr, x, y, y, z, 4) >>> deriv 7 ∂ ⎛ x⋅y⋅z⎞ ──────────⎝ℯ ⎠ 4 2 ∂z ∂y ∂x
To evaluate an unevaluated derivative, use the doit
method.
>>> deriv.doit() 3 2 ⎛ 3 3 3 2 2 2 ⎞ x⋅y⋅z x ⋅y ⋅⎝x ⋅y ⋅z + 14⋅x ⋅y ⋅z + 52⋅x⋅y⋅z + 48⎠⋅ℯ
These unevaluated objects are useful for delaying the evaluation of the
derivative, or for printing purposes. They are also used when SymPy does not
know how to compute the derivative of an expression (for example, if it
contains an undefined function, which are described in the Solving
Differential Equations section).
Derivatives of unspecified order can be created using tuple (x, n)
where
n
is the order of the derivative with respect to x
.
>>> m, n, a, b = symbols('m n a b') >>> expr = (a*x + b)**m >>> expr.diff((x, n)) n ∂ ⎛ m⎞ ───⎝(a⋅x + b) ⎠ n ∂x
Integrals#
To compute an integral, use the integrate
function. There are two kinds
of integrals, definite and indefinite. To compute an indefinite integral,
that is, an antiderivative, or primitive, just pass the variable after the
expression.
>>> integrate(cos(x), x) sin(x)
Note that SymPy does not include the constant of integration. If you want it,
you can add one yourself, or rephrase your problem as a differential equation
and use dsolve
to solve it, which does add the constant (see Solving Differential Equations).
To compute a definite integral, pass the argument (integration_variable,
. For example, to compute
lower_limit, upper_limit)
[int_0^infty e^{-x},dx,]
we would do
>>> integrate(exp(-x), (x, 0, oo)) 1
As with indefinite integrals, you can pass multiple limit tuples to perform a
multiple integral. For example, to compute
[int_{-infty}^{infty}int_{-infty}^{infty} e^{- x^{2} – y^{2}}, dx, dy,]
do
>>> integrate(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo)) π
If integrate
is unable to compute an integral, it returns an unevaluated
Integral
object.
>>> expr = integrate(x**x, x) >>> print(expr) Integral(x**x, x) >>> expr ⌠ ⎮ x ⎮ x dx ⌡
As with Derivative
, you can create an unevaluated integral using
Integral
. To later evaluate this integral, call doit
.
>>> expr = Integral(log(x)**2, x) >>> expr ⌠ ⎮ 2 ⎮ log (x) dx ⌡ >>> expr.doit() 2 x⋅log (x) - 2⋅x⋅log(x) + 2⋅x
integrate
uses powerful algorithms that are always improving to compute
both definite and indefinite integrals, including heuristic pattern matching
type algorithms, a partial implementation of the Risch algorithm, and an algorithm using
Meijer G-functions that is
useful for computing integrals in terms of special functions, especially
definite integrals. Here is a sampling of some of the power of integrate
.
>>> integ = Integral((x**4 + x**2*exp(x) - x**2 - 2*x*exp(x) - 2*x - ... exp(x))*exp(x)/((x - 1)**2*(x + 1)**2*(exp(x) + 1)), x) >>> integ ⌠ ⎮ ⎛ 4 2 x 2 x x⎞ x ⎮ ⎝x + x ⋅ℯ - x - 2⋅x⋅ℯ - 2⋅x - ℯ ⎠⋅ℯ ⎮ ──────────────────────────────────────── dx ⎮ 2 2 ⎛ x ⎞ ⎮ (x - 1) ⋅(x + 1) ⋅⎝ℯ + 1⎠ ⌡ >>> integ.doit() x ⎛ x ⎞ ℯ log⎝ℯ + 1⎠ + ────── 2 x - 1
>>> integ = Integral(sin(x**2), x) >>> integ ⌠ ⎮ ⎛ 2⎞ ⎮ sin⎝x ⎠ dx ⌡ >>> integ.doit() ⎛√2⋅x⎞ 3⋅√2⋅√π⋅S⎜────⎟⋅Γ(3/4) ⎝ √π ⎠ ────────────────────── 8⋅Γ(7/4)
>>> integ = Integral(x**y*exp(-x), (x, 0, oo)) >>> integ ∞ ⌠ ⎮ y -x ⎮ x ⋅ℯ dx ⌡ 0 >>> integ.doit() ⎧ Γ(y + 1) for re(y) > -1 ⎪ ⎪∞ ⎪⌠ ⎨⎮ y -x ⎪⎮ x ⋅ℯ dx otherwise ⎪⌡ ⎪0 ⎩
This last example returned a Piecewise
expression because the integral
does not converge unless (Re(y) > 1.)
Limits#
SymPy can compute symbolic limits with the limit
function. The syntax to compute
is limit(f(x), x, x0)
.
>>> limit(sin(x)/x, x, 0) 1
limit
should be used instead of subs
whenever the point of evaluation
is a singularity. Even though SymPy has objects to represent (infty), using
them for evaluation is not reliable because they do not keep track of things
like rate of growth. Also, things like (infty – infty) and
(frac{infty}{infty}) return (mathrm{nan}) (not-a-number). For example
>>> expr = x**2/exp(x) >>> expr.subs(x, oo) nan >>> limit(expr, x, oo) 0
Like Derivative
and Integral
, limit
has an unevaluated
counterpart, Limit
. To evaluate it, use doit
.
>>> expr = Limit((cos(x) - 1)/x, x, 0) >>> expr ⎛cos(x) - 1⎞ lim ⎜──────────⎟ x─→0⁺⎝ x ⎠ >>> expr.doit() 0
To evaluate a limit at one side only, pass '+'
or '-'
as a fourth
argument to limit
. For example, to compute
[lim_{xto 0^+}frac{1}{x},]
do
>>> limit(1/x, x, 0, '+') ∞
As opposed to
>>> limit(1/x, x, 0, '-') -∞
Series Expansion#
SymPy can compute asymptotic series expansions of functions around a point. To
compute the expansion of (f(x)) around the point (x = x_0) terms of order
(x^n), use f(x).series(x, x0, n)
. x0
and n
can be omitted, in
which case the defaults x0=0
and n=6
will be used.
>>> expr = exp(sin(x)) >>> expr.series(x, 0, 4) 2 x ⎛ 4⎞ 1 + x + ── + O⎝x ⎠ 2
The (Oleft(x^4right)) term at the end represents the Landau order term at
(x=0) (not to be confused with big O notation used in computer science, which
generally represents the Landau order term at (x) where (x rightarrow infty)). It means that all
x terms with power greater than or equal to (x^4) are omitted. Order terms
can be created and manipulated outside of series
. They automatically
absorb higher order terms.
>>> x + x**3 + x**6 + O(x**4) 3 ⎛ 4⎞ x + x + O⎝x ⎠ >>> x*O(1) O(x)
If you do not want the order term, use the removeO
method.
>>> expr.series(x, 0, 4).removeO() 2 x ── + x + 1 2
The O
notation supports arbitrary limit points (other than 0):
>>> exp(x - 6).series(x, x0=6) 2 3 4 5 (x - 6) (x - 6) (x - 6) (x - 6) ⎛ 6 ⎞ -5 + ──────── + ──────── + ──────── + ──────── + x + O⎝(x - 6) ; x → 6⎠ 2 6 24 120
Finite differences#
So far we have looked at expressions with analytic derivatives
and primitive functions respectively. But what if we want to have an
expression to estimate a derivative of a curve for which we lack a
closed form representation, or for which we don’t know the functional
values for yet. One approach would be to use a finite difference
approach.
The simplest way the differentiate using finite differences is to use
the differentiate_finite
function:
>>> f, g = symbols('f g', cls=Function) >>> differentiate_finite(f(x)*g(x)) -f(x - 1/2)⋅g(x - 1/2) + f(x + 1/2)⋅g(x + 1/2)
If you already have a Derivative
instance, you can use the
as_finite_difference
method to generate approximations of the
derivative to arbitrary order:
>>> f = Function('f') >>> dfdx = f(x).diff(x) >>> dfdx.as_finite_difference() -f(x - 1/2) + f(x + 1/2)
here the first order derivative was approximated around x using a
minimum number of points (2 for 1st order derivative) evaluated
equidistantly using a step-size of 1. We can use arbitrary steps
(possibly containing symbolic expressions):
>>> f = Function('f') >>> d2fdx2 = f(x).diff(x, 2) >>> h = Symbol('h') >>> d2fdx2.as_finite_difference([-3*h,-h,2*h]) f(-3⋅h) f(-h) 2⋅f(2⋅h) ─────── - ───── + ──────── 2 2 2 5⋅h 3⋅h 15⋅h
If you are just interested in evaluating the weights, you can do so
manually:
>>> finite_diff_weights(2, [-3, -1, 2], 0)[-1][-1] [1/5, -1/3, 2/15]
note that we only need the last element in the last sublist
returned from finite_diff_weights
. The reason for this is that
the function also generates weights for lower derivatives and
using fewer points (see the documentation of finite_diff_weights
for more details).
If using finite_diff_weights
directly looks complicated, and the
as_finite_difference
method of Derivative
instances
is not flexible enough, you can use apply_finite_diff
which
takes order
, x_list
, y_list
and x0
as parameters:
>>> x_list = [-3, 1, 2] >>> y_list = symbols('a b c') >>> apply_finite_diff(1, x_list, y_list, 0) 3⋅a b 2⋅c - ─── - ─ + ─── 20 4 5
In this article, we are going to learn how to calculate and plot the derivative of a function using Matplotlib in Python.
In our higher standard in school, we all have studied derivatives in the mathematics syllabus of calculus and we know that calculus is a fundamental branch that deals with the study of rates of change and slopes of curves. The derivative of a function measures the rate at which a function changes with respect to its independent variable. In this article, we will learn to calculate the derivative of a function and then plot that derivative of a function using the Matplotlib library in Python.
Numerical Differentiation using Python
Here, we are going to determine the derivative of a function using Python. There are different ways to calculate the derivative of a function in Python, and here we are going to use the NumPy library to derive the derivative of a function. NumPy is used to perform numerical computation in Python and provides functions for numerical differentiation also. The formula for the numerical differentiation can be written as:
Python3
import
numpy as np
def
f(x):
return
x
*
*
2
+
x
x
=
np.linspace(
-
5
,
5
,
100
)
h
=
0.01
cal_derivative
=
(f(x
+
h)
-
f(x
-
h))
/
(
2
*
h)
print
(cal_derivative)
Output:
[-9. -8.7979798 -8.5959596 -8.39393939 -8.19191919 -7.98989899 -7.78787879 -7.58585859 -7.38383838 -7.18181818 -6.97979798 -6.77777778 -6.57575758 -6.37373737 -6.17171717 -5.96969697 -5.76767677 -5.56565657 -5.36363636 -5.16161616 -4.95959596 -4.75757576 -4.55555556 -4.35353535 -4.15151515 -3.94949495 -3.74747475 -3.54545455 -3.34343434 -3.14141414 -2.93939394 -2.73737374 -2.53535354 -2.33333333 -2.13131313 -1.92929293 -1.72727273 -1.52525253 -1.32323232 -1.12121212 -0.91919192 -0.71717172 -0.51515152 -0.31313131 -0.11111111 0.09090909 0.29292929 0.49494949 0.6969697 0.8989899 1.1010101 1.3030303 1.50505051 1.70707071 1.90909091 2.11111111 2.31313131 2.51515152 2.71717172 2.91919192 3.12121212 3.32323232 3.52525253 3.72727273 3.92929293 4.13131313 4.33333333 4.53535354 4.73737374 4.93939394 5.14141414 5.34343434 5.54545455 5.74747475 5.94949495 6.15151515 6.35353535 6.55555556 6.75757576 6.95959596 7.16161616 7.36363636 7.56565657 7.76767677 7.96969697 8.17171717 8.37373737 8.57575758 8.77777778 8.97979798 9.18181818 9.38383838 9.58585859 9.78787879 9.98989899 10.19191919 10.39393939 10.5959596 10.7979798 11. ]
Now let’s plot the function and its derivative on the same plot.
Python3
import
matplotlib.pyplot as plt
plt.plot(x, f(x), label
=
'f(x)'
)
plt.plot(x, cal_derivative, label
=
"f'(x)"
)
plt.legend()
plt.show()
Output:
The plot of the function and its derivative on the same graph
Sympy Library to Calculate Derivative
As we have seen earlier the method of numerical differentiation which require a bit of mathematical knowledge and the know-how of the formula to use it. To exclude this complexity there are multiple libraries that already have utility functions that can calculate the derivative of any function if it is derivable.
Let’s see how can we use sympy library in python to calculate the derivative of the same above function.
Syntax: Sympy.diff(function, variable, order)
where,
- function – This can be a multivariate function/expression as well
- variable – It should be that sympy variable with respect to which you would like to differentiate the expression
- order – Whether we want to calculate the first second or third or so on derivative is defined in here.
R
def
f
(x): return x**3 - 2*x**2 + 3*x - 1
import sympy as sy
x =
sy.Symbol
(
'x'
)
def
h
(x): return
sy.diff
(
f
(x), x)
h
(x)
Output:
3x2 - 4x + 3
Partial Derivatives Calculation using Sympy
We can also solve multivariate differentiation using this function as well for example let’s calculate the partial derivatives of the function:
f(x, y) = x3 - 2y2 + 3xy - 1
Python3
import
sympy as sy
x
=
sy.Symbol(
'x'
)
y
=
sy.Symbol(
'y'
)
def
f(x, y):
return
x
*
*
3
-
2
*
y
*
*
2
+
3
*
y
*
x
-
1
def
g(x, y):
return
sy.diff(f(x, y), x)
def
h(x, y):
return
sy.diff(f(x, y), y)
print
(
"Partial Derivative with respect to x: "
, g(x, y))
print
(
"Partial Derivative with respect to y: "
, h(x, y))
Output:
Partial Derivative with respect to x: 3*x**2 + 3*y Partial Derivative with respect to y: 3*x - 4*y
Scipy Library to Calculate Derivative and Plotting
Scipy library also provides utility functions to perform differentiation of functions. This function is available in scipy.misc. Let’s look at an example of the same on the function f(x) = x3 – 2x2 + 3x – 1.
Python3
from
scipy.misc
import
derivative
def
f(x):
return
x
*
*
3
-
2
*
x
*
*
2
+
3
*
x
-
1
derivative(f,
1
, dx
=
0.0001
)
Output:
2
Now let’s plot the function and its derivative on the same plot.
Python3
xs
=
np.linspace(
-
5
,
5
,
100
)
plt.plot(xs, f(xs))
plt.plot(xs, derivative(f, xs, dx
=
0.001
))
plt.legend([
"f(x)"
,
"f'(x)"
])
plt.show()
Output:
Graph of the function and its derivative on the same plot
Last Updated :
24 Apr, 2023
Like Article
Save Article
Как на Python найти производную функции? В какой библиотеке есть необходимые для этого функции?
задан 20 фев 2017 в 5:59
Алексей ВороновАлексей Воронов
5413 золотых знака8 серебряных знаков24 бронзовых знака
1
2 ответа
SymPy (от слов Symbolic math и Python):
>>> from sympy import diff, symbols, cos, sin
>>> x, y = symbols('x y')
>>> diff(cos(x))
-sin(x)
>>> diff(cos(x) + 1j*sin(y), x)
-sin(x)
>>> diff(cos(x) + 1j*sin(y), y)
1.0*I*cos(y)
ответ дан 21 фев 2017 в 8:56
jfsjfs
51.8k11 золотых знаков107 серебряных знаков306 бронзовых знаков
- HowTo
- Python How-To’s
- Вычислить производную в Python
Muhammad Maisam Abbas
11 Июль 2021
В этом руководстве представлены методы вычисления производной функции в Python.
Производная с библиотекой SymPy на Python
Библиотека SymPy известна как Python Symbolic library
. Его можно использовать для выполнения сложных математических операций, таких как производные над функциями в Python. Функция diff()
внутри библиотеки SymPy может использоваться для вычисления производной функции. Мы можем указать переменную, с которой мы хотим вычислить производную, с помощью функции Symbol()
в Python. См. Следующий пример кода.
from sympy import *
import numpy as np
x = Symbol('x')
y = x**2 + 1
yprime = y.diff(x)
print(yprime)
Выход:
В приведенном выше коде мы вычислили производную функции x^2 + 1
с помощью функции diff()
библиотеки SymPy в Python. Мы задали символом x
с помощью функции Symbol()
и вычислили производную по символу x
.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
Сопутствующая статья – Python Math
- Вычислить кумулятивную функцию распределения в Python
- Вычислить обратную касательную в Python
- Вычислить среднее арифметическое в Python
- Косинусное сходство в Python
- Мнимые числа в Python
report this ad
How to calculate derivatives in Python? In this article, we’ll use the Python SymPy library to play around with derivatives.
What are derivatives?
Derivatives are the fundamental tools of Calculus. It is very useful for optimizing a loss function with gradient descent in Machine Learning is possible only because of derivatives.
Suppose we have a function y = f(x) which is dependent on x then the derivation of this function means the rate at which the value y of the function changes with the change in x.
This is by no means an article about the fundamentals of derivatives, it can’t be. Calculus is a different beast that requires special attention. I presume you have some background in calculus. This article is intended to demonstrate how we can differentiate a function using the Sympy library.
Python SymPy library is created for symbolic mathematics. The SymPy project aims to become a full-featured computer algebra system (CAS) while keeping the code simple to understand. Let’s see how to calculate derivatives in Python using SymPy.
1. Install SymPy using PIP
SymPy has more uses than just calculating derivatives but as of now, we’ll focus on derivatives. Let’s use PIP to install SymPy module.
2. Solving a differential with SymPy diff()
For differentiation, SymPy provides us with the diff
method to output the derivative of the function.
- Suppose we have a function: f(x) = x²
- Derivative of the function w.r.t x : f'(x) = 2x
Let’s see how can we achieve this using SymPy diff() function.
#Importing sympy from sympy import * # create a "symbol" called x x = Symbol('x') #Define function f = x**2 #Calculating Derivative derivative_f = f.diff(x) derivative_f
Declaring a symbol is similar to saying that our function has a variable ‘x’ or simply the function depends on x.
3. Solving Derivatives in Python
SymPy has lambdify
function to calculate the derivative of the function that accepts symbol and the function as argument. Let’s look at example of calculating derivative using SymPy lambdify function.
from sympy import * # create a "symbol" called x x = Symbol('x') #Define function f = x**2 f1 = lambdify(x, f) #passing x=2 to the function f1(2)
OUTPUT : 4
Basic Derivative Rules in Python SymPy
There are certain rules we can use to calculate the derivative of differentiable functions.
Some of the most encountered rules are:
- Power Rule
- Product Rule
- Chain Rule
- Quotient Rule
Let’s dive into how can we actually use sympy to calculate derivatives as implied by the general differentiation rules.
1. Power Rule
In general : f'(xn) = nx(n-1)
Example, Function we have : f(x) = x⁵
It’s derivative will be : f'(x) = 5x(5-1) = 5x4
import sympy as sym #Power rule x = sym.Symbol('x') f = x**5 derivative_f = f.diff(x) derivative_f
2. Product Rule
Let u(x) and v(x) be differentiable functions. Then the product of the functions u(x)v(x) is also differentiable.
Example: f(x) = exp(x)*cos(x)
import sympy as sym #Product Rule x = sym.Symbol('x') f = sym.exp(x)*sym.cos(x) derivative_f = f.diff(x) derivative_f
3. Chain Rule
The chain rule calculate the derivative of a composition of functions.
- Say, we have a function h(x) = f( g(x) )
- Then according to chain rule: h′(x) = f ′(g(x)) g′(x)
- Example: f(x) = cos(x**2)
This process can be extended for quotient rule also. It must be obvious by now that only the function is changing while the application process remains the same, the remaining is taken care of by the library itself.
import sympy as sym #Chain Rule x = sym.Symbol('x') f = sym.cos(x**2) derivative_f = f.diff(x) derivative_f
Python Partial Derivative using SymPy
The examples we saw above just had one variable. But we are more likely to encounter functions having more than one variables. Such derivatives are generally referred to as partial derivative.
A partial derivative of a multivariable function is a derivative with respect to one variable with all other variables held constant.
Example: f(x,y) = x4 + x * y4
Let’s partially differentiate the above derivatives in Python w.r.t x.
import sympy as sym #Derivatives of multivariable function x , y = sym.symbols('x y') f = x**4+x*y**4 #Differentiating partially w.r.t x derivative_f = f.diff(x) derivative_f
We use symbols
method when the number of variables is more than 1. Now, differentiate the derivatives in Python partially w.r.t y
import sympy as sym #Derivatives of multivariable function x , y = sym.symbols('x y') f = x**4+x*y**4 #Differentiating partially w.r.t y derivative_f = f.diff(y) derivative_f
The code is exactly similar but now y is passed as input argument in diff
method.
We can choose to partially differentiate function first w.r.t x and then y.
import sympy as sym #Derivatives of multivariable function x , y = sym.symbols('x y') f = x**4+x*y**4 #Differentiating partially w.r.t x and y derivative_f = f.diff(x,y) derivative_f
Summary
This article by no means was a course about derivatives or how can we solve derivatives in Python but an article about how can we leverage python SymPy packages to perform differentiation on functions. Derivatives are awesome and you should definitely get the idea behind it as they play a crucial role in Machine learning and beyond.
What’s Next?
- Calculus in Python
- NumPy Linear Algebra
- Python Linear Regression
- 3D Plots in Python
- Python Pandas Tutorial
Resources
- SymPy Official Page
- Derivatives Wikipedia Page