Exemple #1
0
def test_squareBlockMatrix():
    n, m, l, k = symbols('n m l k', integer=True)
    A = MatrixSymbol('A', n, n)
    B = MatrixSymbol('B', n, m)
    C = MatrixSymbol('C', m, n)
    D = MatrixSymbol('D', m, m)
    X = BlockMatrix([[A, B], [C, D]])
    Y = BlockMatrix([[A]])

    assert X.is_square

    assert block_collapse(X + Identity(m + n)) == BlockMatrix(
        [[A + Identity(n), B], [C, D + Identity(m)]])
    Q = X + Identity(m + n)
    assert block_collapse(Inverse(Q)) == Inverse(block_collapse(Q))

    assert (X + MatrixSymbol('Q', n + m, n + m)).is_Add
    assert (X * MatrixSymbol('Q', n + m, n + m)).is_Mul

    assert Y.I.blocks[0, 0] == A.I
    assert Inverse(X, expand=True) == BlockMatrix([[
        (-B * D.I * C + A).I, -A.I * B * (D + -C * A.I * B).I
    ], [-(D - C * A.I * B).I * C * A.I, (D - C * A.I * B).I]])

    assert Inverse(X, expand=False).is_Inverse
    assert X.inverse().is_Inverse

    assert not X.is_Identity

    Z = BlockMatrix([[Identity(n), B], [C, D]])
    assert not Z.is_Identity
Exemple #2
0
def test_inverse():
    n, m, l = symbols('n m l', integer=True)
    A = MatrixSymbol('A', n, m)
    B = MatrixSymbol('B', m, l)
    C = MatrixSymbol('C', n, n)
    D = MatrixSymbol('D', n, n)
    E = MatrixSymbol('E', m, n)

    raises(ShapeError, lambda: Inverse(A))
    assert Inverse(Inverse(C)) == C

    assert Inverse(C) * C == Identity(C.rows)

    assert Inverse(eye(3)) == eye(3)

    assert Inverse(S(3)) == S(1) / 3

    assert Inverse(Identity(n)) == Identity(n)

    # Simplifies Muls if possible (i.e. submatrices are square)
    assert Inverse(C * D) == D.I * C.I
    # But still works when not possible
    assert Inverse(A * E).is_Inverse

    # We play nice with traditional explicit matrices
    assert Inverse(Matrix([[1, 2], [3, 4]])) == Matrix([[1, 2], [3, 4]]).inv()
Exemple #3
0
def test_combine_powers():
    assert combine_powers(MatMul(D, Inverse(D), D, evaluate=False)) == \
                 MatMul(Identity(n), D, evaluate=False)
    assert combine_powers(MatMul(B.T, Inverse(E*A), E, A, B, evaluate=False)) == \
        MatMul(B.T, Identity(m), B, evaluate=False)
    assert combine_powers(MatMul(A, E, Inverse(A*E), D, evaluate=False)) == \
        MatMul(Identity(n), D, evaluate=False)
 def pdf(self, x):
     M , U , V = self.location_matrix, self.scale_matrix_1, self.scale_matrix_2
     n, p = M.shape
     if isinstance(x, list):
         x = ImmutableMatrix(x)
     if not isinstance(x, (MatrixBase, MatrixSymbol)):
         raise ValueError("%s should be an isinstance of Matrix "
                 "or MatrixSymbol" % str(x))
     term1 = Inverse(V)*Transpose(x - M)*Inverse(U)*(x - M)
     num = exp(-Trace(term1)/S(2))
     den = (2*pi)**(S(n*p)/2) * Determinant(U)**S(p)/2 * Determinant(V)**S(n)/2
     return num/den
    def pdf(self, x):
        from sympy import eye
        if isinstance(x, list):
            x = ImmutableMatrix(x)
        if not isinstance(x, (MatrixBase, MatrixSymbol)):
            raise ValueError("%s should be an isinstance of Matrix "
                             "or MatrixSymbol" % str(x))
        nu, M, Omega, Sigma = self.nu, self.location_matrix, self.scale_matrix_1, self.scale_matrix_2
        n, p = M.shape

        K = multigamma((nu + n + p - 1)/2, p) * Determinant(Omega)**(-n/2) * Determinant(Sigma)**(-p/2) \
            / ((pi)**(n*p/2) * multigamma((nu + p - 1)/2, p))
        return K * (Determinant(eye(n) + Inverse(Sigma)*(x - M)*Inverse(Omega)*Transpose(x - M))) \
               **(-(nu + n + p -1)/2)
