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

Code before:
        assert u.dtype == u0.dtype
        assert v.dtype == v0.dtype
        assert s.dtype == s0.dtype

        u, s, v = svd(a, full_matrices=False)
        assert_allclose(u, np.empty((m, 0)))
        assert_allclose(s, np.empty((0,)))
        assert_allclose(v, np.empty((0, n)))

        assert u.dtype == u0.dtype
        assert v.dtype == v0.dtype
        assert s.dtype == s0.dtype

        s = svd(a, compute_uv=False)
        assert_allclose(s, np.empty((0,)))

        assert s.dtype == s0.dtype

class TestSVD_GESVD(TestSVD_GESDD):
    lapack_driver = 'gesvd'


# Allocating an array of such a size leads to _ArrayMemoryError(s)
# since the maximum memory that can be in 32-bit (WASM) is 4GB
@pytest.mark.skipif(IS_WASM, reason="out of memory in WASM")
@pytest.mark.parallel_threads(2)  # 1.9 GiB per thread RAM usage
@pytest.mark.fail_slow(10)
def test_svd_gesdd_nofegfault():
    # svd(a) with {U,VT}.size > INT_MAX does not segfault
    # cf https://github.com/scipy/scipy/issues/14001
    df=np.ones((4799, 53130), dtype=np.float64)
    with assert_raises(ValueError):
        svd(df)


def test_gesdd_nan_error_message():
    A = np.eye(2)
    A[0, 0] = np.nan
    with pytest.raises(ValueError, match="NaN"):
        svd(A, check_finite=False)


class TestSVDVals:

    @pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
    def test_empty(self, dt):
        for a in [[]], np.empty((2, 0)), np.ones((0, 3)):
            a = np.array(a, dtype=dt)
            s = svdvals(a)
            assert_equal(s, np.empty(0))


Review comment (bug):
@ev-br thought I should ping you here. Is it OK to skip this on 32-bit? Even after the [other work](https://github.com/scipy/scipy/blame/1dd806b71cb952e563dfa187a3ec77e745501b59/scipy/linalg/tests/test_decomp.py#L1220) adjusting this test, it's [still failing](https://github.com/scipy/scipy/actions/runs/18619998392/job/53097937434) on the 32-bit workflow.

Code after:
        assert u.dtype == u0.dtype
        assert v.dtype == v0.dtype
        assert s.dtype == s0.dtype

        u, s, v = svd(a, full_matrices=False)
        assert_allclose(u, np.empty((m, 0)))
        assert_allclose(s, np.empty((0,)))
        assert_allclose(v, np.empty((0, n)))

        assert u.dtype == u0.dtype
        assert v.dtype == v0.dtype
        assert s.dtype == s0.dtype

        s = svd(a, compute_uv=False)
        assert_allclose(s, np.empty((0,)))

        assert s.dtype == s0.dtype

class TestSVD_GESVD(TestSVD_GESDD):
    lapack_driver = 'gesvd'


# Allocating an array of such a size leads to _ArrayMemoryError(s)
# since the maximum memory that can be in 32-bit (WASM) is 4GB
@pytest.mark.skipif(IS_WASM, reason="out of memory in WASM")
@pytest.mark.xfail_on_32bit("out of memory in 32-bit CI workflow")
@pytest.mark.parallel_threads(2)  # 1.9 GiB per thread RAM usage
@pytest.mark.fail_slow(10)
def test_svd_gesdd_nofegfault():
    # svd(a) with {U,VT}.size > INT_MAX does not segfault
    # cf https://github.com/scipy/scipy/issues/14001
    df=np.ones((4799, 53130), dtype=np.float64)
    with assert_raises(ValueError):
        svd(df)


def test_gesdd_nan_error_message():
    A = np.eye(2)
    A[0, 0] = np.nan
    with pytest.raises(ValueError, match="NaN"):
        svd(A, check_finite=False)


class TestSVDVals:

    @pytest.mark.parametrize('dt', [int, float, np.float32, complex, np.complex64])
    def test_empty(self, dt):
        for a in [[]], np.empty((2, 0)), np.ones((0, 3)):
            a = np.array(a, dtype=dt)
            s = svdvals(a)
            assert_equal(s, np.empty(0))