Orthogonal Array Library  1.0.0
Libray for generating orthogonal arrays based on Art Owen's oa library
All Classes Namespaces Files Functions Variables Typedefs Macros Pages
rutils.h
Go to the documentation of this file.
1 
31 #ifndef RUTILS_H
32 #define RUTILS_H
33 
34 #include "OACommonDefines.h"
35 #include "runif.h"
36 
37 namespace oacpp
38 {
39  /*
40  Namespace for utilities based on runif
41  */
42  namespace rutils
43  {
52  void unifperm( std::vector<int> & pi, int q, RUnif & randomClass );
53 
63  template <class T>
64  void findranks_slow_zero(const std::vector<T> & v, std::vector<int> & indx)
65  {
66  if (indx.size() != v.size())
67  {
68  indx.resize(v.size());
69  }
70  for (size_t i = 0; i < v.size(); i++)
71  {
72  indx[i] = static_cast<int>(i);
73  }
74  std::vector<T> vsort(v);
75  std::sort<typename std::vector<T>::iterator>(vsort.begin(), vsort.end());
76 
77  for (size_t i = 0; i < v.size(); i++)
78  {
79  indx[i] = static_cast<int>(std::find(vsort.begin(), vsort.end(), v[i]) - vsort.begin());
80  }
81  }
82 
92  template <class T>
93  void findranks_slow(const std::vector<T> & v, std::vector<int> & indx)
94  {
95  findranks_slow_zero(v, indx);
96  for (size_t i = 0; i < indx.size(); i++)
97  {
98  indx[i] += 1;
99  }
100  }
101 
108  template <class T>
109  bool findranksCompare(const std::pair<T, int> & first, const std::pair<T, int> & second)
110  {
111  return (first.first < second.first);
112  }
113 
120  template <class T>
121  void findranks_zero(const std::vector<T> & v, std::vector<int> & rank)
122  {
123  // create a vector of pairs to hold the value and the integer rank
124  std::vector<std::pair<T, int> > p(v.size());
125  std::vector<int> temp(p.size());
126  for (size_t i = 0; i < v.size(); i++)
127  {
128  p[i] = std::pair<T, int>(v[i], static_cast<int>(i));
129  }
130 
131  // if the rank vector is not the right size, resize it (the original values may be lost)
132  if (rank.size() != v.size())
133  {
134  rank.resize(v.size());
135  }
136 
137  // sort the pairs of values
138  std::sort(p.begin(), p.end(), findranksCompare<double>);
139 
140  // take the ranks from the pairs and put them in the rank vector
141  for (size_t i = 0; i < v.size(); i++)
142  {
143  rank[p[i].second] = static_cast<int>(i);
144  }
145  }
146 
153  template <class T>
154  void findranks(const std::vector<T> & v, std::vector<int> & rank)
155  {
156  findranks_zero(v, rank);
157  for (size_t i = 0; i < rank.size(); i++)
158  {
159  rank[i] += 1;
160  }
161  }
162  } // end namespace
163 } // end namespace
164 
165 #endif
runif.h
oacpp
Orthogonal Array Namespace.
Definition: ak.h:39
oacpp::rutils::findranks_slow
void findranks_slow(const std::vector< T > &v, std::vector< int > &indx)
Find the rank of each vector element.
Definition: rutils.h:93
oacpp::rutils::findranks_zero
void findranks_zero(const std::vector< T > &v, std::vector< int > &rank)
Find the rank of each vector element (zero based)
Definition: rutils.h:121
oacpp::rutils::unifperm
void unifperm(std::vector< int > &pi, int q, RUnif &randomClass)
In S one just does rank(runif(q)).
Definition: rutils.cpp:37
OACommonDefines.h
oacpp::rutils::findranks
void findranks(const std::vector< T > &v, std::vector< int > &rank)
Find the rank of each vector element.
Definition: rutils.h:154
oacpp::rutils::findranks_slow_zero
void findranks_slow_zero(const std::vector< T > &v, std::vector< int > &indx)
Find the rank of each vector element (zero based)
Definition: rutils.h:64
oacpp::rutils::findranksCompare
bool findranksCompare(const std::pair< T, int > &first, const std::pair< T, int > &second)
Comparison operator to use in the findranks method.
Definition: rutils.h:109