def test_gosper_sum_AeqB_part3(): f3a = 1/n**4 f3b = (6*n + 3)/(4*n**4 + 8*n**3 + 8*n**2 + 4*n + 3) f3c = 2**n*(n**2 - 2*n - 1)/(n**2*(n + 1)**2) f3d = n**2*4**n/((n + 1)*(n + 2)) f3e = 2**n/(n + 1) f3f = 4*(n - 1)*(n**2 - 2*n - 1)/(n**2*(n + 1)**2*(n - 2)**2*(n - 3)**2) f3g = (n**4 - 14*n**2 - 24*n - 9)*2**n/(n**2*(n + 1)**2*(n + 2)**2 * (n + 3)**2) # g3a -> no closed form g3b = m*(m + 2)/(2*m**2 + 4*m + 3) g3c = 2**m/m**2 - 2 g3d = Rational(2, 3) + 4**(m + 1)*(m - 1)/(m + 2)/3 # g3e -> no closed form g3f = -(-Rational(1, 16) + 1/((m - 2)**2*(m + 1)**2)) # the AeqB key is wrong g3g = -Rational(2, 9) + 2**(m + 1)/((m + 1)**2*(m + 3)**2) g = gosper_sum(f3a, (n, 1, m)) assert g is None g = gosper_sum(f3b, (n, 1, m)) assert g is not None and simplify(g - g3b) == 0 g = gosper_sum(f3c, (n, 1, m - 1)) assert g is not None and simplify(g - g3c) == 0 g = gosper_sum(f3d, (n, 1, m)) assert g is not None and simplify(g - g3d) == 0 g = gosper_sum(f3e, (n, 0, m - 1)) assert g is None g = gosper_sum(f3f, (n, 4, m)) assert g is not None and simplify(g - g3f) == 0 g = gosper_sum(f3g, (n, 1, m)) assert g is not None and simplify(g - g3g) == 0
def test_gosper_sum_AeqB_part3(): f3a = 1 / n**4 f3b = (6 * n + 3) / (4 * n**4 + 8 * n**3 + 8 * n**2 + 4 * n + 3) f3c = 2**n * (n**2 - 2 * n - 1) / (n**2 * (n + 1)**2) f3d = n**2 * 4**n / ((n + 1) * (n + 2)) f3e = 2**n / (n + 1) f3f = 4 * (n - 1) * (n**2 - 2 * n - 1) / (n**2 * (n + 1)**2 * (n - 2)**2 * (n - 3)**2) f3g = (n**4 - 14 * n**2 - 24 * n - 9) * 2**n / (n**2 * (n + 1)**2 * (n + 2)**2 * (n + 3)**2) # g3a -> no closed form g3b = m * (m + 2) / (2 * m**2 + 4 * m + 3) g3c = 2**m / m**2 - 2 g3d = Rational(2, 3) + 4**(m + 1) * (m - 1) / (m + 2) / 3 # g3e -> no closed form g3f = -(-Rational(1, 16) + 1 / ((m - 2)**2 * (m + 1)**2)) # the AeqB key is wrong g3g = -Rational(2, 9) + 2**(m + 1) / ((m + 1)**2 * (m + 3)**2) g = gosper_sum(f3a, (n, 1, m)) assert g is None g = gosper_sum(f3b, (n, 1, m)) assert g is not None and simplify(g - g3b) == 0 g = gosper_sum(f3c, (n, 1, m - 1)) assert g is not None and simplify(g - g3c) == 0 g = gosper_sum(f3d, (n, 1, m)) assert g is not None and simplify(g - g3d) == 0 g = gosper_sum(f3e, (n, 0, m - 1)) assert g is None g = gosper_sum(f3f, (n, 4, m)) assert g is not None and simplify(g - g3f) == 0 g = gosper_sum(f3g, (n, 1, m)) assert g is not None and simplify(g - g3g) == 0
def test_diff(): f = Function('f') g = Function('g') assert simplify(g(x).diff(x)*f(x).diff(x) - f(x).diff(x)*g(x).diff(x)) == 0 assert simplify(2*f(x)*f(x).diff(x) - diff(f(x)**2, x)) == 0 assert simplify(diff(1/f(x), x) + f(x).diff(x)/f(x)**2) == 0 assert simplify(f(x).diff(x, y) - f(x).diff(y, x)) == 0
def compute_solution_for_recurrence(recurr_coeff: Expr, inhom_part_solution: Expr, initial_value: Expr): """ Computes the (unique) solution to the recurrence relation: f(0) = initial_value; f(n+1) = recurr_coeff * f(n) + inhom_part_solution """ log( f"Start compute solution for recurrence, { recurr_coeff }, { inhom_part_solution }, { initial_value }", LOG_VERBOSE) n = symbols('n', integer=True, positive=True) if recurr_coeff.is_zero: return expand(inhom_part_solution.xreplace({n: n - 1})) hom_solution = (recurr_coeff**n) * initial_value k = symbols('_k', integer=True, positive=True) summand = simplify( (recurr_coeff**k) * inhom_part_solution.xreplace({n: (n - 1) - k})) particular_solution = summation(summand, (k, 0, (n - 1))) particular_solution = without_piecewise(particular_solution) solution = simplify(hom_solution + particular_solution) log( f"End compute solution for recurrence, { recurr_coeff }, { inhom_part_solution }, { initial_value }", LOG_VERBOSE) return solution
def test_simplification(): """Test working of simplification methods.""" set1 = [[0, 0, 1], [0, 1, 1], [1, 0, 0], [1, 1, 0]] set2 = [[0, 0, 0], [0, 1, 0], [1, 0, 1], [1, 1, 1]] assert SOPform([x, y, z], set1) == Or(And(Not(x), z), And(Not(z), x)) assert Not(SOPform([x, y, z], set2)) == Not(Or(And(Not(x), Not(z)), And(x, z))) assert POSform([x, y, z], set1 + set2) is true assert SOPform([x, y, z], set1 + set2) is true assert SOPform([Dummy(), Dummy(), Dummy()], set1 + set2) is true minterms = [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 1, 1]] dontcares = [[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 1]] assert ( SOPform([w, x, y, z], minterms, dontcares) == Or(And(Not(w), z), And(y, z))) assert POSform([w, x, y, z], minterms, dontcares) == And(Or(Not(w), y), z) # test simplification ans = And(A, Or(B, C)) assert simplify_logic(A & (B | C)) == ans assert simplify_logic((A & B) | (A & C)) == ans assert simplify_logic(Implies(A, B)) == Or(Not(A), B) assert simplify_logic(Equivalent(A, B)) == \ Or(And(A, B), And(Not(A), Not(B))) assert simplify_logic(And(Equality(A, 2), C)) == And(Equality(A, 2), C) assert simplify_logic(And(Equality(A, 2), A)) == And(Equality(A, 2), A) assert simplify_logic(And(Equality(A, B), C)) == And(Equality(A, B), C) assert simplify_logic(Or(And(Equality(A, 3), B), And(Equality(A, 3), C))) \ == And(Equality(A, 3), Or(B, C)) e = And(A, x**2 - x) assert simplify_logic(e) == And(A, x*(x - 1)) assert simplify_logic(e, deep=False) == e pytest.raises(ValueError, lambda: simplify_logic(A & (B | C), form='spam')) e = x & y ^ z | (z ^ x) res = [(x & ~z) | (z & ~x) | (z & ~y), (x & ~y) | (x & ~z) | (z & ~x)] assert simplify_logic(e) in res assert SOPform([z, y, x], [[0, 0, 1], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0]]) == res[1] # check input ans = SOPform([x, y], [[1, 0]]) assert SOPform([x, y], [[1, 0]]) == ans assert POSform([x, y], [[1, 0]]) == ans pytest.raises(ValueError, lambda: SOPform([x], [[1]], [[1]])) assert SOPform([x], [[1]], [[0]]) is true assert SOPform([x], [[0]], [[1]]) is true assert SOPform([x], [], []) is false pytest.raises(ValueError, lambda: POSform([x], [[1]], [[1]])) assert POSform([x], [[1]], [[0]]) is true assert POSform([x], [[0]], [[1]]) is true assert POSform([x], [], []) is false # check working of simplify assert simplify((A & B) | (A & C)) == And(A, Or(B, C)) assert simplify(And(x, Not(x))) is false assert simplify(Or(x, Not(x))) is true
def test_log_expand(): w = Symbol('w', positive=True) e = log(w**(log(5)/log(3))) assert e.expand() == log(5)/log(3) * log(w) x, y, z = symbols('x,y,z', positive=True) assert log(x*(y + z)).expand(mul=False) == log(x) + log(y + z) assert log(log(x**2)*log(y*z)).expand() in [log(2*log(x)*log(y) + 2*log(x)*log(z)), log(log(x)*log(z) + log(y)*log(x)) + log(2), log((log(y) + log(z))*log(x)) + log(2)] assert log(x**log(x**2)).expand(deep=False) == log(x)*log(x**2) assert log(x**log(x**2)).expand() == 2*log(x)**2 assert ((log(x*(y + z))*(x + y)).expand(mul=True, log=True) == y*log(x) + y*log(y + z) + x*log(x) + x*log(y + z)) x, y = symbols('x,y') assert log(x*y).expand(force=True) == log(x) + log(y) assert log(x**y).expand(force=True) == y*log(x) assert log(exp(x)).expand(force=True) == x # there's generally no need to expand out logs since this requires # factoring and if simplification is sought, it's cheaper to put # logs together than it is to take them apart. assert log(2*3**2).expand() != 2*log(3) + log(2) # issue sympy/sympy#8866 assert simplify(log(x, 10, evaluate=False)) == simplify(log(x, 10)) assert expand_log(log(x, 10, evaluate=False)) == expand_log(log(x, 10)) y = Symbol('y', positive=True) l1 = log(exp(y), exp(10)) b1 = log(exp(y), exp(5)) l2 = log(exp(y), exp(10), evaluate=False) b2 = log(exp(y), exp(5), evaluate=False) assert simplify(log(l1, b1)) == simplify(log(l2, b2)) assert expand_log(log(l1, b1)) == expand_log(log(l2, b2))
def test_rational_products(): assert simplify(product(1 + 1/n, (n, a, b))) == (1 + b)/a assert simplify(product(n + 1, (n, a, b))) == gamma(2 + b)/gamma(1 + a) assert simplify(product((n + 1)/(n - 1), (n, a, b))) == b*(1 + b)/(a*(a - 1)) assert simplify(product(n/(n + 1)/(n + 2), (n, a, b))) == \ a*gamma(a + 2)/(b + 1)/gamma(b + 3) assert simplify(product(n*(n + 1)/(n - 1)/(n - 2), (n, a, b))) == \ b**2*(b - 1)*(1 + b)/(a - 1)**2/(a*(a - 2))
def test_simplify(): assert simplify(x*(y + 1) - x*y - x + 1 < x) == (x > 1) assert simplify(Integer(1) < -x) == (x < -1) # issue sympy/sympy#10304 d = -(3*2**pi)**(1/pi) + 2*3**(1/pi) assert d.is_real assert simplify(Eq(1 + I*d, 0)) is False
def test_square_1(self): program = load_benchmark("square") moments = core(program, None, 1) x, y = symbols("x,y") result = prepare_result("n") self.assertEqual(simplify(prepare_moment(moments[x]) - result), 0) result = prepare_result("n**2 + n") self.assertEqual(simplify(prepare_moment(moments[y]) - result), 0)
def test_running_1(self): program = load_benchmark("running") moments = core(program, None, 1) x, y = symbols("x,y") result = prepare_result("0") self.assertEqual(simplify(prepare_moment(moments[x]) - result), 0) result = prepare_result("y(0)") self.assertEqual(simplify(prepare_moment(moments[y]) - result), 0)
def test_random_walk_2d_3(self): program = load_benchmark("random_walk_2d") moments = core(program, None, 3) x, y = symbols("x,y") result = prepare_result("0") self.assertEqual(simplify(prepare_moment(moments[x**3]) - result), 0) result = prepare_result("0") self.assertEqual(simplify(prepare_moment(moments[y**3]) - result), 0)
def test_square_2(self): program = load_benchmark("square") moments = core(program, None, 2) x, y = symbols("x,y") result = prepare_result("n**2 + n") self.assertEqual(simplify(prepare_moment(moments[x**2]) - result), 0) result = prepare_result("n**4 + 6*n**3 + 3*n**2 - 2*n") self.assertEqual(simplify(prepare_moment(moments[y**2]) - result), 0)
def test_sum_rnd_series_2(self): program = load_benchmark("sum_rnd_series") moments = core(program, None, 2) x, y = symbols("x,y") result = prepare_result("n*(3*n**3 + 10*n**2 + 9*n + 2)/48") self.assertEqual(simplify(prepare_moment(moments[x**2]) - result), 0) result = prepare_result("n**2") self.assertEqual(simplify(prepare_moment(moments[y**2]) - result), 0)
def test_weibull_numeric(): # Test for integers and rationals a = 1 bvals = [Rational(1, 2), 1, Rational(3, 2), Integer(5)] for b in bvals: X = Weibull('x', a, b) assert simplify(E(X)) == simplify(a * gamma(1 + 1/b)) assert simplify(variance(X)) == simplify( a**2 * gamma(1 + 2/b) - E(X)**2)
def test_log_product(): 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)))) == \ log(Product(x**i*y**j, (i, 1, n), (j, 1, m))) expr = log(Product(-2, (n, 0, 4))) assert simplify(expr) == expr
def test_sum_rnd_series_1(self): program = load_benchmark("sum_rnd_series") moments = core(program, None, 1) x, y = symbols("x,y") result = prepare_result("n*(n + 1)/4") self.assertEqual(simplify(prepare_moment(moments[x]) - result), 0) result = prepare_result("n") self.assertEqual(simplify(prepare_moment(moments[y]) - result), 0)
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_Piecewise(): e1 = x * (x + y) - y * (x + y) e2 = sin(x)**2 + cos(x)**2 e3 = expand((x + y) * y / x) s1 = simplify(e1) s2 = simplify(e2) s3 = simplify(e3) assert simplify(Piecewise((e1, x < e2), (e3, True))) == \ Piecewise((s1, x < s2), (s3, True))
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_simplify(): assert simplify(x*(y + 1) - x*y - x + 1 < x) == (x > 1) assert simplify(Integer(1) < -x) == (x < -1) # issue sympy/sympy#10304 d = -(3*2**pi)**(1/pi) + 2*3**(1/pi) assert d.is_real assert simplify(Eq(1 + I*d, 0)) is False assert simplify(Ne(1 + I*d, 0)) is True
def test_sum_rnd_series_3(self): program = load_benchmark("sum_rnd_series") moments = core(program, None, 3) x, y = symbols("x,y") result = prepare_result("n**2*(n**4 + 7*n**3 + 13*n**2 + 9*n + 2)/64") self.assertEqual(simplify(prepare_moment(moments[x**3]) - result), 0) result = prepare_result("n**3") self.assertEqual(simplify(prepare_moment(moments[y**3]) - result), 0)
def test_simplify(): x, y = R2_r.coord_functions() dx, dy = R2_r.base_oneforms() ex, ey = R2_r.base_vectors() assert simplify(x) == x assert simplify(x * y) == x * y assert simplify(dx * dy) == dx * dy assert simplify(ex * ey) == ex * ey assert ((1 - x) * dx) / (1 - x)**2 == dx / (1 - x)
def test_expint(): """ Test various exponential integrals. """ from diofant import (expint, unpolarify, Symbol, Ci, Si, Shi, Chi, sin, cos, sinh, cosh, Ei) assert simplify( unpolarify( integrate(exp(-z * x) / x**y, (x, 1, oo), meijerg=True, conds='none').rewrite(expint).expand( func=True))) == expint(y, z) assert integrate(exp(-z*x)/x, (x, 1, oo), meijerg=True, conds='none').rewrite(expint).expand() == \ expint(1, z) assert integrate(exp(-z*x)/x**2, (x, 1, oo), meijerg=True, conds='none').rewrite(expint).expand() == \ expint(2, z).rewrite(Ei).rewrite(expint) assert integrate(exp(-z*x)/x**3, (x, 1, oo), meijerg=True, conds='none').rewrite(expint).expand() == \ expint(3, z).rewrite(Ei).rewrite(expint).expand() t = Symbol('t', positive=True) assert integrate(-cos(x) / x, (x, t, oo), meijerg=True).expand() == Ci(t) assert integrate(-sin(x)/x, (x, t, oo), meijerg=True).expand() == \ Si(t) - pi/2 assert integrate(sin(x) / x, (x, 0, z), meijerg=True) == Si(z) assert integrate(sinh(x) / x, (x, 0, z), meijerg=True) == Shi(z) assert integrate(exp(-x)/x, x, meijerg=True).expand().rewrite(expint) == \ I*pi - expint(1, x) assert integrate(exp(-x)/x**2, x, meijerg=True).rewrite(expint).expand() \ == expint(1, x) - exp(-x)/x - I*pi u = Symbol('u', polar=True) assert integrate(cos(u)/u, u, meijerg=True).expand().as_independent(u)[1] \ == Ci(u) assert integrate(cosh(u)/u, u, meijerg=True).expand().as_independent(u)[1] \ == Chi(u) assert integrate( expint(1, x), x, meijerg=True).rewrite(expint).expand() == x * expint(1, x) - exp(-x) assert integrate(expint(2, x), x, meijerg=True ).rewrite(expint).expand() == \ -x**2*expint(1, x)/2 + x*exp(-x)/2 - exp(-x)/2 assert simplify(unpolarify(integrate(expint(y, x), x, meijerg=True).rewrite(expint).expand(func=True))) == \ -expint(y + 1, x) assert integrate(Si(x), x, meijerg=True) == x * Si(x) + cos(x) assert integrate(Ci(u), u, meijerg=True).expand() == u * Ci(u) - sin(u) assert integrate(Shi(x), x, meijerg=True) == x * Shi(x) - cosh(x) assert integrate(Chi(u), u, meijerg=True).expand() == u * Chi(u) - sinh(u) assert integrate(Si(x) * exp(-x), (x, 0, oo), meijerg=True) == pi / 4 assert integrate(expint(1, x) * sin(x), (x, 0, oo), meijerg=True) == log(2) / 2
def test_running_2(self): program = load_benchmark("running") moments = core(program, None, 2) x, y = symbols("x,y") result = prepare_result("b**2*n/3") self.assertEqual(simplify(prepare_moment(moments[x**2]) - result), 0) result = prepare_result( "b**2*n**3/9 + b**2*n**2/6 + b**2*n/18 + n + y(0)**2") self.assertEqual(simplify(prepare_moment(moments[y**2]) - result), 0)
def test_square_3(self): program = load_benchmark("square") moments = core(program, None, 3) x, y = symbols("x,y") result = prepare_result("n**3 + 3*n**2") self.assertEqual(simplify(prepare_moment(moments[x**3]) - result), 0) result = prepare_result( "n**6 + 15*n**5 + 45*n**4 - 15*n**3 - 30*n**2 + 16*n") self.assertEqual(simplify(prepare_moment(moments[y**3]) - result), 0)
def test_init_rv_3(self): program = load_benchmark("test_init_rv") moments = core(program, None, 3) x, y, s = symbols("x,y,s") result = prepare_result("0") self.assertEqual(simplify(prepare_moment(moments[x**3]) - result), 0) result = prepare_result("0") self.assertEqual(simplify(prepare_moment(moments[y**3]) - result), 0) result = prepare_result("s(0)**3") self.assertEqual(simplify(prepare_moment(moments[s**3]) - result), 0)
def test_running_3(self): program = load_benchmark("running") moments = core(program, None, 3) x, y = symbols("x,y") result = prepare_result("0") self.assertEqual(simplify(prepare_moment(moments[x**3]) - result), 0) result = prepare_result( "b**2*n**3*y(0)/3 + b**2*n**2*y(0)/2 + b**2*n*y(0)/6 + 3*n*y(0) + y(0)**3" ) self.assertEqual(simplify(prepare_moment(moments[y**3]) - result), 0)
def test_cc_3(self): program = load_benchmark("cc") moments = core(program, None, 3) f, c, d = symbols("f,c,d") result = prepare_result("1/2") self.assertEqual(simplify(prepare_moment(moments[f**3]) - result), 0) result = prepare_result("1 - 2**(-n)") self.assertEqual(simplify(prepare_moment(moments[c**3]) - result), 0) result = prepare_result("1 - 2**(-n)") self.assertEqual(simplify(prepare_moment(moments[d**3]) - result), 0)
def test_duelling_cowboys_1(self): program = load_benchmark("duelling_cowboys") moments = core(program, None, 1) ahit, bhit, y = symbols("ahit,bhit,y") result = prepare_result("a") self.assertEqual(simplify(prepare_moment(moments[ahit]) - result), 0) result = prepare_result("-a*b+b") self.assertEqual(simplify(prepare_moment(moments[bhit]) - result), 0) result = prepare_result("a*b - a - b") self.assertEqual(simplify(prepare_moment(moments[y]) - result), 0)
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 simplify(P / (g.subs(i, n) / g.subs(i, m))) == 1
def test_nakagami(): mu = Symbol('mu', positive=True) omega = Symbol('omega', positive=True) X = Nakagami('x', mu, omega) assert density(X)(x) == (2*x**(2*mu - 1)*mu**mu*omega**(-mu) * exp(-x**2*mu/omega)/gamma(mu)) assert simplify(E(X, meijerg=True)) == (sqrt(mu)*sqrt(omega) * gamma(mu + Rational(1, 2))/gamma(mu + 1)) assert simplify(variance(X, meijerg=True)) == ( omega - omega*gamma(mu + Rational(1, 2))**2/(gamma(mu)*gamma(mu + 1)))
def test_nakagami(): mu = Symbol("mu", positive=True) omega = Symbol("omega", positive=True) X = Nakagami('x', mu, omega) assert density(X)(x) == (2*x**(2*mu - 1)*mu**mu*omega**(-mu) * exp(-x**2*mu/omega)/gamma(mu)) assert simplify(E(X, meijerg=True)) == (sqrt(mu)*sqrt(omega) * gamma(mu + Rational(1, 2))/gamma(mu + 1)) assert simplify(variance(X, meijerg=True)) == ( omega - omega*gamma(mu + Rational(1, 2))**2/(gamma(mu)*gamma(mu + 1)))
def test_the_sum(m, n): # g g = i**3 + 2*i**2 - 3*i # f = Delta g f = simplify(g.subs({i: i + 1}) - g) # The sum a = m b = n - 1 S = Sum(f, (i, a, b)).doit() # Test if Sum_{m <= i < n} f(i) = g(n) - g(m) assert simplify(S - (g.subs({i: n}) - g.subs({i: m}))) == 0
def test_the_sum(m, n): # g g = i**3 + 2 * i**2 - 3 * i # f = Delta g f = simplify(g.subs({i: i + 1}) - g) # The sum a = m b = n - 1 S = Sum(f, (i, a, b)).doit() # Test if Sum_{m <= i < n} f(i) = g(n) - g(m) assert simplify(S - (g.subs({i: n}) - g.subs({i: m}))) == 0
def test_sympyissue_8866(): assert simplify(log(x, 10, evaluate=False)) == simplify(log(x, 10)) assert expand_log(log(x, 10, evaluate=False)) == expand_log(log(x, 10)) y = Symbol('y', positive=True) l1 = log(exp(y), exp(10)) b1 = log(exp(y), exp(5)) l2 = log(exp(y), exp(10), evaluate=False) b2 = log(exp(y), exp(5), evaluate=False) assert simplify(log(l1, b1)) == simplify(log(l2, b2)) assert expand_log(log(l1, b1)) == expand_log(log(l2, b2))
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 simplify(P/(g.subs({i: n})/g.subs({i: m}))) == 1
def test_pmint_besselj(): # TODO: in both cases heurisch() gives None. Wrong besselj() derivative? f = besselj(nu + 1, x)/besselj(nu, x) g = nu*log(x) - log(besselj(nu, x)) assert simplify(heurisch(f, x) - g) == 0 f = (nu*besselj(nu, x) - x*besselj(nu + 1, x))/x g = besselj(nu, x) assert simplify(heurisch(f, x) - g) == 0
def test_binomial_symbolic(): n = 10 # Because we're using for loops, can't do symbolic n p = symbols('p', positive=True) X = Binomial('X', n, p) assert simplify(E(X)) == n*p == simplify(moment(X, 1)) assert simplify(variance(X)) == n*p*(1 - p) == simplify(cmoment(X, 2)) assert cancel((skewness(X) - (1-2*p)/sqrt(n*p*(1-p)))) == 0 # Test ability to change success/failure winnings H, T = symbols('H T') Y = Binomial('Y', n, p, succ=H, fail=T) assert simplify(E(Y) - (n*(H*p + T*(1 - p)))) == 0
def test_uniform(): l = Symbol('l', real=True) w = Symbol('w', positive=True, finite=True) X = Uniform('x', l, l + w) assert simplify(E(X)) == l + w/2 assert simplify(variance(X)) == w**2/12 # With numbers all is well X = Uniform('x', 3, 5) assert P(X < 3) == 0 and P(X > 5) == 0 assert P(X < 4) == P(X > 4) == Rational(1, 2)
def test_Piecewise(): e1 = x*(x + y) - y*(x + y) e2 = sin(x)**2 + cos(x)**2 e3 = expand((x + y)*y/x) s1 = simplify(e1) s2 = simplify(e2) s3 = simplify(e3) assert simplify(Piecewise((e1, x < e2), (e3, True))) == \ Piecewise((s1, x < s2), (s3, True)) # trigsimp tries not to touch non-trig containing args assert trigsimp(Piecewise((e1, e3 < e2), (e3, True))) == \ Piecewise((e1, e3 < s2), (e3, True))
def test_expint(): """ Test various exponential integrals. """ assert simplify(integrate(exp(-z*x)/x**y, (x, 1, oo), meijerg=True, conds='none').rewrite(expint)) == expint(y, z) assert integrate(exp(-z*x)/x, (x, 1, oo), meijerg=True, conds='none').rewrite(expint).expand() == \ expint(1, z) assert integrate(exp(-z*x)/x**2, (x, 1, oo), meijerg=True, conds='none').rewrite(expint).expand() == \ expint(2, z).rewrite(Ei).rewrite(expint) assert integrate(exp(-z*x)/x**3, (x, 1, oo), meijerg=True, conds='none').rewrite(expint).expand() == \ expint(3, z).rewrite(Ei).rewrite(expint).expand() t = Symbol('t', positive=True) assert integrate(-cos(x)/x, (x, t, oo), meijerg=True).expand() == Ci(t) assert integrate(-sin(x)/x, (x, t, oo), meijerg=True).expand() == \ Si(t) - pi/2 assert integrate(sin(x)/x, (x, 0, z), meijerg=True) == Si(z) assert integrate(sinh(x)/x, (x, 0, z), meijerg=True) == Shi(z) assert integrate(exp(-x)/x, x, meijerg=True).expand().rewrite(expint) == \ I*pi - expint(1, x) assert integrate(exp(-x)/x**2, x, meijerg=True).rewrite(expint).expand() \ == expint(1, x) - exp(-x)/x - I*pi u = Symbol('u', polar=True) assert integrate(cos(u)/u, u, meijerg=True).expand().as_independent(u)[1] \ == Ci(u) assert integrate(cosh(u)/u, u, meijerg=True).expand().as_independent(u)[1] \ == Chi(u) assert (integrate(expint(1, x), x, meijerg=True).rewrite(expint).expand() == x*expint(1, x) - exp(-x)) assert (integrate(expint(2, x), x, meijerg=True).rewrite(expint).expand() == -x**2*expint(1, x)/2 + x*exp(-x)/2 - exp(-x)/2) assert (simplify(unpolarify(integrate(expint(y, x), x, meijerg=True).rewrite(expint))) == -expint(y + 1, x)) assert integrate(Si(x), x, meijerg=True) == x*Si(x) + cos(x) assert integrate(Ci(u), u, meijerg=True).expand() == u*Ci(u) - sin(u) assert integrate(Shi(x), x, meijerg=True) == x*Shi(x) - cosh(x) assert integrate(Chi(u), u, meijerg=True).expand() == u*Chi(u) - sinh(u) assert integrate(Si(x)*exp(-x), (x, 0, oo), meijerg=True) == pi/4 assert integrate(expint(1, x)*sin(x), (x, 0, oo), meijerg=True) == log(2)/2
def test_bernoulli(): p, a, b = symbols('p a b') X = Bernoulli('B', p, a, b) assert E(X) == a*p + b*(-p + 1) assert density(X)[a] == p assert density(X)[b] == 1 - p X = Bernoulli('B', p, 1, 0) assert E(X) == p assert simplify(variance(X)) == p*(1 - p) assert E(a*X + b) == a*E(X) + b assert simplify(variance(a*X + b)) == simplify(a**2 * variance(X))
def test_recursive(): a, b, c = symbols('a b c', positive=True) r = exp(-(x - a)**2)*exp(-(x - b)**2) e = integrate(r, (x, 0, oo), meijerg=True) assert simplify(e.expand()) == ( sqrt(2)*sqrt(pi)*( (erf(sqrt(2)*(a + b)/2) + 1)*exp(-a**2/2 + a*b - b**2/2))/4) e = integrate(exp(-(x - a)**2)*exp(-(x - b)**2)*exp(c*x), (x, 0, oo), meijerg=True) assert simplify(e) == ( sqrt(2)*sqrt(pi)*(erf(sqrt(2)*(2*a + 2*b + c)/4) + 1)*exp(-a**2 - b**2 + (2*a + 2*b + c)**2/8)/4) assert simplify(integrate(exp(-(x - a - b - c)**2), (x, 0, oo), meijerg=True)) == \ sqrt(pi)/2*(1 + erf(a + b + c)) assert simplify(integrate(exp(-(x + a + b + c)**2), (x, 0, oo), meijerg=True)) == \ sqrt(pi)/2*(1 - erf(a + b + c))
def test_combsimp_gamma(): R = Rational assert combsimp(gamma(x)) == gamma(x) assert combsimp(gamma(x + 1)/x) == gamma(x) assert combsimp(gamma(x)/(x - 1)) == gamma(x - 1) assert combsimp(x*gamma(x)) == gamma(x + 1) assert combsimp((x + 1)*gamma(x + 1)) == gamma(x + 2) assert combsimp(gamma(x + y)*(x + y)) == gamma(x + y + 1) assert combsimp(x/gamma(x + 1)) == 1/gamma(x) assert combsimp((x + 1)**2/gamma(x + 2)) == (x + 1)/gamma(x + 1) assert combsimp(x*gamma(x) + gamma(x + 3)/(x + 2)) == \ (x + 2)*gamma(x + 1) assert combsimp(gamma(2*x)*x) == gamma(2*x + 1)/2 assert combsimp(gamma(2*x)/(x - Rational(1, 2))) == 2*gamma(2*x - 1) assert combsimp(gamma(x)*gamma(1 - x)) == pi/sin(pi*x) assert combsimp(gamma(x)*gamma(-x)) == -pi/(x*sin(pi*x)) assert combsimp(1/gamma(x + 3)/gamma(1 - x)) == \ sin(pi*x)/(pi*x*(x + 1)*(x + 2)) assert powsimp(combsimp( gamma(x)*gamma(x + Rational(1, 2))*gamma(y)/gamma(x + y))) == \ 2**(-2*x + 1)*sqrt(pi)*gamma(2*x)*gamma(y)/gamma(x + y) assert combsimp(1/gamma(x)/gamma(x - Rational(1, 3))/gamma(x + Rational(1, 3))) == \ 3**(3*x - Rational(3, 2))/(2*pi*gamma(3*x - 1)) assert simplify( gamma(Rational(1, 2) + x/2)*gamma(1 + x/2)/gamma(1 + x)/sqrt(pi)*2**x) == 1 assert combsimp(gamma(Rational(-1, 4))*gamma(Rational(-3, 4))) == 16*sqrt(2)*pi/3 assert powsimp(combsimp(gamma(2*x)/gamma(x))) == \ 2**(2*x - 1)*gamma(x + Rational(1, 2))/sqrt(pi) # issue sympy/sympy#6792 e = (-gamma(k)*gamma(k + 2) + gamma(k + 1)**2)/gamma(k)**2 assert combsimp(e) == -k assert combsimp(1/e) == -1/k e = (gamma(x) + gamma(x + 1))/gamma(x) assert combsimp(e) == x + 1 assert combsimp(1/e) == 1/(x + 1) e = (gamma(x) + gamma(x + 2))*(gamma(x - 1) + gamma(x))/gamma(x) assert combsimp(e) == (x**2 + x + 1)*gamma(x + 1)/(x - 1) e = (-gamma(k)*gamma(k + 2) + gamma(k + 1)**2)/gamma(k)**2 assert combsimp(e**2) == k**2 assert combsimp(e**2/gamma(k + 1)) == k/gamma(k) a = R(1, 2) + R(1, 3) b = a + R(1, 3) assert combsimp(gamma(2*k)/gamma(k)*gamma(k + a)*gamma(k + b)) 3*2**(2*k + 1)*3**(-3*k - 2)*sqrt(pi)*gamma(3*k + R(3, 2))/2 A, B = symbols('A B', commutative=False) assert combsimp(e*B*A) == combsimp(e)*B*A # check iteration assert combsimp(gamma(2*k)/gamma(k)*gamma(-k - R(1, 2))) == ( -2**(2*k + 1)*sqrt(pi)/(2*((2*k + 1)*cos(pi*k)))) assert combsimp( gamma(k)*gamma(k + R(1, 3))*gamma(k + R(2, 3))/gamma(3*k/2)) == ( 3*2**(3*k + 1)*3**(-3*k - Rational(1, 2))*sqrt(pi)*gamma(3*k/2 + Rational(1, 2))/2)
def test_sympyissue_from_PR1599(): n1, n2, n3, n4 = symbols('n1 n2 n3 n4', negative=True) assert simplify(I*sqrt(n1)) == -sqrt(-n1) assert (powsimp(sqrt(n1)*sqrt(n2)*sqrt(n3)) == -I*sqrt(-n1)*sqrt(-n2)*sqrt(-n3)) assert (powsimp(root(n1, 3)*root(n2, 3)*root(n3, 3)*root(n4, 3)) == -cbrt(-1)*cbrt(-n1)*cbrt(-n2)*cbrt(-n3)*cbrt(-n4))
def test_sympyissue_6367(): z = -5*sqrt(2)/(2*sqrt(2*sqrt(29) + 29)) + sqrt(-sqrt(29)/29 + Rational(1, 2)) assert Mul(*[powsimp(a) for a in Mul.make_args(z.normal())]) == 0 assert powsimp(z.normal()) == 0 assert simplify(z) == 0 assert powsimp(sqrt(2 + sqrt(3))*sqrt(2 - sqrt(3)) + 1) == 2 assert powsimp(z) != 0
def test_single_normal(): mu = Symbol('mu', real=True) sigma = Symbol('sigma', real=True, positive=True) X = Normal('x', 0, 1) Y = X*sigma + mu assert simplify(E(Y)) == mu assert simplify(variance(Y)) == sigma**2 pdf = density(Y) x = Symbol('x') assert (pdf(x) == sqrt(2)*exp(-(x - mu)**2/(2*sigma**2))/(2*sqrt(pi)*sigma)) assert P(X**2 < 1) == erf(sqrt(2)/2) assert E(X, Eq(X, mu)) == mu
def test_discreteuniform(): # Symbolic a, b, c = symbols('a b c') X = DiscreteUniform('X', [a, b, c]) assert X.pspace.distribution.pdf(a) == Rational(1, 3) assert X.pspace.distribution.pdf(p) == 0 assert E(X) == (a + b + c)/3 assert simplify(variance(X) - ((a**2 + b**2 + c**2)/3 - (a/3 + b/3 + c/3)**2)) == 0 assert P(Eq(X, a)) == P(Eq(X, b)) == P(Eq(X, c)) == Rational(1, 3) Y = DiscreteUniform('Y', range(-5, 5)) # Numeric assert E(Y) == -Rational(1, 2) assert variance(Y) == Rational(33, 4) for x in range(-5, 5): assert P(Eq(Y, x)) == Rational(1, 10) assert P(Y <= x) == Rational(x + 6, 10) assert P(Y >= x) == Rational(5 - x, 10) assert dict(density(Die('D', 6)).items()) == \ dict(density(DiscreteUniform('U', range(1, 7))).items())
def test_hypersum(): assert simplify(summation(x**n/fac(n), (n, 1, oo))) == -1 + exp(x) assert summation((-1)**n * x**(2*n) / fac(2*n), (n, 0, oo)) == cos(x) assert simplify(summation((-1)**n*x**(2*n + 1) / factorial(2*n + 1), (n, 3, oo))) == -x + sin(x) + x**3/6 - x**5/120 assert summation(1/(n + 2)**3, (n, 1, oo)) == -Rational(9, 8) + zeta(3) assert summation(1/n**4, (n, 1, oo)) == pi**4/90 s = summation(x**n*n, (n, -oo, 0)) assert s.is_Piecewise assert s.args[0].args[0] == -1/(x*(1 - 1/x)**2) assert s.args[0].args[1] == (abs(1/x) < 1) m = Symbol('n', integer=True, positive=True) assert summation(binomial(m, k), (k, 0, m)) == 2**m
def test_sympyissue_11526(): df = diff(1/(a*log((x - b)/(x - c))), x) res = -1/(-a*c + a*b) assert limit(df, x, oo) == res assert limit(simplify(df), x, oo) == res e = log((1/x - b)/(1/x - c)) assert e.as_leading_term(x) == x*(c - b)
def test_bessel(): assert simplify(integrate(besselj(a, z)*besselj(b, z)/z, (z, 0, oo), meijerg=True, conds='none')) == \ 2*sin(pi*(a/2 - b/2))/(pi*(a - b)*(a + b)) assert simplify(integrate(besselj(a, z)*besselj(a, z)/z, (z, 0, oo), meijerg=True, conds='none')) == 1/(2*a) # TODO more orthogonality integrals assert simplify(integrate(sin(z*x)*(x**2 - 1)**(-(y + Rational(1, 2))), (x, 1, oo), meijerg=True, conds='none') * 2/((z/2)**y*sqrt(pi)*gamma(Rational(1, 2) - y))) == \ besselj(y, z) # Werner Rosenheinrich # SOME INDEFINITE INTEGRALS OF BESSEL FUNCTIONS assert integrate(x*besselj(0, x), x, meijerg=True) == x*besselj(1, x) assert integrate(x*besseli(0, x), x, meijerg=True) == x*besseli(1, x) # TODO can do higher powers, but come out as high order ... should they be # reduced to order 0, 1? assert integrate(besselj(1, x), x, meijerg=True) == -besselj(0, x) assert integrate(besselj(1, x)**2/x, x, meijerg=True) == \ -(besselj(0, x)**2 + besselj(1, x)**2)/2 # TODO more besseli when tables are extended or recursive mellin works assert integrate(besselj(0, x)**2/x**2, x, meijerg=True) == \ -2*x*besselj(0, x)**2 - 2*x*besselj(1, x)**2 \ + 2*besselj(0, x)*besselj(1, x) - besselj(0, x)**2/x assert integrate(besselj(0, x)*besselj(1, x), x, meijerg=True) == \ -besselj(0, x)**2/2 assert integrate(x**2*besselj(0, x)*besselj(1, x), x, meijerg=True) == \ x**2*besselj(1, x)**2/2 assert integrate(besselj(0, x)*besselj(1, x)/x, x, meijerg=True) == \ (x*besselj(0, x)**2 + x*besselj(1, x)**2 - besselj(0, x)*besselj(1, x)) # TODO how does besselj(0, a*x)*besselj(0, b*x) work? # TODO how does besselj(0, x)**2*besselj(1, x)**2 work? # TODO sin(x)*besselj(0, x) etc come out a mess # TODO can x*log(x)*besselj(0, x) be done? # TODO how does besselj(1, x)*besselj(0, x+a) work? # TODO more indefinite integrals when struve functions etc are implemented # test a substitution assert integrate(besselj(1, x**2)*x, x, meijerg=True) == \ -besselj(0, x**2)/2
def test_sympyissue_4661(): eq = -4*sin(x)**4 + 4*cos(x)**4 - 8*cos(x)**2 assert trigsimp(eq) == -4 n = sin(x)**6 + 4*sin(x)**4*cos(x)**2 + 5*sin(x)**2*cos(x)**4 + 2*cos(x)**6 d = -sin(x)**2 - 2*cos(x)**2 assert simplify(n/d) == -1 assert trigsimp(-2*cos(x)**2 + cos(x)**4 - sin(x)**4) == -1 eq = (- sin(x)**3/4)*cos(x) + (cos(x)**3/4)*sin(x) - sin(2*x)*cos(2*x)/8 assert trigsimp(eq) == 0
def test_gosper_sum_AeqB_part2(): f2a = n**2*a**n f2b = (n - r/2)*binomial(r, n) f2c = factorial(n - 1)**2/(factorial(n - x)*factorial(n + x)) g2a = -a*(a + 1)/(a - 1)**3 + a**( m + 1)*(a**2*m**2 - 2*a*m**2 + m**2 - 2*a*m + 2*m + a + 1)/(a - 1)**3 g2b = (m - r)*binomial(r, m)/2 ff = factorial(1 - x)*factorial(1 + x) g2c = 1/ff*( 1 - 1/x**2) + factorial(m)**2/(x**2*factorial(m - x)*factorial(m + x)) g = gosper_sum(f2a, (n, 0, m)) assert g is not None and simplify(g - g2a) == 0 g = gosper_sum(f2b, (n, 0, m)) assert g is not None and simplify(g - g2b) == 0 g = gosper_sum(f2c, (n, 1, m)) assert g is not None and simplify(g - g2c) == 0
def test_maxwell(): a = Symbol("a", positive=True) X = Maxwell('x', a) assert density(X)(x) == (sqrt(2)*x**2*exp(-x**2/(2*a**2)) / (sqrt(pi)*a**3)) assert E(X) == 2*sqrt(2)*a/sqrt(pi) assert simplify(variance(X)) == a**2*(-8 + 3*pi)/pi
def test_gosper_nan(): a = Symbol('a', positive=True) b = Symbol('b', positive=True) n = Symbol('n', integer=True) m = Symbol('m', integer=True) f2d = n*(n + a + b)*a**n*b**n/(factorial(n + a)*factorial(n + b)) g2d = 1/(factorial(a - 1)*factorial( b - 1)) - a**(m + 1)*b**(m + 1)/(factorial(a + m)*factorial(b + m)) g = gosper_sum(f2d, (n, 0, m)) assert simplify(g - g2d) == 0
def test_exptrigsimp(): def valid(a, b): if not (tn(a, b) and a == b): return False return True assert exptrigsimp(exp(x) + exp(-x)) == 2*cosh(x) assert exptrigsimp(exp(x) - exp(-x)) == 2*sinh(x) e = [cos(x) + I*sin(x), cos(x) - I*sin(x), cosh(x) - sinh(x), cosh(x) + sinh(x)] ok = [exp(I*x), exp(-I*x), exp(-x), exp(x)] assert all(valid(i, j) for i, j in zip( [exptrigsimp(ei) for ei in e], ok)) ue = [cos(x) + sin(x), cos(x) - sin(x), cosh(x) + I*sinh(x), cosh(x) - I*sinh(x)] assert [exptrigsimp(ei) == ei for ei in ue] res = [] ok = [y*tanh(1), 1/(y*tanh(1)), I*y*tan(1), -I/(y*tan(1)), y*tanh(x), 1/(y*tanh(x)), I*y*tan(x), -I/(y*tan(x)), y*tanh(1 + I), 1/(y*tanh(1 + I))] for a in (1, I, x, I*x, 1 + I): w = exp(a) eq = y*(w - 1/w)/(w + 1/w) s = simplify(eq) assert s == exptrigsimp(eq) res.append(s) sinv = simplify(1/eq) assert sinv == exptrigsimp(1/eq) res.append(sinv) assert all(valid(i, j) for i, j in zip(res, ok)) for a in range(1, 3): w = exp(a) e = w + 1/w s = simplify(e) assert s == exptrigsimp(e) assert valid(s, 2*cosh(a)) e = w - 1/w s = simplify(e) assert s == exptrigsimp(e) assert valid(s, 2*sinh(a))