using System.Collections.Generic;
namespace AsbCloudInfrastructure.Services.DetectOperations
{
public class InterpolationLine
{
private readonly double xSum;
private readonly double ySum;
private readonly double xySum;
private readonly double x2Sum;
private readonly int count;
public InterpolationLine(IEnumerable<(double Y, double X)> rawData)
{
var iterator = rawData.GetEnumerator();
while (iterator.MoveNext())
{
xSum += iterator.Current.X;
ySum += iterator.Current.Y;
xySum += iterator.Current.X * iterator.Current.Y;
x2Sum += iterator.Current.X * iterator.Current.X;
count++;
}
}
///
/// tan(alpha)
///
public double A =>
(xSum * ySum - count * xySum) /
(xSum * xSum - count * x2Sum);
///
/// y offset
///
public double B =>
(xSum * xySum - x2Sum * ySum) /
(xSum * xSum - count * x2Sum);
public bool IsYNotChanges(double upperBound = 0d, double lowerBound = 0d) =>
A < upperBound && A > lowerBound;
public bool IsYIncreases(double bound = 0d) =>
A > bound;
public bool IsYDecreases(double bound = 0d) =>
A < bound;
public bool IsAverageYLessThanBound(double bound) =>
(ySum / count) < bound;
public bool IsAverageYMoreThanBound(double bound) =>
(ySum / count) >= bound;
}
}