lu_lagrange.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "lu_lagrange.h"
  9. // Cholesky LLT decomposition for symmetric positive definite
  10. //#include <Eigen/SparseExtra>
  11. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  12. #include <iostream>
  13. #include <unsupported/Eigen/SparseExtra>
  14. #include <cassert>
  15. #include <cstdio>
  16. #include "find.h"
  17. #include "sparse.h"
  18. template <typename T>
  19. IGL_INLINE bool igl::lu_lagrange(
  20. const Eigen::SparseMatrix<T> & ATA,
  21. const Eigen::SparseMatrix<T> & C,
  22. Eigen::SparseMatrix<T> & L,
  23. Eigen::SparseMatrix<T> & U)
  24. {
  25. #if EIGEN_VERSION_AT_LEAST(3,0,92)
  26. # warning lu_lagrange has not yet been implemented for your Eigen Version
  27. return false;
  28. #else
  29. // number of unknowns
  30. int n = ATA.rows();
  31. // number of lagrange multipliers
  32. int m = C.cols();
  33. assert(ATA.cols() == n);
  34. if(m != 0)
  35. {
  36. assert(C.rows() == n);
  37. if(C.nonZeros() == 0)
  38. {
  39. // See note above about empty columns in C
  40. fprintf(stderr,"Error: lu_lagrange() C has columns but no entries\n");
  41. return false;
  42. }
  43. }
  44. // Check that each column of C has at least one entry
  45. std::vector<bool> has_entry; has_entry.resize(C.cols(),false);
  46. // Iterate over outside
  47. for(int k=0; k<C.outerSize(); ++k)
  48. {
  49. // Iterate over inside
  50. for(typename Eigen::SparseMatrix<T>::InnerIterator it (C,k); it; ++it)
  51. {
  52. has_entry[it.col()] = true;
  53. }
  54. }
  55. for(int i=0;i<(int)has_entry.size();i++)
  56. {
  57. if(!has_entry[i])
  58. {
  59. // See note above about empty columns in C
  60. fprintf(stderr,"Error: lu_lagrange() C(:,%d) has no entries\n",i);
  61. return false;
  62. }
  63. }
  64. // Cholesky factorization of ATA
  65. //// Eigen fails if you give a full view of the matrix like this:
  66. //Eigen::SparseLLT<SparseMatrix<T> > ATA_LLT(ATA);
  67. Eigen::SparseMatrix<T> ATA_LT = ATA.template triangularView<Eigen::Lower>();
  68. Eigen::SparseLLT<Eigen::SparseMatrix<T> > ATA_LLT(ATA_LT);
  69. Eigen::SparseMatrix<T> J = ATA_LLT.matrixL();
  70. //if(!ATA_LLT.succeeded())
  71. if(!((J*0).eval().nonZeros() == 0))
  72. {
  73. fprintf(stderr,"Error: lu_lagrange() failed to factor ATA\n");
  74. return false;
  75. }
  76. if(m == 0)
  77. {
  78. // If there are no constraints (C is empty) then LU decomposition is just L
  79. // and L' from cholesky decomposition
  80. L = J;
  81. U = J.transpose();
  82. }else
  83. {
  84. // Construct helper matrix M
  85. Eigen::SparseMatrix<T> M = C;
  86. J.template triangularView<Eigen::Lower>().solveInPlace(M);
  87. // Compute cholesky factorizaiton of M'*M
  88. Eigen::SparseMatrix<T> MTM = M.transpose() * M;
  89. Eigen::SparseLLT<Eigen::SparseMatrix<T> > MTM_LLT(MTM.template triangularView<Eigen::Lower>());
  90. Eigen::SparseMatrix<T> K = MTM_LLT.matrixL();
  91. //if(!MTM_LLT.succeeded())
  92. if(!((K*0).eval().nonZeros() == 0))
  93. {
  94. fprintf(stderr,"Error: lu_lagrange() failed to factor MTM\n");
  95. return false;
  96. }
  97. // assemble LU decomposition of Q
  98. Eigen::Matrix<int,Eigen::Dynamic,1> MI;
  99. Eigen::Matrix<int,Eigen::Dynamic,1> MJ;
  100. Eigen::Matrix<T,Eigen::Dynamic,1> MV;
  101. igl::find(M,MI,MJ,MV);
  102. Eigen::Matrix<int,Eigen::Dynamic,1> KI;
  103. Eigen::Matrix<int,Eigen::Dynamic,1> KJ;
  104. Eigen::Matrix<T,Eigen::Dynamic,1> KV;
  105. igl::find(K,KI,KJ,KV);
  106. Eigen::Matrix<int,Eigen::Dynamic,1> JI;
  107. Eigen::Matrix<int,Eigen::Dynamic,1> JJ;
  108. Eigen::Matrix<T,Eigen::Dynamic,1> JV;
  109. igl::find(J,JI,JJ,JV);
  110. int nnz = JV.size() + MV.size() + KV.size();
  111. Eigen::Matrix<int,Eigen::Dynamic,1> UI(nnz);
  112. Eigen::Matrix<int,Eigen::Dynamic,1> UJ(nnz);
  113. Eigen::Matrix<T,Eigen::Dynamic,1> UV(nnz);
  114. UI << JJ, MI, (KJ.array() + n).matrix();
  115. UJ << JI, (MJ.array() + n).matrix(), (KI.array() + n).matrix();
  116. UV << JV, MV, KV*-1;
  117. igl::sparse(UI,UJ,UV,U);
  118. Eigen::Matrix<int,Eigen::Dynamic,1> LI(nnz);
  119. Eigen::Matrix<int,Eigen::Dynamic,1> LJ(nnz);
  120. Eigen::Matrix<T,Eigen::Dynamic,1> LV(nnz);
  121. LI << JI, (MJ.array() + n).matrix(), (KI.array() + n).matrix();
  122. LJ << JJ, MI, (KJ.array() + n).matrix();
  123. LV << JV, MV, KV;
  124. igl::sparse(LI,LJ,LV,L);
  125. }
  126. return true;
  127. #endif
  128. }
  129. #ifndef IGL_HEADER_ONLY
  130. // Explicit template specialization
  131. template bool igl::lu_lagrange<double>(Eigen::SparseMatrix<double, 0, int> const&, Eigen::SparseMatrix<double, 0, int> const&, Eigen::SparseMatrix<double, 0, int>&, Eigen::SparseMatrix<double, 0, int>&);
  132. #endif