

def companion_matrix(polynomial):
    "\n    Create a companion matrix\n\n    Parameters\n    ----------\n    polynomial : array_like or list\n        If an iterable, interpreted as the coefficients of the polynomial from\n        which to form the companion matrix. Polynomial coefficients are in\n        order of increasing degree, and may be either scalars (as in an AR(p)\n        model) or coefficient matrices (as in a VAR(p) model). If an integer,\n        it is interpereted as the size of a companion matrix of a scalar\n        polynomial, where the polynomial coefficients are initialized to zeros.\n        If a matrix polynomial is passed, :math:`C_0` may be set to the scalar\n        value 1 to indicate an identity matrix (doing so will improve the speed\n        of the companion matrix creation).\n\n    Returns\n    -------\n    companion_matrix : array\n\n    Notes\n    -----\n    Given coefficients of a lag polynomial of the form:\n\n    .. math::\n        c(L) = c_0 + c_1 L + \\dots + c_p L^p\n\n    returns a matrix of the form\n\n    .. math::\n        \\begin{bmatrix}\n            \\phi_1 & 1      & 0 & \\cdots & 0 \\\\\n            \\phi_2 & 0      & 1 &        & 0 \\\\\n            \\vdots &        &   & \\ddots & 0 \\\\\n                   &        &   &        & 1 \\\\\n            \\phi_n & 0      & 0 & \\cdots & 0 \\\\\n        \\end{bmatrix}\n\n    where some or all of the :math:`\\phi_i` may be non-zero (if `polynomial` is\n    None, then all are equal to zero).\n\n    If the coefficients provided are scalars :math:`(c_0, c_1, \\dots, c_p)`,\n    then the companion matrix is an :math:`n \\times n` matrix formed with the\n    elements in the first column defined as\n    :math:`\\phi_i = -\\frac{c_i}{c_0}, i \\in 1, \\dots, p`.\n\n    If the coefficients provided are matrices :math:`(C_0, C_1, \\dots, C_p)`,\n    each of shape :math:`(m, m)`, then the companion matrix is an\n    :math:`nm \\times nm` matrix formed with the elements in the first column\n    defined as :math:`\\phi_i = -C_0^{-1} C_i', i \\in 1, \\dots, p`.\n\n    It is important to understand the expected signs of the coefficients. A\n    typical AR(p) model is written as:\n\n    .. math::\n        y_t = a_1 y_{t-1} + \\dots + a_p y_{t-p} + \\varepsilon_t\n\n    This can be rewritten as:\n\n    .. math::\n        (1 - a_1 L - \\dots - a_p L^p )y_t = \\varepsilon_t \\\\\n        (1 + c_1 L + \\dots + c_p L^p )y_t = \\varepsilon_t \\\\\n        c(L) y_t = \\varepsilon_t\n\n    The coefficients from this form are defined to be :math:`c_i = - a_i`, and\n    it is the :math:`c_i` coefficients that this function expects to be\n    provided.\n\n    "
    identity_matrix = False
    if isinstance(polynomial, int):
        n = polynomial
        m = 1
        polynomial = None
    else:
        n = (len(polynomial) - 1)
        if (n < 1):
            raise ValueError('Companion matrix polynomials must include at least two terms.')
        if (isinstance(polynomial, list) or isinstance(polynomial, tuple)):
            try:
                m = len(polynomial[1])
            except TypeError:
                m = 1
            if (m == 1):
                polynomial = np.asanyarray(polynomial)
            elif (polynomial[0] == 1):
                polynomial[0] = np.eye(m)
                identity_matrix = True
        else:
            m = 1
            polynomial = np.asanyarray(polynomial)
    matrix = np.zeros(((n * m), (n * m)), dtype=np.asanyarray(polynomial).dtype)
    idx = np.diag_indices(((n - 1) * m))
    idx = (idx[0], (idx[1] + m))
    matrix[idx] = 1
    if ((polynomial is not None) and (n > 0)):
        if (m == 1):
            matrix[:, 0] = ((- polynomial[1:]) / polynomial[0])
        elif identity_matrix:
            for i in range(n):
                matrix[(i * m):((i + 1) * m), :m] = (- polynomial[(i + 1)].T)
        else:
            inv = np.linalg.inv(polynomial[0])
            for i in range(n):
                matrix[(i * m):((i + 1) * m), :m] = (- np.dot(inv, polynomial[(i + 1)]).T)
    return matrix

dataset/ETHPy150Open statsmodels/statsmodels/statsmodels/tsa/statespace/tools.py companion_matrix/original