halton.cpp 922 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "halton.h"
  2. #include <cassert>
  3. using namespace std;
  4. // compute \phi_p^\pi(i)
  5. double phi(int p, permutation &pi, int i) {
  6. double r = 0., pl = 1./p;
  7. while(i) {
  8. r += (double)pi[i%p]*pl;
  9. pl /= p;
  10. i /= p;
  11. }
  12. return r;
  13. }
  14. halton::halton(int dim, int *p) : dim(dim), p(p) {
  15. assert(dim > 0);
  16. for(int i = 0; i < dim; i++) {
  17. assert(p[i] > 2); // otherwise there is only one permutation : no optimisations are possible
  18. pi.emplace_back(p[i]);
  19. }
  20. }
  21. permutation& halton::get_pi(int i) {
  22. assert(0 <= i && i < dim);
  23. return pi[i];
  24. }
  25. void halton::set_pis(vector<permutation> &pis) {
  26. assert((int)pis.size() == dim);
  27. for(int i = 0; i < dim; i++)
  28. assert(pis[i].get_size() == p[i]);
  29. pi = pis;
  30. }
  31. void halton::compute(int i, double* res) {
  32. for(int d = 0; d < dim; d++) {
  33. res[d] = phi(p[d], pi[d], i);
  34. }
  35. }
  36. int halton::get_p(int i) {
  37. assert(0 <= i && i < dim);
  38. return p[i];
  39. }