Stan Math Library  2.12.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<> struct NumTraits<stan::math::var>
19  : GenericNumTraits<stan::math::var> {
20  static inline stan::math::var dummy_precision() {
21  return NumTraits<double>::dummy_precision();
22  }
23 
24  enum {
25  RequireInitialization = 0,
26  // ReadCost = twice the cost to copy a double
27  ReadCost = 2 * NumTraits<double>::ReadCost,
28  // AddCost = single addition going forward
29  AddCost = NumTraits<double>::AddCost,
30  // MulCost = single multiplication going forward
31  MulCost = NumTraits<double>::MulCost
32  };
33  };
34 
35  namespace internal {
39  template<>
40  struct significant_decimals_default_impl<stan::math::var, false> {
41  static inline int run() {
42  using std::ceil;
43  using std::log;
44  return cast<double, int>(ceil(-log(std::numeric_limits<double>
45  ::epsilon())
46  / log(10.0)));
47  }
48  };
49 
54  template <>
55  struct scalar_product_traits<stan::math::var, double> {
57  };
58 
63  template <>
64  struct scalar_product_traits<double, stan::math::var> {
66  };
67 
71  template<typename Index, bool ConjugateLhs, bool ConjugateRhs>
72  struct general_matrix_vector_product<Index, stan::math::var, ColMajor,
73  ConjugateLhs, stan::math::var,
74  ConjugateRhs> {
77  typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
79  enum { LhsStorageOrder = ColMajor };
80 
81  EIGEN_DONT_INLINE static void run(
82  Index rows, Index cols,
83  const LhsScalar* lhs, Index lhsStride,
84  const RhsScalar* rhs, Index rhsIncr,
85  ResScalar* res, Index resIncr,
86  const ResScalar &alpha) {
87  for (Index i = 0; i < rows; i++) {
88  res[i*resIncr]
91  (&alpha,
92  (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
93  ?(&lhs[i]):(&lhs[i*lhsStride]),
94  (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
95  ?(lhsStride):(1),
96  rhs, rhsIncr, cols));
97  }
98  }
99  };
100  template<typename Index, bool ConjugateLhs, bool ConjugateRhs>
101  struct general_matrix_vector_product<Index, stan::math::var,
102  RowMajor, ConjugateLhs,
103  stan::math::var, ConjugateRhs> {
106  typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
108  enum { LhsStorageOrder = RowMajor };
109 
110  EIGEN_DONT_INLINE static void
111  run(Index rows, Index cols,
112  const LhsScalar* lhs, Index lhsStride,
113  const RhsScalar* rhs, Index rhsIncr,
114  ResScalar* res, Index resIncr, const RhsScalar &alpha) {
115  for (Index i = 0; i < rows; i++) {
116  res[i*resIncr]
117  += stan::math::var
119  (&alpha,
120  (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
121  ? (&lhs[i]) : (&lhs[i*lhsStride]),
122  (static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
123  ? (lhsStride) : (1),
124  rhs, rhsIncr, cols));
125  }
126  }
127  };
128  template<typename Index, int LhsStorageOrder, bool ConjugateLhs,
129  int RhsStorageOrder, bool ConjugateRhs>
130  struct general_matrix_matrix_product<Index, stan::math::var,
131  LhsStorageOrder, ConjugateLhs,
132  stan::math::var, RhsStorageOrder,
133  ConjugateRhs, ColMajor> {
136  typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
138  static void run(Index rows, Index cols, Index depth,
139  const LhsScalar* lhs, Index lhsStride,
140  const RhsScalar* rhs, Index rhsStride,
141  ResScalar* res, Index resStride,
142  const ResScalar &alpha,
143  level3_blocking<LhsScalar, RhsScalar>& /* blocking */,
144  GemmParallelInfo<Index>* /* info = 0 */) {
145  for (Index i = 0; i < cols; i++) {
146  general_matrix_vector_product<Index, LhsScalar, LhsStorageOrder,
147  ConjugateLhs, RhsScalar, ConjugateRhs>
148  ::run(rows, depth, lhs, lhsStride,
149  &rhs[(static_cast<int>(RhsStorageOrder)
150  == static_cast<int>(ColMajor))
151  ? (i*rhsStride) :(i) ],
152  (static_cast<int>(RhsStorageOrder)
153  == static_cast<int>(ColMajor)) ? (1) : (rhsStride),
154  &res[i*resStride], 1, alpha);
155  }
156  }
157  };
158  }
159 }
160 
161 #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 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()
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
(Expert) Numerical traits for algorithmic differentiation variables.
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 > *)
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
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)
fvar< T > ceil(const fvar< T > &x)
Definition: ceil.hpp:11

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