Stan Math Library  2.20.0
reverse mode automatic differentiation
append_row.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_APPEND_ROW_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_APPEND_ROW_HPP
3 
7 #include <vector>
8 
9 namespace stan {
10 namespace math {
11 
33 template <typename T1, typename T2, int R1, int C1, int R2, int C2>
34 inline Eigen::Matrix<typename return_type<T1, T2>::type, Eigen::Dynamic,
35  Eigen::Dynamic>
36 append_row(const Eigen::Matrix<T1, R1, C1>& A,
37  const Eigen::Matrix<T2, R2, C2>& B) {
38  using Eigen::Dynamic;
39  using Eigen::Matrix;
40 
41  int Arows = A.rows();
42  int Brows = B.rows();
43  int Acols = A.cols();
44  int Bcols = B.cols();
45  check_size_match("append_row", "columns of A", Acols, "columns of B", Bcols);
46 
47  Matrix<typename return_type<T1, T2>::type, Dynamic, Dynamic> result(
48  Arows + Brows, Acols);
49  for (int j = 0; j < Acols; j++) {
50  for (int i = 0; i < Arows; i++)
51  result(i, j) = A(i, j);
52  for (int i = Arows, k = 0; k < Brows; i++, k++)
53  result(i, j) = B(k, j);
54  }
55  return result;
56 }
57 
73 template <typename T1, typename T2, int R1, int R2>
74 inline Eigen::Matrix<typename return_type<T1, T2>::type, Eigen::Dynamic, 1>
75 append_row(const Eigen::Matrix<T1, R1, 1>& A,
76  const Eigen::Matrix<T2, R2, 1>& B) {
77  using Eigen::Dynamic;
78  using Eigen::Matrix;
79 
80  int Asize = A.size();
81  int Bsize = B.size();
82  Matrix<typename return_type<T1, T2>::type, 1, Dynamic> result(Asize + Bsize);
83  for (int i = 0; i < Asize; i++)
84  result(i) = A(i);
85  for (int i = 0, j = Asize; i < Bsize; i++, j++)
86  result(j) = B(i);
87  return result;
88 }
89 
112 template <typename T, int R1, int C1, int R2, int C2>
113 inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> append_row(
114  const Eigen::Matrix<T, R1, C1>& A, const Eigen::Matrix<T, R2, C2>& B) {
115  using Eigen::Dynamic;
116  using Eigen::Matrix;
117 
118  check_size_match("append_row", "columns of A", A.cols(), "columns of B",
119  B.cols());
120 
121  Matrix<T, Dynamic, Dynamic> result(A.rows() + B.rows(), A.cols());
122  result << A, B;
123  return result;
124 }
125 
142 template <typename T, int R1, int R2>
143 inline Eigen::Matrix<T, Eigen::Dynamic, 1> append_row(
144  const Eigen::Matrix<T, R1, 1>& A, const Eigen::Matrix<T, R2, 1>& B) {
145  using Eigen::Dynamic;
146  using Eigen::Matrix;
147 
148  Matrix<T, Dynamic, 1> result(A.size() + B.size());
149  result << A, B;
150  return result;
151 }
152 
166 template <typename T1, typename T2, int R, int C>
167 inline Eigen::Matrix<typename return_type<T1, T2>::type, Eigen::Dynamic, 1>
168 append_row(const T1& A, const Eigen::Matrix<T2, R, C>& B) {
169  using Eigen::Dynamic;
170  using Eigen::Matrix;
171  typedef typename return_type<T1, T2>::type return_type;
172 
173  Matrix<return_type, Dynamic, 1> result(B.size() + 1);
174  result << A, B.template cast<return_type>();
175  return result;
176 }
177 
191 template <typename T1, typename T2, int R, int C>
192 inline Eigen::Matrix<typename return_type<T1, T2>::type, Eigen::Dynamic, 1>
193 append_row(const Eigen::Matrix<T1, R, C>& A, const T2& B) {
194  using Eigen::Dynamic;
195  using Eigen::Matrix;
196  typedef typename return_type<T1, T2>::type return_type;
197 
198  Matrix<return_type, Dynamic, 1> result(A.size() + 1);
199  result << A.template cast<return_type>(), B;
200  return result;
201 }
202 
203 } // namespace math
204 
205 } // namespace stan
206 
207 #endif
Template metaprogram to calculate the base scalar return type resulting from promoting all the scalar...
Definition: return_type.hpp:33
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.
Eigen::Matrix< typename return_type< T1, T2 >::type, Eigen::Dynamic, Eigen::Dynamic > append_row(const Eigen::Matrix< T1, R1, C1 > &A, const Eigen::Matrix< T2, R2, C2 > &B)
Return the result of stacking the rows of the first argument matrix on top of the second argument mat...
Definition: append_row.hpp:36
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36

     [ Stan Home Page ] © 2011–2018, Stan Development Team.