log_falling_factorial.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_SCAL_FUN_LOG_FALLING_FACTORIAL_HPP
#define STAN_MATH_REV_SCAL_FUN_LOG_FALLING_FACTORIAL_HPP
#include <stan/math/rev/core.hpp>
#include <stan/math/prim/scal/fun/digamma.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/log_falling_factorial.hpp>
#include <limits>
namespace stan {
namespace math {
namespace {
class log_falling_factorial_vv_vari : public op_vv_vari {
public:
log_falling_factorial_vv_vari(vari* avi, vari* bvi) :
op_vv_vari(log_falling_factorial(avi->val_, bvi->val_),
avi, bvi) {
}
void chain() {
if (unlikely(is_nan(avi_->val_)
|| is_nan(bvi_->val_))) {
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
} else {
avi_->adj_ += adj_
* (digamma(avi_->val_ + 1)
- digamma(avi_->val_ - bvi_->val_ + 1));
bvi_->adj_ += adj_
* digamma(avi_->val_ - bvi_->val_ + 1);
}
}
};
class log_falling_factorial_vd_vari : public op_vd_vari {
public:
log_falling_factorial_vd_vari(vari* avi, double b) :
op_vd_vari(log_falling_factorial(avi->val_, b), avi, b) {
}
void chain() {
if (unlikely(is_nan(avi_->val_)
|| is_nan(bd_)))
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
avi_->adj_ += adj_
* (digamma(avi_->val_ + 1)
- digamma(avi_->val_ - bd_ + 1));
}
};
class log_falling_factorial_dv_vari : public op_dv_vari {
public:
log_falling_factorial_dv_vari(double a, vari* bvi) :
op_dv_vari(log_falling_factorial(a, bvi->val_), a, bvi) {
}
void chain() {
if (unlikely(is_nan(ad_)
|| is_nan(bvi_->val_)))
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
bvi_->adj_ += adj_
* digamma(ad_ - bvi_->val_ + 1);
}
};
}
inline var log_falling_factorial(const var& a,
double b) {
return var(new log_falling_factorial_vd_vari(a.vi_, b));
}
inline var log_falling_factorial(const var& a,
const var& b) {
return var(new log_falling_factorial_vv_vari(a.vi_, b.vi_));
}
inline var log_falling_factorial(double a,
const var& b) {
return var(new log_falling_factorial_dv_vari(a, b.vi_));
}
}
}
#endif
core.hpp
stan::math::log_falling_factorial
fvar< T > log_falling_factorial(const fvar< T > &x, const fvar< T > &n)
Definition: log_falling_factorial.hpp:14
is_nan.hpp
stan
Definition: log_sum_exp.hpp:8
digamma.hpp
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
unlikely
#define unlikely(x)
Definition: likely.hpp:9
stan::math::var::vi_
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:42
stan::math::is_nan
int is_nan(const fvar< T > &x)
Returns 1 if the input&#39;s value is NaN and 0 otherwise.
Definition: is_nan.hpp:21
log_falling_factorial.hpp
stan::math::digamma
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition: digamma.hpp:22

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