Python 3 Script for GH: error in converting numpy eigenvectors (Rhino8)

Hi everyone.
I’m nwew to Python so I’m sorry if the question may be silly, but I’m really doing my best.

I’m trying to create a simple script that uses numpy in Python 3 script in GH.
I want to extract the eigenvectors of a 2x2 symmetrical matrix, but GH doesn’t recognize the output as a list of vectors. I think it sees it as a nested list somehow.

I attach the script:

import numpy as np

# Create lists
N1 = []
N2 = []
n1 = []
n2 = []

# Assuming N11, N12, and N22 are already defined lists
for i in range(len(N11)):
    # Define 2x2 matrices
    N = np.array([[N11[i], N12[i]],
                  [N12[i], N22[i]]])
    
    # Evaluate eigenvalues and eigenvectors
    eigenvalues, eigenvectors = np.linalg.eig(N)

    # Append eigenvalues to lists
    N1.append(eigenvalues[0].real)
    N2.append(eigenvalues[1].real)
    
    # Append eigenvectors to lists
    n1.append(eigenvectors[:, 0].real.tolist())
    n2.append(eigenvectors[:, 1].real.tolist())

the error that occurs is:

System.Collections.Generic.List’1[System-Object]

instead of a list of vectors.

Do you maybe know how to solve the issue? I know I can extract the components and create vectors then, but I would like to know how to extract the list directly.

Thank you very much

change the category to ‘Scripting’ so someone can see it in there and hopefully you’ll get your answer

edit;
I’m away on my computer but can you try and see what data type is inside the primary list? eg. N1, N2, etc. if it is a list, flatten it and see if you can get the result that you want

The primary list is floats

eigenvalues.gh (7.5 KB)

A few side notes: There is a nice and easy analytical solution for a 2x2 matrix, which requires no numpy.

Thank you very much! I’m aware that I don’t need numpy to solve the eigenvalues problem for 2x2 tensors, but I wanted to understand the process of scripting so that I can solve more complex problems in the future.
Thanks again!

1 Like