Automatic Differentiation
 
Loading...
Searching...
No Matches
erf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_ERF_HPP
2#define STAN_MATH_REV_FUN_ERF_HPP
3
8#include <cmath>
9
10namespace stan {
11namespace math {
12
48inline var erf(const var& a) {
49 auto precomp_erf = TWO_OVER_SQRT_PI * std::exp(-a.val() * a.val());
50 return make_callback_var(erf(a.val()), [a, precomp_erf](auto& vi) mutable {
51 a.adj() += vi.adj() * precomp_erf;
52 });
53}
54
55template <typename T, require_matrix_t<T>* = nullptr>
56inline auto erf(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(erf(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...
fvar< T > erf(const fvar< T > &x)
Definition erf.hpp:15
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
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 ...