[Python] SVD using numpy not returning anything, terminates the program instead [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have a linear algebra exercise wherein I have to find the inverse of a given matrix using SVD. I made this code utilizing numpy. However, when executed, the code pauses for a long time before terminating early--I know it's terminating early since it does not reach line 11 where I try to print the S values. It also does not reach line 17.

    import numpy as np

    A = np.array([[[6,0,2],[2,6,5],[9,7,1]],
    [[1,6,4],[5,4,9],[6,9,2]],
    [[3,5,9],[5,3,7],[8,3,3]]])

    ans = np.zeros((A.shape[0], A.shape[1], 3)) # Where A is 3x3x3 array

    def svd_inverse(mat):
    U, S, V = np.linalg.svd(mat)
    print(S) # to test whether or not the is reaching this line
    return V @ (S**(-1)) @ U.transpose()

    for k in range(3):
    ans[:,:,k] = svd_inverse(A[:,:,k])

    print(f"Inverse: {ans}")


    I already tried solving this using MATLAB and I successfully got the results as seen here in a screenshot. With the live code screenshot here. I already tried searching other forums and posts, reading the documentation, but to no avail. Can someone help me? I'm stuck here for days. Thanks in advance.

    Continue reading...

Compartilhe esta Página