def test_nthroot1(): q = 1 + sqrt(2) + sqrt(3) + Rational(1, 10)**20 p = expand_multinomial(q**5) assert nthroot(p, 5) == q q = 1 + sqrt(2) + sqrt(3) + Rational(1, 10)**30 p = expand_multinomial(q**5) assert nthroot(p, 5) == q
def test_minimal_polynomial_sq(): p = expand_multinomial((1 + 5 * sqrt(2) + 2 * sqrt(3))**3) mp = minimal_polynomial(cbrt(p))(x) assert mp == x**4 - 4 * x**3 - 118 * x**2 + 244 * x + 1321 p = expand_multinomial((1 + sqrt(2) - 2 * sqrt(3) + sqrt(7))**3) mp = minimal_polynomial(cbrt(p))(x) assert mp == x**8 - 8 * x**7 - 56 * x**6 + 448 * x**5 + 480 * x**4 - 5056 * x**3 + 1984 * x**2 + 7424 * x - 3008 p = Add(*[sqrt(i) for i in range(1, 12)]) mp = minimal_polynomial(p)(x) assert mp.subs({x: 0}) == -71965773323122507776
def test_minimal_polynomial_sq(): p = expand_multinomial((1 + 5*sqrt(2) + 2*sqrt(3))**3) mp = minimal_polynomial(cbrt(p))(x) assert mp == x**4 - 4*x**3 - 118*x**2 + 244*x + 1321 p = expand_multinomial((1 + sqrt(2) - 2*sqrt(3) + sqrt(7))**3) mp = minimal_polynomial(cbrt(p))(x) assert mp == x**8 - 8*x**7 - 56*x**6 + 448*x**5 + 480*x**4 - 5056*x**3 + 1984*x**2 + 7424*x - 3008 p = Add(*[sqrt(i) for i in range(1, 12)]) mp = minimal_polynomial(p)(x) assert mp.subs({x: 0}) == -71965773323122507776
def test_sympyissues_5919_6830(): # issue sympy/sympy#5919 n = -1 + 1 / x z = n / x / (-n)**2 - 1 / n / x assert expand( z) == 1 / (x**2 - 2 * x + 1) - 1 / (x - 2 + 1 / x) - 1 / (-x + 1) # issue sympy/sympy#6830 p = (1 + x)**2 assert expand_multinomial( (1 + x * p)**2) == (x**2 * (x**4 + 4 * x**3 + 6 * x**2 + 4 * x + 1) + 2 * x * (x**2 + 2 * x + 1) + 1) assert expand_multinomial( (1 + (y + x) * p)**2) == (2 * ((x + y) * (x**2 + 2 * x + 1)) + (x**2 + 2 * x * y + y**2) * (x**4 + 4 * x**3 + 6 * x**2 + 4 * x + 1) + 1) A = Symbol('A', commutative=False) p = (1 + A)**2 assert expand_multinomial( (1 + x * p)**2) == (x**2 * (1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4) + 2 * x * (1 + 2 * A + A**2) + 1) assert expand_multinomial( (1 + (y + x) * p)**2) == ((x + y) * (1 + 2 * A + A**2) * 2 + (x**2 + 2 * x * y + y**2) * (1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4) + 1) assert expand_multinomial((1 + (y + x) * p)**3) == ( (x + y) * (1 + 2 * A + A**2) * 3 + (x**2 + 2 * x * y + y**2) * (1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4) * 3 + (x**3 + 3 * x**2 * y + 3 * x * y**2 + y**3) * (1 + 6 * A + 15 * A**2 + 20 * A**3 + 15 * A**4 + 6 * A**5 + A**6) + 1) # unevaluate powers eq = (Pow((x + 1) * ((A + 1)**2), 2, evaluate=False)) # - in this case the base is not an Add so no further # expansion is done assert expand_multinomial(eq) == \ (x**2 + 2*x + 1)*(1 + 4*A + 6*A**2 + 4*A**3 + A**4) # - but here, the expanded base *is* an Add so it gets expanded eq = (Pow(((A + 1)**2), 2, evaluate=False)) assert expand_multinomial(eq) == 1 + 4 * A + 6 * A**2 + 4 * A**3 + A**4 # coverage def ok(a, b, n): e = (a + I * b)**n return verify_numerically(e, expand_multinomial(e)) for a in [2, Rational(1, 2)]: for b in [3, Rational(1, 3)]: for n in range(2, 6): assert ok(a, b, n) e = (sin(x) + y)**3 assert (expand_multinomial(e.subs({y: O(x**4)})) == expand_multinomial(e).subs({y: O(x**4)}) == sin(x)**3 + O(x**6)) assert expand_multinomial(3**(x + y + 3)) == 27 * 3**(x + y)
def test_nthroot(): assert nthroot(90 + 34 * sqrt(7), 3) == sqrt(7) + 3 q = 1 + sqrt(2) - 2 * sqrt(3) + sqrt(6) + sqrt(7) assert nthroot(expand_multinomial(q**3), 3) == q assert nthroot(41 + 29 * sqrt(2), 5) == 1 + sqrt(2) assert nthroot(-41 - 29 * sqrt(2), 5) == -1 - sqrt(2) expr = 1320 * sqrt(10) + 4216 + 2576 * sqrt(6) + 1640 * sqrt(15) assert nthroot(expr, 5) == 1 + sqrt(6) + sqrt(15) q = 1 + sqrt(2) + sqrt(3) + sqrt(5) assert expand_multinomial(nthroot(expand_multinomial(q**5), 5)) == q q = 1 + sqrt(2) + 7 * sqrt(6) + 2 * sqrt(10) assert nthroot(expand_multinomial(q**5), 5, 8) == q q = 1 + sqrt(2) - 2 * sqrt(3) + 1171 * sqrt(6) assert nthroot(expand_multinomial(q**3), 3) == q assert nthroot(expand_multinomial(q**6), 6) == q
def ok(a, b, n): e = (a + I * b)**n return verify_numerically(e, expand_multinomial(e))
def test_expand_multinomial(): assert expand_multinomial((x + 1 + O(z))**2) == 1 + 2 * x + x**2 + O(z) assert expand_multinomial( (x + 1 + O(z))**3) == 1 + 3 * x + 3 * x**2 + x**3 + O(z)
def __new__(cls, expr, *args, **kwargs): expr = sympify(expr) if not args: if expr.is_Order: variables = expr.variables point = expr.point else: variables = list(expr.free_symbols) point = [S.Zero]*len(variables) else: args = list(args if is_sequence(args) else [args]) variables, point = [], [] if is_sequence(args[0]): for a in args: v, p = list(map(sympify, a)) variables.append(v) point.append(p) else: variables = list(map(sympify, args)) point = [S.Zero]*len(variables) if not all(isinstance(v, (Dummy, Symbol)) for v in variables): raise TypeError('Variables are not symbols, got %s' % variables) if len(list(uniq(variables))) != len(variables): raise ValueError('Variables are supposed to be unique symbols, got %s' % variables) if expr.is_Order: expr_vp = dict(expr.args[1:]) new_vp = dict(expr_vp) vp = dict(zip(variables, point)) for v, p in vp.items(): if v in new_vp.keys(): if p != new_vp[v]: raise NotImplementedError( "Mixing Order at different points is not supported.") else: new_vp[v] = p if set(expr_vp.keys()) == set(new_vp.keys()): return expr else: variables = list(new_vp.keys()) point = [new_vp[v] for v in variables] if expr is S.NaN: return S.NaN if any(x in p.free_symbols for x in variables for p in point): raise ValueError('Got %s as a point.' % point) if variables: if any(p != point[0] for p in point): raise NotImplementedError if point[0] in [S.Infinity, S.NegativeInfinity]: s = {k: 1/Dummy() for k in variables} rs = {1/v: 1/k for k, v in s.items()} elif point[0] is not S.Zero: s = {k: Dummy() + point[0] for k in variables} rs = {v - point[0]: k - point[0] for k, v in s.items()} else: s = () rs = () expr = expr.subs(s) if expr.is_Add: from diofant import expand_multinomial expr = expand_multinomial(expr) if s: args = tuple(r[0] for r in rs.items()) else: args = tuple(variables) if len(variables) > 1: # XXX: better way? We need this expand() to # workaround e.g: expr = x*(x + y). # (x*(x + y)).as_leading_term(x, y) currently returns # x*y (wrong order term!). That's why we want to deal with # expand()'ed expr (handled in "if expr.is_Add" branch below). expr = expr.expand() if expr.is_Add: lst = expr.extract_leading_order(args) expr = Add(*[f.expr for (e, f) in lst]) elif expr: expr = expr.as_leading_term(*args) expr = expr.as_independent(*args, as_Add=False)[1] expr = expand_power_base(expr) expr = expand_log(expr) if len(args) == 1: # The definition of O(f(x)) symbol explicitly stated that # the argument of f(x) is irrelevant. That's why we can # combine some power exponents (only "on top" of the # expression tree for f(x)), e.g.: # x**p * (-x)**q -> x**(p+q) for real p, q. x = args[0] margs = list(Mul.make_args( expr.as_independent(x, as_Add=False)[1])) for i, t in enumerate(margs): if t.is_Pow: b, q = t.args if b in (x, -x) and q.is_extended_real and not q.has(x): margs[i] = x**q elif b.is_Pow and not b.exp.has(x): b, r = b.args if b in (x, -x) and r.is_extended_real: margs[i] = x**(r*q) elif b.is_Mul and b.args[0] is S.NegativeOne: b = -b if b.is_Pow and not b.exp.has(x): b, r = b.args if b in (x, -x) and r.is_extended_real: margs[i] = x**(r*q) expr = Mul(*margs) expr = expr.subs(rs) if expr is S.Zero: return expr if expr.is_Order: expr = expr.expr if not expr.has(*variables): expr = S.One # create Order instance: vp = dict(zip(variables, point)) variables.sort(key=default_sort_key) point = [vp[v] for v in variables] args = (expr,) + Tuple(*zip(variables, point)) obj = Expr.__new__(cls, *args) return obj