Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
df9a9b2
First pass and passes mix fun test
SteveBronder Jul 9, 2025
b6e0a45
apply_scalar_binary. loglogistic now passes
SteveBronder Jul 11, 2025
13c4fd0
fix apply vector
SteveBronder Jul 11, 2025
dd3458c
update ternary
SteveBronder Jul 11, 2025
45128f6
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 11, 2025
aa5eb5e
fix docs
SteveBronder Jul 11, 2025
6ff0b0e
fix docs
SteveBronder Jul 11, 2025
4edea9f
update test_ad_test to not use apply::return_t
SteveBronder Jul 11, 2025
696d2b2
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 11, 2025
2c8c9c5
fix template for sub_col and sub_row
SteveBronder Jul 11, 2025
2c1a328
update log1m_exp template constraints
SteveBronder Jul 12, 2025
912855d
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 12, 2025
dce5a63
fix templates for var and var matrix funs
SteveBronder Jul 12, 2025
fd7ad01
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 12, 2025
021e150
fix exp2
SteveBronder Jul 12, 2025
c677470
fix exp2
SteveBronder Jul 12, 2025
a77c2b6
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 12, 2025
f385fd2
remover binary operators for +/- etc from holder
SteveBronder Jul 12, 2025
1d5a64c
make operators for holder into free functions
SteveBronder Jul 14, 2025
f5f0ead
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 14, 2025
e884013
have expr test on jenkins use gcc with sanitizer
SteveBronder Jul 14, 2025
57cd3bf
revert before merge: have expr test on jenkins use gcc with sanitizer…
SteveBronder Jul 14, 2025
b7336d2
fix templates for trigamma
SteveBronder Jul 15, 2025
ac2aa50
revert changes to jenkinsfile
SteveBronder Jul 15, 2025
5b87798
remove using boost::math::lgamma from prob functions to avoid name clash
SteveBronder Jul 15, 2025
c9c088e
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 15, 2025
24c54ba
update constraints to also use pf
SteveBronder Jul 16, 2025
a591471
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 16, 2025
fd17149
fix doxygen
SteveBronder Jul 16, 2025
c27c5a4
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 16, 2025
2e7492b
fix fwd in norm2
SteveBronder Jul 16, 2025
b4f2aaf
fix fwd in norm2
SteveBronder Jul 16, 2025
4517133
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 16, 2025
6550d1e
fix inv_square function deduction
SteveBronder Jul 16, 2025
bd698a3
fix typo in ordered_constrain
SteveBronder Jul 17, 2025
4eae692
fixup from andrews review
SteveBronder Jul 28, 2025
b07f4d6
Merge commit '0c13850f5b8d853491d8bdfc85465ca7e500d432' into HEAD
yashikno Jul 28, 2025
0c7c507
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 28, 2025
61d8192
remove if constexpr in apply functors
SteveBronder Jul 28, 2025
76229f0
update sum_to_zero_constrain requires in rev
SteveBronder Jul 28, 2025
926864a
do not use x after x_ref is made in ordered_contrain
SteveBronder Jul 29, 2025
aba568d
make a const data() member function in holder
SteveBronder Jul 29, 2025
51ef9f8
make a const data() member function in holder
SteveBronder Jul 29, 2025
02d57f8
fix as_column_vector_or_scalar so that it copies std::vector types un…
SteveBronder Jul 31, 2025
c083810
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 31, 2025
27dd922
fix as_array_or_scalar so that it copies std::vector types unless the…
SteveBronder Jul 31, 2025
ba4252c
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 31, 2025
d51ce3f
update dirichlet_multinomial
SteveBronder Jul 31, 2025
cef520c
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Jul 31, 2025
ce7af32
allow as_array_or_scalar to return a holder of a map for std vector t…
SteveBronder Aug 1, 2025
1913a45
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Aug 1, 2025
4f97d77
update with brians review
SteveBronder Aug 11, 2025
bb37bf9
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Aug 11, 2025
04fe232
fix all places with Eigen maps and size functions for perfect forwarding
SteveBronder Aug 11, 2025
3f3a52f
[Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1
stan-buildbot Aug 11, 2025
acd3426
remove use of Eigen::Map in divide_columns
SteveBronder Aug 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions stan/math/fwd/fun/inv_logit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ namespace math {
* @param x argument
* @return inverse logit of argument
*/
template <typename T>
inline fvar<T> inv_logit(const fvar<T>& x) {
return fvar<T>(inv_logit(x.val_),
x.d_ * inv_logit(x.val_) * (1 - inv_logit(x.val_)));
template <typename T, require_fvar_t<T>* = nullptr>
inline auto inv_logit(T&& x) {
return std::decay_t<T>(inv_logit(x.val_),
x.d_ * inv_logit(x.val_) * (1 - inv_logit(x.val_)));
}

} // namespace math
Expand Down
13 changes: 7 additions & 6 deletions stan/math/fwd/fun/log_softmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/fun/softmax.hpp>
#include <stan/math/prim/fun/log_softmax.hpp>
#include <stan/math/prim/fun/to_ref.hpp>

namespace stan {
namespace math {
Expand All @@ -19,24 +20,24 @@ namespace math {
* @throw std::domain_error If the input vector is size 0.
*/
template <typename T, require_vector_st<is_fvar, T>* = nullptr>
inline auto log_softmax(const T& x) {
return apply_vector_unary<T>::apply(x, [&](const auto& alpha) {
inline auto log_softmax(T&& x) {
return apply_vector_unary<T>::apply(std::forward<T>(x), [](auto&& alpha) {
using T_alpha = decltype(alpha);
using T_fvar = value_type_t<T_alpha>;
using T_fvar_inner = typename T_fvar::Scalar;

const Eigen::Ref<const plain_type_t<T_alpha>>& alpha_ref = alpha;
auto&& alpha_ref = to_ref(std::forward<decltype(alpha)>(alpha));
Eigen::Matrix<T_fvar_inner, -1, 1> alpha_t = alpha_ref.val();
Eigen::Matrix<T_fvar_inner, -1, 1> softmax_alpha_t = softmax(alpha_t);

Eigen::Matrix<T_fvar, -1, 1> log_softmax_alpha(alpha.size());
Eigen::Matrix<T_fvar, -1, 1> log_softmax_alpha(alpha_ref.size());
log_softmax_alpha.val() = log_softmax(alpha_t);
log_softmax_alpha.d().setZero();

for (int m = 0; m < alpha.size(); ++m) {
for (int m = 0; m < alpha_ref.size(); ++m) {
T_fvar_inner negative_alpha_m_d_times_softmax_alpha_t_m
= -alpha_ref.coeff(m).d_ * softmax_alpha_t(m);
for (int k = 0; k < alpha.size(); ++k) {
for (int k = 0; k < alpha_ref.size(); ++k) {
if (m == k) {
log_softmax_alpha(k).d_
+= alpha_ref.coeff(m).d_
Expand Down
4 changes: 2 additions & 2 deletions stan/math/fwd/fun/log_sum_exp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ inline fvar<T> log_sum_exp(const fvar<T>& x1, double x2) {
* @return The log of the sum of the exponentiated vector values.
*/
template <typename T, require_container_st<is_fvar, T>* = nullptr>
inline auto log_sum_exp(const T& x) {
inline auto log_sum_exp(T&& x) {
return apply_vector_unary<ref_type_t<T>>::reduce(
to_ref(x), [&](const auto& v) {
to_ref(std::forward<T>(x)), [](auto&& v) {
using T_fvar_inner = typename value_type_t<decltype(v)>::Scalar;
using mat_type = Eigen::Matrix<T_fvar_inner, -1, -1>;
mat_type vals = v.val();
Expand Down
6 changes: 3 additions & 3 deletions stan/math/fwd/fun/norm1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace math {
/**
* Compute the L1 norm of the specified vector of values.
*
* @tparam T Type of input vector.
* @tparam Container Type of input vector.
* @param[in] x Vector of specified values.
* @return L1 norm of x.
*/
template <typename Container, require_eigen_vt<is_fvar, Container>* = nullptr>
inline auto norm1(const Container& x) {
inline auto norm1(Container&& x) {
return apply_vector_unary<ref_type_t<Container>>::reduce(
to_ref(x), [&](const auto& v) {
to_ref(std::forward<Container>(x)), [](auto&& v) {
using T_fvar_inner = typename value_type_t<decltype(v)>::Scalar;
return fvar<T_fvar_inner>(norm1(v.val()),
v.d().cwiseProduct(sign(v.val())).sum());
Expand Down
6 changes: 3 additions & 3 deletions stan/math/fwd/fun/norm2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ namespace math {
/**
* Compute the L2 norm of the specified vector of values.
*
* @tparam T Type of input vector.
* @tparam Container Type of input vector.
* @param[in] x Vector of specified values.
* @return L2 norm of x.
*/
template <typename Container, require_eigen_vt<is_fvar, Container>* = nullptr>
inline auto norm2(const Container& x) {
inline auto norm2(Container&& x) {
return apply_vector_unary<ref_type_t<Container>>::reduce(
to_ref(x), [&](const auto& v) {
to_ref(std::forward<Container>(x)), [](auto&& v) {
using T_fvar_inner = typename value_type_t<decltype(v)>::Scalar;
T_fvar_inner res = norm2(v.val());
return fvar<T_fvar_inner>(res,
Expand Down
8 changes: 6 additions & 2 deletions stan/math/fwd/fun/pow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ inline auto pow(const T1& x1, const T2& x2) {
template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr,
require_all_not_matrix_st<is_var, T1, T2>* = nullptr,
require_any_fvar_t<base_type_t<T1>, base_type_t<T2>>* = nullptr>
inline auto pow(const T1& a, const T2& b) {
inline auto pow(T1&& a, T2&& b) {
return apply_scalar_binary(
[](const auto& c, const auto& d) { return stan::math::pow(c, d); }, a, b);
[](auto&& c, auto&& d) {
return stan::math::pow(std::forward<decltype(c)>(c),
std::forward<decltype(d)>(d));
},
std::forward<T1>(a), std::forward<T2>(b));
}

} // namespace math
Expand Down
9 changes: 6 additions & 3 deletions stan/math/fwd/functor/apply_scalar_unary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ namespace math {
* autodiff variable.
*/
template <typename F, typename T>
struct apply_scalar_unary<F, fvar<T> > {
struct apply_scalar_unary<F, T, require_fvar_t<T>> {
/**
* Function return type, which is same as the argument type for
* the function, <code>fvar&lt;T&gt;</code>.
*/
using return_t = fvar<T>;
using return_t = std::decay_t<T>;

/**
* Apply the function specified by F to the specified argument.
*
* @param x Argument variable.
* @return Function applied to the variable.
*/
static inline return_t apply(const fvar<T>& x) { return F::fun(x); }
template <typename T2>
static inline auto apply(T2&& x) {
return F::fun(std::forward<T2>(x));
}
};

} // namespace math
Expand Down
6 changes: 3 additions & 3 deletions stan/math/fwd/functor/finite_diff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ inline constexpr double aggregate_tangent(const FuncTangent& tangent,
template <typename FuncTangent, typename InputArg,
require_st_fvar<InputArg>* = nullptr>
inline auto aggregate_tangent(const FuncTangent& tangent, const InputArg& arg) {
return sum(apply_scalar_binary(
[](const auto& x, const auto& y) { return x * y.d_; }, tangent, arg));
return sum(apply_scalar_binary([](auto&& x, auto&& y) { return x * y.d_; },
tangent, arg));
}
} // namespace internal

Expand Down Expand Up @@ -73,7 +73,7 @@ inline auto finite_diff(const F& func, const TArgs&... args) {
std::vector<FvarInnerT> serialised_args
= serialize<FvarInnerT>(value_of(args)...);

auto serial_functor = [&](const auto& v) {
auto serial_functor = [&](auto&& v) {
auto v_deserializer = to_deserializer(v);
return func(v_deserializer.read(args)...);
};
Expand Down
1 change: 1 addition & 0 deletions stan/math/opencl/prim/dirichlet_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define STAN_MATH_OPENCL_PRIM_DIRICHLET_LPDF_HPP
#ifdef STAN_OPENCL

#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/err.hpp>
#include <stan/math/prim/fun/constants.hpp>
Expand Down
3 changes: 1 addition & 2 deletions stan/math/opencl/prim/log_softmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ template <typename T,
require_all_kernel_expressions_and_none_scalar_t<T>* = nullptr>
inline matrix_cl<double> log_softmax(const T& a) {
check_nonzero_size("log_softmax (OpenCL)", "x", a);
return make_holder_cl([](const auto& x) { return x - log_sum_exp(x); },
to_ref(a));
return make_holder_cl([](auto&& x) { return x - log_sum_exp(x); }, to_ref(a));
}

} // namespace math
Expand Down
20 changes: 11 additions & 9 deletions stan/math/prim/constraint/cholesky_corr_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ cholesky_corr_constrain(const EigVec& y, int K, Lp& lp) {
* @param K The size of the matrix to return
*/
template <typename T, require_std_vector_t<T>* = nullptr>
inline auto cholesky_corr_constrain(const T& y, int K) {
return apply_vector_unary<T>::apply(
y, [K](auto&& v) { return cholesky_corr_constrain(v, K); });
inline auto cholesky_corr_constrain(T&& y, int K) {
return apply_vector_unary<T>::apply(std::forward<T>(y), [K](auto&& v) {
return cholesky_corr_constrain(std::forward<decltype(v)>(v), K);
});
}

/**
Expand All @@ -107,9 +108,10 @@ inline auto cholesky_corr_constrain(const T& y, int K) {
*/
template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
inline auto cholesky_corr_constrain(const T& y, int K, Lp& lp) {
return apply_vector_unary<T>::apply(
y, [&lp, K](auto&& v) { return cholesky_corr_constrain(v, K, lp); });
inline auto cholesky_corr_constrain(T&& y, int K, Lp& lp) {
return apply_vector_unary<T>::apply(std::forward<T>(y), [&lp, K](auto&& v) {
return cholesky_corr_constrain(std::forward<decltype(v)>(v), K, lp);
});
}

/**
Expand All @@ -132,11 +134,11 @@ inline auto cholesky_corr_constrain(const T& y, int K, Lp& lp) {
*/
template <bool Jacobian, typename T, typename Lp,
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
inline auto cholesky_corr_constrain(const T& y, int K, Lp& lp) {
inline auto cholesky_corr_constrain(T&& y, int K, Lp& lp) {
if constexpr (Jacobian) {
return cholesky_corr_constrain(y, K, lp);
return cholesky_corr_constrain(std::forward<T>(y), K, lp);
} else {
return cholesky_corr_constrain(y, K);
return cholesky_corr_constrain(std::forward<T>(y), K);
}
}

Expand Down
7 changes: 4 additions & 3 deletions stan/math/prim/constraint/cholesky_corr_free.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ inline auto cholesky_corr_free(const T& x) {
* @param x The standard vector to untransform.
*/
template <typename T, require_std_vector_t<T>* = nullptr>
inline auto cholesky_corr_free(const T& x) {
return apply_vector_unary<T>::apply(
x, [](auto&& v) { return cholesky_corr_free(v); });
inline auto cholesky_corr_free(T&& x) {
return apply_vector_unary<T>::apply(std::forward<T>(x), [](auto&& v) {
return cholesky_corr_free(std::forward<decltype(v)>(v));
});
}

} // namespace math
Expand Down
20 changes: 11 additions & 9 deletions stan/math/prim/constraint/cholesky_factor_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) {
* @return Cholesky factor
*/
template <typename T, require_std_vector_t<T>* = nullptr>
inline auto cholesky_factor_constrain(const T& x, int M, int N) {
return apply_vector_unary<T>::apply(
x, [M, N](auto&& v) { return cholesky_factor_constrain(v, M, N); });
inline auto cholesky_factor_constrain(T&& x, int M, int N) {
return apply_vector_unary<T>::apply(std::forward<T>(x), [M, N](auto&& v) {
return cholesky_factor_constrain(std::forward<decltype(v)>(v), M, N);
});
}

/**
Expand All @@ -125,9 +126,10 @@ inline auto cholesky_factor_constrain(const T& x, int M, int N) {
*/
template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
inline auto cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) {
return apply_vector_unary<T>::apply(x, [&lp, M, N](auto&& v) {
return cholesky_factor_constrain(v, M, N, lp);
inline auto cholesky_factor_constrain(T&& x, int M, int N, Lp& lp) {
return apply_vector_unary<T>::apply(std::forward<T>(x), [&lp, M,
N](auto&& v) {
return cholesky_factor_constrain(std::forward<decltype(v)>(v), M, N, lp);
});
}

Expand Down Expand Up @@ -155,11 +157,11 @@ inline auto cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) {
*/
template <bool Jacobian, typename T, typename Lp,
require_convertible_t<return_type_t<T>, Lp>* = nullptr>
inline auto cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) {
inline auto cholesky_factor_constrain(T&& x, int M, int N, Lp& lp) {
if constexpr (Jacobian) {
return cholesky_factor_constrain(x, M, N, lp);
return cholesky_factor_constrain(std::forward<T>(x), M, N, lp);
} else {
return cholesky_factor_constrain(x, M, N);
return cholesky_factor_constrain(std::forward<T>(x), M, N);
}
}

Expand Down
7 changes: 4 additions & 3 deletions stan/math/prim/constraint/cholesky_factor_free.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, 1> cholesky_factor_free(
* @param x The standard vector to untransform.
*/
template <typename T, require_std_vector_t<T>* = nullptr>
inline auto cholesky_factor_free(const T& x) {
return apply_vector_unary<T>::apply(
x, [](auto&& v) { return cholesky_factor_free(v); });
inline auto cholesky_factor_free(T&& x) {
return apply_vector_unary<T>::apply(std::forward<T>(x), [](auto&& v) {
return cholesky_factor_free(std::forward<decltype(v)>(v));
});
}

} // namespace math
Expand Down
14 changes: 7 additions & 7 deletions stan/math/prim/constraint/corr_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace math {
* @return tanh transform
*/
template <typename T>
inline plain_type_t<T> corr_constrain(const T& x) {
return tanh(x);
inline plain_type_t<T> corr_constrain(T&& x) {
return tanh(std::forward<T>(x));
}

/**
Expand All @@ -43,8 +43,8 @@ inline plain_type_t<T> corr_constrain(const T& x) {
* @param[in,out] lp log density accumulator
*/
template <typename T_x, typename T_lp>
inline auto corr_constrain(const T_x& x, T_lp& lp) {
plain_type_t<T_x> tanh_x = tanh(x);
inline auto corr_constrain(T_x&& x, T_lp& lp) {
plain_type_t<T_x> tanh_x = tanh(std::forward<T_x>(x));
lp += sum(log1m(square(tanh_x)));
return tanh_x;
}
Expand All @@ -65,11 +65,11 @@ inline auto corr_constrain(const T_x& x, T_lp& lp) {
* @param[in,out] lp log density accumulator
*/
template <bool Jacobian, typename T_x, typename T_lp>
inline auto corr_constrain(const T_x& x, T_lp& lp) {
inline auto corr_constrain(T_x&& x, T_lp& lp) {
if constexpr (Jacobian) {
return corr_constrain(x, lp);
return corr_constrain(std::forward<T_x>(x), lp);
} else {
return corr_constrain(x);
return corr_constrain(std::forward<T_x>(x));
}
}

Expand Down
7 changes: 4 additions & 3 deletions stan/math/prim/constraint/corr_free.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ namespace math {
* @return free scalar that transforms to the specified input
*/
template <typename T>
inline plain_type_t<T> corr_free(const T& y) {
check_bounded("lub_free", "Correlation variable", y, -1.0, 1.0);
return atanh(y);
inline plain_type_t<T> corr_free(T&& y) {
auto&& y_ref = to_ref(std::forward<T>(y));
check_bounded("lub_free", "Correlation variable", y_ref, -1.0, 1.0);
return atanh(std::forward<decltype(y_ref)>(y_ref));
}

} // namespace math
Expand Down
Loading
Loading