def limit(value, min=negative_infinite, max=positive_infinite):
"""Limit a numeric value to the specified range"""
return maximum(min, minimum(value, max))
python类max()的实例源码
def _get_clipfn(size, signed=True):
maxval = _get_maxval(size, signed)
minval = _get_minval(size, signed)
return lambda val: __builtin__.max(min(val, maxval), minval)
def max(cp, size):
_check_params(len(cp), size)
if len(cp) == 0:
return 0
return __builtin__.max(abs(sample) for sample in _get_samples(cp, size))
def minmax(cp, size):
_check_params(len(cp), size)
max_sample, min_sample = 0, 0
for sample in _get_samples(cp, size):
max_sample = __builtin__.max(sample, max_sample)
min_sample = __builtin__.min(sample, min_sample)
return min_sample, max_sample
def maxpp(cp, size):
_check_params(len(cp), size)
sample_count = _sample_count(cp, size)
prevextremevalid = False
prevextreme = None
max = 0
prevval = getsample(cp, size, 0)
val = getsample(cp, size, 1)
prevdiff = val - prevval
for i in range(1, sample_count):
val = getsample(cp, size, i)
diff = val - prevval
if diff * prevdiff < 0:
if prevextremevalid:
extremediff = abs(prevval - prevextreme)
if extremediff > max:
max = extremediff
prevextremevalid = True
prevextreme = prevval
prevval = val
if diff != 0:
prevdiff = diff
return max
def _postprocess_eval_line(self, evalline, fname, span):
lines = evalline.split('\n')
# If line ended on '\n', last element is ''. We remove it and
# add the trailing newline later manually.
trailing_newline = (lines[-1] == '')
if trailing_newline:
del lines[-1]
lnum = linenumdir(span[0], fname) if self._linenums else ''
clnum = lnum if self._contlinenums else ''
linenumsep = '\n' + lnum
clinenumsep = '\n' + clnum
foldedlines = [self._foldline(line) for line in lines]
outlines = [clinenumsep.join(lines) for lines in foldedlines]
result = linenumsep.join(outlines)
# Add missing trailing newline
if trailing_newline:
trailing = '\n'
if self._linenums:
# Last line was folded, but no linenums were generated for
# the continuation lines -> current line position is not
# in sync with the one calculated from the last line number
unsync = (
len(foldedlines) and len(foldedlines[-1]) > 1
and not self._contlinenums)
# Eval directive in source consists of more than one line
multiline = span[1] - span[0] > 1
if unsync or multiline:
# For inline eval directives span[0] == span[1]
# -> next line is span[0] + 1 and not span[1] as for
# line eval directives
nextline = max(span[1], span[0] + 1)
trailing += linenumdir(nextline, fname)
else:
trailing = ''
return result + trailing
def _get_smart_fold_pos(line, start, end):
linelen = end - start
ispace = line.rfind(' ', start, end)
# The space we waste for smart folding should be max. 1/3rd of the line
if ispace != -1 and ispace >= start + (2 * linelen) // 3:
return ispace
else:
return end
def sizeof(self,ix):
if isinstance(ix,int):
n = ix+1
elif isinstance(ix,slice):
n = ix.stop
elif isinstance(ix,(list,np.ndarray)):
n = max(ix)+1
else:
assert 0,ix
if not isinstance(n,int):
raise IndexError
return n
def length(a):
try:
return __builtin__.max(np.asarray(a).shape)
except ValueError:
return 1
def max(a, d=0, nargout=0):
if d or nargout:
raise NotImplementedError
return np.amax(a)
def _get_clipfn(size, signed=True):
maxval = _get_maxval(size, signed)
minval = _get_minval(size, signed)
return lambda val: __builtin__.max(min(val, maxval), minval)
def max(cp, size):
_check_params(len(cp), size)
if len(cp) == 0:
return 0
return __builtin__.max(abs(sample) for sample in _get_samples(cp, size))
def minmax(cp, size):
_check_params(len(cp), size)
max_sample, min_sample = 0, 0
for sample in _get_samples(cp, size):
max_sample = __builtin__.max(sample, max_sample)
min_sample = __builtin__.min(sample, min_sample)
return min_sample, max_sample
def maxpp(cp, size):
_check_params(len(cp), size)
sample_count = _sample_count(cp, size)
prevextremevalid = False
prevextreme = None
max = 0
prevval = getsample(cp, size, 0)
val = getsample(cp, size, 1)
prevdiff = val - prevval
for i in range(1, sample_count):
val = getsample(cp, size, i)
diff = val - prevval
if diff * prevdiff < 0:
if prevextremevalid:
extremediff = abs(prevval - prevextreme)
if extremediff > max:
max = extremediff
prevextremevalid = True
prevextreme = prevval
prevval = val
if diff != 0:
prevdiff = diff
return max
def __setitem__(self,index,value):
#import pdb; pdb.set_trace()
indices = self.compute_indices(index)
try:
if len(indices) == 1:
np.asarray(self).reshape(-1,order="F").__setitem__(indices,value)
else:
np.asarray(self).__setitem__(indices,value)
except (ValueError,IndexError):
#import pdb; pdb.set_trace()
if not self.size:
new_shape = [self.sizeof(s) for s in indices]
self.resize(new_shape,refcheck=0)
np.asarray(self).__setitem__(indices,value)
elif len(indices) == 1:
# One-dimensional resize is only implemented for
# two cases:
#
# a. empty matrices having shape [0 0]. These
# matries may be resized to any shape. A[B]=C
# where A=[], and B is specific -- A[1:10]=C
# rather than A[:]=C or A[1:end]=C
if self.size and not isvector_or_scalar(self):
raise IndexError("One-dimensional resize "
"works only on vectors, and "
"row and column matrices")
# One dimensional resize of scalars creates row matrices
# ai = 3
# a(4) = 1
# 3 0 0 1
n = self.sizeof(indices[0]) # zero-based
if max(self.shape) == 1:
new_shape = list(self.shape)
new_shape[-1] = n
else:
new_shape = [(1 if s==1 else n) for s in self.shape]
self.resize(new_shape,refcheck=0)
np.asarray(self).reshape(-1,order="F").__setitem__(indices,value)
else:
new_shape = list(self.shape)
if self.flags["C_CONTIGUOUS"]:
new_shape[0] = self.sizeof(indices[0])
elif self.flags["F_CONTIGUOUS"]:
new_shape[-1] = self.sizeof(indices[-1])
self.resize(new_shape,refcheck=0)
np.asarray(self).__setitem__(indices,value)