コード例 #1
0
ファイル: _timing.py プロジェクト: tylerjarvis/RootFinding
def run_single_problem(args):
    """
    Run a root solver on a single problem, using the arguments providedself.

    Parameters
    ----------
    args : parsed arguments
        Determines solver type and polynomial type and size.
    """
    np.random.seed(0)

    polys = [getPoly(args.deg, args.dim, args.power) for _ in range(args.dim)]

    prof = cProfile.Profile()

    prof.enable()
    if args.method in ['mult', 'div']:
        prsolve(polys, {'mult': 0, 'div': -1}[args.method])
    elif args.method == 'bertini':
        bertini(polys)
    prof.disable()

    s = io.StringIO()
    ps = pstats.Stats(prof, stream=s).strip_dirs().sort_stats(args.sort)

    if args.verbosity < 5:
        ps.print_stats(args.verbosity * 20 - 15)
    else:
        ps.print_stats()

    print(s.getvalue())
コード例 #2
0
def test_subdivision_solve_1d():
    #Case 6 - One MultiPower 1D of degrees 10
    #choose a seed that has a zero like ?
    np.random.seed(1)
    a = -np.ones(1)
    b = np.ones(1)
    A = getPoly(20, 1, False)
    correctZeros([A], a, b)
コード例 #3
0
def test_bounding_parallelepiped():
    num_test_cases = 10

    np.random.seed(31)
    A = getPoly(1, 2, True)
    p0, edges = bounding_parallelepiped(A.coeff)
    rand = np.random.rand(edges.shape[1], num_test_cases)
    pts = np.dot(edges, rand).T + p0
    assert np.allclose(A(pts), 0)

    A = getPoly(1, 3, True)
    p0, edges = bounding_parallelepiped(A.coeff)
    rand = np.random.rand(edges.shape[1], num_test_cases)
    pts = np.dot(edges, rand).T + p0
    assert np.allclose(A(pts), 0)

    A = getPoly(1, 6, True)
    p0, edges = bounding_parallelepiped(A.coeff)
    rand = np.random.rand(edges.shape[1], num_test_cases)
    pts = np.dot(edges, rand).T + p0
    assert np.allclose(A(pts), 0)
コード例 #4
0
ファイル: _timing.py プロジェクト: tylerjarvis/RootFinding
def timer(solver, dim, power):
    """Timing a specific root solving method for different polynomial sizes.

    Parameters
    ----------
    solver : function
        The root solving function to test
    power : boolean
        True indicates using power basis, False for chebyshev basis

    Returns
    -------
    degrees : list
        list of polynomial degrees that were timed
    times : list
        list of average times for the solver based on degree
    """
    times = []
    degrees = {
        2: [7, 13, 19, 25, 31, 37, 43, 49],  #,55,61],
        3: [3, 5, 7, 9, 11, 13],
        4: [2, 3, 4],
        5: [2, 3]
    }[dim]
    if solver.__name__ == 'bertini':
        degrees = [i for i in degrees if i < 19]
    for deg in degrees:
        np.random.seed(121 * deg)
        tot_time = 0
        for _ in range(args.trials):
            polys = [getPoly(deg, dim=dim, power=power) for _ in range(dim)]
            start = time.time()
            solver(polys)
            tot_time += time.time() - start
        times.append(tot_time / args.trials)
    return degrees, times
コード例 #5
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
コード例 #6
0
def test_div_cheb_roots():
    '''
    The following tests will run polyroots on relatively small random upper trianguler MultiCheb.
    The assert statements will be inside of the correctZeros helper function.
    '''
    #Case 1 - Two MultiCheb 2D degree 10 polynomials.
    A = getPoly(10, 2, False)
    B = getPoly(10, 2, False)
    correctZeros([A, B], -1)

    #Case 2 - Three MultiCheb 3D degree 4 polynomials.
    A = getPoly(4, 3, False)
    B = getPoly(4, 3, False)
    C = getPoly(4, 3, False)
    correctZeros([A, B, C], -1)

    #Case 3 - Four MultiCheb 4D degree 2 polynomials.
    A = getPoly(2, 4, False)
    B = getPoly(2, 4, False)
    C = getPoly(2, 4, False)
    D = getPoly(2, 4, False)
    correctZeros([A, B, C, D], -1)

    #Case 4 - Two MultiCheb 2D, one degree 5 and one degree 7
    A = getPoly(5, 2, False)
    B = getPoly(7, 2, False)
    correctZeros([A, B], -1)

    #Case 5 - Three MultiCheb 3D of degrees 3,4 and 5
    A = getPoly(3, 3, False)
    B = getPoly(4, 3, False)
    C = getPoly(5, 3, False)
    correctZeros([A, B, C], -1)
コード例 #7
0
def test_power_roots_multrand():
    '''
    The following tests will run polyroots on relatively small random upper trianguler MultiPower.
    The assert statements will be inside of the correctZeros helper function.
    '''

    np.random.seed(423)

    #Case 1 - Two MultiPower 2D degree 10 polynomials.
    A = getPoly(10, 2, True)
    B = getPoly(10, 2, True)
    correctZeros([A, B], 0)

    #Case 2 - Three MultiPower 3D degree 4 polynomials.
    A = getPoly(4, 3, True)
    B = getPoly(4, 3, True)
    C = getPoly(4, 3, True)
    correctZeros([A, B, C], 0)

    #Case 3 - Four MultiPower 4D degree 2 polynomials.
    A = getPoly(2, 4, True)
    B = getPoly(2, 4, True)
    C = getPoly(2, 4, True)
    D = getPoly(2, 4, True)
    correctZeros([A, B, C, D], 0)

    #Case 4 - Two MultiPower 2D, one degree 5 and one degree 7
    A = getPoly(5, 2, True)
    B = getPoly(7, 2, True)
    correctZeros([A, B], 0)

    #Case 5 - Three MultiPower 3D of degrees 3,4 and 5
    A = getPoly(3, 3, True)
    B = getPoly(4, 3, True)
    C = getPoly(5, 3, True)
    correctZeros([A, B, C], 0)
