def fix(x, y=None): """ Round x to nearest integer towards zero. """ x = asanyarray(x) if y is None: y = nx.floor(x) else: nx.floor(x, y) if x.ndim == 0: if (x<0): y += 1 else: y[x<0] = y[x<0]+1 return y
def fix(x, out=None): """ Round to nearest integer towards zero. Round an array of floats element-wise to nearest integer towards zero. The rounded values are returned as floats. Parameters ---------- x : array_like An array of floats to be rounded y : ndarray, optional Output array Returns ------- out : ndarray of floats The array of rounded numbers See Also -------- trunc, floor, ceil around : Round to given number of decimals Examples -------- >>> np.fix(3.14) 3.0 >>> np.fix(3) 3.0 >>> np.fix([2.1, 2.9, -2.1, -2.9]) array([ 2., 2., -2., -2.]) """ # promote back to an array if flattened res = nx.asanyarray(nx.ceil(x, out=out)) res = nx.floor(x, out=res, where=nx.greater_equal(x, 0)) # when no out argument is passed and no subclasses are involved, flatten # scalars if out is None and type(res) is nx.ndarray: res = res[()] return res
def fix(x, y=None): """ Round to nearest integer towards zero. Round an array of floats element-wise to nearest integer towards zero. The rounded values are returned as floats. Parameters ---------- x : array_like An array of floats to be rounded y : ndarray, optional Output array Returns ------- out : ndarray of floats The array of rounded numbers See Also -------- trunc, floor, ceil around : Round to given number of decimals Examples -------- >>> np.fix(3.14) 3.0 >>> np.fix(3) 3.0 >>> np.fix([2.1, 2.9, -2.1, -2.9]) array([ 2., 2., -2., -2.]) """ x = nx.asanyarray(x) y1 = nx.floor(x) y2 = nx.ceil(x) if y is None: y = nx.asanyarray(y1) y[...] = nx.where(x >= 0, y1, y2) return y
def _weighted_quantile_ureduce_func(a, q, w=None, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False): a = asarray(a) # ufuncs cause 0d array results to decay to scalars (see gh-13105), which # makes them problematic for __setitem__ and attribute access. As a # workaround, we call this on the result of every ufunc on a possibly-0d # array. not_scalar = np.asanyarray # prepare a for partitioning if overwrite_input: if axis is None: ap = a.ravel() wp = w.ravel() else: ap = a wp = w else: if axis is None: ap = a.flatten() wp = w.flatten() else: ap = a.copy() wp = w.copy() if axis is None: axis = 0 if q.ndim > 2: # The code below works fine for nd, but it might not have useful # semantics. For now, keep the supported dimensions the same as it was # before. raise ValueError("q must be a scalar or 1d") q = np.atleast_1d(q) Nx = ap.shape[axis] sorted_indices = np.argsort(ap, axis=axis) sorted_a = np.take_along_axis(ap, sorted_indices, axis=axis) sorted_w = np.take_along_axis(wp, sorted_indices, axis=axis) sorted_a = np.moveaxis(sorted_a, axis, 0) sorted_w = np.moveaxis(sorted_w, axis, 0) sk = np.asarray([ k * sorted_w.take(indices=k, axis=0) + (Nx - 1) * np.sum( sorted_w.take(indices=range(k), axis=0), axis=0, ) for k in range(Nx) ]) sn = sk.take(indices=(-1, ), axis=0) normalized_w = sk / sn normalized_w = normalized_w.reshape( (normalized_w.shape[0], -1)) # (Nx, -1) sorted_indice_q = np.argsort(q) recover_original_q = np.argsort(sorted_indice_q) target_indices = [] for j in range(len(normalized_w[0])): i = 0 target_indice = [] for k in sorted_indice_q: while i < Nx: if q[k] == normalized_w[i][j]: target_indice.append(i) break if i > 0: if normalized_w[i - 1][j] < q[k] < normalized_w[i][j]: if interpolation == 'lower': target_indice.append(i - 1) elif interpolation == 'higher': target_indice.append(i) elif interpolation == 'midpoint': target_indice.append((2 * i - 1) / 2) elif interpolation == 'nearest': if (q[k] - normalized_w[i-1][j]) < \ (normalized_w[i][j] - q[k]): target_indice.append(i - 1) elif ((i-1)%2 == 0 and q[k] - normalized_w[i-1][j] \ == normalized_w[i][j] - q[k]): target_indice.append(i - 1) else: target_indice.append(i) elif interpolation == 'linear': target_indice.append((2 * i - 1) / 2) else: raise ValueError( "interpolation can only be 'linear', 'lower' " "'higher', 'midpoint', or 'nearest'") break i += 1 if i == Nx: target_indice.append(0) target_indices.append(target_indice) target_indices = asarray(target_indices) target_indices = np.moveaxis(target_indices, -1, 0) # The dimensions of `q` are prepended to the output shape, so we need the # axis being sampled from `ap` to be first. # ap = np.moveaxis(ap, axis, 0) # del axis if np.issubdtype(target_indices.dtype, np.integer): # take the points along axis target_indices = target_indices.reshape((-1, ) + sorted_a.shape[1:]) if np.issubdtype(a.dtype, np.inexact): n = np.isnan(sorted_a[-1]) else: # Cannot contain nan n = np.array(False, dtype=bool) r = np.take_along_axis( sorted_a, target_indices, axis=0, ) else: # weight the points above and below the indices target_indices_below = floor(target_indices).astype(intp) target_indices_above = ceil(target_indices).astype(intp) # Nx x -1 # q x -1 Sk_differ = np.take_along_axis( normalized_w, target_indices_above, axis=0) \ - np.take_along_axis(normalized_w, target_indices_below, axis=0) # print (q.size).reshape(target_indices_below.shape) q_minus_sk = np.expand_dims(q[sorted_indice_q], axis=-1) \ - np.take_along_axis(normalized_w, target_indices_below, axis=0) q_minus_sk_nonzero = q_minus_sk > 0 weights_above = np.divide(q_minus_sk, Sk_differ, where=q_minus_sk_nonzero) weights_above[~q_minus_sk_nonzero] = 0 if target_indices.ndim != sorted_a.ndim: target_indices_below = target_indices_below.reshape( (-1, ) + sorted_a.shape[1:]) target_indices_above = target_indices_above.reshape( (-1, ) + sorted_a.shape[1:]) weights_above = weights_above.reshape((-1, ) + sorted_a.shape[1:]) normalized_w = normalized_w.reshape((-1, ) + sorted_a.shape[1:]) x_below = np.take_along_axis(sorted_a, target_indices_below, axis=0) x_above = np.take_along_axis(sorted_a, target_indices_above, axis=0) if np.issubdtype(a.dtype, np.inexact): # May contain nan, which would sort to the end n = np.isnan(sorted_a[-1]) else: # Cannot contain nan n = np.array(False, dtype=bool) if interpolation == 'midpoint': r = 0.5 * (x_below + x_above) else: r = _lerp(x_below, x_above, weights_above, out=out) # if any slice contained a nan, then all results on that slice are also nan if r.ndim > 0: r = r[recover_original_q] if np.any(n): if r.ndim == 0 and out is None: # can't write to a scalar r = a.dtype.type(np.nan) else: r[..., n] = a.dtype.type(np.nan) return r