-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
177 additions
and
19 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/NetFabric.Numerics.UnitTests/Rectangular3D/SumTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
namespace NetFabric.Numerics.Rectangular3D.UnitTests; | ||
|
||
public class SumTests | ||
{ | ||
public static TheoryData<Vector<double>[], Vector<double>> SumData | ||
=> new() | ||
{ | ||
{ | ||
Array.Empty<Vector<double>>(), | ||
new Vector<double>(0, 0, 0) | ||
}, | ||
{ | ||
new Vector<double>[] { new(1.0, 2.0, 3.0) }, | ||
new Vector<double>(1.0, 2.0, 3.0) | ||
}, | ||
{ | ||
new Vector<double>[] { new(1.0, 2.0, 3.0), new(11.0, 12.0, 13.0) }, | ||
new Vector<double>(12.0, 14.0, 16.0) | ||
}, | ||
{ | ||
Enumerable.Range(0, 97).Select(value => new Vector<double>(value, value + 1, value + 2)).ToArray(), | ||
new Vector<double>( | ||
Enumerable.Range(0, 97).Sum(), | ||
Enumerable.Range(0, 97).Select(value => value + 1).Sum(), | ||
Enumerable.Range(0, 97).Select(value => value + 2).Sum()) | ||
}, | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(SumData))] | ||
public void Sum_For_Enumerable_Should_Succeed(Vector<double>[] source, Vector<double> expected) | ||
{ | ||
// arrange | ||
var enumerable = new ReadOnlyCollection<Vector<double>>(source); | ||
|
||
// act | ||
var result = enumerable.Sum(); | ||
|
||
// assert | ||
result.Should().Be(expected); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(SumData))] | ||
public void Sum_For_Array_Should_Succeed(Vector<double>[] source, Vector<double> expected) | ||
{ | ||
// arrange | ||
|
||
// act | ||
var result = source.Sum(); | ||
|
||
// assert | ||
result.Should().Be(expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace NetFabric.Numerics.Rectangular3D; | ||
|
||
public static partial class Vector | ||
{ | ||
public static Vector<T>? Average<T>(this IEnumerable<Vector<T>> source) | ||
where T : struct, INumber<T>, IMinMaxValue<T> | ||
{ | ||
if(source.TryGetSpan(out var span)) | ||
return span.Average(); | ||
|
||
var sumX = T.Zero; | ||
var sumY = T.Zero; | ||
var sumZ = T.Zero; | ||
var count = T.Zero; | ||
foreach (var vector in source) | ||
{ | ||
checked | ||
{ | ||
sumX += vector.X; | ||
sumY += vector.Y; | ||
sumZ += vector.Z; | ||
count++; | ||
} | ||
} | ||
return T.IsZero(count) | ||
? null | ||
: new Vector<T>(sumX / count, sumY / count, sumZ / count); | ||
} | ||
|
||
public static Vector<T>? Average<T>(this Vector<T>[] source) | ||
where T : struct, INumber<T>, IMinMaxValue<T> | ||
=> source.AsSpan().Average(); | ||
|
||
public static Vector<T>? Average<T>(this Span<Vector<T>> source) | ||
where T : struct, INumber<T>, IMinMaxValue<T> | ||
=> ((ReadOnlySpan<Vector<T>>)source).Average(); | ||
|
||
public static Vector<T>? Average<T>(this ReadOnlySpan<Vector<T>> source) | ||
where T : struct, INumber<T>, IMinMaxValue<T> | ||
=> source.Length is 0 | ||
? null | ||
: Sum(source) / T.CreateChecked(source.Length); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
namespace NetFabric.Numerics.Rectangular3D; | ||
|
||
public static partial class Vector | ||
{ | ||
/// <summary> | ||
/// Calculates the sum of a collection of vectors. | ||
/// </summary> | ||
/// <param name="source">The enumerable collection of vectors.</param> | ||
/// <returns>The sum of the vectors in the collection.</returns> | ||
/// <remarks> | ||
/// The sum of vectors is computed by adding all the vectors in the given <paramref name="source"/> collection. | ||
/// </remarks> | ||
public static Vector<T> Sum<T>(this IEnumerable<Vector<T>> source) | ||
where T : struct, INumber<T>, IMinMaxValue<T> | ||
{ | ||
if(source.TryGetSpan(out var span)) | ||
return span.Sum(); | ||
|
||
var sumX = T.Zero; | ||
var sumY = T.Zero; | ||
var sumZ = T.Zero; | ||
foreach (var vector in source) | ||
{ | ||
checked | ||
{ | ||
sumX += vector.X; | ||
sumY += vector.Y; | ||
sumZ += vector.Z; | ||
} | ||
} | ||
return new Vector<T>(sumX, sumY, sumZ); | ||
} | ||
|
||
/// <summary> | ||
/// Calculates the sum of an array of vectors. | ||
/// </summary> | ||
/// <param name="source">The array collection of vectors.</param> | ||
/// <returns>The sum of the vectors in the collection.</returns> | ||
/// <remarks> | ||
/// The sum of vectors is computed by adding all the vectors in the given <paramref name="source"/> collection. | ||
/// </remarks> | ||
public static Vector<T> Sum<T>(this Vector<T>[] source) | ||
where T : struct, INumber<T>, IMinMaxValue<T> | ||
=> source.AsSpan().Sum(); | ||
|
||
/// <summary> | ||
/// Calculates the sum of a span of vectors. | ||
/// </summary> | ||
/// <param name="source">The <see cref="Span{T}"/> collection of vectors.</param> | ||
/// <returns>The sum of the vectors in the collection.</returns> | ||
/// <remarks> | ||
/// The sum of vectors is computed by adding all the vectors in the given <paramref name="source"/> collection. | ||
/// </remarks> | ||
public static Vector<T> Sum<T>(this Span<Vector<T>> source) | ||
where T : struct, INumber<T>, IMinMaxValue<T> | ||
=> ((ReadOnlySpan<Vector<T>>)source).Sum(); | ||
|
||
/// <summary> | ||
/// Calculates the sum of a read-only span of vectors. | ||
/// </summary> | ||
/// <param name="source">The <see cref="ReadOnlySpan{T}"/> collection of vectors.</param> | ||
/// <returns>The sum of the vectors in the collection.</returns> | ||
/// <remarks> | ||
/// The sum of vectors is computed by adding all the vectors in the given <paramref name="source"/> collection. | ||
/// </remarks> | ||
public static Vector<T> Sum<T>(this ReadOnlySpan<Vector<T>> source) | ||
where T : struct, INumber<T>, IMinMaxValue<T> | ||
{ | ||
(var sumX, var sumY, var sumZ) = Tensor.SumTriplets(MemoryMarshal.Cast<Vector<T>, T>(source)); | ||
return new Vector<T>(sumX, sumY, sumZ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters