sparse_cached.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 Daniele Panozzo <daniele.panozzo@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_CACHED_H
  9. #define IGL_SPARSE_CACHED_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), similarly to
  17. // the sparse function in matlab. Divides the construction in two phases, one
  18. // for fixing the sparsity pattern, and one to populate it with values. Compared to
  19. // igl::sparse, this version is slower for the first time (since it requires a
  20. // precomputation), but faster to the subsequent evaluations.
  21. //
  22. // Templates:
  23. // IndexVector list of indices, value should be non-negative and should
  24. // expect to be cast to an index. Must implement operator(i) to retrieve
  25. // ith element
  26. // ValueVector list of values, value should be expect to be cast to type
  27. // T. Must implement operator(i) to retrieve ith element
  28. // T should be a eigen sparse matrix primitive type like int or double
  29. // Input:
  30. // I nnz vector of row indices of non zeros entries in X
  31. // J nnz vector of column indices of non zeros entries in X
  32. // V nnz vector of non-zeros entries in X
  33. // Optional:
  34. // m number of rows
  35. // n number of cols
  36. // Outputs:
  37. // X m by n matrix of type T whose entries are to be found
  38. //
  39. // Example:
  40. // Eigen::SparseMatrix<double> A;
  41. // std::vector<Eigen::Triplet<double> > IJV;
  42. // slim_buildA(IJV);
  43. // if (A.rows() == 0)
  44. // {
  45. // A = Eigen::SparseMatrix<double>(rows,cols);
  46. // igl::sparse_cached_precompute(IJV,A,A_data);
  47. // }
  48. // else
  49. // igl::sparse_cached(IJV,s.A,s.A_data);
  50. template <typename DerivedI, typename Scalar>
  51. IGL_INLINE void sparse_cached_precompute(
  52. const Eigen::MatrixBase<DerivedI> & I,
  53. const Eigen::MatrixBase<DerivedI> & J,
  54. Eigen::VectorXi& data,
  55. Eigen::SparseMatrix<Scalar>& X
  56. );
  57. template <typename Scalar>
  58. IGL_INLINE void sparse_cached_precompute(
  59. const std::vector<Eigen::Triplet<Scalar> >& triplets,
  60. Eigen::VectorXi& data,
  61. Eigen::SparseMatrix<Scalar>& X
  62. );
  63. template <typename Scalar>
  64. IGL_INLINE void sparse_cached(
  65. const std::vector<Eigen::Triplet<Scalar> >& triplets,
  66. const Eigen::VectorXi& data,
  67. Eigen::SparseMatrix<Scalar>& X);
  68. template <typename DerivedV, typename Scalar>
  69. IGL_INLINE void sparse_cached(
  70. const Eigen::MatrixBase<DerivedV>& V,
  71. const Eigen::VectorXi& data,
  72. Eigen::SparseMatrix<Scalar>& X
  73. );
  74. }
  75. #ifndef IGL_STATIC_LIBRARY
  76. # include "sparse_cached.cpp"
  77. #endif
  78. #endif