示例#1
0
def test_ackermann_args():
    # Not SIxO system
    G = State(eye(2), eye(2), eye(2))
    assert_raises(ValueError, ackermann, G, [1, 2])
    # Wrong # of poles
    G = State(eye(2), [[1], [0]], [1, 0])
    assert_raises(ValueError, ackermann, G, [1, 2, 3])
示例#2
0
def test_State_algebra_mimo_siso():
    static_siso_state = State(5)
    static_mimo_state = State(2.0*np.eye(3))
    dynamic_siso_state = State(haroldcompanion([1, 3, 3, 1]),
                               e_i(3, -1),
                               e_i(3, 1).T,
                               0)

    dynamic_mimo_state = State(haroldcompanion([1, 3, 3, 1]),
                               e_i(3, [1, 2]),
                               np.eye(3),
                               np.zeros((3, 2)))

    dynamic_square_state = State(haroldcompanion([1, 3, 3, 1]),
                                 np.eye(3),
                                 np.eye(3),
                                 np.zeros((3, 3))
                                 )

    assert_raises(IndexError, dynamic_siso_state.__mul__, static_mimo_state)
    assert_raises(IndexError, dynamic_siso_state.__add__, static_mimo_state)
    assert_raises(IndexError, static_mimo_state.__add__, dynamic_mimo_state)
    assert_raises(IndexError, static_mimo_state.__add__, static_siso_state)
    assert_raises(IndexError, static_mimo_state.__mul__, static_siso_state)
    F = static_mimo_state @ dynamic_mimo_state

    assert_almost_equal(F.c, np.eye(3)*2.0)
    assert_almost_equal((dynamic_square_state + static_mimo_state).d,
                        2*np.eye(3))
示例#3
0
def test_minimal_realization_State():
    M = array([[-6.5, 0.5, 6.5, -6.5, 0., 1., 0.],
               [-0.5, -5.5, -5.5, 5.5, 2., 1., 2.],
               [-0.5, 0.5, 0.5, -6.5, 3., 4., 3.],
               [-0.5, 0.5, -5.5, -0.5, 3., 2., 3.],
               [1., 1., 0., 0., 0., 0., 0.]])
    G = State(*matrix_slice(M, (1, 4), corner='sw'))
    H = minimal_realization(G)
    assert H.a.shape == (2, 2)
    #
    G = State(
        array([[0., 1., 0., 0., 0.], [-0.1, -0.5, 1., -1., 0.],
               [0., 0., 0., 1., 0.], [0., 0., 0., 0., 1.],
               [0., 3.5, 1., -2., 2.]]), array([[0.], [1.], [0.], [0.], [1.]]),
        array([[0., 3.5, 1., -1., 0.]]), array([[1.]]))
    H = minimal_realization(G)
    assert H.a.shape == (4, 4)
    #
    G = State(
        array([[-2., 0., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.],
               [0., -12., 4., 3.]]),
        array([[1., 0.], [0., 0.], [0., 0.], [0., 1.]]),
        array([[1., -9., 0., 0.], [0., -20., 0., 5.]]),
        array([[0., 0.], [0., 1.]]))
    H = minimal_realization(G)
    assert H.a.shape == (3, 3)
示例#4
0
def test_State_Instantiations():
    assert_raises(TypeError, State)
    G = State(5)
    assert G.a.size == 0
    assert G._isSISO
    assert G._isgain
    assert_equal(G.d, np.array([[5.]]))

    G = State(np.eye(2))
    assert_equal(G.shape, (2, 2))
    assert G._isgain
    assert not G._isSISO

    assert_raises(ValueError, State,
                  np.eye(2),
                  np.array([[1], [2], [3]]),
                  np.array([1, 2]),
                  0)

    assert_raises(ValueError, State,
                  np.eye(2),
                  np.array([[1], [2]]),
                  np.array([1, 2, 3]),
                  0)
    assert_raises(ValueError, State,
                  np.eye(2),
                  np.array([[1], [2]]),
                  np.array([1, 2]),
                  np.array([0, 0]))
