Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
append_col.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__MATRIX__APPEND__COL_HPP
2 #define STAN__MATH__MATRIX__APPEND__COL_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_col(matrix, matrix)
18  //matrix append_col(matrix, vector)
19  //matrix append_col(vector, matrix)
20  //matrix append_col(vector, 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_col(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_col(%1%)",
30  Arows, "rows of A",
31  Brows, "rows of B",
32  (double*)0);
33 
34  Matrix<typename return_type<T1, T2>::type, Dynamic, Dynamic>
35  result(Arows, Acols+Bcols);
36  for (int j = 0; j < Acols; j++)
37  for (int i = 0; i < Arows; i++)
38  result(i, j) = A(i, j);
39 
40  for (int j = Acols, k = 0; k < Bcols; j++, k++)
41  for (int i = 0; i < Arows; i++)
42  result(i, j) = B(i, k);
43  return result;
44  }
45 
46  //row_vector append_col(row_vector, row_vector)
47  template <typename T1, typename T2, int C1, int C2>
48  inline Matrix<typename return_type<T1, T2>::type, 1, Dynamic>
49  append_col(const Matrix<T1, 1, C1> & A,
50  const Matrix<T2, 1, C2> & 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 
63  //matrix append_col(matrix, matrix)
64  //matrix append_col(matrix, vector)
65  //matrix append_col(vector, matrix)
66  //matrix append_col(vector, vector)
67  template <typename T, int R1, int C1, int R2, int C2>
68  inline Matrix<T, Dynamic, Dynamic>
69  append_col(const Matrix<T, R1, C1> & A,
70  const Matrix<T, R2, C2> & B) {
71  check_size_match("append_col(%1%)",
72  A.rows(), "rows of A",
73  B.rows(), "rows of B",
74  (double*)0);
75 
76  Matrix<T, Dynamic, Dynamic>
77  result(A.rows(), A.cols()+B.cols());
78  result << A, B;
79  return result;
80  }
81 
82  //row_vector append_col(row_vector, row_vector)
83  template <typename T, int C1, int C2>
84  inline Matrix<T, 1, Dynamic>
85  append_col(const Matrix<T, 1, C1> & A,
86  const Matrix<T, 1, C2> & B) {
87  Matrix<T, 1, Dynamic>
88  result(A.size()+B.size());
89  result << A, B;
90  return result;
91  }
92 
93  }
94 }
95 #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_col(const Matrix< T1, R1, C1 > &A, const Matrix< T2, R2, C2 > &B)
Definition: append_col.hpp:23

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