From c13efb4b440b62e4096d01fdfd8a5e15b9e50a3a Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sat, 7 Oct 2023 07:24:38 -0400 Subject: [PATCH] Consolidate a few exception throws in TensorPrimitives --- .../Numerics/Tensors/TensorPrimitives.cs | 26 ++----------------- .../Tensors/TensorPrimitives.netcore.cs | 5 +++- .../Tensors/TensorPrimitives.netstandard.cs | 5 +++- 3 files changed, 10 insertions(+), 26 deletions(-) diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.cs index 4b4a11bfaa84ca..d569b983fb890f 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.cs @@ -215,11 +215,6 @@ public static float Distance(ReadOnlySpan x, ReadOnlySpan y) ThrowHelper.ThrowArgument_SpansMustBeNonEmpty(); } - if (x.Length != y.Length) - { - ThrowHelper.ThrowArgument_SpansMustHaveSameLength(); - } - return MathF.Sqrt(Aggregate(x, y)); } @@ -282,15 +277,8 @@ public static void Divide(ReadOnlySpan x, float y, Span destinatio /// operating systems or architectures. /// /// - public static float Dot(ReadOnlySpan x, ReadOnlySpan y) - { - if (x.Length != y.Length) - { - ThrowHelper.ThrowArgument_SpansMustHaveSameLength(); - } - - return Aggregate(x, y); - } + public static float Dot(ReadOnlySpan x, ReadOnlySpan y) => + Aggregate(x, y); /// Computes the element-wise result of raising e to the single-precision floating-point number powers in the specified tensor. /// The tensor, represented as a span. @@ -910,11 +898,6 @@ public static float ProductOfDifferences(ReadOnlySpan x, ReadOnlySpan(x, y); } @@ -946,11 +929,6 @@ public static float ProductOfSums(ReadOnlySpan x, ReadOnlySpan y) ThrowHelper.ThrowArgument_SpansMustBeNonEmpty(); } - if (x.Length != y.Length) - { - ThrowHelper.ThrowArgument_SpansMustHaveSameLength(); - } - return Aggregate(x, y); } diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netcore.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netcore.cs index db7fce0da92483..276af068addbe6 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netcore.cs @@ -901,7 +901,10 @@ private static float Aggregate( where TBinaryOperator : struct, IBinaryOperator where TAggregationOperator : struct, IAggregationOperator { - Debug.Assert(x.Length == y.Length); + if (x.Length != y.Length) + { + ThrowHelper.ThrowArgument_SpansMustHaveSameLength(); + } if (x.IsEmpty) { diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netstandard.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netstandard.cs index 15d0a8aaf72928..075c6f74e2a8f0 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netstandard.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netstandard.cs @@ -163,7 +163,10 @@ private static float Aggregate( where TBinaryOperator : struct, IBinaryOperator where TAggregationOperator : struct, IAggregationOperator { - Debug.Assert(x.Length == y.Length); + if (x.Length != y.Length) + { + ThrowHelper.ThrowArgument_SpansMustHaveSameLength(); + } if (x.Length == 0) {