def test_matrix_basic():
    A1 = matrix(3)
    for i in xrange(3):
        A1[i,i] = 1
    assert A1 == eye(3)
    assert A1 == matrix(A1)
    A2 = matrix(3, 2)
    assert not A2._matrix__data
    A3 = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    assert list(A3) == range(1, 10)
    A3[1,1] = 0
    assert not (1, 1) in A3._matrix__data
    A4 = matrix([[1, 2, 3], [4, 5, 6]])
    A5 = matrix([[6, -1], [3, 2], [0, -3]])
    assert A4 * A5 == matrix([[12, -6], [39, -12]])
    assert A1 * A3 == A3 * A1 == A3
    try:
        A2 * A2
        assert False
    except ValueError:
        pass
    l = [[10, 20, 30], [40, 0, 60], [70, 80, 90]]
    A6 = matrix(l)
    assert A6.tolist() == l
    assert A6 == eval(repr(A6))
    A6 = matrix(A6, force_type=float)
    assert A6 == eval(repr(A6))
    assert A6*1j == eval(repr(A6*1j))
    assert A3 * 10 == 10 * A3 == A6
    assert A2.rows == 3
    assert A2.cols == 2
    A3.rows = 2
    A3.cols = 2
    assert len(A3._matrix__data) == 3
    assert A4 + A4 == 2*A4
    try:
        A4 + A2
    except ValueError:
        pass
    assert sum(A1 - A1) == 0
    A7 = matrix([[1, 2], [3, 4], [5, 6], [7, 8]])
    x = matrix([10, -10])
    assert A7*x == matrix([-10, -10, -10, -10])
    A8 = ones(5)
    assert sum((A8 + 1) - (2 - zeros(5))) == 0
    assert (1 + ones(4)) / 2 - 1 == zeros(4)
    assert eye(3)**10 == eye(3)
    try:
        A7**2
        assert False
    except ValueError:
        pass
    A9 = randmatrix(3)
    A10 = matrix(A9)
    A9[0,0] = -100
    assert A9 != A10
    A11 = matrix(randmatrix(2, 3), force_type=mpi)
    for a in A11:
        assert isinstance(a, mpi)
    assert nstr(A9)
def test_matrix_basic():
    A1 = matrix(3)
    for i in xrange(3):
        A1[i, i] = 1
    assert A1 == eye(3)
    assert A1 == matrix(A1)
    A2 = matrix(3, 2)
    assert not A2._matrix__data
    A3 = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    assert list(A3) == range(1, 10)
    A3[1, 1] = 0
    assert not (1, 1) in A3._matrix__data
    A4 = matrix([[1, 2, 3], [4, 5, 6]])
    A5 = matrix([[6, -1], [3, 2], [0, -3]])
    assert A4 * A5 == matrix([[12, -6], [39, -12]])
    assert A1 * A3 == A3 * A1 == A3
    try:
        A2 * A2
        assert False
    except ValueError:
        pass
    l = [[10, 20, 30], [40, 0, 60], [70, 80, 90]]
    A6 = matrix(l)
    assert A6.tolist() == l
    assert A6 == eval(repr(A6))
    A6 = matrix(A6, force_type=float)
    assert A6 == eval(repr(A6))
    assert A3 * 10 == 10 * A3 == A6
    assert A2.rows == 3
    assert A2.cols == 2
    A3.rows = 2
    A3.cols = 2
    assert len(A3._matrix__data) == 3
    assert A4 + A4 == 2 * A4
    try:
        A4 + A2
    except ValueError:
        pass
    assert sum(A1 - A1) == 0
    A7 = matrix([[1, 2], [3, 4], [5, 6], [7, 8]])
    x = matrix([10, -10])
    assert A7 * x == matrix([-10, -10, -10, -10])
    A8 = ones(5)
    assert sum((A8 + 1) - (2 - zeros(5))) == 0
    assert (1 + ones(4)) / 2 - 1 == zeros(4)
    assert eye(3)**10 == eye(3)
    try:
        A7**2
        assert False
    except ValueError:
        pass
    A9 = randmatrix(3)
    A10 = matrix(A9)
    A9[0, 0] = -100
    assert A9 != A10
    A11 = matrix(randmatrix(2, 3), force_type=mpi)
    for a in A11:
        assert isinstance(a, mpi)
