def test_care_g(self):
        A = array([[-2, -1], [-1, -1]])
        Q = array([[0, 0], [0, 1]])
        B = array([[1, 0], [0, 4]])
        R = array([[2, 0], [0, 1]])
        S = array([[0, 0], [0, 0]])
        E = array([[2, 1], [1, 2]])

        X, L, G = care(A, B, Q, R, S, E)
        # print "The solution obtained is", X
        assert_array_almost_equal(dot(A.T,dot(X,E)) + dot(E.T,dot(X,A)) - \
            dot(dot(dot(E.T,dot(X,B))+S,inv(R) ) ,
            dot(B.T,dot(X,E))+S.T ) + Q , \
            zeros((2,2)))
        assert_array_almost_equal(dot(inv(R), dot(B.T, dot(X, E)) + S.T), G)

        A = array([[-2, -1], [-1, -1]])
        Q = array([[0, 0], [0, 1]])
        B = array([[1], [0]])
        R = 1
        S = array([[1], [0]])
        E = array([[2, 1], [1, 2]])

        X, L, G = care(A, B, Q, R, S, E)
        # print "The solution obtained is", X
        assert_array_almost_equal(dot(A.T,dot(X,E)) + dot(E.T,dot(X,A)) - \
            dot( dot( dot(E.T,dot(X,B))+S,1/R ) , dot(B.T,dot(X,E))+S.T ) \
            + Q , zeros((2,2)))
        assert_array_almost_equal(dot(1 / R, dot(B.T, dot(X, E)) + S.T), G)
    def test_care_g2(self):
        A = array([[-2, -1],[-1, -1]])
        Q = array([[0, 0],[0, 1]])
        B = array([[1],[0]])
        R = 1
        S = array([[1],[0]])
        E = array([[2, 1],[1, 2]])

        X,L,G = care(A,B,Q,R,S,E)
        # print("The solution obtained is", X)
        Gref = 1/R * (B.T @ X @ E + S.T)
        assert_array_almost_equal(
            A.T @ X @ E + E.T @ X @ A
            - (E.T @ X @ B + S) @ Gref + Q ,
            zeros((2,2)))
        assert_array_almost_equal(Gref , G)

        # Compare methods
        if slycot_check():
            X_scipy, L_scipy, G_scipy = care(
                A, B, Q, R, S, E, method='scipy')
            X_slycot, L_slycot, G_slycot = care(
                A, B, Q, R, S, E, method='slycot')
            assert_array_almost_equal(X_scipy, X_slycot)
            assert_array_almost_equal(L_scipy, L_slycot)
            assert_array_almost_equal(G_scipy, G_slycot)
示例#3
0
    def test_care_g(self):
        A = array([[-2, -1], [-1, -1]])
        Q = array([[0, 0], [0, 1]])
        B = array([[1, 0], [0, 4]])
        R = array([[2, 0], [0, 1]])
        S = array([[0, 0], [0, 0]])
        E = array([[2, 1], [1, 2]])

        X, L, G = care(A, B, Q, R, S, E)
        # print("The solution obtained is", X)
        Gref = solve(R, B.T.dot(X).dot(E) + S.T)
        assert_array_almost_equal(Gref, G)
        assert_array_almost_equal(
            A.T.dot(X).dot(E) + E.T.dot(X).dot(A) -
            (E.T.dot(X).dot(B) + S).dot(Gref) + Q, zeros((2, 2)))

        A = array([[-2, -1], [-1, -1]])
        Q = array([[0, 0], [0, 1]])
        B = array([[1], [0]])
        R = 1
        S = array([[1], [0]])
        E = array([[2, 1], [1, 2]])

        X, L, G = care(A, B, Q, R, S, E)
        # print("The solution obtained is", X)
        Gref = 1 / R * (B.T.dot(X).dot(E) + S.T)
        assert_array_almost_equal(
            A.T.dot(X).dot(E) + E.T.dot(X).dot(A) -
            (E.T.dot(X).dot(B) + S).dot(Gref) + Q, zeros((2, 2)))
        assert_array_almost_equal(Gref, G)
    def test_care_g(self):
        A = array([[-2, -1],[-1, -1]])
        Q = array([[0, 0],[0, 1]])
        B = array([[1, 0],[0, 4]])
        R = array([[2, 0],[0, 1]])
        S = array([[0, 0],[0, 0]])
        E = array([[2, 1],[1, 2]])

        X,L,G = care(A,B,Q,R,S,E)
        # print "The solution obtained is", X
        assert_array_almost_equal(dot(A.T,dot(X,E)) + dot(E.T,dot(X,A)) - \
            dot(dot(dot(E.T,dot(X,B))+S,inv(R) ) ,
            dot(B.T,dot(X,E))+S.T ) + Q , \
            zeros((2,2)))
        assert_array_almost_equal(dot( inv(R) , dot(B.T,dot(X,E)) + S.T) , G)

        A = array([[-2, -1],[-1, -1]])
        Q = array([[0, 0],[0, 1]])
        B = array([[1],[0]])
        R = 1
        S = array([[1],[0]])
        E = array([[2, 1],[1, 2]])

        X,L,G = care(A,B,Q,R,S,E)
        # print "The solution obtained is", X
        assert_array_almost_equal(dot(A.T,dot(X,E)) + dot(E.T,dot(X,A)) - \
            dot( dot( dot(E.T,dot(X,B))+S,1/R ) , dot(B.T,dot(X,E))+S.T ) \
            + Q , zeros((2,2)))
        assert_array_almost_equal(dot( 1/R , dot(B.T,dot(X,E)) + S.T) , G)
