Skip to content

Commit

Permalink
Support triplets
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Jan 5, 2024
1 parent 33eefcc commit 571e8ef
Show file tree
Hide file tree
Showing 51 changed files with 1,807 additions and 1,255 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Antão Almada
Copyright (c) 2023-2024 Antão Almada

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Company>NetFabric</Company>
<Authors>Antao Almada</Authors>
<Copyright>Copyright 2023 Antao Almada</Copyright>
<Copyright>Copyright 2023-2024 Antao Almada</Copyright>
<LangVersion>12</LangVersion>
<Features>strict</Features>
<Nullable>enable</Nullable>
Expand Down
119 changes: 34 additions & 85 deletions src/NetFabric.Numerics.Tensors.UnitTests/AddMultiplyPairsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;

namespace NetFabric.Numerics.Tensors.UnitTests;

Expand All @@ -8,114 +7,64 @@ public class AddMultiplyPairsTests
public static TheoryData<int> AddData
=> new() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 };

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Short_Should_Succeed(int count)
static void AddMultiply_Should_Succeed<T>(int count)
where T : struct, INumber<T>
{
// arrange
var x = Enumerable.Range(0, count).Select(value => ((short)value, (short)(value + 1))).ToArray();
var y = Enumerable.Range(0, count).Select(value => ((short)(value + 2), (short)(value + 3))).ToArray();
var z = Enumerable.Range(0, count).Select(value => ((short)(value + 4), (short)(value + 5))).ToArray();
var result = new ValueTuple<short, short>[count];
var expected = Enumerable.Range(0, count).Select(value => ((short)((value + value + 2) * (value + 4)), (short)((value + value + 4) * (value + 5)))).ToArray();
var source = Enumerable.Range(0, count);
var x = source
.Select(value => new MyVector2<T>(T.CreateChecked(value), T.CreateChecked(value + 1)))
.ToArray();
var y = source
.Select(value => new MyVector2<T>(T.CreateChecked(value + 2), T.CreateChecked(value + 3)))
.ToArray();
var z = source
.Select(value => new MyVector2<T>(T.CreateChecked(value + 4), T.CreateChecked(value + 5)))
.ToArray();
var result = new MyVector2<T>[count];
var expected = source
.Select(value => new MyVector2<T>(T.CreateChecked((value + value + 2) * (value + 4)), T.CreateChecked((value + value + 4) * (value + 5))))
.ToArray();

// act
Tensor.AddMultiply(
MemoryMarshal.Cast<ValueTuple<short, short>, short>(x),
MemoryMarshal.Cast<ValueTuple<short, short>, short>(y),
MemoryMarshal.Cast<ValueTuple<short, short>, short>(z),
MemoryMarshal.Cast<ValueTuple<short, short>, short>(result));
MemoryMarshal.Cast<MyVector2<T>, T>(x),
MemoryMarshal.Cast<MyVector2<T>, T>(y),
MemoryMarshal.Cast<MyVector2<T>, T>(z),
MemoryMarshal.Cast<MyVector2<T>, T>(result));

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Int_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (value, value + 1)).ToArray();
var y = Enumerable.Range(0, count).Select(value => (value + 2, value + 3)).ToArray();
var z = Enumerable.Range(0, count).Select(value => (value + 4, value + 5)).ToArray();
var result = new ValueTuple<int, int>[count];
var expected = Enumerable.Range(0, count).Select(value => ((value + value + 2) * (value + 4), (value + value + 4) * (value + 5))).ToArray();

// act
Tensor.AddMultiply(
MemoryMarshal.Cast<ValueTuple<int, int>, int>(x),
MemoryMarshal.Cast<ValueTuple<int, int>, int>(y),
MemoryMarshal.Cast<ValueTuple<int, int>, int>(z),
MemoryMarshal.Cast<ValueTuple<int, int>, int>(result));
public void AddMultiply_Short_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<short>(count);

