示例#1
0
        /// <summary>
        /// Repeats the NdArray along an axis.
        /// </summary>
        /// <param name="axis">The axis to repeat along.</param>
        /// <param name="repeats">The number of repetitions.</param>
        /// <param name="source">The NdArray to repeat.</param>
        /// <returns>The repeated NdArray.</returns>
        public static NdArray <T> Replicate(int axis, int repeats, NdArray <T> source)
        {
            NdArray <T> .CheckAxis(axis, source);

            if (repeats < 0)
            {
                throw new ArgumentException("Number of repetitions cannot be negative.", "repeats");
            }

            // 1. insert axis of size one left to repetition axis
            // 2. broadcast along the new axis to number of repetitions
            // 3. reshape to result shape
            var step1 = source.Reshape(List.Insert(axis, 1, source.Shape));
            var step2 = NdArray <T> .BraodcastDim(axis, repeats, step1);

            var step3 = step2.Reshape(List.Set(axis, repeats * source.Shape[axis], source.Shape));

            return(step3);
        }