Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THE CODE MUST NOT USE NUMPY AT ALL THANK YOU. In Python3 I need to write a code

ID: 3910606 • Letter: T

Question

THE CODE MUST NOT USE NUMPY AT ALL THANK YOU.
In Python3 I need to write a code that takes a matrix as it's argument and computes and prints the QR factorization. I need the code to be written using nested for loops. The only built in functions I'm trying to use are len, range, abs, .append, and print. Please show step by step will rate. Thank you in advance. THE CODE MUST NOT USE NUMPY AT ALL THANK YOU.
In Python3 I need to write a code that takes a matrix as it's argument and computes and prints the QR factorization. I need the code to be written using nested for loops. The only built in functions I'm trying to use are len, range, abs, .append, and print. Please show step by step will rate. Thank you in advance.

Explanation / Answer

Answer: See the code below:

--------------------------------------------

import math

#sample matrix
A = [[80,75,85],
     [75,80,75],
     [80,80,80]]

#function to find QR factorization of a square matrix
#parameter - matrix to be factorized
def findQRFactorization(A):
    #check if A is a matrix
    if any(isinstance(i,list) for i in A):
        print("Is a matrix")
        #number of rows and columns in A
        rows = len(A)
        cols = len(A[0])
        print("rows:",rows," cols:",cols)
        #check if A is square matrix
        if rows != cols:
            print("Not a square matrix. Aborting...")
        else:
            print("A square matrix. proceeding...")
            #create an Identiry matrix of size rows x cols
            I = [[0]*cols for i in range(rows)]
            for i in range(rows):
                I[i][i] = 1
            #print(I)
            #calculation of QR factorization based on householder reflection method
            #QR factorization represents A as A = QR where Q is a orthogonal matrix
            #while R is a upper triangular matrix.
            #Initialize Q and R
            Q = [[0.0] * cols for i in range(rows)]
            R = A
            #print("Q:",Q)
            #print("R:",R)
            #loop to perform QR factorization
            for k in range(rows-1):
                #calculate x, e
                x = [row[k] for row in R[k:]]
                e = [row[k] for row in I[k:]]
                #calculate norm of x
                norm_x = math.sqrt(sum([i**2 for i in x]))
                #print("norm x:",norm_x)
                #calculate alpha
                sign = lambda x: (1, -1)[x < 0]
                alpha = -sign(x[0])*norm_x
                #print('alpha:',alpha)
                #calculate minor matrix of u and v
                u = list(map(lambda i,j: i + alpha * j, x, e))
                norm_u = math.sqrt(sum([i**2 for i in u]))
                v = list(map(lambda i: i/norm_u, u))
              
                #calculate Q
                Q_minor = [ [float(i==j) - 2.0 * v[i] * v[j] for i in range(rows-k)] for j in range(cols-k) ]
              
                def Q_identity(Q_minor,i,j,k):
                    if i < k or j < k:
                        return float(i == j)
                    else:
                        return Q_minor[i-k][j-k]  
              
                Q_padded = [[Q_identity(Q_minor,i,j,k) for i in range(rows)] for j in range(cols)]
                #update Q and R
                def multiply(P,Q):
                    return [[sum(elemp * elemq for elemp, elemq in zip(rowp, colq)) for colq in zip(*Q)] for rowp in P]              
                if k == 0:
                    Q = Q_padded                  
                    R = multiply(Q_padded,A)
                else:
                    Q = multiply(Q_padded,Q)
                    R = multiply(Q_padded,R)
              
                #calculate transpose of Q
                Q_dash = [[Q[i][j] for i in range(cols)] for j in range(rows)]
            print("QR factorization of ",A)
            print("Q: ",Q_dash)
            print("R: ",R)
    else:
        print("Not a matrix. QR factorization not possible.")
  
#call function
findQRFactorization(A)

--------------------------------------------

Output:

------------------------------------------------

Is a matrix
rows: 3 cols: 3
A square matrix. proceeding...
QR factorization of [[80, 75, 85], [75, 80, 75], [80, 80, 80]]
Q: [[0.589367574947006, 0.6919905240406977, 0.41688724643357217], [0.552532101512818, -0.7217432372796153, 0.4168872464335772], [0.5893675749470058, -0.015356239091058876, -0.8077190399650512]]
R: [[135.7387196049823, 135.55454223781135, 138.68555747971732], [-3.895348486244323e-14, -7.068668806601609, 3.4599526202034525], [-2.0725219688989145e-14, 4.440892098500626e-16, 2.0844362321678425]]

-------------------------------------------------