def test_Mul_is_infinite(): x = Symbol('x') f = Symbol('f', finite=True) i = Symbol('i', infinite=True) z = Dummy(zero=True) nzf = Dummy(finite=True, zero=False) assert (x*f).is_finite is None assert (x*i).is_finite is None assert (f*i).is_finite is False assert (x*f*i).is_finite is None assert (z*i).is_finite is False assert (nzf*i).is_finite is False assert (z*f).is_finite is True assert Mul(0, f, evaluate=False).is_finite is True assert Mul(0, i, evaluate=False).is_finite is False assert (x*f).is_infinite is None assert (x*i).is_infinite is None assert (f*i).is_infinite is None assert (x*f*i).is_infinite is None assert (z*i).is_infinite is nan.is_infinite assert (nzf*i).is_infinite is True assert (z*f).is_infinite is False assert Mul(0, f, evaluate=False).is_infinite is False assert Mul(0, i, evaluate=False).is_infinite is nan.is_infinite
def test_cholesky_solve(): x, y, z = Dummy('x'), Dummy('y'), Dummy('z') assert cholesky_solve( [[QQ(25), QQ(15), QQ(-5)], [QQ(15), QQ(18), QQ(0)], [QQ(-5), QQ(0), QQ(11)]], [[x], [y], [z]], [[QQ(2)], [QQ(3)], [QQ(1)]], QQ) == [[QQ(-1, 225)], [QQ(23, 135)], [QQ(4, 45)]]
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 reflect(self, line): from diofant import atan, Point, Dummy, oo g = self l = line o = Point(0, 0) if l.slope == 0: y = l.args[0].y if not y: # x-axis return g.scale(y=-1) reps = [(p, p.translate(y=2 * (y - p.y))) for p in g.atoms(Point)] elif l.slope == oo: x = l.args[0].x if not x: # y-axis return g.scale(x=-1) reps = [(p, p.translate(x=2 * (x - p.x))) for p in g.atoms(Point)] else: if not hasattr(g, 'reflect') and not all( isinstance(arg, Point) for arg in g.args): raise NotImplementedError( 'reflect undefined or non-Point args in %s' % g) a = atan(l.slope) c = l.coefficients d = -c[-1] / c[1] # y-intercept # apply the transform to a single point x, y = Dummy(), Dummy() xf = Point(x, y) xf = xf.translate(y=-d).rotate(-a, o).scale(y=-1).rotate( a, o).translate(y=d) # replace every point using that transform reps = [(p, xf.xreplace({x: p.x, y: p.y})) for p in g.atoms(Point)] return g.xreplace(dict(reps))
def ratint_ratpart(f, g, x): """ Horowitz-Ostrogradsky algorithm. Given a field K and polynomials f and g in K[x], such that f and g are coprime and deg(f) < deg(g), returns fractions A and B in K(x), such that f/g = A' + B and B has square-free denominator. Examples ======== >>> from diofant.integrals.rationaltools import ratint_ratpart >>> from diofant.abc import x, y >>> from diofant import Poly >>> ratint_ratpart(Poly(1, x, domain='ZZ'), ... Poly(x + 1, x, domain='ZZ'), x) (0, 1/(x + 1)) >>> ratint_ratpart(Poly(1, x, domain='EX'), ... Poly(x**2 + y**2, x, domain='EX'), x) (0, 1/(x**2 + y**2)) >>> ratint_ratpart(Poly(36, x, domain='ZZ'), ... Poly(x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2, x, domain='ZZ'), x) ((12*x + 6)/(x**2 - 1), 12/(x**2 - x - 2)) See Also ======== diofant.integrals.rationaltools.ratint diofant.integrals.rationaltools.ratint_logpart """ from diofant import solve f = Poly(f, x) g = Poly(g, x) u, v, _ = g.cofactors(g.diff()) n = u.degree() m = v.degree() A_coeffs = [Dummy('a' + str(n - i)) for i in range(0, n)] B_coeffs = [Dummy('b' + str(m - i)) for i in range(0, m)] C_coeffs = A_coeffs + B_coeffs A = Poly(A_coeffs, x, domain=ZZ[C_coeffs]) B = Poly(B_coeffs, x, domain=ZZ[C_coeffs]) H = f - A.diff() * v + A * (u.diff() * v).quo(u) - B * u result = solve(H.coeffs(), C_coeffs) A = A.as_expr().subs(result) B = B.as_expr().subs(result) rat_part = cancel(A / u.as_expr(), x) log_part = cancel(B / v.as_expr(), x) return rat_part, log_part
def test_LU_solve(): x, y, z = Dummy('x'), Dummy('y'), Dummy('z') assert LU_solve( [[QQ(2), QQ(-1), QQ(-2)], [QQ(-4), QQ(6), QQ(3)], [QQ(-4), QQ(-2), QQ(8)]], [[x], [y], [z]], [[QQ(-1)], [QQ(13)], [QQ(-6)]], QQ) == [[QQ(2, 1)], [QQ(3, 1)], [QQ(1, 1)]]
def test_gamma(): assert Hyper_Function([2, 3], [-1]).gamma == 0 assert Hyper_Function([-2, -3], [-1]).gamma == 2 n = Dummy(integer=True) assert Hyper_Function([-1, n, 1], []).gamma == 1 assert Hyper_Function([-1, -n, 1], []).gamma == 1 p = Dummy(integer=True, positive=True) assert Hyper_Function([-1, p, 1], []).gamma == 1 assert Hyper_Function([-1, -p, 1], []).gamma == 2
def _intersect(self, other): from diofant import Dummy from diofant.solvers.diophantine import diophantine from diofant.sets.sets import imageset if self.base_set is S.Integers: if isinstance(other, ImageSet) and other.base_set is S.Integers: f, g = self.lamda.expr, other.lamda.expr n, m = self.lamda.variables[0], other.lamda.variables[0] # Diophantine sorts the solutions according to the alphabetic # order of the variable names, since the result should not depend # on the variable name, they are replaced by the dummy variables # below a, b = Dummy('a'), Dummy('b') f, g = f.subs(n, a), g.subs(m, b) solns_set = diophantine(f - g) if solns_set == set(): return EmptySet() solns = list(diophantine(f - g)) if len(solns) == 1: t = list(solns[0][0].free_symbols)[0] else: return # since 'a' < 'b' return imageset(Lambda(t, f.subs(a, solns[0][0])), S.Integers) if other == S.Reals: from diofant.solvers.diophantine import diophantine from diofant.core.function import expand_complex if len(self.lamda.variables ) > 1 or self.base_set is not S.Integers: return f = self.lamda.expr n = self.lamda.variables[0] n_ = Dummy(n.name, integer=True) f_ = f.subs(n, n_) re, im = f_.as_real_imag() im = expand_complex(im) sols = list(diophantine(im, n_)) if not sols: return S.EmptySet elif all(s[0].has(n_) is False for s in sols): s = FiniteSet(*[s[0] for s in sols]) elif len(sols) == 1 and sols[0][0].has(n_): s = imageset(Lambda(n_, sols[0][0]), S.Integers) else: return return imageset(Lambda(n_, re), self.base_set.intersect(s))
def test_sympyissue_7068(): f = Function('f') y1 = Dummy('y') y2 = Dummy('y') func1 = f(a + y1 * b) func2 = f(a + y2 * b) func1_y = func1.diff(y1) func2_y = func2.diff(y2) assert func1_y != func2_y z1 = Subs(f(a), (a, y1)) z2 = Subs(f(a), (a, y2)) assert z1 != z2
def test_sympyissue_7068(): from diofant.abc import a, b f = Function('f') y1 = Dummy('y') y2 = Dummy('y') func1 = f(a + y1 * b) func2 = f(a + y2 * b) func1_y = func1.diff(y1) func2_y = func2.diff(y2) assert func1_y != func2_y z1 = Subs(f(a), a, y1) z2 = Subs(f(a), a, y2) assert z1 != z2
def test_dummy_eq(): x = Symbol('x') y = Symbol('y') u = Dummy('u') assert (u**2 + 1).dummy_eq(x**2 + 1) is True assert ((u**2 + 1) == (x**2 + 1)) is False assert (u**2 + y).dummy_eq(x**2 + y, x) is True assert (u**2 + y).dummy_eq(x**2 + y, y) is False assert (x**2).dummy_eq(x**2 + 1) is False assert u.dummy_eq(1) is False
def test_core_symbol(): # make the Symbol a unique name that doesn't class with any other # testing variable in this file since after this test the symbol # having the same name will be cached as noncommutative for c in (Dummy, Dummy('x', commutative=False), Symbol, Symbol('_sympyissue_6229', commutative=False), Wild, Wild('x')): check(c)
def test_Add_is_pos_neg(): # these cover lines not covered by the rest of tests in core n = Symbol('n', negative=True, infinite=True) nn = Symbol('n', nonnegative=True, infinite=True) np = Symbol('n', nonpositive=True, infinite=True) p = Symbol('p', positive=True, infinite=True) r = Dummy(extended_real=True, finite=False) x = Symbol('x') xf = Symbol('xb', finite=True, real=True) assert (n + p).is_positive is None assert (n + x).is_positive is None assert (p + x).is_positive is None assert (n + p).is_negative is None assert (n + x).is_negative is None assert (p + x).is_negative is None assert (n + xf).is_positive is False assert (p + xf).is_positive is True assert (n + xf).is_negative is True assert (p + xf).is_negative is False assert (x - oo).is_negative is None # issue sympy/sympy#7798 # issue sympy/sympy#8046, 16.2 assert (p + nn).is_positive assert (n + np).is_negative assert (p + r).is_positive is None
def test_plane2(): p1 = Plane(Point3D(0, 0, 0), normal_vector=(1, 0, 0)) p2 = Plane(Point3D(0, 0, 0), normal_vector=(1, 0, -1)) assert p1.intersection(p2) == [Line3D(Point3D(0, 0, 0), Point3D(0, 1, 0))] p1 = Plane(Point3D(0, 0, 1), normal_vector=(0, -5, 3)) p2 = Plane(Point3D(0, 0, 2), normal_vector=(0, -5, -3)) assert p1.intersection(p2) == [ Line3D(Point3D(0, 3 / 10, 3 / 2), Point3D(30, 3 / 10, 3 / 2)) ] p1 = Point3D(0, 0, 0) p2 = Point3D(1, 1, 1) p3 = Point3D(1, 2, 3) pl3 = Plane(p1, p2, p3) # get a segment that does not intersect the plane which is also # parallel to pl3's normal veector t = Dummy() r = pl3.random_point() a = pl3.perpendicular_line(r).arbitrary_point(t) s = Segment3D(a.subs({t: 1}), a.subs({t: 2})) assert s.p1 not in pl3 and s.p2 not in pl3 a = Plane(Point3D(5, 0, 0), normal_vector=(1, -1, 1)) b = Plane(Point3D(0, -2, 0), normal_vector=(3, 1, 1)) c = Plane(Point3D(0, -1, 0), normal_vector=(5, -1, 9)) assert Plane.are_concurrent(a, b) is True assert Plane.are_concurrent(a, b, c) is False p = Plane((0, 0, 0), (0, 0, 1), (0, 1, 0)) assert p.arbitrary_point(t) == Point3D(0, cos(t), sin(t))
def test_apart_list(): assert apart_list(1) == 1 w0, w1, w2 = Symbol('w0'), Symbol('w1'), Symbol('w2') _a = Dummy('a') f = (-2*x - 2*x**2) / (3*x**2 - 6*x) assert (apart_list(f, x, dummies=numbered_symbols('w')) == (-1, Poly(Rational(2, 3), x), [(Poly(w0 - 2, w0), Lambda(_a, 2), Lambda(_a, -_a + x), 1)])) assert (apart_list(2/(x**2-2), x, dummies=numbered_symbols('w')) == (1, Poly(0, x), [(Poly(w0**2 - 2, w0), Lambda(_a, _a/2), Lambda(_a, -_a + x), 1)])) f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2) assert (apart_list(f, x, dummies=numbered_symbols('w')) == (1, Poly(0, x), [(Poly(w0 - 2, w0), Lambda(_a, 4), Lambda(_a, -_a + x), 1), (Poly(w1**2 - 1, w1), Lambda(_a, -3*_a - 6), Lambda(_a, -_a + x), 2), (Poly(w2 + 1, w2), Lambda(_a, -4), Lambda(_a, -_a + x), 1)])) f = 1/(2*(x - 1)**2) assert (apart_list(f, x, dummies=numbered_symbols('w')) == (1, Poly(0, x), [(Poly(2, w0), Lambda(_a, 0), Lambda(_a, x - _a), 1), (Poly(w1 - 1, w1), Lambda(_a, Rational(1, 2)), Lambda(_a, x - _a), 2), (Poly(1, w2), Lambda(_a, 0), Lambda(_a, x - _a), 1)]))
def test_harmonic_rewrite_sum_fail(): n = Symbol('n') m = Symbol('m') _k = Dummy('k') assert harmonic(n).rewrite(Sum) == Sum(1 / _k, (_k, 1, n)) assert harmonic(n, m).rewrite(Sum) == Sum(_k**(-m), (_k, 1, n))
def test_harmonic_rewrite_sum_fail(): n = Symbol("n") m = Symbol("m") _k = Dummy("k") assert harmonic(n).rewrite(Sum) == Sum(1 / _k, (_k, 1, n)) assert harmonic(n, m).rewrite(Sum) == Sum(_k**(-m), (_k, 1, n))
def test_jacobi(): assert jacobi(0, a, b, x) == 1 assert jacobi(1, a, b, x) == a / 2 - b / 2 + x * (a / 2 + b / 2 + 1) assert (jacobi(2, a, b, x) == a**2 / 8 - a * b / 4 - a / 8 + b**2 / 8 - b / 8 + x**2 * (a**2 / 8 + a * b / 4 + 7 * a / 8 + b**2 / 8 + 7 * b / 8 + Rational(3, 2)) + x * (a**2 / 4 + 3 * a / 4 - b**2 / 4 - 3 * b / 4) - S.Half) assert jacobi(n, a, a, x) == RisingFactorial(a + 1, n) * gegenbauer( n, a + Rational(1, 2), x) / RisingFactorial(2 * a + 1, n) assert jacobi(n, a, -a, x) == ((-1)**a * (-x + 1)**(-a / 2) * (x + 1)**(a / 2) * assoc_legendre(n, a, x) * factorial(-a + n) * gamma(a + n + 1) / (factorial(a + n) * gamma(n + 1))) assert jacobi(n, -b, b, x) == ((-x + 1)**(b / 2) * (x + 1)**(-b / 2) * assoc_legendre(n, b, x) * gamma(-b + n + 1) / gamma(n + 1)) assert jacobi(n, 0, 0, x) == legendre(n, x) assert jacobi(n, S.Half, S.Half, x) == RisingFactorial(Rational( 3, 2), n) * chebyshevu(n, x) / factorial(n + 1) assert jacobi(n, -S.Half, -S.Half, x) == RisingFactorial( Rational(1, 2), n) * chebyshevt(n, x) / factorial(n) X = jacobi(n, a, b, x) assert isinstance(X, jacobi) assert jacobi(n, a, b, -x) == (-1)**n * jacobi(n, b, a, x) assert jacobi(n, a, b, 0) == 2**(-n) * gamma(a + n + 1) * hyper( (-b - n, -n), (a + 1, ), -1) / (factorial(n) * gamma(a + 1)) assert jacobi(n, a, b, 1) == RisingFactorial(a + 1, n) / factorial(n) m = Symbol("m", positive=True) assert jacobi(m, a, b, oo) == oo * RisingFactorial(a + b + m + 1, m) assert conjugate(jacobi(m, a, b, x)) == \ jacobi(m, conjugate(a), conjugate(b), conjugate(x)) assert diff(jacobi(n, a, b, x), n) == Derivative(jacobi(n, a, b, x), n) assert diff(jacobi(n, a, b, x), x) == \ (a/2 + b/2 + n/2 + Rational(1, 2))*jacobi(n - 1, a + 1, b + 1, x) # XXX see issue sympy/sympy#5539 assert str(jacobi(n, a, b, x).diff(a)) == \ ("Sum((jacobi(n, a, b, x) + (a + b + 2*_k + 1)*RisingFactorial(b + " "_k + 1, n - _k)*jacobi(_k, a, b, x)/((n - _k)*RisingFactorial(a + " "b + _k + 1, n - _k)))/(a + b + n + _k + 1), (_k, 0, n - 1))") assert str(jacobi(n, a, b, x).diff(b)) == \ ("Sum(((-1)**(n - _k)*(a + b + 2*_k + 1)*RisingFactorial(a + " "_k + 1, n - _k)*jacobi(_k, a, b, x)/((n - _k)*RisingFactorial(a + " "b + _k + 1, n - _k)) + jacobi(n, a, b, x))/(a + b + n + " "_k + 1), (_k, 0, n - 1))") assert jacobi_normalized(n, a, b, x) == \ (jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1) / ((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1)))) pytest.raises(ValueError, lambda: jacobi(-2.1, a, b, x)) pytest.raises(ValueError, lambda: jacobi(Dummy(positive=True, integer=True), 1, 2, oo)) pytest.raises(ArgumentIndexError, lambda: jacobi(n, a, b, x).fdiff(5))
def test_hyper_as_trig(): from diofant.simplify.fu import _osborne as o, _osbornei as i, TR12 eq = sinh(x)**2 + cosh(x)**2 t, f = hyper_as_trig(eq) assert f(fu(t)) == cosh(2*x) e, f = hyper_as_trig(tanh(x + y)) assert f(TR12(e)) == (tanh(x) + tanh(y))/(tanh(x)*tanh(y) + 1) d = Dummy() assert o(sinh(x), d) == I*sin(x*d) assert o(tanh(x), d) == I*tan(x*d) assert o(coth(x), d) == cot(x*d)/I assert o(cosh(x), d) == cos(x*d) for func in (sinh, cosh, tanh, coth): h = func(pi) assert i(o(h, d), d) == h # /!\ the _osborne functions are not meant to work # in the o(i(trig, d), d) direction so we just check # that they work as they are supposed to work assert i(cos(x*y), y) == cosh(x) assert i(sin(x*y), y) == sinh(x)/I assert i(tan(x*y), y) == tanh(x)/I assert i(cot(x*y), y) == coth(x)*I assert i(sec(x*y), y) == 1/cosh(x) assert i(csc(x*y), y) == I/sinh(x)
def test_hyper_rewrite_sum(): _k = Dummy("k") assert replace_dummy(hyper((1, 2), (1, 3), x).rewrite(Sum), _k) == \ Sum(x**_k / factorial(_k) * RisingFactorial(2, _k) / RisingFactorial(3, _k), (_k, 0, oo)) assert hyper((1, 2, 3), (-1, 3), z).rewrite(Sum) == \ hyper((1, 2, 3), (-1, 3), z)
def test_assemble_partfrac_list(): f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2) pfd = apart_list(f) assert assemble_partfrac_list(pfd) == -4/(x + 1) - 3/(x + 1)**2 - 9/(x - 1)**2 + 4/(x - 2) a = Dummy('a') pfd = (1, Poly(0, x, domain='ZZ'), [([sqrt(2), -sqrt(2)], Lambda(a, a/2), Lambda(a, -a + x), 1)]) assert assemble_partfrac_list(pfd) == -1/(sqrt(2)*(x + sqrt(2))) + 1/(sqrt(2)*(x - sqrt(2)))
def test_basic(): assert lambdarepr(x * y) == "x*y" assert lambdarepr(x + y) in ["y + x", "x + y"] assert lambdarepr(x**y) == "x**y" assert lambdarepr(And(x, y)) == "((x) and (y))" assert lambdarepr(Or(x, y)) == "((x) or (y))" assert lambdarepr(Not(x)) == "(not (x))" assert lambdarepr(false) == "False" assert lambdarepr(Dummy('f(x)')) == "_f_lpar_x_rpar_"
def test_harmonic_rewrite_sum(): n = Symbol('n') m = Symbol('m') _k = Dummy('k') assert replace_dummy(harmonic(n).rewrite(Sum), _k) == Sum(1 / _k, (_k, 1, n)) assert replace_dummy(harmonic(n, m).rewrite(Sum), _k) == Sum(_k**(-m), (_k, 1, n))
def test_harmonic_rewrite_sum(): n = Symbol("n") m = Symbol("m") _k = Dummy("k") assert replace_dummy(harmonic(n).rewrite(Sum), _k) == Sum(1 / _k, (_k, 1, n)) assert replace_dummy(harmonic(n, m).rewrite(Sum), _k) == Sum(_k**(-m), (_k, 1, n))
def test_basic(): assert lambdarepr(x * y) == 'x*y' assert lambdarepr(x + y) in ['y + x', 'x + y'] assert lambdarepr(x**y) == 'x**y' assert lambdarepr(And(x, y)) == '((x) and (y))' assert lambdarepr(Or(x, y)) == '((x) or (y))' assert lambdarepr(Not(x)) == '(not (x))' assert lambdarepr(false) == 'False' assert lambdarepr(Dummy('f(x)')) == '_f_lpar_x_rpar_'
def pdf(self, x): x = sympify(x) if x.is_number: if x.is_Integer and x >= 1 and x <= self.sides: return Rational(1, self.sides) return S.Zero if x.is_Symbol: i = Dummy('i', integer=True, positive=True) return Sum(KroneckerDelta(x, i) / self.sides, (i, 1, self.sides)) raise ValueError( "'x' expected as an argument of type 'number' or 'symbol', " "not %s" % (type(x)))
def test_Symbol(): a = Symbol("a") x1 = Symbol("x") x2 = Symbol("x") xdummy1 = Dummy("x") xdummy2 = Dummy("x") assert a != x1 assert a != x2 assert x1 == x2 assert x1 != xdummy1 assert xdummy1 != xdummy2 assert Symbol("x") == Symbol("x") assert Dummy("x") != Dummy("x") d = symbols('d', cls=Dummy) assert isinstance(d, Dummy) c, d = symbols('c,d', cls=Dummy) assert isinstance(c, Dummy) assert isinstance(d, Dummy) pytest.raises(TypeError, lambda: Symbol()) pytest.raises(TypeError, lambda: Symbol(1))
def _minpoly_pow(ex, pw, x, dom, mp=None): """ Returns ``minpoly(ex**pw, x)`` Parameters ========== ex : algebraic element pw : rational number x : indeterminate of the polynomial dom: ground domain mp : minimal polynomial of ``p`` Examples ======== >>> from diofant import sqrt, QQ, Rational >>> from diofant.abc import x, y >>> p = sqrt(1 + sqrt(2)) >>> _minpoly_pow(p, 2, x, QQ) x**2 - 2*x - 1 >>> minpoly(p**2, x) x**2 - 2*x - 1 >>> _minpoly_pow(y, Rational(1, 3), x, QQ.frac_field(y)) x**3 - y >>> minpoly(y**Rational(1, 3), x) x**3 - y """ pw = sympify(pw) if not mp: mp = _minpoly_compose(ex, x, dom) if not pw.is_rational: raise NotAlgebraic("%s doesn't seem to be an algebraic element" % ex) if pw < 0: if mp == x: raise ZeroDivisionError('%s is zero' % ex) mp = _invertx(mp, x) if pw == -1: return mp pw = -pw ex = 1 / ex y = Dummy(str(x)) mp = mp.subs({x: y}) n, d = pw.as_numer_denom() res = Poly(resultant(mp, x**d - y**n, gens=[y]), x, domain=dom) _, factors = res.factor_list() res = _choose_factor(factors, x, ex**pw, dom) return res.as_expr()
def legendre_poly(n, x=None, **args): """Generates Legendre polynomial of degree `n` in `x`. """ if n < 0: raise ValueError("can't generate Legendre polynomial of degree %s" % n) poly = DMP(dup_legendre(int(n), QQ), QQ) if x is not None: poly = Poly.new(poly, x) else: poly = PurePoly.new(poly, Dummy('x')) if not args.get('polys', False): return poly.as_expr() else: return poly
def chebyshevt_poly(n, x=None, **args): """Generates Chebyshev polynomial of the first kind of degree `n` in `x`. """ if n < 0: raise ValueError( "can't generate 1st kind Chebyshev polynomial of degree %s" % n) poly = DMP(dup_chebyshevt(int(n), ZZ), ZZ) if x is not None: poly = Poly.new(poly, x) else: poly = PurePoly.new(poly, Dummy('x')) if not args.get('polys', False): return poly.as_expr() else: return poly