Gauss-Jordan消去法中完全选主元法求解线性方程组
算法名称:
完全主元法
算法描述:
1.
在系数矩阵A
中找到绝对值最大的数作为主元,并记录它的列号col
,行号row
,将第col
行与第row
行交换,即将此主元经过一次初等行变换放到主对角线上。
2.
移动过后,主元所在行每个元除以主元的值,使主元所在位置值为1
。
3.
进行初等行变换,使得主元所在列的其他元为0
,主元所在行不变。
4.
返回1
进行迭代,迭代总次数恰好为A
的行数。
注
:每次初等行变换都要求增广阵
b
同时进行相应变换。
实际例子
Coefficient matrix:
| 0.0 2.0 0.0 1.0 | 0.0 |
| 2.0 2.0 3.0 2.0 | -2.0 |
| 4.0 -3.0 0.0 1.0 | -7.0 |
| 6.0 1.0 -6.0 -5.0 | 6.0 |
-----------------------------------------------
After 1 time(s) elimination:
| 0.0 2.0 0.0 1.0 | 0.0 |
| 5.0 2.5 0.0 -0.5 | 1.0 |
| -1.0 -0.16666666666666666 1.0 0.8333333333333333 | -1.0 |
| 4.0 -3.0 0.0 1.0 | -7.0 |
-----------------------------------------------
After 2 time(s) elimination:
| 1.0 0.5 0.0 -0.1 | 0.2 |
| 0.0 2.0 0.0 1.0 | 0.0 |
| 0.0 0.33333333333333337 1.0 0.7333333333333333 | -0.8 |
| 0.0 -5.0 0.0 1.4 | -7.8 |
-----------------------------------------------
After 3 time(s) elimination:
| 1.0 0.0 0.0 0.03999999999999998 | -0.5800000000000001 |
| -0.0 1.0 -0.0 -0.27999999999999997 | 1.56 |
| 0.0 0.0 1.0 0.8266666666666667 | -1.3200000000000003 |
| 0.0 0.0 0.0 1.56 | -3.12 |
-----------------------------------------------
After 4 time(s) elimination:
| 1.0 0.0 0.0 0.0 | -0.5000000000000001 |
| 0.0 1.0 0.0 0.0 | 1.0 |
| 0.0 0.0 1.0 0.0 | 0.33333333333333304 |
| 0.0 0.0 0.0 1.0 | -2.0 |
-----------------------------------------------
The final solution matrix:
| 1.0 0.0 0.0 0.0 | -0.5000000000000001 |
| 0.0 1.0 0.0 0.0 | 1.0 |
| 0.0 0.0 1.0 0.0 | 0.33333333333333304 |
| 0.0 0.0 0.0 1.0 | -2.0 |
-----------------------------------------------
代码
package
com.nc4nr.chapter02.guassj;
public class GuassJ {
// 4 * 4 coefficient matrix a
double[][] a = {
{0.0, 2.0, 0.0, 1.0},
{2.0, 2.0, 3.0, 2.0},
{4.0, -3.0, 0.0, 1.0},
{6.0, 1.0, -6.0, -5.0}
};
// 4 * 1 coefficient matrix b
double[][] b = {
{0.0},
{-2.0},
{-7.0},
{6.0}
};
private void gaussj(double[][] a, int anrow,
double[][] b, int bncol) {
int icol = 0, irow = 0;
double big, dum, pivinv;
int n = anrow;
int m = bncol;
int[] indxr = new int[n],
indxc = new int[n],
ipiv = new int[n];
System.out.println("Coefficident matrix :");
output(a,4,b,1);
for (int i = 0; i < n; i++) ipiv[i] = 0;
for (int i = 0; i < n; i++) {
big = 0.0;
for (int j = 0; j < n; j++) {
if (ipiv[j] != 1) {
for (int k = 0; k < n; k++) {
if (ipiv[k] == 0) {
if (Math.abs(a[j][k]) >= big) {
big = Math.abs(a[j][k]);
irow = j;
icol = k;
}
}
}
}
}
ipiv[icol]= ipiv[icol] + 1;
if (irow != icol) {
for (int l = 0; l < n; l++) {
double mid = a[irow][l];
a[irow][l] = a[icol][l];
a[icol][l] = mid;
}
for (int l = 0; l < m; l++) {
double mid = b[irow][l];
b[irow][l] = b[icol][l];
b[icol][l] = mid;
}
}
indxr[i] = irow;
indxc[i] = icol;
if (a[icol][icol] == 0.0) System.out.println("gaussj: Singular Matrix");
pivinv = 1.0 / a[icol][icol];
a[icol][icol] = 1.0;
for (int l = 0; l < n; l++) {
if (l != icol) {
a[icol][l] *= pivinv;
}
}
for (int l = 0; l < m; l++) b[icol][l] *= pivinv;
for (int ll = 0; ll < n; ll++) {
if (ll != icol) {
dum = a[ll][icol];
a[ll][icol]=0.0;
for (int l = 0; l < n; l++) {
if (l != icol) {
a[ll][l] -= a[icol][l]*dum;
}
}
for (int l = 0; l < m; l++) b[ll][l] -= b[icol][l]*dum;
}
}
System.out.println("After " + (i + 1) + " time(s) elimination:");
output(a,4,b,1);
}
for (int l=n-1;l>=0;l--) {
if (indxr[l] != indxc[l]) {
for (int k = 0; k < n; k++) {
double mid = a[k][indxr[l]];
a[k][indxr[l]] = a[k][indxc[l]];
a[k][indxc[l]] = mid;
}
}
}
System.out.println("The final solution matrix:");
output(a,4,b,1);
}
private void output(double a[][], int anrow, double b[][], int bncol) {
for (int i = 0; i < anrow; i++) {
System.out.println(" | " + a[i][0] + " " +
a[i][1] + " " +
a[i][2] + " " +
a[i][3] + " | " + b[i][0] + " | ");
}
System.out.println("-----------------------------------------------");
}
public GuassJ() {
gaussj(a, 4, b, 1);
}
public static void main(String[] args) {
new GuassJ();
}
}
public class GuassJ {
// 4 * 4 coefficient matrix a
double[][] a = {
{0.0, 2.0, 0.0, 1.0},
{2.0, 2.0, 3.0, 2.0},
{4.0, -3.0, 0.0, 1.0},
{6.0, 1.0, -6.0, -5.0}
};
// 4 * 1 coefficient matrix b
double[][] b = {
{0.0},
{-2.0},
{-7.0},
{6.0}
};
private void gaussj(double[][] a, int anrow,
double[][] b, int bncol) {
int icol = 0, irow = 0;
double big, dum, pivinv;
int n = anrow;
int m = bncol;
int[] indxr = new int[n],
indxc = new int[n],
ipiv = new int[n];
System.out.println("Coefficident matrix :");
output(a,4,b,1);
for (int i = 0; i < n; i++) ipiv[i] = 0;
for (int i = 0; i < n; i++) {
big = 0.0;
for (int j = 0; j < n; j++) {
if (ipiv[j] != 1) {
for (int k = 0; k < n; k++) {
if (ipiv[k] == 0) {
if (Math.abs(a[j][k]) >= big) {
big = Math.abs(a[j][k]);
irow = j;
icol = k;
}
}
}
}
}
ipiv[icol]= ipiv[icol] + 1;
if (irow != icol) {
for (int l = 0; l < n; l++) {
double mid = a[irow][l];
a[irow][l] = a[icol][l];
a[icol][l] = mid;
}
for (int l = 0; l < m; l++) {
double mid = b[irow][l];
b[irow][l] = b[icol][l];
b[icol][l] = mid;
}
}
indxr[i] = irow;
indxc[i] = icol;
if (a[icol][icol] == 0.0) System.out.println("gaussj: Singular Matrix");
pivinv = 1.0 / a[icol][icol];
a[icol][icol] = 1.0;
for (int l = 0; l < n; l++) {
if (l != icol) {
a[icol][l] *= pivinv;
}
}
for (int l = 0; l < m; l++) b[icol][l] *= pivinv;
for (int ll = 0; ll < n; ll++) {
if (ll != icol) {
dum = a[ll][icol];
a[ll][icol]=0.0;
for (int l = 0; l < n; l++) {
if (l != icol) {
a[ll][l] -= a[icol][l]*dum;
}
}
for (int l = 0; l < m; l++) b[ll][l] -= b[icol][l]*dum;
}
}
System.out.println("After " + (i + 1) + " time(s) elimination:");
output(a,4,b,1);
}
for (int l=n-1;l>=0;l--) {
if (indxr[l] != indxc[l]) {
for (int k = 0; k < n; k++) {
double mid = a[k][indxr[l]];
a[k][indxr[l]] = a[k][indxc[l]];
a[k][indxc[l]] = mid;
}
}
}
System.out.println("The final solution matrix:");
output(a,4,b,1);
}
private void output(double a[][], int anrow, double b[][], int bncol) {
for (int i = 0; i < anrow; i++) {
System.out.println(" | " + a[i][0] + " " +
a[i][1] + " " +
a[i][2] + " " +
a[i][3] + " | " + b[i][0] + " | ");
}
System.out.println("-----------------------------------------------");
}
public GuassJ() {
gaussj(a, 4, b, 1);
}
public static void main(String[] args) {
new GuassJ();
}
}