Matrix basic operations (plus and minus multiplication, inversion, transpose)

Source: Internet
Author: User
Tags mul

Look at the template, find the best understanding, best use the matrix basic operation template

#define MAXN 100
#define ZERO (x) (Fabs (x) <1e-10)
struct MAT
{
int n,m;
Double DATA[MAXN][MAXN];
};
Matrix Plus and minus multiplication
int Add (mat& c,const mat& a,const mat& b)
{
int i,j,k;
if (a.m!=b.m| | A.N!=B.N)
return 0;
C.N=A.N;
C.M=A.M;
for (i=0; i<c.n; i++)
for (j=0; j<c.m; j + +)
C.DATA[I][J]=A.DATA[I][J]+B.DATA[I][J];
return 1;
}
int Jian (mat& c,const mat& a,const mat& b)
{
int i,j,k;
if (a.m!=b.m| | A.N!=B.N)
return 0;
C.N=A.N,C.M=A.M;
memset (c.data,0,sizeof (c.data));
for (i=0; i<c.n; i++)
for (j=0; j<c.m; j + +)
C.DATA[I][J]=A.DATA[I][J]-B.DATA[I][J];
return 1;
}
int Mul (mat& c,const mat& a,const mat& b)
{
int i,j,k;
if (A.M!=B.N)
return 0;
C.N=A.N,C.M=B.M;
for (i=0; i<c.n; i++)
for (j=0; j<c.m; j + +)
for (c.data[i][j]=k=0; k<a.m; k++)
C.DATA[I][J]+=A.DATA[I][K]*B.DATA[K][J];
return 1;
}
Inverse of matrix
int INV (mat& a)
{
int I,J,K,IS[MAXN],JS[MAXN];
Double T;
if (A.N!=A.M)
return 0;
for (k=0; k<a.n; k++)
{
for (t=0,i=k; i<a.n; i++)
for (j=k; j<a.n; j + +)
if (Fabs (A.data[i][j]) >t)
T=fabs (A.data[is[k]=i][js[k]=j]);
if (zero (t))
return 0;
if (is[k]!=k)
for (j=0; j<a.n; j + +)
Swap (a.data[k][j],a.data[is[k]][j]);
if (js[k]!=k)
for (i=0; i<a.n; i++)
Swap (A.data[i][k],a.data[i][js[k]]);
A.DATA[K][K]=1/A.DATA[K][K];
for (j=0; j<a.n; j + +)
if (j!=k)
A.DATA[K][J]*=A.DATA[K][K];
for (i=0; i<a.n; i++)
if (i!=k)
for (j=0; j<a.n; j + +)
if (j!=k)
A.DATA[I][J]-=A.DATA[I][K]*A.DATA[K][J];
for (i=0; i<a.n; i++)
if (i!=k)
A.DATA[I][K]*=-A.DATA[K][K];
}
for (k=a.n-1; k>=0; k--)
{
for (j=0; j<a.n; j + +)
if (js[k]!=k)
Swap (a.data[k][j],a.data[js[k]][j]);
for (i=0; i<a.n; i++)
if (is[k]!=k)
Swap (A.data[i][k],a.data[i][is[k]]);
}
return 1;
}
Transpose matrix, eg2*2 matrix: [1 2 3 4]->[-2,1,1.5,-0.5]
Double det (const mat& a)
{
int i,j,k,sign=0;
Double b[maxn][maxn],ret=1,t;
if (A.N!=A.M)
return 0;
for (i=0; i<a.n; i++)
for (j=0; j<a.m; j + +)
B[I][J]=A.DATA[I][J];
for (i=0; i<a.n; i++)
{
if (zero (b[i][i)))
{
for (j=i+1; j<a.n; j + +)
if (!zero (B[j][i]))
Break
if (J==A.N)
return 0;
for (k=i; k<a.n; k++)
Swap (b[i][k],b[j][k]);
sign++;
}
Ret*=b[i][i];
for (k=i+1; k<a.n; k++)
B[i][k]/=b[i][i];
for (j=i+1; j<a.n; j + +)
for (k=i+1; k<a.n; k++)
B[J][K]-=B[J][I]*B[I][K];
}
if (sign&1)
Ret=-ret;
return ret;
}
int main ()
{
Mat A,b,c;
while (scanf ("%d%d", &AMP;A.N,&AMP;A.M)!=eof)
{
for (int i=0; i<a.n; i++)
for (int j=0; j<a.m; j + +)
scanf ("%lf", &a.data[i][j]);
X=INV (a);
B.n=2,b.m=1;
b.data[0][0]=2;
b.data[1][0]=3;
Y=mul (C,A,B);
for (int i=0; i<c.n; i++)
{
for (int j=0; j<c.m; j + +)
printf ("%lf", C.data[i][j]);
printf ("\ n");
}
Double M=det (a);
cout<<m<<endl;
for (int i=0; i<a.n; i++)
{
for (int j=0; j<a.m; j + +)
printf ("%lf", A.data[i][j]);
printf ("\ n");
}
}
return 0;
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Matrix basic operations (plus and minus multiplication, inversion, transpose)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.