Automatic Differentiation
 
Loading...
Searching...
No Matches
append_array.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_APPEND_ARRAY_HPP
2#define STAN_MATH_PRIM_FUN_APPEND_ARRAY_HPP
3
10#include <vector>
11
12namespace stan {
13namespace math {
14
27template <typename T1, typename T2>
28inline typename append_return_type<std::vector<T1>, std::vector<T2> >::type
29append_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 }
43 for (size_t i = 0; i < y.size(); ++i) {
44 assign(z[i + x.size()], y[i]);
45 }
46 return z;
47}
48
58template <typename T1>
59inline std::vector<T1> append_array(const std::vector<T1>& x,
60 const std::vector<T1>& y) {
61 std::vector<T1> z;
62
63 if (!x.empty() && !y.empty()) {
64 std::vector<int> xdims = dims(x), ydims = dims(y);
65 check_matching_sizes("append_array", "dimension of x", xdims,
66 "dimension of y", ydims);
67 for (size_t i = 1; i < xdims.size(); ++i) {
68 check_size_match("append_array", "shape of x", xdims[i], "shape of y",
69 ydims[i]);
70 }
71 }
72
73 z.reserve(x.size() + y.size());
74 z.insert(z.end(), x.begin(), x.end());
75 z.insert(z.end(), y.begin(), y.end());
76 return z;
77}
78
79} // namespace math
80} // namespace stan
81
82#endif
void dims(const T_x &x, std::vector< int > &result)
matrix_cl overload of the dims helper function in prim/fun/dims.hpp.
Definition dims.hpp:21
void assign(T_lhs &x, const T_rhs &y)
Copy the right-hand side's value to the left-hand side variable.
Definition assign.hpp:46
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.
auto append_array(T_x &&x, T_y &&y)
Return the concatenation of two specified vectors in the order of the arguments.
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:43
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
This template metaprogram is used to compute the return type for append_array.