MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Dynamics.cpp
Go to the documentation of this file.
1#include "Dynamics.hpp"
2
3#include "Kernels.hpp"
4
6
8 double t, double onset, double duration, double rise, double fall, double period) noexcept
9{
10 double local = t - onset;
11 if (local < 0.0)
12 return 0.0;
13
14 double pulse = std::max(duration, 0.0);
15 if (period > 0.0) {
16 local -= period * std::floor(local / period);
17 pulse = std::min(pulse, period);
18 }
19
20 const auto peak_at = [rise](double s) {
21 return rise > 0.0 ? 1.0 - std::exp(-s / rise) : 1.0;
22 };
23
24 if (local <= pulse)
25 return peak_at(local);
26
27 if (fall <= 0.0)
28 return 0.0;
29 return peak_at(pulse) * std::exp(-(local - pulse) / fall);
30}
31
33 std::span<double> out,
34 double t,
35 std::span<const double> onset,
36 std::span<const double> duration,
37 std::span<const double> rise,
38 std::span<const double> fall,
39 std::span<const double> period) noexcept
40{
41 const size_t count = std::min({ out.size(), onset.size(), duration.size(), rise.size(), fall.size(), period.size() });
42 for (size_t i = 0; i < count; ++i)
43 out[i] = pulse_response(t, onset[i], duration[i], rise[i], fall[i], period[i]);
44}
45
47 std::span<double> levels,
48 std::span<const double> growth,
49 std::span<const double> interaction,
50 double dt)
51{
52 const size_t n = std::min(levels.size(), growth.size());
53 if (n == 0 || interaction.size() < n * n)
54 return;
55
56 thread_local std::vector<double> next;
57 next.assign(n, 0.0);
58
59 for (size_t i = 0; i < n; ++i) {
60 const double pressure = weighted_sum(levels.first(n), interaction.subspan(i * n, n));
61 next[i] = std::max(levels[i] + dt * levels[i] * (growth[i] - pressure), 0.0);
62 }
63
64 for (size_t i = 0; i < n; ++i)
65 levels[i] = next[i];
66}
67
68} // namespace MayaFlux::Kinesis::Discrete
Time evolution of arrays of independent levels.
Pure functions of a newest-first window and an array of numbers.
std::vector< float > * out
size_t count
double pulse_response(double t, double onset, double duration, double rise, double fall, double period) noexcept
Level at a time of a leaky response to a rectangular pulse.
Definition Dynamics.cpp:7
double weighted_sum(std::span< const double > window, std::span< const double > coefs) noexcept
Sum of window[k] * coefs[k] over the shorter of the two.
Definition Kernels.cpp:98
void lotka_volterra_step(std::span< double > levels, std::span< const double > growth, std::span< const double > interaction, double dt)
One explicit step of generalised Lotka-Volterra competition.
Definition Dynamics.cpp:46