00001
00002
00003
00004 #ifndef __LIBCRYST_VECTOR_H
00005 #define __LIBCRYST_VECTOR_H
00006
00007 #undef __LIBCRYST_VECTOR_USE_BLITZ__
00008
00009
00010 #ifdef __LIBCRYST_VECTOR_USE_BLITZ__
00011
00012 #include <blitz/array.h>
00013 using namespace blitz;
00014
00015
00016
00017
00018 #define __VFN_GEOM_STRUCT_FACTOR_USE_POINTERS
00019
00020 #define CrystVector_double Array<double,1>
00021 #define CrystVector_float Array<float,1>
00022 #define CrystVector_long Array<long,1>
00023 #define CrystVector_int Array<int,1>
00024 #define CrystVector_uint Array<unsigned int,1>
00025 #define CrystVector_bool Array<bool,1>
00026 #define CrystMatrix_double Array<double,2>
00027 #define CrystMatrix_float Array<float,2>
00028 #define CrystMatrix_long Array<long,2>
00029 #define CrystMatrix_int Array<int,2>
00030 #define CrystMatrix_uint Array<unsigned int,2>
00031 #define CrystMatrix_bool Array<bool,2>
00032
00033 #define CrystVector_T Array<T,1>
00034 #define CrystMatrix_T Array<T,2>
00035
00036 template<class T> T MaxDifference(const Array<T,1> &a,const Array<T,1> &b);
00037
00038 template<class T> T MaxDifference(const Array<T,2> &a,const Array<T,2> &b);
00039
00040 #else // __VFN_VECTOR_USE_BLITZ__
00041
00042
00043 #define CrystVector_double CrystVector<double>
00044 #define CrystVector_float CrystVector<float>
00045 #define CrystVector_long CrystVector<long>
00046 #define CrystVector_int CrystVector<int>
00047 #define CrystVector_uint CrystVector<unsigned int>
00048 #define CrystVector_bool CrystVector<bool>
00049 #define CrystMatrix_double CrystMatrix<double>
00050 #define CrystMatrix_float CrystMatrix<float>
00051 #define CrystMatrix_long CrystMatrix<long>
00052 #define CrystMatrix_int CrystMatrix<int>
00053 #define CrystMatrix_uint CrystMatrix<unsigned int>
00054 #define CrystMatrix_bool CrystMatrix<bool>
00055
00056 #define CrystVector_T CrystVector<T>
00057 #define CrystMatrix_T CrystMatrix<T>
00058
00059 #define __VFN_GEOM_STRUCT_FACTOR_USE_POINTERS
00060
00061 #include <iostream>
00062 #include <cmath>
00063 using namespace std;
00064
00065
00066
00067
00089 template<class T> class CrystVector
00090 {
00091 public:
00092 CrystVector();
00093
00094 CrystVector(const long nbElements);
00095
00096 CrystVector(const CrystVector &old);
00097
00098 ~CrystVector();
00099
00100 void operator=(const CrystVector &old);
00101
00102 template<class U> void operator=(const CrystVector<U> &old)
00103 {
00104 if(mNumElements != old.numElements())
00105 {
00106 mNumElements = old.numElements();
00107 delete[] mpData;
00108 mpData=new T[mNumElements];
00109 };
00110 mIsAreference=false;
00111 T *p1=mpData;
00112 const U *p2=old.data();
00113 for(long i=0;i<mNumElements;i++) *p1++ = (T) *p2++;
00114 }
00115
00116 #ifdef __MWERKS__
00117 operator CrystVector<bool>() const
00118 {
00119 CrystVector<bool> vect;
00120 vect=*this;
00121 return vect;
00122 }
00123 operator CrystVector<int>() const
00124 {
00125 CrystVector<int> vect;
00126 vect=*this;
00127 return vect;
00128 }
00129 operator CrystVector<long>() const
00130 {
00131 CrystVector<long> vect;
00132 vect=*this;
00133 return vect;
00134 }
00135 operator CrystVector<float>() const
00136 {
00137 CrystVector<float> vect;
00138 vect=*this;
00139 return vect;
00140 }
00141 operator CrystVector<double>() const
00142 {
00143 CrystVector<double> vect;
00144 vect=*this;
00145 return vect;
00146 }
00147 #else
00148 template<class U> operator CrystVector<U>() const
00149 {
00150 CrystVector<U> vect;
00151 vect=*this;
00152 return vect;
00153 }
00154 #endif
00155 void reference(CrystVector &old);
00156
00157 long numElements()const;
00158 T sum()const;
00159 T min()const;
00160 T max()const;
00161
00162 T * data();
00163 const T * data() const;
00164
00165 void resize(const long newNbElements);
00166
00167 void resizeAndPreserve(const long newNbElements);
00168
00169 void operator*=(const T num);
00170
00171 void operator*=(const CrystVector &vect);
00172
00173 void operator/=(const T num);
00174
00175 void operator+=(const T num);
00176
00177 void operator+=(const CrystVector &vect);
00178
00179 void operator-=(const CrystVector &vect);
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 void operator=(const T num);
00238
00239 T operator()(const long i) const;
00240
00241 T& operator()(const long i);
00242
00243
00244 protected:
00245 private:
00246 T *mpData;
00247 long mNumElements;
00248 bool mIsAreference;
00249
00250
00251 };
00252
00253 template<class T> ostream& operator<<(ostream &os, CrystVector<T> &vect);
00254
00255
00256 template<class T> CrystVector<long> SortSubs(const CrystVector<T> &vect);
00257
00258 template<class T> long QuickSortSubs(CrystVector<T> &vect,
00259 CrystVector<long> &subscript,
00260 long last,long first=0, int depth=0);
00261
00263 template<class T> CrystVector<T> cos(const CrystVector<T> &vect);
00265 template<class T> CrystVector<T> sin(const CrystVector<T> &vect);
00267 template<class T> CrystVector<T> tan(const CrystVector<T> &vect);
00269 template<class T> CrystVector<T> sqrt(const CrystVector<T> &vect);
00270
00271
00272
00273
00274
00294 template<class T> class CrystMatrix
00295 {
00296 public:
00297 CrystMatrix();
00298
00299 CrystMatrix(const long ySize,const long xSize);
00300
00301 CrystMatrix(const CrystMatrix &old);
00302
00303 ~CrystMatrix();
00304
00305 void operator=(const CrystMatrix &old);
00306
00307 void reference(CrystMatrix &old);
00308 long numElements()const;
00309 T sum()const;
00310 T min()const;
00311 T max()const;
00312 long rows()const;
00313 long cols()const;
00314
00315 T * data();
00316 const T * data() const;
00317
00318 void resize(const long ySize,const long xSize);
00319
00320 void resizeAndPreserve(const long ySize,const long xSize);
00321
00322 void operator*=(const T num);
00323 void operator*=(const CrystMatrix &vect);
00324 void operator/=(const T num);
00325 void operator+=(const T num);
00326 void operator-=(const T num);
00327
00328
00329
00330
00331
00332
00333
00334
00335 class ListInitializer
00336 {
00337 public:
00338 ListInitializer(T *pData):mpData(pData){};
00339 ~ListInitializer(){};
00340 ListInitializer operator,(T x)
00341 {
00342 *mpData=x;
00343 return ListInitializer(mpData+1);
00344 }
00345 private:
00346 ListInitializer(){};
00347 T *mpData;
00348 };
00349 class ListInitializerSwitch
00350 {
00351 public:
00352 ListInitializerSwitch(CrystMatrix &vect, const T value):
00353 mVectData(vect.data()),mValue(value),
00354 mNumElements(vect.numElements()),wipeOnDestruct(true)
00355 {};
00356
00357 ~ListInitializerSwitch()
00358 {
00359 if(wipeOnDestruct)
00360 {
00361 for(int i=0;i<mNumElements;i++)*mVectData++ = mValue;
00362 };
00363 };
00364
00365 ListInitializer operator,(T x)
00366 {
00367 wipeOnDestruct=false;
00368 *mVectData=mValue;
00369 mVectData++;
00370 *mVectData=x;
00371 return ListInitializer(mVectData+1);
00372 }
00373 private:
00374 T *mVectData;
00375 T mValue;
00376 long mNumElements;
00377 bool wipeOnDestruct;
00378 };
00379
00380
00381 ListInitializerSwitch operator=(const T num)
00382 {
00383 return ListInitializerSwitch(*this,num);
00384 }
00385
00386
00387
00388 T operator()(const long i) const;
00389
00390 T operator()(const long row,const long col) const;
00391
00392 T& operator()(const long i);
00393
00394 T& operator()(const long i,const long j);
00395
00396 CrystMatrix transpose(const int dim1, const int dim2)const;
00397
00398 protected:
00399 private:
00400 T *mpData;
00401 long mNumElements;
00402 long mXSize,mYSize;
00403 bool mIsAreference;
00404
00405
00406
00407 };
00408
00409 template<class T> ostream& operator<<(ostream &os, const CrystMatrix<T> &vect);
00410
00411 template<class T> T MaxDifference(const CrystVector<T> &a,const CrystVector<T> &b);
00412
00413 template<class T> T MaxDifference(const CrystMatrix<T> &a,const CrystMatrix<T> &b);
00414
00415 template<class T> CrystMatrix<T> product(const CrystMatrix<T> &a,const CrystMatrix<T> &b);
00416
00417 #endif // __LIBCRYST_VECTOR_USE_BLITZ__
00418
00419
00420
00421 CrystMatrix_double InvertMatrix(const CrystMatrix_double &m);
00422 template<class T> void MatrixExchangeRows(CrystMatrix_T &m, const long row1, const long row2);
00423
00425 template<class T> T MaxAbs(const CrystVector_T &vector);
00427 template<class T> T MinAbs(const CrystVector_T &vector);
00428
00429
00430
00431
00433
00434 {
00435 public:
00437 CubicSpline(const CrystVector_double &x, const CrystVector_double &y,
00438 const double yp1, const double ypn);
00440 CubicSpline(const CrystVector_double &x, const CrystVector_double &y);
00441 ~CubicSpline();
00442 double operator()(const double x) const;
00443 private:
00444 const CrystVector_double mX;
00445 const CrystVector_double mY;
00446 CrystVector_double mYsecond;
00447 };
00448
00449 #endif