Numpy
‘Numpy’ is Numerical Python package developed for Python to do numerical calculations. This is very
useful for multidimensional array management. Once numpy package is installed, we can impot the
package by ‘import numpy’ and start using the modules with reference to numpy. For example to import
numpy and refer array() function, we may write,
import numpy as np
[Link](…)
We can have arrays of many dimensions. Simple lists are one dimensional arrays. Nested lists are
multidimensional arrays. For example, x=[[1,2,3],[4,5,6]] is a two-dimensional array (2 lists inside one
list). If we think of this as a matrix, the dimension is . In other words, the ‘shape’ of the array is
(2,3), i.e. a list of 2 elements, each of which is a list of 3 elements. In the same way, if we have,
y=[[[1,2],[3,4]],[[5,6],[7,8]]], then y is a 3 dimensional array. The shape of y is (2,2,2).
Once the arrays are called from Numpy, the algebra of the numpy becomes so simple, the rules will be
clear as we go through some examples.
>>> import numpy as np
>>> x=[Link]([[1,2,3],[4,5,6]])
>>> x
array([[1, 2, 3],
[4, 5, 6]])
>>> [Link]
(2, 3)
>>> y=[Link]([[3,2,1],[6,5,4]])
>>> y
array([[3, 2, 1],
[6, 5, 4]])
>>> [Link]
(2, 3)
Algebra with arrays
>>> x+y
array([[ 4, 4, 4],
[10, 10, 10]])
>>> x*y
array([[ 3, 4, 3],
[24, 25, 24]])
>>> (x+y)*(x-y)
array([[ -8, 0, 8],
[-20, 0, 20]])
Similarly, we can find 1/x, x/y etc., where the mathematical operations involve the corresponding
elements of the array. Note that x*y is not a matrix product. The rules of matrix multiplications are
different and we do that with other functions from Numpy.
Reshaping an array: We can know the ‘shape’ of an array (a simple or a nested loop) by ‘shape’
command taken from numpy module
>>> import numpy as np
>>> A=[Link]([1,2,3,4,5,6])
>>> [Link]
(6,)
>>> [Link](2,3)
array([[1, 2, 3],
[4, 5, 6]])
The single element in the tuple (6,) implies that the array is one dimensional and contains 6 elements.
Number of elements in a tuple is equal to the dimension of an array. Next, we reshaped the array into a
two dimensional array with (2,3).
Note: Alternatively, for an ordinary list, A=[1,2,3,4,5,6], the shape and reshape by numpy:
>>> [Link](A)
(6,)
>>> [Link](A,(2,3))
array([[1, 2, 3],
[4, 5, 6]])
Instead of ‘reshape’ one can use ‘resize’ as,
>>> [Link](2,3)
>>> A
array([[1, 2, 3],
[4, 5, 6]])
Note that the reshape() function returns its arguments with a modified shape whereas the resize()
function modifies the array itself.
Matrix Addition with Numpy:
import numpy as np
A=[Link]([[9,7,3],[4,5,6],[7,8,9]])
B=[Link]([[5,8,9],[6,7,8],[4,5,2]])
print(A+B)
On running this we obtain,
[[14 15 12]
[10 12 14]
[11 13 11]]
Note: The 2-dimensional numpy array (with numpy) automatically prints like a matrix. Alternatively,
with one dimensional arrays as input, we can reshape the arrays into the forms of desired matrices.
import numpy as np
A=[Link]([9,7,3,4,5,6,7,8,9])
B=[Link]([5,8,9,6,7,8,4,5,2])
print([Link](3,3)+[Link](3,3))
This will print the same result as before.
Matrix Addition with Numpy (input from outside)
import numpy as np
m,n=3,3
print("Enter the elements of A: ")
A=[Link]([float(input()) for i in range(m*n)])
print("Enter elements of B: ")
B=[Link]([float(input()) for i in range(m*n)])
print([Link](m,n)+[Link](m,n))
Note: In the above python script, the matrix elements of A and B are entered as one-dimensional arrays.
The arrays are then reshaped by reshape() to form a matrix and added.
Matrix multiplication with Numpy
import numpy as np
A=[Link]([[1,2,3],[4,5,6],[7,8,9]])
B=[Link]([[3,2,1],[6,5,4],[9,8,7]])
C=[Link](A,B)
print(C)
[[ 42 36 30]
[ 96 81 66]
[150 126 102]]
Matrix Transpose with Numpy
import numpy as np
A=[Link]([[1,2,3],[4,5,6],[7,8,9]])
print(A.T)
[[1 4 7]
[2 5 8]
[3 6 9]]
Check the following on Python interpreter
>>> import numpy as np
>>> M=[Link]([[1,2,3],[4,5,6]])
>>> M
array([[1, 2, 3],
[4, 5, 6]])
>>> M.T
array([[1, 4],
[2, 5],
[3, 6]])
It is important sometimes, that we may receive data in any form as input and can convert it to numpy
array. One numpy function ‘asarray()’ can convert any form of data to numpy array. Once numpy array is
made, we can reshape this to a matrix of some dimension and work.
>>> import numpy as np
>>> x=[1,2,3,4,5,6]
>>> [Link](x)
array([1, 2, 3, 4, 5, 6])
>>> x=(1,2,3,4,5,6)
>>> [Link](x)
array([1, 2, 3, 4, 5, 6])
>>> x=1,2,3,4,5,6
>>> [Link](x)
array([1, 2, 3, 4, 5, 6])
Matrix arrays: There are separate objects, called matrix() under the Numpy arrays. We may directly use
this to define a matrix and proceed for matrix operations.
>>> import numpy as np
>>> A=[Link]([[1,2,3],[4,5,6],[7,8,9]])
>>> A
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> B=[Link]([[3,2,1],[6,5,4],[9,8,7]])
>>> B
array([[3, 2, 1],
[6, 5, 4],
[9, 8, 7]])
>>> A*B
matrix([[ 42, 36, 30],
[ 96, 81, 66],
[150, 126, 102]])
>>> [Link](A)
15
1. Write a program that will generate two random matrices and print their sum and product.
2. Write a program that will determine the product of two matrices by taking the matrix elements
from the user.
Special Arrays:
>>> import numpy as np
>>> [Link](3)
array([0., 0., 0.])
>>> [Link]((2,3))
array([[0., 0., 0.],
[0., 0., 0.]])
>>> [Link](5)
array([1., 1., 1., 1., 1.])
>>> [Link]((3,3))
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
>>> [Link]((3,3),5)
array([[5, 5, 5],
[5, 5, 5],
[5, 5, 5]])
>>> [Link](3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
>>> [Link]((3,3))
array([[0.00636773, 0.52805503, 0.93159676],
[0.96683078, 0.29088168, 0.19198261],
[0.00546459, 0.68236433, 0.88862201]])
There are some functions in Numpy by which we can create arrays of any numbers, integers or
fractional numbers. One of them is ‘arange()’ function.
>>> [Link](10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> [Link](1,5,0.5)
array([1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])
>>> [Link](1,12,2)
array([ 1, 3, 5, 7, 9, 11])
>>> [Link](1,12,2).reshape(2,3)
array([[ 1, 3, 5],
[ 7, 9, 11]])
>>> [Link](1,10,10)
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
The linspace() function created 10 equidistant point. To compare with arange() function.
>>> [Link](0.1,0.5,5)
array([0.1, 0.2, 0.3, 0.4, 0.5])
>>> [Link](0.1,0.5,0.1)
array([0.1, 0.2, 0.3, 0.4])
Algebra with arrays:
>>> a=[Link]([1,2,3,4,5,6])
>>> b=[Link]([7,8,9,10,11,12])
>>> a+b
array([ 8, 10, 12, 14, 16, 18])
>>> b-a
array([6, 6, 6, 6, 6, 6])
>>> b/a
array([7. , 4. , 3. , 2.5, 2.2, 2. ])
>>> b*a
array([ 7, 16, 27, 40, 55, 72])
>>> a**3+b**2
array([ 50, 72, 108, 164, 246, 360], dtype=int32)
Note that such algebra is not possible with ordinary lists. Let us recall, the addition of two lists will give a
list of all elements of the two lists.
>>> a=[Link]([1,2,3,4,5,6])
>>> sum(a)
21
>>> max(a)
6
>>> min(a)
1
>>> [Link](a)
3.5
Note: Functional operations on ordinary lists, e.g., sum(), len(), max(), min() etc., are also methods for
numpy arrays. Additionally, we can perform many more mathematical operations
>>> q=[Link]([1,5,3,2,8])
>>> [Link](q) # calculate the differences between the successive entries
array([ 4, -2, -1, 6])
>>> [Link](q)
240
>>> [Link](q)
3.8
>>> [Link](q)
6.159999999999999
>>> [Link](q)
3.0
>>> z=1+2j
>>> [Link](z)
(1-2j)
>>> x=[Link]([1+2j,3+1j,2+3j])
>>> [Link](x)
array([1.-2.j, 3.-1.j, 2.-3.j])
>>> [Link](8.92)
8.0
>>> [Link](8.92)
9.0
Mathematical functions of arrays:
>>> a=[Link]([1,2,3,4])
>>> [Link](a)
array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 ])
>>> [Link](a)
array([ 2.71828183, 7.3890561 , 20.08553692, 54.59815003])
>>> [Link](a)
array([1. , 1.41421356, 1.73205081, 2. ])
>>> def f(x): return x*x
>>> f(a)
array([ 1, 4, 9, 16])
Slicing of arrays:
>>> a=[Link]([1,2,3,4,5,6])
>>> a[::]
array([1, 2, 3, 4, 5, 6])
>>> a[::2]
array([1, 3, 5])
>>> a[:3:]
array([1, 2, 3])
>>> a[1:4:2]
array([2, 4])
>>> [Link](10)[1:5:2]
array([1, 3])
>>> x=[Link]([[1,2,3],[4,5,6],[7,8,9]])
>>> x[::]
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> x[:,0]
array([1, 4, 7])
>>> x[0,:]
array([1, 2, 3])
Arrays as vectors: A vector is an one dimensional array
>>> u=[Link]([1,2,3])
>>> v=[Link]([-1,0,1])
>>> [Link](u,v)
2
>>> [Link](u,2)
array([2, 4, 6])
Multidimensional inner product:
>>> A=[Link]([[1,2,3],[4,5,6]])
>>> B=[Link]([[1,0,1],[0,1,0]])
>>> [Link](A,B)
array([[ 4, 2],
[10, 5]])
Note inner product between two matrices (multidimensional arrays) is different from the matrix
product. Imagine, each row is a vector. So there are vector inner products between rows of two
multidimensional arrays. 4=1*1+2*0+3*1, 2=1*0+2*1+3*0, 10= 4*1+5*0+6*1, 5= 4*0+5*1+6*0.
Vector Product:
>>> u=[Link]([1,2,3])
>>> v=[Link]([1,0,1])
>>> [Link](u,v)
4
Note: One-dimensional arrays can be treated as vectors with tuples of components. So, the vector
product between two one-dimensional arrays is just the inner product. However, for multidimensional
arrays, inner product and vector product are quite different. This is because, in vector products the
entire matrix is treated as a vector (one dimensional array). For example, [Link]([[1,2,3],[4,5,6]]) is
treated as a vector with tuple, (1,2,3,4,5,6). For one-dimensional arrays dot(), vdot(), inner(), … all
produce same result.
Cross Product:
>>> x=[Link]([1,2,3])
>>> y=[Link]([-1,3,0])
>>> [Link](x,y)
array([-9, -3, 5])
Volume of a parallelepiped:
>>> import numpy as np
>>> a=[Link]([2,-3,0])
>>> b=[Link]([1,1,-1])
>>> c=[Link]([3,0,-1])
>>> [Link](a,[Link](b,c))
4
1. Three forces are given by ̂ ̂ N, ̂ ̂ N and ̂ N, act on an object
to give it an acceleration of magnitude m/s2. What is the direction of the acceleration?
2. For ⃗ ̂ ̂ ̂ , ⃗⃗ ̂ ̂ ̂ , and ⃗ ̂ ̂ , find ⃗ ⃗ ⃗⃗ , ⃗ ⃗⃗ ⃗ and
⃗ ⃗⃗ ⃗ .
3. Take two/three vectors from the users and print dot and cross product/ scalar and vector
products.
4. Given ⃗ , ⃗⃗ and ⃗ ,
find the norms of ⃗ , ⃗⃗ and ⃗. Are the vectors orthogonal?