Simplified common_shape

master
Per A Brodtkorb 9 years ago
parent 67eb1e6a02
commit 990e2a5151

@ -7,7 +7,6 @@ from scipy.special import ndtr as cdfnorm, ndtri as invnorm
from scipy.special import erfc from scipy.special import erfc
import warnings import warnings
import numpy as np import numpy as np
from .misc import common_shape
try: try:
from . import mvn # @UnresolvedImport from . import mvn # @UnresolvedImport
@ -836,10 +835,9 @@ def cdfnorm2d(b1, b2, r):
# pullman, wa 99164-3113 # pullman, wa 99164-3113
# email : alangenz@wsu.edu # email : alangenz@wsu.edu
cshape = common_shape(b1, b2, r, shape=[1, ]) b1, b2, r = np.broadcast_arrays(b1, b2, r)
one = ones(cshape) cshape = b1.shape
h, k, r = -b1.ravel(), -b2.ravel(), r.ravel()
h, k, r = (-b1 * one).ravel(), (-b2 * one).ravel(), (r * one).ravel()
bvn = where(abs(r) > 1, nan, 0.0) bvn = where(abs(r) > 1, nan, 0.0)

@ -1764,48 +1764,10 @@ def common_shape(*args, ** kwds):
-------- --------
broadcast, broadcast_arrays broadcast, broadcast_arrays
''' '''
args = [asarray(x) for x in args]
shapes = [x.shape for x in args]
shape = kwds.get('shape') shape = kwds.get('shape')
if shape is not None: x0 = 1 if shape is None else np.ones(shape)
if not isinstance(shape, (list, tuple)): x1 = np.broadcast(x0, *args)
shape = (shape,) return tuple(x1.shape)
shapes.append(tuple(shape))
if len(set(shapes)) == 1:
# Common case where nothing needs to be broadcasted.
return tuple(shapes[0])
shapes = [list(s) for s in shapes]
nds = [len(s) for s in shapes]
biggest = max(nds)
# Go through each array and prepend dimensions of length 1 to each of the
# shapes in order to make the number of dimensions equal.
for i in range(len(shapes)):
diff = biggest - nds[i]
if diff > 0:
shapes[i] = [1] * diff + shapes[i]
# Check each dimension for compatibility. A dimension length of 1 is
# accepted as compatible with any other length.
c_shape = []
for axis in range(biggest):
lengths = [s[axis] for s in shapes]
unique = set(lengths + [1])
if len(unique) > 2:
# There must be at least two non-1 lengths for this axis.
raise ValueError("shape mismatch: two or more arrays have "
"incompatible dimensions on axis %r." % (axis,))
elif len(unique) == 2:
# There is exactly one non-1 length.
# The common shape will take this value.
unique.remove(1)
new_length = unique.pop()
c_shape.append(new_length)
else:
# Every array has a length of 1 on this axis. Strides can be left
# alone as nothing is broadcasted.
c_shape.append(1)
return tuple(c_shape)
def argsreduce(condition, * args): def argsreduce(condition, * args):
@ -2787,7 +2749,7 @@ def num2pistr(x, n=3, numerator_max=10, denominator_max=10):
return fmt % x return fmt % x
def fourier(data, t=None, T=None, m=None, n=None, method='trapz'): def fourier(data, t=None, period=None, m=None, n=None, method='trapz'):
''' '''
Returns Fourier coefficients. Returns Fourier coefficients.
@ -2797,7 +2759,7 @@ def fourier(data, t=None, T=None, m=None, n=None, method='trapz'):
vector or matrix of row vectors with data points shape p x n. vector or matrix of row vectors with data points shape p x n.
t : array-like t : array-like
vector with n values indexed from 1 to N. vector with n values indexed from 1 to N.
T : real scalar, (default T = t[-1]-t[0]) period : real scalar, (default T = t[-1]-t[0])
primitive period of signal, i.e., smallest period. primitive period of signal, i.e., smallest period.
m : scalar integer m : scalar integer
defines no of harmonics desired (default M = N) defines no of harmonics desired (default M = N)
@ -2845,19 +2807,12 @@ def fourier(data, t=None, T=None, m=None, n=None, method='trapz'):
''' '''
x = np.atleast_2d(data) x = np.atleast_2d(data)
p, n = x.shape p, n = x.shape
if t is None: t = np.arange(n) if t is None else np.atleast_1d(t)
t = np.arange(n)
else:
t = np.atleast_1d(t)
n = len(t) if n is None else n n = len(t) if n is None else n
m = n if n is None else m m = n if m is None else m
T = t[-1] - t[0] if T is None else T period = t[-1] - t[0] if period is None else period
intfun = trapz if method.startswith('trapz') else simps
if method.startswith('trapz'):
intfun = trapz
elif method.startswith('simp'):
intfun = simps
# Define the vectors for computing the Fourier coefficients # Define the vectors for computing the Fourier coefficients
t.shape = (1, -1) t.shape = (1, -1)
@ -2866,8 +2821,7 @@ def fourier(data, t=None, T=None, m=None, n=None, method='trapz'):
a[0] = intfun(x, t, axis=-1) a[0] = intfun(x, t, axis=-1)
# Compute M-1 more coefficients # Compute M-1 more coefficients
tmp = 2 * pi * t / T tmp = 2 * pi * t / period
# tmp = 2*pi*(0:N-1).'/(N-1);
for i in range(1, m): for i in range(1, m):
a[i] = intfun(x * cos(i * tmp), t, axis=-1) a[i] = intfun(x * cos(i * tmp), t, axis=-1)
b[i] = intfun(x * sin(i * tmp), t, axis=-1) b[i] = intfun(x * sin(i * tmp), t, axis=-1)

Loading…
Cancel
Save