コード例 #1
0
        private static ndarray Concatenate(IEnumerable <ndarray> arrays, int?axis = 0)
        {
            int i;

            bool MustFlatten = false;

            if (axis == null)
            {
                MustFlatten = true;
                axis        = 0;
            }

            try
            {
                arrays.First();
            }
            catch (InvalidOperationException)
            {
                throw new ArgumentException("concatenation of zero-length sequence is impossible");
            }

            ndarray[] mps = NpyUtil_ArgProcessing.ConvertToCommonType(arrays);
            int       n   = mps.Length;

            if (MustFlatten)
            {
                // Flatten the arrays
                for (i = 0; i < n; i++)
                {
                    mps[i] = mps[i].Ravel(NPY_ORDER.NPY_CORDER);
                }
                axis = 0;
            }
            else if (axis != 0)
            {
                // Swap to make the axis 0
                for (i = 0; i < n; i++)
                {
                    mps[i] = NpyCoreApi.FromArray(mps[i].SwapAxes(axis.Value, 0), null, NPYARRAYFLAGS.NPY_C_CONTIGUOUS);
                }
            }


            npy_intp[] dims = mps[0].dims;
            if (dims.Length == 0)
            {
                throw new ArgumentException("0-d arrays can't be concatenated");
            }

            npy_intp new_dim = dims[0];

            for (i = 1; i < n; i++)
            {
                npy_intp[] dims2 = mps[i].dims;
                if (dims.Length != dims2.Length)
                {
                    throw new ArgumentException("arrays must have same number of dimensions");
                }
                bool eq = Enumerable.Zip(dims.Skip(1), dims2.Skip(1), (a1, b) => (a1 == b)).All(x => x);
                if (!eq)
                {
                    throw new ArgumentException("array dimensions do not agree");
                }
                new_dim += dims2[0];
            }
            dims[0] = new_dim;


            ndarray result = NpyCoreApi.AllocArray(mps[0].Dtype, dims.Length, dims, false);

            if (NpyCoreApi.CombineInto(result, mps) < 0)
            {
                throw new ArgumentException("unable to concatenate these arrays");
            }

            if (0 < axis && axis < NpyDefs.NPY_MAXDIMS || axis < 0)
            {
                result = result.SwapAxes(axis.Value, 0);
            }


            return(result);
        }