The following function finds the inverse of a matrix (actually its not complete at all):
Code:void Matrix::Inverse() { float det; det = this->Determinant(); if(det == 0) { cout << "The matrix does not have an inverse."; return; } Matrix *identity = new Matrix(a,b); // creates nxn matrix identity->Identity(); // make it an identity matrix Matrix *temp = new Matrix(a,b); temp = this; // so we don't change the original matrix // forward elimination steps for(x = 0; x < a; x++) // a is the number of rows { y = 0; temp->elimination(((float)(temp->Array[x+1][y]/temp->Array[y][y])), (y+1), (x+2)); temp->showMatrix(); } }
elimination is defined as:
Code:void Matrix::elimination(float multiple, int row1, int row2) { // subtracts multiple*row1 from row2 float temp; for(y = 0; y < b; y++) { temp = Array[row2-1][y] - (multiple*Array[row1-1][y]); Array[row2-1][y] = temp; } }
Anyways, if I have the matrix A:
2 1 3
1 2 3
4 2 1
if I do A.Inverse() it SHOULD do this:
2 1 3
0 1.5 1.5
4 2 1
2 1 3
0 1.5 1.5
0 0 -5
But it only does ONE step - it should display a second matrix, even if it isn't right, shouldn't it?
here's rest of the code for reference:
http://dydx.no-ip.com:8080/matrix.cpp
http://dydx.no-ip.com:8080/matrix.h
Thanks!



LinkBack URL
About LinkBacks


