Commit 6dfa3af9 authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

Further developing NormalDistribution classes

parent e9621937
#include "NormalDistribution.hpp"
#define PI 3.14159265358979323846
void NormalDistribution::drawSample(const SampleID &id) {
// Todo Alternative: Ziggurat algorithm
if (internalCounter) {
updateCounter();
return;
}
double randNum1 = randNumberGen->sprng();
double randNum2 = randNumberGen->sprng();
firstSample = sqrt(-2.0 * log(randNum1)) * cos(2.0 * PI * randNum2);
secondSample = sqrt(-2.0 * log(randNum1)) * sin(2.0 * PI * randNum2);
updateCounter();
}
void NormalDistribution::updateCounter() {
internalCounter = internalCounter + 1;
internalCounter = internalCounter % 2;
}
Scalar NormalDistribution::EvalScalarSample() {
if (internalCounter)
return firstSample;
else
return secondSample;
}
......@@ -6,7 +6,6 @@
#include <math.h>
#define PI 3.14159265358979323846
#define SEED 985456376
class NormalDistribution : public SampleGenerator {
......@@ -19,23 +18,9 @@ private:
int internalCounter = 0;
void drawSample(const SampleID &id) override {
// Todo Alternative: Ziggurat algorithm
if (internalCounter) {
updateCounter();
return;
}
double randNum1 = randNumberGen->sprng();
double randNum2 = randNumberGen->sprng();
firstSample = sqrt(-2.0 * log(randNum1)) * cos(2.0 * PI * randNum2);
secondSample = sqrt(-2.0 * log(randNum1)) * sin(2.0 * PI * randNum2);
updateCounter();
}
void drawSample(const SampleID &id) override;
void updateCounter() {
internalCounter = internalCounter + 1;
internalCounter = internalCounter % 2;
};
void updateCounter();;
public:
NormalDistribution(Meshes &meshes,
......@@ -51,12 +36,7 @@ public:
~NormalDistribution() { randNumberGen->free_sprng(); };
Scalar EvalScalarSample() override {
if (internalCounter)
return firstSample;
else
return secondSample;
}
Scalar EvalScalarSample() override;
string Name() const override {
return "NormalDistribution";
......@@ -91,4 +71,66 @@ public:
};
};
class NormalDistributionScalar1DField : public SampleGenerator {
private:
NormalDistribution normalDist;
ScalarField1D sample;
void drawSample(const SampleID &id) override {
for (int i = 0; i < sample.size(); i++) {
normalDist.DrawSample(id);
sample[i] = normalDist.EvalScalarSample();
}
}
public:
NormalDistributionScalar1DField(Meshes &meshes) :
SampleGenerator(meshes),
normalDist(meshes) {}
void Resize(int sizeN) {
sample.resize(sizeN);
}
ScalarField1D EvalScalarField1DSample() override {
return sample;
}
string Name() const override {
return "NormalDistributionScalar1DField";
};
};
class NormalDistributionComplex1DField : public SampleGenerator {
private:
ComplexNormalDistribution normalDist;
ComplexField1D sample;
void drawSample(const SampleID &id) override {
for (int i = 0; i < sample.size(); i++) {
normalDist.DrawSample(id);
sample[i] = normalDist.EvalComplexSample();
}
}
public:
NormalDistributionComplex1DField(Meshes &meshes) :
SampleGenerator(meshes),
normalDist(meshes) {}
void Resize(int sizeN) {
sample.resize(sizeN);
}
ComplexField1D EvalComplexField1DSample() override {
return sample;
}
string Name() const override {
return "NormalDistributionComplex1DField";
};
};
#endif //NORMALDISTRIBUTION_HPP
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment