Exemplo n.º 1
0
def test_householder():
    mp.dps = 15
    A, b = A8, b8
    H, p, x, r = householder(extend(A, b))
    assert H == matrix([[mpf('3.0'), mpf('-2.0'),
                         mpf('-1.0'), 0],
                        [
                            -1.0,
                            mpf('3.333333333333333'),
                            mpf('-2.9999999999999991'),
                            mpf('2.0')
                        ],
                        [
                            -1.0,
                            mpf('-0.66666666666666674'),
                            mpf('2.8142135623730948'),
                            mpf('-2.8284271247461898')
                        ],
                        [
                            1.0,
                            mpf('-1.3333333333333333'),
                            mpf('-0.20000000000000018'),
                            mpf('4.2426406871192857')
                        ]])
    assert p == [-2, -2, mpf('-1.4142135623730949')]
    assert round(norm(r, 2), 10) == 4.2426406870999998

    y = [
        102.102, 58.344, 36.463, 24.310, 17.017, 12.376, 9.282, 7.140, 5.610,
        4.488, 3.6465, 3.003
    ]

    def coeff(n):
        # similiar to Hilbert matrix
        A = []
        for i in xrange(1, 13):
            A.append([1. / (i + j - 1) for j in xrange(1, n + 1)])
        return matrix(A)

    residuals = []
    refres = []
    for n in xrange(2, 7):
        A = coeff(n)
        H, p, x, r = householder(extend(A, y))
        x = matrix(x)
        y = matrix(y)
        residuals.append(norm(r, 2))
        refres.append(norm(residual(A, x, y), 2))
    assert [round(res, 10) for res in residuals] == [
        15.1733888877, 0.82378073210000002, 0.302645887, 0.0260109244,
        0.00058653999999999998
    ]
    assert norm(matrix(residuals) - matrix(refres), inf) < 1.e-13
Exemplo n.º 2
0
def test_norms():
    # matrix norms
    A = matrix([[1, -2], [-3, -1], [2, 1]])
    assert mnorm(A,1) == 6
    assert mnorm(A,inf) == 4
    assert mnorm(A,'F') == sqrt(20)
    # vector norms
    assert norm(-3) == 3
    x = [1, -2, 7, -12]
    assert norm(x, 1) == 22
    assert round(norm(x, 2), 10) == 14.0712472795
    assert round(norm(x, 10), 10) == 12.0054633727
    assert norm(x, inf) == 12
Exemplo n.º 3
0
def test_solve():
    assert norm(residual(A6, lu_solve(A6, b6), b6), inf) < 1.e-10
    assert norm(residual(A7, lu_solve(A7, b7), b7), inf) < 1.5
    assert norm(residual(A8, lu_solve(A8, b8), b8), inf) <= 3 + 1.e-10
    assert norm(residual(A6, qr_solve(A6, b6)[0], b6), inf) < 1.e-10
    assert norm(residual(A7, qr_solve(A7, b7)[0], b7), inf) < 1.5
    assert norm(residual(A8, qr_solve(A8, b8)[0], b8), 2) <= 4.3
    assert norm(residual(A10, lu_solve(A10, b10), b10), 2) < 1.e-10
    assert norm(residual(A10, qr_solve(A10, b10)[0], b10), 2) < 1.e-10
Exemplo n.º 4
0
def test_solve():
    assert norm(residual(A6, lu_solve(A6, b6), b6), inf) < 1.e-10
    assert norm(residual(A7, lu_solve(A7, b7), b7), inf) < 1.5
    assert norm(residual(A8, lu_solve(A8, b8), b8), inf) <= 3 + 1.e-10
    assert norm(residual(A6, qr_solve(A6, b6)[0], b6), inf) < 1.e-10
    assert norm(residual(A7, qr_solve(A7, b7)[0], b7), inf) < 1.5
    assert norm(residual(A8, qr_solve(A8, b8)[0], b8), 2) <= 4.3
    assert norm(residual(A10, lu_solve(A10, b10), b10), 2) < 1.e-10
    assert norm(residual(A10, qr_solve(A10, b10)[0], b10), 2) < 1.e-10
Exemplo n.º 5
0
def test_householder():
    mp.dps = 15
    A, b = A8, b8
    H, p, x, r = householder(extend(A, b))
    assert H == matrix(
    [[mpf('3.0'), mpf('-2.0'), mpf('-1.0'), 0],
     [-1.0,mpf('3.333333333333333'),mpf('-2.9999999999999991'),mpf('2.0')],
     [-1.0, mpf('-0.66666666666666674'),mpf('2.8142135623730948'),
      mpf('-2.8284271247461898')],
     [1.0, mpf('-1.3333333333333333'),mpf('-0.20000000000000018'),
      mpf('4.2426406871192857')]])
    assert p == [-2, -2, mpf('-1.4142135623730949')]
    assert round(norm(r, 2), 10) == 4.2426406870999998

    y = [102.102, 58.344, 36.463, 24.310, 17.017, 12.376, 9.282, 7.140, 5.610,
         4.488, 3.6465, 3.003]

    def coeff(n):
        # similiar to Hilbert matrix
        A = []
        for i in xrange(1, 13):
            A.append([1. / (i + j - 1) for j in xrange(1, n + 1)])
        return matrix(A)

    residuals = []
    refres = []
    for n in xrange(2, 7):
        A = coeff(n)
        H, p, x, r = householder(extend(A, y))
        x = matrix(x)
        y = matrix(y)
        residuals.append(norm(r, 2))
        refres.append(norm(residual(A, x, y), 2))
    assert [round(res, 10) for res in residuals] == [15.1733888877,
           0.82378073210000002, 0.302645887, 0.0260109244,
           0.00058653999999999998]
    assert norm(matrix(residuals) - matrix(refres), inf) < 1.e-13
Exemplo n.º 6
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
Exemplo n.º 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(d, inf).ae(0)
    mp.dps = 15
Exemplo n.º 8
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)
Exemplo n.º 9
0
def test_cholesky():
    A9.force_type = float
    assert cholesky(A9) == matrix([[2, 0, 0], [1, 2, 0], [-1, -3 / 2, 3 / 2]])
    x = cholesky_solve(A9, b9)
    assert norm(residual(A9, x, b9), inf) == 0
Exemplo n.º 10
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)
Exemplo n.º 11
0
def test_cholesky():
    A9.force_type = float
    assert cholesky(A9) == matrix([[2, 0, 0], [1, 2, 0], [-1, -3/2, 3/2]])
    x = cholesky_solve(A9, b9)
    assert norm(residual(A9, x, b9), inf) == 0