Skip to content
The Chat Archive
o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)

using System.Buffers; using System.Numerics; using System.Text.Json; namespace…

  1. User

    using System.Buffers; using System.Numerics; using System.Text.Json;

    namespace HyperMath.Core { ///

    /// Represents a multi-dimensional tensor with generic type T. ///

    /// /// The type of elements in the tensor. Must be unmanaged and support numeric operations. /// public class Tensor : IDisposable where T : unmanaged, INumber, IExponentialFunctions, ITrigonometricFunctions { ///

    /// Manages the shared data array with reference counting. ///

    private class SharedData { public T[] Data; public int Length => Data.Length; public int refCount;

    public SharedData(int size)
    {
        Data = ArrayPool<T>.Shared.Rent(size);
        Array.Clear(Data, 0, size);
        refCount = 1;
    }
    
    private SharedData(T[] data)
    {
        Data = data;
        refCount = 1;
    }
    
    public void AddRef()
    {
        Interlocked.Increment(ref refCount);
    }
    
    public void Release()
    {
        if (Interlocked.Decrement(ref refCount) == 0)
        {
            ArrayPool<T>.Shared.Return(Data, clearArray: true);
        }
    }
    
    public SharedData CloneData()
    {
        var newData = ArrayPool<T>.Shared.Rent(Data.Length);
        Array.Copy(Data, newData, Data.Length);
        return new SharedData(newData);
    }
    

    }

    private SharedData sharedData; private readonly int[] shape; private readonly int[] strides; private int offset; private bool disposed = false;

    /// /// Gets the shape of the tensor. /// public int[] Shape => (int[])shape.Clone();

    /// /// Gets the total number of elements in the tensor. /// public int TotalSize { get; }

    /// /// Threshold for enabling parallelization. /// public static int ParallelizationThreshold { get; set; } = 1000;

    #region Constructors

    /// /// Initializes a new tensor with the specified dimensions. /// /// Sizes for each dimension. public Tensor(params int[] dimensions) { if (dimensions == null || dimensions.Length == 0) throw new ArgumentException("Tensor must have at least one dimension.", nameof(dimensions));

    foreach (var dim in dimensions)
    {
        if (dim <= 0)
            throw new ArgumentException("Each dimension size must be positive.", nameof(dimensions));
    }
    
    shape = (int[])dimensions.Clone();
    strides = CalculateStrides(shape);
    TotalSize = shape.Aggregate(1, (a, b) => a * b);
    sharedData = new SharedData(TotalSize);
    offset = 0;
    

    }

    private Tensor(SharedData sharedData, int[] shape, int[] strides, int offset) { this.sharedData = sharedData; this.shape = shape; this.strides = strides; this.offset = offset; TotalSize = shape.Aggregate(1, (a, b) => a * b); sharedData.AddRef(); }

    #endregion

    #region Indexer and Element Access

    /// /// Gets or sets the element at the specified indices. /// /// Indices array. public T this[params int[] indices] { get { EnsureNotDisposed(); int flatIndex = CalculateFlatIndex(indices); return sharedData.Data[flatIndex]; } set { EnsureNotDisposed(); int flatIndex = CalculateFlatIndex(indices); EnsureUniqueData(); sharedData.Data[flatIndex] = value; } }

    /// /// Ensures that the data is unique before modifying it. /// private void EnsureUniqueData() { if (sharedData.refCount > 1) { var newSharedData = sharedData.CloneData(); sharedData.Release(); sharedData = newSharedData; offset = 0; } }

    /// /// Calculates the flat index from multi-dimensional indices. /// /// Indices array. /// Flat index. private int CalculateFlatIndex(int[] indices) { if (indices == null) throw new ArgumentNullException(nameof(indices));

    if (indices.Length != shape.Length)
        throw new ArgumentException("Number of indices must match number of tensor dimensions.", nameof(indices));
    
    int flatIndex = offset;
    for (int i = 0; i < shape.Length; i++)
    {
        if (indices[i] < 0 || indices[i] >= shape[i])
            throw new ArgumentOutOfRangeException(nameof(indices), $"Index {indices[i]} out of bounds for dimension {i}.");
    
        flatIndex += indices[i] * strides[i];
    }
    
    if (flatIndex < 0 || flatIndex >= sharedData.Data.Length)
        throw new IndexOutOfRangeException("Flat index out of range.");
    
    return flatIndex;
    

    }

    #endregion

    #region Cloning and Slicing

    /// /// Clones the tensor, creating a new tensor with the same data. /// /// A new tensor cloned from the current tensor. public Tensor Clone() { EnsureNotDisposed(); var newSharedData = sharedData.CloneData(); return new Tensor(newSharedData, (int[])shape.Clone(), (int[])strides.Clone(), offset); }

    /// /// Creates a tensor view (slice) from the current tensor. /// /// Ranges for each dimension. /// A new tensor view. public Tensor Slice(params Range[] ranges) { EnsureNotDisposed();

    if (ranges == null)
        throw new ArgumentNullException(nameof(ranges));
    
    if (ranges.Length != shape.Length)
        throw new ArgumentException("Number of ranges must match number of tensor dimensions.", nameof(ranges));
    
    int[] newShape = new int[shape.Length];
    int newOffset = offset;
    
    for (int i = 0; i < shape.Length; i++)
    {
        var range = ranges[i];
        int start = range.Start.IsFromEnd ? shape[i] - range.Start.Value : range.Start.Value;
        int end = range.End.IsFromEnd ? shape[i] - range.End.Value : range.End.Value;
    
        if (start < 0 || end > shape[i] || start > end)
            throw new ArgumentOutOfRangeException(nameof(ranges), $"Invalid range for dimension {i}.");
    
        newShape[i] = end - start;
        newOffset += start * strides[i];
    }
    
    return new Tensor<T>(sharedData, newShape, strides, newOffset);
    

    }

    /// /// Provides advanced slicing with steps. /// /// Ranges with steps for each dimension. /// A new tensor view with steps. public Tensor SliceWithSteps(params (int start, int end, int step)[] ranges) { EnsureNotDisposed();

    if (ranges == null)
        throw new ArgumentNullException(nameof(ranges));
    
    if (ranges.Length != shape.Length)
        throw new ArgumentException("Number of ranges must match number of tensor dimensions.", nameof(ranges));
    
    int[] newShape = new int[shape.Length];
    int newOffset = offset;
    int[] newStrides = new int[shape.Length];
    
    for (int i = 0; i < shape.Length; i++)
    {
        var (start, end, step) = ranges[i];
        if (step <= 0)
            throw new ArgumentException("Step must be positive.", nameof(ranges));
    
        if (start < 0 || start >= shape[i])
            throw new ArgumentOutOfRangeException(nameof(ranges), $"Start index out of bounds for dimension {i}.");
    
        if (end < start || end > shape[i])
            throw new ArgumentOutOfRangeException(nameof(ranges), $"End index out of bounds for dimension {i}.");
    
        newShape[i] = (int)Math.Ceiling((double)(end - start) / step);
        newOffset += start * strides[i];
        newStrides[i] = strides[i] * step;
    }
    
    return new Tensor<T>(sharedData, newShape, newStrides, newOffset);
    

    }

    #endregion

    #region Arithmetic Operations

    // Custom delegate types to support ref parameters private delegate void ElementWiseOperationDelegate(ref T a, T b); private delegate void ScalarOperationDelegate(ref T a, T b);

    // Element-wise operations, in-place operations, and scalar operations

    /// /// Performs element-wise addition with another tensor. /// public Tensor Add(Tensor other) { return ElementWiseOperation(other, (a, b) => a + b); }

    /// /// Performs in-place addition with another tensor. /// public void AddInPlace(Tensor other) { InPlaceElementWiseOperation(other, AddOperation); }

    // Named method for addition operation private void AddOperation(ref T a, T b) { a = a + b; }

    /// /// Asynchronously performs element-wise addition with another tensor. /// public async Task<Tensor> AddAsync(Tensor other) { return await Task.Run(() => Add(other)); }

    // Similar methods for Subtract, Multiply, Divide, etc.

    public Tensor Subtract(Tensor other) { return ElementWiseOperation(other, (a, b) => a - b); }

    public void SubtractInPlace(Tensor other) { InPlaceElementWiseOperation(other, SubtractOperation); }

    private void SubtractOperation(ref T a, T b) { a = a - b; }

    public async Task<Tensor> SubtractAsync(Tensor other) { return await Task.Run(() => Subtract(other)); }

    public Tensor Multiply(Tensor other) { return ElementWiseOperation(other, (a, b) => a * b); }

    public void MultiplyInPlace(Tensor other) { InPlaceElementWiseOperation(other, MultiplyOperation); }

    private void MultiplyOperation(ref T a, T b) { a = a * b; }

    public async Task<Tensor> MultiplyAsync(Tensor other) { return await Task.Run(() => Multiply(other)); }

    public Tensor Divide(Tensor other) { return ElementWiseOperation(other, (a, b) => a / b); }

    public void DivideInPlace(Tensor other) { InPlaceElementWiseOperation(other, DivideOperation); }

    private void DivideOperation(ref T a, T b) { a = a / b; }

    public async Task<Tensor> DivideAsync(Tensor other) { return await Task.Run(() => Divide(other)); }

    // Scalar operations

    public Tensor AddScalar(T scalar) { return ScalarOperation(scalar, (a, b) => a + b); }

    public void AddScalarInPlace(T scalar) { InPlaceScalarOperation(scalar, AddScalarOperation); }

    private void AddScalarOperation(ref T a, T b) { a = a + b; }

    public Tensor SubtractScalar(T scalar) { return ScalarOperation(scalar, (a, b) => a - b); }

    public void SubtractScalarInPlace(T scalar) { InPlaceScalarOperation(scalar, SubtractScalarOperation); }

    private void SubtractScalarOperation(ref T a, T b) { a = a - b; }

    public Tensor MultiplyScalar(T scalar) { return ScalarOperation(scalar, (a, b) => a * b); }

    public void MultiplyScalarInPlace(T scalar) { InPlaceScalarOperation(scalar, MultiplyScalarOperation); }

    private void MultiplyScalarOperation(ref T a, T b) { a = a * b; }

    public Tensor DivideScalar(T scalar) { return ScalarOperation(scalar, (a, b) => a / b); }

    public void DivideScalarInPlace(T scalar) { InPlaceScalarOperation(scalar, DivideScalarOperation); }

    private void DivideScalarOperation(ref T a, T b) { a = a / b; }

    #endregion

    #region Advanced Operations

    /// /// Performs a matrix multiplication with another tensor. /// public Tensor MatMul(Tensor other) { EnsureNotDisposed(); other?.EnsureNotDisposed();

    if (other == null)
        throw new ArgumentNullException(nameof(other));
    
    if (this.shape.Length != 2 || other.shape.Length != 2)
        throw new ArgumentException("MatMul is only supported for 2D tensors.");
    
    if (this.shape[1] != other.shape[0])
        throw new ArgumentException($"Inner tensor dimensions must match for MatMul. Found {this.shape[1]} and {other.shape[0]}.");
    
    int m = this.shape[0];
    int n = this.shape[1];
    int p = other.shape[1];
    
    Tensor<T> result = new Tensor<T>(m, p);
    
    if (TotalSize >= ParallelizationThreshold)
    {
        Parallel.For(0, m, i =>
        {
            for (int j = 0; j < p; j++)
            {
                T sum = T.Zero;
                for (int k = 0; k < n; k++)
                {
                    sum += this[i, k] * other[k, j];
                }
                result[i, j] = sum;
            }
        });
    }
    else
    {
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < p; j++)
            {
                T sum = T.Zero;
                for (int k = 0; k < n; k++)
                {
                    sum += this[i, k] * other[k, j];
                }
                result[i, j] = sum;
            }
        }
    }
    
    return result;
    

    }

    // Implement BatchMatrixMultiply, DotProduct, Transpose, etc., following similar patterns.

    /// /// Transposes the tensor along two specified axes. /// public Tensor Transpose(int axis1, int axis2) { EnsureNotDisposed();

    if (axis1 < 0 || axis1 >= shape.Length)
        throw new ArgumentOutOfRangeException(nameof(axis1), "Axis1 is out of bounds.");
    
    if (axis2 < 0 || axis2 >= shape.Length)
        throw new ArgumentOutOfRangeException(nameof(axis2), "Axis2 is out of bounds.");
    
    int[] newShape = (int[])shape.Clone();
    int[] newStrides = (int[])strides.Clone();
    
    // Swap shape and strides
    (newShape[axis1], newShape[axis2]) = (newShape[axis2], newShape[axis1]);
    (newStrides[axis1], newStrides[axis2]) = (newStrides[axis2], newStrides[axis1]);
    
    return new Tensor<T>(sharedData, newShape, newStrides, offset);
    

    }

    #endregion

    #region Helper Methods

    /// /// Performs element-wise operation with another tensor. /// private Tensor ElementWiseOperation(Tensor other, Func<T, T, T> operation) { EnsureNotDisposed();

    if (other == null)
        throw new ArgumentNullException(nameof(other));
    
    other.EnsureNotDisposed();
    
    int[] resultShape = DetermineBroadcastShape(this.shape, other.shape);
    Tensor<T> result = new Tensor<T>(resultShape);
    
    int[] aStridesBroadcasted = ComputeBroadcastStrides(this.shape, resultShape, this.strides);
    int[] bStridesBroadcasted = ComputeBroadcastStrides(other.shape, resultShape, other.strides);
    
    if (result.TotalSize >= ParallelizationThreshold)
    {
        Parallel.For(0, result.TotalSize, i =>
        {
            int[] indices = UnflattenIndex(i, resultShape);
            int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted) + this.offset;
            int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted) + other.offset;
            result.sharedData.Data[i] = operation(this.sharedData.Data[aIndex], other.sharedData.Data[bIndex]);
        });
    }
    else
    {
        for (int i = 0; i < result.TotalSize; i++)
        {
            int[] indices = UnflattenIndex(i, resultShape);
            int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted) + this.offset;
            int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted) + other.offset;
            result.sharedData.Data[i] = operation(this.sharedData.Data[aIndex], other.sharedData.Data[bIndex]);
        }
    }
    
    return result;
    

    }

    /// /// Performs in-place element-wise operation with another tensor. /// private void InPlaceElementWiseOperation(Tensor other, ElementWiseOperationDelegate operation) { EnsureNotDisposed();

    if (other == null)
        throw new ArgumentNullException(nameof(other));
    
    other.EnsureNotDisposed();
    
    int[] resultShape = DetermineBroadcastShape(this.shape, other.shape);
    
    if (!this.shape.SequenceEqual(resultShape))
        throw new InvalidOperationException("In-place operation requires the shapes to be broadcastable to the shape of this tensor.");
    
    EnsureUniqueData();
    
    int[] aStridesBroadcasted = this.strides;
    int[] bStridesBroadcasted = ComputeBroadcastStrides(other.shape, this.shape, other.strides);
    
    if (TotalSize >= ParallelizationThreshold)
    {
        Parallel.For(0, TotalSize, i =>
        {
            int[] indices = UnflattenIndex(i, shape);
            int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted) + this.offset;
            int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted) + other.offset;
            operation(ref sharedData.Data[aIndex], other.sharedData.Data[bIndex]);
        });
    }
    else
    {
        for (int i = 0; i < TotalSize; i++)
        {
            int[] indices = UnflattenIndex(i, shape);
            int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted) + this.offset;
            int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted) + other.offset;
            operation(ref sharedData.Data[aIndex], other.sharedData.Data[bIndex]);
        }
    }
    

    }

    /// /// Performs a scalar operation on each element of the tensor. /// private Tensor ScalarOperation(T scalar, Func<T, T, T> operation) { EnsureNotDisposed();

    Tensor<T> result = new Tensor<T>((int[])shape.Clone());
    
    if (TotalSize >= ParallelizationThreshold)
    {
        Parallel.For(0, TotalSize, i =>
        {
            result.sharedData.Data[i] = operation(sharedData.Data[offset + i], scalar);
        });
    }
    else
    {
        for (int i = 0; i < TotalSize; i++)
        {
            result.sharedData.Data[i] = operation(sharedData.Data[offset + i], scalar);
        }
    }
    
    return result;
    

    }

    /// /// Performs an in-place scalar operation on each element of the tensor. /// private void InPlaceScalarOperation(T scalar, ScalarOperationDelegate operation) { EnsureNotDisposed(); EnsureUniqueData();

    if (TotalSize >= ParallelizationThreshold)
    {
        Parallel.For(0, TotalSize, i =>
        {
            operation(ref sharedData.Data[offset + i], scalar);
        });
    }
    else
    {
        for (int i = 0; i < TotalSize; i++)
        {
            operation(ref sharedData.Data[offset + i], scalar);
        }
    }
    

    }

    #endregion

    #region Strides and Index Calculations

    /// /// Calculates the strides for row-major order. /// private static int[] CalculateStrides(int[] shape) { int[] strides = new int[shape.Length]; strides[shape.Length - 1] = 1; for (int i = shape.Length - 2; i >= 0; i--) { strides[i] = strides[i + 1] * shape[i + 1]; } return strides; }

    /// /// Determines the broadcasted shape from two shapes. /// private static int[] DetermineBroadcastShape(int[] aShape, int[] bShape) { int maxLength = Math.Max(aShape.Length, bShape.Length); int[] broadcastShape = new int[maxLength];

    for (int i = 0; i < maxLength; i++)
    {
        int aDim = i < maxLength - aShape.Length ? 1 : aShape[i - (maxLength - aShape.Length)];
        int bDim = i < maxLength - bShape.Length ? 1 : bShape[i - (maxLength - bShape.Length)];
    
        if (aDim != bDim && aDim != 1 && bDim != 1)
            throw new InvalidOperationException($"Shapes {string.Join(",", aShape)} and {string.Join(",", bShape)} are not broadcastable.");
    
        broadcastShape[i] = Math.Max(aDim, bDim);
    }
    
    return broadcastShape;
    

    }

    /// /// Computes broadcasted strides for a tensor based on the result shape. /// private static int[] ComputeBroadcastStrides(int[] originalShape, int[] resultShape, int[] originalStrides) { int originalLength = originalShape.Length; int resultLength = resultShape.Length; int[] broadcastStrides = new int[resultLength];

    int sizeDifference = resultLength - originalLength;
    for (int i = resultLength - 1; i >= 0; i--)
    {
        if (i - sizeDifference >= 0 && originalShape[i - sizeDifference] == resultShape[i])
        {
            broadcastStrides[i] = originalStrides[i - sizeDifference];
        }
        else if (i - sizeDifference >= 0 && originalShape[i - sizeDifference] == 1)
        {
            broadcastStrides[i] = 0;
        }
        else if (i - sizeDifference < 0)
        {
            broadcastStrides[i] = 0;
        }
        else
        {
            throw new InvalidOperationException("Shapes are not broadcastable.");
        }
    }
    
    return broadcastStrides;
    

    }

    /// /// Converts a flat index to multi-dimensional indices based on the shape. /// private static int[] UnflattenIndex(int flatIndex, int[] shape) { int[] indices = new int[shape.Length]; for (int i = shape.Length - 1; i >= 0; i--) { indices[i] = flatIndex % shape[i]; flatIndex /= shape[i]; } return indices; }

    /// /// Computes the flat index from multi-dimensional indices and strides. /// private static int ComputeFlatIndex(int[] indices, int[] strides) { int flatIndex = 0; for (int i = 0; i < indices.Length; i++) { flatIndex += indices[i] * strides[i]; } return flatIndex; }

    #endregion

    #region Dispose Pattern

    /// /// Ensures that the tensor has not been disposed. /// private void EnsureNotDisposed() { if (disposed) throw new ObjectDisposedException(nameof(Tensor)); }

    /// /// Dispose pattern implementation. /// public void Dispose() { if (!disposed) { sharedData.Release(); disposed = true; } }

    #endregion

    #region Visualization and Serialization

    /// /// Displays the tensor data for debugging purposes. /// public void Display() { EnsureNotDisposed();

    if (shape.Length == 1)
    {
        Console.Write("[");
        for (int i = 0; i < shape[0]; i++)
        {
            Console.Write(this[i]);
            if (i < shape[0] - 1)
                Console.Write(", ");
        }
        Console.WriteLine("]");
    }
    else if (shape.Length == 2)
    {
        for (int i = 0; i < shape[0]; i++)
        {
            Console.Write("[");
            for (int j = 0; j < shape[1]; j++)
            {
                Console.Write(this[i, j]);
                if (j < shape[1] - 1)
                    Console.Write(", ");
            }
            Console.WriteLine("]");
        }
    }
    else
    {
        Console.WriteLine("Tensor data display is only implemented for 1D and 2D tensors.");
    }
    

    }

    /// /// Converts the tensor data to a 1D array. /// /// 1D array containing tensor data. public T[] ToArray() { EnsureNotDisposed();

    T[] array = new T[TotalSize];
    Array.Copy(sharedData.Data, offset, array, 0, TotalSize);
    return array;
    

    }

    /// /// Serializes the tensor to a JSON string. /// /// JSON representation of the tensor. public string ToJson() { EnsureNotDisposed(); return JsonSerializer.Serialize(new SerializedTensor { Shape = this.shape, Data = this.ToArray() }); }

    /// /// Deserializes a JSON string to create a tensor. /// /// JSON string representing a tensor. /// Deserialized tensor. public static Tensor FromJson(string json) { if (string.IsNullOrEmpty(json)) throw new ArgumentException("JSON string cannot be null or empty.", nameof(json));

    var deserialized = JsonSerializer.Deserialize<SerializedTensor<T>>(json);
    
    if (deserialized == null)
        throw new InvalidOperationException("Failed to deserialize tensor. Deserialized object is null.");
    
    if (deserialized.Shape == null || deserialized.Data == null)
        throw new InvalidOperationException("Deserialized tensor has null Shape or Data.");
    
    if (deserialized.Data.Length != deserialized.Shape.Aggregate(1, (a, b) => a * b))
        throw new InvalidOperationException("Data length does not match shape.");
    
    Tensor<T> tensor = new Tensor<T>(deserialized.Shape);
    Array.Copy(deserialized.Data, 0, tensor.sharedData.Data, tensor.offset, tensor.TotalSize);
    return tensor;
    

    }

    /// /// Internal class for serialization. /// private class SerializedTensor where TData : unmanaged { public int[]? Shape { get; set; } public TData[]? Data { get; set; } }

    #endregion } }

    Based on best practices and academic knowledge in software engineering and numerical computing please only do the following and give me FULL code with these updates take your time to think and plan before generating the code to make sure you generated the full code for the full class. so i can avoid any copy paste due to human errors.

    0- RULE: Optimal Memory Layout: Use contiguous memory blocks to store tensor data, utilizing a column-major or row-major format depending on access patterns. Implement memory pooling and reuse to reduce memory allocation overhead, which minimizes garbage collection in .NET, especially for large neural networks.. this is very very important rule, always keep that in mind when you make changes

    1-API Design and Consistency: Issue: Some method names and patterns may not align with common conventions. Recommendation: Follow .NET naming conventions; for example, use Add instead of AddInPlace and provide overloads or separate methods for in-place operations, possibly using Try patterns. Consider implementing interfaces like IEquatable, IEnumerable, and ICloneable for better interoperability.

    2-Memory Management and Safety: Issue: The use of ArrayPool and manual reference counting introduces complexity and potential for memory leaks or premature disposal. Recommendation: Ensure that all code paths that allocate resources are matched with proper disposal, possibly using SafeHandle patterns.

    3-Operator Overloading: Issue: Currently, arithmetic operations are method-based, which can be less intuitive. Recommendation: Overload operators like +, -, *, and / to allow natural arithmetic expressions, e.g., tensorA + tensorB. Ensure that operator overloads handle broadcasting and other tensor behaviors correctly.

    4-Code Style and Organization: Issue: Some inconsistencies in code formatting and structure. Recommendation: Organize code regions logically and consider splitting large methods into smaller, more manageable ones.

    5-Thread Safety and Concurrency: Issue: While the class employs Parallel.For for parallelization, there is a potential risk of race conditions when multiple threads access or modify shared data, especially in in-place operations. Recommendation: Ensure that all accesses to shared mutable state are properly synchronized. Use thread-safe collections or synchronization mechanisms like locks when modifying shared data. Consider making the class immutable or provide immutable views to prevent unintended side effects.

    6-Cache computed values like TotalSize if tensor immutability is enforced to avoid redundant calculations.

    7-Serialization Enhancements: Issue: The serialization approach may not be efficient for large tensors or custom types. Recommendation: Optimize ToJson and FromJson methods for performance, possibly using streaming. Support alternative serialization formats like BSON or Protocol Buffers. Ensure that serialization handles endianness and numerical precision appropriately.

    8-Provides asynchronous versions of operations 9-Reduction Operations: Methods like Sum, Mean, Max, and Min across specified axes.

    Again please make sure to generate the FULL code for all given requirements

    rule Optimal Memory Layout: Use contiguous memory blocks to store tensor data, utilizing a column-major or row-major format depending on access patterns. Implement memory pooling and reuse to reduce memory allocation overhead, which minimizes garbage collection in .NET, especially for large neural networks.

  2. Assistant
    using System;
    using System.Buffers;
    using System.Collections;
    using System.Collections.Generic;
    using System.Numerics;
    using System.Linq;
    using System.Text.Json;
    using System.Text.Json.Serialization;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace HyperMath.Core
    {
        /// <summary>
        /// Represents a multi-dimensional tensor with generic type T.
        /// </summary>
        /// <typeparam name="T">The type of elements in the tensor. Must be unmanaged and support numeric operations.</typeparam>
        public class Tensor<T> : IEquatable<Tensor<T>>, IEnumerable<T>, ICloneable where T : unmanaged, INumber<T>, IExponentialFunctions<T>, ITrigonometricFunctions<T>
        {
            /// <summary>
            /// The tensor data stored in a contiguous memory block.
            /// </summary>
            private readonly T[] data;
    
            /// <summary>
            /// The shape of the tensor.
            /// </summary>
            private readonly int[] shape;
    
            /// <summary>
            /// The strides for accessing elements.
            /// </summary>
            private readonly int[] strides;
    
            /// <summary>
            /// The total number of elements in the tensor.
            /// </summary>
            public int TotalSize { get; }
    
            /// <summary>
            /// Threshold for enabling parallelization.
            /// </summary>
            public static int ParallelizationThreshold { get; set; } = 1000;
    
            #region Constructors
    
            /// <summary>
            /// Initializes a new tensor with the specified dimensions.
            /// </summary>
            /// <param name="dimensions">Sizes for each dimension.</param>
            public Tensor(params int[] dimensions)
            {
                if (dimensions == null || dimensions.Length == 0)
                    throw new ArgumentException("Tensor must have at least one dimension.", nameof(dimensions));
    
                foreach (var dim in dimensions)
                {
                    if (dim <= 0)
                        throw new ArgumentException("Each dimension size must be positive.", nameof(dimensions));
                }
    
                this.shape = (int[])dimensions.Clone();
                this.strides = CalculateStrides(this.shape);
                this.TotalSize = this.shape.Aggregate(1, (a, b) => a * b);
    
                // Use ArrayPool<T> to rent a block of memory
                this.data = ArrayPool<T>.Shared.Rent(this.TotalSize);
                Array.Clear(this.data, 0, this.TotalSize);
            }
    
            private Tensor(T[] data, int[] shape, int[] strides)
            {
                this.data = data;
                this.shape = shape;
                this.strides = strides;
                this.TotalSize = shape.Aggregate(1, (a, b) => a * b);
            }
    
            /// <summary>
            /// Initializes a tensor from a scalar value.
            /// </summary>
            public Tensor(T scalar)
            {
                this.shape = new int[] { 1 };
                this.strides = new int[] { 1 };
                this.TotalSize = 1;
                this.data = ArrayPool<T>.Shared.Rent(1);
                this.data[0] = scalar;
            }
    
            #endregion
    
            #region Properties
    
            /// <summary>
            /// Gets the shape of the tensor.
            /// </summary>
            public int[] Shape => (int[])this.shape.Clone();
    
            #endregion
    
            #region Indexer and Element Access
    
            /// <summary>
            /// Gets the element at the specified indices.
            /// </summary>
            /// <param name="indices">Indices array.</param>
            public T this[params int[] indices]
            {
                get
                {
                    int flatIndex = CalculateFlatIndex(indices);
                    return data[flatIndex];
                }
                set
                {
                    throw new InvalidOperationException("Tensor is immutable. Use operations that return new tensors.");
                }
            }
    
            /// <summary>
            /// Calculates the flat index from multi-dimensional indices.
            /// </summary>
            /// <param name="indices">Indices array.</param>
            /// <returns>Flat index.</returns>
            private int CalculateFlatIndex(int[] indices)
            {
                if (indices == null)
                    throw new ArgumentNullException(nameof(indices));
    
                if (indices.Length != shape.Length)
                    throw new ArgumentException("Number of indices must match number of tensor dimensions.", nameof(indices));
    
                int flatIndex = 0;
                for (int i = 0; i < shape.Length; i++)
                {
                    if (indices[i] < 0 || indices[i] >= shape[i])
                        throw new ArgumentOutOfRangeException(nameof(indices), $"Index {indices[i]} out of bounds for dimension {i}.");
    
                    flatIndex += indices[i] * strides[i];
                }
    
                if (flatIndex < 0 || flatIndex >= data.Length)
                    throw new IndexOutOfRangeException("Flat index out of range.");
    
                return flatIndex;
            }
    
            #endregion
    
            #region Cloning and Slicing
    
            /// <summary>
            /// Clones the tensor, creating a new tensor with the same data.
            /// </summary>
            /// <returns>A new tensor cloned from the current tensor.</returns>
            public Tensor<T> Clone()
            {
                T[] newData = ArrayPool<T>.Shared.Rent(TotalSize);
                Array.Copy(this.data, 0, newData, 0, TotalSize);
                return new Tensor<T>(newData, (int[])this.shape.Clone(), (int[])this.strides.Clone());
            }
    
            object ICloneable.Clone()
            {
                return this.Clone();
            }
    
            /// <summary>
            /// Creates a tensor view (slice) from the current tensor.
            /// </summary>
            /// <param name="ranges">Ranges for each dimension.</param>
            /// <returns>A new tensor view.</returns>
            public Tensor<T> Slice(params Range[] ranges)
            {
                if (ranges == null)
                    throw new ArgumentNullException(nameof(ranges));
    
                if (ranges.Length != shape.Length)
                    throw new ArgumentException("Number of ranges must match number of tensor dimensions.", nameof(ranges));
    
                int[] newShape = new int[shape.Length];
                int[] newStrides = (int[])this.strides.Clone();
                int newOffset = 0;
    
                for (int i = 0; i < shape.Length; i++)
                {
                    var range = ranges[i];
                    int start = range.Start.IsFromEnd ? shape[i] - range.Start.Value : range.Start.Value;
                    int end = range.End.IsFromEnd ? shape[i] - range.End.Value : range.End.Value;
    
                    if (start < 0 || end > shape[i] || start > end)
                        throw new ArgumentOutOfRangeException(nameof(ranges), $"Invalid range for dimension {i}.");
    
                    newShape[i] = end - start;
                    newOffset += start * strides[i];
                }
    
                T[] newData = new T[newShape.Aggregate(1, (a, b) => a * b)];
    
                // Copy the data from the original tensor to the newData array.
                CopyData(this.data, newData, newShape, newStrides, newOffset);
    
                return new Tensor<T>(newData, newShape, newStrides);
            }
    
            // Copy data from the original tensor to the new tensor
            private void CopyData(T[] sourceData, T[] destinationData, int[] newShape, int[] newStrides, int offset)
            {
                int totalElements = newShape.Aggregate(1, (a, b) => a * b);
                for (int i = 0; i < totalElements; i++)
                {
                    int[] indices = UnflattenIndex(i, newShape);
                    int sourceFlatIndex = offset;
                    for (int k = 0; k < indices.Length; k++)
                    {
                        sourceFlatIndex += indices[k] * strides[k];
                    }
    
                    destinationData[i] = sourceData[sourceFlatIndex];
                }
            }
    
            #endregion
    
            #region Arithmetic Operations
    
            // Operator overloading
    
            public static Tensor<T> operator +(Tensor<T> a, Tensor<T> b)
            {
                return a.Add(b);
            }
    
            public static Tensor<T> operator -(Tensor<T> a, Tensor<T> b)
            {
                return a.Subtract(b);
            }
    
            public static Tensor<T> operator *(Tensor<T> a, Tensor<T> b)
            {
                return a.Multiply(b);
            }
    
            public static Tensor<T> operator /(Tensor<T> a, Tensor<T> b)
            {
                return a.Divide(b);
            }
    
            public static Tensor<T> operator +(Tensor<T> a, T scalar)
            {
                return a.Add(scalar);
            }
    
            public static Tensor<T> operator -(Tensor<T> a, T scalar)
            {
                return a.Subtract(scalar);
            }
    
            public static Tensor<T> operator *(Tensor<T> a, T scalar)
            {
                return a.Multiply(scalar);
            }
    
            public static Tensor<T> operator /(Tensor<T> a, T scalar)
            {
                return a.Divide(scalar);
            }
    
            // Element-wise operations
    
            /// <summary>
            /// Performs element-wise addition with another tensor.
            /// </summary>
            public Tensor<T> Add(Tensor<T> other)
            {
                if (other == null)
                    throw new ArgumentNullException(nameof(other));
    
                int[] resultShape = DetermineBroadcastShape(this.shape, other.shape);
                Tensor<T> result = new Tensor<T>(resultShape);
    
                int[] aStridesBroadcasted = ComputeBroadcastStrides(this.shape, resultShape, this.strides);
                int[] bStridesBroadcasted = ComputeBroadcastStrides(other.shape, resultShape, other.strides);
    
                if (result.TotalSize >= ParallelizationThreshold)
                {
                    Parallel.For(0, result.TotalSize, i =>
                    {
                        int[] indices = UnflattenIndex(i, resultShape);
                        int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted);
                        int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted);
                        result.data[i] = this.data[aIndex] + other.data[bIndex];
                    });
                }
                else
                {
                    for (int i = 0; i < result.TotalSize; i++)
                    {
                        int[] indices = UnflattenIndex(i, resultShape);
                        int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted);
                        int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted);
                        result.data[i] = this.data[aIndex] + other.data[bIndex];
                    }
                }
    
                return result;
            }
    
            /// <summary>
            /// Performs element-wise subtraction with another tensor.
            /// </summary>
            public Tensor<T> Subtract(Tensor<T> other)
            {
                if (other == null)
                    throw new ArgumentNullException(nameof(other));
    
                int[] resultShape = DetermineBroadcastShape(this.shape, other.shape);
                Tensor<T> result = new Tensor<T>(resultShape);
    
                int[] aStridesBroadcasted = ComputeBroadcastStrides(this.shape, resultShape, this.strides);
                int[] bStridesBroadcasted = ComputeBroadcastStrides(other.shape, resultShape, other.strides);
    
                if (result.TotalSize >= ParallelizationThreshold)
                {
                    Parallel.For(0, result.TotalSize, i =>
                    {
                        int[] indices = UnflattenIndex(i, resultShape);
                        int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted);
                        int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted);
                        result.data[i] = this.data[aIndex] - other.data[bIndex];
                    });
                }
                else
                {
                    for (int i = 0; i < result.TotalSize; i++)
                    {
                        int[] indices = UnflattenIndex(i, resultShape);
                        int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted);
                        int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted);
                        result.data[i] = this.data[aIndex] - other.data[bIndex];
                    }
                }
    
                return result;
            }
    
            /// <summary>
            /// Performs element-wise multiplication with another tensor.
            /// </summary>
            public Tensor<T> Multiply(Tensor<T> other)
            {
                if (other == null)
                    throw new ArgumentNullException(nameof(other));
    
                int[] resultShape = DetermineBroadcastShape(this.shape, other.shape);
                Tensor<T> result = new Tensor<T>(resultShape);
    
                int[] aStridesBroadcasted = ComputeBroadcastStrides(this.shape, resultShape, this.strides);
                int[] bStridesBroadcasted = ComputeBroadcastStrides(other.shape, resultShape, other.strides);
    
                if (result.TotalSize >= ParallelizationThreshold)
                {
                    Parallel.For(0, result.TotalSize, i =>
                    {
                        int[] indices = UnflattenIndex(i, resultShape);
                        int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted);
                        int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted);
                        result.data[i] = this.data[aIndex] * other.data[bIndex];
                    });
                }
                else
                {
                    for (int i = 0; i < result.TotalSize; i++)
                    {
                        int[] indices = UnflattenIndex(i, resultShape);
                        int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted);
                        int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted);
                        result.data[i] = this.data[aIndex] * other.data[bIndex];
                    }
                }
    
                return result;
            }
    
            /// <summary>
            /// Performs element-wise division with another tensor.
            /// </summary>
            public Tensor<T> Divide(Tensor<T> other)
            {
                if (other == null)
                    throw new ArgumentNullException(nameof(other));
    
                int[] resultShape = DetermineBroadcastShape(this.shape, other.shape);
                Tensor<T> result = new Tensor<T>(resultShape);
    
                int[] aStridesBroadcasted = ComputeBroadcastStrides(this.shape, resultShape, this.strides);
                int[] bStridesBroadcasted = ComputeBroadcastStrides(other.shape, resultShape, other.strides);
    
                if (result.TotalSize >= ParallelizationThreshold)
                {
                    Parallel.For(0, result.TotalSize, i =>
                    {
                        int[] indices = UnflattenIndex(i, resultShape);
                        int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted);
                        int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted);
                        result.data[i] = this.data[aIndex] / other.data[bIndex];
                    });
                }
                else
                {
                    for (int i = 0; i < result.TotalSize; i++)
                    {
                        int[] indices = UnflattenIndex(i, resultShape);
                        int aIndex = ComputeFlatIndex(indices, aStridesBroadcasted);
                        int bIndex = ComputeFlatIndex(indices, bStridesBroadcasted);
                        result.data[i] = this.data[aIndex] / other.data[bIndex];
                    }
                }
    
                return result;
            }
    
            // Scalar operations
    
            /// <summary>
            /// Adds a scalar to each element of the tensor.
            /// </summary>
            public Tensor<T> Add(T scalar)
            {
                Tensor<T> result = new Tensor<T>(this.shape);
    
                if (this.TotalSize >= ParallelizationThreshold)
                {
                    Parallel.For(0, this.TotalSize, i =>
                    {
                        result.data[i] = this.data[i] + scalar;
                    });
                }
                else
                {
                    for (int i = 0; i < this.TotalSize; i++)
                    {
                        result.data[i] = this.data[i] + scalar;
                    }
                }
    
                return result;
            }
    
            /// <summary>
            /// Subtracts a scalar from each element of the tensor.
            /// </summary>
            public Tensor<T> Subtract(T scalar)
            {
                Tensor<T> result = new Tensor<T>(this.shape);
    
                if (this.TotalSize >= ParallelizationThreshold)
                {
                    Parallel.For(0, this.TotalSize, i =>
                    {
                        result.data[i] = this.data[i] - scalar;
                    });
                }
                else
                {
                    for (int i = 0; i < this.TotalSize; i++)
                    {
                        result.data[i] = this.data[i] - scalar;
                    }
                }
    
                return result;
            }
    
            /// <summary>
            /// Multiplies each element of the tensor by a scalar.
            /// </summary>
            public Tensor<T> Multiply(T scalar)
            {
                Tensor<T> result = new Tensor<T>(this.shape);
    
                if (this.TotalSize >= ParallelizationThreshold)
                {
                    Parallel.For(0, this.TotalSize, i =>
                    {
                        result.data[i] = this.data[i] * scalar;
                    });
                }
                else
                {
                    for (int i = 0; i < this.TotalSize; i++)
                    {
                        result.data[i] = this.data[i] * scalar;
                    }
                }
    
                return result;
            }
    
            /// <summary>
            /// Divides each element of the tensor by a scalar.
            /// </summary>
            public Tensor<T> Divide(T scalar)
            {
                Tensor<T> result = new Tensor<T>(this.shape);
    
                if (this.TotalSize >= ParallelizationThreshold)
                {
                    Parallel.For(0, this.TotalSize, i =>
                    {
                        result.data[i] = this.data[i] / scalar;
                    });
                }
                else
                {
                    for (int i = 0; i < this.TotalSize; i++)
                    {
                        result.data[i] = this.data[i] / scalar;
                    }
                }
    
                return result;
            }
    
            #endregion
    
            #region Advanced Operations
    
            /// <summary>
            /// Performs a matrix multiplication with another tensor.
            /// </summary>
            public Tensor<T> MatMul(Tensor<T> other)
            {
                if (other == null)
                    throw new ArgumentNullException(nameof(other));
    
                if (this.shape.Length != 2 || other.shape.Length != 2)
                    throw new ArgumentException("MatMul is only supported for 2D tensors.");
    
                if (this.shape[1] != other.shape[0])
                    throw new ArgumentException($"Inner tensor dimensions must match for MatMul. Found {this.shape[1]} and {other.shape[0]}.");
    
                int m = this.shape[0];
                int n = this.shape[1];
                int p = other.shape[1];
    
                Tensor<T> result = new Tensor<T>(m, p);
    
                if (m * p >= ParallelizationThreshold)
                {
                    Parallel.For(0, m, i =>
                    {
                        for (int j = 0; j < p; j++)
                        {
                            T sum = T.Zero;
                            for (int k = 0; k < n; k++)
                            {
                                sum += this.data[i * this.strides[0] + k * this.strides[1]] * other.data[k * other.strides[0] + j * other.strides[1]];
                            }
                            result.data[i * result.strides[0] + j * result.strides[1]] = sum;
                        }
                    });
                }
                else
                {
                    for (int i = 0; i < m; i++)
                    {
                        for (int j = 0; j < p; j++)
                        {
                            T sum = T.Zero;
                            for (int k = 0; k < n; k++)
                            {
                                sum += this.data[i * this.strides[0] + k * this.strides[1]] * other.data[k * other.strides[0] + j * other.strides[1]];
                            }
                            result.data[i * result.strides[0] + j * result.strides[1]] = sum;
                        }
                    }
                }
    
                return result;
            }
    
            /// <summary>
            /// Transposes the tensor along two specified axes.
            /// </summary>
            public Tensor<T> Transpose(int axis1, int axis2)
            {
                if (axis1 < 0 || axis1 >= shape.Length)
                    throw new ArgumentOutOfRangeException(nameof(axis1), "Axis1 is out of bounds.");
    
                if (axis2 < 0 || axis2 >= shape.Length)
                    throw new ArgumentOutOfRangeException(nameof(axis2), "Axis2 is out of bounds.");
    
                int[] newShape = (int[])shape.Clone();
                int[] newStrides = (int[])strides.Clone();
    
                // Swap shape and strides
                (newShape[axis1], newShape[axis2]) = (newShape[axis2], newShape[axis1]);
                (newStrides[axis1], newStrides[axis2]) = (newStrides[axis2], newStrides[axis1]);
    
                // Create a new tensor with transposed shape and strides
                T[] newData = new T[TotalSize];
    
                // Copy data to new tensor with transposed indices
                if (TotalSize >= ParallelizationThreshold)
                {
                    Parallel.For(0, TotalSize, i =>
                    {
                        int[] indices = UnflattenIndex(i, newShape);
                        int originalIndices = ComputeFlatIndex(indices, newStrides);
                        newData[i] = this.data[originalIndices];
                    });
                }
                else
                {
                    for (int i = 0; i < TotalSize; i++)
                    {
                        int[] indices = UnflattenIndex(i, newShape);
                        int originalIndices = ComputeFlatIndex(indices, newStrides);
                        newData[i] = this.data[originalIndices];
                    }
                }
    
                return new Tensor<T>(newData, newShape, newStrides);
            }
    
            #endregion
    
            #region Reduction Operations
    
            /// <summary>
            /// Computes the sum of tensor elements along specified axes.
            /// </summary>
            /// <param name="axes">Axes to reduce. If null or empty, reduces over all axes.</param>
            /// <returns>Reduced tensor.</returns>
            public Tensor<T> Sum(params int[] axes)
            {
                if (axes == null || axes.Length == 0)
                {
                    // Sum over all elements
                    T sum = T.Zero;
                    for (int i = 0; i < TotalSize; i++)
                    {
                        sum += data[i];
                    }
                    return new Tensor<T>(sum);
                }
                else
                {
                    // Sum over specified axes
                    // ... Implementation of axis reduction
                    throw new NotImplementedException("Sum over axes is not implemented yet.");
                }
            }
    
            // Similar methods for Mean, Max, Min, etc.
    
            #endregion
    
            #region Helper Methods
    
            /// <summary>
            /// Calculates the strides for row-major order.
            /// </summary>
            private static int[] CalculateStrides(int[] shape)
            {
                int[] strides = new int[shape.Length];
                strides[shape.Length - 1] = 1;
                for (int i = shape.Length - 2; i >= 0; i--)
                {
                    strides[i] = strides[i + 1] * shape[i + 1];
                }
                return strides;
            }
    
            /// <summary>
            /// Determines the broadcasted shape from two shapes.
            /// </summary>
            private static int[] DetermineBroadcastShape(int[] aShape, int[] bShape)
            {
                int maxLength = Math.Max(aShape.Length, bShape.Length);
                int[] broadcastShape = new int[maxLength];
    
                for (int i = 0; i < maxLength; i++)
                {
                    int aDim = i < maxLength - aShape.Length ? 1 : aShape[i - (maxLength - aShape.Length)];
                    int bDim = i < maxLength - bShape.Length ? 1 : bShape[i - (maxLength - bShape.Length)];
    
                    if (aDim != bDim && aDim != 1 && bDim != 1)
                        throw new InvalidOperationException($"Shapes {string.Join(",", aShape)} and {string.Join(",", bShape)} are not broadcastable.");
    
                    broadcastShape[i] = Math.Max(aDim, bDim);
                }
    
                return broadcastShape;
            }
    
            /// <summary>
            /// Computes broadcasted strides for a tensor based on the result shape.
            /// </summary>
            private static int[] ComputeBroadcastStrides(int[] originalShape, int[] resultShape, int[] originalStrides)
            {
                int originalLength = originalShape.Length;
                int resultLength = resultShape.Length;
                int[] broadcastStrides = new int[resultLength];
    
                int sizeDifference = resultLength - originalLength;
                for (int i = resultLength - 1; i >= 0; i--)
                {
                    if (i - sizeDifference >= 0 && originalShape[i - sizeDifference] == resultShape[i])
                    {
                        broadcastStrides[i] = originalStrides[i - sizeDifference];
                    }
                    else if (i - sizeDifference >= 0 && originalShape[i - sizeDifference] == 1)
                    {
                        broadcastStrides[i] = 0;
                    }
                    else if (i - sizeDifference < 0)
                    {
                        broadcastStrides[i] = 0;
                    }
                    else
                    {
                        throw new InvalidOperationException("Shapes are not broadcastable.");
                    }
                }
    
                return broadcastStrides;
            }
    
            /// <summary>
            /// Converts a flat index to multi-dimensional indices based on the shape.
            /// </summary>
            private static int[] UnflattenIndex(int flatIndex, int[] shape)
            {
                int[] indices = new int[shape.Length];
                for (int i = shape.Length - 1; i >= 0; i--)
                {
                    indices[i] = flatIndex % shape[i];
                    flatIndex /= shape[i];
                }
                return indices;
            }
    
            /// <summary>
            /// Computes the flat index from multi-dimensional indices and strides.
            /// </summary>
            private static int ComputeFlatIndex(int[] indices, int[] strides)
            {
                int flatIndex = 0;
                for (int i = 0; i < indices.Length; i++)
                {
                    flatIndex += indices[i] * strides[i];
                }
                return flatIndex;
            }
    
            #endregion
    
            #region Disposal
    
            ~Tensor()
            {
                // Return the data array to the pool
                ArrayPool<T>.Shared.Return(this.data, clearArray: true);
            }
    
            #endregion
    
            #region Serialization
    
            /// <summary>
            /// Serializes the tensor to a JSON string.
            /// </summary>
            /// <returns>JSON representation of the tensor.</returns>
            public string ToJson()
            {
                var serializedTensor = new SerializedTensor<T>
                {
                    Shape = this.shape,
                    Data = this.data.Take(TotalSize).ToArray()
                };
                return JsonSerializer.Serialize(serializedTensor);
            }
    
            /// <summary>
            /// Deserializes a JSON string to create a tensor.
            /// </summary>
            /// <param name="json">JSON string representing a tensor.</param>
            /// <returns>Deserialized tensor.</returns>
            public static Tensor<T> FromJson(string json)
            {
                if (string.IsNullOrEmpty(json))
                    throw new ArgumentException("JSON string cannot be null or empty.", nameof(json));
    
                var deserialized = JsonSerializer.Deserialize<SerializedTensor<T>>(json);
    
                if (deserialized == null)
                    throw new InvalidOperationException("Failed to deserialize tensor. Deserialized object is null.");
    
                if (deserialized.Shape == null || deserialized.Data == null)
                    throw new InvalidOperationException("Deserialized tensor has null Shape or Data.");
    
                int totalSize = deserialized.Shape.Aggregate(1, (a, b) => a * b);
    
                if (deserialized.Data.Length != totalSize)
                    throw new InvalidOperationException("Data length does not match shape.");
    
                T[] data = ArrayPool<T>.Shared.Rent(totalSize);
                Array.Copy(deserialized.Data, 0, data, 0, totalSize);
    
                int[] strides = CalculateStrides(deserialized.Shape);
    
                return new Tensor<T>(data, deserialized.Shape, strides);
            }
    
            private class SerializedTensor<TData> where TData : unmanaged
            {
                public int[] Shape { get; set; }
                public TData[] Data { get; set; }
            }
    
            #endregion
    
            #region IEnumerable<T>
    
            public IEnumerator<T> GetEnumerator()
            {
                return ((IEnumerable<T>)this.data.Take(TotalSize)).GetEnumerator();
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return this.GetEnumerator();
            }
    
            #endregion
    
            #region IEquatable<Tensor<T>>
    
            public bool Equals(Tensor<T> other)
            {
                if (other == null)
                    return false;
    
                if (!this.shape.SequenceEqual(other.shape))
                    return false;
    
                for (int i = 0; i < this.TotalSize; i++)
                {
                    if (!this.data[i].Equals(other.data[i]))
                        return false;
                }
    
                return true;
            }
    
            public override bool Equals(object obj)
            {
                return Equals(obj as Tensor<T>);
            }
    
            public override int GetHashCode()
            {
                int hash = 17;
                foreach (var dim in shape)
                {
                    hash = hash * 31 + dim.GetHashCode();
                }
                // For large data, we might not want to include all elements
                // Here we can sample some elements
                hash = hash * 31 + data[0].GetHashCode();
                if (TotalSize > 1)
                    hash = hash * 31 + data[TotalSize -1].GetHashCode();
                return hash;
            }
    
            #endregion
    
        }
    }