Repository: scipy/scipy
File: scipy/sparse/linalg/tests/test_interface.py (Python)

Code before:
        """
        This check verifies the equivalence of the forward and adjoint computation,
        using `matvec` and `rmatvec` respectively, on randomised data.

        Data is generated with the real dtype `data_dtype` and operated on by the
        linear operator `op`.

        If `complex_data` is set to `True`, complex data is instead generated
        by combining randomised real and imaginary components, each generated
        with `data_dtype`.

        If `check_operators` is set to `True`, equivalence is checked between
        `matvec` and `*`, and between `rmatvec` and the composition of `.H` and `*`.

        If `check_dot` is set to `True`, equivalence is checked between
        `matvec` and `.dot`,
        and between `rmatvec` and the composition of `.H` and `.dot`.
        """
        rng = np.random.default_rng()

        u = rng.standard_normal(op.shape[-1], dtype=data_dtype)
        if complex_data:
            u = u + (1j * rng.standard_normal(op.shape[-1], dtype=data_dtype))
        v = rng.standard_normal(op.shape[-2], dtype=data_dtype)
        if complex_data:
            v = v + (1j * rng.standard_normal(op.shape[-2], dtype=data_dtype))

        op_u = op.matvec(u)
        opH_v = op.rmatvec(v)

        if check_operator:
            assert_allclose(op_u, op * u)
            assert_allclose(opH_v, op.H * v)

        if check_dot:
            assert_allclose(op_u, op.dot(u))
            assert_allclose(opH_v, op.H.dot(v))

        op_u_H_v = np_vecdot(op_u, v, axis=-1)
        uH_opH_v = np_vecdot(u, opH_v, axis=-1)

        rtol = 1e-12 if np.finfo(data_dtype).eps < 1e-8 else 1e-5
        assert_allclose(op_u_H_v, uH_opH_v, rtol=rtol)

    def check_matmat(
        self, op: interface.LinearOperator, data_dtype: str, complex_data: bool = False,
        check_operator: bool = False, check_dot: bool = False
    ):
        """
        This check verifies the equivalence of the forward and adjoint computation,
        using `matmat` and `rmatmat` respectively, on randomised data.

Review comment (suggestion):
```suggestion
        u = rng.standard_normal(op.shape[-1], dtype=data_dtype)
        v = rng.standard_normal(op.shape[-2], dtype=data_dtype)
        if complex_data:
            u = u + (1j * rng.standard_normal(op.shape[-1], dtype=data_dtype))
            v = v + (1j * rng.standard_normal(op.shape[-2], dtype=data_dtype))
```

Code after:
        """
        This check verifies the equivalence of the forward and adjoint computation,
        using `matvec` and `rmatvec` respectively, on randomised data.

        Data is generated with the real dtype `data_dtype` and operated on by the
        linear operator `op`.

        If `complex_data` is set to `True`, complex data is instead generated
        by combining randomised real and imaginary components, each generated
        with `data_dtype`.

        If `check_operators` is set to `True`, equivalence is checked between
        `matvec` and `*` and `@`,
        and between `rmatvec` and the composition of `.H` and `*`.

        If `check_dot` is set to `True`, equivalence is checked between
        `matvec` and `.dot`,
        and between `rmatvec` and the composition of `.H` and `.dot`.
        """
        rng = np.random.default_rng(42)

        u = rng.standard_normal(op.shape[-1], dtype=data_dtype)
        v = rng.standard_normal(op.shape[-2], dtype=data_dtype)
        if complex_data:
            u = u + (1j * rng.standard_normal(op.shape[-1], dtype=data_dtype))
            v = v + (1j * rng.standard_normal(op.shape[-2], dtype=data_dtype))

        op_u = op.matvec(u)
        opH_v = op.rmatvec(v)

        if check_operators:
            assert_allclose(op_u, op * u)
            assert_allclose(op_u, op @ u)
            assert_allclose(opH_v, op.H * v)
            assert_allclose(opH_v, op.H @ v)

        if check_dot:
            assert_allclose(op_u, op.dot(u))
            assert_allclose(opH_v, op.H.dot(v))

        op_u_H_v = np_vecdot(op_u, v, axis=-1)
        uH_opH_v = np_vecdot(u, opH_v, axis=-1)

        rtol = 1e-12 if np.finfo(data_dtype).eps < 1e-8 else 1e-5
        assert_allclose(op_u_H_v, uH_opH_v, rtol=rtol)

    def check_matmat(
        self, op: interface.LinearOperator, data_dtype: str, complex_data: bool = False,
        check_operators: bool = False, check_dot: bool = False
    ):
        """