# encoding=utf8


import numpy as np


def k_function(n):
    r"""
    Returns the K-function up to a given integer n.

    Parameters
    ----------
    n : int
        The length of the returned K-function as in :math:`K(n)`.

    Returns
    -------
    array-like
        numpy array of the computed integers returned by the K-function.

    Notes
    -----
    The K-function generalizes the hyperfactorial for complex numbers and is defined for positive
    integers as:

    .. math::

        K(n) \equiv 1^1 2^2 3^3 \cdots (n - 1)^{n - 1}

    The K-function can also be expressed as a hyperfactorial, :math:`H`:

    .. math::

        K(n) = H(n - 1)

    The Gamma function and Barnes G-Function are also closely related by:

    .. math::

        K(n) = \frac{[\Gamma{n}]^{n - 1}}{G(n)}

    Examples
    --------
    >>> k_function(3)
    array([1., 1., 4.])
    >>> k_function(5).astype(int)
    array([    1,     1,     4,   108, 27648])

    References
    ----------
    Sloane, N. J. A. Sequence A002109/M3706 in "The On-Line Encyclopedia of Integer Sequences."

    Weisstein, Eric W. "K-Function." From MathWorld--A Wolfram Web Resource.
        http://mathworld.wolfram.com/K-Function.html

    Wikipedia contributors. (2015, December 5). K-function. In Wikipedia, The Free Encyclopedia.
        Retrieved 13:56, March 1, 2018, from https://en.wikipedia.org/w/index.php?title=K-function&oldid=693891074

    """
    kn = np.empty(n)
    k = 1

    for i in np.arange(0, n):

        k *= (i) ** (i)
        kn[i] = k

    return kn
