Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import numpy as np def eliminateCol ( A , k = 0 ) : # guarantee that A is float type if A . dtype.kind

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 leftmost k-1 entries are all zero
while i0< dim0:
if not A[i0,0:k].any():
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
# Partial Pivoting: ensures that the largest element of the column is placed on the diagonal
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],]
# row i0 is the pivot row and A[j, k] is zero for i0< j <= i
i = i +1
while i < dim0:
if A[i, k]!=0:
quot = A[i, k]/ A[i0, k]
A[i, :]= A[i, :]- quot * A[i0, :]
oper[i, i0]=-quot
i +=1
return swap, oper
def eliminateRow(A, k=-1):
# 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)
# k < dim0
k = k%dim0
# if the row k is zero column, return
if k ==0 or A[k,:].any()== False: return oper
# find a nonzero element A[k,j] to use as a pivot
j =0;
while j < dim1:
if A[k,j]!=0: break
j+=1
i =k-1
while i >=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 get_echelon_form(A):
# Perform Gaussian elimination on matrix A to achieve Row Echelon Form
for k in range(min(A.shape[0], A.shape[1])):
# Find the k-th pivot
i_max = max(range(k, A.shape[0]), key=lambda i: abs(A[i, k]))
if A[i_max, k]==0:
continue # Skip if the column is zero
# Swap rows
A[[k, i_max]]= A[[i_max, k]]
# Make all rows below this one 0 in the current column
for i in range(k +1, A.shape[0]):
f = A[i, k]/ A[k, k]
A[i, k:]-= f * A[k, k:]
return A
def get_reduced_echelon_form (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
# Example Usage:
C = np.array([[0.02,0.01,0.0,0.0,0.02],
[1.0,2.0,1.0,0.0,1.0],
[0.0,1.0,2.0,1.0,4.0],
[0.0,0.0,100.0,200.0,800.0]], dtype=float)
B = np.array([[1.0,3.0,2.0,5.0],
[2.0,4.0,-6.0,-4.0],
[1.0,5.0,3.0,10.0]], dtype=float)
A = np.array([[1.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)
echelon = get_echelon_form(A.copy())
reduced = get_reduced_echelon_form(A.copy())
print(A)
print(echelon)
print(reduced)

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

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

What are the major steps in the probate of an estate?

Answered: 1 week ago

Question

What are the attributes of a technical decision?

Answered: 1 week ago

Question

How do the two components of this theory work together?

Answered: 1 week ago

Question

Understand links between the university business model and HRM.

Answered: 1 week ago