Ejemplo n.º 1
0
    def m_vectors(alms, l):
        """
        Get multipole vectors in spherical coordinates for a multipole given a map.

        Args:
            alms (complex array): multipole moments from a map (Healpy indexing).
            l (int): multipole.

        Returns:
            Float array [theta, phi] in radians.
        """

        # Context for MPSolve:
        ctx = mp.Context()
        poly = mp.MonomialPoly(ctx, 2 * l)

        # Get maximum multipole moment given an array.
        lmax = hp.sphtfunc.Alm.getlmax(len(alms))

        # Polynomial indexes:
        m_array = np.arange(-l, l + 1, 1)  # m values.
        neg_m = m_array[0:l]  # Negative m values.
        pos_m = m_array[l:]  # Positive m values.

        # Binomial terms:
        binom_term = [sqrt(bincoef(2 * l, l + i)) for i in range(-l, l + 1)]

        # Indexes for all alms list:
        index = hp.sphtfunc.Alm.getidx(lmax, l, abs(m_array))
        alm_ell_real = np.real(alms[index])
        alm_ell_imag = np.imag(alms[index])

        # Negative imaginary part for m:
        neg_rea_m_term = [(-1)**int(i) for i in neg_m]
        # Positive imaginary part for m:
        neg_imag_m_term = [-((-1)**int(i)) for i in neg_m]

        # Join all real signs:
        rea_sign = np.concatenate((neg_rea_m_term, np.ones(l + 1)))
        # Join all imaginary signs:
        imag_sign = np.concatenate((neg_imag_m_term, np.ones(l + 1)))

        rea = rea_sign * alm_ell_real * binom_term  # Real terms.
        ima = imag_sign * alm_ell_imag * binom_term  # Imaginary terms.

        # Set coefficients:
        [
            poly.set_coefficient(i, str(rea[i]), str(ima[i]))
            for i in range(2 * l + 1)
        ]

        # Getting roots:
        ctx.solve(poly, algorithm=mp.Algorithm.SECULAR_GA)
        roots = np.array(ctx.get_roots())

        phi = np.angle(roots)
        r = np.absolute(roots)
        theta = 2 * np.arctan(1 / r)

        return np.vstack((theta, phi)).T
Ejemplo n.º 2
0
def rootsofunity():
    """Solve the polynomial x^n - 1 using rational 
    input for MPSolve"""
    ctx = mpsolve.Context()
    poly = mpsolve.MonomialPoly(ctx, 8)

    poly.set_coefficient(0, "-1")
    poly.set_coefficient(8, "1")

    print("Roots of x^8 - 1 = %s" % ("  ".join(map(str, ctx.solve(poly)))))
Ejemplo n.º 3
0
def roots_of_unity(n):
    """Compute the n-th roots of unity, i.e., the solutions
    of the equation x^n = 1"""

    ctx = mpsolve.Context()
    poly = mpsolve.MonomialPoly(ctx, n)

    poly.set_coefficient(n, 1)
    poly.set_coefficient(0, -1)

    return ctx.solve(poly)
Ejemplo n.º 4
0
def simple_polynomial_float_coefficients():
    n = 3
    ctx = mpsolve.Context()
    poly = mpsolve.MonomialPoly(ctx, n)

    poly.set_coefficient(0, -1.0)
    poly.set_coefficient(1, 0.0)
    poly.set_coefficient(2, 2.5)
    ctx.solve(poly)

    print("")
Ejemplo n.º 5
0
def simple_polynomial_int_coefficients():
    n = 2
    ctx = mpsolve.Context()
    poly = mpsolve.MonomialPoly(ctx, n)

    poly.set_coefficient(0, -1)
    poly.set_coefficient(1, 0)
    poly.set_coefficient(2, 2)
    ctx.solve(poly)

    print("")
Ejemplo n.º 6
0
def simple_chebyshev_example():
    """Here is a simple example of a polynomial expressed in the
    Chebyshev basis."""

    n = 100
    ctx = mpsolve.Context()
    poly = mpsolve.ChebyshevPoly(ctx, n)

    poly.set_coefficient(n, 1)

    for root in ctx.solve(poly):
        print(root)
Ejemplo n.º 7
0
def simple_polynomial_example():
    """Solve a very simple polynomial. """

    n = 100
    ctx = mpsolve.Context()
    poly = mpsolve.MonomialPoly(ctx, n)

    poly.set_coefficient(0, -1)
    poly.set_coefficient(n, 1)

    print("x^%d - 1 roots: " % n)
    for root in ctx.solve(poly):
        print(" - " + str(root))

    print("")
Ejemplo n.º 8
0
from time import time
from sys import argv
#
if 'M' not in locals():
    if len(argv) < 2:
        M = 20
    else:
        M = int(argv[1])

phi = [1]
for m in range(M):
    phi0 = list(phi)
    phi = [0] + phi0
    phi[:m+1] = [x - (m + 1)*y for x, y in zip(phi, phi0)]
del phi0
ctx = mps.Context()
poly = mps.MonomialPoly(ctx, M)
for k in range(len(phi)):
    poly.set_coefficient(k, "{:d}".format(phi[k]))
cf = poly.get_coefficients()
# cf_err = [abs(x - y) for x,y in zip(cf, phi)]
# print("\nget_coefficient() error: {}\n".format(max(cf_err)))
t_begin = time()
roots = ctx.solve(poly)
t_end = time()
roots = sorted(roots, key=lambda x: x.real)
err = [abs(x - y - 1) for x, y in zip(roots, range(M))]
radii = ctx.get_inclusion_radii()
print("Roots:\t\t\t\t\tError\t\tInclusion")
print("real part\t imaginary part\t\t\t\tradius")
print("="*65)