I have been making a matrix class (as a learning exercise) and I have come across and issue whilst testing my inverse function.
I input a arbitrary matrix as such:
2 1 1
1 2 1
1 1 2
And got it to calculate the inverse and I got the correct result:
0.75 -0.25 -0.25
-0.25 0.75 -0.25
-0.25 -0.25 0.75
But when I tried multiplying the two together to make sure I got the identity matrix I get:
1 5.5111512e-017 0
0 1 0
-1.11022302e-0.16 0 1
Why am I getting these results? I would understand if I was multiplying weird numbers where I could understand some rounding errors but the sum it's doing is:
2 * -0.25 + 1 * 0.75 + 1 * -0.25
which is clearly 0, not 5.111512e-017
If I manually get it to do the calculation; eg:
std::cout << (2 * -0.25 + 1 * 0.75 + 1 * -0.25) << "\n";
I get 0 as expected?
All the numbers are represented as doubles. Here's my multiplcation overload:
Matrix operator*(const Matrix& A, const Matrix& B)
{
if(A.get_cols() == B.get_rows())
{
Matrix temp(A.get_rows(), B.get_cols());
for(unsigned m = 0; m < temp.get_rows(); ++m)
{
for(unsigned n = 0; n < temp.get_cols(); ++n)
{
for(unsigned i = 0; i < temp.get_cols(); ++i)
{
temp(m, n) += A(m, i) * B(i, n);
}
}
}
return temp;
}
throw std::runtime_error("Bad Matrix Multiplication");
}
and the access functions:
double& Matrix::operator()(unsigned r, unsigned c)
{
return data[cols * r + c];
}
double Matrix::operator()(unsigned r, unsigned c) const
{
return data[cols * r + c];
}
Here's the function to find the inverse:
Matrix Inverse(Matrix& M)
{
if(M.rows != M.cols)
{
throw std::runtime_error("Matrix is not square");
}
int r = 0;
int c = 0;
Matrix augment(M.rows, M.cols*2);
augment.copy(M);
for(r = 0; r < M.rows; ++r)
{
for(c = M.cols; c < M.cols * 2; ++c)
{
augment(r, c) = (r == (c - M.cols) ? 1.0 : 0.0);
}
}
for(int R = 0; R < augment.rows; ++R)
{
double n = augment(R, R);
for(c = 0; c < augment.cols; ++c)
{
augment(R, c) /= n;
}
for(r = 0; r < augment.rows; ++r)
{
if(r == R) { continue; }
double a = augment(r, R);
for(c = 0; c < augment.cols; ++c)
{
augment(r, c) -= a * augment(R, c);
}
}
}
Matrix inverse(M.rows, M.cols);
for(r = 0; r < M.rows; ++r)
{
for(c = M.cols; c < M.cols * 2; ++c)
{
inverse(r, c - M.cols) = augment(r, c);
}
}
return inverse;
}
Please read this paper: What Every Computer Scientist Should Know About Floating-Point Arithmetic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With