style patterns

This commit is contained in:
Miha Kralj
2026-01-25 16:01:45 -08:00
parent 2836f253c4
commit e59665c8f0
399 changed files with 6892 additions and 1323 deletions
+70 -9
View File
@@ -43,7 +43,9 @@ public sealed class Rse : AbstractBase
public Rse(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_actualBuffer = new RingBuffer(period);
_sqErrorBuffer = new RingBuffer(period);
@@ -67,14 +69,22 @@ public sealed class Rse : AbstractBase
}
if (!double.IsFinite(actualVal))
{
actualVal = double.IsFinite(_state.LastValidActual) ? _state.LastValidActual : 0.0;
}
else
{
_state.LastValidActual = actualVal;
}
if (!double.IsFinite(predictedVal))
{
predictedVal = double.IsFinite(_state.LastValidPredicted) ? _state.LastValidPredicted : 0.0;
}
else
{
_state.LastValidPredicted = predictedVal;
}
if (isNew)
{
@@ -177,7 +187,9 @@ public sealed class Rse : AbstractBase
public static TSeries Calculate(TSeries actual, TSeries predicted, int period)
{
if (actual.Count != predicted.Count)
{
throw new ArgumentException("Actual and predicted series must have the same length", nameof(predicted));
}
int len = actual.Count;
var t = new List<long>(len);
@@ -198,12 +210,20 @@ public sealed class Rse : AbstractBase
public static void Batch(ReadOnlySpan<double> actual, ReadOnlySpan<double> predicted, Span<double> output, int period)
{
if (actual.Length != predicted.Length || actual.Length != output.Length)
{
throw new ArgumentException("All spans must have the same length", nameof(output));
}
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> actualBuffer = period <= StackAllocThreshold
@@ -224,11 +244,19 @@ public sealed class Rse : AbstractBase
for (int k = 0; k < len; k++)
{
if (double.IsFinite(actual[k])) { lastValidActual = actual[k]; break; }
if (double.IsFinite(actual[k]))
{
lastValidActual = actual[k];
break;
}
}
for (int k = 0; k < len; k++)
{
if (double.IsFinite(predicted[k])) { lastValidPredicted = predicted[k]; break; }
if (double.IsFinite(predicted[k]))
{
lastValidPredicted = predicted[k];
break;
}
}
int bufferIndex = 0;
@@ -240,8 +268,23 @@ public sealed class Rse : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
actualSum += act;
actualBuffer[i] = act;
@@ -266,8 +309,23 @@ public sealed class Rse : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
actualSum = actualSum - actualBuffer[bufferIndex] + act;
actualBuffer[bufferIndex] = act;
@@ -284,7 +342,10 @@ public sealed class Rse : AbstractBase
sqBaselineBuffer[bufferIndex] = sqBaseline;
bufferIndex++;
if (bufferIndex >= period) bufferIndex = 0;
if (bufferIndex >= period)
{
bufferIndex = 0;
}
output[i] = sqBaselineSum > 1e-10 ? sqErrorSum / sqBaselineSum : 1.0;
@@ -305,4 +366,4 @@ public sealed class Rse : AbstractBase
}
}
}
}
}