Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please provide a solution that does not exist on chegg because all of it are false and i voted downvote for all answers. Please provide

Please provide a solution that does not exist on chegg because all of it are false and i voted downvote for all answers. Please provide the code without changing my code if you dont find any bugs and shortcoming. Please consider your code performs successfully for not only mentioned 2matrix. This is my method to use partial pivoting
max_index =np.argmax(np.abs(A[i0:,k]))+i0
A[[i0,max_index],]=A[[max_index, i0],]; swap[[i0,max_index],]=swap[[max_index, i0],]
replace this code with
if i !=i0: A[[i0,i],]=A[[i,i0],];swap[[i0,i],]=swap[[i,i0],]
then please read the problem in the attached image. Please dont provide complicated and longer codes. I just need this code performs well for both partial pivoting and like the code i given in the attached image. Just need to implement partial pivoting for only required (not only given matrix)conditions.
My code:
import numpy as np
def eliminateCol(A, k=0):
# guarantee that A is float type
if A.dtype.kind !='f' and A.dtype.kind !='c': return None
# load dimensions
dim0,dim1= np.shape(A)
# initialize outputs
swap = np.identity(dim0,dtype=float)
oper = np.identity(dim0,dtype=float)
# k dim1
k = k%dim1
# if the column k is zero column, return
if A[:,k].any()== False: return swap,oper
i0=0
if k !=0:
# find a row i0 whose left most k-1 entries are all zero
while i0 dim0:
if A[i0,0:k].any()== False: break
i0+=1
# find a nonzero element A[i,k] to use as a pivot
i = i0;
while i dim0:
if A[i,k]!=0: break
i+=1
if i == dim0: return swap,oper
# swap the row i with the row i0
if i != i0: A[[i0,i],]= A[[i,i0],];swap[[i0,i],]= swap[[i,i0],]
# row i0 is the pivot row and A[j,k] is zero for i0=0:
if A[i,j]!=0:
quot = A[i,j]/ A[k,j]
A[i,:]= A[i,:]- quot*A[k,:]
oper[i,k]=-quot
i-=1
return oper
def eliminateScale(A):
# guarantee that A is float type
if A.dtype.kind !='f' and A.dtype.kind !='c': return None
# load dimensions
dim0,dim1= np.shape(A)
# initialize outputs
oper = np.identity(dim0,dtype=float)
# find pivots
i =0
while i dim0:
j =0
while j dim1:
if A[i,j]==0: j+=1; continue
else:
oper[i,i]=1/A[i,j]
A[i,j:]= A[i,j:]/A[i,j]
break
j+=1
i+=1
return oper
def echelon_to_rref (A):
# Perform Gaussian elimination on matrix A
for k in range(A.shape[1]):
eliminateCol(A, k)
eliminateRow(A, k)
# Finally, scale the matrix
eliminateScale(A)
return A
def get_echelon_form(A):
dim0, dim1= np.shape(A)
for k in range(min(dim0, dim1)):
# Eliminate below diagonal in column k
swap, oper = eliminateCol(A, k)
return A
# Example Usage:
A = np.array([[0.0,-1.0,2.0,8.0],
[0.0,0.0,-1.0,-11.0],
[0.0,2.0,-1.0,-3.0]], dtype=float)
B = np.array([[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12]], dtype=float)
echelon = get_echelon_form(B.copy())
reduced = echelon_to_rref(B.copy())
print(A)
print(echelon)
print(reduced)
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Relational Database Design

Authors: Candace C. Fleming, Barbara Von Halle

1st Edition

0201114348, 978-0201114348

More Books

Students also viewed these Databases questions