Matrix.cc
Go to the documentation of this file.
00001 /* $Id: Matrix.cc 85 2010-03-25 23:54:08Z jack.oquin $ */
00002 
00003 #include <art_map/Matrix.h>
00004 #include <string.h>
00005 
00006 // Constructors
00007 Matrix::Matrix()
00008 {
00009         M = 0;
00010         N = 0;
00011         X = 0;
00012 }
00013 
00014 Matrix::Matrix(int m, int n, bool I/*= false*/)  
00015 {       
00016         M=m;
00017         N=n;
00018         X=new float [m*n];
00019         //Initialise matrix to zero
00020         for (int i = 0; i < m; i++)
00021         {
00022                 for (int j = 0; j < n; j++)
00023                 {
00024                         (*this)[i][j] = 0;
00025                 }
00026         }
00027         //Identity Matrix Initialisation
00028         if (I)
00029         {
00030                 if (m != n)
00031                         return;
00032                         
00033                 for (int i = 0; i < m; i++)
00034                 {
00035                         for (int j = 0; j < n; j++)
00036                         {
00037                                 (*this)[i][j] = (i == j) ? 1 : 0;
00038                         }
00039                 }
00040         }
00041 }
00042 
00043 // Copy Constructor
00044 Matrix::Matrix(const Matrix& a)
00045 {
00046         M=a.M;
00047         N=a.N;
00048         X=new float [M*N];
00049         memcpy(X,a.X,sizeof(float)*M*N);
00050 }
00051 
00052 // Destructor
00053 Matrix::~Matrix()
00054 { 
00055         delete [] X; X = 0; 
00056 }
00057 
00058 // Matrix Index Operator
00059 // Returns a pointer to the ith row in the matrix
00060 float*  Matrix::operator []	(int i)     const
00061 { return &X[i*N]; }
00062 
00063 // Matrix Addition
00064 Matrix operator + (const Matrix& a, const Matrix& b)
00065 {
00066         Matrix addAns(a.getm(),a.getn());
00067         int i=0,j=0;
00068         if ((a.getn()==b.getn())&&(a.getm()==b.getm()))
00069         {
00070                 for (i=0; i<a.getm(); i++)
00071                 {
00072                         for (j=0; j<a.getn();j++)
00073                         {
00074                                 addAns[i][j]=a[i][j]+b[i][j];
00075                         }
00076                 }
00077         }
00078         return addAns;
00079         //This return calls the copy constructor which copies the matrix into another block of memory
00080         //and then returns the pointer to this new memory.
00081         //Otherwise the array addAns is deleted by the destructor here and the pointer returned from the addition
00082         //is a pointer to deleted memory. This causes problems when the function calling this tries to delete this memory again.        
00083 }
00084 
00085 // Matrix Subtraction
00086 Matrix  operator -  (const Matrix& a, const Matrix& b)
00087 {
00088         Matrix subAns(a.getm(),a.getn());
00089         int i=0,j=0;
00090         if ((a.getn()==b.getn())&&(a.getm()==b.getm()))
00091         {
00092                 for (i=0; i<a.getm(); i++)
00093                 {
00094                         for (j=0; j<a.getn();j++)
00095                         {
00096                                 subAns[i][j]=a[i][j]-b[i][j];
00097                         }
00098                 }
00099         }
00100         return subAns;
00101 }
00102 
00103 //Matrix Multiplication
00104 Matrix  operator * (const Matrix& a, const Matrix& b)
00105 {       
00106         Matrix multAns(a.getm(),b.getn());
00107         int i=0,j=0,k=0;
00108         if (a.getn()==b.getm())
00109         {
00110                 for (i=0; i<a.getm(); i++)
00111                 {
00112                         for (j=0; j<b.getn();j++)
00113                         {
00114                                 float temp=0;                           
00115                                 for (k=0; k<a.getn(); k++)
00116                                 {
00117                     temp+=a[i][k]*b[k][j];
00118                                 }       
00119                                 multAns[i][j]=temp;
00120                         }
00121                 }                                       
00122         }
00123         return multAns;
00124 }
00125 
00126 // Matrix Multiplication by a Scalar
00127 Matrix  operator * (const float& a, const Matrix& b)
00128 {       
00129         Matrix multAns(b.getm(),b.getn());
00130         int i=0,j=0;
00131         for (i=0; i<b.getm(); i++)
00132         {
00133                 for (j=0; j<b.getn();j++)
00134                 {                               
00135                         multAns[i][j]=b[i][j]*a;
00136                 }
00137         }       
00138         return multAns;
00139 }
00140 
00141 // Matrix Multiplication by a Scalar
00142 Matrix  operator * (const Matrix& a, const float& b)
00143 {       
00144         Matrix multAns(a.getm(),a.getn());
00145         int i=0,j=0;
00146         for (i=0; i<a.getm(); i++)
00147         {
00148                 for (j=0; j<a.getn();j++)
00149                 {                               
00150                         multAns[i][j]=a[i][j]*b;
00151                 }
00152         }       
00153         return multAns;
00154 }
00155 
00156 // Matrix Division by a Scalar
00157 Matrix  operator / (const Matrix& a, const float& b)
00158 {       
00159         Matrix divAns(a.getm(),a.getn());
00160         int i=0,j=0;
00161         for (i=0; i<a.getm(); i++)
00162         {
00163                 for (j=0; j<a.getn();j++)
00164                 {                               
00165                         divAns[i][j]=a[i][j]/b;
00166                 }
00167         }       
00168         return divAns;
00169 }
00170 
00171 // Matrix Equality
00172 Matrix& Matrix::operator =  (const Matrix& a)
00173 {
00174         if (X!=0)
00175                 delete [] X;
00176         M=a.M;
00177         N=a.N;
00178         X=new float [M*N];
00179         memcpy(X,a.X,sizeof(float)*M*N);
00180         return *this;
00181 }
00182 
00183 // Matrix Transpose
00184 Matrix  Matrix::transp()
00185 { 
00186         Matrix transpAns(getn(),getm());
00187         int i=0,j=0;
00188         for (i=0; i<getm(); i++)
00189         {
00190                 for (j=0; j<getn();j++)
00191                 {                               
00192                         transpAns[j][i]=(*this)[i][j];
00193                 }
00194         }       
00195         return transpAns; 
00196 }
00197 
00198 // 2x2 Matrix Inversion
00199 Matrix Invert22(const Matrix& a)
00200 {
00201         Matrix invertAns(a.getm(),a.getn());    
00202         invertAns[0][0]=a[1][1];
00203         invertAns[0][1]=-a[0][1];
00204         invertAns[1][0]=-a[1][0];
00205         invertAns[1][1]=a[0][0];
00206         float divisor=a[0][0]*a[1][1]-a[0][1]*a[1][0];
00207         invertAns=invertAns/divisor;
00208         return invertAns;
00209 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


art_map
Author(s): David Li, Patrick Beeson, Bartley Gillen, Tarun Nimmagadda, Mickey Ristroph, Michael Quinlan, Jack O'Quin
autogenerated on Tue Sep 24 2013 10:41:51