Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
rep_array.hpp
Go to the documentation of this file.
1 #ifndef __STAN__MATH__REP_ARRAY_HPP__
2 #define __STAN__MATH__REP_ARRAY_HPP__
3 
4 #include <stdexcept>
5 #include <vector>
6 
8 
9 namespace stan {
10 
11  namespace math {
12 
13  inline void validate_non_negative_rep(int n, const std::string& fun) {
14  if (n >= 0) return;
15  std::stringstream msg;
16  msg << "error in " << fun
17  << "; number of replications must be positive"
18  << "; found n=" << n;
19  throw std::domain_error(msg.str());
20  }
21 
22  template <typename T>
23  inline std::vector<T>
24  rep_array(const T& x, int n) {
25  validate_non_negative_rep(n,"rep_array 1D");
26  return std::vector<T>(n,x);
27  }
28 
29  template <typename T>
30  inline std::vector<std::vector<T> >
31  rep_array(const T& x, int m, int n) {
32  using std::vector;
33  validate_non_negative_rep(m,"rep_array 2D rows");
34  validate_non_negative_rep(n,"rep_array 2D cols");
35  return vector<vector<T> >(m, vector<T>(n, x));
36  }
37 
38  template <typename T>
39  inline std::vector<std::vector<std::vector<T> > >
40  rep_array(const T& x, int k, int m, int n) {
41  using std::vector;
42  validate_non_negative_rep(k,"rep_array 2D shelfs");
43  validate_non_negative_rep(m,"rep_array 2D rows");
44  validate_non_negative_rep(n,"rep_array 2D cols");
45  return vector<vector<vector<T> > >(k,
46  vector<vector<T> >(m,
47  vector<T>(n, x)));
48  }
49 
50  }
51 }
52 
53 #endif

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