Skip to content

Commit f66acc4

Browse files
committed
Split out apply_scalar overloads
1 parent 26829c1 commit f66acc4

10 files changed

Lines changed: 242 additions & 91 deletions

stan/math/prim/constraint/cholesky_corr_constrain.hpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,34 @@ cholesky_corr_constrain(const EigVec& y, int K, Lp& lp) {
8282
* @tparam T A standard vector with inner type inheriting from
8383
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
8484
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
85-
* @tparam Lp A pack that is either empty, or exactly one scalar type for the lp
86-
* argument. The scalar type of T should be convertable to this.
8785
* @param y Linearly Serialized vector of size `(K * (K - 1))/2` holding the
8886
* column major order elements of the lower triangurlar
8987
* @param K The size of the matrix to return
90-
* @param[in,out] lp log density accumulator or empty
9188
*/
92-
template <typename T, typename... Lp, require_std_vector_t<T>* = nullptr>
93-
inline auto cholesky_corr_constrain(const T& y, int K, Lp&... lp) {
94-
static_assert(sizeof...(lp) == 0 || sizeof...(lp) == 1,
95-
"cholesky_corr_constrain should be called with either "
96-
"two or three arguments");
97-
return apply_vector_unary<T>::apply(y, [&lp..., K](auto&& v) {
98-
return cholesky_corr_constrain(v, K, lp...);
99-
});
89+
template <typename T, require_std_vector_t<T>* = nullptr>
90+
inline auto cholesky_corr_constrain(const T& y, int K) {
91+
return apply_vector_unary<T>::apply(
92+
y, [K](auto&& v) { return cholesky_corr_constrain(v, K); });
93+
}
94+
95+
/**
96+
* Return The cholesky of a `KxK` correlation matrix.
97+
* This overload handles looping over the elements of a standard vector.
98+
* @tparam T A standard vector with inner type inheriting from
99+
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
100+
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
101+
* @tparam Lp Scalar type for the lp argument. The scalar type of T should be
102+
* convertable to this.
103+
* @param y Linearly Serialized vector of size `(K * (K - 1))/2` holding the
104+
* column major order elements of the lower triangurlar
105+
* @param K The size of the matrix to return
106+
* @param[in,out] lp log density accumulator
107+
*/
108+
template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
109+
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
110+
inline auto cholesky_corr_constrain(const T& y, int K, Lp& lp) {
111+
return apply_vector_unary<T>::apply(
112+
y, [&lp, K](auto&& v) { return cholesky_corr_constrain(v, K, lp); });
100113
}
101114

102115
/**

stan/math/prim/constraint/cholesky_factor_constrain.hpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,39 @@ cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) {
9595
* @tparam T A standard vector with inner type inheriting from
9696
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
9797
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
98-
* @tparam Lp A pack that is either empty, or exactly one scalar type for the lp
99-
* argument. The scalar type of T should be convertable to this.
10098
* @param x Vector of unconstrained values
10199
* @param M number of rows
102100
* @param N number of columns
103-
* @param[in,out] lp log density accumulator or empty
104101
* @return Cholesky factor
105102
*/
106-
template <typename T, typename... Lp, require_std_vector_t<T>* = nullptr>
107-
inline auto cholesky_factor_constrain(const T& x, int M, int N, Lp&... lp) {
108-
static_assert(sizeof...(lp) == 0 || sizeof...(lp) == 1,
109-
"cholesky_factor_constrain should be called with either "
110-
"three or four arguments");
111-
return apply_vector_unary<T>::apply(x, [&lp..., M, N](auto&& v) {
112-
return cholesky_factor_constrain(v, M, N, lp...);
103+
template <typename T, , require_std_vector_t<T>* = nullptr>
104+
inline auto cholesky_factor_constrain(const T& x, int M, int N) {
105+
return apply_vector_unary<T>::apply(
106+
x, [M, N](auto&& v) { return cholesky_factor_constrain(v, M, N); });
107+
}
108+
109+
/**
110+
* Return the Cholesky factor of the specified size read from the specified
111+
* vector. A total of (N choose 2) + N + N * (M - N) free parameters are
112+
* required to read an M by N Cholesky factor.
113+
* This overload handles looping over the elements of a standard vector.
114+
*
115+
* @tparam T A standard vector with inner type inheriting from
116+
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
117+
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
118+
* @tparam Lp Scalar type for the lp argument. The scalar type of T should be
119+
* convertable to this.
120+
* @param x Vector of unconstrained values
121+
* @param M number of rows
122+
* @param N number of columns
123+
* @param[in,out] lp log density accumulator
124+
* @return Cholesky factor
125+
*/
126+
template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
127+
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
128+
inline auto cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) {
129+
return apply_vector_unary<T>::apply(x, [&lp, M, N](auto&& v) {
130+
return cholesky_factor_constrain(v, M, N, lp);
113131
});
114132
}
115133

stan/math/prim/constraint/corr_matrix_constrain.hpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,36 @@ corr_matrix_constrain(const T& x, Eigen::Index k, Lp& lp) {
8787
* @tparam T A standard vector with inner type inheriting from
8888
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
8989
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
90-
* @tparam Lp A pack that is either empty, or exactly one scalar type for the lp
91-
* argument. The scalar type of T should be convertable to this.
9290
* @param y Vector of unconstrained partial correlations
9391
* @param K Dimensionality of returned correlation matrix
94-
* @param[in, out] lp log density accumulator or empty
9592
*/
96-
template <typename T, typename... Lp, require_std_vector_t<T>* = nullptr>
97-
inline auto corr_matrix_constrain(const T& y, int K, Lp&... lp) {
98-
static_assert(sizeof...(lp) == 0 || sizeof...(lp) == 1,
99-
"corr_matrix_constrain should be called with either "
100-
"two or three arguments");
93+
template <typename T, require_std_vector_t<T>* = nullptr>
94+
inline auto corr_matrix_constrain(const T& y, int K) {
10195
return apply_vector_unary<T>::apply(
102-
y, [&lp..., K](auto&& v) { return corr_matrix_constrain(v, K, lp...); });
96+
y, [K](auto&& v) { return corr_matrix_constrain(v, K, ); });
97+
}
98+
99+
/**
100+
* Return the correlation matrix of the specified dimensionality derived from
101+
* the specified vector of unconstrained values. The input vector must be of
102+
* length \f${k \choose 2} = \frac{k(k-1)}{2}\f$. The values in the input
103+
* vector represent unconstrained (partial) correlations among the dimensions.
104+
* This overload handles looping over the elements of a standard vector.
105+
*
106+
* @tparam T A standard vector with inner type inheriting from
107+
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
108+
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
109+
* @tparam Lp Scalar type for the lp argument. The scalar type of T should be
110+
* convertable to this.
111+
* @param y Vector of unconstrained partial correlations
112+
* @param K Dimensionality of returned correlation matrix
113+
* @param[in, out] lp log density accumulator o
114+
*/
115+
template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
116+
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
117+
inline auto corr_matrix_constrain(const T& y, int K, Lp& lp) {
118+
return apply_vector_unary<T>::apply(
119+
y, [&lp, K](auto&& v) { return corr_matrix_constrain(v, K, lp); });
103120
}
104121

105122
/**

stan/math/prim/constraint/cov_matrix_constrain.hpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,36 @@ cov_matrix_constrain(const T& x, Eigen::Index K, Lp& lp) {
9797
* @tparam T A standard vector with inner type inheriting from
9898
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
9999
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
100-
* @tparam Lp A pack that is either empty, or exactly one scalar type for the lp
101-
* argument. The scalar type of T should be convertable to this.
102100
* @param x The vector to convert to a covariance matrix
103101
* @param K The dimensions of the resulting covariance matrix
104-
* @param[in, out] lp log density accumulator or empty
105102
* @throws std::domain_error if (x.size() != K + (K choose 2)).
106103
*/
107-
template <typename T, typename... Lp, require_std_vector_t<T>* = nullptr>
108-
inline auto cov_matrix_constrain(const T& x, Eigen::Index K, Lp&... lp) {
109-
static_assert(sizeof...(lp) == 0 || sizeof...(lp) == 1,
110-
"cov_matrix_constrain should be called with either "
111-
"two or three arguments");
104+
template <typename T, require_std_vector_t<T>* = nullptr>
105+
inline auto cov_matrix_constrain(const T& x, Eigen::Index K) {
112106
return apply_vector_unary<T>::apply(
113-
x, [&lp..., K](auto&& v) { return cov_matrix_constrain(v, K, lp...); });
107+
x, [K](auto&& v) { return cov_matrix_constrain(v, K); });
108+
}
109+
110+
/**
111+
* Return the symmetric, positive-definite matrix of dimensions K by K resulting
112+
* from transforming the specified finite vector of size K plus (K choose 2).
113+
* This overload handles looping over the elements of a standard vector.
114+
*
115+
* @tparam T A standard vector with inner type inheriting from
116+
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
117+
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
118+
* @tparam Lp Scalar type for the lp argument. The scalar type of T should be
119+
* convertable to this.
120+
* @param x The vector to convert to a covariance matrix
121+
* @param K The dimensions of the resulting covariance matrix
122+
* @param[in, out] lp log density accumulator
123+
* @throws std::domain_error if (x.size() != K + (K choose 2)).
124+
*/
125+
template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
126+
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
127+
inline auto cov_matrix_constrain(const T& x, Eigen::Index K, Lp& lp) {
128+
return apply_vector_unary<T>::apply(
129+
x, [&lp, K](auto&& v) { return cov_matrix_constrain(v, K, lp) });
114130
}
115131

116132
/**

stan/math/prim/constraint/ordered_constrain.hpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,35 @@ inline auto ordered_constrain(const EigVec& x, Lp& lp) {
7272
* @tparam T A standard vector with inner type inheriting from
7373
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
7474
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
75-
* @tparam Lp A pack that is either empty, or exactly one scalar type for the lp
76-
* argument. The scalar type of T should be convertable to this.
75+
* @param x Free vector of scalars
76+
* @return Positive, increasing ordered vector.
77+
*/
78+
template <typename T, require_std_vector_t<T>* = nullptr>
79+
inline auto ordered_constrain(const T& x) {
80+
return apply_vector_unary<T>::apply(
81+
x, [](auto&& v) { return ordered_constrain(v); });
82+
}
83+
84+
/**
85+
* Return a positive valued, increasing ordered vector derived from the
86+
* specified free vector. The returned constrained vector will have the same
87+
* dimensionality as the specified free vector.
88+
* This overload handles looping over the elements of a standard vector.
89+
*
90+
* @tparam T A standard vector with inner type inheriting from
91+
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
92+
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
93+
* @tparam Lp Scalar type for the lp argument. The scalar type of T should be
94+
* convertable to this.
7795
* @param x Free vector of scalars
7896
* @param[in, out] lp log density accumulator or empty
7997
* @return Positive, increasing ordered vector.
8098
*/
81-
template <typename T, typename... Lp, require_std_vector_t<T>* = nullptr>
82-
inline auto ordered_constrain(const T& x, Lp&... lp) {
83-
static_assert(sizeof...(lp) == 0 || sizeof...(lp) == 1,
84-
"ordered_constrain should be called with either "
85-
"one or two arguments");
99+
template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
100+
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
101+
inline auto ordered_constrain(const T& x, Lp& lp) {
86102
return apply_vector_unary<T>::apply(
87-
x, [&lp...](auto&& v) { return ordered_constrain(v, lp...); });
103+
x, [&lp](auto&& v) { return ordered_constrain(v, lp); });
88104
}
89105

90106
/**

stan/math/prim/constraint/positive_ordered_constrain.hpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,34 @@ inline auto positive_ordered_constrain(const Vec& x, Lp& lp) {
6767
* @tparam T A standard vector with inner type inheriting from
6868
* `Eigen::EigenBase`, a `var_value` with inner type inheriting from
6969
* `Eigen::EigenBase`
70-
* @tparam Lp A pack that is either empty, or exactly one scalar type for the lp
71-
* argument. The scalar type of T should be convertable to this.
7270
* @param x Free vector of scalars
73-
* @param[in, out] lp log density accumulator or empty
7471
* @return Positive, increasing ordered vector
7572
*/
76-
template <typename T, typename... Lp, require_std_vector_t<T>* = nullptr>
73+
template <typename T, require_std_vector_t<T>* = nullptr>
7774
inline auto positive_ordered_constrain(const T& x, Lp&... lp) {
78-
static_assert(sizeof...(lp) == 0 || sizeof...(lp) == 1,
79-
"positive_ordered_constrain should be called with either "
80-
"one or two arguments");
8175
return apply_vector_unary<T>::apply(
82-
x, [&lp...](auto&& v) { return positive_ordered_constrain(v, lp...); });
76+
x, [](auto&& v) { return positive_ordered_constrain(v); });
77+
}
78+
79+
/**
80+
* Return a positive valued, increasing positive ordered vector derived from the
81+
* specified free vector.
82+
* This overload handles looping over the elements of a standard vector.
83+
*
84+
* @tparam T A standard vector with inner type inheriting from
85+
* `Eigen::EigenBase`, a `var_value` with inner type inheriting from
86+
* `Eigen::EigenBase`
87+
* @tparam Lp Scalar type for the lp argument. The scalar type of T should be
88+
* convertable to this.
89+
* @param x Free vector of scalars
90+
* @param[in, out] lp log density accumulator
91+
* @return Positive, increasing ordered vector
92+
*/
93+
template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
94+
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
95+
inline auto positive_ordered_constrain(const T& x, Lp& lp) {
96+
return apply_vector_unary<T>::apply(
97+
x, [&lp](auto&& v) { return positive_ordered_constrain(v, lp); });
8398
}
8499

85100
/**

stan/math/prim/constraint/simplex_constrain.hpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,41 @@ inline plain_type_t<Vec> simplex_constrain(const Vec& y, Lp& lp) {
8383
x.coeffRef(Km1) = stick_len; // no Jacobian contrib for last dim
8484
return x;
8585
}
86+
8687
/**
8788
* Return the simplex corresponding to the specified free vector.
8889
* This overload handles looping over the elements of a standard vector.
8990
*
9091
* @tparam Vec A standard vector with inner type inheriting from
9192
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
9293
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
93-
* @tparam Lp A pack that is either empty, or exactly one scalar type for the lp
94-
* argument. The scalar type of T should be convertable to this.
9594
* @param[in] y free vector
96-
* @param[in, out] lp log density accumulator or empty
9795
* @return simplex of dimensionality one greater than `y`
9896
*/
99-
template <typename T, typename... Lp, require_std_vector_t<T>* = nullptr>
100-
inline auto simplex_constrain(const T& y, Lp&... lp) {
101-
static_assert(sizeof...(lp) == 0 || sizeof...(lp) == 1,
102-
"simplex_constrain should be called with either "
103-
"one or two arguments");
97+
template <typename T, require_std_vector_t<T>* = nullptr>
98+
inline auto simplex_constrain(const T& y) {
99+
return apply_vector_unary<T>::apply(
100+
y, [](auto&& v) { return simplex_constrain(v); });
101+
}
102+
103+
/**
104+
* Return the simplex corresponding to the specified free vector.
105+
* This overload handles looping over the elements of a standard vector.
106+
*
107+
* @tparam Vec A standard vector with inner type inheriting from
108+
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
109+
* `Eigen::DenseBase` with compile time dynamic rows and 1 column
110+
* @tparam Lp Scalar type for the lp argument. The scalar type of T should be
111+
* convertable to this.
112+
* @param[in] y free vector
113+
* @param[in, out] lp log density accumulator
114+
* @return simplex of dimensionality one greater than `y`
115+
*/
116+
template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
117+
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
118+
inline auto simplex_constrain(const T& y, Lp& lp) {
104119
return apply_vector_unary<T>::apply(
105-
y, [&lp...](auto&& v) { return simplex_constrain(v, lp...); });
120+
y, [&lp](auto&& v) { return simplex_constrain(v, lp); });
106121
}
107122

108123
/**

stan/math/prim/constraint/stochastic_column_constrain.hpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ inline plain_type_t<Mat> stochastic_column_constrain(const Mat& y, Lp& lp) {
6262
}
6363
return ret;
6464
}
65+
/**
66+
* Return a vector of column stochastic matrices.
67+
* This overload handles looping over the elements of a standard vector.
68+
*
69+
* @tparam T A standard vector with inner type inheriting from
70+
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
71+
* `Eigen::DenseBase` with compile time dynamic rows and dynamic columns
72+
* @param[in] y free vector
73+
* @return Standard vector containing matrices with simplex columns of
74+
* dimensionality (K, M).
75+
*/
76+
template <typename T, require_std_vector_t<T>* = nullptr>
77+
inline auto stochastic_column_constrain(const T& y) {
78+
return apply_vector_unary<T>::apply(
79+
y, [](auto&& v) { return stochastic_column_constrain(v); });
80+
}
6581

6682
/**
6783
* Return a vector of column stochastic matrices.
@@ -70,20 +86,18 @@ inline plain_type_t<Mat> stochastic_column_constrain(const Mat& y, Lp& lp) {
7086
* @tparam T A standard vector with inner type inheriting from
7187
* `Eigen::DenseBase` or a `var_value` with inner type inheriting from
7288
* `Eigen::DenseBase` with compile time dynamic rows and dynamic columns
73-
* @tparam Lp A pack that is either empty, or exactly one scalar type for the lp
74-
* argument. The scalar type of T should be convertable to this.
89+
* @tparam Lp Scalar type for the lp argument. The scalar type of T should be
90+
* convertable to this.
7591
* @param[in] y free vector
76-
* @param[in, out] lp log density accumulator or empty
92+
* @param[in, out] lp log density accumulator
7793
* @return Standard vector containing matrices with simplex columns of
7894
* dimensionality (K, M).
7995
*/
80-
template <typename T, typename... Lp, require_std_vector_t<T>* = nullptr>
81-
inline auto stochastic_column_constrain(const T& y, Lp&... lp) {
82-
static_assert(sizeof...(lp) == 0 || sizeof...(lp) == 1,
83-
"stochastic_column_constrain should be called with either "
84-
"one or two arguments");
96+
template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
97+
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
98+
inline auto stochastic_column_constrain(const T& y, Lp& lp) {
8599
return apply_vector_unary<T>::apply(
86-
y, [&lp...](auto&& v) { return stochastic_column_constrain(v, lp...); });
100+
y, [&lp](auto&& v) { return stochastic_column_constrain(v, lp); });
87101
}
88102

89103
/**

0 commit comments

Comments
 (0)