invert_diag.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "invert_diag.h"
  9. template <typename T>
  10. IGL_INLINE void igl::invert_diag(
  11. const Eigen::SparseMatrix<T>& X,
  12. Eigen::SparseMatrix<T>& Y)
  13. {
  14. #ifndef NDEBUG
  15. typename Eigen::SparseVector<T> dX = X.diagonal().sparseView();
  16. // Check that there are no zeros along the diagonal
  17. assert(dX.nonZeros() == dX.size());
  18. #endif
  19. // http://www.alecjacobson.com/weblog/?p=2552
  20. if(&Y != &X)
  21. {
  22. Y = X;
  23. }
  24. // Iterate over outside
  25. for(int k=0; k<Y.outerSize(); ++k)
  26. {
  27. // Iterate over inside
  28. for(typename Eigen::SparseMatrix<T>::InnerIterator it (Y,k); it; ++it)
  29. {
  30. if(it.col() == it.row())
  31. {
  32. T v = it.value();
  33. assert(v != 0);
  34. v = ((T)1.0)/v;
  35. Y.coeffRef(it.row(),it.col()) = v;
  36. }
  37. }
  38. }
  39. }
  40. #ifdef IGL_STATIC_LIBRARY
  41. // Explicit template specialization
  42. template void igl::invert_diag<double>(Eigen::SparseMatrix<double, 0, int> const&, Eigen::SparseMatrix<double, 0, int>&);
  43. template void igl::invert_diag<float>(Eigen::SparseMatrix<float, 0, int> const&, Eigen::SparseMatrix<float, 0, int>&);
  44. #endif