sparse.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "sparse.h"
  2. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  3. #include <iostream>
  4. #include <unsupported/Eigen/SparseExtra>
  5. template <class IndexVector, class ValueVector, typename T>
  6. IGL_INLINE void igl::sparse(
  7. const IndexVector & I,
  8. const IndexVector & J,
  9. const ValueVector & V,
  10. Eigen::SparseMatrix<T>& X)
  11. {
  12. size_t m = (size_t)I.maxCoeff()+1;
  13. size_t n = (size_t)J.maxCoeff()+1;
  14. return igl::sparse(I,J,V,m,n,X);
  15. }
  16. #include "verbose.h"
  17. template <class IndexVector, class ValueVector, typename T>
  18. IGL_INLINE void igl::sparse(
  19. const IndexVector & I,
  20. const IndexVector & J,
  21. const ValueVector & V,
  22. const size_t m,
  23. const size_t n,
  24. Eigen::SparseMatrix<T>& X)
  25. {
  26. assert((int)I.maxCoeff() < (int)m);
  27. assert((int)I.minCoeff() >= 0);
  28. assert((int)J.maxCoeff() < (int)n);
  29. assert((int)J.minCoeff() >= 0);
  30. assert(I.size() == J.size());
  31. assert(J.size() == V.size());
  32. // Really we just need .size() to be the same, but this is safer
  33. assert(I.rows() == J.rows());
  34. assert(J.rows() == V.rows());
  35. assert(I.cols() == J.cols());
  36. assert(J.cols() == V.cols());
  37. // number of values
  38. int nv = V.size();
  39. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_X(m,n);
  40. // over estimate the number of entries
  41. dyn_X.reserve(I.size());
  42. for(int i = 0;i < nv;i++)
  43. {
  44. dyn_X.coeffRef((int)I(i),(int)J(i)) += (T)V(i);
  45. }
  46. X = Eigen::SparseMatrix<T>(dyn_X);
  47. }
  48. #ifndef IGL_HEADER_ONLY
  49. // Explicit template specialization
  50. template void igl::sparse<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, double>(Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, size_t, size_t, Eigen::SparseMatrix<double, 0, int>&);
  51. #endif