當前位置: 首頁>>代碼示例>>Python>>正文


Python optimize.newton_krylov方法代碼示例

本文整理匯總了Python中scipy.optimize.newton_krylov方法的典型用法代碼示例。如果您正苦於以下問題:Python optimize.newton_krylov方法的具體用法?Python optimize.newton_krylov怎麽用?Python optimize.newton_krylov使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在scipy.optimize的用法示例。


在下文中一共展示了optimize.newton_krylov方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: solve

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import newton_krylov [as 別名]
def solve(self):
    """Solve the selfconsistent problem"""
    return
    self.iterate() # first iteration
    def r2z(v): # convert real to complex
      n = v.shape[0]//2
      return v[0:n] + 1j*v[n:2*n]
    def z2r(v): # convert complex to real
      return np.concatenate([v.real,v.imag])

    def fopt(cij): # function to return the error
      self.cij = r2z(cij) # store this vector
      self.cij2v()  # update the v vectors
      self.iterate() # do an iteration
      print(self.cij)
      self.v2cij() # convert the v to cij
      return z2r(self.cij)-cij
    cij = optimize.broyden1(fopt,z2r(self.cij),f_tol=1e-8,max_rank=10)
#    cij = optimize.fsolve(fopt,z2r(self.cij),xtol=1e-8)
#    cij = optimize.anderson(fopt,z2r(self.cij),f_tol=1e-6,w0=0.1)
#    cij = optimize.newton_krylov(fopt,z2r(self.cij),f_tol=1e-6,outer_k=8)
    self.cij = cij
    self.cij2v() # update 
開發者ID:joselado,項目名稱:quantum-honeycomp,代碼行數:25,代碼來源:scftypes.py

示例2: newton_krylov_solve

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import newton_krylov [as 別名]
def newton_krylov_solve(simulation, Estart=None, conv_threshold=1e-10, max_num_iter=50,
				 averaging=True, solver=DEFAULT_SOLVER, jac_solver='c2r',
				 matrix_format=DEFAULT_MATRIX_FORMAT):
	# THIS DOESNT WORK YET! 

	# Stores convergence parameters
	conv_array = np.zeros((max_num_iter, 1))

	# num. columns and rows of A
	Nbig = simulation.Nx*simulation.Ny

	# Defne the starting field for the simulation
	if Estart is None:
		if simulation.fields['Ez'] is None:
			(_, _, E0) = simulation.solve_fields()
		else:
			E0 = deepcopy(simulation.fields['Ez'])
	else:
		E0 = Estart

	E0 = np.reshape(E0, (-1,))

	# funtion for roots
	def _f(E):
		E = np.reshape(E, simulation.eps_r.shape)
		fx = nl_eq_and_jac(simulation, Ez=E, compute_jac=False, matrix_format=matrix_format)
		return np.reshape(fx, (-1,))

	print(_f(E0))

	E_nl = newton_krylov(_f, E0, verbose=True)

	# I'm returning these in place of Hx and Hy to not break things
	return (E_nl, E_nl, E_nl, conv_array) 
開發者ID:fancompute,項目名稱:angler,代碼行數:36,代碼來源:nonlinear_solvers.py

示例3: attractive_hubbard

# 需要導入模塊: from scipy import optimize [as 別名]
# 或者: from scipy.optimize import newton_krylov [as 別名]
def attractive_hubbard(h0,mf=None,mix=0.9,g=0.0,nk=8,solver="plain",
        maxerror=1e-5,**kwargs):
    """Perform the SCF mean field"""
    if not h0.check_mode("spinless"): raise # sanity check
    h = h0.copy() # initial Hamiltonian
    if mf is None:
      try: dold = inout.load(mf_file) # load the file
      except: dold = np.random.random(h.intra.shape[0]) # random guess
    else: dold = mf # initial guess
    ii = 0
    os.system("rm -f STOP") # remove stop file
    def f(dold):
      """Function to minimize"""
#      print("Iteration #",ii) # Iteration
      h = h0.copy() # copy Hamiltonian
      if os.path.exists("STOP"): return dold
      h.add_swave(dold*g) # add the pairing to the Hamiltonian
      t0 = time.time()
      d = onsite_delta_vev(h,nk=nk,**kwargs) # compute the pairing
      t1 = time.time()
      print("Time in this iteration = ",t1-t0) # Difference
      diff = np.max(np.abs(d-dold)) # compute the difference
      print("Error = ",diff) # Difference
      print("Average Pairing = ",np.mean(np.abs(d))) # Pairing
      print("Maximum Pairing = ",np.max(np.abs(d))) # Pairing
      print()
#      ii += 1
      return d
    if solver=="plain":
      do_scf = True
      while do_scf:
        d = f(dold) # new vector
        dold = mix*d + (1-mix)*dold # redefine
        diff = np.max(np.abs(d-dold)) # compute the difference
        if diff<maxerror: 
          do_scf = False
    else:
        print("Solver used:",solver)
        import scipy.optimize as optimize 
        if solver=="newton": fsolver = optimize.newton_krylov
        elif solver=="anderson": fsolver = optimize.anderson
        elif solver=="broyden": fsolver = optimize.broyden2
        elif solver=="linear": fsolver = optimize.linearmixing
        else: raise
        def fsol(x): return x - f(x) # function to solve
        dold = fsolver(fsol,dold,f_tol=maxerror)
    h = h0.copy() # copy Hamiltonian
    h.add_swave(dold*g) # add the pairing to the Hamiltonian
    inout.save(dold,mf_file) # save the mean field
    scf = SCF() # create object
    scf.hamiltonian = h # store
    return scf # return SCF object 
開發者ID:joselado,項目名稱:quantum-honeycomp,代碼行數:54,代碼來源:attractive_hubbard_spinless.py


注:本文中的scipy.optimize.newton_krylov方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。