Question: Please generalize this code to work for a matrix of any dimension: def isSingular ( A ) : B = np . array ( A
Please generalize this code to work for a matrix of any dimension:
def isSingularA :
B nparrayAdtypenpfloat # Lets make a copy as we are going to change this matrix
try:
alterRowZeroB
printB
alterRowOneB
printB
alterRowTwoB
printB
alterRowThreeB
printB
except ItIsSingular:
return True
return False
#this is our error class. no need to make any changes here
class ItIsSingularException: pass
#Remember that for the first row, all we need is the first element to be
#We can do it by dividing the whole row by the value of the element ie A
#However if A is zero, then we are in trouble.. so need to test for that.
#if that is the case we can add the lower row one by one to make sure we have a non zero first element.
# Do not edit this.
def alterRowZeroA :
if A :
A A A
if A :
A A A
if A :
A A A
if A :
raise ItIsSingular
A A A
return A
#We need to make the element below the diagonal element zero ie A
# Next make A
# We'll divide the row by the value of A
# Do the zero test again
# There is no need to edit this function.
def alterRowOneA :
A A A A
if A :
A A A
A A A A
if A :
A A A
A A A A
if A :
raise ItIsSingular
A A A
return A
# Complete this function
def alterRowTwoA :
# Make the required elements in row two to zero hint: there are of them
A A A A
A A A A
# zero test
if A:
A A A
A A A A
# make sub diagonal elements zero
if A:
raise ItIsSingular
# Finally set the diagonal element to one by dividing the whole row by that element.
A A A
return A
# You should also complete this function
# Follow the instructions inside the function at each comment.
def alterRowThreeA :
# Insert code below to set the subdiagonal elements of row three to zero.
#Setting elements in row three to zero
A A A A
A A A A
A A A A
#subdiagonal elements zero
if A:
raise ItIsSingular
#Subdiagonal elements to one
A A A
return A
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
