Skip to content

Commit

Permalink
使用 IFloatingPoint 替换 INumber 接口;将 Interval 变成泛型类型
Browse files Browse the repository at this point in the history
  • Loading branch information
SeWZC committed Aug 13, 2024
1 parent 494ebd3 commit 2225f4b
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[*.cs]

# CA2225: 运算符重载具有命名的备用项
dotnet_diagnostic.CA2225.severity = silent
dotnet_diagnostic.CA2225.severity = silent
dotnet_diagnostic.CA1000.severity = none
2 changes: 1 addition & 1 deletion SeWzc.Numerics.Geometry/IBezierCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SeWzc.Numerics.Geometry;
public interface IBezierCurve<TPoint, TVector, TNum> : ICurve
where TPoint :unmanaged, IPoint<TPoint, TVector, TNum>
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
/// <summary>
/// 获取曲线上的点。
Expand Down
2 changes: 1 addition & 1 deletion SeWzc.Numerics.Geometry/IPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace SeWzc.Numerics.Geometry;
public interface IPoint<TSelf, TVector, TNum>
where TSelf : unmanaged, IPoint<TSelf, TVector, TNum>
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
/// <summary>
/// 获取两个点的中点。
Expand Down
20 changes: 10 additions & 10 deletions SeWzc.Numerics.Tests/IntervalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SeWzc.Numerics.Tests;

[TestSubject(typeof(Interval))]
[TestSubject(typeof(Interval<>))]
public class IntervalTest
{
#region 成员方法
Expand All @@ -15,7 +15,7 @@ public class IntervalTest
[InlineData(1, 1, 0)]
public void LengthTest(double start, double end, double expected)
{
var interval = new Interval(start, end);
var interval = new Interval<double>(start, end);
Assert.Equal(expected, interval.Length);
}

Expand All @@ -27,7 +27,7 @@ public void LengthTest(double start, double end, double expected)
[InlineData(0, 1, 2, false)]
public void ContainsTest(double start, double end, double value, bool expected)
{
var interval = new Interval(start, end);
var interval = new Interval<double>(start, end);
Assert.Equal(expected, interval.Contains(value));
}

Expand All @@ -39,7 +39,7 @@ public void ContainsTest(double start, double end, double value, bool expected)
[InlineData(1, 0.99, true)]
public void IsEmptyTest(double start, double end, bool expected)
{
var interval = new Interval(start, end);
var interval = new Interval<double>(start, end);
Assert.Equal(expected, interval.IsEmpty);
}

Expand All @@ -50,7 +50,7 @@ public void IsEmptyTest(double start, double end, bool expected)
[InlineData(1, 1, 1, 1)]
public void CreateTest(double a, double b, double start, double end)
{
var interval = Interval.Create(a, b);
var interval = Interval<double>.Create(a, b);
Assert.Equal(start, interval.Start);
Assert.Equal(end, interval.End);
}
Expand All @@ -71,8 +71,8 @@ public void CreateTest(double a, double b, double start, double end)
[InlineData(0, 0, 1, 0, true)]
public void SubsetTest(double start, double end, double start2, double end2, bool expected)
{
var interval = new Interval(start, end);
var interval2 = new Interval(start2, end2);
var interval = new Interval<double>(start, end);
var interval2 = new Interval<double>(start2, end2);
Assert.Equal(expected, interval2.IsSubsetOf(interval));
Assert.Equal(expected, interval.IsSupersetOf(interval2));
}
Expand All @@ -88,11 +88,11 @@ public void SubsetTest(double start, double end, double start2, double end2, boo
[InlineData(0, 1, 0, 1.1, false)]
public void ProperSubsetTest(double start, double end, double start2, double end2, bool expected)
{
var interval = new Interval(start, end);
var interval2 = new Interval(start2, end2);
var interval = new Interval<double>(start, end);
var interval2 = new Interval<double>(start2, end2);
Assert.Equal(expected, interval2.IsProperSubsetOf(interval));
Assert.Equal(expected, interval.IsProperSupersetOf(interval2));
}

#endregion
}
}
4 changes: 2 additions & 2 deletions SeWzc.Numerics.Tests/MatrixFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static class MatrixFactory<TMatrix, TRow, TColumn, TNum, TTranspose>
where TMatrix : IMatrix<TMatrix, TRow, TColumn, TNum, TTranspose>
where TRow : unmanaged, IVector<TRow, TNum>
where TColumn : unmanaged, IVector<TColumn, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
where TTranspose : IMatrix<TTranspose, TColumn, TRow, TNum, TMatrix>
{
#region 静态变量
Expand Down Expand Up @@ -74,4 +74,4 @@ private static TMatrix CreateRandomMatrix(Random random)
}