def test_matrix_creation():
    assert diag([1, 2, 3]) == matrix([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
    A1 = ones(2, 3)
    assert A1.rows == 2 and A1.cols == 3
    for a in A1:
        assert a == 1
    A2 = zeros(3, 2)
    assert A2.rows == 3 and A2.cols == 2
    for a in A2:
        assert a == 0
    assert randmatrix(10) != randmatrix(10)
def test_matrix_creation():
    assert diag([1, 2, 3]) == matrix([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
    A1 = ones(2, 3)
    assert A1.rows == 2 and A1.cols == 3
    for a in A1:
        assert a == 1
    A2 = zeros(3, 2)
    assert A2.rows == 3 and A2.cols == 2
    for a in A2:
        assert a == 0
    assert randmatrix(10) != randmatrix(10)
Example #5
0
def test_matrix_creation():
    assert diag([1, 2, 3]) == matrix([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
    A1 = ones(2, 3)
    assert A1.rows == 2 and A1.cols == 3
    for a in A1:
        assert a == 1
    A2 = zeros(3, 2)
    assert A2.rows == 3 and A2.cols == 2
    for a in A2:
        assert a == 0
    assert randmatrix(10) != randmatrix(10)
    one = mpf(1)
    assert hilbert(3) == matrix([[one, one / 2, one / 3], [one / 2, one / 3, one / 4], [one / 3, one / 4, one / 5]])
Example #6
0
def test_LU_decomp():
    A = A3.copy()
    b = b3
    A, p = LU_decomp(A)
    y = L_solve(A, b, p)
    x = U_solve(A, y)
    assert p == [2, 1, 2, 3]
    assert [round(i, 14) for i in x] == [
        3.78953107960742, 2.9989094874591098, -0.081788440567070006,
        3.8713195201744801, 2.9171210468920399
    ]
    A = A4.copy()
    b = b4
    A, p = LU_decomp(A)
    y = L_solve(A, b, p)
    x = U_solve(A, y)
    assert p == [0, 3, 4, 3]
    assert [round(i, 14) for i in x] == [
        2.6383625899619201, 2.6643834462368399, 0.79208015947958998,
        -2.5088376454101899, -1.0567657691375001
    ]
    A = randmatrix(3)
    bak = A.copy()
    LU_decomp(A, overwrite=1)
    assert A != bak
Example #7
0
def test_exp_pade():
    for i in range(3):
        dps = 15
        extra = 5
        mp.dps = dps + extra
        dm = 0
        while not dm:
            m = randmatrix(3)
            dm = det(m)
        m = m/dm
        a = diag([1,2,3])
        a1 = m**-1 * a * m
        mp.dps = dps
        e1 = exp_pade(a1)
        mp.dps = dps + extra
        e2 = m * a1 * m**-1
        d = e2 - a
        #print d
        mp.dps = dps
        assert norm_p(d, inf).ae(0)
    mp.dps = 15
Example #8
0
def test_LU_decomp():
    A = A3.copy()
    b = b3
    A, p = LU_decomp(A)
    y = L_solve(A, b, p)
    x = U_solve(A, y)
    assert p == [2, 1, 2, 3]
    assert [round(i, 14) for i in x] == [3.78953107960742, 2.9989094874591098,
            -0.081788440567070006, 3.8713195201744801, 2.9171210468920399]
    A = A4.copy()
    b = b4
    A, p = LU_decomp(A)
    y = L_solve(A, b, p)
    x = U_solve(A, y)
    assert p == [0, 3, 4, 3]
    assert [round(i, 14) for i in x] == [2.6383625899619201, 2.6643834462368399,
            0.79208015947958998, -2.5088376454101899, -1.0567657691375001]
    A = randmatrix(3)
    bak = A.copy()
    LU_decomp(A, overwrite=1)
    assert A != bak
Example #9
0
def test_exp_pade():
    for i in range(3):
        dps = 15
        extra = 5
        mp.dps = dps + extra
        dm = 0
        while not dm:
            m = randmatrix(3)
            dm = det(m)
        m = m / dm
        a = diag([1, 2, 3])
        a1 = m**-1 * a * m
        mp.dps = dps
        e1 = exp_pade(a1)
        mp.dps = dps + extra
        e2 = m * a1 * m**-1
        d = e2 - a
        #print d
        mp.dps = dps
        assert norm(d, inf).ae(0)
    mp.dps = 15
Example #10
0
def test_LU_cache():
    A = randmatrix(3)
    LU = LU_decomp(A)
    assert A._LU == LU_decomp(A)
    A[0, 0] = -1000
    assert A._LU is None
Example #11
0
def test_LU_cache():
    A = randmatrix(3)
    LU = LU_decomp(A)
    assert A._LU == LU_decomp(A)
    A[0,0] = -1000
    assert A._LU is None
Example #12
0
def test_precision():
    A = randmatrix(10, 10)
    assert mnorm_1(inverse(inverse(A)) - A) < 1.e-45
Example #13
0
def test_factorization():
    A = randmatrix(5)
    P, L, U = lu(A)
    assert mnorm_1(P*A - L*U) < 1.e-15
Example #14
0
def test_improve_solution():
    A = randmatrix(5, min=1e-20, max=1e20)
    b = randmatrix(5, 1, min=-1000, max=1000)
    x1 = lu_solve(A, b) + randmatrix(5, 1, min=-1e-5, max=1.e-5)
    x2 = improve_solution(A, x1, b)
    assert norm_p(residual(A, x2, b), 2) < norm_p(residual(A, x1, b), 2)
Example #15
0
def test_factorization():
    A = randmatrix(5)
    P, L, U = lu(A)
    assert mnorm(P * A - L * U, 1) < 1.e-15
Example #16
0
def test_improve_solution():
    A = randmatrix(5, min=1e-20, max=1e20)
    b = randmatrix(5, 1, min=-1000, max=1000)
    x1 = lu_solve(A, b) + randmatrix(5, 1, min=-1e-5, max=1.e-5)
    x2 = improve_solution(A, x1, b)
    assert norm(residual(A, x2, b), 2) < norm(residual(A, x1, b), 2)
Example #17
0
def test_precision():
    A = randmatrix(10, 10)
    assert mnorm(inverse(inverse(A)) - A, 1) < 1.e-45