test_fd_derivation.py 1.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pytest
import sympy as sp
from pystencils.utils import LinearEquationSystem


def test_linear_equation_system():
    unknowns = sp.symbols("x_:3")
    x, y, z = unknowns
    m = LinearEquationSystem(unknowns)
    m.add_equation(x + y - 2)
    m.add_equation(x - y - 1)
    assert m.solution_structure() == 'multiple'
    m.set_unknown_zero(2)
    assert m.solution_structure() == 'single'
    solution = m.solution()
    assert solution[unknowns[2]] == 0
    assert solution[unknowns[1]] == sp.Rational(1, 2)
    assert solution[unknowns[0]] == sp.Rational(3, 2)

    m.set_unknown_zero(0)
    assert m.solution_structure() == 'none'

    # special case where less rows than unknowns, but no solution
    m = LinearEquationSystem(unknowns)
    m.add_equation(x - 3)
    m.add_equation(x - 4)
    assert m.solution_structure() == 'none'
    m.add_equation(y - 4)
    assert m.solution_structure() == 'none'

    with pytest.raises(ValueError) as e:
        m.add_equation(x**2 - 1)
    assert 'Not a linear equation' in str(e)