#endregion
}
}
22 changes: 11 additions & 11 deletions SeWzc.Numerics.Tests/MatrixTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static void AddTestGeneric<TMatrix, TRow, TColumn, TNum, TTranspose>()
where TMatrix : IMatrix<TMatrix, TRow, TColumn, TNum, TTranspose>
where TRow : unmanaged, IVector<TRow, TNum>
where TColumn : unmanaged, IVector<TColumn, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
where TTranspose : IMatrix<TTranspose, TColumn, TRow, TNum, TMatrix>
{
MatrixFactory<TMatrix, TRow, TColumn, TNum, TTranspose>.Test((a, b) =>
Expand All @@ -89,7 +89,7 @@ private static void SubtractTestGeneric<TMatrix, TRow, TColumn, TNum, TTranspose
where TMatrix : IMatrix<TMatrix, TRow, TColumn, TNum, TTranspose>
where TRow : unmanaged, IVector<TRow, TNum>
where TColumn : unmanaged, IVector<TColumn, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
where TTranspose : IMatrix<TTranspose, TColumn, TRow, TNum, TMatrix>
{
MatrixFactory<TMatrix, TRow, TColumn, TNum, TTranspose>.Test((a, b) =>
Expand All @@ -113,7 +113,7 @@ private static void RightMultiplyVectorTestGeneric<TMatrix, TRow, TColumn, TNum,
where TMatrix : IMatrix<TMatrix, TRow, TColumn, TNum, TTranspose>
where TRow : unmanaged, IVector<TRow, TNum>
where TColumn : unmanaged, IVector<TColumn, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
where TTranspose : IMatrix<TTranspose, TColumn, TRow, TNum, TMatrix>
{
MatrixFactory<TMatrix, TRow, TColumn, TNum, TTranspose>.Test((a, b) =>
Expand Down Expand Up @@ -144,7 +144,7 @@ private static void LeftMultiplyVectorTestGeneric<TMatrix, TRow, TColumn, TNum,
where TMatrix : IMatrix<TMatrix, TRow, TColumn, TNum, TTranspose>
where TRow : unmanaged, IVector<TRow, TNum>
where TColumn : unmanaged, IVector<TColumn, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
where TTranspose : IMatrix<TTranspose, TColumn, TRow, TNum, TMatrix>
{
MatrixFactory<TMatrix, TRow, TColumn, TNum, TTranspose>.Test((a, b) =>
Expand Down Expand Up @@ -175,7 +175,7 @@ private static void MultiplyNumberTestGeneric<TMatrix, TRow, TColumn, TNum, TTra
where TMatrix : IMatrix<TMatrix, TRow, TColumn, TNum, TTranspose>
where TRow : unmanaged, IVector<TRow, TNum>
where TColumn : unmanaged, IVector<TColumn, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
where TTranspose : IMatrix<TTranspose, TColumn, TRow, TNum, TMatrix>
{
MatrixFactory<TMatrix, TRow, TColumn, TNum, TTranspose>.Test((a, b) =>
Expand Down Expand Up @@ -206,7 +206,7 @@ private static void DivideNumberTestGeneric<TMatrix, TRow, TColumn, TNum, TTrans
where TMatrix : IMatrix<TMatrix, TRow, TColumn, TNum, TTranspose>
where TRow : unmanaged, IVector<TRow, TNum>
where TColumn : unmanaged, IVector<TColumn, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
where TTranspose : IMatrix<TTranspose, TColumn, TRow, TNum, TMatrix>
{
MatrixFactory<TMatrix, TRow, TColumn, TNum, TTranspose>.Test((a, b) =>
Expand All @@ -230,7 +230,7 @@ private static void TransposeTestGeneric<TMatrix, TRow, TColumn, TNum, TTranspos
where TMatrix : IMatrix<TMatrix, TRow, TColumn, TNum, TTranspose>
where TRow : unmanaged, IVector<TRow, TNum>
where TColumn : unmanaged, IVector<TColumn, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
where TTranspose : IMatrix<TTranspose, TColumn, TRow, TNum, TMatrix>
{
MatrixFactory<TMatrix, TRow, TColumn, TNum, TTranspose>.Test(a =>
Expand All @@ -254,7 +254,7 @@ private static void FrobeniusNormTestGeneric<TMatrix, TRow, TColumn, TNum, TTran
where TMatrix : IMatrix<TMatrix, TRow, TColumn, TNum, TTranspose>
where TRow : unmanaged, IVector<TRow, TNum>
where TColumn : unmanaged, IVector<TColumn, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
where TTranspose : IMatrix<TTranspose, TColumn, TRow, TNum, TMatrix>
{
MatrixFactory<TMatrix, TRow, TColumn, TNum, TTranspose>.Test(m =>
Expand All @@ -281,7 +281,7 @@ private static void UnaryMinusTestGeneric<TMatrix, TRow, TColumn, TNum, TTranspo
where TMatrix : IMatrix<TMatrix, TRow, TColumn, TNum, TTranspose>
where TRow : unmanaged, IVector<TRow, TNum>
where TColumn : unmanaged, IVector<TColumn, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
where TTranspose : IMatrix<TTranspose, TColumn, TRow, TNum, TMatrix>
{
MatrixFactory<TMatrix, TRow, TColumn, TNum, TTranspose>.Test(m =>
Expand All @@ -305,7 +305,7 @@ private static void IndexOutOfRangeTestGeneric<TMatrix, TRow, TColumn, TNum, TTr
where TMatrix : IMatrix<TMatrix, TRow, TColumn, TNum, TTranspose>
where TRow : unmanaged, IVector<TRow, TNum>
where TColumn : unmanaged, IVector<TColumn, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
where TTranspose : IMatrix<TTranspose, TColumn, TRow, TNum, TMatrix>
{
MatrixFactory<TMatrix, TRow, TColumn, TNum, TTranspose>.Test(m =>
Expand All @@ -324,4 +324,4 @@ private static void IndexOutOfRangeTestGeneric<TMatrix, TRow, TColumn, TNum, TTr
}

#endregion
}
}
6 changes: 3 additions & 3 deletions SeWzc.Numerics.Tests/NumAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static class NumAssert
#region 静态方法

public static void CloseEqual<TNum>(TNum expected, TNum actual)
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
if (typeof(TNum) == typeof(double))
{
Expand Down Expand Up @@ -59,10 +59,10 @@ public static void NotCloseZero<TNum>(TNum actual)
}

public static void Equal<TNum>(TNum expected, TNum actual)
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
Assert.Equal(expected, actual);
}

#endregion
}
}
4 changes: 2 additions & 2 deletions SeWzc.Numerics.Tests/NumFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static ImmutableArray<T> RandomCreateRange<T>(IEnumerable<T> other, Func<
}

internal static class NumFactory<TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
#region 静态变量

Expand Down Expand Up @@ -70,4 +70,4 @@ static NumFactory()
}

#endregion
}
}
4 changes: 2 additions & 2 deletions SeWzc.Numerics.Tests/VectorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace SeWzc.Numerics.Tests;

internal static class VectorFactory<TVector, TNum>
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
#region 静态变量

Expand Down Expand Up @@ -51,4 +51,4 @@ private static TVector CreateRandomVector(Random random)
}

#endregion
}
}
22 changes: 11 additions & 11 deletions SeWzc.Numerics.Tests/VectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void AddTest()

private static void AddTestGeneric<TVector, TNum>()
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
VectorFactory<TVector, TNum>.Test((a, b) =>
{
Expand All @@ -68,7 +68,7 @@ public void SubtractTest()

private static void SubtractTestGeneric<TVector, TNum>()
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
VectorFactory<TVector, TNum>.Test((a, b) =>
{
Expand All @@ -86,7 +86,7 @@ public void DotTest()

private static void DotTestGeneric<TVector, TNum>()
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
VectorFactory<TVector, TNum>.Test((a, b) =>
{
Expand All @@ -106,7 +106,7 @@ public void MultiplyTest()

private static void MultiplyTestGeneric<TVector, TNum>()
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
VectorFactory<TVector, TNum>.Test((v, n) =>
{
Expand All @@ -127,7 +127,7 @@ public void DivideTest()

private static void DivideTestGeneric<TVector, TNum>()
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
VectorFactory<TVector, TNum>.Test((v, n) =>
{
Expand All @@ -145,7 +145,7 @@ public void LengthTest()

private static void LengthTestGeneric<TVector, TNum>()
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
VectorFactory<TVector, TNum>.Test(v =>
{
Expand Down Expand Up @@ -179,7 +179,7 @@ public void EqualsTest()

private static void EqualsTestGeneric<TVector, TNum>()
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
#pragma warning disable CS1718
// ReSharper disable ParameterOnlyUsedForPreconditionCheck.Local
Expand Down Expand Up @@ -208,7 +208,7 @@ public void NormalizeTest()

private static void NormalizeTestGeneric<TVector, TNum>()
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
VectorFactory<TVector, TNum>.Test(v =>
{
Expand All @@ -235,7 +235,7 @@ public void UnaryNegationTest()

private static void UnaryNegationTestGeneric<TVector, TNum>()
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
VectorFactory<TVector, TNum>.Test(v =>
{
Expand All @@ -253,12 +253,12 @@ public void IndexOutRangeTest()

private static void IndexOutRangeTestGeneric<TVector, TNum>()
where TVector : unmanaged, IVector<TVector, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
var v = new TVector();
Assert.Throws<ArgumentOutOfRangeException>(() => v[-1]);
Assert.Throws<ArgumentOutOfRangeException>(() => v[TVector.Dimension]);
}

#endregion
}
}
2 changes: 1 addition & 1 deletion SeWzc.Numerics/IVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace SeWzc.Numerics;
/// <typeparam name="TNum">向量元素的类型。</typeparam>
public interface IVector<TSelf, TNum> : IEqualityOperators<TSelf, TSelf, bool>
where TSelf : unmanaged, IVector<TSelf, TNum>
where TNum : unmanaged, INumber<TNum>
where TNum : unmanaged, IFloatingPoint<TNum>
{
#region 静态变量

Expand Down
Loading

0 comments on commit 2225f4b

Please sign in to comment.