Stan Math Library  2.12.0
reverse mode automatic differentiation
append_col.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_APPEND_COL_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_APPEND_COL_HPP
3 
7 #include <vector>
8 
9 namespace stan {
10  namespace math {
11 
35  template <typename T1, typename T2, int R1, int C1, int R2, int C2>
36  inline Eigen::Matrix<typename return_type<T1, T2>::type,
37  Eigen::Dynamic, Eigen::Dynamic>
38  append_col(const Eigen::Matrix<T1, R1, C1>& A,
39  const Eigen::Matrix<T2, R2, C2>& B) {
40  using Eigen::Dynamic;
41  using Eigen::Matrix;
42 
43  int Arows = A.rows();
44  int Brows = B.rows();
45  int Acols = A.cols();
46  int Bcols = B.cols();
47  check_size_match("append_col",
48  "rows of A", Arows,
49  "rows of B", Brows);
50 
51  Matrix<typename return_type<T1, T2>::type, Dynamic, Dynamic>
52  result(Arows, Acols+Bcols);
53  for (int j = 0; j < Acols; j++)
54  for (int i = 0; i < Arows; i++)
55  result(i, j) = A(i, j);
56 
57  for (int j = Acols, k = 0; k < Bcols; j++, k++)
58  for (int i = 0; i < Arows; i++)
59  result(i, j) = B(i, k);
60  return result;
61  }
62 
80  template <typename T1, typename T2, int C1, int C2>
81  inline Eigen::Matrix<typename return_type<T1, T2>::type,
82  1, Eigen::Dynamic>
83  append_col(const Eigen::Matrix<T1, 1, C1>& A,
84  const Eigen::Matrix<T2, 1, C2>& B) {
85  using Eigen::Dynamic;
86  using Eigen::Matrix;
87 
88  int Asize = A.size();
89  int Bsize = B.size();
90  Matrix<typename return_type<T1, T2>::type, 1, Dynamic>
91  result(Asize + Bsize);
92  for (int i = 0; i < Asize; i++)
93  result(i) = A(i);
94  for (int i = 0, j = Asize; i < Bsize; i++, j++)
95  result(j) = B(i);
96  return result;
97  }
98 
123  template <typename T, int R1, int C1, int R2, int C2>
124  inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
125  append_col(const Eigen::Matrix<T, R1, C1>& A,
126  const Eigen::Matrix<T, R2, C2>& B) {
127  using Eigen::Matrix;
128  using Eigen::Dynamic;
129 
130  check_size_match("append_col",
131  "rows of A", A.rows(),
132  "rows of B", B.rows());
133 
134  Matrix<T, Dynamic, Dynamic> result(A.rows(), A.cols()+B.cols());
135  result << A, B;
136  return result;
137  }
138 
155  template <typename T, int C1, int C2>
156  inline Eigen::Matrix<T, 1, Eigen::Dynamic>
157  append_col(const Eigen::Matrix<T, 1, C1>& A,
158  const Eigen::Matrix<T, 1, C2>& B) {
159  using Eigen::Matrix;
160  using Eigen::Dynamic;
161 
162  Matrix<T, 1, Dynamic> result(A.size()+B.size());
163  result << A, B;
164  return result;
165  }
166 
181  template <typename T1, typename T2, int R, int C>
182  inline Eigen::Matrix<typename return_type<T1, T2>::type,
183  1, Eigen::Dynamic>
184  append_col(const T1& A,
185  const Eigen::Matrix<T2, R, C>& B) {
186  using Eigen::Dynamic;
187  using Eigen::Matrix;
188  typedef typename return_type<T1, T2>::type return_type;
189 
190  Matrix<return_type, 1, Dynamic>
191  result(B.size() + 1);
192  result << A, B.template cast<return_type>();
193  return result;
194  }
195 
210  template <typename T1, typename T2, int R, int C>
211  inline Eigen::Matrix<typename return_type<T1, T2>::type,
212  1, Eigen::Dynamic>
213  append_col(const Eigen::Matrix<T1, R, C>& A,
214  const T2& B) {
215  using Eigen::Dynamic;
216  using Eigen::Matrix;
217  typedef typename return_type<T1, T2>::type return_type;
218 
219  Matrix<return_type, 1, Dynamic>
220  result(A.size() + 1);
221  result << A.template cast<return_type>(), B;
222  return result;
223  }
224  }
225 
226 }
227 
228 #endif
Eigen::Matrix< typename return_type< T1, T2 >::type, Eigen::Dynamic, Eigen::Dynamic > append_col(const Eigen::Matrix< T1, R1, C1 > &A, const Eigen::Matrix< T2, R2, C2 > &B)
Return the result of appending the second argument matrix after the first argument matrix...
Definition: append_col.hpp:38
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
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.