FOX/ObjCryst++  1.10.X (development)
CrystVector.h
1 /* ObjCryst++ Object-Oriented Crystallographic Library
2  (c) 2000-2008 Vincent Favre-Nicolin vincefn@users.sourceforge.net
3  2000-2001 University of Geneva (Switzerland)
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 // This header defines which description should be used
20 // for vectors.
21 
22 #ifndef __LIBCRYST_VECTOR_H
23 #define __LIBCRYST_VECTOR_H
24 
25 #include "ObjCryst/ObjCryst/General.h"
26 
27 #undef __LIBCRYST_VECTOR_USE_BLITZ__
28 //#define __LIBCRYST_VECTOR_USE_BLITZ__
29 
30 #ifdef __LIBCRYST_VECTOR_USE_BLITZ__
31 
32 #include <blitz/array.h>
33 using namespace blitz;
34 
35 //Still use pointers for the Geometrical Structure computation ?
36 //(due to the complexity of the formulas, a blitz coding requires
37 //to much memory when compiling)
38 #define __VFN_GEOM_STRUCT_FACTOR_USE_POINTERS
39 
40 #define CrystVector_REAL Array<REAL,1>
41 #define CrystVector_float Array<float,1>
42 #define CrystVector_long Array<long,1>
43 #define CrystVector_int Array<int,1>
44 #define CrystVector_uint Array<unsigned int,1>
45 #define CrystVector_bool Array<bool,1>
46 
47 #define CrystMatrix_REAL Array<REAL,2>
48 #define CrystMatrix_float Array<float,2>
49 #define CrystMatrix_long Array<long,2>
50 #define CrystMatrix_int Array<int,2>
51 #define CrystMatrix_uint Array<unsigned int,2>
52 #define CrystMatrix_bool Array<bool,2>
53 
54 #define CrystVector_T Array<T,1>
55 #define CrystMatrix_T Array<T,2>
56 #define CrystArray3D_T Array<T,3>
57 
58 template<class T> T MaxDifference(const Array<T,1> &a,const Array<T,1> &b);
59 
60 template<class T> T MaxDifference(const Array<T,2> &a,const Array<T,2> &b);
61 
62 #else // __VFN_VECTOR_USE_BLITZ__
63 
64 
65 #define CrystVector_REAL CrystVector<REAL>
66 #define CrystVector_double CrystVector<double>
67 #define CrystVector_float CrystVector<float>
68 #define CrystVector_long CrystVector<long>
69 #define CrystVector_int CrystVector<int>
70 #define CrystVector_uint CrystVector<unsigned int>
71 #define CrystVector_bool CrystVector<bool>
72 
73 #define CrystMatrix_REAL CrystMatrix<REAL>
74 #define CrystMatrix_double CrystMatrix<double>
75 #define CrystMatrix_float CrystMatrix<float>
76 #define CrystMatrix_long CrystMatrix<long>
77 #define CrystMatrix_int CrystMatrix<int>
78 #define CrystMatrix_uint CrystMatrix<unsigned int>
79 #define CrystMatrix_bool CrystMatrix<bool>
80 
81 #define CrystArray3D_REAL CrystArray3D<REAL>
82 
83 #define CrystVector_T CrystVector<T>
84 #define CrystMatrix_T CrystMatrix<T>
85 #define CrystArray3D_T CrystMatrix<T>
86 
87 #define __VFN_GEOM_STRUCT_FACTOR_USE_POINTERS
88 
89 #include <iostream>
90 #include <cmath>
91 using namespace std;
92 
93 #ifdef _MSC_VER // MS VC++ predefined macros....
94 #undef min
95 #undef max
96 #endif
97 
98 //######################################################################
99 // CrystVector
100 //######################################################################
122 template<class T> class CrystVector
123 {
124  public:
125  CrystVector();
126 
127  CrystVector(const long nbElements);
128 
129  CrystVector(const CrystVector &old);
130 
131  ~CrystVector();
132 
133  void operator=(const CrystVector &old);
134 
135  template<class U> void operator=(const CrystVector<U> &old)
136  {
137  if(mNumElements != old.numElements())
138  {
139  mNumElements = old.numElements();
140  delete[] mpData;
141  mpData=new T[mNumElements];
142  };
143  mIsAreference=false;
144  T *p1=mpData;
145  const U *p2=old.data();
146  for(long i=0;i<mNumElements;i++) *p1++ = (T) *p2++;
147  }
148 
149  #ifdef __MWERKS__
150  operator CrystVector<bool>() const
151  {
152  CrystVector<bool> vect;
153  vect=*this;
154  return vect;
155  }
156  operator CrystVector<int>() const
157  {
158  CrystVector<int> vect;
159  vect=*this;
160  return vect;
161  }
162  operator CrystVector<long>() const
163  {
164  CrystVector<long> vect;
165  vect=*this;
166  return vect;
167  }
168  operator CrystVector<float>() const
169  {
170  CrystVector<float> vect;
171  vect=*this;
172  return vect;
173  }
174  operator CrystVector<double>() const
175  {
176  CrystVector<double> vect;
177  vect=*this;
178  return vect;
179  }
180  #else
181  template<class U> operator CrystVector<U>() const
182  {
183  CrystVector<U> vect;
184  vect=*this;
185  return vect;
186  }
187  #endif
188 
194  void reference(CrystVector &old, const long imin=0, const long imax=0);
195 
196  long numElements()const;
197  long size()const;
198  T sum()const;
199  T min()const;
200  T max()const;
202  unsigned long imin(const unsigned long start=0,const unsigned long finish=0)const;
204  unsigned long imax(const unsigned long start=0,const unsigned long finish=0)const;
205 
206  T * data();
207  const T * data() const;
208 
209  void resize(const long newNbElements);
210 
211  void resizeAndPreserve(const long newNbElements);
212 
213  void operator*=(const T num);
214 
215  void operator*=(const CrystVector &vect);
216 
217  void operator/=(const T num);
218 
219  void operator/=(const CrystVector &vect);
220 
221  void operator+=(const T num);
222 
223  void operator+=(const CrystVector &vect);
224 
225  void operator-=(const CrystVector &vect);
226 /* Buggy ? (my version, not Blitz's!)
227  // ListInitializer & ListInitializerSwitch are a simplified
228  //version borrowed from the blitz++ library (see the blitz/listinit.h file)
229  //
230  // It allows a very convenient way of initializing arrays like this:
231  // CrystVector arr(5); arr = 1,3,6,8,9;
232  class ListInitializer
233  {
234  public:
235  ListInitializer(T *pData):mpData(pData){};
236  ~ListInitializer(){cout << "toto";};
237  ListInitializer operator,(T x)
238  {
239  *mpData=x;
240  return ListInitializer(mpData+1);
241  }
242  private:
243  ListInitializer(){};
244  T *mpData;
245  };
246  class ListInitializerSwitch
247  {
248  public:
249  ListInitializerSwitch(CrystVector &vect, const T value):
250  mVectData(vect.data()),mValue(value),
251  mNumElements(vect.numElements()),wipeOnDestruct(true)
252  {};
253 
254  ~ListInitializerSwitch()
255  {
256  if(wipeOnDestruct)
257  { //only one value given -> set all elements
258  for(int i=0;i<mNumElements;i++)*mVectData++ = mValue;
259  };
260  };
261 
262  ListInitializer operator,(T x)
263  {
264  wipeOnDestruct=false;//operator, is used
265  *mVectData=mValue;
266  mVectData++;
267  *mVectData=x;
268  return ListInitializer(mVectData+1);
269  }
270  private:
271  T *mVectData;
272  T mValue;
273  long mNumElements;
274  bool wipeOnDestruct;
275  };
276 
277 
278  ListInitializerSwitch operator=(const T num)
279  {
280  return ListInitializerSwitch(*this,num);
281  }
282 */
283  void operator=(const T num);
284 
285  T operator()(const long i) const;
286 
287  T& operator()(const long i);
288 
289 
290  protected:
291  private:
292  T *mpData;
293  long mNumElements;
294  bool mIsAreference;//is a reference to another vector ?
295  //friend ostream& operator<<(ostream &os,const CrystVector &vect);
296 
297 };
298 
299 template<class T> ostream& operator<<(ostream &os, CrystVector<T> &vect);
300 
301 //Return the sorted subscripts of the array
302 template<class T> CrystVector<long> SortSubs(const CrystVector<T> &vect);
303 //Sort the array in place and also return the sorted subscripts
304 template<class T> long QuickSortSubs(CrystVector<T> &vect,
305  CrystVector<long> &subscript,
306  long last,long first=0, int depth=0);
307 
309 template<class T> CrystVector<T> cos(const CrystVector<T> &vect);
311 template<class T> CrystVector<T> sin(const CrystVector<T> &vect);
313 template<class T> CrystVector<T> tan(const CrystVector<T> &vect);
315 template<class T> CrystVector<T> sqrt(const CrystVector<T> &vect);
316 
317 //######################################################################
318 // CrystMatrix
319 //######################################################################
339 template<class T> class CrystMatrix
340 {
341  public:
342  CrystMatrix();
343  //ySize : number of rows, xSize : number of columns
344  CrystMatrix(const long ySize,const long xSize);
345 
346  CrystMatrix(const CrystMatrix &old);
347 
348  ~CrystMatrix();
349 
350  void operator=(const CrystMatrix &old);
351 
352  void reference(CrystMatrix &old);
353  long numElements()const;
354  long size()const;
355  T sum()const;
356  T min()const;
357  T max()const;
358  long rows()const;
359  long cols()const;
360 
361  T * data();
362  const T * data() const;
363 
364  void resize(const long ySize,const long xSize);
365 
366  void resizeAndPreserve(const long ySize,const long xSize);
367 
368  //void operator=(const T num);
370  void operator*=(const T num);
372  void operator*=(const CrystMatrix &vect);
374  void operator/=(const T num);
376  void operator+=(const T num);
378  void operator-=(const T num);
379 
381  CrystMatrix Mult(const CrystMatrix &rhs);
382  //:TODO: Check the following...
383 
384  // ListInitializer & ListInitializerSwitch are a simplified
385  //version borrowed from the blitz++ library (see the blitz/listinit.h file)
386  //
387  // It allows a very convenient way of initializing arrays like this:
388  // CrystVector arr(5); arr = 1,3,6,8,9;
390  {
391  public:
392  ListInitializer(T *pData):mpData(pData){};
393  ~ListInitializer(){};
394  ListInitializer operator,(T x)
395  {
396  *mpData=x;
397  return ListInitializer(mpData+1);
398  }
399  private:
400  ListInitializer(){};
401  T *mpData;
402  };
404  {
405  public:
406  ListInitializerSwitch(CrystMatrix &vect, const T value):
407  mVectData(vect.data()),mValue(value),
408  mNumElements(vect.numElements()),wipeOnDestruct(true)
409  {};
410 
412  {
413  if(wipeOnDestruct)
414  { //only one value given -> set all elements
415  for(int i=0;i<mNumElements;i++)*mVectData++ = mValue;
416  };
417  };
418 
419  ListInitializer operator,(T x)
420  {
421  wipeOnDestruct=false;//operator, is used
422  *mVectData=mValue;
423  mVectData++;
424  *mVectData=x;
425  return ListInitializer(mVectData+1);
426  }
427  private:
428  T *mVectData;
429  T mValue;
430  long mNumElements;
431  bool wipeOnDestruct;
432  };
433 
434 
435  ListInitializerSwitch operator=(const T num)
436  {
437  return ListInitializerSwitch(*this,num);
438  }
439 
440 
441  T operator()(const long i) const;
442 
443  T operator()(const long row,const long col) const;
444 
445  T& operator()(const long i);
446 
447  T& operator()(const long i,const long j);
448 
449  CrystMatrix transpose()const;
450 
451  protected:
452  private:
453  T *mpData;
454  long mNumElements;
455  long mXSize,mYSize;
456  bool mIsAreference;//is a reference to another vector ?
457 
458  //friend ostream& operator<<(ostream &os,const CrystMatrix &vect);
459 
460 };
461 
462 template<class T> ostream& operator<<(ostream &os, const CrystMatrix<T> &vect);
463 
464 template<class T> T MaxDifference(const CrystVector<T> &a,const CrystVector<T> &b);
465 
466 template<class T> T MaxDifference(const CrystMatrix<T> &a,const CrystMatrix<T> &b);
467 
468 template<class T> CrystMatrix<T> product(const CrystMatrix<T> &a,const CrystMatrix<T> &b);
469 
470 //######################################################################
471 // CrystArray3D
472 //######################################################################
492 template<class T> class CrystArray3D
493 {
494  public:
495  CrystArray3D();
496  //ySize : number of rows, xSize : number of columns
497  CrystArray3D(const long zSize,const long ySize,const long xSize);
498 
499  CrystArray3D(const CrystArray3D &old);
500 
501  ~CrystArray3D();
502 
503  void operator=(const CrystArray3D &old);
504 
505  void reference(CrystArray3D &old);
506  long numElements()const;
507  long size()const;
508  T sum()const;
509  T min()const;
510  T max()const;
511  long rows()const;
512  long cols()const;
513  long depth()const;
514 
515  T * data();
516  const T * data() const;
517 
518  void resize(const long zSize,const long ySize,const long xSize);
519 
520  void resizeAndPreserve(const long zSize,const long ySize,const long xSize);
521 
522  void operator=(const T num);
523  void operator*=(const T num);
524  void operator*=(const CrystArray3D &vect);
525  void operator/=(const T num);
526  void operator+=(const T num);
527  void operator-=(const T num);
528 
529  //void operator=(const T num);
530 
531  T operator()(const long i) const;
532 
533  T operator()(const long depth,const long row,const long col) const;
534 
535  T& operator()(const long i);
536 
537  T& operator()(const long depth,const long row,const long col);
538 
539  protected:
540  private:
541  T *mpData;
542  long mNumElements;
543  long mXSize,mYSize,mZSize;
544  bool mIsAreference;//is a reference to another vector ?
545 };
546 template<class T> ostream& operator<<(ostream &os, const CrystArray3D<T> &vect);
547 
548 #endif // __LIBCRYST_VECTOR_USE_BLITZ__
549 
550 //Basic Gauss-Jordan elimination with partial pivot (rows only, using max pivot)
551 //Definitly *not* optimized !
552 CrystMatrix_REAL InvertMatrix(const CrystMatrix_REAL &m);
553 template<class T> void MatrixExchangeRows(CrystMatrix_T &m, const long row1, const long row2);
554 
556 template<class T> T MaxAbs(const CrystVector_T &vector);
558 template<class T> T MinAbs(const CrystVector_T &vector);
559 
560 //######################################################################
561 // CubicSpline
562 //######################################################################
565 {
566  public:
568  CubicSpline();
570  CubicSpline(const CrystVector_REAL &x, const CrystVector_REAL &y,
571  const REAL yp1, const REAL ypn);
573  CubicSpline(const REAL *px, const REAL *py, const unsigned long nbPoints,
574  const REAL yp1, const REAL ypn);
576  CubicSpline(const CrystVector_REAL &x, const CrystVector_REAL &y);
578  CubicSpline(const REAL *px, const REAL *py, const unsigned long nbPoints);
579  ~CubicSpline();
580 
582  void Init(const CrystVector_REAL &x, const CrystVector_REAL &y,
583  const REAL yp1, const REAL ypn);
585  void Init(const REAL *px, const REAL *py, const unsigned long nbPoints,
586  const REAL yp1, const REAL ypn);
588  void Init(const CrystVector_REAL &x, const CrystVector_REAL &y);
590  void Init(const REAL *px, const REAL *py, const unsigned long nbPoints);
591 
593  CrystVector_REAL operator()(const CrystVector_REAL &x) const;
595  CrystVector_REAL operator()(const REAL min,const REAL step, const long nbpoint) const;
597  REAL operator()(const REAL x) const;
598  private:
599  void InitSpline(const REAL yp1, const REAL ypn);
600  void InitNaturalSpline();
601  CrystVector_REAL mX;
602  CrystVector_REAL mY;
603  CrystVector_REAL mYsecond;
604 };
605 //######################################################################
606 // Savitzky-Golay interpolation
607 //######################################################################
611 CrystVector_REAL SavitzkyGolay(const CrystVector_REAL &v, const unsigned int m, const unsigned int deriv);
612 
613 #endif // __LIBCRYST_VECTOR_H
3D Vector (Blitz++ mimic) for ObjCryst++
Definition: CrystVector.h:492
Vector library (Blitz++ mimic) for ObjCryst++.
Definition: CrystVector.h:122
Cubic spline interpolation.
Definition: CrystVector.h:564
2D Vector library (Blitz++ mimic) for ObjCryst++
Definition: CrystVector.h:339