Ejemplo n.º 1
0
def test_demo():
    V = [11.0, 12.0, 22.0]
    I = [0, 0, 1]
    J = [0, 1, 1]
    c = csr_matrix((V, (I, J)))
    print(c)
    # several constructor forms, as with csr_matrix:
    a = rsb_matrix((V, (I, J)))
    a = rsb_matrix((V, (I, J)), [3, 3])
    a = rsb_matrix((V, (I, J)), sym="S")  # symmetric example
    print(a)
    a = rsb_matrix((4, 4))
    a = rsb_matrix(c)
    nrhs = 1  # set to nrhs>1 to multiply by multiple vectors at once
    nr = a.shape[0]
    nc = a.shape[1]
    order = "F"
    x = numpy.empty([nc, nrhs], dtype=a.dtype, order=order)
    y = numpy.empty([nr, nrhs], dtype=a.dtype, order=order)
    x[:, :] = 1.0
    y[:, :] = 0.0
    print(a)
    print(x)
    print(y)
    # import pyrsb # import operators
    # a.autotune() # makes only sense for large matrices
    y = y + a * x
    # equivalent to y=y+c*x
    print(y)
    del a
Ejemplo n.º 2
0
def test_rescaled_f64():
    [V,I,J,nr,nc,nnz] = gen_tri(dtype=numpy.float64);
    cmat = csr_matrix((V, (I, J)))
    rmat = rsb_matrix((V, (I, J)),dtype=numpy.float64)
    rmat = rsb_matrix((V, (I, J)),dtype=numpy.float64).rescaled(2.0)
    x = gen_x(nc, dtype=numpy.float64)
    rmat.save()
    assert ( (rmat * x) == (2.0 * cmat * x) ).all()
Ejemplo n.º 3
0
def test_io_bytes_ctor(f_gen_tri):
    for dtype in rsb_dtypes:
        [sV,sI,sJ,nr,nc,nnz] = f_gen_tri
        smat = rsb_matrix((sV, (sI, sJ)))
        filename = b"pyrsb_test.tmp.mtx"
        smat.save(filename)
        lmat = rsb_matrix(filename,dtype=dtype)
        [lI,lJ,lV] = lmat.find();
        assert ( sV == lV ).all()
        assert ( sI == lI ).all()
        assert ( sJ == lJ ).all()
Ejemplo n.º 4
0
def test_init_tuple_csr():
    [V,J,P,nr,nc,nnz] = gen_tri_csr()
    mat = rsb_matrix((V, J, P),[nr,nc])
    assert mat.nnz == nnz
    assert mat.shape == (nr, nc)
    assert mat._is_unsymmetric() == True
    assert mat._get_symchar() == 'G'
Ejemplo n.º 5
0
def test_init_from_none():
    mat = rsb_matrix(None)
    assert mat.shape == (0, 0)
    assert mat.nnz == 0
    assert mat._is_unsymmetric() == True
    assert mat.ndim == 2
    assert mat.has_sorted_indices == False
Ejemplo n.º 6
0
def test_rescaled_any_type():
    for dtype in rsb_dtypes:
        [V,I,J,nr,nc,nnz] = gen_tri(dtype=dtype);
        cmat = csr_matrix((V, (I, J)))
        rmat = rsb_matrix((V, (I, J)),dtype=dtype).rescaled(2.0)
        x = gen_x(nc, dtype=dtype)
        assert ( (rmat * x) == (2.0 * cmat * x) ).all()