コード例 #8
0
def test_subdivision_solve_polys():
    '''
    The following tests will run subdivision.solve on relatively small random upper trianguler MultiPower.
    The assert statements will be inside of the correctZeros helper function.
    '''
    #Case 1 - Two MultiPower 2D degree 10 polynomials.
    #choose a seed that has a zero like 1,3,7,8,12,20,21,22,22,27,38,41,42,43,46,51,54,55,57,60,65,67,68,69,73,74,78,80,81,84,86,90,95
    np.random.seed(3)
    a = -np.ones(2)
    b = np.ones(2)
    A = getPoly(10, 2, True)
    B = getPoly(10, 2, True)
    correctZeros([A, B], a, b)

    #Case 2 - Three MultiPower 3D degree 4 polynomials.
    # #choose a seed that has a zero like 1,23,27,29,39,43,44,46,51,53,54,68,71,72,93
    np.random.seed(1)
    a = -np.ones(3)
    b = np.ones(3)
    A = getPoly(4, 3, True)
    B = getPoly(4, 3, True)
    C = getPoly(4, 3, True)
    correctZeros([A, B, C], a, b)

    #Case 3 - Four MultiPower 4D degree 2 polynomials.
    #choose a seed that has a zero like 2
    np.random.seed(2)
    a = -np.ones(4)
    b = np.ones(4)
    A = getPoly(2, 4, True)
    B = getPoly(2, 4, True)
    C = getPoly(2, 4, True)
    D = getPoly(2, 4, True)
    correctZeros([A, B, C, D], a, b)

    #Case 4 - Two MultiPower 2D, one degree 20 and one degree 28
    #choose a seed that has a zero like 0,1,2,3,4,5,6,7,8,9,10
    np.random.seed(0)
    a = -np.ones(2)
    b = np.ones(2)
    A = getPoly(20, 2, True)
    B = getPoly(28, 2, True)
    correctZeros([A, B], a, b)

    #Case 5 - Three MultiPower 3D of degrees 3,4 and 5
    #choose a seed that has a zero like 1,3,5,11,13,16,24,28,31,32,33,41,42
    np.random.seed(1)
    a = -np.ones(3)
    b = np.ones(3)
    A = getPoly(3, 3, True)
    B = getPoly(4, 3, True)
    C = getPoly(5, 3, True)
    correctZeros([A, B, C], a, b)
コード例 #9
0
def test_subdivision_solve_with_transform():
    '''
    The following tests will run subdivision.solve on relatively small random upper trianguler MultiPower.
    The assert statements will be inside of the correctZeros helper function.
    The fit occurs on [-2,2]X[-2,2]X..., so a transform is needed.
    '''
    #Case 1 - Two MultiPower 2D degree 10 polynomials.
    #choose a seed that has a zero like 1,3,7,8,12,20,21,22,22,27,38,41,42,43,46,51,54,55,57,60,65,67,68,69,73,74,78,80,81,84,86,90,95
    np.random.seed(1)
    a = -2 * np.ones(2)
    b = 2 * np.ones(2)
    A = getPoly(10, 2, True)
    B = getPoly(10, 2, True)
    correctZeros([A, B], a, b)

    #Case 2 - Three MultiPower 3D degree 4 polynomials.
    #choose a seed that has a zero like 1,23,27,29,39,43,44,46,51,53,54,68,71,72,93
    # np.random.seed(1)
    # a = -2*np.ones(3);b = 2*np.ones(3)
    # A = getPoly(4,3,True)
    # B = getPoly(4,3,True)
    # C = getPoly(4,3,True)
    # correctZeros([A,B,C], a, b)

    #Case 3 - Four MultiPower 4D degree 2 polynomials.
    #choose a seed that has a zero like 21,43,65,72,83
    np.random.seed(21)
    a = -2 * np.ones(4)
    b = 2 * np.ones(4)
    A = getPoly(2, 4, True)
    B = getPoly(2, 4, True)
    C = getPoly(2, 4, True)
    D = getPoly(2, 4, True)
    correctZeros([A, B, C, D], a, b)

    #Case 4 - Two MultiPower 2D, one degree 20 and one degree 28
    #choose a seed that has a zero like 0,1,2,3,4,5,6,7,8,9,10
    # This test slows down with tighter tolerances.
    np.random.seed(1)
    a = -2 * np.ones(2)
    b = 2 * np.ones(2)
    A = getPoly(20, 2, True)
    B = getPoly(28, 2, True)
    correctZeros([A, B], a, b)

    # This case works, but it's really slow
    # Case 5 - Three MultiPower 3D of degrees 3,4 and 5
    # choose a seed that has a zero like 1,3,5,11,13,16,24,28,31,32,33,41,42
    np.random.seed(1)
    a = -2 * np.ones(3)
    b = 2 * np.ones(3)
    A = getPoly(3, 3, True)
    B = getPoly(4, 3, True)
    C = getPoly(5, 3, True)
    correctZeros([A, B, C], a, b)