Stan Math Library  2.10.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 
11  namespace math {
12 
34  template <typename T1, typename T2, int R1, int C1, int R2, int C2>
35  inline Eigen::Matrix<typename return_type<T1, T2>::type,
36  Eigen::Dynamic, Eigen::Dynamic>
37  append_row(const Eigen::Matrix<T1, R1, C1>& A,
38  const Eigen::Matrix<T2, R2, C2>& B) {
39  using Eigen::Dynamic;
40  using Eigen::Matrix;
41 
42  int Arows = A.rows();
43  int Brows = B.rows();
44  int Acols = A.cols();
45  int Bcols = B.cols();
46  check_size_match("append_row",
47  "columns of A", Acols,
48  "columns of B", Bcols);
49 
50  Matrix<typename return_type<T1, T2>::type, Dynamic, Dynamic>
51  result(Arows + Brows, Acols);
52  for (int j = 0; j < Acols; j++) {
53  for (int i = 0; i < Arows; i++)
54  result(i, j) = A(i, j);
55  for (int i = Arows, k = 0; k < Brows; i++, k++)
56  result(i, j) = B(k, j);
57  }
58  return result;
59  }
60 
61 
77  template <typename T1, typename T2, int R1, int R2>
78  inline Eigen::Matrix<typename return_type<T1, T2>::type,
79  Eigen::Dynamic, 1>
80  append_row(const Eigen::Matrix<T1, R1, 1>& A,
81  const Eigen::Matrix<T2, R2, 1>& B) {
82  using Eigen::Dynamic;
83  using Eigen::Matrix;
84 
85  int Asize = A.size();
86  int Bsize = B.size();
87  Matrix<typename return_type<T1, T2>::type, 1, Dynamic>
88  result(Asize + Bsize);
89  for (int i = 0; i < Asize; i++)
90  result(i) = A(i);
91  for (int i = 0, j = Asize; i < Bsize; i++, j++)
92  result(j) = B(i);
93  return result;
94  }
95 
96 
119  template <typename T, int R1, int C1, int R2, int C2>
120  inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
121  append_row(const Eigen::Matrix<T, R1, C1>& A,
122  const Eigen::Matrix<T, R2, C2>& B) {
123  using Eigen::Dynamic;
124  using Eigen::Matrix;
125 
126  check_size_match("append_row",
127  "columns of A", A.cols(),
128  "columns of B", B.cols());
129 
130  Matrix<T, Dynamic, Dynamic>
131  result(A.rows() + B.rows(), A.cols());
132  result << A, B;
133  return result;
134  }
135 
136 
153  template <typename T, int R1, int R2>
154  inline Eigen::Matrix<T, Eigen::Dynamic, 1>
155  append_row(const Eigen::Matrix<T, R1, 1>& A,
156  const Eigen::Matrix<T, R2, 1>& B) {
157  using Eigen::Dynamic;
158  using Eigen::Matrix;
159 
160  Matrix<T, Dynamic, 1> result(A.size()+B.size());
161  result << A, B;
162  return result;
163  }
164 
165 
179  template <typename T1, typename T2, int R, int C>
180  inline Eigen::Matrix<typename return_type<T1, T2>::type,
181  Eigen::Dynamic, 1>
182  append_row(const T1& A,
183  const Eigen::Matrix<T2, R, C>& B) {
184  using Eigen::Dynamic;
185  using Eigen::Matrix;
186  typedef typename return_type<T1, T2>::type return_type;
187 
188  Matrix<return_type, Dynamic, 1>
189  result(B.size() + 1);
190  result << A, B.template cast<return_type>();
191  return result;
192  }
193 
194 
208  template <typename T1, typename T2, int R, int C>
209  inline Eigen::Matrix<typename return_type<T1, T2>::type,
210  Eigen::Dynamic, 1>
211  append_row(const Eigen::Matrix<T1, R, C>& A,
212  const T2& B) {
213  using Eigen::Dynamic;
214  using Eigen::Matrix;
215  typedef typename return_type<T1, T2>::type return_type;
216 
217  Matrix<return_type, Dynamic, 1>
218  result(A.size() + 1);
219  result << A.template cast<return_type>(), B;
220  return result;
221  }
222 
223  }
224 
225 }
226 
227 #endif
Metaprogram to calculate the base scalar return type resulting from promoting all the scalar types of...
Definition: return_type.hpp:19
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Definition: return_type.hpp:27
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:37
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.

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