-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNaiveSampler.h
56 lines (45 loc) · 892 Bytes
/
NaiveSampler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* NaiveSampler.h
*
* Created on: Jul 25, 2013
* Author: lucas
*/
#ifndef NAIVESAMPLER_H_
#define NAIVESAMPLER_H_
#include "Sampler.h"
template <typename T>
class NaiveSampler: public Sampler<T>
{
public:
NaiveSampler() {}
virtual ~NaiveSampler() {}
virtual void resample(T* samples, double* weights, int numSamples)
{
T* newSamples = new T[numSamples];
double sumWeights = 0.0;
for(int i=0; i<numSamples; i++)
{
sumWeights += weights[i];
}
for(int i=0; i<numSamples; i++)
{
double rand = 0.0;
double tempSum = weights[0];
for(int j=0; j<numSamples; j++)
{
if(tempSum > rand)
{
newSamples[i] = samples[j];
break;
}
tempSum += weights[j];
}
}
for(int i=0; i<numSamples; i++)
{
samples[i] = newSamples[i];
}
delete[] newSamples;
}
};
#endif /* NAIVESAMPLER_H_ */