本文整理汇总了Python中scipy.optimize.golden方法的典型用法代码示例。如果您正苦于以下问题:Python optimize.golden方法的具体用法?Python optimize.golden怎么用?Python optimize.golden使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.optimize
的用法示例。
在下文中一共展示了optimize.golden方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_golden
# 需要导入模块: from scipy import optimize [as 别名]
# 或者: from scipy.optimize import golden [as 别名]
def test_golden(self):
x = optimize.golden(self.fun)
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.golden(self.fun, brack=(-3, -2))
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.golden(self.fun, full_output=True)
assert_allclose(x[0], self.solution, atol=1e-6)
x = optimize.golden(self.fun, brack=(-15, -1, 15))
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.golden(self.fun, tol=0)
assert_allclose(x, self.solution)
maxiter_test_cases = [0, 1, 5]
for maxiter in maxiter_test_cases:
x0 = optimize.golden(self.fun, maxiter=0, full_output=True)
x = optimize.golden(self.fun, maxiter=maxiter, full_output=True)
nfev0, nfev = x0[2], x[2]
assert_equal(nfev - nfev0, maxiter)
示例2: P_dew_at_T
# 需要导入模块: from scipy import optimize [as 别名]
# 或者: from scipy.optimize import golden [as 别名]
def P_dew_at_T(self, T, zs, Psats=None):
Psats = self._Psats(Psats, T)
Pmax = self.P_bubble_at_T(T, zs, Psats)
diff = 1E-7
# EOSs do not solve at very low pressure
if self.use_phis:
Pmin = max(Pmax*diff, 1)
else:
Pmin = Pmax*diff
P_dew = brenth(self._T_VF_err, Pmin, Pmax, args=(T, zs, Psats, Pmax, 1))
self.__TVF_solve_cache = None
return P_dew
# try:
# return brent(self._dew_P_UNIFAC_err, args=(T, zs, Psats, Pmax), brack=(Pmax*diff, Pmax*(1-diff), Pmax))
# except:
# return golden(self._dew_P_UNIFAC_err, args=(T, zs, Psats, Pmax), brack=(Pmax, Pmax*(1-diff)))
#
示例3: fit_optimize_gcv
# 需要导入模块: from scipy import optimize [as 别名]
# 或者: from scipy.optimize import golden [as 别名]
def fit_optimize_gcv(self, y, x=None, weights=None, tol=1.0e-03,
brack=(-100,20)):
"""
Fit smoothing spline trying to optimize GCV.
Try to find a bracketing interval for scipy.optimize.golden
based on bracket.
It is probably best to use target_df instead, as it is
sometimes difficult to find a bracketing interval.
INPUTS:
y -- response variable
x -- if None, uses self.x
df -- target degrees of freedom
weights -- optional array of weights
tol -- (relative) tolerance for convergence
brack -- an initial guess at the bracketing interval
OUTPUTS: None
The smoothing spline is determined by self.coef,
subsequent calls of __call__ will be the smoothing spline.
"""
def _gcv(pen, y, x):
self.fit(y, x=x, pen=np.exp(pen))
a = self.gcv()
return a
a = golden(_gcv, args=(y,x), brack=bracket, tol=tol)
示例4: test_golden
# 需要导入模块: from scipy import optimize [as 别名]
# 或者: from scipy.optimize import golden [as 别名]
def test_golden(self):
""" golden algorithm """
x = optimize.golden(self.fun)
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.golden(self.fun, brack=(-3, -2))
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.golden(self.fun, full_output=True)
assert_allclose(x[0], self.solution, atol=1e-6)
x = optimize.golden(self.fun, brack=(-15, -1, 15))
assert_allclose(x, self.solution, atol=1e-6)
示例5: ee_radius
# 需要导入模块: from scipy import optimize [as 别名]
# 或者: from scipy.optimize import golden [as 别名]
def ee_radius(self, energy=FIRST_AIRY_ENCIRCLED):
"""Radius associated with a certain amount of enclosed energy."""
k, v = list(self._ee.keys()), list(self._ee.values())
if energy in v:
idx = v.index(energy)
return k[idx]
def optfcn(x):
return (self.encircled_energy(x) - energy) ** 2
# golden seems to perform best in presence of shallow local minima as in
# the encircled energy
return optimize.golden(optfcn)
示例6: _inverse_analytic_encircled_energy
# 需要导入模块: from scipy import optimize [as 别名]
# 或者: from scipy.optimize import golden [as 别名]
def _inverse_analytic_encircled_energy(fno, wavelength, energy=FIRST_AIRY_ENCIRCLED):
def optfcn(x):
return (_analytical_encircled_energy(fno, wavelength, x) - energy) ** 2
return optimize.golden(optfcn)
示例7: test_minimize_scalar
# 需要导入模块: from scipy import optimize [as 别名]
# 或者: from scipy.optimize import golden [as 别名]
def test_minimize_scalar(self):
# combine all tests above for the minimize_scalar wrapper
x = optimize.minimize_scalar(self.fun).x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, bracket=(-3, -2),
args=(1.5, ), method='Brent').x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, method='Brent',
args=(1.5,)).x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, bracket=(-15, -1, 15),
args=(1.5, ), method='Brent').x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, bracket=(-3, -2),
args=(1.5, ), method='golden').x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, method='golden',
args=(1.5,)).x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, bracket=(-15, -1, 15),
args=(1.5, ), method='golden').x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, bounds=(0, 1), args=(1.5,),
method='Bounded').x
assert_allclose(x, 1, atol=1e-4)
x = optimize.minimize_scalar(self.fun, bounds=(1, 5), args=(1.5, ),
method='bounded').x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, bounds=(np.array([1]),
np.array([5])),
args=(np.array([1.5]), ),
method='bounded').x
assert_allclose(x, self.solution, atol=1e-6)
assert_raises(ValueError, optimize.minimize_scalar, self.fun,
bounds=(5, 1), method='bounded', args=(1.5, ))
assert_raises(ValueError, optimize.minimize_scalar, self.fun,
bounds=(np.zeros(2), 1), method='bounded', args=(1.5, ))
x = optimize.minimize_scalar(self.fun, bounds=(1, np.array(5)),
method='bounded').x
assert_allclose(x, self.solution, atol=1e-6)
示例8: test_minimize_scalar
# 需要导入模块: from scipy import optimize [as 别名]
# 或者: from scipy.optimize import golden [as 别名]
def test_minimize_scalar(self):
# combine all tests above for the minimize_scalar wrapper
x = optimize.minimize_scalar(self.fun).x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, method='Brent')
assert_(x.success)
x = optimize.minimize_scalar(self.fun, method='Brent',
options=dict(maxiter=3))
assert_(not x.success)
x = optimize.minimize_scalar(self.fun, bracket=(-3, -2),
args=(1.5, ), method='Brent').x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, method='Brent',
args=(1.5,)).x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, bracket=(-15, -1, 15),
args=(1.5, ), method='Brent').x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, bracket=(-3, -2),
args=(1.5, ), method='golden').x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, method='golden',
args=(1.5,)).x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, bracket=(-15, -1, 15),
args=(1.5, ), method='golden').x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, bounds=(0, 1), args=(1.5,),
method='Bounded').x
assert_allclose(x, 1, atol=1e-4)
x = optimize.minimize_scalar(self.fun, bounds=(1, 5), args=(1.5, ),
method='bounded').x
assert_allclose(x, self.solution, atol=1e-6)
x = optimize.minimize_scalar(self.fun, bounds=(np.array([1]),
np.array([5])),
args=(np.array([1.5]), ),
method='bounded').x
assert_allclose(x, self.solution, atol=1e-6)
assert_raises(ValueError, optimize.minimize_scalar, self.fun,
bounds=(5, 1), method='bounded', args=(1.5, ))
assert_raises(ValueError, optimize.minimize_scalar, self.fun,
bounds=(np.zeros(2), 1), method='bounded', args=(1.5, ))
x = optimize.minimize_scalar(self.fun, bounds=(1, np.array(5)),
method='bounded').x
assert_allclose(x, self.solution, atol=1e-6)