首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

矩阵转置的函数如何编(用c++)

2012-02-25 
矩阵转置的函数怎么编(用c++)大家好:请教问题:用c++怎么编矩阵转置的函数,原矩阵和转置矩阵保存在二维数组

矩阵转置的函数怎么编(用c++)
大家好:
      请教问题:用c++   怎么编矩阵转置的函数,原矩阵和转置矩阵保存在二维数组中。


[解决办法]
#include <stdio.h>
#include <stdlib.h>

double * MatrixOpp(double *A,int m,int n); /*矩阵求逆*/
double * MatrixInver(double *A,int m,int n); /*矩阵转置*/
double Surplus(double A[],int m,int n); /*求矩阵行列式*/

double * MatrixOpp(double A[],int m,int n) /*矩阵求逆*/
{
int i,j,x,y,k;
double *SP=NULL,*AB=NULL,*B=NULL,X,*C;
SP=(double *)malloc(m*n*sizeof(double));
AB=(double *)malloc(m*n*sizeof(double));
B=(double *)malloc(m*n*sizeof(double));

X=Surplus(A,m,n);
X=1/X;

for(i=0;i <m;i++)
for(j=0;j <n;j++)
{
for(k=0;k <m*n;k++)
B[k]=A[k];
{
for(x=0;x <n;x++)
B[i*n+x]=0;
for(y=0;y <m;y++)
B[m*y+j]=0;
B[i*n+j]=1;
SP[i*n+j]=Surplus(B,m,n);
AB[i*n+j]=X*SP[i*n+j];
}
}
C=MatrixInver(AB,m,n);

return C;
}

double * MatrixInver(double A[],int m,int n) /*矩阵转置*/
{
int i,j;
double *B=NULL;
B=(double *)malloc(m*n*sizeof(double));

for(i=0;i <n;i++)
for(j=0;j <m;j++)
B[i*m+j]=A[j*n+i];
return B;
}

double Surplus(double A[],int m,int n) /*求矩阵行列式*/
{
int i,j,k,p,r;
double X,temp=1,temp1=1,s=0,s1=0;

if(n==2)
{
for(i=0;i <m;i++)
for(j=0;j <n;j++)
if((i+j)%2) temp1*=A[i*n+j];
else temp*=A[i*n+j];
X=temp-temp1;
}
else
{
for(k=0;k <n;k++)
{
for(i=0,j=k;i <m,j <n;i++,j++)
temp*=A[i*n+j];
if(m-i)
{
for(p=m-i,r=m-1;p> 0;p--,r--)
temp*=A[r*n+p-1];
}
s+=temp;
temp=1;
}

for(k=n-1;k> =0;k--)
{
for(i=0,j=k;i <m,j> =0;i++,j--)
temp1*=A[i*n+j];
if(m-i)
{for(p=m-1,r=i;r <m;p--,r++)
temp1*=A[r*n+p];}
s1+=temp1;
temp1=1;
}
X=s-s1;
}
return X;
}

/* Test */
int main()
{
int i,j;
double arr[5][5], *result, *t=arr;
for(i=0; i <5; i++)
for(j=0; j <5; j++)
scanf( "%lf ", &arr[i][j]);

result=MatrixOpp((double *)arr,5,5); //求逆

/*...其他操作,如显示结果*/
printf( "\n\nThe result is:\n ");
for(i=0; i <5*5; i++)
{
printf( "%lf\t ", *(result+i));
if(i%5==4)printf( "\n ");
}
free(result);
system( "PAUSE ");
return 0;
}
[解决办法]
#include <iostream>
using namespace std;
#define RANK 3 // N 阶矩阵
#define DEBUG //调试
////////////////////////////////////////////////////////////////////////
//判断数组内是否全为数字型
bool IsNum(char* num)
{
int flag = 0;
unsigned long Len;
Len = strlen(num);
for (int i = 0;i < Len;i ++)


{
if (num[i] < '0 ' || num[i] > '9 ') //非数字
{
if (num[i] == '. ') //是小数点
{
if (flag) //多个小数点
{
return false;
}
flag = 1;
}
else //非小数点
{
return false;
}
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////////
int main(void)
{
int i,j;
double *p; //指向一个一维数组
p = new double[RANK * RANK]; //动态分配
#ifdef DEBUG
//测试用(自动填充)
for (int k=0; k <RANK * RANK; k++)
*(p+k) = 1+k;
#else
//用户输入
char Input[50];
cout < < "请输入 " < <RANK < < "阶矩阵的元素: " < <endl < <endl;
for (i = 0; i < RANK; i ++)
{
for (j = 0; j < RANK; j ++)
{
error:
cout < < "请输入第 " < <i+1 < < "行,第 " < <j+1 < < "个元素为: ";
cin> > Input;
if (strlen(Input) > = 50) //输入过长
{
cout < < "你输入的值过长,请重新输入! " < <endl;
goto error;
}
else if (! IsNum(Input)) //检测合法性
{
//不合法
cout < < "你输入的值不合法,请重新输入! " < <endl;
goto error;
}
else
{
*(p + i * RANK + j) = atof( Input );
}
}
}
#endif
//显示用户输入
cout < < "用户输入为 " < <RANK < < "阶矩阵: " < <endl;
for (i = 0; i < RANK; i ++)
{
for (j = 0; j < RANK; j ++)
{
cout < <*(p + i * RANK + j) < < "\t ";
}
cout < <endl < <endl;
}
//开始转置
for (i = 0; i < RANK; i ++)
{
for (j = i; j < RANK; j ++)
{
if ( i != j ) //不在对角线上
{
//两数交换
*(p + i * RANK + j) += *(p + j * RANK + i); //a=a+b
*(p + j * RANK + i) = *(p + i * RANK + j) - *(p + j * RANK + i); //b=a-b
*(p + i * RANK + j) -= *(p + j * RANK + i); //a=a-b
//异或好像不能对double型进行操作,所以,用加减来代替
// *(p + i * RANK + j) ^= *(p + j * RANK + i); //a=a^b
// *(p + j * RANK + i) ^= *(p + i * RANK + j); //b=a^b
// *(p + i * RANK + j) ^= *(p + j * RANK + i); //a=a^b
}
}
}
//显示转置结果
cout < < "转置后的结果为: " < <endl;
for (i = 0; i < RANK; i ++)
{
for (j = 0; j < RANK; j ++)
{
cout < <*(p + i * RANK + j) < < "\t ";
}
cout < <endl < <endl;
}
return 1;
}

热点排行