invert_diag.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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 DerivedX, typename MatY>
  10. IGL_INLINE void igl::invert_diag(
  11. const Eigen::SparseCompressedBase<DerivedX>& X,
  12. MatY& Y)
  13. {
  14. typedef typename DerivedX::Scalar Scalar;
  15. #ifndef NDEBUG
  16. Eigen::SparseMatrix<Scalar> tmp = X;
  17. Eigen::SparseVector<Scalar> dX = tmp.diagonal().sparseView();
  18. // Check that there are no zeros along the diagonal
  19. assert(dX.nonZeros() == dX.size());
  20. #endif
  21. // http://www.alecjacobson.com/weblog/?p=2552
  22. if((void *)&Y != (void *)&X)
  23. {
  24. Y = X;
  25. }
  26. // Iterate over outside
  27. for(int k=0; k<Y.outerSize(); ++k)
  28. {
  29. // Iterate over inside
  30. for(typename MatY::InnerIterator it (Y,k); it; ++it)
  31. {
  32. if(it.col() == it.row())
  33. {
  34. Scalar v = it.value();
  35. assert(v != 0);
  36. v = ((Scalar)1.0)/v;
  37. Y.coeffRef(it.row(),it.col()) = v;
  38. }
  39. }
  40. }
  41. }
  42. #ifdef IGL_STATIC_LIBRARY
  43. // Explicit template instantiation
  44. template void igl::invert_diag<Eigen::SparseMatrix<double, 0, int>, Eigen::SparseMatrix<double, 0, int> >(Eigen::SparseCompressedBase<Eigen::SparseMatrix<double, 0, int>> const&, Eigen::SparseMatrix<double, 0, int>&);
  45. template void igl::invert_diag<Eigen::SparseMatrix<float, 0, int>, Eigen::SparseMatrix<float, 0, int> >(Eigen::SparseCompressedBase<Eigen::SparseMatrix<float, 0, int>> const&, Eigen::SparseMatrix<float, 0, int>&);
  46. #endif