Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

/home/slang/XVision2/src/Tools/XVMatrix.h

00001 // *** BEGIN_XVISION2_COPYRIGHT_NOTICE ***
00002 // *** END_XVISION2_COPYRIGHT_NOTICE ***
00003 
00004 //---------------------------------------------------------------------
00005 //*
00006 //* XVMatrix.h
00007 //*
00008 //* 02/20/95 Gregory D. Hager
00009 //* 
00010 //* Header file for a XVMatrix package for small systems
00011 //* A simple matrix package for small applications.  Matrices are stored in
00012 //* row-major order using a ragged-array data structure.  Submatrices are
00013 //* also supported as are row and column vectors.
00014 //*
00015 //* Bounds checking and size checking are also included by default.
00016 //* Defining the NO_BOUNDS_CHECK turns off bounds checking.
00017 //* Defining check_size turns off size checking on matrix operations.
00018 //* Check in XVMatrix.cc at the start of the file to see whether
00019 //* NO_BOUNDS_CHECK or CHECKSIZE are defined as you would like them to be.
00020 //*
00021 //* The SUBSCRIPT_START_WITH_1 preprocessor variable, if defined,
00022 //* allows you to index these structures from 1 to n, rather than from
00023 //* 0 to n-1.
00024 //*
00025 //* Matrices should be templates, but for some reason templates aren't
00026 //* working so I'll just hardcode the type for now.
00027 //*
00028 //* This header file contains the definitions for the classes
00029 //* _rowvec, XVRowVector, XVColumnVector, XVMatrix, and XVXVDiagonalMatrix
00030 //*
00031 //---------------------------------------------------------------------
00032 
00033 //#define SUBSCRIPT_START_WITH_1
00034 
00035 #ifndef _XVMATRIX_H_
00036 #define _XVMATRIX_H_
00037 
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <iostream>
00041 #include <string>
00042 
00043 #include <XVImageScalar.h>
00044 
00045 // Next two #defines used to be in XVMatrix.cc, but should be here.
00046 #define NO_BOUNDS_CHECK
00047 //#define CHECKSIZE(m,msg) /* do nothing */
00048 
00049 #define FR_EPSFCN 1.0E-16       // FrReal is the type of the matrices
00050 #define FrReal double           // FR_EPSFCN is the minimum available value
00051 
00052 extern void _panic(char *);     // For simple exception handling.
00053 
00054 typedef struct {                // Data structure for reference counts
00055   FrReal *data;
00056   int refCount;
00057 } RefCounter;
00058 
00060 #ifndef CHECKSIZE
00061 #define CHECKSIZE(m,mess) \
00062 {if ((m.rowNum != rowNum) || (m.colNum != colNum)) _panic(mess);}
00063 #endif
00064 
00068 class _rowvec
00069 {
00070 private:
00071   FrReal *data;
00072   int colNum;
00073 
00074 public:
00075 
00076   _rowvec(FrReal *datain,int ncols) {data = datain; colNum = ncols;}
00077 
00078   FrReal&
00079   operator[] (int n) {
00080     if ((n < 0) || (n >= colNum))
00081       _panic("XVMatrix second argument out of range");
00082     return data[n];
00083   }
00084 
00085   const FrReal&
00086   operator[] (int n) const {
00087     if ((n < 0) || (n >= colNum))
00088       _panic("XVMatrix second argument out of range");
00089     return data[n];
00090   }
00091 
00092 };
00093 
00094 class XVRowVector;
00095 class XVColVector;
00096 class XVMatrix;
00097 class XVDiagonalMatrix;
00098 
00099 
00100 // The XVMatrix class definition starts here
00101 //---------------------------------------------------------------------
00102 //*
00103 //* XVMatrix.h
00104 //*
00105 //* 02/20/95 Gregory D. Hager
00106 //* 
00107 //* Header file for a XVMatrix package for small systems
00108 //* A simple matrix package for small applications.  Matrices are stored in
00109 //* row-major order using a ragged-array data structure.  Submatrices are
00110 //* also supported as are row and column vectors.
00111 //*
00112 //* Bounds checking and size checking are also included by default.
00113 //* Defining the NO_BOUNDS_CHECK turns off bounds checking.
00114 //* Defining check_size turns off size checking on matrix operations.
00115 //* Check in XVMatrix.cc at the start of the file to see whether
00116 //* NO_BOUNDS_CHECK or CHECKSIZE are defined as you would like them to be.
00117 //*
00118 //* The SUBSCRIPT_START_WITH_1 preprocessor variable, if defined,
00119 //* allows you to index these structures from 1 to n, rather than from
00120 //* 0 to n-1.
00121 //*
00122 //* Matrices should be templates, but for some reason templates aren't
00123 //* working so I'll just hardcode the type for now.
00124 //*
00125 //* This header file contains the definitions for the classes
00126 //* _rowvec, XVRowVector, XVColumnVector, XVMatrix, and XVDiagonalMatrix
00127 //*
00128 //---------------------------------------------------------------------
00129 
00130 class XVMatrix
00131 {
00132   friend class XVRowVector;
00133   friend class XVColVector;
00134   friend class XVDiagonalMatrix;
00135   friend XVMatrix add_column(XVMatrix &x, XVColVector &c);
00136 
00137 private:
00140   static FrReal deriv_from_pts(FrReal y1, FrReal y2, FrReal y3,
00141                       FrReal dstep, FrReal x);
00142 
00145   static void deriv_n(XVColVector (*fn)(const XVColVector&, const XVColVector&),
00146       const XVColVector& val, int n, const XVColVector& extra, XVColVector& deriv);
00147 
00149   int init(XVMatrix &m,int startr, int startc, int nrows, int ncols);
00150 
00151   // initialise all fields to 0
00152   void init_empty(void);
00153 
00154 protected:
00157   RefCounter *refPtr;
00158   int rowNum;
00159   int colNum;
00160 
00162   FrReal *data;
00163   FrReal **rowPtrs;
00164 
00167   int csize;
00169   int dsize;
00171   int trsize;
00172 
00174   RefCounter *ref();
00175   void unref();
00176 
00178   int dataShared();
00179 
00181   XVMatrix(XVMatrix &m, int startr, int startc, int nrows, int ncols);
00182 
00183 
00184 public:
00186   XVMatrix();
00187   XVMatrix(const XVMatrix& m);
00188   XVMatrix(int rr,int cc);
00189   XVMatrix(int rr,int cc,FrReal* x);
00190   virtual ~XVMatrix ();
00192   int n_of_rows() const;
00193   int n_of_cols() const;
00194 
00197   int resize(int nrows, int ncols);
00198 
00203   XVMatrix& operator=(FrReal x);
00204   XVMatrix& operator=(const XVMatrix &m);
00205   XVMatrix& operator<<(const XVMatrix &m);
00207   XVMatrix& operator<<(FrReal*);
00208   XVMatrix& operator+=(FrReal x);
00209   XVMatrix& operator+=(const XVMatrix &mat);
00210   XVMatrix& operator-=(FrReal x);
00211   XVMatrix& operator-=(const XVMatrix &mat);
00212   XVMatrix& operator*=(FrReal x);
00213   XVMatrix& operator/=(FrReal x);
00214   XVMatrix operator*(FrReal x) const;
00215   XVMatrix operator*(const XVMatrix &mat) const;
00216   XVColVector operator*(XVColVector &mat) const;
00217   XVRowVector operator*(XVRowVector &mat) const;
00218   XVMatrix operator/(FrReal x) const;
00219   XVMatrix operator+(const XVMatrix &mat) const;
00220   XVMatrix operator-(const XVMatrix &mat) const;
00221   XVMatrix operator-() const;
00223   FrReal SumSquare() const;
00225   FrReal Sum() const;
00226 
00228 #ifndef NO_BOUNDS_CHECK
00229   _rowvec operator[](int n);
00230 #else
00231   FrReal* operator[](int n);
00232 #endif
00233 
00235   FrReal* operator[](int n) const;
00236   XVRowVector row(int i);
00237   XVRowVector Row(int i);
00238   XVColVector col(int j);
00239   XVColVector Column(int j);
00240 
00241 #ifdef SUBSCRIPT_START_WITH_1  
00242 
00243   XVMatrix operator()(int sr, int lr, int sc, int lc);
00244   XVMatrix Rows(int first_row, int last_row);
00245   XVMatrix Columns(int first_col, int last_col);
00246         
00247 #else
00248 
00249   XVMatrix operator()(int sr, int lr, int sc, int lc);
00250   XVMatrix Rows(int first_row, int last_row);
00251   XVMatrix Columns(int first_col, int last_col);
00252 
00253 #endif // SUBSCRIPT_START_WITH_1
00254  
00256   XVMatrix i() const;
00257   XVMatrix t() const;
00258   XVMatrix ip() const;
00259   XVMatrix op() const;
00260 
00263   void LUDcmp(int* perm, int& d);
00265   void LUBksb(int* perm, XVColVector& b);
00267   void solveByLUD(const XVColVector& b, XVColVector& x);
00269   XVColVector LUDsolve(const XVColVector& b);
00271   void LDLtDcmp();
00273   XVMatrix sqrt();
00275   void SVDcmp(XVColVector& w, XVMatrix& v);
00277   void SVBksb(const XVColVector& w,const XVMatrix& v,
00278            const XVColVector& b, XVColVector& x);
00280   void solveBySVD(const XVColVector& b, XVColVector& x);
00282   XVColVector SVDsolve(const XVColVector& b);
00286   static XVMatrix jacobian(XVColVector (*fn)(const XVColVector&, const XVColVector&),
00287                 int odim, const XVColVector& where, const XVColVector& extra);
00289   friend ostream &operator << (ostream &s,const XVMatrix &m);
00293   XVMatrix Map(FrReal (*fn)(FrReal)) const;
00294 
00295   XVMatrix add_column(const XVColVector &c) const;
00296 
00297 };
00298 
00299 
00300 
00305 class XVRowVector : public XVMatrix
00306 {
00307   friend class XVMatrix;
00308 
00309 protected:
00310   XVRowVector(XVMatrix &m, int i);
00311 
00312 public:
00313   XVRowVector();
00314   XVRowVector(int nn);
00315   XVRowVector(const XVRowVector &v);
00316 #ifdef __xvimagescalar_h
00317   XVRowVector(const XVImageScalar<double> &x);
00318 #endif
00319 
00320   void resize(int i);
00321   FrReal &operator [](int n);
00322   const FrReal &operator [](int n) const;
00323   XVRowVector &operator=(const XVRowVector &v);
00324   XVRowVector &operator<<(const XVRowVector &v);
00325   XVRowVector &operator=(const XVMatrix &m);
00326   XVRowVector &operator=(FrReal x);
00327   XVColVector t() const;
00328 };
00329 
00330 
00331 
00336 class XVColVector : public XVMatrix
00337 {
00338   friend class XVMatrix;
00339 
00340 protected:
00341   XVColVector (XVMatrix &m, int j);
00342   XVColVector (XVColVector &m, int startr, int nrows);
00343 
00344 public:
00345 
00346   XVColVector();
00347   XVColVector(int nn);
00348   XVColVector (const XVColVector &v);
00349   ~XVColVector();
00350 
00351   void resize(int i);
00352   FrReal &operator [](int n);
00353   const FrReal &operator [](int n) const;
00354   XVColVector &operator=(const XVColVector &v);
00355   XVColVector &operator<<(const XVColVector &v);
00356   XVColVector &operator<<(FrReal *);
00357   XVColVector &operator=(const XVMatrix &m);
00358   XVColVector &operator=(FrReal x);
00359 
00360   // J.W. added the below Aug 15th
00361   
00362   XVColVector operator+(const XVColVector &mat) const;
00363   XVColVector operator-(const XVColVector &mat) const;
00364   XVColVector operator*(FrReal x) const;
00365   XVColVector operator-();
00366 
00367   XVColVector Rows(int first_row, int last_row);
00368   XVRowVector t() const;
00369   FrReal ip() const;
00370   FrReal ip(const XVColVector &) const;
00371 };
00372 
00374 extern void SVD(XVMatrix& , XVMatrix&, XVMatrix&, XVMatrix& );
00375 
00377 //#ifdef __xvimagescalar_h
00378 template <class T> XVColVector  &operator <<(XVColVector &x,const XVImageScalar<T> &y);
00379 template <class T> XVRowVector  &operator <<(XVRowVector &x,const XVImageScalar<T> &y);
00380 template <class T> XVImageScalar<T> &operator >>(const XVColVector &x,const XVImageScalar<T> &y);
00381 template <class T> XVImageScalar<T> &operator >>(const XVRowVector &x,const XVImageScalar<T> &y);
00382 
00383 //#endif
00384 
00385 
00386 
00387 
00388 
00395 class XVDiagonalMatrix
00396 {
00397 private:  
00398   XVColVector t;
00399 
00400 public:
00401   XVDiagonalMatrix(int n= 0);
00402   XVDiagonalMatrix(const XVColVector &tin);
00403 
00404   int n_of_cols() const;
00405   int n_of_rows() const;
00406   XVColVector& diagonal();
00407   void resize(int n);
00408 
00409   FrReal &operator() (int n);
00410   FrReal operator() (int n) const;
00411   XVDiagonalMatrix &operator = (const XVDiagonalMatrix &m);
00412   XVDiagonalMatrix &operator = (FrReal x);
00413 
00414   friend XVMatrix operator*(const XVDiagonalMatrix &, const XVMatrix &);
00415   friend XVRowVector operator*(const XVDiagonalMatrix &, const XVRowVector &);
00416   friend XVMatrix operator*(const XVMatrix&, const XVDiagonalMatrix &);
00417   friend XVDiagonalMatrix operator*(const XVDiagonalMatrix &x,const XVDiagonalMatrix &y);
00418   friend ostream &operator << (ostream &,const XVDiagonalMatrix &);
00419 };
00420 
00421 
00422 //----------------------------------------------------------------
00423 // utilities
00424 //----------------------------------------------------------------
00425 
00426 FrReal invs(FrReal x);
00427 
00428 template <class T>
00429 XVMatrix add_column(XVMatrix &x, XVImageScalar<T> &I);
00430 
00431 
00432 #include "XVMatrix.icc"
00433 
00434 #endif
00435 
00436 
00437 
00438 

Generated at Thu Mar 29 22:37:28 2001 for XVision by doxygen1.2.0 written by Dimitri van Heesch, © 1997-2000