summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2013-09-26 20:08:59 -0700
committerCharles Harris <charlesr.harris@gmail.com>2013-09-26 20:08:59 -0700
commit67b5392a8a31e5a4412a961c4e4f5a31bfd9a780 (patch)
treeb994bb20dfece5bb2414b0d79dfdebbe0f50605d /numpy/lib
parent21d99a683464b311ff60972293382a292dcbbe5d (diff)
parentec50fb1b7683c44d065914b946d250dbe01aae9f (diff)
downloadnumpy-67b5392a8a31e5a4412a961c4e4f5a31bfd9a780.tar.gz
Merge pull request #3775 from jarondl/doc-function_base-pep8
STY: make function_base.py pep8 compatible
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/function_base.py488
1 files changed, 276 insertions, 212 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 472d7eecc..fe41a996f 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -11,33 +11,37 @@ __all__ = [
'meshgrid', 'delete', 'insert', 'append', 'interp', 'add_newdoc_ufunc']
import warnings
-import types
import sys
+import collections
+
+import numpy as np
import numpy.core.numeric as _nx
-from numpy.core import linspace
-from numpy.core.numeric import ones, zeros, arange, concatenate, array, \
- asarray, asanyarray, empty, empty_like, ndarray, around, floor, \
- ceil, take
-from numpy.core.numeric import ScalarType, dot, where, newaxis, intp, \
- integer, isscalar
-from numpy.core.umath import pi, multiply, add, arctan2, \
- frompyfunc, isnan, cos, less_equal, sqrt, sin, mod, exp, log10
-from numpy.core.fromnumeric import ravel, nonzero, choose, sort, partition, mean
+from numpy.core import linspace, atleast_1d, atleast_2d
+from numpy.core.numeric import (
+ ones, zeros, arange, concatenate, array, asarray, asanyarray, empty,
+ empty_like, ndarray, around, floor, ceil, take, ScalarType, dot, where,
+ newaxis, intp, integer, isscalar
+ )
+from numpy.core.umath import (
+ pi, multiply, add, arctan2, frompyfunc, isnan, cos, less_equal, sqrt, sin,
+ mod, exp, log10
+ )
+from numpy.core.fromnumeric import (
+ ravel, nonzero, choose, sort, partition, mean
+ )
from numpy.core.numerictypes import typecodes, number
-from numpy.core import atleast_1d, atleast_2d
from numpy.lib.twodim_base import diag
from ._compiled_base import _insert, add_docstring
from ._compiled_base import digitize, bincount, interp as compiled_interp
from .utils import deprecate
from ._compiled_base import add_newdoc_ufunc
-import numpy as np
-import collections
from numpy.compat import long
# Force range to be a generator, for np.delete's usage.
if sys.version_info[0] < 3:
range = xrange
+
def iterable(y):
"""
Check whether or not an object can be iterated over.
@@ -62,11 +66,15 @@ def iterable(y):
0
"""
- try: iter(y)
- except: return 0
+ try:
+ iter(y)
+ except:
+ return 0
return 1
-def histogram(a, bins=10, range=None, normed=False, weights=None, density=None):
+
+def histogram(a, bins=10, range=None, normed=False, weights=None,
+ density=None):
"""
Compute the histogram of a set of data.
@@ -155,9 +163,9 @@ def histogram(a, bins=10, range=None, normed=False, weights=None, density=None):
weights = asarray(weights)
if np.any(weights.shape != a.shape):
raise ValueError(
- 'weights should have the same shape as a.')
+ 'weights should have the same shape as a.')
weights = weights.ravel()
- a = a.ravel()
+ a = a.ravel()
if (range is not None):
mn, mx = range
@@ -167,23 +175,24 @@ def histogram(a, bins=10, range=None, normed=False, weights=None, density=None):
if not iterable(bins):
if np.isscalar(bins) and bins < 1:
- raise ValueError("`bins` should be a positive integer.")
+ raise ValueError(
+ '`bins` should be a positive integer.')
if range is None:
if a.size == 0:
# handle empty arrays. Can't determine range, so use 0-1.
range = (0, 1)
else:
range = (a.min(), a.max())
- mn, mx = [mi+0.0 for mi in range]
+ mn, mx = [mi + 0.0 for mi in range]
if mn == mx:
mn -= 0.5
mx += 0.5
- bins = linspace(mn, mx, bins+1, endpoint=True)
+ bins = linspace(mn, mx, bins + 1, endpoint=True)
else:
bins = asarray(bins)
if (np.diff(bins) < 0).any():
raise AttributeError(
- 'bins must increase monotonically.')
+ 'bins must increase monotonically.')
# Histogram is an integer or a float array depending on the weights.
if weights is None:
@@ -196,8 +205,8 @@ def histogram(a, bins=10, range=None, normed=False, weights=None, density=None):
if weights is None:
for i in arange(0, len(a), block):
sa = sort(a[i:i+block])
- n += np.r_[sa.searchsorted(bins[:-1], 'left'), \
- sa.searchsorted(bins[-1], 'right')]
+ n += np.r_[sa.searchsorted(bins[:-1], 'left'),
+ sa.searchsorted(bins[-1], 'right')]
else:
zero = array(0, dtype=ntype)
for i in arange(0, len(a), block):
@@ -206,9 +215,9 @@ def histogram(a, bins=10, range=None, normed=False, weights=None, density=None):
sorting_index = np.argsort(tmp_a)
sa = tmp_a[sorting_index]
sw = tmp_w[sorting_index]
- cw = np.concatenate(([zero,], sw.cumsum()))
- bin_index = np.r_[sa.searchsorted(bins[:-1], 'left'), \
- sa.searchsorted(bins[-1], 'right')]
+ cw = np.concatenate(([zero, ], sw.cumsum()))
+ bin_index = np.r_[sa.searchsorted(bins[:-1], 'left'),
+ sa.searchsorted(bins[-1], 'right')]
n += cw[bin_index]
n = np.diff(n)
@@ -298,8 +307,8 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
M = len(bins)
if M != D:
raise AttributeError(
- 'The dimension of bins must be equal'\
- ' to the dimension of the sample x.')
+ 'The dimension of bins must be equal to the dimension of the '
+ ' sample x.')
except TypeError:
# bins is an integer
bins = D*[bins]
@@ -330,20 +339,21 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
for i in arange(D):
if isscalar(bins[i]):
if bins[i] < 1:
- raise ValueError("Element at index %s in `bins` should be "
- "a positive integer." % i)
- nbin[i] = bins[i] + 2 # +2 for outlier bins
+ raise ValueError(
+ "Element at index %s in `bins` should be a positive "
+ "integer." % i)
+ nbin[i] = bins[i] + 2 # +2 for outlier bins
edges[i] = linspace(smin[i], smax[i], nbin[i]-1)
else:
edges[i] = asarray(bins[i], float)
- nbin[i] = len(edges[i])+1 # +1 for outlier bins
+ nbin[i] = len(edges[i]) + 1 # +1 for outlier bins
dedges[i] = diff(edges[i])
if np.any(np.asarray(dedges[i]) <= 0):
- raise ValueError("""
- Found bin edge of size <= 0. Did you specify `bins` with
- non-monotonic sequence?""")
+ raise ValueError(
+ "Found bin edge of size <= 0. Did you specify `bins` with"
+ "non-monotonic sequence?")
- nbin = asarray(nbin)
+ nbin = asarray(nbin)
# Handle empty input.
if N == 0:
@@ -363,8 +373,8 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
if not np.isinf(mindiff):
decimal = int(-log10(mindiff)) + 6
# Find which points are on the rightmost edge.
- on_edge = where(around(sample[:, i], decimal) == around(edges[i][-1],
- decimal))[0]
+ on_edge = where(around(sample[:, i], decimal) ==
+ around(edges[i][-1], decimal))[0]
# Shift these points one bin to the left.
Ncount[i][on_edge] -= 1
@@ -411,7 +421,7 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
if (hist.shape != nbin - 2).any():
raise RuntimeError(
- "Internal Shape Error")
+ "Internal Shape Error")
return hist, edges
@@ -489,30 +499,28 @@ def average(a, axis=None, weights=None, returned=False):
TypeError: Axis must be specified when shapes of a and weights differ.
"""
- if not isinstance(a, np.matrix) :
+ if not isinstance(a, np.matrix):
a = np.asarray(a)
- if weights is None :
+ if weights is None:
avg = a.mean(axis)
scl = avg.dtype.type(a.size/avg.size)
- else :
+ else:
a = a + 0.0
wgt = np.array(weights, dtype=a.dtype, copy=0)
# Sanity checks
- if a.shape != wgt.shape :
- if axis is None :
+ if a.shape != wgt.shape:
+ if axis is None:
raise TypeError(
- "Axis must be specified when shapes of a "\
- "and weights differ.")
- if wgt.ndim != 1 :
+ "Axis must be specified when shapes of a and weights "
+ "differ.")
+ if wgt.ndim != 1:
raise TypeError(
- "1D weights expected when shapes of a and "\
- "weights differ.")
- if wgt.shape[0] != a.shape[axis] :
+ "1D weights expected when shapes of a and weights differ.")
+ if wgt.shape[0] != a.shape[axis]:
raise ValueError(
- "Length of weights not compatible with "\
- "specified axis.")
+ "Length of weights not compatible with specified axis.")
# setup wgt to broadcast along axis
wgt = np.array(wgt, copy=0, ndmin=a.ndim).swapaxes(-1, axis)
@@ -520,7 +528,7 @@ def average(a, axis=None, weights=None, returned=False):
scl = wgt.sum(axis=axis)
if (scl == 0.0).any():
raise ZeroDivisionError(
- "Weights sum to zero, can't be normalized")
+ "Weights sum to zero, can't be normalized")
avg = np.multiply(a, wgt).sum(axis)/scl
@@ -530,6 +538,7 @@ def average(a, axis=None, weights=None, returned=False):
else:
return avg
+
def asarray_chkfinite(a, dtype=None, order=None):
"""
Convert the input to an array, checking for NaNs or Infs.
@@ -593,9 +602,10 @@ def asarray_chkfinite(a, dtype=None, order=None):
a = asarray(a, dtype=dtype, order=order)
if a.dtype.char in typecodes['AllFloat'] and not np.isfinite(a).all():
raise ValueError(
- "array must not contain infs or NaNs")
+ "array must not contain infs or NaNs")
return a
+
def piecewise(x, condlist, funclist, *args, **kw):
"""
Evaluate a piecewise-defined function.
@@ -679,13 +689,12 @@ def piecewise(x, condlist, funclist, *args, **kw):
"""
x = asanyarray(x)
n2 = len(funclist)
- if isscalar(condlist) or \
- not (isinstance(condlist[0], list) or
- isinstance(condlist[0], ndarray)):
+ if (isscalar(condlist) or not (isinstance(condlist[0], list) or
+ isinstance(condlist[0], ndarray))):
condlist = [condlist]
condlist = [asarray(c, dtype=bool) for c in condlist]
n = len(condlist)
- if n == n2-1: # compute the "otherwise" condition.
+ if n == n2 - 1: # compute the "otherwise" condition.
totlist = condlist[0]
for k in range(1, n):
totlist |= condlist[k]
@@ -693,7 +702,7 @@ def piecewise(x, condlist, funclist, *args, **kw):
n += 1
if (n != n2):
raise ValueError(
- "function list and condition list must be the same")
+ "function list and condition list must be the same")
zerod = False
# This is a hack to work around problems with NumPy's
# handling of 0-d arrays and boolean indexing with
@@ -723,6 +732,7 @@ def piecewise(x, condlist, funclist, *args, **kw):
y = y.squeeze()
return y
+
def select(condlist, choicelist, default=0):
"""
Return an array drawn from elements in choicelist, depending on conditions.
@@ -764,7 +774,7 @@ def select(condlist, choicelist, default=0):
n2 = len(choicelist)
if n2 != n:
raise ValueError(
- "list of cases must be same length as list of conditions")
+ "list of cases must be same length as list of conditions")
choicelist = [default] + choicelist
S = 0
pfac = 1
@@ -774,7 +784,7 @@ def select(condlist, choicelist, default=0):
pfac *= (1-asarray(condlist[k-1]))
# handle special case of a 1-element condition but
# a multi-element choice
- if type(S) in ScalarType or max(asarray(S).shape)==1:
+ if type(S) in ScalarType or max(asarray(S).shape) == 1:
pfac = asarray(1)
for k in range(n2+1):
pfac = pfac + asarray(choicelist[k])
@@ -784,6 +794,7 @@ def select(condlist, choicelist, default=0):
S = S*ones(asarray(pfac).shape, S.dtype)
return choose(S, tuple(choicelist))
+
def copy(a, order='K'):
"""
Return an array copy of the given object.
@@ -832,6 +843,7 @@ def copy(a, order='K'):
# Basic operations
+
def gradient(f, *varargs):
"""
Return the gradient of an N-dimensional array.
@@ -886,7 +898,7 @@ def gradient(f, *varargs):
dx = list(varargs)
else:
raise SyntaxError(
- "invalid number of arguments")
+ "invalid number of arguments")
# use central differences on interior and one-sided differences on the
# endpoints. This preserves second order-accuracy over the full domain.
@@ -904,10 +916,10 @@ def gradient(f, *varargs):
otype = 'd'
# Difference of datetime64 elements results in timedelta64
- if otype == 'M' :
+ if otype == 'M':
# Need to use the full dtype name because it contains unit information
otype = f.dtype.name.replace('datetime', 'timedelta')
- elif otype == 'm' :
+ elif otype == 'm':
# Needs to keep the specific units, can't be a general unit
otype = f.dtype
@@ -922,7 +934,9 @@ def gradient(f, *varargs):
for axis in range(N):
if y.shape[axis] < 2:
- raise ValueError("Shape of array too small to calculate a numerical gradient, at least two elements are required.")
+ raise ValueError(
+ "Shape of array too small to calculate a numerical gradient, "
+ "at least two elements are required.")
# Numerical differentiation: 1st order edges, 2nd order interior
if y.shape[axis] == 2:
@@ -986,6 +1000,7 @@ def gradient(f, *varargs):
else:
return outvals
+
def diff(a, n=1, axis=-1):
"""
Calculate the n-th order discrete difference along given axis.
@@ -1033,7 +1048,7 @@ def diff(a, n=1, axis=-1):
return a
if n < 0:
raise ValueError(
- "order must be non-negative but got " + repr(n))
+ "order must be non-negative but got " + repr(n))
a = asanyarray(a)
nd = len(a.shape)
slice1 = [slice(None)]*nd
@@ -1047,6 +1062,7 @@ def diff(a, n=1, axis=-1):
else:
return a[slice1]-a[slice2]
+
def interp(x, xp, fp, left=None, right=None):
"""
One-dimensional linear interpolation.
@@ -1169,6 +1185,7 @@ def angle(z, deg=0):
zreal = z
return arctan2(zimag, zreal) * fact
+
def unwrap(p, discont=pi, axis=-1):
"""
Unwrap by changing deltas between values to 2*pi complement.
@@ -1215,14 +1232,15 @@ def unwrap(p, discont=pi, axis=-1):
dd = diff(p, axis=axis)
slice1 = [slice(None, None)]*nd # full slices
slice1[axis] = slice(1, None)
- ddmod = mod(dd+pi, 2*pi)-pi
- _nx.copyto(ddmod, pi, where=(ddmod==-pi) & (dd > 0))
- ph_correct = ddmod - dd;
- _nx.copyto(ph_correct, 0, where=abs(dd)<discont)
+ ddmod = mod(dd + pi, 2*pi) - pi
+ _nx.copyto(ddmod, pi, where=(ddmod == -pi) & (dd > 0))
+ ph_correct = ddmod - dd
+ _nx.copyto(ph_correct, 0, where=abs(dd) < discont)
up = array(p, copy=True, dtype='d')
up[slice1] = p[slice1] + ph_correct.cumsum(axis)
return up
+
def sort_complex(a):
"""
Sort a complex array using the real part first, then the imaginary part.
@@ -1258,6 +1276,7 @@ def sort_complex(a):
else:
return b
+
def trim_zeros(filt, trim='fb'):
"""
Trim the leading and/or trailing zeros from a 1-D array or sequence.
@@ -1295,18 +1314,19 @@ def trim_zeros(filt, trim='fb'):
trim = trim.upper()
if 'F' in trim:
for i in filt:
- if i != 0.: break
- else: first = first + 1
+ if i != 0.:
+ break
+ else:
+ first = first + 1
last = len(filt)
if 'B' in trim:
for i in filt[::-1]:
- if i != 0.: break
- else: last = last - 1
+ if i != 0.:
+ break
+ else:
+ last = last - 1
return filt[first:last]
-import sys
-if sys.hexversion < 0x2040000:
- from sets import Set as set
@deprecate
def unique(x):
@@ -1319,12 +1339,13 @@ def unique(x):
if tmp.size == 0:
return tmp
tmp.sort()
- idx = concatenate(([True], tmp[1:]!=tmp[:-1]))
+ idx = concatenate(([True], tmp[1:] != tmp[:-1]))
return tmp[idx]
except AttributeError:
items = sorted(set(x))
return asarray(items)
+
def extract(condition, arr):
"""
Return the elements of an array that satisfy some condition.
@@ -1373,6 +1394,7 @@ def extract(condition, arr):
"""
return _nx.take(ravel(arr), nonzero(ravel(condition))[0])
+
def place(arr, mask, vals):
"""
Change elements of an array based on conditional and input values.
@@ -1410,6 +1432,7 @@ def place(arr, mask, vals):
"""
return _insert(arr, mask, vals)
+
def disp(mesg, device=None, linefeed=True):
"""
Display a message on a device.
@@ -1443,7 +1466,6 @@ def disp(mesg, device=None, linefeed=True):
"""
if device is None:
- import sys
device = sys.stdout
if linefeed:
device.write('%s\n' % mesg)
@@ -1452,6 +1474,7 @@ def disp(mesg, device=None, linefeed=True):
device.flush()
return
+
class vectorize(object):
"""
vectorize(pyfunc, otypes='', doc=None, excluded=None, cache=False)
@@ -1558,14 +1581,15 @@ class vectorize(object):
If `otypes` is not specified, then a call to the function with the first
argument will be used to determine the number of outputs. The results of
this call will be cached if `cache` is `True` to prevent calling the
- function twice. However, to implement the cache, the original function must
- be wrapped which will slow down subsequent calls, so only do this if your
- function is expensive.
+ function twice. However, to implement the cache, the original function
+ must be wrapped which will slow down subsequent calls, so only do this if
+ your function is expensive.
The new keyword argument interface and `excluded` argument support further
degrades performance.
"""
- def __init__(self, pyfunc, otypes='', doc=None, excluded=None, cache=False):
+ def __init__(self, pyfunc, otypes='', doc=None, excluded=None,
+ cache=False):
self.pyfunc = pyfunc
self.cache = cache
@@ -1578,11 +1602,13 @@ class vectorize(object):
self.otypes = otypes
for char in self.otypes:
if char not in typecodes['All']:
- raise ValueError("Invalid otype specified: %s" % (char,))
+ raise ValueError(
+ "Invalid otype specified: %s" % (char,))
elif iterable(otypes):
self.otypes = ''.join([_nx.dtype(x).char for x in otypes])
else:
- raise ValueError("Invalid otype specification")
+ raise ValueError(
+ "Invalid otype specification")
# Excluded variable support
if excluded is None:
@@ -1610,6 +1636,7 @@ class vectorize(object):
names = [_n for _n in kwargs if _n not in excluded]
inds = [_i for _i in range(nargs) if _i not in excluded]
the_args = list(args)
+
def func(*vargs):
for _n, _i in enumerate(inds):
the_args[_i] = vargs[_n]
@@ -1645,11 +1672,13 @@ class vectorize(object):
inputs = [asarray(_a).flat[0] for _a in args]
outputs = func(*inputs)
- # Performance note: profiling indicates that -- for simple functions
- # at least -- this wrapping can almost double the execution time.
+ # Performance note: profiling indicates that -- for simple
+ # functions at least -- this wrapping can almost double the
+ # execution time.
# Hence we make it optional.
if self.cache:
_cache = [outputs]
+
def _func(*vargs):
if _cache:
return _cache.pop()
@@ -1695,6 +1724,7 @@ class vectorize(object):
for _x, _t in zip(outputs, otypes)])
return _res
+
def cov(m, y=None, rowvar=1, bias=0, ddof=None):
"""
Estimate a covariance matrix, given data.
@@ -1776,7 +1806,8 @@ def cov(m, y=None, rowvar=1, bias=0, ddof=None):
"""
# Check inputs
if ddof is not None and ddof != int(ddof):
- raise ValueError("ddof must be integer")
+ raise ValueError(
+ "ddof must be integer")
X = array(m, ndmin=2, dtype=float)
if X.size == 0:
@@ -1791,7 +1822,6 @@ def cov(m, y=None, rowvar=1, bias=0, ddof=None):
axis = 1
tup = (newaxis, slice(None))
-
if y is not None:
y = array(y, copy=False, ndmin=2, dtype=float)
X = concatenate((X, y), axis)
@@ -1868,10 +1898,11 @@ def corrcoef(x, y=None, rowvar=1, bias=0, ddof=None):
return c
try:
d = diag(c)
- except ValueError: # scalar covariance
+ except ValueError: # scalar covariance
return 1
return c/sqrt(multiply.outer(d, d))
+
def blackman(M):
"""
Return the Blackman window.
@@ -1967,7 +1998,8 @@ def blackman(M):
if M == 1:
return ones(1, float)
n = arange(0, M)
- return 0.42-0.5*cos(2.0*pi*n/(M-1)) + 0.08*cos(4.0*pi*n/(M-1))
+ return 0.42 - 0.5*cos(2.0*pi*n/(M-1)) + 0.08*cos(4.0*pi*n/(M-1))
+
def bartlett(M):
"""
@@ -2073,7 +2105,8 @@ def bartlett(M):
if M == 1:
return ones(1, float)
n = arange(0, M)
- return where(less_equal(n, (M-1)/2.0), 2.0*n/(M-1), 2.0-2.0*n/(M-1))
+ return where(less_equal(n, (M-1)/2.0), 2.0*n/(M-1), 2.0 - 2.0*n/(M-1))
+
def hanning(M):
"""
@@ -2171,7 +2204,8 @@ def hanning(M):
if M == 1:
return ones(1, float)
n = arange(0, M)
- return 0.5-0.5*cos(2.0*pi*n/(M-1))
+ return 0.5 - 0.5*cos(2.0*pi*n/(M-1))
+
def hamming(M):
"""
@@ -2268,68 +2302,71 @@ def hamming(M):
if M == 1:
return ones(1, float)
n = arange(0, M)
- return 0.54-0.46*cos(2.0*pi*n/(M-1))
+ return 0.54 - 0.46*cos(2.0*pi*n/(M-1))
## Code from cephes for i0
_i0A = [
--4.41534164647933937950E-18,
- 3.33079451882223809783E-17,
--2.43127984654795469359E-16,
- 1.71539128555513303061E-15,
--1.16853328779934516808E-14,
- 7.67618549860493561688E-14,
--4.85644678311192946090E-13,
- 2.95505266312963983461E-12,
--1.72682629144155570723E-11,
- 9.67580903537323691224E-11,
--5.18979560163526290666E-10,
- 2.65982372468238665035E-9,
--1.30002500998624804212E-8,
- 6.04699502254191894932E-8,
--2.67079385394061173391E-7,
- 1.11738753912010371815E-6,
--4.41673835845875056359E-6,
- 1.64484480707288970893E-5,
--5.75419501008210370398E-5,
- 1.88502885095841655729E-4,
--5.76375574538582365885E-4,
- 1.63947561694133579842E-3,
--4.32430999505057594430E-3,
- 1.05464603945949983183E-2,
--2.37374148058994688156E-2,
- 4.93052842396707084878E-2,
--9.49010970480476444210E-2,
- 1.71620901522208775349E-1,
--3.04682672343198398683E-1,
- 6.76795274409476084995E-1]
+ -4.41534164647933937950E-18,
+ 3.33079451882223809783E-17,
+ -2.43127984654795469359E-16,
+ 1.71539128555513303061E-15,
+ -1.16853328779934516808E-14,
+ 7.67618549860493561688E-14,
+ -4.85644678311192946090E-13,
+ 2.95505266312963983461E-12,
+ -1.72682629144155570723E-11,
+ 9.67580903537323691224E-11,
+ -5.18979560163526290666E-10,
+ 2.65982372468238665035E-9,
+ -1.30002500998624804212E-8,
+ 6.04699502254191894932E-8,
+ -2.67079385394061173391E-7,
+ 1.11738753912010371815E-6,
+ -4.41673835845875056359E-6,
+ 1.64484480707288970893E-5,
+ -5.75419501008210370398E-5,
+ 1.88502885095841655729E-4,
+ -5.76375574538582365885E-4,
+ 1.63947561694133579842E-3,
+ -4.32430999505057594430E-3,
+ 1.05464603945949983183E-2,
+ -2.37374148058994688156E-2,
+ 4.93052842396707084878E-2,
+ -9.49010970480476444210E-2,
+ 1.71620901522208775349E-1,
+ -3.04682672343198398683E-1,
+ 6.76795274409476084995E-1
+ ]
_i0B = [
--7.23318048787475395456E-18,
--4.83050448594418207126E-18,
- 4.46562142029675999901E-17,
- 3.46122286769746109310E-17,
--2.82762398051658348494E-16,
--3.42548561967721913462E-16,
- 1.77256013305652638360E-15,
- 3.81168066935262242075E-15,
--9.55484669882830764870E-15,
--4.15056934728722208663E-14,
- 1.54008621752140982691E-14,
- 3.85277838274214270114E-13,
- 7.18012445138366623367E-13,
--1.79417853150680611778E-12,
--1.32158118404477131188E-11,
--3.14991652796324136454E-11,
- 1.18891471078464383424E-11,
- 4.94060238822496958910E-10,
- 3.39623202570838634515E-9,
- 2.26666899049817806459E-8,
- 2.04891858946906374183E-7,
- 2.89137052083475648297E-6,
- 6.88975834691682398426E-5,
- 3.36911647825569408990E-3,
- 8.04490411014108831608E-1]
+ -7.23318048787475395456E-18,
+ -4.83050448594418207126E-18,
+ 4.46562142029675999901E-17,
+ 3.46122286769746109310E-17,
+ -2.82762398051658348494E-16,
+ -3.42548561967721913462E-16,
+ 1.77256013305652638360E-15,
+ 3.81168066935262242075E-15,
+ -9.55484669882830764870E-15,
+ -4.15056934728722208663E-14,
+ 1.54008621752140982691E-14,
+ 3.85277838274214270114E-13,
+ 7.18012445138366623367E-13,
+ -1.79417853150680611778E-12,
+ -1.32158118404477131188E-11,
+ -3.14991652796324136454E-11,
+ 1.18891471078464383424E-11,
+ 4.94060238822496958910E-10,
+ 3.39623202570838634515E-9,
+ 2.26666899049817806459E-8,
+ 2.04891858946906374183E-7,
+ 2.89137052083475648297E-6,
+ 6.88975834691682398426E-5,
+ 3.36911647825569408990E-3,
+ 8.04490411014108831608E-1
+ ]
+
def _chbevl(x, vals):
b0 = vals[0]
@@ -2342,12 +2379,15 @@ def _chbevl(x, vals):
return 0.5*(b0 - b2)
+
def _i0_1(x):
return exp(x) * _chbevl(x/2.0-2, _i0A)
+
def _i0_2(x):
return exp(x) * _chbevl(32.0/x - 2.0, _i0B) / sqrt(x)
+
def i0(x):
"""
Modified Bessel function of the first kind, order 0.
@@ -2404,9 +2444,9 @@ def i0(x):
"""
x = atleast_1d(x).copy()
y = empty_like(x)
- ind = (x<0)
+ ind = (x < 0)
x[ind] = -x[ind]
- ind = (x<=8.0)
+ ind = (x <= 8.0)
y[ind] = _i0_1(x[ind])
ind2 = ~ind
y[ind2] = _i0_2(x[ind2])
@@ -2414,6 +2454,7 @@ def i0(x):
## End of cephes code for i0
+
def kaiser(M, beta):
"""
Return the Kaiser window.
@@ -2541,6 +2582,7 @@ def kaiser(M, beta):
alpha = (M-1)/2.0
return i0(beta * sqrt(1-((n-alpha)/alpha)**2.0))/i0(float(beta))
+
def sinc(x):
"""
Return the sinc function.
@@ -2616,9 +2658,10 @@ def sinc(x):
"""
x = np.asanyarray(x)
- y = pi* where(x == 0, 1.0e-20, x)
+ y = pi * where(x == 0, 1.0e-20, x)
return sin(y)/y
+
def msort(a):
"""
Return a copy of an array sorted along the first axis.
@@ -2646,6 +2689,7 @@ def msort(a):
b.sort(0)
return b
+
def median(a, axis=None, out=None, overwrite_input=False):
"""
Compute the median along the specified axis.
@@ -2722,7 +2766,8 @@ def median(a, axis=None, out=None, overwrite_input=False):
"""
a = np.asarray(a)
if axis is not None and axis >= a.ndim:
- raise IndexError("axis %d out of bounds (%d)" % (axis, a.ndim))
+ raise IndexError(
+ "axis %d out of bounds (%d)" % (axis, a.ndim))
if overwrite_input:
if axis is None:
@@ -2864,7 +2909,8 @@ def percentile(a, q, interpolation='linear', axis=None, out=None,
q = atleast_1d(q)
q = q / 100.0
if (q < 0).any() or (q > 1).any():
- raise ValueError("Percentiles must be in the range [0,100]")
+ raise ValueError(
+ "Percentiles must be in the range [0,100]")
# prepare a for partioning
if overwrite_input:
@@ -2896,8 +2942,9 @@ def percentile(a, q, interpolation='linear', axis=None, out=None,
elif interpolation == 'linear':
pass # keep index as fraction and interpolate
else:
- raise ValueError("interpolation can only be 'linear', 'lower' "
- "'higher', 'midpoint', or 'nearest'")
+ raise ValueError(
+ "interpolation can only be 'linear', 'lower' 'higher', "
+ "'midpoint', or 'nearest'")
if indices.dtype == intp: # take the points along axis
ap.partition(indices, axis=axis)
@@ -3003,13 +3050,15 @@ def trapz(y, x=None, dx=1.0, axis=-1):
slice1[axis] = slice(1, None)
slice2[axis] = slice(None, -1)
try:
- ret = (d * (y[slice1] +y [slice2]) / 2.0).sum(axis)
- except ValueError: # Operations didn't work, cast to ndarray
+ ret = (d * (y[slice1] + y[slice2]) / 2.0).sum(axis)
+ except ValueError:
+ # Operations didn't work, cast to ndarray
d = np.asarray(d)
y = np.asarray(y)
ret = add.reduce(d * (y[slice1]+y[slice2])/2.0, axis)
return ret
+
#always succeed
def add_newdoc(place, obj, doc):
"""Adds documentation to obj which is in module place.
@@ -3083,14 +3132,14 @@ def meshgrid(*xi, **kwargs):
Notes
-----
- This function supports both indexing conventions through the indexing keyword
- argument. Giving the string 'ij' returns a meshgrid with matrix indexing,
- while 'xy' returns a meshgrid with Cartesian indexing. In the 2-D case
- with inputs of length M and N, the outputs are of shape (N, M) for 'xy'
- indexing and (M, N) for 'ij' indexing. In the 3-D case with inputs of
- length M, N and P, outputs are of shape (N, M, P) for 'xy' indexing and (M,
- N, P) for 'ij' indexing. The difference is illustrated by the following
- code snippet::
+ This function supports both indexing conventions through the indexing
+ keyword argument. Giving the string 'ij' returns a meshgrid with matrix
+ indexing, while 'xy' returns a meshgrid with Cartesian indexing. In the
+ 2-D case with inputs of length M and N, the outputs are of shape (N, M) for
+ 'xy' indexing and (M, N) for 'ij' indexing. In the 3-D case with inputs of
+ length M, N and P, outputs are of shape (N, M, P) for 'xy' indexing and
+ (M, N, P) for 'ij' indexing. The difference is illustrated by the
+ following code snippet::
xv, yv = meshgrid(x, y, sparse=False, indexing='ij')
for i in range(nx):
@@ -3138,8 +3187,9 @@ def meshgrid(*xi, **kwargs):
"""
if len(xi) < 2:
- msg = 'meshgrid() takes 2 or more arguments (%d given)' % int(len(xi) > 0)
- raise ValueError(msg)
+ raise ValueError(
+ 'meshgrid() takes 2 or more arguments '
+ '(%d given)' % int(len(xi) > 0))
args = np.atleast_1d(*xi)
ndim = len(args)
@@ -3148,10 +3198,12 @@ def meshgrid(*xi, **kwargs):
sparse = kwargs.get('sparse', False)
indexing = kwargs.get('indexing', 'xy')
if not indexing in ['xy', 'ij']:
- raise ValueError("Valid values for `indexing` are 'xy' and 'ij'.")
+ raise ValueError(
+ "Valid values for `indexing` are 'xy' and 'ij'.")
s0 = (1,) * ndim
- output = [x.reshape(s0[:i] + (-1,) + s0[i + 1::]) for i, x in enumerate(args)]
+ output = [x.reshape(s0[:i] + (-1,) + s0[i + 1::])
+ for i, x in enumerate(args)]
shape = [x.size for x in output]
@@ -3242,12 +3294,12 @@ def delete(arr, obj, axis=None):
if axis is None:
if ndim != 1:
arr = arr.ravel()
- ndim = arr.ndim;
- axis = ndim-1;
+ ndim = arr.ndim
+ axis = ndim - 1
if ndim == 0:
- warnings.warn("in the future the special handling of scalars "
- "will be removed from delete and raise an error",
- DeprecationWarning)
+ warnings.warn(
+ "in the future the special handling of scalars will be removed "
+ "from delete and raise an error", DeprecationWarning)
if wrap:
return wrap(arr)
else:
@@ -3312,18 +3364,20 @@ def delete(arr, obj, axis=None):
# After removing the special handling of booleans and out of
# bounds values, the conversion to the array can be removed.
if obj.dtype == bool:
- warnings.warn("in the future insert will treat boolean arrays "
- "and array-likes as boolean index instead "
- "of casting it to integer", FutureWarning)
+ warnings.warn(
+ "in the future insert will treat boolean arrays and array-likes "
+ "as boolean index instead of casting it to integer", FutureWarning)
obj = obj.astype(intp)
if isinstance(_obj, (int, long, integer)):
# optimization for a single value
obj = obj.item()
- if (obj < -N or obj >=N):
- raise IndexError("index %i is out of bounds for axis "
- "%i with size %i" % (obj, axis, N))
- if (obj < 0): obj += N
- newshape[axis]-=1;
+ if (obj < -N or obj >= N):
+ raise IndexError(
+ "index %i is out of bounds for axis %i with "
+ "size %i" % (obj, axis, N))
+ if (obj < 0):
+ obj += N
+ newshape[axis] -= 1
new = empty(newshape, arr.dtype, arr.flags.fnc)
slobj[axis] = slice(None, obj)
new[slobj] = arr[slobj]
@@ -3337,25 +3391,28 @@ def delete(arr, obj, axis=None):
if not np.can_cast(obj, intp, 'same_kind'):
# obj.size = 1 special case always failed and would just
# give superfluous warnings.
- warnings.warn("using a non-integer array as obj in delete "
- "will result in an error in the future", DeprecationWarning)
+ warnings.warn(
+ "using a non-integer array as obj in delete will result in an "
+ "error in the future", DeprecationWarning)
obj = obj.astype(intp)
keep = ones(N, dtype=bool)
# Test if there are out of bound indices, this is deprecated
inside_bounds = (obj < N) & (obj >= -N)
if not inside_bounds.all():
- warnings.warn("in the future out of bounds indices will raise an "
- "error instead of being ignored by `numpy.delete`.",
- DeprecationWarning)
+ warnings.warn(
+ "in the future out of bounds indices will raise an error "
+ "instead of being ignored by `numpy.delete`.",
+ DeprecationWarning)
obj = obj[inside_bounds]
positive_indices = obj >= 0
if not positive_indices.all():
- warnings.warn("in the future negative indices will not be ignored "
- "by `numpy.delete`.", FutureWarning)
+ warnings.warn(
+ "in the future negative indices will not be ignored by "
+ "`numpy.delete`.", FutureWarning)
obj = obj[positive_indices]
- keep[obj,] = False
+ keep[obj, ] = False
slobj[axis] = keep
new = arr[slobj]
@@ -3464,16 +3521,18 @@ def insert(arr, obj, values, axis=None):
if ndim != 1:
arr = arr.ravel()
ndim = arr.ndim
- axis = ndim-1
+ axis = ndim - 1
else:
if ndim > 0 and (axis < -ndim or axis >= ndim):
- raise IndexError("axis %i is out of bounds for an array "
- "of dimension %i" % (axis, ndim))
- if (axis < 0): axis += ndim
+ raise IndexError(
+ "axis %i is out of bounds for an array of "
+ "dimension %i" % (axis, ndim))
+ if (axis < 0):
+ axis += ndim
if (ndim == 0):
- warnings.warn("in the future the special handling of scalars "
- "will be removed from insert and raise an error",
- DeprecationWarning)
+ warnings.warn(
+ "in the future the special handling of scalars will be removed "
+ "from insert and raise an error", DeprecationWarning)
arr = arr.copy()
arr[...] = values
if wrap:
@@ -3486,15 +3545,16 @@ def insert(arr, obj, values, axis=None):
if isinstance(obj, slice):
# turn it into a range object
- indices = arange(*obj.indices(N),**{'dtype':intp})
+ indices = arange(*obj.indices(N), **{'dtype': intp})
else:
# need to copy obj, because indices will be changed in-place
indices = np.array(obj)
if indices.dtype == bool:
# See also delete
- warnings.warn("in the future insert will treat boolean arrays "
- "and array-likes as a boolean index instead "
- "of casting it to integer", FutureWarning)
+ warnings.warn(
+ "in the future insert will treat boolean arrays and "
+ "array-likes as a boolean index instead of casting it to "
+ "integer", FutureWarning)
indices = indices.astype(intp)
# Code after warning period:
#if obj.ndim != 1:
@@ -3502,14 +3562,17 @@ def insert(arr, obj, values, axis=None):
# 'must be one dimensional')
#indices = np.flatnonzero(obj)
elif indices.ndim > 1:
- raise ValueError("index array argument obj to insert must "
- "be one dimensional or scalar")
+ raise ValueError(
+ "index array argument obj to insert must be one dimensional "
+ "or scalar")
if indices.size == 1:
index = indices.item()
if index < -N or index > N:
- raise IndexError("index %i is out of bounds for axis "
- "%i with size %i" % (obj, axis, N))
- if (index < 0): index += N
+ raise IndexError(
+ "index %i is out of bounds for axis %i with "
+ "size %i" % (obj, axis, N))
+ if (index < 0):
+ index += N
values = array(values, copy=False, ndmin=arr.ndim)
if indices.ndim == 0:
@@ -3536,15 +3599,15 @@ def insert(arr, obj, values, axis=None):
indices = indices.astype(intp)
if not np.can_cast(indices, intp, 'same_kind'):
- warnings.warn("using a non-integer array as obj in insert "
- "will result in an error in the future",
- DeprecationWarning)
+ warnings.warn(
+ "using a non-integer array as obj in insert will result in an "
+ "error in the future", DeprecationWarning)
indices = indices.astype(intp)
indices[indices < 0] += N
numnew = len(indices)
- order = indices.argsort(kind='mergesort') # stable sort
+ order = indices.argsort(kind='mergesort') # stable sort
indices[order] += np.arange(numnew)
newshape[axis] += numnew
@@ -3562,6 +3625,7 @@ def insert(arr, obj, values, axis=None):
return wrap(new)
return new
+
def append(arr, values, axis=None):
"""
Append values to the end of an array.