speye.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // Implementation
  28. template <typename T>
  29. inline void igl::speye(const int m, const int n, Eigen::SparseMatrix<T> & I)
  30. {
  31. // size of diagonal
  32. int d = (m<n?m:n);
  33. I = Eigen::SparseMatrix<T>(m,n);
  34. I.reserve(d);
  35. for(int i = 0;i<d;i++)
  36. {
  37. I.insert(i,i) = 1.0;
  38. }
  39. I.finalize();
  40. }
  41. template <typename T>
  42. inline void igl::speye(const int n, Eigen::SparseMatrix<T> & I)
  43. {
  44. return igl::speye(n,n,I);
  45. }
  46. #endif