 # Python Program to Check Whether a Matrix is a Scalar or Not

In the previous article, we have discussed Python Program to Find the Sum of a Lower Triangular Matrix

Given a matrix and the task is to check if the given matrix is a scalar matrix or not in Python.

What is a matrix:

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

Example: Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.

Scalar Matrix :

A scalar matrix is a square matrix with all of its primary diagonal members equal and all off-diagonal elements equal. It’s an identity matrix’s multiplicative constant.

Examples:

Example1:

Input:

Given Matrix :
5 0 0
0 5 0
0 0 5

Output:

The Matrix given is a Scalar Matrix.

Example2:

Input:

Given Matrix :
0 8
2 5

Output:

The Matrix given is not a Scalar Matrix.

## Program to Check Whether a Matrix is a Scalar or Not in Python

Below are the ways to check if the given matrix is a scalar matrix or not in Python:

### Method #1: Using For Loop (Static Input)

Approach:

• Give the matrix as static input and store it in a variable.
• Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
• Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
• Take a variable say tempo and initialize its value to zero.
• Loop till the given number of rows using the For loop.
• Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
• Check if the condition n is not equal to m and gvnmatrix[n][m] not equal to 0 using the if conditional statement where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
• If the statement is true, then make the value of tempo as 1.
• Break the statement.
• Check if the condition n is equal to m and gvnmatrix[n][m] not equal to gvnmatrix[n][m] using the if conditional statement where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
• If the statement is true, then make the value of tempo as 1.
• Break the statement and come out of the loop.
• Check if the value of tempo is equal to 1 using the if conditional statement.
• If the statement is true, then print “The Matrix given is not a Scalar Matrix.”
• Else print “The Matrix given is a Scalar Matrix.”
• The Exit of the Program.

Below is the implementation:

# Give the matrix as static input and store it in a variable.
mtrx = [[5, 0, 0], [0, 5, 0], [0, 0, 5]]
# Calculate the number of rows of the given matrix by
# calculating the length of the nested list using the len() function
# and store it in a variable mtrxrows.
mtrxrows = len(mtrx)
# Calculate the number of columns of the given matrix by
# calculating the length of the first list in the nested list
# using the len() function and store it in a variable mtrxcols.
mtrxcols = len(mtrx)
# Take a variable and initialize its value to zero.
tempo = 0
# To print all the elements of the given matrix.
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
# Inside the For loop, Iterate till the given number of columns using another
# Nested For loop(Inner For loop).
for m in range(mtrxcols):
# Check if the condition n is not equal to m and gvnmatrix[n][m] not equal to 0 using the
# if conditional statement where n is the iterator value of the parent For loop and m
# is the iterator value of the inner For loop.
if n != m and mtrx[n][m] != 0:
# If the statement is true, then make the value of tempo as 1.
tempo = 1
# Break the statement.
break
# Check if the condition n is equal to m and gvnmatrix[n][m] not equal to gvnmatrix[n][m]
# using the if conditional statement where n is the iterator value of the parent For loop
# and m is the iterator value of the inner For loop.
if n == m and mtrx[n][m] != mtrx[n][m]:
# If the statement is true, then make the value of tempo as 1.
tempo = 1
# Break the statement.
break
# Check if the value of tempo is equal to 1 using the if conditional statement.
if tempo == 1:
# If the statement is true, then print "The Matrix given is not a Scalar Matrix."
print("The Matrix given is not a Scalar Matrix.")
else:
# Else print "The Matrix given is a Scalar Matrix."
print("The Matrix given is a Scalar Matrix.")


Output:

The Matrix given is a Scalar Matrix.

### Method #2: Using For loop (User Input)

Approach:

• Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
• Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
• Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
• Loop till the given number of rows using the For loop
• Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
• Add the above row elements list to gvnmatrix using the append() function.
• Take a variable say tempo and initialize its value to zero.
• Loop till the given number of rows using the For loop.
• Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
• Check if the condition n is not equal to m and gvnmatrix[n][m] not equal to 0 using the if conditional statement where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
• If the statement is true, then make the value of tempo as 1.
• Break the statement.
• Check if the condition n is equal to m and gvnmatrix[n][m] not equal to gvnmatrix[n][m] using the if conditional statement where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop.
• If the statement is true, then make the value of tempo as 1.
• Break the statement and come out of the loop.
• Check if the value of tempo is equal to 1 using the if conditional statement.
• If the statement is true, then print “The Matrix given is not a Scalar Matrix.”
• Else print “The Matrix given is a Scalar Matrix.”
• The Exit of the Program.

Below is the implementation:

# Give the number of rows of the matrix as user input using the int(input()) function
# and store it in a variable.
mtrxrows = int(input('Enter some random number of rows of the matrix = '))
# Give the number of columns of the matrix as user input using the int(input()) function
# and store it in another variable.
mtrxcols = int(input('Enter some random number of columns of the matrix = '))
# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
mtrx = []
# Loop till the given number of rows using the For loop
for n in range(mtrxrows):
# Inside the For loop, Give all the row elements of the given Matrix as a list using
# the list(),map(),int(),split() functions and store it in a variable.
l = list(map(int, input(
'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))
# Add the above row elements list to gvnmatrix using the append() function.

mtrx.append(l)

# Take a variable and initialize its value to zero.
tempo = 0
# To print all the elements of the given matrix.
# Loop till the given number of rows using the For loop.
for n in range(mtrxrows):
# Inside the For loop, Iterate till the given number of columns using another
# Nested For loop(Inner For loop).
for m in range(mtrxcols):
# Check if the condition n is not equal to m and gvnmatrix[n][m] not equal to 0 using the
# if conditional statement where n is the iterator value of the parent For loop and m
# is the iterator value of the inner For loop.
if n != m and mtrx[n][m] != 0:
# If the statement is true, then make the value of tempo as 1.
tempo = 1
# Break the statement.
break
# Check if the condition n is equal to m and gvnmatrix[n][m] not equal to gvnmatrix[n][m]
# using the if conditional statement where n is the iterator value of the parent For loop
# and m is the iterator value of the inner For loop.
if n == m and mtrx[n][m] != mtrx[n][m]:
# If the statement is true, then make the value of tempo as 1.
tempo = 1
# Break the statement.
break
# Check if the value of tempo is equal to 1 using the if conditional statement.
if tempo == 1:
# If the statement is true, then print "The Matrix given is not a Scalar Matrix."
print("The Matrix given is not a Scalar Matrix.")
else:
# Else print "The Matrix given is a Scalar Matrix."
print("The Matrix given is a Scalar Matrix.")



Output:

Enter some random number of rows of the matrix = 2
Enter some random number of columns of the matrix = 2
Enter {2} elements of row {1} separated by spaces = 0 8
Enter {2} elements of row {2} separated by spaces = 2 5
The Matrix given is not a Scalar Matrix.

The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners. Using them you can develop code on your own and master coding skills.