You can decompose a square or rectangular matrix (M) into an orthogonal matrix (Q) and an upper-triangular matrix (R) by applying QR decomposition. This can be expressed in the following formula:
M=QR
The following is an illustration of QR decomposition:
Let's see how it's implemented using numpy:
from numpy import arrayfrom numpy.linalg import qrM = np.random.randint(low=0, high=20, size=20).reshape(4,5)print(M)# Output[[14 6 0 19 3] [ 9 6 17 8 8] [ 4 13 17 4 4] [ 0 0 2 7 11]]Q, R = qr(M, 'complete')print("Q:n {}n".format(Q))print("R:n {}".format(R))# OutputQ: [[-0.81788873 0.28364908 -0.49345895 0.08425845] [-0.52578561 ...