Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def newton_step(system, solutions, precision='d', decimals=100):
"""
Applies one Newton step to the solutions of the system.
For each solution, prints its last line of diagnostics.
Four levels of precision are supported:
d : standard double precision (1.1e-15 or 2^(-53)),
dd : double double precision (4.9e-32 or 2^(-104)),
qd : quad double precision (1.2e-63 or 2^(-209)).
mp : arbitrary precision, where the number of decimal places
in the working precision is determined by decimals.
"""
if(precision == 'd'):
from interface import store_standard_system
from interface import store_standard_solutions, load_standard_solutions
store_standard_system(system)
store_standard_solutions(len(system), solutions)
from phcpy2c import py2c_standard_Newton_step
py2c_standard_Newton_step()
result = load_standard_solutions()
elif(precision == 'dd'):
from interface import store_dobldobl_system
from interface import store_dobldobl_solutions, load_dobldobl_solutions
store_dobldobl_system(system)
store_dobldobl_solutions(len(system), solutions)
from phcpy2c import py2c_dobldobl_Newton_step
py2c_dobldobl_Newton_step()
result = load_dobldobl_solutions()
elif(precision == 'qd'):
from interface import store_quaddobl_system
from interface import store_quaddobl_solutions, load_quaddobl_solutions
store_quaddobl_system(system)
def initialize_standard_tracker(target, start, fixedgamma=True):
"""
Initializes a path tracker with a generator for a target
and start system given in standard double precision.
If fixedgamma, then gamma will be a fixed default value,
otherwise, a random complex constant for gamma is generated.
"""
from phcpy2c import py2c_copy_container_to_target_system
from phcpy2c import py2c_copy_container_to_start_system
from phcpy2c import py2c_initialize_standard_homotopy
from interface import store_standard_system
store_standard_system(target)
py2c_copy_container_to_target_system()
store_standard_system(start)
py2c_copy_container_to_start_system()
if fixedgamma:
return py2c_initialize_standard_homotopy(1)
else:
return py2c_initialize_standard_homotopy(0)
On return are the string representations of the solutions
computed at the end of the paths.
"""
from phcpy2c import py2c_copy_standard_container_to_target_system
from phcpy2c import py2c_copy_standard_container_to_start_system
from phcpy2c import py2c_copy_standard_container_to_start_solutions
from phcpy2c import py2c_create_standard_homotopy
from phcpy2c import py2c_create_standard_homotopy_with_gamma
from phcpy2c import py2c_solve_by_standard_homotopy_continuation
from phcpy2c import py2c_solcon_clear_standard_solutions
from phcpy2c import py2c_copy_standard_target_solutions_to_container
from interface import store_standard_system
from interface import store_standard_solutions, load_standard_solutions
store_standard_system(target)
py2c_copy_standard_container_to_target_system()
store_standard_system(start)
py2c_copy_standard_container_to_start_system()
# py2c_clear_standard_homotopy()
if(gamma == 0):
py2c_create_standard_homotopy()
else:
py2c_create_standard_homotopy_with_gamma(gamma.real, gamma.imag)
dim = len(start)
store_standard_solutions(dim, sols)
py2c_copy_standard_container_to_start_solutions()
py2c_solve_by_standard_homotopy_continuation(tasks)
py2c_solcon_clear_standard_solutions()
py2c_copy_standard_target_solutions_to_container()
return load_standard_solutions()
def initialize_standard_tracker(target, start, fixedgamma=True):
"""
Initializes a path tracker with a generator for a target
and start system given in standard double precision.
If fixedgamma, then gamma will be a fixed default value,
otherwise, a random complex constant for gamma is generated.
"""
from phcpy2c import py2c_copy_container_to_target_system
from phcpy2c import py2c_copy_container_to_start_system
from phcpy2c import py2c_initialize_standard_homotopy
from interface import store_standard_system
store_standard_system(target)
py2c_copy_container_to_target_system()
store_standard_system(start)
py2c_copy_container_to_start_system()
if fixedgamma:
return py2c_initialize_standard_homotopy(1)
else:
return py2c_initialize_standard_homotopy(0)
def permute_standard_system(pols):
"""
Permutes the equations in the list of polynomials in pols
with coefficients in standard double precision,
along the permutation used in the mixed volume computation.
"""
from phcpy2c import py2c_celcon_permute_standard_system
from interface import store_standard_system, load_standard_system
store_standard_system(pols)
py2c_celcon_permute_standard_system()
return load_standard_system()
def standard_deflate(system, solutions):
"""
The deflation method augments the given system with
derivatives to restore the quadratic convergence of
Newton's method at isolated singular solutions,
in standard double precision.
After application of deflation with default settings,
the new approximate solutions are returned.
"""
from phcpy2c import py2c_standard_deflate
from interface import store_standard_system
from interface import store_standard_solutions, load_standard_solutions
store_standard_system(system)
store_standard_solutions(len(system), solutions)
py2c_standard_deflate()
result = load_standard_solutions()
return result
def standard_usolve(pol, mxi, eps):
"""
Applies the method of Durand-Kerner (aka Weierstrass)
to the polynomial in the string pol, in standard double precision
The maximum number of iterations is in mxi,
the requirement on the accuracy in eps.
"""
from phcpy2c import py2c_usolve_standard
from interface import store_standard_system, load_standard_solutions
store_standard_system([pol])
nit = py2c_usolve_standard(mxi, eps)
rts = load_standard_solutions()
return (nit, rts)
The start is a list of strings representing the polynomials
of the start system, with known solutions in sols.
The sols is a list of strings representing start solutions.
On return are the string representations of the solutions
computed at the end of the paths.
If gamma on input equals zero, then a random complex number is generated,
otherwise the real and imaginary parts of gamma are used.
"""
from phcpy2c import py2c_copy_standard_container_to_target_system
from phcpy2c import py2c_copy_standard_container_to_start_system
from phcpy2c import py2c_ade_manypaths_d
from interface import store_standard_system
from interface import store_standard_solutions, load_standard_solutions
store_standard_system(target)
py2c_copy_standard_container_to_target_system()
store_standard_system(start)
py2c_copy_standard_container_to_start_system()
dim = len(start)
store_standard_solutions(dim, sols)
if(gamma == 0):
from random import uniform
from cmath import exp, pi
angle = uniform(0, 2*pi)
gamma = exp(angle*complex(0,1))
if(verbose > 0):
print 'random gamma constant :', gamma
fail = py2c_ade_manypaths_d(verbose,gamma.real,gamma.imag)
if(fail == 0):
if(verbose > 0):
print 'Path tracking with AD was a success!'
else:
print 'Path tracking with AD failed!'
The target is a list of strings representing the polynomials
of the target system (which has to be solved).
The start is a list of strings representing the polynomials
of the start system, with known solutions in sols.
The sols is a list of strings representing start solutions.
On return are the string representations of the solutions
computed at the end of the paths.
If gamma on input equals zero, then a random complex number is generated,
otherwise the real and imaginary parts of gamma are used.
"""
from phcpy2c import py2c_copy_standard_container_to_target_system
from phcpy2c import py2c_copy_standard_container_to_start_system
from phcpy2c import py2c_ade_manypaths_d
from interface import store_standard_system
from interface import store_standard_solutions, load_standard_solutions
store_standard_system(target)
py2c_copy_standard_container_to_target_system()
store_standard_system(start)
py2c_copy_standard_container_to_start_system()
dim = len(start)
store_standard_solutions(dim, sols)
if(gamma == 0):
from random import uniform
from cmath import exp, pi
angle = uniform(0, 2*pi)
gamma = exp(angle*complex(0,1))
if(verbose > 0):
print 'random gamma constant :', gamma
fail = py2c_ade_manypaths_d(verbose,gamma.real,gamma.imag)
if(fail == 0):
if(verbose > 0):
print 'Path tracking with AD was a success!'