Пример #1
0
 def test_chebfromroots(self) :
     res = cheb.chebfromroots([])
     assert_almost_equal(trim(res), [1])
     for i in range(1,5) :
         roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
         tgt = [0]*i + [1]
         res = cheb.chebfromroots(roots)*2**(i-1)
         assert_almost_equal(trim(res),trim(tgt))
Пример #2
0
 def test_chebroots(self) :
     assert_almost_equal(cheb.chebroots([1]), [])
     assert_almost_equal(cheb.chebroots([1, 2]), [-.5])
     for i in range(2,5) :
         tgt = np.linspace(-1, 1, i)
         res = cheb.chebroots(cheb.chebfromroots(tgt))
         assert_almost_equal(trim(res), trim(tgt))
Пример #3
0
def chebfromroots(roots) :
    from numpy.polynomial.chebyshev import chebfromroots
    return chebfromroots(roots)
Пример #4
0
def run_n_dimension(args, radius, eigvals):
    num_points = args.num_points
    eps = args.eps
    power = args.power
    real = args.real
    by_coeffs = args.coeffs
    dim = args.dimension

    root_pts = {}
    residuals = {}
    powerpolys = []
    chebpolys = []
    if by_coeffs:
        for i in range(dim):
            from yroots.polynomial import getPoly
            powerpolys.append(getPoly(num_points, dim, power=True))
            chebpolys.append(getPoly(num_points, dim, power=False))
    else:
        r = np.random.random((num_points, dim)) * radius + eps
        roots = 2 * r - radius

        root_pts = {'roots': np.array(list(product(*np.rot90(roots))))}

        for i in range(dim):
            coeffs = np.zeros((num_points + 1, ) * dim)
            idx = [
                slice(None),
            ] * dim
            idx[i] = 0
            coeffs[tuple(idx)] = polyfromroots(roots[:, i])
            lt = [0] * dim
            lt[i] = num_points
            powerpolys.append(
                MultiPower(coeffs))  #, lead_term=lt, clean_zeros=False))

            coeffs[tuple(idx)] = chebfromroots(roots[:, i])
            chebpolys.append(
                MultiCheb(coeffs))  #, lead_term=lt, clean_zeros=False))
            # plt.subplot(121);plt.imshow(coeffs);plt.subplot(122);plt.imshow(chebpolys[0].coeff);plt.show()

    for solver in all_solvers:
        if not isinstance(solver, OneDSolver) and solver.basis in [
                'power', 'both'
        ]:
            # if ((not eigvals) or solver.eigvals):
            name = str(solver) + ' Power'
            root_pts[name] = solver(powerpolys)
            residuals[name] = maximal_residual(powerpolys, root_pts[name])

    for solver in all_solvers:
        if not isinstance(solver, OneDSolver) and solver.basis in [
                'cheb', 'both'
        ]:
            # if ((not eigvals) or solver.eigvals):
            name = str(solver) + ' Cheb'
            root_pts[name] = solver(chebpolys)
            residuals[name] = maximal_residual(chebpolys, root_pts[name])

    if args.hist:
        evaluations = {}
        for k, v in root_pts.items():
            if k == 'roots': continue
            # evaluations[k] = []
            polys = powerpolys if 'Power' in k else chebpolys
            # for poly in polys:
            evaluations[k] = sum(np.abs(poly(root_pts[k])) for poly in polys)
        ncols = len(evaluations)
        # plt.figure(figsize=(12,6))
        fig, ax = plt.subplots(1, ncols, sharey=True, figsize=(12, 4))
        minimal = -15
        maximal = 1
        for i, (k, v) in enumerate(evaluations.items()):
            ax[i].hist(np.clip(np.log10(v), minimal, maximal),
                       range=(minimal, maximal),
                       bins=40)
            ax[i].set_xlabel(r'$log_{10}(p(r_i))$')
            ax[i].set_title(k)
        plt.suptitle("Eigenvalues" if eigvals else "Eigenvectors")
        plt.show()

    return root_pts, residuals
Пример #5
0
def run_one_dimension(args, radius, eigvals):
    num_points = args.num_points
    eps = args.eps
    power = args.power
    real = args.real
    by_coeffs = args.coeffs

    root_pts = {}
    residuals = {}
    if by_coeffs:
        coeffs = (np.random.random(num_points + 1) * 2 - 1) * radius
        powerpoly = MultiPower(coeffs)
        chebpoly = MultiCheb(coeffs)
    else:
        r = np.sqrt(np.random.random(num_points)) * radius + eps
        angles = 2 * np.pi * np.random.random(num_points)
        if power and not real:
            roots = r * np.exp(angles * 1j)
        else:
            roots = 2 * r - radius
        root_pts = {'roots': roots}

        powerpoly = MultiPower(polyfromroots(roots))
        chebpoly = MultiCheb(chebfromroots(roots))
    n = 1000
    x = np.linspace(-1, 1, n)
    X, Y = np.meshgrid(x, 1j * x)

    for solver in all_solvers:
        if isinstance(solver, OneDSolver):
            if (solver.basis == 'cheb' and args.cheb) and ((not eigvals)
                                                           or solver.eigvals):
                name = str(solver)
                root_pts[name] = solver(chebpoly, eigvals)
                residuals[name] = maximal_residual(chebpoly, root_pts[name])
            if (solver.basis == 'power'
                    and args.power) and ((not eigvals) or solver.eigvals):
                name = str(solver)
                root_pts[name] = solver(powerpoly, eigvals)
                residuals[name] = maximal_residual(powerpoly, root_pts[name])

    if args.hist:
        evaluations = {}
        for k, v in root_pts.items():
            if k == 'roots': continue
            poly = powerpoly if 'power' in k else chebpoly
            evaluations[k] = np.abs(poly(root_pts[k]))
        ncols = len(evaluations)
        fig, ax = plt.subplots(1, ncols, sharey=True, figsize=(12, 4))
        minimal = -20
        maximal = 1
        for i, (k, v) in enumerate(evaluations.items()):
            ax[i].hist(np.clip(np.log10(v), minimal, maximal),
                       range=(minimal, maximal),
                       bins=40)
            ax[i].set_xlabel(r'$log_{10}(p(r_i))$')
            ax[i].set_title(k)
        plt.suptitle("Eigenvalues" if eigvals else "Eigenvectors")
        plt.show()

    return root_pts, residuals
Пример #6
0
def chebfromroots(roots):
    from numpy.polynomial.chebyshev import chebfromroots
    return chebfromroots(roots)