Repository: scipy/scipy
File: scipy/signal/_lti_conversion.py (Python)

Code before:
    ((2, 2), (2, 1), (1, 2), (1, 1))
    >>> CC
    array([[0., 0.]])
    """
    if A is None and B is None and C is None:
        raise ValueError("Dimension n is undefined for parameters A = B = C = None!")
    if B is None and D is None:
        raise ValueError("Dimension p is undefined for parameters B = D = None!")
    if C is None and D is None:
        raise ValueError("Dimension q is undefined for parameters C = D = None!")

    xp = array_namespace(A, B, C, D)
    A, B, C, D = (xpx.atleast_nd(xp.asarray(M_), ndim=2) if M_ is not None else
                  xp.zeros((0, 0)) for M_ in (A, B, C, D))

    n = A.shape[0] or B.shape[0] or C.shape[1] or 0  # try finding non-zero dimensions
    p = B.shape[1] or D.shape[1] or 0
    q = C.shape[0] or D.shape[0] or 0

    A = xp.zeros((n, n)) if xp_size(A) == 0 else A  # Create zero-matrices if needed
    B = xp.zeros((n, p)) if xp_size(B) == 0 else B
    C = xp.zeros((q, n)) if xp_size(C) == 0 else C
    D = xp.zeros((q, p)) if xp_size(D) == 0 else D

    if A.shape != (n, n):
        raise ValueError(f"Parameter A was converted to shape {A.shape} != ({n}, {n})!")
    if B.shape != (n, p):
        raise ValueError(f"Parameter B was converted to shape {B.shape} != ({n}, {p})!")
    if C.shape != (q, n):
        raise ValueError(f"Parameter C was converted to shape {C.shape} != ({q}, {n})!")
    if D.shape != (q, p):
        raise ValueError(f"Parameter D was converted to shape {D.shape} != ({q}, {p})!")

    return A, B, C, D


def ss2tf(A, B, C, D, input=0):
    r"""State-space to transfer function.

    A, B, C, D defines a linear state-space system with `p` inputs,
    `q` outputs, and `n` state variables.

    Parameters
    ----------
    A : array_like
        State (or system) matrix of shape ``(n, n)``
    B : array_like
        Input matrix of shape ``(n, p)``
    C : array_like
        Output matrix of shape ``(q, n)``
    D : array_like

Review comment (suggestion):
I find this message slightly confusing, maybe this?
```suggestion
        raise ValueError(f"Parameter A has shape {A.shape} but should be ({n}, {n})!")
```

Code after:
    ((2, 2), (2, 1), (1, 2), (1, 1))
    >>> CC
    array([[0., 0.]])
    """
    if A is None and B is None and C is None:
        raise ValueError("Dimension n is undefined for parameters A = B = C = None!")
    if B is None and D is None:
        raise ValueError("Dimension p is undefined for parameters B = D = None!")
    if C is None and D is None:
        raise ValueError("Dimension q is undefined for parameters C = D = None!")

    xp = array_namespace(A, B, C, D)
    A, B, C, D = (xpx.atleast_nd(xp.asarray(M_), ndim=2) if M_ is not None else
                  xp.zeros((0, 0)) for M_ in (A, B, C, D))

    n = A.shape[0] or B.shape[0] or C.shape[1] or 0  # try finding non-zero dimensions
    p = B.shape[1] or D.shape[1] or 0
    q = C.shape[0] or D.shape[0] or 0

    A = xp.zeros((n, n)) if xp_size(A) == 0 else A  # Create zero-matrices if needed
    B = xp.zeros((n, p)) if xp_size(B) == 0 else B
    C = xp.zeros((q, n)) if xp_size(C) == 0 else C
    D = xp.zeros((q, p)) if xp_size(D) == 0 else D

    if A.shape != (n, n):
        raise ValueError(f"Parameter A has shape {A.shape} but should be ({n}, {n})!")
    if B.shape != (n, p):
        raise ValueError(f"Parameter B has shape {B.shape} but should be ({n}, {p})!")
    if C.shape != (q, n):
        raise ValueError(f"Parameter C has shape {C.shape} but should be ({q}, {n})!")
    if D.shape != (q, p):
        raise ValueError(f"Parameter D has shape {D.shape} but should be ({q}, {p})!")

    return A, B, C, D


def ss2tf(A, B, C, D, input=0):
    r"""State-space to transfer function.

    A, B, C, D defines a linear state-space system with `p` inputs,
    `q` outputs, and `n` state variables.

    Parameters
    ----------
    A : array_like
        State (or system) matrix of shape ``(n, n)``
    B : array_like
        Input matrix of shape ``(n, p)``
    C : array_like
        Output matrix of shape ``(q, n)``
    D : array_like