 Timestamp:
 11/13/15 21:02:54 (9 years ago)
 Location:
 stable
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

stable
 Property svn:mergeinfo changed
/trunk/sources merged: 13034,13051
 Property svn:mergeinfo changed

stable/HeuristicLab.Common/3.3/EnumerableStatisticExtensions.cs
r13150 r13151 65 65 66 66 // Numerical Recipes in C++, §8.5 Selecting the Mth Largest, O(n) 67 // Gi ben k in [0..n1] returns an array value from array arr[0..n1] such that k array values are67 // Given k in [0..n1] returns an array value from array arr[0..n1] such that k array values are 68 68 // less than or equal to the one returned. The input array will be rearranged to have this value in 69 69 // location arr[k], with all smaller elements moved to arr[0..k1] (in arbitrary order) and all 70 70 // larger elements in arr[k+1..n1] (also in arbitrary order). 71 // 72 // Could be changed to Select<T> where T is IComparable but in this case is significantly slower for double values 71 73 private static double Select(int k, double[] arr) { 72 74 Contract.Assert(arr.GetLowerBound(0) == 0); … … 198 200 } 199 201 200 /// <summary>201 /// Calculates the pth percentile of the values.202 /// </summary>203 public static double Percentile(this IEnumerable<double> values, double p) {204 // iterate only once205 double[] valuesArr = values.ToArray();206 int n = valuesArr.Length;207 if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");208 if (n == 1) return values.ElementAt(0);209 210 if (p.IsAlmost(0.0)) return valuesArr[0];211 if (p.IsAlmost(1.0)) return valuesArr[n  1];212 213 double t = p * (n  1);214 int index = (int)Math.Floor(t);215 double percentage = t  index;216 return valuesArr[index] * (1  percentage) + valuesArr[index + 1] * percentage;217 }218 219 202 public static IEnumerable<double> LimitToRange(this IEnumerable<double> values, double min, double max) { 220 203 if (min > max) throw new ArgumentException(string.Format("Minimum {0} is larger than maximum {1}.", min, max));
Note: See TracChangeset
for help on using the changeset viewer.