def test_nthroot(): assert real_root(-8, 3) == -2 assert real_root(-16, 4) == root(-16, 4) r = root(-7, 4) assert real_root(r) == r r1 = root(-1, 3) r2 = r1**2 r3 = root(-1, 4) assert real_root(r1 + r2 + r3) == -1 + r2 + r3
def test_sqrtdenest2(): assert sqrtdenest(sqrt(16 - 2*r29 + 2*sqrt(55 - 10*r29))) == \ r5 + sqrt(11 - 2*r29) e = sqrt(-r5 + sqrt(-2*r29 + 2*sqrt(-10*r29 + 55) + 16)) assert sqrtdenest(e) == root(-2*r29 + 11, 4) r = sqrt(1 + r7) assert sqrtdenest(sqrt(1 + r)) == sqrt(1 + r) e = sqrt(((1 + sqrt(1 + 2*sqrt(3 + r2 + r5)))**2).expand()) assert sqrtdenest(e) == 1 + sqrt(1 + 2*sqrt(r2 + r5 + 3)) assert sqrtdenest(sqrt(5*r3 + 6*r2)) == \ sqrt(2)*root(3, 4) + root(3, 4)**3 assert sqrtdenest(sqrt(((1 + r5 + sqrt(1 + r3))**2).expand())) == \ 1 + r5 + sqrt(1 + r3) assert sqrtdenest(sqrt(((1 + r5 + r7 + sqrt(1 + r3))**2).expand())) == \ 1 + sqrt(1 + r3) + r5 + r7 e = sqrt(((1 + cos(2) + cos(3) + sqrt(1 + r3))**2).expand()) assert sqrtdenest(e) == cos(3) + cos(2) + 1 + sqrt(1 + r3) e = sqrt(-2*r10 + 2*r2*sqrt(-2*r10 + 11) + 14) assert sqrtdenest(e) == sqrt(-2*r10 - 2*r2 + 4*r5 + 14) # check that the result is not more complicated than the input z = sqrt(-2*r29 + cos(2) + 2*sqrt(-10*r29 + 55) + 16) assert sqrtdenest(z) == z assert sqrtdenest(sqrt(r6 + sqrt(15))) == sqrt(r6 + sqrt(15)) z = sqrt(15 - 2*sqrt(31) + 2*sqrt(55 - 10*r29)) assert sqrtdenest(z) == z
def test_issue_from_PR1599(): n1, n2, n3, n4 = symbols('n1 n2 n3 n4', negative=True) 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)) == -(-1)**Rational(1, 3)* (-n1)**Rational(1, 3)*(-n2)**Rational(1, 3)*(-n3)**Rational(1, 3)*(-n4)**Rational(1, 3))
def roots_cubic(f, trig=False): """Returns a list of roots of a cubic polynomial.""" if trig: a, b, c, d = f.all_coeffs() p = (3*a*c - b**2)/3/a**2 q = (2*b**3 - 9*a*b*c + 27*a**2*d)/(27*a**3) D = 18*a*b*c*d - 4*b**3*d + b**2*c**2 - 4*a*c**3 - 27*a**2*d**2 if (D > 0) == True: rv = [] for k in range(3): rv.append(2*sqrt(-p/3)*cos(acos(3*q/2/p*sqrt(-3/p))/3 - k*2*pi/3)) return [i - b/3/a for i in rv] _, a, b, c = f.monic().all_coeffs() if c is S.Zero: x1, x2 = roots([1, a, b], multiple=True) return [x1, S.Zero, x2] p = b - a**2/3 q = c - a*b/3 + 2*a**3/27 pon3 = p/3 aon3 = a/3 if p is S.Zero: if q is S.Zero: return [-aon3]*3 else: if q.is_real: if (q > 0) == True: u1 = -root(q, 3) else: u1 = root(-q, 3) else: u1 = root(-q, 3) elif q is S.Zero: y1, y2 = roots([1, 0, p], multiple=True) return [tmp - aon3 for tmp in [y1, S.Zero, y2]] elif q.is_real and q < 0: u1 = -root(-q/2 + sqrt(q**2/4 + pon3**3), 3) else: u1 = root(q/2 + sqrt(q**2/4 + pon3**3), 3) coeff = I*sqrt(3)/2 u2 = u1*(-S.Half + coeff) u3 = u1*(-S.Half - coeff) if p is S.Zero: return [u1 - aon3, u2 - aon3, u3 - aon3] soln = [ -u1 + pon3/u1 - aon3, -u2 + pon3/u2 - aon3, -u3 + pon3/u3 - aon3 ] return soln
def roots_cubic(f, trig=False): """Returns a list of roots of a cubic polynomial.""" if trig: a, b, c, d = f.all_coeffs() p = (3 * a * c - b**2) / 3 / a**2 q = (2 * b**3 - 9 * a * b * c + 27 * a**2 * d) / (27 * a**3) D = 18 * a * b * c * d - 4 * b**3 * d + b**2 * c**2 - 4 * a * c**3 - 27 * a**2 * d**2 if (D > 0) == True: rv = [] for k in range(3): rv.append(2 * sqrt(-p / 3) * cos( acos(3 * q / 2 / p * sqrt(-3 / p)) / 3 - k * 2 * pi / 3)) return list(sorted([i - b / 3 / a for i in rv])) _, a, b, c = f.monic().all_coeffs() if c is S.Zero: x1, x2 = roots([1, a, b], multiple=True) return [x1, S.Zero, x2] p = b - a**2 / 3 q = c - a * b / 3 + 2 * a**3 / 27 pon3 = p / 3 aon3 = a / 3 if p is S.Zero: if q is S.Zero: return [-aon3] * 3 else: if q.is_real: if (q > 0) == True: u1 = -root(q, 3) else: u1 = root(-q, 3) else: u1 = root(-q, 3) elif q is S.Zero: y1, y2 = roots([1, 0, p], multiple=True) return [tmp - aon3 for tmp in [y1, S.Zero, y2]] elif q.is_real and q < 0: u1 = -root(-q / 2 + sqrt(q**2 / 4 + pon3**3), 3) else: u1 = root(q / 2 + sqrt(q**2 / 4 + pon3**3), 3) coeff = S.ImaginaryUnit * sqrt(3) / 2 u2 = u1 * (-S.Half + coeff) u3 = u1 * (-S.Half - coeff) if p is S.Zero: return [u1 - aon3, u2 - aon3, u3 - aon3] soln = [ -u1 + pon3 / u1 - aon3, -u2 + pon3 / u2 - aon3, -u3 + pon3 / u3 - aon3 ] return soln
def test_issue_6208(): from sympy.functions.elementary.miscellaneous import root assert sqrt(33**(I * 9 / 10)) == -33**(I * 9 / 20) assert root((6 * I)**(2 * I), 3).as_base_exp()[1] == Rational(1, 3) # != 2*I/3 assert root((6 * I)**(I / 3), 3).as_base_exp()[1] == I / 9 assert sqrt(exp(3 * I)) == exp(3 * I / 2) assert sqrt(-sqrt(3) * (1 + 2 * I)) == sqrt(sqrt(3)) * sqrt(-1 - 2 * I) assert sqrt(exp(5 * I)) == -exp(5 * I / 2) assert root(exp(5 * I), 3).exp == Rational(1, 3)
def test_issue_14000(): assert isinstance(sqrt(4, evaluate=False), Pow) == True assert isinstance(cbrt(3.5, evaluate=False), Pow) == True assert isinstance(root(16, 4, evaluate=False), Pow) == True assert sqrt(4, evaluate=False) == Pow(4, S.Half, evaluate=False) assert cbrt(3.5, evaluate=False) == Pow(3.5, Rational(1, 3), evaluate=False) assert root(4, 2, evaluate=False) == Pow(4, S.Half, evaluate=False) assert root(16, 4, 2, evaluate=False).has(Pow) == True assert real_root(-8, 3, evaluate=False).has(Pow) == True
def test_issue_14000(): assert isinstance(sqrt(4, evaluate=False), Pow) == True assert isinstance(cbrt(3.5, evaluate=False), Pow) == True assert isinstance(root(16, 4, evaluate=False), Pow) == True assert sqrt(4, evaluate=False) == Pow(4, S.Half, evaluate=False) assert cbrt(3.5, evaluate=False) == Pow(3.5, Rational(1, 3), evaluate=False) assert root(4, 2, evaluate=False) == Pow(4, Rational(1, 2), evaluate=False) assert root(16, 4, 2, evaluate=False).has(Pow) == True assert real_root(-8, 3, evaluate=False).has(Pow) == True
def taylor_term(n, x, *previous_terms): if n < 0: return S.Zero else: x = sympify(x) if len(previous_terms) > 1: p = previous_terms[-1] return (3**(S(1)/3)*x * Abs(sin(2*pi*(n + S.One)/S(3))) * C.factorial((n - S.One)/S(3)) / ((n + S.One) * Abs(cos(2*pi*(n + S.Half)/S(3))) * C.factorial((n - 2)/S(3))) * p) else: return (S.One/(root(3, 6)*pi) * gamma((n + S.One)/S(3)) * Abs(sin(2*pi*(n + S.One)/S(3))) / C.factorial(n) * (root(3, 3)*x)**n)
def roots_cyclotomic(f, factor=False): """Compute roots of cyclotomic polynomials. """ L, U = _inv_totient_estimate(f.degree()) for n in range(L, U + 1): g = cyclotomic_poly(n, f.gen, polys=True) if f == g: break else: # pragma: no cover raise RuntimeError("failed to find index of a cyclotomic polynomial") roots = [] if not factor: # get the indices in the right order so the computed # roots will be sorted h = n//2 ks = [i for i in range(1, n + 1) if igcd(i, n) == 1] ks.sort(key=lambda x: (x, -1) if x <= h else (abs(x - n), 1)) d = 2*I*pi/n for k in reversed(ks): roots.append(exp(k*d).expand(complex=True)) else: g = Poly(f, extension=root(-1, n)) for h, _ in ordered(g.factor_list()[1]): roots.append(-h.TC()) return roots
def taylor_term(n, x, *previous_terms): if n < 0: return S.Zero else: x = sympify(x) if len(previous_terms) > 1: p = previous_terms[-1] return ( (3 ** (S(1) / 3) * x) ** (-n) * (3 ** (S(1) / 3) * x) ** (n + 1) * sin(pi * (2 * n / 3 + S(4) / 3)) * factorial(n) * gamma(n / 3 + S(2) / 3) / (sin(pi * (2 * n / 3 + S(2) / 3)) * factorial(n + 1) * gamma(n / 3 + S(1) / 3)) * p ) else: return ( S.One / (3 ** (S(2) / 3) * pi) * gamma((n + S.One) / S(3)) * sin(2 * pi * (n + S.One) / S(3)) / factorial(n) * (root(3, 3) * x) ** n )
def test_trig_split(): assert trig_split(cos(x), cos(y)) == (1, 1, 1, x, y, True) assert trig_split(2 * cos(x), -2 * cos(y)) == (2, 1, -1, x, y, True) assert trig_split(cos(x)*sin(y), cos(y)*sin(y)) == \ (sin(y), 1, 1, x, y, True) assert trig_split(cos(x), -sqrt(3)*sin(x), two=True) == \ (2, 1, -1, x, pi/6, False) assert trig_split(cos(x), sin(x), two=True) == \ (sqrt(2), 1, 1, x, pi/4, False) assert trig_split(cos(x), -sin(x), two=True) == \ (sqrt(2), 1, -1, x, pi/4, False) assert trig_split(sqrt(2)*cos(x), -sqrt(6)*sin(x), two=True) == \ (2*sqrt(2), 1, -1, x, pi/6, False) assert trig_split(-sqrt(6)*cos(x), -sqrt(2)*sin(x), two=True) == \ (-2*sqrt(2), 1, 1, x, pi/3, False) assert trig_split(cos(x)/sqrt(6), sin(x)/sqrt(2), two=True) == \ (sqrt(6)/3, 1, 1, x, pi/6, False) assert trig_split(-sqrt(6)*cos(x)*sin(y), -sqrt(2)*sin(x)*sin(y), two=True) == \ (-2*sqrt(2)*sin(y), 1, 1, x, pi/3, False) assert trig_split(cos(x), sin(x)) is None assert trig_split(cos(x), sin(z)) is None assert trig_split(2 * cos(x), -sin(x)) is None assert trig_split(cos(x), -sqrt(3) * sin(x)) is None assert trig_split(cos(x) * cos(y), sin(x) * sin(z)) is None assert trig_split(cos(x) * cos(y), sin(x) * sin(y)) is None assert trig_split(-sqrt(6)*cos(x), sqrt(2)*sin(x)*sin(y), two=True) is \ None assert trig_split(sqrt(3) * sqrt(x), cos(3), two=True) is None assert trig_split(sqrt(3) * root(x, 3), sin(3) * cos(2), two=True) is None assert trig_split(cos(5) * cos(6), cos(7) * sin(5), two=True) is None
def roots_cyclotomic(f, factor=False): """Compute roots of cyclotomic polynomials. """ L, U = _inv_totient_estimate(f.degree()) for n in xrange(L, U + 1): g = cyclotomic_poly(n, f.gen, polys=True) if f == g: break else: # pragma: no cover raise RuntimeError("failed to find index of a cyclotomic polynomial") roots = [] if not factor: for k in xrange(1, n + 1): if igcd(k, n) == 1: roots.append(exp(2 * k * S.Pi * I / n).expand(complex=True)) else: g = Poly(f, extension=root(-1, n)) for h, _ in g.factor_list()[1]: roots.append(-h.TC()) return sorted(roots, key=default_sort_key)
def roots_cyclotomic(f, factor=False): """Compute roots of cyclotomic polynomials. """ L, U = _inv_totient_estimate(f.degree()) for n in xrange(L, U + 1): g = cyclotomic_poly(n, f.gen, polys=True) if f == g: break else: # pragma: no cover raise RuntimeError("failed to find index of a cyclotomic polynomial") roots = [] if not factor: for k in xrange(1, n + 1): if igcd(k, n) == 1: roots.append(exp(2*k*S.Pi*I/n).expand(complex=True)) else: g = Poly(f, extension=root(-1, n)) for h, _ in g.factor_list()[1]: roots.append(-h.TC()) return sorted(roots, key=default_sort_key)
def test_roots_cyclotomic(): assert roots_cyclotomic(cyclotomic_poly(1, x, polys=True)) == [1] assert roots_cyclotomic(cyclotomic_poly(2, x, polys=True)) == [-1] assert roots_cyclotomic(cyclotomic_poly(3, x, polys=True)) == [ Rational(-1, 2) - I * sqrt(3) / 2, Rational(-1, 2) + I * sqrt(3) / 2 ] assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True)) == [-I, I] assert roots_cyclotomic(cyclotomic_poly(6, x, polys=True)) == [ S.Half - I * sqrt(3) / 2, S.Half + I * sqrt(3) / 2 ] assert roots_cyclotomic(cyclotomic_poly(7, x, polys=True)) == [ -cos(pi / 7) - I * sin(pi / 7), -cos(pi / 7) + I * sin(pi / 7), -cos(pi * Rational(3, 7)) - I * sin(pi * Rational(3, 7)), -cos(pi * Rational(3, 7)) + I * sin(pi * Rational(3, 7)), cos(pi * Rational(2, 7)) - I * sin(pi * Rational(2, 7)), cos(pi * Rational(2, 7)) + I * sin(pi * Rational(2, 7)), ] assert roots_cyclotomic(cyclotomic_poly(8, x, polys=True)) == [ -sqrt(2) / 2 - I * sqrt(2) / 2, -sqrt(2) / 2 + I * sqrt(2) / 2, sqrt(2) / 2 - I * sqrt(2) / 2, sqrt(2) / 2 + I * sqrt(2) / 2, ] assert roots_cyclotomic(cyclotomic_poly(12, x, polys=True)) == [ -sqrt(3) / 2 - I / 2, -sqrt(3) / 2 + I / 2, sqrt(3) / 2 - I / 2, sqrt(3) / 2 + I / 2, ] assert roots_cyclotomic(cyclotomic_poly(1, x, polys=True), factor=True) == [1] assert roots_cyclotomic(cyclotomic_poly(2, x, polys=True), factor=True) == [-1] assert roots_cyclotomic(cyclotomic_poly(3, x, polys=True), factor=True) == \ [-root(-1, 3), -1 + root(-1, 3)] assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True), factor=True) == \ [-I, I] assert roots_cyclotomic(cyclotomic_poly(5, x, polys=True), factor=True) == \ [-root(-1, 5), -root(-1, 5)**3, root(-1, 5)**2, -1 - root(-1, 5)**2 + root(-1, 5) + root(-1, 5)**3] assert roots_cyclotomic(cyclotomic_poly(6, x, polys=True), factor=True) == \ [1 - root(-1, 3), root(-1, 3)]
def roots_binomial(f): """Returns a list of roots of a binomial polynomial. If the domain is ZZ then the roots will be sorted with negatives coming before positives. The ordering will be the same for any numerical coefficients as long as the assumptions tested are correct, otherwise the ordering will not be sorted (but will be canonical). """ n = f.degree() a, b = f.nth(n), f.nth(0) base = -cancel(b/a) alpha = root(base, n) if alpha.is_number: alpha = alpha.expand(complex=True) # define some parameters that will allow us to order the roots. # If the domain is ZZ this is guaranteed to return roots sorted # with reals before non-real roots and non-real sorted according # to real part and imaginary part, e.g. -1, 1, -1 + I, 2 - I neg = base.is_negative even = n % 2 == 0 if neg: if even == True and (base + 1).is_positive: big = True else: big = False # get the indices in the right order so the computed # roots will be sorted when the domain is ZZ ks = [] imax = n//2 if even: ks.append(imax) imax -= 1 if not neg: ks.append(0) for i in range(imax, 0, -1): if neg: ks.extend([i, -i]) else: ks.extend([-i, i]) if neg: ks.append(0) if big: for i in range(0, len(ks), 2): pair = ks[i: i + 2] pair = list(reversed(pair)) # compute the roots roots, d = [], 2*I*pi/n for k in ks: zeta = exp(k*d).expand(complex=True) roots.append((alpha*zeta).expand(power_base=False)) return roots
def test_AlgebraicNumber(): a = AlgebraicNumber(sqrt(2)) sT( a, "AlgebraicNumber(Pow(Integer(2), Rational(1, 2)), [Integer(1), Integer(0)])" ) a = AlgebraicNumber(root(-2, 3)) sT( a, "AlgebraicNumber(Pow(Integer(-2), Rational(1, 3)), [Integer(1), Integer(0)])" )
def taylor_term(n, x, *previous_terms): if n < 0: return S.Zero else: x = sympify(x) if len(previous_terms) > 1: p = previous_terms[-1] return ((3**(S(1)/3)*x)**(-n)*(3**(S(1)/3)*x)**(n + 1)*sin(pi*(2*n/3 + S(4)/3))*C.factorial(n) * gamma(n/3 + S(2)/3)/(sin(pi*(2*n/3 + S(2)/3))*C.factorial(n + 1)*gamma(n/3 + S(1)/3)) * p) else: return (S.One/(3**(S(2)/3)*pi) * gamma((n+S.One)/S(3)) * sin(2*pi*(n+S.One)/S(3)) / C.factorial(n) * (root(3, 3)*x)**n)
def test_real_root(): assert real_root(-8, 3) == -2 assert real_root(-16, 4) == root(-16, 4) r = root(-7, 4) assert real_root(r) == r r1 = root(-1, 3) r2 = r1**2 r3 = root(-1, 4) assert real_root(r1 + r2 + r3) == -1 + r2 + r3 assert real_root(root(-2, 3)) == -root(2, 3) assert real_root(-8., 3) == -2
def test_roots_cubic(): assert roots_cubic(Poly(2 * x**3, x)) == [0, 0, 0] assert roots_cubic(Poly(x**3 - 3 * x**2 + 3 * x - 1, x)) == [1, 1, 1] # valid for arbitrary y (issue 21263) r = root(y, 3) assert roots_cubic(Poly(x**3 - y, x)) == [ r, r * (-S.Half + sqrt(3) * I / 2), r * (-S.Half - sqrt(3) * I / 2) ] # simpler form when y is negative assert roots_cubic(Poly(x**3 - -1, x)) == \ [-1, S.Half - I*sqrt(3)/2, S.Half + I*sqrt(3)/2] assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \ S.Half + 3**Rational(1, 3)/2 + 3**Rational(2, 3)/2 eq = -x**3 + 2 * x**2 + 3 * x - 2 assert roots(eq, trig=True, multiple=True) == \ roots_cubic(Poly(eq, x), trig=True) == [ Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3, -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3), -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3), ]
def test_real_root(): assert real_root(-8, 3) == -2 assert real_root(-16, 4) == root(-16, 4) r = root(-7, 4) assert real_root(r) == r r1 = root(-1, 3) r2 = r1**2 r3 = root(-1, 4) assert real_root(r1 + r2 + r3) == -1 + r2 + r3 assert real_root(root(-2, 3)) == -root(2, 3) assert real_root(-8., 3) == -2 x = Symbol('x') n = Symbol('n') g = real_root(x, n) assert g.subs(dict(x=-8, n=3)) == -2 assert g.subs(dict(x=8, n=3)) == 2 # give principle root if there is no real root -- if this is not desired # then maybe a Root class is needed to raise an error instead assert g.subs(dict(x=I, n=3)) == cbrt(I) assert g.subs(dict(x=-8, n=2)) == sqrt(-8) assert g.subs(dict(x=I, n=2)) == sqrt(I)
def _eval_rewrite_as_hyper(self, z): pf1 = z ** 2 / (2 * root(3, 6) * gamma(S(2) / 3)) pf2 = root(3, 6) / gamma(S(1) / 3) return pf1 * hyper([], [S(5) / 3], z ** 3 / 9) + pf2 * hyper([], [S(1) / 3], z ** 3 / 9)
def test_root(): from sympy.abc import x n = Symbol('n', integer=True) k = Symbol('k', integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == 2**Rational(1, 3) assert root(2, 3) == cbrt(2) assert root(2, -5) == 2**Rational(4, 5) / 2 assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2) * I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == x**Rational(1, 3) assert root(x, 3) == cbrt(x) assert root(x, -5) == x**Rational(-1, 5) assert root(x, n) == x**(1 / n) assert root(x, -n) == x**(-1 / n) assert root(x, n, k) == x**(1 / n) * (-1)**(2 * k / n)
def test_roots0(): assert roots(1, x) == {} assert roots(x, x) == {S.Zero: 1} assert roots(x**9, x) == {S.Zero: 9} assert roots(((x - 2) * (x + 3) * (x - 4)).expand(), x) == { -S(3): 1, S(2): 1, S(4): 1 } assert roots(2 * x + 1, x) == {Rational(-1, 2): 1} assert roots((2 * x + 1)**2, x) == {Rational(-1, 2): 2} assert roots((2 * x + 1)**5, x) == {Rational(-1, 2): 5} assert roots((2 * x + 1)**10, x) == {Rational(-1, 2): 10} assert roots(x**4 - 1, x) == {I: 1, S.One: 1, -S.One: 1, -I: 1} assert roots((x**4 - 1)**2, x) == {I: 2, S.One: 2, -S.One: 2, -I: 2} assert roots(((2 * x - 3)**2).expand(), x) == {Rational(3, 2): 2} assert roots(((2 * x + 3)**2).expand(), x) == {Rational(-3, 2): 2} assert roots(((2 * x - 3)**3).expand(), x) == {Rational(3, 2): 3} assert roots(((2 * x + 3)**3).expand(), x) == {Rational(-3, 2): 3} assert roots(((2 * x - 3)**5).expand(), x) == {Rational(3, 2): 5} assert roots(((2 * x + 3)**5).expand(), x) == {Rational(-3, 2): 5} assert roots(((a * x - b)**5).expand(), x) == {b / a: 5} assert roots(((a * x + b)**5).expand(), x) == {-b / a: 5} assert roots(x**2 + (-a - 1) * x + a, x) == {a: 1, S.One: 1} assert roots(x**4 - 2 * x**2 + 1, x) == {S.One: 2, S.NegativeOne: 2} assert roots(x**6 - 4*x**4 + 4*x**3 - x**2, x) == \ {S.One: 2, -1 - sqrt(2): 1, S.Zero: 2, -1 + sqrt(2): 1} assert roots(x**8 - 1, x) == { sqrt(2) / 2 + I * sqrt(2) / 2: 1, sqrt(2) / 2 - I * sqrt(2) / 2: 1, -sqrt(2) / 2 + I * sqrt(2) / 2: 1, -sqrt(2) / 2 - I * sqrt(2) / 2: 1, S.One: 1, -S.One: 1, I: 1, -I: 1 } f = -2016*x**2 - 5616*x**3 - 2056*x**4 + 3324*x**5 + 2176*x**6 - \ 224*x**7 - 384*x**8 - 64*x**9 assert roots(f) == { S.Zero: 2, -S(2): 2, S(2): 1, Rational(-7, 2): 1, Rational(-3, 2): 1, Rational(-1, 2): 1, Rational(3, 2): 1 } assert roots((a + b + c) * x - (a + b + c + d), x) == { (a + b + c + d) / (a + b + c): 1 } assert roots(x**3 + x**2 - x + 1, x, cubics=False) == {} assert roots(((x - 2) * (x + 3) * (x - 4)).expand(), x, cubics=False) == { -S(3): 1, S(2): 1, S(4): 1 } assert roots(((x - 2)*(x + 3)*(x - 4)*(x - 5)).expand(), x, cubics=False) == \ {-S(3): 1, S(2): 1, S(4): 1, S(5): 1} assert roots(x**3 + 2 * x**2 + 4 * x + 8, x) == { -S(2): 1, -2 * I: 1, 2 * I: 1 } assert roots(x**3 + 2*x**2 + 4*x + 8, x, cubics=True) == \ {-2*I: 1, 2*I: 1, -S(2): 1} assert roots((x**2 - x)*(x**3 + 2*x**2 + 4*x + 8), x ) == \ {S.One: 1, S.Zero: 1, -S(2): 1, -2*I: 1, 2*I: 1} r1_2, r1_3 = S.Half, Rational(1, 3) x0 = (3 * sqrt(33) + 19)**r1_3 x1 = 4 / x0 / 3 x2 = x0 / 3 x3 = sqrt(3) * I / 2 x4 = x3 - r1_2 x5 = -x3 - r1_2 assert roots(x**3 + x**2 - x + 1, x, cubics=True) == { -x1 - x2 - r1_3: 1, -x1 / x4 - x2 * x4 - r1_3: 1, -x1 / x5 - x2 * x5 - r1_3: 1, } f = (x**2 + 2 * x + 3).subs(x, 2 * x**2 + 3 * x).subs(x, 5 * x - 4) r13_20, r1_20 = [Rational(*r) for r in ((13, 20), (1, 20))] s2 = sqrt(2) assert roots(f, x) == { r13_20 + r1_20 * sqrt(1 - 8 * I * s2): 1, r13_20 - r1_20 * sqrt(1 - 8 * I * s2): 1, r13_20 + r1_20 * sqrt(1 + 8 * I * s2): 1, r13_20 - r1_20 * sqrt(1 + 8 * I * s2): 1, } f = x**4 + x**3 + x**2 + x + 1 r1_4, r1_8, r5_8 = [Rational(*r) for r in ((1, 4), (1, 8), (5, 8))] assert roots(f, x) == { -r1_4 + r1_4 * 5**r1_2 + I * (r5_8 + r1_8 * 5**r1_2)**r1_2: 1, -r1_4 + r1_4 * 5**r1_2 - I * (r5_8 + r1_8 * 5**r1_2)**r1_2: 1, -r1_4 - r1_4 * 5**r1_2 + I * (r5_8 - r1_8 * 5**r1_2)**r1_2: 1, -r1_4 - r1_4 * 5**r1_2 - I * (r5_8 - r1_8 * 5**r1_2)**r1_2: 1, } f = z**3 + (-2 - y) * z**2 + (1 + 2 * y - 2 * x**2) * z - y + 2 * x**2 assert roots(f, z) == { S.One: 1, S.Half + S.Half * y + S.Half * sqrt(1 - 2 * y + y**2 + 8 * x**2): 1, S.Half + S.Half * y - S.Half * sqrt(1 - 2 * y + y**2 + 8 * x**2): 1, } assert roots(a * b * c * x**3 + 2 * x**2 + 4 * x + 8, x, cubics=False) == {} assert roots(a * b * c * x**3 + 2 * x**2 + 4 * x + 8, x, cubics=True) != {} assert roots(x**4 - 1, x, filter='Z') == {S.One: 1, -S.One: 1} assert roots(x**4 - 1, x, filter='I') == {I: 1, -I: 1} assert roots((x - 1) * (x + 1), x) == {S.One: 1, -S.One: 1} assert roots((x - 1) * (x + 1), x, predicate=lambda r: r.is_positive) == { S.One: 1 } assert roots(x**4 - 1, x, filter='Z', multiple=True) == [-S.One, S.One] assert roots(x**4 - 1, x, filter='I', multiple=True) == [I, -I] ar, br = symbols('a, b', real=True) p = x**2 * (ar - br)**2 + 2 * x * (br - ar) + 1 assert roots(p, x, filter='R') == {1 / (ar - br): 2} assert roots(x**3, x, multiple=True) == [S.Zero, S.Zero, S.Zero] assert roots(1234, x, multiple=True) == [] f = x**6 - x**5 + x**4 - x**3 + x**2 - x + 1 assert roots(f) == { -I * sin(pi / 7) + cos(pi / 7): 1, -I * sin(pi * Rational(2, 7)) - cos(pi * Rational(2, 7)): 1, -I * sin(pi * Rational(3, 7)) + cos(pi * Rational(3, 7)): 1, I * sin(pi / 7) + cos(pi / 7): 1, I * sin(pi * Rational(2, 7)) - cos(pi * Rational(2, 7)): 1, I * sin(pi * Rational(3, 7)) + cos(pi * Rational(3, 7)): 1, } g = ((x**2 + 1) * f**2).expand() assert roots(g) == { -I * sin(pi / 7) + cos(pi / 7): 2, -I * sin(pi * Rational(2, 7)) - cos(pi * Rational(2, 7)): 2, -I * sin(pi * Rational(3, 7)) + cos(pi * Rational(3, 7)): 2, I * sin(pi / 7) + cos(pi / 7): 2, I * sin(pi * Rational(2, 7)) - cos(pi * Rational(2, 7)): 2, I * sin(pi * Rational(3, 7)) + cos(pi * Rational(3, 7)): 2, -I: 1, I: 1, } r = roots(x**3 + 40 * x + 64) real_root = [rx for rx in r if rx.is_real][0] cr = 108 + 6 * sqrt(1074) assert real_root == -2 * root(cr, 3) / 3 + 20 / root(cr, 3) eq = Poly((7 + 5 * sqrt(2)) * x**3 + (-6 - 4 * sqrt(2)) * x**2 + (-sqrt(2) - 1) * x + 2, x, domain='EX') assert roots(eq) == {-1 + sqrt(2): 1, -2 + 2 * sqrt(2): 1, -sqrt(2) + 1: 1} eq = Poly(41 * x**5 + 29 * sqrt(2) * x**5 - 153 * x**4 - 108 * sqrt(2) * x**4 + 175 * x**3 + 125 * sqrt(2) * x**3 - 45 * x**2 - 30 * sqrt(2) * x**2 - 26 * sqrt(2) * x - 26 * x + 24, x, domain='EX') assert roots(eq) == { -sqrt(2) + 1: 1, -2 + 2 * sqrt(2): 1, -1 + sqrt(2): 1, -4 + 4 * sqrt(2): 1, -3 + 3 * sqrt(2): 1 } eq = Poly(x**3 - 2 * x**2 + 6 * sqrt(2) * x**2 - 8 * sqrt(2) * x + 23 * x - 14 + 14 * sqrt(2), x, domain='EX') assert roots(eq) == { -2 * sqrt(2) + 2: 1, -2 * sqrt(2) + 1: 1, -2 * sqrt(2) - 1: 1 } assert roots(Poly((x + sqrt(2))**3 - 7, x, domain='EX')) == \ {-sqrt(2) + root(7, 3)*(-S.Half - sqrt(3)*I/2): 1, -sqrt(2) + root(7, 3)*(-S.Half + sqrt(3)*I/2): 1, -sqrt(2) + root(7, 3): 1}
def test_root(): from sympy.abc import x n = Symbol("n", integer=True) k = Symbol("k", integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == 2 ** Rational(1, 3) assert root(2, 3) == cbrt(2) assert root(2, -5) == 2 ** Rational(4, 5) / 2 assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2) * I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == x ** Rational(1, 3) assert root(x, 3) == cbrt(x) assert root(x, -5) == x ** Rational(-1, 5) assert root(x, n) == x ** (1 / n) assert root(x, -n) == x ** (-1 / n) assert root(x, n, k) == x ** (1 / n) * (-1) ** (2 * k / n)
def _eval_rewrite_as_hyper(self, z, **kwargs): pf1 = S.One / (3**(S(2) / 3) * gamma(S(2) / 3)) pf2 = z / (root(3, 3) * gamma(S(1) / 3)) return pf1 * hyper([], [S(2) / 3], z**3 / 9) - pf2 * hyper( [], [S(4) / 3], z**3 / 9)
def _eval_rewrite_as_meijerg(self, z): return (pi*z**(S(3)/4) / (sqrt(2)*root(z**2, 4)*root(-z, 4)) * meijerg([], [1], [S(1)/4], [S(3)/4, 0], -pi**2*z**4/16))
(r"x_a", Symbol('x_{a}')), (r"x_{b}", Symbol('x_{b}')), (r"h_\theta", Symbol('h_{theta}')), (r"h_{\theta}", Symbol('h_{theta}')), (r"h_{\theta}(x_0, x_1)", Function('h_{theta}')(Symbol('x_{0}'), Symbol('x_{1}'))), (r"x!", _factorial(x)), (r"100!", _factorial(100)), (r"\theta!", _factorial(theta)), (r"(x + 1)!", _factorial(_Add(x, 1))), (r"(x!)!", _factorial(_factorial(x))), (r"x!!!", _factorial(_factorial(_factorial(x)))), (r"5!7!", _Mul(_factorial(5), _factorial(7))), (r"\sqrt{x}", sqrt(x)), (r"\sqrt{x + b}", sqrt(_Add(x, b))), (r"\sqrt[3]{\sin x}", root(sin(x), 3)), (r"\sqrt[y]{\sin x}", root(sin(x), y)), (r"\sqrt[\theta]{\sin x}", root(sin(x), theta)), (r"\sqrt{\frac{12}{6}}", _Sqrt(_Mul(12, _Pow(6, -1)))), (r"\overline{z}", _Conjugate(z)), (r"\overline{\overline{z}}", _Conjugate(_Conjugate(z))), (r"\overline{x + y}", _Conjugate(_Add(x, y))), (r"\overline{x} + \overline{y}", _Conjugate(x) + _Conjugate(y)), (r"x < y", StrictLessThan(x, y)), (r"x \leq y", LessThan(x, y)), (r"x > y", StrictGreaterThan(x, y)), (r"x \geq y", GreaterThan(x, y)), (r"\mathit{x}", Symbol('x')), (r"\mathit{test}", Symbol('test')), (r"\mathit{TEST}", Symbol('TEST')), (r"\mathit{HELLO world}", Symbol('HELLO world')),
def test_slow_general_univariate(): r = rootof(x**5 - x**2 + 1, 0) assert solve(sqrt(x) + 1/root(x, 3) > 1) == \ Or(And(0 < x, x < r**6), And(r**6 < x, x < oo))
def _eval_rewrite_as_hyper(self, z, **kwargs): pf1 = z**2 / (2 * 3**(S(2) / 3) * gamma(S(2) / 3)) pf2 = 1 / (root(3, 3) * gamma(S(1) / 3)) return pf1 * hyper([], [S(5) / 3], z**3 / 9) - pf2 * hyper( [], [S(1) / 3], z**3 / 9)
def _eval_rewrite_as_hyper(self, z): pf1 = z ** 2 / (2 * 3 ** (S(2) / 3) * gamma(S(2) / 3)) pf2 = 1 / (root(3, 3) * gamma(S(1) / 3)) return pf1 * hyper([], [S(5) / 3], z ** 3 / 9) - pf2 * hyper([], [S(1) / 3], z ** 3 / 9)
def _eval_rewrite_as_hyper(self, z, **kwargs): pf1 = z**2 / (2 * root(3, 6) * gamma(S(2) / 3)) pf2 = root(3, 6) / gamma(S(1) / 3) return pf1 * hyper([], [S(5) / 3], z**3 / 9) + pf2 * hyper( [], [S(1) / 3], z**3 / 9)
def test_root(): from sympy.abc import x, y, z n = Symbol('n', integer=True) assert root(2, 2) == sqrt(2) assert root(2, 1) == 2 assert root(2, 3) == 2**Rational(1,3) assert root(2, -5) == 2**Rational(4,5)/2 assert root(-2, 1) == -2 assert root(-2, 2) == sqrt(2)*I assert root(-2, 1) == -2 assert root(x, 2) == sqrt(x) assert root(x, 1) == x assert root(x, 3) == x**Rational(1,3) assert root(x, -5) == x**Rational(-1,5) assert root(x, n) == x**(1/n) assert root(x, -n) == x**(-1/n)
def bivariate_type(f, x, y, **kwargs): """Given an expression, f, 3 tests will be done to see what type of composite bivariate it might be, options for u(x, y) are:: x*y x+y x*y+x x*y+y If it matches one of these types, ``u(x, y)``, ``P(u)`` and dummy variable ``u`` will be returned. Solving ``P(u)`` for ``u`` and equating the solutions to ``u(x, y)`` and then solving for ``x`` or ``y`` is equivalent to solving the original expression for ``x`` or ``y``. If ``x`` and ``y`` represent two functions in the same variable, e.g. ``x = g(t)`` and ``y = h(t)``, then if ``u(x, y) - p`` can be solved for ``t`` then these represent the solutions to ``P(u) = 0`` when ``p`` are the solutions of ``P(u) = 0``. Only positive values of ``u`` are considered. Examples ======== >>> from sympy.solvers.solvers import solve >>> from sympy.solvers.bivariate import bivariate_type >>> from sympy.abc import x, y >>> eq = (x**2 - 3).subs(x, x + y) >>> bivariate_type(eq, x, y) (x + y, _u**2 - 3, _u) >>> uxy, pu, u = _ >>> usol = solve(pu, u); usol [sqrt(3)] >>> [solve(uxy - s) for s in solve(pu, u)] [[{x: -y + sqrt(3)}]] >>> all(eq.subs(s).equals(0) for sol in _ for s in sol) True """ u = Dummy('u', positive=True) if kwargs.pop('first', True): p = Poly(f, x, y) f = p.as_expr() _x = Dummy() _y = Dummy() rv = bivariate_type(Poly(f.subs({x: _x, y: _y}), _x, _y), _x, _y, first=False) if rv: reps = {_x: x, _y: y} return rv[0].xreplace(reps), rv[1].xreplace(reps), rv[2] return p = f f = p.as_expr() # f(x*y) args = Add.make_args(p.as_expr()) new = [] for a in args: a = _mexpand(a.subs(x, u/y)) free = a.free_symbols if x in free or y in free: break new.append(a) else: return x*y, Add(*new), u def ok(f, v, c): new = _mexpand(f.subs(v, c)) free = new.free_symbols return None if (x in free or y in free) else new # f(a*x + b*y) new = [] d = p.degree(x) if p.degree(y) == d: a = root(p.coeff_monomial(x**d), d) b = root(p.coeff_monomial(y**d), d) new = ok(f, x, (u - b*y)/a) if new is not None: return a*x + b*y, new, u # f(a*x*y + b*y) new = [] d = p.degree(x) if p.degree(y) == d: for itry in range(2): a = root(p.coeff_monomial(x**d*y**d), d) b = root(p.coeff_monomial(y**d), d) new = ok(f, x, (u - b*y)/a/y) if new is not None: return a*x*y + b*y, new, u x, y = y, x
def _eval_rewrite_as_hyper(self, z): pf1 = S.One / (3 ** (S(2) / 3) * gamma(S(2) / 3)) pf2 = z / (root(3, 3) * gamma(S(1) / 3)) return pf1 * hyper([], [S(2) / 3], z ** 3 / 9) - pf2 * hyper([], [S(4) / 3], z ** 3 / 9)
def test_factor_terms(): A = Symbol('A', commutative=False) assert factor_terms(9*(x + x*y + 1) + (3*x + 3)**(2 + 2*x)) == \ 9*x*y + 9*x + _keep_coeff(S(3), x + 1)**_keep_coeff(S(2), x + 1) + 9 assert factor_terms(9*(x + x*y + 1) + (3)**(2 + 2*x)) == \ _keep_coeff(S(9), 3**(2*x) + x*y + x + 1) assert factor_terms(3**(2 + 2*x) + a*3**(2 + 2*x)) == \ 9*3**(2*x)*(a + 1) assert factor_terms(x + x*A) == \ x*(1 + A) assert factor_terms(sin(x + x*A)) == \ sin(x*(1 + A)) assert factor_terms((3*x + 3)**((2 + 2*x)/3)) == \ _keep_coeff(S(3), x + 1)**_keep_coeff(Rational(2, 3), x + 1) assert factor_terms(x + (x*y + x)**(3*x + 3)) == \ x + (x*(y + 1))**_keep_coeff(S(3), x + 1) assert factor_terms(a*(x + x*y) + b*(x*2 + y*x*2)) == \ x*(a + 2*b)*(y + 1) i = Integral(x, (x, 0, oo)) assert factor_terms(i) == i assert factor_terms(x / 2 + y) == x / 2 + y # fraction doesn't apply to integer denominators assert factor_terms(x / 2 + y, fraction=True) == x / 2 + y # clear *does* apply to the integer denominators assert factor_terms(x / 2 + y, clear=True) == Mul(S.Half, x + 2 * y, evaluate=False) # check radical extraction eq = sqrt(2) + sqrt(10) assert factor_terms(eq) == eq assert factor_terms(eq, radical=True) == sqrt(2) * (1 + sqrt(5)) eq = root(-6, 3) + root(6, 3) assert factor_terms( eq, radical=True) == 6**(S.One / 3) * (1 + (-1)**(S.One / 3)) eq = [x + x * y] ans = [x * (y + 1)] for c in [list, tuple, set]: assert factor_terms(c(eq)) == c(ans) assert factor_terms(Tuple(x + x * y)) == Tuple(x * (y + 1)) assert factor_terms(Interval(0, 1)) == Interval(0, 1) e = 1 / sqrt(a / 2 + 1) assert factor_terms(e, clear=False) == 1 / sqrt(a / 2 + 1) assert factor_terms(e, clear=True) == sqrt(2) / sqrt(a + 2) eq = x / (x + 1 / x) + 1 / (x**2 + 1) assert factor_terms(eq, fraction=False) == eq assert factor_terms(eq, fraction=True) == 1 assert factor_terms((1/(x**3 + x**2) + 2/x**2)*y) == \ y*(2 + 1/(x + 1))/x**2 # if not True, then processesing for this in factor_terms is not necessary assert gcd_terms(-x - y) == -x - y assert factor_terms(-x - y) == Mul(-1, x + y, evaluate=False) # if not True, then "special" processesing in factor_terms is not necessary assert gcd_terms(exp(Mul(-1, x + 1))) == exp(-x - 1) e = exp(-x - 2) + x assert factor_terms(e) == exp(Mul(-1, x + 2, evaluate=False)) + x assert factor_terms(e, sign=False) == e assert factor_terms(exp(-4 * x - 2) - x) == -x + exp(Mul(-2, 2 * x + 1, evaluate=False)) # sum/integral tests for F in (Sum, Integral): assert factor_terms(F(x, (y, 1, 10))) == x * F(1, (y, 1, 10)) assert factor_terms(F(x, (y, 1, 10)) + x) == x * (1 + F(1, (y, 1, 10))) assert factor_terms(F(x * y + x * y**2, (y, 1, 10))) == x * F(y * (y + 1), (y, 1, 10))
def roots_cubic(f, trig=False): """Returns a list of roots of a cubic polynomial. References ========== [1] https://en.wikipedia.org/wiki/Cubic_function, General formula for roots, (accessed November 17, 2014). """ if trig: a, b, c, d = f.all_coeffs() p = (3*a*c - b**2)/3/a**2 q = (2*b**3 - 9*a*b*c + 27*a**2*d)/(27*a**3) D = 18*a*b*c*d - 4*b**3*d + b**2*c**2 - 4*a*c**3 - 27*a**2*d**2 if (D > 0) == True: rv = [] for k in range(3): rv.append(2*sqrt(-p/3)*cos(acos(3*q/2/p*sqrt(-3/p))/3 - k*2*pi/3)) return [i - b/3/a for i in rv] _, a, b, c = f.monic().all_coeffs() if c is S.Zero: x1, x2 = roots([1, a, b], multiple=True) return [x1, S.Zero, x2] p = b - a**2/3 q = c - a*b/3 + 2*a**3/27 pon3 = p/3 aon3 = a/3 u1 = None if p is S.Zero: if q is S.Zero: return [-aon3]*3 if q.is_real: if q.is_positive: u1 = -root(q, 3) elif q.is_negative: u1 = root(-q, 3) elif q is S.Zero: y1, y2 = roots([1, 0, p], multiple=True) return [tmp - aon3 for tmp in [y1, S.Zero, y2]] elif q.is_real and q.is_negative: u1 = -root(-q/2 + sqrt(q**2/4 + pon3**3), 3) coeff = I*sqrt(3)/2 if u1 is None: u1 = S(1) u2 = -S.Half + coeff u3 = -S.Half - coeff a, b, c, d = S(1), a, b, c D0 = b**2 - 3*a*c D1 = 2*b**3 - 9*a*b*c + 27*a**2*d C = root((D1 + sqrt(D1**2 - 4*D0**3))/2, 3) return [-(b + uk*C + D0/C/uk)/3/a for uk in [u1, u2, u3]] u2 = u1*(-S.Half + coeff) u3 = u1*(-S.Half - coeff) if p is S.Zero: return [u1 - aon3, u2 - aon3, u3 - aon3] soln = [ -u1 + pon3/u1 - aon3, -u2 + pon3/u2 - aon3, -u3 + pon3/u3 - aon3 ] return soln
def test_radsimp(): r2 = sqrt(2) r3 = sqrt(3) r5 = sqrt(5) r7 = sqrt(7) assert fraction(radsimp(1 / r2)) == (sqrt(2), 2) assert radsimp(1/(1 + r2)) == \ -1 + sqrt(2) assert radsimp(1/(r2 + r3)) == \ -sqrt(2) + sqrt(3) assert fraction(radsimp(1/(1 + r2 + r3))) == \ (-sqrt(6) + sqrt(2) + 2, 4) assert fraction(radsimp(1/(r2 + r3 + r5))) == \ (-sqrt(30) + 2*sqrt(3) + 3*sqrt(2), 12) assert fraction(radsimp( 1 / (1 + r2 + r3 + r5))) == ((-34 * sqrt(10) - 26 * sqrt(15) - 55 * sqrt(3) - 61 * sqrt(2) + 14 * sqrt(30) + 93 + 46 * sqrt(6) + 53 * sqrt(5), 71)) assert fraction(radsimp( 1 / (r2 + r3 + r5 + r7))) == ((-50 * sqrt(42) - 133 * sqrt(5) - 34 * sqrt(70) - 145 * sqrt(3) + 22 * sqrt(105) + 185 * sqrt(2) + 62 * sqrt(30) + 135 * sqrt(7), 215)) z = radsimp(1 / (1 + r2 / 3 + r3 / 5 + r5 + r7)) assert len((3616791619821680643598 * z).args) == 16 assert radsimp(1 / z) == 1 / z assert radsimp(1 / z, max_terms=20).expand() == 1 + r2 / 3 + r3 / 5 + r5 + r7 assert radsimp(1/(r2*3)) == \ sqrt(2)/6 assert radsimp(1 / (r2 * a + r3 + r5 + r7)) == ( (8 * sqrt(2) * a**7 - 8 * sqrt(7) * a**6 - 8 * sqrt(5) * a**6 - 8 * sqrt(3) * a**6 - 180 * sqrt(2) * a**5 + 8 * sqrt(30) * a**5 + 8 * sqrt(42) * a**5 + 8 * sqrt(70) * a**5 - 24 * sqrt(105) * a**4 + 84 * sqrt(3) * a**4 + 100 * sqrt(5) * a**4 + 116 * sqrt(7) * a**4 - 72 * sqrt(70) * a**3 - 40 * sqrt(42) * a**3 - 8 * sqrt(30) * a**3 + 782 * sqrt(2) * a**3 - 462 * sqrt(3) * a**2 - 302 * sqrt(7) * a**2 - 254 * sqrt(5) * a**2 + 120 * sqrt(105) * a**2 - 795 * sqrt(2) * a - 62 * sqrt(30) * a + 82 * sqrt(42) * a + 98 * sqrt(70) * a - 118 * sqrt(105) + 59 * sqrt(7) + 295 * sqrt(5) + 531 * sqrt(3)) / (16 * a**8 - 480 * a**6 + 3128 * a**4 - 6360 * a**2 + 3481)) assert radsimp(1 / (r2 * a + r2 * b + r3 + r7)) == ( (sqrt(2) * a * (a + b)**2 - 5 * sqrt(2) * a + sqrt(42) * a + sqrt(2) * b * (a + b)**2 - 5 * sqrt(2) * b + sqrt(42) * b - sqrt(7) * (a + b)**2 - sqrt(3) * (a + b)**2 - 2 * sqrt(3) + 2 * sqrt(7)) / (2 * a**4 + 8 * a**3 * b + 12 * a**2 * b**2 - 20 * a**2 + 8 * a * b**3 - 40 * a * b + 2 * b**4 - 20 * b**2 + 8)) assert radsimp(1/(r2*a + r2*b + r2*c + r2*d)) == \ sqrt(2)/(2*a + 2*b + 2*c + 2*d) assert radsimp(1 / (1 + r2 * a + r2 * b + r2 * c + r2 * d)) == ( (sqrt(2) * a + sqrt(2) * b + sqrt(2) * c + sqrt(2) * d - 1) / (2 * a**2 + 4 * a * b + 4 * a * c + 4 * a * d + 2 * b**2 + 4 * b * c + 4 * b * d + 2 * c**2 + 4 * c * d + 2 * d**2 - 1)) assert radsimp((y**2 - x)/(y - sqrt(x))) == \ sqrt(x) + y assert radsimp(-(y**2 - x)/(y - sqrt(x))) == \ -(sqrt(x) + y) assert radsimp(1/(1 - I + a*I)) == \ (-I*a + 1 + I)/(a**2 - 2*a + 2) assert radsimp(1/((-x + y)*(x - sqrt(y)))) == \ (-x - sqrt(y))/((x - y)*(x**2 - y)) e = (3 + 3 * sqrt(2)) * x * (3 * x - 3 * sqrt(y)) assert radsimp(e) == x * (3 + 3 * sqrt(2)) * (3 * x - 3 * sqrt(y)) assert radsimp(1 / e) == ( (-9 * x + 9 * sqrt(2) * x - 9 * sqrt(y) + 9 * sqrt(2) * sqrt(y)) / (9 * x * (9 * x**2 - 9 * y))) assert radsimp(1 + 1/(1 + sqrt(3))) == \ Mul(S.Half, -1 + sqrt(3), evaluate=False) + 1 A = symbols("A", commutative=False) assert radsimp(x**2 + sqrt(2)*x**2 - sqrt(2)*x*A) == \ x**2 + sqrt(2)*x**2 - sqrt(2)*x*A assert radsimp(1 / sqrt(5 + 2 * sqrt(6))) == -sqrt(2) + sqrt(3) assert radsimp(1 / sqrt(5 + 2 * sqrt(6))**3) == -(-sqrt(3) + sqrt(2))**3 # issue 6532 assert fraction(radsimp(1 / sqrt(x))) == (sqrt(x), x) assert fraction(radsimp(1 / sqrt(2 * x + 3))) == (sqrt(2 * x + 3), 2 * x + 3) assert fraction(radsimp(1 / sqrt(2 * (x + 3)))) == (sqrt(2 * x + 6), 2 * x + 6) # issue 5994 e = S('-(2 + 2*sqrt(2) + 4*2**(1/4))/' '(1 + 2**(3/4) + 3*2**(1/4) + 3*sqrt(2))') assert radsimp(e).expand( ) == -2 * 2**Rational(3, 4) - 2 * 2**Rational(1, 4) + 2 + 2 * sqrt(2) # issue 5986 (modifications to radimp didn't initially recognize this so # the test is included here) assert radsimp(1 / (-sqrt(5) / 2 - S.Half + (-sqrt(5) / 2 - S.Half)**2)) == 1 # from issue 5934 eq = ((-240 * sqrt(2) * sqrt(sqrt(5) + 5) * sqrt(8 * sqrt(5) + 40) - 360 * sqrt(2) * sqrt(-8 * sqrt(5) + 40) * sqrt(-sqrt(5) + 5) - 120 * sqrt(10) * sqrt(-8 * sqrt(5) + 40) * sqrt(-sqrt(5) + 5) + 120 * sqrt(2) * sqrt(-sqrt(5) + 5) * sqrt(8 * sqrt(5) + 40) + 120 * sqrt(2) * sqrt(-8 * sqrt(5) + 40) * sqrt(sqrt(5) + 5) + 120 * sqrt(10) * sqrt(-sqrt(5) + 5) * sqrt(8 * sqrt(5) + 40) + 120 * sqrt(10) * sqrt(-8 * sqrt(5) + 40) * sqrt(sqrt(5) + 5)) / (-36000 - 7200 * sqrt(5) + (12 * sqrt(10) * sqrt(sqrt(5) + 5) + 24 * sqrt(10) * sqrt(-sqrt(5) + 5))**2)) assert radsimp(eq) is S.NaN # it's 0/0 # work with normal form e = 1 / sqrt(sqrt(7) / 7 + 2 * sqrt(2) + 3 * sqrt(3) + 5 * sqrt(5)) + 3 assert radsimp(e) == ( -sqrt(sqrt(7) + 14 * sqrt(2) + 21 * sqrt(3) + 35 * sqrt(5)) * (-11654899 * sqrt(35) - 1577436 * sqrt(210) - 1278438 * sqrt(15) - 1346996 * sqrt(10) + 1635060 * sqrt(6) + 5709765 + 7539830 * sqrt(14) + 8291415 * sqrt(21)) / 1300423175 + 3) # obey power rules base = sqrt(3) - sqrt(2) assert radsimp(1 / base**3) == (sqrt(3) + sqrt(2))**3 assert radsimp(1 / (-base)**3) == -(sqrt(2) + sqrt(3))**3 assert radsimp(1 / (-base)**x) == (-base)**(-x) assert radsimp(1 / base**x) == (sqrt(2) + sqrt(3))**x assert radsimp(root(1 / (-1 - sqrt(2)), -x)) == (-1)**(-1 / x) * (1 + sqrt(2))**(1 / x) # recurse e = cos(1 / (1 + sqrt(2))) assert radsimp(e) == cos(-sqrt(2) + 1) assert radsimp(e / 2) == cos(-sqrt(2) + 1) / 2 assert radsimp(1 / e) == 1 / cos(-sqrt(2) + 1) assert radsimp(2 / e) == 2 / cos(-sqrt(2) + 1) assert fraction(radsimp(e / sqrt(x))) == (sqrt(x) * cos(-sqrt(2) + 1), x) # test that symbolic denominators are not processed r = 1 + sqrt(2) assert radsimp(x / r, symbolic=False) == -x * (-sqrt(2) + 1) assert radsimp(x / (y + r), symbolic=False) == x / (y + 1 + sqrt(2)) assert radsimp(x/(y + r)/r, symbolic=False) == \ -x*(-sqrt(2) + 1)/(y + 1 + sqrt(2)) # issue 7408 eq = sqrt(x) / sqrt(y) assert radsimp(eq) == umul(sqrt(x), sqrt(y), 1 / y) assert radsimp(eq, symbolic=False) == eq # issue 7498 assert radsimp(sqrt(x) / sqrt(y)**3) == umul(sqrt(x), sqrt(y**3), 1 / y**3) # for coverage eq = sqrt(x) / y**2 assert radsimp(eq) == eq
def _eval_rewrite_as_hyper(self, z): pf1 = S.One / (root(3, 6) * gamma(S(2) / 3)) pf2 = z * root(3, 6) / gamma(S(1) / 3) return pf1 * hyper([], [S(2) / 3], z ** 3 / 9) + pf2 * hyper([], [S(4) / 3], z ** 3 / 9)
def _eval_rewrite_as_hyper(self, z): pf1 = S.One / (root(3, 6) * gamma(S(2) / 3)) pf2 = z * root(3, 6) / gamma(S(1) / 3) return pf1 * hyper([], [S(2) / 3], z**3 / 9) + pf2 * hyper( [], [S(4) / 3], z**3 / 9)