NumPy  Matrix Operations  and Element-Wise Product

Author: Al-mamun Sarkar Date: 2020-03-30 20:49:35

NumPy  Matrix Operations and Element-Wise Product. In the following code, I will Elementwise product and Matrix operations of Python NumPy Module.

 

import numpy as np
from numpy import pi

 

Code:

A = np.array( [[1,1],
             [0,1]] )
B = np.array( [[2,0],
             [3,4]] )
A*B 

Output:

array([[2, 0],
       [0, 4]])

 

Matrix product:

A.dot(B)

Output:

array([[5, 4],
       [3, 4]])

 

Another matrix product:

np.dot(A, B)

Output:

array([[5, 4],
       [3, 4]])

 

Code:

a = np.ones((2,3), dtype=int)
b = np.random.random((2,3))
a *= 3
a

Output:

array([[3, 3, 3],
       [3, 3, 3]])

 

Code:

b

Output:

array([[ 6.96176389,  6.15431585,  6.64806932],
       [ 6.58159376,  6.53335857,  6.0749873 ]])

 

Code:

b += a
b

Output:

array([[ 6.96176389,  6.15431585,  6.64806932],
       [ 6.58159376,  6.53335857,  6.0749873 ]])

 

Code:

a = np.ones(3, dtype=np.int32)
b = np.linspace(0,pi,3)
b.dtype.name

Output:

'float64'

 

Code:

c = a + b
c

Output:

array([ 1.        ,  2.57079633,  4.14159265])

 

Code:

c.dtype.name

Output:

'float64'

 

Code:

d = np.exp(c*1j)
d

Output:

array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j,
       -0.54030231-0.84147098j])

 

Code:

d.dtype.name

Output:

'complex128'