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
+10 -10
View File
@@ -44,7 +44,7 @@ public class DemaTests
}
// Act
var demaSeries = Dema.Calculate(source, period);
var demaSeries = Dema.Batch(source, period);
var demaObj = new Dema(period);
// Assert
@@ -71,7 +71,7 @@ public class DemaTests
}
// Act
Dema.Calculate(source, output, period);
Dema.Batch(source, output, period);
var demaObj = new Dema(period);
// Assert
@@ -130,7 +130,7 @@ public class DemaTests
}
// Act
var demaSeries = Dema.Calculate(source, alpha);
var demaSeries = Dema.Batch(source, alpha);
var demaObj = new Dema(alpha);
// Assert
@@ -157,7 +157,7 @@ public class DemaTests
}
// Act
Dema.Calculate(source, output, alpha);
Dema.Batch(source, output, alpha);
var demaObj = new Dema(alpha);
// Assert
@@ -250,8 +250,8 @@ public class DemaTests
double[] output = new double[5];
double[] wrongSizeOutput = new double[3];
Assert.Throws<ArgumentException>(() => Dema.Calculate(source.AsSpan(), output.AsSpan(), 0));
Assert.Throws<ArgumentException>(() => Dema.Calculate(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
Assert.Throws<ArgumentException>(() => Dema.Batch(source.AsSpan(), output.AsSpan(), 0));
Assert.Throws<ArgumentException>(() => Dema.Batch(source.AsSpan(), wrongSizeOutput.AsSpan(), 3));
}
[Fact]
@@ -260,7 +260,7 @@ public class DemaTests
double[] source = [100, 110, double.NaN, 120, 130];
double[] output = new double[5];
Dema.Calculate(source.AsSpan(), output.AsSpan(), 3);
Dema.Batch(source.AsSpan(), output.AsSpan(), 3);
foreach (var val in output)
{
@@ -278,14 +278,14 @@ public class DemaTests
var series = bars.Close;
// 1. Batch Mode
var batchSeries = Dema.Calculate(series, period);
var batchSeries = Dema.Batch(series, period);
double expected = batchSeries.Last.Value;
// 2. Span Mode
var tValues = series.Values.ToArray();
var spanInput = new ReadOnlySpan<double>(tValues);
var spanOutput = new double[tValues.Length];
Dema.Calculate(spanInput, spanOutput, period);
Dema.Batch(spanInput, spanOutput, period);
double spanResult = spanOutput[^1];
// 3. Streaming Mode
@@ -317,7 +317,7 @@ public class DemaTests
double[] source = { double.NaN, double.NaN, 10.0, 11.0, 12.0 };
double[] output = new double[source.Length];
Dema.Calculate(source, output, 3);
Dema.Batch(source, output, 3);
// We expect the first two outputs to be NaN because the input was NaN
Assert.True(double.IsNaN(output[0]), $"Output[0] should be NaN, but was {output[0]}");
+1 -1
View File
@@ -140,7 +140,7 @@ public sealed class DemaValidationTests : IDisposable
{
// Calculate QuanTAlib DEMA (Span API)
double[] qOutput = new double[sourceData.Length];
global::QuanTAlib.Dema.Calculate(sourceData.AsSpan(), qOutput.AsSpan(), period);
global::QuanTAlib.Dema.Batch(sourceData.AsSpan(), qOutput.AsSpan(), period);
// Calculate TA-Lib DEMA
var retCode = TALib.Functions.Dema<double>(sourceData, 0..^0, talibOutput, out var outRange, period);
+13 -6
View File
@@ -230,19 +230,19 @@ public sealed class Dema : AbstractBase
return result;
}
public static TSeries Calculate(TSeries source, int period)
public static TSeries Batch(TSeries source, int period)
{
var dema = new Dema(period);
return dema.Update(source);
}
public static TSeries Calculate(TSeries source, double alpha)
public static TSeries Batch(TSeries source, double alpha)
{
var dema = new Dema(alpha);
return dema.Update(source);
}
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int period)
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period)
{
if (period <= 0)
{
@@ -250,10 +250,10 @@ public sealed class Dema : AbstractBase
}
double alpha = 2.0 / (period + 1);
Calculate(source, output, alpha);
Batch(source, output, alpha);
}
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, double alpha)
public static void Batch(ReadOnlySpan<double> source, Span<double> output, double alpha)
{
if (source.Length != output.Length)
{
@@ -348,6 +348,13 @@ public sealed class Dema : AbstractBase
}
}
public static (TSeries Results, Dema Indicator) Calculate(TSeries source, int period)
{
var indicator = new Dema(period);
TSeries results = indicator.Update(source);
return (results, indicator);
}
public override void Reset()
{
_state1 = EmaState.New();
@@ -369,4 +376,4 @@ public sealed class Dema : AbstractBase
}
private void Handle(object? sender, in TValueEventArgs e) => Update(e.Value, e.IsNew);
}
}