重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
#include stdlib.h
创新互联公司是一家专注网站建设、网络营销策划、重庆小程序开发公司、电子商务建设、网络推广、移动互联开发、研究、服务为一体的技术型公司。公司成立十载以来,已经为成百上千家门帘各业的企业公司提供互联网服务。现在,服务的成百上千家客户与我们一路同行,见证我们的成长;未来,我们一起分享成功的喜悦。
#include math.h
#include stdio.h
int brinv(double a[], int n)
{ int *is,*js,i,j,k,l,u,v;
double d,p;
is=malloc(n*sizeof(int));
js=malloc(n*sizeof(int));
for (k=0; k=n-1; k++)
{ d=0.0;
for (i=k; i=n-1; i++)
for (j=k; j=n-1; j++)
{ l=i*n+j; p=fabs(a[l]);
if (pd) { d=p; is[k]=i; js[k]=j;}
}
if (d+1.0==1.0)
{ free(is); free(js); printf("err**not inv\n");
return(0);
}
if (is[k]!=k)
for (j=0; j=n-1; j++)
{ u=k*n+j; v=is[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (js[k]!=k)
for (i=0; i=n-1; i++)
{ u=i*n+k; v=i*n+js[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
l=k*n+k;
a[l]=1.0/a[l];
for (j=0; j=n-1; j++)
if (j!=k)
{ u=k*n+j; a[u]=a[u]*a[l];}
for (i=0; i=n-1; i++)
if (i!=k)
for (j=0; j=n-1; j++)
if (j!=k)
{ u=i*n+j;
a[u]=a[u]-a[i*n+k]*a[k*n+j];
}
for (i=0; i=n-1; i++)
if (i!=k)
{ u=i*n+k; a[u]=-a[u]*a[l];}
}
for (k=n-1; k=0; k--)
{ if (js[k]!=k)
for (j=0; j=n-1; j++)
{ u=k*n+j; v=js[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (is[k]!=k)
for (i=0; i=n-1; i++)
{ u=i*n+k; v=i*n+is[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
}
free(is); free(js);
return(1);
}
void brmul(double a[], double b[],int m,int n,int k,double c[])
{ int i,j,l,u;
for (i=0; i=m-1; i++)
for (j=0; j=k-1; j++)
{ u=i*k+j; c[u]=0.0;
for (l=0; l=n-1; l++)
c[u]=c[u]+a[i*n+l]*b[l*k+j];
}
return;
}
int main()
{ int i,j;
static double a[4][4]={ {0.2368,0.2471,0.2568,1.2671},
{1.1161,0.1254,0.1397,0.1490},
{0.1582,1.1675,0.1768,0.1871},
{0.1968,0.2071,1.2168,0.2271}};
static double b[4][4],c[4][4];
for (i=0; i=3; i++)
for (j=0; j=3; j++)
b[i][j]=a[i][j];
i=brinv(a,4);
if (i!=0)
{ printf("MAT A IS:\n");
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",b[i][j]);
printf("\n");
}
printf("\n");
printf("MAT A- IS:\n");
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",a[i][j]);
printf("\n");
}
printf("\n");
printf("MAT AA- IS:\n");
brmul(b,a,4,4,4,c);
for (i=0; i=3; i++)
{ for (j=0; j=3; j++)
printf("%13.7e ",c[i][j]);
printf("\n");
}
}
}
#include stdio.h
#include stdlib.h
#include malloc.h
void MatrixOpp(double *A, int m, int n, double* invmat);
void MatrixInver(double *A, int m, int n, double* invmat);
double Surplus(double A[], int m, int n);
int matrix_inv(double* p, int num, double* invmat);
void MatrixOpp(double A[], int m, int n, double* invmat)
{
int i, j, x, y, k;
double *SP = NULL, *AB = NULL, *B = NULL, X;
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];
}
}
MatrixInver(AB, m, n, invmat);
free(SP);
free(AB);
free(B);
}
void MatrixInver(double A[], int m, int n, double* invmat)
{
int i, j;
double *B = invmat;
for (i = 0; i n; i++)
for (j = 0; j m; j++)
B[i * m + j] = A[j * n + i];
}
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;
}
int matrix_inv(double* p, int num, double* invmat)
{
if (p == NULL || invmat == NULL)
{
return 1;
}
if (num 10)
{
return 2;
}
MatrixOpp(p, num, num, invmat);
return 0;
}
int main()
{
int i, j;
int num;
double *arr=NULL;
double *result=NULL;
int flag;
printf("请输入矩阵维数:\n");
scanf("%d",num);
arr=(double *)malloc(sizeof(double)*num*num);
result=(double *)malloc(sizeof(double)*num*num);
printf("请输入%d*%d矩阵:\n", num, num);
for (i = 0; i num; i++)
{
for (j = 0; j num; j++)
{
scanf("%lf", arr[i * num + j]);
}
}
flag = matrix_inv(arr, num, result);
if(flag==0)
{
printf("逆矩阵为:\n");
for (i = 0; i num * num; i++)
{
printf("%lf\t ", *(result + i));
if (i % num == (num - 1))
printf("\n");
}
}
else if(flag==1)
{
printf("p/q为空\n");
}
else
{
printf("超过最大维数\n");
}
system("PAUSE");
free(arr);
free(result);
return 0;
}
//源程序如下#includestdio.h
#includeconio.h
#includestring.h
#includeiostream.h
#includestdlib.h
#includemath.h
#define max 100void inputstyle(int *); //输入函数
void input(int **,int); //输入函数
long danx(int **,int);
int sgnx(int);
void martx(int **,int);int main(void)
{
int style=0,i=0;
int matrix[max][max],*p[max];
for(i=0;imax;i++)*(p+i)=matrix[i]; //*(p+i)是指针,指向第i个字符串
char exit1=' ';
while(exit1!='E' exit1!='e'){ printf("求n阶矩阵的逆\n"); inputstyle(style);
input(p,style);
printf("原矩阵为:\n");
for(i=0;istyle;i++){
for(int j=0;jstyle;j++){
printf("%4d",matrix[i][j]);
}
printf("\n");
}
martx(p,style);
printf("\n");
printf("Exit=e Continue=Press any key\n");
cinexit1;
fflush(stdin);
printf("\n\n"); }
return(0);
} void input(int **p,int n){
for(int i=0;in;i++){
for(int j=0;jn;j++){
printf("输入矩阵(%d行,%d列)元素:",j+1,i+1);
*(*(p+j)+i)=0;
scanf("%d",*(p+j)+i);
fflush(stdin);
}
}
}void inputstyle(int *style){
do{
printf("输入矩阵n*n阶数n(0n%d):",max);
fflush(stdin);
scanf("%d",style);
fflush(stdin);
}while(*style=0 *stylemax);
}long danx(int **p,int n){
int i=0,j1=0,k1=0,j2=0,k2=0;
long sum=0;
int operate[max][max],*po[max];
for(i=0;imax;i++)*(po+i)=operate[i]; if(n==1)return *(*(p+0)+0);
else{
for(i=0;in;i++){
for(j1=1,j2=0;j1n;j1++,j2++){
k1=-1;k2=-1;
while(k2n-1){
k1++;
k2++;
if(k1==i)k1++;
*(*(po+j2)+k2)=*(*(p+j1)+k1);
}
}
/*for(int i1=0;i1n-1;i1++){
for(int h1=0;h1n-1;h1++){
printf("(%d,%d)%d ",i1,h1,*(*(po+h1)+i1));
}
printf("\n");
}*/
sum+=*(*(p+0)+i) * sgnx(1+i+1) * danx(po,n-1);
}
return sum;
}
}int sgnx(int i){
if(i%2==0)return(1);
else return(-1);
}void martx(int **p,int n){
int i=0,j=0,j1=0,k1=0,j2=0,k2=0,num=0;
int tramform[max][max];
int operate[max][max],*po[max];
for(i=0;imax;i++)*(po+i)=operate[i];
num=danx(p,n);
if(num==0)printf("矩阵不可逆\n");
else{
if(n==1)printf("矩阵的逆为: 1/%d\n",num);
else{
printf("矩阵的逆为: 系数 1/%d *\n",num);
for(i=0;in;i++){
for(j=0;jn;j++){
j1=-1;j2=-1;
while(j2n-1){
j1++;j2++;
if(j1==j)j1++; k1=-1;k2=-1;
while(k2n-1){
k1++;
k2++;
if(k1==i)k1++;
*(*(po+j2)+k2)=*(*(p+j1)+k1);
}
}
tramform[i][j]=sgnx(2+i+j) * danx(po,n-1);
}
}
for(i=0;in;i++){
for(j=0;jn;j++){
printf("%4d",tramform[i][j]);
}
printf("\n");
}
}
}
}
//运行结果//希望对你有帮助