diff --git a/src/Numerics/Statistics/SortedArrayStatistics.cs b/src/Numerics/Statistics/SortedArrayStatistics.cs index a0639d97..1c105b32 100644 --- a/src/Numerics/Statistics/SortedArrayStatistics.cs +++ b/src/Numerics/Statistics/SortedArrayStatistics.cs @@ -54,6 +54,20 @@ namespace MathNet.Numerics.Statistics return data[0]; } + /// + /// Returns the smallest value from the sorted data array (ascending). + /// + /// Sample array, must be sorted ascendingly. + public static float Minimum(float[] data) + { + if (data.Length == 0) + { + return float.NaN; + } + + return data[0]; + } + /// /// Returns the largest value from the sorted data array (ascending). /// @@ -68,6 +82,20 @@ namespace MathNet.Numerics.Statistics return data[data.Length - 1]; } + /// + /// Returns the largest value from the sorted data array (ascending). + /// + /// Sample array, must be sorted ascendingly. + public static float Maximum(float[] data) + { + if (data.Length == 0) + { + return float.NaN; + } + + return data[data.Length - 1]; + } + /// /// Returns the order statistic (order 1..N) from the sorted data array (ascending). /// diff --git a/src/Numerics/Statistics/Statistics.cs b/src/Numerics/Statistics/Statistics.cs index 981d2ef5..d22ede28 100644 --- a/src/Numerics/Statistics/Statistics.cs +++ b/src/Numerics/Statistics/Statistics.cs @@ -53,6 +53,21 @@ namespace MathNet.Numerics.Statistics : StreamingStatistics.Minimum(data); } + /// + /// Returns the minimum value in the sample data. + /// Returns NaN if data is empty or if any entry is NaN. + /// + /// The sample data. + /// The minimum value in the sample data. + public static float Minimum(this IEnumerable data) + { + var array = data as float[]; + return array != null + ? ArrayStatistics.Minimum(array) + : StreamingStatistics.Minimum(data); + } + + /// /// Returns the minimum value in the sample data. /// Returns NaN if data is empty or if any entry is NaN. @@ -79,6 +94,20 @@ namespace MathNet.Numerics.Statistics : StreamingStatistics.Maximum(data); } + /// + /// Returns the maximum value in the sample data. + /// Returns NaN if data is empty or if any entry is NaN. + /// + /// The sample data. + /// The maximum value in the sample data. + public static float Maximum(this IEnumerable data) + { + var array = data as float[]; + return array != null + ? ArrayStatistics.Maximum(array) + : StreamingStatistics.Maximum(data); + } + /// /// Returns the maximum value in the sample data. /// Returns NaN if data is empty or if any entry is NaN. diff --git a/src/Numerics/Statistics/StreamingStatistics.cs b/src/Numerics/Statistics/StreamingStatistics.cs index 7ec802bb..538bd560 100644 --- a/src/Numerics/Statistics/StreamingStatistics.cs +++ b/src/Numerics/Statistics/StreamingStatistics.cs @@ -66,6 +66,29 @@ namespace MathNet.Numerics.Statistics return any ? min : double.NaN; } + /// + /// Returns the smallest value from the enumerable, in a single pass without memoization. + /// Returns NaN if data is empty or any entry is NaN. + /// + /// Sample stream, no sorting is assumed. + public static float Minimum(IEnumerable stream) + { + var min = float.PositiveInfinity; + bool any = false; + + foreach (var d in stream) + { + if (d < min || float.IsNaN(d)) + { + min = d; + } + + any = true; + } + + return any ? min : float.NaN; + } + /// /// Returns the largest value from the enumerable, in a single pass without memoization. /// Returns NaN if data is empty or any entry is NaN. @@ -89,6 +112,29 @@ namespace MathNet.Numerics.Statistics return any ? max : double.NaN; } + /// + /// Returns the largest value from the enumerable, in a single pass without memoization. + /// Returns NaN if data is empty or any entry is NaN. + /// + /// Sample stream, no sorting is assumed. + public static float Maximum(IEnumerable stream) + { + var max = float.NegativeInfinity; + bool any = false; + + foreach (var d in stream) + { + if (d > max || float.IsNaN(d)) + { + max = d; + } + + any = true; + } + + return any ? max : float.NaN; + } + /// /// Estimates the arithmetic sample mean from the enumerable, in a single pass without memoization. /// Returns NaN if data is empty or any entry is NaN. diff --git a/src/UnitTests/StatisticsTests/StatisticsTests.cs b/src/UnitTests/StatisticsTests/StatisticsTests.cs index 3943609f..73d0dace 100644 --- a/src/UnitTests/StatisticsTests/StatisticsTests.cs +++ b/src/UnitTests/StatisticsTests/StatisticsTests.cs @@ -291,6 +291,22 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests Assert.That(SortedArrayStatistics.Maximum(samples), Is.EqualTo(10), "Max"); } + [Test] + public void MinimumMaximumOnShortSequence32() + { + var samples = new[] { -1.0f, 5f, 0f, -3f, 10f, -0.5f, 4f }; + Assert.That(Statistics.Minimum(samples), Is.EqualTo(-3), "Min"); + Assert.That(Statistics.Maximum(samples), Is.EqualTo(10), "Max"); + Assert.That(ArrayStatistics.Minimum(samples), Is.EqualTo(-3), "Min"); + Assert.That(ArrayStatistics.Maximum(samples), Is.EqualTo(10), "Max"); + Assert.That(StreamingStatistics.Minimum(samples), Is.EqualTo(-3), "Min"); + Assert.That(StreamingStatistics.Maximum(samples), Is.EqualTo(10), "Max"); + + Array.Sort(samples); + Assert.That(SortedArrayStatistics.Minimum(samples), Is.EqualTo(-3), "Min"); + Assert.That(SortedArrayStatistics.Maximum(samples), Is.EqualTo(10), "Max"); + } + [Test] public void OrderStatisticsOnShortSequence() {