Exemplo n.º 1
0
def wiz_sols_to_phc_sols(sols):
    result = []
    poly_vars = sols[0][0]
    for sol in sols:
        sol_coords = [complex(coord) for coord in sol[1]]
        result.append(make_solution(poly_vars, sol_coords))
    return result
Exemplo n.º 2
0
def extend_solutions(sols):
    """
    To each solution in sols, adds L1 and L2 with values 1,
    and zz2 and zz3 with values zero.
    """
    from phcpy.solutions import make_solution, coordinates
    result = []
    for sol in sols:
        (vars, vals) = coordinates(sol)
        vars = vars + ['L1', 'L2', 'zz2', 'zz3']
        vals = vals + [1, 1, 0, 0]
        extsol = make_solution(vars, vals)
        result.append(extsol)
    return result
Exemplo n.º 3
0
def membership_test(witsys, witsols, verbose=True):
    """
    Given a witness sets in the tuple witset,
    runs a membership test on a solution.
    """
    from phcpy.solutions import make_solution
    from phcpy.sets import is_member
    point = make_solution(['x', 'y', 's'], [2, 2, 1])
    print 'testing the point\n', point
    ismb = is_member(witsys, witsols, 1, point, verbose=False)
    if ismb:
        print 'the point is a member'
    else:
        print 'the point is NOT a member'
    return ismb
Exemplo n.º 4
0
def test_newton_laurent():
    """
    Tests Newton's method on simple Laurent system,
    refining the square root of 2 with increasing precision.
    """
    laurpols = ['x - y^-1;', 'x^2 - 2;']
    from phcpy.solutions import make_solution   
    sol = make_solution(['x', 'y'], [1.414, 0.707])
    sols = [sol]
    print 'start solution :\n', sols[0]
    for k in range(1, 4):
        sols = newton_laurent_step(laurpols, sols)
        print 'at step', k, ':\n', sols[0]
    for k in range(4, 6):
        sols = newton_laurent_step(laurpols, sols, precision='dd')
        print 'at step', k, ':\n', sols[0]
    for k in range(6, 8):
        sols = newton_laurent_step(laurpols, sols, precision='qd')
        print 'at step', k, ':\n', sols[0]
    for k in range(8, 10):
        sols = newton_laurent_step(laurpols, sols, precision='mp')
        print 'at step', k, ':\n', sols[0]
Exemplo n.º 5
0
def special_solutions(pols, slope):
    """
    Given in pols the polynomials for the line intersecting a circle
    for a general slope s, solves the problem for a special numerical
    value for s, given in the slope.
    The value of the slope is added as last coordinate to each solution.
    """
    from phcpy.solver import solve
    from phcpy.solutions import make_solution, coordinates
    special = []
    for pol in pols:
        rpl = pol.replace('s', '(' + str(slope) + ')')
        special.append(rpl)
    sols = solve(special, silent=True)
    result = []
    for sol in sols:
        (vars, vals) = coordinates(sol)
        vars.append('s')
        vals.append(slope)
        extsol = make_solution(vars, vals)
        result.append(extsol)
    return result
Exemplo n.º 6
0
def test_newton_laurent():
    """
    Tests Newton's method on simple Laurent system,
    refining the square root of 2 with increasing precision.
    """
    laurpols = ['x - y^-1;', 'x^2 - 2;']
    from phcpy.solutions import make_solution
    sol = make_solution(['x', 'y'], [1.414, 0.707])
    sols = [sol]
    print('start solution :\n', sols[0])
    for k in range(1, 4):
        sols = newton_laurent_step(laurpols, sols)
        print('at step', k, ':\n', sols[0])
    for k in range(4, 6):
        sols = newton_laurent_step(laurpols, sols, precision='dd')
        print('at step', k, ':\n', sols[0])
    for k in range(6, 8):
        sols = newton_laurent_step(laurpols, sols, precision='qd')
        print('at step', k, ':\n', sols[0])
    for k in range(8, 10):
        sols = newton_laurent_step(laurpols, sols, precision='mp')
        print('at step', k, ':\n', sols[0])
Exemplo n.º 7
0
def extend_solutions(sols, dim, pivots):
    """
    Adds one extra zero coordinate to all solutions in sols,
    corresponding to the rightmost nonfull pivot.
    """
    from phcpy.solutions import strsol2dict, variables, make_solution
    piv = rightmost_nonfull_pivot(dim, pivots)
    print '-> in extend_solutions, piv =', piv
    if (piv < 0):
        result = sols
    else:
        rown = pivots[piv] + 1  # row number of new variable
        name = 'x_' + str(rown + 1) + '_' + str(piv + 1)
        print '-> the name of the new variable :', name
        result = []
        for sol in sols:
            dicsol = strsol2dict(sol)
            solvar = variables(dicsol)
            solval = [dicsol[var] for var in solvar]
            solvar.append(name)
            solval.append(complex(0.0))
            result.append(make_solution(solvar, solval))
    return result
Exemplo n.º 8
0
def extend_solutions(sols, dim, pivots):
    """
    Adds one extra zero coordinate to all solutions in sols,
    corresponding to the rightmost nonfull pivot.
    """
    from phcpy.solutions import strsol2dict, variables, make_solution
    piv = rightmost_nonfull_pivot(dim, pivots)
    print '-> in extend_solutions, piv =', piv
    if(piv < 0):
        result = sols
    else:
        rown = pivots[piv] + 1  # row number of new variable 
        name = 'x_' + str(rown+1) + '_' + str(piv+1)
        print '-> the name of the new variable :', name
        result = []
        for sol in sols:
            dicsol = strsol2dict(sol)
            solvar = variables(dicsol)
            solval = [dicsol[var] for var in solvar] 
            solvar.append(name)
            solval.append(complex(0.0))
            result.append(make_solution(solvar, solval))
    return result
Exemplo n.º 9
0
def embedtofile(embsys, point, addslack=True):
    """
    Prompts the user for a file name and writes the
    embedded system in embsys and the point to file.
    If addslack, then slack variables will be used
    to square the system.
    """
    name = raw_input('Give a file name : ')
    file = open(name, 'w')
    if addslack:
        file.write(str(len(embsys)) + '\n')
    else:
        file.write(str(len(embsys)) + ' ' + str(len(point)) + '\n')
    for pol in embsys:
        file.write(pol + '\n')
    from phcpy.solutions import make_solution
    vars = []  # symbols for the variables
    vals = []  # values for the variables
    for k in range(len(point)):
        vars.append('x' + str(k))
        vals.append(point[k])
    dim = len(embsys) - len(point)
    if addslack:
        for k in range(1, dim + 1):
            vars.append('zz' + str(k))
            vals.append(complex(0))
    print 'vars = \n', vars
    print 'vals = \n', vals
    sol = make_solution(vars, vals)
    n = len(point)
    file.write('\nTITLE : sample of tropical Backelin cyclic %d-roots\n' % n)
    file.write('\nTHE SOLUTIONS :\n')
    file.write('1 ' + str(len(vars)) + '\n')
    file.write('====================================================\n')
    file.write('solution 1 :\n')
    file.write(sol + '\n')
Exemplo n.º 10
0
def embedtofile(embsys, point, addslack=True):
    """
    Prompts the user for a file name and writes the
    embedded system in embsys and the point to file.
    If addslack, then slack variables will be used
    to square the system.
    """
    name = raw_input('Give a file name : ')
    file = open(name, 'w')
    if addslack:
        file.write(str(len(embsys)) + '\n')
    else:
        file.write(str(len(embsys)) + ' ' + str(len(point))+ '\n')
    for pol in embsys:
        file.write(pol + '\n')
    from phcpy.solutions import make_solution
    vars = []  # symbols for the variables
    vals = []  # values for the variables
    for k in range(len(point)):
        vars.append('x' + str(k))
        vals.append(point[k])
    dim = len(embsys) - len(point)
    if addslack:
        for k in range(1, dim+1):
            vars.append('zz' + str(k))
            vals.append(complex(0))
    print 'vars = \n', vars
    print 'vals = \n', vals
    sol = make_solution(vars, vals)
    n = len(point)
    file.write('\nTITLE : sample of tropical Backelin cyclic %d-roots\n' % n)
    file.write('\nTHE SOLUTIONS :\n')
    file.write('1 ' + str(len(vars)) + '\n')
    file.write('====================================================\n')
    file.write('solution 1 :\n')
    file.write(sol + '\n')
Exemplo n.º 11
0
"""
We illustrate deflation on the example of Griewank and Osborne.
The point (0, 0) is an irregular singularity and Newton fails
to converge even if we start already quite close to (0, 0).
Deflation solves this problem.
"""
p = ['(29/16)*x^3 - 2*x*y;', 'x^2 - y;']
from phcpy.solutions import make_solution
sol = make_solution(['x', 'y'],[1.0e-6, 1.0e-6])
print 'the initial solution :'
print sol
from phcpy.solver import newton_step
from phcpy.solutions import diagnostics
sols = [sol]
for k in range(5):
    sols = newton_step(p, sols)
    (err, rco, res) = diagnostics(sols[0])
    print 'forward error :', err
    print 'estimate for inverse condition :', rco
    print 'backward error :', res
print 'the solution after five Newton steps :'
print sols[0]
from phcpy.solver import standard_deflate
sols = standard_deflate(p, sols)
print 'after deflation :'
print sols[0]