Files
Chronos/src/Chronos.Core/Math/NaturalCubicSpline.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

89 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace Chronos.Core.Mathematics;
/// <summary>
/// Interpolating cubic spline with natural end conditions (zero second derivative), second derivatives solved
/// with the Thomas tridiagonal algorithm. C² inside the knot range; extrapolates linearly with the end slopes.
/// </summary>
public sealed class NaturalCubicSpline
{
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 NaturalCubicSpline(ReadOnlySpan<double> x, ReadOnlySpan<double> y)
{
SplineInput.Validate(x, y, minimum: 1);
_x = x.ToArray();
_y = y.ToArray();
var n = _x.Length;
_m = new double[n];
if (n < 3) return;
// Interior equations: h[i-1]·M[i-1] + 2(h[i-1]+h[i])·M[i] + h[i]·M[i+1] = 6(δ[i] − δ[i-1]).
var c = new double[n];
var d = new double[n];
for (var i = 1; i < n - 1; i++)
{
var h0 = _x[i] - _x[i - 1];
var h1 = _x[i + 1] - _x[i];
var rhs = 6 * ((_y[i + 1] - _y[i]) / h1 - (_y[i] - _y[i - 1]) / h0);
var diag = 2 * (h0 + h1) - h0 * c[i - 1];
c[i] = h1 / diag;
d[i] = (rhs - h0 * d[i - 1]) / diag;
}
for (var i = n - 2; i >= 1; i--) _m[i] = d[i] - c[i] * _m[i + 1];
}
public int Count => _x.Length;
public double Evaluate(double x)
{
var n = _x.Length;
if (n == 1) return _y[0];
if (x < _x[0]) return _y[0] + Slope(0, atEnd: false) * (x - _x[0]);
if (x > _x[^1]) return _y[^1] + Slope(n - 2, atEnd: true) * (x - _x[^1]);
var k = Interval(x);
var h = _x[k + 1] - _x[k];
var a = (_x[k + 1] - x) / h;
var b = 1 - a;
return a * _y[k] + b * _y[k + 1] + ((a * a * a - a) * _m[k] + (b * b * b - b) * _m[k + 1]) * h * h / 6;
}
public double Derivative(double x)
{
var n = _x.Length;
if (n == 1) return 0;
if (x < _x[0]) return Slope(0, atEnd: false);
if (x > _x[^1]) return Slope(n - 2, atEnd: true);
var k = Interval(x);
var h = _x[k + 1] - _x[k];
var a = (_x[k + 1] - x) / h;
var b = 1 - a;
return (_y[k + 1] - _y[k]) / h - (3 * a * a - 1) / 6 * h * _m[k] + (3 * b * b - 1) / 6 * h * _m[k + 1];
}
public double SecondDerivative(double x)
{
if (_x.Length == 1 || x <= _x[0] || x >= _x[^1]) return 0;
var k = Interval(x);
var a = (_x[k + 1] - x) / (_x[k + 1] - _x[k]);
return a * _m[k] + (1 - a) * _m[k + 1];
}
private double Slope(int k, bool atEnd)
{
var h = _x[k + 1] - _x[k];
var secant = (_y[k + 1] - _y[k]) / h;
return atEnd ? secant + h * (_m[k] + 2 * _m[k + 1]) / 6 : secant - h * (2 * _m[k] + _m[k + 1]) / 6;
}
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);
}
}