import numpy
from numpy.linalg import LinAlgError
n = 3
A = numpy.random.randint(low=1, high=10, size=(n, n))
print("Matrix A: \n", A)
try:
Q, R = numpy.linalg.qr(A)
print("The Orthogonal Matrix Q: \n", Q)
except LinAlgError:
print("The QR Decomposition of The Matrix A Failed")
Here, A is a 3×3 square matrix that contains random integers. We are using the numpy.randopm.randint() function for generating random integers. The generated random integers will be within the range [low, high). And the size=(n, n) argument specifies that the created matrix will have n rows and n columns.
Now, we are using the qr() function from the numpy.linalg module to perform QR decomposition of the matrix A. Please note that if the QR decomposition of the matrix fails, the qr() function will raise a LinAlgError. Otherwise, we will get an orthogonal matrix Q and an upper triangular matrix R.
The output of the given program will be:
Matrix A: [[1 3 5] [3 5 4] [9 1 9]] The Orthogonal Matrix Q: [[-0.10482848 -0.5203556 -0.84749103] [-0.31448545 -0.79110973 0.5246373 ] [-0.94345635 0.32152053 -0.08071343]] The Upper Triangular Matrix R: [[ -9.53939201 -2.83036906 -10.2731914 ] [ 0. -5.1950949 -2.87253211] [ 0. 0. -2.86532681]]








































0 Comments