DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/InterpolationLine.cs

63 lines
2.1 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Linq;
namespace AsbCloudInfrastructure.Services
{
2021-07-20 11:24:57 +05:00
public class InterpolationLine
{
2021-07-20 11:24:57 +05:00
private readonly decimal xSum;
private readonly decimal ySum;
private readonly decimal xySum;
private readonly decimal x2Sum;
private readonly IEnumerable<(double value, double timestamp)> rawData = new List<(double value, double timestamp)>();
public InterpolationLine(IEnumerable<(double value, double timestamp)> rawData)
{
2021-07-20 11:24:57 +05:00
var data = rawData.Select((d) => new
{
X = d.timestamp,
Y = d.value
});
xSum = (decimal)data.Sum(d => d.X);
ySum = (decimal)data.Sum(d => d.Y);
xySum = (decimal)data.Sum(d => d.X * d.Y);
x2Sum = (decimal)data.Sum(d => d.X * d.X);
2021-07-20 11:24:57 +05:00
this.rawData = rawData;
}
public double GetAForLinearFormula()
{
var result = (xSum * ySum - rawData.Count() * xySum) /
(xSum * xSum - rawData.Count() * x2Sum);
return (double)result;
}
2021-07-20 11:24:57 +05:00
public double GetBForLinearFormula()
{
var result = (xSum * xySum - x2Sum * ySum) /
(xSum * xSum - rawData.Count() * x2Sum);
return (double)result;
}
2021-07-21 15:29:19 +05:00
public static bool IsValueNotChanges(double value,
(double upperBound, double lowerBound) bounds) =>
value < bounds.upperBound && value > bounds.lowerBound;
2021-07-20 11:24:57 +05:00
public static bool IsValueIncreases(double value, double bound) =>
value > bound;
2021-07-20 11:24:57 +05:00
public static bool IsValueDecreases(double value, double bound) =>
value < bound;
2021-07-21 15:29:19 +05:00
public static bool IsAverageLessThanBound(IEnumerable<(double Value, double TotalSeconds)> values,
double bound) =>
(values.Sum(s => s.Value) / values.Count()) < bound;
public static bool IsAverageMoreThanBound(IEnumerable<(double Value, double TotalSeconds)> values,
double bound) =>
(values.Sum(s => s.Value) / values.Count()) >= bound;
}
}