-
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.
Add operations SumNumber and SumOfSquaresNumber
- Loading branch information
Showing
8 changed files
with
284 additions
and
15 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/NetFabric.Numerics.Tensors.UnitTests/SumNumberTests.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,62 @@ | ||
using System.Numerics.Tensors; | ||
|
||
namespace NetFabric.Numerics.Tensors.UnitTests; | ||
|
||
public class SumNumberTests | ||
{ | ||
public static TheoryData<int> SumNumberData | ||
=> new() { | ||
{ 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 100 }, | ||
}; | ||
|
||
static void SumNumber_Should_Succeed<T>(int count) | ||
where T : struct, INumber<T> | ||
{ | ||
// arrange | ||
var source = new T[count]; | ||
var expected = T.Zero; | ||
var random = new Random(42); | ||
for (var index = 0; index < source.Length; index++) | ||
{ | ||
var value = T.CreateChecked(random.Next(10)); | ||
source[index] = value; | ||
expected += value; | ||
} | ||
|
||
// act | ||
var result = TensorOperations.SumNumber<T>(source); | ||
|
||
// assert | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(SumNumberData))] | ||
public void SumNumber_Short_Should_Succeed(int count) | ||
=> SumNumber_Should_Succeed<short>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumNumberData))] | ||
public void SumNumber_Int_Should_Succeed(int count) | ||
=> SumNumber_Should_Succeed<int>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumNumberData))] | ||
public void SumNumber_Long_Should_Succeed(int count) | ||
=> SumNumber_Should_Succeed<long>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumNumberData))] | ||
public void SumNumber_Half_Should_Succeed(int count) | ||
=> SumNumber_Should_Succeed<Half>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumNumberData))] | ||
public void SumNumber_Float_Should_Succeed(int count) | ||
=> SumNumber_Should_Succeed<float>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumNumberData))] | ||
public void SumNumber_Double_Should_Succeed(int count) | ||
=> SumNumber_Should_Succeed<double>(count); | ||
} |
62 changes: 62 additions & 0 deletions
62
src/NetFabric.Numerics.Tensors.UnitTests/SumOfSquaresNumberTests.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,62 @@ | ||
using System.Numerics.Tensors; | ||
|
||
namespace NetFabric.Numerics.Tensors.UnitTests; | ||
|
||
public class SumOfSquaresNumberTests | ||
{ | ||
public static TheoryData<int> SumOfSquaresNumberData | ||
=> new() { | ||
{ 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 100 }, | ||
}; | ||
|
||
static void SumOfSquaresNumber_Should_Succeed<T>(int count) | ||
where T : struct, INumber<T> | ||
{ | ||
// arrange | ||
var source = new T[count]; | ||
var expected = T.Zero; | ||
var random = new Random(42); | ||
for (var index = 0; index < source.Length; index++) | ||
{ | ||
var value = T.CreateChecked(random.Next(10)); | ||
source[index] = value; | ||
expected += value * value; | ||
} | ||
|
||
// act | ||
var result = TensorOperations.SumOfSquaresNumber<T>(source); | ||
|
||
// assert | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresNumberData))] | ||
public void SumOfSquaresNumber_Short_Should_Succeed(int count) | ||
=> SumOfSquaresNumber_Should_Succeed<short>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresNumberData))] | ||
public void SumOfSquaresNumber_Int_Should_Succeed(int count) | ||
=> SumOfSquaresNumber_Should_Succeed<int>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresNumberData))] | ||
public void SumOfSquaresNumber_Long_Should_Succeed(int count) | ||
=> SumOfSquaresNumber_Should_Succeed<long>(count); | ||
|
||
//[Theory] | ||
//[MemberData(nameof(SumOfSquaresNumberData))] | ||
//public void SumOfSquaresNumber_Half_Should_Succeed(int count) | ||
// => SumOfSquaresNumber_Should_Succeed<Half>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresNumberData))] | ||
public void SumOfSquaresNumber_Float_Should_Succeed(int count) | ||
=> SumOfSquaresNumber_Should_Succeed<float>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresNumberData))] | ||
public void SumOfSquaresNumber_Double_Should_Succeed(int count) | ||
=> SumOfSquaresNumber_Should_Succeed<double>(count); | ||
} |
89 changes: 89 additions & 0 deletions
89
src/NetFabric.Numerics.Tensors.UnitTests/SumOfSquaresTests.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,89 @@ | ||
using System.Numerics.Tensors; | ||
|
||
namespace NetFabric.Numerics.Tensors.UnitTests; | ||
|
||
public class SumOfSquaresTests | ||
{ | ||
public static TheoryData<float[]> SumOfSquaresNaNData | ||
=> new() { | ||
new[] { float.NaN, float.NaN, float.NaN, float.NaN, float.NaN, float.NaN, float.NaN, float.NaN, float.NaN, float.NaN, float.NaN }, | ||
new[] { float.NaN, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }, | ||
new[] { 1.0f, float.NaN, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }, | ||
new[] { 1.0f, 1.0f, float.NaN, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }, | ||
new[] { 1.0f, 1.0f, 1.0f, float.NaN, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }, | ||
new[] { 1.0f, 1.0f, 1.0f, 1.0f, float.NaN, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }, | ||
new[] { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, float.NaN, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }, | ||
new[] { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, float.NaN, 1.0f, 1.0f, 1.0f, 1.0f }, | ||
new[] { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, float.NaN, 1.0f, 1.0f, 1.0f }, | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresNaNData))] | ||
public static void SumOfSquares_With_NaN_Should_Return_NaN(float[] source) | ||
{ | ||
// arrange | ||
var expected = TensorPrimitives.SumOfSquares(source); | ||
|
||
// act | ||
var result = TensorOperations.SumOfSquares<float>(source); | ||
|
||
// assert | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
public static TheoryData<int> SumOfSquaresData | ||
=> new() { | ||
{ 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 100 }, | ||
}; | ||
|
||
static void SumOfSquares_Should_Succeed<T>(int count) | ||
where T : struct, INumber<T> | ||
{ | ||
// arrange | ||
var source = new T[count]; | ||
var expected = T.Zero; | ||
var random = new Random(42); | ||
for (var index = 0; index < source.Length; index++) | ||
{ | ||
var value = T.CreateChecked(random.Next(10)); | ||
source[index] = value; | ||
expected += value * value; | ||
} | ||
|
||
// act | ||
var result = TensorOperations.SumOfSquares<T>(source); | ||
|
||
// assert | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresData))] | ||
public void SumOfSquares_Short_Should_Succeed(int count) | ||
=> SumOfSquares_Should_Succeed<short>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresData))] | ||
public void SumOfSquares_Int_Should_Succeed(int count) | ||
=> SumOfSquares_Should_Succeed<int>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresData))] | ||
public void SumOfSquares_Long_Should_Succeed(int count) | ||
=> SumOfSquares_Should_Succeed<long>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresData))] | ||
public void SumOfSquares_Half_Should_Succeed(int count) | ||
=> SumOfSquares_Should_Succeed<Half>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresData))] | ||
public void SumOfSquares_Float_Should_Succeed(int count) | ||
=> SumOfSquares_Should_Succeed<float>(count); | ||
|
||
[Theory] | ||
[MemberData(nameof(SumOfSquaresData))] | ||
public void SumOfSquares_Double_Should_Succeed(int count) | ||
=> SumOfSquares_Should_Succeed<double>(count); | ||
} |
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,12 @@ | ||
namespace NetFabric.Numerics.Tensors; | ||
|
||
public static partial class TensorOperations | ||
{ | ||
public static T SumOfSquares<T>(ReadOnlySpan<T> source) | ||
where T : struct, INumberBase<T> | ||
=> Tensor.Aggregate<T, SquareOperator<T>, SumOperator<T>>(source); | ||
|
||
public static T SumOfSquaresNumber<T>(ReadOnlySpan<T> source) | ||
where T : struct, IMultiplyOperators<T, T, T>, IAdditionOperators<T, T, T>, IAdditiveIdentity<T, T> | ||
=> Tensor.AggregateNumber<T, SquareOperator<T>, SumOperator<T>>(source); | ||
} |