Exemplo n.º 1
0
    def _vcjh_fn(self, sym):
        k = self._order
        eta = self._cfg.get('solver-elements-' + self.name, 'vcjh-eta')

        # Expand shorthand forms of eta for common schemes
        etacommon = dict(dg='0', sd='k/(k+1)', hu='(k+1)/k')
        eta_k = sy.S(etacommon.get(eta, eta), locals=dict(k=k))

        lkm1, lk, lkp1 = [sy.legendre_poly(m, sym) for m in [k - 1, k, k + 1]]
        return (sy.S(1)/2 * (lk + (eta_k*lkm1 + lkp1)/(1 + eta_k)))
Exemplo n.º 2
0
def wigner_d(l, m, m_prime, beta, wdsympy=False):
    """Computation of Wigner-d-functions for the rotation of a T-matrix
    
    Args:
        l (int):          Degree :math:`l` (1, ..., lmax)
        m (int):          Order :math:`m` (-min(l,mmax),...,min(l,mmax))
        m_prime (int):    Order :math:`m_prime` (-min(l,mmax),...,min(l,mmax))
        beta (float):     Second Euler angle in rad
        wdsympy (bool):   If True, Wigner-d-functions come from the sympy toolbox 
        
    Returns:
        real value of Wigner-d-function
    """
    wig_d = np.zeros(l + 1, dtype=complex)

    if wdsympy == False:

        if beta < 0:
            aa = m
            bb = m_prime
            m = bb
            m_prime = aa

        if m == 0 and m_prime == 0:
            for nn in range(1, l + 1):
                wig_d[nn] = sympy.legendre_poly(nn, np.cos(beta))
        else:
            # recursion formulation (Mishchenko, Scattering, Absorption and Emission of Light by small Particles, p.365 (B.22 - B.24))
            l_min = max(abs(m), abs(m_prime))
            wig_d[l_min - 1] = 0
            if m_prime >= m:
                zeta = 1
            else:
                zeta = (-1)**(m - m_prime)

            wig_d[l_min] = (
                zeta * 2.0**(-l_min) *
                (factorial(2 * l_min) /
                 (factorial(abs(m - m_prime)) * factorial(abs(m + m_prime))))**
                0.5 * (1 - np.cos(beta))**(abs(m - m_prime) / 2) *
                (1 + np.cos(beta))**(abs(m + m_prime) / 2))

            for ll in range(l_min, l):
                wig_d[ll + 1] = (
                    ((2 * ll + 1) *
                     (ll * (ll + 1) * np.cos(beta) - m * m_prime) * wig_d[ll] -
                     (ll + 1) * (ll**2 - m**2)**0.5 *
                     (ll**2 - m_prime**2)**0.5 * wig_d[ll - 1]) /
                    (ll * ((ll + 1)**2 - m**2)**0.5 *
                     ((ll + 1)**2 - m_prime**2)**0.5))

    else:
        wig_d[l] = complex(Rotation.d(l, m, m_prime, beta).doit())

    return wig_d[l].real
Exemplo n.º 3
0
 def __init__(self, npts):
     # Form a suitable Legendre poly
     Pn = sy.legendre_poly(npts, x)
     dPn = Pn.diff()
     
     # Roots
     self.points = mp.polyroots(map(mp.mpf, sy.Poly(Pn).all_coeffs()))
             
     # Weights
     self.weights = [2/((1 - p**2)*dPn.evalf(mp.dps, subs={x: p})**2)
                     for p in self.points]
Exemplo n.º 4
0
def test_CRootOf_eval_rational():
    p = legendre_poly(4, x, polys=True)
    roots = [r.eval_rational(n=18) for r in p.real_roots()]
    for r in roots:
        assert isinstance(r, Rational)
    roots = [str(r.n(17)) for r in roots]
    assert roots == [
        "-0.86113631159405258",
        "-0.33998104358485626",
        "0.33998104358485626",
        "0.86113631159405258",
    ]
Exemplo n.º 5
0
def test_CRootOf_eval_rational():
    p = legendre_poly(4, x, polys=True)
    roots = [r.eval_rational(n=18) for r in p.real_roots()]
    for r in roots:
        assert isinstance(r, Rational)
    roots = [str(r.n(17)) for r in roots]
    assert roots == [
            "-0.86113631159405258",
            "-0.33998104358485626",
             "0.33998104358485626",
             "0.86113631159405258",
             ]
