Eigen_NumTraits.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_MAT_FUN_EIGEN_NUMTRAITS_HPP
#define STAN_MATH_REV_MAT_FUN_EIGEN_NUMTRAITS_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/rev/core.hpp>
#include <stan/math/rev/core/std_numeric_limits.hpp>
#include <limits>
namespace Eigen {
template <>
struct NumTraits<stan::math::var> : GenericNumTraits<stan::math::var> {
typedef stan::math::var Real;
typedef stan::math::var NonInteger;
typedef stan::math::var Nested;
static inline stan::math::var dummy_precision() {
return NumTraits<double>::dummy_precision();
}
enum {
IsComplex = 0,
IsInteger = 0,
IsSigned = 1,
RequireInitialization = 0,
ReadCost = 2 * NumTraits<double>::ReadCost,
AddCost = NumTraits<double>::AddCost,
MulCost = NumTraits<double>::MulCost
};
static int digits10() {
return std::numeric_limits<double>::digits10;
}
};
namespace internal {
#if EIGEN_VERSION_AT_LEAST(3, 3, 0)
template <>
struct scalar_product_traits<stan::math::var, double> {
typedef stan::math::var ReturnType;
};
template <>
struct scalar_product_traits<double, stan::math::var> {
typedef stan::math::var ReturnType;
};
template <typename Index, typename LhsMapper, bool ConjugateLhs,
bool ConjugateRhs, typename RhsMapper, int Version>
struct general_matrix_vector_product<Index, stan::math::var, LhsMapper,
ColMajor, ConjugateLhs,
stan::math::var, RhsMapper,
ConjugateRhs, Version> {
typedef stan::math::var LhsScalar;
typedef stan::math::var RhsScalar;
typedef stan::math::var ResScalar;
enum { LhsStorageOrder = ColMajor };
EIGEN_DONT_INLINE static void
run(Index rows, Index cols,
const LhsMapper& lhsMapper,
const RhsMapper& rhsMapper,
ResScalar* res, Index resIncr,
const ResScalar &alpha) {
const LhsScalar* lhs = lhsMapper.data();
const Index lhsStride = lhsMapper.stride();
const RhsScalar* rhs = rhsMapper.data();
const Index rhsIncr = rhsMapper.stride();
run(rows, cols, lhs, lhsStride, rhs, rhsIncr, res, resIncr, alpha);
}
EIGEN_DONT_INLINE static void
run(Index rows, Index cols,
const LhsScalar* lhs, Index lhsStride,
const RhsScalar* rhs, Index rhsIncr,
ResScalar* res, Index resIncr,
const ResScalar &alpha) {
using stan::math::var;
using stan::math::gevv_vvv_vari;
for (Index i = 0; i < rows; ++i) {
res[i * resIncr]
+= var(new gevv_vvv_vari(&alpha, &lhs[i], lhsStride,
rhs, rhsIncr, cols));
}
}
};
template <typename Index, typename LhsMapper, bool ConjugateLhs,
bool ConjugateRhs, typename RhsMapper, int Version>
struct general_matrix_vector_product<Index, stan::math::var, LhsMapper,
RowMajor, ConjugateLhs,
stan::math::var, RhsMapper,
ConjugateRhs, Version> {
typedef stan::math::var LhsScalar;
typedef stan::math::var RhsScalar;
typedef stan::math::var ResScalar;
enum { LhsStorageOrder = RowMajor };
EIGEN_DONT_INLINE static void
run(Index rows, Index cols,
const LhsMapper& lhsMapper,
const RhsMapper& rhsMapper,
ResScalar* res, Index resIncr,
const RhsScalar &alpha) {
const LhsScalar* lhs = lhsMapper.data();
const Index lhsStride = lhsMapper.stride();
const RhsScalar* rhs = rhsMapper.data();
const Index rhsIncr = rhsMapper.stride();
run(rows, cols, lhs, lhsStride, rhs, rhsIncr, res, resIncr, alpha);
}
EIGEN_DONT_INLINE static void
run(Index rows, Index cols,
const LhsScalar* lhs, Index lhsStride,
const RhsScalar* rhs, Index rhsIncr,
ResScalar* res, Index resIncr,
const RhsScalar &alpha) {
for (Index i = 0; i < rows; i++) {
res[i*resIncr]
+= stan::math::var
(new stan::math::gevv_vvv_vari
(&alpha,
(static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
? (&lhs[i]) : (&lhs[i*lhsStride]),
(static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
? (lhsStride) : (1),
rhs, rhsIncr, cols));
}
}
};
template <typename Index, int LhsStorageOrder, bool ConjugateLhs,
int RhsStorageOrder, bool ConjugateRhs>
struct general_matrix_matrix_product<Index, stan::math::var,
LhsStorageOrder, ConjugateLhs,
stan::math::var, RhsStorageOrder,
ConjugateRhs, ColMajor> {
typedef stan::math::var LhsScalar;
typedef stan::math::var RhsScalar;
typedef stan::math::var ResScalar;
typedef gebp_traits<RhsScalar, LhsScalar> Traits;
typedef const_blas_data_mapper<stan::math::var, Index, LhsStorageOrder>
LhsMapper;
typedef const_blas_data_mapper<stan::math::var, Index, RhsStorageOrder>
RhsMapper;
EIGEN_DONT_INLINE
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>& /* blocking */,
GemmParallelInfo<Index>* /* info = 0 */) {
for (Index i = 0; i < cols; i++) {
general_matrix_vector_product<Index, LhsScalar, LhsMapper,
LhsStorageOrder,
ConjugateLhs, RhsScalar, RhsMapper,
ConjugateRhs>
::run(rows, depth, lhs, lhsStride,
&rhs[static_cast<int>(RhsStorageOrder)
== static_cast<int>(ColMajor)
? i * rhsStride : i],
static_cast<int>(RhsStorageOrder)
== static_cast<int>(ColMajor)
? 1 : rhsStride,
&res[i * resStride], 1, alpha);
}
}
EIGEN_DONT_INLINE
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) {
const LhsScalar* lhs = lhsMapper.data();
const Index lhsStride = lhsMapper.stride();
const RhsScalar* rhs = rhsMapper.data();
const Index rhsStride = rhsMapper.stride();
run(rows, cols, depth, lhs, lhsStride, rhs, rhsStride, res, resStride,
alpha, blocking, info);
}
};
#else
template<>
struct significant_decimals_default_impl<stan::math::var, false> {
static inline int run() {
using std::ceil;
using std::log;
return cast<double, int>(ceil(-log(std::numeric_limits<double>
::epsilon())
/ log(10.0)));
}
};
template <>
struct scalar_product_traits<stan::math::var, double> {
typedef stan::math::var ReturnType;
};
template <>
struct scalar_product_traits<double, stan::math::var> {
typedef stan::math::var ReturnType;
};
template<typename Index, bool ConjugateLhs, bool ConjugateRhs>
struct general_matrix_vector_product<Index, stan::math::var, ColMajor,
ConjugateLhs, stan::math::var,
ConjugateRhs> {
typedef stan::math::var LhsScalar;
typedef stan::math::var RhsScalar;
typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
ResScalar;
enum { LhsStorageOrder = ColMajor };
EIGEN_DONT_INLINE static void run(
Index rows, Index cols,
const LhsScalar* lhs, Index lhsStride,
const RhsScalar* rhs, Index rhsIncr,
ResScalar* res, Index resIncr,
const ResScalar &alpha) {
for (Index i = 0; i < rows; i++) {
res[i*resIncr]
+= stan::math::var
(new stan::math::gevv_vvv_vari
(&alpha,
(static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
?(&lhs[i]):(&lhs[i*lhsStride]),
(static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
?(lhsStride):(1),
rhs, rhsIncr, cols));
}
}
};
template<typename Index, bool ConjugateLhs, bool ConjugateRhs>
struct general_matrix_vector_product<Index, stan::math::var,
RowMajor, ConjugateLhs,
stan::math::var, ConjugateRhs> {
typedef stan::math::var LhsScalar;
typedef stan::math::var RhsScalar;
typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
ResScalar;
enum { LhsStorageOrder = RowMajor };
EIGEN_DONT_INLINE static void
run(Index rows, Index cols,
const LhsScalar* lhs, Index lhsStride,
const RhsScalar* rhs, Index rhsIncr,
ResScalar* res, Index resIncr, const RhsScalar &alpha) {
for (Index i = 0; i < rows; i++) {
res[i*resIncr]
+= stan::math::var
(new stan::math::gevv_vvv_vari
(&alpha,
(static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
? (&lhs[i]) : (&lhs[i*lhsStride]),
(static_cast<int>(LhsStorageOrder) == static_cast<int>(ColMajor))
? (lhsStride) : (1),
rhs, rhsIncr, cols));
}
}
};
template<typename Index, int LhsStorageOrder, bool ConjugateLhs,
int RhsStorageOrder, bool ConjugateRhs>
struct general_matrix_matrix_product<Index, stan::math::var,
LhsStorageOrder, ConjugateLhs,
stan::math::var, RhsStorageOrder,
ConjugateRhs, ColMajor> {
typedef stan::math::var LhsScalar;
typedef stan::math::var RhsScalar;
typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
ResScalar;
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>& /* blocking */,
GemmParallelInfo<Index>* /* info = 0 */) {
for (Index i = 0; i < cols; i++) {
general_matrix_vector_product<Index, LhsScalar, LhsStorageOrder,
ConjugateLhs, RhsScalar, ConjugateRhs>
::run(rows, depth, lhs, lhsStride,
&rhs[(static_cast<int>(RhsStorageOrder)
== static_cast<int>(ColMajor))
? (i*rhsStride) :(i) ],
(static_cast<int>(RhsStorageOrder)
== static_cast<int>(ColMajor)) ? (1) : (rhsStride),
&res[i*resStride], 1, alpha);
}
}
};
#endif
}
}
#endif
stan::math::rows
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
Eigen::NumTraits< stan::math::var >::digits10
static int digits10()
Return the number of decimal digits that can be represented without change.
Definition: Eigen_NumTraits.hpp:79
core.hpp
Eigen::internal::significant_decimals_default_impl< stan::math::var, false >::run
static int run()
Definition: Eigen_NumTraits.hpp:264
Eigen::internal::general_matrix_vector_product< Index, stan::math::var, RowMajor, ConjugateLhs, stan::math::var, ConjugateRhs >::RhsScalar
stan::math::var RhsScalar
Definition: Eigen_NumTraits.hpp:328
std_numeric_limits.hpp
stan
Definition: log_sum_exp.hpp:8
Eigen::internal::general_matrix_matrix_product< Index, stan::math::var, LhsStorageOrder, ConjugateLhs, stan::math::var, RhsStorageOrder, ConjugateRhs, ColMajor >::ResScalar
scalar_product_traits< LhsScalar, RhsScalar >::ReturnType ResScalar
Definition: Eigen_NumTraits.hpp:360
Eigen::internal::general_matrix_vector_product< Index, stan::math::var, ColMajor, ConjugateLhs, stan::math::var, ConjugateRhs >::run
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)
Definition: Eigen_NumTraits.hpp:304
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
Eigen::NumTraits< stan::math::var >::dummy_precision
static stan::math::var dummy_precision()
Return the precision for stan::math::var delegates to precision for douboe.
Definition: Eigen_NumTraits.hpp:30
Eigen::internal::general_matrix_vector_product< Index, stan::math::var, ColMajor, ConjugateLhs, stan::math::var, ConjugateRhs >::LhsScalar
stan::math::var LhsScalar
Definition: Eigen_NumTraits.hpp:298
Eigen::NumTraits< stan::math::var >::NonInteger
stan::math::var NonInteger
Definition: Eigen_NumTraits.hpp:21
Eigen::internal::general_matrix_matrix_product< Index, stan::math::var, LhsStorageOrder, ConjugateLhs, stan::math::var, RhsStorageOrder, ConjugateRhs, ColMajor >::run
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 > *)
Definition: Eigen_NumTraits.hpp:361
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
Eigen::internal::general_matrix_matrix_product< Index, stan::math::var, LhsStorageOrder, ConjugateLhs, stan::math::var, RhsStorageOrder, ConjugateRhs, ColMajor >::RhsScalar
stan::math::var RhsScalar
Definition: Eigen_NumTraits.hpp:358
Eigen
(Expert) Numerical traits for algorithmic differentiation variables.
Definition: Eigen_NumTraits.hpp:9
Eigen::internal::scalar_product_traits< stan::math::var, double >::ReturnType
stan::math::var ReturnType
Definition: Eigen_NumTraits.hpp:279
stan::math::gevv_vvv_vari
Definition: gevv_vvv_vari.hpp:11
Eigen::internal::general_matrix_vector_product< Index, stan::math::var, RowMajor, ConjugateLhs, stan::math::var, ConjugateRhs >::LhsScalar
stan::math::var LhsScalar
Definition: Eigen_NumTraits.hpp:327
Eigen::NumTraits< stan::math::var >::Real
stan::math::var Real
Definition: Eigen_NumTraits.hpp:20
Eigen::internal::general_matrix_matrix_product< Index, stan::math::var, LhsStorageOrder, ConjugateLhs, stan::math::var, RhsStorageOrder, ConjugateRhs, ColMajor >::LhsScalar
stan::math::var LhsScalar
Definition: Eigen_NumTraits.hpp:357
stan::math::cols
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
Eigen.hpp
Eigen::internal::general_matrix_vector_product< Index, stan::math::var, ColMajor, ConjugateLhs, stan::math::var, ConjugateRhs >::ResScalar
scalar_product_traits< LhsScalar, RhsScalar >::ReturnType ResScalar
Definition: Eigen_NumTraits.hpp:301
Eigen::internal::general_matrix_vector_product< Index, stan::math::var, ColMajor, ConjugateLhs, stan::math::var, ConjugateRhs >::RhsScalar
stan::math::var RhsScalar
Definition: Eigen_NumTraits.hpp:299
Eigen::NumTraits< stan::math::var >::Nested
stan::math::var Nested
Definition: Eigen_NumTraits.hpp:22
Eigen::internal::general_matrix_vector_product< Index, stan::math::var, RowMajor, ConjugateLhs, stan::math::var, ConjugateRhs >::ResScalar
scalar_product_traits< LhsScalar, RhsScalar >::ReturnType ResScalar
Definition: Eigen_NumTraits.hpp:330
stan::math::ceil
fvar< T > ceil(const fvar< T > &x)
Definition: ceil.hpp:11
Eigen::internal::general_matrix_vector_product< Index, stan::math::var, RowMajor, ConjugateLhs, stan::math::var, ConjugateRhs >::run
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)
Definition: Eigen_NumTraits.hpp:334
Eigen::internal::scalar_product_traits< double, stan::math::var >::ReturnType
stan::math::var ReturnType
Definition: Eigen_NumTraits.hpp:288

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