is_symmetric.cpp 2.0 KB

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