speye.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef IGL_SPEYE_H
  2. #define IGL_SPEYE_H
  3. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  4. #include <Eigen/Sparse>
  5. namespace igl
  6. {
  7. // Builds an m by n sparse identity matrix like matlab's speye function
  8. // Templates:
  9. // T should be a eigen sparse matrix primitive type like int or double
  10. // Inputs:
  11. // m number of rows
  12. // n number of cols
  13. // Outputs:
  14. // I m by n sparse matrix with 1's on the main diagonal
  15. template <typename T>
  16. inline void speye(const int n,const int m, Eigen::SparseMatrix<T> & I);
  17. // Builds an n by n sparse identity matrix like matlab's speye function
  18. // Templates:
  19. // T should be a eigen sparse matrix primitive type like int or double
  20. // Inputs:
  21. // n number of rows and cols
  22. // Outputs:
  23. // I n by n sparse matrix with 1's on the main diagonal
  24. template <typename T>
  25. inline void speye(const int n, Eigen::SparseMatrix<T> & I);
  26. }
  27. template <typename T>
  28. inline void igl::speye(const int m, const int n, Eigen::SparseMatrix<T> & I)
  29. {
  30. // size of diagonal
  31. int d = (m<n?m:n);
  32. I = Eigen::SparseMatrix<T>(m,n);
  33. I.reserve(d);
  34. for(int i = 0;i<d;i++)
  35. {
  36. I.insert(i,i) = 1.0;
  37. }
  38. I.finalize();
  39. }
  40. template <typename T>
  41. inline void igl::speye(const int n, Eigen::SparseMatrix<T> & I)
  42. {
  43. return igl::speye(n,n,I);
  44. }
  45. #endif