@@ -50,14 +50,17 @@ inline auto lb_constrain(const T& x, const L& lb) {
5050 *
5151 * @tparam T Scalar.
5252 * @tparam L Scalar.
53+ * @tparam Lp scalar, T and L should be convertible to this.
5354 * @param[in] x unconstrained input
5455 * @param[in] lb lower bound on output
5556 * @param[in,out] lp reference to log probability to increment
5657 * @return lower-bound constrained value corresponding to inputs
5758 */
58- template <typename T, typename L, require_all_stan_scalar_t <T, L>* = nullptr ,
59- require_all_not_st_var<T, L>* = nullptr >
60- inline auto lb_constrain (const T& x, const L& lb, return_type_t <T, L>& lp) {
59+ template <typename T, typename L, typename Lp,
60+ require_all_stan_scalar_t <T, L>* = nullptr ,
61+ require_all_not_st_var<T, L>* = nullptr ,
62+ require_convertible_t <return_type_t <T, L>, Lp>* = nullptr >
63+ inline auto lb_constrain (const T& x, const L& lb, Lp& lp) {
6164 if (value_of_rec (lb) == NEGATIVE_INFTY) {
6265 return identity_constrain (x, lb);
6366 } else {
@@ -89,15 +92,18 @@ inline auto lb_constrain(T&& x, L&& lb) {
8992 *
9093 * @tparam T A type inheriting from `EigenBase`.
9194 * @tparam L Scalar.
95+ * @tparam Lp scalar, T and L should be convertible to this.
9296 * @param[in] x unconstrained input
9397 * @param[in] lb lower bound on output
9498 * @param[in,out] lp reference to log probability to increment
9599 * @return lower-bound constrained value corresponding to inputs
96100 */
97- template <typename T, typename L, require_eigen_t <T>* = nullptr ,
101+ template <typename T, typename L, typename Lp, require_eigen_t <T>* = nullptr ,
98102 require_stan_scalar_t <L>* = nullptr ,
99- require_all_not_st_var<T, L>* = nullptr >
100- inline auto lb_constrain (const T& x, const L& lb, return_type_t <T, L>& lp) {
103+ require_all_not_st_var<T, L>* = nullptr ,
104+ require_convertible_t <return_type_t <T, L>, Lp>* = nullptr >
105+
106+ inline auto lb_constrain (const T& x, const L& lb, Lp& lp) {
101107 return eval (
102108 x.unaryExpr ([lb, &lp](auto && xx) { return lb_constrain (xx, lb, lp); }));
103109}
@@ -126,14 +132,17 @@ inline auto lb_constrain(T&& x, L&& lb) {
126132 *
127133 * @tparam T A type inheriting from `EigenBase`.
128134 * @tparam L A type inheriting from `EigenBase`.
135+ * @tparam Lp Scalar, the scalar type of T and L should be convertible to this.
129136 * @param[in] x unconstrained input
130137 * @param[in] lb lower bound on output
131138 * @param[in,out] lp reference to log probability to increment
132139 * @return lower-bound constrained value corresponding to inputs
133140 */
134- template <typename T, typename L, require_all_eigen_t <T, L>* = nullptr ,
135- require_all_not_st_var<T, L>* = nullptr >
136- inline auto lb_constrain (const T& x, const L& lb, return_type_t <T, L>& lp) {
141+ template <typename T, typename L, typename Lp,
142+ require_all_eigen_t <T, L>* = nullptr ,
143+ require_all_not_st_var<T, L>* = nullptr ,
144+ require_convertible_t <return_type_t <T, L>, Lp>* = nullptr >
145+ inline auto lb_constrain (const T& x, const L& lb, Lp& lp) {
137146 check_matching_dims (" lb_constrain" , " x" , x, " lb" , lb);
138147 return eval (x.binaryExpr (
139148 lb, [&lp](auto && xx, auto && lbb) { return lb_constrain (xx, lbb, lp); }));
@@ -164,14 +173,16 @@ inline auto lb_constrain(const std::vector<T>& x, const L& lb) {
164173 *
165174 * @tparam T A Any type with a Scalar `scalar_type`.
166175 * @tparam L A type inheriting from `EigenBase` or a standard vector.
176+ * @tparam Lp Scalar, T and the scalar type of L should be convertible to this.
167177 * @param[in] x unconstrained input
168178 * @param[in] lb lower bound on output
169179 * @param[in,out] lp reference to log probability to increment
170180 * @return lower-bound constrained value corresponding to inputs
171181 */
172- template <typename T, typename L, require_not_std_vector_t <L>* = nullptr >
173- inline auto lb_constrain (const std::vector<T>& x, const L& lb,
174- return_type_t <T, L>& lp) {
182+ template <typename T, typename L, typename Lp,
183+ require_not_std_vector_t <L>* = nullptr ,
184+ require_convertible_t <return_type_t <T, L>, Lp>* = nullptr >
185+ inline auto lb_constrain (const std::vector<T>& x, const L& lb, Lp& lp) {
175186 std::vector<plain_type_t <decltype (lb_constrain (x[0 ], lb))>> ret (x.size ());
176187 for (size_t i = 0 ; i < x.size (); ++i) {
177188 ret[i] = lb_constrain (x[i], lb, lp);
@@ -205,14 +216,16 @@ inline auto lb_constrain(const std::vector<T>& x, const std::vector<L>& lb) {
205216 *
206217 * @tparam T A Any type with a Scalar `scalar_type`.
207218 * @tparam L A type inheriting from `EigenBase` or a standard vector.
219+ * @tparam Lp Scalar, T and the scalar type of L should be convertible to this.
208220 * @param[in] x unconstrained input
209221 * @param[in] lb lower bound on output
210222 * @param[in,out] lp reference to log probability to increment
211223 * @return lower-bound constrained value corresponding to inputs
212224 */
213- template <typename T, typename L>
225+ template <typename T, typename L, typename Lp,
226+ require_convertible_t <return_type_t <T, L>, Lp>* = nullptr >
214227inline auto lb_constrain (const std::vector<T>& x, const std::vector<L>& lb,
215- return_type_t <T, L> & lp) {
228+ Lp & lp) {
216229 check_matching_dims (" lb_constrain" , " x" , x, " lb" , lb);
217230 std::vector<plain_type_t <decltype (lb_constrain (x[0 ], lb[0 ]))>> ret (x.size ());
218231 for (size_t i = 0 ; i < x.size (); ++i) {
@@ -234,13 +247,15 @@ inline auto lb_constrain(const std::vector<T>& x, const std::vector<L>& lb,
234247 * type inheriting from `Eigen::EigenBase`, a standard vector, or a scalar
235248 * @tparam L A type inheriting from `Eigen::EigenBase`, a `var_value` with inner
236249 * type inheriting from `Eigen::EigenBase`, a standard vector, or a scalar
250+ * @tparam Lp Scalar, the scalar type of T and L should be convertible to this.
237251 * @param[in] x unconstrained input
238252 * @param[in] lb lower bound on output
239253 * @param[in, out] lp log density accumulator
240254 * @return lower-bound constrained value corresponding to inputs
241255 */
242- template <bool Jacobian, typename T, typename L>
243- inline auto lb_constrain (const T& x, const L& lb, return_type_t <T, L>& lp) {
256+ template <bool Jacobian, typename T, typename L, typename Lp,
257+ require_convertible_t <return_type_t <T, L>, Lp>* = nullptr >
258+ inline auto lb_constrain (const T& x, const L& lb, Lp& lp) {
244259 if (Jacobian) {
245260 return lb_constrain (x, lb, lp);
246261 } else {
0 commit comments