Learn Computing from the Experts | The Rheinwerk Computing Blog

5 Python Modules for Scientific Computing

Written by Rheinwerk Computing | Apr 5, 2024 1:10:00 PM

Let’s take a look at the capabilities and features of the module concept in Python; we’ll describe five scientific computing modules in a keyword-like manner.

 

Instead of module, the terms library or software library are also commonly used. The capabilities of Python are best illustrated by using short sample programs. Of course, you don’t need understand the source code shown in this blog post. This is just to introduce you to the different modules.

 

NumPy

The NumPy module (numerical Python) enables you to perform extensive numerical calculations. For example, you can solve linear systems of equations, even with complex numbers. Below shows a simple vector calculus program.

 

01 import numpy as np

02 A=np.array([1, 2, 3])

03 B=np.array([4, 5, 6])

04 print("Vector       A:",A)

05 print("Vector       B:",B)

06 print("Total        A+B:",A+B)

07 print("Product      A*B:",A*B)

08 print("Cross product :",np.cross(A,B))

09 print("Scalar product:",np.dot(A,B))

 

Output

Vector A: [1 2 3]

Vector B: [4 5 6]

Total    A+B: [5 7 9]

Product              A*B: [ 4 10 18]

Cross product : [-3 6 -3]

Scalar product: 32

 

Matplotlib

The Matplotlib module allows you to display mathematical functions, histograms, and many other diagram types as well as to simulate and animate physical processes. The graphical design options are remarkably diverse and rich in detail. Below shows a simple example of the function plot of a polynomial.

 

01 import numpy as np

02 import matplotlib.pyplot as plt

03 x=np.arange(-2,6,0.01)

04 y=x**3-7*x**2+7*x+15

05 plt.plot(x,y)

06 plt.show()

 

Output

This figure shows the output of the function plot.

 

 

SymPy

Using SymPy (symbolic Python), you can calculate integrals or derivatives symbolically or solve differential equations symbolically. A simplification of mathematical terms is also possible (and much more). The code below shows a simple example of symbolic differentiation and integration.

 

01 from sympy import *

02 x=symbols("x")

03 y=x**3-7*x**2+7*x+15

04 y_1=diff(y,x,1)

05 y_2=diff(y,x,2)

06 y_3=diff(y,x,3)

07 Y=integrate(y,x)

08 print("1. Derivative:",y_1)

09 print("2. Derivative:",y_2)

10 print("3. Derivative:",y_3)

11 print("   Integral :",Y)

 

Output

  1. Derivative: 3*x**2 - 14*x + 7
  2. Derivative: 2*(3*x - 7)
  3. Derivative: 6

Integral : x**4/4 - 7*x**3/3 + 7*x**2/2 + 15*x

 

SciPy

SciPy (scientific Python) allows you to numerically differentiate, integrate, and numerically solve systems of differential equations. SciPy is as comprehensive as it is versatile. The capabilities of SciPy can only be partially described in this book. Below is a simple example of a numerical integration program.

 

01 import scipy.integrate as integral

02 def f(x):

03          return x**2

04 A=integral.quad(f,0,5)

05 print("Area A=",A[0])

 

Output

Area A= 41.66666666666666

 

VPython

Using VPython, you can display fields in a 3D view or even animate their movements in 3D space. As of version 7, the animations are displayed in the standard browser after the program starts. The final listing shows an example of how you can program the animation of a bouncing ball.

 

01 from vpython import *

02 r=1. #radius

03 h=5. #height

04 scene.background=color.white

05 scene.center=vector(0,h,0)

06 box(pos=vector(0,0,0),size=vector(2*h,r/2,h), color=color.green)

07 ball = sphere(radius=r, color=color.yellow)

08 ball.pos=vector(0,2*h,0) #drop height

09 ball.v = vector(0,0,0) #initial velocity

10 g=9.81

11 dt = 0.01

12 while True:

13          rate(100)

14          ball.pos = ball.pos + ball.v*dt

15          if ball.pos.y < r:

16                    ball.v.y = -ball.v.y

17          else:

18                    ball.v.y = ball.v.y - g*dt

 

Output

The figure below shows a snapshot of the animation.

 

 

Editor’s note: This post has been adapted from a section of the book Python for Engineering and Scientific Computing by Veit Steinkamp.