Matrix manipulation code performing unknown operations -
i having trouble understanding of following code. think middle section of code point out performing multiplication, not sure, can please explain me middle part of code trying accomplish?
i understand input , output parts of array elements, not actual manipulations.
i understand this:
#include "stdafx.h" #include<iostream> using namespace std; int main() { int i,j,k,n; float a[100][200],t; cout<<"enter order of matrix-"; cin>>n; cout<<"enter elements of matrix"<<endl; for(i=0;i<n;i++) for(j=0;j<n;j++) cin>>a[i][j];
but following lines confusing:
for(i=0;i<n;i++) { for(j=n;j<2*n;j++) { if(i==j-n) a[i][j]=1; else a[i][j]=0; } } for(i=0;i<n;i++) { t=a[i][i]; for(j=i;j<2*n;j++) a[i][j]=a[i][j]/t; for(j=0;j<n;j++) { if(i!=j) { t=a[j][i]; for(k=0;k<2*n;k++) a[j][k]=a[j][k]-t*a[i][k]; } } }
again, following lines understandable:
cout<<"\n\ninverse matrix\n\n"; for(i=0;i<n;i++) { for(j=n;j<2*n;j++) cout<<"\t"<<a[i][j]; cout<<"\n"; } return 0; }
your code attempting invert matrix. here code doing.
assuming have nxn matrix a, can create new nx2n matrix
b = ai
where nxn identity matrix. creation of b these lines trying do:
for(i=0;i<n;i++) { for(j=n;j<2*n;j++) // makes sure accessing columns in right hand part of matrix { if(i==j-n) a[i][j]=1; else a[i][j]=0; } }
you perform series of row operations on 2n wide matrix identity matrix on left hand side resulting in third matrix
c=id
where again nxn identity matrix. creation of c through row operations part of code trying do: (though won't speak correctness of algorithm being used has been while since have done , typically use svd algorithm inverses)
for(i=0;i<n;i++) //transform b matrix c matrix { t=a[i][i]; for(j=i;j<2*n;j++) a[i][j]=a[i][j]/t; for(j=0;j<n;j++) { if(i!=j) { t=a[j][i]; for(k=0;k<2*n;k++) a[j][k]=a[j][k]-t*a[i][k]; } } }
now when complete row operations , identity matrix on left hand side of c matrix, turns out nxn d matrix makes right hand side of c matrix inverse of a. have
ad = i
which why last bit of code grabs right hand side of called c matrix.
that's code trying do. if want understand more theory behind it, take @ "numerical recipes in c" book. it's gold standard kind of thing , freely available online. http://www.nrbook.com/a/bookcpdf.html
Comments
Post a Comment