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 540736a2df0553..9cfa839832edf4 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 @@ -55,44 +55,72 @@ namespace System.Buffers } namespace System.Numerics.Tensors { - public partial interface ITensor : System.Collections.Generic.IEnumerable, System.Collections.IEnumerable, System.IEquatable, System.Numerics.IEqualityOperators where TSelf : System.Numerics.Tensors.ITensor + public partial interface IReadOnlyTensor : System.Collections.Generic.IEnumerable, System.Collections.IEnumerable where TSelf : System.Numerics.Tensors.IReadOnlyTensor { - static TSelf? Empty { get { throw null; } } + static abstract TSelf? Empty { get; } + nint FlattenedLength { get; } bool IsEmpty { get; } bool IsPinned { get; } - ref T this[scoped System.ReadOnlySpan indices] { get; } + T this[scoped System.ReadOnlySpan indexes] { get; } + TSelf this[scoped System.ReadOnlySpan ranges] { get; } + T this[scoped System.ReadOnlySpan indexes] { get; } int Rank { get; } - System.ReadOnlySpan Strides { get; } - System.Numerics.Tensors.ReadOnlyTensorSpan AsReadOnlyTensorSpan(scoped System.ReadOnlySpan ranges); - System.Numerics.Tensors.TensorSpan AsTensorSpan(scoped System.ReadOnlySpan ranges); - void Clear(); + System.Numerics.Tensors.ReadOnlyTensorSpan AsReadOnlyTensorSpan(); + System.Numerics.Tensors.ReadOnlyTensorSpan AsReadOnlyTensorSpan(scoped System.ReadOnlySpan startIndex); + System.Numerics.Tensors.ReadOnlyTensorSpan AsReadOnlyTensorSpan(scoped System.ReadOnlySpan range); + System.Numerics.Tensors.ReadOnlyTensorSpan AsReadOnlyTensorSpan(scoped System.ReadOnlySpan start); void CopyTo(System.Numerics.Tensors.TensorSpan destination); - void Fill(T value); - ref T GetPinnableReference(); - static abstract bool operator ==(TSelf left, TSelf right); - static abstract bool operator ==(TSelf left, T right); - static abstract implicit operator System.Numerics.Tensors.ReadOnlyTensorSpan (TSelf value); - static abstract implicit operator System.Numerics.Tensors.TensorSpan (TSelf value); - static abstract bool operator !=(TSelf left, TSelf right); - static abstract bool operator !=(TSelf left, T right); - TSelf Slice(scoped System.ReadOnlySpan ranges); + void FlattenTo(System.Span destination); + void GetLengths(System.Span destination); + ref readonly T GetPinnableReference(); + void GetStrides(System.Span destination); + TSelf Slice(scoped System.ReadOnlySpan startIndex); + TSelf Slice(scoped System.ReadOnlySpan range); + TSelf Slice(scoped System.ReadOnlySpan start); bool TryCopyTo(System.Numerics.Tensors.TensorSpan destination); + bool TryFlattenTo(System.Span destination); + } + public partial interface ITensor : System.Collections.Generic.IEnumerable, System.Collections.IEnumerable, System.Numerics.Tensors.IReadOnlyTensor where TSelf : System.Numerics.Tensors.ITensor + { + bool IsReadOnly { get; } + new T this[scoped System.ReadOnlySpan indexes] { get; set; } + new TSelf this[scoped System.ReadOnlySpan ranges] { get; set; } + new T this[scoped System.ReadOnlySpan indexes] { get; set; } + System.Numerics.Tensors.TensorSpan AsTensorSpan(); + System.Numerics.Tensors.TensorSpan AsTensorSpan(scoped System.ReadOnlySpan startIndex); + System.Numerics.Tensors.TensorSpan AsTensorSpan(scoped System.ReadOnlySpan range); + System.Numerics.Tensors.TensorSpan AsTensorSpan(scoped System.ReadOnlySpan start); + void Clear(); + static abstract TSelf Create(System.ReadOnlySpan lengths, bool pinned = false); + static abstract TSelf Create(System.ReadOnlySpan lengths, System.ReadOnlySpan strides, bool pinned = false); + static abstract TSelf CreateUninitialized(System.ReadOnlySpan lengths, bool pinned = false); + static abstract TSelf CreateUninitialized(System.ReadOnlySpan lengths, System.ReadOnlySpan strides, bool pinned = false); + void Fill(T value); + new ref T GetPinnableReference(); } public readonly ref partial struct ReadOnlyTensorSpan { private readonly object _dummy; private readonly int _dummyPrimitive; + public ReadOnlyTensorSpan(System.Span span) { throw null; } + public ReadOnlyTensorSpan(System.Span span, scoped System.ReadOnlySpan lengths, scoped System.ReadOnlySpan strides) { throw null; } + [System.CLSCompliantAttribute(false)] + public unsafe ReadOnlyTensorSpan(T* data, nint dataLength) { throw null; } [System.CLSCompliantAttribute(false)] - public unsafe ReadOnlyTensorSpan(void* pointer, System.ReadOnlySpan lengths, System.ReadOnlySpan strides = default(System.ReadOnlySpan)) { throw null; } - public ReadOnlyTensorSpan(T[]? array, nint start, System.ReadOnlySpan lengths) { throw null; } - public ReadOnlyTensorSpan(T[]? array, System.ReadOnlySpan lengths) { throw null; } + public unsafe ReadOnlyTensorSpan(T* data, nint dataLength, scoped System.ReadOnlySpan lengths, scoped System.ReadOnlySpan strides) { throw null; } + public ReadOnlyTensorSpan(T[]? array) { throw null; } + public ReadOnlyTensorSpan(T[]? array, System.Index startIndex, System.ReadOnlySpan lengths, System.ReadOnlySpan strides) { throw null; } + public ReadOnlyTensorSpan(T[]? array, int start, System.ReadOnlySpan lengths, System.ReadOnlySpan strides) { throw null; } public static System.Numerics.Tensors.ReadOnlyTensorSpan Empty { get { throw null; } } + public nint FlattenedLength { get { throw null; } } public bool IsEmpty { get { throw null; } } - public bool IsPinned { get { throw null; } } - public ref readonly T this[scoped System.ReadOnlySpan indices] { get { throw null; } } - public nint LinearLength { get { throw null; } } + public ref readonly T this[scoped System.ReadOnlySpan indexes] { get { throw null; } } + public System.Numerics.Tensors.ReadOnlyTensorSpan this[scoped System.ReadOnlySpan ranges] { get { throw null; } } + public ref readonly T this[scoped System.ReadOnlySpan indexes] { get { throw null; } } + [System.Diagnostics.CodeAnalysis.UnscopedRefAttribute] + public System.ReadOnlySpan Lengths { get { throw null; } } public int Rank { get { throw null; } } - public System.ReadOnlySpan Shape { get { throw null; } } + [System.Diagnostics.CodeAnalysis.UnscopedRefAttribute] public System.ReadOnlySpan Strides { get { throw null; } } public static System.Numerics.Tensors.ReadOnlyTensorSpan CastUp(System.Numerics.Tensors.ReadOnlyTensorSpan items) where TDerived : class?, T? { throw null; } public void CopyTo(System.Numerics.Tensors.TensorSpan destination) { } @@ -103,18 +131,20 @@ public void CopyTo(System.Numerics.Tensors.TensorSpan destination) { } #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member public System.Numerics.Tensors.ReadOnlyTensorSpan.Enumerator GetEnumerator() { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] -#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member [System.ObsoleteAttribute("GetHashCode() on ReadOnlyTensorSpan will always throw an exception.")] +#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member public override int GetHashCode() { throw null; } #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public ref readonly T GetPinnableReference() { throw null; } public static bool operator ==(System.Numerics.Tensors.ReadOnlyTensorSpan left, System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public static implicit operator System.Numerics.Tensors.ReadOnlyTensorSpan (T[]? array) { throw null; } public static bool operator !=(System.Numerics.Tensors.ReadOnlyTensorSpan left, System.Numerics.Tensors.ReadOnlyTensorSpan right) { throw null; } + public System.Numerics.Tensors.ReadOnlyTensorSpan Slice(scoped System.ReadOnlySpan indexes) { throw null; } public System.Numerics.Tensors.ReadOnlyTensorSpan Slice(scoped System.ReadOnlySpan ranges) { throw null; } - public T[] ToArray() { throw null; } public override string ToString() { throw null; } public bool TryCopyTo(System.Numerics.Tensors.TensorSpan destination) { throw null; } + public bool TryFlattenTo(System.Span destination) { throw null; } public ref partial struct Enumerator { private object _dummy; @@ -142,10 +172,10 @@ public static partial class Tensor public static System.Numerics.Tensors.Tensor CreateAndFillGaussianNormalDistribution(scoped System.ReadOnlySpan lengths) where T : System.IEquatable, System.Numerics.IEqualityOperators, System.Numerics.IFloatingPoint { throw null; } public static System.Numerics.Tensors.Tensor CreateAndFillUniformDistribution(scoped System.ReadOnlySpan lengths) where T : System.IEquatable, System.Numerics.IEqualityOperators, System.Numerics.IFloatingPoint { throw null; } public static System.Numerics.Tensors.Tensor CreateFromEnumerable(System.Collections.Generic.IEnumerable data) where T : System.IEquatable, System.Numerics.IEqualityOperators { throw null; } - public static System.Numerics.Tensors.Tensor CreateUninitialized(bool mustPin, System.ReadOnlySpan lengths) where T : System.IEquatable { throw null; } - public static System.Numerics.Tensors.Tensor CreateUninitialized(bool mustPin, System.ReadOnlySpan lengths, System.ReadOnlySpan strides) where T : System.IEquatable { throw null; } - public static System.Numerics.Tensors.Tensor Create(bool mustPin, System.ReadOnlySpan lengths) where T : System.IEquatable { throw null; } - public static System.Numerics.Tensors.Tensor Create(bool mustPin, System.ReadOnlySpan lengths, System.ReadOnlySpan strides) where T : System.IEquatable { throw null; } + public static System.Numerics.Tensors.Tensor CreateUninitialized(System.ReadOnlySpan lengths, bool pinned = false) where T : System.IEquatable { throw null; } + public static System.Numerics.Tensors.Tensor CreateUninitialized(System.ReadOnlySpan lengths, System.ReadOnlySpan strides, bool pinned = false) where T : System.IEquatable { throw null; } + public static System.Numerics.Tensors.Tensor Create(System.ReadOnlySpan lengths, bool pinned = false) where T : System.IEquatable { throw null; } + public static System.Numerics.Tensors.Tensor Create(System.ReadOnlySpan lengths, System.ReadOnlySpan strides, bool pinned = false) where T : System.IEquatable { throw null; } public static System.Numerics.Tensors.Tensor Create(T[] values, System.ReadOnlySpan lengths) where T : System.IEquatable { throw null; } public static System.Numerics.Tensors.Tensor Create(T[] values, System.ReadOnlySpan lengths, System.ReadOnlySpan strides) where T : System.IEquatable { throw null; } public static System.Numerics.Tensors.TensorSpan DivideInPlace(System.Numerics.Tensors.TensorSpan left, System.Numerics.Tensors.TensorSpan right) where T : System.IEquatable, System.Numerics.IEqualityOperators, System.Numerics.IDivisionOperators { throw null; } @@ -230,8 +260,8 @@ public static partial class Tensor public static System.Numerics.Tensors.Tensor Subtract(T val, System.Numerics.Tensors.Tensor input) where T : System.IEquatable, System.Numerics.IEqualityOperators, System.Numerics.ISubtractionOperators { throw null; } public static T Sum(System.Numerics.Tensors.TensorSpan input) where T : System.IEquatable, System.Numerics.IEqualityOperators, System.Numerics.IAdditionOperators, System.Numerics.IAdditiveIdentity { throw null; } public static T Sum(System.Numerics.Tensors.Tensor input) where T : System.IEquatable, System.Numerics.IEqualityOperators, System.Numerics.IAdditionOperators, System.Numerics.IAdditiveIdentity { throw null; } - public static string ToString(this System.Numerics.Tensors.ReadOnlyTensorSpan span, int maxRows, int maxColumns) { throw null; } - public static string ToString(this System.Numerics.Tensors.TensorSpan span, int maxRows, int maxColumns) { throw null; } + public static string ToString(this System.Numerics.Tensors.ReadOnlyTensorSpan span, scoped System.ReadOnlySpan maximumLengths) { throw null; } + public static string ToString(this System.Numerics.Tensors.TensorSpan span, scoped System.ReadOnlySpan maximumLengths) { throw null; } public static System.Numerics.Tensors.Tensor Transpose(System.Numerics.Tensors.Tensor input) where T : System.IEquatable, System.Numerics.IEqualityOperators { throw null; } public static System.Numerics.Tensors.Tensor Unsqueeze(System.Numerics.Tensors.Tensor input, int axis) where T : System.IEquatable, System.Numerics.IEqualityOperators { throw null; } } @@ -395,19 +425,25 @@ public readonly ref partial struct TensorSpan { private readonly object _dummy; private readonly int _dummyPrimitive; + public TensorSpan(System.Span span) { throw null; } + public TensorSpan(System.Span span, scoped System.ReadOnlySpan lengths, scoped System.ReadOnlySpan strides) { throw null; } [System.CLSCompliantAttribute(false)] - public unsafe TensorSpan(void* pointer, System.ReadOnlySpan lengths, bool isPinned, System.ReadOnlySpan strides = default(System.ReadOnlySpan)) { throw null; } - public TensorSpan(T[]? array, nint start, System.ReadOnlySpan lengths) { throw null; } - public TensorSpan(T[]? array, System.ReadOnlySpan lengths) { throw null; } - public TensorSpan(T[]? array, System.ReadOnlySpan lengths, bool isPinned) { throw null; } + public unsafe TensorSpan(T* data, nint dataLength) { throw null; } + [System.CLSCompliantAttribute(false)] + public unsafe TensorSpan(T* data, nint dataLength, scoped System.ReadOnlySpan lengths, scoped System.ReadOnlySpan strides) { throw null; } + public TensorSpan(T[]? array) { throw null; } + public TensorSpan(T[]? array, System.Index startIndex, scoped System.ReadOnlySpan lengths, scoped System.ReadOnlySpan strides) { throw null; } + public TensorSpan(T[]? array, int start, scoped System.ReadOnlySpan lengths, scoped System.ReadOnlySpan strides) { throw null; } public static System.Numerics.Tensors.TensorSpan Empty { get { throw null; } } + public nint FlattenedLength { get { throw null; } } public bool IsEmpty { get { throw null; } } - public bool IsPinned { get { throw null; } } - public System.Numerics.Tensors.TensorSpan this[scoped System.ReadOnlySpan indices] { get { throw null; } set { } } - public ref T this[scoped System.ReadOnlySpan indices] { get { throw null; } } - public nint LinearLength { get { throw null; } } + public ref T this[scoped System.ReadOnlySpan indexes] { get { throw null; } } + public System.Numerics.Tensors.TensorSpan this[scoped System.ReadOnlySpan ranges] { get { throw null; } set { } } + public ref T this[scoped System.ReadOnlySpan indexes] { get { throw null; } } + [System.Diagnostics.CodeAnalysis.UnscopedRefAttribute] + public System.ReadOnlySpan Lengths { get { throw null; } } public int Rank { get { throw null; } } - public System.ReadOnlySpan Shape { get { throw null; } } + [System.Diagnostics.CodeAnalysis.UnscopedRefAttribute] public System.ReadOnlySpan Strides { get { throw null; } } public void Clear() { } public void CopyTo(System.Numerics.Tensors.TensorSpan destination) { } @@ -417,6 +453,7 @@ public void CopyTo(System.Numerics.Tensors.TensorSpan destination) { } public override bool Equals(object? obj) { throw null; } #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member public void Fill(T value) { } + public void FlattenTo(System.Span destination) { } public System.Numerics.Tensors.TensorSpan.Enumerator GetEnumerator() { throw null; } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] [System.ObsoleteAttribute("GetHashCode() on TensorSpan will always throw an exception.")] @@ -427,11 +464,13 @@ public void Fill(T value) { } public ref T GetPinnableReference() { throw null; } public static bool operator ==(System.Numerics.Tensors.TensorSpan left, System.Numerics.Tensors.TensorSpan right) { throw null; } public static implicit operator System.Numerics.Tensors.ReadOnlyTensorSpan (System.Numerics.Tensors.TensorSpan span) { throw null; } + public static implicit operator System.Numerics.Tensors.TensorSpan (T[]? array) { throw null; } public static bool operator !=(System.Numerics.Tensors.TensorSpan left, System.Numerics.Tensors.TensorSpan right) { throw null; } + public System.Numerics.Tensors.TensorSpan Slice(scoped System.ReadOnlySpan indexes) { throw null; } public System.Numerics.Tensors.TensorSpan Slice(scoped System.ReadOnlySpan ranges) { throw null; } - public T[] ToArray() { throw null; } public override string ToString() { throw null; } public bool TryCopyTo(System.Numerics.Tensors.TensorSpan destination) { throw null; } + public bool TryFlattenTo(System.Span destination) { throw null; } public ref partial struct Enumerator { private object _dummy; @@ -440,40 +479,64 @@ public ref partial struct Enumerator public bool MoveNext() { throw null; } } } - public sealed partial class Tensor : System.Collections.Generic.IEnumerable, System.Collections.IEnumerable, System.IEquatable>, System.Numerics.IEqualityOperators, System.Numerics.Tensors.Tensor, bool>, System.Numerics.Tensors.ITensor, T> + public sealed partial class Tensor : System.Collections.Generic.IEnumerable, System.Collections.IEnumerable, System.Numerics.Tensors.IReadOnlyTensor, T>, System.Numerics.Tensors.ITensor, T> { internal Tensor() { } public static System.Numerics.Tensors.Tensor Empty { get { throw null; } } + public nint FlattenedLength { get { throw null; } } public bool IsEmpty { get { throw null; } } public bool IsPinned { get { throw null; } } + public bool IsReadOnly { get { throw null; } } public System.Numerics.Tensors.Tensor this[System.Numerics.Tensors.Tensor filter] { get { throw null; } } - public ref T this[scoped System.ReadOnlySpan indices] { get { throw null; } } - public nint LinearLength { get { throw null; } } + public ref T this[scoped System.ReadOnlySpan indexes] { get { throw null; } } + public System.Numerics.Tensors.Tensor this[scoped System.ReadOnlySpan ranges] { get { throw null; } set { } } + public ref T this[scoped System.ReadOnlySpan indexes] { get { throw null; } } + public System.ReadOnlySpan Lengths { get { throw null; } } public int Rank { get { throw null; } } - public System.ReadOnlySpan Shape { get { throw null; } } public System.ReadOnlySpan Strides { get { throw null; } } + T System.Numerics.Tensors.IReadOnlyTensor, T>.this[scoped System.ReadOnlySpan indexes] { get { throw null; } } + System.Numerics.Tensors.Tensor System.Numerics.Tensors.IReadOnlyTensor, T>.this[scoped System.ReadOnlySpan ranges] { get { throw null; } } + T System.Numerics.Tensors.IReadOnlyTensor, T>.this[scoped System.ReadOnlySpan indexes] { get { throw null; } } + T System.Numerics.Tensors.ITensor, T>.this[scoped System.ReadOnlySpan indexes] { get { throw null; } set { } } + T System.Numerics.Tensors.ITensor, T>.this[scoped System.ReadOnlySpan indexes] { get { throw null; } set { } } public System.Numerics.Tensors.ReadOnlyTensorSpan AsReadOnlyTensorSpan() { throw null; } - public System.Numerics.Tensors.ReadOnlyTensorSpan AsReadOnlyTensorSpan(scoped System.ReadOnlySpan ranges) { throw null; } + public System.Numerics.Tensors.ReadOnlyTensorSpan AsReadOnlyTensorSpan(scoped System.ReadOnlySpan startIndex) { throw null; } + public System.Numerics.Tensors.ReadOnlyTensorSpan AsReadOnlyTensorSpan(scoped System.ReadOnlySpan start) { throw null; } + public System.Numerics.Tensors.ReadOnlyTensorSpan AsReadOnlyTensorSpan(scoped System.ReadOnlySpan start) { throw null; } public System.Numerics.Tensors.TensorSpan AsTensorSpan() { throw null; } - public System.Numerics.Tensors.TensorSpan AsTensorSpan(scoped System.ReadOnlySpan ranges) { throw null; } + public System.Numerics.Tensors.TensorSpan AsTensorSpan(scoped System.ReadOnlySpan startIndex) { throw null; } + public System.Numerics.Tensors.TensorSpan AsTensorSpan(scoped System.ReadOnlySpan start) { throw null; } + public System.Numerics.Tensors.TensorSpan AsTensorSpan(scoped System.ReadOnlySpan start) { throw null; } public void Clear() { } public void CopyTo(System.Numerics.Tensors.TensorSpan destination) { } + public static System.Numerics.Tensors.Tensor Create(System.ReadOnlySpan lengths, bool pinned = false) { throw null; } + public static System.Numerics.Tensors.Tensor Create(System.ReadOnlySpan lengths, System.ReadOnlySpan strides, bool pinned = false) { throw null; } + public static System.Numerics.Tensors.Tensor CreateUninitialized(System.ReadOnlySpan lengths, bool pinned = false) { throw null; } + public static System.Numerics.Tensors.Tensor CreateUninitialized(System.ReadOnlySpan lengths, System.ReadOnlySpan strides, bool pinned = false) { throw null; } public bool Equals(System.Numerics.Tensors.Tensor? other) { throw null; } public override bool Equals(object? obj) { throw null; } public void Fill(T value) { } + public void FlattenTo(System.Span destination) { } public System.Collections.Generic.IEnumerator GetEnumerator() { throw null; } public override int GetHashCode() { throw null; } + public void GetLengths(System.Span destination) { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public ref T GetPinnableReference() { throw null; } + public void GetStrides(System.Span destination) { } public static bool operator ==(System.Numerics.Tensors.Tensor? left, System.Numerics.Tensors.Tensor? right) { throw null; } public static bool operator ==(System.Numerics.Tensors.Tensor left, T right) { throw null; } public static implicit operator System.Numerics.Tensors.ReadOnlyTensorSpan (System.Numerics.Tensors.Tensor value) { throw null; } public static implicit operator System.Numerics.Tensors.TensorSpan (System.Numerics.Tensors.Tensor value) { throw null; } public static bool operator !=(System.Numerics.Tensors.Tensor? left, System.Numerics.Tensors.Tensor? right) { throw null; } public static bool operator !=(System.Numerics.Tensors.Tensor left, T right) { throw null; } - public System.Numerics.Tensors.Tensor Slice(scoped System.ReadOnlySpan ranges) { throw null; } + public System.Numerics.Tensors.Tensor Slice(scoped System.ReadOnlySpan startIndex) { throw null; } + public System.Numerics.Tensors.Tensor Slice(scoped System.ReadOnlySpan start) { throw null; } + public System.Numerics.Tensors.Tensor Slice(scoped System.ReadOnlySpan start) { throw null; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } - public string ToString(int maxRows, int maxColumns) { throw null; } + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] + ref readonly T System.Numerics.Tensors.IReadOnlyTensor, T>.GetPinnableReference() { throw null; } + public string ToString(scoped System.ReadOnlySpan maximumLengths) { throw null; } public bool TryCopyTo(System.Numerics.Tensors.TensorSpan destination) { throw null; } + public bool TryFlattenTo(System.Span destination) { throw null; } } } 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 bb3af1c3b83f1b..b40960a24e4b09 100644 --- a/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj +++ b/src/libraries/System.Numerics.Tensors/src/System.Numerics.Tensors.csproj @@ -16,6 +16,7 @@ + diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Common/IReadOnlyTensor.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Common/IReadOnlyTensor.cs new file mode 100644 index 00000000000000..94059476a57b46 --- /dev/null +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Common/IReadOnlyTensor.cs @@ -0,0 +1,46 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace System.Numerics.Tensors +{ + public interface IReadOnlyTensor : IEnumerable + where TSelf : IReadOnlyTensor + { + static abstract TSelf? Empty { get; } + + bool IsEmpty { get; } + bool IsPinned { get; } + nint FlattenedLength { get; } + int Rank { get; } + + T this[params ReadOnlySpan indexes] { get; } + T this[params ReadOnlySpan indexes] { get; } + TSelf this[params scoped ReadOnlySpan ranges] { get; } + + ReadOnlyTensorSpan AsReadOnlyTensorSpan(); + ReadOnlyTensorSpan AsReadOnlyTensorSpan(params scoped ReadOnlySpan start); + ReadOnlyTensorSpan AsReadOnlyTensorSpan(params scoped ReadOnlySpan startIndex); + ReadOnlyTensorSpan AsReadOnlyTensorSpan(params scoped ReadOnlySpan range); + + void CopyTo(TensorSpan destination); + void FlattenTo(Span destination); + + // These are not properties so that structs can implement the interface without allocating: + void GetLengths(Span destination); + void GetStrides(Span destination); + + ref readonly T GetPinnableReference(); + TSelf Slice(params scoped ReadOnlySpan start); + TSelf Slice(params scoped ReadOnlySpan startIndex); + TSelf Slice(params scoped ReadOnlySpan range); + bool TryCopyTo(TensorSpan destination); + bool TryFlattenTo(Span destination); + } +} diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ITensor.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ITensor.cs index 5ba15d0e9516ab..49199a1b98f501 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ITensor.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ITensor.cs @@ -11,35 +11,32 @@ namespace System.Numerics.Tensors { public interface ITensor - : IEnumerable + : IReadOnlyTensor where TSelf : ITensor { // TODO: Determine if we can implement `IEqualityOperators`. // It looks like C#/.NET currently hits limitations here as it believes TSelf and T could be the same type // Ideally we could annotate it such that they cannot be the same type and no conflicts would exist - static abstract TSelf? Empty { get; } + static abstract TSelf Create(ReadOnlySpan lengths, bool pinned = false); + static abstract TSelf Create(ReadOnlySpan lengths, ReadOnlySpan strides, bool pinned = false); - bool IsEmpty { get; } - bool IsPinned { get; } - int Rank { get; } - nint FlattenedLength { get; } - // THIS IS GOING TO PEND A DISCUSSION WITH THE LANGUAGE TEAM - void GetStrides(Span destination); - void GetLengths(Span destination); + static abstract TSelf CreateUninitialized(ReadOnlySpan lengths, bool pinned = false); + static abstract TSelf CreateUninitialized(ReadOnlySpan lengths, ReadOnlySpan strides, bool pinned = false); - T this[params scoped ReadOnlySpan indexes] { get; } - T this[params scoped ReadOnlySpan indexes] { get; set; } - TSelf this[params scoped ReadOnlySpan ranges] { get; set; } + bool IsReadOnly { get; } - TensorSpan AsTensorSpan(params scoped ReadOnlySpan ranges); - ReadOnlyTensorSpan AsReadOnlyTensorSpan(params scoped ReadOnlySpan ranges); - ref T GetPinnableReference(); - TSelf Slice(params scoped ReadOnlySpan ranges); + new T this[params ReadOnlySpan indexes] { get; set; } + new T this[params ReadOnlySpan indexes] { get; set; } + new TSelf this[params scoped ReadOnlySpan ranges] { get; set; } + + TensorSpan AsTensorSpan(); + TensorSpan AsTensorSpan(params scoped ReadOnlySpan start); + TensorSpan AsTensorSpan(params scoped ReadOnlySpan startIndex); + TensorSpan AsTensorSpan(params scoped ReadOnlySpan range); void Clear(); - void CopyTo(TensorSpan destination); void Fill(T value); - bool TryCopyTo(TensorSpan destination); + new ref T GetPinnableReference(); } } diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs index 7c6fc043700b91..3e5541dd66b5d9 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs @@ -56,7 +56,7 @@ public ReadOnlyTensorSpan(T[]? array) : this(array, 0, default, default) /// Returns default when is null. /// Thrown when is covariant and array's type is not exactly T[]. /// - /// Thrown when the specified or end index is not in the range (<0 or >Length). + /// Thrown when the specified or end index is not in the range (<0 or >FlattenedLength). /// public ReadOnlyTensorSpan(T[]? array, Index startIndex, ReadOnlySpan lengths, ReadOnlySpan strides) : this(array, startIndex.GetOffset(array?.Length ?? 0), lengths, strides) @@ -74,7 +74,7 @@ public ReadOnlyTensorSpan(T[]? array, Index startIndex, ReadOnlySpan lengt /// Returns default when is null. /// Thrown when is covariant and array's type is not exactly T[]. /// - /// Thrown when the specified or end index is not in the range (<0 or >Length). + /// Thrown when the specified or end index is not in the range (<0 or >FlattenedLength). /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlyTensorSpan(T[]? array, int start, ReadOnlySpan lengths, ReadOnlySpan strides) @@ -205,7 +205,7 @@ internal ReadOnlyTensorSpan(ref T reference, scoped ReadOnlySpan lengths, /// /// /// - /// Thrown when any index is less than 0 or any index is greater than or equal to Length + /// Thrown when any index is less than 0 or any index is greater than or equal to FlattenedLength /// public ref readonly T this[params scoped ReadOnlySpan indexes] { @@ -229,7 +229,7 @@ public ref readonly T this[params scoped ReadOnlySpan indexes] /// /// /// - /// Thrown when any index is less than 0 or any index is greater than or equal to Length + /// Thrown when any index is less than 0 or any index is greater than or equal to FlattenedLength /// public ref readonly T this[params scoped ReadOnlySpan indexes] { @@ -252,7 +252,7 @@ public ref readonly T this[params scoped ReadOnlySpan indexes] /// /// /// - /// Thrown when any index is less than 0 or any index is greater than or equal to Length + /// Thrown when any index is less than 0 or any index is greater than or equal to FlattenedLength /// public ReadOnlyTensorSpan this[params scoped ReadOnlySpan ranges] { @@ -515,7 +515,7 @@ public bool TryCopyTo(TensorSpan destination) /// The indexes for the slice. /// /// - /// Thrown when any index is less than 0 or any index is greater than or equal to Length + /// Thrown when any index is less than 0 or any index is greater than or equal to FlattenedLength /// public ReadOnlyTensorSpan Slice(params scoped ReadOnlySpan indexes) { diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.Factory.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.Factory.cs index cadcd28f2d2579..552398f097727a 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.Factory.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/Tensor.Factory.cs @@ -13,30 +13,30 @@ namespace System.Numerics.Tensors public static partial class Tensor { /// - /// Creates a and initializes it with the default value of T. If is true, the memory will be pinned. + /// Creates a and initializes it with the default value of T. If is true, the memory will be pinned. /// - /// A whether the underlying data should be pinned or not. /// A indicating the lengths of each dimension. - public static Tensor Create(bool mustPin, ReadOnlySpan lengths) + /// A whether the underlying data should be pinned or not. + public static Tensor Create(ReadOnlySpan lengths, bool pinned = false) where T : IEquatable { nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths); - T[] values = mustPin ? GC.AllocateArray((int)linearLength, mustPin) : (new T[linearLength]); - return new Tensor(values, lengths.ToArray(), mustPin); + T[] values = pinned ? GC.AllocateArray((int)linearLength, pinned) : (new T[linearLength]); + return new Tensor(values, lengths.ToArray(), pinned); } /// - /// Creates a and initializes it with the default value of T. If is true, the memory will be pinned. + /// Creates a and initializes it with the default value of T. If is true, the memory will be pinned. /// - /// A whether the underlying data should be pinned or not. /// A indicating the lengths of each dimension. /// A indicating the strides of each dimension. - public static Tensor Create(bool mustPin, ReadOnlySpan lengths, ReadOnlySpan strides) + /// A whether the underlying data should be pinned or not. + public static Tensor Create(ReadOnlySpan lengths, ReadOnlySpan strides, bool pinned = false) where T : IEquatable { nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths); - T[] values = mustPin ? GC.AllocateArray((int)linearLength, mustPin) : (new T[linearLength]); - return new Tensor(values, lengths.ToArray(), strides.ToArray(), mustPin); + T[] values = pinned ? GC.AllocateArray((int)linearLength, pinned) : (new T[linearLength]); + return new Tensor(values, lengths.ToArray(), strides.ToArray(), pinned); } /// @@ -75,30 +75,30 @@ public static Tensor Create(T[] values, ReadOnlySpan lengths, ReadOn } /// - /// Creates a and does not initialize it. If is true, the memory will be pinned. + /// Creates a and does not initialize it. If is true, the memory will be pinned. /// - /// A whether the underlying data should be pinned or not. /// A indicating the lengths of each dimension. - public static Tensor CreateUninitialized(bool mustPin, ReadOnlySpan lengths) + /// A whether the underlying data should be pinned or not. + public static Tensor CreateUninitialized(ReadOnlySpan lengths, bool pinned = false) where T : IEquatable { nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths); - T[] values = GC.AllocateUninitializedArray((int)linearLength, mustPin); - return new Tensor(values, lengths.ToArray(), mustPin); + T[] values = GC.AllocateUninitializedArray((int)linearLength, pinned); + return new Tensor(values, lengths.ToArray(), pinned); } /// - /// Creates a and does not initialize it. If is true, the memory will be pinned. + /// Creates a and does not initialize it. If is true, the memory will be pinned. /// - /// A whether the underlying data should be pinned or not. /// A indicating the lengths of each dimension. /// A indicating the strides of each dimension. - public static Tensor CreateUninitialized(bool mustPin, ReadOnlySpan lengths, ReadOnlySpan strides) + /// A whether the underlying data should be pinned or not. + public static Tensor CreateUninitialized(ReadOnlySpan lengths, ReadOnlySpan strides, bool pinned = false ) where T : IEquatable { nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths); - T[] values = GC.AllocateUninitializedArray((int)linearLength, mustPin); - return new Tensor(values, lengths.ToArray(), strides.ToArray(), mustPin); + T[] values = GC.AllocateUninitializedArray((int)linearLength, pinned); + return new Tensor(values, lengths.ToArray(), strides.ToArray(), pinned); } /// 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 17d3c5c7428de8..795e8585dbe3b9 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 @@ -76,8 +76,57 @@ internal Tensor(T[] values, scoped ReadOnlySpan lengths, ReadOnlySpan + /// Creates a and initializes it with the default value of T. If is true, the memory will be pinned. + /// + /// A indicating the lengths of each dimension. + /// A whether the underlying data should be pinned or not. + public static Tensor Create(ReadOnlySpan lengths, bool pinned = false) + { + nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths); + T[] values = pinned ? GC.AllocateArray((int)linearLength, pinned) : (new T[linearLength]); + return new Tensor(values, lengths.ToArray(), pinned); + } + + /// + /// Creates a and initializes it with the default value of T. If is true, the memory will be pinned. + /// + /// A indicating the lengths of each dimension. + /// A indicating the strides of each dimension. + /// A whether the underlying data should be pinned or not. + public static Tensor Create(ReadOnlySpan lengths, ReadOnlySpan strides, bool pinned = false) + { + nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths); + T[] values = pinned ? GC.AllocateArray((int)linearLength, pinned) : (new T[linearLength]); + return new Tensor(values, lengths.ToArray(), strides.ToArray(), pinned); + } + + /// + /// Creates a and does not initialize it. If is true, the memory will be pinned. + /// + /// A indicating the lengths of each dimension. + /// A whether the underlying data should be pinned or not. + public static Tensor CreateUninitialized(ReadOnlySpan lengths, bool pinned = false) + { + nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths); + T[] values = GC.AllocateUninitializedArray((int)linearLength, pinned); + return new Tensor(values, lengths.ToArray(), pinned); + } + + /// + /// Creates a and does not initialize it. If is true, the memory will be pinned. + /// + /// A indicating the lengths of each dimension. + /// A indicating the strides of each dimension. + /// A whether the underlying data should be pinned or not. + public static Tensor CreateUninitialized(ReadOnlySpan lengths, ReadOnlySpan strides, bool pinned = false) + { + nint linearLength = TensorSpanHelpers.CalculateTotalLength(lengths); + T[] values = GC.AllocateUninitializedArray((int)linearLength, pinned); + return new Tensor(values, lengths.ToArray(), strides.ToArray(), pinned); + } + // ITensor /// /// The Empty Tensor. /// @@ -113,26 +162,148 @@ internal Tensor(T[] values, scoped ReadOnlySpan lengths, ReadOnlySpan with the lengths of each dimension. public ReadOnlySpan Lengths => _lengths; + /// + /// Gets the length of each dimension in this . + /// + /// with the lengths of each dimension. + public void GetLengths(Span destination) => _lengths.CopyTo(destination); + + /// /// Gets the strides of each dimension in this . /// /// with the strides of each dimension. public ReadOnlySpan Strides => _strides; - public Tensor this[params ReadOnlySpan ranges] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public T this[params ReadOnlySpan indexes] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + /// + /// Gets the strides of each dimension in this . + /// + /// with the strides of each dimension. + public void GetStrides(Span destination) => _strides.CopyTo(destination); + + public bool IsReadOnly => false; + + /// + /// Returns a reference to specified element of the Tensor. + /// + /// + /// + /// + /// Thrown when index less than 0 or index greater than or equal to FlattenedLength + /// + public ref T this[params ReadOnlySpan indexes] => ref AsTensorSpan()[indexes]; + + /// + /// Returns a reference to specified element of the Tensor. + /// + /// + /// + /// + /// Thrown when index less than 0 or index greater than or equal to FlattenedLength + /// + public ref T this[params ReadOnlySpan indexes] => ref AsTensorSpan()[indexes]; + + /// + /// Returns a slice of the Tensor. + /// + /// + /// + /// + /// Thrown when any index is less than 0 or any index is greater than or equal to FlattenedLength + /// + public Tensor this[params ReadOnlySpan ranges] + { + get + { + if (ranges.Length != Rank) + ThrowHelper.ThrowIndexOutOfRangeException(); + + return Slice(ranges); + } + set + { + + } + } + + /// + /// Returns the specified element of the Tensor. + /// + /// + /// + /// + /// Thrown when index less than 0 or index greater than or equal to FlattenedLength + /// + T ITensor, T>.this[params scoped ReadOnlySpan indexes] + { + get + { + return this[indexes]; + } + set + { + this[indexes] = value; + } + } + + /// + /// Returns the specified element of the Tensor. + /// + /// + /// + /// + /// Thrown when index less than 0 or index greater than or equal to FlattenedLength + /// + T ITensor, T>.this[params scoped ReadOnlySpan indexes] + { + get + { + return this[indexes]; + } + set + { + this[indexes] = value; + } + } + + /// + /// Returns the specified element of the ReadOnlyTensor. + /// + /// + /// + /// + /// Thrown when index less than 0 or index greater than or equal to FlattenedLength + /// + T IReadOnlyTensor, T>.this[params ReadOnlySpan indexes] => AsReadOnlyTensorSpan()[indexes]; - T ITensor, T>.this[params ReadOnlySpan indexes] => throw new NotImplementedException(); + /// + /// Returns the specified element of the ReadOnlyTensor. + /// + /// + /// + /// + /// Thrown when index less than 0 or index greater than or equal to FlattenedLength + /// + T IReadOnlyTensor, T>.this[params ReadOnlySpan indexes] => AsReadOnlyTensorSpan()[indexes]; /// - /// Returns a reference to specified element of the Span. + /// Returns a slice of the ReadOnlyTensor. /// - /// + /// /// /// - /// Thrown when index less than 0 or index greater than or equal to Length + /// Thrown when any index is less than 0 or any index is greater than or equal to FlattenedLength /// - public ref T this[params ReadOnlySpan indices] => ref AsTensorSpan()[indices]; + Tensor IReadOnlyTensor, T>.this[params ReadOnlySpan ranges] + { + get + { + if (ranges.Length != Rank) + ThrowHelper.ThrowIndexOutOfRangeException(); + + return Slice(ranges); + } + } // REVIEW: WE WILL WANT THIS CHANGED FROM A BOOL TO SOME FILTER EXPRESSION. /// @@ -183,6 +354,27 @@ public Tensor this[Tensor filter] /// public TensorSpan AsTensorSpan() => new TensorSpan(ref MemoryMarshal.GetArrayDataReference(_values), _lengths, _strides); + /// + /// Converts this to a pointing to the same backing memory based on the provided ranges."/> + /// + /// The ranges you want in the . + /// based on the provided ranges. + public TensorSpan AsTensorSpan(params ReadOnlySpan start) => AsTensorSpan().Slice(start); + + /// + /// Converts this to a pointing to the same backing memory based on the provided start locations."/> + /// + /// The start location you want in the . + /// based on the provided ranges. + public TensorSpan AsTensorSpan(params ReadOnlySpan start) => Slice(start); + + /// + /// Converts this to a pointing to the same backing memory based on the provided start indexes."/> + /// + /// The ranges you want in the . + /// based on the provided ranges. + public TensorSpan AsTensorSpan(params ReadOnlySpan startIndex) => AsTensorSpan().Slice(startIndex); + /// /// Converts this to a pointing to the same backing memory."/> /// @@ -190,18 +382,25 @@ public Tensor this[Tensor filter] public ReadOnlyTensorSpan AsReadOnlyTensorSpan() => new ReadOnlyTensorSpan(ref MemoryMarshal.GetArrayDataReference(_values), _lengths, _strides); /// - /// Converts this to a pointing to the same backing memory based on the provided ranges."/> + /// Converts this to a pointing to the same backing memory based on the provided ranges."/> /// - /// The ranges you want in the . - /// based on the provided ranges. - public TensorSpan AsTensorSpan(params ReadOnlySpan ranges) => AsTensorSpan().Slice(ranges); + /// The ranges you want in the + /// + public ReadOnlyTensorSpan AsReadOnlyTensorSpan(params ReadOnlySpan start) => AsTensorSpan().Slice(start); /// - /// Converts this to a pointing to the same backing memory based on the provided ranges."/> + /// Converts this to a pointing to the same backing memory based on the provided start locations."/> /// - /// The ranges you want in the + /// The start locations you want in the /// - public ReadOnlyTensorSpan AsReadOnlyTensorSpan(params ReadOnlySpan ranges) => AsTensorSpan().Slice(ranges); + public ReadOnlyTensorSpan AsReadOnlyTensorSpan(params ReadOnlySpan start) => Slice(start); + + /// + /// Converts this to a pointing to the same backing memory based on the provided start indexes."/> + /// + /// The start indexes you want in the + /// + public ReadOnlyTensorSpan AsReadOnlyTensorSpan(params ReadOnlySpan startIndex) => AsTensorSpan().Slice(startIndex); /// /// Returns a reference to the 0th element of the Tensor. If the Tensor is empty, returns null reference. @@ -210,25 +409,63 @@ public Tensor this[Tensor filter] [EditorBrowsable(EditorBrowsableState.Never)] public ref T GetPinnableReference() => ref AsTensorSpan().GetPinnableReference(); + /// + /// Returns a reference to the 0th element of the ReadOnlyTensor. If the ReadOnlyTensor is empty, returns null reference. + /// It can be used for pinning and is required to support the use of ReadOnlyTensor within a fixed statement. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + ref readonly T IReadOnlyTensor, T>.GetPinnableReference() => ref AsReadOnlyTensorSpan().GetPinnableReference(); + /// /// Forms a slice out of the given tensor /// - /// The ranges for the slice + /// The ranges for the slice /// as a copy of the provided ranges. // REVIEW: CURRENTLY DOES A COPY. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Tensor Slice(params ReadOnlySpan ranges) + public Tensor Slice(params ReadOnlySpan start) { - if (ranges.Length != Lengths.Length) - throw new ArgumentOutOfRangeException(nameof(ranges), "Number of dimensions to slice does not equal the number of dimensions in the span"); + if (start.Length != Lengths.Length) + throw new ArgumentOutOfRangeException(nameof(start), "Number of dimensions to slice does not equal the number of dimensions in the span"); - TensorSpan s = AsTensorSpan(ranges); + TensorSpan s = AsTensorSpan(start); T[] values = _isPinned ? GC.AllocateArray(checked((int)s.FlattenedLength), _isPinned) : (new T[s.FlattenedLength]); var outTensor = new Tensor(values, s.Lengths.ToArray(), _isPinned); s.CopyTo(outTensor); return outTensor; } + /// + /// Forms a slice out of the given tensor + /// + /// The start indexes for the slice + /// as a copy of the provided ranges. + // REVIEW: CURRENTLY DOES A COPY. + public Tensor Slice(params ReadOnlySpan start) + { + NRange[] ranges = new NRange[start.Length]; + for (int i = 0; i < start.Length; i++) + { + ranges[i] = new NRange(start[i], new NIndex(0, fromEnd: true)); + } + return Slice(ranges); + } + + /// + /// Forms a slice out of the given tensor + /// + /// The start indexes for the slice + /// as a copy of the provided ranges. + // REVIEW: CURRENTLY DOES A COPY. + public Tensor Slice(params ReadOnlySpan startIndex) + { + NRange[] ranges = new NRange[startIndex.Length]; + for (int i = 0; i < startIndex.Length; i++) + { + ranges[i] = new NRange(startIndex[i], new NIndex(0, fromEnd: true)); + } + return Slice(ranges); + } + /// /// Clears the contents of this tensor. /// @@ -263,6 +500,18 @@ public Tensor Slice(params ReadOnlySpan ranges) /// return false and no data is written to the destination. public bool TryCopyTo(TensorSpan destination) => AsTensorSpan().TryCopyTo(destination); + /// + /// Flattens the contents of this Tensor into the provided . + /// + /// The span to copy items into. + public void FlattenTo(Span destination) => AsTensorSpan().FlattenTo(destination); + + /// + /// Flattens the contents of this Tensor into the provided . + /// + /// The span to copy items into. + public bool TryFlattenTo(Span destination) => AsTensorSpan().TryFlattenTo(destination); + // IEnumerable /// /// Gets an for the . @@ -442,7 +691,12 @@ private string ToMetadataString() return sb.ToString(); } - public string ToString(int maxRows, int maxColumns) + /// + /// Creates a representation of the ."/> + /// + /// Maximum Length of each dimension + /// A representation of the + public string ToString(params ReadOnlySpan maximumLengths) { var sb = new StringBuilder(); sb.AppendLine(ToMetadataString()); @@ -451,8 +705,5 @@ public string ToString(int maxRows, int maxColumns) sb.AppendLine("}"); return sb.ToString(); } - - public void GetStrides(Span destination) => throw new NotImplementedException(); - public void GetLengths(Span destination) => throw new NotImplementedException(); } } diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorExtensions.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorExtensions.cs index 8a0802c325b3ed..d35cdc639347ce 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorExtensions.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorExtensions.cs @@ -26,19 +26,17 @@ public static partial class Tensor /// Creates a representation of the ."/> /// /// The you want to represent as a string. - /// Max rows to show. - /// Max columns to show. + /// Maximum Length of each dimension /// A representation of the - public static string ToString(this TensorSpan span, int maxRows, int maxColumns) => ((ReadOnlyTensorSpan)span).ToString(maxRows, maxColumns); + public static string ToString(this TensorSpan span, params ReadOnlySpan maximumLengths) => ((ReadOnlyTensorSpan)span).ToString(maximumLengths); /// /// Creates a representation of the ."/> /// /// /// The you want to represent as a string. - /// Max rows to show. - /// Max columns to show. - public static string ToString(this ReadOnlyTensorSpan span, int maxRows, int maxColumns) + /// Maximum Length of each dimension + public static string ToString(this ReadOnlyTensorSpan span, params ReadOnlySpan maximumLengths) { var sb = new StringBuilder(); scoped Span curIndexes; @@ -557,7 +555,7 @@ public static Tensor SequenceEqual(Tensor left, Tensor right) Tensor result; if (TensorHelpers.AreShapesTheSame(left, right)) { - result = Tensor.Create(false, left.Lengths); + result = Tensor.Create(left.Lengths, false); for (int i = 0; i < left.FlattenedLength; i++) { @@ -567,7 +565,7 @@ public static Tensor SequenceEqual(Tensor left, Tensor right) else { nint[] newSize = TensorHelpers.GetSmallestBroadcastableSize(left.Lengths, right.Lengths); - result = Tensor.Create(false, newSize); + result = Tensor.Create(newSize, false); Tensor broadcastedLeft = BroadcastTo(left, newSize); Tensor broadcastedRight = BroadcastTo(right, newSize); @@ -616,7 +614,7 @@ public static Tensor LessThan(Tensor left, Tensor right) Tensor result; if (TensorHelpers.AreShapesTheSame(left, right)) { - result = Tensor.Create(false, left.Lengths); + result = Tensor.Create(left.Lengths, false); for (int i = 0; i < left.FlattenedLength; i++) { @@ -626,7 +624,7 @@ public static Tensor LessThan(Tensor left, Tensor right) else { nint[] newSize = TensorHelpers.GetSmallestBroadcastableSize(left.Lengths, right.Lengths); - result = Tensor.Create(false, newSize); + result = Tensor.Create(newSize, false); Tensor broadcastedLeft = BroadcastTo(left, newSize); Tensor broadcastedRight = BroadcastTo(right, newSize); @@ -669,7 +667,7 @@ public static Tensor LessThan(Tensor left, Tensor right) public static Tensor LessThan(Tensor left, T right) where T : IEquatable, IEqualityOperators, IComparisonOperators { - Tensor result = Tensor.Create(false, left.Lengths); + Tensor result = Tensor.Create(left.Lengths, false); for (int i = 0; i < left.FlattenedLength; i++) { @@ -801,7 +799,7 @@ public static Tensor GreaterThan(Tensor left, Tensor right) Tensor result; if (TensorHelpers.AreShapesTheSame(left, right)) { - result = Tensor.Create(false, left.Lengths); + result = Tensor.Create(left.Lengths, false); for (int i = 0; i < left.FlattenedLength; i++) { @@ -811,7 +809,7 @@ public static Tensor GreaterThan(Tensor left, Tensor right) else { nint[] newSize = TensorHelpers.GetSmallestBroadcastableSize(left.Lengths, right.Lengths); - result = Tensor.Create(false, newSize); + result = Tensor.Create(newSize, false); Tensor broadcastedLeft = BroadcastTo(left, newSize); Tensor broadcastedRight = BroadcastTo(right, newSize); @@ -854,7 +852,7 @@ public static Tensor GreaterThan(Tensor left, Tensor right) public static Tensor GreaterThan(Tensor left, T right) where T : IEquatable, IEqualityOperators, IComparisonOperators { - Tensor result = Tensor.Create(false, left.Lengths); + Tensor result = Tensor.Create(left.Lengths, false); for (int i = 0; i < left.FlattenedLength; i++) { @@ -1422,7 +1420,7 @@ public static Tensor Multiply(Tensor input, T val) where T : IEquatable, IEqualityOperators, IMultiplyOperators, IMultiplicativeIdentity { ReadOnlySpan span = MemoryMarshal.CreateSpan(ref input._values[0], (int)input._flattenedLength); - Tensor output = Create(input.IsPinned, input.Lengths); + Tensor output = Create(input.Lengths, input.IsPinned); Span ospan = MemoryMarshal.CreateSpan(ref output._values[0], (int)output._flattenedLength); TensorPrimitives.Multiply(span, val, ospan); return output; @@ -1533,7 +1531,7 @@ public static Tensor Divide(Tensor input, T val) where T : IEquatable, IEqualityOperators, IDivisionOperators { ReadOnlySpan span = MemoryMarshal.CreateSpan(ref input._values[0], (int)input._flattenedLength); - Tensor output = Create(input.IsPinned, input.Lengths); + Tensor output = Create(input.Lengths, input.IsPinned); Span ospan = MemoryMarshal.CreateSpan(ref output._values[0], (int)output._flattenedLength); TensorPrimitives.Divide(span, val, ospan); return output; @@ -1563,7 +1561,7 @@ public static Tensor Divide(T val, Tensor input) where T : IEquatable, IEqualityOperators, IDivisionOperators { ReadOnlySpan span = MemoryMarshal.CreateSpan(ref input._values[0], (int)input._flattenedLength); - Tensor output = Create(input.IsPinned, input.Lengths); + Tensor output = Create(input.Lengths, input.IsPinned); Span ospan = MemoryMarshal.CreateSpan(ref output._values[0], (int)output._flattenedLength); TensorPrimitives.Divide(val, span, ospan); return output; @@ -1704,7 +1702,7 @@ public static Tensor Subtract(Tensor input, T val) where T : IEquatable, IEqualityOperators, ISubtractionOperators { ReadOnlySpan span = MemoryMarshal.CreateSpan(ref input._values[0], (int)input._flattenedLength); - Tensor output = Create(input.IsPinned, input.Lengths); + Tensor output = Create(input.Lengths, input.IsPinned); Span ospan = MemoryMarshal.CreateSpan(ref output._values[0], (int)output._flattenedLength); TensorPrimitives.Subtract(span, val, ospan); return output; @@ -1734,7 +1732,7 @@ public static Tensor Subtract(T val, Tensor input) where T : IEquatable, IEqualityOperators, ISubtractionOperators { ReadOnlySpan span = MemoryMarshal.CreateSpan(ref input._values[0], (int)input._flattenedLength); - Tensor output = Create(input.IsPinned, input.Lengths); + Tensor output = Create(input.Lengths, input.IsPinned); Span ospan = MemoryMarshal.CreateSpan(ref output._values[0], (int)output._flattenedLength); TensorPrimitives.Subtract(val, span, ospan); return output; @@ -1921,7 +1919,7 @@ public static Tensor Add(Tensor input, T val) where T : IEquatable, IEqualityOperators, IAdditionOperators, IAdditiveIdentity { ReadOnlySpan span = MemoryMarshal.CreateSpan(ref input._values[0], (int)input._flattenedLength); - Tensor output = Create(input.IsPinned, input.Lengths); + Tensor output = Create(input.Lengths, input.IsPinned); Span ospan = MemoryMarshal.CreateSpan(ref output._values[0], (int)output._flattenedLength); TensorPrimitives.Add(span, val, ospan); return output; @@ -2290,7 +2288,7 @@ private static Tensor TensorPrimitivesHelperT1(Tensor input, PerformCal where T : IEquatable, IEqualityOperators { ReadOnlySpan span = MemoryMarshal.CreateSpan(ref input._values[0], (int)input._flattenedLength); - Tensor output = inPlace ? input : Create(input.IsPinned, input.Lengths); + Tensor output = inPlace ? input : Create(input.Lengths, input.IsPinned); Span ospan = MemoryMarshal.CreateSpan(ref output._values[0], (int)output._flattenedLength); performCalculation(span, ospan); return output; @@ -2327,7 +2325,7 @@ private static Tensor TensorPrimitivesHelperT1T2(Tensor left, Tensor { ReadOnlySpan span = MemoryMarshal.CreateSpan(ref left.AsTensorSpan()._reference, (int)left.FlattenedLength); ReadOnlySpan rspan = MemoryMarshal.CreateSpan(ref right.AsTensorSpan()._reference, (int)right.FlattenedLength); - output = Create(left.IsPinned, left.Lengths); + output = Create(left.Lengths, left.IsPinned); Span ospan = MemoryMarshal.CreateSpan(ref output.AsTensorSpan()._reference, (int)output.FlattenedLength); performCalculation(span, rspan, ospan); return output; @@ -2344,7 +2342,7 @@ private static Tensor TensorPrimitivesHelperT1T2(Tensor left, Tensor var broadcastedLeft = Tensor.BroadcastTo(left, newSize); var broadcastedRight = Tensor.BroadcastTo(right, newSize); - output = Create(left.IsPinned, newSize); + output = Create(newSize, left.IsPinned); nint rowLength = newSize[^1]; Span ospan; Span ispan; diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs index bf79dd34bcf0aa..20da07b537ee1c 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs @@ -59,7 +59,7 @@ public TensorSpan(T[]? array) : this(array, 0, default, default) /// Returns default when is null. /// Thrown when is covariant and array's type is not exactly T[]. /// - /// Thrown when the specified or end index is not in the range (<0 or >Length). + /// Thrown when the specified or end index is not in the range (<0 or >FlattenedLength). /// public TensorSpan(T[]? array, Index startIndex, scoped ReadOnlySpan lengths, scoped ReadOnlySpan strides) : this(array, startIndex.GetOffset(array?.Length ?? 0), lengths, strides) @@ -77,7 +77,7 @@ public TensorSpan(T[]? array, Index startIndex, scoped ReadOnlySpan length /// Returns default when is null. /// Thrown when is covariant and array's type is not exactly T[]. /// - /// Thrown when the specified or end index is not in the range (<0 or >Length). + /// Thrown when the specified or end index is not in the range (<0 or >FlattenedLength). /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public TensorSpan(T[]? array, int start, scoped ReadOnlySpan lengths, scoped ReadOnlySpan strides) @@ -208,7 +208,7 @@ internal TensorSpan(ref T reference, scoped ReadOnlySpan lengths, scoped R /// /// /// - /// Thrown when any index is less than 0 or any index is greater than or equal to Length + /// Thrown when any index is less than 0 or any index is greater than or equal to FlattenedLength /// public ref T this[params scoped ReadOnlySpan indexes] { @@ -232,7 +232,7 @@ public ref T this[params scoped ReadOnlySpan indexes] /// /// /// - /// Thrown when any index is less than 0 or any index is greater than or equal to Length + /// Thrown when any index is less than 0 or any index is greater than or equal to FlattenedLength /// public ref T this[params scoped ReadOnlySpan indexes] { @@ -257,7 +257,7 @@ public ref T this[params scoped ReadOnlySpan indexes] /// /// /// - /// Thrown when any index is less than 0 or any index is greater than or equal to Length + /// Thrown when any index is less than 0 or any index is greater than or equal to FlattenedLength /// public TensorSpan this[params scoped ReadOnlySpan ranges] { @@ -441,7 +441,7 @@ public void Fill(T value) { Span curIndexes = stackalloc nint[Rank]; nint filledValues = 0; - // REVIEW: If we track the actual length of the backing data, because Length doesn't always equal the actual length, we could use that here to not need to loop. + // REVIEW: If we track the actual length of the backing data, because FlattenedLength doesn't always equal the actual length, we could use that here to not need to loop. while (filledValues < _flattenedLength) { TensorSpanHelpers.Fill(ref Unsafe.Add(ref _reference, TensorSpanHelpers.ComputeLinearIndex(curIndexes, Strides, Lengths)), (nuint)Lengths[Rank - 1], value); @@ -564,7 +564,7 @@ public static implicit operator ReadOnlyTensorSpan(TensorSpan span) => /// The indexes for the slice. /// /// - /// Thrown when any index is less than 0 or any index is greater than or equal to Length + /// Thrown when any index is less than 0 or any index is greater than or equal to FlattenedLength /// public TensorSpan Slice(params scoped ReadOnlySpan indexes) { diff --git a/src/libraries/System.Numerics.Tensors/tests/NRangeTests.cs b/src/libraries/System.Numerics.Tensors/tests/NRangeTests.cs index 0dd4156c7c4194..eb5d7ba88ae45e 100644 --- a/src/libraries/System.Numerics.Tensors/tests/NRangeTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/NRangeTests.cs @@ -128,7 +128,7 @@ public static void CustomTypeTest() } } - // CustomNativeRangeTester is a custom class which containing the members Length, Slice and int NativeIndexer. + // CustomNativeRangeTester is a custom class which containing the members FlattenedLength, Slice and int NativeIndexer. // Having these members allow the C# compiler to support // this[NIndex] // this[NRange] diff --git a/src/libraries/System.Numerics.Tensors/tests/TensorTests.cs b/src/libraries/System.Numerics.Tensors/tests/TensorTests.cs index 54486908f13d66..a8eae72369ecfb 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorTests.cs @@ -825,7 +825,7 @@ public static void IntArrayAsTensor() int[] a = [91, 92, -93, 94]; TensorSpan spanInt = a.AsTensorSpan(4); nint[] dims = [4]; - var tensor = Tensor.CreateUninitialized(false, dims.AsSpan()); + var tensor = Tensor.CreateUninitialized(dims.AsSpan(), false); spanInt.CopyTo(tensor); Assert.Equal(1, tensor.Rank); @@ -854,11 +854,11 @@ public static void IntArrayAsTensor() a[3] = 94; spanInt = a.AsTensorSpan(2, 2); dims = [2, 2]; - tensor = Tensor.CreateUninitialized(false, dims.AsSpan()); + tensor = Tensor.CreateUninitialized(dims.AsSpan(), false); spanInt.CopyTo(tensor); Assert.Equal(a, tensor.ToArray()); Assert.Equal(2, tensor.Rank); - //Assert.Equal(4, spanInt.Length); + //Assert.Equal(4, spanInt.FlattenedLength); Assert.Equal(2, tensor.Lengths.Length); Assert.Equal(2, tensor.Lengths[0]); Assert.Equal(2, tensor.Lengths[1]); @@ -885,7 +885,7 @@ public static void IntArrayAsTensor() public static void TensorFillTest() { nint[] dims = [3, 3]; - var tensor = Tensor.CreateUninitialized(false, dims.AsSpan()); + var tensor = Tensor.CreateUninitialized(dims.AsSpan(), false); tensor.Fill(-1); var enumerator = tensor.GetEnumerator(); while (enumerator.MoveNext()) @@ -908,7 +908,7 @@ public static void TensorFillTest() } dims = [9]; - tensor = Tensor.CreateUninitialized(false, dims.AsSpan()); + tensor = Tensor.CreateUninitialized(dims.AsSpan(), false); tensor.Fill(-1); enumerator = tensor.GetEnumerator(); while (enumerator.MoveNext()) @@ -917,7 +917,7 @@ public static void TensorFillTest() } dims = [3, 3, 3]; - tensor = Tensor.CreateUninitialized(false, dims.AsSpan()); + tensor = Tensor.CreateUninitialized(dims.AsSpan(), false); tensor.Fill(-1); enumerator = tensor.GetEnumerator(); while (enumerator.MoveNext()) @@ -926,7 +926,7 @@ public static void TensorFillTest() } dims = [3, 2, 2]; - tensor = Tensor.CreateUninitialized(false, dims.AsSpan()); + tensor = Tensor.CreateUninitialized(dims.AsSpan(), false); tensor.Fill(-1); enumerator = tensor.GetEnumerator(); while (enumerator.MoveNext()) @@ -935,7 +935,7 @@ public static void TensorFillTest() } dims = [2, 2, 2, 2]; - tensor = Tensor.CreateUninitialized(false , dims.AsSpan()); + tensor = Tensor.CreateUninitialized(dims.AsSpan(), false); tensor.Fill(-1); enumerator = tensor.GetEnumerator(); while (enumerator.MoveNext()) @@ -944,7 +944,7 @@ public static void TensorFillTest() } dims = [3, 2, 2, 2]; - tensor = Tensor.CreateUninitialized(false, dims.AsSpan()); + tensor = Tensor.CreateUninitialized(dims.AsSpan(), false); tensor.Fill(-1); enumerator = tensor.GetEnumerator(); while (enumerator.MoveNext()) @@ -958,7 +958,7 @@ public static void TensorClearTest() { int[] a = [1, 2, 3, 4, 5, 6, 7, 8, 9]; TensorSpan spanInt = a.AsTensorSpan(3, 3); - var tensor = Tensor.CreateUninitialized(false, [3, 3]); + var tensor = Tensor.CreateUninitialized([3, 3], false); spanInt.CopyTo(tensor); var slice = tensor.Slice(0..2, 0..2); slice.Clear(); @@ -988,7 +988,7 @@ public static void TensorClearTest() a = [1, 2, 3, 4, 5, 6, 7, 8, 9]; spanInt = a.AsTensorSpan(9); - tensor = Tensor.CreateUninitialized(false, [9]); + tensor = Tensor.CreateUninitialized([9], false); spanInt.CopyTo(tensor); slice = tensor.Slice(0..1); slice.Clear(); @@ -1015,7 +1015,7 @@ public static void TensorClearTest() a = [.. Enumerable.Range(0, 27)]; spanInt = a.AsTensorSpan(3, 3, 3); - tensor = Tensor.CreateUninitialized(false, [3, 3, 3]); + tensor = Tensor.CreateUninitialized([3, 3, 3], false); spanInt.CopyTo(tensor); tensor.Clear(); enumerator = tensor.GetEnumerator(); @@ -1026,7 +1026,7 @@ public static void TensorClearTest() a = [.. Enumerable.Range(0, 12)]; spanInt = a.AsTensorSpan(3, 2, 2); - tensor = Tensor.CreateUninitialized(false, [3, 2, 2]); + tensor = Tensor.CreateUninitialized([3, 2, 2], false); spanInt.CopyTo(tensor); tensor.Clear(); enumerator = tensor.GetEnumerator(); @@ -1037,7 +1037,7 @@ public static void TensorClearTest() a = [.. Enumerable.Range(0, 16)]; spanInt = a.AsTensorSpan(2, 2, 2, 2); - tensor = Tensor.CreateUninitialized(false, [2, 2, 2, 2]); + tensor = Tensor.CreateUninitialized([2, 2, 2, 2], false); spanInt.CopyTo(tensor); tensor.Clear(); enumerator = tensor.GetEnumerator(); @@ -1048,7 +1048,7 @@ public static void TensorClearTest() a = [.. Enumerable.Range(0, 24)]; spanInt = a.AsTensorSpan(3, 2, 2, 2); - tensor = Tensor.CreateUninitialized(false, [3, 2, 2, 2]); + tensor = Tensor.CreateUninitialized([3, 2, 2, 2], false); spanInt.CopyTo(tensor); tensor.Clear(); enumerator = tensor.GetEnumerator(); @@ -1202,7 +1202,7 @@ public static void TensorCopyTest() int[] rightData = new int[9]; nint[] dims = [3, 3]; TensorSpan leftSpan = leftData.AsTensorSpan(3, 3); - var tensor = Tensor.CreateUninitialized(false, dims.AsSpan()); + var tensor = Tensor.CreateUninitialized(dims.AsSpan(), false); TensorSpan rightSpan = rightData.AsTensorSpan(3, 3); leftSpan.CopyTo(tensor); var leftEnum = leftSpan.GetEnumerator(); @@ -1227,7 +1227,7 @@ public static void TensorCopyTest() leftData = [1, 2, 3, 4, 5, 6, 7, 8, 9]; dims = [15]; leftSpan = leftData.AsTensorSpan(9); - tensor = Tensor.Create(false, dims.AsSpan()); + tensor = Tensor.Create(dims.AsSpan(), false); leftSpan.CopyTo(tensor); leftEnum = leftSpan.GetEnumerator(); tensorEnum = tensor.GetEnumerator(); @@ -1257,7 +1257,7 @@ public static void TensorTryCopyTest() int[] rightData = new int[9]; TensorSpan leftSpan = leftData.AsTensorSpan(3, 3); nint[] dims = [3, 3]; - var tensor = Tensor.CreateUninitialized(false, dims.AsSpan()); + var tensor = Tensor.CreateUninitialized(dims.AsSpan(), false); TensorSpan rightSpan = rightData.AsTensorSpan(3, 3); var success = leftSpan.TryCopyTo(tensor); Assert.True(success); @@ -1279,7 +1279,7 @@ public static void TensorTryCopyTest() leftData = [1, 2, 3, 4, 5, 6, 7, 8, 9]; dims = [15]; leftSpan = leftData.AsTensorSpan(9); - tensor = Tensor.Create(false, dims.AsSpan()); + tensor = Tensor.Create(dims.AsSpan(), false); success = leftSpan.TryCopyTo(tensor); leftEnum = leftSpan.GetEnumerator(); tensorEnum = tensor.GetEnumerator(); @@ -1298,7 +1298,7 @@ public static void TensorTryCopyTest() leftData = [.. Enumerable.Range(0, 27)]; var l = leftData.AsTensorSpan(3, 3, 3); dims = [2, 2]; - tensor = Tensor.Create(false, dims.AsSpan()); + tensor = Tensor.Create(dims.AsSpan(), false); var r = new TensorSpan(); success = l.TryCopyTo(tensor); Assert.False(success); @@ -1310,7 +1310,7 @@ public static void TensorTryCopyTest() public static void TensorSliceTest() { int[] a = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - var tensor = Tensor.CreateUninitialized(false, [3, 3]); + var tensor = Tensor.CreateUninitialized([3, 3], false); //Assert.Throws(() => tensor.Slice(0..1)); //Assert.Throws(() => tensor.Slice(1..2)); @@ -1378,7 +1378,7 @@ public static void TensorSliceTest() int[] numbers = [.. Enumerable.Range(0, 27)]; intSpan = numbers.AsTensorSpan(3, 3, 3); - tensor = Tensor.CreateUninitialized(false, [3, 3, 3]); + tensor = Tensor.CreateUninitialized([3, 3, 3], false); intSpan.CopyTo(tensor.AsTensorSpan()); sp = tensor.Slice(1..2, 1..2, 1..2); Assert.Equal(13, sp[0, 0, 0]); @@ -1411,7 +1411,7 @@ public static void TensorSliceTest() numbers = [.. Enumerable.Range(0, 16)]; intSpan = numbers.AsTensorSpan(2, 2, 2, 2); - tensor = Tensor.CreateUninitialized(false, [2, 2, 2, 2]); + tensor = Tensor.CreateUninitialized([2, 2, 2, 2], false); intSpan.CopyTo(tensor.AsTensorSpan()); sp = tensor.Slice(1..2, 0..2, 1..2, 0..2); Assert.Equal(10, sp[0, 0, 0, 0]); @@ -1433,7 +1433,7 @@ public static void TensorReshapeTest() { int[] a = [1, 2, 3, 4, 5, 6, 7, 8, 9]; nint[] dims = [9]; - var tensor = Tensor.CreateUninitialized(false, dims.AsSpan()); + var tensor = Tensor.CreateUninitialized(dims.AsSpan(), false); var span = a.AsTensorSpan(dims); span.CopyTo(tensor); @@ -1476,7 +1476,7 @@ public static void TensorReshapeTest() public static void TensorSqueezeTest() { nint[] dims = [1, 2]; - var tensor = Tensor.Create(false, dims.AsSpan()); + var tensor = Tensor.Create(dims.AsSpan(), false); Assert.Equal(2, tensor.Rank); Assert.Equal(1, tensor.Lengths[0]); Assert.Equal(2, tensor.Lengths[1]); @@ -1486,7 +1486,7 @@ public static void TensorSqueezeTest() Assert.Equal(2, tensor.Lengths[0]); dims = [1, 2, 1]; - tensor = Tensor.Create(false, dims.AsSpan()); + tensor = Tensor.Create(dims.AsSpan(), false); Assert.Equal(3, tensor.Rank); Assert.Equal(1, tensor.Lengths[0]); Assert.Equal(2, tensor.Lengths[1]); @@ -1497,7 +1497,7 @@ public static void TensorSqueezeTest() Assert.Equal(2, tensor.Lengths[0]); dims = [1, 2, 1]; - tensor = Tensor.Create(false, dims.AsSpan()); + tensor = Tensor.Create(dims.AsSpan(), false); Assert.Equal(3, tensor.Rank); Assert.Equal(1, tensor.Lengths[0]); Assert.Equal(2, tensor.Lengths[1]); @@ -1509,7 +1509,7 @@ public static void TensorSqueezeTest() Assert.Equal(1, tensor.Lengths[1]); dims = [1, 2, 1]; - tensor = Tensor.Create(false, dims.AsSpan()); + tensor = Tensor.Create(dims.AsSpan(), false); Assert.Equal(3, tensor.Rank); Assert.Equal(1, tensor.Lengths[0]); Assert.Equal(2, tensor.Lengths[1]); @@ -1521,7 +1521,7 @@ public static void TensorSqueezeTest() Assert.Equal(2, tensor.Lengths[1]); dims = [1, 2, 1]; - tensor = Tensor.Create(false, dims.AsSpan()); + tensor = Tensor.Create(dims.AsSpan(), false); Assert.Equal(3, tensor.Rank); Assert.Equal(1, tensor.Lengths[0]); Assert.Equal(2, tensor.Lengths[1]); @@ -1534,7 +1534,7 @@ public static void TensorSqueezeTest() [Fact] public static void TensorUnsqueezeTest() { - var tensor = Tensor.Create(false, [2]); + var tensor = Tensor.Create([2], false); Assert.Equal(1, tensor.Rank); Assert.Equal(2, tensor.Lengths[0]); @@ -1543,7 +1543,7 @@ public static void TensorUnsqueezeTest() Assert.Equal(1, tensor.Lengths[0]); Assert.Equal(2, tensor.Lengths[1]); - tensor = Tensor.Create(false, [2]); + tensor = Tensor.Create([2], false); Assert.Equal(1, tensor.Rank); Assert.Equal(2, tensor.Lengths[0]); @@ -1552,7 +1552,7 @@ public static void TensorUnsqueezeTest() Assert.Equal(2, tensor.Lengths[0]); Assert.Equal(1, tensor.Lengths[1]); - tensor = Tensor.Create(false, [2]); + tensor = Tensor.Create([2], false); Assert.Equal(1, tensor.Rank); Assert.Equal(2, tensor.Lengths[0]);