00001 00002 00005 class Matrix { 00006 protected: 00008 double **mtx; 00010 int order; 00011 00012 public: 00014 00016 Matrix(int dim) { 00017 mtx = (double **)malloc(dim*sizeof(double *)); 00018 for (int i=0; i<dim; i++) 00019 mtx[i] = (double *)malloc(dim*sizeof(double)); 00020 order = dim; 00021 } 00022 00024 void setElement(int i, int j, double value) { 00025 CmiAssert((i < order) && (j < order)); 00026 mtx[i][j] = value; 00027 } 00028 00030 const double &elem(int i, int j) const { 00031 CmiAssert((i < order) && (j < order)); 00032 return mtx[i][j]; 00033 } 00034 00036 double determinant(void) const { 00037 double d = 0.0; 00038 if (order <= 1) return elem(0, 0); 00039 for (int i=0; i<order; i++) { 00040 double sign = (i%2) ? -1 : 1; 00041 d += sign * elem(i, 0) * cofactor(i, 0).determinant(); 00042 } 00043 return d; 00044 } 00045 00047 00049 Matrix cofactor(int aI, int aJ) const { 00050 CmiAssert((aI < order) && (aJ < order)); 00051 Matrix a(order-1); 00052 for (int i=0, k=0; i<order; i++) 00053 if (i != aI) { 00054 for (int j=0, l=0; j<order; j++) { 00055 if (j != aJ) { 00056 a.setElement(k, l, elem(i, j)); 00057 l++; 00058 } 00059 } 00060 k++; 00061 } 00062 return a; 00063 } 00064 };