Quote:Original post by Repetit
Thanks for the quick answer!
Well, the problem is the cases in which IsHermitian() is called on for example a CMatrix<double> object. This will generate compiler errors, but the function call should be valid. CMatrix<double> is obviosly a real matrix (cannot contain complex values) so IsHermitian() should return true if the matrix is symmetric, even on CMatrix<double> objects. And if I have to assume that IsHermitian() is only called on CMatrix<CNumber> objects, what is the point of making it a template anyway? The template should be generic right?
Oh, I see - you want to be able to call IsHermitian() on any matrix type, but the behavior should be different depending on whether the elements are real only, or whether some of them can be complex or imaginary.
There are various ways to accomplish this; I won't presume to suggest a 'best' way, but I'll tell you one way it can be done. You could make IsHermitian() a non-member function, and specialize it:
template < class T >bool IsHermitian(const Matrix<T>& m){ // Matrix is real - just check if it's symmetric}bool IsHermitian(const Matrix<Number>& m){ // Matrix may have complex elements - perform full check}
I realize you currently have it as a member function, and may want to keep it that way. In this case there are other techniques you could employ, such as defining a numerical_traits<> class for the element type that determines the behavior of the function. If you're writing a matrix class that is intended to be used with complex and imaginary numbers as well as real, you'll probably encounter this problem again, so it might be a good idea to get the architecture in place for it now.
[Edit: Or use eyal's solution, below...]