本文整理汇总了Python中scipy.isscalar函数的典型用法代码示例。如果您正苦于以下问题:Python isscalar函数的具体用法?Python isscalar怎么用?Python isscalar使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isscalar函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _oneEvaluation
def _oneEvaluation(self, evaluable):
""" This method should be called by all optimizers for producing an evaluation. """
if self._wasUnwrapped:
self.wrappingEvaluable._setParameters(evaluable)
res = self.__evaluator(self.wrappingEvaluable)
elif self._wasWrapped:
res = self.__evaluator(evaluable.params)
else:
res = self.__evaluator(evaluable)
''' added by JPQ '''
if self.constrained :
self.feasible = self.__evaluator.outfeasible
self.violation = self.__evaluator.outviolation
# ---
if isscalar(res):
# detect numerical instability
if isnan(res) or isinf(res):
raise DivergenceError
# always keep track of the best
if (self.numEvaluations == 0
or self.bestEvaluation is None
or (self.minimize and res <= self.bestEvaluation)
or (not self.minimize and res >= self.bestEvaluation)):
self.bestEvaluation = res
self.bestEvaluable = evaluable.copy()
self.numEvaluations += 1
# if desired, also keep track of all evaluables and/or their fitness.
if self.storeAllEvaluated:
if self._wasUnwrapped:
self._allEvaluated.append(self.wrappingEvaluable.copy())
elif self._wasWrapped:
self._allEvaluated.append(evaluable.params.copy())
else:
self._allEvaluated.append(evaluable.copy())
if self.storeAllEvaluations:
if self._wasOpposed and isscalar(res):
''' added by JPQ '''
if self.constrained :
self._allEvaluations.append([-res,self.feasible,self.violation])
# ---
else:
self._allEvaluations.append(-res)
else:
''' added by JPQ '''
if self.constrained :
self._allEvaluations.append([res,self.feasible,self.violation])
# ---
else:
self._allEvaluations.append(res)
''' added by JPQ '''
if self.constrained :
return [res,self.feasible,self.violation]
else:
# ---
return res
示例2: angcomp
def angcomp(ra1,dec1,ra2,dec2,method=3):
"""
Return the delta_RA and delta_dec (delta = 1-2) components of the angular
distance between objects. This is simply an alternate output of the
angular_distance function above. Distance is returned as degrees, and method chooses a more or less accurate way of determining the distance (with 1 being the fastest/least accurate).
from astorlib.py
# UNITS: degrees (output), degrees (input)
"""
DEGRAD = pi/180.
import scipy
if scipy.isscalar(ra1) and scipy.isscalar(ra2):
from numpy import cos,sin,sqrt
if ra1-ra2>180:
ra1 -= 360.
elif ra2-ra1>180:
ra2 -= 360.
else:
from scipy import cos,sin,sqrt
if scipy.isscalar(ra1):
t = ra2.copy()
ra2 = ra2*0. + ra1
ra1 = t.copy()
t = dec2.copy()
dec2 = dec2*0. + dec1
dec1 = t.copy()
del t
ra1 = scipy.where(ra1-ra2>180,ra1-360.,ra1)
ra2 = scipy.where(ra2-ra1>180,ra2-360.,ra2)
ra1 = ra1*DEGRAD
dec1 = dec1*DEGRAD
ra2 = ra2*DEGRAD
dec2 = dec2*DEGRAD
if method==1:
deltadec = dec1-dec2
deltara = (ra1-ra2)*cos(dec2)
else:
div = 1.
if method==3:
div = sin(dec2)*sin(dec1)+cos(dec2)*cos(dec1)*cos((ra1-ra2))
deltara = cos(dec2)*sin(ra1-ra2)/div
deltadec = -(sin(dec2)*cos(dec1)-cos(dec2)*sin(dec1)*cos(ra1-ra2)/div)
#if sum(div == 0) != 0: #attempt at making array compatable but doesn't
#work for single integers. Note that could just remove this section of
#the code since it only here for QA
if div == 0:
import sys
print 'tools: div = 0, exiting'
sys.exit()
return deltara/DEGRAD, deltadec/DEGRAD
示例3: angcomp
def angcomp(ra1,dec1,ra2,dec2,method=3):
"""
Return the delta_RA and delta_dec (delta = 1-2) components of the angular
distance between objects. This is simply an alternate output of the
angular_distance function above. Distance is returned as degrees, and method chooses a more or less accurate way of determining the distance (with 1 being the fastest/least accurate).
from astorlib.py
"""
DEGRAD = pi/180.
import scipy
if scipy.isscalar(ra1) and scipy.isscalar(ra2):
from math import cos,sin,sqrt
if ra1-ra2>180:
ra1 -= 360.
elif ra2-ra1>180:
ra2 -= 360.
else:
from scipy import cos,sin,sqrt
if scipy.isscalar(ra1):
t = ra2.copy()
ra2 = ra2*0. + ra1
ra1 = t.copy()
t = dec2.copy()
dec2 = dec2*0. + dec1
dec1 = t.copy()
del t
ra1 = scipy.where(ra1-ra2>180,ra1-360.,ra1)
ra2 = scipy.where(ra2-ra1>180,ra2-360.,ra2)
ra1 = ra1*DEGRAD
dec1 = dec1*DEGRAD
ra2 = ra2*DEGRAD
dec2 = dec2*DEGRAD
if method==1:
deltadec = dec1-dec2
deltara = (ra1-ra2)*cos(dec2)
else:
div = 1.
if method==3:
div = sin(dec2)*sin(dec1)+cos(dec2)*cos(dec1)*cos((ra1-ra2))
deltara = cos(dec2)*sin(ra1-ra2)/div
deltadec = -(sin(dec2)*cos(dec1)-cos(dec2)*sin(dec1)*cos(ra1-ra2)/div)
if isinstance(div, float) and div == 0:
raise ValueError("dividing by div = 0")
elif isinstance(div, numpy.ndarray) and div.any() == 0:
raise ValueError("dividing by div = 0")
return deltara/DEGRAD, deltadec/DEGRAD
示例4: generate_tolerances
def generate_tolerances(net, rtol, atol=None):
if rtol == None:
rtol = global_rtol
if scipy.isscalar(rtol):
rtol = scipy.ones(len(net.dynamicVars)) * rtol
# We set atol to be a minimum of global_atol to avoid variables with large
# typical values going negative.
if (scipy.isscalar(atol) or atol==None):
typ_vals = [abs(net.get_var_typical_val(id))
for id in net.dynamicVars.keys()]
atol = rtol * scipy.asarray(typ_vals)
if global_atol:
atol = scipy.minimum(atol, global_atol)
return rtol, atol
示例5: oscillate_hessian
def oscillate_hessian(self, params, eps=1e-5,
relativeScale=True, stepSizeCutoff=1e-6,
verbose=False, f0_zero=False):
"""
Same as hessian except uses oscillate cost function
"""
nOv = len(params)
if scipy.isscalar(eps):
eps = scipy.ones(len(params), scipy.float_) * eps
## compute cost at f(x), set f0 to zero if f0_zero is true
if f0_zero:
f0 = 0
else:
f0 = self.oscillate_cost(params)
hess = scipy.zeros((nOv, nOv), scipy.float_)
## compute all (numParams*(numParams + 1))/2 unique hessian elements
for i in range(nOv):
for j in range(i, nOv):
hess[i][j] = self.hessian_elem(self.oscillate_cost, f0,
params, i, j, eps[i], eps[j],
relativeScale, stepSizeCutoff,
verbose)
hess[j][i] = hess[i][j]
return hess
示例6: periodic_hessian_log_params
def periodic_hessian_log_params(self, params, eps=1e-5,
relativeScale=False, stepSizeCutoff=1e-6,
verbose=False):
"""
Same as hessian_log_params except uses periodic cost function
Relative scale is false by default here
"""
nOv = len(params)
if scipy.isscalar(eps):
eps = scipy.ones(len(params), scipy.float_) * eps
## compute cost at f(x)
f0 = self.periodic_cost_log_params(scipy.log(params))
hess = scipy.zeros((nOv, nOv), scipy.float_)
## compute all (numParams*(numParams + 1))/2 unique hessian elements
for i in range(nOv):
for j in range(i, nOv):
hess[i][j] = self.hessian_elem(self.periodic_cost_log_params, f0,
scipy.log(params),
i, j, eps[i], eps[j],
relativeScale, stepSizeCutoff,
verbose)
hess[j][i] = hess[i][j]
return hess
示例7: hessian_log_params
def hessian_log_params(self, params, eps,
relativeScale=False, stepSizeCutoff=1e-6,
verbose=False):
"""
Returns the hessian of the model in log parameters.
eps: Sets the stepsize to try
relativeScale: If True, step i is of size p[i] * eps, otherwise it is
eps
stepSizeCutoff: The minimum stepsize to take
vebose: If True, a message will be printed with each hessian element
calculated
"""
nOv = len(params)
if scipy.isscalar(eps):
eps = scipy.ones(len(params), scipy.float_) * eps
## compute cost at f(x)
f0 = self.cost_log_params(scipy.log(params))
hess = scipy.zeros((nOv, nOv), scipy.float_)
## compute all (numParams*(numParams + 1))/2 unique hessian elements
for i in range(nOv):
for j in range(i, nOv):
hess[i][j] = self.hessian_elem(self.cost_log_params, f0,
scipy.log(params),
i, j, eps[i], eps[j],
relativeScale, stepSizeCutoff,
verbose)
hess[j][i] = hess[i][j]
return hess
示例8: __init__
def __init__(self,net,prior=S.array([100,1])):
if SP.isscalar(prior):
self.fixE1=prior
CNodeEps.__init__(self,net)
else:
self.fixE1 = None
CNodeEps.__init__(self,net,prior)
示例9: errorScalingFactor
def errorScalingFactor(observable, beta):
"""
Look up the numerical factors to apply to the sky averaged parallax error in order to obtain error
values for a given astrometric parameter, taking the Ecliptic latitude and the number of transits into
account.
Parameters
----------
observable - Name of astrometric observable (one of: alphaStar, delta, parallax, muAlphaStar, muDelta)
beta - Values(s) of the Ecliptic latitude.
Returns
-------
Numerical factors to apply to the errors of the given observable.
"""
if isscalar(beta):
index=int(floor(abs(sin(beta))*_numStepsSinBeta))
if index == _numStepsSinBeta:
return _astrometricErrorFactors[observable][_numStepsSinBeta-1]
else:
return _astrometricErrorFactors[observable][index]
else:
indices = array(floor(abs(sin(beta))*_numStepsSinBeta), dtype=int)
indices[(indices==_numStepsSinBeta)] = _numStepsSinBeta-1
return _astrometricErrorFactors[observable][indices]
示例10: _compute_qth_percentile
def _compute_qth_percentile(sorted, q, axis, out):
if not isscalar(q):
p = [_compute_qth_percentile(sorted, qi, axis, None)
for qi in q]
if out is not None:
out.flat = p
return p
q = q / 100.0
if (q < 0) or (q > 1):
raise ValueError("percentile must be either in the range [0,100]")
indexer = [slice(None)] * sorted.ndim
Nx = sorted.shape[axis]
index = q * (Nx - 1)
i = int(index)
if i == index:
indexer[axis] = slice(i, i + 1)
weights = array(1)
sumval = 1.0
else:
indexer[axis] = slice(i, i + 2)
j = i + 1
weights = array([(j - index), (index - i)], float)
wshape = [1] * sorted.ndim
wshape[axis] = 2
weights.shape = wshape
sumval = weights.sum()
# Use add.reduce in both cases to coerce data type as well as
# check and use out array.
return add.reduce(sorted[indexer] * weights, axis=axis, out=out) / sumval
示例11: get_data
def get_data(self, pos, phase=None):
"""return voltage data for relative position and phase
The relative position vector is matched with neuron_data.horizon and
:Parameters:
pos : ndarray
The relative position in the dataset.
phase : sequence
The phase within the waveform to retrieve. Either a single
sample index or a sequence. If None is given, return the
complete waveform.
Default=None
"""
# checking pos
if vector_norm(pos) > self.horizon:
raise BeyondHorizonError('norm of relative position [%s] '
'lies beyond the horizon [%s]!'
% (vector_norm(pos), self.horizon))
# check for phase
if phase is None:
phase = xrange(self.intra_v.size)
if N.isscalar(phase):
phase = xrange(phase, phase + 1)
# call get_data implementation
return self._get_data(pos, phase)
示例12: callback
def callback(x):
if sp.isscalar(x):
residuals.append(x)
else:
residuals.append(residual_norm(A, x, b))
if cb is not None:
cb(x)
示例13: _bestFound
def _bestFound(self):
""" return the best found evaluable and its associated fitness. """
bestE = self.bestEvaluable.params.copy() if self._wasWrapped else self.bestEvaluable
if self._wasOpposed and isscalar(self.bestEvaluation):
bestF = -self.bestEvaluation
else:
bestF = self.bestEvaluation
return bestE, bestF
示例14: __init__
def __init__(self, C, taud, tauf, U):
if isinstance(C, DelayConnection):
raise AttributeError, "STP does not handle heterogeneous connections yet."
NetworkOperation.__init__(self, lambda:None, clock=C.source.clock)
N = len(C.source)
P = STPGroup(N, clock=C.source.clock)
P.x = 1
P.u = U
P.ux = U
if (isscalar(taud) & isscalar(tauf) & isscalar(U)):
updater = STPUpdater(C.source, P, taud, tauf, U, delay=C.delay * C.source.clock.dt)
else:
updater = STPUpdater2(C.source, P, taud, tauf, U, delay=C.delay * C.source.clock.dt)
self.contained_objects = [updater]
C.source = P
C.delay = 0
C._nstate_mod = 0 # modulation of synaptic weights
self.vars = P
示例15: jacobian
def jacobian(self, reference_point):
jac = self._element.function_gradient(self._mesh_entity.vertex_coords(),
reference_point)
if sp.isscalar(jac):
return sp.array([[jac]])
elif sp.all(sp.array(jac.shape) == 1):
return jac.reshape((1, 1))
else:
return jac