Symbol calculation Library-sympy in the Python Circle (reproduced)

Source: Internet
Author: User
Tags cos diff log log sin

"This article from the public number" Deng takes you to play Python ", reproduced"

Import mathmath.sqrt (8)2.8284271247461903

Let's look at the results in Python

MATH.SQRT (8). MATH.SQRT (8)8.000000000000002

Ben thought it would get 8.0, but didn't expect to get 8.000000000000002.

First, why is this?

If our usual tasks often have expressions similar to those in the example above, then directly using Python to calculate the result is just an approximation of the real value. If this is a lot of calculation, the error will accumulate gradually, which we can not tolerate, so this time we need python to handle this mathematical symbol calculation.

Second, what is mathematical symbolic calculation?

Mathematical symbolic calculation can deal with symbolic calculation of characterizing numbers. This means that the mathematical object is represented precisely , not approximately, and the mathematical expression with the non-computed variable is left in the symbolic form.

SymPy Library Introduction

SymPy is a mathematical notation library for Python. It is intended to be a characteristic computer algebra system. It ensures that its code is as simple as possible, easy to understand and extensible. SymPy is written entirely by Python and does not require additional libraries.

The expression of sympy is slightly different from the mathematical expression of our usual handwriting, the following is the SymPy equation notation

    • Plus +
    • Minus sign
    • Division Sign
    • Multiplication sign
    • Equals Eq ()
    • Index * *
    • Log log ()
    • Exponential power exp () of E

The example above is implemented in Python.

Import sympysympy.sqrt (8)2*sqrt (2)

Calculate with SymPy

SYMPY.SQRT (8) *sympy.sqrt (8)8

Third, simply Learn SymPy a few examples in

    • Define mathematical symbols (similar to variables in mathematics)
    • Expand and collapse
    • Simplifying an expression
    • Solution equation
    • Assignment calculation
    • Log calculation
    • Derivative
    • Integral
    • Find the Limit

3.1 Defining mathematical Symbols

Let's define a symbolic expression that represents the mathematical expression x+2yx+2y. First we have to notice that the variables in Python must be assigned in order to be used, so the mathematical expression cannot be expressed. So be sure to introduce special symbols here, there are two ways

    • Method One
 from Import  = symbols ('x y'= x + ++ 2*y
    • Method Two
 from Import  = x + ++ 2*y

* * When a variable in a mathematical expression is not a single character, such as X, Y, but a variable of multiple character length of result, only method one is used.

3.2 Expand and collapse

 from Import Expand,factor  from Import  = x**2+x*y+3*xexprx**2 + x*y + 3*x
    • Folded
factor (expr) x**2 + x*y + 3*x
    • Expand
EXPR2 = x* (x+y+3) expand (EXPR2) x**2 + x*y + 3*x

3.3 simplifying An expression

Sometimes we need to simplify the expression

    • Ordinary simplification
 from Import Simplify  from Import xsimplify ((x**3 + x**2-x-1)/(x**2 + 2*x + 1-1
    • Triangular simplification of trigsimp
 from Import Trigsimp,sin,cos  from Import  = sin (x)/cos (x) trigsimp (y) tan (x)
    • Exponential simplification
 from Import Powsimp  from Import  = x**a * x**byx**a*x**b# exponential simplification powsimp (y) x* * (A + b)

3.4 Solution Equation

Note that in Python = is the meaning of the assignment, = = Although the expression equals, there is a big problem. In SymPy, we use EQ (x, y) to represent x=y

 from Import x, y  from Import Solve,linsolve,eq # to solve an equation, use solvesolve (Eq (2*x-1,3), x) [2]

Using Linsolve ([Equation 1, Equation 2, ...], (variable 1, variable 2, ...))

# for solving multiple equations, use Linsolve. The solution of the equation is x=-1,y=3linsolve ([X+2*y-5,2*x+y-1], (x, Y)) {(-1, 3)}

3.5 Assignment Calculation

 from Import x, y  from Import  = sin (x) ++ cos (x) y.subs (x, x**2) sin (x**2) + cos (x**2)

Here the assignment, not only can realize the substitution of variables, but also can be assigned to the number, the calculation.

y.subs (x, 0)1

3.6 Log arithmetic

 from Import Log,expand_log  from Import x,y,e # Expand_log to expand Log, but need to force=true, expand to occur expand_log (log (x**3), force=True)* * log (x) # Expand_log to expand Log, but force=true must be expanded to occur Expand_log (log (x**3)) log (x**3) expand_log (log ( e**x), force=True) x*log (E)

3.7 derivative

 from Import Diff,sin,cos  from Import x,y,z,f # Derivation of sin (x) diff (Sin (x)) cos (x) diff (cos (x))-sin (x)

Bias Guide

# For bias f = 3*x**2*y*Zdiff (f, x, y)6*x*z

3.8 points

 from Import pi,x  from Import integrate,sinintegrate (sin (x), (X,0,PI))-cos (pi) + 1

3.9 Limit

 from Import x  from Import limitlimit ('+') oo

3.10 expanded Type

In the high number, there are Taylor expansion, Lagrange expansion type.

E^x=1+x+x^2/2!+x^3/3!+x^4/4!+...+x^n/n!+o (X^n)

For example, when N=3,

E^x=1+x+x^2/2+o (x^3)

The method implemented here is: SymPy expression. Series (variable, 0, N)

 from Import  = symbols ('x'=3)1 + x + X**2/2 + O (x**3)

Symbol calculation Library-sympy in the Python Circle (reproduced)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.