|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace UnitsNet |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// A set of extension methods for some of the most common Math operations, such as Min, Max, Sum and Average |
| 9 | + /// </summary> |
| 10 | + public static class UnitMath |
| 11 | + { |
| 12 | + /// <summary>Computes the sum of a sequence of <typeparamref name="TQuantity" /> values.</summary> |
| 13 | + /// <param name="source">A sequence of <typeparamref name="TQuantity" /> values to calculate the sum of.</param> |
| 14 | + /// <param name="unitType">The desired unit type for the resulting quantity</param> |
| 15 | + /// <returns>The sum of the values in the sequence, represented in the specified unit type.</returns> |
| 16 | + /// <exception cref="T:System.ArgumentNullException"> |
| 17 | + /// <paramref name="source">source</paramref> is null. |
| 18 | + /// </exception> |
| 19 | + /// <exception cref="ArgumentException"> |
| 20 | + /// <paramref name="source">source</paramref> contains quantity types different from <paramref name="unitType" />. |
| 21 | + /// </exception> |
| 22 | + public static TQuantity Sum<TQuantity>(this IEnumerable<TQuantity> source, Enum unitType) |
| 23 | + where TQuantity : IQuantity |
| 24 | + { |
| 25 | + return (TQuantity) Quantity.From(source.Sum(x => x.As(unitType)), unitType); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Computes the sum of the sequence of <typeparamref name="TQuantity" /> values that are obtained by invoking a |
| 30 | + /// transform function on each element of the input sequence. |
| 31 | + /// </summary> |
| 32 | + /// <param name="source">A sequence of values that are used to calculate a sum.</param> |
| 33 | + /// <param name="selector">A transform function to apply to each element.</param> |
| 34 | + /// <param name="unitType">The desired unit type for the resulting quantity</param> |
| 35 | + /// <typeparam name="TSource">The type of the elements of source.</typeparam> |
| 36 | + /// <typeparam name="TQuantity">The type of quantity that is produced by this operation</typeparam> |
| 37 | + /// <returns>The sum of the projected values, represented in the specified unit type.</returns> |
| 38 | + /// <exception cref="T:System.ArgumentNullException"> |
| 39 | + /// <paramref name="source">source</paramref> or <paramref name="selector">selector</paramref> is null. |
| 40 | + /// </exception> |
| 41 | + /// <exception cref="ArgumentException"> |
| 42 | + /// <paramref name="source">source</paramref> contains quantity types different from <paramref name="unitType" />. |
| 43 | + /// </exception> |
| 44 | + public static TQuantity Sum<TSource, TQuantity>(this IEnumerable<TSource> source, Func<TSource, TQuantity> selector, Enum unitType) |
| 45 | + where TQuantity : IQuantity |
| 46 | + { |
| 47 | + return source.Select(selector).Sum(unitType); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary>Computes the min of a sequence of <typeparamref name="TQuantity" /> values.</summary> |
| 51 | + /// <param name="source">A sequence of <typeparamref name="TQuantity" /> values to calculate the min of.</param> |
| 52 | + /// <param name="unitType">The desired unit type for the resulting quantity</param> |
| 53 | + /// <returns>The min of the values in the sequence, represented in the specified unit type.</returns> |
| 54 | + /// <exception cref="T:System.ArgumentNullException"> |
| 55 | + /// <paramref name="source">source</paramref> is null. |
| 56 | + /// </exception> |
| 57 | + /// <exception cref="T:System.InvalidOperationException"><paramref name="source">source</paramref> contains no elements.</exception> |
| 58 | + /// <exception cref="ArgumentException"> |
| 59 | + /// <paramref name="source">source</paramref> contains quantity types different from <paramref name="unitType" />. |
| 60 | + /// </exception> |
| 61 | + public static TQuantity Min<TQuantity>(this IEnumerable<TQuantity> source, Enum unitType) |
| 62 | + where TQuantity : IQuantity |
| 63 | + { |
| 64 | + return (TQuantity) Quantity.From(source.Min(x => x.As(unitType)), unitType); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Computes the min of the sequence of <typeparamref name="TQuantity" /> values that are obtained by invoking a |
| 69 | + /// transform function on each element of the input sequence. |
| 70 | + /// </summary> |
| 71 | + /// <param name="source">A sequence of values that are used to calculate a min.</param> |
| 72 | + /// <param name="selector">A transform function to apply to each element.</param> |
| 73 | + /// <param name="unitType">The desired unit type for the resulting quantity</param> |
| 74 | + /// <typeparam name="TSource">The type of the elements of source.</typeparam> |
| 75 | + /// <typeparam name="TQuantity">The type of quantity that is produced by this operation</typeparam> |
| 76 | + /// <returns>The min of the projected values, represented in the specified unit type.</returns> |
| 77 | + /// <exception cref="T:System.ArgumentNullException"> |
| 78 | + /// <paramref name="source">source</paramref> or <paramref name="selector">selector</paramref> is null. |
| 79 | + /// </exception> |
| 80 | + /// <exception cref="T:System.InvalidOperationException"><paramref name="source">source</paramref> contains no elements.</exception> |
| 81 | + /// <exception cref="ArgumentException"> |
| 82 | + /// <paramref name="source">source</paramref> contains quantity types different from <paramref name="unitType" />. |
| 83 | + /// </exception> |
| 84 | + public static TQuantity Min<TSource, TQuantity>(this IEnumerable<TSource> source, Func<TSource, TQuantity> selector, Enum unitType) |
| 85 | + where TQuantity : IQuantity |
| 86 | + { |
| 87 | + return source.Select(selector).Min(unitType); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary>Computes the max of a sequence of <typeparamref name="TQuantity" /> values.</summary> |
| 91 | + /// <param name="source">A sequence of <typeparamref name="TQuantity" /> values to calculate the max of.</param> |
| 92 | + /// <param name="unitType">The desired unit type for the resulting quantity</param> |
| 93 | + /// <returns>The max of the values in the sequence, represented in the specified unit type.</returns> |
| 94 | + /// <exception cref="T:System.ArgumentNullException"> |
| 95 | + /// <paramref name="source">source</paramref> is null. |
| 96 | + /// </exception> |
| 97 | + /// <exception cref="T:System.InvalidOperationException"><paramref name="source">source</paramref> contains no elements.</exception> |
| 98 | + /// <exception cref="ArgumentException"> |
| 99 | + /// <paramref name="source">source</paramref> contains quantity types different from <paramref name="unitType" />. |
| 100 | + /// </exception> |
| 101 | + public static TQuantity Max<TQuantity>(this IEnumerable<TQuantity> source, Enum unitType) |
| 102 | + where TQuantity : IQuantity |
| 103 | + { |
| 104 | + return (TQuantity) Quantity.From(source.Max(x => x.As(unitType)), unitType); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Computes the max of the sequence of <typeparamref name="TQuantity" /> values that are obtained by invoking a |
| 109 | + /// transform function on each element of the input sequence. |
| 110 | + /// </summary> |
| 111 | + /// <param name="source">A sequence of values that are used to calculate a max.</param> |
| 112 | + /// <param name="selector">A transform function to apply to each element.</param> |
| 113 | + /// <param name="unitType">The desired unit type for the resulting quantity</param> |
| 114 | + /// <typeparam name="TSource">The type of the elements of source.</typeparam> |
| 115 | + /// <typeparam name="TQuantity">The type of quantity that is produced by this operation</typeparam> |
| 116 | + /// <returns>The max of the projected values, represented in the specified unit type.</returns> |
| 117 | + /// <exception cref="T:System.ArgumentNullException"> |
| 118 | + /// <paramref name="source">source</paramref> or <paramref name="selector">selector</paramref> is null. |
| 119 | + /// </exception> |
| 120 | + /// <exception cref="T:System.InvalidOperationException"><paramref name="source">source</paramref> contains no elements.</exception> |
| 121 | + /// <exception cref="ArgumentException"> |
| 122 | + /// <paramref name="source">source</paramref> contains quantity types different from <paramref name="unitType" />. |
| 123 | + /// </exception> |
| 124 | + public static TQuantity Max<TSource, TQuantity>(this IEnumerable<TSource> source, Func<TSource, TQuantity> selector, Enum unitType) |
| 125 | + where TQuantity : IQuantity |
| 126 | + { |
| 127 | + return source.Select(selector).Max(unitType); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary>Computes the average of a sequence of <typeparamref name="TQuantity" /> values.</summary> |
| 131 | + /// <param name="source">A sequence of <typeparamref name="TQuantity" /> values to calculate the average of.</param> |
| 132 | + /// <param name="unitType">The desired unit type for the resulting quantity</param> |
| 133 | + /// <returns>The average of the values in the sequence, represented in the specified unit type.</returns> |
| 134 | + /// <exception cref="T:System.ArgumentNullException"> |
| 135 | + /// <paramref name="source">source</paramref> is null. |
| 136 | + /// </exception> |
| 137 | + /// <exception cref="T:System.InvalidOperationException"><paramref name="source">source</paramref> contains no elements.</exception> |
| 138 | + /// <exception cref="ArgumentException"> |
| 139 | + /// <paramref name="source">source</paramref> contains quantity types different from <paramref name="unitType" />. |
| 140 | + /// </exception> |
| 141 | + public static TQuantity Average<TQuantity>(this IEnumerable<TQuantity> source, Enum unitType) |
| 142 | + where TQuantity : IQuantity |
| 143 | + { |
| 144 | + return (TQuantity) Quantity.From(source.Average(x => x.As(unitType)), unitType); |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Computes the average of the sequence of <typeparamref name="TQuantity" /> values that are obtained by invoking |
| 149 | + /// a transform function on each element of the input sequence. |
| 150 | + /// </summary> |
| 151 | + /// <param name="source">A sequence of values that are used to calculate an average.</param> |
| 152 | + /// <param name="selector">A transform function to apply to each element.</param> |
| 153 | + /// <param name="unitType">The desired unit type for the resulting quantity</param> |
| 154 | + /// <typeparam name="TSource">The type of the elements of source.</typeparam> |
| 155 | + /// <typeparam name="TQuantity">The type of quantity that is produced by this operation</typeparam> |
| 156 | + /// <returns>The average of the projected values, represented in the specified unit type.</returns> |
| 157 | + /// <exception cref="T:System.ArgumentNullException"> |
| 158 | + /// <paramref name="source">source</paramref> or <paramref name="selector">selector</paramref> is null. |
| 159 | + /// </exception> |
| 160 | + /// <exception cref="T:System.InvalidOperationException"><paramref name="source">source</paramref> contains no elements.</exception> |
| 161 | + /// <exception cref="ArgumentException"> |
| 162 | + /// <paramref name="source">source</paramref> contains quantity types different from <paramref name="unitType" />. |
| 163 | + /// </exception> |
| 164 | + public static TQuantity Average<TSource, TQuantity>(this IEnumerable<TSource> source, Func<TSource, TQuantity> selector, Enum unitType) |
| 165 | + where TQuantity : IQuantity |
| 166 | + { |
| 167 | + return source.Select(selector).Average(unitType); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments