is_symmetric.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "is_symmetric.h"
  9. #include "find.h"
  10. template <typename T>
  11. IGL_INLINE bool igl::is_symmetric(const Eigen::SparseMatrix<T>& A)
  12. {
  13. if(A.rows() != A.cols())
  14. {
  15. return false;
  16. }
  17. Eigen::SparseMatrix<T> AT = A.transpose();
  18. Eigen::SparseMatrix<T> AmAT = A-AT;
  19. //// Eigen screws up something with LLT if you try to do
  20. //SparseMatrix<T> AmAT = A-A.transpose();
  21. //// Eigen crashes at runtime if you try to do
  22. // return (A-A.transpose()).nonZeros() == 0;
  23. return AmAT.nonZeros() == 0;
  24. }
  25. template <typename DerivedA>
  26. IGL_INLINE bool igl::is_symmetric(
  27. const Eigen::PlainObjectBase<DerivedA>& A)
  28. {
  29. if(A.rows() != A.cols())
  30. {
  31. return false;
  32. }
  33. const typename Eigen::PlainObjectBase<DerivedA>& AT = A.transpose();
  34. const typename Eigen::PlainObjectBase<DerivedA>& AmAT = A-AT;
  35. //// Eigen screws up something with LLT if you try to do
  36. //SparseMatrix<T> AmAT = A-A.transpose();
  37. //// Eigen crashes at runtime if you try to do
  38. // return (A-A.transpose()).nonZeros() == 0;
  39. return AmAT.nonZeros() == 0;
  40. }
  41. template <typename AType, typename epsilonT>
  42. IGL_INLINE bool igl::is_symmetric(
  43. const Eigen::SparseMatrix<AType>& A,
  44. const epsilonT epsilon)
  45. {
  46. using namespace Eigen;
  47. using namespace igl;
  48. if(A.rows() != A.cols())
  49. {
  50. return false;
  51. }
  52. SparseMatrix<AType> AT = A.transpose();
  53. SparseMatrix<AType> AmAT = A-AT;
  54. VectorXi AmATI,AmATJ;
  55. Matrix<AType,Dynamic,1> AmATV;
  56. find(AmAT,AmATI,AmATJ,AmATV);
  57. return AmATV.maxCoeff() < epsilon && AmATV.minCoeff() > -epsilon;
  58. }
  59. #ifndef IGL_HEADER_ONLY
  60. // Explicit template specialization
  61. // generated by autoexplicit.sh
  62. template bool igl::is_symmetric<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&);
  63. // generated by autoexplicit.sh
  64. template bool igl::is_symmetric<double>(Eigen::SparseMatrix<double, 0, int> const&);
  65. template bool igl::is_symmetric<double, double>(Eigen::SparseMatrix<double, 0, int> const&, double);
  66. template bool igl::is_symmetric<double, int>(Eigen::SparseMatrix<double, 0, int> const&, int);
  67. #endif