Automatic Differentiation
 
Loading...
Searching...
No Matches
Eigen_NumTraits.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CORE_EIGEN_NUMTRAITS_HPP
2#define STAN_MATH_REV_CORE_EIGEN_NUMTRAITS_HPP
3
12#include <limits>
13
14namespace Eigen {
15
23template <>
24struct NumTraits<stan::math::var> : GenericNumTraits<stan::math::var> {
35 static inline Real dummy_precision() {
36 return NumTraits<double>::dummy_precision();
37 }
38
39 static inline Real epsilon() { return NumTraits<double>::epsilon(); }
40
41 static inline Real highest() { return NumTraits<double>::highest(); }
42 static inline Real lowest() { return NumTraits<double>::lowest(); }
43
44 enum {
48 IsComplex = 0,
49
53 IsInteger = 0,
54
58 IsSigned = 1,
59
65 RequireInitialization = 0,
66
70 ReadCost = 2 * NumTraits<double>::ReadCost,
71
76 AddCost = NumTraits<double>::AddCost,
77
82 MulCost = NumTraits<double>::MulCost
83 };
84
90 static int digits10() { return std::numeric_limits<double>::digits10; }
91};
92
100template <typename BinaryOp>
101struct ScalarBinaryOpTraits<stan::math::var, double, BinaryOp> {
103};
104
112template <typename BinaryOp>
113struct ScalarBinaryOpTraits<double, stan::math::var, BinaryOp> {
115};
116
124template <typename BinaryOp>
125struct ScalarBinaryOpTraits<stan::math::var, int, BinaryOp> {
127};
128
136template <typename BinaryOp>
137struct ScalarBinaryOpTraits<int, stan::math::var, BinaryOp> {
139};
140
149template <typename BinaryOp>
150struct ScalarBinaryOpTraits<stan::math::var, stan::math::var, BinaryOp> {
152};
153
161template <typename BinaryOp>
162struct ScalarBinaryOpTraits<double, std::complex<stan::math::var>, BinaryOp> {
163 using ReturnType = std::complex<stan::math::var>;
164};
165
173template <typename BinaryOp>
174struct ScalarBinaryOpTraits<std::complex<stan::math::var>, double, BinaryOp> {
175 using ReturnType = std::complex<stan::math::var>;
176};
177
185template <typename BinaryOp>
186struct ScalarBinaryOpTraits<int, std::complex<stan::math::var>, BinaryOp> {
187 using ReturnType = std::complex<stan::math::var>;
188};
189
197template <typename BinaryOp>
198struct ScalarBinaryOpTraits<std::complex<stan::math::var>, int, BinaryOp> {
199 using ReturnType = std::complex<stan::math::var>;
200};
201
209template <typename BinaryOp>
210struct ScalarBinaryOpTraits<stan::math::var, std::complex<double>, BinaryOp> {
211 using ReturnType = std::complex<stan::math::var>;
212};
213
221template <typename BinaryOp>
222struct ScalarBinaryOpTraits<std::complex<double>, stan::math::var, BinaryOp> {
223 using ReturnType = std::complex<stan::math::var>;
224};
225
233template <typename BinaryOp>
234struct ScalarBinaryOpTraits<std::complex<double>, std::complex<stan::math::var>,
235 BinaryOp> {
236 using ReturnType = std::complex<stan::math::var>;
237};
238
246template <typename BinaryOp>
247struct ScalarBinaryOpTraits<std::complex<stan::math::var>, std::complex<double>,
248 BinaryOp> {
249 using ReturnType = std::complex<stan::math::var>;
250};
251
252template <typename BinaryOp>
253struct ScalarBinaryOpTraits<stan::math::var, std::complex<stan::math::var>,
254 BinaryOp> {
255 using ReturnType = std::complex<stan::math::var>;
256};
257
258template <typename BinaryOp>
259struct ScalarBinaryOpTraits<std::complex<stan::math::var>, stan::math::var,
260 BinaryOp> {
261 using ReturnType = std::complex<stan::math::var>;
262};
263
264template <typename BinaryOp>
265struct ScalarBinaryOpTraits<std::complex<stan::math::var>,
266 std::complex<stan::math::var>, BinaryOp> {
267 using ReturnType = std::complex<stan::math::var>;
268};
269
270namespace internal {
271
272#if EIGEN_VERSION_AT_LEAST(5, 0, 0)
288template <typename CondScalar, typename Arg1, typename Arg2, typename Arg3>
289struct ternary_evaluator<
290 CwiseTernaryOp<
291 scalar_boolean_select_op<stan::math::var, stan::math::var, CondScalar>,
292 Arg1, Arg2, Arg3>,
293 IndexBased, IndexBased>
295 scalar_boolean_select_op<stan::math::var, stan::math::var,
296 CondScalar>,
297 Arg1, Arg2, Arg3> {
299 scalar_boolean_select_op<stan::math::var, stan::math::var, CondScalar>,
300 Arg1, Arg2, Arg3>;
301 using Base::Base;
302};
303#endif
304
308template <typename EigVar, typename EigVari, typename EigDbl>
309struct functor_has_linear_access<
310 stan::math::vi_val_adj_functor<EigVar, EigVari, EigDbl>> {
311 enum { ret = 1 };
312};
313
317template <typename EigVar, typename EigDbl>
318struct functor_has_linear_access<stan::math::val_adj_functor<EigVar, EigDbl>> {
319 enum { ret = 1 };
320};
321
325template <typename EigVar, typename EigVari>
326struct functor_has_linear_access<stan::math::vi_val_functor<EigVar, EigVari>> {
327 enum { ret = 1 };
328};
329
333template <typename EigVar, typename EigVari>
334struct functor_has_linear_access<stan::math::vi_adj_functor<EigVar, EigVari>> {
335 enum { ret = 1 };
336};
337
342template <>
343struct remove_all<stan::math::vari*> {
345};
346
358template <typename Index, typename LhsMapper, bool ConjugateLhs,
359 bool ConjugateRhs, typename RhsMapper, int Version>
360struct general_matrix_vector_product<Index, stan::math::var, LhsMapper,
361 ColMajor, ConjugateLhs, stan::math::var,
362 RhsMapper, ConjugateRhs, Version> {
366 enum { LhsStorageOrder = ColMajor };
367
368 EIGEN_DONT_INLINE static void run(Index rows, Index cols,
369 const LhsMapper& lhsMapper,
370 const RhsMapper& rhsMapper, ResScalar* res,
371 Index resIncr, const ResScalar& alpha) {
372 const LhsScalar* lhs = lhsMapper.data();
373 const Index lhsStride = lhsMapper.stride();
374 const RhsScalar* rhs = rhsMapper.data();
375 const Index rhsIncr = rhsMapper.stride();
376 run(rows, cols, lhs, lhsStride, rhs, rhsIncr, res, resIncr, alpha);
377 }
378
379 EIGEN_DONT_INLINE static void run(Index rows, Index cols,
380 const LhsScalar* lhs, Index lhsStride,
381 const RhsScalar* rhs, Index rhsIncr,
382 ResScalar* res, Index resIncr,
383 const ResScalar& alpha) {
385 using stan::math::var;
386 for (Index i = 0; i < rows; ++i) {
387 res[i * resIncr] += var(
388 new gevv_vvv_vari(&alpha, &lhs[i], lhsStride, rhs, rhsIncr, cols));
389 }
390 }
391};
392
393template <typename Index, typename LhsMapper, bool ConjugateLhs,
394 bool ConjugateRhs, typename RhsMapper, int Version>
395struct general_matrix_vector_product<Index, stan::math::var, LhsMapper,
396 RowMajor, ConjugateLhs, stan::math::var,
397 RhsMapper, ConjugateRhs, Version> {
401 enum { LhsStorageOrder = RowMajor };
402
403 EIGEN_DONT_INLINE static void run(Index rows, Index cols,
404 const LhsMapper& lhsMapper,
405 const RhsMapper& rhsMapper, ResScalar* res,
406 Index resIncr, const RhsScalar& alpha) {
407 const LhsScalar* lhs = lhsMapper.data();
408 const Index lhsStride = lhsMapper.stride();
409 const RhsScalar* rhs = rhsMapper.data();
410 const Index rhsIncr = rhsMapper.stride();
411 run(rows, cols, lhs, lhsStride, rhs, rhsIncr, res, resIncr, alpha);
412 }
413
414 EIGEN_DONT_INLINE static void run(Index rows, Index cols,
415 const LhsScalar* lhs, Index lhsStride,
416 const RhsScalar* rhs, Index rhsIncr,
417 ResScalar* res, Index resIncr,
418 const RhsScalar& alpha) {
419 for (Index i = 0; i < rows; i++) {
420 res[i * resIncr] += stan::math::var(new stan::math::gevv_vvv_vari(
421 &alpha,
422 (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
423 ? (&lhs[i])
424 : (&lhs[i * lhsStride]),
425 (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
426 ? (lhsStride)
427 : (1),
428 rhs, rhsIncr, cols));
429 }
430 }
431};
432
433#if EIGEN_VERSION_AT_LEAST(3, 3, 8)
434template <typename Index, int LhsStorageOrder, bool ConjugateLhs,
435 int RhsStorageOrder, bool ConjugateRhs, int ResInnerStride>
436struct general_matrix_matrix_product<
437 Index, stan::math::var, LhsStorageOrder, ConjugateLhs, stan::math::var,
438 RhsStorageOrder, ConjugateRhs, ColMajor, ResInnerStride> {
439#else
440template <typename Index, int LhsStorageOrder, bool ConjugateLhs,
441 int RhsStorageOrder, bool ConjugateRhs>
442struct general_matrix_matrix_product<Index, stan::math::var, LhsStorageOrder,
443 ConjugateLhs, stan::math::var,
444 RhsStorageOrder, ConjugateRhs, ColMajor> {
445#endif
449
450 using Traits = gebp_traits<RhsScalar, LhsScalar>;
451
453 = const_blas_data_mapper<stan::math::var, Index, LhsStorageOrder>;
455 = const_blas_data_mapper<stan::math::var, Index, RhsStorageOrder>;
456
457 EIGEN_DONT_INLINE
458#if EIGEN_VERSION_AT_LEAST(3, 3, 8)
459 static void run(Index rows, Index cols, Index depth, const LhsScalar* lhs,
460 Index lhsStride, const RhsScalar* rhs, Index rhsStride,
461 ResScalar* res, Index resIncr, Index resStride,
462 const ResScalar& alpha,
463 level3_blocking<LhsScalar, RhsScalar>& /* blocking */,
464 GemmParallelInfo<Index>* /* info = 0 */)
465#else
466 static void run(Index rows, Index cols, Index depth, const LhsScalar* lhs,
467 Index lhsStride, const RhsScalar* rhs, Index rhsStride,
468 ResScalar* res, Index resStride, const ResScalar& alpha,
469 level3_blocking<LhsScalar, RhsScalar>& /* blocking */,
470 GemmParallelInfo<Index>* /* info = 0 */)
471#endif
472 {
473 for (Index i = 0; i < cols; i++) {
474 general_matrix_vector_product<
475 Index, LhsScalar, LhsMapper, LhsStorageOrder, ConjugateLhs, RhsScalar,
476 RhsMapper,
477 ConjugateRhs>::run(rows, depth, lhs, lhsStride,
478 &rhs[static_cast<int>(RhsStorageOrder)
479 == static_cast<int>(ColMajor)
480 ? i * rhsStride
481 : i],
482 static_cast<int>(RhsStorageOrder)
483 == static_cast<int>(ColMajor)
484 ? 1
485 : rhsStride,
486 &res[i * resStride], 1, alpha);
487 }
488 } // namespace internal
489
490 EIGEN_DONT_INLINE
491 static void run(Index rows, Index cols, Index depth,
492 const LhsMapper& lhsMapper, const RhsMapper& rhsMapper,
493 ResScalar* res, Index resStride, const ResScalar& alpha,
494 level3_blocking<LhsScalar, RhsScalar>& blocking,
495 GemmParallelInfo<Index>* info = 0) {
496 const LhsScalar* lhs = lhsMapper.data();
497 const Index lhsStride = lhsMapper.stride();
498 const RhsScalar* rhs = rhsMapper.data();
499 const Index rhsStride = rhsMapper.stride();
500
501 run(rows, cols, depth, lhs, lhsStride, rhs, rhsStride, res, resStride,
502 alpha, blocking, info);
503 }
504};
505} // namespace internal
506} // namespace Eigen
507#endif
Specialization of the standard library complex number type for reverse-mode autodiff type stan::math:...
(Expert) Numerical traits for algorithmic differentiation variables.
var_value< double > var
Definition var.hpp:1187
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
STL namespace.
static Real dummy_precision()
Return the precision for stan::math::var delegates to precision for double.
static int digits10()
Return the number of decimal digits that can be represented without change.
static void run(Index rows, Index cols, Index depth, const LhsScalar *lhs, Index lhsStride, const RhsScalar *rhs, Index rhsStride, ResScalar *res, Index resIncr, Index resStride, const ResScalar &alpha, level3_blocking< LhsScalar, RhsScalar > &, GemmParallelInfo< Index > *)
static void run(Index rows, Index cols, Index depth, const LhsMapper &lhsMapper, const RhsMapper &rhsMapper, ResScalar *res, Index resStride, const ResScalar &alpha, level3_blocking< LhsScalar, RhsScalar > &blocking, GemmParallelInfo< Index > *info=0)
static void run(Index rows, Index cols, const LhsScalar *lhs, Index lhsStride, const RhsScalar *rhs, Index rhsIncr, ResScalar *res, Index resIncr, const RhsScalar &alpha)
static void run(Index rows, Index cols, const LhsMapper &lhsMapper, const RhsMapper &rhsMapper, ResScalar *res, Index resIncr, const RhsScalar &alpha)
static void run(Index rows, Index cols, const LhsMapper &lhsMapper, const RhsMapper &rhsMapper, ResScalar *res, Index resIncr, const ResScalar &alpha)
static void run(Index rows, Index cols, const LhsScalar *lhs, Index lhsStride, const RhsScalar *rhs, Index rhsIncr, ResScalar *res, Index resIncr, const ResScalar &alpha)
Evaluator for .select() expressions that evaluates only the branch chosen by the condition,...