Matrix Inversion (using partial pivoting)

本文详细阐述了使用部分置换的矩阵逆运算算法实现过程,包括矩阵增广、上三角化、下三角化以及最终求得逆矩阵的方法。
部署运行你感兴趣的模型镜像

//

//  main.cpp

//  2015 New Start

//

//  Created by zr9558 on 13/2/15.

//  Copyright (c) 2015 zr9558. All rights reserved.

//


#include <iostream>

using namespace std;

#include<math.h>


// 5.3 Algorithm Matrix_Inversion (using partial pivoting)


const double eps=1e-10;


int max1( double b[], int k, int n) // find the pivot from b[k] to b[n-1], absolute value

{

    double max=fabs(b[k]); int index=k;

    for( int i=k; i!=n; ++i)

        if( fabs(b[i])>max) { max=fabs(b[i]); index=i;}

    

    return index;

}


int main()

{

    double a[100][200], b[100];

    int n;

    cout<<"Enter the dimension of the matrix"<<endl;

    cin>>n;

    cout<<"Enter the matrix"<<endl;

    for( int i=0; i!=n; ++i)

        for( int j=0; j!=n; ++j)

            cin>>a[i][j];

    

    for( int i=0; i!=n; ++i) //Augment the matrix A

    {

        for( int j=n; j!=2*n; ++j)

        {

            if( j-i==n) a[i][j]=1;

            else a[i][j]=0;

        }

    }

    

    // Make upper triangular form;

    

    for( int k=0; k!=n; ++k)

    {

        for( int i=k; i!=n; ++i)

            b[i]=a[i][k];

        

        int j=max1(b,k,n); // find the pivot from the elements a[k][k],...,a[n-1][k] in the kth column of A and let a[j][k] be the pivot;

        

        if( fabs(a[j][k])<=eps) // if a[j][k]==0, then not invertible;

        {

            cout<<"not invertible"<<endl;

            exit(0);

        }

        

        if( j!=k)

        {

            for( int i=0; i!=2*n; ++i)

            {

                double temp=a[j][i]; a[j][i]=a[k][i]; a[k][i]=temp;

            }

        }

        

        if( a[k][k]!=1) // if a[k][k]!=1, then divide all the elements of kth row by a[k][k],

        {

            double temp=a[k][k];

            for( int i=0; i!=2*n; ++i)

                a[k][i]/=temp;

        }

        

        for( int j=k+1; j!=n; ++j) // substract a[j][k] times the kth row to the jth row for j=k+1,...,n-1;

        {

            double temp=a[j][k];

            for( int i=0; i!=2*n; ++i)

                a[j][i]-=temp*a[k][i];

        }

    }

    

    

    // Make the left half of A a unit matrix;

    

    for( int k=1; k!=n; ++k)

    {

        for( int j=k-1; j>=0; --j) // substract a[j][k] times the kth row to the jth row for j=k-1,...,0;

        {

            double temp=a[j][k];

            for( int i=0; i!=2*n; ++i)

                a[j][i]-=temp*a[k][i];

        }

    }

    

    

    //output the inverse matrix

    cout<<"Inverse Matrix is"<<endl;

    for( int j=0; j!=n; ++j)

    {

        for( int i=n; i!=2*n; ++i)

        {

            cout<<a[j][i]<<" ";

        }

        cout<<endl;

    }

    

    

    

    return 0;

}

您可能感兴趣的与本文相关的镜像

AutoGPT

AutoGPT

AI应用

AutoGPT于2023年3月30日由游戏公司Significant Gravitas Ltd.的创始人Toran Bruce Richards发布,AutoGPT是一个AI agent(智能体),也是开源的应用程序,结合了GPT-4和GPT-3.5技术,给定自然语言的目标,它将尝试通过将其分解成子任务,并在自动循环中使用互联网和其他工具来实现这一目标

在Python的`factor_analyzer`库中,出现`UserWarning: The inverse of the variance-covariance matrix was calculated using the Moore-Penrose generalized matrix inversion, due to its determinant being at or very close to zero.`警告,通常是因为方差 - 协方差矩阵的行列式接近或等于零,导致无法使用常规方法求逆,只能使用Moore - Penrose广义逆。以下是一些解决办法: #### 1. 数据预处理 - **去除高度相关的特征**:高度相关的特征会使方差 - 协方差矩阵接近奇异,可通过计算特征之间的相关性,去除相关性高于某个阈值(如0.9)的特征。 ```python import pandas as pd import numpy as np from factor_analyzer import FactorAnalyzer # 假设data是一个DataFrame data = pd.DataFrame(np.random.randn(100, 5)) corr_matrix = data.corr().abs() upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1).astype(np.bool_)) to_drop = [column for column in upper.columns if any(upper[column] > 0.9)] data = data.drop(data[to_drop], axis=1) fa = FactorAnalyzer() fa.fit(data) ``` - **增加样本量**:样本数量过少可能导致方差 - 协方差矩阵不稳定,增加样本量可使矩阵更稳定。 #### 2. 调整算法参数 - **使用正则化**:在某些情况下,可通过正则化方法使方差 - 协方差矩阵更稳定。不过`factor_analyzer`库本身可能没有直接的正则化参数,可考虑使用其他支持正则化的因子分析库。 #### 3. 检查数据质量 - **处理缺失值**:数据中的缺失值可能会影响方差 - 协方差矩阵的计算,可使用均值、中位数等方法填充缺失值。 ```python data = data.fillna(data.mean()) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值