Stan  2.14.0
probability, sampling & optimization
lvalue.hpp
Go to the documentation of this file.
1 #ifndef STAN_MODEL_INDEXING_LVALUE_HPP
2 #define STAN_MODEL_INDEXING_LVALUE_HPP
3 
4 #include <boost/utility/enable_if.hpp>
5 #include <boost/type_traits/is_same.hpp>
6 #include <Eigen/Dense>
7 #include <stan/math/prim/mat.hpp>
12 #include <vector>
13 
14 namespace stan {
15 
16  namespace model {
17 
31  template <typename T, typename U>
32  inline void assign(T& x, const nil_index_list& /* idxs */, const U& y,
33  const char* name = "ANON", int depth = 0) {
34  x = y;
35  }
36 
37  template <typename T, typename U, int R, int C>
38  inline void assign(Eigen::Matrix<T, R, C>& x,
39  const nil_index_list& /* idxs */,
40  const Eigen::Matrix<U, R, C>& y,
41  const char* name = "ANON",
42  int depth = 0) {
43  x.resize(y.rows(), y.cols());
44  for (size_t i = 0; i < y.size(); ++i)
45  assign(x(i), nil_index_list(), y(i), name, depth + 1);
46  }
47 
48 
49  template <typename T, typename U>
50  inline void assign(std::vector<T>& x, const nil_index_list& /* idxs */,
51  const std::vector<U>& y, const char* name = "ANON",
52  int depth = 0) {
53  x.resize(y.size());
54  for (size_t i = 0; i < y.size(); ++i)
55  assign(x[i], nil_index_list(), y[i], name, depth + 1);
56  }
57 
58 
74  template <typename T, typename U>
75  inline void assign(Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
77  const U& y,
78  const char* name = "ANON", int depth = 0) {
79  int i = idxs.head_.n_;
80  math::check_range("vector[uni] assign range", name, x.size(), i);
81  x(i - 1) = y;
82  }
83 
99  template <typename T, typename U>
100  inline void assign(Eigen::Matrix<T, 1, Eigen::Dynamic>& x,
102  const U& y,
103  const char* name = "ANON", int depth = 0) {
104  int i = idxs.head_.n_;
105  math::check_range("row_vector[uni] assign range", name, x.size(), i);
106  x(i - 1) = y;
107  }
108 
127  template <typename T, typename I, typename U>
128  inline typename boost::disable_if<boost::is_same<I, index_uni>, void>::type
129  assign(Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
131  const Eigen::Matrix<U, Eigen::Dynamic, 1>& y,
132  const char* name = "ANON", int depth = 0) {
133  math::check_size_match("vector[multi] assign sizes",
134  "lhs", rvalue_index_size(idxs.head_, x.size()),
135  name, y.size());
136  for (int n = 0; n < y.size(); ++n) {
137  int i = rvalue_at(n, idxs.head_);
138  math::check_range("vector[multi] assign range", name, x.size(), i);
139  x(i - 1) = y(n);
140  }
141  }
142 
162  template <typename T, typename I, typename U>
163  inline typename boost::disable_if<boost::is_same<I, index_uni>, void>::type
164  assign(Eigen::Matrix<T, 1, Eigen::Dynamic>& x,
166  const Eigen::Matrix<U, 1, Eigen::Dynamic>& y,
167  const char* name = "ANON", int depth = 0) {
168  math::check_size_match("row_vector[multi] assign sizes",
169  "lhs", rvalue_index_size(idxs.head_, x.size()),
170  name, y.size());
171  for (int n = 0; n < y.size(); ++n) {
172  int i = rvalue_at(n, idxs.head_);
173  math::check_range("row_vector[multi] assign range", name, x.size(), i);
174  x(i - 1) = y(n);
175  }
176  }
177 
196  template <typename T, typename U>
197  void assign(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& x,
199  const Eigen::Matrix<U, 1, Eigen::Dynamic>& y,
200  const char* name = "ANON", int depth = 0) {
201  math::check_size_match("matrix[uni] assign sizes",
202  "lhs", x.cols(),
203  name, y.cols());
204  int i = idxs.head_.n_;
205  math::check_range("matrix[uni] assign range", name, x.rows(), i);
206  for (int j = 0; j < x.cols(); ++j) // loop allows double to var assgn
207  x(i - 1, j) = y(j);
208  }
209 
228  template <typename T, typename I, typename U>
229  inline typename boost::disable_if<boost::is_same<I, index_uni>, void>::type
230  assign(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& x,
232  const Eigen::Matrix<U, Eigen::Dynamic, Eigen::Dynamic>& y,
233  const char* name = "ANON", int depth = 0) {
234  int x_idx_rows = rvalue_index_size(idxs.head_, x.rows());
235  math::check_size_match("matrix[multi] assign row sizes",
236  "lhs", x_idx_rows,
237  name, y.rows());
238  math::check_size_match("matrix[multi] assign col sizes",
239  "lhs", x.cols(),
240  name, y.cols());
241  for (int i = 0; i < y.rows(); ++i) {
242  int m = rvalue_at(i, idxs.head_);
243  math::check_range("matrix[multi] assign range", name, x.rows(), m);
244  // recurse to allow double to var assign
245  for (int j = 0; j < x.cols(); ++j)
246  x(m - 1, j) = y(i, j);
247  }
248  }
249 
265  template <typename T, typename U>
266  void assign(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& x,
269  nil_index_list> >& idxs,
270  const U& y,
271  const char* name = "ANON", int depth = 0) {
272  int m = idxs.head_.n_;
273  int n = idxs.tail_.head_.n_;
274  math::check_range("matrix[uni,uni] assign range", name, x.rows(), m);
275  math::check_range("matrix[uni,uni] assign range", name, x.cols(), n);
276  x(m - 1, n - 1) = y;
277  }
278 
298  template <typename T, typename I, typename U>
299  inline typename boost::disable_if<boost::is_same<I, index_uni>, void>::type
300  assign(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& x,
303  const Eigen::Matrix<U, 1, Eigen::Dynamic>& y,
304  const char* name = "ANON", int depth = 0) {
305  int x_idxs_cols = rvalue_index_size(idxs.tail_.head_, x.cols());
306  math::check_size_match("matrix[uni,multi] assign sizes",
307  "lhs", x_idxs_cols,
308  name, y.cols());
309  int m = idxs.head_.n_;
310  math::check_range("matrix[uni,multi] assign range", name, x.rows(), m);
311  for (int i = 0; i < y.size(); ++i) {
312  int n = rvalue_at(i, idxs.tail_.head_);
313  math::check_range("matrix[uni,multi] assign range", name, x.cols(), n);
314  x(m - 1, n - 1) = y(i);
315  }
316  }
317 
336  template <typename T, typename I, typename U>
337  inline typename boost::disable_if<boost::is_same<I, index_uni>, void>::type
338  assign(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& x,
339  const cons_index_list<I,
341  nil_index_list> >& idxs,
342  const Eigen::Matrix<U, Eigen::Dynamic, 1>& y,
343  const char* name = "ANON", int depth = 0) {
344  int x_idxs_rows = rvalue_index_size(idxs.head_, x.rows());
345  math::check_size_match("matrix[multi,uni] assign sizes",
346  "lhs", x_idxs_rows,
347  name, y.rows());
348  int n = idxs.tail_.head_.n_;
349  math::check_range("matrix[multi,uni] assign range", name, x.cols(), n);
350  for (int i = 0; i < y.size(); ++i) {
351  int m = rvalue_at(i, idxs.head_);
352  math::check_range("matrix[multi,uni] assign range", name, x.rows(), m);
353  x(m - 1, n - 1) = y(i);
354  }
355  }
356 
376  template <typename T, typename I1, typename I2, typename U>
377  inline typename
378  boost::disable_if_c<boost::is_same<I1, index_uni>::value
379  || boost::is_same<I2, index_uni>::value, void>::type
380  assign(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& x,
381  const cons_index_list<I1,
383  const Eigen::Matrix<U, Eigen::Dynamic, Eigen::Dynamic>& y,
384  const char* name = "ANON", int depth = 0) {
385  int x_idxs_rows = rvalue_index_size(idxs.head_, x.rows());
386  int x_idxs_cols = rvalue_index_size(idxs.tail_.head_, x.cols());
387  math::check_size_match("matrix[multi,multi] assign sizes",
388  "lhs", x_idxs_rows,
389  name, y.rows());
390  math::check_size_match("matrix[multi,multi] assign sizes",
391  "lhs", x_idxs_cols,
392  name, y.cols());
393  for (int j = 0; j < y.cols(); ++j) {
394  int n = rvalue_at(j, idxs.tail_.head_);
395  math::check_range("matrix[multi,multi] assign range", name,
396  x.cols(), n);
397  for (int i = 0; i < y.rows(); ++i) {
398  int m = rvalue_at(i, idxs.head_);
399  math::check_range("matrix[multi,multi] assign range", name,
400  x.rows(), m);
401  x(m - 1, n - 1) = y(i, j);
402  }
403  }
404  }
405 
429  template <typename T, typename L, typename U>
430  inline void assign(std::vector<T>& x,
431  const cons_index_list<index_uni, L>& idxs, const U& y,
432  const char* name = "ANON", int depth = 0) {
433  int i = idxs.head_.n_;
434  math::check_range("vector[uni,...] assign range", name, x.size(), i);
435  assign(x[i - 1], idxs.tail_, y, name, depth + 1);
436  }
437 
463  template <typename T, typename I, typename L, typename U>
464  typename boost::disable_if<boost::is_same<I, index_uni>, void>::type
465  inline assign(std::vector<T>& x, const cons_index_list<I, L>& idxs,
466  const std::vector<U>& y,
467  const char* name = "ANON", int depth = 0) {
468  int x_idx_size = rvalue_index_size(idxs.head_, x.size());
469  math::check_size_match("vector[multi,...] assign sizes",
470  "lhs", x_idx_size,
471  name, y.size());
472  for (size_t n = 0; n < y.size(); ++n) {
473  int i = rvalue_at(n, idxs.head_);
474  math::check_range("vector[multi,...] assign range", name, x.size(), i);
475  assign(x[i - 1], idxs.tail_, y[n], name, depth + 1);
476  }
477  }
478 
479  }
480 }
481 #endif
int rvalue_index_size(const index_multi &idx, int size)
Return size of specified multi-index.
Probability, optimization and sampling library.
int rvalue_at(int n, const index_multi &idx)
Return the index in the underlying array corresponding to the specified position in the specified mul...
Definition: rvalue_at.hpp:21
void assign(T &x, const nil_index_list &, const U &y, const char *name="ANON", int depth=0)
Assign the specified scalar reference under the specified indexing to the specified scalar value...
Definition: lvalue.hpp:32
Template structure for an index list consisting of a head and tail index.
Definition: index_list.hpp:23
Structure for an indexing consisting of a single index.
Definition: index.hpp:17
Structure for an empty (size zero) index list.
Definition: index_list.hpp:11

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