sum.cpp 791 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "sum.h"
  2. template <typename T>
  3. IGL_INLINE void igl::sum(
  4. const Eigen::SparseMatrix<T>& X,
  5. const int dim,
  6. Eigen::SparseVector<T>& S)
  7. {
  8. // dim must be 2 or 1
  9. assert(dim == 1 || dim == 2);
  10. // Get size of input
  11. int m = X.rows();
  12. int n = X.cols();
  13. // resize output
  14. if(dim==1)
  15. {
  16. S = Eigen::SparseVector<T>(n);
  17. }else
  18. {
  19. S = Eigen::SparseVector<T>(m);
  20. }
  21. // Iterate over outside
  22. for(int k=0; k<X.outerSize(); ++k)
  23. {
  24. // Iterate over inside
  25. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  26. {
  27. if(dim == 1)
  28. {
  29. S.coeffRef(it.col()) += it.value();
  30. }else
  31. {
  32. S.coeffRef(it.row()) += it.value();
  33. }
  34. }
  35. }
  36. }
  37. #ifndef IGL_HEADER_ONLY
  38. // Explicit template specialization
  39. #endif