mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
31 lines
987 B
C#
31 lines
987 B
C#
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>
|
|
/// <param name="array">The array of floats.</param>
|
|
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>
|
|
/// <param name="array">The array of decimal.</param>
|
|
public static decimal Mode(this IEnumerable<decimal> array)
|
|
{
|
|
return array.GroupBy(v => v).OrderByDescending(g => g.Count()).First().Key;
|
|
}
|
|
}
|
|
}
|