The
continues to solve the equation group.
Gauss elimination Method: http://blog.csdn.net/qq_26025363/article/details/53027926
Column main element Gauss elimination method: Http://blog.csdn.net/qq_ 26025363/article/details/53044843,
This time using LU decomposition to solve a system of equations, the idea is to decompose a matrix into a unit of the lower triangular matrix L and an upper triangular matrix U, which belongs to the triangular decomposition method of the Matrix, Also known as Doolittle (Doolittle) decomposition. In fact, every step of the elimination operation of Gauss elimination method is equal to a primary displacement matrix of left multiplication. The proof of this method should be in the textbook of numerical analysis or linear algebra, and no proof or explanation is given here. This article mainly gives the specific C + + implementation process. The
is the same as it is, put the code first.
#include <iostream> using namespace std;
const int n = 3;
Alu of matrix decomposition void ALU (double a[n][n], double b[n]) {double L[n][n] = {0};
Double U[n][n] = {0};
int I, r, K;
The first row of the U is assigned for (i = 0; i<n; i++) {u[0][i] = A[0][i];
The assignment for (i = 1; i<n; i++) {l[i][0] = a[i][0]/u[0][0) in the first column of L; (r = 1; r<n; r++) {for (i = r; I <n; i++) {Doub) compute the remainder of the number of rows and L of U
Le sum1 = 0;
for (k = 0; K < R k++) {sum1 + = l[r][k] * U[k][i];
cout << "" << R << "" << sum1 << Endl;
} U[r][i] = a[r][i]-sum1;
} if (R!=n) for (i=r+1;i<n;i++) {double sum2 = 0;
for (k = 0; k<r; k++) {sum2 + = l[i][k] * U[k][r]; } L[i][r] = (A[i][r]-sum2)/u[r][r];
Double Y[n] = {0};
Y[0] = b[0];
for (i = 1; i<n; i++) {double sum3 = 0;
for (k = 0; k<i; k++) sum3 + = l[i][k] * Y[k];
Y[i] = b[i]-sum3;
Double X[n] = {0};
X[n-1] = y[n-1]/u[n-1][n-1];
for (i = n-2 i >= 0; i--) {double sum4 = 0;
for (k = i + 1; k<n; k++) Sum4 + = u[i][k] * X[k];
X[i] = (Y[i]-SUM4)/u[i][i];
for (i = 0; i<n; i++) cout << "x[" << i + 1 << "]=" << X[i] << Endl;
Return
int main () {double a[3][3] = {1,2,3,2,5,2,3,1,5};
Double B[3] = {14,18,20};
ALU (A, b);
return 0;
}
To analyze the implementation process, first of all, a=lu decomposition, according to the decomposition method, U first row and L of the first column is initialized, you can simply calculate the value of the assignment, and then calculate the remaining rows and columns. Here's a question of the order of computation, you must first calculate a row of U, can calculate L of a column, otherwise it is the wrong calculation method, I started to realize the time to make this blunder, picked a long time bug, there is a point of attention is the cycle variable range of issues, but also more difficult to grasp. LU decomposition, you can be directly solved, first press ly=b, solve Y, and then press ux=y, solve X. This is exactly the case above. That is to ask for Ax=b, first A=lu, then Lux=b, first ly=b, find Y, then ux=y, and find X. The time complexity of the
solving a triangular matrix is O (n^2), but the decomposition process is O (n^3), but overall, the time complexity is almost the same as the Gaussian elimination, but the main element of the Gaussian elimination method is avoided 0.
Space Performance Analysis: My implementation method is not the most provincial space, I each intermediate matrix has opened up a new space, no doubt wasted a lot of unused space. Given the improved method, the A=lu decomposed elements can be stored in a matrix, because when the AIJ element is used, it is not used, so it can occupy the original space. But each has its pros and cons, consider if it is a matrix of thousands of elements, reference to the argument, so that the
will undoubtedly change the original matrix, if you do not want to change the original matrix, or honestly build a matrix, and then put Lu all put in, I was built 2 matrices, a great waste. But the implementation is simple, logic is simple, so put it on.
If the article has errors or irregularities, please point out, thank you.