Skip to content

Commit 56785ec

Browse files
committed
proposal
1 parent 84185f4 commit 56785ec

3 files changed

Lines changed: 67 additions & 12 deletions

File tree

include/geode/stochastic/geometry/random_engine.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <geode/basic/pimpl.hpp>
3131
namespace geode
3232
{
33-
class opengeode_stochastic_geometry_api RandomEngine
33+
class RandomEngine
3434
{
3535
OPENGEODE_DISABLE_COPY_AND_MOVE( RandomEngine );
3636

@@ -43,6 +43,9 @@ namespace geode
4343
template < typename RANDSPEC >
4444
auto sample( const RANDSPEC& spec );
4545

46+
template < typename Type >
47+
Type sample_uniform( const Uniform< Type >& law );
48+
4649
private:
4750
IMPLEMENTATION_MEMBER( impl_ );
4851
};

include/geode/stochastic/geometry/random_sample_spec.hpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@
3131

3232
namespace geode
3333
{
34-
/*!
35-
* Spec to draw a int value in a Uniform Distribution. Describe a
36-
* closed interval [min,max]
37-
*/
38-
struct UniformInt
34+
template < typename Type >
35+
struct IntervalLimit
3936
{
40-
int min;
41-
int max;
37+
IntervalLimit() = default;
38+
39+
Type value{ 0 };
40+
bool is_included{ true };
41+
};
42+
43+
template < typename Type >
44+
struct Uniform
45+
{
46+
Uniform() = default;
47+
48+
IntervalLimit< Type > min;
49+
IntervalLimit< Type > max;
4250
};
4351

4452
/*!

src/geode/stochastic/geometry/random_engine.cpp

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ namespace geode
6060
return do_sample_impl( spec );
6161
}
6262

63+
template < typename Type >
64+
Type sample_uniform( const Uniform< Type >& law )
65+
{
66+
// check interval values correctness
67+
if( law.min.is_included )
68+
{
69+
if( law.max.is_included )
70+
{
71+
return absl::Uniform(
72+
absl::IntervalClosed, rand_gen_, law.min, law.max );
73+
}
74+
return absl::Uniform(
75+
absl::IntervalClosedOpen, rand_gen_, law.min, law.max );
76+
}
77+
if( law.max.is_included )
78+
{
79+
return absl::Uniform(
80+
absl::IntervalOpenClosed, rand_gen_, law.min, law.max );
81+
}
82+
return absl::Uniform(
83+
absl::IntervalOpen, rand_gen_, law.min, law.max );
84+
}
85+
6386
private:
6487
int do_sample_impl( const UniformInt& spec )
6588
{
@@ -103,9 +126,30 @@ namespace geode
103126
return impl_->do_sample( spec );
104127
}
105128

106-
template auto RandomEngine::sample( const UniformInt& );
107-
template auto RandomEngine::sample( const UniformDouble& );
108-
template auto RandomEngine::sample( const Gaussian& );
109-
template auto RandomEngine::sample( const Bernoulli& );
129+
template < typename Type >
130+
Type RandomEngine::sample_uniform( const Uniform< Type >& law )
131+
{
132+
return impl_->sample_uniform( law );
133+
}
134+
135+
template index_t opengeode_stochastic_geometry_api
136+
RandomEngine::sample_uniform( const Uniform< index_t >& );
137+
template local_index_t opengeode_stochastic_geometry_api
138+
RandomEngine::sample_uniform( const Uniform< local_index_t >& );
139+
template signed_index_t opengeode_stochastic_geometry_api
140+
RandomEngine::sample_uniform( const Uniform< signed_index_t >& );
141+
template float opengeode_stochastic_geometry_api
142+
RandomEngine::sample_uniform( const Uniform< float >& );
143+
template double opengeode_stochastic_geometry_api
144+
RandomEngine::sample_uniform( const Uniform< double >& );
145+
146+
147+
148+
template auto opengeode_stochastic_geometry_api RandomEngine::sample(
149+
const UniformDouble& );
150+
template auto opengeode_stochastic_geometry_api RandomEngine::sample(
151+
const Gaussian& );
152+
template auto opengeode_stochastic_geometry_api RandomEngine::sample(
153+
const Bernoulli& );
110154

111155
} // namespace geode

0 commit comments

Comments
 (0)