123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- """test sparse matrix construction functions"""
- import numpy as np
- from numpy import array
- from numpy.testing import (assert_equal, assert_,
- assert_array_equal, assert_array_almost_equal_nulp)
- import pytest
- from pytest import raises as assert_raises
- from scipy._lib._testutils import check_free_memory
- from scipy._lib._util import check_random_state
- from scipy.sparse import (csr_matrix, coo_matrix,
- _construct as construct)
- from scipy.sparse._construct import rand as sprand
- from scipy.sparse._sputils import matrix
- sparse_formats = ['csr','csc','coo','bsr','dia','lil','dok']
- #TODO check whether format=XXX is respected
- def _sprandn(m, n, density=0.01, format="coo", dtype=None, random_state=None):
- # Helper function for testing.
- random_state = check_random_state(random_state)
- data_rvs = random_state.standard_normal
- return construct.random(m, n, density, format, dtype,
- random_state, data_rvs)
- class TestConstructUtils:
- def test_spdiags(self):
- diags1 = array([[1, 2, 3, 4, 5]])
- diags2 = array([[1, 2, 3, 4, 5],
- [6, 7, 8, 9,10]])
- diags3 = array([[1, 2, 3, 4, 5],
- [6, 7, 8, 9,10],
- [11,12,13,14,15]])
- cases = []
- cases.append((diags1, 0, 1, 1, [[1]]))
- cases.append((diags1, [0], 1, 1, [[1]]))
- cases.append((diags1, [0], 2, 1, [[1],[0]]))
- cases.append((diags1, [0], 1, 2, [[1,0]]))
- cases.append((diags1, [1], 1, 2, [[0,2]]))
- cases.append((diags1,[-1], 1, 2, [[0,0]]))
- cases.append((diags1, [0], 2, 2, [[1,0],[0,2]]))
- cases.append((diags1,[-1], 2, 2, [[0,0],[1,0]]))
- cases.append((diags1, [3], 2, 2, [[0,0],[0,0]]))
- cases.append((diags1, [0], 3, 4, [[1,0,0,0],[0,2,0,0],[0,0,3,0]]))
- cases.append((diags1, [1], 3, 4, [[0,2,0,0],[0,0,3,0],[0,0,0,4]]))
- cases.append((diags1, [2], 3, 5, [[0,0,3,0,0],[0,0,0,4,0],[0,0,0,0,5]]))
- cases.append((diags2, [0,2], 3, 3, [[1,0,8],[0,2,0],[0,0,3]]))
- cases.append((diags2, [-1,0], 3, 4, [[6,0,0,0],[1,7,0,0],[0,2,8,0]]))
- cases.append((diags2, [2,-3], 6, 6, [[0,0,3,0,0,0],
- [0,0,0,4,0,0],
- [0,0,0,0,5,0],
- [6,0,0,0,0,0],
- [0,7,0,0,0,0],
- [0,0,8,0,0,0]]))
- cases.append((diags3, [-1,0,1], 6, 6, [[6,12, 0, 0, 0, 0],
- [1, 7,13, 0, 0, 0],
- [0, 2, 8,14, 0, 0],
- [0, 0, 3, 9,15, 0],
- [0, 0, 0, 4,10, 0],
- [0, 0, 0, 0, 5, 0]]))
- cases.append((diags3, [-4,2,-1], 6, 5, [[0, 0, 8, 0, 0],
- [11, 0, 0, 9, 0],
- [0,12, 0, 0,10],
- [0, 0,13, 0, 0],
- [1, 0, 0,14, 0],
- [0, 2, 0, 0,15]]))
- cases.append((diags3, [-1, 1, 2], len(diags3[0]), len(diags3[0]),
- [[0, 7, 13, 0, 0],
- [1, 0, 8, 14, 0],
- [0, 2, 0, 9, 15],
- [0, 0, 3, 0, 10],
- [0, 0, 0, 4, 0]]))
- for d, o, m, n, result in cases:
- if len(d[0]) == m and m == n:
- assert_equal(construct.spdiags(d, o).toarray(), result)
- assert_equal(construct.spdiags(d, o, m, n).toarray(), result)
- assert_equal(construct.spdiags(d, o, (m, n)).toarray(), result)
- def test_diags(self):
- a = array([1, 2, 3, 4, 5])
- b = array([6, 7, 8, 9, 10])
- c = array([11, 12, 13, 14, 15])
- cases = []
- cases.append((a[:1], 0, (1, 1), [[1]]))
- cases.append(([a[:1]], [0], (1, 1), [[1]]))
- cases.append(([a[:1]], [0], (2, 1), [[1],[0]]))
- cases.append(([a[:1]], [0], (1, 2), [[1,0]]))
- cases.append(([a[:1]], [1], (1, 2), [[0,1]]))
- cases.append(([a[:2]], [0], (2, 2), [[1,0],[0,2]]))
- cases.append(([a[:1]],[-1], (2, 2), [[0,0],[1,0]]))
- cases.append(([a[:3]], [0], (3, 4), [[1,0,0,0],[0,2,0,0],[0,0,3,0]]))
- cases.append(([a[:3]], [1], (3, 4), [[0,1,0,0],[0,0,2,0],[0,0,0,3]]))
- cases.append(([a[:1]], [-2], (3, 5), [[0,0,0,0,0],[0,0,0,0,0],[1,0,0,0,0]]))
- cases.append(([a[:2]], [-1], (3, 5), [[0,0,0,0,0],[1,0,0,0,0],[0,2,0,0,0]]))
- cases.append(([a[:3]], [0], (3, 5), [[1,0,0,0,0],[0,2,0,0,0],[0,0,3,0,0]]))
- cases.append(([a[:3]], [1], (3, 5), [[0,1,0,0,0],[0,0,2,0,0],[0,0,0,3,0]]))
- cases.append(([a[:3]], [2], (3, 5), [[0,0,1,0,0],[0,0,0,2,0],[0,0,0,0,3]]))
- cases.append(([a[:2]], [3], (3, 5), [[0,0,0,1,0],[0,0,0,0,2],[0,0,0,0,0]]))
- cases.append(([a[:1]], [4], (3, 5), [[0,0,0,0,1],[0,0,0,0,0],[0,0,0,0,0]]))
- cases.append(([a[:1]], [-4], (5, 3), [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[1,0,0]]))
- cases.append(([a[:2]], [-3], (5, 3), [[0,0,0],[0,0,0],[0,0,0],[1,0,0],[0,2,0]]))
- cases.append(([a[:3]], [-2], (5, 3), [[0,0,0],[0,0,0],[1,0,0],[0,2,0],[0,0,3]]))
- cases.append(([a[:3]], [-1], (5, 3), [[0,0,0],[1,0,0],[0,2,0],[0,0,3],[0,0,0]]))
- cases.append(([a[:3]], [0], (5, 3), [[1,0,0],[0,2,0],[0,0,3],[0,0,0],[0,0,0]]))
- cases.append(([a[:2]], [1], (5, 3), [[0,1,0],[0,0,2],[0,0,0],[0,0,0],[0,0,0]]))
- cases.append(([a[:1]], [2], (5, 3), [[0,0,1],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]))
- cases.append(([a[:3],b[:1]], [0,2], (3, 3), [[1,0,6],[0,2,0],[0,0,3]]))
- cases.append(([a[:2],b[:3]], [-1,0], (3, 4), [[6,0,0,0],[1,7,0,0],[0,2,8,0]]))
- cases.append(([a[:4],b[:3]], [2,-3], (6, 6), [[0,0,1,0,0,0],
- [0,0,0,2,0,0],
- [0,0,0,0,3,0],
- [6,0,0,0,0,4],
- [0,7,0,0,0,0],
- [0,0,8,0,0,0]]))
- cases.append(([a[:4],b,c[:4]], [-1,0,1], (5, 5), [[6,11, 0, 0, 0],
- [1, 7,12, 0, 0],
- [0, 2, 8,13, 0],
- [0, 0, 3, 9,14],
- [0, 0, 0, 4,10]]))
- cases.append(([a[:2],b[:3],c], [-4,2,-1], (6, 5), [[0, 0, 6, 0, 0],
- [11, 0, 0, 7, 0],
- [0,12, 0, 0, 8],
- [0, 0,13, 0, 0],
- [1, 0, 0,14, 0],
- [0, 2, 0, 0,15]]))
- # too long arrays are OK
- cases.append(([a], [0], (1, 1), [[1]]))
- cases.append(([a[:3],b], [0,2], (3, 3), [[1, 0, 6], [0, 2, 0], [0, 0, 3]]))
- cases.append((np.array([[1, 2, 3], [4, 5, 6]]), [0,-1], (3, 3), [[1, 0, 0], [4, 2, 0], [0, 5, 3]]))
- # scalar case: broadcasting
- cases.append(([1,-2,1], [1,0,-1], (3, 3), [[-2, 1, 0],
- [1, -2, 1],
- [0, 1, -2]]))
- for d, o, shape, result in cases:
- err_msg = "%r %r %r %r" % (d, o, shape, result)
- assert_equal(construct.diags(d, o, shape=shape).toarray(),
- result, err_msg=err_msg)
- if shape[0] == shape[1] and hasattr(d[0], '__len__') and len(d[0]) <= max(shape):
- # should be able to find the shape automatically
- assert_equal(construct.diags(d, o).toarray(), result,
- err_msg=err_msg)
- def test_diags_default(self):
- a = array([1, 2, 3, 4, 5])
- assert_equal(construct.diags(a).toarray(), np.diag(a))
- def test_diags_default_bad(self):
- a = array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6]])
- assert_raises(ValueError, construct.diags, a)
- def test_diags_bad(self):
- a = array([1, 2, 3, 4, 5])
- b = array([6, 7, 8, 9, 10])
- c = array([11, 12, 13, 14, 15])
- cases = []
- cases.append(([a[:0]], 0, (1, 1)))
- cases.append(([a[:4],b,c[:3]], [-1,0,1], (5, 5)))
- cases.append(([a[:2],c,b[:3]], [-4,2,-1], (6, 5)))
- cases.append(([a[:2],c,b[:3]], [-4,2,-1], None))
- cases.append(([], [-4,2,-1], None))
- cases.append(([1], [-5], (4, 4)))
- cases.append(([a], 0, None))
- for d, o, shape in cases:
- assert_raises(ValueError, construct.diags, d, o, shape)
- assert_raises(TypeError, construct.diags, [[None]], [0])
- def test_diags_vs_diag(self):
- # Check that
- #
- # diags([a, b, ...], [i, j, ...]) == diag(a, i) + diag(b, j) + ...
- #
- np.random.seed(1234)
- for n_diags in [1, 2, 3, 4, 5, 10]:
- n = 1 + n_diags//2 + np.random.randint(0, 10)
- offsets = np.arange(-n+1, n-1)
- np.random.shuffle(offsets)
- offsets = offsets[:n_diags]
- diagonals = [np.random.rand(n - abs(q)) for q in offsets]
- mat = construct.diags(diagonals, offsets)
- dense_mat = sum([np.diag(x, j) for x, j in zip(diagonals, offsets)])
- assert_array_almost_equal_nulp(mat.toarray(), dense_mat)
- if len(offsets) == 1:
- mat = construct.diags(diagonals[0], offsets[0])
- dense_mat = np.diag(diagonals[0], offsets[0])
- assert_array_almost_equal_nulp(mat.toarray(), dense_mat)
- def test_diags_dtype(self):
- x = construct.diags([2.2], [0], shape=(2, 2), dtype=int)
- assert_equal(x.dtype, int)
- assert_equal(x.toarray(), [[2, 0], [0, 2]])
- def test_diags_one_diagonal(self):
- d = list(range(5))
- for k in range(-5, 6):
- assert_equal(construct.diags(d, k).toarray(),
- construct.diags([d], [k]).toarray())
- def test_diags_empty(self):
- x = construct.diags([])
- assert_equal(x.shape, (0, 0))
- def test_identity(self):
- assert_equal(construct.identity(1).toarray(), [[1]])
- assert_equal(construct.identity(2).toarray(), [[1,0],[0,1]])
- I = construct.identity(3, dtype='int8', format='dia')
- assert_equal(I.dtype, np.dtype('int8'))
- assert_equal(I.format, 'dia')
- for fmt in sparse_formats:
- I = construct.identity(3, format=fmt)
- assert_equal(I.format, fmt)
- assert_equal(I.toarray(), [[1,0,0],[0,1,0],[0,0,1]])
- def test_eye(self):
- assert_equal(construct.eye(1,1).toarray(), [[1]])
- assert_equal(construct.eye(2,3).toarray(), [[1,0,0],[0,1,0]])
- assert_equal(construct.eye(3,2).toarray(), [[1,0],[0,1],[0,0]])
- assert_equal(construct.eye(3,3).toarray(), [[1,0,0],[0,1,0],[0,0,1]])
- assert_equal(construct.eye(3,3,dtype='int16').dtype, np.dtype('int16'))
- for m in [3, 5]:
- for n in [3, 5]:
- for k in range(-5,6):
- assert_equal(construct.eye(m, n, k=k).toarray(), np.eye(m, n, k=k))
- if m == n:
- assert_equal(construct.eye(m, k=k).toarray(), np.eye(m, n, k=k))
- def test_eye_one(self):
- assert_equal(construct.eye(1).toarray(), [[1]])
- assert_equal(construct.eye(2).toarray(), [[1,0],[0,1]])
- I = construct.eye(3, dtype='int8', format='dia')
- assert_equal(I.dtype, np.dtype('int8'))
- assert_equal(I.format, 'dia')
- for fmt in sparse_formats:
- I = construct.eye(3, format=fmt)
- assert_equal(I.format, fmt)
- assert_equal(I.toarray(), [[1,0,0],[0,1,0],[0,0,1]])
- def test_kron(self):
- cases = []
- cases.append(array([[0]]))
- cases.append(array([[-1]]))
- cases.append(array([[4]]))
- cases.append(array([[10]]))
- cases.append(array([[0],[0]]))
- cases.append(array([[0,0]]))
- cases.append(array([[1,2],[3,4]]))
- cases.append(array([[0,2],[5,0]]))
- cases.append(array([[0,2,-6],[8,0,14]]))
- cases.append(array([[5,4],[0,0],[6,0]]))
- cases.append(array([[5,4,4],[1,0,0],[6,0,8]]))
- cases.append(array([[0,1,0,2,0,5,8]]))
- cases.append(array([[0.5,0.125,0,3.25],[0,2.5,0,0]]))
- for a in cases:
- for b in cases:
- expected = np.kron(a, b)
- for fmt in sparse_formats:
- result = construct.kron(csr_matrix(a), csr_matrix(b), format=fmt)
- assert_equal(result.format, fmt)
- assert_array_equal(result.toarray(), expected)
- def test_kron_large(self):
- n = 2**16
- a = construct.eye(1, n, n-1)
- b = construct.eye(n, 1, 1-n)
- construct.kron(a, a)
- construct.kron(b, b)
- def test_kronsum(self):
- cases = []
- cases.append(array([[0]]))
- cases.append(array([[-1]]))
- cases.append(array([[4]]))
- cases.append(array([[10]]))
- cases.append(array([[1,2],[3,4]]))
- cases.append(array([[0,2],[5,0]]))
- cases.append(array([[0,2,-6],[8,0,14],[0,3,0]]))
- cases.append(array([[1,0,0],[0,5,-1],[4,-2,8]]))
- for a in cases:
- for b in cases:
- result = construct.kronsum(
- csr_matrix(a), csr_matrix(b)).toarray()
- expected = np.kron(np.eye(len(b)), a) + \
- np.kron(b, np.eye(len(a)))
- assert_array_equal(result,expected)
- def test_vstack(self):
- A = coo_matrix([[1,2],[3,4]])
- B = coo_matrix([[5,6]])
- expected = array([[1, 2],
- [3, 4],
- [5, 6]])
- assert_equal(construct.vstack([A, B]).toarray(), expected)
- assert_equal(construct.vstack([A, B], dtype=np.float32).dtype,
- np.float32)
- assert_equal(construct.vstack([A.tocsr(), B.tocsr()]).toarray(),
- expected)
- result = construct.vstack([A.tocsr(), B.tocsr()], dtype=np.float32)
- assert_equal(result.dtype, np.float32)
- assert_equal(result.indices.dtype, np.int32)
- assert_equal(result.indptr.dtype, np.int32)
- assert_equal(construct.vstack([A.tocsc(), B.tocsc()]).toarray(),
- expected)
- result = construct.vstack([A.tocsc(), B.tocsc()], dtype=np.float32)
- assert_equal(result.dtype, np.float32)
- assert_equal(result.indices.dtype, np.int32)
- assert_equal(result.indptr.dtype, np.int32)
- def test_hstack(self):
- A = coo_matrix([[1,2],[3,4]])
- B = coo_matrix([[5],[6]])
- expected = array([[1, 2, 5],
- [3, 4, 6]])
- assert_equal(construct.hstack([A, B]).toarray(), expected)
- assert_equal(construct.hstack([A, B], dtype=np.float32).dtype,
- np.float32)
- assert_equal(construct.hstack([A.tocsc(), B.tocsc()]).toarray(),
- expected)
- assert_equal(construct.hstack([A.tocsc(), B.tocsc()],
- dtype=np.float32).dtype,
- np.float32)
- assert_equal(construct.hstack([A.tocsr(), B.tocsr()]).toarray(),
- expected)
- assert_equal(construct.hstack([A.tocsr(), B.tocsr()],
- dtype=np.float32).dtype,
- np.float32)
- def test_bmat(self):
- A = coo_matrix([[1, 2], [3, 4]])
- B = coo_matrix([[5],[6]])
- C = coo_matrix([[7]])
- D = coo_matrix((0, 0))
- expected = array([[1, 2, 5],
- [3, 4, 6],
- [0, 0, 7]])
- assert_equal(construct.bmat([[A, B], [None, C]]).toarray(), expected)
- E = csr_matrix((1, 2), dtype=np.int32)
- assert_equal(construct.bmat([[A.tocsr(), B.tocsr()],
- [E, C.tocsr()]]).toarray(),
- expected)
- assert_equal(construct.bmat([[A.tocsc(), B.tocsc()],
- [E.tocsc(), C.tocsc()]]).toarray(),
- expected)
- expected = array([[1, 2, 0],
- [3, 4, 0],
- [0, 0, 7]])
- assert_equal(construct.bmat([[A, None], [None, C]]).toarray(),
- expected)
- assert_equal(construct.bmat([[A.tocsr(), E.T.tocsr()],
- [E, C.tocsr()]]).toarray(),
- expected)
- assert_equal(construct.bmat([[A.tocsc(), E.T.tocsc()],
- [E.tocsc(), C.tocsc()]]).toarray(),
- expected)
- Z = csr_matrix((1, 1), dtype=np.int32)
- expected = array([[0, 5],
- [0, 6],
- [7, 0]])
- assert_equal(construct.bmat([[None, B], [C, None]]).toarray(),
- expected)
- assert_equal(construct.bmat([[E.T.tocsr(), B.tocsr()],
- [C.tocsr(), Z]]).toarray(),
- expected)
- assert_equal(construct.bmat([[E.T.tocsc(), B.tocsc()],
- [C.tocsc(), Z.tocsc()]]).toarray(),
- expected)
- expected = matrix(np.empty((0, 0)))
- assert_equal(construct.bmat([[None, None]]).toarray(), expected)
- assert_equal(construct.bmat([[None, D], [D, None]]).toarray(),
- expected)
- # test bug reported in gh-5976
- expected = array([[7]])
- assert_equal(construct.bmat([[None, D], [C, None]]).toarray(),
- expected)
- # test failure cases
- with assert_raises(ValueError) as excinfo:
- construct.bmat([[A], [B]])
- excinfo.match(r'Got blocks\[1,0\]\.shape\[1\] == 1, expected 2')
- with assert_raises(ValueError) as excinfo:
- construct.bmat([[A.tocsr()], [B.tocsr()]])
- excinfo.match(r'incompatible dimensions for axis 1')
- with assert_raises(ValueError) as excinfo:
- construct.bmat([[A.tocsc()], [B.tocsc()]])
- excinfo.match(r'Mismatching dimensions along axis 1: ({1, 2}|{2, 1})')
- with assert_raises(ValueError) as excinfo:
- construct.bmat([[A, C]])
- excinfo.match(r'Got blocks\[0,1\]\.shape\[0\] == 1, expected 2')
- with assert_raises(ValueError) as excinfo:
- construct.bmat([[A.tocsr(), C.tocsr()]])
- excinfo.match(r'Mismatching dimensions along axis 0: ({1, 2}|{2, 1})')
- with assert_raises(ValueError) as excinfo:
- construct.bmat([[A.tocsc(), C.tocsc()]])
- excinfo.match(r'incompatible dimensions for axis 0')
- @pytest.mark.slow
- @pytest.mark.xfail_on_32bit("Can't create large array for test")
- def test_concatenate_int32_overflow(self):
- """ test for indptr overflow when concatenating matrices """
- check_free_memory(30000)
- n = 33000
- A = csr_matrix(np.ones((n, n), dtype=bool))
- B = A.copy()
- C = construct._compressed_sparse_stack((A,B), 0)
- assert_(np.all(np.equal(np.diff(C.indptr), n)))
- assert_equal(C.indices.dtype, np.int64)
- assert_equal(C.indptr.dtype, np.int64)
- def test_block_diag_basic(self):
- """ basic test for block_diag """
- A = coo_matrix([[1,2],[3,4]])
- B = coo_matrix([[5],[6]])
- C = coo_matrix([[7]])
- expected = array([[1, 2, 0, 0],
- [3, 4, 0, 0],
- [0, 0, 5, 0],
- [0, 0, 6, 0],
- [0, 0, 0, 7]])
- assert_equal(construct.block_diag((A, B, C)).toarray(), expected)
- def test_block_diag_scalar_1d_args(self):
- """ block_diag with scalar and 1d arguments """
- # one 1d matrix and a scalar
- assert_array_equal(construct.block_diag([[2,3], 4]).toarray(),
- [[2, 3, 0], [0, 0, 4]])
- def test_block_diag_1(self):
- """ block_diag with one matrix """
- assert_equal(construct.block_diag([[1, 0]]).toarray(),
- array([[1, 0]]))
- assert_equal(construct.block_diag([[[1, 0]]]).toarray(),
- array([[1, 0]]))
- assert_equal(construct.block_diag([[[1], [0]]]).toarray(),
- array([[1], [0]]))
- # just on scalar
- assert_equal(construct.block_diag([1]).toarray(),
- array([[1]]))
- def test_block_diag_sparse_matrices(self):
- """ block_diag with sparse matrices """
- sparse_col_matrices = [coo_matrix(([[1, 2, 3]]), shape=(1, 3)),
- coo_matrix(([[4, 5]]), shape=(1, 2))]
- block_sparse_cols_matrices = construct.block_diag(sparse_col_matrices)
- assert_equal(block_sparse_cols_matrices.toarray(),
- array([[1, 2, 3, 0, 0], [0, 0, 0, 4, 5]]))
- sparse_row_matrices = [coo_matrix(([[1], [2], [3]]), shape=(3, 1)),
- coo_matrix(([[4], [5]]), shape=(2, 1))]
- block_sparse_row_matrices = construct.block_diag(sparse_row_matrices)
- assert_equal(block_sparse_row_matrices.toarray(),
- array([[1, 0], [2, 0], [3, 0], [0, 4], [0, 5]]))
- def test_random_sampling(self):
- # Simple sanity checks for sparse random sampling.
- for f in sprand, _sprandn:
- for t in [np.float32, np.float64, np.longdouble,
- np.int32, np.int64, np.complex64, np.complex128]:
- x = f(5, 10, density=0.1, dtype=t)
- assert_equal(x.dtype, t)
- assert_equal(x.shape, (5, 10))
- assert_equal(x.nnz, 5)
- x1 = f(5, 10, density=0.1, random_state=4321)
- assert_equal(x1.dtype, np.double)
- x2 = f(5, 10, density=0.1,
- random_state=np.random.RandomState(4321))
- assert_array_equal(x1.data, x2.data)
- assert_array_equal(x1.row, x2.row)
- assert_array_equal(x1.col, x2.col)
- for density in [0.0, 0.1, 0.5, 1.0]:
- x = f(5, 10, density=density)
- assert_equal(x.nnz, int(density * np.prod(x.shape)))
- for fmt in ['coo', 'csc', 'csr', 'lil']:
- x = f(5, 10, format=fmt)
- assert_equal(x.format, fmt)
- assert_raises(ValueError, lambda: f(5, 10, 1.1))
- assert_raises(ValueError, lambda: f(5, 10, -0.1))
- def test_rand(self):
- # Simple distributional checks for sparse.rand.
- random_states = [None, 4321, np.random.RandomState()]
- try:
- gen = np.random.default_rng()
- random_states.append(gen)
- except AttributeError:
- pass
- for random_state in random_states:
- x = sprand(10, 20, density=0.5, dtype=np.float64,
- random_state=random_state)
- assert_(np.all(np.less_equal(0, x.data)))
- assert_(np.all(np.less_equal(x.data, 1)))
- def test_randn(self):
- # Simple distributional checks for sparse.randn.
- # Statistically, some of these should be negative
- # and some should be greater than 1.
- random_states = [None, 4321, np.random.RandomState()]
- try:
- gen = np.random.default_rng()
- random_states.append(gen)
- except AttributeError:
- pass
- for random_state in random_states:
- x = _sprandn(10, 20, density=0.5, dtype=np.float64,
- random_state=random_state)
- assert_(np.any(np.less(x.data, 0)))
- assert_(np.any(np.less(1, x.data)))
- def test_random_accept_str_dtype(self):
- # anything that np.dtype can convert to a dtype should be accepted
- # for the dtype
- construct.random(10, 10, dtype='d')
- def test_random_sparse_matrix_returns_correct_number_of_non_zero_elements(self):
- # A 10 x 10 matrix, with density of 12.65%, should have 13 nonzero elements.
- # 10 x 10 x 0.1265 = 12.65, which should be rounded up to 13, not 12.
- sparse_matrix = construct.random(10, 10, density=0.1265)
- assert_equal(sparse_matrix.count_nonzero(),13)
|