public static void AssertShapeMatch(IndexArray xShape, IndexArray yShape, string xArgName, string yArgName) { if (xShape != yShape) { ThrowShapeMismatch($"NdArray shapes of the arguments were mismatched. ({xArgName}={xShape}, {yArgName}={yShape})"); } }
/// <summary> /// For axes-formed linq, operator, calculates the partial array shape. /// </summary> /// <param name="sourceShape"></param> /// <param name="axes"></param> /// <param name="flattenIndex"></param> /// <returns></returns> internal static IndexOrRange[] CalculatePartialShape( IndexArray sourceShape, ReadOnlySpan <int> axes, int flattenIndex) { var len = sourceShape.Length; var newShape = CalculateAxesFormedShape(sourceShape, axes); var indicesOnThis = NdArrayImpl.ToShapedIndices(newShape, flattenIndex); var indexOrRangesOnSource = new IndexOrRange[len]; for (int i = 0, j = 0; i < len; ++i) { if (!axes.Contains(i)) { indexOrRangesOnSource[i] = new Index(indicesOnThis[j], false); ++j; } else { indexOrRangesOnSource[i] = Range.Whole; } } return(indexOrRangesOnSource); }
/// <summary> /// Creates immutable <see cref="NdArray{T}"/> instance which has any shape. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <param name="shape"> [<c>array.Length == shape.TotalLength</c>] </param> /// <returns></returns> public static NdArray <T> Create <T>(T[] array, IndexArray shape) { var entity = new RawNdArrayImpl <T>(shape); array.CopyTo(entity.Buffer.Span); return(new NdArray <T>(entity)); }
public static void AssertShapeMatch(IndexArray expected, IndexArray actual, string argName) { if (expected != actual) { ThrowShapeMismatch($"NdArray shapes of the arguments were mismatched. (expected={expected}, {argName}={actual})"); } }
internal static IndexArray CalculateShape( IndexArray sourceShape, IndexOrRange[] slices) => slices .Zip(sourceShape, (slice, shape) => (slice, shape)) .Where(x => x.slice.IsRange) .Select(x => x.slice.Range.MapLength(x.shape)) .ToIndexArray();
public static void AssertIndices(IndexArray shape, ReadOnlySpan <int> indices) { if (shape.Length != indices.Length) { ThrowArgumentOutOfRange($"Shape={shape}, indices={new IndexArray(indices.ToArray())}"); } for (int i = 0, len = shape.Length; i < len; ++i) { if ((uint)indices[i] >= (uint)shape[i]) { ThrowArgumentOutOfRange($"Shape={shape}, indices={new IndexArray(indices.ToArray())}"); } } }
/// <summary> /// For axes-formed linq operator, calculates new shape. /// </summary> /// <param name="sourceShape"></param> /// <param name="axes"></param> /// <returns></returns> internal static IndexArray CalculateAxesFormedShape( IndexArray sourceShape, ReadOnlySpan <int> axes) { var newShape = new int[sourceShape.Length - axes.Length]; for (int i = 0, j = 0; i < sourceShape.Length; ++i) { if (axes.Contains(i)) { continue; } newShape[j] = sourceShape[i]; ++j; } return(new IndexArray(newShape)); }
public static void AssertIndices(IndexArray shape, IndexOrRange[] indices) { void throwIfFailed(bool requiredCondition) => AssertArgumentRange(requiredCondition, $"Shape={shape}, indices={shape}"); throwIfFailed(shape.Length == indices.Length); for (int i = 0, len = shape.Length; i < len; ++i) { var index = indices[i]; if (index.IsRange) { var start = index.Range.Map(0, shape[i]); throwIfFailed((uint)start < (uint)shape[i]); var end = index.Range.Map(index.Range.MapLength(shape[i]) - 1, shape[i]); throwIfFailed((uint)end <= (uint)shape[i]); } else { throwIfFailed((uint)index.Index.Value < (uint)shape[i]); } } }
internal static void ToSlicedShapedIndices( IndexArray sourceShape, ReadOnlySpan <int> shapedIndices, IndexOrRange[] slices, Span <int> resultIndices) { var rank = sourceShape.Length; for (int i = 0, j = 0; i < rank; ++i) { if (slices[i].IsRange) { resultIndices[i] = slices[i] .Range .Map(shapedIndices[j], sourceShape[i]); ++j; } else { resultIndices[i] = slices[i] .Index.Map(sourceShape[i]); } } }
/// <summary> /// Returns a value indicating whether this instance and a specified <see cref="IndexArray"/> represent the same value. /// </summary> /// <param name="other"></param> /// <returns></returns> public bool Equals(IndexArray other) { if (_hashCode != other._hashCode) { return(false); } if (TotalLength != other.TotalLength) { return(false); } if (Length != other.Length) { return(false); } for (int i = 0, len = Length; i < len; ++i) { if (this[i] != other[i]) { return(false); } } return(true); }
/// <summary> /// Creates a <see cref="NdArray{T}"/> instance whose all elements are zero. /// </summary> /// <typeparam name="T"> [Primitive] </typeparam> /// <param name="shape"></param> /// <returns></returns> /// <exception cref="InvalidCastException"> <typeparamref name="T"/> is not primitive type. </exception> public static NdArray <T> Zeros <T>(IndexArray shape) => new NdArray <T>(new ZeroNdArrayImpl <T>(shape));
/// <summary> /// Creates mutable <see cref="NdArray{T}"/> instance which has any shape. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="shape"> [<c>array.Length == shape.TotalLength</c>] </param> /// <returns></returns> public static MutableNdArray <T> CreateMutable <T>(IndexArray shape) { var entity = new RawNdArrayImpl <T>(shape); return(new MutableNdArray <T>(entity)); }
internal ZeroNdArrayImpl(IndexArray shape) : base(shape) => Guard.AssertCast(ValueTrait.IsNumber <T>(), $"{typeof(T)} has no available zero value.");
internal ReshapeViewNdArrayImpl(NdArrayImpl <T> source, IndexArray shape) : base(shape) { _source = source; }
internal OneNdArrayImpl(IndexArray shape) : base(shape) => Guard.AssertCast(ValueTrait.IsNumber <T>(),
/// <summary></summary> /// <param name="shape"></param> internal NdArray(IndexArray shape) { Entity = new RawNdArrayImpl <T>(shape); }
/// <summary> /// Creates new NdArray object with values and shapes. /// </summary> /// <param name="shape"> [<c>shape.Product() == array.Length</c>] </param> internal RawNdArray(IndexArray shape) : base(new RawNdArrayImpl <T>(shape)) { }
protected NdArrayImpl(IndexArray shape) { Shape = shape; }
internal MutableReshapeViewNdArrayImpl(MutableNdArrayImpl <T> source, IndexArray shape) : base(shape) { _source = source; }
/// <inheritdoc /> /// <summary> /// Create new RawNdArrayImpl{T} object. /// </summary> /// <param name="shape"></param> public RawNdArrayImpl(IndexArray shape) : base(shape) { Length = Shape.TotalLength; _bufferCollector = new Allocation <T>(Length); }
/// <inheritdoc /> /// <summary> /// Create new MutableNdArrayImpl{T} object with assigned shape. /// </summary> /// <param name="shape"></param> protected MutableNdArrayImpl(IndexArray shape) : base(shape) { }