repdiag.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "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. using namespace std;
  17. using namespace Eigen;
  18. int m = A.rows();
  19. int n = A.cols();
  20. #if false
  21. vector<Triplet<T> > IJV;
  22. IJV.reserve(A.nonZeros()*d);
  23. // Loop outer level
  24. for (int k=0; k<A.outerSize(); ++k)
  25. {
  26. // loop inner level
  27. for (typename Eigen::SparseMatrix<T>::InnerIterator it(A,k); it; ++it)
  28. {
  29. for(int i = 0;i<d;i++)
  30. {
  31. IJV.push_back(Triplet<T>(i*m+it.row(),i*n+it.col(),it.value()));
  32. }
  33. }
  34. }
  35. B.resize(m*d,n*d);
  36. B.setFromTriplets(IJV.begin(),IJV.end());
  37. #else
  38. // This will not work for RowMajor
  39. B.resize(m*d,n*d);
  40. Eigen::VectorXi per_col = Eigen::VectorXi::Zero(n*d);
  41. for (int k=0; k<A.outerSize(); ++k)
  42. {
  43. for (typename Eigen::SparseMatrix<T>::InnerIterator it(A,k); it; ++it)
  44. {
  45. for(int r = 0;r<d;r++) per_col(n*r + k)++;
  46. }
  47. }
  48. B.reserve(per_col);
  49. for(int r = 0;r<d;r++)
  50. {
  51. const int mr = m*r;
  52. const int nr = n*r;
  53. for (int k=0; k<A.outerSize(); ++k)
  54. {
  55. for (typename Eigen::SparseMatrix<T>::InnerIterator it(A,k); it; ++it)
  56. {
  57. B.insert(it.row()+mr,k+nr) = it.value();
  58. }
  59. }
  60. }
  61. B.makeCompressed();
  62. #endif
  63. }
  64. template <typename T>
  65. IGL_INLINE void igl::repdiag(
  66. const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & A,
  67. const int d,
  68. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B)
  69. {
  70. int m = A.rows();
  71. int n = A.cols();
  72. B.resize(m*d,n*d);
  73. B.array() *= 0;
  74. for(int i = 0;i<d;i++)
  75. {
  76. B.block(i*m,i*n,m,n) = A;
  77. }
  78. }
  79. // Wrapper with B as output
  80. template <class Mat>
  81. IGL_INLINE Mat igl::repdiag(const Mat & A, const int d)
  82. {
  83. Mat B;
  84. repdiag(A,d,B);
  85. return B;
  86. }
  87. #ifdef IGL_STATIC_LIBRARY
  88. // Explicit template specialization
  89. // generated by autoexplicit.sh
  90. template void igl::repdiag<double>(Eigen::SparseMatrix<double, 0, int> const&, int, Eigen::SparseMatrix<double, 0, int>&);
  91. // generated by autoexplicit.sh
  92. template Eigen::SparseMatrix<double, 0, int> igl::repdiag<Eigen::SparseMatrix<double, 0, int> >(Eigen::SparseMatrix<double, 0, int> const&, int);
  93. #endif