Exemple #6
0
def test_MatPow():
    n = Symbol('n', integer=True)
    A = MatrixSymbol('A', n, n)

    assert Inverse(A).is_Pow
    assert (A * A).is_Pow
    assert (A * A).exp == 2
    assert (A * A).base == A
    assert (A**n).exp == n

    assert A**0 == Identity(n)
    assert A**1 == A
    assert A**-1 == Inverse(A)
    raises(ShapeError, "MatrixSymbol('B', 3,2)**2")
Exemple #7
0
def test_Adjoint():
    from sympy.matrices import MatrixSymbol, Adjoint, Inverse, Transpose
    X = MatrixSymbol('X', 2, 2)
    Y = MatrixSymbol('Y', 2, 2)
    assert latex(Adjoint(X)) == r'X^\dag'
    assert latex(Adjoint(X + Y)) == r'\left(X + Y\right)^\dag'
    assert latex(Adjoint(X) + Adjoint(Y)) == r'X^\dag + Y^\dag'
    assert latex(Adjoint(X * Y)) == r'\left(X Y\right)^\dag'
    assert latex(Adjoint(Y) * Adjoint(X)) == r'Y^\dag X^\dag'
    assert latex(Adjoint(X**2)) == r'\left(X^{2}\right)^\dag'
    assert latex(Adjoint(X)**2) == r'\left(X^\dag\right)^{2}'
    assert latex(Adjoint(Inverse(X))) == r'\left(X^{-1}\right)^\dag'
    assert latex(Inverse(Adjoint(X))) == r'\left(X^\dag\right)^{-1}'
    assert latex(Adjoint(Transpose(X))) == r'\left(X^T\right)^\dag'
    assert latex(Transpose(Adjoint(X))) == r'\left(X^\dag\right)^T'
Exemple #8
0
def test_invariants():
    A = MatrixSymbol('A', n, m)
    B = MatrixSymbol('B', m, l)
    X = MatrixSymbol('X', n, n)
    objs = [Identity(n), ZeroMatrix(m, n), A, MatMul(A, B), MatAdd(A, A),
            Transpose(A), Adjoint(A), Inverse(X), MatPow(X, 2), MatPow(X, -1),
            MatPow(X, 0)]
    for obj in objs:
        assert obj == obj.__class__(*obj.args)
Exemple #9
0
def test_inverse():
    raises(ShapeError, lambda: Inverse(A))
    assert Inverse(Inverse(C)) == C

    assert Inverse(C) * C == Identity(C.rows)

    assert Inverse(eye(3)) == eye(3)

    assert Inverse(S(3)) == S(1) / 3

    assert Inverse(Identity(n)) == Identity(n)

    # Simplifies Muls if possible (i.e. submatrices are square)
    assert Inverse(C * D) == D.I * C.I
    # But still works when not possible
    assert Inverse(A * E).is_Inverse

    # We play nice with traditional explicit matrices
    assert Inverse(Matrix([[1, 2], [3, 4]])) == Matrix([[1, 2], [3, 4]]).inv()
Exemple #10
0
def test_Identity():
    A = MatrixSymbol('A', n, m)
    In = Identity(n)
    Im = Identity(m)

    assert A * Im == A
    assert In * A == A

    assert Transpose(In) == In
    assert Inverse(In) == In
    assert In.conjugate() == In
Exemple #11
0
def test_Identity():
    n, m = symbols('n m', integer=True)
    A = MatrixSymbol('A', n, m)
    In = Identity(n)
    Im = Identity(m)

    assert A * Im == A
    assert In * A == A

    assert Transpose(In) == In
    assert Inverse(In) == In
 def pdf(self, x):
     n, scale_matrix = self.n, self.scale_matrix
     p = scale_matrix.shape[0]
     if isinstance(x, list):
         x = ImmutableMatrix(x)
     if not isinstance(x, (MatrixBase, MatrixSymbol)):
         raise ValueError("%s should be an isinstance of Matrix "
                 "or MatrixSymbol" % str(x))
     sigma_inv_x = - Inverse(scale_matrix)*x / S(2)
     term1 = exp(Trace(sigma_inv_x))/((2**(p*n/S(2))) * multigamma(n/S(2), p))
     term2 = (Determinant(scale_matrix))**(-n/S(2))
     term3 = (Determinant(x))**(S(n - p - 1)/2)
     return term1 * term2 * term3
 def pdf(self, x):
     alpha , beta , scale_matrix = self.alpha, self.beta, self.scale_matrix
     p = scale_matrix.shape[0]
     if isinstance(x, list):
         x = ImmutableMatrix(x)
     if not isinstance(x, (MatrixBase, MatrixSymbol)):
         raise ValueError("%s should be an isinstance of Matrix "
                 "or MatrixSymbol" % str(x))
     sigma_inv_x = - Inverse(scale_matrix)*x / beta
     term1 = exp(Trace(sigma_inv_x))/((beta**(p*alpha)) * multigamma(alpha, p))
     term2 = (Determinant(scale_matrix))**(-alpha)
     term3 = (Determinant(x))**(alpha - S(p + 1)/2)
     return term1 * term2 * term3
