Latin Hypercube Samples (lhs)  1.0
R, C++, and Rcpp code to generate Latin hypercube samples
All Classes Namespaces Files Functions Variables Typedefs Macros Pages
lhs_r_utilities.h
Go to the documentation of this file.
1 
21 #ifndef LHS_R_UTILITIES_H
22 #define LHS_R_UTILITIES_H
23 
24 #include <Rcpp.h>
25 #include "LHSCommonDefines.h"
26 
30 namespace lhs_r
31 {
37  void findorder_zero(const Rcpp::NumericVector & v, Rcpp::IntegerVector & order);
43  Rcpp::NumericMatrix convertIntegerToNumericLhs(const bclib::matrix<int> & intMat);
49  Rcpp::NumericMatrix convertMatrixToNumericLhs(const bclib::matrix<double> & intMat);
55  Rcpp::NumericMatrix convertIntegerToNumericLhs(const Rcpp::IntegerMatrix & intMat);
63  Rcpp::IntegerVector runifint(unsigned int n, int min_int, int max_int);
69  void checkArguments(int n, int k);
76  void checkArguments(int n, int k, int dup);
84  void checkArguments(int n, int k, int maxsweeps, double eps);
91  Rcpp::NumericMatrix degenerateCase(int k, bclib::CRandom<double> & oRandom);
92 
99  template <int RTYPE>
100  Rcpp::NumericMatrix calculateDistance(Rcpp::Matrix<RTYPE> & mat) // non-const because of the matrix row
101  {
102  Rcpp::NumericMatrix result(mat.rows(), mat.cols());
103  for (int i = 0; i < mat.rows() - 1; i++)
104  {
105  for (int j = i+1; j < mat.rows(); j++)
106  {
107  typename Rcpp::Matrix<RTYPE>::Row rowi = mat.row(i);
108  typename Rcpp::Matrix<RTYPE>::Row rowj = mat.row(j);
109  double sum = static_cast<double>(Rcpp::sum((rowi - rowj) * (rowi - rowj)));
110  result(i,j) = sqrt(sum);
111  }
112  }
113  return result;
114  }
115 
122  template <int RTYPE>
123  double calculateSOptimal(Rcpp::Matrix<RTYPE> & mat)
124  {
125  // B[i] <- 1/sum(1/dist(A[, , i]))
126  Rcpp::NumericMatrix dist = lhs_r::calculateDistance<RTYPE>(mat);
127  Rcpp::NumericMatrix::iterator i;
128  for (i = dist.begin(); i != dist.end(); ++i)
129  {
130  if (*i != 0.0)
131  {
132  *i = 1.0 / *i;
133  }
134  }
135  double sum = std::accumulate<Rcpp::NumericMatrix::iterator>(dist.begin(), dist.end(), 0.0);
136  if (sum > 0)
137  {
138  return 1.0 / sum;
139  }
140  else
141  {
142  throw std::runtime_error("problem with calculateSOptimal");
143  }
144  }
145 }
146 
147 #endif /* LHS_R_UTILITIES_H */
148 
lhs_r::runifint
Rcpp::IntegerVector runifint(unsigned int n, int min_int, int max_int)
Definition: lhs_r_utilities.cpp:97
LHSCommonDefines.h
lhs_r::degenerateCase
Rcpp::NumericMatrix degenerateCase(int k, bclib::CRandom< double > &oRandom)
Definition: lhs_r_utilities.cpp:169
lhs_r::findorder_zero
void findorder_zero(const Rcpp::NumericVector &v, Rcpp::IntegerVector &order)
Definition: lhs_r_utilities.cpp:25
lhs_r::calculateSOptimal
double calculateSOptimal(Rcpp::Matrix< RTYPE > &mat)
Definition: lhs_r_utilities.h:123
lhs_r::calculateDistance
Rcpp::NumericMatrix calculateDistance(Rcpp::Matrix< RTYPE > &mat)
Definition: lhs_r_utilities.h:100
lhs_r::checkArguments
void checkArguments(int n, int k)
Definition: lhs_r_utilities.cpp:112
lhs_r::convertMatrixToNumericLhs
Rcpp::NumericMatrix convertMatrixToNumericLhs(const bclib::matrix< double > &intMat)
Definition: lhs_r_utilities.cpp:78
bclib::CRandom< double >
lhs_r
Definition: lhs_r_utilities.cpp:24
lhs_r::convertIntegerToNumericLhs
Rcpp::NumericMatrix convertIntegerToNumericLhs(const bclib::matrix< int > &intMat)
Definition: lhs_r_utilities.cpp:34