|
2 | 2 | // Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
|
3 | 3 |
|
4 | 4 | #if NET7_0_OR_GREATER
|
| 5 | +using System; |
5 | 6 | using System.Collections.Generic;
|
6 |
| -using System.Linq; |
7 | 7 | using System.Numerics;
|
8 | 8 |
|
9 | 9 | namespace UnitsNet.GenericMath;
|
10 | 10 |
|
11 | 11 | /// <summary>
|
12 |
| -/// Provides generic math operations to test out the new generic math interfaces implemented in .NET7 for UnitsNet |
13 |
| -/// quantities using <see cref="double" /> as the internal value type, which is the majority of quantities. |
| 12 | +/// Provides generic math operations using the generic math interfaces implemented in .NET7 for UnitsNet. |
14 | 13 | /// </summary>
|
15 | 14 | public static class GenericMathExtensions
|
16 | 15 | {
|
17 | 16 | /// <summary>
|
18 |
| - /// Returns the sum of values. |
| 17 | + /// Returns the sum of a sequence of vector quantities, such as Mass and Length. |
19 | 18 | /// </summary>
|
| 19 | + /// <param name="source">The values.</param> |
| 20 | + /// <typeparam name="TQuantity">The type of the quantity elements in the source sequence.</typeparam> |
| 21 | + /// <returns>The sum of the quantities, using the unit of the first element in the sequence.</returns> |
20 | 22 | /// <remarks>
|
21 |
| - /// This method is experimental and intended to test out the new generic math interfaces implemented in .NET7 for |
22 |
| - /// UnitsNet quantities.<br /> |
23 |
| - /// Generic math interfaces might replace <see cref="UnitMath" />.<br /> |
24 |
| - /// Generic math LINQ support is still missing in the BCL, but is being worked on: |
| 23 | + /// Note that the generic math LINQ support is still missing in the BCL, but is being worked on: |
25 | 24 | /// <a href="https://github.com/dotnet/runtime/issues/64031">
|
26 |
| - /// API Proposal: Generic LINQ Numeric Operators · Issue #64031 · dotnet/runtime |
| 25 | + /// API Proposal: Generic LINQ Numeric Operators · Issue |
| 26 | + /// #64031 · dotnet/runtime |
27 | 27 | /// </a>
|
28 | 28 | /// </remarks>
|
29 |
| - /// <param name="source">The values.</param> |
30 |
| - /// <typeparam name="T">The type of value.</typeparam> |
31 |
| - /// <returns>The sum.</returns> |
32 |
| - public static T Sum<T>(this IEnumerable<T> source) |
33 |
| - where T : IAdditionOperators<T, T, T>, IAdditiveIdentity<T, T> |
| 29 | + public static TQuantity Sum<TQuantity>(this IEnumerable<TQuantity> source) |
| 30 | + where TQuantity : IQuantity, IAdditionOperators<TQuantity, TQuantity, TQuantity>, IAdditiveIdentity<TQuantity, TQuantity> |
34 | 31 | {
|
35 |
| - // Put accumulator on right hand side of the addition operator to construct quantities with the same unit as the values. |
36 |
| - // The addition operator implementation picks the unit from the left hand side, and the additive identity (e.g. Length.Zero) is always the base unit. |
37 |
| - return source.Aggregate(T.AdditiveIdentity, (acc, item) => item + acc); |
| 32 | + using IEnumerator<TQuantity> e = source.GetEnumerator(); |
| 33 | + if (!e.MoveNext()) |
| 34 | + { |
| 35 | + return TQuantity.AdditiveIdentity; |
| 36 | + } |
| 37 | + |
| 38 | + TQuantity result = e.Current; |
| 39 | + while (e.MoveNext()) |
| 40 | + { |
| 41 | + result += e.Current; |
| 42 | + } |
| 43 | + |
| 44 | + return result; |
38 | 45 | }
|
39 | 46 |
|
40 | 47 | /// <summary>
|
41 |
| - /// Returns the average of values. |
| 48 | + /// Calculates the arithmetic average of a sequence of vector quantities, such as Mass and Length. |
42 | 49 | /// </summary>
|
| 50 | + /// <param name="source">The values.</param> |
| 51 | + /// <typeparam name="TQuantity">The type of the quantity elements in the source sequence.</typeparam> |
| 52 | + /// <returns>The average of the quantities, using the unit of the first element in the sequence.</returns> |
| 53 | + /// <exception cref="InvalidOperationException">Thrown when the sequence is empty.</exception> |
43 | 54 | /// <remarks>
|
44 |
| - /// This method is experimental and intended to test out the new generic math interfaces implemented in .NET7 for |
45 |
| - /// UnitsNet quantities.<br /> |
46 |
| - /// Generic math interfaces might replace <see cref="UnitMath" />.<br /> |
47 |
| - /// Generic math LINQ support is still missing in the BCL, but is being worked on: |
| 55 | + /// Note that the generic math LINQ support is still missing in the BCL, but is being worked on: |
48 | 56 | /// <a href="https://github.com/dotnet/runtime/issues/64031">
|
49 | 57 | /// API Proposal: Generic LINQ Numeric Operators · Issue
|
50 | 58 | /// #64031 · dotnet/runtime
|
51 | 59 | /// </a>
|
52 | 60 | /// </remarks>
|
53 |
| - /// <param name="source">The values.</param> |
54 |
| - /// <typeparam name="T">The value type.</typeparam> |
55 |
| - /// <returns>The average.</returns> |
56 |
| - public static T Average<T>(this IEnumerable<T> source) |
57 |
| - where T : IAdditionOperators<T, T, T>, IAdditiveIdentity<T, T>, IDivisionOperators<T, double, T> |
| 61 | + public static TQuantity Average<TQuantity>(this IEnumerable<TQuantity> source) |
| 62 | + where TQuantity : IQuantity, IAdditionOperators<TQuantity, TQuantity, TQuantity>, IAdditiveIdentity<TQuantity, TQuantity>, |
| 63 | + IDivisionOperators<TQuantity, double, TQuantity> |
58 | 64 | {
|
59 |
| - // Put accumulator on right hand side of the addition operator to construct quantities with the same unit as the values. |
60 |
| - // The addition operator implementation picks the unit from the left hand side, and the additive identity (e.g. Length.Zero) is always the base unit. |
61 |
| - (T value, int count) result = source.Aggregate( |
62 |
| - (value: T.AdditiveIdentity, count: 0), |
63 |
| - (acc, item) => (value: item + acc.value, count: acc.count + 1)); |
| 65 | + using IEnumerator<TQuantity> e = source.GetEnumerator(); |
| 66 | + if (!e.MoveNext()) |
| 67 | + { |
| 68 | + throw new InvalidOperationException("Sequence contains no elements"); |
| 69 | + } |
| 70 | + |
| 71 | + TQuantity result = e.Current; |
| 72 | + var nbQuantities = 1; |
| 73 | + while (e.MoveNext()) |
| 74 | + { |
| 75 | + result += e.Current; |
| 76 | + nbQuantities++; |
| 77 | + } |
64 | 78 |
|
65 |
| - return result.value / result.count; |
| 79 | + return result / nbQuantities; |
66 | 80 | }
|
67 | 81 | }
|
68 | 82 | #endif
|
0 commit comments