1 #ifndef STAN_MATH_PRIM_FUN_ASSIGN_HPP
2 #define STAN_MATH_PRIM_FUN_ASSIGN_HPP
3
4 #include <stan/math/prim/err.hpp>
5 #include <stan/math/prim/fun/Eigen.hpp>
6 #include <iostream>
7 #include <sstream>
8 #include <stdexcept>
9 #include <string>
10 #include <vector>
11
12 namespace stan {
13 namespace math {
14
15 /**
16 * Helper function to return the matrix size as either "dynamic" or "1".
17 *
18 * @tparam N Eigen matrix size specification
19 * @param o output stream
20 */
21 template <int N>
print_mat_size(std::ostream & o)22 inline void print_mat_size(std::ostream& o) {
23 if (N == Eigen::Dynamic) {
24 o << "dynamically sized";
25 } else {
26 o << N;
27 }
28 }
29
30 /**
31 * Copy the right-hand side's value to the left-hand side
32 * variable.
33 *
34 * The <code>assign()</code> function is overloaded. This
35 * instance will match arguments where the right-hand side is
36 * assignable to the left and they are not both
37 * <code>std::vector</code> or <code>Eigen::Matrix</code> types.
38 *
39 * @tparam T_lhs Type of left-hand side.
40 * @tparam T_rhs Type of right-hand side.
41 * @param x Left-hand side.
42 * @param y Right-hand side.
43 */
44 template <typename T_lhs, typename T_rhs,
45 require_all_stan_scalar_t<T_lhs, T_rhs>* = nullptr>
assign(T_lhs & x,const T_rhs & y)46 inline void assign(T_lhs& x, const T_rhs& y) {
47 x = y;
48 }
49
50 /**
51 * Copy the right-hand side's value to the left-hand side
52 * variable.
53 *
54 * The <code>assign()</code> function is overloaded. This
55 * instance will be called for arguments that are both
56 * <code>Eigen::Matrix</code> types.
57 *
58 * @tparam T_lhs type of the left-hand side matrix
59 * @tparam T_rhs type of the right-hand side matrix
60 *
61 * @param x Left-hand side matrix.
62 * @param y Right-hand side matrix.
63 * @throw std::invalid_argument if sizes do not match.
64 */
65 template <typename T_lhs, typename T_rhs,
66 require_all_eigen_t<T_lhs, T_rhs>* = nullptr>
assign(T_lhs && x,const T_rhs & y)67 inline void assign(T_lhs&& x, const T_rhs& y) {
68 check_matching_dims("assign", "left-hand-side", x, "right-hand-side", y);
69 x = y.template cast<value_type_t<T_lhs>>();
70 }
71
72 /**
73 * Copy the right-hand side's value to the left-hand side
74 * variable.
75 *
76 * The <code>assign()</code> function is overloaded. This
77 * instance will be called for arguments that are both
78 * <code>std::vector</code>, and will call <code>assign()</code>
79 * element-by element.
80 *
81 * For example, a <code>std::vector<int></code> can be
82 * assigned to a <code>std::vector<double></code> using this
83 * function.
84 *
85 * @tparam T_lhs type of elements in the left-hand side vector
86 * @tparam T_rhs type of elements in the right-hand side vector
87 * @param x Left-hand side vector.
88 * @param y Right-hand side vector.
89 * @throw std::invalid_argument if sizes do not match.
90 */
91 template <typename T_lhs, typename T_rhs>
assign(std::vector<T_lhs> & x,const std::vector<T_rhs> & y)92 inline void assign(std::vector<T_lhs>& x, const std::vector<T_rhs>& y) {
93 check_matching_sizes("assign", "left-hand side", x, "right-hand side", y);
94 for (size_t i = 0; i < x.size(); ++i) {
95 assign(x[i], y[i]);
96 }
97 }
98
99 } // namespace math
100 } // namespace stan
101
102 #endif
103