sympy-Symbol Arithmetic Good helper

Source: Internet
Author: User
Tags cos mathematical constants pprint

sympy-Symbol Arithmetic Good helper

SymPy is a mathematical notation library for Python, which can be used to derive the symbolic derivation of mathematical formulas. For ease of invocation, all of the following instance programs assume that all content has been imported from the SymPy library in advance:

*
4.1 Classic formulas on the cover

The formula on the cover of this book:

Called the Oura equation, where e is the base of the natural index, I is the imaginary unit, and is the PI. This formula is known as the most wonderful formula for mathematics, which links 5 basic mathematical constants with addition, multiplication, and power operations. Let's verify this formula with SymPy.

In the loaded symbol, E denotes the base of the natural exponent, I represents the imaginary unit, pi is the π, so the above formula can be calculated directly as follows:

E* *(I*pi)+10        

The Oura equation can be calculated with the following formula,

In order to use SymPy to verify the above formula, we need to introduce the variable x. In SymPy, a mathematical symbol is an object of the symbol class, so it must be created before it can be used:

Symbol(' x ')  

Expand function can expand the formula, we use it to expand e** (I*PI) Try:

ExpandE* *(I*x)exp (i*x)       

There is no success, just a change of the wording. The exp here is not math.exp or numpy.exp, but Sympy.exp, which is a class that is used to express the natural exponential function.

The expand function has the keyword parameter complex, and when it is true, expand divides the formula into real and imaginary two parts:

Expand(exp(I*xComplex=True)i*exp (-im (x)) *sin (Re (x)) + cos (re (x)) *exp (-im (x))          

This time the results are quite complex, where sin, cos, re, im are sympy defined classes, re represents the real part, IM represents take the imaginary number part. Obviously the operation here treats the symbol x as a plural. In order to specify that the symbol x must be a real number, we need to redefine the symbol x as follows:

Symbol("x"real=True)expand(exp(I*xComplex =True)i*sin (x) + cos (x)             

Finally we get the formula we need. So how to prove it. We can expand with Taylor polynomial:

Series(exp(I*xx0)pprint(tmp) 2 3 4 5 6 7 8 9 x i*x x i*x x i*x x i*x1 + i*x--------+--+-------------+-----+------+ O (x**10) 2 6 24 120 720 5040 40320 362880                

Series is the Taylor expansion function, pprint the formula in a better-looking format. The following are the actual and imaginary parts of the TMP, respectively, and the expansion formulas of the cos (x) and sin (x) are compared:

Pprint(re(tmp)) 2 4 6 8 x x x x1 + Re (O (x**10))---+------+----- 2 24 720 4032 0        
Pprintseriescos(xx0) 2 4 6 8 x x x x 1---+------+---- -+ O (x**10) 2 720 40320           
Pprint(im(tmp)) 3 5 7 9 x x x x x + im (O (x**10))--+--------+------ 6 120 5040 362880        
Pprint(series(sin(xx0)) 3 5 7 9 x x x x X---+----- ---+------+ O (x**10) 6 5040 362880            
4.2 Sphere Volume

In the scipy numerical Integration section we describe how to use numerical definite integrals to calculate the volume of a sphere, while SymPy's symbolic integral function integrate can help us with symbolic integrals. Integrate can make indefinite integrals:

Integrate(x*sin(xx)-x*cos (x) + sin (x)       

If you specify the range of values for X, integrate will perform definite integration operations:

Integrate(x*sin(x(x02*pi))-2*pi  

To calculate the sphere volume, let's first look at how to calculate the circular area, assuming that the radius of the circle is R, then the y-coordinate function of any point on the circle is:

So we can get a semicircle area directly on the above function in the-R to R interval, note here we use the symbols function to create multiple symbols at once:

 >>> xy, r = symbols ( ' x,y,r ' ) >>> 2 * integrate< Span class= "P" > (sqrt (r*r -x**2),  (x-rr 2*integral ((r**2-x**2) * * ((x, R, R))       

Unfortunately, the integrate function does not calculate the result, but instead returns the calculation we entered directly. This is because SymPy does not know that R is greater than 0, the following redefinition of R, you can get the correct answer:

>>>R= symbols ( ' R ' =true) >>>  Circle_area = 2 * integrate (sqrt (r**2- x**2),  (x-rr) >>> circle_areapi*r**2       

Next, the area formula to set the integral, you can get the volume of the sphere, but with the x-axis coordinate changes, the corresponding section of the radius will change, now assume that the x axis coordinates x, the radius of the sphere is r, the radius of the slice at x is calculated using the preceding formula Y (x).

Fig. 4.1 Double definite integral of sphere volume

So we need to replace the variable R in Circle_area:

Circle_area.  Subs(rsqrt(r* *2-x* *2))Circle_areapi* (r**2-x **2)               

To replace a formula with a subs

The subs function can replace a symbol in a calculation with 3 ways to call it:

    • Expression.subs (x, y): replace x in calculation with Y
    • Expression.subs ({x:y,u:v}): Multiple substitutions using a dictionary
    • Expression.subs ([(x, Y), (u,v)]): Multiple substitutions using lists

Note that multiple substitutions are performed sequentially, so:

Expression.  Sub([(x,y), (y,x)])          

It is not possible to swap two symbols x, Y.

Then the variable x in the Circle_area is determined by the integral on the interval-R to R, and the volume formula of the sphere is obtained:

Integrate(Circle_area(x-RR))4*PI*R**3/3     

sympy-Symbol Arithmetic Good helper

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.