blkdiag.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. int nnz = 0;
  17. for(const auto & A : L)
  18. {
  19. nr += A.rows();
  20. nc += A.cols();
  21. }
  22. Y.resize(nr,nc);
  23. {
  24. int i = 0;
  25. int j = 0;
  26. for(const auto & A : L)
  27. {
  28. for(int k = 0;k<A.outerSize();++k)
  29. {
  30. for(typename Eigen::SparseMatrix<Scalar>::InnerIterator it(A,k);it;++it)
  31. {
  32. Y.insert(i+it.row(),j+k) = it.value();
  33. }
  34. }
  35. i += A.rows();
  36. j += A.cols();
  37. }
  38. }
  39. }
  40. template <typename DerivedY>
  41. IGL_INLINE void igl::blkdiag(
  42. const std::vector<DerivedY> & L,
  43. Eigen::PlainObjectBase<DerivedY> & Y)
  44. {
  45. int nr = 0;
  46. int nc = 0;
  47. for(const auto & A : L)
  48. {
  49. nr += A.rows();
  50. nc += A.cols();
  51. }
  52. Y.setZero(nr,nc);
  53. {
  54. int i = 0;
  55. int j = 0;
  56. for(const auto & A : L)
  57. {
  58. Y.block(i,j,A.rows(),A.cols()) = A;
  59. i += A.rows();
  60. j += A.cols();
  61. }
  62. }
  63. }
  64. #ifdef IGL_STATIC_LIBRARY
  65. // explicit template instantiations
  66. 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> >&);
  67. 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>&);
  68. #endif