// assert
result.Should().Equal(expected);
}
[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Int_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<int>(count);

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Long_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => ((long)value, (long)(value + 1))).ToArray();
var y = Enumerable.Range(0, count).Select(value => ((long)(value + 2), (long)(value + 3))).ToArray();
var z = Enumerable.Range(0, count).Select(value => ((long)(value + 4), (long)(value + 5))).ToArray();
var result = new ValueTuple<long, long>[count];
var expected = Enumerable.Range(0, count).Select(value => ((long)((value + value + 2) * (value + 4)), (long)((value + value + 4) * (value + 5)))).ToArray();
=> AddMultiply_Should_Succeed<long>(count);

// act
Tensor.AddMultiply(
MemoryMarshal.Cast<ValueTuple<long, long>, long>(x),
MemoryMarshal.Cast<ValueTuple<long, long>, long>(y),
MemoryMarshal.Cast<ValueTuple<long, long>, long>(z),
MemoryMarshal.Cast<ValueTuple<long, long>, long>(result));

// assert
result.Should().Equal(expected);
}
[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Half_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<Half>(count);

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Float_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => ((float)value, (float)(value + 1))).ToArray();
var y = Enumerable.Range(0, count).Select(value => ((float)(value + 2), (float)(value + 3))).ToArray();
var z = Enumerable.Range(0, count).Select(value => ((float)(value + 4), (float)(value + 5))).ToArray();
var result = new ValueTuple<float, float>[count];
var expected = Enumerable.Range(0, count).Select(value => ((float)((value + value + 2) * (value + 4)), (float)((value + value + 4) * (value + 5)))).ToArray();

// act
Tensor.AddMultiply(
MemoryMarshal.Cast<ValueTuple<float, float>, float>(x),
MemoryMarshal.Cast<ValueTuple<float, float>, float>(y),
MemoryMarshal.Cast<ValueTuple<float, float>, float>(z),
MemoryMarshal.Cast<ValueTuple<float, float>, float>(result));

// assert
result.Should().Equal(expected);
}
=> AddMultiply_Should_Succeed<float>(count);

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Double_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => ((double)value, (double)(value + 1))).ToArray();
var y = Enumerable.Range(0, count).Select(value => ((double)(value + 2), (double)(value + 3))).ToArray();
var z = Enumerable.Range(0, count).Select(value => ((double)(value + 4), (double)(value + 5))).ToArray();
var result = new ValueTuple<double, double>[count];
var expected = Enumerable.Range(0, count).Select(value => ((double)((value + value + 2) * (value + 4)), (double)((value + value + 4) * (value + 5)))).ToArray();

// act
Tensor.AddMultiply(
MemoryMarshal.Cast<ValueTuple<double, double>, double>(x),
MemoryMarshal.Cast<ValueTuple<double, double>, double>(y),
MemoryMarshal.Cast<ValueTuple<double, double>, double>(z),
MemoryMarshal.Cast<ValueTuple<double, double>, double>(result));

// assert
result.Should().Equal(expected);
}
=> AddMultiply_Should_Succeed<double>(count);

}
98 changes: 31 additions & 67 deletions src/NetFabric.Numerics.Tensors.UnitTests/AddMultiplyTests.cs
Original file line number Diff line number Diff line change
@@ -1,100 +1,64 @@
using System.Linq;

namespace NetFabric.Numerics.Tensors.UnitTests;
namespace NetFabric.Numerics.Tensors.UnitTests;

public class AddMultiplyTests
{
public static TheoryData<int> AddData
=> new() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 };

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Short_Should_Succeed(int count)
static void AddMultiply_Should_Succeed<T>(int count)
where T : struct, INumber<T>
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (short)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (short)(value + 1)).ToArray();
var z = Enumerable.Range(0, count).Select(value => (short)(value + 2)).ToArray();
var result = new short[count];
var expected = Enumerable.Range(0, count).Select(value => (short)((value + value + 1) * (value + 2))).ToArray();
var source = Enumerable.Range(0, count);
var x = source
.Select(value => T.CreateChecked(value))
.ToArray();
var y = source
.Select(value => T.CreateChecked(value + 1))
.ToArray();
var z = source
.Select(value => T.CreateChecked(value + 2))
.ToArray();
var result = new T[count];
var expected = source
.Select(value => T.CreateChecked((value + value + 1) * (value + 2)))
.ToArray();