示例#5
0
def test_simulate_step_response_response():
    G = State(4)
    yout, tout = simulate_step_response(G)
    assert_allclose(yout, 4 * ones(len(yout))[:, None])
    G = State(4, dt=0.01)
    y2, t2 = simulate_step_response(G)
    assert_allclose(y2, 4 * ones(len(y2))[:, None])
示例#6
0
def test_State_slicing():
    F = State(np.random.rand(4, 4))
    H = State(F.d, np.random.rand(4, 3), np.random.rand(5, 4))
    Hind = [(1, 3), (5, 1),
            (5, 3), (1, 1),
            (2, 3), (5, 2),
            (2, 3), (5, 2),
            (1, 2), (2, 1),
            (3, 3), (5, 2)]
    Find = [(1, 4), (4, 1),
            (4, 4), (1, 1),
            (2, 4), (4, 2),
            (2, 4), (4, 2),
            (1, 2), (2, 1),
            (2, 4), (4, 2)]

    for s, sind in ((H, Hind), (F, Find)):
        for ind, x in enumerate([s[1, :], s[:, 1],
                                s[:, :], s[0, 0],
                                s[1:3, :], s[:, 1:3],
                                s[[1, 2], :], s[:, [1, 2]],
                                s[2, [1, 2]], s[[1, 2], 2],
                                s[::2, :], s[:, ::2]]):
            assert_equal(x.shape, sind[ind])

    assert_raises(ValueError, H.__setitem__)
示例#7
0
def test_State_algebra_add_radd():
    sta_siso = State(5)
    sta_mimo = State(2.0*np.eye(3))
    dyn_siso = State(haroldcompanion([1, 3, 3, 1]), e_i(3, -1), e_i(3, 1).T)
    dyn_mimo = State(haroldcompanion([1, 3, 3, 1]), e_i(3, [1, 2]), np.eye(3))
    dyn_mimo_sq = State(haroldcompanion([1, 3, 3, 1]), np.eye(3), np.eye(3))

    G = dyn_mimo + sta_siso
    assert_array_almost_equal(G.d, sta_siso.to_array()*np.ones(dyn_mimo.shape))
    assert_raises(ValueError, dyn_mimo.__add__, sta_mimo)
    G = dyn_mimo_sq + sta_mimo
    assert_array_almost_equal(G.d, 2.*np.eye(3))
    G = dyn_mimo + dyn_siso
    J = np.array([[0., 1., 0., 0., 0., 0., 0., 0.],
                  [0., 0., 1., 0., 0., 0., 1., 0.],
                  [-1., -3., -3., 0., 0., 0., 0., 1.],
                  [0., 0., 0., 0., 1., 0., 0., 0.],
                  [0., 0., 0., 0., 0., 1., 0., 0.],
                  [0., 0., 0., -1., -3., -3., 1., 1.],
                  [1., 0., 0., 0., 1., 0., 0., 0.],
                  [0., 1., 0., 0., 1., 0., 0., 0.],
                  [0., 0., 1., 0., 1., 0., 0., 0.]])
    assert_array_almost_equal(concatenate_state_matrices(G), J)
    assert_raises(ValueError, dyn_mimo.__add__, dyn_mimo_sq)
    assert_raises(ValueError, dyn_mimo.__sub__, dyn_mimo_sq)
    assert_raises(ValueError, dyn_mimo.__radd__, dyn_mimo_sq)
    assert_raises(ValueError, dyn_mimo.__rsub__, dyn_mimo_sq)
