def test_invertible(): assert ask(Q.invertible(X), Q.invertible(X)) assert ask(Q.invertible(Y)) is False assert ask(Q.invertible(X*Y), Q.invertible(X)) is False assert ask(Q.invertible(X*Z), Q.invertible(X)) is None assert ask(Q.invertible(X*Z), Q.invertible(X) & Q.invertible(Z)) is True assert ask(Q.invertible(X.T)) is None assert ask(Q.invertible(X.T), Q.invertible(X)) is True assert ask(Q.invertible(X.I)) is True assert ask(Q.invertible(Identity(3))) is True assert ask(Q.invertible(ZeroMatrix(3, 3))) is False assert ask(Q.invertible(X), Q.fullrank(X) & Q.square(X))
def test_positive_definite(): assert ask(Q.positive_definite(X), Q.positive_definite(X)) assert ask(Q.positive_definite(X.T), Q.positive_definite(X)) is True assert ask(Q.positive_definite(X.I), Q.positive_definite(X)) is True assert ask(Q.positive_definite(Y)) is False assert ask(Q.positive_definite(X)) is None assert ask(Q.positive_definite(X * Z * X), Q.positive_definite(X) & Q.positive_definite(Z)) is True assert ask(Q.positive_definite(X), Q.orthogonal(X)) assert ask(Q.positive_definite(Y.T * X * Y), Q.positive_definite(X) & Q.fullrank(Y)) is True assert not ask(Q.positive_definite(Y.T * X * Y), Q.positive_definite(X)) assert ask(Q.positive_definite(Identity(3))) is True assert ask(Q.positive_definite(ZeroMatrix(3, 3))) is False assert ask(Q.positive_definite(X + Z), Q.positive_definite(X) & Q.positive_definite(Z)) is True assert not ask(Q.positive_definite(-X), Q.positive_definite(X))
def test_positive_definite(): assert ask(Q.positive_definite(X), Q.positive_definite(X)) assert ask(Q.positive_definite(X.T), Q.positive_definite(X)) is True assert ask(Q.positive_definite(X.I), Q.positive_definite(X)) is True assert ask(Q.positive_definite(Y)) is False assert ask(Q.positive_definite(X)) is None assert ask(Q.positive_definite(X*Z*X), Q.positive_definite(X) & Q.positive_definite(Z)) is True assert ask(Q.positive_definite(X), Q.orthogonal(X)) assert ask(Q.positive_definite(Y.T*X*Y), Q.positive_definite(X) & Q.fullrank(Y)) is True assert not ask(Q.positive_definite(Y.T*X*Y), Q.positive_definite(X)) assert ask(Q.positive_definite(Identity(3))) is True assert ask(Q.positive_definite(ZeroMatrix(3, 3))) is False assert ask(Q.positive_definite(X + Z), Q.positive_definite(X) & Q.positive_definite(Z)) is True assert not ask(Q.positive_definite(-X), Q.positive_definite(X))
def test_fullrank(): assert ask(Q.fullrank(X), Q.fullrank(X)) assert ask(Q.fullrank(X**2), Q.fullrank(X)) assert ask(Q.fullrank(X.T), Q.fullrank(X)) is True assert ask(Q.fullrank(X)) is None assert ask(Q.fullrank(Y)) is None assert ask(Q.fullrank(X*Z), Q.fullrank(X) & Q.fullrank(Z)) is True assert ask(Q.fullrank(Identity(3))) is True assert ask(Q.fullrank(ZeroMatrix(3, 3))) is False assert ask(Q.invertible(X), ~Q.fullrank(X)) == False
def test_invertible_fullrank(): assert ask(Q.invertible(X), Q.fullrank(X)) is True
def test_fullrank(): assert ask(Q.fullrank(X), Q.fullrank(X)) assert ask(Q.fullrank(X.T), Q.fullrank(X)) is True assert ask(Q.fullrank(X)) is None assert ask(Q.fullrank(Y)) is None assert ask(Q.fullrank(X*Z), Q.fullrank(X) & Q.fullrank(Z)) is True assert ask(Q.fullrank(Identity(3))) is True assert ask(Q.fullrank(ZeroMatrix(3, 3))) is False assert ask(Q.invertible(X), ~Q.fullrank(X)) == False
def test_invertible_fullrank(): assert ask(Q.invertible(X), Q.fullrank(X))
""" Linear Regression """ from sympy import MatrixSymbol, Q, ZeroMatrix, assuming from sympy import Symbol n = Symbol('n') m = Symbol('m') X = MatrixSymbol('X', n, m) y = MatrixSymbol('y', n, 1) beta = (X.T*X).I * X.T*y from computations.matrices.blas import SYRK, GEMM from computations.matrices.lapack import POSV c = (POSV(X.T*X, X.T*y) + SYRK(1.0, X.T, 0.0, ZeroMatrix(m, m)) + GEMM(1.0, X.T, y, 0.0, ZeroMatrix(n, 1))) assumptions = Q.fullrank(X), Q.real_elements(X), Q.real_elements(y)
from sympy.matrices.expressions import MatrixSymbol from sympy import Symbol, Q n, k = 1000, 500 #Symbol('n'), Symbol('k') mu = MatrixSymbol('mu', n, 1) Sigma = MatrixSymbol('Sigma', n, n) H = MatrixSymbol('H', k, n) R = MatrixSymbol('R', k, k) data = MatrixSymbol('data', k, 1) newmu = mu + Sigma * H.T * (R + H * Sigma * H.T).I * (H * mu - data) newSigma = Sigma - Sigma * H.T * (R + H * Sigma * H.T).I * H * Sigma assumptions = (Q.positive_definite(Sigma), Q.symmetric(Sigma), Q.positive_definite(R), Q.symmetric(R), Q.fullrank(H)) inputs = mu, Sigma, H, R, data outputs = newmu, newSigma
from sympy.matrices.expressions import MatrixSymbol, Transpose from sympy import Symbol, Q n, k = Symbol('n'), Symbol('k') mu = MatrixSymbol('mu', n, 1) Sigma = MatrixSymbol('Sigma', n, n) H = MatrixSymbol('H', k, n) R = MatrixSymbol('R', k, k) data = MatrixSymbol('data', k, 1) newmu = mu + Sigma*H.T * (R + H*Sigma*H.T).I * (H*mu - data) newSigma= Sigma - Sigma*H.T * (R + H*Sigma*H.T).I * H * Sigma newSigma = -1.0*Sigma*H.T*(H*Transpose(H*Sigma) + R).I*H*Sigma + Sigma newmu = Sigma*H.T*(H*Transpose(H*Sigma) + R).I*(-1.0*data + H*mu) + mu assumptions = (Q.positive_definite(Sigma), Q.symmetric(Sigma), Q.positive_definite(R), Q.symmetric(R), Q.fullrank(H)) inputs = mu, Sigma, H, R, data outputs = newmu, newSigma