选主元求矩阵逆(C++)

时间:2016-01-17 03:53:32
【文件属性】:

文件名称:选主元求矩阵逆(C++)

文件大小:17KB

文件格式:DOCX

更新时间:2016-01-17 03:53:32

矩阵的逆C++

使用选主元法求矩阵的逆 #include #include #include #include using namespace std; //选主元求矩阵的逆 vector SolveInverseMatrix(vector dt) { double inputMatrix[5][5]; int mark = 1; double detVal = 0.0; vector outputVal; vector::iterator ibeg = dt.begin(); for (int i = 0; i < 5; ++i) { for (int j = 0; j < 5; ++j) { inputMatrix[i][j] = *ibeg; ++ibeg; } } int row[5]; int column[5]; for (int k = 0; k < 5; ++k) { double maxVal = 0.0; for (int i = k; i < 5; ++i) { for (int j = k; j < 5; ++j) { double elem = abs(inputMatrix[i][j]); if (elem > maxVal) { maxVal = elem; row[k] = i; column[k] = j; } } } try { if (row[k] != k) { mark = -mark; int temp = row[k]; swap(inputMatrix[k][0],inputMatrix[temp][0]); swap(inputMatrix[k][1],inputMatrix[temp][1]); swap(inputMatrix[k][2],inputMatrix[temp][2]); swap(inputMatrix[k][3],inputMatrix[temp][3]); swap(inputMatrix[k][4],inputMatrix[temp][4]); } if (column[k] != k) { mark = -mark; int temp = column[k]; swap(inputMatrix[0][k],inputMatrix[0][temp]); swap(inputMatrix[1][k],inputMatrix[1][temp]); swap(inputMatrix[2][k],inputMatrix[2][temp]); swap(inputMatrix[3][k],inputMatrix[3][temp]); swap(inputMatrix[4][k],inputMatrix[4][temp]); } } catch (exception ex) { } if (abs(maxVal) < 0.0001) return 0; detVal = inputMatrix[k][k]; inputMatrix[k][k] = 1.0 / inputMatrix[k][k]; for (int j = 0; j < 5; ++j) { if(j != k)inputMatrix[k][j] *= inputMatrix[k][k]; inputMatrix[k][j] /= maxVal; } for (int i = 0; i < 5; ++i) { if (i != k) { for (int j = 0; j < 5; ++j) { if(j != k)inputMatrix[i][j] = inputMatrix[i][j] - inputMatrix[i][k] * inputMatrix[k][j]; } } } for (int i = 0; i < 5; ++i) { if (i != k) { inputMatrix[i][k] *= -inputMatrix[k][k]; } } } for (int k = 4; k >= 0; --k) { if (column[k] != k) { int temp = column[k]; swap(inputMatrix[k][0],inputMatrix[temp][0]); swap(inputMatrix[k][1],inputMatrix[temp][1]); swap(inputMatrix[k][2],inputMatrix[temp][2]); swap(inputMatrix[k][3],inputMatrix[temp][3]); swap(inputMatrix[k][4],inputMatrix[temp][4]); } if (row[k] != k) { int temp = row[k]; swap(inputMatrix[0][k],inputMatrix[0][temp]); swap(inputMatrix[1][k],inputMatrix[1][temp]); swap(inputMatrix[2][k],inputMatrix[2][temp]); swap(inputMatrix[3][k],inputMatrix[3][temp]); swap(inputMatrix[4][k],inputMatrix[4][temp]); } } for (int i = 0; i < 5; ++i) { for (int j = 0; j < 5; ++j) { outputVal.push_back(inputMatrix[i][j]); } } detVal *= mark; //行列式的值 return outputVal; } int main() { int count = 0; vector tempVector; tempVector.push_back(5); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(5); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(5); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(5); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(0); tempVector.push_back(5); vector outputVector = SolveInverseMatrix(tempVector); vector::iterator ibeg = outputVector.begin(); vector::iterator iend = outputVector.end(); for (;ibeg != iend; ++ibeg) { ++count; cout<<*ibeg<<"\t\t"; if (count % 5 == 0) { cout<


网友评论