In Mathematics, a Matrix represents a rectangular array of elements arranged the in rows and columns. If a Matrix has only one row is called a Row Matrix, and a Matrix with only one column is called a Column Matrix. These are one dimensional(1D) arrays.
If a Matrix is having more than one row and one column is called a m x n Matrix, where 'm' is the number of rows and 'n' is the number of columns. These matrices can show using the 'numpy' 2D arrays.
Matrics in 'numpy':
The package 'numpy' provided an object called 'matrix' to work with the Matrices in Python. In numpy the matrix is a specialized 2D array that retains its 2D nature through operations.
Syntax of Matrix:
matrix-name = matrix(2D array or string)
Examples:
arr1 = [[1, 2, 3], [4, 5, 6]]
a = matrix(arr1)
print('The matrix(2x3) with 2 rows and 3 cols:\n', a)
# Creating a 3x2 Matrix from string
str1 = '1 3 ; 5 7 ; 9 11'
b = matrix(str1)
print('The matrix(3x2) from string with 3 rows and 2 cols:\n', b)
# Output:
The matrix(2x3) with 2 rows and 3 cols:
[[1 2 3]
[4 5 6]]
The matrix(3x2) from string with 3 rows and 2 cols:
[[ 1 3]
[ 5 7]
[ 9 11]]
The 3x3 matrix from string is: [[1 2 3]
[4 5 6]
[7 8 9]]
Now will look into the various functions and methods to process and perform operations on Matrices.
diagonal():
The diagonal() is used to find the diagonal elements of an arry.
a = matrix('1 2 3; 4 5 6; 7 8 9')
print('The 3x3 matrix from string is:', a)
d = diagonal(a)
print('The diagonal elements of matrix is:', d)
# Output:
The 3x3 matrix from string is: [[1 2 3]
[4 5 6]
[7 8 9]]
The diagonal elements of matrix is: [1 5 9]
min() , max():
The min() function is used find the minimum value element in the matrix, and max() function is used find the maximum value element in the matrix.
a = matrix('1 2 3; 4 5 6; 7 8 9')
print('The Min element of the Matrix is:', a.min())
print('The Max element of the Matrix is:', a.max())
# Output:
The Min element of the Matrix is: 1
The Max element of the Matrix is: 9
sum(), mean():
The sum() method is used to find the sum of elements of an array, and the mean() method is used find the average of the elements of an array.
print('The Sum of elements of the Matrix is:', a.sum())
print('The Average of elements of the Matrix is:', a.mean())
# Output:
The Sum of elements of the Matrix is: 45
The Average of elements of the Matrix is: 5.0
prod():
The pod() method is used to find the product of elements of a Matrix. The prod(0) will produce a matrix with products of elements of each column, and prod(1) will produce a matrix with products of elements of each row.
a = matrix(arange(12).reshape(3, 4))
print('The 3x4 Matrix is:\n', a)
cp = a.prod(0) # column wise product
rp = a.prod(1) # row wise product
print('The column wise product of matrix elements is:\n', cp)
print('The row wise product of matrix elements is:\n', rp)
# Output:
The 3x4 Matrix is: [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
The column wise product of matrix elements is:
[[ 0 45 120 231]]
The row wise product of matrix elements is:
[[ 0]
[ 840]
[7920]]
sort():
The 'numpy' provides the sort() function to sort the Matrix elements either by row or column in ascending order.
The general format of the sort function is:
sort(matrixname, axis)
If axis=1 then it sorts the elements in each row by ascending order.
If axis=0 then it sorts the elements in each column by ascending order.
If we not mention axis, then the default values will be taken as axis=1.
a = matrix('5 4 6; 3 2 1; 9 8 7')
b = sort(a, 0)
c = sort(a, 1)
print('The original matrix is:\n', a)
print('The matrix after column sorting(asc):\n', b)
print('The matrix after row sorting(asc):\n', c)
# Output:
The original matrix is:
[[5 4 6]
[3 2 1]
[9 8 7]]
The matrix after column sorting(asc):
[[3 2 1]
[5 4 6]
[9 8 7]]
The matrix after row sorting(asc):
[[4 5 6]
[1 2 3]
[7 8 9]]
transpose(), getT():
The transpose() or getT() method is used transpose a Matrix, which means changing the rows into columns and vice versa. If a m x n Matrix having m rows and n columns, after transpose it will have n rows and m columns as n x m Matrix.
a = matrix('5 4 6; 3 2 1; 9 8 7')
b = a.transpose()
c = a.getT()
print('The original matrix is:\n', a)
print('The transpose of a matrix is:\n', b)
print('The transpose of a matrix is:\n', c)
# Output:
The original matrix is:
[[5 4 6]
[3 2 1]
[9 8 7]]
The transpose of a matrix is:
[[5 3 9]
[4 2 8]
[6 1 7]]
The transpose of a matrix is:
[[5 3 9]
[4 2 8]
[6 1 7]]
Arithmetical operations on Matrices:
We can use the arithmetical operators like + , - , \ to perform various operations on Matrices.
These operators will perform the operations between the corresponding elements of the matrices, like a[r1] + b[r1] , a[c1]-b[c1], a[c3]/b[c3].
a = matrix('1 2 3; 4 5 6') # 2 by 3 matrix
b = matrix('2 1 -2; 1 2 -1') # 2 by 3 matrix
c = a + b
d = a - b
f = a / b
print('The matrix a is:\n', a)
print('The matrix b is:\n', b)
print('The addition of matrices is:\n', c)
print('The subtraction of matrices is:\n', d)
print('The division of matrices is:\n', f)
# Output:
The matrix a is:
[[1 2 3]
[4 5 6]]
The matrix b is:
[[ 2 1 -2]
[ 1 2 -1]]
The addition of matrices is:
[[3 3 1]
[5 7 5]]
The subtraction of matrices is:
[[-1 1 5]
[ 3 3 7]]
The division of matrices is:
[[ 0.5 2. -1.5]
[ 4. 2.5 -6. ]]
Note :
In case of +, - , / operations, the both the matrices should have same shape like a is (r2xc3) and b is (r2xc3) as above. Other wise it will produce a Value error like below:
ValueError: operands could not be broadcast together with shapes (2,3) (3,2)
The * operator is used perform the Matrix multiplication. Please note that, this operator(*) will not multiply the corresponding elements of the matrices to produce the new matrix like we have seen incase of other operators discussed above.
The multiplication operation between the matrices of same shape like a is (r2xc3) and b is (r2xc3) will produce a Value error like below:
ValueError: shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)
When we multiply(*) the two matrices with shape like a is (r2xc3) and b is (r3xc2), the result will be a (r2xc2) matrix.
a = matrix('1 2 3; 4 5 6') # r2 x c3 matrix
b = matrix('2 1; -2 1; 2 -1') # r3 x c2 matrix
e = a*b
print('The matrix a is:\n', a)
print('The matrix b is:\n', b)
print('The multiplication of matrices is:\n', e)
# Output:
The matrix a is:
[[1 2 3]
[4 5 6]]
The matrix b is:
[[ 2 1]
[-2 1]
[ 2 -1]]
The multiplication of matrices is:
[[ 4 0]
[10 3]]
Multiplication logic in Matrices:
First, take the first row of 'a' and first column of 'b'. Multiply each element of first row of 'a' with each element of first column in 'b'., and find their sum as :
(1x2)+(2x-2)+(3x2)=2-4+6=4 , the first element in first row of the result matrix.
Next, multiply each element in first row of 'a' with each element in second column of 'b' and find their sum as :
(1x1)+(2x1)+(3x-1)=1+2-3=0 , the second element in first row the result matrix.
Repeat this similar process for each row in 'a' with for each column in 'b'
#--------------------------------------------------------------Thanks--------------------------------------------------------------#
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.