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
+6 -1
View File
@@ -425,7 +425,9 @@ public class SmaTests
double[] output = new double[10000];
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 42);
for (int i = 0; i < source.Length; i++)
{
source[i] = gbm.Next().Close;
}
// Warm up
Sma.Batch(source.AsSpan(), output.AsSpan(), 100);
@@ -571,7 +573,10 @@ public class SmaTests
public void Calculate_ReturnsCorrectResultsAndHotIndicator()
{
var series = new TSeries();
for (int i = 1; i <= 10; i++) series.Add(DateTime.UtcNow, i * 10);
for (int i = 1; i <= 10; i++)
{
series.Add(DateTime.UtcNow, i * 10);
}
// 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
// SMA(5)
+37 -3
View File
@@ -45,7 +45,9 @@ public sealed class Sma : AbstractBase
public Sma(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_period = period;
_buffer = new RingBuffer(period);
@@ -92,7 +94,10 @@ public sealed class Sma : AbstractBase
/// <param name="source">Historical data (only the last 'period' is actually needed)</param>
public override void Prime(ReadOnlySpan<double> source, TimeSpan? step = null)
{
if (source.Length == 0) return;
if (source.Length == 0)
{
return;
}
// Reset state
_buffer.Clear();
@@ -213,7 +218,10 @@ public sealed class Sma : AbstractBase
public override TSeries Update(TSeries source)
{
if (source.Count == 0) return [];
if (source.Count == 0)
{
return [];
}
int len = source.Count;
var t = new List<long>(len);
@@ -262,12 +270,20 @@ public sealed class Sma : AbstractBase
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period)
{
if (source.Length != output.Length)
{
throw new ArgumentException("Source and output must have the same length", nameof(output));
}
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
int len = source.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
// Try SIMD path for large, clean datasets
// Requirements: SIMD support, large enough dataset, no NaN values
@@ -345,9 +361,13 @@ public sealed class Sma : AbstractBase
{
double val = source[i];
if (double.IsFinite(val))
{
lastValid = val;
}
else
{
val = lastValid;
}
sum += val;
buffer[i] = val;
@@ -359,16 +379,22 @@ public sealed class Sma : AbstractBase
{
double val = source[i];
if (double.IsFinite(val))
{
lastValid = val;
}
else
{
val = lastValid;
}
sum = Math.FusedMultiplyAdd(-1.0, buffer[bufferIndex], sum + val);
buffer[bufferIndex] = val;
bufferIndex++;
if (bufferIndex >= period)
{
bufferIndex = 0;
}
output[i] = sum / period;
@@ -388,7 +414,9 @@ public sealed class Sma : AbstractBase
finally
{
if (rented != null)
{
ArrayPool<double>.Shared.Return(rented);
}
}
}
@@ -412,7 +440,9 @@ public sealed class Sma : AbstractBase
}
if (len <= period)
{
return;
}
var vInvPeriod = Vector512.Create(invPeriod);
int simdEnd = period + (len - period) / VectorWidth * VectorWidth;
@@ -486,7 +516,9 @@ public sealed class Sma : AbstractBase
}
if (len <= period)
{
return;
}
var vInvPeriod = Vector256.Create(invPeriod);
var vZero = Vector256<double>.Zero;
@@ -559,7 +591,9 @@ public sealed class Sma : AbstractBase
}
if (len <= period)
{
return;
}
var vInvPeriod = Vector128.Create(invPeriod);
int simdEnd = period + (len - period) / VectorWidth * VectorWidth;