Commonly used functions

In [1]:
import numpy as np
  • Create an array as input for the functions
In [2]:
x = np.array([-2, 1.5, 0, 1/2*np.pi, np.pi]); print(x);
[-2.          1.5         0.          1.57079633  3.14159265]
  • Find out the absolute value of all the elements of an array
In [3]:
xa = np.abs(x); print(x); print(xa);
[-2.          1.5         0.          1.57079633  3.14159265]
[2.         1.5        0.         1.57079633 3.14159265]
  • Some trigonometric functions on $\textbf{x}$
In [4]:
b = np.cos(x); print(b);
c = np.sin(x); print(c);
d = np.tan(x); print(d);
[-4.16146837e-01  7.07372017e-02  1.00000000e+00  6.12323400e-17
 -1.00000000e+00]
[-9.09297427e-01  9.97494987e-01  0.00000000e+00  1.00000000e+00
  1.22464680e-16]
[ 2.18503986e+00  1.41014199e+01  0.00000000e+00  1.63312394e+16
 -1.22464680e-16]
  • Some inverse trignometric functions on the output of previous cell
In [5]:
bi = np.arccos(b); print(bi); print(x);
ci = np.arcsin(c); 
di = np.arctan(d);
[2.         1.5        0.         1.57079633 3.14159265]
[-2.          1.5         0.          1.57079633  3.14159265]
  • Inverse tan has another function: $\textbf{atan2}$ which takes appropriate quadrant into consideration
In [6]:
e = np.arctan2(-1, 1); print(e);
f = np.arctan2(1, 1); print(f);
print(np.pi/4)
-0.7853981633974483
0.7853981633974483
0.7853981633974483
  • Hyperbolic functions can also be computed: $\cosh (x) = \frac{\left( \exp(x) + \exp(-x)\right)}{2}$
In [7]:
y = np.array([-2, -1, 1, 2]);
In [8]:
ys = np.sinh((y)); print(ys);
[-3.62686041 -1.17520119  1.17520119  3.62686041]
In [9]:
ysp1 = np.exp(y); ysp2 = np.exp(-y);
ys2 = 1/2*(ysp1 - ysp2); print(ys); print(ys2);
[-3.62686041 -1.17520119  1.17520119  3.62686041]
[-3.62686041 -1.17520119  1.17520119  3.62686041]
  • In python $\textbf{j}$ is used for complex part
  • Let us create $\textbf{c}$ variable with complex value and perform the following operations:
    • Absolute value of $\textbf{c}$
    • Real part of $\textbf{c}$
    • Imaginary part of $\textbf{c}$
    • Conjugate of $\textbf{c}$
In [10]:
c = 2 + 3j;
print(np.abs(c));
print(np.sqrt(2**2 + 3**2));
print(np.real(c));
print(np.imag(c));
print(np.conj(c));
3.605551275463989
3.605551275463989
2.0
3.0
(2-3j)
  • We can also perform the same operations on an array of values
In [11]:
d = np.array([2+3j, 1-1j, 4+np.pi*1j]); print(d);
[2.+3.j         1.-1.j         4.+3.14159265j]
In [12]:
dc = np.conj(d); print(dc)
[2.-3.j         1.+1.j         4.-3.14159265j]

Plotting

  • Let us see how plotting works by makin use of $\textbf{matplotlib}$ module
    • We specify latex rendering
    • We specify the figure format to .svg
In [13]:
import matplotlib.pyplot as plt
plt.rcParams.update({"text.usetex":True});
%config InlineBackend.figure_format = 'svg';
  • Let us plot $\textrm{sin}(\textbf{x})$ and $\textrm{cos}(\textbf{x})$
In [14]:
x = np.linspace(-2, 2, 100);
y = np.cos(2*np.pi*x);
z = np.sin(2*np.pi*x);
  • For latex rendering we need to specify the contents between $$.
  • We can plot multiple functions in the same windows
In [15]:
# We can change the linspec of the plot. Here we plot cos(x) as a dotted line
plt.plot(x, y,'.', label="$\\cos(x)$");


plt.plot(x, z, label="$\\sin(x)$");
plt.plot(x, np.tan(x), label="$\\tan(x)$");

# Specify the x and y labels
plt.xlabel("$x$"); # Latex rendering
plt.ylabel("$y$"); 

# Enable the legend. We have already specified the values earlier in plot command
plt.legend();

# Specify the plot title
plt.title("Some functions plotted");

# Specify the limits of x and y axis
plt.ylim(-1,1);
plt.xlim(0, 2);

Plotting some special functions

  • The module $\textbf{scipy}$ has a submodule $\textbf{special}$ which contains some special functions
In [16]:
import scipy.special as sp
  • Let us look at the $\textrm{0th}$ order bessel function of first kind
    • $\textbf{jv(n,x)}$ outputs the $\textbf{nth}$ order bessel function of first kind queried at $\textbf{x}$
In [17]:
x = np.linspace(0, 10);
y = sp.jv(0,x);
plt.plot(x, y);
plt.xlabel("$x$");
plt.ylabel("$J_0$");
  • We can now plot functions of different orders
    • We loop over the order using for loop
In [18]:
x = np.linspace(0, 10);
for i in np.arange(0,4):
    y = sp.jv(i,x);
    plt.plot(x, y, label="$J_%d$"%i); # In C, sprintf
plt.xlabel("$x$");
plt.ylabel("Bessel function");
plt.legend();

# Turn on Grid
plt.grid();

# useful to set properties of the axes 
# we query the axes and then we can modify it
ax = plt.gca();

# Set the aspect ratio of x and y axes
ax.set_aspect(2.5) 

Defining your own function

  • We can also define our own functions
  • Let us define a function to calculate the circumference and area of a circle with the radius $\textbf{r}$ as input