示例#5
0
    def test_care_g(self):
        A = matrix([[-2, -1],[-1, -1]])
        Q = matrix([[0, 0],[0, 1]])
        B = matrix([[1, 0],[0, 4]])
        R = matrix([[2, 0],[0, 1]])
        S = matrix([[0, 0],[0, 0]])
        E = matrix([[2, 1],[1, 2]])

        X,L,G = care(A,B,Q,R,S,E)
        # print("The solution obtained is", X)
        assert_array_almost_equal(
            A.T * X * E + E.T * X * A -
            (E.T * X * B + S) * solve(R, B.T * X * E + S.T)  + Q, zeros((2,2)))
        assert_array_almost_equal(solve(R, B.T * X * E + S.T), G)

        A = matrix([[-2, -1],[-1, -1]])
        Q = matrix([[0, 0],[0, 1]])
        B = matrix([[1],[0]])
        R = 1
        S = matrix([[1],[0]])
        E = matrix([[2, 1],[1, 2]])

        X,L,G = care(A,B,Q,R,S,E)
        # print("The solution obtained is", X)
        assert_array_almost_equal(
            A.T * X * E + E.T * X * A -
            (E.T * X * B + S) / R * (B.T * X * E + S.T) + Q , zeros((2,2)))
        assert_array_almost_equal(dot( 1/R , dot(B.T,dot(X,E)) + S.T) , G)
示例#6
0
    def test_care_g(self):
        A = matrix([[-2, -1], [-1, -1]])
        Q = matrix([[0, 0], [0, 1]])
        B = matrix([[1, 0], [0, 4]])
        R = matrix([[2, 0], [0, 1]])
        S = matrix([[0, 0], [0, 0]])
        E = matrix([[2, 1], [1, 2]])

        X, L, G = care(A, B, Q, R, S, E)
        # print("The solution obtained is", X)
        assert_array_almost_equal(
            A.T * X * E + E.T * X * A - (E.T * X * B + S) * inv(R) *
            (B.T * X * E + S.T) + Q, zeros((2, 2)))
        assert_array_almost_equal(inv(R) * (B.T * X * E + S.T), G)

        A = matrix([[-2, -1], [-1, -1]])
        Q = matrix([[0, 0], [0, 1]])
        B = matrix([[1], [0]])
        R = 1
        S = matrix([[1], [0]])
        E = matrix([[2, 1], [1, 2]])

        X, L, G = care(A, B, Q, R, S, E)
        # print("The solution obtained is", X)
        assert_array_almost_equal(
            A.T * X * E + E.T * X * A - (E.T * X * B + S) / R *
            (B.T * X * E + S.T) + Q, zeros((2, 2)))
        assert_array_almost_equal(dot(1 / R, dot(B.T, dot(X, E)) + S.T), G)
示例#7
0
 def test_care(self, matarrayin):
     """Test stabilizing and anti-stabilizing feedbacks, continuous"""
     A = matarrayin(np.diag([1, -1]))
     B = matarrayin(np.identity(2))
     Q = matarrayin(np.identity(2))
     R = matarrayin(np.identity(2))
     S = matarrayin(np.zeros((2, 2)))
     E = matarrayin(np.identity(2))
     X, L, G = care(A, B, Q, R, S, E, stabilizing=True)
     assert np.all(np.real(L) < 0)
     X, L, G = care(A, B, Q, R, S, E, stabilizing=False)
     assert np.all(np.real(L) > 0)
