All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
OperandsAndPartials.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_SCAL_META_OPERANDSANDPARTIALS_HPP
#define STAN_MATH_PRIM_SCAL_META_OPERANDSANDPARTIALS_HPP
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/scal/meta/is_fvar.hpp>
#include <stan/math/fwd/scal/meta/partials_type.hpp>
#include <stan/math/prim/scal/meta/is_vector.hpp>
#include <stan/math/prim/mat/meta/is_vector.hpp>
#include <stan/math/prim/arr/meta/is_vector.hpp>
#include <stan/math/prim/arr/meta/length.hpp>
#include <stan/math/prim/mat/meta/length.hpp>
#include <stan/math/prim/scal/meta/length.hpp>
#include <stan/math/prim/mat/meta/is_vector_like.hpp>
#include <stan/math/prim/scal/meta/is_vector_like.hpp>
#include <stan/math/prim/scal/meta/VectorView.hpp>
#include <stan/math/prim/scal/meta/is_constant_struct.hpp>
#include <stan/math/prim/scal/meta/contains_fvar.hpp>
#include <stan/math/prim/scal/meta/partials_return_type.hpp>
#include <stan/math/prim/scal/meta/partials_type.hpp>
#include <stan/math/prim/scal/meta/return_type.hpp>
#include <stan/math/rev/core.hpp>
#include <stan/math/rev/scal/meta/is_var.hpp>
#include <stan/math/rev/scal/meta/partials_type.hpp>
namespace stan {
namespace math {
class partials_vari : public vari {
private:
const size_t N_;
vari** operands_;
double* partials_;
public:
partials_vari(double value,
size_t N,
vari** operands, double* partials)
: vari(value),
N_(N),
operands_(operands),
partials_(partials) { }
void chain() {
for (size_t n = 0; n < N_; ++n)
operands_[n]->adj_ += adj_ * partials_[n];
}
};
namespace {
template<typename T1, typename T2, typename T3,
bool is_vec = is_vector<T2>::value,
bool is_const = is_constant_struct<T2>::value>
struct incr_deriv {
inline T3 incr(T1 d_x, const T2& x_d ) {
return 0;
}
};
template<typename T1, typename T2, typename T3>
struct incr_deriv<T1, T2, T3, false, false> {
inline T3 incr(T1 d_x, const T2& x_d) {
return d_x[0]*x_d.d_;
}
};
template<typename T1, typename T2, typename T3>
struct incr_deriv<T1, T2, T3, true, false> {
inline T3 incr(T1 d_x, const T2& x_d) {
T3 temp = 0;
for (size_t n = 0; n < length(x_d); n++)
temp += d_x[n] * x_d[n].d_;
return temp;
}
};
template<typename T_return_type, typename T_partials_return,
typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6,
bool is_fvar = stan::contains_fvar<T_return_type>::value,
bool is_const = stan::is_constant_struct<T_return_type>::value>
struct partials_to_var {
inline
T_return_type to_var(double logp, size_t /* nvaris */,
vari** /* all_varis */,
T_partials_return* /* all_partials */,
const T1& x1, const T2& x2, const T3& x3,
const T4& x4, const T5& x5, const T6& x6,
VectorView<T_partials_return,
is_vector<T1>::value,
is_constant_struct<T1>::value> d_x1,
VectorView<T_partials_return,
is_vector<T2>::value,
is_constant_struct<T2>::value> d_x2,
VectorView<T_partials_return,
is_vector<T3>::value,
is_constant_struct<T3>::value> d_x3,
VectorView<T_partials_return,
is_vector<T4>::value,
is_constant_struct<T4>::value> d_x4,
VectorView<T_partials_return,
is_vector<T5>::value,
is_constant_struct<T5>::value> d_x5,
VectorView<T_partials_return,
is_vector<T6>::value,
is_constant_struct<T6>::value> d_x6) {
return logp;
}
};
template<typename T_return_type, typename T_partials_return,
typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6>
struct partials_to_var<T_return_type, T_partials_return,
T1, T2, T3, T4, T5, T6,
false, false> {
inline T_return_type to_var(T_partials_return logp, size_t nvaris,
vari** all_varis,
T_partials_return* all_partials,
const T1& x1, const T2& x2, const T3& x3,
const T4& x4, const T5& x5, const T6& x6,
VectorView<T_partials_return,
is_vector<T1>::value,
is_constant_struct<T1>::value> d_x1,
VectorView<T_partials_return,
is_vector<T2>::value,
is_constant_struct<T2>::value> d_x2,
VectorView<T_partials_return,
is_vector<T3>::value,
is_constant_struct<T3>::value> d_x3,
VectorView<T_partials_return,
is_vector<T4>::value,
is_constant_struct<T4>::value> d_x4,
VectorView<T_partials_return,
is_vector<T5>::value,
is_constant_struct<T5>::value> d_x5,
VectorView<T_partials_return,
is_vector<T6>::value,
is_constant_struct<T6>::value> d_x6) {
return var(new partials_vari(logp, nvaris, all_varis,
all_partials));
}
};
template<typename T_return_type, typename T_partials_return,
typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6>
struct partials_to_var<T_return_type, T_partials_return,
T1, T2, T3, T4, T5, T6,
true, false> {
inline T_return_type to_var(T_partials_return logp, size_t nvaris,
vari** all_varis,
T_partials_return* all_partials,
const T1& x1, const T2& x2, const T3& x3,
const T4& x4, const T5& x5, const T6& x6,
VectorView<T_partials_return,
is_vector<T1>::value,
is_constant_struct<T1>::value> d_x1,
VectorView<T_partials_return,
is_vector<T2>::value,
is_constant_struct<T2>::value> d_x2,
VectorView<T_partials_return,
is_vector<T3>::value,
is_constant_struct<T3>::value> d_x3,
VectorView<T_partials_return,
is_vector<T4>::value,
is_constant_struct<T4>::value> d_x4,
VectorView<T_partials_return,
is_vector<T5>::value,
is_constant_struct<T5>::value> d_x5,
VectorView<T_partials_return,
is_vector<T6>::value,
is_constant_struct<T6>::value> d_x6) {
T_partials_return temp_deriv = 0;
temp_deriv += incr_deriv<VectorView<T_partials_return,
is_vector<T1>::value,
is_constant_struct<T1>::value>,
T1, T_partials_return>().incr(d_x1, x1);
temp_deriv += incr_deriv<VectorView<T_partials_return,
is_vector<T2>::value,
is_constant_struct<T2>::value>,
T2, T_partials_return>().incr(d_x2, x2);
temp_deriv += incr_deriv<VectorView<T_partials_return,
is_vector<T3>::value,
is_constant_struct<T3>::value>,
T3, T_partials_return>().incr(d_x3, x3);
temp_deriv += incr_deriv<VectorView<T_partials_return,
is_vector<T4>::value,
is_constant_struct<T4>::value>,
T4, T_partials_return>().incr(d_x4, x4);
temp_deriv += incr_deriv<VectorView<T_partials_return,
is_vector<T5>::value,
is_constant_struct<T5>::value>,
T5, T_partials_return>().incr(d_x5, x5);
temp_deriv += incr_deriv<VectorView<T_partials_return,
is_vector<T6>::value,
is_constant_struct<T6>::value>,
T6, T_partials_return>().incr(d_x6, x6);
return stan::math::fvar<T_partials_return>(logp, temp_deriv);
}
};
template<typename T,
bool is_vec = is_vector<T>::value,
bool is_const = is_constant_struct<T>::value,
bool contain_fvar = contains_fvar<T>::value>
struct set_varis {
inline size_t set(vari** /*varis*/, const T& /*x*/) {
return 0U;
}
};
template<typename T>
struct set_varis<T, true, false, false> {
inline size_t set(vari** varis, const T& x) {
for (size_t n = 0; n < length(x); n++)
varis[n] = x[n].vi_;
return length(x);
}
};
template<typename T>
struct set_varis<T, true, false, true> {
inline size_t set(vari** varis, const T& x) {
for (size_t n = 0; n < length(x); n++)
varis[n] = 0;
return length(x);
}
};
template<>
struct set_varis<var, false, false, false> {
inline size_t set(vari** varis, const var& x) {
varis[0] = x.vi_;
return (1);
}
};
}
template<typename T1 = double, typename T2 = double, typename T3 = double,
typename T4 = double, typename T5 = double, typename T6 = double>
struct OperandsAndPartials {
typedef
typename stan::partials_return_type<T1, T2, T3, T4, T5, T6>::type
T_partials_return;
typedef
typename stan::return_type<T1, T2, T3, T4, T5, T6>::type T_return_type;
static const bool all_constant = is_constant<T_return_type>::value;
size_t nvaris;
vari** all_varis;
T_partials_return* all_partials;
VectorView<T_partials_return,
is_vector<T1>::value,
is_constant_struct<T1>::value> d_x1;
VectorView<T_partials_return,
is_vector<T2>::value,
is_constant_struct<T2>::value> d_x2;
VectorView<T_partials_return,
is_vector<T3>::value,
is_constant_struct<T3>::value> d_x3;
VectorView<T_partials_return,
is_vector<T4>::value,
is_constant_struct<T4>::value> d_x4;
VectorView<T_partials_return,
is_vector<T5>::value,
is_constant_struct<T5>::value> d_x5;
VectorView<T_partials_return,
is_vector<T6>::value,
is_constant_struct<T6>::value> d_x6;
OperandsAndPartials(const T1& x1 = 0, const T2& x2 = 0, const T3& x3 = 0,
const T4& x4 = 0, const T5& x5 = 0, const T6& x6 = 0)
: nvaris(!is_constant_struct<T1>::value * length(x1) +
!is_constant_struct<T2>::value * length(x2) +
!is_constant_struct<T3>::value * length(x3) +
!is_constant_struct<T4>::value * length(x4) +
!is_constant_struct<T5>::value * length(x5) +
!is_constant_struct<T6>::value * length(x6)),
all_varis(static_cast<vari**>
(chainable::operator new
(sizeof(vari*) * nvaris))),
all_partials(static_cast<T_partials_return*>
(chainable::operator new
(sizeof(T_partials_return) * nvaris))),
d_x1(all_partials),
d_x2(all_partials
+ (!is_constant_struct<T1>::value) * length(x1)),
d_x3(all_partials
+ (!is_constant_struct<T1>::value) * length(x1)
+ (!is_constant_struct<T2>::value) * length(x2)),
d_x4(all_partials
+ (!is_constant_struct<T1>::value) * length(x1)
+ (!is_constant_struct<T2>::value) * length(x2)
+ (!is_constant_struct<T3>::value) * length(x3)),
d_x5(all_partials
+ (!is_constant_struct<T1>::value) * length(x1)
+ (!is_constant_struct<T2>::value) * length(x2)
+ (!is_constant_struct<T3>::value) * length(x3)
+ (!is_constant_struct<T4>::value) * length(x4)),
d_x6(all_partials
+ (!is_constant_struct<T1>::value) * length(x1)
+ (!is_constant_struct<T2>::value) * length(x2)
+ (!is_constant_struct<T3>::value) * length(x3)
+ (!is_constant_struct<T4>::value) * length(x4)
+ (!is_constant_struct<T5>::value) * length(x5)) {
size_t base = 0;
if (!is_constant_struct<T1>::value)
base += set_varis<T1>().set(&all_varis[base], x1);
if (!is_constant_struct<T2>::value)
base += set_varis<T2>().set(&all_varis[base], x2);
if (!is_constant_struct<T3>::value)
base += set_varis<T3>().set(&all_varis[base], x3);
if (!is_constant_struct<T4>::value)
base += set_varis<T4>().set(&all_varis[base], x4);
if (!is_constant_struct<T5>::value)
base += set_varis<T5>().set(&all_varis[base], x5);
if (!is_constant_struct<T6>::value)
set_varis<T6>().set(&all_varis[base], x6);
std::fill(all_partials, all_partials+nvaris, 0);
}
T_return_type
to_var(T_partials_return logp,
const T1& x1 = 0, const T2& x2 = 0, const T3& x3 = 0,
const T4& x4 = 0, const T5& x5 = 0, const T6& x6 = 0) {
return partials_to_var
<T_return_type, T_partials_return, T1,
T2, T3, T4, T5, T6>().to_var(logp, nvaris, all_varis,
all_partials,
x1, x2, x3, x4, x5, x6, d_x1, d_x2,
d_x3, d_x4, d_x5, d_x6);
}
};
}
}
#endif
stan::math::OperandsAndPartials::d_x6
VectorView< T_partials_return, is_vector< T6 >::value, is_constant_struct< T6 >::value > d_x6
Definition: OperandsAndPartials.hpp:268
stan::math::OperandsAndPartials::T_return_type
stan::return_type< T1, T2, T3, T4, T5, T6 >::type T_return_type
Definition: OperandsAndPartials.hpp:244
stan::is_constant
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Definition: is_constant.hpp:22
stan::math::OperandsAndPartials::all_partials
T_partials_return * all_partials
Definition: OperandsAndPartials.hpp:249
core.hpp
core.hpp
stan::math::chainable
Abstract base class for variable implementations that handles memory management and applying the chai...
Definition: chainable.hpp:14
is_vector.hpp
is_vector_like.hpp
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:28
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
partials_type.hpp
stan::math::partials_vari::chain
void chain()
Apply the chain rule to this variable based on the variables on which it depends. ...
Definition: OperandsAndPartials.hpp:41
length.hpp
is_vector.hpp
stan::math::OperandsAndPartials::T_partials_return
stan::partials_return_type< T1, T2, T3, T4, T5, T6 >::type T_partials_return
Definition: OperandsAndPartials.hpp:241
stan::math::OperandsAndPartials::to_var
T_return_type to_var(T_partials_return logp, const T1 &x1=0, const T2 &x2=0, const T3 &x3=0, const T4 &x4=0, const T5 &x5=0, const T6 &x6=0)
Definition: OperandsAndPartials.hpp:322
return_type.hpp
stan::is_vector
Definition: is_vector.hpp:10
partials_type.hpp
stan::math::to_var
var to_var(const double &x)
Converts argument to an automatic differentiation variable.
Definition: to_var.hpp:21
stan::return_type::type
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Definition: return_type.hpp:27
VectorView.hpp
stan::math::OperandsAndPartials::d_x1
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Definition: OperandsAndPartials.hpp:253
stan::is_constant_struct
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
Definition: is_constant_struct.hpp:16
is_fvar.hpp
is_vector_like.hpp
stan::math::OperandsAndPartials::d_x3
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
Definition: OperandsAndPartials.hpp:259
stan::math::OperandsAndPartials::d_x4
VectorView< T_partials_return, is_vector< T4 >::value, is_constant_struct< T4 >::value > d_x4
Definition: OperandsAndPartials.hpp:262
stan::math::fill
void fill(T &x, const S &y)
Fill the specified container with the specified value.
Definition: fill.hpp:22
stan::math::OperandsAndPartials
A variable implementation that stores operands and derivatives with respect to the variable...
Definition: OperandsAndPartials.hpp:238
stan::math::partials_vari
Definition: OperandsAndPartials.hpp:28
stan::math::partials_vari::partials_vari
partials_vari(double value, size_t N, vari **operands, double *partials)
Definition: OperandsAndPartials.hpp:34
partials_return_type.hpp
length.hpp
partials_type.hpp
stan::math::OperandsAndPartials::d_x2
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
Definition: OperandsAndPartials.hpp:256
is_var.hpp
is_constant_struct.hpp
stan::math::OperandsAndPartials::OperandsAndPartials
OperandsAndPartials(const T1 &x1=0, const T2 &x2=0, const T3 &x3=0, const T4 &x4=0, const T5 &x5=0, const T6 &x6=0)
Definition: OperandsAndPartials.hpp:270
stan::math::vari::adj_
double adj_
The adjoint of this variable, which is the partial derivative of this variable with respect to the ro...
Definition: vari.hpp:42
stan::math::OperandsAndPartials::nvaris
size_t nvaris
Definition: OperandsAndPartials.hpp:247
stan::math::OperandsAndPartials::d_x5
VectorView< T_partials_return, is_vector< T5 >::value, is_constant_struct< T5 >::value > d_x5
Definition: OperandsAndPartials.hpp:265
stan::contains_fvar
Metaprogram to calculate the base scalar return type resulting from promoting all the scalar types of...
Definition: contains_fvar.hpp:19
stan::math::OperandsAndPartials::all_constant
static const bool all_constant
Definition: OperandsAndPartials.hpp:246
is_vector.hpp
stan::VectorView
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
stan::math::OperandsAndPartials::all_varis
vari ** all_varis
Definition: OperandsAndPartials.hpp:248
stan::partials_return_type::type
boost::math::tools::promote_args< typename partials_type< typename scalar_type< T1 >::type >::type, typename partials_type< typename scalar_type< T2 >::type >::type, typename partials_type< typename scalar_type< T3 >::type >::type, typename partials_type< typename scalar_type< T4 >::type >::type, typename partials_type< typename scalar_type< T5 >::type >::type, typename partials_type< typename scalar_type< T6 >::type >::type >::type type
Definition: partials_return_type.hpp:26
contains_fvar.hpp
stan::math::fvar
Definition: fvar.hpp:13
length.hpp

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