mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-18 02:28:05 +00:00
Class optimization
This commit is contained in:
+25
-20
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
@@ -29,6 +29,7 @@ namespace QuanTAlib;
|
||||
public class Sinema : AbstractBase
|
||||
{
|
||||
private readonly Convolution _convolution;
|
||||
private readonly double[] _kernel;
|
||||
|
||||
/// <param name="period">The number of data points used in the SINEMA calculation.</param>
|
||||
/// <exception cref="ArgumentException">Thrown when period is less than 1.</exception>
|
||||
@@ -36,9 +37,10 @@ public class Sinema : AbstractBase
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentException("Period must be greater than or equal to 1.", nameof(period));
|
||||
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
|
||||
}
|
||||
_convolution = new Convolution(GenerateKernel(period));
|
||||
_kernel = GenerateKernel(period);
|
||||
_convolution = new Convolution(_kernel);
|
||||
Name = "Sinema";
|
||||
WarmupPeriod = period;
|
||||
Init();
|
||||
@@ -52,12 +54,14 @@ public class Sinema : AbstractBase
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private new void Init()
|
||||
{
|
||||
base.Init();
|
||||
_convolution.Init();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
@@ -67,42 +71,43 @@ public class Sinema : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
// Use Convolution for calculation
|
||||
TValue convolutionResult = _convolution.Calc(Input);
|
||||
|
||||
double result = convolutionResult.Value;
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates the sine-based convolution kernel for the SINEMA calculation.
|
||||
/// </summary>
|
||||
/// <param name="period">The period for which to generate the kernel.</param>
|
||||
/// <returns>An array of normalized sine-based weights for the convolution operation.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double[] GenerateKernel(int period)
|
||||
{
|
||||
double[] kernel = new double[period];
|
||||
double weightSum = 0;
|
||||
double piDivPeriodPlus1 = System.Math.PI / (period + 1);
|
||||
|
||||
// Calculate weights and sum in one pass
|
||||
for (int i = 0; i < period; i++)
|
||||
{
|
||||
// Use sine function to generate weights
|
||||
kernel[i] = Math.Sin((i + 1) * Math.PI / (period + 1));
|
||||
kernel[i] = System.Math.Sin((i + 1) * piDivPeriodPlus1);
|
||||
weightSum += kernel[i];
|
||||
}
|
||||
|
||||
// Normalize the kernel
|
||||
// Normalize using multiplication instead of division
|
||||
double invWeightSum = 1.0 / weightSum;
|
||||
for (int i = 0; i < period; i++)
|
||||
{
|
||||
kernel[i] /= weightSum;
|
||||
kernel[i] *= invWeightSum;
|
||||
}
|
||||
|
||||
return kernel;
|
||||
}
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
// Use Convolution for calculation
|
||||
var convolutionResult = _convolution.Calc(Input);
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
|
||||
return convolutionResult.Value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user