-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added several options to define integrands: either an array parameter…
… or a multiple parameter function.
- Loading branch information
1 parent
77c244f
commit 432d372
Showing
7 changed files
with
162 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "../../viltrum.h" | ||
#include <iostream> | ||
#include <iomanip> | ||
|
||
using namespace viltrum; | ||
|
||
int main() { | ||
const std::size_t bins = 24; | ||
const unsigned long samples = 1000000; | ||
|
||
auto f =[] (float x, float y) -> double { | ||
if ((x+y)<1) return 1.0; | ||
else return 0.0; | ||
}; | ||
|
||
//With floats, due to numerica stability, for a very large number of samples, monte_carlo fails to converge? | ||
{ | ||
LoggerProgress logger("Simple"); | ||
std::cout<<integrate(monte_carlo(samples*bins),f,range_primary<2>(),logger)<<std::endl; | ||
} | ||
|
||
{ | ||
LoggerProgress logger("Monte-Carlo"); | ||
std::vector<float> sol(bins,0.0f); | ||
integrate(monte_carlo(bins*samples),sol,f,range_primary<2>(),logger); | ||
for (float v : sol) std::cout<<std::fixed<<std::setprecision(2)<<std::setw(4)<<v<<" "; | ||
std::cout<<std::endl; | ||
} | ||
{ | ||
LoggerProgress logger("Per bin"); | ||
std::vector<float> sol(bins,0.0f); | ||
integrate(integrator_per_bin(monte_carlo(samples)),sol,f,range_primary<2>(),logger); | ||
for (float v : sol) std::cout<<std::fixed<<std::setprecision(2)<<std::setw(4)<<v<<" "; | ||
std::cout<<std::endl; | ||
} | ||
{ | ||
LoggerProgress logger("Parallel"); //I'm suprised this works with a RNG shared among threads | ||
std::vector<float> sol(bins,0.0f); | ||
integrate(integrator_per_bin_parallel(monte_carlo(samples)),sol,f,range_primary<2>(),logger); | ||
for (float v : sol) std::cout<<std::fixed<<std::setprecision(2)<<std::setw(4)<<v<<" "; | ||
std::cout<<std::endl; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "../../src/monte-carlo/random-sequence.h" | ||
#include <iostream> | ||
|
||
|
||
|
||
using namespace viltrum; | ||
|
||
int main() { | ||
auto l = random_sequence(0.0,1.0); | ||
int i = 0; | ||
for (double d : l) { | ||
std::cout<<d<<" "; | ||
if (++i>=10) break; | ||
} | ||
std::cout<<std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
#include <random> | ||
|
||
namespace viltrum { | ||
|
||
//I am implementing it so it is predictable once we initialize the RNG. | ||
template<typename RNG, typename Number> | ||
class RandomSequence { | ||
RNG rng; | ||
std::uniform_real_distribution<Number> dis; | ||
public: | ||
RandomSequence(RNG&& r, const Number& lower = Number(0), const Number& upper = Number(1)) : | ||
rng(std::forward<RNG>(r)), dis(lower,upper) {} | ||
|
||
class const_iterator { | ||
private: | ||
RNG rng; | ||
std::uniform_real_distribution<Number> dis; | ||
Number n; | ||
const_iterator(const RNG& r, const std::uniform_real_distribution<Number>& d) : | ||
rng(r), dis(d), n(dis(rng)) {} | ||
friend class RandomSequence<RNG,Number>; | ||
public: | ||
const Number& operator*() const { return n; } | ||
const_iterator& operator++() { n = dis(rng); return (*this); } | ||
bool operator==(const const_iterator& that) const { return false; } //Infinite list | ||
bool operator!=(const const_iterator& that) const { return true; } //Infinite list | ||
}; | ||
|
||
const_iterator begin() const { return const_iterator(rng,dis); } | ||
const_iterator end() const { return const_iterator(rng,dis); } | ||
}; | ||
|
||
template<typename Number,typename RNG = std::mt19937> | ||
auto random_sequence(const Number& lower, const Number& upper, std::size_t seed = std::random_device()()) { | ||
return RandomSequence<RNG,Number>(RNG(seed),lower,upper); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters