Stan Math Library  2.14.0
reverse mode automatic differentiation
Eigen_NumTraits.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_EIGEN_NUMTRAITS_HPP
2 #define STAN_MATH_REV_MAT_FUN_EIGEN_NUMTRAITS_HPP
3 
5 #include <stan/math/rev/core.hpp>
7 #include <limits>
8 
9 namespace Eigen {
10 
18  template <>
19  struct NumTraits<stan::math::var> : GenericNumTraits<stan::math::var> {
23 
30  static inline stan::math::var dummy_precision() {
31  return NumTraits<double>::dummy_precision();
32  }
33 
34  enum {
38  IsComplex = 0,
39 
43  IsInteger = 0,
44 
45 
49  IsSigned = 1,
50 
54  RequireInitialization = 0,
55 
59  ReadCost = 2 * NumTraits<double>::ReadCost,
60 
65  AddCost = NumTraits<double>::AddCost,
66 
71  MulCost = NumTraits<double>::MulCost
72  };
73 
79  static int digits10() {
80  return std::numeric_limits<double>::digits10;
81  }
82  };
83 
84  namespace internal {
85 
86 #if EIGEN_VERSION_AT_LEAST(3, 3, 0)
87 
91  template <>
92  struct scalar_product_traits<stan::math::var, double> {
93  typedef stan::math::var ReturnType;
94  };
95 
100  template <>
101  struct scalar_product_traits<double, stan::math::var> {
102  typedef stan::math::var ReturnType;
103  };
104 
116  template <typename Index, typename LhsMapper, bool ConjugateLhs,
117  bool ConjugateRhs, typename RhsMapper, int Version>
118  struct general_matrix_vector_product<Index, stan::math::var, LhsMapper,
119  ColMajor, ConjugateLhs,
120  stan::math::var, RhsMapper,
121  ConjugateRhs, Version> {
122  typedef stan::math::var LhsScalar;
123  typedef stan::math::var RhsScalar;
124  typedef stan::math::var ResScalar;
125  enum { LhsStorageOrder = ColMajor };
126 
127  EIGEN_DONT_INLINE static void
128  run(Index rows, Index cols,
129  const LhsMapper& lhsMapper,
130  const RhsMapper& rhsMapper,
131  ResScalar* res, Index resIncr,
132  const ResScalar &alpha) {
133  const LhsScalar* lhs = lhsMapper.data();
134  const Index lhsStride = lhsMapper.stride();
135  const RhsScalar* rhs = rhsMapper.data();
136  const Index rhsIncr = rhsMapper.stride();
137  run(rows, cols, lhs, lhsStride, rhs, rhsIncr, res, resIncr, alpha);
138  }
139 
140  EIGEN_DONT_INLINE static void
141  run(Index rows, Index cols,
142  const LhsScalar* lhs, Index lhsStride,
143  const RhsScalar* rhs, Index rhsIncr,
144  ResScalar* res, Index resIncr,
145  const ResScalar &alpha) {
146  using stan::math::var;
148  for (Index i = 0; i < rows; ++i) {
149  res[i * resIncr]
150  += var(new gevv_vvv_vari(&alpha, &lhs[i], lhsStride,
151  rhs, rhsIncr, cols));
152  }
153  }
154  };
155 
156  template <typename Index, typename LhsMapper, bool ConjugateLhs,
157  bool ConjugateRhs, typename RhsMapper, int Version>
158  struct general_matrix_vector_product<Index, stan::math::var, LhsMapper,
159  RowMajor, ConjugateLhs,
160  stan::math::var, RhsMapper,
161  ConjugateRhs, Version> {
162  typedef stan::math::var LhsScalar;
163  typedef stan::math::var RhsScalar;
164  typedef stan::math::var ResScalar;
165  enum { LhsStorageOrder = RowMajor };
166 
167  EIGEN_DONT_INLINE static void
168  run(Index rows, Index cols,
169  const LhsMapper& lhsMapper,
170  const RhsMapper& rhsMapper,
171  ResScalar* res, Index resIncr,
172  const RhsScalar &alpha) {
173  const LhsScalar* lhs = lhsMapper.data();
174  const Index lhsStride = lhsMapper.stride();
175  const RhsScalar* rhs = rhsMapper.data();
176  const Index rhsIncr = rhsMapper.stride();
177  run(rows, cols, lhs, lhsStride, rhs, rhsIncr, res, resIncr, alpha);
178  }
179 
180  EIGEN_DONT_INLINE static void
181  run(Index rows, Index cols,
182  const LhsScalar* lhs, Index lhsStride,
183  const RhsScalar* rhs, Index rhsIncr,
184  ResScalar* res, Index resIncr,
185  const RhsScalar &alpha) {
186  for (Index i = 0; i < rows; i++) {
187  res[i*resIncr]
188  += stan::math::var
190  (&alpha,
191  (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
192  ? (&lhs[i]) : (&lhs[i*lhsStride]),
193  (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
194  ? (lhsStride) : (1),
195  rhs, rhsIncr, cols));
196  }
197  }
198  };
199 
200  template <typename Index, int LhsStorageOrder, bool ConjugateLhs,
201  int RhsStorageOrder, bool ConjugateRhs>
202  struct general_matrix_matrix_product<Index, stan::math::var,
203  LhsStorageOrder, ConjugateLhs,
204  stan::math::var, RhsStorageOrder,
205  ConjugateRhs, ColMajor> {
206  typedef stan::math::var LhsScalar;
207  typedef stan::math::var RhsScalar;
208  typedef stan::math::var ResScalar;
209 
210  typedef gebp_traits<RhsScalar, LhsScalar> Traits;
211 
212  typedef const_blas_data_mapper<stan::math::var, Index, LhsStorageOrder>
213  LhsMapper;
214  typedef const_blas_data_mapper<stan::math::var, Index, RhsStorageOrder>
215  RhsMapper;
216 
217  EIGEN_DONT_INLINE
218  static void run(Index rows, Index cols, Index depth,
219  const LhsScalar* lhs, Index lhsStride,
220  const RhsScalar* rhs, Index rhsStride,
221  ResScalar* res, Index resStride,
222  const ResScalar &alpha,
223  level3_blocking<LhsScalar, RhsScalar>& /* blocking */,
224  GemmParallelInfo<Index>* /* info = 0 */) {
225  for (Index i = 0; i < cols; i++) {
226  general_matrix_vector_product<Index, LhsScalar, LhsMapper,
227  LhsStorageOrder,
228  ConjugateLhs, RhsScalar, RhsMapper,
229  ConjugateRhs>
230  ::run(rows, depth, lhs, lhsStride,
231  &rhs[static_cast<int>(RhsStorageOrder)
232  == static_cast<int>(ColMajor)
233  ? i * rhsStride : i],
234  static_cast<int>(RhsStorageOrder)
235  == static_cast<int>(ColMajor)
236  ? 1 : rhsStride,
237  &res[i * resStride], 1, alpha);
238  }
239  }
240 
241  EIGEN_DONT_INLINE
242  static void run(Index rows, Index cols, Index depth,
243  const LhsMapper& lhsMapper,
244  const RhsMapper& rhsMapper,
245  ResScalar* res, Index resStride,
246  const ResScalar &alpha,
247  level3_blocking<LhsScalar, RhsScalar>& blocking,
248  GemmParallelInfo<Index>* info = 0) {
249  const LhsScalar* lhs = lhsMapper.data();
250  const Index lhsStride = lhsMapper.stride();
251  const RhsScalar* rhs = rhsMapper.data();
252  const Index rhsStride = rhsMapper.stride();
253 
254  run(rows, cols, depth, lhs, lhsStride, rhs, rhsStride, res, resStride,
255  alpha, blocking, info);
256  }
257  };
258 #else
259 
262  template<>
263  struct significant_decimals_default_impl<stan::math::var, false> {
264  static inline int run() {
265  using std::ceil;
266  using std::log;
267  return cast<double, int>(ceil(-log(std::numeric_limits<double>
268  ::epsilon())
269  / log(10.0)));
270  }
271  };
272 
277  template <>
278  struct scalar_product_traits<stan::math::var, double> {
280  };
281 
286  template <>
287  struct scalar_product_traits<double, stan::math::var> {
289  };
290 
294  template<typename Index, bool ConjugateLhs, bool ConjugateRhs>
295  struct general_matrix_vector_product<Index, stan::math::var, ColMajor,
296  ConjugateLhs, stan::math::var,
297  ConjugateRhs> {
300  typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
302  enum { LhsStorageOrder = ColMajor };
303 
304  EIGEN_DONT_INLINE static void run(
305  Index rows, Index cols,
306  const LhsScalar* lhs, Index lhsStride,
307  const RhsScalar* rhs, Index rhsIncr,
308  ResScalar* res, Index resIncr,
309  const ResScalar &alpha) {
310  for (Index i = 0; i < rows; i++) {
311  res[i*resIncr]
312  += stan::math::var
314  (&alpha,
315  (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
316  ?(&lhs[i]):(&lhs[i*lhsStride]),
317  (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
318  ?(lhsStride):(1),
319  rhs, rhsIncr, cols));
320  }
321  }
322  };
323  template<typename Index, bool ConjugateLhs, bool ConjugateRhs>
324  struct general_matrix_vector_product<Index, stan::math::var,
325  RowMajor, ConjugateLhs,
326  stan::math::var, ConjugateRhs> {
329  typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
331  enum { LhsStorageOrder = RowMajor };
332 
333  EIGEN_DONT_INLINE static void
334  run(Index rows, Index cols,
335  const LhsScalar* lhs, Index lhsStride,
336  const RhsScalar* rhs, Index rhsIncr,
337  ResScalar* res, Index resIncr, const RhsScalar &alpha) {
338  for (Index i = 0; i < rows; i++) {
339  res[i*resIncr]
340  += stan::math::var
342  (&alpha,
343  (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
344  ? (&lhs[i]) : (&lhs[i*lhsStride]),
345  (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
346  ? (lhsStride) : (1),
347  rhs, rhsIncr, cols));
348  }
349  }
350  };
351  template<typename Index, int LhsStorageOrder, bool ConjugateLhs,
352  int RhsStorageOrder, bool ConjugateRhs>
353  struct general_matrix_matrix_product<Index, stan::math::var,
354  LhsStorageOrder, ConjugateLhs,
355  stan::math::var, RhsStorageOrder,
356  ConjugateRhs, ColMajor> {
359  typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
361  static void run(Index rows, Index cols, Index depth,
362  const LhsScalar* lhs, Index lhsStride,
363  const RhsScalar* rhs, Index rhsStride,
364  ResScalar* res, Index resStride,
365  const ResScalar &alpha,
366  level3_blocking<LhsScalar, RhsScalar>& /* blocking */,
367  GemmParallelInfo<Index>* /* info = 0 */) {
368  for (Index i = 0; i < cols; i++) {
369  general_matrix_vector_product<Index, LhsScalar, LhsStorageOrder,
370  ConjugateLhs, RhsScalar, ConjugateRhs>
371  ::run(rows, depth, lhs, lhsStride,
372  &rhs[(static_cast<int>(RhsStorageOrder)
373  == static_cast<int>(ColMajor))
374  ? (i*rhsStride) :(i) ],
375  (static_cast<int>(RhsStorageOrder)
376  == static_cast<int>(ColMajor)) ? (1) : (rhsStride),
377  &res[i*resStride], 1, alpha);
378  }
379  }
380  };
381 #endif
382  }
383 }
384 #endif
int rows(const Eigen::Matrix< T, R, C > &m)
Return the number of rows in the specified matrix, vector, or row vector.
Definition: rows.hpp:20
static int digits10()
Return the number of decimal digits that can be represented without change.
static EIGEN_DONT_INLINE void run(Index rows, Index cols, const LhsScalar *lhs, Index lhsStride, const RhsScalar *rhs, Index rhsIncr, ResScalar *res, Index resIncr, const ResScalar &alpha)
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
static stan::math::var dummy_precision()
Return the precision for stan::math::var delegates to precision for douboe.
static void run(Index rows, Index cols, Index depth, const LhsScalar *lhs, Index lhsStride, const RhsScalar *rhs, Index rhsStride, ResScalar *res, Index resStride, const ResScalar &alpha, level3_blocking< LhsScalar, RhsScalar > &, GemmParallelInfo< Index > *)
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
(Expert) Numerical traits for algorithmic differentiation variables.
int cols(const Eigen::Matrix< T, R, C > &m)
Return the number of columns in the specified matrix, vector, or row vector.
Definition: cols.hpp:20
fvar< T > ceil(const fvar< T > &x)
Definition: ceil.hpp:11
static EIGEN_DONT_INLINE void run(Index rows, Index cols, const LhsScalar *lhs, Index lhsStride, const RhsScalar *rhs, Index rhsIncr, ResScalar *res, Index resIncr, const RhsScalar &alpha)

     [ Stan Home Page ] © 2011–2016, Stan Development Team.