def test_log_product_simplify_to_sum(): from sympy.abc import n, m i, j = symbols('i,j', positive=True, integer=True) x, y = symbols('x,y', positive=True) assert simplify(log(Product(x**i, (i, 1, n)))) == Sum(i * log(x), (i, 1, n)) assert simplify(log(Product(x**i*y**j, (i, 1, n), (j, 1, m)))) == \ Sum(i*log(x) + j*log(y), (i, 1, n), (j, 1, m))
def test_multigamma(): from sympy.concrete.products import Product p = Symbol('p') _k = Dummy('_k') assert multigamma(x, p).dummy_eq(pi**(p*(p - 1)/4)*\ Product(gamma(x + (1 - _k)/2), (_k, 1, p))) assert conjugate(multigamma(x, p)).dummy_eq(pi**((conjugate(p) - 1)*\ conjugate(p)/4)*Product(gamma(conjugate(x) + (1-conjugate(_k))/2), (_k, 1, p))) assert conjugate(multigamma(x, 1)) == gamma(conjugate(x)) p = Symbol('p', positive=True) assert conjugate(multigamma(x, p)).dummy_eq(pi**((p - 1)*p/4)*\ Product(gamma(conjugate(x) + (1-conjugate(_k))/2), (_k, 1, p))) assert multigamma(nan, 1) is nan assert multigamma(oo, 1).doit() is oo assert multigamma(1, 1) == 1 assert multigamma(2, 1) == 1 assert multigamma(3, 1) == 2 assert multigamma(102, 1) == factorial(101) assert multigamma(S.Half, 1) == sqrt(pi) assert multigamma(1, 2) == pi assert multigamma(2, 2) == pi/2 assert multigamma(1, 3) is zoo assert multigamma(2, 3) == pi**2/2 assert multigamma(3, 3) == 3*pi**2/2 assert multigamma(x, 1).diff(x) == gamma(x)*polygamma(0, x) assert multigamma(x, 2).diff(x) == sqrt(pi)*gamma(x)*gamma(x - S.Half)*\ polygamma(0, x) + sqrt(pi)*gamma(x)*gamma(x - S.Half)*polygamma(0, x - S.Half) assert multigamma(x - 1, 1).expand(func=True) == gamma(x)/(x - 1) assert multigamma(x + 2, 1).expand(func=True, mul=False) == x*(x + 1)*\ gamma(x) assert multigamma(x - 1, 2).expand(func=True) == sqrt(pi)*gamma(x)*\ gamma(x + S.Half)/(x**3 - 3*x**2 + x*Rational(11, 4) - Rational(3, 4)) assert multigamma(x - 1, 3).expand(func=True) == pi**Rational(3, 2)*gamma(x)**2*\ gamma(x + S.Half)/(x**5 - 6*x**4 + 55*x**3/4 - 15*x**2 + x*Rational(31, 4) - Rational(3, 2)) assert multigamma(n, 1).rewrite(factorial) == factorial(n - 1) assert multigamma(n, 2).rewrite(factorial) == sqrt(pi)*\ factorial(n - Rational(3, 2))*factorial(n - 1) assert multigamma(n, 3).rewrite(factorial) == pi**Rational(3, 2)*\ factorial(n - 2)*factorial(n - Rational(3, 2))*factorial(n - 1) assert multigamma(Rational(-1, 2), 3, evaluate=False).is_real == False assert multigamma(S.Half, 3, evaluate=False).is_real == False assert multigamma(0, 1, evaluate=False).is_real == False assert multigamma(1, 3, evaluate=False).is_real == False assert multigamma(-1.0, 3, evaluate=False).is_real == False assert multigamma(0.7, 3, evaluate=False).is_real == True assert multigamma(3, 3, evaluate=False).is_real == True
def test_change_index(): b, y, c, d, z = symbols('b, y, c, d, z', integer = True) assert Product(x, (x, a, b)).change_index(x, x + 1, y) == \ Product(y - 1, (y, a + 1, b + 1)) assert Product(x**2, (x, a, b)).change_index(x, x - 1) == \ Product((x + 1)**2, (x, a - 1, b - 1)) assert Product(x**2, (x, a, b)).change_index(x, -x, y) == \ Product((-y)**2, (y, -b, -a)) assert Product(x, (x, a, b)).change_index(x, -x - 1) == \ Product(-x - 1, (x, - b - 1, -a - 1)) assert Product(x*y, (x, a, b), (y, c, d)).change_index(x, x - 1, z) == \ Product((z + 1)*y, (z, a - 1, b - 1), (y, c, d))
def test_MatrixGamma(): M = MatrixGamma('M', 1, 2, [[1, 0], [0, 1]]) assert M.pspace.distribution.set == MatrixSet(2, 2, S.Reals) assert isinstance(density(M), MatrixGammaDistribution) X = MatrixSymbol('X', 2, 2) num = exp(Trace(Matrix([[-S(1) / 2, 0], [0, -S(1) / 2]]) * X)) assert density(M)(X).doit() == num / (4 * pi * sqrt(Determinant(X))) assert density(M)([[2, 1], [1, 2]]).doit() == sqrt(3) * exp(-2) / (12 * pi) X = MatrixSymbol('X', 1, 2) Y = MatrixSymbol('Y', 1, 2) assert density(M)([X, Y]).doit() == exp(-X[0, 0] / 2 - Y[0, 1] / 2) / ( 4 * pi * sqrt(X[0, 0] * Y[0, 1] - X[0, 1] * Y[0, 0])) # symbolic a, b = symbols('a b', positive=True) d = symbols('d', positive=True, integer=True) Y = MatrixSymbol('Y', d, d) Z = MatrixSymbol('Z', 2, 2) SM = MatrixSymbol('SM', d, d) M2 = MatrixGamma('M2', a, b, SM) M3 = MatrixGamma('M3', 2, 3, [[2, 1], [1, 2]]) k = Dummy('k') exprd = pi**(-d * (d - 1) / 4) * b**(-a * d) * exp( Trace((-1 / b) * SM**(-1) * Y)) * Determinant(SM)**(-a) * Determinant( Y)**(a - d / 2 - S(1) / 2) / Product(gamma(-k / 2 + a + S(1) / 2), (k, 1, d)) assert density(M2)(Y).dummy_eq(exprd) raises(NotImplementedError, lambda: density(M3 + M)(Z)) raises(ValueError, lambda: density(M)(1)) raises(ValueError, lambda: MatrixGamma('M', -1, 2, [[1, 0], [0, 1]])) raises(ValueError, lambda: MatrixGamma('M', -1, -2, [[1, 0], [0, 1]])) raises(ValueError, lambda: MatrixGamma('M', -1, 2, [[1, 0], [2, 1]])) raises(ValueError, lambda: MatrixGamma('M', -1, 2, [[1, 0], [0]]))
def test_MultivariateEwens(): n, theta, i = symbols('n theta i', positive=True) # tests for integer dimensions theta_f = symbols('t_f', negative=True) a = symbols('a_1:4', positive=True, integer=True) ed = MultivariateEwens('E', 3, theta) assert density(ed)(a[0], a[1], a[2]) == Piecewise( (6 * 2**(-a[1]) * 3**(-a[2]) * theta**a[0] * theta**a[1] * theta**a[2] / (theta * (theta + 1) * (theta + 2) * factorial(a[0]) * factorial(a[1]) * factorial(a[2])), Eq(a[0] + 2 * a[1] + 3 * a[2], 3)), (0, True)) assert marginal_distribution(ed, ed[1])(a[1]) == Piecewise( (6 * 2**(-a[1]) * theta**a[1] / ((theta + 1) * (theta + 2) * factorial(a[1])), Eq(2 * a[1] + 1, 3)), (0, True)) raises(ValueError, lambda: MultivariateEwens('e1', 5, theta_f)) assert ed.pspace.distribution.set == ProductSet(Range(0, 4, 1), Range(0, 2, 1), Range(0, 2, 1)) # tests for symbolic dimensions eds = MultivariateEwens('E', n, theta) a = IndexedBase('a') j, k = symbols('j, k') den = Piecewise((factorial(n) * Product(theta**a[j] * (j + 1)**(-a[j]) / factorial(a[j]), (j, 0, n - 1)) / RisingFactorial(theta, n), Eq(n, Sum((k + 1) * a[k], (k, 0, n - 1)))), (0, True)) assert density(eds)(a).dummy_eq(den)
def _compute_joint_eigen_distribution(self, beta): """ Helper function for computing the joint probability distribution of eigen values of the random matrix. """ n = self.dimension Zbn = self._compute_normalization_constant(beta, n) l = IndexedBase('l') i = Dummy('i', integer=True, positive=True) j = Dummy('j', integer=True, positive=True) k = Dummy('k', integer=True, positive=True) term1 = exp((-S(n) / 2) * Sum(l[k]**2, (k, 1, n)).doit()) sub_term = Lambda(i, Product(Abs(l[j] - l[i])**beta, (j, i + 1, n))) term2 = Product(sub_term(i).doit(), (i, 1, n - 1)).doit() syms = ArrayComprehension(l[k], (k, 1, n)).doit() return Lambda(tuple(syms), (term1 * term2) / Zbn)
def _compute_joint_eigen_distribution(self, beta): """ Helper function to compute the joint distribution of phases of the complex eigen values of matrices belonging to any circular ensembles. """ n = self.dimension Zbn = ((2 * pi)** n) * (gamma(beta * n / 2 + 1) / S(gamma(beta / 2 + 1))**n) t = IndexedBase('t') i, j, k = (Dummy('i', integer=True), Dummy('j', integer=True), Dummy('k', integer=True)) syms = ArrayComprehension(t[i], (i, 1, n)).doit() f = Product( Product(Abs(exp(I * t[k]) - exp(I * t[j]))**beta, (j, k + 1, n)).doit(), (k, 1, n - 1)).doit() return Lambda(tuple(syms), f / Zbn)
def test_factorial_rewrite(): n = Symbol('n', integer=True) k = Symbol('k', integer=True, nonnegative=True) assert factorial(n).rewrite(gamma) == gamma(n + 1) _i = Dummy('i') assert factorial(k).rewrite(Product).dummy_eq(Product(_i, (_i, 1, k))) assert factorial(n).rewrite(Product) == factorial(n)
def test_guess(): i0, i1 = symbols('i0 i1') assert guess([1, 2, 6, 24, 120], evaluate=False) == [Product(i1 + 1, (i1, 1, i0 - 1))] assert guess([1, 2, 6, 24, 120]) == [RisingFactorial(2, i0 - 1)] assert guess([1, 2, 7, 42, 429, 7436, 218348, 10850216], niter=4) == [ 2**(i0 - 1) * (Rational(27, 16))**(i0**2 / 2 - 3 * i0 / 2 + 1) * Product( RisingFactorial(Rational(5, 3), i1 - 1) * RisingFactorial(Rational(7, 3), i1 - 1) / (RisingFactorial(Rational(3, 2), i1 - 1) * RisingFactorial(Rational(5, 2), i1 - 1)), (i1, 1, i0 - 1)) ] assert guess([1, 0, 2]) == [] x, y = symbols('x y') assert guess([1, 2, 6, 24, 120], variables=[x, y]) == [RisingFactorial(2, x - 1)]
def eval(cls, x, p): from sympy.concrete.products import Product x, p = map(sympify, (x, p)) if p.is_positive is False or p.is_integer is False: raise ValueError('Order parameter p must be positive integer.') k = Dummy("k") return (pi**(p * (p - 1) / 4) * Product(gamma(x + (1 - k) / 2), (k, 1, p))).doit()
def test_the_product(l, n, m): # Productmand s = i**3 # First product a = l b = n - 1 S1 = Product(s, (i, a, b)).doit() # Second product a = l b = m - 1 S2 = Product(s, (i, a, b)).doit() # Third product a = m b = n - 1 S3 = Product(s, (i, a, b)).doit() # Test if S1 = S2 * S3 as required assert combsimp(S1 / (S2 * S3)) == 1
def test_CircularOrthogonalEnsemble(): CO = COE('U', 3) j, k = (Dummy('j', integer=True, positive=True), Dummy('k', integer=True, positive=True)) t = IndexedBase('t') assert joint_eigen_distribution(CO).dummy_eq( Lambda((t[1], t[2], t[3]), Product(Abs(exp(I * t[j]) - exp(I * t[k])), (j, k + 1, 3), (k, 1, 2)) / (48 * pi**2)))
def _eval_expand_power_exp(self, **hints): from sympy.concrete.products import Product from sympy.concrete.summations import Sum arg = self.args[0] if arg.is_Add and arg.is_commutative: return Mul.fromiter(self.func(x) for x in arg.args) elif isinstance(arg, Sum) and arg.is_commutative: return Product(self.func(arg.function), *arg.limits) return self.func(arg)
def set(self): if not isinstance(self.n, Integer): i = Symbol('i', integer=True, positive=True) return Product(Intersection(S.Naturals0, Interval(0, self.n//i)), (i, 1, self.n)) prod_set = Range(0, self.n + 1) for i in range(2, self.n + 1): prod_set *= Range(0, self.n//i + 1) return prod_set.flatten()
def test_issue_20848(): _i = Dummy('i') t, y, z = symbols('t y z') assert diff(Product(x, (y, 1, z)), x).as_dummy() == Sum(Product(x, (y, 1, _i - 1))*Product(x, (y, _i + 1, z)), (_i, 1, z)).as_dummy() assert diff(Product(x, (y, 1, z)), x).doit() == x**z*z/x assert diff(Product(x, (y, x, z)), x) == Derivative(Product(x, (y, x, z)), x) assert diff(Product(t, (x, 1, z)), x) == S(0) assert Product(sin(n*x), (n, -1, 1)).diff(x).doit() == S(0)
def test_CircularSymplecticEnsemble(): CS = CSE('U', 3) j, k = (Dummy('j', integer=True, positive=True), Dummy('k', integer=True, positive=True)) t = IndexedBase('t') assert joint_eigen_distribution(CS).dummy_eq( Lambda((t[1], t[2], t[3]), Product( Abs(exp(I * t[j]) - exp(I * t[k]))**4, (j, k + 1, 3), (k, 1, 2)) / (720 * pi**3)))
def apply(a, b): n = a.shape[0] i = Symbol.i(integer=True) j = Symbol.j(integer=True) return Equality( Det(LAMBDA[j:n, i:n](a[Min(i, j)] * b[Max(i, j)])), a[0] * b[n - 1] * Product(a[i] * b[i - 1] - a[i - 1] * b[i], (i, 1, n - 1)))
def test_multiple_products(): assert product(x, (n, 1, k), (k, 1, m)) == x**(m**2/2 + m/2) assert product(f(n), ( n, 1, m), (m, 1, k)) == Product(f(n), (n, 1, m), (m, 1, k)).doit() assert Product(f(n), (m, 1, k), (n, 1, k)).doit() == \ Product(Product(f(n), (m, 1, k)), (n, 1, k)).doit() == \ product(f(n), (m, 1, k), (n, 1, k)) == \ product(product(f(n), (m, 1, k)), (n, 1, k)) == \ Product(f(n)**k, (n, 1, k)) assert Product( x, (x, 1, k), (k, 1, n)).doit() == Product(factorial(k), (k, 1, n)) assert Product(x**k, (n, 1, k), (k, 1, m)).variables == [n, k]
def test_the_product(m, n): # g g = i**3 + 2*i**2 - 3*i # f = Delta g f = simplify(g.subs(i, i+1) / g) # The product a = m b = n - 1 P = Product(f, (i, a, b)).doit() # Test if Product_{m <= i < n} f(i) = g(n) / g(m) assert combsimp(P / (g.subs(i, n) / g.subs(i, m))) == 1
def test_log_product(): from sympy.abc import n, m i, j = symbols('i,j', positive=True, integer=True) x, y = symbols('x,y', positive=True) z = symbols('z', real=True) w = symbols('w') expr = log(Product(x**i, (i, 1, n))) assert simplify(expr) == expr assert expr.expand() == Sum(i * log(x), (i, 1, n)) expr = log(Product(x**i * y**j, (i, 1, n), (j, 1, m))) assert simplify(expr) == expr assert expr.expand() == Sum(i * log(x) + j * log(y), (i, 1, n), (j, 1, m)) expr = log(Product(-2, (n, 0, 4))) assert simplify(expr) == expr assert expr.expand() == expr assert expr.expand(force=True) == Sum(log(-2), (n, 0, 4)) expr = log(Product(exp(z * i), (i, 0, n))) assert expr.expand() == Sum(z * i, (i, 0, n)) expr = log(Product(exp(w * i), (i, 0, n))) assert expr.expand() == expr assert expr.expand(force=True) == Sum(w * i, (i, 0, n)) expr = log(Product(i**2 * abs(j), (i, 1, n), (j, 1, m))) assert expr.expand() == Sum(2 * log(i) + log(j), (i, 1, n), (j, 1, m))
def test_simple_products(): assert product(2, (k, a, n)) == 2**(n - a + 1) assert product(k, (k, 1, n)) == factorial(n) assert product(k**3, (k, 1, n)) == factorial(n)**3 assert product(k + 1, (k, 0, n - 1)) == factorial(n) assert product(k + 1, (k, a, n - 1)) == rf(1 + a, n - a) assert product(cos(k), (k, 0, 5)) == cos(1)*cos(2)*cos(3)*cos(4)*cos(5) assert product(cos(k), (k, 3, 5)) == cos(3)*cos(4)*cos(5) assert product(cos(k), (k, 1, Rational(5, 2))) != cos(1)*cos(2) assert isinstance(product(k**k, (k, 1, n)), Product) assert Product(x**k, (k, 1, n)).variables == [k] raises(ValueError, lambda: Product(n)) raises(ValueError, lambda: Product(n, k)) raises(ValueError, lambda: Product(n, k, 1)) raises(ValueError, lambda: Product(n, k, 1, 10)) raises(ValueError, lambda: Product(n, (k, 1))) assert product(1, (n, 1, oo)) == 1 # issue 8301 assert product(2, (n, 1, oo)) is oo assert product(-1, (n, 1, oo)).func is Product
def test__eval_product(): from sympy.abc import i, n # issue 4809 a = Function('a') assert product(2*a(i), (i, 1, n)) == 2**n * Product(a(i), (i, 1, n)) # issue 4810 assert product(2**i, (i, 1, n)) == 2**(n*(n + 1)/2) k, m = symbols('k m', integer=True) assert product(2**i, (i, k, m)) == 2**(-k**2/2 + k/2 + m**2/2 + m/2) n = Symbol('n', negative=True, integer=True) p = Symbol('p', positive=True, integer=True) assert product(2**i, (i, n, p)) == 2**(-n**2/2 + n/2 + p**2/2 + p/2) assert product(2**i, (i, p, n)) == 2**(n**2/2 + n/2 - p**2/2 + p/2)
def test_GaussianUnitaryEnsemble(): H = RandomMatrixSymbol('H', 3, 3) G = GUE('U', 3) assert density(G)(H) == sqrt(2) * exp( -3 * Trace(H**2) / 2) / (4 * pi**Rational(9, 2)) i, j = (Dummy('i', integer=True, positive=True), Dummy('j', integer=True, positive=True)) l = IndexedBase('l') assert joint_eigen_distribution(G).dummy_eq( Lambda((l[1], l[2], l[3]), 27 * sqrt(6) * exp(-3 * (l[1]**2) / 2 - 3 * (l[2]**2) / 2 - 3 * (l[3]**2) / 2) * Product(Abs(l[i] - l[j])**2, (j, i + 1, 3), (i, 1, 2)) / (16 * pi**Rational(3, 2)))) s = Dummy('s') assert level_spacing_distribution(G).dummy_eq( Lambda(s, 32 * s**2 * exp(-4 * s**2 / pi) / pi**2))
def pdf(self, *syms): n, theta = self.n, self.theta condi = isinstance(self.n, Integer) if not (isinstance(syms[0], IndexedBase) or condi): raise ValueError("Please use IndexedBase object for syms as " "the dimension is symbolic") term_1 = factorial(n)/rf(theta, n) if condi: term_2 = Mul.fromiter(theta**syms[j]/((j+1)**syms[j]*factorial(syms[j])) for j in range(n)) cond = Eq(sum([(k + 1)*syms[k] for k in range(n)]), n) return Piecewise((term_1 * term_2, cond), (0, True)) syms = syms[0] j, k = symbols('j, k', positive=True, integer=True) term_2 = Product(theta**syms[j]/((j+1)**syms[j]*factorial(syms[j])), (j, 0, n - 1)) cond = Eq(Sum((k + 1)*syms[k], (k, 0, n - 1)), n) return Piecewise((term_1 * term_2, cond), (0, True))
def test_GaussianSymplecticEnsemble(): H = RandomMatrixSymbol('H', 3, 3) _H = MatrixSymbol('_H', 3, 3) G = GSE('O', 3) assert density(G)(H) == exp(-3 * Trace(H**2)) / Integral( exp(-3 * Trace(_H**2)), _H) i, j = (Dummy('i', integer=True, positive=True), Dummy('j', integer=True, positive=True)) l = IndexedBase('l') assert joint_eigen_distribution(G).dummy_eq( Lambda((l[1], l[2], l[3]), 162 * sqrt(3) * exp(-3 * l[1]**2 / 2 - 3 * l[2]**2 / 2 - 3 * l[3]**2 / 2) * Product(Abs(l[i] - l[j])**4, (j, i + 1, 3), (i, 1, 2)) / (5 * pi**Rational(3, 2)))) s = Dummy('s') assert level_spacing_distribution(G).dummy_eq( Lambda(s, S(262144) * s**4 * exp(-64 * s**2 / (9 * pi)) / (729 * pi**3)))
def test_GaussianOrthogonalEnsemble(): H = RandomMatrixSymbol('H', 3, 3) _H = MatrixSymbol('_H', 3, 3) G = GOE('O', 3) assert density(G)(H) == exp(-3 * Trace(H**2) / 4) / Integral( exp(-3 * Trace(_H**2) / 4), _H) i, j = (Dummy('i', integer=True, positive=True), Dummy('j', integer=True, positive=True)) l = IndexedBase('l') assert joint_eigen_distribution(G).dummy_eq( Lambda((l[1], l[2], l[3]), 9 * sqrt(2) * exp(-3 * l[1]**2 / 2 - 3 * l[2]**2 / 2 - 3 * l[3]**2 / 2) * Product(Abs(l[i] - l[j]), (j, i + 1, 3), (i, 1, 2)) / (32 * pi))) s = Dummy('s') assert level_spacing_distribution(G).dummy_eq( Lambda(s, s * pi * exp(-s**2 * pi / 4) / 2))
def _compute_normalization_constant(self, beta, n): """ Helper function for computing normalization constant for joint probability density of eigen values of Gaussian ensembles. References ========== .. [1] https://en.wikipedia.org/wiki/Selberg_integral#Mehta's_integral """ n = S(n) prod_term = lambda j: gamma(1 + beta * S(j) / 2) / gamma(S.One + beta / S(2)) j = Dummy('j', integer=True, positive=True) term1 = Product(prod_term(j), (j, 1, n)).doit() term2 = (2 / (beta * n))**(beta * n * (n - 1) / 4 + n / 2) term3 = (2 * pi)**(n / 2) return term1 * term2 * term3
def test_Wishart(): W = Wishart('W', 5, [[1, 0], [0, 1]]) assert W.pspace.distribution.set == MatrixSet(2, 2, S.Reals) X = MatrixSymbol('X', 2, 2) term1 = exp(Trace(Matrix([[-S(1) / 2, 0], [0, -S(1) / 2]]) * X)) assert density(W)(X).doit() == term1 * Determinant(X) / (24 * pi) assert density(W)([[2, 1], [1, 2]]).doit() == exp(-2) / (8 * pi) n = symbols('n', positive=True) d = symbols('d', positive=True, integer=True) Y = MatrixSymbol('Y', d, d) SM = MatrixSymbol('SM', d, d) W = Wishart('W', n, SM) k = Dummy('k') exprd = 2**(-d * n / 2) * pi**(-d * (d - 1) / 4) * exp( Trace(-(S(1) / 2) * SM**(-1) * Y)) * Determinant(SM)**( -n / 2) * Determinant(Y)**(-d / 2 + n / 2 - S(1) / 2) / Product( gamma(-k / 2 + n / 2 + S(1) / 2), (k, 1, d)) assert density(W)(Y).dummy_eq(exprd) raises(ValueError, lambda: density(W)(1)) raises(ValueError, lambda: Wishart('W', -1, [[1, 0], [0, 1]])) raises(ValueError, lambda: Wishart('W', -1, [[1, 0], [2, 1]])) raises(ValueError, lambda: Wishart('W', 2, [[1, 0], [0]]))
def test_Product(): assert precedence(Product(x, (x, y, y + 1))) == PRECEDENCE["Atom"]
def test_concrete(): x = Symbol("x") for c in (Product, Product(x, (x, 2, 4)), Sum, Sum(x, (x, 2, 4))): check(c)