Ejemplo n.º 7
0
def test_init_tuples_sym(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    mat = rsb_matrix((V, (I, J)),sym="S")
    assert mat.shape == (nr, nc)
    assert mat.nnz == nnz
    assert mat._is_unsymmetric() == False
    assert mat._get_symchar() == 'S'
Ejemplo n.º 8
0
def test__otn2obc_rect_t(f_gen_rect):
    [V,I,J,nr,nc,nnz] = f_gen_rect
    rmat = rsb_matrix((V, (I, J)),[nr,nc])
    nrhs = 2
    (cm_o,cm_ldB,cm_ldC) = rmat._otn2obc(False,'T',nrhs)
    assert ( (cm_ldB,cm_ldC) == ( nr, nc ) )
    (rm_o,rm_ldB,rm_ldC) = rmat._otn2obc(True ,'T',nrhs)
    assert ( (rm_ldB,rm_ldC) == ( nrhs, nrhs ) )
Ejemplo n.º 9
0
def test_init_tuples_herm(f_gen_tri_complex):
    [V,I,J,nr,nc,nnz] = f_gen_tri_complex
    mat = rsb_matrix((V, (I, J)),sym="H",dtype=V.dtype) # Note: dtype not inherited from V.
    assert mat.shape == (nr, nc)
    assert mat.nnz == nnz
    assert mat._is_unsymmetric() == False
    assert mat._get_symchar() == 'H'
    assert mat._is_complex()
Ejemplo n.º 10
0
def test_init_tuples_and_dims(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    mat = rsb_matrix((V, (I, J)),[nr,nc])
    assert mat.shape == (nr, nc)
    assert mat.nnz == nnz
    assert mat._is_unsymmetric() == True
    assert mat._idx_bpnz() > 0
    assert mat._idx_bpnz() <= max_idx_bpnz
Ejemplo n.º 11
0
def test_init_from_none_dtype_D():
    mat = rsb_matrix(None,dtype='d')
    assert mat.shape == (0, 0)
    assert mat.nnz == 0
    assert mat._is_unsymmetric() == True
    assert mat.dtype == _dt2dt('d')
    assert mat.ndim == 2
    assert mat.has_sorted_indices == False
Ejemplo n.º 12
0
def test_spmm_permitted_mismatch(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    rmat = rsb_matrix((V, (I, J)))
    nrhs = 1
    x1 = gen_x(nc,nrhs,order='F')
    x2 = gen_x(nc,nrhs,order='C')
    assert ( (rmat * x1).shape == (rmat * x2).shape )
    assert ( (rmat * x1) == (rmat * x2) ).all()
Ejemplo n.º 13
0
def test__otn2obc_tri(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    rmat = rsb_matrix((V, (I, J)),[nr,nc])
    nrhs = 1
    (cm_o,cm_ldB,cm_ldC) = rmat._otn2obc(False,'N',nrhs)
    assert ( (cm_ldB,cm_ldC) == ( nr, nc ) )
    (rm_o,rm_ldB,rm_ldC) = rmat._otn2obc(True ,'N',nrhs)
    assert ( (rm_ldB,rm_ldC) == ( nrhs, nrhs ) )
Ejemplo n.º 14
0
def test_spmm_wrong_transA(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    rmat = rsb_matrix((V, (I, J)))
    nrhs = 2
    x = gen_x(nc,nrhs)
    y = numpy.empty([nr, nrhs], dtype=prv_t)
    y[:, :] = 0.0
    with assert_raises(ValueError):
       rmat._spmm(x,y,transA='?')
Ejemplo n.º 15
0
def test_spmv_matvec_gmres():
     for dtype in [float]:
        A = csr_matrix([[3, 2, 0], [1, -1, 0], [0, 5, 1]], dtype=dtype)
        A = rsb_matrix(A)
        n = A.shape[0]
        b = numpy.sin(numpy.array(range(1,n+1)))
        from scipy.sparse.linalg import gmres
        x, exitCode = gmres(A, b)
        assert ( exitCode == 0 )
Ejemplo n.º 16
0
def test_init_tuples(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    mat = rsb_matrix((V, (I, J)))
    assert mat.shape == (nr, nc)
    assert mat.nnz == nnz
    assert mat._is_unsymmetric() == True
    assert mat._get_typechar() in [ 'S', 'D', 'C', 'Z' ]
    assert mat._idx_bpnz() > 0
    assert mat._idx_bpnz() <= max_idx_bpnz
Ejemplo n.º 17
0
def test__find_block(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    rmat = rsb_matrix((V, (I, J)),[nr,nc])
    rmat._find_block(0,rmat.nr()-1,0,rmat.nc()-1)
    [rI,rJ,rV] = rmat.find();
    # order matters: won't work for any matrix
    assert ( V == rV ).all()
    assert ( I == rI ).all()
    assert ( J == rJ ).all()
Ejemplo n.º 18
0
def test_nonzero(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    rmat = rsb_matrix((V, (I, J)),[nr,nc])
    cmat = csr_matrix((V, (I, J)),[nr,nc])
    [cI,cJ] = cmat.nonzero();
    [rI,rJ] = rmat.nonzero();
    # order matters: won't work for any matrix
    assert ( cI == rI ).all()
    assert ( cJ == rJ ).all()
Ejemplo n.º 19
0
def test_init_from_none_none():
    mat = rsb_matrix(None,None)
    assert mat.shape == (0, 0)
    assert mat.nnz == 0
    assert mat._is_unsymmetric() == True
    assert mat._get_symchar() == 'G'
    #TODO; enable these one 1.2.0.10 and 1.3 available:
    #assert mat._idx_bpnz() > 0
    #assert mat._idx_bpnz() <= max_idx_bpnz
    assert mat._total_size > 0
Ejemplo n.º 20
0
def test_rescaled(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    cmat = csr_matrix((V, (I, J)))
    rmat = rsb_matrix((V, (I, J))).rescaled(2.0)
    x = gen_x(nc)
    assert ( (rmat * x) == (2.0 * cmat * x) ).all()
Ejemplo n.º 21
0
def test__spmul():
    for dtype in rsb_dtypes:
        [V,I,J,nr,nc,nnz] = gen_tri(dtype=dtype);
        cmat = csr_matrix((V, (I, J)))
        rmat = rsb_matrix((V, (I, J)))
        assert( (cmat*cmat).todense() == (rmat*rmat).todense() ).all()
Ejemplo n.º 22
0
def test_autotune_simple(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    omat = rsb_matrix((V, (I, J)))
    rmat = rsb_matrix((V, (I, J)))
    rmat.autotune()
    assert( rmat.todense() == omat.todense() ).all()
Ejemplo n.º 23
0
def test_init_from_dense():
    d = numpy.ones(shape=(2,2), dtype=prv_t)
    cmat = csr_matrix(d)
    rmat = rsb_matrix(cmat)
    assert ((cmat - rmat.tocsr())).nnz == 0
Ejemplo n.º 24
0
def test_init_from_csc(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    cmat = csc_matrix((V, (I, J)),[nr,nc])
    rmat = rsb_matrix(cmat)
    assert ((cmat - rmat.tocsr())).nnz == 0
Ejemplo n.º 25
0
def test_mini_self_print_test(f_gen_tri):
    """Call mini self test."""
    [V,I,J,nr,nc,nnz] = f_gen_tri
    rmat = rsb_matrix((V, (I, J)),[nr,nc])
    rmat._mini_self_print_test()
Ejemplo n.º 26
0
def f_gen_mats(request):
    [V,I,J,nr,nc,nnz] = gen_tri(dtype=request.param)
    rmat = rsb_matrix((V, (I, J)),[nr,nc])
    cmat = csr_matrix((V, (I, J)),[nr,nc])
    return [rmat,cmat]
Ejemplo n.º 27
0
def test_rescaled_c64():
    [V,I,J,nr,nc,nnz] = gen_tri(dtype=numpy.complex128);
    cmat = csr_matrix((V, (I, J)))
    rmat = rsb_matrix((V, (I, J)),dtype=numpy.complex128).rescaled(2.0)
    x = gen_x(nc, dtype=numpy.complex128)
    assert ( (rmat * x) == (2.0 * cmat * x) ).all()
Ejemplo n.º 28
0
def test_render_to_stdout():
    mat = rsb_matrix([1,1],dtype='d')
    mat.render()
Ejemplo n.º 29
0
def test_init_tuples_wrong_sym(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    with assert_raises(ValueError):
        mat = rsb_matrix((V, (I, J)),sym="W")
Ejemplo n.º 30
0
def test_do_print(f_gen_tri):
    [V,I,J,nr,nc,nnz] = f_gen_tri
    rmat = rsb_matrix((V, (I, J)),[nr,nc])
    rmat.do_print(brief=True)
    rmat.do_print(brief=False)