Automatic Differentiation
 
Loading...
Searching...
No Matches
offset_multiplier_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_OFFSET_MULTIPLIER_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_OFFSET_MULTIPLIER_CONSTRAIN_HPP
3
12#include <cmath>
13
14namespace stan {
15namespace math {
16
40template <typename T, typename M, typename S,
42 T, M, S>* = nullptr>
43inline auto offset_multiplier_constrain(const T& x, const M& mu,
44 const S& sigma) {
45 const auto& mu_ref = to_ref(mu);
46 const auto& sigma_ref = to_ref(sigma);
48 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
49 }
51 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
52 } else if constexpr (is_matrix<M>::value && is_matrix<S>::value) {
53 check_matching_dims("offset_multiplier_constrain", "mu", mu, "sigma",
54 sigma);
55 }
56
57 check_finite("offset_multiplier_constrain", "offset", value_of_rec(mu_ref));
58 check_positive_finite("offset_multiplier_constrain", "multiplier",
59 value_of_rec(sigma_ref));
60 return stan::math::eval(fma(sigma_ref, x, mu_ref));
61}
62
90template <typename T, typename M, typename S, typename Lp,
93 T, M, S>* = nullptr>
94inline auto offset_multiplier_constrain(const T& x, const M& mu, const S& sigma,
95 Lp& lp) {
96 const auto& mu_ref = to_ref(mu);
97 const auto& sigma_ref = to_ref(sigma);
99 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
100 }
101 if constexpr (is_matrix<T>::value && is_matrix<S>::value) {
102 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
103 } else if constexpr (is_matrix<M>::value && is_matrix<S>::value) {
104 check_matching_dims("offset_multiplier_constrain", "mu", mu, "sigma",
105 sigma);
106 }
107
108 check_finite("offset_multiplier_constrain", "offset", value_of_rec(mu_ref));
109 check_positive_finite("offset_multiplier_constrain", "multiplier",
110 value_of_rec(sigma_ref));
111 if (math::size(sigma_ref) == 1) {
112 lp += sum(lmultiply(math::size(x), sigma_ref));
113 } else {
114 lp += sum(log(sigma_ref));
115 }
116 return stan::math::eval(fma(sigma_ref, x, mu_ref));
117}
118
122template <typename T, typename M, typename S,
124inline auto offset_multiplier_constrain(const std::vector<T>& x, const M& mu,
125 const S& sigma) {
126 std::vector<
127 plain_type_t<decltype(offset_multiplier_constrain(x[0], mu, sigma))>>
128 ret;
129 ret.reserve(x.size());
130 const auto& mu_ref = to_ref(mu);
131 const auto& sigma_ref = to_ref(sigma);
132 for (size_t i = 0; i < x.size(); ++i) {
133 ret.emplace_back(offset_multiplier_constrain(x[i], mu_ref, sigma_ref));
134 }
135 return ret;
136}
137
141template <typename T, typename M, typename S, typename Lp,
144inline auto offset_multiplier_constrain(const std::vector<T>& x, const M& mu,
145 const S& sigma, Lp& lp) {
146 std::vector<
147 plain_type_t<decltype(offset_multiplier_constrain(x[0], mu, sigma, lp))>>
148 ret;
149 ret.reserve(x.size());
150 const auto& mu_ref = to_ref(mu);
151 const auto& sigma_ref = to_ref(sigma);
152 for (size_t i = 0; i < x.size(); ++i) {
153 ret.emplace_back(offset_multiplier_constrain(x[i], mu_ref, sigma_ref, lp));
154 }
155 return ret;
156}
157
161template <typename T, typename M, typename S,
163inline auto offset_multiplier_constrain(const std::vector<T>& x, const M& mu,
164 const std::vector<S>& sigma) {
165 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
166 std::vector<
167 plain_type_t<decltype(offset_multiplier_constrain(x[0], mu, sigma[0]))>>
168 ret;
169 ret.reserve(x.size());
170 const auto& mu_ref = to_ref(mu);
171 for (size_t i = 0; i < x.size(); ++i) {
172 ret.emplace_back(offset_multiplier_constrain(x[i], mu_ref, sigma[i]));
173 }
174 return ret;
175}
176
180template <typename T, typename M, typename S, typename Lp,
183inline auto offset_multiplier_constrain(const std::vector<T>& x, const M& mu,
184 const std::vector<S>& sigma, Lp& lp) {
185 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
186 std::vector<plain_type_t<decltype(
187 offset_multiplier_constrain(x[0], mu, sigma[0], lp))>>
188 ret;
189 ret.reserve(x.size());
190 const auto& mu_ref = to_ref(mu);
191 for (size_t i = 0; i < x.size(); ++i) {
192 ret.emplace_back(offset_multiplier_constrain(x[i], mu_ref, sigma[i], lp));
193 }
194 return ret;
195}
196
200template <typename T, typename M, typename S,
202inline auto offset_multiplier_constrain(const std::vector<T>& x,
203 const std::vector<M>& mu,
204 const S& sigma) {
205 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
206 std::vector<
207 plain_type_t<decltype(offset_multiplier_constrain(x[0], mu[0], sigma))>>
208 ret;
209 ret.reserve(x.size());
210 const auto& sigma_ref = to_ref(sigma);
211 for (size_t i = 0; i < x.size(); ++i) {
212 ret.emplace_back(offset_multiplier_constrain(x[i], mu[i], sigma_ref));
213 }
214 return ret;
215}
216
220template <typename T, typename M, typename S, typename Lp,
223inline auto offset_multiplier_constrain(const std::vector<T>& x,
224 const std::vector<M>& mu,
225 const S& sigma, Lp& lp) {
226 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
227 std::vector<plain_type_t<decltype(
228 offset_multiplier_constrain(x[0], mu[0], sigma, lp))>>
229 ret;
230 ret.reserve(x.size());
231 const auto& sigma_ref = to_ref(sigma);
232 for (size_t i = 0; i < x.size(); ++i) {
233 ret.emplace_back(offset_multiplier_constrain(x[i], mu[i], sigma_ref, lp));
234 }
235 return ret;
236}
237
241template <typename T, typename M, typename S>
242inline auto offset_multiplier_constrain(const std::vector<T>& x,
243 const std::vector<M>& mu,
244 const std::vector<S>& sigma) {
245 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
246 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
247 std::vector<plain_type_t<decltype(
248 offset_multiplier_constrain(x[0], mu[0], sigma[0]))>>
249 ret;
250 ret.reserve(x.size());
251 for (size_t i = 0; i < x.size(); ++i) {
252 ret.emplace_back(offset_multiplier_constrain(x[i], mu[i], sigma[i]));
253 }
254 return ret;
255}
256
260template <typename T, typename M, typename S, typename Lp,
262inline auto offset_multiplier_constrain(const std::vector<T>& x,
263 const std::vector<M>& mu,
264 const std::vector<S>& sigma, Lp& lp) {
265 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
266 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
267 std::vector<plain_type_t<decltype(
268 offset_multiplier_constrain(x[0], mu[0], sigma[0], lp))>>
269 ret;
270 ret.reserve(x.size());
271 for (size_t i = 0; i < x.size(); ++i) {
272 ret.emplace_back(offset_multiplier_constrain(x[i], mu[i], sigma[i], lp));
273 }
274 return ret;
275}
276
302template <bool Jacobian, typename T, typename M, typename S, typename Lp,
304inline auto offset_multiplier_constrain(T&& x, M&& mu, S&& sigma, Lp& lp) {
305 if constexpr (Jacobian) {
306 return offset_multiplier_constrain(std::forward<T>(x), std::forward<M>(mu),
307 std::forward<S>(sigma), lp);
308 } else {
309 return offset_multiplier_constrain(std::forward<T>(x), std::forward<M>(mu),
310 std::forward<S>(sigma));
311 }
312}
313
314} // namespace math
315} // namespace stan
316
317#endif
require_t< std::is_convertible< std::decay_t< T >, std::decay_t< S > > > require_convertible_t
Require types T and S satisfies std::is_convertible.
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
require_not_t< is_std_vector< std::decay_t< T > > > require_not_std_vector_t
Require type does not satisfy is_std_vector.
require_all_not_t< is_std_vector< std::decay_t< Types > >... > require_all_not_std_vector_t
Require none of the types satisfy is_std_vector.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
fvar< T > lmultiply(const fvar< T > &x1, const fvar< T > &x2)
Definition lmultiply.hpp:14
void check_matching_dims(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the two containers have the same dimensions.
auto offset_multiplier_constrain(const T &x, const M &mu, const S &sigma)
Return the linearly transformed value for the specified unconstrained input and specified offset and ...
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< return_type_t< T1, T2, T3 > > fma(const fvar< T1 > &x1, const fvar< T2 > &x2, const fvar< T3 > &x3)
The fused multiply-add operation (C99).
Definition fma.hpp:60
typename plain_type< std::decay_t< T > >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Check if a type is derived from Eigen::EigenBase or is a var_value whose value_type is derived from E...
Definition is_matrix.hpp:18