MIPMatrix.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //
00003 // $Id$
00004 //
00005 // Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno    
00006 //
00007 // This file is part of MIP.
00008 //
00009 // MIP is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // MIP is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
00023 //
00024 // ----------------------------------------------------------------------------
00025 
00026 
00031 
00035 
00036 
00037 #ifndef __MATRIX_H_
00038 #define __MATRIX_H_
00039 
00040 //#define _NONSTD_SOURCE
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 //#include <R2.h>
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   /*costr*/
00095   MIPMatrix(int r,int c,CanonicalMIPMatrix init=ZERO_MATRIX){
00096 //   printf("DEBUG MIPMatrix def-costr\n");
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]; /*allocazione della matrice*/
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 //     printf("Ended succesfully\n");
00113   }
00114 
00115   /*Default constructor only for 4x4 identity matrix. RotoTranslation matrix.*/
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]; /*allocazione della matrice*/
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   /*copy construtctor*/
00135   MIPMatrix(const MIPMatrix &A){
00136 //   printf("DEBUG MIPMatrix copy-costr\n");
00137    rows=A.rows;
00138 //   cout << "DEBUG rows = " << rows << endl;
00139    cols=A.cols;
00140 //   cout << "DEBUG cols = " << cols << endl;
00141    elms = (Decimal**)new Decimal*[rows]; /*allocazione della matrice*/
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 //   printf("Ended succesfully\n");
00147   }
00148  
00149   /*destr*/
00150   ~MIPMatrix(){
00151    if(DEBUG_MEM) cout << "DEBUG elimino matrice modded "<<rows<<"x"<<cols<<endl;
00152 //   print();
00153    
00154    for(int k = 0; k<rows; k++)  delete elms[k];
00155    delete elms;
00156    
00157   }
00158  
00159   /*special operators*/
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);//This function builds the minor matrix of an element in A matrix position (row,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   /*operators*/
00191   MIPMatrix& operator=(const MIPMatrix& A){
00192    if (this != &A){
00193 //   printf("DEBUG MIPMatrix operator=\n");
00194    if((rows!=A.rows)||(cols!=A.cols)){
00195     /*deallocazione*/
00196     for(int k = 0; k<rows; k++)  delete elms[k];
00197     delete elms;
00198     /*riallocazione*/
00199     elms = (Decimal**)new Decimal*[A.rows]; /*allocazione della matrice*/
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 //   printf("Ended succesfully\n");
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 };// end of namespace MipBaselib
00250 
00251 
00252 
00253 
00254 #endif
00255 
00256 
00257 /* @} */
00258 
00259 

Generated on Mon Feb 20 07:01:06 2017 for MIP by  doxygen 1.5.6