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
13#include <cmath>
14
15namespace stan {
16namespace math {
17
41template <typename T, typename M, typename S,
43 T, M, S>* = nullptr>
44inline auto offset_multiplier_constrain(const T& x, const M& mu,
45 const S& sigma) {
46 const auto& mu_ref = to_ref(mu);
47 const auto& sigma_ref = to_ref(sigma);
49 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
50 }
52 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
54 check_matching_dims("offset_multiplier_constrain", "mu", mu, "sigma",
55 sigma);
56 }
57
58 check_finite("offset_multiplier_constrain", "offset", value_of_rec(mu_ref));
59 check_positive_finite("offset_multiplier_constrain", "multiplier",
60 value_of_rec(sigma_ref));
61 return fma(sigma_ref, x, mu_ref);
62}
63
90template <typename T, typename M, typename S,
92 T, M, S>* = nullptr>
93inline auto offset_multiplier_constrain(const T& x, const M& mu, const S& sigma,
95 const auto& mu_ref = to_ref(mu);
96 const auto& sigma_ref = to_ref(sigma);
98 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
99 }
101 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
103 check_matching_dims("offset_multiplier_constrain", "mu", mu, "sigma",
104 sigma);
105 }
106
107 check_finite("offset_multiplier_constrain", "offset", value_of_rec(mu_ref));
108 check_positive_finite("offset_multiplier_constrain", "multiplier",
109 value_of_rec(sigma_ref));
110 if (math::size(sigma_ref) == 1) {
111 lp += sum(multiply_log(math::size(x), sigma_ref));
112 } else {
113 lp += sum(log(sigma_ref));
114 }
115 return fma(sigma_ref, x, mu_ref);
116}
117
121template <typename T, typename M, typename S,
123inline auto offset_multiplier_constrain(const std::vector<T>& x, const M& mu,
124 const S& sigma) {
125 std::vector<
126 plain_type_t<decltype(offset_multiplier_constrain(x[0], mu, sigma))>>
127 ret;
128 ret.reserve(x.size());
129 const auto& mu_ref = to_ref(mu);
130 const auto& sigma_ref = to_ref(sigma);
131 for (size_t i = 0; i < x.size(); ++i) {
132 ret.emplace_back(offset_multiplier_constrain(x[i], mu_ref, sigma_ref));
133 }
134 return ret;
135}
136
140template <typename T, typename M, typename S,
142inline auto offset_multiplier_constrain(const std::vector<T>& x, const M& mu,
143 const S& sigma,
145 std::vector<
146 plain_type_t<decltype(offset_multiplier_constrain(x[0], mu, sigma, lp))>>
147 ret;
148 ret.reserve(x.size());
149 const auto& mu_ref = to_ref(mu);
150 const auto& sigma_ref = to_ref(sigma);
151 for (size_t i = 0; i < x.size(); ++i) {
152 ret.emplace_back(offset_multiplier_constrain(x[i], mu_ref, sigma_ref, lp));
153 }
154 return ret;
155}
156
160template <typename T, typename M, typename S,
162inline auto offset_multiplier_constrain(const std::vector<T>& x, const M& mu,
163 const std::vector<S>& sigma) {
164 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
165 std::vector<
166 plain_type_t<decltype(offset_multiplier_constrain(x[0], mu, sigma[0]))>>
167 ret;
168 ret.reserve(x.size());
169 const auto& mu_ref = to_ref(mu);
170 for (size_t i = 0; i < x.size(); ++i) {
171 ret.emplace_back(offset_multiplier_constrain(x[i], mu_ref, sigma[i]));
172 }
173 return ret;
174}
175
179template <typename T, typename M, typename S,
181inline auto offset_multiplier_constrain(const std::vector<T>& x, const M& mu,
182 const std::vector<S>& sigma,
184 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
185 std::vector<plain_type_t<decltype(
186 offset_multiplier_constrain(x[0], mu, sigma[0], lp))>>
187 ret;
188 ret.reserve(x.size());
189 const auto& mu_ref = to_ref(mu);
190 for (size_t i = 0; i < x.size(); ++i) {
191 ret.emplace_back(offset_multiplier_constrain(x[i], mu_ref, sigma[i], lp));
192 }
193 return ret;
194}
195
199template <typename T, typename M, typename S,
201inline auto offset_multiplier_constrain(const std::vector<T>& x,
202 const std::vector<M>& mu,
203 const S& sigma) {
204 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
205 std::vector<
206 plain_type_t<decltype(offset_multiplier_constrain(x[0], mu[0], sigma))>>
207 ret;
208 ret.reserve(x.size());
209 const auto& sigma_ref = to_ref(sigma);
210 for (size_t i = 0; i < x.size(); ++i) {
211 ret.emplace_back(offset_multiplier_constrain(x[i], mu[i], sigma_ref));
212 }
213 return ret;
214}
215
219template <typename T, typename M, typename S,
221inline auto offset_multiplier_constrain(const std::vector<T>& x,
222 const std::vector<M>& mu,
223 const S& sigma,
225 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
226 std::vector<plain_type_t<decltype(
227 offset_multiplier_constrain(x[0], mu[0], sigma, lp))>>
228 ret;
229 ret.reserve(x.size());
230 const auto& sigma_ref = to_ref(sigma);
231 for (size_t i = 0; i < x.size(); ++i) {
232 ret.emplace_back(offset_multiplier_constrain(x[i], mu[i], sigma_ref, lp));
233 }
234 return ret;
235}
236
240template <typename T, typename M, typename S>
241inline auto offset_multiplier_constrain(const std::vector<T>& x,
242 const std::vector<M>& mu,
243 const std::vector<S>& sigma) {
244 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
245 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
246 std::vector<plain_type_t<decltype(
247 offset_multiplier_constrain(x[0], mu[0], sigma[0]))>>
248 ret;
249 ret.reserve(x.size());
250 for (size_t i = 0; i < x.size(); ++i) {
251 ret.emplace_back(offset_multiplier_constrain(x[i], mu[i], sigma[i]));
252 }
253 return ret;
254}
255
259template <typename T, typename M, typename S>
260inline auto offset_multiplier_constrain(const std::vector<T>& x,
261 const std::vector<M>& mu,
262 const std::vector<S>& sigma,
264 check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu);
265 check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma);
266 std::vector<plain_type_t<decltype(
267 offset_multiplier_constrain(x[0], mu[0], sigma[0], lp))>>
268 ret;
269 ret.reserve(x.size());
270 for (size_t i = 0; i < x.size(); ++i) {
271 ret.emplace_back(offset_multiplier_constrain(x[i], mu[i], sigma[i], lp));
272 }
273 return ret;
274}
275
299template <bool Jacobian, typename T, typename M, typename S>
300inline auto offset_multiplier_constrain(const T& x, const M& mu, const S& sigma,
302 if (Jacobian) {
303 return offset_multiplier_constrain(x, mu, sigma, lp);
304 } else {
305 return offset_multiplier_constrain(x, mu, sigma);
306 }
307}
308
309} // namespace math
310} // namespace stan
311
312#endif
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.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
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.
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
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 ...
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
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
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< 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