#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
//初始化矩阵
void InitMatrix(double *const lMatrix, const int &dim)
{
cout << "按行输入矩阵!" << endl << endl;
for (int i=0; i<dim; i++)
{
cout << "第" << i+1 << "行" << endl;
for (int j=0; j<dim; j++)
{
cout << "a[" << i+1 << "][" << j+1 << "]: ";
cin >> lMatrix[i*dim+j];
}
cout << endl;
}
}
//打印矩阵
void DisplayMatrix(double *const pl, const int &dim)
{
for (int i=0; i<dim; i++)
{
for (int j=0; j<dim; j++)
{
cout << setw(18) << pl[i*dim+j] ;
}
cout << endl;
}
cout << endl;
}
//求pMatrix的逆矩阵,并存结果于矩阵_pMatrix中
void ContraryMatrix(double *const pMatrix, double *const _pMatrix, const int &dim)
{
double *tMatrix = new double[2*dim*dim];
for (int i=0; i<dim; i++){
for (int j=0; j<dim; j++)
tMatrix[i*dim*2+j] = pMatrix[i*dim+j];
}
for (i=0; i<dim; i++){
for (int j=dim; j<dim*2; j++)
tMatrix[i*dim*2+j] = 0.0;
tMatrix[i*dim*2+dim+i] = 1.0;
}
//Initialization over!
for (i=0; i<dim; i++)//Process Cols
{
double base = tMatrix[i*dim*2+i];
if (fabs(base) < 1E-300){
cout << endl << "求逆矩阵过程中被零除,无法求解!" << endl;
exit(0);
}
for (int j=0; j<dim; j++)//row
{
if (j == i) continue;
double times = tMatrix[j*dim*2+i]/base;
for (int k=0; k<dim*2; k++)//col
{
tMatrix[j*dim*2+k] = tMatrix[j*dim*2+k] - times*tMatrix[i*dim*2+k];
}
}
for (int k=0; k<dim*2; k++){
tMatrix[i*dim*2+k] /= base;
}
}
for (i=0; i<dim; i++)
{
for (int j=0; j<dim; j++)
_pMatrix[i*dim+j] = tMatrix[i*dim*2+j+dim];
}
delete[] tMatrix;
}
void main()
{
int dim;
cout << "\t\t欢迎使用逆矩阵求解程序!" << endl << endl;
cout << "矩阵的维数:" << endl;
cin >> dim;
double *const MatrixL = new double[dim*dim]; //矩阵
double *const _MatrixL = new double[dim*dim]; //逆矩阵
InitMatrix(MatrixL, dim);
system("cls");
cout << "矩阵为:" << endl;
DisplayMatrix(MatrixL, dim);
ContraryMatrix(MatrixL, _MatrixL, dim);//求逆
cout << endl << "逆矩阵为:" << endl;
DisplayMatrix(_MatrixL, dim);
delete[] MatrixL;
delete[] _MatrixL;
}
是通过初等行变换求解逆矩阵的,非用伴随矩阵!(那个太繁琐)
比如求:
1 2 3
2 2 1
3 4 3
则把下面矩阵:
1 2 3 1 0 0
2 2 1 0 1 0
3 4 3 0 0 1
通过初等行变换变为:
1 0 0 x x x
0 1 0 x x x
0 0 1 x x x
则x矩阵就是要求的逆矩阵了 |
//***************************
//求任何一个矩阵的逆矩阵
//***************************
#include <stdio.h>
#include <malloc.h>
void main( void )
{
float *buffer,*p; //定义数组首地址指针变量
short int row,num; //定义矩阵行数row及矩阵元素个数
short int i,j;
float determ; //定义矩阵的行列式
float comput_D(float *p,short int n); //求矩阵的行列式
float Creat_M(float *p, short int m,short int n,short int k); //求代数余子式
void Print( float *p,short int n); //打印n×n的矩阵
printf("\nPlease input the number of rows: ");
scanf("%d",&row);
num=2 * row * row;
buffer = (float *)calloc(num, sizeof(float)); //分配内存单元
p=buffer;
if(p != NULL)
{
for(i=0;i<row;i++) //输入各单元值
{
printf("Input the number of %d row ",i+1);
for(j=0;j<row;j++)
{
scanf("%f",p++);
}
}
}
else
printf( "Can't allocate memory\n" );
printf("\nThe original matrix is:\n");
Print(buffer,row); //打印该矩阵
determ=comput_D(buffer,row); //求整个矩阵的行列式
p=buffer + row * row;
if (determ != 0)
{
for (i=0;i<row; i++) //求逆矩阵
for (j=0; j<row; j++)
*(p+j*row+i)= Creat_M(buffer,i,j,row)/determ;
printf("The determinant is %G\n",determ);
p=buffer + row * row;
printf("\nThe inverse matrix is:\n");
Print(p,row); //打印该矩阵
}
else
printf("The determnant is 0, and there is no inverse matrix !\n");
free( buffer );
}
//--------------------------------------------------------
//功能:求矩阵 n X n 的行列式
//入口参数:矩阵首地址 p;矩阵行数 n
//返回值:矩阵的行列式值
//--------------------------------------------------------
float comput_D(float *p,short int n)
{
short int i,j,m; //i--row; j--column
short int lop=0;
float result=0;
float mid=1;
if (n!=1)
{
lop=(n==2)?1:n; //控制求和循环次数,若为2阶,则循环1次,否则为n次
for(m=0;m<lop;m++)
{
mid=1; //顺序求和
for(i=0,j=m;i<n;i++,j++)
mid = mid * ( *(p+i*n+j%n) );
result+=mid;
}
for(m=0;m<lop;m++)
{
mid=1; //逆序相减
for(i=0,j=n-1-m+n; i<n; i++,j--)
mid=mid * ( *(p+i*n+j%n));
result-=mid;
}
}
else result=*p;
return(result);
}
//----------------------------------------------------
//功能:求k×k矩阵中元素A(mn)的代数余子式
//入口参数:k×k矩阵首地址;元素A的下标m,n; 矩阵行数 k
//返回值: k×k矩阵中元素A(mn)的代数余子式
//----------------------------------------------------
float Creat_M(float *p, short int m,short int n,short int k)
{
short int len;
short int i,j;
float mid_result=0;
short int quo=1;
float *p_creat,*p_mid;
len=(k-1)*(k-1);
p_creat = (float *)calloc(len, sizeof(float)); //分配内存单元
p_mid=p_creat;
for(i=0;i<k;i++)
for(j=0;j<k;j++)
{
if (i!=m && j!=n)
*p_mid++ =* (p+i*k+j);
}
// Print(p_creat,k-1);
quo = (m + n) %2==0 ? 1:-1;
mid_result = (float ) quo * comput_D(p_creat,k-1);
free(p_creat);
return(mid_result);
}
//-------------------------------------------
//功能:打印n×n的矩阵
//入口参数:n×n矩阵的首地址;该矩阵的行数 n
//返回值: 无
//-------------------------------------------
void Print( float *p,short int n)
{
int i,j;
for (i=0;i<n;i++)
{
for (j=0; j<n;j++)
printf("%10G ",*p++);
printf("\n");
}
printf("--------------\n");
}