Stan Math Library  2.8.0
reverse mode automatic differentiation
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
assign.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_ASSIGN_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_ASSIGN_HPP
3 
9 #include <iostream>
10 #include <sstream>
11 #include <stdexcept>
12 #include <string>
13 #include <vector>
14 
15 namespace stan {
16 
17  namespace math {
18 
26  void print_mat_size(int n, std::ostream& o) {
27  if (n == Eigen::Dynamic)
28  o << "dynamically sized";
29  else
30  o << n;
31  }
32 
33  // Recursive assignment with size match checking and promotion
34 
49  template <typename LHS, typename RHS>
50  inline void
51  assign(LHS& lhs, const RHS& rhs) {
52  lhs = rhs;
53  }
54 
75  template <typename LHS, typename RHS, int R1, int C1, int R2, int C2>
76  inline void
77  assign(Eigen::Matrix<LHS, R1, C1>& x,
78  const Eigen::Matrix<RHS, R2, C2>& y) {
79  std::stringstream ss;
80  ss << "shapes must match, but found"
81  << " left-hand side rows=";
82  print_mat_size(R1, ss);
83  ss << "; left-hand side cols=";
84  print_mat_size(C1, ss);
85  ss << "; right-hand side rows=";
86  print_mat_size(R2, ss);
87  ss << "; right-hand side cols=";
88  print_mat_size(C2, ss);
89  std::string ss_str(ss.str());
90  invalid_argument("assign(Eigen::Matrix, Eigen::Matrix)",
91  "", "", ss_str.c_str());
92  }
93 
111  template <typename LHS, typename RHS, int R, int C>
112  inline void
113  assign(Eigen::Matrix<LHS, R, C>& x,
114  const Eigen::Matrix<RHS, R, C>& y) {
116  "left-hand-side", x,
117  "right-hand-side", y);
118  for (int i = 0; i < x.size(); ++i)
119  assign(x(i), y(i));
120  }
121 
140  template <typename LHS, typename RHS, int R, int C>
141  inline void
142  assign(Eigen::Block<LHS> x,
143  const Eigen::Matrix<RHS, R, C>& y) {
145  "left-hand side rows", x.rows(),
146  "right-hand side rows", y.rows());
148  "left-hand side cols", x.cols(),
149  "right-hand side cols", y.cols());
150  for (int n = 0; n < y.cols(); ++n)
151  for (int m = 0; m < y.rows(); ++m)
152  assign(x(m, n), y(m, n));
153  }
154 
155 
175  template <typename LHS, typename RHS>
176  inline void
177  assign(std::vector<LHS>& x, const std::vector<RHS>& y) {
179  "left-hand side", x,
180  "right-hand side", y);
181  for (size_t i = 0; i < x.size(); ++i)
182  assign(x[i], y[i]);
183  }
184 
185  }
186 }
187 #endif
bool check_matching_dims(const char *function, const char *name1, const Eigen::Matrix< T1, R1, C1 > &y1, const char *name2, const Eigen::Matrix< T2, R2, C2 > &y2)
Return true if the two matrices are of the same size.
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw an invalid_argument exception with a consistently formatted message.
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.
bool check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Return true if two structures at the same size.
void assign(LHS &lhs, const RHS &rhs)
Copy the right-hand side's value to the left-hand side variable.
Definition: assign.hpp:51
void print_mat_size(int n, std::ostream &o)
Helper function to return the matrix size as either "dynamic" or "1".
Definition: assign.hpp:26

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