1 #ifndef STAN_MATH_PRIM_SCAL_FUN_UB_FREE_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_UB_FREE_HPP
3 
4 #include <stan/math/prim/scal/fun/identity_free.hpp>
5 #include <stan/math/prim/scal/err/check_less_or_equal.hpp>
6 #include <boost/math/tools/promotion.hpp>
7 #include <cmath>
8 #include <limits>
9 
10 namespace stan {
11 namespace math {
12 
13 /**
14  * Return the free scalar that corresponds to the specified
15  * upper-bounded value with respect to the specified upper bound.
16  *
17  * <p>The transform is the reverse of the
18  * <code>ub_constrain(T, double)</code> transform,
19  *
20  * <p>\f$f^{-1}(y) = \log -(y - U)\f$
21  *
22  * <p>where \f$U\f$ is the upper bound.
23  *
24  * If the upper bound is positive infinity, this function
25  * reduces to <code>identity_free(y)</code>.
26  *
27  * @tparam T type of scalar
28  * @tparam U type of upper bound
29  * @param y constrained scalar with specified upper bound
30  * @param ub upper bound
31  * @return unconstrained scalar with respect to upper bound
32  * @throw std::invalid_argument if constrained scalar is greater
33  *   than the upper bound.
34  */
35 template <typename T, typename U>
ub_free(const T & y,const U & ub)36 inline typename boost::math::tools::promote_args<T, U>::type ub_free(
37     const T& y, const U& ub) {
38   using std::log;
39   if (ub == std::numeric_limits<double>::infinity())
40     return identity_free(y);
41   check_less_or_equal("ub_free", "Upper bounded variable", y, ub);
42   return log(ub - y);
43 }
44 
45 }  // namespace math
46 }  // namespace stan
47 #endif
48