Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
append_row.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__MATRIX__APPEND__ROW_HPP
2 #define STAN__MATH__MATRIX__APPEND__ROW_HPP
3 
5 #include <stan/meta/traits.hpp> //stan::return_type
7 #include <vector>
8 
9 namespace stan {
10  namespace math {
11 
12  using Eigen::Dynamic;
13  using Eigen::Matrix;
14  using std::vector;
16 
17  //matrix append_row(matrix, matrix)
18  //matrix append_row(matrix, row_vector)
19  //matrix append_row(row_vector, matrix)
20  //matrix append_row(row_vector, row_vector)
21  template <typename T1, typename T2, int R1, int C1, int R2, int C2>
22  inline Matrix<typename return_type<T1, T2>::type, Dynamic, Dynamic>
23  append_row(const Matrix<T1, R1, C1> & A,
24  const Matrix<T2, R2, C2> & B) {
25  int Arows = A.rows();
26  int Brows = B.rows();
27  int Acols = A.cols();
28  int Bcols = B.cols();
29  check_size_match("append_row(%1%)",
30  Acols, "columns of A",
31  Bcols, "columns of B",
32  (double*)0);
33 
34  Matrix<typename return_type<T1, T2>::type, Dynamic, Dynamic>
35  result(Arows + Brows, Acols);
36  for (int j = 0; j < Acols; j++) {
37  for (int i = 0; i < Arows; i++)
38  result(i, j) = A(i, j);
39  for (int i = Arows, k = 0; k < Brows; i++, k++)
40  result(i, j) = B(k, j);
41  }
42 
43  return result;
44  }
45 
46  //vector append_row(vector, vector)
47  template <typename T1, typename T2, int R1, int R2>
48  inline Matrix<typename return_type<T1, T2>::type, Dynamic, 1>
49  append_row(const Matrix<T1, R1, 1> & A,
50  const Matrix<T2, R1, 1> & B) {
51  int Asize = A.size();
52  int Bsize = B.size();
53  Matrix<typename return_type<T1, T2>::type, 1, Dynamic>
54  result(Asize + Bsize);
55  for (int i = 0; i < Asize; i++)
56  result(i) = A(i);
57  for (int i = 0, j = Asize; i < Bsize; i++, j++)
58  result(j) = B(i);
59  return result;
60  }
61 
62  //matrix append_row(matrix, matrix)
63  //matrix append_row(matrix, row_vector)
64  //matrix append_row(row_vector, matrix)
65  //matrix append_row(row_vector, row_vector)
66  template <typename T, int R1, int C1, int R2, int C2>
67  inline Matrix<T, Dynamic, Dynamic>
68  append_row(const Matrix<T, R1, C1> & A,
69  const Matrix<T, R2, C2> & B) {
70  check_size_match("append_row(%1%)",
71  A.cols(), "columns of A",
72  B.cols(), "columns of B",
73  (double*)0);
74 
75  Matrix<T, Dynamic, Dynamic>
76  result(A.rows() + B.rows(), A.cols());
77  result << A, B;
78  return result;
79  }
80 
81  //vector append_row(vector, vector)
82  template <typename T, int R1, int R2>
83  inline Matrix<T, Dynamic, 1>
84  append_row(const Matrix<T, R1, 1> & A,
85  const Matrix<T, R1, 1> & B) {
86  Matrix<T, Dynamic, 1>
87  result(A.size()+B.size());
88  result << A, B;
89  return result;
90  }
91 
92  }
93 }
94 #endif
bool check_size_match(const char *function, T_size1 i, const char *name_i, T_size2 j, const char *name_j, T_result *result)
Matrix< typename return_type< T1, T2 >::type, Dynamic, Dynamic > append_row(const Matrix< T1, R1, C1 > &A, const Matrix< T2, R2, C2 > &B)
Definition: append_row.hpp:23

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