All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
operator_division.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_CORE_OPERATOR_DIVISION_HPP
#define STAN_MATH_REV_CORE_OPERATOR_DIVISION_HPP
#include <stan/math/rev/core/var.hpp>
#include <stan/math/rev/core/vv_vari.hpp>
#include <stan/math/rev/core/vd_vari.hpp>
#include <stan/math/rev/core/dv_vari.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <limits>
namespace stan {
namespace math {
namespace {
// (a/b)' = a' * (1 / b) - b' * (a / [b * b])
class divide_vv_vari : public op_vv_vari {
public:
divide_vv_vari(vari* avi, vari* bvi) :
op_vv_vari(avi->val_ / bvi->val_, avi, bvi) {
}
void chain() {
if (unlikely(boost::math::isnan(avi_->val_)
|| boost::math::isnan(bvi_->val_))) {
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
} else {
avi_->adj_ += adj_ / bvi_->val_;
bvi_->adj_ -= adj_ * avi_->val_ / (bvi_->val_ * bvi_->val_);
}
}
};
class divide_vd_vari : public op_vd_vari {
public:
divide_vd_vari(vari* avi, double b) :
op_vd_vari(avi->val_ / b, avi, b) {
}
void chain() {
if (unlikely(boost::math::isnan(avi_->val_)
|| boost::math::isnan(bd_)))
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
avi_->adj_ += adj_ / bd_;
}
};
class divide_dv_vari : public op_dv_vari {
public:
divide_dv_vari(double a, vari* bvi) :
op_dv_vari(a / bvi->val_, a, bvi) {
}
void chain() {
bvi_->adj_ -= adj_ * ad_ / (bvi_->val_ * bvi_->val_);
}
};
}
inline var operator/(const var& a, const var& b) {
return var(new divide_vv_vari(a.vi_, b.vi_));
}
inline var operator/(const var& a, const double b) {
if (b == 1.0)
return a;
return var(new divide_vd_vari(a.vi_, b));
}
inline var operator/(const double a, const var& b) {
return var(new divide_dv_vari(a, b.vi_));
}
}
}
#endif
dv_vari.hpp
stan::math::operator/
fvar< T > operator/(const fvar< T > &x1, const fvar< T > &x2)
Definition: operator_division.hpp:14
vd_vari.hpp
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
boost::math::isnan
bool isnan(const stan::math::var &v)
Checks if the given number is NaN.
Definition: boost_isnan.hpp:22
unlikely
#define unlikely(x)
Definition: likely.hpp:9
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:44
var.hpp
vv_vari.hpp

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