Join 149,921 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,988 people online right now. Registration is fast and FREE... Join Now!
Please read the rules, and don't ask for other people to just spoon-feed you with complete solutions.
You might want to start out by writing a program which simply represents a matrix using arrays. Then look into ways of inverting matrices, and experiment with translating these into C code http://en.wikipedia.org/wiki/Invertible_matrix
This post has been edited by Bench: 14 Dec, 2007 - 05:02 AM
If your question remains that you want to be provided with the full code for inverting a matrix, the answer remains the same. We're not going to do your work for you, and you still haven't provided anything that shows you've made an effort on this yourself.
If you've made an attempt to implement this yourself, please post your code and we will be more than happy to help you move forward with it. If you haven't, there are a number of snippets available in the code database that have dealt with matrices and calculating their inverses. These should be helpful in getting you started.
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
/* Function to invert matrix a[][] with the inverse stored in x[][] in the output. { int indx[]; double a[n][n],x[n][n]; { int i,j,k; double b[n][n]; void elgs();
if (n > 6) { printf("The matrix dimension is too large.\n"); exit(1); }
for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { b[i][j] = 0; } } for (i = 0; i < n; ++i) { b[i][i] = 1; }
elgs (a,n,indx);
for (i = 0; i < n-1; ++i) { for (j = i+1; j < n; ++j) { for (k = 0; k < n; ++k) { b[indx[j]][k] = b[indx[j]][k]-a[indx[j]][i]*b[indx[i]][k]; } } }
for (i = 0; i < n; ++i) { x[n-1][i] = b[indx[n-1]][i]/a[indx[n-1]][n-1]; for (j = n-2; j >= 0; j = j-1) { x[j][i] = b[indx[j]][i]; for (k = j+1; k < n; ++k) { x[j][i] = x[j][i]-a[indx[j]][k]*x[k][i]; } x[j][i] = x[j][i]/a[indx[j]][j]; } } }
void elgs (a,n,indx)
/* Function to perform the partial-pivoting Gaussian elimination. a[][] is the original matrix in the input and transformed matrix plus the pivoting element ratios below the diagonal in the output. indx[] records the pivoting order.
{ int n; int indx[]; double a[n][n]; { int i, j, k, itmp; double c1, pi, pi1, pj; double c[n];
if (n > 6) { printf("The matrix dimension is too large.\n"); exit(1); }
/* Initialize the index */
for (i = 0; i < n; ++i) { indx[i] = i; }
/* Find the rescaling factors, one from each row */
for (i = 0; i < n; ++i) { c1 = 0; for (j = 0; j < n; ++j) { if (fabs(a[i][j]) > c1) c1 = fabs(a[i][j]); } c[i] = c1; }
/* Search the pivoting (largest) element from each column */
for (j = 0; j < n-1; ++j) { pi1 = 0; for (i = j; i < n; ++i) { pi = fabs(a[indx[i]][j])/c[indx[i]]; if (pi > pi1) { pi1 = pi; k = i; } }
/* Interchange the rows via indx[] to record pivoting order */
itmp = indx[j]; indx[j] = indx[k]; indx[k] = itmp; for (i = j+1; i < n; ++i) { pj = a[indx[i]][j]/a[indx[j]][j];
/* Record pivoting ratios below the diagonal */
a[indx[i]][j] = pj;
/* Modify other elements accordingly */
for (k = j+1; k < n; ++k) { a[indx[i]][k] = a[indx[i]][k]-pj*a[indx[j]][k]; } } } }
This is what I get after compiling code
Compiling... code44.c code44.c(42) : error C2059: syntax error : 'type' code44.c(46) : error C2449: found '{' at file scope (missing function header?) code44.c(178) : fatal error C1004: unexpected end of file found Error executing cl.exe.
code44.obj - 3 error(s), 0 warning(s)
After this I can't find way to continue. Please, help me to remove this errors. Thanks
This post has been edited by jjhaag: 27 Dec, 2007 - 02:44 PM
The formatting of this is all over the place, and it makes it incredibly difficult to figure out exactly what you're trying to do. My recommendation with this would be to start over with a new file, paying special attention to the layout of the function prototypes and their definitions. It appears that you are making several function declarations and definitons right in the middle of main(), which is going to cause problems.
It appears that you have two functions that you're trying to use here - migs() and elgs(). Start by laying out the prototypes and definitions properly with respect to the position of the main() routine:
CODE
#include <stdio.h> #include <math.h>
double migs (double a[][], double x[][], int n, int indx);// function prototype for migs() void elgs (a[][], int n, int indx);// function prototype for elgs()
int main() { //start of main() //code to declare and initialize variables //call functions, etc. //display results return 0; } //end of main()
// function definition for migs() double migs (double a[][], double x[][], int n, int indx) { //code defining the procedure for migs() }
// function definition for elgs() void elgs (a[][], int n, int indx) { //code defining the procedure for elgs() }
Once you've got that, start filling in the code in the appropriate places. Sorry not to be of more help, but the organization of the original code precludes being able to actually figure out what you're trying to do in each sections.
And next time, please use the code tags when posting code, like this . It makes it way easier to read, as well as copying and pasting. It's been corrected in the above post, but keep it in mind for next time.
I am still working with some codes in C and C++ for getting inverse matrix. Now, I have problem with compiling. I can find 'bits/types.h' to complite compiling of code for inverse matrix in C++. Please, if somebody can help me with this. It will be best if you post a link or attach it with reply. Olso you can send file to me on: **(email address removed for spam prevention purposes) Thanks
This post has been edited by jjhaag: 3 Jan, 2008 - 01:42 PM
Matrix& Matrix :: operator+= (const Matrix& right) { if (rows!=right.rows || cols!=right.cols) return *this; for (int i=0;i<rows;i++) for (int j=0;j<cols;j++) mat[i][j]+=right.mat[i][j]; return *this;
}
Matrix& Matrix :: operator-= (const Matrix& right) { if (rows!=right.rows || cols!=right.cols) return *this; for (int i=0;i<rows;i++) for (int j=0;j<cols;j++) mat[i][j]-=right.mat[i][j]; return *this;
}
Matrix operator * (const Matrix& left, const Matrix& right) { Matrix m (left.rows, right.cols); if (left.cols!=right.rows) return m; for (int i=0;i<left.rows;i++) for (int j=0;j<right.cols;j++) for (int k=0;k<left.cols;k++) m.mat[i][j]+=left.mat[i][k]*right.mat[k][j]; return m;
}
Matrix& Matrix :: operator*= (const Type& t) { for (int i=0;i<rows;i++) for (int j=0;j<cols;j++) mat[i][j]*=t; return *this; }
istream& operator >> (istream& is, Matrix& m) { for (int i=0;i<m.rows;i++) for (int j=0;j<m.cols;j++) is>>m.mat[i][j]; return is; }
ostream& operator << (ostream& os, const Matrix& m) { os<<"{\n"; for (int i=0;i<m.rows;i++) { os <<"{"; for (int j=0;j<m.cols;j++) os << m.mat[i][j] << ((j<m.cols-1)?",":"}"); os << ((i<m.rows-1)?",\n":"\n}\n"); } return os; }
// transpose matrix Matrix trans (const Matrix& m) { Matrix temp(m.cols, m.rows); for (int i=0;i<m.rows;i++) for (int j=0;j<m.cols;j++) temp.mat[j][i]=m.mat[i][j]; return temp; }
// one row and col is out Matrix compl (const Matrix& m, int r, int c) { Matrix temp(m.rows-1, m.cols-1); if (m.cols<1 || m.rows<1) return temp; for (int i=0,x=0;i<m.rows;i++) { if (i==r-1) continue; for (int j=0,y=0;j<m.cols;j++) { if (j==c-1) continue; temp.mat[x][y]=m.mat[i][j]; y++; } x++; } return temp; }
// det matrix Type det (const Matrix& m) { if (m.rows!=m.cols) return 0; if (m.rows==1) return m.mat[0][0]; Type temp=Type(0); for (int j=0,c=1;j<m.cols;j++,c*=-1) temp+=(Type(c)*m.mat[0][j]*det(compl(m,1,j+1))); return temp; }
// cofact Type cofact (const Matrix& m, int r, int c) { if (m.rows!=m.cols) return 0; if ((r+c)%2!=0) return (-det (compl(m,r,c))); else return (det (compl(m,r,c))); }
// adj matrix Matrix adj (const Matrix& m) { Matrix temp(m.rows, m.cols); if (m.rows!=m.cols) return temp; for (int i=0;i<m.rows;i++) for (int j=0;j<m.cols;j++) temp.mat[i][j]=cofact(m,i+1,j+1); return trans(temp); }
// invers matrix Matrix inv (const Matrix& m) { Type temp=det(m); if (temp!=0) return (1/temp)*adj(m); else return Matrix(0,0); }
void main () {
#define NMAX 4
Matrix m (NMAX,NMAX);
cin>>m;
const Matrix& m1=m; const Matrix m2=inv (m1);
cout<<"m="<<m2<<'\n'; }
Where:
CODE
// Modul matrix.h
#ifndef _Matrix #define _Matrix
class istream; class ostream;
typedef int Type;
class Matrix{ public: Matrix (int m, int n); Matrix (const Matrix& mat); ~Matrix ();
Matrix& operator = (const Matrix&);
friend int operator == (const Matrix&, const Matrix&); friend int operator != (const Matrix&, const Matrix&); Type& operator () (int i, int j);
friend Matrix trans (const Matrix&); friend Matrix compl (const Matrix&, int i, int j); friend Type det (const Matrix&); friend Type cofact (const Matrix&, int i, int j); friend Matrix adj (const Matrix&); friend Matrix inv (const Matrix&);
You can also see doc file. I can figure out what is problem with main function. It allow me to write elements of matrix but not to see what is hepenning in output. Can somebody help me with this?
Attached File(s) Matrix.doc ( 45k )
Number of downloads: 82