示例#8
0
    def test_care(self):
        #unit test for stabilizing and anti-stabilizing feedbacks
        #continuous-time

        A = np.diag([1,-1])
        B = np.identity(2)
        Q = np.identity(2)
        R = np.identity(2)
        S = 0 * B
        E = np.identity(2)
        X, L , G = care(A, B, Q, R, S, E, stabilizing=True)
        assert np.all(np.real(L) < 0)
        X, L , G = care(A, B, Q, R, S, E, stabilizing=False)
        assert np.all(np.real(L) > 0)
    def test_care(self):
        #unit test for stabilizing and anti-stabilizing feedbacks
        #continuous-time

        A = np.diag([1,-1])
        B = np.identity(2)
        Q = np.identity(2)
        R = np.identity(2)
        S = 0 * B
        E = np.identity(2)
        X, L , G = care(A, B, Q, R, S, E, stabilizing=True)
        assert np.all(np.real(L) < 0)
        X, L , G = care(A, B, Q, R, S, E, stabilizing=False)
        assert np.all(np.real(L) > 0)
示例#10
0
    def test_care(self, matarrayin):
        """Test stabilizing and anti-stabilizing feedback, continuous"""
        A = matarrayin(np.diag([1, -1]))
        B = matarrayin(np.identity(2))
        Q = matarrayin(np.identity(2))
        R = matarrayin(np.identity(2))
        S = matarrayin(np.zeros((2, 2)))
        E = matarrayin(np.identity(2))

        X, L, G = care(A, B, Q, R, S, E, stabilizing=True)
        assert np.all(np.real(L) < 0)

        if slycot_check():
            X, L, G = care(A, B, Q, R, S, E, stabilizing=False)
            assert np.all(np.real(L) > 0)
        else:
            with pytest.raises(ControlArgument, match="'scipy' not valid"):
                X, L, G = care(A, B, Q, R, S, E, stabilizing=False)
示例#11
0
    def test_care(self):
        A = array([[-2, -1], [-1, -1]])
        Q = array([[0, 0], [0, 1]])
        B = array([[1, 0], [0, 4]])

        X, L, G = care(A, B, Q)
        # print("The solution obtained is", X)
        M = A.T.dot(X) + X.dot(A) - X.dot(B).dot(B.T).dot(X) + Q
        assert_array_almost_equal(M, zeros((2, 2)))
        assert_array_almost_equal(B.T.dot(X), G)
示例#12
0
    def test_care(self):
        A = matrix([[-2, -1], [-1, -1]])
        Q = matrix([[0, 0], [0, 1]])
        B = matrix([[1, 0], [0, 4]])

        X, L, G = care(A, B, Q)
        # print("The solution obtained is", X)
        assert_array_almost_equal(A.T * X + X * A - X * B * B.T * X + Q,
                                  zeros((2, 2)))
        assert_array_almost_equal(B.T * X, G)
    def test_care(self):
        A = array([[-2, -1], [-1, -1]])
        Q = array([[0, 0], [0, 1]])
        B = array([[1, 0], [0, 4]])

        X, L, G = care(A, B, Q)
        # print "The solution obtained is", X
        assert_array_almost_equal(dot(A.T,X) + dot(X,A) - \
            dot(X,dot(B,dot(B.T,X))) + Q , zeros((2,2)))
        assert_array_almost_equal(dot(B.T, X), G)
示例#14
0
    def test_care(self):
        A = array([[-2, -1],[-1, -1]])
        Q = array([[0, 0],[0, 1]])
        B = array([[1, 0],[0, 4]])

        X, L, G = care(A, B, Q)
        # print("The solution obtained is", X)
        M = A.T @ X + X @ A - X @ B @ B.T @ X + Q
        assert_array_almost_equal(M,
                                  zeros((2,2)))
        assert_array_almost_equal(B.T @ X, G)

        # Compare methods
        if slycot_check():
            X_scipy, L_scipy, G_scipy = care(A, B, Q, method='scipy')
            X_slycot, L_slycot, G_slycot = care(A, B, Q, method='slycot')
            assert_array_almost_equal(X_scipy, X_slycot)
            assert_array_almost_equal(np.sort(L_scipy), np.sort(L_slycot))
            assert_array_almost_equal(G_scipy, G_slycot)
