MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Polynomial.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
8
9/**
10 * @enum PolynomialMode
11 * @brief Defines how the polynomial function processes input values
12 */
13enum class PolynomialMode : uint8_t {
14 DIRECT, ///< Evaluates f(x) where x is the current phase/input
15 RECURSIVE, ///< Evaluates using current and previous outputs: y[n] = f(y[n-1], y[n-2], ...)
16 FEEDFORWARD ///< Evaluates using current and previous inputs: y[n] = f(x[n], x[n-1], ...)
17};
18
19class MAYAFLUX_API PolynomialContext : public NodeContext {
20public:
21 /**
22 * @brief Constructs a PolynomialContext
23 * @param value Current output value
24 * @param mode Current polynomial mode
25 * @param buffer_size Size of the input/output buffers
26 * @param input_buffer Current input buffer contents
27 * @param output_buffer Current output buffer contents
28 * @param coefficients Current polynomial coefficients
29 */
30 PolynomialContext(double value,
31 PolynomialMode mode,
32 size_t buffer_size,
33 std::span<double> input_buffer,
34 std::span<double> output_buffer,
35 const std::vector<double>& coefficients)
36 : NodeContext(value, typeid(PolynomialContext).name())
37 , m_mode(mode)
38 , m_buffer_size(buffer_size)
39 , m_input_buffer(input_buffer)
40 , m_output_buffer(output_buffer)
41 , m_coefficients(coefficients)
42 {
43 }
44
45 /**
46 * @brief Gets the current polynomial mode
47 * @return Current polynomial mode
48 */
49 [[nodiscard]] PolynomialMode get_mode() const { return m_mode; }
50
51 /**
52 * @brief Gets the buffer size
53 * @return Current buffer size
54 */
55 [[nodiscard]] size_t get_buffer_size() const { return m_buffer_size; }
56
57 /**
58 * @brief Gets the input buffer
59 * @return Reference to the input buffer
60 */
61 [[nodiscard]] std::span<double> get_input_buffer() const { return m_input_buffer; }
62
63 /**
64 * @brief Gets the output buffer
65 * @return Reference to the output buffer
66 */
67 [[nodiscard]] std::span<double> get_output_buffer() const { return m_output_buffer; }
68
69 /**
70 * @brief Gets the polynomial coefficients
71 * @return Reference to the coefficient vector
72 */
73 [[nodiscard]] const std::vector<double>& get_coefficients() const { return m_coefficients; }
74
75private:
76 PolynomialMode m_mode; ///< Current processing mode
77 size_t m_buffer_size; ///< Size of the buffers
78 std::span<double> m_input_buffer; ///< Copy of input buffer
79 std::span<double> m_output_buffer; ///< Copy of output buffer
80 const std::vector<double>& m_coefficients; ///< Copy of polynomial coefficients
81
82 friend class Polynomial;
83};
84
85class MAYAFLUX_API PolynomialContextGpu
86 : public PolynomialContext,
87 public GpuVectorData {
88public:
90 double value,
91 PolynomialMode mode,
92 size_t buffer_size,
93 std::span<double> input_buffer,
94 std::span<double> output_buffer,
95 const std::vector<double>& coefficients,
96 std::span<const float> gpu_data)
97 : PolynomialContext(value, mode, buffer_size, input_buffer, output_buffer, coefficients)
98 , GpuVectorData(gpu_data)
99 {
100 type_id = typeid(PolynomialContextGpu).name();
101 }
102
103 friend class Polynomial;
104
105 std::vector<float> gpu_float_buffer;
106};
107
108/**
109 * @class Polynomial
110 * @brief Generator that produces values based on polynomial functions
111 *
112 * The Polynomial generator creates a signal based on mathematical functions
113 * that can operate in different modes:
114 * - Direct mode: evaluates f(x) where x is the current phase or input
115 * - Recursive mode: evaluates using current and previous outputs
116 * - Feedforward mode: evaluates using current and previous inputs
117 *
118 * This flexible approach allows implementing various types of polynomial
119 * equations, difference equations, and recurrence relations.
120 */
121class MAYAFLUX_API Polynomial : public Generator {
122public:
123 /**
124 * @brief Function type for direct polynomial evaluation
125 *
126 * Takes a single input value and returns the computed output.
127 */
128 using DirectFunction = std::function<double(double)>;
129
130 /**
131 * @brief Function type for recursive/feedforward polynomial evaluation
132 *
133 * Takes a reference to a buffer of values and returns the computed output.
134 * For recursive mode, the buffer contains previous outputs.
135 * For feedforward mode, the buffer contains current and previous inputs.
136 */
137 using BufferFunction = std::function<double(std::span<double>)>;
138
139 /**
140 * @class PolynomialContext
141 * @brief Context object for polynomial node callbacks
142 *
143 * Extends the base NodeContext with polynomial-specific information
144 * such as mode, buffer contents, and coefficients.
145 */
146 ;
147
148 /**
149 * @brief Constructs a Polynomial generator in direct mode with coefficient-based definition
150 * @param coefficients Vector of polynomial coefficients (highest power first)
151 *
152 * Creates a polynomial generator that evaluates a standard polynomial function
153 * defined by the provided coefficients. The coefficients are ordered from
154 * highest power to lowest (e.g., [2, 3, 1] represents 2x² + 3x + 1).
155 */
156 explicit Polynomial(const std::vector<double>& coefficients);
157
158 /**
159 * @brief Constructs a Polynomial generator in direct mode with a custom function
160 * @param function Custom function that maps input to output value
161 *
162 * Creates a polynomial generator that evaluates a custom mathematical function
163 * provided by the user.
164 */
165 explicit Polynomial(DirectFunction function);
166
167 /**
168 * @brief Constructs a Polynomial generator in recursive or feedforward mode
169 * @param function Function that processes a buffer of values
170 * @param mode Processing mode (RECURSIVE or FEEDFORWARD)
171 * @param buffer_size Number of previous values to maintain
172 *
173 * Creates a polynomial generator that evaluates using current and previous values.
174 * In recursive mode, the buffer contains previous outputs.
175 * In feedforward mode, the buffer contains current and previous inputs.
176 */
177 Polynomial(BufferFunction function, PolynomialMode mode, size_t buffer_size);
178
179 ~Polynomial() override = default;
180
181 /**
182 * @brief Processes a single sample
183 * @param input Input value
184 * @return The next sample value from the polynomial function
185 *
186 * Computes the next output value based on the polynomial function and current mode.
187 */
188 double process_sample(double input = 0.) override;
189
190 /**
191 * @brief Processes multiple samples at once
192 * @param num_samples Number of samples to generate
193 * @return Vector of generated samples
194 *
195 * This method is more efficient than calling process_sample() repeatedly
196 * when generating multiple samples at once.
197 */
198 std::vector<double> process_batch(unsigned int num_samples) override;
199
200 /**
201 * @brief Resets the generator to its initial state
202 *
203 * Clears the internal buffers and resets to initial conditions.
204 */
205 void reset();
206
207 /**
208 * @brief Sets the polynomial coefficients (for direct mode)
209 * @param coefficients Vector of polynomial coefficients (highest power first)
210 *
211 * Updates the polynomial function to use the specified coefficients.
212 * This allows dynamically changing the polynomial during operation.
213 */
214 void set_coefficients(const std::vector<double>& coefficients);
215
216 /**
217 * @brief Sets a custom direct function
218 * @param function Custom function that maps input to output value
219 *
220 * Updates the generator to use the specified custom function.
221 */
222 void set_direct_function(DirectFunction function);
223
224 /**
225 * @brief Sets a custom buffer function
226 * @param function Function that processes a buffer of values
227 * @param mode Processing mode (RECURSIVE or FEEDFORWARD)
228 * @param buffer_size Number of previous values to maintain
229 *
230 * Updates the generator to use the specified buffer function and mode.
231 */
232 void set_buffer_function(BufferFunction function, PolynomialMode mode, size_t buffer_size);
233
234 /**
235 * @brief Sets initial conditions for recursive mode
236 * @param initial_values Vector of initial output values
237 *
238 * Sets the initial values for the output buffer in recursive mode.
239 * The values are ordered from newest to oldest.
240 */
241 void set_initial_conditions(const std::vector<double>& initial_values);
242
243 /**
244 * @brief Creates a polynomial function from coefficients
245 * @param coefficients Vector of polynomial coefficients (highest power first)
246 * @return Function that evaluates the polynomial
247 *
248 * Generates a function that evaluates the polynomial defined by the
249 * specified coefficients.
250 * The cofficients are ordered from highest power to lowest.
251 */
252 DirectFunction create_polynomial_function(const std::vector<double>& coefficients);
253
254 /**
255 * @brief Sets the input node to generate polynomial values from
256 * @param input_node Node providing the input values
257 *
258 * Configures the node to receive input from another node
259 */
260 inline void set_input_node(const std::shared_ptr<Node>& input_node) { m_input_node = input_node; }
261
262 /**
263 * @brief Gets the current polynomial mode
264 * @return Current polynomial mode
265 */
266 [[nodiscard]] PolynomialMode get_mode() const { return m_mode; }
267
268 /**
269 * @brief Gets the buffer size
270 * @return Current buffer size
271 */
272 [[nodiscard]] size_t get_buffer_size() const { return m_buffer_size; }
273
274 /**
275 * @brief Gets the polynomial coefficients
276 * @return Reference to the coefficient vector
277 */
278 [[nodiscard]] const std::vector<double>& get_coefficients() const { return m_coefficients; }
279
280 /**
281 * @brief Gets the input buffer
282 * @return Reference to the input buffer
283 */
284 [[nodiscard]] std::span<double> get_input_buffer() { return m_history.linearized_view(); }
285
286 /**
287 * @brief Gets the output buffer
288 * @return Reference to the output buffer
289 */
290 [[nodiscard]] std::span<double> get_output_buffer() { return m_history.linearized_view(); }
291
292 /**
293 * @brief Prints a visual representation of the polynomial function
294 *
295 * Outputs a text-based graph of the polynomial function over a range of inputs.
296 */
297 inline void printGraph() override { }
298
299 /**
300 * @brief Prints the current state and parameters
301 *
302 * Outputs the current mode, buffer contents, and other relevant information.
303 */
304 inline void printCurrent() override { }
305
306 /**
307 * @brief Sets the scaling factor for the output values
308 */
309 inline void set_amplitude(double amplitude) override { m_scale_factor = amplitude; }
310
311 /**
312 * @brief Gets the current amplitude scaling factor
313 * @return Current amplitude scaling factor
314 *
315 * This method retrieves the scaling factor applied to the output values,
316 * which controls the overall amplitude of the generated signal.
317 */
318 [[nodiscard]] inline double get_amplitude() const override { return m_scale_factor; }
319
320 void save_state() override;
321 void restore_state() override;
322
323 /**
324 * @brief Uses an external buffer context for processing
325 * @param buffer_view Span representing the external buffer
326 *
327 * Configures the generator to use an external buffer context
328 * for its internal processing, allowing integration with
329 * external data sources.
330 */
331 void set_buffer_context(std::span<double> buffer_view)
332 {
333 m_external_buffer_context = buffer_view;
334 m_use_external_context = true;
335 }
336
337 /**
338 * @brief Clear external buffer context, resume internal accumulation
339 */
341 {
342 m_use_external_context = false;
343 m_external_buffer_context = {};
344 }
345
346 [[nodiscard]] inline bool using_external_context() const
347 {
348 return m_use_external_context;
349 }
350
351 /**
352 * @brief Retrieves the last created context object
353 * @return Reference to the last PolynomialContext object
354 *
355 * This method provides access to the most recent PolynomialContext object
356 * created by the polynomial generator. This context contains information
357 * about the generator's state at the time of the last output generation.
358 */
359 NodeContext& get_last_context() override;
360
361 void set_gpu_compatible(bool compatible) override
362 {
363 m_gpu_compatible = compatible;
364 if (compatible) {
365 m_node_capability |= NodeCapability::VECTOR;
366 } else {
367 m_node_capability &= ~NodeCapability::VECTOR;
368 }
369 }
370
371protected:
372 /**
373 * @brief Updates the context object with the current node state
374 * @param value The current sample value
375 */
376 void update_context(double value) override;
377
378 /**
379 * @brief Notifies all registered callbacks about a new sample
380 * @param value The newly generated sample
381 *
382 * This method is called internally whenever a new sample is generated,
383 * creating the appropriate context and invoking all registered callbacks
384 * that should receive notification about this sample.
385 */
386 void notify_tick(double value) override;
387
388private:
389 /**
390 * @brief Converts coefficient vector to a polynomial function
391 * @param coefficients Vector of polynomial coefficients (highest power first)
392 * @return Function that evaluates the polynomial
393 *
394 * Creates a function that evaluates the polynomial defined by the
395 * specified coefficients.
396 */
397 // DirectFunction create_polynomial_function(const std::vector<double>& coefficients);
398
399 PolynomialMode m_mode; ///< Current processing mode
400 DirectFunction m_direct_function; ///< Function for direct mode
401 BufferFunction m_buffer_function; ///< Function for recursive/feedforward mode
402 std::vector<double> m_coefficients; ///< Polynomial coefficients (if using coefficient-based definition)
403 std::span<double> m_external_buffer_context; ///< View into external buffer
404
405 Memory::HistoryBuffer<double> m_history; ///< Ring buffer for input/output history
406 std::vector<double> m_linear_view; ///< Linearized view of history for easy access
407
408 std::vector<double> m_saved_history_state; ///< Saved state of the history buffer
409
410 size_t m_buffer_size {}; ///< Maximum size of the buffers
411 double m_scale_factor; ///< Scaling factor for output
412 std::shared_ptr<Node> m_input_node; ///< Input node for processing
413 size_t m_current_buffer_position {}; // Where we are in the external buffer
414
415 double m_saved_last_output {};
416 bool m_use_external_context {}; // Whether to use it
417
420
421 std::span<double> external_context_view(double input);
422};
423
424} // namespace MayaFlux::Nodes::Generator
History buffer for difference equations and recursive relations.
Base class for all signal and pattern generators in Maya Flux.
PolynomialContextGpu(double value, PolynomialMode mode, size_t buffer_size, std::span< double > input_buffer, std::span< double > output_buffer, const std::vector< double > &coefficients, std::span< const float > gpu_data)
std::span< double > get_output_buffer() const
Gets the output buffer.
const std::vector< double > & m_coefficients
Copy of polynomial coefficients.
const std::vector< double > & get_coefficients() const
Gets the polynomial coefficients.
std::span< double > m_output_buffer
Copy of output buffer.
PolynomialMode m_mode
Current processing mode.
PolynomialContext(double value, PolynomialMode mode, size_t buffer_size, std::span< double > input_buffer, std::span< double > output_buffer, const std::vector< double > &coefficients)
Constructs a PolynomialContext.
std::span< double > m_input_buffer
Copy of input buffer.
std::span< double > get_input_buffer() const
Gets the input buffer.
size_t get_buffer_size() const
Gets the buffer size.
PolynomialMode get_mode() const
Gets the current polynomial mode.
std::function< double(std::span< double >)> BufferFunction
Function type for recursive/feedforward polynomial evaluation.
void set_gpu_compatible(bool compatible) override
Sets whether the node is compatible with GPU processing.
PolynomialMode m_mode
Converts coefficient vector to a polynomial function.
std::vector< double > m_saved_history_state
Saved state of the history buffer.
void set_amplitude(double amplitude) override
Sets the scaling factor for the output values.
Memory::HistoryBuffer< double > m_history
Ring buffer for input/output history.
PolynomialMode get_mode() const
Gets the current polynomial mode.
void set_input_node(const std::shared_ptr< Node > &input_node)
Sets the input node to generate polynomial values from.
void set_buffer_context(std::span< double > buffer_view)
Uses an external buffer context for processing.
void printCurrent() override
Prints the current state and parameters.
const std::vector< double > & get_coefficients() const
Gets the polynomial coefficients.
double get_amplitude() const override
Gets the current amplitude scaling factor.
double m_scale_factor
Scaling factor for output.
std::span< double > m_external_buffer_context
View into external buffer.
size_t get_buffer_size() const
Gets the buffer size.
void clear_buffer_context()
Clear external buffer context, resume internal accumulation.
std::vector< double > m_linear_view
Linearized view of history for easy access.
BufferFunction m_buffer_function
Function for recursive/feedforward mode.
std::shared_ptr< Node > m_input_node
Input node for processing.
std::span< double > get_output_buffer()
Gets the output buffer.
std::function< double(double)> DirectFunction
Function type for direct polynomial evaluation.
DirectFunction m_direct_function
Function for direct mode.
void printGraph() override
Prints a visual representation of the polynomial function.
std::span< double > get_input_buffer()
Gets the input buffer.
std::vector< double > m_coefficients
Polynomial coefficients (if using coefficient-based definition)
Generator that produces values based on polynomial functions.
GPU-uploadable 1D array data interface.
Base context class for node callbacks.
Definition Node.hpp:30
@ DIRECT
Stateless evaluation of current input only (combinational logic)
PolynomialMode
Defines how the polynomial function processes input values.
@ RECURSIVE
Evaluates using current and previous outputs: y[n] = f(y[n-1], y[n-2], ...)
@ FEEDFORWARD
Evaluates using current and previous inputs: y[n] = f(x[n], x[n-1], ...)
NodeCapability
Bitmask flags declaring what data shapes a node's context can produce.
Definition NodeSpec.hpp:104