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
+9 -9
View File
@@ -115,7 +115,7 @@ public class AlmaTests
double[] input = series.Values.ToArray();
double[] output = new double[input.Length];
Alma.Calculate(input.AsSpan(), output.AsSpan(), 10);
Alma.Batch(input.AsSpan(), output.AsSpan(), 10);
for (int i = 0; i < input.Length; i++)
{
@@ -284,7 +284,7 @@ public class AlmaTests
var tValues = series.Values.ToArray();
var spanInput = new ReadOnlySpan<double>(tValues);
var spanOutput = new double[tValues.Length];
Alma.Calculate(spanInput, spanOutput, period);
Alma.Batch(spanInput, spanOutput, period);
double spanResult = spanOutput[^1];
// 3. Streaming Mode
@@ -317,12 +317,12 @@ public class AlmaTests
double[] output = new double[5];
double[] wrongSizeOutput = new double[3];
Assert.Throws<ArgumentException>(() => Alma.Calculate(source.AsSpan(), output.AsSpan(), 0));
Assert.Throws<ArgumentException>(() => Alma.Calculate(source.AsSpan(), output.AsSpan(), 3, sigma: 0));
Assert.Throws<ArgumentException>(() => Alma.Calculate(source.AsSpan(), output.AsSpan(), 3, sigma: -1));
Assert.Throws<ArgumentOutOfRangeException>(() => Alma.Calculate(source.AsSpan(), output.AsSpan(), 3, offset: -0.1));
Assert.Throws<ArgumentOutOfRangeException>(() => Alma.Calculate(source.AsSpan(), output.AsSpan(), 3, offset: 1.1));
Assert.Throws<ArgumentException>(() => Alma.Calculate(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
Assert.Throws<ArgumentException>(() => Alma.Batch(source.AsSpan(), output.AsSpan(), 0));
Assert.Throws<ArgumentException>(() => Alma.Batch(source.AsSpan(), output.AsSpan(), 3, sigma: 0));
Assert.Throws<ArgumentException>(() => Alma.Batch(source.AsSpan(), output.AsSpan(), 3, sigma: -1));
Assert.Throws<ArgumentOutOfRangeException>(() => Alma.Batch(source.AsSpan(), output.AsSpan(), 3, offset: -0.1));
Assert.Throws<ArgumentOutOfRangeException>(() => Alma.Batch(source.AsSpan(), output.AsSpan(), 3, offset: 1.1));
Assert.Throws<ArgumentException>(() => Alma.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
}
[Fact]
@@ -331,7 +331,7 @@ public class AlmaTests
double[] source = [100, 110, double.NaN, 120, 130];
double[] output = new double[5];
Alma.Calculate(source.AsSpan(), output.AsSpan(), 3);
Alma.Batch(source.AsSpan(), output.AsSpan(), 3);
foreach (var val in output)
{
+1 -1
View File
@@ -102,7 +102,7 @@ public sealed class AlmaValidationTests : IDisposable
{
// Calculate QuanTAlib ALMA (Span API)
double[] qOutput = new double[sourceData.Length];
global::QuanTAlib.Alma.Calculate(sourceData, qOutput.AsSpan(), period, offset, sigma);
global::QuanTAlib.Alma.Batch(sourceData, qOutput.AsSpan(), period, offset, sigma);
// Calculate Skender ALMA
var sResult = _testData.SkenderQuotes.GetAlma(period, offset, sigma).ToList();
+17 -5
View File
@@ -26,6 +26,7 @@ public sealed class Alma : AbstractBase
private readonly ITValuePublisher? _source;
private readonly TValuePublishedHandler? _pubHandler;
private bool _isNew = true;
private bool _disposed;
[StructLayout(LayoutKind.Auto)]
private record struct State(double LastValidValue, bool IsInitialized);
@@ -83,9 +84,13 @@ public sealed class Alma : 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);
}
@@ -181,7 +186,7 @@ public sealed class Alma : AbstractBase
var tSpan = CollectionsMarshal.AsSpan(t);
var vSpan = CollectionsMarshal.AsSpan(v);
Calculate(source.Values, vSpan, _period, _offset, _sigma);
Batch(source.Values, vSpan, _period, _offset, _sigma);
source.Times.CopyTo(tSpan);
// Restore state
@@ -305,7 +310,7 @@ public sealed class Alma : AbstractBase
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int period, double offset = 0.85, double sigma = 6.0)
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period, double offset = 0.85, double sigma = 6.0)
{
if (period <= 0)
{
@@ -433,6 +438,13 @@ public sealed class Alma : AbstractBase
}
}
public static (TSeries Results, Alma Indicator) Calculate(TSeries source, int period, double offset = 0.85, double sigma = 6.0)
{
var indicator = new Alma(period, offset, sigma);
TSeries results = indicator.Update(source);
return (results, indicator);
}
public override void Reset()
{
_buffer.Clear();
@@ -440,4 +452,4 @@ public sealed class Alma : AbstractBase
_p_state = _state;
Last = default;
}
}
}