示例#15
0
    def test_care(self):
        A = array([[-2, -1],[-1, -1]])
        Q = array([[0, 0],[0, 1]])
        B = array([[1, 0],[0, 4]])

        X,L,G = care(A,B,Q)
        # print "The solution obtained is", X
        assert_array_almost_equal(dot(A.T,X) + dot(X,A) - \
            dot(X,dot(B,dot(B.T,X))) + Q , zeros((2,2)))
        assert_array_almost_equal(dot(B.T,X) , G)
示例#16
0
    def test_care(self):
        A = matrix([[-2, -1],[-1, -1]])
        Q = matrix([[0, 0],[0, 1]])
        B = matrix([[1, 0],[0, 4]])

        X,L,G = care(A,B,Q)
        # print("The solution obtained is", X)
        assert_array_almost_equal(A.T * X + X * A - X * B * B.T * X + Q,
                                  zeros((2,2)))
        assert_array_almost_equal(B.T * X, G)
示例#17
0
    def test_raise(self):
        """ Test exception raise for invalid inputs """

        # correct shapes and forms
        A = array([[1, 0], [-1, -1]])
        Q = array([[2, 1], [1, 2]])
        C = array([[1, 0], [0, 1]])
        E = array([[2, 1], [1, 2]])

        # these fail
        Afq = array([[1, 0, 0], [-1, -1, 0]])
        Qfq = array([[2, 1, 0], [1, 2, 0]])
        Qfs = array([[2, 1], [-1, 2]])
        Cfd = array([[1, 0, 0], [0, 1, 0]])
        Efq = array([[2, 1, 0], [1, 2, 0]])

        for cdlyap in [lyap, dlyap]:
            with pytest.raises(ControlDimension):
                cdlyap(Afq, Q)
            with pytest.raises(ControlDimension):
                cdlyap(A, Qfq)
            with pytest.raises(ControlArgument):
                cdlyap(A, Qfs)
            with pytest.raises(ControlDimension):
                cdlyap(Afq, Q, C)
            with pytest.raises(ControlDimension):
                cdlyap(A, Qfq, C)
            with pytest.raises(ControlDimension):
                cdlyap(A, Q, Cfd)
            with pytest.raises(ControlDimension):
                cdlyap(A, Qfq, None, E)
            with pytest.raises(ControlDimension):
                cdlyap(A, Q, None, Efq)
            with pytest.raises(ControlArgument):
                cdlyap(A, Qfs, None, E)
            with pytest.raises(ControlArgument):
                cdlyap(A, Q, C, E)

        B = array([[1, 0], [0, 1]])
        Bf = array([[1, 0], [0, 1], [1, 1]])
        R = Q
        Rfs = Qfs
        Rfq = Qfq
        S = array([[0, 0], [0, 0]])
        Sf = array([[0, 0, 0], [0, 0, 0]])
        E = array([[2, 1], [1, 2]])
        Ef = array([[2, 1], [1, 2], [1, 2]])

        with pytest.raises(ControlDimension):
            care(Afq, B, Q)
        with pytest.raises(ControlDimension):
            care(A, B, Qfq)
        with pytest.raises(ControlDimension):
            care(A, Bf, Q)
        with pytest.raises(ControlDimension):
            care(1, B, 1)
        with pytest.raises(ControlArgument):
            care(A, B, Qfs)
        with pytest.raises(ControlArgument):
            dare(A, B, Q, Rfs)
        for cdare in [care, dare]:
            with pytest.raises(ControlDimension):
                cdare(Afq, B, Q, R, S, E)
            with pytest.raises(ControlDimension):
                cdare(A, B, Qfq, R, S, E)
            with pytest.raises(ControlDimension):
                cdare(A, Bf, Q, R, S, E)
            with pytest.raises(ControlDimension):
                cdare(A, B, Q, R, S, Ef)
            with pytest.raises(ControlDimension):
                cdare(A, B, Q, Rfq, S, E)
            with pytest.raises(ControlDimension):
                cdare(A, B, Q, R, Sf, E)
            with pytest.raises(ControlArgument):
                cdare(A, B, Qfs, R, S, E)
            with pytest.raises(ControlArgument):
                cdare(A, B, Q, Rfs, S, E)