def test_sparse_formats(spformat: str,
                        sparse_matrix_class: scipy.sparse.spmatrix,
                        rng: np.random.Generator):
    """Test whether sparse matrices in different formats can be created."""

    # Scipy warns that creating DIA matrices with many diagonals is inefficient.
    # This should not dilute the test output, as the tests
    # only checks the *ability* to create large random sparse matrices.
    if spformat == "dia":
        with pytest.warns(scipy.sparse.SparseEfficiencyWarning):
            sparse_mat = random_sparse_spd_matrix(rng=rng,
                                                  dim=1000,
                                                  density=10**-3,
                                                  format=spformat)
    else:
        sparse_mat = random_sparse_spd_matrix(rng=rng,
                                              dim=1000,
                                              density=10**-3,
                                              format=spformat)
    assert isinstance(sparse_mat, sparse_matrix_class)
 def setUp(self) -> None:
     """Define parameters and define test problems."""
     self.rng = np.random.default_rng(42)
     self.dim_list = [1, 2, 25, 100, 250]
     self.spd_matrices = [
         random_spd_matrix(dim=n, random_state=self.rng)
         for n in self.dim_list
     ]
     self.density = 0.01
     self.sparse_spd_matrices = [
         random_sparse_spd_matrix(dim=n,
                                  density=self.density,
                                  random_state=self.rng)
         for n in self.dim_list
     ]
     self.matrices = self.spd_matrices + self.sparse_spd_matrices
Example #3
0
def get_linear_system(name: str, dim: int):
    rng = np.random.default_rng(0)

    if name == "dense":
        if dim > 1000:
            raise NotImplementedError()
        A = random_spd_matrix(rng=rng, dim=dim)
    elif name == "sparse":
        A = random_sparse_spd_matrix(rng=rng,
                                     dim=dim,
                                     density=np.minimum(1.0, 1000 / dim**2))
    elif name == "linop":
        if dim > 100:
            raise NotImplementedError()
            # TODO: Larger benchmarks currently fail. Remove once PLS refactor (https://github.com/probabilistic-numerics/probnum/issues/51) is resolved
        A = linops.Scaling(factors=rng.normal(size=(dim, )))
    else:
        raise NotImplementedError()

    solution = rng.normal(size=(dim, ))
    b = A @ solution
    return problems.LinearSystem(A=A, b=b, solution=solution)
Example #4
0
def case_random_sparse_spd_matrix(
        rng: np.random.Generator) -> scipy.sparse.spmatrix:
    return random_sparse_spd_matrix(dim=1000, density=0.01, rng=rng)
Example #5
0
def case_random_sparse_spd_linsys(ncols: int, ) -> problems.LinearSystem:
    rng = np.random.default_rng(1)
    A = random_sparse_spd_matrix(rng=rng, dim=ncols, density=0.1)
    x = rng.normal(size=(ncols, ))
    b = A @ x
    return problems.LinearSystem(A=A, b=b, solution=x)
def test_large_sparse_matrix(rng: np.random.Generator):
    """Test whether a large random spd matrix can be created."""
    n = 10**5
    sparse_mat = random_sparse_spd_matrix(rng=rng, dim=n, density=10**-8)
    assert sparse_mat.shape == (n, n)
Example #7
0
def rnd_sparse_spd_mat(n_cols: int, density: float,
                       rng: np.random.Generator) -> scipy.sparse.spmatrix:
    """Random sparse spd matrix generated from :meth:`random_sparse_spd_matrix`."""
    return random_sparse_spd_matrix(rng=rng, dim=n_cols, density=density)