Stan Math Library  2.20.0
reverse mode automatic differentiation
append_array.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_APPEND_ARRAY_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_APPEND_ARRAY_HPP
3 
11 #include <vector>
12 
13 namespace stan {
14 namespace math {
27 template <typename T1, typename T2>
28 inline typename append_return_type<std::vector<T1>, std::vector<T2> >::type
29 append_array(const std::vector<T1>& x, const std::vector<T2>& y) {
30  typename append_return_type<std::vector<T1>, std::vector<T2> >::type z;
31  std::vector<int> zdims;
32  if (x.empty()) {
33  zdims = dims(y);
34  zdims[0] += x.size();
35  } else {
36  zdims = dims(x);
37  zdims[0] += y.size();
38  }
39  resize(z, zdims);
40  for (size_t i = 0; i < x.size(); ++i)
41  assign(z[i], x[i]);
42  for (size_t i = 0; i < y.size(); ++i)
43  assign(z[i + x.size()], y[i]);
44  return z;
45 }
46 
56 template <typename T1>
57 inline std::vector<T1> append_array(const std::vector<T1>& x,
58  const std::vector<T1>& y) {
59  std::vector<T1> z;
60 
61  if (!x.empty() && !y.empty()) {
62  std::vector<int> xdims = dims(x), ydims = dims(y);
63  check_matching_sizes("append_array", "dimension of x", xdims,
64  "dimension of y", ydims);
65  for (size_t i = 1; i < xdims.size(); ++i) {
66  check_size_match("append_array", "shape of x", xdims[i], "shape of y",
67  ydims[i]);
68  }
69  }
70 
71  z.reserve(x.size() + y.size());
72  z.insert(z.end(), x.begin(), x.end());
73  z.insert(z.end(), y.begin(), y.end());
74  return z;
75 }
76 } // namespace math
77 } // namespace stan
78 #endif
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.
void dims(const T &x, std::vector< int > &result)
Definition: dims.hpp:11
void resize(T &x, std::vector< int > dims)
Recursively resize the specified vector of vectors, which must bottom out at scalar values...
Definition: resize.hpp:41
append_return_type< std::vector< T1 >, std::vector< T2 > >::type append_array(const std::vector< T1 > &x, const std::vector< T2 > &y)
Return the concatenation of two specified vectors in the order of the arguments.
void check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Check if two structures at the same size.
This template metaprogram is used to compute the return type for append_array.
void assign(T_lhs &x, const T_rhs &y)
Copy the right-hand side&#39;s value to the left-hand side variable.
Definition: assign.hpp:47

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