Added way to check if matrix is orthogonal

This commit is contained in:
Robert Altner 2019-01-24 21:06:46 +01:00
parent 548de30100
commit af0572cc87
2 changed files with 40 additions and 2 deletions

View file

@ -44,6 +44,7 @@ public:
bool IsInvertable();
bool IsSquare();
bool IsOrthogonal();
// Transformation / arithmetic functions
@ -80,6 +81,8 @@ public:
Matrix operator*(const Matrix& other);
void operator*=(const Matrix& other);
bool operator==(const Matrix& other);
friend std::ostream& operator<<(std::ostream& stream, const Matrix& other);
private:
doubleMatrix m_matrix;
@ -226,6 +229,19 @@ bool Matrix::IsSquare()
return m_rows == m_cols;
}
////////////////////////////////////////////////////
/// \brief Returns wether the matrix is orthogonal
///
////////////////////////////////////////////////////
bool Matrix::IsOrthogonal()
{
if (!IsSquare())
return false;
return ((*this * _transpose(*this)) == Identity(m_rows));
}
////////////////////////////////////////////////////
/// \brief Prints the whole matrix to the console
@ -699,6 +715,25 @@ void Matrix::operator*=(const Matrix& other)
}
bool Matrix::operator==(const Matrix& other)
{
DimensionsFitting(*this, other);
bool equal = true;
for (int row = 0; row < m_rows; row++) {
for (int col = 0; col < m_cols; col++) {
if (m_matrix[row][col] == other.GetNumber(row, col)) {
equal = false;
break;
}
}
}
return equal;
}
std::ostream& operator<<(std::ostream& stream, const Matrix& other)
{
for (uint row = 0; row < other.m_rows; row++)

View file

@ -20,8 +20,7 @@ int main(int argc, char** argv)
myMatrix.SetNumber(2, 1, 0);
myMatrix.SetNumber(2, 2, 1);
Matrix inverse = myMatrix;
inverse.Invert();
Matrix inverse = Invert(myMatrix);
std::cout << myMatrix << std::endl;
@ -30,6 +29,10 @@ int main(int argc, char** argv)
std::cout << mul(myMatrix, inverse) << std::endl;
std::cout << Transpose(myMatrix) << std::endl;
std::cout << Transpose(myMatrix) * myMatrix << std::endl;
std::cout << myMatrix.IsOrthogonal() << std::endl;
getchar();
return 0;