Repository: scipy/scipy
File: scipy/signal/tests/test_filter_design.py (Python)

Code before:
        b_ref = xp.asarray([[ 2.5, -7.5,  5. ], [ 2.5,  2.5,  0. ]], dtype=xp.float64)
        a_ref = xp.asarray([ 1., -7., 12.], dtype=xp.float64)

        xp_assert_close(b, b_ref, atol=1e-14)
        xp_assert_close(a, a_ref, atol=1e-14)

    @skip_xp_backends("jax.numpy",
                      reason="zpk2tf not compatible with jax yet on multi-dim arrays")
    @skip_xp_backends("cupy",
                      reason="multi-dim arrays not supported yet on cupy")
    def test_zpk2tf_complex_k_multi_dim_z(self, xp):
        k = 1j
        z = xp.asarray([[1., 2.], [0., -1.]])
        p = xp.asarray([3., 4.])

        b, a = zpk2tf(z, p, k)

        z1 = xp.asarray([1., 2.])
        b1, a1 = zpk2tf(z1, p, k)

        z2 = xp.asarray([0., -1.])
        b2, a2 = zpk2tf(z2, p, k)

        xp_assert_close(a, a1)
        xp_assert_close(a, a2)
        xp_assert_close(b[0], b1)
        xp_assert_close(b[1], b2)
        assert xp.isdtype(b.dtype, 'complex floating')


@make_xp_test_case(sos2zpk)
class TestSos2Zpk:

    @skip_xp_backends("dask.array", reason="it https://github.com/dask/dask/issues/11883")
    @xfail_xp_backends("jax.numpy", reason="inaccurate with jax")
    def test_basic(self, xp):
        sos = [[1, 0, 1, 1, 0, -0.81],
               [1, 0, 0, 1, 0, +0.49]]
        sos = xp.asarray(sos)
        z, p, k = sos2zpk(sos)
        z2 = xp.asarray([1j, -1j, 0, 0])
        p2 = xp.asarray([0.9, -0.9, 0.7j, -0.7j])
        k2 = 1.
        assert_array_almost_equal(_sort_cmplx(z, xp), _sort_cmplx(z2, xp), decimal=4)
        assert_array_almost_equal(_sort_cmplx(p, xp), _sort_cmplx(p2, xp), decimal=4)
        assert math.isclose(k, k2)

        sos = [[1.00000, +0.61803, 1.0000, 1.00000, +0.60515, 0.95873],
               [1.00000, -1.61803, 1.0000, 1.00000, -1.58430, 0.95873],
               [1.00000, +1.00000, 0.0000, 1.00000, +0.97915, 0.00000]]
        sos = xp.asarray(sos)

Review comment (suggestion):
```suggestion
    def test_zpk2tf_complex_k_multi_dim_z(self, xp):
        # Regression test for gh-24395
        k = 1j
        z = xp.asarray([[1., 2.], [0., -1.]])
        p = xp.asarray([3., 4.])

        b, a = zpk2tf(z, p, k)

        b1, a1 = zpk2tf(z[0, :], p, k)
        b2, a2 = zpk2tf(z[1, :], p, k)

        xp_assert_close(a, a1)
        xp_assert_close(a, a2)
        xp_assert_close(b[0], b1)
        xp_assert_close(b[1], b2)
        assert xp.isdtype(b.dtype, 'complex floating')
```

Code after:
        b_ref = xp.asarray([[ 2.5, -7.5,  5. ], [ 2.5,  2.5,  0. ]], dtype=xp.float64)
        a_ref = xp.asarray([ 1., -7., 12.], dtype=xp.float64)

        xp_assert_close(b, b_ref, atol=1e-14)
        xp_assert_close(a, a_ref, atol=1e-14)

    @skip_xp_backends("jax.numpy",
                      reason="zpk2tf not compatible with jax yet on multi-dim arrays")
    @skip_xp_backends("cupy",
                      reason="multi-dim arrays not supported yet on cupy")
    def test_zpk2tf_complex_k_multi_dim_z(self, xp):
        # Regression test for gh-24395
        k = 1j
        z = xp.asarray([[1., 2.], [0., -1.]])
        p = xp.asarray([3., 4.])

        b, a = zpk2tf(z, p, k)

        b1, a1 = zpk2tf(z[0, :], p, k)
        b2, a2 = zpk2tf(z[1, :], p, k)

        xp_assert_close(a, a1)
        xp_assert_close(a, a2)
        xp_assert_close(b[0, :], b1)
        xp_assert_close(b[1, :], b2)
        assert xp.isdtype(b.dtype, 'complex floating')


@make_xp_test_case(sos2zpk)
class TestSos2Zpk:

    @skip_xp_backends("dask.array", reason="it https://github.com/dask/dask/issues/11883")
    @xfail_xp_backends("jax.numpy", reason="inaccurate with jax")
    def test_basic(self, xp):
        sos = [[1, 0, 1, 1, 0, -0.81],
               [1, 0, 0, 1, 0, +0.49]]
        sos = xp.asarray(sos)
        z, p, k = sos2zpk(sos)
        z2 = xp.asarray([1j, -1j, 0, 0])
        p2 = xp.asarray([0.9, -0.9, 0.7j, -0.7j])
        k2 = 1.
        assert_array_almost_equal(_sort_cmplx(z, xp), _sort_cmplx(z2, xp), decimal=4)
        assert_array_almost_equal(_sort_cmplx(p, xp), _sort_cmplx(p2, xp), decimal=4)
        assert math.isclose(k, k2)

        sos = [[1.00000, +0.61803, 1.0000, 1.00000, +0.60515, 0.95873],
               [1.00000, -1.61803, 1.0000, 1.00000, -1.58430, 0.95873],
               [1.00000, +1.00000, 0.0000, 1.00000, +0.97915, 0.00000]]
        sos = xp.asarray(sos)
        z, p, k = sos2zpk(sos)
        z2 = [-0.3090 + 0.9511j, -0.3090 - 0.9511j, 0.8090 + 0.5878j,