sparse.cpp 1.6 KB

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