Exemple #14
0
def test_MatPow():
    A = MatrixSymbol('A', n, n)

    AA = MatPow(A, 2)
    assert AA.exp == 2
    assert AA.base == A
    assert (A**n).exp == n

    assert A**0 == Identity(n)
    assert A**1 == A
    assert A**2 == AA
    assert A**-1 == Inverse(A)
    assert A**S.Half == sqrt(A)
    raises(ShapeError, lambda: MatrixSymbol('B', 3, 2)**2)
Exemple #15
0
def test_MatPow():
    A = MatrixSymbol('A', n, n)

    AA = MatPow(A, 2)
    assert AA.exp == 2
    assert AA.base == A
    assert (A**n).exp == n

    assert A**0 == Identity(n)
    assert A**1 == A
    assert A**2 == AA
    assert A**-1 == Inverse(A)
    assert (A**-1)**-1 == A
    assert (A**2)**3 == A**6
    assert A**S.Half == sqrt(A)
    assert A**Rational(1, 3) == cbrt(A)
    raises(NonSquareMatrixError, lambda: MatrixSymbol('B', 3, 2)**2)
Exemple #16
0
def test_xxinv():
    assert xxinv(MatMul(D, Inverse(D), D, evaluate=False)) == \
                 MatMul(Identity(n), D, evaluate=False)
Exemple #17
0
# importing sympy functions
import sympy
from sympy.matrices import Matrix, Inverse
x1, x2 = sympy.symbols('x1, x2')

XX1 = Matrix([0, 0])  # Sarting point at the origin XX1= [0,0]
FUNC = (x1 + 1)**2 + (x2 + 3)**2 + 4  # Function
F_PRIM = Matrix([[sympy.diff(FUNC, x1)], [sympy.diff(FUNC, x2)]
                 ])  # Derivating FUNC with respect to x1 and than x2
H = Matrix(
    [[sympy.diff(F_PRIM[0], x1),
      sympy.diff(F_PRIM[1], x1)],
     [sympy.diff(F_PRIM[0], x2),
      sympy.diff(F_PRIM[1], x2)]]
)  # Derivating F_PRIM with respect to x1 and than x2, becuase of quadratic funstion Hessian is obtained

#################### NEWTON'S METHOD ####################
XX2 = XX1 - Inverse(H) * F_PRIM.subs({
    x1: XX1[0],
    x2: XX1[0]
})  # XX2 = [-1, -3]

print(
    f"Function is quadratic therefore converges in one step. Hessian = {H} is positive therefore X2 = {XX2} is function's minimum"
)
'''
#Output:
Function is quadratic therefore converges in one step. Hessian = [2, 0, 0, 2] is positive therefore X2 = [-1, -3] is function's minimum.
'''
Exemple #18
0
def test_combine_powers():
    assert combine_powers(MatMul(D, Inverse(D), D, evaluate=False)) == \
                 MatMul(Identity(n), D, evaluate=False)
#################### NEWTON'S METHOD ####################

F_PRIM = Matrix([0, 0])  # Setting up empty matrix for X_PRIM
F_PRIM[0] = FUNC.diff(
    X[0])  # deriving FUNC with respect to x and y separatelly
F_PRIM[1] = FUNC.diff(X[1])  # F_PRIM = [[1.0*x], [1000.0*y]] = H * X

F_BIS = Matrix([[0, 0], [0, 0]])  # Setting up empty matrix for X_BIS
F_BIS[0] = F_PRIM[0].diff(
    X[0])  # deriving F_PRIM with respect to x and y separatelly
F_BIS[1] = F_PRIM[1].diff(X[0])
F_BIS[2] = F_PRIM[0].diff(X[1])
F_BIS[3] = F_PRIM[1].diff(X[1])  # F_BIS = [[1,0],[0,1000]] = H

X2 = X - Inverse(F_BIS) * F_PRIM  # X2 = [0, 0], cnvergence in one step

print("Newton's Method")
print(f"  Newton's Method converges in one step, X2 = {list(X2)}\n")

#################### GRADIENT DESCENT ####################

X2_gd = X1 - F_PRIM.subs({X[0]: X1[0], X[1]: X1[1]})
X3_gd = X2_gd - F_PRIM.subs({X[0]: X2_gd[0], X[1]: X2_gd[1]})
print("Gradient Descent Method")
print(f"  After first step of Gradient Descent Method X2 = {list(X2_gd)}")
print(f"  After second step of Gradient Descent Method X3 = {list(X3_gd)}")
print("  Gradient Descent Method Diverges.\n")

#################### CONJUGATE DESCENT ####################