MIPMatrix.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00031
00035
00036
00037 #ifndef __MATRIX_H_
00038 #define __MATRIX_H_
00039
00040
00041
00042 #define DEBUG_MEM 0
00043
00044 #ifdef MIP_HOST_APPLE
00045 #include </usr/include/time.h>
00046 #include <ctime>
00047 #endif
00048
00049 #include <assert.h>
00050 #include <stdio.h>
00051 #include <iostream>
00052 #include <sstream>
00053 #include <vector>
00054
00055 #include <Types.h>
00056
00057
00058
00059
00060 using namespace std;
00061
00062 namespace MipBaselib{
00063
00064
00066
00067
00068
00069 typedef enum {
00070 IDENTITY_MATRIX,
00071 ZERO_MATRIX,
00072 NUM_CANONICAL_MATRIX
00073 } CanonicalMIPMatrix;
00074
00075 static const char* CanonicalMIPMatrixName[NUM_CANONICAL_MATRIX] ={
00076 "IDENTITY_MATRIX",
00077 "ZERO_MATRIX"
00078 };
00079
00084 struct MIPMatrix{
00085 public:
00087 int rows;
00089 int cols;
00091 Decimal **elms;
00092
00093
00095 MIPMatrix(int r,int c,CanonicalMIPMatrix init=ZERO_MATRIX){
00096
00097
00098 assert(r>0);
00099 assert(c>0);
00100 rows = r;
00101 cols = c;
00102 if(DEBUG_MEM) cout << "DEBUG sto per costruire elms << "<< rows<<" << "<<cols << endl;
00103 elms = (Decimal**)new Decimal*[rows];
00104 for(int k = 0; k<rows; k++) elms[k] = (Decimal*) new Decimal[cols];
00105
00106 for(int i = 0; i<rows; i++)
00107 for(int j = 0; j<cols; j++) elms[i][j] = 0.0;
00108 if(init==IDENTITY_MATRIX){
00109 assert(cols==rows);
00110 for(int i = 0; i<cols; i++) elms[i][i] = 1.0;
00111 }
00112
00113 }
00114
00115
00117 MIPMatrix()
00118 {
00119 rows = 4;
00120 cols = 4;
00121 if(DEBUG_MEM) cout << "DEBUG sto per costruire elms << "<< rows<<" << "<<cols << endl;
00122 elms = (Decimal**)new Decimal*[rows];
00123 for(int k = 0; k<rows; k++) elms[k] = (Decimal*) new Decimal[cols];
00124
00125 for(int i = 0; i<rows; i++)
00126 for(int j = 0; j<cols; j++)
00127 if(i == j)
00128 elms[i][i] = 1.0;
00129 else
00130 elms[i][j] = 0.0;
00131 }
00132
00133
00134
00135 MIPMatrix(const MIPMatrix &A){
00136
00137 rows=A.rows;
00138
00139 cols=A.cols;
00140
00141 elms = (Decimal**)new Decimal*[rows];
00142 for(int k = 0; k<rows; k++) elms[k] = (Decimal*) new Decimal[cols];
00143
00144 for(int i = 0; i<rows; i++)
00145 for(int j = 0; j<cols; j++) elms[i][j] = A.elms[i][j];
00146
00147 }
00148
00149
00150 ~MIPMatrix(){
00151 if(DEBUG_MEM) cout << "DEBUG elimino matrice modded "<<rows<<"x"<<cols<<endl;
00152
00153
00154 for(int k = 0; k<rows; k++) delete elms[k];
00155 delete elms;
00156
00157 }
00158
00159
00160 void eq(MIPMatrix &A);
00166 void sum(MIPMatrix &A, MIPMatrix &B);
00172 void sub(MIPMatrix &A, MIPMatrix &B);
00178 void mul(MIPMatrix &A, MIPMatrix &B);
00179 void trans(MIPMatrix &A);
00180 void MinorMatrix(MIPMatrix &A, int row, int col);
00181 Decimal det22(MIPMatrix &A);
00182 Decimal inv22(MIPMatrix &A);
00183 Decimal det33(MIPMatrix &A);
00184 Decimal inv33(MIPMatrix &A);
00185 Decimal det44(MIPMatrix &A);
00186 Decimal inv44(MIPMatrix &A);
00187 Decimal det55(MIPMatrix &A);
00188 Decimal inv55(MIPMatrix &A);
00189
00190
00191 MIPMatrix& operator=(const MIPMatrix& A){
00192 if (this != &A){
00193
00194 if((rows!=A.rows)||(cols!=A.cols)){
00195
00196 for(int k = 0; k<rows; k++) delete elms[k];
00197 delete elms;
00198
00199 elms = (Decimal**)new Decimal*[A.rows];
00200 for(int k = 0; k<A.rows; k++) elms[k] = (Decimal*) new Decimal[A.cols];
00201 rows=A.rows;
00202 cols=A.cols;
00203 }
00204 for(int i = 0; i<rows; i++)
00205 for(int j = 0; j<cols; j++) elms[i][j] = A.elms[i][j];
00206 }return *this;
00207
00208 }
00209
00210
00211 Decimal GetMIPMatrixVal(int r,int c)
00212 {
00213 assert(r>0);
00214 assert(c>0);
00215 assert(r<=rows);
00216 assert(c<=cols);
00217 return elms[r-1][c-1];
00218 }
00219
00220 void SetMIPMatrixVal(int r,int c, Decimal val)
00221 {
00222 assert(r>0);
00223 assert(c>0);
00224 assert(r<=rows);
00225 assert(c<=cols);
00226 elms[r-1][c-1] = val;
00227 return;
00228 }
00229
00230 void printMIPMatrix()
00231 {
00232 for(int i = 0; i<rows; i++)
00233 {
00234 for(int j = 0; j<cols; j++)
00235 cout << elms[i][j] << "; ";
00236 cout << "\n";
00237 }
00238 return;
00239 }
00240
00243 string print(bool sameLine = false);
00244 void printStat(){cout << "matrice "<<rows<<"x"<<cols<<endl;}
00245 };
00246
00247
00248
00249 };
00250
00251
00252
00253
00254 #endif
00255
00256
00257
00258
00259