Exemplo n.º 6
0
    def __init__(self, npts):
        # Form a suitable Legendre poly
        Pn = sy.legendre_poly(npts - 1, x)
        dPn = Pn.diff()

        # Roots
        roots = mp.polyroots(map(mp.mpf, sy.Poly(dPn).all_coeffs()))
        self.points = [mp.mpf(-1)] + roots + [mp.mpf(1)]

        # Weights
        wts0 = mp.mpf(2)/(npts*(npts - 1))
        wtsi = [2/(npts*(npts - 1)*Pn.evalf(mp.dps, subs={x: p})**2)
                for p in self.points[1:-1]]
        self.weights = [wts0] + wtsi + [wts0]
Exemplo n.º 7
0
def test_RootOf_eval_rational():
    p = legendre_poly(4, x, polys=True)
    roots = [r.eval_rational(S(1) / 10**20) for r in p.real_roots()]
    for r in roots:
        assert isinstance(r, Rational)
    # All we know is that the Rational instance will be at most 1/10^20 from
    # the exact root. So if we evaluate to 17 digits, it must be exactly equal
    # to:
    roots = [str(r.n(17)) for r in roots]
    assert roots == [
        "-0.86113631159405258",
        "-0.33998104358485626",
        "0.33998104358485626",
        "0.86113631159405258",
    ]
def test_RootOf_eval_rational():
    p = legendre_poly(4, x, polys=True)
    roots = [r.eval_rational(S(1) / 10**20) for r in p.real_roots()]
    for r in roots:
        assert isinstance(r, Rational)
    # All we know is that the Rational instance will be at most 1/10^20 from
    # the exact root. So if we evaluate to 17 digits, it must be exactly equal
    # to:
    roots = [str(r.n(17)) for r in roots]
    assert roots == [
        "-0.86113631159405258",
        "-0.33998104358485626",
        "0.33998104358485626",
        "0.86113631159405258",
    ]
Exemplo n.º 9
0
def max_rE_gains_3d(order, numeric=True):
    """max rE for a given order is the largest root of the order+1 Legendre
    polynomial"""

    x = sp.symbols('x')
    lp = sp.legendre_poly(order + 1, x)

    # there are more efficient methods to find the roots of the Legendre
    # polynomials, but this is good enough for our purposes
    # See discussion at:
    #   https://math.stackexchange.com/questions/12160/roots-of-legendre-polynomial
    if order < 5 and not numeric:
        roots = sp.roots(lp)
    else:
        roots = sp.nroots(lp)

    # the roots can be in the keys of a dictionary or in a list,
    # this works for either one
    max_rE = np.max([*roots])

    return [sp.legendre(n, max_rE) for n in range(order + 1)]
