Home  >  Article  >  Backend Development  >  Four lines of code to solve calculus in seconds! This Python module is amazing!

Four lines of code to solve calculus in seconds! This Python module is amazing!

PHPz
PHPzforward
2023-04-12 12:16:161368browse

Four lines of code to solve calculus in seconds! This Python module is amazing!

Give a simple example, such as expanding a quadratic equation:

from sympy import *
x = Symbol('x')
y = Symbol('y')
d = ((x+y)**2).expand()
print(d)
# 结果:x**2 + 2*x*y + y**2

You can enter any expression, even if it is to the tenth power, it can easily Expand, very convenient:

from sympy import *
x = Symbol('x')
y = Symbol('y')
d = ((x+y)**10).expand()
print(d)
# 结果:x**10 + 10*x**9*y + 45*x**8*y**2 + 120*x**7*y**3 + 210*x**6*y**4 + 252*x**5*y**5 + 210*x**4*y**6 + 120*x**3*y**7 + 45*x**2*y**8 + 10*x*y**9 + y**10

Let’s talk about the specific usage methods and examples of this module.

1. Preparation

Please choose any of the following methods to enter the command to install dependencies:

1. Windows environment Open Cmd (Start-Run-CMD).

2. MacOS environment Open Terminal (command space and enter Terminal).

3. If you are using the VSCode editor or Pycharm, you can directly use the Terminal at the bottom of the interface.

pip install Sympy

2. Basically use

to simplify expressions (simplify )

sympy supports three simplification methods, namely ordinary simplification, trigonometric simplification, and exponential simplification.

Normal simplificationsimplify():

from sympy import *
x = Symbol('x')
d = simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))
print(d)
# 结果:x - 1

Trigonometric simplification trigsimp():

from sympy import *
x = Symbol('x')
d = trigsimp(sin(x)/cos(x))
print(d)
# 结果:tan(x)

Exponential simplification powsimp():

from sympy import *
x = Symbol('x')
a = Symbol('a')
b = Symbol('b')
d = powsimp(x**a*x**b)
print(d)
# 结果:x**(a + b)

Solve equations solve()

The first parameter is the equation to be solved, and the right side is required to be equal to 0, and the second parameter is the unknown number to be solved.

For example, a linear equation of one variable:

from sympy import *
x = Symbol('x')
d = solve(x * 3 - 6, x)
print(d)
# 结果:[2]

A linear equation of two variables:

from sympy import *
x = Symbol('x')
y = Symbol('y')
d = solve([2 * x - y - 3, 3 * x + y - 7],[x, y])
print(d)
# 结果:{x: 2, y: 1}

Find the limit limit()

dir=' ' means solving the right limit, dir='-' means solving the left limit:

from sympy import *
x = Symbol('x')
d = limit(1/x,x,oo,dir='+')
print(d)
# 结果:0
d = limit(1/x,x,oo,dir='-')
print(d)
# 结果:0

Find the integral integrate( )

Try to solve the indefinite integral first:

from sympy import *
x = Symbol('x')
d = integrate(sin(x),x)
print(d)
# 结果:-cos(x)

Then try the definite integral:

from sympy import *
x = Symbol('x')
d = integrate(sin(x),(x,0,pi/2))
print(d)
# 结果:1

Derivation diff()

Use the diff function to derive the equation:

from sympy import *
x = Symbol('x')
d = diff(x**3,x)
print(d)
# 结果:3*x**2
d = diff(x**3,x,2)
print(d)
# 结果:6*x

Solve the differential equation dsolve( )

With y′=2xy For example:

from sympy import *
x = Symbol('x')
f = Function('f')
d = dsolve(diff(f(x),x) - 2*f(x)*x,f(x))
print(d)
# 结果:Eq(f(x), C1*exp(x**2))

3. Practical practice

Some students asked this question, "Boys, I would like to ask how to write this point in Python, thank you all." :

Four lines of code to solve calculus in seconds! This Python module is amazing!

# Python 实用宝典
from sympy import *
x = Symbol('x')
y = Symbol('y')
d = integrate(x-y, (y, 0, 1))
print(d)
# 结果:x - 1/2

In order to calculate this result, the first parameter of integrate is the formula, and the second parameter is the integration variable and the subscript and superscript of the integration range.

The result obtained after running is x - 1/2, which is consistent with expectations.

If you also need to solve calculus and complex equations, you can try sympy, it is almost perfect.

The above is the detailed content of Four lines of code to solve calculus in seconds! This Python module is amazing!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete