Exemple #1
0
        /// <summary>
        /// Transposes the specified dimension1.
        /// </summary>
        /// <param name="dimension1">The dimension1.</param>
        /// <param name="dimension2">The dimension2.</param>
        /// <returns>Tensor.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// dimension1
        /// or
        /// dimension2
        /// </exception>
        private NDArray Transpose(int dimension1, int dimension2, bool NewContiguous = false)
        {
            if (dimension1 < 0 || dimension1 >= DimensionCount)
            {
                throw new ArgumentOutOfRangeException("dimension1");
            }
            if (dimension2 < 0 || dimension2 >= DimensionCount)
            {
                throw new ArgumentOutOfRangeException("dimension2");
            }

            if (dimension1 == dimension2)
            {
                storage.AddRef();
                return(this);
            }

            var newSizes   = (long[])shape.Clone();
            var newStrides = (long[])strides.Clone();

            ArraySwap(newSizes, dimension1, dimension2);
            ArraySwap(newStrides, dimension1, dimension2);

            if (NewContiguous)
            {
                return(TOps.NewContiguous(new NDArray(newSizes, newStrides, storage, storageOffset)).Reshape(newSizes));
            }

            return(new NDArray(newSizes, newStrides, storage, storageOffset));
        }