blkdiag.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2020 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 "blkdiag.h"
  9. template <typename Scalar>
  10. IGL_INLINE void igl::blkdiag(
  11. const std::vector<Eigen::SparseMatrix<Scalar>> & L,
  12. Eigen::SparseMatrix<Scalar> & Y)
  13. {
  14. int nr = 0;
  15. int nc = 0;
  16. for(const auto & A : L)
  17. {
  18. nr += A.rows();
  19. nc += A.cols();
  20. }
  21. Y.resize(nr,nc);
  22. {
  23. int i = 0;
  24. int j = 0;
  25. for(const auto & A : L)
  26. {
  27. for(int k = 0;k<A.outerSize();++k)
  28. {
  29. for(typename Eigen::SparseMatrix<Scalar>::InnerIterator it(A,k);it;++it)
  30. {
  31. Y.insert(i+it.row(),j+k) = it.value();
  32. }
  33. }
  34. i += A.rows();
  35. j += A.cols();
  36. }
  37. }
  38. }
  39. template <typename DerivedY>
  40. IGL_INLINE void igl::blkdiag(
  41. const std::vector<DerivedY> & L,
  42. Eigen::PlainObjectBase<DerivedY> & Y)
  43. {
  44. int nr = 0;
  45. int nc = 0;
  46. for(const auto & A : L)
  47. {
  48. nr += A.rows();
  49. nc += A.cols();
  50. }
  51. Y.setZero(nr,nc);
  52. {
  53. int i = 0;
  54. int j = 0;
  55. for(const auto & A : L)
  56. {
  57. Y.block(i,j,A.rows(),A.cols()) = A;
  58. i += A.rows();
  59. j += A.cols();
  60. }
  61. }
  62. }
  63. #ifdef IGL_STATIC_LIBRARY
  64. // explicit template instantiations
  65. template void igl::blkdiag<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::vector<Eigen::Matrix<double, -1, -1, 0, -1, -1>, std::allocator<Eigen::Matrix<double, -1, -1, 0, -1, -1> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  66. template void igl::blkdiag<double>(std::vector<Eigen::SparseMatrix<double, 0, int>, std::allocator<Eigen::SparseMatrix<double, 0, int> > > const&, Eigen::SparseMatrix<double, 0, int>&);
  67. #endif