Esempio n. 1
0
def test_check_sys_equal():
    assert not sys_equal(np.zeros(2), np.zeros(3))

    assert s != z
    assert not z == s
    assert LinearSystem(5, analog=True) != LinearSystem(5, analog=False)

    with pytest.raises(ValueError):
        sys_equal(s, z)

    with pytest.raises(ValueError):
        ss_equal(s, z)
Esempio n. 2
0
def test_check_sys_equal():
    assert not sys_equal(np.ones(2), np.ones(3))

    assert s != z
    assert not z == s
    assert LinearSystem(5, analog=True) != LinearSystem(5, analog=False)

    with pytest.raises(ValueError):
        sys_equal(s, z)

    with pytest.raises(ValueError):
        ss_equal(s, z)
Esempio n. 3
0
def test_similarity_transform():
    sys = Alpha(0.1)

    TA, TB, TC, TD = sys.transform(np.eye(2), np.eye(2)).ss
    A, B, C, D = sys2ss(sys)
    assert np.allclose(A, TA)
    assert np.allclose(B, TB)
    assert np.allclose(C, TC)
    assert np.allclose(D, TD)

    T = [[1, 1], [-0.5, 0]]
    rsys = sys.transform(T)
    assert ss_equal(rsys, sys.transform(T, inv(T)))

    TA, TB, TC, TD = rsys.ss
    assert not np.allclose(A, TA)
    assert not np.allclose(B, TB)
    assert not np.allclose(C, TC)
    assert np.allclose(D, TD)
    assert sys_equal(sys, (TA, TB, TC, TD))

    length = 1000
    dt = 0.001
    x_old = np.asarray(
        [sub.impulse(length=length, dt=dt) for sub in sys])
    x_new = np.asarray(
        [sub.impulse(length=length, dt=dt) for sub in rsys])

    # dot(T, x_new(t)) = x_old(t)
    assert np.allclose(np.dot(T, x_new), x_old)
Esempio n. 4
0
def test_zerodim_system():
    sys = LinearSystem(1)
    assert len(sys) == 0
    assert ss_equal(sys, (0, 0, 0, 1))

    # However, this following system could have dimension 0 or 1
    # depending on whether we're before or after scipy 0.18
    # see https://github.com/scipy/scipy/issues/5760
    # TODO: ideally it would stay 0, but documenting this weirdness for now
    ss_sys = LinearSystem(sys.ss)
    assert len(ss_sys) in (0, 1)
Esempio n. 5
0
def test_canonical():
    sys = ([1], [1], [1], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0], [1, 0]], [[1], [0]], [[1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0], [0, 1]], [[0], [1]], [[1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0], [0, 1]], [[1], [0]], [[1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0], [0, 0]], [[1], [0]], [[1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0], [0, 0]], [[0], [1]], [[1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0, 1], [0, 1, 1], [1, 0, 0]], [[0], [1], [-1]],
           [[1, 1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = nengo.Alpha(0.1)
    csys = canonical(sys, controllable=True)
    osys = canonical(sys, controllable=False)

    assert ss_equal(csys, LinearSystem(sys).controllable)
    assert ss_equal(osys, LinearSystem(sys).observable)

    assert sys_equal(csys, osys)
    assert not ss_equal(csys, osys)  # different state-space realizations

    A, B, C, D = csys.ss
    assert sys_equal(csys, sys)
    assert ss_equal(csys,
                    ([[-20, -100], [1, 0]], [[1], [0]], [[0, 100]], [[0]]))

    assert sys_equal(osys, sys)
    assert ss_equal(osys,
                    ([[-20, 1], [-100, 0]], [[0], [100]], [[1, 0]], [[0]]))
Esempio n. 6
0
def test_non_siso_manipulation():
    sys = Alpha(0.1)
    A, B, C, D = sys.ss

    SIMO = LinearSystem((A, B, np.eye(len(A)), [[0], [0]]))
    assert not SIMO.is_SISO
    assert SIMO.size_in == 1
    assert SIMO.size_out == 2
    assert SIMO.shape == (2, 1)
    assert not SIMO.has_passthrough
    assert ss_equal(_eval(SIMO), SIMO)
    assert isinstance(str(SIMO), str)
    assert ss_equal(canonical(SIMO), SIMO)
    for sub1, sub2 in zip(sys, SIMO):
        assert ss_equal(sub1, sub2)

    MISO = LinearSystem((A, [[1, 1]], C, [[0, 1]]))
    assert not MISO.is_SISO
    assert MISO.size_in == 2
    assert MISO.size_out == 1
    assert MISO.shape == (1, 2)
    assert MISO.has_passthrough
    assert ss_equal(_eval(MISO), MISO)
    assert isinstance(str(MISO), str)

    MIMO = LinearSystem((A, [[1, 1]], np.eye(len(A)), np.zeros((2, 2))))
    assert not MIMO.is_SISO
    assert MIMO.size_in == MIMO.size_out == 2
    assert MIMO.shape == (2, 2)
    assert not MIMO.has_passthrough
    assert ss_equal(_eval(MIMO), MIMO)
    assert isinstance(str(MIMO), str)
    for sub1, sub2 in zip(MISO, MIMO):
        assert ss_equal(sub1, sub2)
Esempio n. 7
0
def test_canonical():
    sys = ([1], [1], [1], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0], [1, 0]], [[1], [0]], [[1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0], [0, 1]], [[0], [1]], [[1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0], [0, 1]], [[1], [0]], [[1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0], [0, 0]], [[1], [0]], [[1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0], [0, 0]], [[0], [1]], [[1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = ([[1, 0, 1], [0, 1, 1], [1, 0, 0]], [[0], [1], [-1]],
           [[1, 1, 1]], [0])
    assert sys_equal(canonical(sys), sys)

    sys = nengo.Alpha(0.1)
    csys = canonical(sys, controllable=True)
    osys = canonical(sys, controllable=False)

    assert sys_equal(csys, osys)
    assert not ss_equal(csys, osys)  # different state-space realizations

    A, B, C, D = csys.ss
    assert sys_equal(csys, sys)
    assert ss_equal(csys,
                    ([[-20, -100], [1, 0]], [[1], [0]], [[0, 100]], [[0]]))

    assert sys_equal(osys, sys)
    assert ss_equal(osys,
                    ([[-20, 1], [-100, 0]], [[0], [100]], [[1, 0]],  [[0]]))