From 003fcd3ba1ab8f6d50cc6bf2112d119a6c8951a7 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 10:50:16 -0700 Subject: [PATCH 01/20] Ensure the DynamicallyAccessedMembersAnalyzer doesn't crash for new language features --- .../DynamicallyAccessedMembersAnalyzer.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs b/src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs index 7391b1809abc69..5f63510347c8c7 100644 --- a/src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs +++ b/src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs @@ -106,11 +106,21 @@ public override void Initialize(AnalysisContext context) context.RegisterOperationBlockAction(context => { - foreach (var operationBlock in context.OperationBlocks) + try { - TrimDataFlowAnalysis trimDataFlowAnalysis = new(context, dataFlowAnalyzerContext, operationBlock); - trimDataFlowAnalysis.InterproceduralAnalyze(); - trimDataFlowAnalysis.ReportDiagnostics(context.ReportDiagnostic); + foreach (var operationBlock in context.OperationBlocks) + { + TrimDataFlowAnalysis trimDataFlowAnalysis = new(context, dataFlowAnalyzerContext, operationBlock); + trimDataFlowAnalysis.InterproceduralAnalyze(); + trimDataFlowAnalysis.ReportDiagnostics(context.ReportDiagnostic); + } + } + catch (InvalidCastException) + { + // Newer language features may produce NonErrorNamedTypeSymbol + // which will fail to cast to a given target type. Catch and + // ignore the failure in this scenario since it otherwise blocks + // the analyzer } }); From 166c3d456103c15d619bc65575eedbdc2ff23096 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 12:17:07 -0700 Subject: [PATCH 02/20] Adding the extensions for operator + and operator += --- .../ref/System.Numerics.Tensors.netcore.cs | 25 ++++ .../src/System.Numerics.Tensors.csproj | 1 + .../System/Numerics/Tensors/netcore/Tensor.cs | 56 -------- .../Tensors/netcore/Tensor.op_Addition.cs | 132 ++++++++++++++++++ 4 files changed, 158 insertions(+), 56 deletions(-) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Addition.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index 84d727f4011b00..7a520954b6c491 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -578,6 +578,31 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static ref readonly System.Numerics.Tensors.TensorSpan Xor(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x, scoped in System.Numerics.Tensors.ReadOnlyTensorSpan y, in System.Numerics.Tensors.TensorSpan destination) where T : System.Numerics.IBitwiseOperators { throw null; } public static System.Numerics.Tensors.Tensor Xor(in System.Numerics.Tensors.ReadOnlyTensorSpan x, T y) where T : System.Numerics.IBitwiseOperators { throw null; } public static ref readonly System.Numerics.Tensors.TensorSpan Xor(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x, T y, in System.Numerics.Tensors.TensorSpan destination) where T : System.Numerics.IBitwiseOperators { throw null; } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IAdditionOperators, System.Numerics.IAdditiveIdentity + { + public static System.Numerics.Tensors.Tensor operator +(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator +(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator +(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + } + extension(Tensor tensor) + where TScalar : System.Numerics.IAdditionOperators, System.Numerics.IAdditiveIdentity + { + public static System.Numerics.Tensors.Tensor operator +(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } + public static System.Numerics.Tensors.Tensor operator +(System.Numerics.Tensors.Tensor left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator +(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } + public void operator +=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator +=(TScalar other) { throw null; } + } + extension(ref TensorSpan tensor) + where TScalar : System.Numerics.IAdditionOperators, System.Numerics.IAdditiveIdentity + { + public static System.Numerics.Tensors.Tensor operator +(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator +(in System.Numerics.Tensors.TensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator +(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public void operator +=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator +=(TScalar other) { throw null; } + } } public readonly ref partial struct TensorDimensionSpan { diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 73464e9b6106ed..2da2a555f9e710 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -39,6 +39,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs index bba7b8c07c050c..e83b0e8a728c20 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs @@ -2453,62 +2453,6 @@ public static ref readonly TensorSpan AcosPi(scoped in ReadOnlyTensorSpan< } #endregion - #region Add - /// - /// Adds each element of to each element of and returns a new with the result. - /// - /// The of values to add. - /// The second of values to add. - public static Tensor Add(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) - where T : IAdditionOperators, IAdditiveIdentity - { - TensorOperation.ValidateCompatibility(x, y, out Tensor destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Adds to each element of and returns a new with the result. - /// - /// The of values to add. - /// The to add to each element of . - public static Tensor Add(in ReadOnlyTensorSpan x, T y) - where T : IAdditionOperators, IAdditiveIdentity - { - Tensor destination = CreateFromShapeUninitialized(x.Lengths); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Adds each element of to each element of and returns a new with the result. - /// - /// The of values to add. - /// The second of values to add. - /// - public static ref readonly TensorSpan Add(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) - where T : IAdditionOperators, IAdditiveIdentity - { - TensorOperation.ValidateCompatibility(x, y, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - - /// - /// Adds to each element of and returns a new with the result. - /// - /// The of values to add. - /// The to add to each element of . - /// - public static ref readonly TensorSpan Add(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) - where T : IAdditionOperators, IAdditiveIdentity - { - TensorOperation.ValidateCompatibility(x, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - #endregion - #region Asin /// /// Takes the inverse sin of each element of the and returns a new with the result. diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Addition.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Addition.cs new file mode 100644 index 00000000000000..1da8f2c678d998 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Addition.cs @@ -0,0 +1,132 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs element-wise addition between two tensors. + /// The type of the elements in the tensor. + /// The tensor to add with . + /// The tensor to add with . + /// A new tensor containing the result of + . + /// The shapes of and are not compatible. + public static Tensor Add(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) + where T : IAdditionOperators, IAdditiveIdentity + { + TensorOperation.ValidateCompatibility(x, y, out Tensor destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs element-wise addition between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor to add with . + /// The scalar to add with . + /// A new tensor containing the result of + . + public static Tensor Add(in ReadOnlyTensorSpan x, T y) + where T : IAdditionOperators, IAdditiveIdentity + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs element-wise addition between two tensors. + /// The type of the elements in the tensor. + /// The tensor to add with . + /// The tensor to add with . + /// The destination where the result of + is written. + /// A reference to . + /// The shapes of , , and are not compatible. + public static ref readonly TensorSpan Add(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) + where T : IAdditionOperators, IAdditiveIdentity + { + TensorOperation.ValidateCompatibility(x, y, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// Performs element-wise addition between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor to add with . + /// The scalar to add with . + /// The destination where the result of + is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan Add(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) + where T : IAdditionOperators, IAdditiveIdentity + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IAdditionOperators, IAdditiveIdentity + { + /// Performs element-wise addition between two tensors. + /// The tensor to add with . + /// The tensor to add with . + /// A new tensor containing the result of + . + /// The shapes of and are not compatible. + public static Tensor operator +(in ReadOnlyTensorSpan left, in ReadOnlyTensorSpan right) => Add(left, right); + + /// Performs element-wise addition between a tensor and scalar. + /// The tensor to add with . + /// The scalar to add with . + /// A new tensor containing the result of + . + public static Tensor operator +(in ReadOnlyTensorSpan left, TScalar right) => Add(left, right); + + /// Performs element-wise addition between a tensor and scalar. + /// The scalar to add with . + /// The tensor to add with . + /// A new tensor containing the result of + . + public static Tensor operator +(TScalar left, in ReadOnlyTensorSpan right) => Add(right, left); + } + + /// The type of the elements in the tensor. + extension(Tensor tensor) + where TScalar : IAdditionOperators, IAdditiveIdentity + { + /// + public static Tensor operator +(Tensor left, Tensor right) => Add(left, right); + + /// + public static Tensor operator +(Tensor left, TScalar right) => Add(left, right); + + /// + public static Tensor operator +(TScalar left, Tensor right) => Add(right, left); + + /// + public void operator +=(in ReadOnlyTensorSpan other) => Add(tensor, other, tensor); + + /// + public void operator +=(TScalar other) => Add(tensor, other, tensor); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : IAdditionOperators, IAdditiveIdentity + { + /// + public static Tensor operator +(in TensorSpan left, in TensorSpan right) => Add(left, right); + + /// + public static Tensor operator +(in TensorSpan left, TScalar right) => Add(left, right); + + /// + public static Tensor operator +(TScalar left, in TensorSpan right) => Add(right, left); + + /// Performs in-place element-wise addition between two tensors. + /// The tensor to add to the tensor being operated on. + public void operator +=(in ReadOnlyTensorSpan other) => Add(tensor, other, tensor); + + /// Performs in-place element-wise addition between a tensor and scalar. + /// The scalar to add to the tensor being operated on. + public void operator +=(TScalar other) => Add(tensor, other, tensor); + } + } +} From 1a797d849e6287708f87f3783ce28dca4f2a05ad Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 12:18:11 -0700 Subject: [PATCH 03/20] Adding the extensions for operator / and operator /= --- .../ref/System.Numerics.Tensors.netcore.cs | 25 +++ .../src/System.Numerics.Tensors.csproj | 1 + .../System/Numerics/Tensors/netcore/Tensor.cs | 85 ---------- .../Tensors/netcore/Tensor.op_Division.cs | 159 ++++++++++++++++++ 4 files changed, 185 insertions(+), 85 deletions(-) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Division.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index 7a520954b6c491..5ed2c3e183230f 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -603,6 +603,31 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator +=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator +=(TScalar other) { throw null; } } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IDivisionOperators + { + public static System.Numerics.Tensors.Tensor operator /(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator /(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator /(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + } + extension(Tensor tensor) + where TScalar : System.Numerics.IDivisionOperators + { + public static System.Numerics.Tensors.Tensor operator /(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } + public static System.Numerics.Tensors.Tensor operator /(System.Numerics.Tensors.Tensor left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator /(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } + public void operator /=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator /=(TScalar other) { throw null; } + } + extension(ref TensorSpan tensor) + where TScalar : System.Numerics.IDivisionOperators + { + public static System.Numerics.Tensors.Tensor operator /(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator /(in System.Numerics.Tensors.TensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator /(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public void operator /=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator /=(TScalar other) { throw null; } + } } public readonly ref partial struct TensorDimensionSpan { diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 2da2a555f9e710..0af5b0817f5e1c 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -40,6 +40,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs index e83b0e8a728c20..71ce9c8f3d2b48 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs @@ -3273,91 +3273,6 @@ public static T Distance(scoped in ReadOnlyTensorSpan x, scoped in ReadOnl } #endregion - #region Divide - /// - /// Divides each element of by and returns a new with the result. - /// - /// Input . - /// The divisor - public static Tensor Divide(in ReadOnlyTensorSpan x, T y) - where T : IDivisionOperators - { - Tensor destination = CreateFromShapeUninitialized(x.Lengths); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Divides by each element of and returns a new with the result."/> - /// - /// The value to be divided. - /// The divisor. - public static Tensor Divide(T x, in ReadOnlyTensorSpan y) - where T : IDivisionOperators - { - Tensor destination = CreateFromShapeUninitialized(y.Lengths); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Divides each element of by its corresponding element in and returns - /// a new with the result. - /// - /// The to be divided. - /// The divisor. - public static Tensor Divide(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) - where T : IDivisionOperators - { - TensorOperation.ValidateCompatibility(x, y, out Tensor destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Divides each element of by and returns a new with the result. - /// - /// Input . - /// The divisor - /// - public static ref readonly TensorSpan Divide(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) - where T : IDivisionOperators - { - TensorOperation.ValidateCompatibility(x, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - - /// - /// Divides by each element of and returns a new with the result."/> - /// - /// The value to be divided. - /// The divisor. - /// - public static ref readonly TensorSpan Divide(T x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) - where T : IDivisionOperators - { - TensorOperation.ValidateCompatibility(y, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - - /// - /// Divides each element of by its corresponding element in and returns - /// a new with the result. - /// - /// The to be divided. - /// The divisor. - /// - public static ref readonly TensorSpan Divide(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) - where T : IDivisionOperators - { - TensorOperation.ValidateCompatibility(x, y, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - #endregion - #region Dot /// /// Computes the dot product of two tensors containing numbers. diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Division.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Division.cs new file mode 100644 index 00000000000000..c94881f48091a1 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Division.cs @@ -0,0 +1,159 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs element-wise division between two tensors. + /// The type of the elements in the tensor. + /// The tensor dividend. + /// The tensor divisor. + /// A new tensor containing the result of / . + /// The shapes of and are not compatible. + public static Tensor Divide(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) + where T : IDivisionOperators + { + TensorOperation.ValidateCompatibility(x, y, out Tensor destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs element-wise division between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor dividend. + /// The scalar divisor. + /// A new tensor containing the result of / . + public static Tensor Divide(in ReadOnlyTensorSpan x, T y) + where T : IDivisionOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs element-wise division between a tensor and scalar. + /// The type of the elements in the tensor. + /// The scalar dividend. + /// The tensor divisor. + /// A new tensor containing the result of / . + public static Tensor Divide(T x, in ReadOnlyTensorSpan y) + where T : IDivisionOperators + { + Tensor destination = CreateFromShapeUninitialized(y.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs element-wise division between two tensors. + /// The type of the elements in the tensor. + /// The tensor dividend. + /// The tensor divisor. + /// The destination where the result of / is written. + /// A reference to . + /// The shapes of , , and are not compatible. + public static ref readonly TensorSpan Divide(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) + where T : IDivisionOperators + { + TensorOperation.ValidateCompatibility(x, y, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// Performs element-wise division between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor dividend. + /// The scalar divisor. + /// The destination where the result of / is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan Divide(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) + where T : IDivisionOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// Performs element-wise division between a tensor and scalar. + /// The type of the elements in the tensor. + /// The scalar dividend. + /// The tensor divisor. + /// The destination where the result of / is written. + /// The shapes of and are not compatible. + public static ref readonly TensorSpan Divide(T x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) + where T : IDivisionOperators + { + TensorOperation.ValidateCompatibility(y, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IDivisionOperators + { + /// Performs element-wise division between two tensors. + /// The tensor dividend. + /// The tensor divisor. + /// A new tensor containing the result of / . + /// The shapes of and are not compatible. + public static Tensor operator /(in ReadOnlyTensorSpan left, in ReadOnlyTensorSpan right) => Divide(left, right); + + /// Performs element-wise division between a tensor and scalar. + /// The tensor dividend. + /// The scalar divisor. + /// A new tensor containing the result of / . + public static Tensor operator /(in ReadOnlyTensorSpan left, TScalar right) => Divide(left, right); + + /// Performs element-wise division between a tensor and scalar. + /// The scalar dividend. + /// The tensor divisor. + /// A new tensor containing the result of / . + public static Tensor operator /(TScalar left, in ReadOnlyTensorSpan right) => Divide(right, left); + } + + /// The type of the elements in the tensor. + extension(Tensor tensor) + where TScalar : IDivisionOperators + { + /// + public static Tensor operator /(Tensor left, Tensor right) => Divide(left, right); + + /// + public static Tensor operator /(Tensor left, TScalar right) => Divide(left, right); + + /// + public static Tensor operator /(TScalar left, Tensor right) => Divide(right, left); + + /// + public void operator /=(in ReadOnlyTensorSpan other) => Divide(tensor, other, tensor); + + /// + public void operator /=(TScalar other) => Divide(tensor, other, tensor); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : IDivisionOperators + { + /// + public static Tensor operator /(in TensorSpan left, in TensorSpan right) => Divide(left, right); + + /// + public static Tensor operator /(in TensorSpan left, TScalar right) => Divide(left, right); + + /// + public static Tensor operator /(TScalar left, in TensorSpan right) => Divide(right, left); + + /// Performs in-place element-wise division between two tensors. + /// The tensor divisor. + public void operator /=(in ReadOnlyTensorSpan other) => Divide(tensor, other, tensor); + + /// Performs in-place element-wise division between a tensor and scalar. + /// The scalar divisor. + public void operator /=(TScalar other) => Divide(tensor, other, tensor); + } + } +} From 9688da1096daec3e0bc59f093eaa00671a880f7b Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 12:18:55 -0700 Subject: [PATCH 04/20] Adding the extensions for operator * and operator *= --- .../ref/System.Numerics.Tensors.netcore.cs | 25 ++++ .../src/System.Numerics.Tensors.csproj | 1 + .../System/Numerics/Tensors/netcore/Tensor.cs | 58 -------- .../Tensors/netcore/Tensor.op_Multiply.cs | 132 ++++++++++++++++++ 4 files changed, 158 insertions(+), 58 deletions(-) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Multiply.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index 5ed2c3e183230f..d4b71fd2065f7d 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -628,6 +628,31 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator /=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator /=(TScalar other) { throw null; } } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IMultiplyOperators, System.Numerics.IMultiplicativeIdentity + { + public static System.Numerics.Tensors.Tensor operator *(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator *(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator *(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + } + extension(Tensor tensor) + where TScalar : System.Numerics.IMultiplyOperators, System.Numerics.IMultiplicativeIdentity + { + public static System.Numerics.Tensors.Tensor operator *(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } + public static System.Numerics.Tensors.Tensor operator *(System.Numerics.Tensors.Tensor left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator *(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } + public void operator *=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator *=(TScalar other) { throw null; } + } + extension(ref TensorSpan tensor) + where TScalar : System.Numerics.IMultiplyOperators, System.Numerics.IMultiplicativeIdentity + { + public static System.Numerics.Tensors.Tensor operator *(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator *(in System.Numerics.Tensors.TensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator *(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public void operator *=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator *=(TScalar other) { throw null; } + } } public readonly ref partial struct TensorDimensionSpan { diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 0af5b0817f5e1c..ede6f90ab9c452 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -41,6 +41,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs index 71ce9c8f3d2b48..97410fc81f355f 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs @@ -4371,64 +4371,6 @@ public static ref readonly TensorSpan MinNumber(scoped in ReadOnlyTensorSp } #endregion - #region Multiply - /// - /// Multiplies each element of with and returns a new with the result. - /// - /// Input - /// value to multiply by. - public static Tensor Multiply(in ReadOnlyTensorSpan x, T y) - where T : IMultiplyOperators, IMultiplicativeIdentity - { - Tensor destination = CreateFromShape(x.Lengths); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Multiplies each element of with and returns a new with the result. - /// If the shapes are not the same they are broadcast to the smallest compatible shape. - /// - /// Left for multiplication. - /// Right for multiplication. - public static Tensor Multiply(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) - where T : IMultiplyOperators, IMultiplicativeIdentity - { - TensorOperation.ValidateCompatibility(x, y, out Tensor destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Multiplies each element of with and returns a new with the result. - /// - /// Input - /// value to multiply by. - /// - public static ref readonly TensorSpan Multiply(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) - where T : IMultiplyOperators, IMultiplicativeIdentity - { - TensorOperation.ValidateCompatibility(x, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - - /// - /// Multiplies each element of with and returns a new with the result. - /// If the shapes are not the same they are broadcast to the smallest compatible shape. - /// - /// Left for multiplication. - /// Right for multiplication. - /// - public static ref readonly TensorSpan Multiply(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) - where T : IMultiplyOperators, IMultiplicativeIdentity - { - TensorOperation.ValidateCompatibility(x, y, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - #endregion - #region Negate /// Computes the element-wise negation of each number in the specified tensor. /// The diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Multiply.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Multiply.cs new file mode 100644 index 00000000000000..ca38d7b970a1f8 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Multiply.cs @@ -0,0 +1,132 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs element-wise multiplication between two tensors. + /// The type of the elements in the tensor. + /// The tensor to multiply with . + /// The tensor to multiply with . + /// A new tensor containing the result of * . + /// The shapes of and are not compatible. + public static Tensor Multiply(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) + where T : IMultiplyOperators, IMultiplicativeIdentity + { + TensorOperation.ValidateCompatibility(x, y, out Tensor destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs element-wise multiplication between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor to multiply with . + /// The scalar to multiply with . + /// A new tensor containing the result of * . + public static Tensor Multiply(in ReadOnlyTensorSpan x, T y) + where T : IMultiplyOperators, IMultiplicativeIdentity + { + Tensor destination = CreateFromShape(x.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs element-wise multiplication between two tensors. + /// The type of the elements in the tensor. + /// The tensor to multiply with . + /// The tensor to multiply with . + /// The destination where the result of * is written. + /// A reference to . + /// The shapes of , , and are not compatible. + public static ref readonly TensorSpan Multiply(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) + where T : IMultiplyOperators, IMultiplicativeIdentity + { + TensorOperation.ValidateCompatibility(x, y, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// Performs element-wise multiplication between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor to multiply with . + /// The scalar to multiply with . + /// The destination where the result of * is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan Multiply(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) + where T : IMultiplyOperators, IMultiplicativeIdentity + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IMultiplyOperators, System.Numerics.IMultiplicativeIdentity + { + /// Performs element-wise multiplication between two tensors. + /// The tensor to multiply with . + /// The tensor to multiply with . + /// A new tensor containing the result of * . + /// The shapes of and are not compatible. + public static Tensor operator *(in ReadOnlyTensorSpan left, in ReadOnlyTensorSpan right) => Multiply(left, right); + + /// Performs element-wise multiplication between a tensor and scalar. + /// The tensor to multiply with . + /// The scalar to multiply with . + /// A new tensor containing the result of * . + public static Tensor operator *(in ReadOnlyTensorSpan left, TScalar right) => Multiply(left, right); + + /// Performs element-wise multiplication between a tensor and scalar. + /// The scalar to multiply with . + /// The tensor to multiply with . + /// A new tensor containing the result of * . + public static Tensor operator *(TScalar left, in ReadOnlyTensorSpan right) => Multiply(right, left); + } + + /// The type of the elements in the tensor. + extension(Tensor tensor) + where TScalar : IMultiplyOperators, System.Numerics.IMultiplicativeIdentity + { + /// + public static Tensor operator *(Tensor left, Tensor right) => Multiply(left, right); + + /// + public static Tensor operator *(Tensor left, TScalar right) => Multiply(left, right); + + /// + public static Tensor operator *(TScalar left, Tensor right) => Multiply(right, left); + + /// + public void operator *=(in ReadOnlyTensorSpan other) => Multiply(tensor, other, tensor); + + /// + public void operator *=(TScalar other) => Multiply(tensor, other, tensor); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : IMultiplyOperators, System.Numerics.IMultiplicativeIdentity + { + /// + public static Tensor operator *(in TensorSpan left, in TensorSpan right) => Multiply(left, right); + + /// + public static Tensor operator *(in TensorSpan left, TScalar right) => Multiply(left, right); + + /// + public static Tensor operator *(TScalar left, in TensorSpan right) => Multiply(right, left); + + /// Performs in-place element-wise multiplication between two tensors. + /// The tensor used to multiply the tensor being operated on. + public void operator *=(in ReadOnlyTensorSpan other) => Multiply(tensor, other, tensor); + + /// Performs in-place element-wise multiplication between a tensor and scalar. + /// The scalar used to multiply the tensor being operated on. + public void operator *=(TScalar other) => Multiply(tensor, other, tensor); + } + } +} From b79804bffbe09436d18d0b7a6e9cedaf49ad5e26 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 12:14:08 -0700 Subject: [PATCH 05/20] Adding the extensions for operator - and operator -= --- .../ref/System.Numerics.Tensors.netcore.cs | 25 +++ .../src/System.Numerics.Tensors.csproj | 1 + .../System/Numerics/Tensors/netcore/Tensor.cs | 83 --------- .../Tensors/netcore/Tensor.op_Subtraction.cs | 160 ++++++++++++++++++ 4 files changed, 186 insertions(+), 83 deletions(-) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Subtraction.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index d4b71fd2065f7d..a182231affaf18 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -653,6 +653,31 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator *=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator *=(TScalar other) { throw null; } } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.ISubtractionOperators + { + public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator -(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + } + extension(Tensor tensor) + where TScalar : System.Numerics.ISubtractionOperators + { + public static System.Numerics.Tensors.Tensor operator -(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } + public static System.Numerics.Tensors.Tensor operator -(System.Numerics.Tensors.Tensor left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator -(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } + public void operator -=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator -=(TScalar other) { throw null; } + } + extension(ref TensorSpan tensor) + where TScalar : System.Numerics.ISubtractionOperators + { + public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.TensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator -(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public void operator -=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator -=(TScalar other) { throw null; } + } } public readonly ref partial struct TensorDimensionSpan { diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index ede6f90ab9c452..c85c0f4c3dfb84 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -42,6 +42,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs index 97410fc81f355f..b5339be6796093 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs @@ -4926,89 +4926,6 @@ public static T StdDev(in ReadOnlyTensorSpan x) } #endregion - #region Subtract - /// - /// Subtracts from each element of and returns a new with the result. - /// - /// The . - /// The to subtract. - public static Tensor Subtract(in ReadOnlyTensorSpan x, T y) - where T : ISubtractionOperators - { - Tensor destination = CreateFromShapeUninitialized(x.Lengths); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Subtracts each element of from and returns a new with the result. - /// - /// The to be subtracted from. - /// The of values to subtract. - public static Tensor Subtract(T x, in ReadOnlyTensorSpan y) - where T : ISubtractionOperators - { - Tensor destination = CreateFromShapeUninitialized(y.Lengths); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Subtracts each element of from and returns a new with the result. - /// - /// The with values to be subtracted from. - /// The with values to subtract. - public static Tensor Subtract(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) - where T : ISubtractionOperators - { - TensorOperation.ValidateCompatibility(x, y, out Tensor destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Subtracts from each element of and returns a new with the result. - /// - /// The with values to be subtracted from. - /// The value to subtract. - /// - public static ref readonly TensorSpan Subtract(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) - where T : ISubtractionOperators - { - TensorOperation.ValidateCompatibility(x, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - - /// - /// Subtracts each element of from and returns a new with the result. - /// - /// The value to be subtracted from. - /// The values to subtract. - /// - public static ref readonly TensorSpan Subtract(T x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) - where T : ISubtractionOperators - { - TensorOperation.ValidateCompatibility(y, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - - /// - /// Subtracts each element of from and returns a new with the result. - /// - /// The of values to be subtracted from. - /// The of values to subtract. - /// - public static ref readonly TensorSpan Subtract(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) - where T : ISubtractionOperators - { - TensorOperation.ValidateCompatibility(x, y, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - #endregion - #region Sum /// /// Sums the elements of the specified tensor. diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Subtraction.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Subtraction.cs new file mode 100644 index 00000000000000..0a601528529052 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Subtraction.cs @@ -0,0 +1,160 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs element-wise subtraction between two tensors. + /// The type of the elements in the tensor. + /// The tensor from which to subtract . + /// The tensor to subtract from . + /// A new tensor containing the result of - . + /// The shapes of and are not compatible. + public static Tensor Subtract(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) + where T : ISubtractionOperators + { + TensorOperation.ValidateCompatibility(x, y, out Tensor destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs element-wise subtraction between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor from which to subtract . + /// The scalar to subtract from . + /// A new tensor containing the result of - . + public static Tensor Subtract(in ReadOnlyTensorSpan x, T y) + where T : ISubtractionOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs element-wise subtraction between a tensor and scalar. + /// The type of the elements in the tensor. + /// The scalar from which to subtract . + /// The tensor to subtract from . + /// A new tensor containing the result of - . + public static Tensor Subtract(T x, in ReadOnlyTensorSpan y) + where T : ISubtractionOperators + { + Tensor destination = CreateFromShapeUninitialized(y.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs element-wise subtraction between two tensors. + /// The type of the elements in the tensor. + /// The tensor from which to subtract . + /// The tensor to subtract from . + /// The destination where the result of - is written. + /// A reference to . + /// The shapes of , , and are not compatible. + public static ref readonly TensorSpan Subtract(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) + where T : ISubtractionOperators + { + TensorOperation.ValidateCompatibility(x, y, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// Performs element-wise subtraction between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor from which to subtract . + /// The scalar to subtract from . + /// The destination where the result of - is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan Subtract(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) + where T : ISubtractionOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// Performs element-wise subtraction between a tensor and scalar. + /// The type of the elements in the tensor. + /// The scalar from which to subtract . + /// The tensor to subtract from . + /// The destination where the result of - is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan Subtract(T x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) + where T : ISubtractionOperators + { + TensorOperation.ValidateCompatibility(y, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : ISubtractionOperators + { + /// Performs element-wise subtraction between two tensors. + /// The tensor from which to subtract . + /// The tensor to subtract from . + /// A new tensor containing the result of - . + /// The shapes of and are not compatible. + public static Tensor operator -(in ReadOnlyTensorSpan left, in ReadOnlyTensorSpan right) => Subtract(left, right); + + /// Performs element-wise subtraction between a tensor and scalar. + /// The tensor from which to subtract . + /// The scalar to subtract from . + /// A new tensor containing the result of - . + public static Tensor operator -(in ReadOnlyTensorSpan left, TScalar right) => Subtract(left, right); + + /// Performs element-wise subtraction between a tensor and scalar. + /// The scalar from which to subtract . + /// The tensor to subtract from . + /// A new tensor containing the result of - . + public static Tensor operator -(TScalar left, in ReadOnlyTensorSpan right) => Subtract(right, left); + } + + /// The type of the elements in the tensor. + extension(Tensor tensor) + where TScalar : ISubtractionOperators + { + /// + public static Tensor operator -(Tensor left, Tensor right) => Subtract(left, right); + + /// + public static Tensor operator -(Tensor left, TScalar right) => Subtract(left, right); + + /// + public static Tensor operator -(TScalar left, Tensor right) => Subtract(right, left); + + /// + public void operator -=(in ReadOnlyTensorSpan other) => Subtract(tensor, other, tensor); + + /// + public void operator -=(TScalar other) => Subtract(tensor, other, tensor); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : ISubtractionOperators + { + /// + public static Tensor operator -(in TensorSpan left, in TensorSpan right) => Subtract(left, right); + + /// + public static Tensor operator -(in TensorSpan left, TScalar right) => Subtract(left, right); + + /// + public static Tensor operator -(TScalar left, in TensorSpan right) => Subtract(right, left); + + /// Performs in-place element-wise subtraction between two tensors. + /// The tensor to subtract from the tensor being operated on. + public void operator -=(in ReadOnlyTensorSpan other) => Subtract(tensor, other, tensor); + + /// Performs in-place element-wise subtraction between a tensor and scalar. + /// The scalar to subtract from the tensor being operated on. + public void operator -=(TScalar other) => Subtract(tensor, other, tensor); + } + } +} From b6e34c4a3c5ca333597c623cc8b3ac88478ef683 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 12:33:23 -0700 Subject: [PATCH 06/20] Adding the extensions for operator & and operator &= --- .../ref/System.Numerics.Tensors.netcore.cs | 25 ++++ .../src/System.Numerics.Tensors.csproj | 1 + .../System/Numerics/Tensors/netcore/Tensor.cs | 56 -------- .../Tensors/netcore/Tensor.op_BitwiseAnd.cs | 132 ++++++++++++++++++ 4 files changed, 158 insertions(+), 56 deletions(-) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_BitwiseAnd.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index a182231affaf18..7b37144e1d4056 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -603,6 +603,31 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator +=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator +=(TScalar other) { throw null; } } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IBitwiseOperators + { + public static System.Numerics.Tensors.Tensor operator &(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator &(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator &(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + } + extension(Tensor tensor) + where TScalar : System.Numerics.IBitwiseOperators + { + public static System.Numerics.Tensors.Tensor operator &(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } + public static System.Numerics.Tensors.Tensor operator &(System.Numerics.Tensors.Tensor left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator &(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } + public void operator &=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator &=(TScalar other) { throw null; } + } + extension(ref TensorSpan tensor) + where TScalar : System.Numerics.IBitwiseOperators + { + public static System.Numerics.Tensors.Tensor operator &(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator &(in System.Numerics.Tensors.TensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator &(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public void operator &=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator &=(TScalar other) { throw null; } + } extension(System.Numerics.Tensors.ReadOnlyTensorSpan) where TScalar : System.Numerics.IDivisionOperators { diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index c85c0f4c3dfb84..77ab1eb029ffc4 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -40,6 +40,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs index b5339be6796093..4e9e1e787d6035 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs @@ -2798,62 +2798,6 @@ public static T Average(scoped in ReadOnlyTensorSpan x) } #endregion - #region BitwiseAnd - /// - /// Computes the element-wise bitwise and of the two input and returns a new with the result. - /// - /// The left . - /// The right . - public static Tensor BitwiseAnd(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) - where T : IBitwiseOperators - { - Tensor destination = CreateFromShapeUninitialized(x.Lengths); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Computes the element-wise bitwise and of the two input and returns a new with the result. - /// - /// The left . - /// The right . - /// - public static ref readonly TensorSpan BitwiseAnd(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) - where T : IBitwiseOperators - { - TensorOperation.ValidateCompatibility(x, y, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - - /// - /// Computes the element-wise bitwise and of the two input and returns a new with the result. - /// - /// The left . - /// The second value. - public static Tensor BitwiseAnd(in ReadOnlyTensorSpan x, T y) - where T : IBitwiseOperators - { - Tensor destination = CreateFromShapeUninitialized(x.Lengths); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Computes the element-wise bitwise and of the two input and returns a new with the result. - /// - /// The left . - /// The second value. - /// - public static ref readonly TensorSpan BitwiseAnd(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) - where T : IBitwiseOperators - { - TensorOperation.ValidateCompatibility(x, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - #endregion - #region BitwiseOr /// /// Computes the element-wise bitwise of of the two input and returns a new with the result. diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_BitwiseAnd.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_BitwiseAnd.cs new file mode 100644 index 00000000000000..dc2417aeb7db48 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_BitwiseAnd.cs @@ -0,0 +1,132 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs bitwise-and between two tensors. + /// The type of the elements in the tensor. + /// The tensor to bitwise-and with . + /// The tensor to bitwise-and with . + /// A new tensor containing the result of & . + /// The shapes of and are not compatible. + public static Tensor BitwiseAnd(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) + where T : IBitwiseOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs bitwise-and between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor to bitwise-and with . + /// The scalar to bitwise-and with . + /// A new tensor containing the result of & . + public static Tensor BitwiseAnd(in ReadOnlyTensorSpan x, T y) + where T : IBitwiseOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs bitwise-and between two tensors. + /// The type of the elements in the tensor. + /// The tensor to bitwise-and with . + /// The tensor to bitwise-and with . + /// The destination where the result of & is written. + /// A reference to . + /// The shapes of , , and are not compatible. + public static ref readonly TensorSpan BitwiseAnd(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) + where T : IBitwiseOperators + { + TensorOperation.ValidateCompatibility(x, y, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// Performs bitwise-and between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor to bitwise-and with . + /// The scalar to bitwise-and with . + /// The destination where the result of & is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan BitwiseAnd(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) + where T : IBitwiseOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IBitwiseOperators + { + /// Performs bitwise-and between two tensors. + /// The tensor to bitwise-and with . + /// The tensor to bitwise-and with . + /// A new tensor containing the result of & . + /// The shapes of and are not compatible. + public static Tensor operator &(in ReadOnlyTensorSpan left, in ReadOnlyTensorSpan right) => BitwiseAnd(left, right); + + /// Performs bitwise-and between a tensor and scalar. + /// The tensor to bitwise-and with . + /// The scalar to bitwise-and with . + /// A new tensor containing the result of & . + public static Tensor operator &(in ReadOnlyTensorSpan left, TScalar right) => BitwiseAnd(left, right); + + /// Performs bitwise-and between a tensor and scalar. + /// The scalar to bitwise-and with . + /// The tensor to bitwise-and with . + /// A new tensor containing the result of & . + public static Tensor operator &(TScalar left, in ReadOnlyTensorSpan right) => BitwiseAnd(right, left); + } + + /// The type of the elements in the tensor. + extension(Tensor tensor) + where TScalar : IBitwiseOperators + { + /// + public static Tensor operator &(Tensor left, Tensor right) => BitwiseAnd(left, right); + + /// + public static Tensor operator &(Tensor left, TScalar right) => BitwiseAnd(left, right); + + /// + public static Tensor operator &(TScalar left, Tensor right) => BitwiseAnd(right, left); + + /// + public void operator &=(in ReadOnlyTensorSpan other) => BitwiseAnd(tensor, other, tensor); + + /// + public void operator &=(TScalar other) => BitwiseAnd(tensor, other, tensor); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : IBitwiseOperators + { + /// + public static Tensor operator &(in TensorSpan left, in TensorSpan right) => BitwiseAnd(left, right); + + /// + public static Tensor operator &(in TensorSpan left, TScalar right) => BitwiseAnd(left, right); + + /// + public static Tensor operator &(TScalar left, in TensorSpan right) => BitwiseAnd(right, left); + + /// Performs in-place bitwise-and between two tensors. + /// The tensor to bitwise-and with the tensor being operated on. + public void operator &=(in ReadOnlyTensorSpan other) => BitwiseAnd(tensor, other, tensor); + + /// Performs in-place bitwise-and between a tensor and scalar. + /// The scalar to bitwise-and with the tensor being operated on. + public void operator &=(TScalar other) => BitwiseAnd(tensor, other, tensor); + } + } +} From 237895f8bb8e4e02ffd3fdd959d0d43c5710d70e Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 12:37:20 -0700 Subject: [PATCH 07/20] Adding the extensions for operator | and operator |= --- .../ref/System.Numerics.Tensors.netcore.cs | 13 ++ .../src/System.Numerics.Tensors.csproj | 1 + .../System/Numerics/Tensors/netcore/Tensor.cs | 56 -------- .../Tensors/netcore/Tensor.op_BitwiseOr.cs | 132 ++++++++++++++++++ 4 files changed, 146 insertions(+), 56 deletions(-) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_BitwiseOr.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index 7b37144e1d4056..c12ccad3de4aff 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -609,6 +609,9 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator &(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } public static System.Numerics.Tensors.Tensor operator &(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } public static System.Numerics.Tensors.Tensor operator &(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } } extension(Tensor tensor) where TScalar : System.Numerics.IBitwiseOperators @@ -618,6 +621,11 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator &(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } public void operator &=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator &=(TScalar other) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(System.Numerics.Tensors.Tensor left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } + public void operator |=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator |=(TScalar other) { throw null; } } extension(ref TensorSpan tensor) where TScalar : System.Numerics.IBitwiseOperators @@ -627,6 +635,11 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator &(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } public void operator &=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator &=(TScalar other) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.TensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public void operator |=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator |=(TScalar other) { throw null; } } extension(System.Numerics.Tensors.ReadOnlyTensorSpan) where TScalar : System.Numerics.IDivisionOperators diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 77ab1eb029ffc4..17d75b10be4420 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -41,6 +41,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs index 4e9e1e787d6035..b56e3c179b04d0 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs @@ -2798,62 +2798,6 @@ public static T Average(scoped in ReadOnlyTensorSpan x) } #endregion - #region BitwiseOr - /// - /// Computes the element-wise bitwise of of the two input and returns a new with the result. - /// - /// The left . - /// The right . - public static Tensor BitwiseOr(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) - where T : IBitwiseOperators - { - Tensor destination = CreateFromShapeUninitialized(x.Lengths); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Computes the element-wise bitwise of of the two input and returns a new with the result. - /// - /// The left . - /// The right . - /// - public static ref readonly TensorSpan BitwiseOr(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) - where T : IBitwiseOperators - { - TensorOperation.ValidateCompatibility(x, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - - /// - /// Computes the element-wise bitwise or of the two input and returns a new with the result. - /// - /// The left . - /// The second value. - public static Tensor BitwiseOr(in ReadOnlyTensorSpan x, T y) - where T : IBitwiseOperators - { - Tensor destination = CreateFromShapeUninitialized(x.Lengths); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Computes the element-wise bitwise or of the two input and returns a new with the result. - /// - /// The left . - /// The second value. - /// - public static ref readonly TensorSpan BitwiseOr(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) - where T : IBitwiseOperators - { - TensorOperation.ValidateCompatibility(x, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - #endregion - #region CubeRoot /// /// Computes the element-wise cube root of the input and returns a new with the result. diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_BitwiseOr.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_BitwiseOr.cs new file mode 100644 index 00000000000000..f54f3600adeb12 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_BitwiseOr.cs @@ -0,0 +1,132 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs bitwise-or between two tensors. + /// The type of the elements in the tensor. + /// The tensor to bitwise-or with . + /// The tensor to bitwise-or with . + /// A new tensor containing the result of | . + /// The shapes of and are not compatible. + public static Tensor BitwiseOr(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) + where T : IBitwiseOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs bitwise-or between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor to bitwise-or with . + /// The scalar to bitwise-or with . + /// A new tensor containing the result of | . + public static Tensor BitwiseOr(in ReadOnlyTensorSpan x, T y) + where T : IBitwiseOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs bitwise-or between two tensors. + /// The type of the elements in the tensor. + /// The tensor to bitwise-or with . + /// The tensor to bitwise-or with . + /// The destination where the result of | is written. + /// A reference to . + /// The shapes of , , and are not compatible. + public static ref readonly TensorSpan BitwiseOr(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) + where T : IBitwiseOperators + { + TensorOperation.ValidateCompatibility(x, y, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// Performs bitwise-or between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor to bitwise-or with . + /// The scalar to bitwise-or with . + /// The destination where the result of | is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan BitwiseOr(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) + where T : IBitwiseOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IBitwiseOperators + { + /// Performs bitwise-or between two tensors. + /// The tensor to bitwise-or with . + /// The tensor to bitwise-or with . + /// A new tensor containing the result of | . + /// The shapes of and are not compatible. + public static Tensor operator |(in ReadOnlyTensorSpan left, in ReadOnlyTensorSpan right) => BitwiseOr(left, right); + + /// Performs bitwise-or between a tensor and scalar. + /// The tensor to bitwise-or with . + /// The scalar to bitwise-or with . + /// A new tensor containing the result of | . + public static Tensor operator |(in ReadOnlyTensorSpan left, TScalar right) => BitwiseOr(left, right); + + /// Performs bitwise-or between a tensor and scalar. + /// The scalar to bitwise-or with . + /// The tensor to bitwise-or with . + /// A new tensor containing the result of | . + public static Tensor operator |(TScalar left, in ReadOnlyTensorSpan right) => BitwiseOr(right, left); + } + + /// The type of the elements in the tensor. + extension(Tensor tensor) + where TScalar : IBitwiseOperators + { + /// + public static Tensor operator |(Tensor left, Tensor right) => BitwiseOr(left, right); + + /// + public static Tensor operator |(Tensor left, TScalar right) => BitwiseOr(left, right); + + /// + public static Tensor operator |(TScalar left, Tensor right) => BitwiseOr(right, left); + + /// + public void operator |=(in ReadOnlyTensorSpan other) => BitwiseOr(tensor, other, tensor); + + /// + public void operator |=(TScalar other) => BitwiseOr(tensor, other, tensor); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : IBitwiseOperators + { + /// + public static Tensor operator |(in TensorSpan left, in TensorSpan right) => BitwiseOr(left, right); + + /// + public static Tensor operator |(in TensorSpan left, TScalar right) => BitwiseOr(left, right); + + /// + public static Tensor operator |(TScalar left, in TensorSpan right) => BitwiseOr(right, left); + + /// Performs in-place bitwise-or between two tensors. + /// The tensor to bitwise-or with the tensor being operated on. + public void operator |=(in ReadOnlyTensorSpan other) => BitwiseOr(tensor, other, tensor); + + /// Performs in-place bitwise-or between a tensor and scalar. + /// The scalar to bitwise-or with the tensor being operated on. + public void operator |=(TScalar other) => BitwiseOr(tensor, other, tensor); + } + } +} From 939e9d3a31a0946e42e34104c3706ccee9ea0b31 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 12:39:55 -0700 Subject: [PATCH 08/20] Adding the extensions for operator ^ and operator ^= --- .../ref/System.Numerics.Tensors.netcore.cs | 13 ++ .../src/System.Numerics.Tensors.csproj | 1 + .../System/Numerics/Tensors/netcore/Tensor.cs | 52 ------- .../Tensors/netcore/Tensor.op_ExclusiveOr.cs | 132 ++++++++++++++++++ 4 files changed, 146 insertions(+), 52 deletions(-) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_ExclusiveOr.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index c12ccad3de4aff..a1398c22c45b0c 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -612,6 +612,9 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } public static System.Numerics.Tensors.Tensor operator |(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } } extension(Tensor tensor) where TScalar : System.Numerics.IBitwiseOperators @@ -626,6 +629,11 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator |(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } public void operator |=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator |=(TScalar other) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(System.Numerics.Tensors.Tensor left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } + public void operator ^=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator ^=(TScalar other) { throw null; } } extension(ref TensorSpan tensor) where TScalar : System.Numerics.IBitwiseOperators @@ -640,6 +648,11 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator |(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } public void operator |=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator |=(TScalar other) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.TensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public void operator ^=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator ^=(TScalar other) { throw null; } } extension(System.Numerics.Tensors.ReadOnlyTensorSpan) where TScalar : System.Numerics.IDivisionOperators diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 17d75b10be4420..22915f8a70a9e5 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -43,6 +43,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs index b56e3c179b04d0..4d3bf117a72ad6 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs @@ -4958,58 +4958,6 @@ public static ref readonly TensorSpan Truncate(scoped in ReadOnlyTensorSpa return ref destination; } #endregion - - #region Xor - /// Computes the element-wise XOR of numbers in the specified tensors. - /// The left . - /// The right . - public static Tensor Xor(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) - where T : IBitwiseOperators - { - TensorOperation.ValidateCompatibility(x, y, out Tensor destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// Computes the element-wise XOR of numbers in the specified tensors. - /// The left . - /// The right . - /// - public static ref readonly TensorSpan Xor(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) - where T : IBitwiseOperators - { - TensorOperation.ValidateCompatibility(x, y, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - - /// - /// Computes the element-wise Xor of the two input and returns a new with the result. - /// - /// The left . - /// The second value. - public static Tensor Xor(in ReadOnlyTensorSpan x, T y) - where T : IBitwiseOperators - { - Tensor destination = CreateFromShapeUninitialized(x.Lengths); - TensorOperation.Invoke, T, T>(x, y, destination); - return destination; - } - - /// - /// Computes the element-wise Xor of the two input and returns a new with the result. - /// - /// The left . - /// The second value. - /// - public static ref readonly TensorSpan Xor(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) - where T : IBitwiseOperators - { - TensorOperation.ValidateCompatibility(x, destination); - TensorOperation.Invoke, T, T>(x, y, destination); - return ref destination; - } - #endregion #endregion } } diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_ExclusiveOr.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_ExclusiveOr.cs new file mode 100644 index 00000000000000..867873be53aa24 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_ExclusiveOr.cs @@ -0,0 +1,132 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs exclusive-or between two tensors. + /// The type of the elements in the tensor. + /// The tensor to exclusive-or with . + /// The tensor to exclusive-or with . + /// A new tensor containing the result of ^ . + /// The shapes of and are not compatible. + public static Tensor Xor(in ReadOnlyTensorSpan x, in ReadOnlyTensorSpan y) + where T : IBitwiseOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs exclusive-or between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor to exclusive-or with . + /// The scalar to exclusive-or with . + /// A new tensor containing the result of ^ . + public static Tensor Xor(in ReadOnlyTensorSpan x, T y) + where T : IBitwiseOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, y, destination); + return destination; + } + + /// Performs exclusive-or between two tensors. + /// The type of the elements in the tensor. + /// The tensor to exclusive-or with . + /// The tensor to exclusive-or with . + /// The destination where the result of ^ is written. + /// A reference to . + /// The shapes of , , and are not compatible. + public static ref readonly TensorSpan Xor(scoped in ReadOnlyTensorSpan x, scoped in ReadOnlyTensorSpan y, in TensorSpan destination) + where T : IBitwiseOperators + { + TensorOperation.ValidateCompatibility(x, y, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// Performs exclusive-or between a tensor and scalar. + /// The type of the elements in the tensor. + /// The tensor to exclusive-or with . + /// The scalar to exclusive-or with . + /// The destination where the result of ^ is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan Xor(scoped in ReadOnlyTensorSpan x, T y, in TensorSpan destination) + where T : IBitwiseOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, T>(x, y, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IBitwiseOperators + { + /// Performs exclusive-or between two tensors. + /// The tensor to exclusive-or with . + /// The tensor to exclusive-or with . + /// A new tensor containing the result of ^ . + /// The shapes of and are not compatible. + public static Tensor operator ^(in ReadOnlyTensorSpan left, in ReadOnlyTensorSpan right) => Xor(left, right); + + /// Performs exclusive-or between a tensor and scalar. + /// The tensor to exclusive-or with . + /// The scalar to exclusive-or with . + /// A new tensor containing the result of ^ . + public static Tensor operator ^(in ReadOnlyTensorSpan left, TScalar right) => Xor(left, right); + + /// Performs exclusive-or between a tensor and scalar. + /// The scalar to exclusive-or with . + /// The tensor to exclusive-or with . + /// A new tensor containing the result of ^ . + public static Tensor operator ^(TScalar left, in ReadOnlyTensorSpan right) => Xor(right, left); + } + + /// The type of the elements in the tensor. + extension(Tensor tensor) + where TScalar : IBitwiseOperators + { + /// + public static Tensor operator ^(Tensor left, Tensor right) => Xor(left, right); + + /// + public static Tensor operator ^(Tensor left, TScalar right) => Xor(left, right); + + /// + public static Tensor operator ^(TScalar left, Tensor right) => Xor(right, left); + + /// + public void operator ^=(in ReadOnlyTensorSpan other) => Xor(tensor, other, tensor); + + /// + public void operator ^=(TScalar other) => Xor(tensor, other, tensor); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : IBitwiseOperators + { + /// + public static Tensor operator ^(in TensorSpan left, in TensorSpan right) => Xor(left, right); + + /// + public static Tensor operator ^(in TensorSpan left, TScalar right) => Xor(left, right); + + /// + public static Tensor operator ^(TScalar left, in TensorSpan right) => Xor(right, left); + + /// Performs in-place exclusive-or between two tensors. + /// The tensor to exclusive-or with the tensor being operated on. + public void operator ^=(in ReadOnlyTensorSpan other) => Xor(tensor, other, tensor); + + /// Performs in-place exclusive-or between a tensor and scalar. + /// The scalar to exclusive-or with the tensor being operated on. + public void operator ^=(TScalar other) => Xor(tensor, other, tensor); + } + } +} From 2eab0d8ccfbb44728bd2f8dc890bf5f62d19628c Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 12:48:45 -0700 Subject: [PATCH 09/20] Adding the extensions for operator ~ --- .../ref/System.Numerics.Tensors.netcore.cs | 11 ++++ .../src/CompatibilitySuppressions.xml | 18 ++++++ .../src/System.Numerics.Tensors.csproj | 1 + .../System/Numerics/Tensors/netcore/Tensor.cs | 23 ------- .../netcore/Tensor.op_OnesComplement.cs | 60 +++++++++++++++++++ 5 files changed, 90 insertions(+), 23 deletions(-) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_OnesComplement.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index a1398c22c45b0c..ac2ddbdeffdded 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -615,6 +615,12 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } public static System.Numerics.Tensors.Tensor operator ^(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ~(in System.Numerics.Tensors.ReadOnlyTensorSpan tensor) { throw null; } + } + extension(Tensor) + where TScalar : System.Numerics.IBitwiseOperators + { + public static System.Numerics.Tensors.Tensor operator ~(in System.Numerics.Tensors.Tensor tensor) { throw null; } } extension(Tensor tensor) where TScalar : System.Numerics.IBitwiseOperators @@ -635,6 +641,11 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator ^=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator ^=(TScalar other) { throw null; } } + extension(TensorSpan) + where TScalar : System.Numerics.IBitwiseOperators + { + public static System.Numerics.Tensors.Tensor operator ~(in System.Numerics.Tensors.TensorSpan tensor) { throw null; } + } extension(ref TensorSpan tensor) where TScalar : System.Numerics.IBitwiseOperators { diff --git a/src/libraries/System.Numerics.Tensors/src/CompatibilitySuppressions.xml b/src/libraries/System.Numerics.Tensors/src/CompatibilitySuppressions.xml index 9c2a17e39157a9..a4ce8a42bcd7e0 100644 --- a/src/libraries/System.Numerics.Tensors/src/CompatibilitySuppressions.xml +++ b/src/libraries/System.Numerics.Tensors/src/CompatibilitySuppressions.xml @@ -1107,6 +1107,24 @@ lib/net9.0/System.Numerics.Tensors.dll true + + CP0017 + M:System.Numerics.Tensors.Tensor.OnesComplement``1(System.Numerics.Tensors.ReadOnlyTensorSpan{``0}@,System.Numerics.Tensors.TensorSpan{``0}@)$0 + ref/net10.0/System.Numerics.Tensors.dll + lib/net10.0/System.Numerics.Tensors.dll + + + CP0017 + M:System.Numerics.Tensors.Tensor.OnesComplement``1(System.Numerics.Tensors.ReadOnlyTensorSpan{``0}@,System.Numerics.Tensors.TensorSpan{``0}@)$0 + ref/net8.0/System.Numerics.Tensors.dll + lib/net8.0/System.Numerics.Tensors.dll + + + CP0017 + M:System.Numerics.Tensors.Tensor.OnesComplement``1(System.Numerics.Tensors.ReadOnlyTensorSpan{``0}@,System.Numerics.Tensors.TensorSpan{``0}@)$0 + ref/net9.0/System.Numerics.Tensors.dll + lib/net9.0/System.Numerics.Tensors.dll + CP0021 M:System.Numerics.Tensors.Tensor.Average``1(System.Numerics.Tensors.ReadOnlyTensorSpan{``0}@)``0:T:System.Numerics.INumberBase{``0} diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 22915f8a70a9e5..66789a65f0c958 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -45,6 +45,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs index 4d3bf117a72ad6..569e90c664968d 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs @@ -4296,29 +4296,6 @@ public static T Norm(scoped in ReadOnlyTensorSpan x) } #endregion - #region OnesComplement - /// Computes the element-wise one's complement of numbers in the specified tensor. - /// The - public static Tensor OnesComplement(in ReadOnlyTensorSpan x) - where T : IBitwiseOperators - { - Tensor destination = CreateFromShapeUninitialized(x.Lengths); - TensorOperation.Invoke, T, T>(x, destination); - return destination; - } - - /// Computes the element-wise one's complement of numbers in the specified tensor. - /// The - /// - public static ref readonly TensorSpan OnesComplement(scoped in ReadOnlyTensorSpan y, in TensorSpan destination) - where T : IBitwiseOperators - { - TensorOperation.ValidateCompatibility(y, destination); - TensorOperation.Invoke, T, T>(y, destination); - return ref destination; - } - #endregion - #region PopCount /// Computes the element-wise population count of numbers in the specified tensor. /// The diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_OnesComplement.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_OnesComplement.cs new file mode 100644 index 00000000000000..484a134d6548c6 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_OnesComplement.cs @@ -0,0 +1,60 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs a one's complement on a tensor. + /// The type of the elements in the tensor. + /// The tensor to one's complement. + /// A new tensor containing the result of ~. + public static Tensor OnesComplement(in ReadOnlyTensorSpan x) + where T : IBitwiseOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, destination); + return destination; + } + + /// Performs a one's complement on a tensor. + /// The type of the elements in the tensor. + /// The tensor to one's complement. + /// The destination where the result of ~ is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan OnesComplement(scoped in ReadOnlyTensorSpan x, in TensorSpan destination) + where T : IBitwiseOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, T>(x, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IBitwiseOperators + { + /// Performs a one's complement on a tensor. + /// The tensor to one's complement. + /// A new tensor containing the result of ~. + public static Tensor operator ~(in ReadOnlyTensorSpan tensor) => OnesComplement(tensor); + } + + /// The type of the elements in the tensor. + extension(Tensor) + where TScalar : IBitwiseOperators + { + /// + public static Tensor operator ~(Tensor tensor) => OnesComplement(tensor); + } + + /// The type of the elements in the tensor. + extension(TensorSpan) + where TScalar : IBitwiseOperators + { + /// + public static Tensor operator ~(in TensorSpan tensor) => OnesComplement(tensor); + } + } +} From 58d8f1c29ec10d531fd57afa1f63f88d64d6836b Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 13:07:24 -0700 Subject: [PATCH 10/20] Adding the extensions for operator - (unary) --- .../ref/System.Numerics.Tensors.netcore.cs | 16 +++++ .../src/System.Numerics.Tensors.csproj | 1 + .../System/Numerics/Tensors/netcore/Tensor.cs | 23 -------- .../netcore/Tensor.op_UnaryNegation.cs | 58 +++++++++++++++++++ 4 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnaryNegation.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index ac2ddbdeffdded..7467b8cec76609 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -740,6 +740,22 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator -=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator -=(TScalar other) { throw null; } } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IUnaryNegationOperators + { + public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.ReadOnlyTensorSpan tensor) { throw null; } + } + extension(Tensor) + where TScalar : System.Numerics.IUnaryNegationOperators + { + public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.Tensor tensor) { throw null; } + } + + extension(TensorSpan) + where TScalar : System.Numerics.IUnaryNegationOperators + { + public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.TensorSpan tensor) { throw null; } + } } public readonly ref partial struct TensorDimensionSpan { diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 66789a65f0c958..18ac694673dd45 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -47,6 +47,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs index 569e90c664968d..8bc145f8e0e042 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.cs @@ -4259,29 +4259,6 @@ public static ref readonly TensorSpan MinNumber(scoped in ReadOnlyTensorSp } #endregion - #region Negate - /// Computes the element-wise negation of each number in the specified tensor. - /// The - public static Tensor Negate(in ReadOnlyTensorSpan x) - where T : IUnaryNegationOperators - { - Tensor destination = CreateFromShapeUninitialized(x.Lengths); - TensorOperation.Invoke, T, T>(x, destination); - return destination; - } - - /// Computes the element-wise negation of each number in the specified tensor. - /// The - /// - public static ref readonly TensorSpan Negate(scoped in ReadOnlyTensorSpan x, in TensorSpan destination) - where T : IUnaryNegationOperators - { - TensorOperation.ValidateCompatibility(x, destination); - TensorOperation.Invoke, T, T>(x, destination); - return ref destination; - } - #endregion - #region Norm /// /// Takes the norm of the and returns the result. diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnaryNegation.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnaryNegation.cs new file mode 100644 index 00000000000000..261ed655688352 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnaryNegation.cs @@ -0,0 +1,58 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs element-wise unary negation on a tensor. + /// The tensor to negate. + /// A new tensor containing the result of -. + public static Tensor Negate(in ReadOnlyTensorSpan x) + where T : IUnaryNegationOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, destination); + return destination; + } + + /// Performs element-wise unary negation on a tensor. + /// The tensor to negate. + /// The destination where the result of - is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan Negate(scoped in ReadOnlyTensorSpan x, in TensorSpan destination) + where T : IUnaryNegationOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, T>(x, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IUnaryNegationOperators + { + /// Performs element-wise unary negation on a tensor. + /// The tensor to negate. + /// A new tensor containing the result of -. + public static Tensor operator -(in ReadOnlyTensorSpan tensor) => Negate(tensor); + } + + /// The type of the elements in the tensor. + extension(Tensor) + where TScalar : IUnaryNegationOperators + { + /// + public static Tensor operator -(Tensor tensor) => Negate(tensor); + } + + /// The type of the elements in the tensor. + extension(TensorSpan) + where TScalar : IUnaryNegationOperators + { + /// + public static Tensor operator -(in TensorSpan tensor) => Negate(tensor); + } + } +} From 8c00f6e1fd5f2e9929366323b40399212bf62ab1 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 13:16:45 -0700 Subject: [PATCH 11/20] Adding the extensions for operator + (unary) --- .../ref/System.Numerics.Tensors.netcore.cs | 16 +++++++++ .../src/System.Numerics.Tensors.csproj | 1 + .../Tensors/netcore/Tensor.op_UnaryPlus.cs | 34 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnaryPlus.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index 7467b8cec76609..39c05a4099f2ce 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -756,6 +756,22 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso { public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.TensorSpan tensor) { throw null; } } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IUnaryPlusOperators + { + public static System.Numerics.Tensors.ReadOnlyTensorSpan operator +(in System.Numerics.Tensors.ReadOnlyTensorSpan tensor) { throw null; } + } + extension(Tensor) + where TScalar : System.Numerics.IUnaryPlusOperators + { + public static System.Numerics.Tensors.Tensor operator +(in System.Numerics.Tensors.Tensor tensor) { throw null; } + } + + extension(TensorSpan) + where TScalar : System.Numerics.IUnaryPlusOperators + { + public static System.Numerics.Tensors.TensorSpan operator +(in System.Numerics.Tensors.TensorSpan tensor) { throw null; } + } } public readonly ref partial struct TensorDimensionSpan { diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 18ac694673dd45..0512e4fe45bb1e 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -48,6 +48,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnaryPlus.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnaryPlus.cs new file mode 100644 index 00000000000000..db11f6d750c962 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnaryPlus.cs @@ -0,0 +1,34 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IUnaryPlusOperators + { + /// Performs element-wise unary plus on a tensor. + /// The tensor to return. + /// + public static ReadOnlyTensorSpan operator +(in ReadOnlyTensorSpan tensor) => tensor; + } + + /// The type of the elements in the tensor. + extension(Tensor) + where TScalar : IUnaryPlusOperators + { + /// + public static Tensor operator +(Tensor tensor) => tensor; + } + + /// The type of the elements in the tensor. + extension(TensorSpan) + where TScalar : IUnaryPlusOperators + { + /// + public static TensorSpan operator +(in TensorSpan tensor) => tensor; + } + } +} From 6aa410867713eba046075f6595fcea1f207ed2f2 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 22 Jul 2025 13:35:19 -0700 Subject: [PATCH 12/20] Adding the extensions for operator << and operator <<= --- .../ref/System.Numerics.Tensors.netcore.cs | 27 ++++++ .../src/System.Numerics.Tensors.csproj | 1 + .../Tensors/netcore/Tensor.op_LeftShift.cs | 82 +++++++++++++++++++ .../Tensors/netcore/TensorOperation.cs | 15 ++++ 4 files changed, 125 insertions(+) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_LeftShift.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index 39c05a4099f2ce..097f1a5b4dbd0d 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -523,6 +523,8 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static bool SequenceEqual(this scoped in System.Numerics.Tensors.TensorSpan tensor, scoped in System.Numerics.Tensors.ReadOnlyTensorSpan other) where T : System.IEquatable? { throw null; } public static ref readonly System.Numerics.Tensors.TensorSpan SetSlice(this in System.Numerics.Tensors.TensorSpan tensor, scoped in System.Numerics.Tensors.ReadOnlyTensorSpan values, params scoped System.ReadOnlySpan ranges) { throw null; } public static System.Numerics.Tensors.Tensor SetSlice(this System.Numerics.Tensors.Tensor tensor, in System.Numerics.Tensors.ReadOnlyTensorSpan values, params scoped System.ReadOnlySpan ranges) { throw null; } + public static Tensor ShiftLeft(in ReadOnlyTensorSpan x, int shiftAmount) where T : IShiftOperators { throw null; } + public static ref readonly TensorSpan ShiftLeft(scoped in ReadOnlyTensorSpan x, int shiftAmount, in TensorSpan destination) where T : IShiftOperators { throw null; } public static System.Numerics.Tensors.Tensor Sigmoid(in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.IExponentialFunctions { throw null; } public static ref readonly System.Numerics.Tensors.TensorSpan Sigmoid(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x, in System.Numerics.Tensors.TensorSpan destination) where T : System.Numerics.IExponentialFunctions { throw null; } public static System.Numerics.Tensors.Tensor Sinh(in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.IHyperbolicFunctions { throw null; } @@ -715,6 +717,31 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator *=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator *=(TScalar other) { throw null; } } + extension(ReadOnlyTensorSpan) + where TScalar : IShiftOperators + { + public static Tensor operator <<(in ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } + } + extension(Tensor) + where TScalar : IShiftOperators + { + public static Tensor operator <<(Tensor tensor, int shiftAmount) { throw null; } + } + extension(Tensor tensor) + where TScalar : IShiftOperators + { + public void operator <<=(int shiftAmount) { throw null; } + } + extension(TensorSpan) + where TScalar : IShiftOperators + { + public static Tensor operator <<(in TensorSpan tensor, int shiftAmount) { throw null; } + } + extension(ref TensorSpan tensor) + where TScalar : IShiftOperators + { + public void operator <<=(int shiftAmount) { throw null; } + } extension(System.Numerics.Tensors.ReadOnlyTensorSpan) where TScalar : System.Numerics.ISubtractionOperators { diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 0512e4fe45bb1e..d3f9f336a68017 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -44,6 +44,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_LeftShift.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_LeftShift.cs new file mode 100644 index 00000000000000..68f1e304e7c2f8 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_LeftShift.cs @@ -0,0 +1,82 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs an element-wise left shift on a tensor. + /// The type of the elements in the tensor. + /// The tensor to left shift. + /// The amount to shift each element in . + /// A new tensor containing the result of << . + public static Tensor ShiftLeft(in ReadOnlyTensorSpan x, int shiftAmount) + where T : IShiftOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, int, T>(x, shiftAmount, destination); + return destination; + } + + /// Performs an element-wise left shift on a tensor. + /// The type of the elements in the tensor. + /// The tensor to left shift. + /// The amount to shift each element in . + /// The destination where the result of << is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan ShiftLeft(scoped in ReadOnlyTensorSpan x, int shiftAmount, in TensorSpan destination) + where T : IShiftOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, int, T>(x, shiftAmount, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IShiftOperators + { + /// Performs an element-wise left shift on a tensor. + /// The tensor to left shift. + /// The amount to shift each element in . + /// A new tensor containing the result of << . + public static Tensor operator <<(in ReadOnlyTensorSpan tensor, int shiftAmount) => ShiftLeft(tensor, shiftAmount); + } + + /// The type of the elements in the tensor. + extension(Tensor) + where TScalar : IShiftOperators + { + /// + public static Tensor operator <<(Tensor tensor, int shiftAmount) => ShiftLeft(tensor, shiftAmount); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(Tensor tensor) + where TScalar : IShiftOperators + { + /// + public void operator <<=(int shiftAmount) => ShiftLeft(tensor, shiftAmount, tensor); + } + + /// The type of the elements in the tensor. + extension(TensorSpan) + where TScalar : IShiftOperators + { + /// + public static Tensor operator <<(in TensorSpan tensor, int shiftAmount) => ShiftLeft(tensor, shiftAmount); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : IShiftOperators + { + /// Performs in-place element-wise left shift on a tensor. + /// The amount to shift each element in the tensor. + public void operator <<=(int shiftAmount) => ShiftLeft(tensor, shiftAmount, tensor); + } + } +} diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs index 42092b950a156f..01514214009f5a 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs @@ -1831,6 +1831,21 @@ public static void Invoke(ReadOnlySpan x, Tuple y, Spa } } + public readonly struct ShiftLeft + : IBinaryOperation_Tensor_Scalar + where T : IShiftOperators + { + public static void Invoke(ref readonly T x, int shiftAmount, ref T destination) + { + destination = x << shiftAmount; + } + + public static void Invoke(ReadOnlySpan x, int shiftAmount, Span destination) + { + TensorPrimitives.ShiftLeft(x, shiftAmount, destination); + } + } + public readonly struct Sigmoid : IUnaryOperation_Tensor where T : IExponentialFunctions From bebb06fb31a6bcee396fde59c7d509f081570d86 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 23 Jul 2025 10:21:43 -0700 Subject: [PATCH 13/20] Adding the extensions for operator >> and operator >>= --- .../ref/System.Numerics.Tensors.netcore.cs | 7 ++ .../src/System.Numerics.Tensors.csproj | 2 + .../Tensors/netcore/Tensor.op_RightShift.cs | 82 +++++++++++++++++++ .../Tensors/netcore/TensorOperation.cs | 15 ++++ .../netcore/TensorPrimitives.ShiftLeft.cs | 28 ------- .../TensorPrimitives.ShiftRightArithmetic.cs | 38 +++++++++ 6 files changed, 144 insertions(+), 28 deletions(-) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_RightShift.cs create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftRightArithmetic.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index 097f1a5b4dbd0d..f05246328e292d 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -525,6 +525,8 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor SetSlice(this System.Numerics.Tensors.Tensor tensor, in System.Numerics.Tensors.ReadOnlyTensorSpan values, params scoped System.ReadOnlySpan ranges) { throw null; } public static Tensor ShiftLeft(in ReadOnlyTensorSpan x, int shiftAmount) where T : IShiftOperators { throw null; } public static ref readonly TensorSpan ShiftLeft(scoped in ReadOnlyTensorSpan x, int shiftAmount, in TensorSpan destination) where T : IShiftOperators { throw null; } + public static Tensor ShiftRightArithmetic(in ReadOnlyTensorSpan x, int shiftAmount) where T : IShiftOperators { throw null; } + public static ref readonly TensorSpan ShiftRightArithmetic(scoped in ReadOnlyTensorSpan x, int shiftAmount, in TensorSpan destination) where T : IShiftOperators { throw null; } public static System.Numerics.Tensors.Tensor Sigmoid(in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.IExponentialFunctions { throw null; } public static ref readonly System.Numerics.Tensors.TensorSpan Sigmoid(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x, in System.Numerics.Tensors.TensorSpan destination) where T : System.Numerics.IExponentialFunctions { throw null; } public static System.Numerics.Tensors.Tensor Sinh(in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.IHyperbolicFunctions { throw null; } @@ -721,26 +723,31 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso where TScalar : IShiftOperators { public static Tensor operator <<(in ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } + public static Tensor operator >>(in ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } } extension(Tensor) where TScalar : IShiftOperators { public static Tensor operator <<(Tensor tensor, int shiftAmount) { throw null; } + public static Tensor operator >>(Tensor tensor, int shiftAmount) { throw null; } } extension(Tensor tensor) where TScalar : IShiftOperators { public void operator <<=(int shiftAmount) { throw null; } + public void operator >>=(int shiftAmount) { throw null; } } extension(TensorSpan) where TScalar : IShiftOperators { public static Tensor operator <<(in TensorSpan tensor, int shiftAmount) { throw null; } + public static Tensor operator >>(in TensorSpan tensor, int shiftAmount) { throw null; } } extension(ref TensorSpan tensor) where TScalar : IShiftOperators { public void operator <<=(int shiftAmount) { throw null; } + public void operator >>=(int shiftAmount) { throw null; } } extension(System.Numerics.Tensors.ReadOnlyTensorSpan) where TScalar : System.Numerics.ISubtractionOperators diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index d3f9f336a68017..1e91285f6e30a3 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -47,6 +47,7 @@ + @@ -160,6 +161,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_RightShift.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_RightShift.cs new file mode 100644 index 00000000000000..d61559441b96ec --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_RightShift.cs @@ -0,0 +1,82 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs an element-wise arithmetic right shift on a tensor. + /// The type of the elements in the tensor. + /// The tensor to arithmetic right shift. + /// The amount to shift each element in . + /// A new tensor containing the result of >> . + public static Tensor ShiftRightArithmetic(in ReadOnlyTensorSpan x, int shiftAmount) + where T : IShiftOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, int, T>(x, shiftAmount, destination); + return destination; + } + + /// Performs an element-wise arithmetic right shift on a tensor. + /// The type of the elements in the tensor. + /// The tensor to arithmetic right shift. + /// The amount to shift each element in . + /// The destination where the result of >> is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan ShiftRightArithmetic(scoped in ReadOnlyTensorSpan x, int shiftAmount, in TensorSpan destination) + where T : IShiftOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, int, T>(x, shiftAmount, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IShiftOperators + { + /// Performs an element-wise arithmetic right shift on a tensor. + /// The tensor to arithmetic right shift. + /// The amount to shift each element in . + /// A new tensor containing the result of >> . + public static Tensor operator >>(in ReadOnlyTensorSpan tensor, int shiftAmount) => ShiftRightArithmetic(tensor, shiftAmount); + } + + /// The type of the elements in the tensor. + extension(Tensor) + where TScalar : IShiftOperators + { + /// + public static Tensor operator >>(Tensor tensor, int shiftAmount) => ShiftRightArithmetic(tensor, shiftAmount); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(Tensor tensor) + where TScalar : IShiftOperators + { + /// + public void operator >>=(int shiftAmount) => ShiftRightArithmetic(tensor, shiftAmount, tensor); + } + + /// The type of the elements in the tensor. + extension(TensorSpan) + where TScalar : IShiftOperators + { + /// + public static Tensor operator >>(in TensorSpan tensor, int shiftAmount) => ShiftRightArithmetic(tensor, shiftAmount); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : IShiftOperators + { + /// Performs in-place element-wise arithmetic right shift on a tensor. + /// The amount to shift each element in the tensor. + public void operator >>=(int shiftAmount) => ShiftRightArithmetic(tensor, shiftAmount, tensor); + } + } +} diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs index 01514214009f5a..0a6f6dbd2df8a3 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs @@ -1846,6 +1846,21 @@ public static void Invoke(ReadOnlySpan x, int shiftAmount, Span destinatio } } + public readonly struct ShiftRightArithmetic + : IBinaryOperation_Tensor_Scalar + where T : IShiftOperators + { + public static void Invoke(ref readonly T x, int shiftAmount, ref T destination) + { + destination = x >> shiftAmount; + } + + public static void Invoke(ReadOnlySpan x, int shiftAmount, Span destination) + { + TensorPrimitives.ShiftRightArithmetic(x, shiftAmount, destination); + } + } + public readonly struct Sigmoid : IUnaryOperation_Tensor where T : IExponentialFunctions diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftLeft.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftLeft.cs index 22d5ea4c268679..c79c8b05102102 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftLeft.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftLeft.cs @@ -22,21 +22,6 @@ public static void ShiftLeft(ReadOnlySpan x, int shiftAmount, Span dest where T : IShiftOperators => InvokeSpanIntoSpan(x, new ShiftLeftOperator(shiftAmount), destination); - /// Computes the element-wise arithmetic (signed) shifting right of numbers in the specified tensor by the specified shift amount. - /// The tensor, represented as a span. - /// The destination tensor, represented as a span. - /// The number of bits to shift, represented as a scalar. - /// Destination is too short. - /// and reference overlapping memory locations and do not begin at the same location. - /// - /// - /// This method effectively computes [i] = [i] >> . - /// - /// - public static void ShiftRightArithmetic(ReadOnlySpan x, int shiftAmount, Span destination) - where T : IShiftOperators => - InvokeSpanIntoSpan(x, new ShiftRightArithmeticOperator(shiftAmount), destination); - /// Computes the element-wise logical (unsigned) shifting right of numbers in the specified tensor by the specified shift amount. /// The tensor, represented as a span. /// The destination tensor, represented as a span. @@ -65,19 +50,6 @@ private readonly struct ShiftLeftOperator(int amount) : IStatefulUnaryOperato public Vector512 Invoke(Vector512 x) => x << _amount; } - /// T >> amount - private readonly struct ShiftRightArithmeticOperator(int amount) : IStatefulUnaryOperator where T : IShiftOperators - { - private readonly int _amount = amount; - - public static bool Vectorizable => true; - - public T Invoke(T x) => x >> _amount; - public Vector128 Invoke(Vector128 x) => x >> _amount; - public Vector256 Invoke(Vector256 x) => x >> _amount; - public Vector512 Invoke(Vector512 x) => x >> _amount; - } - /// T >>> amount private readonly struct ShiftRightLogicalOperator(int amount) : IStatefulUnaryOperator where T : IShiftOperators { diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftRightArithmetic.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftRightArithmetic.cs new file mode 100644 index 00000000000000..26c5bbe388e6cd --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftRightArithmetic.cs @@ -0,0 +1,38 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.Intrinsics; + +namespace System.Numerics.Tensors +{ + public static partial class TensorPrimitives + { + /// Computes the element-wise arithmetic (signed) shifting right of numbers in the specified tensor by the specified shift amount. + /// The tensor, represented as a span. + /// The destination tensor, represented as a span. + /// The number of bits to shift, represented as a scalar. + /// Destination is too short. + /// and reference overlapping memory locations and do not begin at the same location. + /// + /// + /// This method effectively computes [i] = [i] >> . + /// + /// + public static void ShiftRightArithmetic(ReadOnlySpan x, int shiftAmount, Span destination) + where T : IShiftOperators => + InvokeSpanIntoSpan(x, new ShiftRightArithmeticOperator(shiftAmount), destination); + + /// T >> amount + private readonly struct ShiftRightArithmeticOperator(int amount) : IStatefulUnaryOperator where T : IShiftOperators + { + private readonly int _amount = amount; + + public static bool Vectorizable => true; + + public T Invoke(T x) => x >> _amount; + public Vector128 Invoke(Vector128 x) => x >> _amount; + public Vector256 Invoke(Vector256 x) => x >> _amount; + public Vector512 Invoke(Vector512 x) => x >> _amount; + } + } +} From 21ca60d338b5bbd0b2ed35c15aac14001a966bb0 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 23 Jul 2025 10:30:32 -0700 Subject: [PATCH 14/20] Adding the extensions for operator >>> and operator >>>= --- .../ref/System.Numerics.Tensors.netcore.cs | 7 ++ .../src/System.Numerics.Tensors.csproj | 2 + .../netcore/Tensor.op_UnsignedRightShift.cs | 82 +++++++++++++++++++ .../Tensors/netcore/TensorOperation.cs | 15 ++++ .../netcore/TensorPrimitives.ShiftLeft.cs | 28 ------- .../TensorPrimitives.ShiftRightLogical.cs | 38 +++++++++ 6 files changed, 144 insertions(+), 28 deletions(-) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnsignedRightShift.cs create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftRightLogical.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index f05246328e292d..0f524c5821c117 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -527,6 +527,8 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static ref readonly TensorSpan ShiftLeft(scoped in ReadOnlyTensorSpan x, int shiftAmount, in TensorSpan destination) where T : IShiftOperators { throw null; } public static Tensor ShiftRightArithmetic(in ReadOnlyTensorSpan x, int shiftAmount) where T : IShiftOperators { throw null; } public static ref readonly TensorSpan ShiftRightArithmetic(scoped in ReadOnlyTensorSpan x, int shiftAmount, in TensorSpan destination) where T : IShiftOperators { throw null; } + public static Tensor ShiftRightLogical(in ReadOnlyTensorSpan x, int shiftAmount) where T : IShiftOperators { throw null; } + public static ref readonly TensorSpan ShiftRightLogical(scoped in ReadOnlyTensorSpan x, int shiftAmount, in TensorSpan destination) where T : IShiftOperators { throw null; } public static System.Numerics.Tensors.Tensor Sigmoid(in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.IExponentialFunctions { throw null; } public static ref readonly System.Numerics.Tensors.TensorSpan Sigmoid(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x, in System.Numerics.Tensors.TensorSpan destination) where T : System.Numerics.IExponentialFunctions { throw null; } public static System.Numerics.Tensors.Tensor Sinh(in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.IHyperbolicFunctions { throw null; } @@ -724,30 +726,35 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso { public static Tensor operator <<(in ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } public static Tensor operator >>(in ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } + public static Tensor operator >>>(in ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } } extension(Tensor) where TScalar : IShiftOperators { public static Tensor operator <<(Tensor tensor, int shiftAmount) { throw null; } public static Tensor operator >>(Tensor tensor, int shiftAmount) { throw null; } + public static Tensor operator >>>(Tensor tensor, int shiftAmount) { throw null; } } extension(Tensor tensor) where TScalar : IShiftOperators { public void operator <<=(int shiftAmount) { throw null; } public void operator >>=(int shiftAmount) { throw null; } + public void operator >>>=(int shiftAmount) { throw null; } } extension(TensorSpan) where TScalar : IShiftOperators { public static Tensor operator <<(in TensorSpan tensor, int shiftAmount) { throw null; } public static Tensor operator >>(in TensorSpan tensor, int shiftAmount) { throw null; } + public static Tensor operator >>>(in TensorSpan tensor, int shiftAmount) { throw null; } } extension(ref TensorSpan tensor) where TScalar : IShiftOperators { public void operator <<=(int shiftAmount) { throw null; } public void operator >>=(int shiftAmount) { throw null; } + public void operator >>>=(int shiftAmount) { throw null; } } extension(System.Numerics.Tensors.ReadOnlyTensorSpan) where TScalar : System.Numerics.ISubtractionOperators diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 1e91285f6e30a3..1346b4db90231e 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -51,6 +51,7 @@ + @@ -162,6 +163,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnsignedRightShift.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnsignedRightShift.cs new file mode 100644 index 00000000000000..19147047592fe0 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_UnsignedRightShift.cs @@ -0,0 +1,82 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs an element-wise logical right shift on a tensor. + /// The type of the elements in the tensor. + /// The tensor to logical right shift. + /// The amount to shift each element in . + /// A new tensor containing the result of >>> . + public static Tensor ShiftRightLogical(in ReadOnlyTensorSpan x, int shiftAmount) + where T : IShiftOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, int, T>(x, shiftAmount, destination); + return destination; + } + + /// Performs an element-wise logical right shift on a tensor. + /// The type of the elements in the tensor. + /// The tensor to logical right shift. + /// The amount to shift each element in . + /// The destination where the result of >>> is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan ShiftRightLogical(scoped in ReadOnlyTensorSpan x, int shiftAmount, in TensorSpan destination) + where T : IShiftOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, int, T>(x, shiftAmount, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + extension(ReadOnlyTensorSpan) + where TScalar : IShiftOperators + { + /// Performs an element-wise logical right shift on a tensor. + /// The tensor to logical right shift. + /// The amount to shift each element in . + /// A new tensor containing the result of >>> . + public static Tensor operator >>>(in ReadOnlyTensorSpan tensor, int shiftAmount) => ShiftRightLogical(tensor, shiftAmount); + } + + /// The type of the elements in the tensor. + extension(Tensor) + where TScalar : IShiftOperators + { + /// + public static Tensor operator >>>(Tensor tensor, int shiftAmount) => ShiftRightLogical(tensor, shiftAmount); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(Tensor tensor) + where TScalar : IShiftOperators + { + /// + public void operator >>>=(int shiftAmount) => ShiftRightLogical(tensor, shiftAmount, tensor); + } + + /// The type of the elements in the tensor. + extension(TensorSpan) + where TScalar : IShiftOperators + { + /// + public static Tensor operator >>>(in TensorSpan tensor, int shiftAmount) => ShiftRightLogical(tensor, shiftAmount); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : IShiftOperators + { + /// Performs in-place element-wise logical right shift on a tensor. + /// The amount to shift each element in the tensor. + public void operator >>>=(int shiftAmount) => ShiftRightLogical(tensor, shiftAmount, tensor); + } + } +} diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs index 0a6f6dbd2df8a3..5df7d0a95d4af9 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs @@ -1861,6 +1861,21 @@ public static void Invoke(ReadOnlySpan x, int shiftAmount, Span destinatio } } + public readonly struct ShiftRightLogical + : IBinaryOperation_Tensor_Scalar + where T : IShiftOperators + { + public static void Invoke(ref readonly T x, int shiftAmount, ref T destination) + { + destination = x >>> shiftAmount; + } + + public static void Invoke(ReadOnlySpan x, int shiftAmount, Span destination) + { + TensorPrimitives.ShiftRightLogical(x, shiftAmount, destination); + } + } + public readonly struct Sigmoid : IUnaryOperation_Tensor where T : IExponentialFunctions diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftLeft.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftLeft.cs index c79c8b05102102..63b3ee4d101ee4 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftLeft.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftLeft.cs @@ -22,21 +22,6 @@ public static void ShiftLeft(ReadOnlySpan x, int shiftAmount, Span dest where T : IShiftOperators => InvokeSpanIntoSpan(x, new ShiftLeftOperator(shiftAmount), destination); - /// Computes the element-wise logical (unsigned) shifting right of numbers in the specified tensor by the specified shift amount. - /// The tensor, represented as a span. - /// The destination tensor, represented as a span. - /// The number of bits to shift, represented as a scalar. - /// Destination is too short. - /// and reference overlapping memory locations and do not begin at the same location. - /// - /// - /// This method effectively computes [i] = [i] >>> . - /// - /// - public static void ShiftRightLogical(ReadOnlySpan x, int shiftAmount, Span destination) - where T : IShiftOperators => - InvokeSpanIntoSpan(x, new ShiftRightLogicalOperator(shiftAmount), destination); - /// T << amount private readonly struct ShiftLeftOperator(int amount) : IStatefulUnaryOperator where T : IShiftOperators { @@ -49,18 +34,5 @@ private readonly struct ShiftLeftOperator(int amount) : IStatefulUnaryOperato public Vector256 Invoke(Vector256 x) => x << _amount; public Vector512 Invoke(Vector512 x) => x << _amount; } - - /// T >>> amount - private readonly struct ShiftRightLogicalOperator(int amount) : IStatefulUnaryOperator where T : IShiftOperators - { - private readonly int _amount = amount; - - public static bool Vectorizable => true; - - public T Invoke(T x) => x >>> _amount; - public Vector128 Invoke(Vector128 x) => x >>> _amount; - public Vector256 Invoke(Vector256 x) => x >>> _amount; - public Vector512 Invoke(Vector512 x) => x >>> _amount; - } } } diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftRightLogical.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftRightLogical.cs new file mode 100644 index 00000000000000..0b720c8e9fbf92 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.ShiftRightLogical.cs @@ -0,0 +1,38 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.Intrinsics; + +namespace System.Numerics.Tensors +{ + public static partial class TensorPrimitives + { + /// Computes the element-wise logical (unsigned) shifting right of numbers in the specified tensor by the specified shift amount. + /// The tensor, represented as a span. + /// The destination tensor, represented as a span. + /// The number of bits to shift, represented as a scalar. + /// Destination is too short. + /// and reference overlapping memory locations and do not begin at the same location. + /// + /// + /// This method effectively computes [i] = [i] >>> . + /// + /// + public static void ShiftRightLogical(ReadOnlySpan x, int shiftAmount, Span destination) + where T : IShiftOperators => + InvokeSpanIntoSpan(x, new ShiftRightLogicalOperator(shiftAmount), destination); + + /// T >>> amount + private readonly struct ShiftRightLogicalOperator(int amount) : IStatefulUnaryOperator where T : IShiftOperators + { + private readonly int _amount = amount; + + public static bool Vectorizable => true; + + public T Invoke(T x) => x >>> _amount; + public Vector128 Invoke(Vector128 x) => x >>> _amount; + public Vector256 Invoke(Vector256 x) => x >>> _amount; + public Vector512 Invoke(Vector512 x) => x >>> _amount; + } + } +} From c38c44b14ad5a18718b115c70e65c868576ce126 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 23 Jul 2025 10:57:23 -0700 Subject: [PATCH 15/20] Adding the extensions for operator ++ --- .../ref/System.Numerics.Tensors.netcore.cs | 10 ++++ .../src/System.Numerics.Tensors.csproj | 1 + .../Tensors/netcore/Tensor.op_Increment.cs | 52 +++++++++++++++++++ .../Tensors/netcore/TensorOperation.cs | 16 ++++++ 4 files changed, 79 insertions(+) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Increment.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index 0f524c5821c117..3a08b33c6f355e 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -385,6 +385,8 @@ public static void BroadcastTo(this System.Numerics.Tensors.Tensor source, public static ref readonly System.Numerics.Tensors.TensorSpan Ieee754Remainder(T x, scoped in System.Numerics.Tensors.ReadOnlyTensorSpan y, in System.Numerics.Tensors.TensorSpan destination) where T : System.Numerics.IFloatingPointIeee754 { throw null; } public static System.Numerics.Tensors.Tensor ILogB(in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.IFloatingPointIeee754 { throw null; } public static ref readonly System.Numerics.Tensors.TensorSpan ILogB(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x, in System.Numerics.Tensors.TensorSpan destination) where T : System.Numerics.IFloatingPointIeee754 { throw null; } + public static System.Numerics.Tensors.Tensor Increment(in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.IIncrementOperators { throw null; } + public static ref readonly System.Numerics.Tensors.TensorSpan Increment(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x, in System.Numerics.Tensors.TensorSpan destination) where T : System.Numerics.IIncrementOperators { throw null; } public static nint IndexOfMaxMagnitude(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.INumber { throw null; } public static nint IndexOfMax(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.INumber { throw null; } public static nint IndexOfMinMagnitude(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.INumber { throw null; } @@ -696,6 +698,14 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator /=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator /=(TScalar other) { throw null; } } + extension(System.Numerics.Tensors.Tensor tensor) where TScalar : System.Numerics.IIncrementOperators + { + public void operator ++() { throw null; } + } + extension(ref System.Numerics.Tensors.TensorSpan tensor) where TScalar : System.Numerics.IIncrementOperators + { + public void operator ++() { throw null; } + } extension(System.Numerics.Tensors.ReadOnlyTensorSpan) where TScalar : System.Numerics.IMultiplyOperators, System.Numerics.IMultiplicativeIdentity { diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 1346b4db90231e..8d1c694659efa4 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -44,6 +44,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Increment.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Increment.cs new file mode 100644 index 00000000000000..6fb730c7d5b002 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Increment.cs @@ -0,0 +1,52 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs an increment on a tensor. + /// The type of the elements in the tensor. + /// The tensor to increment. + /// A new tensor containing the result of ~. + public static Tensor Increment(in ReadOnlyTensorSpan x) + where T : IIncrementOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, destination); + return destination; + } + + /// Performs an increment on a tensor. + /// The type of the elements in the tensor. + /// The tensor to increment. + /// The destination where the result of ~ is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan Increment(scoped in ReadOnlyTensorSpan x, in TensorSpan destination) + where T : IIncrementOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, T>(x, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(Tensor tensor) + where TScalar : IIncrementOperators + { + /// + public void operator ++() => Increment(tensor, tensor); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : IIncrementOperators + { + /// Performs in-place increment on a tensor. + public void operator ++() => Increment(tensor, tensor); + } + } +} diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs index 5df7d0a95d4af9..edf8100a35af42 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs @@ -1186,6 +1186,22 @@ public static void Invoke(ReadOnlySpan x, Span destination) } } + public readonly struct Increment + : IUnaryOperation_Tensor + where T : IIncrementOperators + { + public static void Invoke(ref readonly T x, ref T destination) + { + T tmp = x; + destination = ++tmp; + } + + public static void Invoke(ReadOnlySpan x, Span destination) + { + TensorPrimitives.Increment(x, destination); + } + } + public readonly struct LeadingZeroCount : IUnaryOperation_Tensor where T : IBinaryInteger From 6ccf1768076319c6c4879cc251aad31cf98e0a79 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 23 Jul 2025 11:07:39 -0700 Subject: [PATCH 16/20] Adding the extensions for operator -- --- .../ref/System.Numerics.Tensors.netcore.cs | 10 ++++ .../src/System.Numerics.Tensors.csproj | 1 + .../Tensors/netcore/Tensor.op_Decrement.cs | 52 +++++++++++++++++++ .../Tensors/netcore/TensorOperation.cs | 16 ++++++ 4 files changed, 79 insertions(+) create mode 100644 src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Decrement.cs diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index 3a08b33c6f355e..54ebf754eedb19 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -315,6 +315,8 @@ public static void BroadcastTo(this System.Numerics.Tensors.Tensor source, public static System.Numerics.Tensors.Tensor CreateFromShape(scoped System.ReadOnlySpan lengths, scoped System.ReadOnlySpan strides, bool pinned = false) { throw null; } public static System.Numerics.Tensors.Tensor CreateFromShapeUninitialized(scoped System.ReadOnlySpan lengths, bool pinned = false) { throw null; } public static System.Numerics.Tensors.Tensor CreateFromShapeUninitialized(scoped System.ReadOnlySpan lengths, scoped System.ReadOnlySpan strides, bool pinned = false) { throw null; } + public static System.Numerics.Tensors.Tensor Decrement(in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.IDecrementOperators { throw null; } + public static ref readonly System.Numerics.Tensors.TensorSpan Decrement(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x, in System.Numerics.Tensors.TensorSpan destination) where T : System.Numerics.IDecrementOperators { throw null; } public static System.Numerics.Tensors.Tensor DegreesToRadians(in System.Numerics.Tensors.ReadOnlyTensorSpan x) where T : System.Numerics.ITrigonometricFunctions { throw null; } public static ref readonly System.Numerics.Tensors.TensorSpan DegreesToRadians(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x, in System.Numerics.Tensors.TensorSpan destination) where T : System.Numerics.ITrigonometricFunctions { throw null; } public static T Distance(scoped in System.Numerics.Tensors.ReadOnlyTensorSpan x, scoped in System.Numerics.Tensors.ReadOnlyTensorSpan y) where T : System.Numerics.IRootFunctions { throw null; } @@ -673,6 +675,14 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator ^=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator ^=(TScalar other) { throw null; } } + extension(System.Numerics.Tensors.Tensor tensor) where TScalar : System.Numerics.IDecrementOperators + { + public void operator --() { throw null; } + } + extension(ref System.Numerics.Tensors.TensorSpan tensor) where TScalar : System.Numerics.IDecrementOperators + { + public void operator --() { throw null; } + } extension(System.Numerics.Tensors.ReadOnlyTensorSpan) where TScalar : System.Numerics.IDivisionOperators { diff --git a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj index 8d1c694659efa4..24fcfcbaf6225b 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -42,6 +42,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Decrement.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Decrement.cs new file mode 100644 index 00000000000000..e1353be9719371 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Decrement.cs @@ -0,0 +1,52 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Numerics.Tensors +{ + public static partial class Tensor + { + /// Performs an decrement on a tensor. + /// The type of the elements in the tensor. + /// The tensor to decrement. + /// A new tensor containing the result of ~. + public static Tensor Decrement(in ReadOnlyTensorSpan x) + where T : IDecrementOperators + { + Tensor destination = CreateFromShapeUninitialized(x.Lengths); + TensorOperation.Invoke, T, T>(x, destination); + return destination; + } + + /// Performs an decrement on a tensor. + /// The type of the elements in the tensor. + /// The tensor to decrement. + /// The destination where the result of ~ is written. + /// A reference to . + /// The shapes of and are not compatible. + public static ref readonly TensorSpan Decrement(scoped in ReadOnlyTensorSpan x, in TensorSpan destination) + where T : IDecrementOperators + { + TensorOperation.ValidateCompatibility(x, destination); + TensorOperation.Invoke, T, T>(x, destination); + return ref destination; + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(Tensor tensor) + where TScalar : IDecrementOperators + { + /// + public void operator --() => Decrement(tensor, tensor); + } + + /// The type of the elements in the tensor. + /// The tensor to operate on. + extension(ref TensorSpan tensor) + where TScalar : IDecrementOperators + { + /// Performs in-place decrement on a tensor. + public void operator --() => Decrement(tensor, tensor); + } + } +} diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs index edf8100a35af42..227a8d95db58ed 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorOperation.cs @@ -949,6 +949,22 @@ public static void Invoke(ReadOnlySpan x, Span destination) } } + public readonly struct Decrement + : IUnaryOperation_Tensor + where T : IDecrementOperators + { + public static void Invoke(ref readonly T x, ref T destination) + { + T tmp = x; + destination = --tmp; + } + + public static void Invoke(ReadOnlySpan x, Span destination) + { + TensorPrimitives.Decrement(x, destination); + } + } + public readonly struct DegreesToRadians : IUnaryOperation_Tensor where T : ITrigonometricFunctions From f80fc86423ed21da8706f2fc7075f79f7c150a8d Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 23 Jul 2025 21:48:16 -0700 Subject: [PATCH 17/20] Ensure the extension member order in the ref matches the impl --- .../ref/System.Numerics.Tensors.netcore.cs | 211 ++++++++++++------ 1 file changed, 139 insertions(+), 72 deletions(-) diff --git a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs index 54ebf754eedb19..ebe7a37a7de8ed 100644 --- a/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/ref/System.Numerics.Tensors.netcore.cs @@ -595,7 +595,7 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator +(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } public static System.Numerics.Tensors.Tensor operator +(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } } - extension(Tensor tensor) + extension(System.Numerics.Tensors.Tensor tensor) where TScalar : System.Numerics.IAdditionOperators, System.Numerics.IAdditiveIdentity { public static System.Numerics.Tensors.Tensor operator +(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } @@ -604,7 +604,7 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator +=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator +=(TScalar other) { throw null; } } - extension(ref TensorSpan tensor) + extension(ref System.Numerics.Tensors.TensorSpan tensor) where TScalar : System.Numerics.IAdditionOperators, System.Numerics.IAdditiveIdentity { public static System.Numerics.Tensors.Tensor operator +(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } @@ -619,20 +619,8 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator &(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } public static System.Numerics.Tensors.Tensor operator &(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } public static System.Numerics.Tensors.Tensor operator &(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } - public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } - public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } - public static System.Numerics.Tensors.Tensor operator |(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } - public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } - public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } - public static System.Numerics.Tensors.Tensor operator ^(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } - public static System.Numerics.Tensors.Tensor operator ~(in System.Numerics.Tensors.ReadOnlyTensorSpan tensor) { throw null; } - } - extension(Tensor) - where TScalar : System.Numerics.IBitwiseOperators - { - public static System.Numerics.Tensors.Tensor operator ~(in System.Numerics.Tensors.Tensor tensor) { throw null; } } - extension(Tensor tensor) + extension(System.Numerics.Tensors.Tensor tensor) where TScalar : System.Numerics.IBitwiseOperators { public static System.Numerics.Tensors.Tensor operator &(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } @@ -640,23 +628,8 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator &(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } public void operator &=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator &=(TScalar other) { throw null; } - public static System.Numerics.Tensors.Tensor operator |(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } - public static System.Numerics.Tensors.Tensor operator |(System.Numerics.Tensors.Tensor left, TScalar right) { throw null; } - public static System.Numerics.Tensors.Tensor operator |(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } - public void operator |=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } - public void operator |=(TScalar other) { throw null; } - public static System.Numerics.Tensors.Tensor operator ^(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } - public static System.Numerics.Tensors.Tensor operator ^(System.Numerics.Tensors.Tensor left, TScalar right) { throw null; } - public static System.Numerics.Tensors.Tensor operator ^(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } - public void operator ^=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } - public void operator ^=(TScalar other) { throw null; } } - extension(TensorSpan) - where TScalar : System.Numerics.IBitwiseOperators - { - public static System.Numerics.Tensors.Tensor operator ~(in System.Numerics.Tensors.TensorSpan tensor) { throw null; } - } - extension(ref TensorSpan tensor) + extension(ref System.Numerics.Tensors.TensorSpan tensor) where TScalar : System.Numerics.IBitwiseOperators { public static System.Numerics.Tensors.Tensor operator &(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } @@ -664,16 +637,31 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator &(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } public void operator &=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator &=(TScalar other) { throw null; } + } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IBitwiseOperators + { + public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + } + extension(System.Numerics.Tensors.Tensor tensor) + where TScalar : System.Numerics.IBitwiseOperators + { + public static System.Numerics.Tensors.Tensor operator |(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(System.Numerics.Tensors.Tensor left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator |(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } + public void operator |=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator |=(TScalar other) { throw null; } + } + extension(ref System.Numerics.Tensors.TensorSpan tensor) + where TScalar : System.Numerics.IBitwiseOperators + { public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } public static System.Numerics.Tensors.Tensor operator |(in System.Numerics.Tensors.TensorSpan left, TScalar right) { throw null; } public static System.Numerics.Tensors.Tensor operator |(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } public void operator |=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator |=(TScalar other) { throw null; } - public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } - public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.TensorSpan left, TScalar right) { throw null; } - public static System.Numerics.Tensors.Tensor operator ^(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } - public void operator ^=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } - public void operator ^=(TScalar other) { throw null; } } extension(System.Numerics.Tensors.Tensor tensor) where TScalar : System.Numerics.IDecrementOperators { @@ -690,7 +678,7 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator /(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } public static System.Numerics.Tensors.Tensor operator /(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } } - extension(Tensor tensor) + extension(System.Numerics.Tensors.Tensor tensor) where TScalar : System.Numerics.IDivisionOperators { public static System.Numerics.Tensors.Tensor operator /(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } @@ -699,7 +687,7 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator /=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator /=(TScalar other) { throw null; } } - extension(ref TensorSpan tensor) + extension(ref System.Numerics.Tensors.TensorSpan tensor) where TScalar : System.Numerics.IDivisionOperators { public static System.Numerics.Tensors.Tensor operator /(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } @@ -708,13 +696,64 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator /=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator /=(TScalar other) { throw null; } } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IBitwiseOperators + { + public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.ReadOnlyTensorSpan left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + } + extension(System.Numerics.Tensors.Tensor tensor) + where TScalar : System.Numerics.IBitwiseOperators + { + public static System.Numerics.Tensors.Tensor operator ^(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(System.Numerics.Tensors.Tensor left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(TScalar left, System.Numerics.Tensors.Tensor right) { throw null; } + public void operator ^=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator ^=(TScalar other) { throw null; } + } + + extension(ref System.Numerics.Tensors.TensorSpan tensor) + where TScalar : System.Numerics.IBitwiseOperators + { + public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(in System.Numerics.Tensors.TensorSpan left, TScalar right) { throw null; } + public static System.Numerics.Tensors.Tensor operator ^(TScalar left, in System.Numerics.Tensors.TensorSpan right) { throw null; } + public void operator ^=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } + public void operator ^=(TScalar other) { throw null; } + } extension(System.Numerics.Tensors.Tensor tensor) where TScalar : System.Numerics.IIncrementOperators { public void operator ++() { throw null; } } extension(ref System.Numerics.Tensors.TensorSpan tensor) where TScalar : System.Numerics.IIncrementOperators { - public void operator ++() { throw null; } + public void operator ++() { throw null; } + } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IShiftOperators + { + public static System.Numerics.Tensors.Tensor operator <<(in System.Numerics.Tensors.ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } + } + extension(System.Numerics.Tensors.Tensor) + where TScalar : System.Numerics.IShiftOperators + { + public static System.Numerics.Tensors.Tensor operator <<(System.Numerics.Tensors.Tensor tensor, int shiftAmount) { throw null; } + } + extension(System.Numerics.Tensors.Tensor tensor) + where TScalar : System.Numerics.IShiftOperators + { + public void operator <<=(int shiftAmount) { throw null; } + } + extension(System.Numerics.Tensors.TensorSpan) + where TScalar : System.Numerics.IShiftOperators + { + public static Tensor operator <<(in TensorSpan tensor, int shiftAmount) { throw null; } + } + extension(ref System.Numerics.Tensors.TensorSpan tensor) + where TScalar : System.Numerics.IShiftOperators + { + public void operator <<=(int shiftAmount) { throw null; } } extension(System.Numerics.Tensors.ReadOnlyTensorSpan) where TScalar : System.Numerics.IMultiplyOperators, System.Numerics.IMultiplicativeIdentity @@ -723,7 +762,7 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator *(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } public static System.Numerics.Tensors.Tensor operator *(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } } - extension(Tensor tensor) + extension(System.Numerics.Tensors.Tensor tensor) where TScalar : System.Numerics.IMultiplyOperators, System.Numerics.IMultiplicativeIdentity { public static System.Numerics.Tensors.Tensor operator *(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } @@ -732,7 +771,7 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator *=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator *=(TScalar other) { throw null; } } - extension(ref TensorSpan tensor) + extension(ref System.Numerics.Tensors.TensorSpan tensor) where TScalar : System.Numerics.IMultiplyOperators, System.Numerics.IMultiplicativeIdentity { public static System.Numerics.Tensors.Tensor operator *(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } @@ -741,40 +780,45 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator *=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator *=(TScalar other) { throw null; } } - extension(ReadOnlyTensorSpan) - where TScalar : IShiftOperators + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IBitwiseOperators { - public static Tensor operator <<(in ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } - public static Tensor operator >>(in ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } - public static Tensor operator >>>(in ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } + public static System.Numerics.Tensors.Tensor operator ~(in System.Numerics.Tensors.ReadOnlyTensorSpan tensor) { throw null; } } - extension(Tensor) - where TScalar : IShiftOperators + extension(System.Numerics.Tensors.Tensor) + where TScalar : System.Numerics.IBitwiseOperators { - public static Tensor operator <<(Tensor tensor, int shiftAmount) { throw null; } - public static Tensor operator >>(Tensor tensor, int shiftAmount) { throw null; } - public static Tensor operator >>>(Tensor tensor, int shiftAmount) { throw null; } + public static System.Numerics.Tensors.Tensor operator ~(in System.Numerics.Tensors.Tensor tensor) { throw null; } } - extension(Tensor tensor) - where TScalar : IShiftOperators + extension(System.Numerics.Tensors.TensorSpan) + where TScalar : System.Numerics.IBitwiseOperators + { + public static System.Numerics.Tensors.Tensor operator ~(in System.Numerics.Tensors.TensorSpan tensor) { throw null; } + } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IShiftOperators + { + public static System.Numerics.Tensors.Tensor operator >>(in System.Numerics.Tensors.ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } + } + extension(System.Numerics.Tensors.Tensor) + where TScalar : System.Numerics.IShiftOperators + { + public static System.Numerics.Tensors.Tensor operator >>(System.Numerics.Tensors.Tensor tensor, int shiftAmount) { throw null; } + } + extension(System.Numerics.Tensors.Tensor tensor) + where TScalar : System.Numerics.IShiftOperators { - public void operator <<=(int shiftAmount) { throw null; } public void operator >>=(int shiftAmount) { throw null; } - public void operator >>>=(int shiftAmount) { throw null; } } - extension(TensorSpan) - where TScalar : IShiftOperators + extension(System.Numerics.Tensors.TensorSpan) + where TScalar : System.Numerics.IShiftOperators { - public static Tensor operator <<(in TensorSpan tensor, int shiftAmount) { throw null; } public static Tensor operator >>(in TensorSpan tensor, int shiftAmount) { throw null; } - public static Tensor operator >>>(in TensorSpan tensor, int shiftAmount) { throw null; } } - extension(ref TensorSpan tensor) - where TScalar : IShiftOperators + extension(ref System.Numerics.Tensors.TensorSpan tensor) + where TScalar : System.Numerics.IShiftOperators { - public void operator <<=(int shiftAmount) { throw null; } public void operator >>=(int shiftAmount) { throw null; } - public void operator >>>=(int shiftAmount) { throw null; } } extension(System.Numerics.Tensors.ReadOnlyTensorSpan) where TScalar : System.Numerics.ISubtractionOperators @@ -783,7 +827,7 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.ReadOnlyTensorSpan left, TScalar right) { throw null; } public static System.Numerics.Tensors.Tensor operator -(TScalar left, in System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } } - extension(Tensor tensor) + extension(System.Numerics.Tensors.Tensor tensor) where TScalar : System.Numerics.ISubtractionOperators { public static System.Numerics.Tensors.Tensor operator -(System.Numerics.Tensors.Tensor left, System.Numerics.Tensors.Tensor right) { throw null; } @@ -792,7 +836,7 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso public void operator -=(in System.Numerics.Tensors.ReadOnlyTensorSpan other) { throw null; } public void operator -=(TScalar other) { throw null; } } - extension(ref TensorSpan tensor) + extension(ref System.Numerics.Tensors.TensorSpan tensor) where TScalar : System.Numerics.ISubtractionOperators { public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.TensorSpan left, in System.Numerics.Tensors.TensorSpan right) { throw null; } @@ -806,13 +850,12 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso { public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.ReadOnlyTensorSpan tensor) { throw null; } } - extension(Tensor) + extension(System.Numerics.Tensors.Tensor) where TScalar : System.Numerics.IUnaryNegationOperators { public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.Tensor tensor) { throw null; } } - - extension(TensorSpan) + extension(System.Numerics.Tensors.TensorSpan) where TScalar : System.Numerics.IUnaryNegationOperators { public static System.Numerics.Tensors.Tensor operator -(in System.Numerics.Tensors.TensorSpan tensor) { throw null; } @@ -822,17 +865,41 @@ public static void ResizeTo(scoped in System.Numerics.Tensors.Tensor tenso { public static System.Numerics.Tensors.ReadOnlyTensorSpan operator +(in System.Numerics.Tensors.ReadOnlyTensorSpan tensor) { throw null; } } - extension(Tensor) + extension(System.Numerics.Tensors.Tensor) where TScalar : System.Numerics.IUnaryPlusOperators { - public static System.Numerics.Tensors.Tensor operator +(in System.Numerics.Tensors.Tensor tensor) { throw null; } + public static System.Numerics.Tensors.Tensor operator +(System.Numerics.Tensors.Tensor tensor) { throw null; } } - - extension(TensorSpan) + extension(System.Numerics.Tensors.TensorSpan) where TScalar : System.Numerics.IUnaryPlusOperators { public static System.Numerics.Tensors.TensorSpan operator +(in System.Numerics.Tensors.TensorSpan tensor) { throw null; } } + extension(System.Numerics.Tensors.ReadOnlyTensorSpan) + where TScalar : System.Numerics.IShiftOperators + { + public static System.Numerics.Tensors.Tensor operator >>>(in System.Numerics.Tensors.ReadOnlyTensorSpan tensor, int shiftAmount) { throw null; } + } + extension(System.Numerics.Tensors.Tensor) + where TScalar : System.Numerics.IShiftOperators + { + public static System.Numerics.Tensors.Tensor operator >>>(System.Numerics.Tensors.Tensor tensor, int shiftAmount) { throw null; } + } + extension(System.Numerics.Tensors.Tensor tensor) + where TScalar : System.Numerics.IShiftOperators + { + public void operator >>>=(int shiftAmount) { throw null; } + } + extension(System.Numerics.Tensors.TensorSpan) + where TScalar : System.Numerics.IShiftOperators + { + public static Tensor operator >>>(in TensorSpan tensor, int shiftAmount) { throw null; } + } + extension(ref System.Numerics.Tensors.TensorSpan tensor) + where TScalar : System.Numerics.IShiftOperators + { + public void operator >>>=(int shiftAmount) { throw null; } + } } public readonly ref partial struct TensorDimensionSpan { From 82baad563ad9a65ceacbc62ee5328370bb8646c6 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 23 Jul 2025 23:08:03 -0700 Subject: [PATCH 18/20] Update CompatibilitySuppressions for a fixed parameter name in an experimental API --- .../src/CompatibilitySuppressions.xml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/libraries/System.Numerics.Tensors/src/CompatibilitySuppressions.xml b/src/libraries/System.Numerics.Tensors/src/CompatibilitySuppressions.xml index a4ce8a42bcd7e0..b9631acb2d7041 100644 --- a/src/libraries/System.Numerics.Tensors/src/CompatibilitySuppressions.xml +++ b/src/libraries/System.Numerics.Tensors/src/CompatibilitySuppressions.xml @@ -841,6 +841,13 @@ lib/net8.0/System.Numerics.Tensors.dll true + + CP0017 + M:System.Numerics.Tensors.Tensor.OnesComplement``1(System.Numerics.Tensors.ReadOnlyTensorSpan{``0}@,System.Numerics.Tensors.TensorSpan{``0}@)$0 + lib/net8.0/System.Numerics.Tensors.dll + lib/net8.0/System.Numerics.Tensors.dll + true + CP0017 M:System.Numerics.Tensors.Tensor`1.AsReadOnlyTensorSpan(System.ReadOnlySpan{System.Buffers.NIndex})$0 @@ -1016,6 +1023,13 @@ lib/net9.0/System.Numerics.Tensors.dll true + + CP0017 + M:System.Numerics.Tensors.Tensor.OnesComplement``1(System.Numerics.Tensors.ReadOnlyTensorSpan{``0}@,System.Numerics.Tensors.TensorSpan{``0}@)$0 + lib/net9.0/System.Numerics.Tensors.dll + lib/net9.0/System.Numerics.Tensors.dll + true + CP0017 M:System.Numerics.Tensors.Tensor`1.AsReadOnlyTensorSpan(System.ReadOnlySpan{System.Buffers.NIndex})$0 From ce60d191e30158eafab9630d0cd68fc4242c6532 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Thu, 24 Jul 2025 14:56:23 -0700 Subject: [PATCH 19/20] Change TrimAnalysisVisitor.VisitParameterReference to skip parameters that are not on methods so the analyzer doesn't throw --- .../DynamicallyAccessedMembersAnalyzer.cs | 18 ++++-------------- .../TrimAnalysis/TrimAnalysisVisitor.cs | 12 +++++++++++- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs b/src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs index 5f63510347c8c7..7391b1809abc69 100644 --- a/src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs +++ b/src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs @@ -106,21 +106,11 @@ public override void Initialize(AnalysisContext context) context.RegisterOperationBlockAction(context => { - try + foreach (var operationBlock in context.OperationBlocks) { - foreach (var operationBlock in context.OperationBlocks) - { - TrimDataFlowAnalysis trimDataFlowAnalysis = new(context, dataFlowAnalyzerContext, operationBlock); - trimDataFlowAnalysis.InterproceduralAnalyze(); - trimDataFlowAnalysis.ReportDiagnostics(context.ReportDiagnostic); - } - } - catch (InvalidCastException) - { - // Newer language features may produce NonErrorNamedTypeSymbol - // which will fail to cast to a given target type. Catch and - // ignore the failure in this scenario since it otherwise blocks - // the analyzer + TrimDataFlowAnalysis trimDataFlowAnalysis = new(context, dataFlowAnalyzerContext, operationBlock); + trimDataFlowAnalysis.InterproceduralAnalyze(); + trimDataFlowAnalysis.ReportDiagnostics(context.ReportDiagnostic); } }); diff --git a/src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimAnalysisVisitor.cs b/src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimAnalysisVisitor.cs index fe26d2a7593ff8..96a04fd4ef63e0 100644 --- a/src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimAnalysisVisitor.cs +++ b/src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimAnalysisVisitor.cs @@ -134,8 +134,18 @@ public override MultiValue VisitConversion(IConversionOperation operation, State public override MultiValue VisitParameterReference(IParameterReferenceOperation paramRef, StateValue state) { + IParameterSymbol parameter = paramRef.Parameter; + + if (parameter.ContainingSymbol is not IMethodSymbol) + { + // TODO: Extension members allows parameters to be on types, rather than methods. + // For example: `extension(ref T value) { }` will enumerate `value` where + // the containing symbol is a `NonErrorNamedTypeSymbol` + return TopValue; + } + // Reading from a parameter always returns the same annotated value. We don't track modifications. - return GetParameterTargetValue(paramRef.Parameter); + return GetParameterTargetValue(parameter); } public override MultiValue VisitInstanceReference(IInstanceReferenceOperation instanceRef, StateValue state) From 52ca7ac61847a755e4375582e2df2d9be3cf7a00 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Sun, 3 Aug 2025 06:53:47 -0700 Subject: [PATCH 20/20] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../System/Numerics/Tensors/netcore/Tensor.op_Decrement.cs | 4 ++-- .../System/Numerics/Tensors/netcore/Tensor.op_Division.cs | 6 +++--- .../System/Numerics/Tensors/netcore/Tensor.op_Increment.cs | 4 ++-- .../Numerics/Tensors/netcore/Tensor.op_Subtraction.cs | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Decrement.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Decrement.cs index e1353be9719371..dbeace8824cc05 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Decrement.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Decrement.cs @@ -8,7 +8,7 @@ public static partial class Tensor /// Performs an decrement on a tensor. /// The type of the elements in the tensor. /// The tensor to decrement. - /// A new tensor containing the result of ~. + /// A new tensor containing the result of --. public static Tensor Decrement(in ReadOnlyTensorSpan x) where T : IDecrementOperators { @@ -20,7 +20,7 @@ public static Tensor Decrement(in ReadOnlyTensorSpan x) /// Performs an decrement on a tensor. /// The type of the elements in the tensor. /// The tensor to decrement. - /// The destination where the result of ~ is written. + /// The destination where the result of -- is written. /// A reference to . /// The shapes of and are not compatible. public static ref readonly TensorSpan Decrement(scoped in ReadOnlyTensorSpan x, in TensorSpan destination) diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Division.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Division.cs index c94881f48091a1..7dc50d5959f349 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Division.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Division.cs @@ -110,7 +110,7 @@ public static ref readonly TensorSpan Divide(T x, scoped in ReadOnlyTensor /// The scalar dividend. /// The tensor divisor. /// A new tensor containing the result of / . - public static Tensor operator /(TScalar left, in ReadOnlyTensorSpan right) => Divide(right, left); + public static Tensor operator /(TScalar left, in ReadOnlyTensorSpan right) => Divide(left, right); } /// The type of the elements in the tensor. @@ -124,7 +124,7 @@ public static ref readonly TensorSpan Divide(T x, scoped in ReadOnlyTensor public static Tensor operator /(Tensor left, TScalar right) => Divide(left, right); /// - public static Tensor operator /(TScalar left, Tensor right) => Divide(right, left); + public static Tensor operator /(TScalar left, Tensor right) => Divide(left, right); /// public void operator /=(in ReadOnlyTensorSpan other) => Divide(tensor, other, tensor); @@ -145,7 +145,7 @@ public static ref readonly TensorSpan Divide(T x, scoped in ReadOnlyTensor public static Tensor operator /(in TensorSpan left, TScalar right) => Divide(left, right); /// - public static Tensor operator /(TScalar left, in TensorSpan right) => Divide(right, left); + public static Tensor operator /(TScalar left, in TensorSpan right) => Divide(left, right); /// Performs in-place element-wise division between two tensors. /// The tensor divisor. diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Increment.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Increment.cs index 6fb730c7d5b002..5773aa51e86351 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Increment.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Increment.cs @@ -8,7 +8,7 @@ public static partial class Tensor /// Performs an increment on a tensor. /// The type of the elements in the tensor. /// The tensor to increment. - /// A new tensor containing the result of ~. + /// A new tensor containing the result of ++. public static Tensor Increment(in ReadOnlyTensorSpan x) where T : IIncrementOperators { @@ -20,7 +20,7 @@ public static Tensor Increment(in ReadOnlyTensorSpan x) /// Performs an increment on a tensor. /// The type of the elements in the tensor. /// The tensor to increment. - /// The destination where the result of ~ is written. + /// The destination where the result of ++ is written. /// A reference to . /// The shapes of and are not compatible. public static ref readonly TensorSpan Increment(scoped in ReadOnlyTensorSpan x, in TensorSpan destination) diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Subtraction.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Subtraction.cs index 0a601528529052..dd8ea6c028e0b9 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Subtraction.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.op_Subtraction.cs @@ -111,7 +111,7 @@ public static ref readonly TensorSpan Subtract(T x, scoped in ReadOnlyTens /// The scalar from which to subtract . /// The tensor to subtract from . /// A new tensor containing the result of - . - public static Tensor operator -(TScalar left, in ReadOnlyTensorSpan right) => Subtract(right, left); + public static Tensor operator -(TScalar left, in ReadOnlyTensorSpan right) => Subtract(left, right); } /// The type of the elements in the tensor. @@ -125,7 +125,7 @@ public static ref readonly TensorSpan Subtract(T x, scoped in ReadOnlyTens public static Tensor operator -(Tensor left, TScalar right) => Subtract(left, right); /// - public static Tensor operator -(TScalar left, Tensor right) => Subtract(right, left); + public static Tensor operator -(TScalar left, Tensor right) => Subtract(left, right); /// public void operator -=(in ReadOnlyTensorSpan other) => Subtract(tensor, other, tensor); @@ -146,7 +146,7 @@ public static ref readonly TensorSpan Subtract(T x, scoped in ReadOnlyTens public static Tensor operator -(in TensorSpan left, TScalar right) => Subtract(left, right); /// - public static Tensor operator -(TScalar left, in TensorSpan right) => Subtract(right, left); + public static Tensor operator -(TScalar left, in TensorSpan right) => Subtract(left, right); /// Performs in-place element-wise subtraction between two tensors. /// The tensor to subtract from the tensor being operated on.