sparse.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #ifndef IGL_SPARSE_H
  9. #define IGL_SPARSE_H
  10. #include "igl_inline.h"
  11. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  12. #include <Eigen/Dense>
  13. #include <Eigen/Sparse>
  14. namespace igl
  15. {
  16. // Build a sparse matrix from list of indices and values (I,J,V), functions
  17. // like the sparse function in matlab
  18. //
  19. // Templates:
  20. // IndexVector list of indices, value should be non-negative and should
  21. // expect to be cast to an index. Must implement operator(i) to retrieve
  22. // ith element
  23. // ValueVector list of values, value should be expect to be cast to type
  24. // T. Must implement operator(i) to retrieve ith element
  25. // T should be a eigen sparse matrix primitive type like int or double
  26. // Input:
  27. // I nnz vector of row indices of non zeros entries in X
  28. // J nnz vector of column indices of non zeros entries in X
  29. // V nnz vector of non-zeros entries in X
  30. // Optional:
  31. // m number of rows
  32. // n number of cols
  33. // Outputs:
  34. // X m by n matrix of type T whose entries are to be found
  35. //
  36. template <class IndexVector, class ValueVector, typename T>
  37. IGL_INLINE void sparse(
  38. const IndexVector & I,
  39. const IndexVector & J,
  40. const ValueVector & V,
  41. Eigen::SparseMatrix<T>& X);
  42. template <class IndexVector, class ValueVector, typename T>
  43. IGL_INLINE void sparse(
  44. const IndexVector & I,
  45. const IndexVector & J,
  46. const ValueVector & V,
  47. const size_t m,
  48. const size_t n,
  49. Eigen::SparseMatrix<T>& X);
  50. // THIS MAY BE SUPERSEDED BY EIGEN'S .sparseView Indeed it is.
  51. // Convert a full, dense matrix to a sparse one
  52. //
  53. // Templates:
  54. // T should be a eigen sparse matrix primitive type like int or double
  55. // Input:
  56. // D m by n full, dense matrix
  57. // Output:
  58. // X m by n sparse matrix
  59. template <typename DerivedD, typename T>
  60. IGL_INLINE void sparse(
  61. const Eigen::PlainObjectBase<DerivedD>& D,
  62. Eigen::SparseMatrix<T>& X);
  63. // Wrapper with return
  64. template <typename DerivedD>
  65. IGL_INLINE Eigen::SparseMatrix<typename DerivedD::Scalar > sparse(
  66. const Eigen::PlainObjectBase<DerivedD>& D);
  67. }
  68. #ifndef IGL_STATIC_LIBRARY
  69. # include "sparse.cpp"
  70. #endif
  71. #endif