Question: import numpy as np #from copy import copy def forward _ subs ( G , b ) : rows,cols = G . shape x =
import numpy as np
#from copy import copy
def forwardsubsGb:
rows,cols Gshape
x npzerosrowsdtype float
for i in rangerows:
Gx
for j in rangei:
Gx Gi j xj
xibi Gx Gi i
return x
def backsubsGb:
rows,cols Gshape
x npzerosrowsdtype float
for i in rangerows :
Gx
for j in rangei cols:
Gx Gi j xj
xibi Gx Gi i
returnx
def choleskyfactorA:
rows, cols Ashape
R npzerosrows cols dtypefloat
for row in rangerows:
for col in rangerow :
if row col:
# Diagonal elements
Rrow col npsqrtArow row npsumRrow :row
else:
# Offdiagonal elements
Rrow colArow col npsumRrow :col Rcol :col Rcol col
return R
def solvesystemcholeskyRb:
# Solve RT y b using forward substitution
y forwardsubsRT b
# Solve R x y using backward substitution
x backsubsR y
return x
def LUdecompA:
rows, cols Ashape
L npzerosrows cols dtypefloat
U Acopy
for i in rangerows:
Li i
for j in rangei rows:
factor Uj i Ui i
Lj i factor
Uj i: factor Ui i:
returnLU
def solvesystemLULUb:
x npzerosbshape
y forwardsubsL b # Forward substitution to solve Ly b
x backsubsU y # Backward substitution to solve Ux y
returnx
Use the forward substitution and back substitution functions above
define a function
name : PLUdecomp
inputs : A D numpy array
output : P d numpy array L d numpy array U d numpy array
note : the function should work for square matrices of any size
note : use LU decomposition with pivoting
note : PA LU
def PLUdecompA:
# add code here
returnPLU
define a function
name : solvesystemPLU
inputs : P d numpy array L D numpy array U D numpy array b d numpy array
output : x d numpy array
note : the function should work for square matrices of any size
note : the function should solve the equation Axb where
PALU is the LU decomposition with pivoting of A
note : call the forward subs and back subs functions to solve
def solvesystemPLUPLUb:
x npzerosbshape
# add code here
returnx
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
