fmatvec  0.0.0
Fast Matrix/Vector Library
types.h
1 /* Copyright (C) 2003-2005 Martin Förg
2 
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact:
18  * martin.o.foerg@googlemail.com
19  *
20  */
21 
22 #ifndef types_h
23 #define types_h
24 
25 #include <complex>
26 #include <string>
27 #include <stdexcept>
28 #include <boost/current_function.hpp>
29 
30 #ifdef _MSC_VER
31 #ifdef fmatvec_EXPORTS
32 #define FMATVEC_EXPORT __declspec(dllexport)
33 #else
34 #define FMATVEC_EXPORT __declspec(dllimport)
35 #endif
36 #else
37 #ifdef fmatvec_EXPORTS
38 #define FMATVEC_EXPORT __attribute((visibility ("default")))
39 #else
40 #define FMATVEC_EXPORT
41 #endif
42 #endif
43 
44 
45 #ifndef HAVE_LIBMKL_INTEL_LP64
46 #ifndef CBLAS_ENUM_DEFINED_H
47  #define CBLAS_ENUM_DEFINED_H
48  enum CBLAS_ORDER {CblasRowMajor=101, CblasColMajor=102 };
49  enum CBLAS_TRANSPOSE {CblasNoTrans=111, CblasTrans=112, CblasConjTrans=113,
50  AtlasConj=114};
51  enum CBLAS_UPLO {CblasUpper=121, CblasLower=122};
52  enum CBLAS_DIAG {CblasNonUnit=131, CblasUnit=132};
53  enum CBLAS_SIDE {CblasLeft=141, CblasRight=142};
54 #endif
55 #define CBLAS_INDEX int
56 
57 #ifndef ATLAS_ENUM_H
58  #define ATLAS_ENUM_H
59  #define ATLAS_ORDER CBLAS_ORDER
60  #define AtlasRowMajor CblasRowMajor
61  #define AtlasColMajor CblasColMajor
62  #define ATLAS_TRANS CBLAS_TRANSPOSE
63  #define AtlasNoTrans CblasNoTrans
64  #define AtlasTrans CblasTrans
65  #define AtlasConjTrans CblasConjTrans
66  #define ATLAS_UPLO CBLAS_UPLO
67  #define AtlasUpper CblasUpper
68  #define AtlasLower CblasLower
69  #define ATLAS_DIAG CBLAS_DIAG
70  #define AtlasNonUnit CblasNonUnit
71  #define AtlasUnit CblasUnit
72  #define ATLAS_SIDE CBLAS_SIDE
73  #define AtlasLeft CblasLeft
74  #define AtlasRight CblasRight
75 #endif
76 #else
77 #include "mkl_cblas.h"
78 #endif
79 
80 namespace std {
81  template<class T> class shared_ptr;
82 }
83 
84 namespace fmatvec {
85 
86  /***** initialization types *****/
87 
88  class Noinit { };
89  class Init { };
90  class Eye { };
91 
92  static Noinit NONINIT = Noinit();
93  static Init INIT = Init();
94  static Eye EYE = Eye();
95 
96  /***** matrix/vector (sub) types *****/
97 
98  class Ref {
99  };
100 
101  class Var {
102  };
103 
104  template<int M> class Fixed {
105  };
106 
107 // class General {
108 // };
109 //
110 // class GeneralBand {
111 // };
112 //
113 // class Symmetric {
114 // };
115 //
116 // class Diagonal {
117 // };
118 //
119 // class Sparse {
120 // };
121 //
122 // /*!
123 // * \brief Basic shape class for matrices.
124 // *
125 // * Class BasicType is the basic shape type.
126 // * */
127 // template<class Shape, class Mem>
128 // class Type {
129 // };
130 
136  class General {
137  };
138 
144  class GeneralBand {
145  };
146 
152  class Symmetric {
153  };
154 
160  class Rotation {
161  };
162 
168  class Diagonal {
169  };
170 
176  class Sparse {
177  };
178 
185  class Square {
186  };
187 
188 // template<int M, int N>
189 // class GeneralFixed: public BasicType {
190 // };
191 //
192 // template<int M>
193 // class SymmetricFixed: public BasicType {
194 // };
195 //
196 // class GeneralVar: public BasicType {
197 // };
198 //
199 // class SymmetricVar: public BasicType {
200 // };
201 //
202 // template<int M>
203 // class GeneralFixedVar: public BasicType {
204 // };
205 //
206 // template<int N>
207 // class GeneralVarFixed: public BasicType {
208 // };
209 
210  template<class AT1, class AT2> struct OperatorResult;
211 
212  #define FMATVEC_OPERATORRESULT1(AT, ATRes) \
213  template<> struct OperatorResult<AT, AT> { typedef ATRes Type; };
214 
215  #define FMATVEC_OPERATORRESULT2(AT1, AT2, ATRes) \
216  template<> struct OperatorResult<AT1, AT2> { typedef ATRes Type; }; \
217  template<> struct OperatorResult<AT2, AT1> { typedef ATRes Type; };
218 
219  FMATVEC_OPERATORRESULT1(double, double)
220  FMATVEC_OPERATORRESULT1(int, int)
221  FMATVEC_OPERATORRESULT1(std::complex<double>, std::complex<double>)
222 
223  FMATVEC_OPERATORRESULT2(double, std::complex<double>, std::complex<double>)
224  FMATVEC_OPERATORRESULT2(int, std::complex<double>, std::complex<double>)
225  FMATVEC_OPERATORRESULT2(int, double, double)
226 
227  // FMATVEC_ASSERT(expr, AT) is fully equal to assert(expr) if no spezialization of AssertUseException for AT exists.
228  // FMATVEC_ASSERT(expr, AT) is similar to assert(expr) but throws an exception instead of calling abort regardless whether
229  // NDEBUG is defined or not if their is a spezialization of AssertUseException for AT with value = true
230  #define FMATVEC_ASSERT(expr, AT) fmatvec_assert<AssertUseException<AT>::value>(expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, #expr);
231 
232  // a template which defined if for AT exceptions or assert should be used in fmatvec.
233  // spezialize this template to use exceptions.
234  // see also ast.h for a spezialization.
235  template<typename AT> struct AssertUseException { constexpr static bool value = false; };
236 
237  template<bool value>
238  inline void fmatvec_assert(bool expr, std::string_view file, int line, std::string_view func, std::string_view exprStr);
239 
240  template<>
241  inline void fmatvec_assert<false>(bool expr, std::string_view file, int line, std::string_view func, std::string_view exprStr) {
242  #ifndef NDEBUG
243  if(!expr) {
244  // print with fprintf since printing with higher level functions like cerr may be redirected.
245  fprintf(stderr, "%s", (std::string(file)+":"+std::to_string(line)+": "+
246  std::string(func)+": Assertion `"+std::string(exprStr)+"' failed.").c_str());
247  std::abort();
248  }
249  #endif
250  }
251 
252  template<>
253  inline void fmatvec_assert<true>(bool expr, std::string_view file, int line, std::string_view func, std::string_view exprStr) {
254  if(!expr)
255  throw std::runtime_error(std::string(file)+":"+std::to_string(line)+": "+
256  std::string(func)+": Assertion `"+std::string(exprStr)+"' failed.");
257  }
258 
259  // tag dispatching
260  struct Transpose {};
261 
262  class ErrorType;
263  class SymbolicExpression;
264  template<class, class> class Vector;
265  template<class, class> class RowVector;
266  template<class, class, class, class> class Matrix;
267  template<class, class> class SquareMatrix;
268 
269  // Replace in a fmatvec scalar, vector or matrix the AT with ATNew.
270  template<class MatVec, class NewAT>
271  struct ReplaceAT;
272 
273  template<class NewAT>
274  struct ReplaceAT<ErrorType, NewAT> {
275  using Type = ErrorType;
276  };
277 
278  template<class NewAT>
279  struct ReplaceAT<double, NewAT> {
280  using Type = NewAT;
281  };
282 
283  template<class NewAT>
284  struct ReplaceAT<SymbolicExpression, NewAT> {
285  using Type = NewAT;
286  };
287 
288  template<class Shape, class OldAT, class NewAT>
289  struct ReplaceAT<Vector<Shape, OldAT>, NewAT> {
290  using Type = Vector<Shape, NewAT>;
291  };
292 
293  template<class Shape, class OldAT, class NewAT>
294  struct ReplaceAT<RowVector<Shape, OldAT>, NewAT> {
296  };
297 
298  template<class MatType, class RowShape, class ColShape, class OldAT, class NewAT>
299  struct ReplaceAT<Matrix<MatType, RowShape, ColShape, OldAT>, NewAT> {
301  };
302 
303  template<class Shape, class OldAT, class NewAT>
304  struct ReplaceAT<SquareMatrix<Shape, OldAT>, NewAT> {
306  };
307 
308 }
309 
310 #endif
Definition: fmatvec.h:38
Definition: types.h:260
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Shape class for symmetric matrices.
Definition: types.h:152
Definition: types.h:89
Definition: types.h:101
Dummy shape class for square matrices.
Definition: types.h:185
Definition: fmatvec.h:50
Shape class for rotation matrices.
Definition: types.h:160
Definition: function.h:13
Shape class for general band matrices.
Definition: types.h:144
Definition: types.h:98
Definition: types.h:81
Definition: fmatvec.h:44
Definition: fmatvec.h:47
Definition: types.h:235
Namespace fmatvec.
Definition: _memory.cc:28
Shape class for diagonal matrices.
Definition: types.h:168
Shape class for sparse matrices.
Definition: types.h:176
Definition: types.h:88
Definition: types.h:210
Basic shape class for matrices.
Definition: types.h:136
Definition: types.h:271
Definition: ast.h:32
Definition: types.h:90