Пример #1
0
        public static Array <T> Tile <T>(Array <T> a, params int[] reps)
        {
            // Construct an array by repeating A the number of times given by reps.

            // If `reps` has length ``d``, the result will have dimension of
            // ``max(d, A.ndim)``.

            // If ``A.ndim < d``, `A` is promoted to be d-dimensional by prepending new
            // axes. So a shape (3,) array is promoted to (1, 3) for 2-D replication,
            // or shape (1, 1, 3) for 3-D replication. If this is not the desired
            // behavior, promote `A` to d-dimensions manually before calling this
            // function.

            // If ``A.ndim > d``, `reps` is promoted to `A`.ndim by pre-pending 1's to it.
            // Thus for an `A` of shape (2, 3, 4, 5), a `reps` of (2, 2) is treated as
            // (1, 1, 2, 2).

            // Parameters
            // ----------
            // A : array_like
            //     The input array.
            // reps : array_like
            //     The number of repetitions of `A` along each axis.

            // Returns
            // -------
            // c : ndarray
            //     The tiled output array.

            // See Also
            // --------
            // repeat : Repeat elements of an array.

            // Examples
            // --------
            // >>> a = np.array([0, 1, 2])
            // >>> np.tile(a, 2)
            // array([0, 1, 2, 0, 1, 2])
            // >>> np.tile(a, (2, 2))
            // array([[0, 1, 2, 0, 1, 2],
            //        [0, 1, 2, 0, 1, 2]])
            // >>> np.tile(a, (2, 1, 2))
            // array([[[0, 1, 2, 0, 1, 2]],
            //        [[0, 1, 2, 0, 1, 2]]])

            // >>> b = np.array([[1, 2], [3, 4]])
            // >>> np.tile(b, 2)
            // array([[1, 2, 1, 2],
            //        [3, 4, 3, 4]])
            // >>> np.tile(b, (2, 1))
            // array([[1, 2],
            //        [3, 4],
            //        [1, 2],
            //        [3, 4]])
            var tup = reps;
            var d   = tup.Length;
            //c = _nx.array(A, copy=False, subok=True, ndmin=d)
            var c         = a;
            var shape     = (int[])c.Shape.Clone();
            var oldLength = shape.Length;

            if (oldLength < d)
            {
                //System.Array.Resize(ref shape, d);
                //for (int i = oldLength; i < d; i++) shape[i] = 1;
                //c = c.Reshape(shape);
                var newShape = new int[d];
                for (int i = 0; i < d - oldLength; i++)
                {
                    newShape[i] = 1;
                }
                System.Array.Copy(shape, 0, newShape, d - oldLength, oldLength);
                shape = newShape;
                c     = c.Reshape(shape);
            }

            var n = Math.Max(c.Size, 1);

            if (d < c.NDim)
            {
                //tup = (1,)*(c.ndim - d) + tup
                throw new NotImplementedException();
            }
            for (int i = 0; i < tup.Length; i++)
            {
                var nrep = tup[i];
                if (nrep != 1)
                {
                    c = NN.Repeat(c.Reshape(-1, n), nrep, axis: 0);
                }
                var dim_in  = shape[i];
                var dim_out = dim_in * nrep;
                shape[i] = dim_out;
                n       /= Math.Max(dim_in, 1);
            }
            return(c.Reshape(shape));
        }