I have 3 2x2 Matrices, P1, P2, and P3, which are populated with randomly generated integers. I want to make sure that these matrices are positive definite (i.e. All eigenvalues are all greater than 0). My code is below.
P1 = np.random.randint(10, size=(m,n))
P2 = np.random.randint(10, size=(m,n))
P3 = np.random.randint(10, size=(m,n))
lambda1 = np.linalg.eigvals(P1)
lambda2 = np.linalg.eigvals(P2)
lambda3 = np.linalg.eigvals(P3)
for i in lambda1:
if (i <= 0): P1 = np.random.randint(10, size=(m,n))
for i in lambda2:
if (i <= 0): P2 = np.random.randint(10, size=(m,n))
for i in lambda3:
if (i <= 0): P3 = np.random.randint(10, size=(m,n))
print('Eigenvalue output to to verify that matrices are positive definite:\n')
print(u'\u03BB(P\u2081) = ' + str(np.linalg.eigvals(P1)))
print(u'\u03BB(P\u2082) = ' + str(np.linalg.eigvals(P2)))
print(u'\u03BB(P\u2083) = ' + str(np.linalg.eigvals(P3)))
Right now, the if statement will pretty much re-generate the matrix once or twice if the eigenvalues are not positive, but it will not verify that the eigenvalues are always positive. My first guess was to nest a while loop within the for loop, but I could not figure out a way to get that to work, and I'm unsure if that is the most efficient way.