invert_diag.cpp 961 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "invert_diag.h"
  2. #include "diag.h"
  3. template <typename T>
  4. IGL_INLINE void igl::invert_diag(
  5. const Eigen::SparseMatrix<T>& X,
  6. Eigen::SparseMatrix<T>& Y)
  7. {
  8. #ifndef NDEBUG
  9. typename Eigen::SparseVector<T> dX;
  10. igl::diag(X,dX);
  11. // Check that there are no zeros along the diagonal
  12. assert(dX.nonZeros() == dX.size());
  13. #endif
  14. // http://www.alecjacobson.com/weblog/?p=2552
  15. if(&Y != &X)
  16. {
  17. Y = X;
  18. }
  19. // Iterate over outside
  20. for(int k=0; k<Y.outerSize(); ++k)
  21. {
  22. // Iterate over inside
  23. for(typename Eigen::SparseMatrix<T>::InnerIterator it (Y,k); it; ++it)
  24. {
  25. if(it.col() == it.row())
  26. {
  27. T v = it.value();
  28. assert(v != 0);
  29. v = ((T)1.0)/v;
  30. Y.coeffRef(it.row(),it.col()) = v;
  31. }
  32. }
  33. }
  34. }
  35. #ifndef IGL_HEADER_ONLY
  36. // Explicit template specialization
  37. template void igl::invert_diag<double>(Eigen::SparseMatrix<double, 0, int> const&, Eigen::SparseMatrix<double, 0, int>&);
  38. #endif