MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Stochastic.cpp
Go to the documentation of this file.
1#include "Stochastic.hpp"
2
4
6 : m_random_engine(std::random_device {}())
7 , m_current_start(-1.0F)
8 , m_current_end(1.0F)
9 , m_normal_spread(4.0F)
10 , m_type(type)
11 , m_xorshift_state(std::random_device {}() | (static_cast<uint64_t>(std::random_device {}()) << 32))
12 , m_context(0.0, type, 1.0, m_current_start, m_current_end, m_normal_spread)
13 , m_context_gpu(0.0, type, 1.0, m_current_start, m_current_end, m_normal_spread, get_gpu_data_buffer())
14{
15 if (m_xorshift_state == 0)
16 m_xorshift_state = 0xDEADBEEFCAFEBABE;
17}
18
28
29double Random::random_sample(double start, double end)
30{
31 validate_range(start, end);
32
33 if (start != m_current_start || end != m_current_end) {
34 m_current_start = start;
35 m_current_end = end;
36 m_dist_dirty = true;
37 }
38
40}
41
42std::vector<double> Random::random_array(double start, double end, unsigned int num_samples)
43{
44 validate_range(start, end);
45 m_current_start = start;
46 m_current_end = end;
47
48 std::vector<double> samples;
49 samples.reserve(num_samples);
50
51 for (unsigned int i = 0; i < num_samples; ++i) {
52 samples.push_back(transform_sample(generate_distributed_sample(), start, end) * m_amplitude);
53 }
54
55 return samples;
56}
57
58std::vector<double> Random::process_batch(unsigned int num_samples)
59{
60 return random_array(m_current_start, m_current_end, num_samples);
61}
62
64{
65 switch (m_type) {
66 [[likely]] case Utils::distribution::UNIFORM:
68
72
76
78 std::poisson_distribution<int> dist(
79 static_cast<int>(m_current_end - m_current_start));
80 return static_cast<double>(dist(m_random_engine));
81 }
82 default:
84 }
85}
86
87double Random::transform_sample(double sample, double start, double end) const
88{
90 sample = std::max(start, std::min(end, sample));
92 sample /= end;
93 sample = start + sample * (end - start);
94 }
95 return sample;
96}
97
98void Random::validate_range(double start, double end) const
99{
100 if (start > end) {
101 throw std::invalid_argument("Start must be less than or equal to end");
102 }
103}
104
106{
107 if (!m_dist_dirty)
108 return;
109
110 const double range = m_current_end - m_current_start;
111 m_normal_dist = std::normal_distribution<double>(0.0, range / m_normal_spread);
112 m_exponential_dist = std::exponential_distribution<double>(1.0);
113
117 m_dist_dirty = false;
118}
119
120void Random::set_normal_spread(double spread)
121{
122 if (spread != m_normal_spread) {
123 m_normal_spread = spread;
124 m_dist_dirty = true;
125 }
126}
127
146
147void Random::notify_tick(double value)
148{
149 update_context(value);
150 auto& ctx = get_last_context();
151
152 for (auto& callback : m_callbacks) {
153 callback(ctx);
154 }
155 for (auto& [callback, condition] : m_conditional_callbacks) {
156 if (condition(ctx)) {
157 callback(ctx);
158 }
159 }
160}
161
163{
164 if (m_gpu_compatible) {
165 return m_context_gpu;
166 }
167 return m_context;
168}
169
171{
172 // When opengl, vulkan or sciplot plugged in
173}
174
176{
177 // When opengl, vulkan or sciplot plugged in
178}
179}
double m_amplitude
Base amplitude of the generator.
Utils::distribution m_type
Current probability distribution algorithm.
uint64_t m_xorshift_state
Internal state for xorshift random number generation.
std::exponential_distribution< double > m_exponential_dist
Exponential distribution with lambda = 1.
NodeContext & get_last_context() override
Gets the last created context object.
void validate_range(double start, double end) const
Validates that the specified range is mathematically valid.
double m_normal_spread
Variance parameter for normal distribution.
std::vector< double > process_batch(unsigned int num_samples) override
Generates multiple stochastic values at once.
std::vector< double > random_array(double start, double end, unsigned int num_samples)
Generates an array of stochastic values within a specified range.
std::normal_distribution< double > m_normal_dist
Normal distribution with mean 0 and standard deviation 1.
void set_normal_spread(double spread)
Sets the variance parameter for normal distribution.
std::mt19937 m_random_engine
Mersenne Twister entropy generator.
double transform_sample(double sample, double start, double end) const
Transforms a raw value to fit within the specified range.
void printGraph() override
Visualizes the distribution characteristics.
Random(Utils::distribution type=Utils::distribution::UNIFORM)
Constructor for the stochastic generator.
Definition Stochastic.cpp:5
void update_context(double value) override
Updates the context object with the current node state.
double m_current_start
Lower bound of the current output range.
double generate_distributed_sample()
Generates a raw value according to the current distribution.
double fast_uniform() noexcept
Fast uniform random number generator using xorshift algorithm.
double m_current_end
Upper bound of the current output range.
double random_sample(double start, double end)
Generates a stochastic value within a specified range.
void rebuild_distributions_if_needed() noexcept
Rebuilds distribution objects if parameters have changed.
void printCurrent() override
Outputs the current configuration parameters.
void notify_tick(double value) override
Notifies all registered callbacks about a new value.
double process_sample(double input=0.) override
Generates a single stochastic value.
Utils::distribution distribution_type
Current distribution type.
double normal_spread
Current variance parameter for normal distribution.
double amplitude
Current amplitude scaling factor.
double range_start
Current lower bound of the range.
double range_end
Current upper bound of the range.
double value
Current sample value.
Definition Node.hpp:40
Base context class for node callbacks.
Definition Node.hpp:30
std::vector< NodeHook > m_callbacks
Collection of standard callback functions.
Definition Node.hpp:403
bool m_state_saved
tracks if the node's state has been saved by a snapshot operation
Definition Node.hpp:426
bool m_networked_node
Flag indicating if the node is part of a NodeNetwork This flag is used to disable event firing when t...
Definition Node.hpp:421
double m_last_output
The most recent sample value generated by this oscillator.
Definition Node.hpp:374
bool m_fire_events_during_snapshot
Internal flag controlling whether notify_tick fires during state snapshots Default: false (events don...
Definition Node.hpp:448
std::vector< std::pair< NodeHook, NodeCondition > > m_conditional_callbacks
Collection of conditional callback functions with their predicates.
Definition Node.hpp:413
bool m_gpu_compatible
Flag indicating if the node supports GPU processing This flag is set by derived classes to indicate w...
Definition Node.hpp:383