normalization of methods

This commit is contained in:
Miha Kralj
2026-02-10 21:33:16 -08:00
parent 915d7a007b
commit 6d6259a47d
527 changed files with 10525 additions and 2123 deletions
+6 -6
View File
@@ -109,7 +109,7 @@ public class SgmaTests
double[] input = series.Values.ToArray();
double[] output = new double[input.Length];
Sgma.Calculate(input.AsSpan(), output.AsSpan(), 9);
Sgma.Batch(input.AsSpan(), output.AsSpan(), 9);
for (int i = 0; i < input.Length; i++)
{
@@ -268,7 +268,7 @@ public class SgmaTests
var tValues = series.Values.ToArray();
var spanInput = new ReadOnlySpan<double>(tValues);
var spanOutput = new double[tValues.Length];
Sgma.Calculate(spanInput, spanOutput, period);
Sgma.Batch(spanInput, spanOutput, period);
double spanResult = spanOutput[^1];
var streamingInd = new Sgma(period);
@@ -298,9 +298,9 @@ public class SgmaTests
double[] output = new double[5];
double[] wrongSizeOutput = new double[3];
Assert.Throws<ArgumentException>(() => Sgma.Calculate(source.AsSpan(), output.AsSpan(), 2));
Assert.Throws<ArgumentException>(() => Sgma.Calculate(source.AsSpan(), output.AsSpan(), 5, 5));
Assert.Throws<ArgumentException>(() => Sgma.Calculate(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
Assert.Throws<ArgumentException>(() => Sgma.Batch(source.AsSpan(), output.AsSpan(), 2));
Assert.Throws<ArgumentException>(() => Sgma.Batch(source.AsSpan(), output.AsSpan(), 5, 5));
Assert.Throws<ArgumentException>(() => Sgma.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
}
[Fact]
@@ -309,7 +309,7 @@ public class SgmaTests
double[] source = [100, 110, double.NaN, 120, 130];
double[] output = new double[5];
Sgma.Calculate(source.AsSpan(), output.AsSpan(), 3);
Sgma.Batch(source.AsSpan(), output.AsSpan(), 3);
foreach (var val in output)
{
+3 -3
View File
@@ -65,7 +65,7 @@ public class SgmaValidationTests
double[] sgmaOutput = new double[input.Length];
double[] smaOutput = new double[input.Length];
Sgma.Calculate(input.AsSpan(), sgmaOutput.AsSpan(), 9, 0);
Sgma.Batch(input.AsSpan(), sgmaOutput.AsSpan(), 9, 0);
Sma.Batch(input.AsSpan(), smaOutput.AsSpan(), 9);
for (int i = 0; i < input.Length; i++)
@@ -209,7 +209,7 @@ public class SgmaValidationTests
// Span
double[] input = series.Values.ToArray();
double[] output = new double[input.Length];
Sgma.Calculate(input.AsSpan(), output.AsSpan(), 9, degree);
Sgma.Batch(input.AsSpan(), output.AsSpan(), 9, degree);
Assert.Equal(expected, output[^1], Tolerance);
// Streaming
@@ -238,7 +238,7 @@ public class SgmaValidationTests
double[] sourceWithNaN = [100, 110, 120, double.NaN, 140, 150, 160, 170, 180];
double[] output = new double[sourceWithNaN.Length];
Sgma.Calculate(sourceWithNaN.AsSpan(), output.AsSpan(), 5, 2);
Sgma.Batch(sourceWithNaN.AsSpan(), output.AsSpan(), 5, 2);
// All outputs should be finite
foreach (var val in output)
+17 -5
View File
@@ -25,6 +25,7 @@ public sealed class Sgma : AbstractBase
private readonly ITValuePublisher? _source;
private readonly TValuePublishedHandler? _pubHandler;
private bool _isNew = true;
private bool _disposed;
private double _lastValidValue = double.NaN;
private double _p_lastValidValue = double.NaN;
@@ -163,9 +164,13 @@ public sealed class Sgma : AbstractBase
protected override void Dispose(bool disposing)
{
if (disposing && _source != null && _pubHandler != null)
if (!_disposed)
{
_source.Pub -= _pubHandler;
if (disposing && _source != null && _pubHandler != null)
{
_source.Pub -= _pubHandler;
}
_disposed = true;
}
base.Dispose(disposing);
}
@@ -277,7 +282,7 @@ public sealed class Sgma : AbstractBase
var tSpan = CollectionsMarshal.AsSpan(t);
var vSpan = CollectionsMarshal.AsSpan(v);
Calculate(source.Values, vSpan, _period, _degree);
Batch(source.Values, vSpan, _period, _degree);
source.Times.CopyTo(tSpan);
// Restore state by replaying last period bars
@@ -317,7 +322,7 @@ public sealed class Sgma : AbstractBase
/// <param name="degree">Polynomial degree (default: 2)</param>
/// <exception cref="ArgumentException">Thrown when output length doesn't match source length.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int period = 9, int degree = 2)
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period = 9, int degree = 2)
{
if (period < 3)
{
@@ -422,6 +427,13 @@ public sealed class Sgma : AbstractBase
}
}
public static (TSeries Results, Sgma Indicator) Calculate(TSeries source, int period = 9, int degree = 2)
{
var indicator = new Sgma(period, degree);
TSeries results = indicator.Update(source);
return (results, indicator);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double CalculateWeightedSumFull(RingBuffer buffer, double[] weights, double invWeightSum, double fallbackValue)
{
@@ -557,4 +569,4 @@ public sealed class Sgma : AbstractBase
_p_lastValidValue = double.NaN;
Last = default;
}
}
}