Commit 8f2c957a authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

implementing and testing uniformly distributed vectors and matrix

parent bbff5a4a
Pipeline #147217 passed with stages
in 48 minutes and 24 seconds
......@@ -40,17 +40,12 @@ private:
};
public:
UniformDistributionReal(Meshes &meshes,
double a = 0.0,
double b = 1.0,
int streamID = 0,
int numStreams = 1,
int gType = 0,
int seed = SEED) : a(a), b(b),
SampleGenerator(meshes) {
UniformDistributionReal(Meshes &meshes, double a, double b,
int streamID = 0, int numStreams = 1,
int gType = 0, int seed = SEED) :
a(a), b(b), SampleGenerator(meshes) {
this->randNumberGen = SelectType(gType);
this->randNumberGen->init_sprng(streamID, numStreams,
seed, SPRNG_DEFAULT);
this->randNumberGen->init_sprng(streamID, numStreams, seed, SPRNG_DEFAULT);
}
~UniformDistributionReal() { randNumberGen->free_sprng(); };
......@@ -67,4 +62,78 @@ public:
};
};
class UniformDistributionComplex : public SampleGenerator<Complex> {};
class UniformDistributionRVector : public SampleGenerator<RVector> {
private:
UniformDistributionReal uniformDist;
RVector sample;
void drawSample(const SampleID &id) override {
for (int i = 0; i < sample.size(); i++)
sample[i] = uniformDist.DrawAndEvalSample(id);
}
public:
UniformDistributionRVector(Meshes &meshes, int size, double a, double b) :
SampleGenerator(meshes),
uniformDist(meshes, a, b) {
if (size != -1)
sample.resize(size);
else
sample.resize(meshes.fine().CellCountGeometry());
}
void Resize(int sizeN) {
sample.resize(sizeN);
}
RVector EvalSample() override {
return sample;
}
string Name() const override {
return "UniformDistributionRVector";
};
};
class UniformDistributionCVector : public SampleGenerator<Complex> {};
class UniformDistributionRMatrix : public SampleGenerator<RMatrix> {
private:
UniformDistributionRVector uniformDist;
RMatrix sample;
void drawSample(const SampleID &id) override {
for (int i = 0; i < sample.rows(); i++)
sample.InsertRow(uniformDist.DrawAndEvalSample(id), i);
}
public:
UniformDistributionRMatrix(Meshes &meshes, int rows, int cols, double a, double b) :
SampleGenerator(meshes),
uniformDist(meshes, cols, a, b) {
if (rows != -1)
sample.resize(rows, cols);
else
sample.resize(meshes.fine().CellCountGeometry());
}
void Resize(int rows = -1, int cols = -1) {
sample.resize(rows, cols);
}
RMatrix EvalSample() override {
return sample;
}
string Name() const override {
return "UniformDistributionRMatrix";
};
};
class UniformDistributionCMatrix : public SampleGenerator<RMatrix> {};
#endif //UNIFORMDISTRIBUTION_HPP
......@@ -30,10 +30,7 @@ TEST_F(TestUniformDistribution, UniformDistributionReal) {
double b = 1.0;
UniformDistributionReal uniformDist(*meshes, a, b);
RVector results;
for (auto &id : sampleIDs) {
uniformDist.DrawSample(id);
results.push_back(uniformDist.EvalSample());
}
for (auto &id : sampleIDs) results.push_back(uniformDist.DrawAndEvalSample(id));
double mean = results.Mean();
double var = results.Variance();
EXPECT_NEAR(a + b / 2, mean, sqrt(1.0 / numSamples));
......@@ -45,67 +42,48 @@ TEST_F(TestUniformDistribution, UniformDistributionReal2) {
double b = 1.0;
UniformDistributionReal uniformDist(*meshes, a, b);
RVector results;
for (auto &id : sampleIDs) {
uniformDist.DrawSample(id);
results.push_back(uniformDist.EvalSample());
}
for (auto &id : sampleIDs) results.push_back(uniformDist.DrawAndEvalSample(id));
double mean = results.Mean();
double var = results.Variance();
EXPECT_NEAR((a + b) / 2.0, mean, sqrt(1.0 / numSamples));
EXPECT_NEAR(pow(b - a, 2) / 12.0, var, sqrt(1.0 / numSamples));
}
//TEST_F(TestUniformDistribution, ComplexNormalDistribution) {
// NormalDistributionComplex normalDist(*meshes);
// CVector results;
// for (auto &id : sampleIDs) {
// normalDist.DrawSample(id);
// results.push_back(normalDist.EvalSample());
// }
// Complex mean = results.Mean();
// double var = results.Variance();
// EXPECT_NEAR(0.0, mean.real(), sqrt(1.0 / numSamples));
// EXPECT_NEAR(0.0, mean.imag(), sqrt(1.0 / numSamples));
// EXPECT_NEAR(2.0, var, sqrt(100.0 / numSamples));
//}
//
//TEST_F(TestUniformDistribution, NormalDistributionRVector) {
// NormalDistributionRVector normalDist(*meshes, numSamples);
// normalDist.DrawSample(sampleIDs[0]);
// RVector results = normalDist.EvalSample();
// EXPECT_NEAR(0.0, results.Mean(), sqrt(1.0 / numSamples));
// EXPECT_NEAR(1.0, results.Variance(), sqrt(10.0 / numSamples));
//}
//
//
//TEST_F(TestUniformDistribution, NormalDistributionCVector) {
// NormalDistributionCVector normalDist(*meshes, numSamples);
// normalDist.DrawSample(sampleIDs[0]);
// CVector results = normalDist.EvalSample();
// Complex mean = results.Mean();
// EXPECT_NEAR(0.0, mean.real(), sqrt(1.0 / numSamples));
// EXPECT_NEAR(0.0, mean.imag(), sqrt(1.0 / numSamples));
// EXPECT_NEAR(1.0, results.Variance() / 2.0, sqrt(10.0 / numSamples));
//}
//
//
//TEST_F(TestUniformDistribution, NormalDistributionRMatrix) {
// NormalDistributionRMatrix normalDist(*meshes, sqrt(numSamples), sqrt(numSamples));
// normalDist.DrawSample(sampleIDs[0]);
// RMatrix results = normalDist.EvalSample();
// EXPECT_NEAR(0.0, results.Mean(), sqrt(1.0 / numSamples));
// EXPECT_NEAR(1.0, results.Variance(), sqrt(10.0 / numSamples));
//}
//
//TEST_F(TestUniformDistribution, NormalDistributionCMatrix) {
// NormalDistributionCMatrix normalDist(*meshes, sqrt(numSamples), sqrt(numSamples));
// normalDist.DrawSample(sampleIDs[0]);
// CMatrix results = normalDist.EvalSample();
// Complex mean = results.Mean();
// EXPECT_NEAR(0.0, mean.real(), sqrt(1.0 / numSamples));
// EXPECT_NEAR(0.0, mean.imag(), sqrt(1.0 / numSamples));
// EXPECT_NEAR(1.0, results.Variance() / 2.0, sqrt(10.0 / numSamples));
//}
TEST_F(TestUniformDistribution, UniformDistributionRVector) {
double a = 0.0;
double b = 1.0;
UniformDistributionRVector uniformDist(*meshes, numSamples, a, b);
RVector results = uniformDist.DrawAndEvalSample(sampleIDs[0]);
EXPECT_NEAR(a + b / 2, results.Mean(), sqrt(1.0 / numSamples));
EXPECT_NEAR(pow(b - a, 2) / 12, results.Variance(), sqrt(1.0 / numSamples));
}
TEST_F(TestUniformDistribution, UniformDistributionRVector2) {
double a = -1.0;
double b = 1.0;
UniformDistributionRVector uniformDist(*meshes, numSamples, a, b);
RVector results = uniformDist.DrawAndEvalSample(sampleIDs[0]);
EXPECT_NEAR((a + b) / 2.0, results.Mean(), sqrt(1.0 / numSamples));
EXPECT_NEAR(pow(b - a, 2) / 12.0, results.Variance(), sqrt(1.0 / numSamples));
}
TEST_F(TestUniformDistribution, UniformDistributionRMatrix) {
double a = 0.0;
double b = 1.0;
UniformDistributionRMatrix uniformDist(*meshes, sqrt(numSamples), sqrt(numSamples), a, b);
RMatrix results = uniformDist.DrawAndEvalSample(sampleIDs[0]);
EXPECT_NEAR(a + b / 2, results.Mean(), sqrt(1.0 / numSamples));
EXPECT_NEAR(pow(b - a, 2) / 12, results.Variance(), sqrt(1.0 / numSamples));
}
TEST_F(TestUniformDistribution, UniformDistributionRMatrix2) {
double a = -1.0;
double b = 1.0;
UniformDistributionRMatrix uniformDist(*meshes, sqrt(numSamples), sqrt(numSamples), a, b);
RMatrix results = uniformDist.DrawAndEvalSample(sampleIDs[0]);
EXPECT_NEAR((a + b) / 2.0, results.Mean(), sqrt(1.0 / numSamples));
EXPECT_NEAR(pow(b - a, 2) / 12.0, results.Variance(), sqrt(1.0 / numSamples));
}
int main(int argc, char **argv) {
......
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