本文整理汇总了Python中mystic.math.almostEqual函数的典型用法代码示例。如果您正苦于以下问题:Python almostEqual函数的具体用法?Python almostEqual怎么用?Python almostEqual使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了almostEqual函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_compare
def test_compare(solvername, x0, **kwds):
exec "my = solvers.%s" % solvername
exec "sp = %s" % solvername
maxiter = kwds.get('maxiter', None)
maxfun = kwds.get('maxfun', None)
my_x = my(rosen, x0, disp=0, full_output=True, **kwds)
# itermon = kwds.pop('itermon',None)
sp_x = sp(rosen, x0, disp=0, full_output=True, **kwds)
# similar bestSolution and bestEnergy
# print 'my:', my_x[0:2]
# print 'sp:', sp_x[0:2]
if my_x[3] == sp_x[-2]: # mystic can stop at iter=0, scipy can't
assert almostEqual(my_x[0], sp_x[0])
assert almostEqual(my_x[1], sp_x[1])
# print (iters, fcalls) and [maxiter, maxfun]
# print my_x[2:4], (sp_x[-3],sp_x[-2]), [maxiter, maxfun]
# test same number of iters and fcalls
if maxiter and maxfun is not None:
assert my_x[2] == sp_x[-3]
assert my_x[3] == sp_x[-2]
# # test fcalls <= maxfun
# assert my_x[3] <= maxfun
if maxiter is not None:
# test iters <= maxiter
assert my_x[2] <= maxiter
return
示例2: test_impose_reweighted_variance
def test_impose_reweighted_variance():
x0 = [1,2,3,4,5]
w0 = [3,1,1,1,1]
v = 1.0
w = impose_reweighted_variance(v, x0, w0)
assert almostEqual(variance(x0,w), v)
assert almostEqual(mean(x0,w0), mean(x0,w))
示例3: test_generate_constraint
def test_generate_constraint():
constraints = """
spread([x0, x1, x2]) = 10.0
mean([x0, x1, x2]) = 5.0"""
from mystic.math.measures import mean, spread
solv = generate_solvers(constraints)
assert almostEqual(mean(solv[0]([1,2,3])), 5.0)
assert almostEqual(spread(solv[1]([1,2,3])), 10.0)
constraint = generate_constraint(solv)
assert almostEqual(constraint([1,2,3]), [0.0,5.0,10.0], 1e-10)
示例4: test_with_mean_spread
def test_with_mean_spread():
from mystic.math.measures import mean, spread, impose_mean, impose_spread
@with_spread(50.0)
@with_mean(5.0)
def constrained_squared(x):
return [i**2 for i in x]
from numpy import array
x = array([1,2,3,4,5])
y = impose_spread(50.0, impose_mean(5.0,[i**2 for i in x]))
assert almostEqual(mean(y), 5.0, tol=1e-15)
assert almostEqual(spread(y), 50.0, tol=1e-15)
assert constrained_squared(x) == y
示例5: factory
def factory(x, *args, **kwds):
# apply decorated constraints function
x = constraints(x, *args, **kwds)
# constrain x such that mean(x) == target
if not almostEqual(mean(x), target):
x = impose_mean(target, x)#, weights=weights)
return x
示例6: test_constrain
def test_constrain():
from mystic.math.measures import mean, spread
from mystic.math.measures import impose_mean, impose_spread
def mean_constraint(x, mean=0.0):
return impose_mean(mean, x)
def range_constraint(x, spread=1.0):
return impose_spread(spread, x)
@inner(inner=range_constraint, kwds={'spread':5.0})
@inner(inner=mean_constraint, kwds={'mean':5.0})
def constraints(x):
return x
def cost(x):
return abs(sum(x) - 5.0)
from mystic.solvers import fmin_powell
from numpy import array
x = array([1,2,3,4,5])
y = fmin_powell(cost, x, constraints=constraints, disp=False)
assert mean(y) == 5.0
assert spread(y) == 5.0
assert almostEqual(cost(y), 4*(5.0))
示例7: constraints
def constraints(x):
# constrain the last x_i to be the same value as the first x_i
x[-1] = x[0]
# constrain x such that mean(x) == target
if not almostEqual(mean(x), target):
x = impose_mean(target, x)
return x
示例8: test_impose_reweighted_mean
def test_impose_reweighted_mean():
x0 = [1,2,3,4,5]
w0 = [3,1,1,1,1]
m = 3.5
w = impose_reweighted_mean(m, x0, w0)
assert almostEqual(mean(x0,w), m)
示例9: test_solve_constraint
def test_solve_constraint():
from mystic.math.measures import mean
@with_mean(1.0)
def constraint(x):
x[-1] = x[0]
return x
x = solve(constraint, guess=[2,3,1])
assert almostEqual(mean(x), 1.0, tol=1e-15)
assert x[-1] == x[0]
assert issolution(constraint, x)
示例10: constraints
def constraints(rv):
c = product_measure().load(rv, npts)
# NOTE: bounds wi in [0,1] enforced by filtering
# impose norm on each discrete measure
for measure in c:
if not almostEqual(float(measure.mass), 1.0, tol=atol, rel=rtol):
measure.normalize()
# impose expectation on product measure
##################### begin function-specific #####################
E = float(c.expect(model))
if not (E <= float(target[0] + error[0])) \
or not (float(target[0] - error[0]) <= E):
c.set_expect(target[0], model, (x_lb,x_ub), tol=error[0])
###################### end function-specific ######################
# extract weights and positions
return c.flatten()
示例11: test_numpy_penalty
def test_numpy_penalty():
constraints = """
mean([x0, x1, x2]) = 5.0
x0 = x1 + x2"""
ineq,eq = generate_conditions(constraints)
assert eq[0]([7,5,3]) == 0.0
assert eq[1]([7,4,3]) == 0.0
penalty = generate_penalty((ineq,eq))
assert penalty([9.0,5,4.0]) == 100.0
assert penalty([7.5,4,3.5]) == 0.0
constraint = as_constraint(penalty, solver='fmin')
assert almostEqual(penalty(constraint([3,4,5])), 0.0, 1e-10)
示例12: test_generate_penalty
def test_generate_penalty():
constraints = """
x0**2 = 2.5*x3 - a
exp(x2/x0) >= b"""
ineq,eq = generate_conditions(constraints, nvars=4, locals={'a':5.0, 'b':7.0})
assert ineq[0]([4,0,0,1,0]) == 6.0
assert eq[0]([4,0,0,1,0]) == 18.5
penalty = generate_penalty((ineq,eq))
assert penalty([1,0,2,2.4]) == 0.0
assert penalty([1,0,0,2.4]) == 7200.0
assert penalty([1,0,2,2.8]) == 100.0
constraint = as_constraint(penalty, nvars=4, solver='fmin')
assert almostEqual(penalty(constraint([1,0,0,2.4])), 0.0, 1e-10)
示例13: impose_reweighted_variance
def impose_reweighted_variance(v, samples, weights=None, solver=None):
"""impose a variance on a list of points by reweighting weights"""
ndim = len(samples)
if weights is None:
weights = [1.0/ndim] * ndim
if solver is None or solver == 'fmin':
from mystic.solvers import fmin as solver
elif solver == 'fmin_powell':
from mystic.solvers import fmin_powell as solver
elif solver == 'diffev':
from mystic.solvers import diffev as solver
elif solver == 'diffev2':
from mystic.solvers import diffev2 as solver
norm = sum(weights)
m = mean(samples, weights)
inequality = ""
equality = ""; equality2 = ""; equality3 = ""
for i in range(ndim):
inequality += "x%s >= 0.0\n" % (i) # positive
equality += "x%s + " % (i) # normalized
equality2 += "%s * x%s + " % (float(samples[i]),(i)) # mean
equality3 += "x%s*(%s-%s)**2 + " % ((i),float(samples[i]),m) # var
equality += "0.0 = %s\n" % float(norm)
equality += equality2 + "0.0 = %s*%s\n" % (float(norm),m)
equality += equality3 + "0.0 = %s*%s\n" % (float(norm),v)
penalties = generate_penalty(generate_conditions(inequality))
constrain = generate_constraint(generate_solvers(solve(equality)))
def cost(x): return sum(x)
results = solver(cost, weights, constraints=constrain, \
penalty=penalties, disp=False, full_output=True)
wts = list(results[0])
_norm = results[1] # should have _norm == norm
warn = results[4] # nonzero if didn't converge
#XXX: better to fail immediately if xlo < m < xhi... or the below?
if warn or not almostEqual(_norm, norm):
print "Warning: could not impose mean through reweighting"
return None #impose_variance(v, samples, weights), weights
return wts #samples, wts # "mean-preserving"
示例14: cone_mesh
def cone_mesh(length):
""" construct a conical mesh for a given length of cone """
L1,L2,L3 = slope
radius = length / L3 #XXX: * 0.5
r0 = ZERO
if almostEqual(radius, r0, tol=r0): radius = r0
r = np.linspace(radius,radius,6)
r[0]= np.zeros(r[0].shape)
r[1] *= r0/radius
r[5] *= r0/radius
r[3]= np.zeros(r[3].shape)
p = np.linspace(0,2*np.pi,50)
R,P = np.meshgrid(r,p)
X,Y = L1 * R*np.cos(P), L2 * R*np.sin(P)
tmp=list()
for i in range(np.size(p)):
tmp.append([0,0,length,length,length,0]) # len = size(r)
Z = np.array(tmp)
return X,Z,Y
示例15: generate_constraint
from mystic.symbolic import generate_constraint, generate_solvers, solve
from mystic.symbolic import generate_penalty, generate_conditions
equations = """
x0**2 - x1 + 1.0 <= 0.0
1.0 - x0 + (x1 - 4)**2 <= 0.0
"""
#cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities
pf = generate_penalty(generate_conditions(equations), k=1e12)
from mystic.constraints import as_constraint
cf = as_constraint(pf)
if __name__ == '__main__':
from mystic.solvers import buckshot
from mystic.math import almostEqual
result = buckshot(objective, 2, 40, bounds=bounds, penalty=pf, disp=False, full_output=True)
assert almostEqual(result[0], xs, tol=1e-2)
assert almostEqual(result[1], ys, rel=1e-2)
# EOF