예제 #1
0
def test_nnz_sparsity(sqlmatrix, r, c, nset):
    M = matrix.SqlMatrix(r, c)
    pos = [(i, j) for i in range(r) for j in range(c)][:nset]
    for i, j in pos:
        M.set(i, j, i * j - i + j + 0.5)
    assert M.nnz == len(pos)
    assert M.sparsity == 1.0 - len(pos) / (r * c)
    Ilist, Jlist = M.nonzero()
    assert set(zip(Ilist, Jlist)) == set(pos)
    M.destroy()
예제 #2
0
def test_scale(sqlmatrix, random, r, c, scalar):
    A = numpy.matrix(numpy.random.normal(size=(r, c)))
    A2 = matrix.SqlMatrix.from_dense(A)
    RES = matrix.SqlMatrix(r, c)
    resptr = A2.scale(scalar, RES)
    assert _is_close(A * scalar, RES.to_dense())  #The result is correct
    assert _is_close(A, A2.to_dense())  #The input is unchanged
    assert resptr._table == RES._table  #The returned matrix is correct
    A2.destroy()
    RES.destroy()
예제 #3
0
def test_pow(sqlmatrix, random, n, exp):
    A = 10.0 * numpy.matrix(numpy.random.normal(size=(n, n)))
    A2 = matrix.SqlMatrix.from_dense(A)
    R = matrix.SqlMatrix(n, n)
    resptr = A2.pow(exp, R)
    assert _is_close(A**exp, R.to_dense())  #The result is correct
    assert _is_close(A, A2.to_dense())  #The input is unchanged
    assert resptr._table == R._table  #The returned matrix is correct
    A2.destroy()
    R.destroy()
예제 #4
0
def test_matmul_square(sqlmatrix, random, n):
    A = 10.0 * numpy.matrix(numpy.random.normal(size=(n, n)))
    B = matrix.SqlMatrix.from_dense(A)
    C = matrix.SqlMatrix(n, n)
    resptr = B.matmul(B, C)
    assert _is_close(A**2, C.to_dense())  #The result is correct
    assert _is_close(A, B.to_dense())  #The input is unchanged
    assert resptr._table == C._table  #The returned matrix is correct
    B.destroy()
    C.destroy()
예제 #5
0
def test_hadamard_product(sqlmatrix, random, r, c):
    left = 10.0 * numpy.matrix(numpy.random.normal(size=(r, c)))
    right = 10.0 * numpy.matrix(numpy.random.normal(size=(r, c)))
    sqlleft = matrix.SqlMatrix.from_dense(left)
    sqlright = matrix.SqlMatrix.from_dense(right)
    sqlresult = matrix.SqlMatrix(r, c)
    resptr = sqlleft.hadamard_product(sqlright, sqlresult)
    assert _is_close(numpy.multiply(left, right),
                     sqlresult.to_dense())  #The result is correct
    assert _is_close(left, sqlleft.to_dense())  #The input is unchanged
    assert _is_close(right, sqlright.to_dense())  #The input is unchanged
    assert resptr._table == sqlresult._table  #The returned matrix is correct
    sqlleft.destroy()
    sqlright.destroy()
    sqlresult.destroy()
예제 #6
0
def test_get_set(sqlmatrix, random, n):
    M = matrix.SqlMatrix(n, n)
    for i, j in [(0.5, 0), (0, 0.5), (0.5, 0.5), (-12.2, 18.9)]:
        with pytest.raises(TypeError):
            M.get(i, j)
        with pytest.raises(TypeError):
            M.set(i, j, 3.14)
    for i, j in [(-1, 0), (n, 0), (0, -1), (0, n), (-1, -1), (-1, n), (n, -1),
                 (n, n)]:
        with pytest.raises(IndexError):
            M.get(i, j)
        with pytest.raises(IndexError):
            M.set(i, j, 3.14)
    for i, j in zip(numpy.random.randint(n, size=n),
                    numpy.random.randint(n, size=n)):
        v = numpy.random.normal()
        M.set(i, j, v)
        assert M.get(i, j) == v
    M.destroy()