Fermat
latin_hypercube_inline.h
1 /*
2  * Copyright (c) 2010-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 namespace cugar {
29 
30 // get a set of stratified samples
31 template <typename T, uint32 DIM>
33  const uint32 num_samples,
34  Vector<T, DIM>* samples)
35 {
36  const T inv = T(1.0) / T(num_samples);
37 
38  for (uint32 i = 0; i < num_samples; ++i)
39  {
40  for (uint32 d = 0; d < DIM; ++d)
41  samples[i][d] = (T(i) + m_random.next()) * inv;
42  }
43 
44  for (uint32 d = 0; d < DIM; ++d)
45  {
46  for (uint32 i = 0; i < num_samples; i++)
47  {
48  const float r = m_random.next();
49  const uint32 j = min(uint32(float(r) * (num_samples - i)) + i, num_samples - 1u);
50  std::swap(samples[i][d], samples[j][d]);
51  }
52  }
53 }
54 
55 // get a set of stratified samples
56 template <bool SOA, typename T>
58  const uint32 num_samples,
59  const uint32 num_dims,
60  T* samples)
61 {
62  const T inv = T(1.0) / T(num_samples);
63 
64  for (uint32 i = 0; i < num_samples; ++i)
65  {
66  for (uint32 d = 0; d < num_dims; ++d)
67  samples[SOA ? i + d*num_samples : i * num_dims + d] = (T(i) + m_random.next()) * inv;
68  }
69 
70  for (uint32 d = 0; d < num_dims; ++d)
71  {
72  for (uint32 i = 0; i < num_samples; i++)
73  {
74  const float r = m_random.next();
75  const uint32 j = min(uint32(float(r) * (num_samples - i)) + i, num_samples - 1u);
76  std::swap(samples[SOA ? i + d*num_samples : i * num_dims + d], samples[SOA ? j + d*num_samples : j * num_dims + d]);
77  }
78  }
79 }
80 
81 } // namespace cugar
void sample(const uint32 n_samples, Vector< T, DIM > *samples)
Definition: latin_hypercube_inline.h:32
Definition: vector.h:54
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38