Exemplo n.º 10
0
def test_CRootOf_evalf():
    real = rootof(x**3 + x + 3, 0).evalf(n=20)

    assert real.epsilon_eq(Float("-1.2134116627622296341"))

    re, im = rootof(x**3 + x + 3, 1).evalf(n=20).as_real_imag()

    assert re.epsilon_eq(Float("0.60670583138111481707"))
    assert im.epsilon_eq(-Float("1.45061224918844152650"))

    re, im = rootof(x**3 + x + 3, 2).evalf(n=20).as_real_imag()

    assert re.epsilon_eq(Float("0.60670583138111481707"))
    assert im.epsilon_eq(Float("1.45061224918844152650"))

    p = legendre_poly(4, x, polys=True)
    roots = [str(r.n(17)) for r in p.real_roots()]
    # magnitudes are given by
    # sqrt(3/S(7) - 2*sqrt(6/S(5))/7)
    #   and
    # sqrt(3/S(7) + 2*sqrt(6/S(5))/7)
    assert roots == [
        "-0.86113631159405258",
        "-0.33998104358485626",
        "0.33998104358485626",
        "0.86113631159405258",
    ]

    re = rootof(x**5 - 5 * x + 12, 0).evalf(n=20)
    assert re.epsilon_eq(Float("-1.84208596619025438271"))

    re, im = rootof(x**5 - 5 * x + 12, 1).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("-0.351854240827371999559"))
    assert im.epsilon_eq(Float("-1.709561043370328882010"))

    re, im = rootof(x**5 - 5 * x + 12, 2).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("-0.351854240827371999559"))
    assert im.epsilon_eq(Float("+1.709561043370328882010"))

    re, im = rootof(x**5 - 5 * x + 12, 3).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("+1.272897223922499190910"))
    assert im.epsilon_eq(Float("-0.719798681483861386681"))

    re, im = rootof(x**5 - 5 * x + 12, 4).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("+1.272897223922499190910"))
    assert im.epsilon_eq(Float("+0.719798681483861386681"))

    # issue 6393
    assert str(rootof(x**5 + 2 * x**4 + x**3 - 68719476736, 0).n(3)) == '147.'
    eq = (531441 * x**11 + 3857868 * x**10 + 13730229 * x**9 +
          32597882 * x**8 + 55077472 * x**7 + 60452000 * x**6 +
          32172064 * x**5 - 4383808 * x**4 - 11942912 * x**3 - 1506304 * x**2 +
          1453312 * x + 512)
    a, b = rootof(eq, 1).n(2).as_real_imag()
    c, d = rootof(eq, 2).n(2).as_real_imag()
    assert a == c
    assert b < d
    assert b == -d
    # issue 6451
    r = rootof(legendre_poly(64, x), 7)
    assert r.n(2) == r.n(100).n(2)
    # issue 8617
    ans = [w.n(2) for w in solve(x**3 - x - 4)]
    assert rootof(exp(x)**3 - exp(x) - 4, 0).n(2) in ans
    # issue 9019
    r0 = rootof(x**2 + 1, 0, radicals=False)
    r1 = rootof(x**2 + 1, 1, radicals=False)
    assert r0.n(4) == -1.0 * I
    assert r1.n(4) == 1.0 * I

    # make sure verification is used in case a max/min traps the "root"
    assert str(rootof(4 * x**5 + 16 * x**3 + 12 * x**2 + 7,
                      0).n(3)) == '-0.976'

    # watch out for UnboundLocalError
    c = CRootOf(90720 * x**6 - 4032 * x**4 + 84 * x**2 - 1, 0)
    assert c._eval_evalf(2)  # doesn't fail

    # watch out for imaginary parts that don't want to evaluate
    assert str(
        RootOf(
            x**16 + 32 * x**14 + 508 * x**12 + 5440 * x**10 + 39510 * x**8 +
            204320 * x**6 + 755548 * x**4 + 1434496 * x**2 + 877969,
            10).n(2)) == '-3.4*I'
    assert abs(RootOf(x**4 + 10 * x**2 + 1, 0).n(2)) < 0.4

    # check reset and args
    r = [RootOf(x**3 + x + 3, i) for i in range(3)]
    r[0]._reset()
    for ri in r:
        i = ri._get_interval()
        n = ri.n(2)
        assert i != ri._get_interval()
        ri._reset()
        assert i == ri._get_interval()
        assert i == i.func(*i.args)
Exemplo n.º 11
0
def test_RootOf_evalf():
    real = RootOf(x**3 + x + 3, 0).evalf(n=20)

    assert real.epsilon_eq(Float("-1.2134116627622296341"))

    re, im = RootOf(x**3 + x + 3, 1).evalf(n=20).as_real_imag()

    assert re.epsilon_eq(Float("0.60670583138111481707"))
    assert im.epsilon_eq(-Float("1.45061224918844152650"))

    re, im = RootOf(x**3 + x + 3, 2).evalf(n=20).as_real_imag()

    assert re.epsilon_eq(Float("0.60670583138111481707"))
    assert im.epsilon_eq(Float("1.45061224918844152650"))

    p = legendre_poly(4, x, polys=True)
    roots = [str(r.n(17)) for r in p.real_roots()]
    assert roots == [
        "-0.86113631159405258",
        "-0.33998104358485626",
        "0.33998104358485626",
        "0.86113631159405258",
    ]

    re = RootOf(x**5 - 5 * x + 12, 0).evalf(n=20)
    assert re.epsilon_eq(Float("-1.84208596619025438271"))

    re, im = RootOf(x**5 - 5 * x + 12, 1).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("-0.351854240827371999559"))
    assert im.epsilon_eq(Float("-1.709561043370328882010"))

    re, im = RootOf(x**5 - 5 * x + 12, 2).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("-0.351854240827371999559"))
    assert im.epsilon_eq(Float("+1.709561043370328882010"))

    re, im = RootOf(x**5 - 5 * x + 12, 3).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("+1.272897223922499190910"))
    assert im.epsilon_eq(Float("-0.719798681483861386681"))

    re, im = RootOf(x**5 - 5 * x + 12, 4).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("+1.272897223922499190910"))
    assert im.epsilon_eq(Float("+0.719798681483861386681"))

    # issue 6393
    assert str(RootOf(x**5 + 2 * x**4 + x**3 - 68719476736, 0).n(3)) == '147.'
    eq = (531441 * x**11 + 3857868 * x**10 + 13730229 * x**9 +
          32597882 * x**8 + 55077472 * x**7 + 60452000 * x**6 +
          32172064 * x**5 - 4383808 * x**4 - 11942912 * x**3 - 1506304 * x**2 +
          1453312 * x + 512)
    a, b = RootOf(eq, 1).n(2).as_real_imag()
    c, d = RootOf(eq, 2).n(2).as_real_imag()
    assert a == c
    assert b < d
    assert b == -d
    # issue 6451
    r = RootOf(legendre_poly(64, x), 7)
    assert r.n(2) == r.n(100).n(2)
    # issue 8617
    ans = [w.n(2) for w in solve(x**3 - x - 4)]
    assert RootOf(exp(x)**3 - exp(x) - 4, 0).n(2) in ans
    # issue 9019
    r0 = RootOf(x**2 + 1, 0, radicals=False)
    r1 = RootOf(x**2 + 1, 1, radicals=False)
    assert r0.n(4) == -1.0 * I
    assert r1.n(4) == 1.0 * I

    # make sure verification is used in case a max/min traps the "root"
    assert str(RootOf(4 * x**5 + 16 * x**3 + 12 * x**2 + 7,
                      0).n(3)) == '-0.976'
Exemplo n.º 12
0
def test_CRootOf_evalf():
    real = rootof(x**3 + x + 3, 0).evalf(n=20)

    assert real.epsilon_eq(Float("-1.2134116627622296341"))

    re, im = rootof(x**3 + x + 3, 1).evalf(n=20).as_real_imag()

    assert re.epsilon_eq( Float("0.60670583138111481707"))
    assert im.epsilon_eq(-Float("1.45061224918844152650"))

    re, im = rootof(x**3 + x + 3, 2).evalf(n=20).as_real_imag()

    assert re.epsilon_eq(Float("0.60670583138111481707"))
    assert im.epsilon_eq(Float("1.45061224918844152650"))

    p = legendre_poly(4, x, polys=True)
    roots = [str(r.n(17)) for r in p.real_roots()]
    # magnitudes are given by
    # sqrt(3/S(7) - 2*sqrt(6/S(5))/7)
    #   and
    # sqrt(3/S(7) + 2*sqrt(6/S(5))/7)
    assert roots == [
            "-0.86113631159405258",
            "-0.33998104358485626",
             "0.33998104358485626",
             "0.86113631159405258",
             ]

    re = rootof(x**5 - 5*x + 12, 0).evalf(n=20)
    assert re.epsilon_eq(Float("-1.84208596619025438271"))

    re, im = rootof(x**5 - 5*x + 12, 1).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("-0.351854240827371999559"))
    assert im.epsilon_eq(Float("-1.709561043370328882010"))

    re, im = rootof(x**5 - 5*x + 12, 2).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("-0.351854240827371999559"))
    assert im.epsilon_eq(Float("+1.709561043370328882010"))

    re, im = rootof(x**5 - 5*x + 12, 3).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("+1.272897223922499190910"))
    assert im.epsilon_eq(Float("-0.719798681483861386681"))

    re, im = rootof(x**5 - 5*x + 12, 4).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("+1.272897223922499190910"))
    assert im.epsilon_eq(Float("+0.719798681483861386681"))

    # issue 6393
    assert str(rootof(x**5 + 2*x**4 + x**3 - 68719476736, 0).n(3)) == '147.'
    eq = (531441*x**11 + 3857868*x**10 + 13730229*x**9 + 32597882*x**8 +
        55077472*x**7 + 60452000*x**6 + 32172064*x**5 - 4383808*x**4 -
        11942912*x**3 - 1506304*x**2 + 1453312*x + 512)
    a, b = rootof(eq, 1).n(2).as_real_imag()
    c, d = rootof(eq, 2).n(2).as_real_imag()
    assert a == c
    assert b < d
    assert b == -d
    # issue 6451
    r = rootof(legendre_poly(64, x), 7)
    assert r.n(2) == r.n(100).n(2)
    # issue 9019
    r0 = rootof(x**2 + 1, 0, radicals=False)
    r1 = rootof(x**2 + 1, 1, radicals=False)
    assert r0.n(4) == -1.0*I
    assert r1.n(4) == 1.0*I

    # make sure verification is used in case a max/min traps the "root"
    assert str(rootof(4*x**5 + 16*x**3 + 12*x**2 + 7, 0).n(3)) == '-0.976'

    # watch out for UnboundLocalError
    c = CRootOf(90720*x**6 - 4032*x**4 + 84*x**2 - 1, 0)
    assert c._eval_evalf(2)  # doesn't fail

    # watch out for imaginary parts that don't want to evaluate
    assert str(RootOf(x**16 + 32*x**14 + 508*x**12 + 5440*x**10 +
        39510*x**8 + 204320*x**6 + 755548*x**4 + 1434496*x**2 +
        877969, 10).n(2)) == '-3.4*I'
    assert abs(RootOf(x**4 + 10*x**2 + 1, 0).n(2)) < 0.4

    # check reset and args
    r = [RootOf(x**3 + x + 3, i) for i in range(3)]
    r[0]._reset()
    for ri in r:
        i = ri._get_interval()
        n = ri.n(2)
        assert i != ri._get_interval()
        ri._reset()
        assert i == ri._get_interval()
        assert i == i.func(*i.args)
def test_RootOf_evalf():
    real = RootOf(x**3 + x + 3, 0).evalf(n=20)

    assert real.epsilon_eq(Float("-1.2134116627622296341"))

    re, im = RootOf(x**3 + x + 3, 1).evalf(n=20).as_real_imag()

    assert re.epsilon_eq(Float("0.60670583138111481707"))
    assert im.epsilon_eq(-Float("1.45061224918844152650"))

    re, im = RootOf(x**3 + x + 3, 2).evalf(n=20).as_real_imag()

    assert re.epsilon_eq(Float("0.60670583138111481707"))
    assert im.epsilon_eq(Float("1.45061224918844152650"))

    p = legendre_poly(4, x, polys=True)
    roots = [str(r.n(17)) for r in p.real_roots()]
    assert roots == [
        "-0.86113631159405258",
        "-0.33998104358485626",
        "0.33998104358485626",
        "0.86113631159405258",
    ]

    re = RootOf(x**5 - 5 * x + 12, 0).evalf(n=20)
    assert re.epsilon_eq(Float("-1.84208596619025438271"))

    re, im = RootOf(x**5 - 5 * x + 12, 1).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("-0.351854240827371999559"))
    assert im.epsilon_eq(Float("-1.709561043370328882010"))

    re, im = RootOf(x**5 - 5 * x + 12, 2).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("-0.351854240827371999559"))
    assert im.epsilon_eq(Float("+1.709561043370328882010"))

    re, im = RootOf(x**5 - 5 * x + 12, 3).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("+1.272897223922499190910"))
    assert im.epsilon_eq(Float("-0.719798681483861386681"))

    re, im = RootOf(x**5 - 5 * x + 12, 4).evalf(n=20).as_real_imag()
    assert re.epsilon_eq(Float("+1.272897223922499190910"))
    assert im.epsilon_eq(Float("+0.719798681483861386681"))

    # issue 6393
    assert str(RootOf(x**5 + 2 * x**4 + x**3 - 68719476736, 0).n(3)) == '147.'
    eq = (
        531441 * x**11 + 3857868 * x**10 + 13730229 * x**9 + 32597882 * x**8 +
        55077472 * x**7 + 60452000 * x**6 + 32172064 * x**5 - 4383808 * x**4 -
        11942912 * x**3 - 1506304 * x**2 + 1453312 * x + 512)
    a, b = RootOf(eq, 1).n(2).as_real_imag()
    c, d = RootOf(eq, 2).n(2).as_real_imag()
    assert a == c
    assert b < d
    assert b == -d
    # issue 6451
    r = RootOf(legendre_poly(64, x), 7)
    assert r.n(2) == r.n(100).n(2)
    # issue 8617
    ans = [w.n(2) for w in solve(x**3 - x - 4)]
    assert RootOf(exp(x)**3 - exp(x) - 4, 0).n(2) in ans

    # make sure verification is used in case a max/min traps the "root"
    assert str(RootOf(4 * x**5 + 16 * x**3 + 12 * x**2 + 7,
                      0).n(3)) == '-0.976'