Automatic Differentiation
 
Loading...
Searching...
No Matches
append_row.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_APPEND_ROW_HPP
2#define STAN_MATH_REV_FUN_APPEND_ROW_HPP
3
9#include <vector>
10
11namespace stan {
12namespace math {
13
32template <typename T1, typename T2, require_any_var_matrix_t<T1, T2>* = nullptr>
33inline auto append_row(const T1& A, const T2& B) {
34 check_size_match("append_row", "columns of A", A.cols(), "columns of B",
35 B.cols());
36 if (!is_constant<T1>::value && !is_constant<T2>::value) {
37 arena_t<promote_scalar_t<var, T1>> arena_A = A;
38 arena_t<promote_scalar_t<var, T2>> arena_B = B;
39 return make_callback_var(
40 append_row(value_of(arena_A), value_of(arena_B)),
41 [arena_A, arena_B](auto& vi) mutable {
42 arena_A.adj() += vi.adj().topRows(arena_A.rows());
43 arena_B.adj() += vi.adj().bottomRows(arena_B.rows());
44 });
45 } else if (!is_constant<T1>::value) {
46 arena_t<promote_scalar_t<var, T1>> arena_A = A;
47 return make_callback_var(append_row(value_of(arena_A), value_of(B)),
48 [arena_A](auto& vi) mutable {
49 arena_A.adj()
50 += vi.adj().topRows(arena_A.rows());
51 });
52 } else {
53 arena_t<promote_scalar_t<var, T2>> arena_B = B;
54 return make_callback_var(append_row(value_of(A), value_of(arena_B)),
55 [arena_B](auto& vi) mutable {
56 arena_B.adj()
57 += vi.adj().bottomRows(arena_B.rows());
58 });
59 }
60}
61
75template <typename Scal, typename ColVec,
76 require_stan_scalar_t<Scal>* = nullptr,
77 require_t<is_eigen_col_vector<ColVec>>* = nullptr>
78inline auto append_row(const Scal& A, const var_value<ColVec>& B) {
80 var arena_A = A;
82 return make_callback_var(append_row(value_of(arena_A), value_of(arena_B)),
83 [arena_A, arena_B](auto& vi) mutable {
84 arena_A.adj() += vi.adj().coeff(0);
85 arena_B.adj() += vi.adj().tail(arena_B.size());
86 });
87 } else if (!is_constant<Scal>::value) {
88 var arena_A = A;
89 return make_callback_var(
90 append_row(value_of(arena_A), value_of(B)),
91 [arena_A](auto& vi) mutable { arena_A.adj() += vi.adj().coeff(0); });
92 } else {
94 return make_callback_var(append_row(value_of(A), value_of(arena_B)),
95 [arena_B](auto& vi) mutable {
96 arena_B.adj() += vi.adj().tail(arena_B.size());
97 });
98 }
99}
100
114template <typename ColVec, typename Scal,
117inline auto append_row(const var_value<ColVec>& A, const Scal& B) {
120 var arena_B = B;
121 return make_callback_var(append_row(value_of(arena_A), value_of(arena_B)),
122 [arena_A, arena_B](auto& vi) mutable {
123 arena_A.adj() += vi.adj().head(arena_A.size());
124 arena_B.adj()
125 += vi.adj().coeff(vi.adj().size() - 1);
126 });
127 } else if (!is_constant<ColVec>::value) {
129 return make_callback_var(append_row(value_of(arena_A), value_of(B)),
130 [arena_A](auto& vi) mutable {
131 arena_A.adj() += vi.adj().head(arena_A.size());
132 });
133 } else {
135 return make_callback_var(append_row(value_of(A), value_of(arena_B)),
136 [arena_B](auto& vi) mutable {
137 arena_B.adj()
138 += vi.adj().coeff(vi.adj().size() - 1);
139 });
140 }
141}
142
143} // namespace math
144} // namespace stan
145
146#endif
auto append_row(Ta &&a, Tb &&b)
Stack the rows of the first argument on top of the second argument.
Definition append.hpp:183
require_t< is_stan_scalar< std::decay_t< T > > > require_stan_scalar_t
Require type satisfies is_stan_scalar.
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
std::enable_if_t< Check::value > require_t
If condition is true, template is enabled.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...