Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Jan 6, 2024
1 parent eb447c4 commit 484b1e7
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 19 deletions.
55 changes: 55 additions & 0 deletions src/NetFabric.Numerics.UnitTests/Rectangular3D/SumTests.cs
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);
}
}
1 change: 1 addition & 0 deletions src/NetFabric.Numerics/NetFabric.Numerics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Using Include="System.Collections" />
<Using Include="System.Numerics" />
<Using Include="System.Runtime.CompilerServices" />
<Using Include="System.Runtime.InteropServices" />
</ItemGroup>

<PropertyGroup>
Expand Down
4 changes: 1 addition & 3 deletions src/NetFabric.Numerics/Polar/VectorSpanOperations.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.InteropServices;

namespace NetFabric.Numerics.Polar;
namespace NetFabric.Numerics.Polar;

public static partial class Vector
{
Expand Down
4 changes: 1 addition & 3 deletions src/NetFabric.Numerics/Polar/VectorSum.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.InteropServices;

namespace NetFabric.Numerics.Polar;
namespace NetFabric.Numerics.Polar;

public static partial class Vector
{
Expand Down
1 change: 0 additions & 1 deletion src/NetFabric.Numerics/Rectangular2D/Vector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;

namespace NetFabric.Numerics.Rectangular2D;

Expand Down
4 changes: 1 addition & 3 deletions src/NetFabric.Numerics/Rectangular2D/VectorSpanOperations.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.InteropServices;

namespace NetFabric.Numerics.Rectangular2D;
namespace NetFabric.Numerics.Rectangular2D;

public static partial class Vector
{
Expand Down
4 changes: 1 addition & 3 deletions src/NetFabric.Numerics/Rectangular2D/VectorSum.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.InteropServices;

namespace NetFabric.Numerics.Rectangular2D;
namespace NetFabric.Numerics.Rectangular2D;

public static partial class Vector
{
Expand Down
43 changes: 43 additions & 0 deletions src/NetFabric.Numerics/Rectangular3D/VectorAverage.cs
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);
}
4 changes: 1 addition & 3 deletions src/NetFabric.Numerics/Rectangular3D/VectorSpanOperations.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.InteropServices;

namespace NetFabric.Numerics.Rectangular3D;
namespace NetFabric.Numerics.Rectangular3D;

public static partial class Vector
{
Expand Down
72 changes: 72 additions & 0 deletions src/NetFabric.Numerics/Rectangular3D/VectorSum.cs
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);
}
}
4 changes: 1 addition & 3 deletions src/NetFabric.Numerics/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.InteropServices;

namespace NetFabric.Numerics;
namespace NetFabric.Numerics;

/// <summary>
/// Utility methods for performing mathematical operations on generic types.
Expand Down

0 comments on commit 484b1e7

Please sign in to comment.