Automatic Differentiation
 
Loading...
Searching...
No Matches
erfcx.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_ERFCX_HPP
2#define STAN_MATH_REV_FUN_ERFCX_HPP
3
10#include <cmath>
11
12namespace stan {
13namespace math {
14
54template <
55 typename T, require_var_t<T>* = nullptr,
56 require_all_not_nonscalar_prim_or_rev_kernel_expression_t<T>* = nullptr>
57inline auto erfcx(T&& a) {
58 auto val = to_arena(erfcx(a.val()));
59 return make_callback_var(val, [a, val](auto& vi) mutable {
60 // apply_scalar_binary returns a lazy binaryExpr for Eigen inputs, so
61 // this allocates nothing, and calls the functor directly for scalars.
62 auto deriv = apply_scalar_binary(
63 [](double x, double v) { return internal::erfcx_derivative(x, v); },
64 a.val(), val);
65 as_array_or_scalar(a.adj())
66 += as_array_or_scalar(vi.adj()) * as_array_or_scalar(deriv);
67 });
68}
69
70} // namespace math
71} // namespace stan
72#endif
T erfcx_derivative(const T &x, const T &value)
Derivative of erfcx, 2 * x * erfcx(x) - 2 / sqrt(pi).
Definition erfcx.hpp:128
T as_array_or_scalar(T &&v)
Returns specified input value.
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...
auto apply_scalar_binary(F &&f, T1 &&x, T2 &&y)
Base template function for vectorization of binary scalar functions defined by applying a functor to ...
fvar< T > erfcx(const fvar< T > &x)
Return the scaled complementary error function of the argument.
Definition erfcx.hpp:26
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
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...