2019-06-16 13:57:30 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
|
|
namespace UglyToad.PdfPig.DocumentLayoutAnalysis
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Useful math extensions.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class MathExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Computes the mode of a sequence of float values.
|
|
|
|
|
|
/// </summary>
|
2019-06-18 20:48:49 +01:00
|
|
|
|
/// <param name="array">The array of floats.</param>
|
2019-06-16 13:57:30 +01:00
|
|
|
|
public static float Mode(this IEnumerable<float> array)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (array == null || array.Count() == 0) return float.NaN;
|
|
|
|
|
|
return array.GroupBy(v => v).OrderByDescending(g => g.Count()).First().Key;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Computes the mode of a sequence of decimal values.
|
|
|
|
|
|
/// </summary>
|
2019-06-18 20:48:49 +01:00
|
|
|
|
/// <param name="array">The array of decimal.</param>
|
2019-06-16 13:57:30 +01:00
|
|
|
|
public static decimal Mode(this IEnumerable<decimal> array)
|
|
|
|
|
|
{
|
|
|
|
|
|
return array.GroupBy(v => v).OrderByDescending(g => g.Count()).First().Key;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|