def test_Identity(): n, m = symbols('n m', integer=True) 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 In.transpose() == 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() == ImmutableDenseMatrix.eye(3)
def _entry(self, i, j, expand=True): coeff, matrices = self.as_coeff_matrices() if len(matrices) == 1: # situation like 2*X, matmul is just X return coeff * matrices[0][i, j] head, tail = matrices[0], matrices[1:] if len(tail) == 0: raise ValueError("lenth of tail cannot be 0") X = head Y = MatMul(*tail) from sympy.core.symbol import Dummy from sympy.concrete.summations import Sum from sympy.matrices import ImmutableMatrix k = Dummy('k', integer=True) if X.has(ImmutableMatrix) or Y.has(ImmutableMatrix): return coeff*Add(*[X[i, k]*Y[k, j] for k in range(X.cols)]) result = Sum(coeff*X[i, k]*Y[k, j], (k, 0, X.cols - 1)) try: if not X.cols.is_number: # Don't waste time in result.doit() if the sum bounds are symbolic expand = False except AttributeError: pass return result.doit() if expand else result
def _entry(self, i, j, expand=True): coeff, matrices = self.as_coeff_matrices() if len(matrices) == 1: # situation like 2*X, matmul is just X return coeff * matrices[0][i, j] head, tail = matrices[0], matrices[1:] if len(tail) == 0: raise ValueError("lenth of tail cannot be 0") X = head Y = MatMul(*tail) from sympy.core.symbol import Dummy from sympy.concrete.summations import Sum from sympy.matrices import ImmutableMatrix k = Dummy('k', integer=True) if X.has(ImmutableMatrix) or Y.has(ImmutableMatrix): return coeff * Add(*[X[i, k] * Y[k, j] for k in range(X.cols)]) result = Sum(coeff * X[i, k] * Y[k, j], (k, 0, X.cols - 1)) try: if not X.cols.is_number: # Don't waste time in result.doit() if the sum bounds are symbolic expand = False except AttributeError: pass return result.doit() if expand else result
def _marginalise(self, expr, rv, evaluate): if isinstance(rv.pspace.distribution, SingleFiniteDistribution): rv_dens = rv.pspace.distribution.pmf(rv) else: rv_dens = rv.pspace.distribution.pdf(rv) rv_dom = rv.pspace.domain.set if rv.pspace.is_Discrete or rv.pspace.is_Finite: expr = Sum(expr * rv_dens, (rv, rv_dom._inf, rv_dom._sup)) else: expr = Integral(expr * rv_dens, (rv, rv_dom._inf, rv_dom._sup)) if evaluate: return expr.doit() return expr
def test_evalf_sum(): assert Sum(n, (n, 1, 2)).evalf() == 3. assert Sum(n, (n, 1, 2)).doit().evalf() == 3. # the next test should return instantly assert Sum(1 / n, (n, 1, 2)).evalf() == 1.5 # issue 8219 assert Sum(E / factorial(n), (n, 0, oo)).evalf() == (E * E).evalf() # issue 8254 assert Sum(2**n * n / factorial(n), (n, 0, oo)).evalf() == (2 * E * E).evalf() # issue 8411 s = Sum(1 / x**2, (x, 100, oo)) assert s.n() == s.doit().n()
def _entry(self, i, j, expand=True): coeff, matrices = self.as_coeff_matrices() if len(matrices) == 1: # situation like 2*X, matmul is just X return coeff * matrices[0][i, j] head, tail = matrices[0], matrices[1:] assert len(tail) != 0 X = head Y = MatMul(*tail) from sympy.core.symbol import Dummy from sympy.concrete.summations import Sum from sympy.matrices import ImmutableMatrix, MatrixBase k = Dummy('k', integer=True) if X.has(ImmutableMatrix) or Y.has(ImmutableMatrix): return coeff * Add(*[X[i, k] * Y[k, j] for k in range(X.cols)]) result = Sum(coeff * X[i, k] * Y[k, j], (k, 0, X.cols - 1)) return result.doit() if expand else result
def _entry(self, i, j, expand=True): coeff, matrices = self.as_coeff_matrices() if len(matrices) == 1: # situation like 2*X, matmul is just X return coeff * matrices[0][i, j] head, tail = matrices[0], matrices[1:] assert len(tail) != 0 X = head Y = MatMul(*tail) from sympy.core.symbol import Dummy from sympy.concrete.summations import Sum from sympy.matrices import ImmutableMatrix, MatrixBase k = Dummy('k', integer=True) if X.has(ImmutableMatrix) or Y.has(ImmutableMatrix): return coeff*Add(*[X[i, k]*Y[k, j] for k in range(X.cols)]) result = Sum(coeff*X[i, k]*Y[k, j], (k, 0, X.cols - 1)) return result.doit() if expand else result
def test_BernoulliProcess(): B = BernoulliProcess("B", p=0.6, success=1, failure=0) assert B.state_space == FiniteSet(0, 1) assert B.index_set == S.Naturals0 assert B.success == 1 assert B.failure == 0 X = BernoulliProcess("X", p=Rational(1, 3), success='H', failure='T') assert X.state_space == FiniteSet('H', 'T') H, T = symbols("H,T") assert E(X[1] + X[2] * X[3] ) == H**2 / 9 + 4 * H * T / 9 + H / 3 + 4 * T**2 / 9 + 2 * T / 3 t, x = symbols('t, x', positive=True, integer=True) assert isinstance(B[t], RandomIndexedSymbol) raises(ValueError, lambda: BernoulliProcess("X", p=1.1, success=1, failure=0)) raises(NotImplementedError, lambda: B(t)) raises(IndexError, lambda: B[-3]) assert B.joint_distribution(B[3], B[9]) == JointDistributionHandmade( Lambda( (B[3], B[9]), Piecewise((0.6, Eq(B[3], 1)), (0.4, Eq(B[3], 0)), (0, True)) * Piecewise((0.6, Eq(B[9], 1)), (0.4, Eq(B[9], 0)), (0, True)))) assert B.joint_distribution(2, B[4]) == JointDistributionHandmade( Lambda( (B[2], B[4]), Piecewise((0.6, Eq(B[2], 1)), (0.4, Eq(B[2], 0)), (0, True)) * Piecewise((0.6, Eq(B[4], 1)), (0.4, Eq(B[4], 0)), (0, True)))) # Test for the sum distribution of Bernoulli Process RVs Y = B[1] + B[2] + B[3] assert P(Eq(Y, 0)).round(2) == Float(0.06, 1) assert P(Eq(Y, 2)).round(2) == Float(0.43, 2) assert P(Eq(Y, 4)).round(2) == 0 assert P(Gt(Y, 1)).round(2) == Float(0.65, 2) # Test for independency of each Random Indexed variable assert P(Eq(B[1], 0) & Eq(B[2], 1) & Eq(B[3], 0) & Eq(B[4], 1)).round(2) == Float(0.06, 1) assert E(2 * B[1] + B[2]).round(2) == Float(1.80, 3) assert E(2 * B[1] + B[2] + 5).round(2) == Float(6.80, 3) assert E(B[2] * B[4] + B[10]).round(2) == Float(0.96, 2) assert E(B[2] > 0, Eq(B[1], 1) & Eq(B[2], 1)).round(2) == Float(0.60, 2) assert E(B[1]) == 0.6 assert P(B[1] > 0).round(2) == Float(0.60, 2) assert P(B[1] < 1).round(2) == Float(0.40, 2) assert P(B[1] > 0, B[2] <= 1).round(2) == Float(0.60, 2) assert P(B[12] * B[5] > 0).round(2) == Float(0.36, 2) assert P(B[12] * B[5] > 0, B[4] < 1).round(2) == Float(0.36, 2) assert P(Eq(B[2], 1), B[2] > 0) == 1 assert P(Eq(B[5], 3)) == 0 assert P(Eq(B[1], 1), B[1] < 0) == 0 assert P(B[2] > 0, Eq(B[2], 1)) == 1 assert P(B[2] < 0, Eq(B[2], 1)) == 0 assert P(B[2] > 0, B[2] == 7) == 0 assert P(B[5] > 0, B[5]) == BernoulliDistribution(0.6, 0, 1) raises(ValueError, lambda: P(3)) raises(ValueError, lambda: P(B[3] > 0, 3)) # test issue 19456 expr = Sum(B[t], (t, 0, 4)) expr2 = Sum(B[t], (t, 1, 3)) expr3 = Sum(B[t]**2, (t, 1, 3)) assert expr.doit() == B[0] + B[1] + B[2] + B[3] + B[4] assert expr2.doit() == Y assert expr3.doit() == B[1]**2 + B[2]**2 + B[3]**2 assert B[2 * t].free_symbols == {B[2 * t], t} assert B[4].free_symbols == {B[4]} assert B[x * t].free_symbols == {B[x * t], x, t} #test issue 20078 assert (2 * B[t] + 3 * B[t]).simplify() == 5 * B[t] assert (2 * B[t] - 3 * B[t]).simplify() == -B[t] assert (2 * (0.25 * B[t])).simplify() == 0.5 * B[t] assert (2 * B[t] * 0.25 * B[t]).simplify() == 0.5 * B[t]**2 assert (B[t]**2 + B[t]**3).simplify() == (B[t] + 1) * B[t]**2
def _eval_rewrite_as_Sum(self, expr, **kwargs): from sympy.concrete.summations import Sum i = uniquely_named_symbol('i', expr) s = Sum(self.arg[i, i], (i, 0, self.arg.rows - 1)) return s.doit()