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.02.00.01.0},
            
{2.02.03.02.0},
            
{4.0-3.00.01.0},
            
{6.01.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();
    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Python 实现 Gauss-Jordan元法的代码,其包括了列主元选取的方法: ```python import numpy as np def pivot_matrix(M): """Returns the pivoting matrix for M, used in Doolittle's method.""" m = len(M) # Create an identity matrix, with floating point values id_mat = [[float(i ==j) for i in range(m)] for j in range(m)] # Rearrange the identity matrix such that the largest element of # each column of M is placed on the diagonal of of M for j in range(m): row = max(range(j, m), key=lambda i: abs(M[i][j])) if j != row: # Swap the rows id_mat[j], id_mat[row] = id_mat[row], id_mat[j] return id_mat def gauss_jordan(A, b): """Solves the equation Ax = b via Gauss-Jordan elimination.""" # Create the augmented matrix M = np.column_stack((A, b)) n, m = M.shape # Create the pivoting matrix P and the inverse of P id_mat = pivot_matrix(M) P = np.matrix(id_mat) P_inv = np.linalg.inv(P) # Apply the pivoting matrix to M M = P * M # Perform the Gauss-Jordan elimination for j in range(n): div = M[j, j] M[j, :] /= div for i in range(n): if i != j: sub = M[i, j] M[i, :] -= sub * M[j, :] # Separate the results (x) from the augmented matrix x = M[:, :-1] return P_inv * x # Example usage: A = np.array([[2, 1, -1], [3, 2, 1], [1, 0, 1]]) b = np.array([8, 13, 2]) x = gauss_jordan(A, b) print(x) ``` 在这个例子,我们使用了 NumPy 库来处理矩阵运算。函数 `pivot_matrix()` 生成了一个大小为 $n \times n$ 的矩阵,其第 $i$ 行和第 $j$ 行交换,是为了使得第 $j$ 列的最大元素出现在第 $j$ 行。函数 `gauss_jordan()` 对输入的矩阵 A 和向量 b 进行了消元操作,并返回解向量 x。在最后一个例子,我们使用这个函数解了一个线性方程组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值