TooN
TooN.h
1 //-*- c++ -*-
2 
3 // Copyright (C) 2005,2009 Tom Drummond (twd20@cam.ac.uk),
4 // Ed Rosten (er258@cam.ac.uk), Gerhard Reitmayr (gr281@cam.ac.uk)
5 
6 //All rights reserved.
7 //
8 //Redistribution and use in source and binary forms, with or without
9 //modification, are permitted provided that the following conditions
10 //are met:
11 //1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 //
17 //THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
18 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 //ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
21 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 //POSSIBILITY OF SUCH DAMAGE.
28 
29 
30 #ifndef TOON_INCLUDE_TOON_H
31 #define TOON_INCLUDE_TOON_H
32 #include <iostream>
33 #include <cstdlib>
34 #include <limits>
35 #include <new>
36 #include <utility>
37 #include <vector>
38 #include <initializer_list>
39 #include <complex>
40 #include <TooN/internal/config.hh>
41 
42 #if defined TOON_NDEBUG || defined NDEBUG
43  #define TOON_NDEBUG_MISMATCH
44  #define TOON_NDEBUG_SLICE
45  #define TOON_NDEBUG_SIZE
46  #define TOON_NDEBUG_FILL
47 #endif
48 
49 #ifdef TOON_INITIALIZE_RANDOM
50 #include <ctime>
51 #endif
52 
53 #ifdef TOON_USE_LAPACK
54  #ifndef TOON_DETERMINANT_LAPACK
55  #define TOON_DETERMINANT_LAPACK 35
56  #endif
57 #endif
58 
60 namespace TooN {
61 
62 #ifdef TOON_TEST_INTERNALS
63  namespace Internal
64  {
65  struct BadIndex{};
66  struct SliceError{};
67  struct StaticSliceError{};
68  struct SizeMismatch{};
69  struct StaticSizeMismatch{};
70  struct VectorOverfill{};
71  struct StaticVectorOverfill{};
72  struct MatrixOverfill{};
73  struct StaticMatrixOverfill{};
74  struct Underfill{};
75  }
76 #endif
77 
78  using std::numeric_limits;
113  template<class C> struct IsField
114  {
115  static const int value = numeric_limits<C>::is_specialized;
116  };
117 
118  template<class C> struct IsField<std::complex<C> >
119  {
120  static const int value = numeric_limits<C>::is_specialized;
121  };
122 
127  template<class C> struct IsField<const C>
128  {
129  static const int value = IsField<C>::value;
130  };
131 
132  template<class C, class D> struct These_Types_Do_Not_Form_A_Field;
133 
136  namespace Internal
137  {
141  static const unsigned int max_bytes_on_stack=1000;
145  struct Slicing{};
146  template<int RowStride, int ColStride> struct Slice;
147  template<int Size, typename Precision, int Stride, typename Mem> struct GenericVBase;
148  }
149 
150  template<int Size, class Precision, class Base> struct Vector;
151  template<int Rows, int Cols, class Precision, class Base> struct Matrix;
152  template<int Size, class Precision, class Base> struct DiagonalMatrix;
153 
154 
155  #ifdef DOXYGEN_INCLUDE_ONLY_FOR_DOCS
156  template<typename T> struct Operator{
185 
187  int size() const;
189  int num_rows() const;
191  int num_cols() const;
193 
196 
199  template<int Size, class Precision, class Base>
200  void eval(Vector<Size, Precision, Base>& v) const;
201 
203  template <int Size, typename P1, typename B1>
204  void plusequals(Vector<Size, P1, B1>& v) const;
205 
207  template <int Size, typename P1, typename B1>
208  void minusequals(Vector<Size, P1, B1>& v) const;
209 
212  template <int Size, typename P1, typename B1>
213  Operator<T> add(const Vector<Size, P1, B1>& v) const;
214 
216  template <int Size, typename P1, typename B1>
217  Operator<T> rsubtract(const Vector<Size, P1, B1>& v) const;
218 
220  template <int Size, typename P1, typename B1>
221  Operator<T> lsubtract(const Vector<Size, P1, B1>& v) const;
222 
224 
229  template<int R, int C, class P, class B>
230  void eval(Matrix<R,C,P,B>& m) const;
231 
234  template <int Rows, int Cols, typename P1, typename B1>
235  Operator<T> add(const Matrix<Rows,Cols, P1, B1>& m) const;
236 
237 
239  template <int Rows, int Cols, typename P1, typename B1>
240  Operator<T> rsubtract(const Matrix<Rows,Cols, P1, B1>& m) const;
241 
243  template <int Rows, int Cols, typename P1, typename B1>
244  Operator<T> lsubtract(const Matrix<Rows,Cols, P1, B1>& m) const;
245 
247  template <int Rows, int Cols, typename P1, typename B1>
248  void plusequals(Matrix<Rows,Cols, P1, B1>& m) const;
249 
251  template <int Rows, int Cols, typename P1, typename B1>
252  void minusequals(Matrix<Rows,Cols, P1, B1>& m) const;
254 
255 
258 
261  Operator<T> operator()(int size) const;
262 
264  Operator<T> operator()(int num_rows, int num_cols) const;
266 
269  typedef T Precision;
270 
272  template<class Pout, class Pmult> Operator<Internal::Identity<Pout> > scale_me(const Pmult& m) const
273  {
274  return Operator<Internal::Identity<Pout> >(val*m);
275  }
277 
278  };
279  #else
280  template<typename T> struct Operator;
281  #endif
282 
284  static const int Dynamic = -1;
285  static const int Resizable = -0x7fffffff;
286 
287  namespace Internal
288  {
289  template<int i, int j> struct SimpleSizer{static const int size=i;};
290  template<int i> struct SimpleSizer<Dynamic, i>{static const int size=i;};
291  template<int i> struct SimpleSizer<i, Dynamic>{static const int size=i;};
292  template<> struct SimpleSizer<Dynamic, Dynamic> {static const int size=-1;};
293 
294  template<int i> struct IsStatic
295  {
296  static const bool is = (i!=Dynamic && i != Resizable);
297  };
298 
299  //Choose an output size, given a pair of input sizes. Be static if possible.
300  template<int i, int j=i> struct Sizer{
301  static const int size=SimpleSizer<Sizer<i>::size, Sizer<j>::size>::size;
302  };
303 
304  //Choose an output size, given an input size. Be static if possible.
305  //Otherwise be dynamic. Never generate a resizable vector.
306  template<int i> struct Sizer<i,i>{
307  static const int size = IsStatic<i>::is?i:Dynamic;
308  };
309  }
310 
312 #if defined TOON_DEFAULT_PRECISION_TYPE
313  typedef TOON_DEFAULT_PRECISION_TYPE DefaultPrecision;
314 #else
315  typedef double DefaultPrecision;
316 #endif
317 
318 #if defined TOON_FORTRAN_INTEGER && defined TOON_CLAPACK
319  #error Error: both TOON_FORTRAN_INTEGER and TOON_CLAPACK defined
320 #elif defined TOON_CLAPACK
321  typedef long FortranInteger;
322 #elif defined TOON_FORTRAN_INTEGER
323  typedef TOON_FORTRAN_INTEGER FortranInteger;
324 #else
325  typedef int FortranInteger;
326 #endif
327 
328 }
329 
330 #include <TooN/internal/size_mismatch.hh>
331 #include <TooN/internal/debug.hh>
332 
333 #include <TooN/internal/introspection.hh>
334 
335 
336 #include <TooN/internal/dchecktest.hh>
337 #include <TooN/internal/allocator.hh>
338 
339 #include <TooN/internal/overfill_error.hh>
340 #include <TooN/internal/slice_error.hh>
341 
342 #include <TooN/internal/comma.hh>
343 
344 #include <TooN/internal/vbase.hh>
345 #include <TooN/internal/vector.hh>
346 
347 #include <TooN/internal/mbase.hh>
348 #include <TooN/internal/matrix.hh>
349 #include <TooN/internal/reference.hh>
350 
351 #include <TooN/internal/make_vector.hh>
352 #include <TooN/internal/operators.hh>
353 
354 #include <TooN/internal/objects.h>
355 
356 #include <TooN/internal/diagmatrix.h>
357 
358 #include <TooN/internal/data.hh>
359 #include <TooN/internal/data_functions.hh>
360 
361 #include <TooN/helpers.h>
362 #include <TooN/determinant.h>
363 
364 namespace std
365 {
366  //Specialising std templates is explicitly allowed.
367  using TooN::swap;
368 }
369 
370 #endif
Definition: mbase.hh:43
Definition: vbase.hh:32
Definition: TooN.h:300
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
A vector.
Definition: vector.hh:126
Is a number a field? i.e., +, -, *, / defined.
Definition: TooN.h:113
Definition: TooN.h:289
Definition: TooN.h:364
A matrix.
Definition: matrix.hh:105
Definition: TooN.h:294
A diagonal matrix.
Definition: diagmatrix.h:108
Definition: operators.hh:119
double DefaultPrecision
All TooN classes default to using this precision for computations and storage.
Definition: TooN.h:315
Definition: TooN.h:145