public void EnumStatsTest() { MultiPrecision <Pow2.N8>[] xs = new MultiPrecision <Pow2.N8>[] { 1, 3, 6, 2, 4 }; Assert.AreEqual(16, xs.Sum()); Assert.AreEqual(16 / 5.0, (double)xs.Average(), 1e-10); Assert.AreEqual(2.96, (double)xs.Variance(), 1e-10); }
public static void SmoothnessSatisfied <N>(IEnumerable <MultiPrecision <N> > vs, double safe_error) where N : struct, IConstant { if (vs.Count() < 3) { throw new InvalidOperationException("Sequence contains less 3 elements"); } MultiPrecision <N>[] us = vs.ToArray(); MultiPrecision <N>[] diff = new MultiPrecision <N> [us.Length - 1]; for (int i = 1; i < us.Length; i++) { MultiPrecision <N> a = us[i - 1], b = us[i]; diff[i - 1] = a - b; } diff = diff.Where((d) => d.IsFinite).ToArray(); MultiPrecision <N> diff_avg = diff.Average(); MultiPrecision <N> diff_absave = diff.Select((d) => MultiPrecision <N> .Abs(d)).Average(); MultiPrecision <N> diff_nonzeromin = diff.Where((d) => !d.IsZero).Select((d) => MultiPrecision <N> .Abs(d)).Min(); MultiPrecision <N> error_range = diff_absave * safe_error + diff_nonzeromin; if (diff_absave.IsZero || error_range.IsZero) { Console.WriteLine("Smoothness satisfied. (No error)"); return; } MultiPrecision <N> max_error_rate = diff.Select((d) => MultiPrecision <N> .Abs(d - diff_avg) / error_range).Max(); if (max_error_rate < safe_error) { Console.WriteLine($"Smoothness satisfied. (Max Error Rate:{max_error_rate:E4})"); return; } else { Assert.Fail($"Smoothness not satisfied. (Max Error Rate:{max_error_rate:E4})"); } }