Googlo sam ga ali mi izbacuje gresku, ne znam u cemu je greska.....Ako imas vremena pogledaj...
Code:
01 // matrix inversioon
02 // the result is put in Y
03 void MatrixInversion(float **A, int order, float **Y)
04 {
05 // get the determinant of a
06 double det = 1.0/CalcDeterminant(A,order);
07
08 // memory allocation
09 float *temp = new float[(order-1)*(order-1)];
10 float **minor = new float*[order-1];
11 for(int i=0;i<order-1;i++)
12 minor[i] = temp+(i*(order-1));
13
14 for(int j=0;j<order;j++)
15 {
16 for(int i=0;i<order;i++)
17 {
18 // get the co-factor (matrix) of A(j,i)
19 GetMinor(A,minor,j,i,order);
20 Y[i][j] = det*CalcDeterminant(minor,order-1);
21 if( (i+j)%2 == 1)
22 Y[i][j] = -Y[i][j];
23 }
24 }
25
26 // release memory
27 delete [] minor[0];
28 delete [] minor;
29 }
30
31 // calculate the cofactor of element (row,col)
32 int GetMinor(float **src, float **dest, int row, int col, int order)
33 {
34 // indicate which col and row is being copied to dest
35 int colCount=0,rowCount=0;
36
37 for(int i = 0; i < order; i++ )
38 {
39 if( i != row )
40 {
41 colCount = 0;
42 for(int j = 0; j < order; j++ )
43 {
44 // when j is not the element
45 if( j != col )
46 {
47 dest[rowCount][colCount] = src[i][j];
48 colCount++;
49 }
50 }
51 rowCount++;
52 }
53 }
54
55 return 1;
56 }
57
58 // Calculate the determinant recursively.
59 double CalcDeterminant( float **mat, int order)
60 {
61 // order must be >= 0
62 // stop the recursion when matrix is a single element
63 if( order == 1 )
64 return mat[0][0];
65
66 // the determinant value
67 float det = 0;
68
69 // allocate the cofactor matrix
70 float **minor;
71 minor = new float*[order-1];
72 for(int i=0;i<order-1;i++)
73 minor[i] = new float[order-1];
74
75 for(int i = 0; i < order; i++ )
76 {
77 // get minor of element (0,i)
78 GetMinor( mat, minor, 0, i , order);
79 // the recusion is here!
80 det += pow( -1.0, i ) * mat[0][i] * CalcDeterminant( minor,order-1 );
81 }
82
83 // release memory
84 for(int i=0;i<order-1;i++)
85 delete [] minor[i];
86 delete [] minor;
87
88 return det;
89 }
[Ovu poruku je menjao X Files dana 07.01.2010. u 18:55 GMT+1]