Question: from helpers import gram _ schmidt from structures import Vec, Matrix import numpy as np import cmath # - - - - - - -
from helpers import gramschmidt
from structures import Vec, Matrix
import numpy as np
import cmath
# PROBLEM #
def qrsolveA: Matrix, b: Vec:
Solves the system of equations Ax b by using the
QR factorization of Matrix A
:param A: Matrix of coefficients of the system
:param b: Vec of constants
:return: Vec solution to the system
# Constructing U
# U should be the set of orthonormal vectors returned
# by applying GramSchmidt Process to the columns of A
U None # FIXME: Replace with the appropriate line
n lenU
# Constructing Q
# Q should be the matrix whose columns are the elements
# of the vector in set U
Q MatrixNone for j in rangen for i in rangen
for j in rangen:
pass # FIXME: Replace with the appropriate line
# Constructing R
R Matrix for j in rangen for i in rangen
for j in rangen:
for i in rangen:
if i j:
pass # FIXME: Replace with the appropriate line
# Constructing the solution vector x
bstar Qtranspose b
x None for i in rangen
# FIXME: find the components of the solution vector
# and replace them into elements of x
return Vecx
# PROBLEM #
def submatrixA: Matrix, i: int, j: int:
constructs the submatrix of an mxn Matrix A that
results from omitting the ith row and jth column;
i and j satisfy that i m and j n
:param A: Matrix object
:param i: int index of row to omit
:param j: int index of column to omit
:return: Matrix object representing the submatrix
m n Adim
pass # FIXME: Implement this function
# PROBLEM #
def determinantA: Matrix:
computes the determinant of square Matrix A;
Raises ValueError if A is not a square matrix.
:param A: Matrix object
:return: float value of determinant
m n Adim
if m n:
raise ValueError
fDeterminant is not defined for Matrix with dimension mxn Matrix must be square."
if n :
return None # FIXME: Return the correct value
elif n :
return None # FIXME: Return the correct value
else:
d
# FIXME: Update d so that it holds the determinant
# of the matrix. HINT: You should apply a
# recursive call to determinant
return d
# PROBLEM #
def eigenwrapperA: Matrix:
uses numpy.linalg.eig to create a dictionary with
eigenvalues of Matrix A as keys, and their corresponding
list of eigenvectors as values.
:param A: Matrix object
:return: Python dictionary where keys are eigenvalues of type 'float' or 'complex' and values eigenvectors of type 'Vec'
pass # FIXME: Implement this function
# PROBLEM #
def svdA: Matrix:
computes the singular value decomposition of Matrix A;
returns Matrix objects U Sigma, and V such that:
V is the Matrix whose columns are eigenvectors of
Atranspose A
Sigma is a diagonal Matrix of singular values of
Atranspose A appearing in descending order along
the main diagonal
U is the Matrix whose jth column uj satisfies
A vj sigmaj uj where sigmaj is the jth singular
value in decreasing order and vj is the jth column vector of V
A U Sigma Vtranspose
:param A: Matrix object
:return: tuple with Matrix objects; U Sigma, V
m n Adim
aTa Atranspose A
eigen eigenwrapperaTa
eigenvalues npsortcomplexlisteigenkeystolist::
# Constructing V
# V should be the mxm matrix whose columns
# are the eigenvectors of matrix Atranspose A
V MatrixNone for j in rangen for i in rangen
for j in range n :
pass # FIXME: Replace this with the lines that will
# correctly build the entries of V
# Constructing Sigma
# Sigma should be the mxn matrix of singular values.
singularvalues None # FIXME: Replace this so that singularvalues
# holds a list of singular values of A
# in decreasing order
Sigma Matrix for j in rangen for i in rangem
for i in range m :
pass # FIXME: Replace this with the lines that will correctly
# build the entries of Sigma
# Constructing U
# U should be the matrix whose jth column is given by
# A vj sj where vj is the jth eigenvector of Atranspose A
# and sj is the corresponding jth singular value
U MatrixNone for j in rangem for i in rangem
for j in range m :
pass # FIXME: Replace this with the lines that will
# correctly build the entries of U
return U Sigma, V
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
