Python Sympy: A powerful tool for calculating calculus

The calculations of calculus may not be used in daily life, which makes people feel a bit advanced.
But in fact, in the eyes of mathematics majors, it is just an extremely common calculation, a common calculation method at the same level as addition, subtraction, multiplication and division.

Calculus and limiting operations are not difficult in themselves, but their calculation rules are not as simple as addition, subtraction, multiplication and division.
Their calculation process requires the use of many calculation rules (which are not easy to remember), just like calculating trigonometric functions requires memorizing many formulas and theorems of trigonometric functions.

Using Sympy can effectively reduce the burden in this area, allowing us to solve calculus problems programmatically.

1. Limit calculation

Extreme calculation has a wide range of application scenarios and is also a prerequisite for learning calculus.

1.1. Function limits

For example, this simple function lim?x→∞1x\lim\limits_{x\rarr\infty}\frac{1}{x}x→∞lim?x1?,
We want to know its value when x approaches infinity, and ordinary addition, subtraction, multiplication and division algorithms cannot operate.
Use Sympy to calculate:

from sympy import Symbol, Limit, S

Limit(1/x, x, S.Infinity).doit()
#operation result
0

When x tends to 0,

Limit(1/x, x, 0).doit()
#Running results, the following symbols represent positive infinity
oo

1.2. Instantaneous speed

In physics, when calculating instantaneous speed, limit calculations are also used.

For example, there is a formula for distance and time: S=t2 + 2t + 10S = t^2 + 2t + 10S=t2 + 2t + 10 (S represents distance, t indicates time)
When calculating instantaneous speed, the steps are as follows:

  1. Assume the initial distance SSS
  2. After Δt\Delta tΔt time, the distance becomes ΔS=(t + Δt)2 + 2(t + Δt) + 10\Delta S = (t + \Delta t)^2 + 2(t + \ \Delta t) + 10ΔS=(t + Δt)2 + 2(t + Δt) + 10
  3. The average speed during this time interval is: V=ΔS?SΔtV = \frac{\Delta S – S}{\Delta t}V=ΔtΔS?S?
  4. When Δt\Delta tΔt tends to 0, this speed VVV is the instantaneous speed.

It is very convenient to use Sympy:

# 2. Clockwise speed
t = Symbol("t")
s_t = t * t + 2 * t + 10
delta_t = Symbol("delta_t")

s_delta = s_t.subs({t: t + delta_t})
expr = Limit((s_delta - s_t) / delta_t, delta_t, 0)
expr.doit()

Running result: 2t + 22t + 22t + 2

This is the relationship between instantaneous speed and time. Through this formula, the instantaneous speed at each point in time can be calculated.

3. Differential calculation

Differential calculation can be regarded as an operation method for finding limits. Through the operation rules of differential, finding limits is simpler.

3.1. Derivatives

Still using the instantaneous speed example above, using differential methods can get results faster.

from sympy import Derivative

#Derivative
s = t * t + 2 * t + 10
Derivative(s, t).doit()

The result of the operation: 2t + 22t + 22t + 2, which is the same as the result calculated using the limit method above.

3.2. Partial derivatives

When a function has more than one variable, different variables can be derived separately, which is called partial derivative.
For example, function: f(x,y)=5×2 + 6y2 + 10xy + 2x + 3yf(x,y) = 5x^2 + 6y^2 + 10xy + 2x + 3yf(x,y)=5×2 + 6y2 + 10xy + 2x + 3y

  1. Partial derivative of variable x: df(x,y)dx=10x + 10y + 2\frac{df(x,y)}{dx} = 10x + 10y + 2dxdf(x, y)?=10x + 10y + 2
  2. Partial derivative of variable y: df(x,y)dy=10x + 12y + 3\frac{df(x,y)}{dy} = 10x + 12y + 3dydf(x, y)?=10x + 12y + 3

Sympy implementation:

f_xy = 5 * x * x + 6 * y * y + 10 * x * y + 2 * x + 3 * y

dx = Derivative(f_xy, x).doit()
dy = Derivative(f_xy, y).doit()

Running results: dx=10x + 10y + 2dx = 10x + 10y + 2dx=10x + 10y + 2, dy=10x + 12y + 3dy = 10x + 12y + 3dy=10x + 12y + 3

3.3. Higher order derivatives

The above differential calculations all solve for first-order derivatives. When looking for the global extreme point of the function, higher-order derivatives are also used.

Calculating higher-order derivatives is also simple. The third parameter of the Derivative function above is the order of derivation (the default is 1).

#Higher order derivatives
f = x**5 - 3 * x**3 + 5 * x

#3rd order derivative
dx3 = Derivative(f, x, 3).doit()

#4th order derivative
dx4 = Derivative(f, x, 4).doit()

Running results: dx3=6(10×2?3)dx3 = 6(10x^2-3)dx3=6(10×2?3), dx4=120xdx4 = 120xdx4=120x

4. Points calculation

Integral is the inverse operation of differential. Manual calculation generally requires consulting the integral table, which is very troublesome.

Using Sympy is just one line of code.

from sympy import Integral

expr = 2*x
Integral(expr).doit()

Running result: x2x^2×2

In addition to obtaining the expression after integration, the value of the integral can also be calculated directly.
For example, calculate: ∫2.57.5(x2 + 2)\int_{2.5}{7.5}(x2 + 2)∫2.57.5?(x2 + 2)

expr = x * x + 2
Integral(expr, (x, 2.5, 7.5)).doit()
#Run result: 145.416666666667

5. Summary review

This article mainly introduces the use of Sympy in calculus.

However, being able to calculate calculus is not what attracts me to Sympy.
Its main feature is its ability to symbolize variables and expressions in programs.
In this way, the process of writing a program is very similar to the process of deriving mathematical formulas, and you can express your mathematical knowledge more intuitively.

PS.
When I used Sympy in jupyter notebook, I found that the variables and expressions directly displayed in Sympy were very beautiful, all in Latex format.


———————————END——————- ——–

Digression

In the current era of big data, how can one keep up with the times without mastering a programming language? Python, the hottest programming language at the moment, has a bright future! If you also want to keep up with the times and improve yourself, please take a look.

Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.


CSDN gift package:The most complete “Python learning materials” on the Internet are given away for free! (Safe link, click with confidence)

1. Python learning routes in all directions

The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.

img
img

2. Python essential development tools

The tools have been organized for you, and you can get started directly after installation! img

3. Latest Python study notes

When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.

img

4. Python video collection

Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.

img

5. Practical cases

What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.

img

6. Interview Guide

Resume template

CSDN gift package:The most complete “Python learning materials” on the Internet are given away for free! (Safe link, click with confidence)

If there is any infringement, please contact us for deletion.