How do I replace loops with matrices in Python scientific calculations?

Source: Internet
Author: User
For example, to find a plane steady heat conduction problem, the control equation is the Laplace equation:



(I found that the original [insert formula] this function)

Write according to the simplest kind of cycle:

def Laplace (U):     nx, NY = U.shape for     i in Xrange (1,nx-1):          for J in Xrange (1, ny-1):              u[i,j] = ((U[i+1, J] + U[i-1, J]) * Dy2 +  (u[i, j+1] + u[i, j-1]) * dx2)/((DX2+DY2))

You don't even know Numexpr's ←_←?
A technology that is darker than numpy →_→

Although the operation can be used not much, but the overall operation of the large matrix is very fast ←_← recently just learning numpy this module. The master can look at this tutorial, not very full, but there are still a lot of things in scientific computing: numpy-fast processing of data
Reference the code in the tutorial:

import timeimport mathimport numpy as npx = [i * 0.001 for i in Xrange (1000000)] # Initial                0.000~999.999 start = Time.clock () for I, T in enumerate (x): # Calculates the sine value with a loop x[i] = Math.sin (t)                          Print "Math.sin:", Time.clock ()-STARTX = [i * 0.001 for i in Xrange (1000000)]x = Np.array (x) # initialization matrix (here is one dimension) start = Time.clock () np.sin (x,x) # NumPy broadcast calculation (instead of loop) print "Numpy.sin:", time.cl Ock ()-start# output # math.sin:1.15426932753# numpy.sin:0.0882399858083 

With NumPy, Cython, or weave.
Speed up Python
SCIPY website has a tutorial on how to improve the Python performance
Performancepython
Using Pyrex/cython or weave can basically achieve the speed of C + +.
Example of Laplace, 500*500 matrix, 100 cycles.
NumPy and Pandas. The matrix operation of Dataframe can be broadcast and map can be used. The first trick is to use map and lambda expressions to generate the iteration parameters you want, such as generating a square table: map (Lambda x:x*x, xrange (100)), which is a black technology that can quickly generate the loop parameters you need;
The second trick is to use a matrix mask to simplify loops, such as placing a value less than 100 in matrix A 0: a[a<100] = 0, much faster than a loop;
The third trick is to use a variety of libraries, such as NumPy, scipy (signal library is simply great), and if you do an image, the OpenCV library is the only option.
Roughly so, the actual application is more of the first two trick mixed use. To be quick, inline C,python is an explanatory language and will be slower.
There are mature computational software used in the C/c+++python model, core algorithms and time-consuming logic in C + +, others with Python.

  • 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.