Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please find bugs and shortcomings of attached code, then fix the code. import numpy as np def eliminateCol ( A , k = 0 )
Please find bugs and shortcomings of attached code, then fix the code. import numpy as np def eliminateColA k: # guarantee that A is float type if Adtype.kind f and Adtype.kind c: return None # load dimensions dimdim npshapeA # initialize outputs swap npidentitydimdtypefloat oper npidentitydimdtypefloat # k dim k kdim # if the column k is zero column, return if A:kany False: return swap,oper i if k : # find a row i whose leftmost k entries are all zero while i dim: if not Ai:kany: break i # find a nonzero element Ai k to use as a pivot i i while i dim: if Ai k: break i if i dim: return swap, oper # Partial Pivoting: ensures that the largest element of the column is placed on the diagonal maxindex npargmaxnpabsAi: k i Ai maxindex Amaxindex, i swapi maxindex swapmaxindex, i # row i is the pivot row and Aj k is zero for i j i i i while i dim: if Ai k: quot Ai k Ai k Ai : Ai : quot Ai : operi iquot i return swap, oper def eliminateRowA k: # guarantee that A is float type if Adtype.kind f and Adtype.kind c: return None # load dimensions dimdim npshapeA # initialize outputs oper npidentitydimdtypefloat # k dim k kdim # if the row k is zero column, return if k or Ak:any False: return oper # find a nonzero element Akj to use as a pivot j ; while j dim: if Akj: break j i k while i : if Aij: quot Aij Akj Ai: Ai: quotAk: operikquot i return oper def eliminateScaleA: # guarantee that A is float type if Adtype.kind f and Adtype.kind c: return None # load dimensions dimdim npshapeA # initialize outputs oper npidentitydimdtypefloat # find pivots i while i dim: j while j dim: if Aij: j; continue else: operiiAij Aij: Aij:Aij break j i return oper def getreducedechelonform A: # Perform Gaussian elimination on matrix A for k in rangeAshape: eliminateColA k eliminateRowA k # Finally, scale the matrix eliminateScaleA return A def getechelonformA: dim dim npshapeA for k in rangemindim dim: # Eliminate below diagonal in column k swap, oper eliminateColA k return A # Example Usage: C nparray dtypefloat B nparray dtypefloat A nparray dtypefloat echelon getechelonformAcopy reduced getreducedechelonformAcopy printA printechelon printreduced
Please find bugs and shortcomings of attached code, then fix the code.
import numpy as np
def eliminateColA k:
# guarantee that A is float type
if Adtype.kind f and Adtype.kind c: return None
# load dimensions
dimdim npshapeA
# initialize outputs
swap npidentitydimdtypefloat
oper npidentitydimdtypefloat
# k dim
k kdim
# if the column k is zero column, return
if A:kany False: return swap,oper
i
if k :
# find a row i whose leftmost k entries are all zero
while i dim:
if not Ai:kany:
break
i
# find a nonzero element Ai k to use as a pivot
i i
while i dim:
if Ai k:
break
i
if i dim:
return swap, oper
# Partial Pivoting: ensures that the largest element of the column is placed on the diagonal
maxindex npargmaxnpabsAi: k i
Ai maxindex Amaxindex, i
swapi maxindex swapmaxindex, i
# row i is the pivot row and Aj k is zero for i j i
i i
while i dim:
if Ai k:
quot Ai k Ai k
Ai : Ai : quot Ai :
operi iquot
i
return swap, oper
def eliminateRowA k:
# guarantee that A is float type
if Adtype.kind f and Adtype.kind c: return None
# load dimensions
dimdim npshapeA
# initialize outputs
oper npidentitydimdtypefloat
# k dim
k kdim
# if the row k is zero column, return
if k or Ak:any False: return oper
# find a nonzero element Akj to use as a pivot
j ;
while j dim:
if Akj: break
j
i k
while i :
if Aij:
quot Aij Akj
Ai: Ai: quotAk:
operikquot
i
return oper
def eliminateScaleA:
# guarantee that A is float type
if Adtype.kind f and Adtype.kind c: return None
# load dimensions
dimdim npshapeA
# initialize outputs
oper npidentitydimdtypefloat
# find pivots
i
while i dim:
j
while j dim:
if Aij: j; continue
else:
operiiAij
Aij: Aij:Aij
break
j
i
return oper
def getreducedechelonform A:
# Perform Gaussian elimination on matrix A
for k in rangeAshape:
eliminateColA k
eliminateRowA k
# Finally, scale the matrix
eliminateScaleA
return A
def getechelonformA:
dim dim npshapeA
for k in rangemindim dim:
# Eliminate below diagonal in column k
swap, oper eliminateColA k
return A
# Example Usage:
C nparray
dtypefloat
B nparray
dtypefloat
A nparray
dtypefloat
echelon getechelonformAcopy
reduced getreducedechelonformAcopy
printA
printechelon
printreduced
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started