Exemplo n.º 1
0
def test_solvers():
    """Main testing function."""

    print("\nTest 1")
    aa = np.array([[2.0, 4.0, 4.0], [5.0, 4.0, 2.0], [1.0, 2.0, -1.0]])
    bb = np.array([1.0, 4.0, 2.0])
    xx_expected = np.array([0.666666666666667, 0.416666666666667, -0.5])
    xx_gauss = solvers.gaussian_eliminate(aa, bb)
    _check_result(xx_expected, xx_gauss)
    assert np.all(np.abs(xx_gauss - xx_expected) < 1e-10)

    print("\nTest 2")
    aa = np.array([[2.0, 4.0, 4.0], [1.0, 2.0, -1.0], [5.0, 4.0, 2.0]])
    bb = np.array([1.0, 2.0, 4.0])
    xx_expected = np.array([0.666666666666667, 0.416666666666667, -0.5])
    xx_gauss = solvers.gaussian_eliminate(aa, bb)
    _check_result(xx_expected, xx_gauss)
    assert np.all(np.abs(xx_gauss - xx_expected) < 1e-10)

    print("\nTest 3")
    aa = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
    bb = np.array([1.0, 2.0, 3.0])
    xx_expected = None
    xx_gauss = solvers.gaussian_eliminate(aa, bb)
    _check_result(xx_expected, xx_gauss)
    assert xx_gauss == xx_expected
Exemplo n.º 2
0
def test_elimination(testname):
    "Tests elimination."
    aa, bb = get_test_input(testname)
    xx_expected = get_test_output(testname)
    if xx_expected is None:
        # Linear system of equations can not be solved -> expecting exception
        with pytest.raises(ValueError):
            solvers.gaussian_eliminate(aa, bb)
    else:
        xx_gauss = solvers.gaussian_eliminate(aa, bb)
        assert np.allclose(xx_gauss,
                           xx_expected,
                           atol=ABSOLUTE_TOLERANCE,
                           rtol=RELATIVE_TOLERANCE)
Exemplo n.º 3
0
def test_gaussian_eliminate_1():
    """Test gaussian_eliminate-Function"""
    print("\nTest 1")
    a = np.array([[2.0, 4.0, 4.0], [5.0, 4.0, 2.0], [1.0, 2.0, -1.0]])
    b = np.array([1.0, 4.0, 2.0])
    x_expected = np.array([0.666666666666667, 0.416666666666667, -0.5])
    x_gauss = solvers.gaussian_eliminate(a, b)
    assert np.all(np.abs(x_gauss - x_expected) < 1e-10)
Exemplo n.º 4
0
def test_gaussian_eliminate_3():
    """Test gaussian_eliminate-Function on depedency"""
    print("\nTest 3")
    a = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
    b = np.array([1.0, 2.0, 3.0])
    x_expected = None
    x_gauss = solvers.gaussian_eliminate(a, b)
    assert x_expected == x_gauss
Exemplo n.º 5
0
def test_solvers1():
    """Simple testing function."""
    print("\nTest 1")
    aa = np.array([[2.0, 4.0, 4.0], [5.0, 4.0, 2.0], [1.0, 2.0, -1.0]])
    bb = np.array([1.0, 4.0, 2.0])
    xx_expected = np.array([0.666666666666667, 0.416666666666667, -0.5])
    xx_gauss = solvers.gaussian_eliminate(aa, bb)
    _check_result(xx_expected, xx_gauss)
    assert np.all(xx_expected - xx_gauss < 1e-10)
Exemplo n.º 6
0
def test_solvers3():
    """Testing for linear dependencies."""
    print("\nTest 3")
    aa = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
    bb = np.array([1.0, 2.0, 3.0])
    xx_expected = None
    xx_gauss = solvers.gaussian_eliminate(aa, bb)
    _check_result(xx_expected, xx_gauss)
    assert xx_expected == xx_gauss
Exemplo n.º 7
0
def test_elimination(testname):
    "Tests successful elimination."
    aa, bb = _get_test_input(testname)
    xx_expected = _get_test_output(testname)
    xx_gauss = solvers.gaussian_eliminate(aa, bb)
    if xx_expected is None:
        assert xx_gauss is None
    else:
        assert np.all(np.abs(xx_gauss - xx_expected) < TOLERANCE)
Exemplo n.º 8
0
#!/usr/bin/env python3

import solvers
import linsolveio as lio

(aa, bb) = lio.read_input('testdata/linearly_dependant/linsolve.in')

xx = solvers.gaussian_eliminate(aa, bb)

lio.write_result('Result/result.out', xx)

xx = lio.read_result('Result/result.out')

print(xx)
Exemplo n.º 9
0
import sys
import solvers
import numpy as np

try:
    fp = open('linsolver.in', 'r')
except FileNotFoundError:
    print("Can not open file {}".format('linsolver.in'))
    print('Exiting...')
    sys.exit(1)
else:
    data = np.loadtxt('linsolver.in', skiprows=1, dtype=float, delimiter=' ')

    array = data[0:3]

    vector = data[3]

    # numstr = fp.readline(1)

    # numval = int(numstr)

    result = solvers.gaussian_eliminate(array, vector)

    if result is None:
        fp = open('linsolver.out', 'w')
        fp.write('ERROR: LINDEP')
        fp.close()
    else:
        np.savetxt('linsolver.out', result)