2.1. Common Functions in Physics#

2.1.1. Elementary Functions#

We cannot discuss physics without elementary functions such as exponential, logarithmic, and trigonometric functions. None of them are included in the python core. We need to load a mathematical package to use them. There are many different packages to choose. In this lecture, we use math, numpy, scipy, and sympy, and mpmath.

2.1.1.1. math package#

The math packages provide basic mathematical functions including elementary functions. See the documentation to find the available functions. You can load it in many different ways depending on the situation.

First we load the whole package

import math

print(math.cos(math.pi/6))

del math
0.8660254037844387

Every function in the package can be used but the function name must begin with math.. you can unload the package using del command. If the prefix is too long, you can specify an alias.

import math as m

print(m.cos(m.pi/6))

del m
0.8660254037844387

It is also possible to load individual functions in the package

from math import cos, pi

print(cos(pi))

del cos
-1.0

This method is convenient if we use only a few functions. Notice that no namespace prefix is needed in this case. In fact, you can load all modules in the package in the same way.

from math import *

print(cos(pi/6),sin(pi/6))
0.8660254037844387 0.49999999999999994

This seems convenient but actually not. It will be confusing if you load multiple packages containing the same name of functions, In addition, we cannot unload the module using del command. We should use this loading method only for simple calculation.

2.1.1.2. numpy package#

As we already mentioned, for scientific computation, the most popular mathematical package is numpy. Actually, numpy is more than a mathematical package. I provides many useful tools. See the documentation. On the other hand, it uses its own numerical format. If we use multiple packages, compatibility between them needs to be considered. We will discuss it when this issue comes up. We will be using numpy through out this lecture. AS we already used in the previous chapter, we load numpy using alias “np”.

import numpy as np

print(np.cos(np.pi/6))

del np
0.8660254037844387

2.1.1.3. mpmath package#

numpy is based on the standard floating point and thus the precision is limited as discussed in the previous chapter. The mpmath packge uses a different kind of floating point arithmetic algorithm which provides arbitrary precision. The format is called mpf. When a great precision is needed, we use this package.

import mpmath as mp

# specify the number of digits we want
mp.mp.dps = 30
print(mp.cos(mp.pi/6),mp.sin(mp.pi/6))

del mp
0.866025403784438646763723170753 0.5

2.1.2. Special Functions#

Special functions such as Bessel function appear in many parts of physics. Since the algorithms to evaluate special functions are well developed and many packages provide them. Therefore, we use the canned routines. The following special functions are popular in physics.

2.1.2.1. scipy#

SciPy provides modules for optimization, integration, interpolation, eigenvalue problems, algebraic equations, differential equations, statistics and many other classes of problems. See the documentation. It is implemented with compiler based language such Fortran and C++. Many modules are in fact interface to well-known mathematical libraries. Among many modules, scipy provides modules called scipy.special. All kinds of special functions are included. See the documentation for available functions. As examples, we evaluate gamma function and Hermite polynomial.

from scipy.special import gamma, hermite

# gamma function
print(gamma(pi))

# scipy returns hermite polynomial
H2 = hermite(2)
# Hermite polynomial of 2nd order evaluated at x=2.
print(H2(pi))

del gamma, hermite
2.288037795340032
37.47841760435743

2.1.2.2. mpmath package#

mpmath package provides also special functions.

from mpmath import hermite

print(hermite(2,pi))

del hermite
37.4784176043574313974648540446