Skip to content

Commit

Permalink
Implemented the frobenius_norm for real matrices.
Browse files Browse the repository at this point in the history
Implemented are_similar matrix (fuzzy) similarity check
  • Loading branch information
cassinaj committed Jun 10, 2015
1 parent 481b0cc commit 2781153
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions include/fl/util/math/linear_algebra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,40 @@ void invert_diagonal_Vector(const SrcDiagonalMatrix& diagonal,
}
}


/**
* \ingroup linear_algebra
* Computes the Frobenius norm of a given real matrix.
*
* The Frobenius norm is given by
*
* \f$
* \| A \|_F := \sqrt{\sum_{i=1}^m \sum_{j=1}^n |a_{ij} |^2 }
* \f$
*/
template <typename Derived>
double frobenius_norm(const Eigen::MatrixBase<Derived>& a)
{
return std::sqrt(a.array().abs().square().sum());
}


/**
* Checks whether two matrices of the same size are similiar w.r.t. some
* \f$\epsilon\f$.
*
* The similarity check is based of the Frobenius Norm:
*
* \f$ are_similar(A, B) = \| A - B \|_F < \epsilon\f$
*/
template <typename DerivedA, typename DerivedB>
bool are_similar(const Eigen::MatrixBase<DerivedA>& a,
const Eigen::MatrixBase<DerivedB>& b,
const double epsilon = 1.e-6)
{
return frobenius_norm(a - b) < epsilon;
}

}

#endif

0 comments on commit 2781153

Please sign in to comment.