Page 1 of 1

import math class Matrix: def __init__(self, row=[]): # FIXME: Add necessary parameters and default values self

Posted: Sun May 15, 2022 1:18 pm
by answerhappygod
import math
class Matrix:
def __init__(self, row=[]): # FIXME: Add
necessary parameters and default values
self.rowsp = row
self.colsp = []
for i in range(0,
len(self.rowsp[0])):
self.colsp.append([row
for row in self.rowsp])
def set_col(self, j, u):
if len(u) != len(self.colsp[0]):
raise
ValueError("Incompatible column length.")
else:
self.colsp[j-1] = u
self.rowsp = []
for i in range(0,
len(self.colsp[0])):

self.rowsp.append([col for col in self.colsp])
def set_row(self, i, v):
if len(v) != len(self.rowsp[0]):
raise
ValueError("Incompatible row length.")
else:
self.rowsp[i-1] = v
self.colsp = []
for i in range(0,
len(self.rowsp[0])):

self.colsp.append([row for row in self.rowsp])
def set_entry(self, i, j, x):
self.rowsp[i-1][j-1] = x
self.colsp = []
for i in range(0,
len(self.rowsp[0])):
self.colsp.append([row
for row in self.rowsp])
def get_col(self, j):
return self.colsp[j-1]
def get_row(self, i):
return self.rowsp[i-1]
def get_entry(self, i, j):
return self.rowsp[j]
def col_space(self):
return self.colsp
def row_space(self):
return self.rowsp
def get_diag(self, k):
diag = []
l = len(self.rowsp[0])
if k == 0:
for i in
range(len(self.rowsp)):
for j in
range(len(self.rowsp)):

if i == j:

diag.append(self.rowsp[j])
return diag
elif k > 0:
for i in range(0,
len(self.rowsp)):
for j in
range(k, len(self.colsp)):

if i + k == j:

diag.append(self.rowsp[j])
return diag
else:
for i in range(-k,
len(self.rowsp)):
for j in
range(0, len(self.colsp)):

if i + k == j:

diag.append(self.rowsp[j])
return diag
def rank(self):
# TODO
r = 0
z = 0
if type(self.rowsp) == Matrix:
# goes through each
row
for i in
range(len(self.rowsp)):
check_row =
[]
count =
0
# adds each
element in row to temp list
for j in
range(len(self.rowsp[0])):

check_row.append(self.rowsp[j])

# checks each element in lest if its a zero

for k in check_row:

# if there's non zeros then adds to
count

if k != 0:

count += 1
# if
there's count then that means the rank increases by 1
if count
> 0:

r += 1
'''if
all(j == 0 for j in i):

z += 1
else:

r += 1'''
return r
def __add__(self, other):
allSums = []
if (len(self.rowsp) !=
len(other.rowsp)) or (len(self.colsp) != len(other.colsp)):
raise ValueError
else:
for i in
range(len(self.rowsp)):
row =
[]
for j in
range(len(self.rowsp[0])):

row.append(self.rowsp[j] + other.rowsp[i][j])

allSums.append(row)
return Matrix(allSums)
def __sub__(self, other):
dif = []
if (len(self.rowsp) !=
len(other.rowsp)) or (len(self.colsp) != len(other.colsp)):
raise ValueError
else:
for i in
range(len(self.rowsp)):
row =
[]
for j in
range(len(self.rowsp[0])):

row.append(self.rowsp[i][j] - other.rowsp[i][j])

dif.append(row)
return Matrix(dif)
def __mul__(self, other):
if type(other) == float or type(other)
== int:
fprod = []
for i in
range(len(self.rowsp)):
row =
[]
for j in
range(len(self.rowsp[0])):

row.append(self.rowsp[i][j] * other)

fprod.append(row)
return Matrix(fprod)
elif type(other) == Matrix:
mprod = [] # row
space of product of matrices
for i in
range(len(self.rowsp)):
row_i = []
# new row
for j in
range(len(other.colsp)):

prod = 0

for k in range(len(self.colsp)):

prod += self.rowsp[i][k] *
other.colsp[j][k]

row_i.append(prod)

mprod.append(row_i)
return Matrix(mprod)
elif type(other) == Vec:
if len(self.colsp) ==
len(other.elements):
vprod =
[]
for m in
range(len(self.rowsp)):
sumofprod = 0

for n in range(len(other.elements)):

sumofprod += (self.rowsp[m][n] *
other.elements[n])

vprod.append(sumofprod)
return
Vec(vprod)
else:
print("ERROR: Unsupported
Type.")
return
def __rmul__(self, other):
if type(other) == float or type(other)
== int:
fprod = []
for i in
range(len(self.rowsp)):
row =
[]
for j in
range(len(self.rowsp[0])):

row.append(other * self.rowsp[i][j])

fprod.append(row)
return
Matrix(fprod)
else:
print("ERROR: Unsupported
Type.")
return
def __str__(self):
"""prints the rows and columns in
matrix form """
matrix = ""
for i in range(0,
len(self.rowsp)):
matrix +=
str(self.rowsp[i]) + "\n"
return matrix
def __eq__(self, other):
"""overloads the == operator to return
True if
two Matrix objects have the same row
space and column space"""
this_rows = self.row_space()
other_rows = other.row_space()
this_cols = self.col_space()
other_cols = other.col_space()
return this_rows == other_rows and
this_cols == other_cols
def __req__(self, other):
"""overloads the == operator to return
True if
two Matrix objects have the same row
space and column space"""
this_rows = self.row_space()
other_rows = other.row_space()
this_cols = self.col_space()
other_cols = other.col_space()
return this_rows == other_rows and
this_cols == other_cols
Your Task:
Assuming 𝐴∈ℝ𝑛×𝑛A∈Rn×n is a Matrix object,
and 𝑏→∈ℝ𝑛b→∈Rn is a Vec object, implement a
function solve_qr(A, b) that uses the QR-factorization
of 𝐴A to compute and return the solution to the
system 𝐴𝑥→=𝑏→Ax→=b→.
Hints: