コード例 #1
0
ファイル: test_eval_power.py プロジェクト: Blendify/diofant
def test_sympyissue_6208():
    assert sqrt(33**(9*I/10)) == -33**(9*I/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)
コード例 #2
0
ファイル: test_eval_power.py プロジェクト: skirpichev/diofant
def test_sympyissue_6208():
    assert sqrt(33**(9*I/10)) == -33**(9*I/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)
コード例 #3
0
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
コード例 #4
0
ファイル: test_eval_power.py プロジェクト: Blendify/diofant
def test_sympyissue_12578():
    s = root(1 - ((x - 1/x)/2)**(-4), 8)
    assert s.series(x, n=17) == (1 - 2*x**4 - 8*x**6 - 34*x**8 -
                                 152*x**10 - 714*x**12 - 3472*x**14 -
                                 17318*x**16 + O(x**17))
    d10 = s.diff(x, 10)
    assert d10.limit(x, 0) == -551577600
コード例 #5
0
ファイル: test_eval_power.py プロジェクト: skirpichev/diofant
def test_sympyissue_12578():
    s = root(1 - ((x - 1/x)/2)**(-4), 8)
    assert s.series(x, n=17) == (1 - 2*x**4 - 8*x**6 - 34*x**8 -
                                 152*x**10 - 714*x**12 - 3472*x**14 -
                                 17318*x**16 + O(x**17))
    d10 = s.diff(x, 10)
    assert d10.limit(x, 0) == -551577600
コード例 #6
0
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 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
コード例 #7
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**Rational(1, 3) * x *
                     Abs(sin(2 * pi *
                             (n + S.One) / Integer(3))) * factorial(
                                 (n - S.One) / Integer(3)) /
                     ((n + S.One) *
                      Abs(cos(2 * pi *
                              (n + S.Half) / Integer(3))) * factorial(
                                  (n - 2) / Integer(3))) * p)
         else:
             return (S.One / (root(3, 6) * pi) * gamma(
                 (n + S.One) / Integer(3)) *
                     Abs(sin(2 * pi * (n + S.One) / Integer(3))) /
                     factorial(n) * (root(3, 3) * x)**n)
コード例 #8
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**Rational(1, 3) * x)**(-n) *
                     (3**Rational(1, 3) * x)**(n + 1) *
                     sin(pi * (2 * n / 3 + Rational(4, 3))) * factorial(n) *
                     gamma(n / 3 + Rational(2, 3)) /
                     (sin(pi * (2 * n / 3 + Rational(2, 3))) *
                      factorial(n + 1) * gamma(n / 3 + Rational(1, 3))) * p)
         else:
             return (S.One / (3**Rational(2, 3) * pi) * gamma(
                 (n + S.One) / Integer(3)) * sin(2 * pi *
                                                 (n + S.One) / Integer(3)) /
                     factorial(n) * (root(3, 3) * x)**n)
コード例 #9
0
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)
コード例 #10
0
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) is S.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_extended_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_extended_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 = Integer(1)
        u2 = -S.Half + coeff
        u3 = -S.Half - coeff
        a, b, c, d = Integer(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
コード例 #11
0
def test_root():
    from diofant.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)
コード例 #12
0
 def _eval_rewrite_as_hyper(self, z):
     pf1 = S.One / (3**Rational(2, 3) * gamma(Rational(2, 3)))
     pf2 = z / (root(3, 3) * gamma(Rational(1, 3)))
     return pf1 * hyper([], [Rational(2, 3)], z**3 / 9) - pf2 * hyper(
         [], [Rational(4, 3)], z**3 / 9)
コード例 #13
0
 def _eval_rewrite_as_hyper(self, z):
     pf1 = z**2 / (2 * root(3, 6) * gamma(Rational(2, 3)))
     pf2 = root(3, 6) / gamma(Rational(1, 3))
     return pf1 * hyper([], [Rational(5, 3)], z**3 / 9) + pf2 * hyper(
         [], [Rational(1, 3)], z**3 / 9)
コード例 #14
0
 def _eval_rewrite_as_hyper(self, z):
     pf1 = z**2 / (2 * 3**Rational(2, 3) * gamma(Rational(2, 3)))
     pf2 = 1 / (root(3, 3) * gamma(Rational(1, 3)))
     return pf1 * hyper([], [Rational(5, 3)], z**3 / 9) - pf2 * hyper(
         [], [Rational(1, 3)], z**3 / 9)