In [19]:
def circ_properties(r):
    # local variables - scope is limited to the definition of the function
    area  = np.pi*r**2;
    circ = 2*np.pi*r;
    return area, circ
  • Here $\textrm{circ_properties}$ the name of the function with input as $\textbf{r}$
  • We calculate the area and circumference and store them in variables and then return those values

  • We can now call the function for any radius

  • We store the return value in the variable $\textrm{props}$
In [20]:
props = circ_properties(1);
In [21]:
print("For a circle with radius: ", 1)
print("The area is: ", props[0]);
print("Circumferance: ", props[1]);
For a circle with radius:  1
The area is:  3.141592653589793
Circumferance:  6.283185307179586
  • Let's create a function which takes two inputs, $\textbf{x}$ and $\textbf{c}$ and creates a ploynomial $x^2 + cx + 2c$
  • We return the value of function for the given $\textbf{x}$ and $\textbf{c}$
  • We also return a scalar to show how to return mixed type of variables
In [22]:
def fx(x, c):
    return x**2 + c*x + 2*c, np.exp(c);
  • We plot a family of curves by varying $\textbf{c}$
In [23]:
x = np.linspace(-3, 3);
for i in np.arange(-2, 3):
    y, e = fx(x, i);
    plt.plot(x, y, label="$c=%d$"%i);
plt.legend();
print(e);
print(np.exp(2));
7.38905609893065
7.38905609893065

File IO

In [24]:
print(x);
[-3.         -2.87755102 -2.75510204 -2.63265306 -2.51020408 -2.3877551
 -2.26530612 -2.14285714 -2.02040816 -1.89795918 -1.7755102  -1.65306122
 -1.53061224 -1.40816327 -1.28571429 -1.16326531 -1.04081633 -0.91836735
 -0.79591837 -0.67346939 -0.55102041 -0.42857143 -0.30612245 -0.18367347
 -0.06122449  0.06122449  0.18367347  0.30612245  0.42857143  0.55102041
  0.67346939  0.79591837  0.91836735  1.04081633  1.16326531  1.28571429
  1.40816327  1.53061224  1.65306122  1.7755102   1.89795918  2.02040816
  2.14285714  2.26530612  2.3877551   2.51020408  2.63265306  2.75510204
  2.87755102  3.        ]
In [25]:
print(y)
[ 7.          6.52519783  6.08038317  5.66555602  5.28071637  4.92586422
  4.60099958  4.30612245  4.04123282  3.8063307   3.60141608  3.42648896
  3.28154935  3.16659725  3.08163265  3.02665556  3.00166597  3.00666389
  3.04164931  3.10662224  3.20158267  3.32653061  3.48146606  3.666389
  3.88129946  4.12619742  4.40108288  4.70595585  5.04081633  5.40566431
  5.80049979  6.22532278  6.68013328  7.16493128  7.67971678  8.2244898
  8.79925031  9.40399833 10.03873386 10.70345689 11.39816743 12.12286547
 12.87755102 13.66222407 14.47688463 15.32153269 16.19616826 17.10079134
 18.03540192 19.        ]
In [26]:
print(np.shape(x)); print(np.shape(y))
(50,)
(50,)
  • The following line shows how the data can be put inside the file $\textrm{"datafile.txt"}$
In [27]:
np.savetxt("Datafile.txt", (x, y));
  • We can load the file into variable
In [28]:
d = np.loadtxt("Datafile.txt");
print(np.shape(d))
(2, 50)
  • Let's concatenate the two arrays in a column wise fashion to output them to a file in a "human readable" format.
In [29]:
e = np.c_[x, y]; print(e); np.savetxt("data_human.txt", e); 
[[-3.          7.        ]
 [-2.87755102  6.52519783]
 [-2.75510204  6.08038317]
 [-2.63265306  5.66555602]
 [-2.51020408  5.28071637]
 [-2.3877551   4.92586422]
 [-2.26530612  4.60099958]
 [-2.14285714  4.30612245]
 [-2.02040816  4.04123282]
 [-1.89795918  3.8063307 ]
 [-1.7755102   3.60141608]
 [-1.65306122  3.42648896]
 [-1.53061224  3.28154935]
 [-1.40816327  3.16659725]
 [-1.28571429  3.08163265]
 [-1.16326531  3.02665556]
 [-1.04081633  3.00166597]
 [-0.91836735  3.00666389]
 [-0.79591837  3.04164931]
 [-0.67346939  3.10662224]
 [-0.55102041  3.20158267]
 [-0.42857143  3.32653061]
 [-0.30612245  3.48146606]
 [-0.18367347  3.666389  ]
 [-0.06122449  3.88129946]
 [ 0.06122449  4.12619742]
 [ 0.18367347  4.40108288]
 [ 0.30612245  4.70595585]
 [ 0.42857143  5.04081633]
 [ 0.55102041  5.40566431]
 [ 0.67346939  5.80049979]
 [ 0.79591837  6.22532278]
 [ 0.91836735  6.68013328]
 [ 1.04081633  7.16493128]
 [ 1.16326531  7.67971678]
 [ 1.28571429  8.2244898 ]
 [ 1.40816327  8.79925031]
 [ 1.53061224  9.40399833]
 [ 1.65306122 10.03873386]
 [ 1.7755102  10.70345689]
 [ 1.89795918 11.39816743]
 [ 2.02040816 12.12286547]
 [ 2.14285714 12.87755102]
 [ 2.26530612 13.66222407]
 [ 2.3877551  14.47688463]
 [ 2.51020408 15.32153269]
 [ 2.63265306 16.19616826]
 [ 2.75510204 17.10079134]
 [ 2.87755102 18.03540192]
 [ 3.         19.        ]]
In [ ]: