Creation of matrix
Creation of Matrix:
Create a Matrix and perform various types of operations on it.
In python, a matrix can be implemented as a 2D list or 2D Array. Create a matrix that gives the greater functionalities for performing numerous operations within the matrix. These operations are defined in a module “numpy“. When we start to work with Python Matrix, the very first step is we need to import Python numpy module.
If you do no longer have any idea about numpy module you can study Python libraries first. Python matrix is used to perform operations involving matrix, which may also be used for scientific purposes, image processing, etc.
The matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. So, in the following code, we will be initializing various kinds of matrices. A matrix has created with the usage of numpy.matrix() function.
In this section, We discuss how to Create a Matrix in Python and also perform various types of operations on the Matrix.
numpy.shape - know the dimension of the matrix.
Operations on Matrix :
1. Creation of 2D matrix:- we can create a 2D matrix using numpy.import numpy as np
a = np.matrix('1 2 ; 3 4')
print(a)
OutPut: [[1 2]
[3 4]]
b = np.matrix('1 2 ; 3 4')
print(b)
OutPut: [[1 2]
[3 4]]
2. Creation of 3D matrix:- we can create a 3D matrix using numpy.
import numpy as np
a = np.matrix('1 2 3 ; 11 12 13 ; 21 22 23')
print(a)
Output: [[ 1 2 3]
[11 12 13]
[21 22 23]]
b = np.matrix('1 2 3 ; 5 6 1 ; 3 3 4')
print(b)
Output: [[1 2 3]
[5 6 1]
[3 3 4]]
3. Addition():- This function is used to perform element wise addition operation on a given Matrix.
c = a+b
print(c)
Output: [[ 2 4 6]
[16 18 14]
[24 25 27]]
Also, Read the Basics of Python Programming
4. Subtraction():- This function is used to perform element wise matrix subtraction.
d = a-b
print(d)
Output: [[ 0 0 0]
[ 6 6 12]
[18 19 19]]
5. Multiplication():- This function is used to perform element-wise matrix multiplication.
e = a*b
print(e)
Output: [[ 20 23 17]
[110 133 97]
[200 243 177]]
6. Division():- This function is used to perform element-wise matrix division.
f = a/b
print(f)
Output: [[ 1. 1. 1. ]
[ 2.2 2. 13. ]
[ 7. 7.33333333 5.75 ]]
Also, Read Python Libraries
7. Transpose of a Matrix:- This function is used to transpose a certain matrix.
g = np.transpose(a)
print(g)
Output: [[ 1 11 21]
[ 2 12 22]
[ 3 13 23]]
8. Inverse of a Matrix:- This function is used to perfrom inverse operation on a matrix.
h = a.I
print(h)
Output: [[ 2.81474977e+14 -5.62949953e+14 2.81474977e+14]
[-5.62949953e+14 1.12589991e+15 -5.62949953e+14]
[ 2.81474977e+14 -5.62949953e+14 2.81474977e+14]]
Also, Read Introduction to Python Programming
9. Creation of zeros Matrix:- Create a matrix filled of zeroes with the use of np.zeros
i = np.zeros([3,3])
print(i)
Output: [[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
10. Creation of ones Matrix:- Create a matrix filled of ones with the use of np.ones
j = np.ones([3,3])
print(j)
Output: [[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
11. Determinant of a Matrix:- This function is used to return Determinant of a Matrix.
k = np.matrix([[1,3],[5,8]])
print(k)
Output: [[1 3]
[5 8]]
l = np.linalg.det(k)
print(l)
Output: -6.999999999999999
Also, Read Introduction to Python Programming
12. Vector Product of a Matrix:- This function is used to perform Vector Product of a Matrix
q = np.array([[1,5],[3,9]])
print(q)
Output: [[1 5]
[3 9]]
r = np.array([[5,8],[9,4]])
print(r)
Output: [[5 8]
[9 4]]
s = np.vdot(q,r)
print(s)
Output: 108
13. Dot():-This function is used to perform matrix multiplication, rather than element-wise matrix multiplication.
m = np.matrix([[1,5],[3,9]])
print(m)
Output: [[1 5]
[3 9]]
n = np.matrix([[5,8],[9,4]])
print(n)
Output: [[5 8]
[9 4]]
z = np.dot(m,n)
print(z)
Output: [[50 28]
[96 60]