示例#8
0
def test_State_algebra_mul_rmul_mimo_siso():
    sta_siso = State(5)
    sta_mimo = State(2.0 * np.eye(3))
    dyn_siso = State(haroldcompanion([1, 3, 3, 1]), e_i(3, -1), e_i(3, 1).T)
    dyn_mimo = State(haroldcompanion([1, 3, 3, 1]), e_i(3, [1, 2]), np.eye(3))
    dyn_mimo_sq = State(haroldcompanion([1, 3, 3, 1]), np.eye(3), np.eye(3))

    G = dyn_siso * dyn_mimo
    J = np.array([[0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                  [0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0.],
                  [-1., -3., -3., 0., 0., 0., 0., 1., 0., 0., 0.],
                  [0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
                  [0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
                  [0., 0., 0., -1., -3., -3., 0., 0., 0., 1., 0.],
                  [0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
                  [0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
                  [0., 0., 0., 0., 0., 0., -1., -3., -3., 0., 1.],
                  [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                  [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                  [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.]])
    assert_array_almost_equal(concatenate_state_matrices(G), J)
    G = dyn_mimo * dyn_siso
    assert_array_almost_equal(concatenate_state_matrices(G), J)

    G = dyn_mimo * sta_siso
    assert_array_almost_equal(G.b, 5 * dyn_mimo.b)
    assert_array_almost_equal(G.d, 5 * dyn_mimo.d)
    assert_raises(ValueError, sta_mimo.__add__, dyn_mimo)
    F = sta_mimo @ dyn_mimo
    J = np.array([[0., 1., 0., 0., 0.], [0., 0., 1., 1., 0.],
                  [-1., -3., -3., 0., 1.], [2., 0., 0., 0., 0.],
                  [0., 2., 0., 0., 0.], [0., 0., 2., 0., 0.]])
    assert_array_almost_equal(concatenate_state_matrices(F), J)
    assert_almost_equal((dyn_mimo_sq + sta_mimo).d, 2 * np.eye(3))
示例#9
0
def test_State_algebra_mul_rmul_dt():
    G = State(1, 2, 3, 4, dt=0.1)
    F = State(4, 3, 2, 1)
    with assert_raises(ValueError):
        F * G
    with assert_raises(ValueError):
        G * F
示例#10
0
def test_State_to_array():
    G = State(1, 1, 1)
    H = State(5)
    with assert_raises(TypeError):
        G.to_array()

    assert_equal(H.to_array(), np.array([[5]]))
    H = State(np.ones((4, 4)))
    assert_equal(H.to_array(), np.ones((4, 4)))
示例#11
0
def test_simulate_step_response():
    G = State(4)
    yout, tout = simulate_step_response(G)
    assert_allclose(yout, 4 * ones(len(yout))[:, None])
    G = State(4, dt=0.01)
    y2, t2 = simulate_step_response(G)
    assert_allclose(y2, 4 * ones(len(y2))[:, None])
    # Custom time input
    G = Transfer(1, [1, 1, 1])
    t = [0, 1, 2, 3]
    yout, tout = simulate_step_response(G, t)
    assert_allclose(tout, t)
示例#12
0
def test_simple_dlqr():
    # Example taken from M. de Oliveira's MAE280B lecture notes
    H = State([[0, 0, 1, 0],
               [0, 0, 0, 1],
               [4.03428022844288e-06, 0, 0, 0.0515652322798669],
               [0, 0, -0.000104315254033883, 0]],
              [[0, 0], [1e-5/3, 0], [0, 0], [0, 0.01]],
              eye(4), dt=0.1)
    k, _, _ = lqr(H[:, 1], eye(4))
    H.a = H.a.T
    f, _, _ = lqr(H[:, 0], block_diag(0, 0, 1e-5, 1e-5), 0.1)
    assert_almost_equal(k, array([[0, 0, -2.08727337333631e-06, 0]]))
    assert_almost_equal(f,  array([[1.71884123e-11, 0, 0, -1.79301359e-15]]))
示例#13
0
def test_simple_dlqr():
    # Example taken from M. de Oliveira's MAE280B lecture notes
    H = State([[0, 0, 1, 0], [0, 0, 0, 1],
               [4.03428022844288e-06, 0, 0, 0.0515652322798669],
               [0, 0, -0.000104315254033883, 0]],
              [[0, 0], [1e-5 / 3, 0], [0, 0], [0, 0.01]],
              eye(4),
              dt=0.1)
    k, _, _ = lqr(H[:, 1], eye(4))
    H.a = H.a.T
    f, _, _ = lqr(H[:, 0], block_diag(0, 0, 1e-5, 1e-5), 0.1)
    assert_almost_equal(k, array([[0, 0, -2.08727337333631e-06, 0]]))
    assert_almost_equal(f, array([[1.71884123e-11, 0, 0, -1.79301359e-15]]))
示例#14
0
def test_simple_lqr():
    # Example taken from M. de Oliveira's MAE280B lecture notes
    H = State([[0, 0, 1, 0],
               [0, 0, 0, 1],
               [4.03428022844288e-06, 0, 0, 0.0515652322798669],
               [0, 0, -0.000104315254033883, 0]],
              [[0, 0], [1e-5/3, 0], [0, 0], [0, 0.01]],
              eye(4))
    k, _, _ = lqr(H[:, 1], eye(4))
    H.a = H.a.T
    f, _, _ = lqr(H[:, 0], block_diag(0, 0, 1e-5, 1e-5), 0.1)
    assert_almost_equal(k, array([[1.00554916, -1, 52.52180106, 18.51107167]]))
    assert_almost_equal(f,  array([[-577.370350, 173.600463,
                                    0.383744946, 0.050228534]]), decimal=5)
示例#15
0
def test_simulate_linear_system_check_x0():
    t = [0, 0.01, 0.02]
    u = [1, 2, 3]
    G = State(5)
    # No IC for static models
    assert_raises(ValueError, simulate_linear_system, G, u, t, x0=1)
    # No IC for Transfer
    G = Transfer(1, [1, 2])
    assert_raises(ValueError, simulate_linear_system, G, u, t, x0=1)
    # x0 is not 1D
    G = State(eye(3), ones([3, 1]), ones([1, 3]), dt=0.01)
    assert_raises(ValueError, simulate_linear_system, G, u, t, x0=ones([5, 5]))
    # x0 isnot compatible with number of states
    assert_raises(ValueError, simulate_linear_system, G, u, t, x0=arange(4))
示例#16
0
def test_simple_lqr():
    # Example taken from M. de Oliveira's MAE280B lecture notes
    H = State([[0, 0, 1, 0], [0, 0, 0, 1],
               [4.03428022844288e-06, 0, 0, 0.0515652322798669],
               [0, 0, -0.000104315254033883, 0]],
              [[0, 0], [1e-5 / 3, 0], [0, 0], [0, 0.01]], eye(4))
    k, _, _ = lqr(H[:, 1], eye(4))
    H.a = H.a.T
    f, _, _ = lqr(H[:, 0], block_diag(0, 0, 1e-5, 1e-5), 0.1)
    assert_almost_equal(k, array([[1.00554916, -1, 52.52180106, 18.51107167]]))
    assert_almost_equal(
        f,
        array([[-577.370350, 173.600463, 0.383744946, 0.050228534]]),
        decimal=5)
示例#17
0
def test_simulate_impulse_response():
    G = State(4)
    yout, tout = simulate_impulse_response(G)
    # Check if the DC_gain * dt * u[0] == 1
    dt = tout[1] - tout[0]
    assert yout[0] * dt == approx(4)
    assert_allclose(yout[1:], 0)
    G = State(4, dt=0.01)
    y2, t2 = simulate_impulse_response(G)
    assert y2[0] == approx(400)
    # Custom time input
    G = Transfer(1, [1, 1, 1])
    t = [0, 1, 2, 3]
    yout, tout = simulate_step_response(G, t)
    assert_allclose(tout, t)
示例#18
0
def test_Transfer_algebra_mul_rmul_siso_mimo():
    F = Transfer(2, [1, 1])
    H = Transfer(np.arange(1, 5).reshape(2, 2), [1, 2])
    K = Transfer([1, 3], [1, 0, 1])

    FK_num, FK_den = (F * K).polynomials
    assert_equal(FK_num, np.array([[2, 6]]))
    assert_equal(FK_den, np.array([[1, 1, 1, 1]]))

    for J in (F * H, H * F):
        for x in range(2):
            for y in range(2):
                assert_equal(J.num[x][y], np.array([[(1 + y + 2 * x) * 2]]))
                assert_equal(J.den[x][y], np.array([[1, 3, 2]]))

    H = Transfer([1, 2], [1, 2, 3]) * np.arange(1, 5).reshape(2, 2)
    HH = H * H

    for x in range(4):
        assert_equal(sum(HH.den, [])[x], np.array([[1., 4., 10., 12., 9.]]))
        assert_equal(sum(HH.num, [])[x], (x + 1)**2 * np.array([[1., 4., 4.]]))

    F = Transfer(1, [1, 1])
    H = State(1, 2, 3, 4, 0.1)
    with assert_raises(ValueError):
        F * H
    with assert_raises(ValueError):
        F * 'string'
示例#19
0
def test_simple_lqry():
    # Scalar matrices
    H = State(1, 1, 1, 1)
    k, x, e = lqr(H, Q=3, weight_on='output')
    assert_almost_equal(array([k[0, 0], x[0, 0], e[0]]), [1.5, 3, -0.5 + 0j])
    # Wrong S shape
    assert_raises(ValueError, lqr, H, Q=3, S=eye(2), weight_on='output')
示例#20
0
def test_State_matmul_rmatmul_ndarray():
    H = State([[-5, -2], [1, 0]], [[2], [0]], [3, 1], 1)
    J1 = np.array([[-5., -2., 0., 0., 0., 0., 2., 4., 6., 8.],
                   [1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
                   [0., 0., -5., -2., 0., 0., 10., 12., 14., 16.],
                   [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
                   [0., 0., 0., 0., -5., -2., 18., 20., 22., 24.],
                   [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
                   [3., 1., 0., 0., 0., 0., 1., 2., 3., 4.],
                   [0., 0., 3., 1., 0., 0., 5., 6., 7., 8.],
                   [0., 0., 0., 0., 3., 1., 9., 10., 11., 12.]])

    J2 = np.array([[-5., -2., 0., 0., 0., 0., 2., 0., 0.],
                   [1., 0., 0., 0., 0., 0., 0., 0., 0.],
                   [0., 0., -5., -2., 0., 0., 0., 2., 0.],
                   [0., 0., 1., 0., 0., 0., 0., 0., 0.],
                   [0., 0., 0., 0., -5., -2., 0., 0., 2.],
                   [0., 0., 0., 0., 1., 0., 0., 0., 0.],
                   [3., 1., 6., 2., 9., 3., 1., 2., 3.],
                   [12., 4., 15., 5., 18., 6., 4., 5., 6.],
                   [21., 7., 24., 8., 27., 9., 7., 8., 9.],
                   [30., 10., 33., 11., 36., 12., 10., 11., 12.]])

    mat = np.arange(1, 13).reshape(3, 4)
    Fm = concatenate_state_matrices(mat @ H)
    assert_array_almost_equal(J1, Fm)
    Fm = concatenate_state_matrices(H @ mat)
    assert_array_almost_equal(J1, Fm)

    mat = np.arange(1, 13).reshape(4, 3)
    Fm = concatenate_state_matrices(mat @ H)
    assert_array_almost_equal(J2, Fm)
    Fm = concatenate_state_matrices(H @ mat)
    assert_array_almost_equal(J2, Fm)
示例#21
0
def test_verbosity_State(capsys):
    _ = State.validate_arguments(-2 * np.eye(2),
                                 np.eye(2), [[1, -3], [0, 0]],
                                 [[0, 1], [-1, 0]],
                                 verbose=True)

    out, err = capsys.readouterr()
    assert not err.strip()
    assert out == ("========================================\n"
                   "Handling A\n"
                   "========================================\n"
                   "Trying to np.array A\n"
                   "========================================\n"
                   "Handling B\n"
                   "========================================\n"
                   "Trying to np.array B\n"
                   "========================================\n"
                   "Handling C\n"
                   "========================================\n"
                   "Trying to np.array C\n"
                   "========================================\n"
                   "Handling D\n"
                   "========================================\n"
                   "Trying to np.array D\n"
                   "All seems OK. Moving to shape mismatch check\n")
示例#22
0
def test_feedback_static_static():
    G = State(5)
    H = Transfer(4)
    assert_almost_equal(feedback(G, G).d, array([[10.208333333333334]]))
    assert_almost_equal(feedback(G, H).d, array([[10.263157894736842]]))
    assert_almost_equal(feedback(H, G).d, array([[8.210526315789473]]))
    assert_almost_equal(feedback(H, H).num, array([[8.266666666666666]]))
def test_impulse_response_plot_shape():
    seed(1234)
    assert_equal(len(impulse_response_plot(Transfer(5, dt=0.5)).axes), 1)
    a, b, c = -3*eye(5) + rand(5, 5), rand(5, 3), rand(4, 5)
    G = State(a, b, c)
    f = impulse_response_plot(G)
    prop = f.axes[0].get_subplotspec().get_topmost_subplotspec().get_gridspec()
    assert_equal(prop.get_geometry(), G.shape)
示例#24
0
def test_step_response_plot_shape():
    seed(1234)
    f = step_response_plot(Transfer(5, dt=0.5))
    assert f.shape == (1, 1)
    a, b, c = -3*eye(5) + rand(5, 5), rand(5, 3), rand(4, 5)
    G = State(a, b, c)
    f = step_response_plot(G)
    assert f.shape == G.shape
示例#25
0
def test_static_model_conversion_sampling_period():
    G = State(np.eye(5), dt=0.001)
    H = state_to_transfer(G)
    assert_(H._isgain)  # 0
    assert_(not H._isSISO)  # 1
    assert_equal(H.SamplingPeriod, 0.001)  # 2
    K = transfer_to_state(H)
    assert_equal(K.SamplingPeriod, 0.001)  # 3
示例#26
0
def test_State_algebra_add_radd():
    sta_siso = State(5)
    sta_mimo = State(2.0 * np.eye(3))
    dyn_siso = State(haroldcompanion([1, 3, 3, 1]), e_i(3, -1), e_i(3, 1).T)
    dyn_mimo = State(haroldcompanion([1, 3, 3, 1]), e_i(3, [1, 2]), np.eye(3))
    dyn_mimo_sq = State(haroldcompanion([1, 3, 3, 1]), np.eye(3), np.eye(3))

    G = dyn_mimo + sta_siso
    assert_array_almost_equal(G.d,
                              sta_siso.to_array() * np.ones(dyn_mimo.shape))
    assert_raises(ValueError, dyn_mimo.__add__, sta_mimo)
    G = dyn_mimo_sq + sta_mimo
    assert_array_almost_equal(G.d, 2. * np.eye(3))
    G = dyn_mimo + dyn_siso
    J = np.array([[0., 1., 0., 0., 0., 0., 0., 0.],
                  [0., 0., 1., 0., 0., 0., 1., 0.],
                  [-1., -3., -3., 0., 0., 0., 0., 1.],
                  [0., 0., 0., 0., 1., 0., 0., 0.],
                  [0., 0., 0., 0., 0., 1., 0., 0.],
                  [0., 0., 0., -1., -3., -3., 1., 1.],
                  [1., 0., 0., 0., 1., 0., 0., 0.],
                  [0., 1., 0., 0., 1., 0., 0., 0.],
                  [0., 0., 1., 0., 1., 0., 0., 0.]])
    assert_array_almost_equal(concatenate_state_matrices(G), J)
    assert_raises(ValueError, dyn_mimo.__add__, dyn_mimo_sq)
    assert_raises(ValueError, dyn_mimo.__sub__, dyn_mimo_sq)
    assert_raises(ValueError, dyn_mimo.__radd__, dyn_mimo_sq)
    assert_raises(ValueError, dyn_mimo.__rsub__, dyn_mimo_sq)
示例#27
0
def test_simple_kalman_decomp():
    assert_raises(ValueError, kalman_decomposition, 1)
    G = State([[2, 1, 1], [5, 3, 6], [-5, -1, -4]], [[1], [0], [0]], [1, 0, 0])
    F = kalman_decomposition(G)
    J = np.array([[2, 0., -1.41421356, -1.], [7.07106781, -3, -7., 0.],
                  [0, 0, 2., 0.], [-1, 0, 0., 0.]])
    assert_array_almost_equal(F.a, J[:3, :3])
    assert_array_almost_equal(F.b, J[:3, [-1]])
    assert_array_almost_equal(F.c, J[[-1], :3])
示例#28
0
def test_State_Instantiations():
    assert_raises(TypeError, State)
    G = State(5)
    assert_(G.a.size == 0)
    assert_(G._isSISO)
    assert_(G._isgain)
    assert_equal(G.d, np.array([[5.]]))

    G = State(np.eye(2))
    assert_equal(G.shape, (2, 2))
    assert_(G._isgain)
    assert_(not G._isSISO)

    # Wrong sized A, B, C, D
    assert_raises(ValueError, State, np.ones((3, 2)), [[1], [2]], [1, 2])
    assert_raises(ValueError, State, np.eye(2), [[1], [2], [3]], [1, 2])
    assert_raises(ValueError, State, np.eye(2), [[1], [2]], [1, 2, 3])
    assert_raises(ValueError, State, np.eye(2), [[1], [2]], [1, 2], [0, 0])
示例#29
0
def test_frequency_response():
    seed(1234)
    # SISO, only integrators
    G = Transfer([1, 0], [1, 0, 0, 0])
    _, _ = frequency_response(G)
    G = Transfer([1, -1], [1, -2, 1], dt=0.1)
    _, _ = frequency_response(G)
    # Static Gains
    G = Transfer(5, dt=0.5)
    _, _ = frequency_response(G)
    G = Transfer(eye(5))
    _, _ = frequency_response(G)
    # MIMO
    a, b, c = -3 * eye(5) + rand(5, 5), rand(5, 3), rand(4, 5)
    G = State(a, b, c)
    _, _ = frequency_response(G)
    G = State(a, b, c, dt=1)
    _, _ = frequency_response(G)
示例#30
0
def test_minimal_realization_State():
    M = array([[-6.5, 0.5, 6.5, -6.5, 0., 1., 0.],
               [-0.5, -5.5, -5.5, 5.5, 2., 1., 2.],
               [-0.5, 0.5, 0.5, -6.5, 3., 4., 3.],
               [-0.5, 0.5, -5.5, -0.5, 3., 2., 3.],
               [1., 1., 0., 0., 0., 0., 0.]])
    G = State(*matrix_slice(M, (1, 4), corner='sw'))
    H = minimal_realization(G)
    assert_(H.a.shape, (2, 2))
示例#31
0
def test_undiscretize_backeuler_nearzero_eig():
    G = Transfer(1, [1, 0, 0], 0.1)
    assert_raises(ValueError, undiscretize, G, method='<<')
    arr = np.arange(9).reshape(3, 3).astype(float)
    arr[0, 0] = 1e-15
    G = State(arr, np.random.rand(3), np.random.rand(3), dt=0.1)
    with catch_warnings():
        simplefilter("error")
        assert_raises(_rcond_warn, undiscretize, G, '<<')
示例#32
0
def test_undiscretize():
    ac = eye(2)
    bc = 0.5 * ones((2, 1))
    cc = np.array([[0.75, 1.0], [1.0, 1.0], [1.0, 0.25]])
    dc = np.array([[0.0], [0.0], [-0.33]])
    G = State(ac, bc, cc, dc)
    for method in ('zoh', 'foh', 'tustin', '>>', '<<'):
        print(method)
        Gd = discretize(G, dt=0.5, method=method)
        Gu = undiscretize(Gd)
        assert_array_almost_equal(Gu.a, ac)
        assert_array_almost_equal(Gu.b, bc)
        assert_array_almost_equal(Gu.c, cc)
        assert_array_almost_equal(Gu.d, dc)

    Gd = State(zeros([2, 2]), ones([2, 1]), ones([1, 2]), dt=1)
    Gd.DiscretizedWith = '<<'
    assert_raises(ValueError, undiscretize, Gd)
示例#33
0
def test_State_algebra_mul_rmul_scalar_array():
    G = State(np.diag([-1, -2]), [[1, 2], [3, 4]], np.eye(2))
    F = G * np.eye(2)
    Fm = G @ np.eye(2)
    assert_equal(concatenate_state_matrices(F), concatenate_state_matrices(Fm))
    F = np.eye(2) * G
    Fm = np.eye(2) @ G
    assert_equal(concatenate_state_matrices(F), concatenate_state_matrices(Fm))
    H = 1 / 2 * G
    assert_equal(H.c, 0.5 * G.c)
示例#34
0
def test_State_to_array():
    G = State(1, 1, 1)
    H = State(5)
    with assert_raises(ValueError):
        G.to_array()

    assert_equal(H.to_array(), np.array([[5]]))
    H = State(np.ones((4, 4)))
    assert_equal(H.to_array(), np.ones((4, 4)))
示例#35
0
def test_State_algebra_truediv_rtruediv():
    G = State(1, 2, 3, 4)
    F = G/0.5
    assert_equal(F.b, np.array([[4.]]))
    assert_equal(F.d, np.array([[8.]]))
    G.d = 0.
    with assert_raises(LinAlgError):
        G/G
    with assert_raises(ValueError):
        G/3j

    G.d = 4
    # nonminimal but acceptable
    H = G / G
    ha, hb, hc, hd = H.matrices

    assert_array_almost_equal(ha, [[1, -1.5], [0, -0.5]])
    assert_array_almost_equal(hb, [[0.5], [0.5]])
    assert_array_almost_equal(hc, [[3, -3]])
    assert_array_almost_equal(hd, [[1]])

    G = State(np.eye(3)*0.5)
    assert_array_almost_equal((1 / G).to_array(), np.eye(3)*2)
示例#36
0
def test_verbosity_State(capsys):
    _ = State.validate_arguments(-2*np.eye(2), np.eye(2),
                                 [[1, -3], [0, 0]], [[0, 1], [-1, 0]],
                                 verbose=True)

    out, err = capsys.readouterr()
    assert not err.strip()
    assert out == ("========================================\n"
                   "Handling A\n"
                   "========================================\n"
                   "Trying to np.array A\n"
                   "========================================\n"
                   "Handling B\n"
                   "========================================\n"
                   "Trying to np.array B\n"
                   "========================================\n"
                   "Handling C\n"
                   "========================================\n"
                   "Trying to np.array C\n"
                   "========================================\n"
                   "Handling D\n"
                   "========================================\n"
                   "Trying to np.array D\n"
                   "All seems OK. Moving to shape mismatch check\n")