Skip to content

Commit e85b678

Browse files
authored
Consolidate a few exception throws in TensorPrimitives (#93168)
1 parent a42e97f commit e85b678

File tree

3 files changed

+10
-26
lines changed

3 files changed

+10
-26
lines changed

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,6 @@ public static float Distance(ReadOnlySpan<float> x, ReadOnlySpan<float> y)
215215
ThrowHelper.ThrowArgument_SpansMustBeNonEmpty();
216216
}
217217

218-
if (x.Length != y.Length)
219-
{
220-
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
221-
}
222-
223218
return MathF.Sqrt(Aggregate<SubtractSquaredOperator, AddOperator>(x, y));
224219
}
225220

@@ -282,15 +277,8 @@ public static void Divide(ReadOnlySpan<float> x, float y, Span<float> destinatio
282277
/// operating systems or architectures.
283278
/// </para>
284279
/// </remarks>
285-
public static float Dot(ReadOnlySpan<float> x, ReadOnlySpan<float> y)
286-
{
287-
if (x.Length != y.Length)
288-
{
289-
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
290-
}
291-
292-
return Aggregate<MultiplyOperator, AddOperator>(x, y);
293-
}
280+
public static float Dot(ReadOnlySpan<float> x, ReadOnlySpan<float> y) =>
281+
Aggregate<MultiplyOperator, AddOperator>(x, y);
294282

295283
/// <summary>Computes the element-wise result of raising <c>e</c> to the single-precision floating-point number powers in the specified tensor.</summary>
296284
/// <param name="x">The tensor, represented as a span.</param>
@@ -910,11 +898,6 @@ public static float ProductOfDifferences(ReadOnlySpan<float> x, ReadOnlySpan<flo
910898
ThrowHelper.ThrowArgument_SpansMustBeNonEmpty();
911899
}
912900

913-
if (x.Length != y.Length)
914-
{
915-
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
916-
}
917-
918901
return Aggregate<SubtractOperator, MultiplyOperator>(x, y);
919902
}
920903

@@ -946,11 +929,6 @@ public static float ProductOfSums(ReadOnlySpan<float> x, ReadOnlySpan<float> y)
946929
ThrowHelper.ThrowArgument_SpansMustBeNonEmpty();
947930
}
948931

949-
if (x.Length != y.Length)
950-
{
951-
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
952-
}
953-
954932
return Aggregate<AddOperator, MultiplyOperator>(x, y);
955933
}
956934

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netcore.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,10 @@ private static float Aggregate<TBinaryOperator, TAggregationOperator>(
901901
where TBinaryOperator : struct, IBinaryOperator
902902
where TAggregationOperator : struct, IAggregationOperator
903903
{
904-
Debug.Assert(x.Length == y.Length);
904+
if (x.Length != y.Length)
905+
{
906+
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
907+
}
905908

906909
if (x.IsEmpty)
907910
{

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netstandard.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ private static float Aggregate<TBinaryOperator, TAggregationOperator>(
163163
where TBinaryOperator : struct, IBinaryOperator
164164
where TAggregationOperator : struct, IAggregationOperator
165165
{
166-
Debug.Assert(x.Length == y.Length);
166+
if (x.Length != y.Length)
167+
{
168+
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
169+
}
167170

168171
if (x.Length == 0)
169172
{

0 commit comments

Comments
 (0)