code reviews

This commit is contained in:
Miha Kralj
2026-01-18 22:23:50 -08:00
parent 86fe32a682
commit 4673f48a70
40 changed files with 1155 additions and 309 deletions
@@ -87,27 +87,37 @@ public class LogtransValidationTests
}
[Fact]
public void Logtrans_ProductRule()
public void Logtrans_ZeroInput_UsesLastValid()
{
// ln(a*b) = ln(a) + ln(b)
double a = 2.5;
double b = 3.7;
// Zero input uses last valid value (robustness pattern)
var indicator = new Logtrans();
var time = DateTime.UtcNow;
indicator.Update(new TValue(time, a));
double lnA = indicator.Last.Value;
// First update with valid value
indicator.Update(new TValue(time, Math.E));
double lastValid = indicator.Last.Value; // ln(e) = 1.0
indicator.Reset();
indicator.Update(new TValue(time, b));
double lnB = indicator.Last.Value;
// Zero input - should use last valid
indicator.Update(new TValue(time.AddMinutes(1), 0.0));
indicator.Reset();
indicator.Update(new TValue(time, a * b));
double lnAB = indicator.Last.Value;
Assert.Equal(lastValid, indicator.Last.Value, Tolerance);
}
Assert.Equal(lnA + lnB, lnAB, Tolerance);
[Fact]
public void Logtrans_NegativeInput_UsesLastValid()
{
// Negative input uses last valid value (robustness pattern)
var indicator = new Logtrans();
var time = DateTime.UtcNow;
// First update with valid value
indicator.Update(new TValue(time, 2.0));
double lastValid = indicator.Last.Value; // ln(2)
// Negative input - should use last valid
indicator.Update(new TValue(time.AddMinutes(1), -1.0));
Assert.Equal(lastValid, indicator.Last.Value, Tolerance);
}
[Fact]
@@ -153,4 +163,111 @@ public class LogtransValidationTests
Assert.Equal(n * lnA, lnAPowN, Tolerance);
}
}
[Fact]
public void Logtrans_VerySmallPositive_ApproachesNegativeInfinity()
{
// ln(ε) → -∞ as ε → 0+
var indicator = new Logtrans();
var time = DateTime.UtcNow;
indicator.Update(new TValue(time, double.Epsilon));
double result = indicator.Last.Value;
Assert.True(double.IsFinite(result));
Assert.True(result < -700); // ln(double.Epsilon) ≈ -744
}
[Fact]
public void Logtrans_VeryLargeValue_Handles()
{
// ln(large) should be finite
var indicator = new Logtrans();
var time = DateTime.UtcNow;
indicator.Update(new TValue(time, 1e300));
double result = indicator.Last.Value;
Assert.True(double.IsFinite(result));
Assert.Equal(Math.Log(1e300), result, Tolerance);
}
[Fact]
public void Logtrans_Span_ZeroInput_UsesLastValid()
{
// Span API: zero input uses last valid value (robustness pattern)
var values = new double[] { 2.0, 0.0, 3.0 };
var output = new double[3];
Logtrans.Calculate(values, output);
Assert.Equal(Math.Log(2.0), output[0], Tolerance); // ln(2)
Assert.Equal(Math.Log(2.0), output[1], Tolerance); // zero -> uses last valid (ln(2))
Assert.Equal(Math.Log(3.0), output[2], Tolerance); // ln(3)
}
[Fact]
public void Logtrans_Span_NegativeInput_UsesLastValid()
{
// Span API: negative input uses last valid value (robustness pattern)
var values = new double[] { 2.0, -5.0, 3.0 };
var output = new double[3];
Logtrans.Calculate(values, output);
Assert.Equal(Math.Log(2.0), output[0], Tolerance); // ln(2)
Assert.Equal(Math.Log(2.0), output[1], Tolerance); // negative -> uses last valid (ln(2))
Assert.Equal(Math.Log(3.0), output[2], Tolerance); // ln(3)
}
[Fact]
public void Logtrans_NaNInput_UsesLastValid()
{
// NaN input uses last valid value (robustness pattern)
var indicator = new Logtrans();
var time = DateTime.UtcNow;
// First update with valid value
indicator.Update(new TValue(time, Math.E));
double lastValid = indicator.Last.Value; // ln(e) = 1.0
// NaN input - should use last valid
indicator.Update(new TValue(time.AddMinutes(1), double.NaN));
Assert.Equal(lastValid, indicator.Last.Value, Tolerance);
}
[Fact]
public void Logtrans_PositiveInfinityInput_UsesLastValid()
{
// Positive infinity input uses last valid value (robustness pattern)
var indicator = new Logtrans();
var time = DateTime.UtcNow;
// First update with valid value
indicator.Update(new TValue(time, 10.0));
double lastValid = indicator.Last.Value; // ln(10)
// Positive infinity input - should use last valid
indicator.Update(new TValue(time.AddMinutes(1), double.PositiveInfinity));
Assert.Equal(lastValid, indicator.Last.Value, Tolerance);
}
[Fact]
public void Logtrans_NegativeInfinityInput_UsesLastValid()
{
// Negative infinity input uses last valid value (robustness pattern)
var indicator = new Logtrans();
var time = DateTime.UtcNow;
// First update with valid value
indicator.Update(new TValue(time, 5.0));
double lastValid = indicator.Last.Value; // ln(5)
// Negative infinity input - should use last valid
indicator.Update(new TValue(time.AddMinutes(1), double.NegativeInfinity));
Assert.Equal(lastValid, indicator.Last.Value, Tolerance);
}
}
+3 -31
View File
@@ -2,9 +2,6 @@
// Transforms values using natural logarithm (base e)
using System.Runtime.CompilerServices;
using System.Numerics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
namespace QuanTAlib;
@@ -102,7 +99,8 @@ public sealed class Logtrans : AbstractBase
}
/// <summary>
/// Calculates natural logarithm over a span of values using SIMD when available.
/// Calculates natural logarithm over a span of values.
/// Note: Math.Log has no SIMD intrinsic; uses scalar path with last-valid substitution.
/// </summary>
public static void Calculate(ReadOnlySpan<double> source, Span<double> output)
{
@@ -112,34 +110,8 @@ public sealed class Logtrans : AbstractBase
throw new ArgumentException("Output length must be >= source length", nameof(output));
double lastValid = 0.0;
int i = 0;
// SIMD path for AVX2 (process 4 doubles at a time)
if (Avx2.IsSupported && source.Length >= Vector256<double>.Count)
{
int vectorLength = source.Length - (source.Length % Vector256<double>.Count);
for (; i < vectorLength; i += Vector256<double>.Count)
{
// Process scalar for proper last-valid handling (Logtrans has no SIMD intrinsic)
for (int j = 0; j < Vector256<double>.Count; j++)
{
double val = source[i + j];
if (double.IsFinite(val) && val > 0)
{
lastValid = Math.Log(val);
output[i + j] = lastValid;
}
else
{
output[i + j] = lastValid;
}
}
}
}
// Scalar fallback for remaining elements
for (; i < source.Length; i++)
for (int i = 0; i < source.Length; i++)
{
double val = source[i];
if (double.IsFinite(val) && val > 0)