Ejemplo n.º 1
0
 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})");
     }
 }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <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));
        }
Ejemplo n.º 4
0
 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})");
     }
 }
Ejemplo n.º 5
0
 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();
Ejemplo n.º 6
0
 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())}");
         }
     }
 }
Ejemplo n.º 7
0
        /// <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));
        }
Ejemplo n.º 8
0
        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]);
                }
            }
        }
Ejemplo n.º 9
0
        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]);
                }
            }
        }
Ejemplo n.º 10
0
        /// <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);
        }
Ejemplo n.º 11
0
 /// <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));
Ejemplo n.º 12
0
        /// <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));
        }
Ejemplo n.º 13
0
 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;
 }
Ejemplo n.º 15
0
 internal OneNdArrayImpl(IndexArray shape) : base(shape)
     => Guard.AssertCast(ValueTrait.IsNumber <T>(),
Ejemplo n.º 16
0
 /// <summary></summary>
 /// <param name="shape"></param>
 internal NdArray(IndexArray shape)
 {
     Entity = new RawNdArrayImpl <T>(shape);
 }
Ejemplo n.º 17
0
 /// <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))
 {
 }
Ejemplo n.º 18
0
 protected NdArrayImpl(IndexArray shape)
 {
     Shape = shape;
 }
 internal MutableReshapeViewNdArrayImpl(MutableNdArrayImpl <T> source, IndexArray shape)
     : base(shape)
 {
     _source = source;
 }
Ejemplo n.º 20
0
 /// <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);
 }
Ejemplo n.º 21
0
 /// <inheritdoc />
 /// <summary>
 ///     Create new MutableNdArrayImpl{T} object with assigned shape.
 /// </summary>
 /// <param name="shape"></param>
 protected MutableNdArrayImpl(IndexArray shape)
     : base(shape)
 {
 }