def pdf(self, *args): from sympy.functions.special.bessel import besselk mu, sigma = self.mu, self.sigma mu_T = mu.transpose() k = S(len(mu)) sigma_inv = sigma.inv() args = ImmutableMatrix(args) args_T = args.transpose() x = (mu_T*sigma_inv*mu)[0] y = (args_T*sigma_inv*args)[0] v = 1 - k/2 return S(2)/((2*pi)**(S(k)/2)*sqrt(det(sigma)))\ *(y/(2 + x))**(S(v)/2)*besselk(v, sqrt((2 + x)*(y)))\ *exp((args_T*sigma_inv*mu)[0])
def pdf(self, *args): mu, sigma = self.mu, self.shape_mat v = S(self.dof) k = S(mu.shape[0]) sigma_inv = sigma.inv() args = ImmutableMatrix(args) x = args - mu return gamma((k + v)/2)/(gamma(v/2)*(v*pi)**(k/2)*sqrt(det(sigma)))\ *(1 + 1/v*(x.transpose()*sigma_inv*x)[0])**((-v - k)/2)
def test_classof(): A = Matrix(3, 3, range(9)) B = ImmutableMatrix(3, 3, range(9)) C = MatrixSymbol('C', 3, 3) assert classof(A, A) == Matrix assert classof(B, B) == ImmutableMatrix assert classof(A, B) == ImmutableMatrix assert classof(B, A) == ImmutableMatrix raises(TypeError, lambda: classof(A, C))
def test_UGate_OneQubitGate_combo(): v, w, f, g = symbols('v w f g') uMat1 = ImmutableMatrix([[v, w], [f, g]]) cMat1 = Matrix([[v, w + 1, 0, 0], [f + 1, g, 0, 0], [0, 0, v, w + 1], [0, 0, f + 1, g]]) u1 = X(0) + UGate(0, uMat1) assert represent(u1, nqubits=2) == cMat1 uMat2 = ImmutableMatrix([[1 / sqrt(2), 1 / sqrt(2)], [I / sqrt(2), -I / sqrt(2)]]) cMat2_1 = Matrix([[Rational(1, 2) + I / 2, Rational(1, 2) - I / 2], [Rational(1, 2) - I / 2, Rational(1, 2) + I / 2]]) cMat2_2 = Matrix([[1, 0], [0, I]]) u2 = UGate(0, uMat2) assert represent(H(0) * u2, nqubits=1) == cMat2_1 assert represent(u2 * H(0), nqubits=1) == cMat2_2
def pdf(self, *args): from sympy.functions.special.gamma_functions import gamma mu, sigma = self.mu, self.shape_mat v = S(self.dof) k = S(len(mu)) sigma_inv = sigma.inv() args = ImmutableMatrix(args) x = args - mu return gamma((k + v)/2)/(gamma(v/2)*(v*pi)**(k/2)*sqrt(det(sigma)))\ *(1 + 1/v*(x.transpose()*sigma_inv*x)[0])**((-v - k)/2)
def test_doit_with_MatrixBase(): X = ImmutableMatrix([[1, 2], [3, 4]]) assert MatPow(X, 0).doit() == ImmutableMatrix(Identity(2)) assert MatPow(X, 1).doit() == X assert MatPow(X, 2).doit() == X**2 assert MatPow(X, -1).doit() == X.inv() assert MatPow(X, -2).doit() == (X.inv())**2 # less expensive than testing on a 2x2 assert MatPow(ImmutableMatrix([4]), S.Half).doit() == ImmutableMatrix([2])
def pdf(self, *args): mu, sigma = self.mu, self.sigma k = mu.shape[0] if len(args) == 1 and args[0].is_Matrix: args = args[0] else: args = ImmutableMatrix(args) x = args - mu density = S.One / sqrt((2 * pi)**(k) * det(sigma)) * exp( Rational(-1, 2) * x.transpose() * (sigma.inv() * x)) return MatrixElement(density, 0, 0)
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 _marginal_distribution(self, indices, sym): sym = ImmutableMatrix([Indexed(sym, i) for i in indices]) _mu, _sigma = self.mu, self.sigma k = self.mu.shape[0] for i in range(k): if i not in indices: _mu = _mu.row_del(i) _sigma = _sigma.col_del(i) _sigma = _sigma.row_del(i) return Lambda(tuple(sym), S.One/sqrt((2*pi)**(len(_mu))*det(_sigma))*exp( Rational(-1, 2)*(_mu - sym).transpose()*(_sigma.inv()*\ (_mu - sym)))[0])
def test_as_explicit(): A = ImmutableMatrix([[1, 2], [3, 4]]) assert MatPow(A, 0).as_explicit() == ImmutableMatrix(Identity(2)) assert MatPow(A, 1).as_explicit() == A assert MatPow(A, 2).as_explicit() == A**2 assert MatPow(A, -1).as_explicit() == A.inv() assert MatPow(A, -2).as_explicit() == (A.inv())**2 # less expensive than testing on a 2x2 A = ImmutableMatrix([4]); assert MatPow(A, S.Half).as_explicit() == A**S.Half
def marginal_distribution(self, indices, sym): sym = ImmutableMatrix([Indexed(sym, i) for i in indices]) _mu, _sigma = self.mu, self.sigma k = len(self.mu) for i in range(k): if i not in indices: _mu = _mu.row_del(i) _sigma = _sigma.col_del(i) _sigma = _sigma.row_del(i) return Lambda(sym, S(1)/sqrt((2*pi)**(len(_mu))*det(_sigma))*exp( -S(1)/2*(_mu - sym).transpose()*(_sigma.inv()*\ (_mu - sym)))[0])
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
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)
def MatrixGamma(symbol, alpha, beta, scale_matrix): """ Creates a random variable with Matrix Gamma Distribution. The density of the said distribution can be found at [1]. Parameters ========== alpha: Positive Real number Shape Parameter beta: Positive Real number Scale Parameter scale_matrix: Positive definite real square matrix Scale Matrix Returns ======= RandomSymbol Examples ======== >>> from sympy.stats import density, MatrixGamma >>> from sympy import MatrixSymbol, symbols >>> a, b = symbols('a b', positive=True) >>> M = MatrixGamma('M', a, b, [[2, 1], [1, 2]]) >>> X = MatrixSymbol('X', 2, 2) >>> density(M)(X).doit() 3**(-a)*b**(-2*a)*exp(Trace(Matrix([ [-2/3, 1/3], [ 1/3, -2/3]])*X)/b)*Determinant(X)**(a - 3/2)/(sqrt(pi)*gamma(a)*gamma(a - 1/2)) >>> density(M)([[1, 0], [0, 1]]).doit() 3**(-a)*b**(-2*a)*exp(-4/(3*b))/(sqrt(pi)*gamma(a)*gamma(a - 1/2)) References ========== .. [1] https://en.wikipedia.org/wiki/Matrix_gamma_distribution """ if isinstance(scale_matrix, list): scale_matrix = ImmutableMatrix(scale_matrix) return rv(symbol, MatrixGammaDistribution, (alpha, beta, scale_matrix))
def Wishart(symbol, n, scale_matrix): """ Creates a random variable with Wishart Distribution. The density of the said distribution can be found at [1]. Parameters ========== n: Positive Real number Represents degrees of freedom scale_matrix: Positive definite real square matrix Scale Matrix Returns ======= RandomSymbol Examples ======== >>> from sympy.stats import density, Wishart >>> from sympy import MatrixSymbol, symbols >>> n = symbols('n', positive=True) >>> W = Wishart('W', n, [[2, 1], [1, 2]]) >>> X = MatrixSymbol('X', 2, 2) >>> density(W)(X).doit() 2**(-n)*3**(-n/2)*exp(Trace(Matrix([ [-1/3, 1/6], [ 1/6, -1/3]])*X))*Determinant(X)**(n/2 - 3/2)/(sqrt(pi)*gamma(n/2)*gamma(n/2 - 1/2)) >>> density(W)([[1, 0], [0, 1]]).doit() 2**(-n)*3**(-n/2)*exp(-2/3)/(sqrt(pi)*gamma(n/2)*gamma(n/2 - 1/2)) References ========== .. [1] https://en.wikipedia.org/wiki/Wishart_distribution """ if isinstance(scale_matrix, list): scale_matrix = ImmutableMatrix(scale_matrix) return rv(symbol, WishartDistribution, (n, scale_matrix))
def test_OneMatrix(): A = MatrixSymbol('A', n, m) a = MatrixSymbol('a', n, 1) U = OneMatrix(n, m) assert U.shape == (n, m) assert isinstance(A + U, Add) assert transpose(U) == OneMatrix(m, n) assert U.conjugate() == U assert OneMatrix(n, n)**0 == Identity(n) with raises(ShapeError): U**0 with raises(ShapeError): U**2 U = OneMatrix(n, n) assert U[1, 2] == 1 U = OneMatrix(2, 3) assert U.as_explicit() == ImmutableMatrix.ones(2, 3)
def test_OneMatrix(): A = MatrixSymbol('A', n, m) a = MatrixSymbol('a', n, 1) U = OneMatrix(n, m) assert U.shape == (n, m) assert isinstance(A + U, Add) assert transpose(U) == OneMatrix(m, n) assert U.conjugate() == U assert OneMatrix(n, n) ** 0 == Identity(n) with raises(ShapeError): U ** 0 with raises(ShapeError): U ** 2 U = OneMatrix(n, n) assert U[1, 2] == 1 U = OneMatrix(2, 3) assert U.as_explicit() == ImmutableMatrix.ones(2, 3)
def test_ZeroMatrix(): A = MatrixSymbol('A', n, m) Z = ZeroMatrix(n, m) assert A + Z == A assert A*Z.T == ZeroMatrix(n, n) assert Z*A.T == ZeroMatrix(n, n) assert A - A == ZeroMatrix(*A.shape) assert Z assert transpose(Z) == ZeroMatrix(m, n) assert Z.conjugate() == Z assert ZeroMatrix(n, n)**0 == Identity(n) with raises(NonSquareMatrixError): Z**0 with raises(NonSquareMatrixError): Z**1 with raises(NonSquareMatrixError): Z**2 assert ZeroMatrix(3, 3).as_explicit() == ImmutableMatrix.zeros(3, 3)
def test_Identity(): A = MatrixSymbol('A', n, m) i, j = symbols('i j') In = Identity(n) Im = Identity(m) assert A*Im == A assert In*A == A assert transpose(In) == In assert In.inverse() == In assert In.conjugate() == In assert In[i, j] != 0 assert Sum(In[i, j], (i, 0, n-1), (j, 0, n-1)).subs(n,3).doit() == 3 assert Sum(Sum(In[i, j], (i, 0, n-1)), (j, 0, n-1)).subs(n,3).doit() == 3 # If range exceeds the limit `(0, n-1)`, do not remove `Piecewise`: expr = Sum(In[i, j], (i, 0, n-1)) assert expr.doit() == 1 expr = Sum(In[i, j], (i, 0, n-2)) assert expr.doit().dummy_eq( Piecewise( (1, (j >= 0) & (j <= n-2)), (0, True) ) ) expr = Sum(In[i, j], (i, 1, n-1)) assert expr.doit().dummy_eq( Piecewise( (1, (j >= 1) & (j <= n-1)), (0, True) ) ) assert Identity(3).as_explicit() == ImmutableMatrix.eye(3)
def test_nfloat(): from sympy.core.basic import _aresame from sympy.polys.rootoftools import rootof x = Symbol("x") eq = x**Rational(4, 3) + 4 * x**(S.One / 3) / 3 assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0 / 3) * x**(S.One / 3)) assert _aresame(nfloat(eq, exponent=True), x**(4.0 / 3) + (4.0 / 3) * x**(1.0 / 3)) eq = x**Rational(4, 3) + 4 * x**(x / 3) / 3 assert _aresame(nfloat(eq), x**Rational(4, 3) + (4.0 / 3) * x**(x / 3)) big = 12345678901234567890 # specify precision to match value used in nfloat Float_big = Float(big, 15) assert _aresame(nfloat(big), Float_big) assert _aresame(nfloat(big * x), Float_big * x) assert _aresame(nfloat(x**big, exponent=True), x**Float_big) assert nfloat(cos(x + sqrt(2))) == cos(x + nfloat(sqrt(2))) # issue 6342 f = S('x*lamda + lamda**3*(x/2 + 1/2) + lamda**2 + 1/4') assert not any(a.free_symbols for a in solveset(f.subs(x, -0.139))) # issue 6632 assert nfloat(-100000*sqrt(2500000001) + 5000000001) == \ 9.99999999800000e-11 # issue 7122 eq = cos(3 * x**4 + y) * rootof(x**5 + 3 * x**3 + 1, 0) assert str(nfloat(eq, exponent=False, n=1)) == '-0.7*cos(3.0*x**4 + y)' # issue 10933 for ti in (dict, Dict): d = ti({S.Half: S.Half}) n = nfloat(d) assert isinstance(n, ti) assert _aresame(list(n.items()).pop(), (S.Half, Float(.5))) for ti in (dict, Dict): d = ti({S.Half: S.Half}) n = nfloat(d, dkeys=True) assert isinstance(n, ti) assert _aresame(list(n.items()).pop(), (Float(.5), Float(.5))) d = [S.Half] n = nfloat(d) assert type(n) is list assert _aresame(n[0], Float(.5)) assert _aresame(nfloat(Eq(x, S.Half)).rhs, Float(.5)) assert _aresame(nfloat(S(True)), S(True)) assert _aresame(nfloat(Tuple(S.Half))[0], Float(.5)) assert nfloat(Eq((3 - I)**2 / 2 + I, 0)) == S.false # pass along kwargs assert nfloat([{S.Half: x}], dkeys=True) == [{Float(0.5): x}] # Issue 17706 A = MutableMatrix([[1, 2], [3, 4]]) B = MutableMatrix( [[Float('1.0', precision=53), Float('2.0', precision=53)], [Float('3.0', precision=53), Float('4.0', precision=53)]]) assert _aresame(nfloat(A), B) A = ImmutableMatrix([[1, 2], [3, 4]]) B = ImmutableMatrix( [[Float('1.0', precision=53), Float('2.0', precision=53)], [Float('3.0', precision=53), Float('4.0', precision=53)]]) assert _aresame(nfloat(A), B)
def test_doit_drills_down(): X = ImmutableMatrix([[1, 2], [3, 4]]) Y = ImmutableMatrix([[2, 3], [4, 5]]) assert MatMul(X, MatPow(Y, 2)).doit() == X * Y**2 assert MatMul(C, Transpose(D * C)).doit().args == (C, C.T, D.T)
def test_collapse_MatrixBase(): A = Matrix([[1, 1], [1, 1]]) B = Matrix([[1, 2], [3, 4]]) assert MatMul(A, B).doit() == ImmutableMatrix([[4, 6], [4, 6]])
def test_MatrixElement_doit(): u = MatrixSymbol('u', 2, 1) v = ImmutableMatrix([3, 5]) assert u[0, 0].subs(u, v).doit() == v[0, 0]
def test_matmul_simplify(): A = MatrixSymbol('A', 1, 1) assert simplify(MatMul(A, ImmutableMatrix([[sin(x)**2 + cos(x)**2]]))) == \ MatMul(A, ImmutableMatrix([[1]]))
def test_dense_conversion(): X = MatrixSymbol('X', 2, 2) assert ImmutableMatrix(X) == ImmutableMatrix(2, 2, lambda i, j: X[i, j]) assert Matrix(X) == Matrix(2, 2, lambda i, j: X[i, j])
def __new__(cls, *args): args = list(map(sympify, args)) for i in range(len(args)): if isinstance(args[i], list): args[i] = ImmutableMatrix(args[i]) return Basic.__new__(cls, *args)
def test_equality(): a, b, c = Identity(3), eye(3), ImmutableMatrix(eye(3)) for x in [a, b, c]: for y in [a, b, c]: assert x.equals(y)
Matrix, ImmutableMatrix, MatrixExpr Here we test the extent to which they cooperate """ from sympy.core.symbol import symbols from sympy.matrices import (Matrix, MatrixSymbol, eye, Identity, ImmutableMatrix) from sympy.matrices.expressions import MatrixExpr, MatAdd from sympy.matrices.common import classof from sympy.testing.pytest import raises SM = MatrixSymbol('X', 3, 3) SV = MatrixSymbol('v', 3, 1) MM = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) IM = ImmutableMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) meye = eye(3) imeye = ImmutableMatrix(eye(3)) ideye = Identity(3) a, b, c = symbols('a,b,c') def test_IM_MM(): assert isinstance(MM + IM, ImmutableMatrix) assert isinstance(IM + MM, ImmutableMatrix) assert isinstance(2 * IM + MM, ImmutableMatrix) assert MM.equals(IM) def test_ME_MM(): assert isinstance(Identity(3) + MM, MatrixExpr)
def test_dense_conversion(): X = MatrixSymbol('X', 2, 2) x00, x01, x10, x11 = symbols('X_00 X_01 X_10 X_11') assert ImmutableMatrix(X) == ImmutableMatrix([[x00, x01], [x10, x11]]) assert Matrix(X) == Matrix([[x00, x01], [x10, x11]])
def test_TransferFunctionMatrix_functions(): tf5 = TransferFunction(a1 * s**2 + a2 * s - a0, s + a0, s) # Classmethod (from_matrix) mat_1 = ImmutableMatrix([[s * (s + 1) * (s - 3) / (s**4 + 1), 2], [p, p * (s + 1) / (s * (s**1 + 1))]]) mat_2 = ImmutableMatrix([[(2 * s + 1) / (s**2 - 9)]]) mat_3 = ImmutableMatrix([[1, 2], [3, 4]]) assert TransferFunctionMatrix.from_Matrix(mat_1, s) == \ TransferFunctionMatrix([[TransferFunction(s*(s - 3)*(s + 1), s**4 + 1, s), TransferFunction(2, 1, s)], [TransferFunction(p, 1, s), TransferFunction(p, s, s)]]) assert TransferFunctionMatrix.from_Matrix(mat_2, s) == \ TransferFunctionMatrix([[TransferFunction(2*s + 1, s**2 - 9, s)]]) assert TransferFunctionMatrix.from_Matrix(mat_3, p) == \ TransferFunctionMatrix([[TransferFunction(1, 1, p), TransferFunction(2, 1, p)], [TransferFunction(3, 1, p), TransferFunction(4, 1, p)]]) # Negating a TFM tfm1 = TransferFunctionMatrix([[TF1], [TF2]]) assert -tfm1 == TransferFunctionMatrix([[-TF1], [-TF2]]) tfm2 = TransferFunctionMatrix([[TF1, TF2, TF3], [tf5, -TF1, -TF3]]) assert -tfm2 == TransferFunctionMatrix([[-TF1, -TF2, -TF3], [-tf5, TF1, TF3]]) # subs() H_1 = TransferFunctionMatrix.from_Matrix(mat_1, s) H_2 = TransferFunctionMatrix([[ TransferFunction(a * p * s, k * s**2, s), TransferFunction(p * s, k * (s**2 - a), s) ]]) assert H_1.subs(p, 1) == TransferFunctionMatrix([[ TransferFunction(s * (s - 3) * (s + 1), s**4 + 1, s), TransferFunction(2, 1, s) ], [TransferFunction(1, 1, s), TransferFunction(1, s, s)]]) assert H_1.subs({p: 1}) == TransferFunctionMatrix([[ TransferFunction(s * (s - 3) * (s + 1), s**4 + 1, s), TransferFunction(2, 1, s) ], [TransferFunction(1, 1, s), TransferFunction(1, s, s)]]) assert H_1.subs({ p: 1, s: 1 }) == TransferFunctionMatrix([[ TransferFunction(s * (s - 3) * (s + 1), s**4 + 1, s), TransferFunction(2, 1, s) ], [TransferFunction(1, 1, s), TransferFunction(1, s, s)]]) # This should ignore `s` as it is `var` assert H_2.subs(p, 2) == TransferFunctionMatrix([[ TransferFunction(2 * a * s, k * s**2, s), TransferFunction(2 * s, k * (-a + s**2), s) ]]) assert H_2.subs(k, 1) == TransferFunctionMatrix([[ TransferFunction(a * p * s, s**2, s), TransferFunction(p * s, -a + s**2, s) ]]) assert H_2.subs(a, 0) == TransferFunctionMatrix([[ TransferFunction(0, k * s**2, s), TransferFunction(p * s, k * s**2, s) ]]) assert H_2.subs({ p: 1, k: 1, a: a0 }) == TransferFunctionMatrix([[ TransferFunction(a0 * s, s**2, s), TransferFunction(s, -a0 + s**2, s) ]]) # transpose() assert H_1.transpose() == TransferFunctionMatrix([[ TransferFunction(s * (s - 3) * (s + 1), s**4 + 1, s), TransferFunction(p, 1, s) ], [TransferFunction(2, 1, s), TransferFunction(p, s, s)]]) assert H_2.transpose() == TransferFunctionMatrix( [[TransferFunction(a * p * s, k * s**2, s)], [TransferFunction(p * s, k * (-a + s**2), s)]]) assert H_1.transpose().transpose() == H_1 assert H_2.transpose().transpose() == H_2 # elem_poles() assert H_1.elem_poles() == [[[ -sqrt(2) / 2 - sqrt(2) * I / 2, -sqrt(2) / 2 + sqrt(2) * I / 2, sqrt(2) / 2 - sqrt(2) * I / 2, sqrt(2) / 2 + sqrt(2) * I / 2 ], []], [[], [0]]] assert H_2.elem_poles() == [[[0, 0], [sqrt(a), -sqrt(a)]]] assert tfm2.elem_poles() == [[[ wn * (-zeta + sqrt((zeta - 1) * (zeta + 1))), wn * (-zeta - sqrt( (zeta - 1) * (zeta + 1))) ], [], [-p / a2]], [[-a0], [ wn * (-zeta + sqrt( (zeta - 1) * (zeta + 1))), wn * (-zeta - sqrt( (zeta - 1) * (zeta + 1))) ], [-p / a2]]] # elem_zeros() assert H_1.elem_zeros() == [[[-1, 0, 3], []], [[], []]] assert H_2.elem_zeros() == [[[0], [0]]] assert tfm2.elem_zeros() == [ [[], [], [a2 * p]], [[ -a2 / (2 * a1) - sqrt(4 * a0 * a1 + a2**2) / (2 * a1), -a2 / (2 * a1) + sqrt(4 * a0 * a1 + a2**2) / (2 * a1) ], [], [a2 * p]] ] # doit() H_3 = TransferFunctionMatrix([[ Series(TransferFunction(1, s**3 - 3, s), TransferFunction(s**2 - 2 * s + 5, 1, s), TransferFunction(1, s, s)) ]]) H_4 = TransferFunctionMatrix([[ Parallel(TransferFunction(s**3 - 3, 4 * s**4 - s**2 - 2 * s + 5, s), TransferFunction(4 - s**3, 4 * s**4 - s**2 - 2 * s + 5, s)) ]]) assert H_3.doit() == TransferFunctionMatrix( [[TransferFunction(s**2 - 2 * s + 5, s * (s**3 - 3), s)]]) assert H_4.doit() == TransferFunctionMatrix([[ TransferFunction((4 - s**3) * (4 * s**4 - s**2 - 2 * s + 5) + (s**3 - 3) * (4 * s**4 - s**2 - 2 * s + 5), (4 * s**4 - s**2 - 2 * s + 5)**2, s) ]]) # _flat() assert H_1._flat() == [ TransferFunction(s * (s - 3) * (s + 1), s**4 + 1, s), TransferFunction(2, 1, s), TransferFunction(p, 1, s), TransferFunction(p, s, s) ] assert H_2._flat() == [ TransferFunction(a * p * s, k * s**2, s), TransferFunction(p * s, k * (-a + s**2), s) ] assert H_3._flat() == [ Series(TransferFunction(1, s**3 - 3, s), TransferFunction(s**2 - 2 * s + 5, 1, s), TransferFunction(1, s, s)) ] assert H_4._flat() == [ Parallel(TransferFunction(s**3 - 3, 4 * s**4 - s**2 - 2 * s + 5, s), TransferFunction(4 - s**3, 4 * s**4 - s**2 - 2 * s + 5, s)) ]
def __call__(self, expr): if isinstance(expr, list): expr = ImmutableMatrix(expr) return self.pdf(expr)