Stan Math Library  2.20.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, Eigen::Dynamic,
37  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", "rows of A", Arows, "rows of B", Brows);
48 
49  Matrix<typename return_type<T1, T2>::type, Dynamic, Dynamic> result(
50  Arows, Acols + Bcols);
51  for (int j = 0; j < Acols; j++)
52  for (int i = 0; i < Arows; i++)
53  result(i, j) = A(i, j);
54 
55  for (int j = Acols, k = 0; k < Bcols; j++, k++)
56  for (int i = 0; i < Arows; i++)
57  result(i, j) = B(i, k);
58  return result;
59 }
60 
78 template <typename T1, typename T2, int C1, int C2>
79 inline Eigen::Matrix<typename return_type<T1, T2>::type, 1, Eigen::Dynamic>
80 append_col(const Eigen::Matrix<T1, 1, C1>& A,
81  const Eigen::Matrix<T2, 1, C2>& 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> result(Asize + Bsize);
88  for (int i = 0; i < Asize; i++)
89  result(i) = A(i);
90  for (int i = 0, j = Asize; i < Bsize; i++, j++)
91  result(j) = B(i);
92  return result;
93 }
94 
119 template <typename T, int R1, int C1, int R2, int C2>
120 inline Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> append_col(
121  const Eigen::Matrix<T, R1, C1>& A, const Eigen::Matrix<T, R2, C2>& B) {
122  using Eigen::Dynamic;
123  using Eigen::Matrix;
124 
125  check_size_match("append_col", "rows of A", A.rows(), "rows of B", B.rows());
126 
127  Matrix<T, Dynamic, Dynamic> result(A.rows(), A.cols() + B.cols());
128  result << A, B;
129  return result;
130 }
131 
148 template <typename T, int C1, int C2>
149 inline Eigen::Matrix<T, 1, Eigen::Dynamic> append_col(
150  const Eigen::Matrix<T, 1, C1>& A, const Eigen::Matrix<T, 1, C2>& B) {
151  using Eigen::Dynamic;
152  using Eigen::Matrix;
153 
154  Matrix<T, 1, Dynamic> result(A.size() + B.size());
155  result << A, B;
156  return result;
157 }
158 
173 template <typename T1, typename T2, int R, int C>
174 inline Eigen::Matrix<typename return_type<T1, T2>::type, 1, Eigen::Dynamic>
175 append_col(const T1& A, const Eigen::Matrix<T2, R, C>& B) {
176  using Eigen::Dynamic;
177  using Eigen::Matrix;
178  typedef typename return_type<T1, T2>::type return_type;
179 
180  Matrix<return_type, 1, Dynamic> result(B.size() + 1);
181  result << A, B.template cast<return_type>();
182  return result;
183 }
184 
199 template <typename T1, typename T2, int R, int C>
200 inline Eigen::Matrix<typename return_type<T1, T2>::type, 1, Eigen::Dynamic>
201 append_col(const Eigen::Matrix<T1, R, C>& A, const T2& B) {
202  using Eigen::Dynamic;
203  using Eigen::Matrix;
204  typedef typename return_type<T1, T2>::type return_type;
205 
206  Matrix<return_type, 1, Dynamic> result(A.size() + 1);
207  result << A.template cast<return_type>(), B;
208  return result;
209 }
210 } // namespace math
211 
212 } // namespace stan
213 
214 #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
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.
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.