diag.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "diag.h"
  2. #include "verbose.h"
  3. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  4. #include <iostream>
  5. #include <unsupported/Eigen/SparseExtra>
  6. template <typename T>
  7. IGL_INLINE void igl::diag(
  8. const Eigen::SparseMatrix<T>& X,
  9. Eigen::SparseVector<T>& V)
  10. {
  11. assert(false && "Just call X.diagonal().sparseView() directly");
  12. V = X.diagonal().sparseView();
  13. //// Get size of input
  14. //int m = X.rows();
  15. //int n = X.cols();
  16. //V = Eigen::SparseVector<T>((m>n?n:m));
  17. //V.reserve(V.size());
  18. //// Iterate over outside
  19. //for(int k=0; k<X.outerSize(); ++k)
  20. //{
  21. // // Iterate over inside
  22. // for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  23. // {
  24. // if(it.col() == it.row())
  25. // {
  26. // V.coeffRef(it.col()) += it.value();
  27. // }
  28. // }
  29. //}
  30. }
  31. template <typename T,typename DerivedV>
  32. IGL_INLINE void igl::diag(
  33. const Eigen::SparseMatrix<T>& X,
  34. Eigen::MatrixBase<DerivedV> & V)
  35. {
  36. assert(false && "Just call X.diagonal() directly");
  37. V = X.diagonal();
  38. //// Get size of input
  39. //int m = X.rows();
  40. //int n = X.cols();
  41. //V.derived().resize((m>n?n:m),1);
  42. //// Iterate over outside
  43. //for(int k=0; k<X.outerSize(); ++k)
  44. //{
  45. // // Iterate over inside
  46. // for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  47. // {
  48. // if(it.col() == it.row())
  49. // {
  50. // V(it.col()) = it.value();
  51. // }
  52. // }
  53. //}
  54. }
  55. template <typename T>
  56. IGL_INLINE void igl::diag(
  57. const Eigen::SparseVector<T>& V,
  58. Eigen::SparseMatrix<T>& X)
  59. {
  60. // clear and resize output
  61. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_X(V.size(),V.size());
  62. dyn_X.reserve(V.size());
  63. // loop over non-zeros
  64. for(typename Eigen::SparseVector<T>::InnerIterator it(V); it; ++it)
  65. {
  66. dyn_X.coeffRef(it.index(),it.index()) += it.value();
  67. }
  68. X = Eigen::SparseMatrix<T>(dyn_X);
  69. }
  70. template <typename T, typename DerivedV>
  71. IGL_INLINE void igl::diag(
  72. const Eigen::MatrixBase<DerivedV> & V,
  73. Eigen::SparseMatrix<T>& X)
  74. {
  75. assert(V.rows() == 1 || V.cols() == 1);
  76. // clear and resize output
  77. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_X(V.size(),V.size());
  78. dyn_X.reserve(V.size());
  79. // loop over non-zeros
  80. for(int i = 0;i<V.size();i++)
  81. {
  82. dyn_X.coeffRef(i,i) += V[i];
  83. i++;
  84. }
  85. X = Eigen::SparseMatrix<T>(dyn_X);
  86. }
  87. #ifndef IGL_HEADER_ONLY
  88. // Explicit template specialization
  89. template void igl::diag<double, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  90. template void igl::diag<double>(Eigen::SparseMatrix<double, 0, int> const&, Eigen::SparseVector<double, 0, int>&);
  91. #endif