repdiag.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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 "repdiag.h"
  9. #include <vector>
  10. template <typename T>
  11. IGL_INLINE void igl::repdiag(
  12. const Eigen::SparseMatrix<T>& A,
  13. const int d,
  14. Eigen::SparseMatrix<T>& B)
  15. {
  16. int m = A.rows();
  17. int n = A.cols();
  18. #if false
  19. vector<Eigen::Triplet<T> > IJV;
  20. IJV.reserve(A.nonZeros()*d);
  21. // Loop outer level
  22. for (int k=0; k<A.outerSize(); ++k)
  23. {
  24. // loop inner level
  25. for (typename Eigen::SparseMatrix<T>::InnerIterator it(A,k); it; ++it)
  26. {
  27. for(int i = 0;i<d;i++)
  28. {
  29. IJV.push_back(Eigen::Triplet<T>(i*m+it.row(),i*n+it.col(),it.value()));
  30. }
  31. }
  32. }
  33. B.resize(m*d,n*d);
  34. B.setFromTriplets(IJV.begin(),IJV.end());
  35. #else
  36. // This will not work for RowMajor
  37. B.resize(m*d,n*d);
  38. Eigen::VectorXi per_col = Eigen::VectorXi::Zero(n*d);
  39. for (int k=0; k<A.outerSize(); ++k)
  40. {
  41. for (typename Eigen::SparseMatrix<T>::InnerIterator it(A,k); it; ++it)
  42. {
  43. for(int r = 0;r<d;r++) per_col(n*r + k)++;
  44. }
  45. }
  46. B.reserve(per_col);
  47. for(int r = 0;r<d;r++)
  48. {
  49. const int mr = m*r;
  50. const int nr = n*r;
  51. for (int k=0; k<A.outerSize(); ++k)
  52. {
  53. for (typename Eigen::SparseMatrix<T>::InnerIterator it(A,k); it; ++it)
  54. {
  55. B.insert(it.row()+mr,k+nr) = it.value();
  56. }
  57. }
  58. }
  59. B.makeCompressed();
  60. #endif
  61. }
  62. template <typename T>
  63. IGL_INLINE void igl::repdiag(
  64. const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & A,
  65. const int d,
  66. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B)
  67. {
  68. int m = A.rows();
  69. int n = A.cols();
  70. B.setZero(m*d,n*d);
  71. for(int i = 0;i<d;i++)
  72. {
  73. B.block(i*m,i*n,m,n) = A;
  74. }
  75. }
  76. // Wrapper with B as output
  77. template <class Mat>
  78. IGL_INLINE Mat igl::repdiag(const Mat & A, const int d)
  79. {
  80. Mat B;
  81. repdiag(A,d,B);
  82. return B;
  83. }
  84. #ifdef IGL_STATIC_LIBRARY
  85. // Explicit template instantiation
  86. // generated by autoexplicit.sh
  87. template void igl::repdiag<double>(Eigen::SparseMatrix<double, 0, int> const&, int, Eigen::SparseMatrix<double, 0, int>&);
  88. // generated by autoexplicit.sh
  89. template Eigen::SparseMatrix<double, 0, int> igl::repdiag<Eigen::SparseMatrix<double, 0, int> >(Eigen::SparseMatrix<double, 0, int> const&, int);
  90. #endif