Commit 271a7bd3 authored by 潘栩锋's avatar 潘栩锋 🚴
parents 975f48ba 78c4f7f6
......@@ -72,7 +72,10 @@ namespace Misc
{
return Max(buf, 0, buf.Count() - 1);
}
public static double Max(IEnumerable<double> buf)
{
return Max(buf, 0, buf.Count() - 1);
}
/// <summary>
/// 排除 NULL_VALUE, 求最大值
/// </summary>
......@@ -95,6 +98,21 @@ namespace Misc
}
return max;
}
public static double Max(IEnumerable<double> buf, int first, int last)
{
double max = double.NaN;
for (int i = first; i <= last; i++)
{
if (!double.IsNaN(buf.ElementAt(i)))
{
if (max == double.NaN)
max = buf.ElementAt(i);
else if (max < buf.ElementAt(i))
max = buf.ElementAt(i);
}
}
return max;
}
/// <summary>
/// 排除 NULL_VALUE, 求最小值
/// </summary>
......@@ -104,13 +122,16 @@ namespace Misc
{
return Min(buf, 0, buf.Count() - 1);
}
/// <summary>
/// 排除 NULL_VALUE, 求最小值
/// </summary>
/// <param name="buf"></param>
/// <param name="first">开始序号</param>
/// <param name="last">结束序号</param>
/// <returns></returns>
public static double Min(IEnumerable<double> buf)
{
return Min(buf, 0, buf.Count() - 1);
} /// <summary>
/// 排除 NULL_VALUE, 求最小值
/// </summary>
/// <param name="buf"></param>
/// <param name="first">开始序号</param>
/// <param name="last">结束序号</param>
/// <returns></returns>
public static int Min(IEnumerable<int> buf, int first, int last)
{
int min = MyBase.NULL_VALUE;
......@@ -127,6 +148,22 @@ namespace Misc
}
return min;
}
public static double Min(IEnumerable<double> buf, int first, int last)
{
double min = double.NaN;
for (int i = first; i <= last; i++)
{
if (!double.IsNaN(buf.ElementAt(i)))
{
if (min == double.NaN)
min = buf.ElementAt(i);
else if (min > buf.ElementAt(i))
min = buf.ElementAt(i);
}
}
return min;
}
/// <summary>
/// 排除 NULL_VALUE, 求平均值
/// </summary>
......@@ -136,6 +173,10 @@ namespace Misc
{
return Avg(buf, 0, buf.Count() - 1);
}
public static double Avg(IEnumerable<double> buf)
{
return Avg(buf, 0, buf.Count() - 1);
}
/// <summary>
/// 排除 NULL_VALUE, 求平均值
/// </summary>
......@@ -164,7 +205,27 @@ namespace Misc
else
return MyBase.NULL_VALUE;
}
public static double Avg(IEnumerable<double> buf, int first, int last)
{
double sum = 0;
int cnt = 0;
for (int i = first; i <= last; i++)
{
if ((i >= 0) && (i < buf.Count()))
{
if (!double.IsNaN(buf.ElementAt(i)))
{
sum += buf.ElementAt(i);
cnt++;
}
}
}
if (cnt != 0)
return sum / cnt;
else
return double.NaN;
}
/// <summary>
/// 排除 NULL_VALUE, 求平均值, 无方向 last &lt; first 也可以
/// </summary>
......@@ -397,6 +458,42 @@ namespace Misc
}
}
public static void Linest(IEnumerable<double> y, IEnumerable<double> x, out double a, out double b)
{
if (y.Count() != x.Count())
{
throw new Exception("Linest() 参数异常 (y.Length=" + y.Count() + ") != (x.Length=" + x.Count() + ")");
}
double sum_x = 0;
double sum_y = 0;
double sum_x2 = 0;
double sum_xy = 0;
int n = y.Count();
int cnt = 0;
for (int i = 0; i < n; i++)
{
if (!double.IsNaN(x.ElementAt(i)) && !double.IsNaN(y.ElementAt(i)))
{
sum_xy += y.ElementAt(i) * x.ElementAt(i);
sum_x2 += x.ElementAt(i) * x.ElementAt(i);
sum_x += x.ElementAt(i);
sum_y += y.ElementAt(i);
cnt++;
}
}
if (cnt > 1)
{
a = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x * sum_x);
b = sum_y / n - a * sum_x / n;
}
else
{
a = 1;
b = 0;
}
}
/// <summary>
/// 对环形数据 分区移位
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment