/// <inheritdoc /> public override INumber AsNumber(INDArray array, params long[] indices) { ADFloat32NDArray internalArray = InternaliseArray(array); long flatIndex = NDArrayUtils.GetFlatIndex(array.Shape, array.Strides, indices); return(new ADFloat32Number(DNDArray.ToDNumber(internalArray.Handle, (int)flatIndex))); }
/// <inheritdoc /> public override void Fill(INDArray filler, INDArray arrayToFill, long[] sourceBeginIndices, long[] sourceEndIndices, long[] destinationBeginIndices, long[] destinationEndIndices) { IDataBuffer <float> fillerData = InternaliseArray(filler).Data; IDataBuffer <float> arrayToFillData = InternaliseArray(arrayToFill).Data; int sourceOffset = (int)NDArrayUtils.GetFlatIndex(filler.Shape, filler.Strides, sourceBeginIndices); int sourceLength = (int)NDArrayUtils.GetFlatIndex(filler.Shape, filler.Strides, sourceEndIndices) - sourceOffset + 1; // +1 because end is inclusive int destinationOffset = (int)NDArrayUtils.GetFlatIndex(arrayToFill.Shape, arrayToFill.Strides, destinationBeginIndices); int destinationLength = (int)NDArrayUtils.GetFlatIndex(arrayToFill.Shape, arrayToFill.Strides, destinationEndIndices) - destinationOffset + 1; // same here if (sourceLength < 0) { throw new ArgumentOutOfRangeException($"Source begin indices must be smaller than source end indices, but source length was {sourceLength}."); } if (destinationLength < 0) { throw new ArgumentOutOfRangeException($"Destination begin indices must be smaller than destination end indices, but destination length was {destinationLength}."); } if (sourceLength != destinationLength) { throw new ArgumentException($"Source and destination indices length must batch, but source length was {sourceLength} and destination legnth was {destinationLength}."); } Array.Copy(fillerData.Data, sourceOffset, arrayToFillData.Data, destinationOffset, sourceLength); }
/// <summary> /// Get a slice of this ndarray of a certain region as a new ndarray with the same underlying data. /// </summary> /// <param name="beginIndices">The begin indices (inclusively, where the slice should begin).</param> /// <param name="endIndices">The end indices (exclusively, where the slice should end).</param> /// <returns></returns> public override INDArray Slice(long[] beginIndices, long[] endIndices) { long[] slicedShape = GetSlicedShape(beginIndices, endIndices); //we want the end indices to be inclusive for easier handling endIndices = endIndices.Select(i => i - 1).ToArray(); long absoluteBeginOffset = NDArrayUtils.GetFlatIndex(Shape, Strides, beginIndices); long absoluteEndOffset = NDArrayUtils.GetFlatIndex(Shape, Strides, endIndices); long length = absoluteEndOffset - absoluteBeginOffset + 1; return(new CudaFloat32NDArray(new DNDArray(new CudaSigmaDiffDataBuffer <float>(Data, absoluteBeginOffset, length, ((SigmaDiffDataBuffer <float>)Data).BackendTag, _underlyingCudaBuffer.CudaContext), slicedShape))); }
/// <inheritdoc /> public virtual INDArray Slice(long[] beginIndices, long[] endIndices) { long[] slicedShape = GetSlicedShape(beginIndices, endIndices); //we want the end indices to be inclusive for easier handling endIndices = endIndices.Select(i => i - 1).ToArray(); long absoluteBeginOffset = NDArrayUtils.GetFlatIndex(Shape, Strides, beginIndices); long absoluteEndOffset = NDArrayUtils.GetFlatIndex(Shape, Strides, endIndices); long length = absoluteEndOffset - absoluteBeginOffset + 1; return(new ADNDArray <T>(new DataBuffer <T>(Data, absoluteBeginOffset, length), slicedShape)); }
/// <inheritdoc /> public override void Fill <T>(T[] filler, INDArray arrayToFill, long[] destinationBeginIndices, long[] destinationEndIndices) { IDataBuffer <float> arrayToFillData = InternaliseArray(arrayToFill).Data; int destinationOffset = (int)NDArrayUtils.GetFlatIndex(arrayToFill.Shape, arrayToFill.Strides, destinationBeginIndices); int destinationLength = (int)NDArrayUtils.GetFlatIndex(arrayToFill.Shape, arrayToFill.Strides, destinationEndIndices) - destinationOffset + 1; // +1 because end is inclusive if (destinationLength < 0) { throw new ArgumentOutOfRangeException($"Destination begin indices must be smaller than destination end indices, but destination length was {destinationLength}."); } Array.Copy(filler, 0, arrayToFillData.Data, destinationOffset, destinationLength); }
/// <summary> /// Permute this ndarray's data according to rearranged dimensions in place. /// Note: Arguments are not checked for correctness (and are asssumed to be correct). /// </summary> /// <param name="rearrangedDimensions">The re-arranged dimensions (numbered 0 to rank - 1).</param> /// <param name="originalShape">The original shape.</param> /// <param name="rearrangedShape">The new, re-arranged shape.</param> /// <param name="data">The data array.</param> /// <param name="offset">The data array offset.</param> /// <param name="length">The data array length.</param> internal static void _InternalPermuteSelf(T[] data, int offset, int length, int[] rearrangedDimensions, long[] originalShape, long[] rearrangedShape) { // TODO optimise heavily long[] originalStrides = NDArrayUtils.GetStrides(originalShape); long[] rearrangedStrides = NDArrayUtils.GetStrides(rearrangedShape); long[] bufferIndices = new long[rearrangedDimensions.Length]; BitArray traversedIndicies = new BitArray(length); for (int i = 0; i < length; i++) { int currentIndex = i; T previousValue = data[offset + currentIndex]; if (traversedIndicies[i]) { continue; } do { NDArrayUtils.GetIndices(currentIndex, originalShape, originalStrides, bufferIndices); bufferIndices = ArrayUtils.PermuteArray(bufferIndices, rearrangedDimensions); int swapIndex = (int)NDArrayUtils.GetFlatIndex(rearrangedShape, rearrangedStrides, bufferIndices); T nextPreviousValue = data[offset + swapIndex]; if (swapIndex != currentIndex) { data[offset + swapIndex] = previousValue; } previousValue = nextPreviousValue; traversedIndicies[currentIndex] = true; currentIndex = swapIndex; } while (i != currentIndex && !traversedIndicies[currentIndex]); } }
/// <inheritdoc /> public void SetValue <TOther>(TOther value, params long[] indices) { Data.SetValue((T)Convert.ChangeType(value, Data.Type.UnderlyingType), NDArrayUtils.GetFlatIndex(Shape, Strides, indices)); }
/// <inheritdoc /> public TOther GetValue <TOther>(params long[] indices) { return(Data.GetValueAs <TOther>(NDArrayUtils.GetFlatIndex(Shape, Strides, indices))); }