pystella/derivs.py
def centered_diff(f, coefs, direction, order):
    """
    A convenience wrapper to :func:`expand_stencil` for computing centered
    differences. By assuming the symmetry of the stencil (which has parity given
    by the parity of ``order``), no redundant coefficients need to be supplied.
    Further, by supplying the ``direction`` parameter, the input offset (the keys
    of ``coefs``) need only be integers.

    :arg f: A :class:`~pystella.Field`.

    :arg coefs: A :class:`dict` whose values are the coefficients of the stencil
        at an offset given by the key. The keys must be integers, and the
        values may be :mod:`pymbolic` expressions or constants. Only
        non-redundant ``(offset, coefficient)`` pairs are needed.

    :arg direction: An integer in ``(1, 2, 3)`` denoting the direction over which
        to expand the stencil (i.e., to apply the offset).

    :arg order: The order of the derivative being computed, which determines
        whether coefficients at the opposite offset have the same or opposite
        sign.

    Example:

    .. doctest::

        >>> f = ps.Field("f", offset="h")
        >>> coefs = {1: 1}
        >>> stencil = ps.derivs.centered_diff(f, coefs, 1, 1)
        >>> print(ps.index_fields(stencil))
        f[i + h + 1, j + h, k + h] + (-1)*f[i + h + -1, j + h, k + h]
    """

    all_coefs = {}
    for s, c in coefs.items():
        offset = [0, 0, 0]

        # skip central point (s == 0) for odd order
        if s != 0 or order % 2 == 0:
            offset[direction-1] = s
            all_coefs[tuple(offset)] = c

        # add the opposite point
        if s != 0:
            offset[direction-1] = - s
            all_coefs[tuple(offset)] = (-1)**order * c

    return expand_stencil(f, all_coefs)

    A convenience wrapper to :func:`expand_stencil` for computing centered
    differences. By assuming the symmetry of the stencil (which has parity given
    by the parity of ``order``), no redundant coefficients need to be supplied.
    Further, by supplying the ``direction`` parameter, the input offset (the keys
    of ``coefs``) need only be integers.

    :arg f: A :class:`~pystella.Field`.

    :arg coefs: A :class:`dict` whose values are the coefficients of the stencil
        at an offset given by the key. The keys must be integers, and the
        values may be :mod:`pymbolic` expressions or constants. Only
        non-redundant ``(offset, coefficient)`` pairs are needed.

    :arg direction: An integer in ``(1, 2, 3)`` denoting the direction over which
        to expand the stencil (i.e., to apply the offset).

    :arg order: The order of the derivative being computed, which determines
        whether coefficients at the opposite offset have the same or opposite
        sign.

    Example:

    .. doctest::

        >>> f = ps.Field("f", offset="h")
        >>> coefs = {1: 1}
        >>> stencil = ps.derivs.centered_diff(f, coefs, 1, 1)
        >>> print(ps.index_fields(stencil))
        f[i + h + 1, j + h, k + h] + (-1)*f[i + h + -1, j + h, k + h]
    
A convenience wrapper to :func:`expand_stencil` for computing centered
differences. By assuming the symmetry of the stencil (which has parity given
by the parity of ``order``), no redundant coefficients need to be supplied.
A convenience wrapper to : func : ` expand_stencil ` for computing centered differences . By assuming the symmetry of the stencil ( which has parity given by the parity of ` ` order ` ` ) no redundant coefficients need to be supplied .
def centered_diff(f, coefs, direction, order):
    all_coefs = {}
    for s, c in coefs.items():
        offset = [0, 0, 0]
        if s != 0 or order % 2 == 0:
            offset[direction-1] = s
            all_coefs[tuple(offset)] = c
        if s != 0:
            offset[direction-1] = - s
            all_coefs[tuple(offset)] = (-1)**order * c
    return expand_stencil(f, all_coefs)
def centered_diff ( f , coefs , direction , order ) : all_coefs = { } for s , c in coefs . items ( ) : offset = [ 0 , 0 , 0 ] if s != 0 or order % 2 == 0 : offset [ direction - 1 ] = s all_coefs [ tuple ( offset ) ] = c if s != 0 : offset [ direction - 1 ] = - s all_coefs [ tuple ( offset ) ] = ( - 1 ) ** order * c return expand_stencil ( f , all_coefs )
A convenience wrapper to :func:`expand_stencil` for computing centered
differences.
A convenience wrapper to : func : ` expand_stencil ` for computing centered differences .
"""
    A convenience wrapper to :func:`expand_stencil` for computing centered
    differences. By assuming the symmetry of the stencil (which has parity given
    by the parity of ``order``), no redundant coefficients need to be supplied.
    Further, by supplying the ``direction`` parameter, the input offset (the keys
    of ``coefs``) need only be integers.

    :arg f: A :class:`~pystella.Field`.

    :arg coefs: A :class:`dict` whose values are the coefficients of the stencil
        at an offset given by the key. The keys must be integers, and the
        values may be :mod:`pymbolic` expressions or constants. Only
        non-redundant ``(offset, coefficient)`` pairs are needed.

    :arg direction: An integer in ``(1, 2, 3)`` denoting the direction over which
        to expand the stencil (i.e., to apply the offset).

    :arg order: The order of the derivative being computed, which determines
        whether coefficients at the opposite offset have the same or opposite
        sign.

    Example:

    .. doctest::

        >>> f = ps.Field("f", offset="h")
        >>> coefs = {1: 1}
        >>> stencil = ps.derivs.centered_diff(f, coefs, 1, 1)
        >>> print(ps.index_fields(stencil))
        f[i + h + 1, j + h, k + h] + (-1)*f[i + h + -1, j + h, k + h]
    """ # skip central point (s == 0) for odd order
 # add the opposite point

f
None
coefs
A :class:`dict` whose values are the coefficients of the stencil
at an offset given by the key. The keys must be integers, and the
values may be :mod:`pymbolic` expressions or constants. Only
non-redundant ``(offset, coefficient)`` pairs are needed.
A : class : ` dict ` whose values are the coefficients of the stencil at an offset given by the key . The keys must be integers and the values may be : mod : ` pymbolic ` expressions or constants . Only non - redundant ` ` ( offset coefficient ) ` ` pairs are needed .
direction
An integer in ``(1, 2, 3)`` denoting the direction over which
to expand the stencil .
An integer in ` ` ( 1 2 3 ) ` ` denoting the direction over which to expand the stencil .
order
The order of the derivative being computed, which determines
whether coefficients at the opposite offset have the same or opposite
sign.






The order of the derivative being computed which determines whether coefficients at the opposite offset have the same or opposite sign .