Stan Math Library  2.20.0
reverse mode automatic differentiation
locscale_constrain.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_FUN_LOCSCALE_CONSTRAIN_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_LOCSCALE_CONSTRAIN_HPP
3 
5 #include <boost/math/tools/promotion.hpp>
9 #include <cmath>
10 #include <limits>
11 
12 namespace stan {
13 namespace math {
14 
38 template <typename T, typename M, typename S>
39 inline typename boost::math::tools::promote_args<T, M, S>::type
40 locscale_constrain(const T& x, const M& mu, const S& sigma) {
41  check_finite("locscale_constrain", "location", mu);
42  if (sigma == 1) {
43  if (mu == 0)
44  return identity_constrain(x);
45  return mu + x;
46  }
47  check_positive_finite("locscale_constrain", "scale", sigma);
48  return fma(sigma, x, mu);
49 }
50 
77 template <typename T, typename M, typename S>
78 inline typename boost::math::tools::promote_args<T, M, S>::type
79 locscale_constrain(const T& x, const M& mu, const S& sigma, T& lp) {
80  using std::log;
81  check_finite("locscale_constrain", "location", mu);
82  if (sigma == 1) {
83  if (mu == 0)
84  return identity_constrain(x);
85  return mu + x;
86  }
87  check_positive_finite("locscale_constrain", "scale", sigma);
88  lp += multiply_log(size_of(x), sigma);
89  return fma(sigma, x, mu);
90 }
91 
92 } // namespace math
93 
94 } // namespace stan
95 
96 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
boost::math::tools::promote_args< T, M, S >::type locscale_constrain(const T &x, const M &mu, const S &sigma)
Return the linearly transformed value for the specified unconstrained input and specified location an...
T identity_constrain(const T &x)
Returns the result of applying the identity constraint transform to the input.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< typename stan::return_type< T1, T2, T3 >::type > fma(const fvar< T1 > &x1, const fvar< T2 > &x2, const fvar< T3 > &x3)
The fused multiply-add operation (C99).
Definition: fma.hpp:59
size_t size_of(const T &x)
Returns the size of the provided vector or the constant 1 if the input argument is not a vector...
Definition: size_of.hpp:27
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)

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