Python Matrices numpy

To work with matrices, numpy provides a special object called matrix. In numpy, a matrix is a specialized 2D array that retains its 2D nature through operations.

We can create numpy matrices using the following syntax:

matrix-name = matrix(2D array or string)

It means, the matrix object receives a 2D array or a string that contains elements which can be converted into a matrix.

For example, we are taking a 2D array with 2 rows and 3 columns as:

arr = [[1, 2, 3], [4, 5, 6]]

This array can be converted into a matrix 'a' as:

a = matrix(arr)

Alternately, we can also pass the 2D array directly to matrix object as:

a = matrix([[1,2,3],[4,5,6]])

Another way of creating a matrix is by passing a string with elements to matrix object as:

str = '1 2; 3 4; 5 6'

b = matrix(str)

numpy matrix diagonal function

To retrieve the diagonal elements of a matrix, we can use diagonal() function as:

a = diagonal(matrix)

The diagonal() function returns a 1D array that contains diagonal elements of the original matrix.

To understand this function, we can take an example.

CopiedCopy Code
a = matrix('1 2 3; 4 5 6; 7 8 9')
						print(a)

The preceding lines of code will display the following output:

CopiedCopy Code

[[1 2 3]
 [4 5 6]
 [7 8 9]]
 

Consider the following code:

d = diagonal(a)

print(d)

numpy matrix maximum and minimum element

To know the maximum element, we can use max() method and to know the minimum element, we can use min() methods. These methods should be called using the matrix name.

big = a.max()

print(big)

9

small = a.min()

print(small)

1

numpy matrix sum and average of element

To find sum of all the elements in the matrix, we can use sum() method and to find average, we can use mean() method. These methods should be called using the matrix name.

a.sum()

45

a.mean()

5.0

Sort numpy matrix

numpy provides sort() function that sorts the matrix elements into ascending order.

sort(matrixname, axis)

CopiedCopy Code

m = matrix([[5, 4, 1], [2, 7, 0]]) 
print(m)
a=sort(m)
print(a)

numpy transpose matrix

Rewriting matrix rows into columns and vice versa is called 'transpose'. To find the transpose, we can use transpose() and getT() methods in numpy. These methods should be called using matrix name.

CopiedCopy Code
						
m = matrix('1 2 3; 4 5 6; 7 8 9')
print(m)
CopiedCopy Code

[[1 2 3]
 [4 5 6]
 [7 8 9]]
CopiedCopy Code

t = m.transpose() 
print(t)
CopiedCopy Code

[[1 4 7]
 [2 5 8]
 [3 6 9]]
 
CopiedCopy Code

t1=m.getT()
print(t1)
CopiedCopy Code

[[1 4 7]
 [2 5 8]
 [3 6 9]]

A Python program to accept a matrix from the keyboard and display its transpose matrix.

CopiedCopy Code

frofrom numpy import* 
r,c = [int(a) for a in input("Enter rows, cols: ").split()] 
str = input('Enter matrix elements:\n') 
x = reshape(matrix(str), (r, c)) 
print('The original matrix:') 
print(x) 
print('The transpose matrix:')
y = x.transpose() 
print(y)

numpy matrix addition

We can use arithmetic operators like +, - and/ to perform addition, subtraction and division operations on 2 matrices. Consider the following code snippet:

CopiedCopy Code

a = matrix('1 2 3; 4 5 6')
b = matrix('2 2 2; 1 -1 2') 
c=a+b
print(c)
d=a/b
print(d)

numpy matrix multiplication

A Python program to accept two matrices and find their product.

CopiedCopy Code

import sys 
from numpy import* 
r1, c1 = [int(a) for a in input("First matrix rows, cols: ").split()] 
r2, c2 = [int(a) for a in input("Second matrix rows, cols: ").split()] 
if c1!=r2: 
print('Multiplication is not possible') 
sys.exit() 
str1 = input('Enter first matrix elements:\n')
x = reshape(matrix(str1), (r1, c1)) 
str2 = input('Enter second matrix elements:\n') 
y = reshape(matrix(str2), (r2, c2)) 
print('The product matrix:') 
z = x * y 
print(z)