001_BasicTypes.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from iglhelpers import *
  2. ############# Dense Matrix Types #############
  3. # Create a numpy dense array
  4. # 2 types are supported by the wrappers: float64 and int64
  5. dense_matrix = np.array( [ (1,2,3), (4,5,6) , (7,8,9) ], dtype='float64')
  6. # libigl wrappers uses Eigen as a matrix type, you can easily convert between numpy and Eigen using
  7. # the helper function p2e. This operation duplicates the data.
  8. dense_matrix_eigen = p2e(dense_matrix)
  9. # The Eigen wrappers allows you to do operations directly on this matrix,
  10. # without having to convert back to numpy
  11. dense_matrix_eigen_2 = dense_matrix_eigen * dense_matrix_eigen
  12. # You can also inspect the data without converting it ...
  13. print("Eigen Matrix: \n", dense_matrix_eigen_2, "\n", sep='')
  14. # and access single elements
  15. print("Eigen Matrix(0,0): ", dense_matrix_eigen_2[0,0], "\n")
  16. # To convert it back to a numpy array, use the helper function e2p
  17. dense_matrix_2 = e2p(dense_matrix_eigen_2)
  18. print("Numpy Array: \n", dense_matrix_2, "\n", sep='')
  19. ############# Sparse Matrix Types #############
  20. # Sparse matrices are handled in a very similar way
  21. # 2 types are supported by the wrappers: float64 and int64
  22. sparse_matrix = sparse.rand(10, 10, 0.1)
  23. # To convert to the eigen forma use p2e
  24. sparse_matrix_eigen = p2e(sparse_matrix)
  25. # They can directly be used plotted or used in computations
  26. print("Sparse matrix Eigen: ", sparse_matrix_eigen, sep='')
  27. # And converted back with e2p
  28. sparse_matrix_2 = e2p(sparse_matrix_eigen)
  29. print("Sparse matrix Numpy: ", sparse_matrix_2.todense(), sep='')