diag.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "diag.h"
  2. #include "verbose.h"
  3. template <typename T>
  4. IGL_INLINE void igl::diag(
  5. const Eigen::SparseMatrix<T>& X,
  6. Eigen::SparseVector<T>& V)
  7. {
  8. // Get size of input
  9. int m = X.rows();
  10. int n = X.cols();
  11. V = Eigen::SparseVector<T>((m>n?n:m));
  12. // Iterate over outside
  13. for(int k=0; k<X.outerSize(); ++k)
  14. {
  15. // Iterate over inside
  16. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  17. {
  18. if(it.col() == it.row())
  19. {
  20. V.coeffRef(it.col()) += it.value();
  21. }
  22. }
  23. }
  24. }
  25. template <typename T>
  26. IGL_INLINE void igl::diag(
  27. const Eigen::SparseVector<T>& V,
  28. Eigen::SparseMatrix<T>& X)
  29. {
  30. // clear and resize output
  31. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_X(V.size(),V.size());
  32. // loop over non-zeros
  33. for(typename Eigen::SparseVector<T>::InnerIterator it(V); it; ++it)
  34. {
  35. dyn_X.coeffRef(it.index(),it.index()) += it.value();
  36. }
  37. X = Eigen::SparseMatrix<T>(dyn_X);
  38. }
  39. #ifndef IGL_HEADER_ONLY
  40. // Explicit template specialization
  41. #endif