def test_wallis_product(): # Wallis product, given in two different forms to ensure that Product # can factor simple rational expressions A = Product(4*n**2 / (4*n**2 - 1), (n, 1, b)) B = Product((2*n)*(2*n)/(2*n - 1)/(2*n + 1), (n, 1, b)) R = pi*gamma(b + 1)**2/(2*gamma(b + S(1)/2)*gamma(b + S(3)/2)) assert simplify(A.doit()) == R assert simplify(B.doit()) == R
def test_wallis_product(): # Wallis product, given in two different forms to ensure that Product # can factor simple rational expressions A = Product(4*n**2 / (4*n**2 - 1), (n, 1, b)) B = Product((2*n)*(2*n)/(2*n - 1)/(2*n + 1), (n, 1, b)) half = Rational(1, 2) R = pi/2 * factorial(b)**2 / factorial(b - half) / factorial(b + half) assert simplify(A.doit()) == R assert simplify(B.doit()) == R
def test_simple_products(): assert Product(S.NaN, (x, 1, 3)) is S.NaN assert product(S.NaN, (x, 1, 3)) is S.NaN assert Product(x, (n, a, a)).doit() == x assert Product(x, (x, a, a)).doit() == a assert Product(x, (y, 1, a)).doit() == x**a lo, hi = 1, 2 s1 = Product(n, (n, lo, hi)) s2 = Product(n, (n, hi, lo)) assert s1 != s2 assert s1.doit() == s2.doit() == 2 lo, hi = x, x + 1 s1 = Product(n, (n, lo, hi)) s2 = Product(n, (n, hi, lo)) assert s1 != s2 assert s1.doit() == s2.doit() == x*(x + 1) assert Product(Integral(2*x, (x, 1, y)) + 2*x, (x, 1, 2)).doit() == \ (y**2 + 1)*(y**2 + 3) assert product(2, (n, a, b)) == 2**(b - a + 1) assert product(n, (n, 1, b)) == factorial(b) assert product(n**3, (n, 1, b)) == factorial(b)**3 assert product(3**(2 + n), (n, a, b)) \ == 3**(2*(1 - a + b) + b/2 + (b**2)/2 + a/2 - (a**2)/2) assert product(cos(n), (n, 3, 5)) == cos(3)*cos(4)*cos(5) assert product(cos(n), (n, x, x + 2)) == cos(x)*cos(x + 1)*cos(x + 2) assert isinstance(product(cos(n), (n, x, x + S.Half)), Product) # If Product managed to evaluate this one, it most likely got it wrong! assert isinstance(Product(n**n, (n, 1, b)), Product)
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 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_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)))
def test_indexed_idx_sum(): i = symbols('i', cls=Idx) r = Indexed('r', i) assert Sum(r, (i, 0, 3)).doit() == sum([r.xreplace({i: j}) for j in range(4)]) assert Product(r, (i, 0, 3)).doit() == prod([r.xreplace({i: j}) for j in range(4)]) j = symbols('j', integer=True) assert Sum(r, (i, j, j+2)).doit() == sum([r.xreplace({i: j+k}) for k in range(3)]) assert Product(r, (i, j, j+2)).doit() == prod([r.xreplace({i: j+k}) for k in range(3)]) k = Idx('k', range=(1, 3)) A = IndexedBase('A') assert Sum(A[k], k).doit() == sum([A[Idx(j, (1, 3))] for j in range(1, 4)]) assert Product(A[k], k).doit() == prod([A[Idx(j, (1, 3))] for j in range(1, 4)]) raises(ValueError, lambda: Sum(A[k], (k, 1, 4))) raises(ValueError, lambda: Sum(A[k], (k, 0, 3))) raises(ValueError, lambda: Sum(A[k], (k, 2, oo))) raises(ValueError, lambda: Product(A[k], (k, 1, 4))) raises(ValueError, lambda: Product(A[k], (k, 0, 3))) raises(ValueError, lambda: Product(A[k], (k, 2, oo)))
def test_rewrite_Sum(): assert Product(1 - S.Half**2/k**2, (k, 1, oo)).rewrite(Sum) == \ exp(Sum(log(1 - 1/(4*k**2)), (k, 1, oo)))
def test_issue_9983(): n = Symbol('n', integer=True, positive=True) p = Product(1 + 1/n**Rational(2, 3), (n, 1, oo)) assert p.is_convergent() is S.false assert product(1 + 1/n**Rational(2, 3), (n, 1, oo)) == p.doit()
def test_issue_13546(): n = Symbol('n') k = Symbol('k') p = Product(n + 1 / 2**k, (k, 0, n-1)).doit() assert p.subs(n, 2).doit() == Rational(15, 2)
def test_issue_9983(): n = Symbol('n', integer=True, positive=True) p = Product(1 + 1/n**(S(2)/3), (n, 1, oo)) assert p.is_convergent() is S.false assert product(1 + 1/n**(S(2)/3), (n, 1, oo)) == p.doit()
def test_reverse_order(): x, y, a, b, c, d= symbols('x, y, a, b, c, d', integer = True) assert Product(x, (x, 0, 3)).reverse_order(0) == Product(1/x, (x, 4, -1)) assert Product(x*y, (x, 1, 5), (y, 0, 6)).reverse_order(0, 1) == \ Product(x*y, (x, 6, 0), (y, 7, -1)) assert Product(x, (x, 1, 2)).reverse_order(0) == Product(1/x, (x, 3, 0)) assert Product(x, (x, 1, 3)).reverse_order(0) == Product(1/x, (x, 4, 0)) assert Product(x, (x, 1, a)).reverse_order(0) == Product(1/x, (x, a + 1, 0)) assert Product(x, (x, a, 5)).reverse_order(0) == Product(1/x, (x, 6, a - 1)) assert Product(x, (x, a + 1, a + 5)).reverse_order(0) == \ Product(1/x, (x, a + 6, a)) assert Product(x, (x, a + 1, a + 2)).reverse_order(0) == \ Product(1/x, (x, a + 3, a)) assert Product(x, (x, a + 1, a + 1)).reverse_order(0) == \ Product(1/x, (x, a + 2, a)) assert Product(x, (x, a, b)).reverse_order(0) == Product(1/x, (x, b + 1, a - 1)) assert Product(x, (x, a, b)).reverse_order(x) == Product(1/x, (x, b + 1, a - 1)) assert Product(x*y, (x, a, b), (y, 2, 5)).reverse_order(x, 1) == \ Product(x*y, (x, b + 1, a - 1), (y, 6, 1)) assert Product(x*y, (x, a, b), (y, 2, 5)).reverse_order(y, x) == \ Product(x*y, (x, b + 1, a - 1), (y, 6, 1))
def test_evalf_product(): assert Product(n, (n, 1, 10)).evalf() == 3628800. assert Product(1 - S.Half**2 / n**2, (n, 1, oo)).evalf(5) == 0.63662 assert Product(n, (n, -1, 3)).evalf() == 0
def test_issue_13546(): n = Symbol('n') k = Symbol('k') p = Product(n + 1 / 2**k, (k, 0, n-1)).doit() assert p.subs(n, 2).doit() == S(15)/2
def test_multigamma(): from sympy 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_Product_doit(): assert Product(n * Integral(a**2), (n, 1, 3)).doit() == 2 * a**9 / 9 assert Product(n*Integral(a**2), (n, 1, 3)).doit(deep=False) == \ 6*Integral(a**2)**3 assert product(n * Integral(a**2), (n, 1, 3)) == 6 * Integral(a**2)**3
("\\sqrt[y]{\\sin x}", root(sin(x), y)), ("\\sqrt[\\theta]{\\sin x}", root(sin(x), theta)), ("x < y", StrictLessThan(x, y)), ("x \\leq y", LessThan(x, y)), ("x > y", StrictGreaterThan(x, y)), ("x \\geq y", GreaterThan(x, y)), ("\\mathit{x}", Symbol('x')), ("\\mathit{test}", Symbol('test')), ("\\mathit{TEST}", Symbol('TEST')), ("\\mathit{HELLO world}", Symbol('HELLO world')), ("\\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))), ("\\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum^3_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))), ("\\sum_{n = 0}^{\\infty} \\frac{1}{n!}", Sum(_Pow(_factorial(n), -1), (n, 0, oo))), ("\\prod_{a = b}^{c} x", Product(x, (a, b, c))), ("\\prod_{a = b}^c x", Product(x, (a, b, c))), ("\\prod^{c}_{a = b} x", Product(x, (a, b, c))), ("\\prod^c_{a = b} x", Product(x, (a, b, c))), ("\\ln x", _log(x, E)), ("\\ln xy", _log(x * y, E)), ("\\log x", _log(x, 10)), ("\\log xy", _log(x * y, 10)), ("\\log_{2} x", _log(x, 2)), ("\\log_{a} x", _log(x, a)), ("\\log_{11} x", _log(x, 11)), ("\\log_{a^2} x", _log(x, _Pow(a, 2))), ("[x]", x), ("[a + b]", _Add(a, b)), ("\\frac{d}{dx} [ \\tan x ]", Derivative(tan(x), x))] def test_parseable(): from sympy.parsing.latex import parse_latex for latex_str, sympy_expr in GOOD_PAIRS:
def test_conjugate_transpose(): p = Product(x**k, (k, 1, 3)) assert p.adjoint().doit() == p.doit().adjoint() assert p.conjugate().doit() == p.doit().conjugate() assert p.transpose().doit() == p.doit().transpose() A, B = symbols("A B", commutative=False) p = Product(A*B**k, (k, 1, 3)) assert p.adjoint().doit() == p.doit().adjoint() assert p.conjugate().doit() == p.doit().conjugate() assert p.transpose().doit() == p.doit().transpose()
def test_exp_summation(): w = symbols("w") m, n, i, j = symbols("m n i j") expr = exp(Sum(w * i, (i, 0, n), (j, 0, m))) assert expr.expand() == Product(exp(w * i), (i, 0, n), (j, 0, m))
def _eval_rewrite_as_Product(self, n): from sympy import Product if not (n.is_integer and n.is_nonnegative): return self k = Dummy('k', integer=True, positive=True) return Product((n + k) / k, (k, 2, n))
def test_simplify(): y, t, b, c = symbols('y, t, b, c', integer=True) assert simplify(Product(x*y, (x, n, m), (y, a, k)) * \ Product(y, (x, n, m), (y, a, k))) == \ Product(x*y**2, (x, n, m), (y, a, k)) assert simplify(3 * y* Product(x, (x, n, m)) * Product(x, (x, m + 1, a))) \ == 3 * y * Product(x, (x, n, a)) assert simplify(Product(x, (x, k + 1, a)) * Product(x, (x, n, k))) == \ Product(x, (x, n, a)) assert simplify(Product(x, (x, k + 1, a)) * Product(x + 1, (x, n, k))) == \ Product(x, (x, k + 1, a)) * Product(x + 1, (x, n, k)) assert simplify(Product(x, (t, a, b)) * Product(y, (t, a, b)) * \ Product(x, (t, b+1, c))) == Product(x*y, (t, a, b)) * \ Product(x, (t, b+1, c)) assert simplify(Product(x, (t, a, b)) * Product(x, (t, b+1, c)) * \ Product(y, (t, a, b))) == Product(x*y, (t, a, b)) * \ Product(x, (t, b+1, c))
def test_KroneckerDelta_Product(): y = Symbol('y') assert Product(x*KroneckerDelta(x, y), (x, 0, 1)).doit() == 0
def IIo(n, k): i = sympy.abc.i if k < 1: return Product(1, (i,0,0)) return Product(n-2*i-1, (i,0,k-1))
class TestAllGood(object): # These latex strings should parse to the corresponding SymPy expression GOOD_PAIRS = [ ("0", Rational(0)), ("1", Rational(1)), ("-3.14", Rational(-314, 100)), ("5-3", _Add(5, _Mul(-1, 3))), ("(-7.13)(1.5)", _Mul(Rational('-7.13'), Rational('1.5'))), ("\\left(-7.13\\right)\\left(1.5\\right)", _Mul(Rational('-7.13'), Rational('1.5'))), ("x", x), ("2x", 2 * x), ("x^2", x**2), ("x^{3 + 1}", x**_Add(3, 1)), ("x^{\\left\\{3 + 1\\right\\}}", x**_Add(3, 1)), ("-3y + 2x", _Add(_Mul(2, x), Mul(-1, 3, y, evaluate=False))), ("-c", -c), ("a \\cdot b", a * b), ("a / b", a / b), ("a \\div b", a / b), ("a + b", a + b), ("a + b - a", Add(a, b, _Mul(-1, a), evaluate=False)), ("a^2 + b^2 = c^2", Eq(a**2 + b**2, c**2)), ("a^2 + b^2 != 2c^2", Ne(a**2 + b**2, 2 * c**2)), ("a\\mod b", Mod(a, b)), ("\\sin \\theta", sin(theta)), ("\\sin(\\theta)", sin(theta)), ("\\sin\\left(\\theta\\right)", sin(theta)), ("\\sin^{-1} a", asin(a)), ("\\sin a \\cos b", _Mul(sin(a), cos(b))), ("\\sin \\cos \\theta", sin(cos(theta))), ("\\sin(\\cos \\theta)", sin(cos(theta))), ("\\arcsin(a)", asin(a)), ("\\arccos(a)", acos(a)), ("\\arctan(a)", atan(a)), ("\\sinh(a)", sinh(a)), ("\\cosh(a)", cosh(a)), ("\\tanh(a)", tanh(a)), ("\\sinh^{-1}(a)", asinh(a)), ("\\cosh^{-1}(a)", acosh(a)), ("\\tanh^{-1}(a)", atanh(a)), ("\\arcsinh(a)", asinh(a)), ("\\arccosh(a)", acosh(a)), ("\\arctanh(a)", atanh(a)), ("\\arsinh(a)", asinh(a)), ("\\arcosh(a)", acosh(a)), ("\\artanh(a)", atanh(a)), ("\\operatorname{arcsinh}(a)", asinh(a)), ("\\operatorname{arccosh}(a)", acosh(a)), ("\\operatorname{arctanh}(a)", atanh(a)), ("\\operatorname{arsinh}(a)", asinh(a)), ("\\operatorname{arcosh}(a)", acosh(a)), ("\\operatorname{artanh}(a)", atanh(a)), ("\\operatorname{gcd}(a, b)", UnevaluatedExpr(gcd(a, b))), ("\\operatorname{lcm}(a, b)", UnevaluatedExpr(lcm(a, b))), ("\\operatorname{gcd}(a,b)", UnevaluatedExpr(gcd(a, b))), ("\\operatorname{lcm}(a,b)", UnevaluatedExpr(lcm(a, b))), ("\\operatorname{floor}(a)", floor(a)), ("\\operatorname{ceil}(b)", ceiling(b)), ("\\cos^2(x)", cos(x)**2), ("\\cos(x)^2", cos(x)**2), ("\\gcd(a, b)", UnevaluatedExpr(gcd(a, b))), ("\\lcm(a, b)", UnevaluatedExpr(lcm(a, b))), ("\\gcd(a,b)", UnevaluatedExpr(gcd(a, b))), ("\\lcm(a,b)", UnevaluatedExpr(lcm(a, b))), ("\\floor(a)", floor(a)), ("\\ceil(b)", ceiling(b)), ("\\max(a, b)", Max(a, b)), ("\\min(a, b)", Min(a, b)), ("\\frac{a}{b}", a / b), ("\\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))), ("\\frac{7}{3}", Rational(7, 3)), ("(\\csc x)(\\sec y)", csc(x) * sec(y)), ("\\lim_{x \\to 3} a", Limit(a, x, 3)), ("\\lim_{x \\rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\to 3^{+}} a", Limit(a, x, 3, dir='+')), ("\\lim_{x \\to 3^{-}} a", Limit(a, x, 3, dir='-')), ("\\infty", oo), ("\\infty\\%", oo), ("\\$\\infty", oo), ("-\\infty", -oo), ("-\\infty\\%", -oo), ("-\\$\\infty", -oo), ("\\lim_{x \\to \\infty} \\frac{1}{x}", Limit(_Mul(1, _Pow(x, -1)), x, oo)), ("\\frac{d}{dx} x", Derivative(x, x)), ("\\frac{d}{dt} x", Derivative(x, t)), # ("f(x)", f(x)), # ("f(x, y)", f(x, y)), # ("f(x, y, z)", f(x, y, z)), # ("\\frac{d f(x)}{dx}", Derivative(f(x), x)), # ("\\frac{d\\theta(x)}{dx}", Derivative(theta(x), x)), ("|x|", _Abs(x)), ("\\left|x\\right|", _Abs(x)), ("||x||", _Abs(_Abs(x))), ("|x||y|", _Abs(x) * _Abs(y)), ("||x||y||", _Abs(_Abs(x) * _Abs(y))), ("\\lfloor x\\rfloor", floor(x)), ("\\lceil y\\rceil", ceiling(y)), ("\\pi^{|xy|}", pi**_Abs(x * y)), ("\\frac{\\pi}{3}", _Mul(pi, _Pow(3, -1))), ("\\sin{\\frac{\\pi}{2}}", sin(_Mul(pi, _Pow(2, -1)), evaluate=False)), ("a+bI", a + I * b), ("e^{I\\pi}", Integer(-1)), ("\\int x dx", Integral(x, x)), ("\\int x d\\theta", Integral(x, theta)), ("\\int (x^2 - y)dx", Integral(x**2 - y, x)), ("\\int x + a dx", Integral(_Add(x, a), x)), ("\\int da", Integral(1, a)), ("\\int_0^7 dx", Integral(1, (x, 0, 7))), ("\\int_a^b x dx", Integral(x, (x, a, b))), ("\\int^b_a x dx", Integral(x, (x, a, b))), ("\\int_{a}^b x dx", Integral(x, (x, a, b))), ("\\int^{b}_a x dx", Integral(x, (x, a, b))), ("\\int_{a}^{b} x dx", Integral(x, (x, a, b))), ("\\int_{ }^{}x dx", Integral(x, x)), ("\\int^{ }_{ }x dx", Integral(x, x)), ("\\int^{b}_{a} x dx", Integral(x, (x, a, b))), # ("\\int_{f(a)}^{f(b)} f(z) dz", Integral(f(z), (z, f(a), f(b)))), ("\\int (x+a)", Integral(_Add(x, a), x)), ("\\int a + b + c dx", Integral(Add(a, b, c, evaluate=False), x)), ("\\int \\frac{dz}{z}", Integral(Pow(z, -1), z)), ("\\int \\frac{3 dz}{z}", Integral(3 * Pow(z, -1), z)), ("\\int \\frac{1}{x} dx", Integral(Pow(x, -1), x)), ("\\int \\frac{1}{a} + \\frac{1}{b} dx", Integral(_Add(_Pow(a, -1), Pow(b, -1)), x)), ("\\int \\frac{3 \\cdot d\\theta}{\\theta}", Integral(3 * _Pow(theta, -1), theta)), ("\\int \\frac{1}{x} + 1 dx", Integral(_Add(_Pow(x, -1), 1), x)), ("x_0", Symbol('x_0', real=True, positive=True)), ("x_{1}", Symbol('x_1', real=True, positive=True)), ("x_a", Symbol('x_a', real=True, positive=True)), ("x_{b}", Symbol('x_b', real=True, positive=True)), ("h_\\theta", Symbol('h_{\\theta}', real=True, positive=True)), ("h_\\theta ", Symbol('h_{\\theta}', real=True, positive=True)), ("h_{\\theta}", Symbol('h_{\\theta}', real=True, positive=True)), # ("h_{\\theta}(x_0, x_1)", Symbol('h_{theta}', real=True)(Symbol('x_{0}', real=True), Symbol('x_{1}', real=True))), ("x!", _factorial(x)), ("100!", _factorial(100)), ("\\theta!", _factorial(theta)), ("(x + 1)!", _factorial(_Add(x, 1))), ("\\left(x + 1\\right)!", _factorial(_Add(x, 1))), ("(x!)!", _factorial(_factorial(x))), ("x!!!", _factorial(_factorial(_factorial(x)))), ("5!7!", _Mul(_factorial(5), _factorial(7))), ("\\sqrt{x}", sqrt(x)), ("\\sqrt{x + b}", sqrt(_Add(x, b))), ("\\sqrt[3]{\\sin x}", root(sin(x), 3)), ("\\sqrt[y]{\\sin x}", root(sin(x), y)), ("\\sqrt[\\theta]{\\sin x}", root(sin(x), theta)), ("x < y", StrictLessThan(x, y)), ("x \\leq y", LessThan(x, y)), ("x > y", StrictGreaterThan(x, y)), ("x \\geq y", GreaterThan(x, y)), ("\\sum_{k = 1}^{3} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^3 c", Sum(c, (k, 1, 3))), ("\\sum^{3}_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum^3_{k = 1} c", Sum(c, (k, 1, 3))), ("\\sum_{k = 1}^{10} k^2", Sum(k**2, (k, 1, 10))), ("\\sum_{n = 0}^{\\infty} \\frac{1}{n!}", Sum(_Pow(_factorial(n), -1), (n, 0, oo))), ("\\prod_{a = b}^{c} x", Product(x, (a, b, c))), ("\\prod_{a = b}^c x", Product(x, (a, b, c))), ("\\prod^{c}_{a = b} x", Product(x, (a, b, c))), ("\\prod^c_{a = b} x", Product(x, (a, b, c))), ("\\ln x", _log(x, E)), ("\\ln xy", _log(x * y, E)), ("\\log x", _log(x, 10)), ("\\log xy", _log(x * y, 10)), # ("\\log_2 x", _log(x, 2)), ("\\log_{2} x", _log(x, 2)), # ("\\log_a x", _log(x, a)), ("\\log_{a} x", _log(x, a)), ("\\log_{11} x", _log(x, 11)), ("\\log_{a^2} x", _log(x, _Pow(a, 2))), ("[x]", x), ("[a + b]", _Add(a, b)), ("\\frac{d}{dx} [ \\tan x ]", Derivative(tan(x), x)), ("2\\overline{x}", 2 * Symbol('xbar', real=True, positive=True)), ("2\\overline{x}_n", 2 * Symbol('xbar_n', real=True, positive=True)), ("\\frac{x}{\\overline{x}_n}", x / Symbol('xbar_n', real=True, positive=True)), ("\\frac{\\sin(x)}{\\overline{x}_n}", sin(x) / Symbol('xbar_n', real=True, positive=True)), ("2\\bar{x}", 2 * Symbol('xbar', real=True, positive=True)), ("2\\bar{x}_n", 2 * Symbol('xbar_n', real=True, positive=True)), ("\\sin\\left(\\theta\\right) \\cdot4", sin(theta) * 4), ("\\ln\\left(\\theta\\right)", _log(theta, E)), ("\\ln\\left(x-\\theta\\right)", _log(x - theta, E)), ("\\ln\\left(\\left(x-\\theta\\right)\\right)", _log(x - theta, E)), ("\\ln\\left(\\left[x-\\theta\\right]\\right)", _log(x - theta, E)), ("\\ln\\left(\\left\\{x-\\theta\\right\\}\\right)", _log(x - theta, E)), ("\\ln\\left(\\left|x-\\theta\\right|\\right)", _log(_Abs(x - theta), E)), ("\\frac{1}{2}xy(x+y)", Mul(Rational(1, 2), x, y, (x + y), evaluate=False)), ("\\frac{1}{2}\\theta(x+y)", Mul(Rational(1, 2), theta, (x + y), evaluate=False)), ("1-f(x)", 1 - f * x), ("\\begin{matrix}1&2\\\\3&4\\end{matrix}", Matrix([[1, 2], [3, 4]])), ("\\begin{matrix}x&x^2\\\\\\sqrt{x}&x\\end{matrix}", Matrix([[x, x**2], [_Pow(x, S.Half), x]])), ("\\begin{matrix}\\sqrt{x}\\\\\\sin(\\theta)\\end{matrix}", Matrix([_Pow(x, S.Half), sin(theta)])), ("\\begin{pmatrix}1&2\\\\3&4\\end{pmatrix}", Matrix([[1, 2], [3, 4]])), ("\\begin{bmatrix}1&2\\\\3&4\\end{bmatrix}", Matrix([[1, 2], [3, 4]])), # scientific notation ("2.5\\times 10^2", Rational(250)), ("1,500\\times 10^{-1}", Rational(150)), # e notation ("2.5E2", Rational(250)), ("1,500E-1", Rational(150)), # multiplication without cmd ("2x2y", Mul(2, x, 2, y, evaluate=False)), ("2x2", Mul(2, x, 2, evaluate=False)), ("x2", x * 2), # lin alg processing ("\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(theta, Matrix([[1, 2], [3, 4]]), evaluate=False)), ("\\theta\\begin{matrix}1\\\\3\\end{matrix} - \\begin{matrix}-1\\\\2\\end{matrix}", MatAdd(MatMul(theta, Matrix([[1], [3]]), evaluate=False), MatMul(-1, Matrix([[-1], [2]]), evaluate=False), evaluate=False)), ("\\theta\\begin{matrix}1&0\\\\0&1\\end{matrix}*\\begin{matrix}3\\\\-2\\end{matrix}", MatMul(theta, Matrix([[1, 0], [0, 1]]), Matrix([3, -2]), evaluate=False)), ("\\frac{1}{9}\\theta\\begin{matrix}1&2\\\\3&4\\end{matrix}", MatMul(Rational(1, 9), theta, Matrix([[1, 2], [3, 4]]), evaluate=False)), ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix};\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix},\\begin{pmatrix}4\\\\3\\\\1\\end{pmatrix},\\begin{pmatrix}1\\\\1\\\\1\\end{pmatrix}\\right\\}", [Matrix([1, 2, 3]), Matrix([4, 3, 1]), Matrix([1, 1, 1])]), ("\\left\\{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right\\}", Matrix([1, 2, 3])), ("\\left{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}\\right}", Matrix([1, 2, 3])), ("{\\begin{pmatrix}1\\\\2\\\\3\\end{pmatrix}}", Matrix([1, 2, 3])), # us dollars ("\\$1,000.00", Rational(1000)), ("\\$543.21", Rational(54321, 100)), ("\\$0.009", Rational(9, 1000)), # percentages ("100\\%", Rational(1)), ("1.5\\%", Rational(15, 1000)), ("0.05\\%", Rational(5, 10000)), # empty set ("\\emptyset", S.EmptySet), # divide by zero ("\\frac{1}{0}", _Pow(0, -1)), ("1+\\frac{5}{0}", _Add(1, _Mul(5, _Pow(0, -1)))), # adjacent single char sub sup ("4^26^2", _Mul(_Pow(4, 2), _Pow(6, 2))), ("x_22^2", _Mul(Symbol('x_2', real=True, positive=True), _Pow(2, 2))) ] def test_good_pair(self, s, eq): assert_equal(s, eq)
print(T3) for i in range(0,5): D = -2*factorial(i) a2 = T2.subs(k,i).doit().simplify() #.expand(force=True) #print(factor(a)) b2 = a2.subs(n, n/2) print('[even n]: {} = ({})/{}'.format(factor(b2), (b2*D).expand(), D)) a3 = T3.subs(k,i).doit().simplify() b3 = a3.subs(n, (n-1)/2) print('[odd n]: {} = {}'.format(factor(b3), gcd_terms(b3.expand()))) raise f = T - (1-(-1)**n)/(-2)**(k+1) f *= -2*factorial(k) / Product(n-2*i, (i, 0, floor(k/2))) print(f) for i in range(0,8): a = f.subs(k,i).doit().simplify() #.expand(force=True) print(factor(a)) ''' -2*(-(-2)**(-k - 1)*(-(-1)**n + 1) + Sum((-1)**(-j + n)*binomial(j, k), (j, 0, n - 1)))*k!/Product(-2*i + n, (i, 0, floor(k/2))) 0 1 1 (2*n - 5)/2 n - 2 (2*n**2 - 13*n + 16)/2 (2*n**2 - 12*n + 13)/2 (4*n**3 - 50*n**2 + 176*n - 151)/4
def test_infinite_product(): # Issue 2638 assert isinstance(Product(2**(1 / factorial(n)), (n, 0, oo)), Product)
def test_Product_is_convergent(): assert Product(1/n**2, (n, 1, oo)).is_convergent() is S.false assert Product(exp(1/n**2), (n, 1, oo)).is_convergent() is S.true assert Product(1/n, (n, 1, oo)).is_convergent() is S.false assert Product(1 + 1/n, (n, 1, oo)).is_convergent() is S.false assert Product(1 + 1/n**2, (n, 1, oo)).is_convergent() is S.true
def test_karr_convention(): # Test the Karr product convention that we want to hold. # See his paper "Summation in Finite Terms" for a detailed # reasoning why we really want exactly this definition. # The convention is described for sums on page 309 and # essentially in section 1.4, definition 3. For products # we can find in analogy: # # \prod_{m <= i < n} f(i) 'has the obvious meaning' for m < n # \prod_{m <= i < n} f(i) = 0 for m = n # \prod_{m <= i < n} f(i) = 1 / \prod_{n <= i < m} f(i) for m > n # # It is important to note that he defines all products with # the upper limit being *exclusive*. # In contrast, sympy and the usual mathematical notation has: # # prod_{i = a}^b f(i) = f(a) * f(a+1) * ... * f(b-1) * f(b) # # with the upper limit *inclusive*. So translating between # the two we find that: # # \prod_{m <= i < n} f(i) = \prod_{i = m}^{n-1} f(i) # # where we intentionally used two different ways to typeset the # products and its limits. i = Symbol("i", integer=True) k = Symbol("k", integer=True) j = Symbol("j", integer=True) # A simple example with a concrete factors and symbolic limits. # The normal product: m = k and n = k + j and therefore m < n: m = k n = k + j a = m b = n - 1 S1 = Product(i**2, (i, a, b)).doit() # The reversed product: m = k + j and n = k and therefore m > n: m = k + j n = k a = m b = n - 1 S2 = Product(i**2, (i, a, b)).doit() assert simplify(S1 * S2) == 1 # Test the empty product: m = k and n = k and therefore m = n: m = k n = k a = m b = n - 1 Sz = Product(i**2, (i, a, b)).doit() assert Sz == 1 # Another example this time with an unspecified factor and # numeric limits. (We can not do both tests in the same example.) f = Function("f") # The normal product with m < n: m = 2 n = 11 a = m b = n - 1 S1 = Product(f(i), (i, a, b)).doit() # The reversed product with m > n: m = 11 n = 2 a = m b = n - 1 S2 = Product(f(i), (i, a, b)).doit() assert simplify(S1 * S2) == 1 # Test the empty product with m = n: m = 5 n = 5 a = m b = n - 1 Sz = Product(f(i), (i, a, b)).doit() assert Sz == 1
import numpy.polynomial.chebyshev as cheb import sympy from sympy import Product, Sum, DeferredVector, lambdify from sympy.abc import a, j from sympy.tensor import IndexedBase, Idx N = 4 A = -3 B = 3 x = IndexedBase('x') y = IndexedBase('y') i = sympy.symbols('i', cls=Idx) L = Sum( y[i] * Product((a - x[j]) / (x[i] - x[j]), (j, 0, i - 1)).doit() * Product( (a - x[j]) / (x[i] - x[j]), (j, i + 1, N)).doit(), (i, 0, N)).doit() L_lambda = lambdify([a, DeferredVector('x'), DeferredVector('y')], L, 'numpy') display_grid = np.linspace(A, B, 1000) interpol_grid = np.linspace(A, B, N + 1) che_interpol_grid = (cheb.chebroots((0, 0, 0, 0, 0, 1)) * (B - A) + A + B) / 2 plt.plot(display_grid, L_lambda(a=display_grid, x=interpol_grid, y=np.sin(interpol_grid)), color='red', label='Lagrange polynomial (equidistant nodes)') plt.plot(display_grid, L_lambda(a=display_grid, x=che_interpol_grid, y=np.sin(che_interpol_grid)), color='blue',
def test_simple_products(): assert Product(S.NaN, (x, 1, 3)) is S.NaN assert product(S.NaN, (x, 1, 3)) is S.NaN assert Product(x, (n, a, a)).doit() == x assert Product(x, (x, a, a)).doit() == a assert Product(x, (y, 1, a)).doit() == x**a lo, hi = 1, 2 s1 = Product(n, (n, lo, hi)) s2 = Product(n, (n, hi, lo)) assert s1 != s2 # This IS correct according to Karr product convention assert s1.doit() == 2 assert s2.doit() == 1 lo, hi = x, x + 1 s1 = Product(n, (n, lo, hi)) s2 = Product(n, (n, hi, lo)) s3 = 1 / Product(n, (n, hi + 1, lo - 1)) assert s1 != s2 # This IS correct according to Karr product convention assert s1.doit() == x * (x + 1) assert s2.doit() == 1 assert s3.doit() == x * (x + 1) assert Product(Integral(2*x, (x, 1, y)) + 2*x, (x, 1, 2)).doit() == \ (y**2 + 1)*(y**2 + 3) assert product(2, (n, a, b)) == 2**(b - a + 1) assert product(n, (n, 1, b)) == factorial(b) assert product(n**3, (n, 1, b)) == factorial(b)**3 assert product(3**(2 + n), (n, a, b)) \ == 3**(2*(1 - a + b) + b/2 + (b**2)/2 + a/2 - (a**2)/2) assert product(cos(n), (n, 3, 5)) == cos(3) * cos(4) * cos(5) assert product(cos(n), (n, x, x + 2)) == cos(x) * cos(x + 1) * cos(x + 2) assert isinstance(product(cos(n), (n, x, x + S.Half)), Product) # If Product managed to evaluate this one, it most likely got it wrong! assert isinstance(Product(n**n, (n, 1, b)), Product)
def test_conjugate_transpose(): p = Product(x**k, (k, 1, 3)) assert p.adjoint().doit() == p.doit().adjoint() assert p.conjugate().doit() == p.doit().conjugate() assert p.transpose().doit() == p.doit().transpose() A, B = symbols("A B", commutative=False) p = Product(A*B**k, (k, 1, 3)) assert p.adjoint().doit() == p.doit().adjoint() assert p.conjugate().doit() == p.doit().conjugate() assert p.transpose().doit() == p.doit().transpose() p = Product(B**k*A, (k, 1, 3)) assert p.adjoint().doit() == p.doit().adjoint() assert p.conjugate().doit() == p.doit().conjugate() assert p.transpose().doit() == p.doit().transpose()
def test_distribution_over_equality(): assert Product(Eq(x * 2, f(x)), (x, 1, 3)).doit() == Eq(48, f(1) * f(2) * f(3)) assert Sum(Eq(f(x), x**2), (x, 0, y)) == \ Eq(Sum(f(x), (x, 0, y)), Sum(x**2, (x, 0, y)))
def test_simplify_prod(): y, t, b, c = symbols('y, t, b, c', integer = True) _simplify = lambda e: simplify(e, doit=False) assert _simplify(Product(x*y, (x, n, m), (y, a, k)) * \ Product(y, (x, n, m), (y, a, k))) == \ Product(x*y**2, (x, n, m), (y, a, k)) assert _simplify(3 * y* Product(x, (x, n, m)) * Product(x, (x, m + 1, a))) \ == 3 * y * Product(x, (x, n, a)) assert _simplify(Product(x, (x, k + 1, a)) * Product(x, (x, n, k))) == \ Product(x, (x, n, a)) assert _simplify(Product(x, (x, k + 1, a)) * Product(x + 1, (x, n, k))) == \ Product(x, (x, k + 1, a)) * Product(x + 1, (x, n, k)) assert _simplify(Product(x, (t, a, b)) * Product(y, (t, a, b)) * \ Product(x, (t, b+1, c))) == Product(x*y, (t, a, b)) * \ Product(x, (t, b+1, c)) assert _simplify(Product(x, (t, a, b)) * Product(x, (t, b+1, c)) * \ Product(y, (t, a, b))) == Product(x*y, (t, a, b)) * \ Product(x, (t, b+1, c))
def _eval_rewrite_as_Product(self, n, **kwargs): from sympy import Product if n.is_nonnegative and n.is_integer: i = Dummy('i', integer=True) return Product(i, (i, 1, n))
def test_reorder(): b, y, c, d, z = symbols('b, y, c, d, z', integer = True) assert Product(x*y, (x, a, b), (y, c, d)).reorder((0, 1)) == \ Product(x*y, (y, c, d), (x, a, b)) assert Product(x, (x, a, b), (x, c, d)).reorder((0, 1)) == \ Product(x, (x, c, d), (x, a, b)) assert Product(x*y + z, (x, a, b), (z, m, n), (y, c, d)).reorder(\ (2, 0), (0, 1)) == Product(x*y + z, (z, m, n), (y, c, d), (x, a, b)) assert Product(x*y*z, (x, a, b), (y, c, d), (z, m, n)).reorder(\ (0, 1), (1, 2), (0, 2)) == \ Product(x*y*z, (x, a, b), (z, m, n), (y, c, d)) assert Product(x*y*z, (x, a, b), (y, c, d), (z, m, n)).reorder(\ (x, y), (y, z), (x, z)) == \ Product(x*y*z, (x, a, b), (z, m, n), (y, c, d)) assert Product(x*y, (x, a, b), (y, c, d)).reorder((x, 1)) == \ Product(x*y, (y, c, d), (x, a, b)) assert Product(x*y, (x, a, b), (y, c, d)).reorder((y, x)) == \ Product(x*y, (y, c, d), (x, a, b))