diff --git a/.editorconfig b/.editorconfig index 2f7d5ac..3dc6173 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,4 +1,5 @@ [*.cs] # CA2225: 运算符重载具有命名的备用项 -dotnet_diagnostic.CA2225.severity = silent \ No newline at end of file +dotnet_diagnostic.CA2225.severity = silent +dotnet_diagnostic.CA1000.severity = none diff --git a/SeWzc.Numerics.Geometry/IBezierCurve.cs b/SeWzc.Numerics.Geometry/IBezierCurve.cs index 27a2b3e..a90f239 100644 --- a/SeWzc.Numerics.Geometry/IBezierCurve.cs +++ b/SeWzc.Numerics.Geometry/IBezierCurve.cs @@ -8,7 +8,7 @@ namespace SeWzc.Numerics.Geometry; public interface IBezierCurve : ICurve where TPoint :unmanaged, IPoint where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { /// /// 获取曲线上的点。 diff --git a/SeWzc.Numerics.Geometry/IPoint.cs b/SeWzc.Numerics.Geometry/IPoint.cs index c634e6e..623bc60 100644 --- a/SeWzc.Numerics.Geometry/IPoint.cs +++ b/SeWzc.Numerics.Geometry/IPoint.cs @@ -13,7 +13,7 @@ namespace SeWzc.Numerics.Geometry; public interface IPoint where TSelf : unmanaged, IPoint where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { /// /// 获取两个点的中点。 diff --git a/SeWzc.Numerics.Tests/IntervalTest.cs b/SeWzc.Numerics.Tests/IntervalTest.cs index 02278f8..c5f9af3 100644 --- a/SeWzc.Numerics.Tests/IntervalTest.cs +++ b/SeWzc.Numerics.Tests/IntervalTest.cs @@ -3,7 +3,7 @@ namespace SeWzc.Numerics.Tests; -[TestSubject(typeof(Interval))] +[TestSubject(typeof(Interval<>))] public class IntervalTest { #region 成员方法 @@ -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(start, end); Assert.Equal(expected, interval.Length); } @@ -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(start, end); Assert.Equal(expected, interval.Contains(value)); } @@ -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(start, end); Assert.Equal(expected, interval.IsEmpty); } @@ -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.Create(a, b); Assert.Equal(start, interval.Start); Assert.Equal(end, interval.End); } @@ -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(start, end); + var interval2 = new Interval(start2, end2); Assert.Equal(expected, interval2.IsSubsetOf(interval)); Assert.Equal(expected, interval.IsSupersetOf(interval2)); } @@ -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(start, end); + var interval2 = new Interval(start2, end2); Assert.Equal(expected, interval2.IsProperSubsetOf(interval)); Assert.Equal(expected, interval.IsProperSupersetOf(interval2)); } #endregion -} \ No newline at end of file +} diff --git a/SeWzc.Numerics.Tests/MatrixFactory.cs b/SeWzc.Numerics.Tests/MatrixFactory.cs index 2e28cfc..a0e1317 100644 --- a/SeWzc.Numerics.Tests/MatrixFactory.cs +++ b/SeWzc.Numerics.Tests/MatrixFactory.cs @@ -10,7 +10,7 @@ internal static class MatrixFactory where TMatrix : IMatrix where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { #region 静态变量 @@ -74,4 +74,4 @@ private static TMatrix CreateRandomMatrix(Random random) } #endregion -} \ No newline at end of file +} diff --git a/SeWzc.Numerics.Tests/MatrixTest.cs b/SeWzc.Numerics.Tests/MatrixTest.cs index 6c7e77f..2558824 100644 --- a/SeWzc.Numerics.Tests/MatrixTest.cs +++ b/SeWzc.Numerics.Tests/MatrixTest.cs @@ -65,7 +65,7 @@ private static void AddTestGeneric() where TMatrix : IMatrix where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { MatrixFactory.Test((a, b) => @@ -89,7 +89,7 @@ private static void SubtractTestGeneric where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { MatrixFactory.Test((a, b) => @@ -113,7 +113,7 @@ private static void RightMultiplyVectorTestGeneric where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { MatrixFactory.Test((a, b) => @@ -144,7 +144,7 @@ private static void LeftMultiplyVectorTestGeneric where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { MatrixFactory.Test((a, b) => @@ -175,7 +175,7 @@ private static void MultiplyNumberTestGeneric where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { MatrixFactory.Test((a, b) => @@ -206,7 +206,7 @@ private static void DivideNumberTestGeneric where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { MatrixFactory.Test((a, b) => @@ -230,7 +230,7 @@ private static void TransposeTestGeneric where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { MatrixFactory.Test(a => @@ -254,7 +254,7 @@ private static void FrobeniusNormTestGeneric where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { MatrixFactory.Test(m => @@ -281,7 +281,7 @@ private static void UnaryMinusTestGeneric where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { MatrixFactory.Test(m => @@ -305,7 +305,7 @@ private static void IndexOutOfRangeTestGeneric where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { MatrixFactory.Test(m => @@ -324,4 +324,4 @@ private static void IndexOutOfRangeTestGeneric(TNum expected, TNum actual) - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { if (typeof(TNum) == typeof(double)) { @@ -59,10 +59,10 @@ public static void NotCloseZero(TNum actual) } public static void Equal(TNum expected, TNum actual) - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { Assert.Equal(expected, actual); } #endregion -} \ No newline at end of file +} diff --git a/SeWzc.Numerics.Tests/NumFactory.cs b/SeWzc.Numerics.Tests/NumFactory.cs index c339ae8..09e3396 100644 --- a/SeWzc.Numerics.Tests/NumFactory.cs +++ b/SeWzc.Numerics.Tests/NumFactory.cs @@ -37,7 +37,7 @@ public static ImmutableArray RandomCreateRange(IEnumerable other, Func< } internal static class NumFactory - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { #region 静态变量 @@ -70,4 +70,4 @@ static NumFactory() } #endregion -} \ No newline at end of file +} diff --git a/SeWzc.Numerics.Tests/VectorFactory.cs b/SeWzc.Numerics.Tests/VectorFactory.cs index 29ab11f..be74f7f 100644 --- a/SeWzc.Numerics.Tests/VectorFactory.cs +++ b/SeWzc.Numerics.Tests/VectorFactory.cs @@ -7,7 +7,7 @@ namespace SeWzc.Numerics.Tests; internal static class VectorFactory where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { #region 静态变量 @@ -51,4 +51,4 @@ private static TVector CreateRandomVector(Random random) } #endregion -} \ No newline at end of file +} diff --git a/SeWzc.Numerics.Tests/VectorTest.cs b/SeWzc.Numerics.Tests/VectorTest.cs index faf98a5..6eec047 100644 --- a/SeWzc.Numerics.Tests/VectorTest.cs +++ b/SeWzc.Numerics.Tests/VectorTest.cs @@ -50,7 +50,7 @@ public void AddTest() private static void AddTestGeneric() where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { VectorFactory.Test((a, b) => { @@ -68,7 +68,7 @@ public void SubtractTest() private static void SubtractTestGeneric() where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { VectorFactory.Test((a, b) => { @@ -86,7 +86,7 @@ public void DotTest() private static void DotTestGeneric() where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { VectorFactory.Test((a, b) => { @@ -106,7 +106,7 @@ public void MultiplyTest() private static void MultiplyTestGeneric() where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { VectorFactory.Test((v, n) => { @@ -127,7 +127,7 @@ public void DivideTest() private static void DivideTestGeneric() where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { VectorFactory.Test((v, n) => { @@ -145,7 +145,7 @@ public void LengthTest() private static void LengthTestGeneric() where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { VectorFactory.Test(v => { @@ -179,7 +179,7 @@ public void EqualsTest() private static void EqualsTestGeneric() where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { #pragma warning disable CS1718 // ReSharper disable ParameterOnlyUsedForPreconditionCheck.Local @@ -208,7 +208,7 @@ public void NormalizeTest() private static void NormalizeTestGeneric() where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { VectorFactory.Test(v => { @@ -235,7 +235,7 @@ public void UnaryNegationTest() private static void UnaryNegationTestGeneric() where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { VectorFactory.Test(v => { @@ -253,7 +253,7 @@ public void IndexOutRangeTest() private static void IndexOutRangeTestGeneric() where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { var v = new TVector(); Assert.Throws(() => v[-1]); @@ -261,4 +261,4 @@ private static void IndexOutRangeTestGeneric() } #endregion -} \ No newline at end of file +} diff --git a/SeWzc.Numerics/IVector.cs b/SeWzc.Numerics/IVector.cs index 4e11efd..44c1239 100644 --- a/SeWzc.Numerics/IVector.cs +++ b/SeWzc.Numerics/IVector.cs @@ -11,7 +11,7 @@ namespace SeWzc.Numerics; /// 向量元素的类型。 public interface IVector : IEqualityOperators where TSelf : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { #region 静态变量 diff --git a/SeWzc.Numerics/Interval.cs b/SeWzc.Numerics/Interval.cs index 64f21e1..4ea7ee6 100644 --- a/SeWzc.Numerics/Interval.cs +++ b/SeWzc.Numerics/Interval.cs @@ -1,4 +1,6 @@ -namespace SeWzc.Numerics; +using System.Numerics; + +namespace SeWzc.Numerics; /// /// 区间。 @@ -8,7 +10,8 @@ /// /// 区间的左端点。 /// 区间的右端点。 -public readonly record struct Interval(double Start, double End) +public readonly record struct Interval(TNum Start, TNum End) + where TNum : unmanaged, IFloatingPoint { #region 静态方法 @@ -18,9 +21,9 @@ public readonly record struct Interval(double Start, double End) /// 区间的一个端点。 /// 区间的另一个端点。 /// - public static Interval Create(double a, double b) + public static Interval Create(TNum a, TNum b) { - return a > b ? new Interval(b, a) : new Interval(a, b); + return a > b ? new Interval(b, a) : new Interval(a, b); } #endregion @@ -48,7 +51,7 @@ public static Interval Create(double a, double b) /// /// 区间长度。 /// - public double Length => Start >= End ? 0 : End - Start; + public TNum Length => Start >= End ? TNum.Zero : End - Start; #endregion @@ -59,7 +62,7 @@ public static Interval Create(double a, double b) /// /// /// - public bool Contains(double value) + public bool Contains(TNum value) { return Start <= value && value <= End; } @@ -69,6 +72,7 @@ public override string ToString() { return _isNotEmpty ? $"[{Start}, {End}]" : "Empty"; } + #endregion } @@ -85,7 +89,8 @@ public static class IntervalExtensions /// /// /// - public static bool IsProperSubsetOf(this Interval interval, Interval other) + public static bool IsProperSubsetOf(this Interval interval, Interval other) + where TNum : unmanaged, IFloatingPoint { if (other.IsEmpty) return false; @@ -104,7 +109,8 @@ public static bool IsProperSubsetOf(this Interval interval, Interval other) /// /// /// - public static bool IsSubsetOf(this Interval interval, Interval other) + public static bool IsSubsetOf(this Interval interval, Interval other) + where TNum : unmanaged, IFloatingPoint { if (interval.IsEmpty) return true; @@ -121,7 +127,8 @@ public static bool IsSubsetOf(this Interval interval, Interval other) /// /// /// - public static bool IsProperSupersetOf(this Interval interval, Interval other) + public static bool IsProperSupersetOf(this Interval interval, Interval other) + where TNum : unmanaged, IFloatingPoint { return other.IsProperSubsetOf(interval); } @@ -132,7 +139,8 @@ public static bool IsProperSupersetOf(this Interval interval, Interval other) /// /// /// - public static bool IsSupersetOf(this Interval interval, Interval other) + public static bool IsSupersetOf(this Interval interval, Interval other) + where TNum : unmanaged, IFloatingPoint { return other.IsSubsetOf(interval); } diff --git a/SeWzc.Numerics/Matrix/IMatrix.cs b/SeWzc.Numerics/Matrix/IMatrix.cs index 26f2f11..0431935 100644 --- a/SeWzc.Numerics/Matrix/IMatrix.cs +++ b/SeWzc.Numerics/Matrix/IMatrix.cs @@ -13,7 +13,7 @@ namespace SeWzc.Numerics.Matrix; public interface IMatrix where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { #region 静态变量 @@ -81,7 +81,7 @@ public interface IMatrix : IMatrix where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { #region 静态变量 @@ -125,7 +125,7 @@ public interface IMatrix : IMatrix where TRow : unmanaged, IVector where TColumn : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint where TTranspose : IMatrix { #region 属性 @@ -150,7 +150,7 @@ public interface IMatrix : IMatrix : IMatrix where TSelf : ISquareMatrix where TVector : unmanaged, IVector - where TNum : unmanaged, INumber + where TNum : unmanaged, IFloatingPoint { #region 静态变量