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
00027
00028
00029
00030
00031
00032
00033
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
00046 #define NO_BOUNDS_CHECK
00047
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 *);
00053
00054 typedef struct {
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
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
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
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
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
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
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
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