Automatic Differentiation
 
Loading...
Searching...
No Matches
erfc.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_ERFC_HPP
2#define STAN_MATH_REV_FUN_ERFC_HPP
3
8#include <cmath>
9
10namespace stan {
11namespace math {
12
48inline var erfc(const var& a) {
49 auto precomp_erfc = TWO_OVER_SQRT_PI * std::exp(-a.val() * a.val());
50 return make_callback_var(erfc(a.val()), [a, precomp_erfc](auto& vi) mutable {
51 a.adj() -= vi.adj() * precomp_erfc;
52 });
53}
54
55template <typename T, require_matrix_t<T>* = nullptr>
56inline auto erfc(const var_value<T>& a) {
57 auto precomp_erf
58 = to_arena(TWO_OVER_SQRT_PI * (-a.val().array().square()).exp());
59 return make_callback_var(erfc(a.val()), [a, precomp_erf](auto& vi) mutable {
60 a.adj().array() -= vi.adj().array() * precomp_erf;
61 });
62}
63
64} // namespace math
65} // namespace stan
66#endif
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
Definition to_arena.hpp:25
fvar< T > erfc(const fvar< T > &x)
Definition erfc.hpp:16
static constexpr double TWO_OVER_SQRT_PI
The value of 2 over the square root of , .
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...