diag.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include "diag.h"
  9. #include "verbose.h"
  10. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  11. #include <iostream>
  12. #include <unsupported/Eigen/SparseExtra>
  13. template <typename T>
  14. IGL_INLINE void igl::diag(
  15. const Eigen::SparseMatrix<T>& X,
  16. Eigen::SparseVector<T>& V)
  17. {
  18. assert(false && "Just call X.diagonal().sparseView() directly");
  19. V = X.diagonal().sparseView();
  20. //// Get size of input
  21. //int m = X.rows();
  22. //int n = X.cols();
  23. //V = Eigen::SparseVector<T>((m>n?n:m));
  24. //V.reserve(V.size());
  25. //// Iterate over outside
  26. //for(int k=0; k<X.outerSize(); ++k)
  27. //{
  28. // // Iterate over inside
  29. // for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  30. // {
  31. // if(it.col() == it.row())
  32. // {
  33. // V.coeffRef(it.col()) += it.value();
  34. // }
  35. // }
  36. //}
  37. }
  38. template <typename T,typename DerivedV>
  39. IGL_INLINE void igl::diag(
  40. const Eigen::SparseMatrix<T>& X,
  41. Eigen::MatrixBase<DerivedV> & V)
  42. {
  43. assert(false && "Just call X.diagonal() directly");
  44. V = X.diagonal();
  45. //// Get size of input
  46. //int m = X.rows();
  47. //int n = X.cols();
  48. //V.derived().resize((m>n?n:m),1);
  49. //// Iterate over outside
  50. //for(int k=0; k<X.outerSize(); ++k)
  51. //{
  52. // // Iterate over inside
  53. // for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  54. // {
  55. // if(it.col() == it.row())
  56. // {
  57. // V(it.col()) = it.value();
  58. // }
  59. // }
  60. //}
  61. }
  62. template <typename T>
  63. IGL_INLINE void igl::diag(
  64. const Eigen::SparseVector<T>& V,
  65. Eigen::SparseMatrix<T>& X)
  66. {
  67. // clear and resize output
  68. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_X(V.size(),V.size());
  69. dyn_X.reserve(V.size());
  70. // loop over non-zeros
  71. for(typename Eigen::SparseVector<T>::InnerIterator it(V); it; ++it)
  72. {
  73. dyn_X.coeffRef(it.index(),it.index()) += it.value();
  74. }
  75. X = Eigen::SparseMatrix<T>(dyn_X);
  76. }
  77. template <typename T, typename DerivedV>
  78. IGL_INLINE void igl::diag(
  79. const Eigen::MatrixBase<DerivedV> & V,
  80. Eigen::SparseMatrix<T>& X)
  81. {
  82. assert(V.rows() == 1 || V.cols() == 1);
  83. // clear and resize output
  84. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_X(V.size(),V.size());
  85. dyn_X.reserve(V.size());
  86. // loop over non-zeros
  87. for(int i = 0;i<V.size();i++)
  88. {
  89. dyn_X.coeffRef(i,i) += V[i];
  90. }
  91. X = Eigen::SparseMatrix<T>(dyn_X);
  92. }
  93. #ifdef IGL_STATIC_LIBRARY
  94. // Explicit template specialization
  95. template void igl::diag<double, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  96. template void igl::diag<double>(Eigen::SparseMatrix<double, 0, int> const&, Eigen::SparseVector<double, 0, int>&);
  97. template void igl::diag<double, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::SparseMatrix<double, 0, int>&);
  98. template void igl::diag<double>(Eigen::SparseVector<double, 0, int> const&, Eigen::SparseMatrix<double, 0, int>&);
  99. #endif