From 9efaa09baca5ce2d64447bdcc91556227ab717c2 Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Sat, 17 Jan 2009 21:24:13 +0000 Subject: Make `trapz` accept 1-D `x` parameter for n-d `y`, even if axis != -1. Additional tests included. --- numpy/lib/function_base.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'numpy/lib/function_base.py') diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 425960639..269a97721 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -2818,9 +2818,9 @@ def trapz(y, x=None, dx=1.0, axis=-1): y : array_like Input array to integrate. x : array_like, optional - If `x` is None, then spacing between all `y` elements is 1. + If `x` is None, then spacing between all `y` elements is `dx`. dx : scalar, optional - If `x` is None, spacing given by `dx` is assumed. + If `x` is None, spacing given by `dx` is assumed. Default is 1. axis : int, optional Specify the axis. @@ -2836,7 +2836,15 @@ def trapz(y, x=None, dx=1.0, axis=-1): if x is None: d = dx else: - d = diff(x,axis=axis) + x = asarray(x) + if x.ndim == 1: + d = diff(x) + # reshape to correct shape + shape = [1]*y.ndim + shape[axis] = d.shape[0] + d = d.reshape(shape) + else: + d = diff(x, axis=axis) nd = len(y.shape) slice1 = [slice(None)]*nd slice2 = [slice(None)]*nd -- cgit v1.2.1