// act
Tensor.AddMultiply<short>(x, y, z, result);
Tensor.AddMultiply<T>(x, y, z, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Int_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).ToArray();
var y = Enumerable.Range(0, count).Select(value => value + 1).ToArray();
var z = Enumerable.Range(0, count).Select(value => value + 2).ToArray();
var result = new int[count];
var expected = Enumerable.Range(0, count).Select(value => (value + value + 1) * (value + 2)).ToArray();

// act
Tensor.AddMultiply<int>(x, y, z, result);
public void AddMultiply_Short_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<short>(count);

// assert
result.Should().Equal(expected);
}
[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Int_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<int>(count);

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Long_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (long)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (long)(value + 1)).ToArray();
var z = Enumerable.Range(0, count).Select(value => (long)(value + 2)).ToArray();
var result = new long[count];
var expected = Enumerable.Range(0, count).Select(value => (long)((value + value + 1) * (value + 2))).ToArray();
=> AddMultiply_Should_Succeed<long>(count);

// act
Tensor.AddMultiply<long>(x, y, z, result);

// assert
result.Should().Equal(expected);
}
[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Half_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<Half>(count);

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Float_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (float)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (float)(value + 1)).ToArray();
var z = Enumerable.Range(0, count).Select(value => (float)(value + 2)).ToArray();
var result = new float[count];
var expected = Enumerable.Range(0, count).Select(value => (float)((value + value + 1) * (value + 2))).ToArray();

// act
Tensor.AddMultiply<float>(x, y, z, result);

// assert
result.Should().Equal(expected);
}
=> AddMultiply_Should_Succeed<float>(count);

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Double_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (double)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (double)(value + 1)).ToArray();
var z = Enumerable.Range(0, count).Select(value => (double)(value + 2)).ToArray();
var result = new double[count];
var expected = Enumerable.Range(0, count).Select(value => (double)((value + value + 1) * (value + 2))).ToArray();

// act
Tensor.AddMultiply<double>(x, y, z, result);

// assert
result.Should().Equal(expected);
}
=> AddMultiply_Should_Succeed<double>(count);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Runtime.InteropServices;

namespace NetFabric.Numerics.Tensors.UnitTests;

public class AddMultiplyTripletsTests
{
public static TheoryData<int> AddData
=> new() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 };

static void AddMultiply_Should_Succeed<T>(int count)
where T : struct, INumber<T>
{
// arrange
var source = Enumerable.Range(0, count);
var x = source
.Select(value => new MyVector3<T>(T.CreateChecked(value), T.CreateChecked(value + 1), T.CreateChecked(value + 2)))
.ToArray();
var y = source
.Select(value => new MyVector3<T>(T.CreateChecked(value + 2), T.CreateChecked(value + 3), T.CreateChecked(value + 4)))
.ToArray();
var z = source
.Select(value => new MyVector3<T>(T.CreateChecked(value + 4), T.CreateChecked(value + 5), T.CreateChecked(value + 6)))
.ToArray();
var result = new MyVector3<T>[count];
var expected = source
.Select(value => new MyVector3<T>(T.CreateChecked((value + value + 2) * (value + 4)), T.CreateChecked((value + value + 4) * (value + 5)), T.CreateChecked((value + value + 6) * (value + 6))))
.ToArray();

// act
Tensor.AddMultiply(
MemoryMarshal.Cast<MyVector3<T>, T>(x),
MemoryMarshal.Cast<MyVector3<T>, T>(y),
MemoryMarshal.Cast<MyVector3<T>, T>(z),
MemoryMarshal.Cast<MyVector3<T>, T>(result));

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Short_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<short>(count);

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Int_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<int>(count);

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Long_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<long>(count);

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Half_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<Half>(count);

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Float_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<float>(count);

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Double_Should_Succeed(int count)
=> AddMultiply_Should_Succeed<double>(count);

}
Loading

0 comments on commit 571e8ef

Please sign in to comment.