Files
Chronos/src/Chronos.Core/Math/MonotoneSpline.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

178 lines
6.1 KiB
C#

namespace Chronos.Core.Mathematics;
/// <summary>
/// Monotone piecewise cubic Hermite interpolant (Fritsch &amp; Carlson 1980): three-point tangents limited to the
/// circle α² + β² ≤ 9 so the curve never overshoots monotone data. Outside the knot range the value is clamped
/// to the end values (derivative 0), which keeps time remaps monotone.
/// </summary>
public sealed class MonotoneSpline
{
private readonly double[] _x;
private readonly double[] _y;
private readonly double[] _m;
/// <param name="x">Strictly increasing, finite knot positions (at least one).</param>
/// <param name="y">Finite knot values.</param>
public MonotoneSpline(ReadOnlySpan<double> x, ReadOnlySpan<double> y)
{
SplineInput.Validate(x, y, minimum: 1);
_x = x.ToArray();
_y = y.ToArray();
_m = new double[_x.Length];
var n = _x.Length;
if (n == 1) return;
var delta = new double[n - 1];
var increasing = true;
var decreasing = true;
for (var k = 0; k < n - 1; k++)
{
delta[k] = (_y[k + 1] - _y[k]) / (_x[k + 1] - _x[k]);
increasing &= delta[k] >= 0;
decreasing &= delta[k] <= 0;
}
IsMonotone = increasing || decreasing;
_m[0] = delta[0];
_m[n - 1] = delta[n - 2];
for (var k = 1; k < n - 1; k++)
{
if (delta[k - 1] * delta[k] <= 0)
{
_m[k] = 0;
continue;
}
// Three-point derivative for non-uniform spacing (exact for quadratics).
var h0 = _x[k] - _x[k - 1];
var h1 = _x[k + 1] - _x[k];
_m[k] = (h1 * delta[k - 1] + h0 * delta[k]) / (h0 + h1);
}
for (var k = 0; k < n - 1; k++)
{
if (delta[k] == 0)
{
_m[k] = 0;
_m[k + 1] = 0;
continue;
}
var alpha = _m[k] / delta[k];
var beta = _m[k + 1] / delta[k];
if (alpha < 0) _m[k] = alpha = 0;
if (beta < 0) _m[k + 1] = beta = 0;
var s = alpha * alpha + beta * beta;
if (s > 9)
{
var tau = 3 / Math.Sqrt(s);
_m[k] = tau * alpha * delta[k];
_m[k + 1] = tau * beta * delta[k];
}
}
}
public int Count => _x.Length;
public double MinX => _x[0];
public double MaxX => _x[^1];
/// <summary>True when the knot values are entirely non-decreasing or non-increasing (required by <see cref="Inverse"/>).</summary>
public bool IsMonotone { get; } = true;
public double Evaluate(double x)
{
if (!(x > _x[0])) return _y[0];
if (x >= _x[^1]) return _y[^1];
var k = Interval(x);
var h = _x[k + 1] - _x[k];
var t = (x - _x[k]) / h;
var t2 = t * t;
var t3 = t2 * t;
// Increment form: rounding scales with the interval's rise, so flat intervals stay exactly flat.
return _y[k] + (_y[k + 1] - _y[k]) * (3 * t2 - 2 * t3) + h * ((t3 - 2 * t2 + t) * _m[k] + (t3 - t2) * _m[k + 1]);
}
public double Derivative(double x)
{
if (!(x > _x[0]) || x >= _x[^1]) return 0;
var k = Interval(x);
var h = _x[k + 1] - _x[k];
var t = (x - _x[k]) / h;
var t2 = t * t;
return ((6 * t2 - 6 * t) * _y[k]
+ (3 * t2 - 4 * t + 1) * h * _m[k]
+ (-6 * t2 + 6 * t) * _y[k + 1]
+ (3 * t2 - 2 * t) * h * _m[k + 1]) / h;
}
/// <summary>
/// The x at which the spline reaches <paramref name="y"/> (values outside the range clamp to the end knots;
/// on a flat stretch the first x is returned). Safeguarded Newton iteration inside the bracketing interval.
/// </summary>
/// <exception cref="InvalidOperationException">The knot values are not monotone.</exception>
public double Inverse(double y)
{
if (!IsMonotone) throw new InvalidOperationException("Inverse requires monotone knot values.");
var n = _x.Length;
var sign = _y[^1] >= _y[0] ? 1.0 : -1.0;
var target = sign * y;
if (n == 1 || !(target > sign * _y[0])) return _x[0];
if (target >= sign * _y[^1]) return FirstAtEnd();
int lo = 0, hi = n - 1;
while (hi - lo > 1)
{
var mid = (lo + hi) >> 1;
if (sign * _y[mid] < target) lo = mid;
else hi = mid;
}
if (sign * _y[hi] == target)
{
while (hi > 0 && _y[hi - 1] == _y[hi]) hi--;
return _x[hi];
}
double a = _x[lo], b = _x[hi];
var x = a + (b - a) * (target - sign * _y[lo]) / (sign * (_y[hi] - _y[lo]));
for (var i = 0; i < 100 && b - a > 1e-14 * (1 + Math.Abs(a)); i++)
{
var f = sign * Evaluate(x) - target;
if (f == 0) return x;
if (f < 0) a = x;
else b = x;
var d = sign * Derivative(x);
var next = d > 0 ? x - f / d : double.NaN;
x = next > a && next < b ? next : 0.5 * (a + b);
}
return x;
}
private double FirstAtEnd()
{
var i = _x.Length - 1;
while (i > 0 && _y[i - 1] == _y[i]) i--;
return _x[i];
}
private int Interval(double x)
{
var i = Array.BinarySearch(_x, x);
if (i < 0) i = ~i - 1;
return Math.Clamp(i, 0, _x.Length - 2);
}
}
internal static class SplineInput
{
public static void Validate(ReadOnlySpan<double> x, ReadOnlySpan<double> y, int minimum)
{
if (x.Length != y.Length) throw new ArgumentException("x and y lengths differ.", nameof(y));
if (x.Length < minimum) throw new ArgumentException($"At least {minimum} knot(s) required.", nameof(x));
for (var i = 0; i < x.Length; i++)
{
if (!double.IsFinite(x[i]) || !double.IsFinite(y[i])) throw new ArgumentException("Knots must be finite.", nameof(x));
if (i > 0 && !(x[i] > x[i - 1])) throw new ArgumentException("x must be strictly increasing.", nameof(x));
}
}
}