예제 #1
0
def dstack(tup):
    """
    Stack arrays in sequence depth wise (along third axis).

    This is equivalent to concatenation along the third axis after 2-D arrays
    of shape `(M,N)` have been reshaped to `(M,N,1)` and 1-D arrays of shape
    `(N,)` have been reshaped to `(1,N,1)`. Rebuilds arrays divided by
    `dsplit`.

    This function makes most sense for arrays with up to 3 dimensions. For
    instance, for pixel-data with a height (first axis), width (second axis),
    and r/g/b channels (third axis). The functions `concatenate`, `stack` and
    `block` provide more general stacking and concatenation operations.

    Args:
        tup (Sequence[numpoly.ndpoly]):
            The arrays must have the same shape along all but the third axis.
            1-D or 2-D arrays must have the same shape.

    Returns:
        (numpoly.ndpoly):
            The array formed by stacking the given arrays, will be at least
            3-D.

    Examples:
        >>> a = numpoly.symbols("x y z")
        >>> b = numpoly.polynomial([1, 2, 3])
        >>> numpoly.dstack([a, b])
        polynomial([[[x, 1],
                     [y, 2],
                     [z, 3]]])
        >>> c = numpoly.polynomial([[1], [2], [3]])
        >>> d = a.reshape(3, 1)
        >>> numpoly.dstack([c, d])
        polynomial([[[1, x]],
        <BLANKLINE>
                    [[2, y]],
        <BLANKLINE>
                    [[3, z]]])

    """
    arrays = numpoly.align_exponents(*tup)
    arrays = numpoly.align_dtype(*arrays)
    result = numpy.dstack([array.values for array in arrays])
    return numpoly.aspolynomial(result, names=arrays[0].indeterminants)
예제 #2
0
def stack(arrays, axis=0, out=None):
    """
    Join a sequence of arrays along a new axis.

    The ``axis`` parameter specifies the index of the new axis in the
    dimensions of the result. For example, if ``axis=0`` it will be the first
    dimension and if ``axis=-1`` it will be the last dimension.

    Args:
        arrays (Sequence[numpoly.ndpoly]):
            Each array must have the same shape.
        axis (Optional[int]):
            The axis in the result array along which the input arrays are
            stacked.
        out (Optional[numpy.ndarray]):
            If provided, the destination to place the result. The shape must be
            correct, matching that of what stack would have returned if no out
            argument were specified.

    Returns:
        (numpoly.ndpoly):
            The stacked array has one more dimension than the input arrays.

    Examples:
        >>> a = numpoly.symbols("x y z")
        >>> b = numpoly.polynomial([1, 2, 3])
        >>> numpoly.stack([a, b])
        polynomial([[x, y, z],
                    [1, 2, 3]])
        >>> numpoly.stack([a, b], axis=-1)
        polynomial([[x, 1],
                    [y, 2],
                    [z, 3]])

    """
    arrays = numpoly.align_exponents(*arrays)
    arrays = numpoly.align_dtype(*arrays)
    result = numpy.stack(
        [array.values for array in arrays], axis=axis, out=out)
    return numpoly.aspolynomial(result, names=arrays[0].indeterminants)
예제 #3
0
def concatenate(arrays, axis=0, out=None):
    """
    Join a sequence of arrays along an existing axis.

    Args:
        arrays (Iterable[numpoly.ndpoly]):
            The arrays must have the same shape, except in the dimension
            corresponding to `axis` (the first, by default).
        axis (Optional[int]):
            The axis along which the arrays will be joined.  If axis is None,
            arrays are flattened before use.  Default is 0.
        out (Optional[numpy.ndarray]):
            If provided, the destination to place the result. The shape must be
            correct, matching that of what concatenate would have returned if
            no out argument were specified.

    Returns:
        (numpoly.ndpoly):
            The concatenated array.

    Examples:
        >>> a = numpy.array([[1, 2], [3, 4]])
        >>> b = numpoly.symbols("x y").reshape(1, 2)
        >>> numpoly.concatenate((a, b), axis=0)
        polynomial([[1, 2],
                    [3, 4],
                    [x, y]])
        >>> numpoly.concatenate((a, b.T), axis=1)
        polynomial([[1, 2, x],
                    [3, 4, y]])
        >>> numpoly.concatenate((a, b), axis=None)
        polynomial([1, 2, 3, 4, x, y])

    """
    arrays = numpoly.align_exponents(*arrays)
    arrays = numpoly.align_dtype(*arrays)
    result = numpy.concatenate(
        [array.values for array in arrays], axis=axis, out=out)
    return numpoly.aspolynomial(result, names=arrays[0].indeterminants)
예제 #4
0
def hstack(tup):
    """
    Stack arrays in sequence horizontally (column wise).

    This is equivalent to concatenation along the second axis, except for 1-D
    arrays where it concatenates along the first axis. Rebuilds arrays divided
    by `hsplit`.

    This function makes most sense for arrays with up to 3 dimensions. For
    instance, for pixel-data with a height (first axis), width (second axis),
    and r/g/b channels (third axis). The functions `concatenate`, `stack` and
    `block` provide more general stacking and concatenation operations.

    Args:
        tup (Sequence[numpoly.ndpoly]):
            The arrays must have the same shape along all but the second axis,
            except 1-D arrays which can be any length.

    Returns:
        (numpoly.ndpoly):
            The array formed by stacking the given arrays.

    Examples:
        >>> a = numpoly.symbols("x y z")
        >>> b = numpoly.polynomial([1, 2, 3])
        >>> numpoly.hstack([a, b])
        polynomial([x, y, z, 1, 2, 3])
        >>> c = numpoly.polynomial([[1], [2], [3]])
        >>> d = a.reshape(3, 1)
        >>> numpoly.hstack([c, d])
        polynomial([[1, x],
                    [2, y],
                    [3, z]])

    """
    arrays = numpoly.align_exponents(*tup)
    arrays = numpoly.align_dtype(*arrays)
    result = numpy.hstack([array.values for array in arrays])
    return numpoly.aspolynomial(result, names=arrays[0].indeterminants)