Python numpy.result_type() Examples
The following are 30
code examples of numpy.result_type().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: align.py From Computable with MIT License | 6 votes |
def _filter_special_cases(f): @wraps(f) def wrapper(terms): # single unary operand if len(terms) == 1: return _align_core_single_unary_op(terms[0]) term_values = (term.value for term in terms) # only scalars or indexes if all(isinstance(term.value, pd.Index) or term.isscalar for term in terms): return np.result_type(*term_values), None # no pandas objects if not _any_pandas_objects(terms): return np.result_type(*term_values), None return f(terms) return wrapper
Example #2
Source File: histogram.py From mars with Apache License 2.0 | 6 votes |
def _unsigned_subtract(a, b): """ Subtract two values where a >= b, and produce an unsigned result This is needed when finding the difference between the upper and lower bound of an int16 histogram """ # coerce to a single type signed_to_unsigned = { np.byte: np.ubyte, np.short: np.ushort, np.intc: np.uintc, np.int_: np.uint, np.longlong: np.ulonglong } dt = np.result_type(a, b) try: dt = signed_to_unsigned[dt.type] except KeyError: # pragma: no cover return np.subtract(a, b, dtype=dt) else: # we know the inputs are integers, and we are deliberately casting # signed to unsigned return np.subtract(a, b, casting='unsafe', dtype=dt)
Example #3
Source File: lax_numpy_test.py From trax with Apache License 2.0 | 6 votes |
def testQuantile(self, op, a_rng, q_rng, a_shape, a_dtype, q_shape, q_dtype, axis, keepdims): if op == "quantile" and numpy_version < (1, 15): raise SkipTest("Numpy < 1.15 does not have np.quantile") if op == "median": args_maker = lambda: [a_rng(a_shape, a_dtype)] else: args_maker = lambda: [a_rng(a_shape, a_dtype), q_rng(q_shape, q_dtype)] def onp_fun(*args): args = [x if lnp.result_type(x) != lnp.bfloat16 else onp.asarray(x, onp.float32) for x in args] return getattr(onp, op)(*args, axis=axis, keepdims=keepdims) lnp_fun = partial(getattr(lnp, op), axis=axis, keepdims=keepdims) # TODO(phawkins): we currently set dtype=False because we aren't as # aggressive about promoting to float64. It's not clear we want to mimic # Numpy here. tol_spec = {onp.float32: 2e-4, onp.float64: 5e-6} tol = max(jtu.tolerance(a_dtype, tol_spec), jtu.tolerance(q_dtype, tol_spec)) self._CheckAgainstNumpy(onp_fun, lnp_fun, args_maker, check_dtypes=False, tol=tol) self._CompileAndCheck(lnp_fun, args_maker, check_dtypes=True, rtol=tol)
Example #4
Source File: align.py From Computable with MIT License | 6 votes |
def _align(terms): """Align a set of terms""" try: # flatten the parse tree (a nested list, really) terms = list(com.flatten(terms)) except TypeError: # can't iterate so it must just be a constant or single variable if isinstance(terms.value, pd.core.generic.NDFrame): typ = type(terms.value) return typ, _zip_axes_from_type(typ, terms.value.axes) return np.result_type(terms.type), None # if all resolved variables are numeric scalars if all(term.isscalar for term in terms): return np.result_type(*(term.value for term in terms)).type, None # perform the main alignment typ, axes = _align_core(terms) return typ, axes
Example #5
Source File: lax_numpy_test.py From trax with Apache License 2.0 | 6 votes |
def testDot(self, lhs_shape, lhs_dtype, rhs_shape, rhs_dtype, rng_factory): rng = rng_factory() args_maker = lambda: [rng(lhs_shape, lhs_dtype), rng(rhs_shape, rhs_dtype)] tol = {onp.float16: 1e-2, onp.float32: 1e-5, onp.float64: 1e-14, onp.complex128: 1e-14} if jtu.device_under_test() == "tpu": tol[onp.float32] = tol[onp.complex64] = 2e-1 def onp_dot(x, y): x = x.astype(onp.float32) if lhs_dtype == lnp.bfloat16 else x y = y.astype(onp.float32) if rhs_dtype == lnp.bfloat16 else y # `onp.dot(x, y).dtype` sometimes differs from `onp.result_type(x, y)` # (e.g. when x is float64[] and y is complex64[3,3], or when x is # float16[3,3] and y is int64[]). We ignore this corner case and pretend # that they agree. return onp.dot(x, y).astype(onp.result_type(x, y)) self._CheckAgainstNumpy(onp_dot, lnp.dot, args_maker, check_dtypes=True, tol=tol) # We disable dtype check in the following cases because `np.dot` does # value-dependent type promotion in those cases. check_dtypes = () not in (lhs_shape, rhs_shape) self._CompileAndCheck(lnp.dot, args_maker, check_dtypes=check_dtypes, atol=tol, rtol=tol, check_incomplete_shape=True)
Example #6
Source File: lax_numpy_test.py From trax with Apache License 2.0 | 6 votes |
def testBitwiseOp(self, onp_op, lnp_op, rng_factory, shapes, dtypes): rng = rng_factory() args_maker = self._GetArgsMaker(rng, shapes, dtypes) has_python_scalar = jtu.PYTHON_SCALAR_SHAPE in shapes self._CheckAgainstNumpy(onp_op, lnp_op, args_maker, check_dtypes=True) if onp_op == onp.bitwise_not and has_python_scalar: # For bitwise_not with a Python `int`, npe.jit may choose a different # dtype for the `int` from onp's choice, which may result in a different # result value, so we skip _CompileAndCheck. return # Numpy does value-dependent dtype promotion on Python/numpy/array scalars # which `jit` can't do (when np.result_type is called inside `jit`, tensor # values are not available), so we skip dtype check in this case. check_dtypes = not(set(shapes) & set([jtu.NUMPY_SCALAR_SHAPE, jtu.PYTHON_SCALAR_SHAPE, ()])) self._CompileAndCheck(lnp_op, args_maker, check_dtypes=check_dtypes)
Example #7
Source File: lax_numpy_test.py From trax with Apache License 2.0 | 6 votes |
def _promote_like_lnp(fun, inexact=False): """Decorator that promotes the arguments of `fun` to `lnp.result_type(*args)`. lnp and onp have different type promotion semantics; this decorator allows tests make an onp reference implementation act more like an lnp implementation. """ def wrapper(*args, **kw): flat_args = tf.nest.flatten(args) if inexact and not any(lnp.issubdtype(lnp.result_type(x), lnp.inexact) for x in flat_args): dtype = lnp.result_type(lnp.float_, *flat_args) else: dtype = lnp.result_type(*flat_args) args = tf.nest.map_structure(lambda a: onp.asarray(a, dtype), args) return fun(*args, **kw) return wrapper
Example #8
Source File: histograms.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def _unsigned_subtract(a, b): """ Subtract two values where a >= b, and produce an unsigned result This is needed when finding the difference between the upper and lower bound of an int16 histogram """ # coerce to a single type signed_to_unsigned = { np.byte: np.ubyte, np.short: np.ushort, np.intc: np.uintc, np.int_: np.uint, np.longlong: np.ulonglong } dt = np.result_type(a, b) try: dt = signed_to_unsigned[dt.type] except KeyError: return np.subtract(a, b, dtype=dt) else: # we know the inputs are integers, and we are deliberately casting # signed to unsigned return np.subtract(a, b, casting='unsafe', dtype=dt)
Example #9
Source File: histograms.py From lambda-packs with MIT License | 6 votes |
def _unsigned_subtract(a, b): """ Subtract two values where a >= b, and produce an unsigned result This is needed when finding the difference between the upper and lower bound of an int16 histogram """ # coerce to a single type signed_to_unsigned = { np.byte: np.ubyte, np.short: np.ushort, np.intc: np.uintc, np.int_: np.uint, np.longlong: np.ulonglong } dt = np.result_type(a, b) try: dt = signed_to_unsigned[dt.type] except KeyError: return np.subtract(a, b, dtype=dt) else: # we know the inputs are integers, and we are deliberately casting # signed to unsigned return np.subtract(a, b, casting='unsafe', dtype=dt)
Example #10
Source File: incore.py From pyscf with Apache License 2.0 | 6 votes |
def _conc_mos(moi, moj, compact=False): if numpy.result_type(moi, moj) != numpy.double: compact = False nmoi = moi.shape[1] nmoj = moj.shape[1] if compact and iden_coeffs(moi, moj): ijmosym = 's2' nij_pair = nmoi * (nmoi+1) // 2 moij = numpy.asarray(moi, order='F') ijshape = (0, nmoi, 0, nmoi) else: ijmosym = 's1' nij_pair = nmoi * nmoj moij = numpy.asarray(numpy.hstack((moi,moj)), order='F') ijshape = (0, nmoi, nmoi, nmoi+nmoj) return ijmosym, nij_pair, moij, ijshape
Example #11
Source File: pywannier90.py From pyscf with Apache License 2.0 | 6 votes |
def cartesian_prod(arrays, out=None, order = 'C'): ''' This function is similar to lib.cartesian_prod of PySCF, except the output can be in Fortran or in C order ''' arrays = [np.asarray(x) for x in arrays] dtype = np.result_type(*arrays) nd = len(arrays) dims = [nd] + [len(x) for x in arrays] if out is None: out = np.empty(dims, dtype) else: out = np.ndarray(dims, dtype, buffer=out) tout = out.reshape(dims) shape = [-1] + [1] * nd for i, arr in enumerate(arrays): tout[i] = arr.reshape(shape[:nd-i]) return tout.reshape((nd,-1),order=order).T
Example #12
Source File: sparse.py From recruit with Apache License 2.0 | 6 votes |
def __array__(self, dtype=None, copy=True): fill_value = self.fill_value if self.sp_index.ngaps == 0: # Compat for na dtype and int values. return self.sp_values if dtype is None: # Can NumPy represent this type? # If not, `np.result_type` will raise. We catch that # and return object. if is_datetime64_any_dtype(self.sp_values.dtype): # However, we *do* special-case the common case of # a datetime64 with pandas NaT. if fill_value is NaT: # Can't put pd.NaT in a datetime64[ns] fill_value = np.datetime64('NaT') try: dtype = np.result_type(self.sp_values.dtype, type(fill_value)) except TypeError: dtype = object out = np.full(self.shape, fill_value, dtype=dtype) out[self.sp_index.to_int_index().indices] = self.sp_values return out
Example #13
Source File: align.py From vnpy_crypto with MIT License | 6 votes |
def _align(terms): """Align a set of terms""" try: # flatten the parse tree (a nested list, really) terms = list(com.flatten(terms)) except TypeError: # can't iterate so it must just be a constant or single variable if isinstance(terms.value, pd.core.generic.NDFrame): typ = type(terms.value) return typ, _zip_axes_from_type(typ, terms.value.axes) return np.result_type(terms.type), None # if all resolved variables are numeric scalars if all(term.is_scalar for term in terms): return _result_type_many(*(term.value for term in terms)).type, None # perform the main alignment typ, axes = _align_core(terms) return typ, axes
Example #14
Source File: matrix.py From ektelo with Apache License 2.0 | 5 votes |
def __init__(self, matrices): self.matrices = matrices self.shape = tuple(np.prod([Q.shape for Q in matrices], axis=0)) self.dtype = np.result_type(*[Q.dtype for Q in matrices])
Example #15
Source File: test_numeric.py From vnpy_crypto with MIT License | 5 votes |
def test_result_type(self): self.check_promotion_cases(np.result_type) assert_(np.result_type(None) == np.dtype(None))
Example #16
Source File: common.py From vnpy_crypto with MIT License | 5 votes |
def _result_type_many(*arrays_and_dtypes): """ wrapper around numpy.result_type which overcomes the NPY_MAXARGS (32) argument limit """ try: return np.result_type(*arrays_and_dtypes) except ValueError: # we have > NPY_MAXARGS terms in our expression return reduce(np.result_type, arrays_and_dtypes)
Example #17
Source File: lax_numpy_test.py From trax with Apache License 2.0 | 5 votes |
def testClipStaticBounds(self, shape, dtype, a_min, a_max, rng_factory): rng = rng_factory() onp_fun = lambda x: onp.clip(x, a_min=a_min, a_max=a_max) lnp_fun = lambda x: lnp.clip(x, a_min=a_min, a_max=a_max) args_maker = lambda: [rng(shape, dtype)] tol_spec = {onp.float64: 2e-7} tol = jtu.tolerance(dtype, tol_spec) is_x32_scalar = (dtype in [onp.int32, onp.float32] and shape in [jtu.NUMPY_SCALAR_SHAPE, ()]) # Turns check_dtypes off if is_x32_scalar is True because there is # a weird promotion inconsistency in numpy: # ``` # print(np.result_type(np.ones([], np.int32), 1)) # print(np.result_type(np.ones([1], np.int32), 1)) # print(np.result_type(np.int32(1), 1)) # print(np.result_type(np.int32, 1)) # print(np.result_type(np.ones([], np.float32), 1)) # print(np.result_type(np.ones([1], np.float32), 1)) # print(np.result_type(np.float32(1), 1)) # print(np.result_type(np.float32, 1)) # ``` # >>> # int64 # int32 # int64 # int32 # float64 # float32 # float64 # float32 self._CheckAgainstNumpy(onp_fun, lnp_fun, args_maker, check_dtypes=not is_x32_scalar, tol=tol) self._CompileAndCheck(lnp_fun, args_maker, check_dtypes=not is_x32_scalar, atol=tol, rtol=tol, check_incomplete_shape=True)
Example #18
Source File: test_function_base.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_weights(self): y = np.arange(10) w = np.arange(10) actual = average(y, weights=w) desired = (np.arange(10) ** 2).sum() * 1. / np.arange(10).sum() assert_almost_equal(actual, desired) y1 = np.array([[1, 2, 3], [4, 5, 6]]) w0 = [1, 2] actual = average(y1, weights=w0, axis=0) desired = np.array([3., 4., 5.]) assert_almost_equal(actual, desired) w1 = [0, 0, 1] actual = average(y1, weights=w1, axis=1) desired = np.array([3., 6.]) assert_almost_equal(actual, desired) # This should raise an error. Can we test for that ? # assert_equal(average(y1, weights=w1), 9./2.) # 2D Case w2 = [[0, 0, 1], [0, 0, 2]] desired = np.array([3., 6.]) assert_array_equal(average(y1, weights=w2, axis=1), desired) assert_equal(average(y1, weights=w2), 5.) y3 = rand(5).astype(np.float32) w3 = rand(5).astype(np.float64) assert_(np.average(y3, weights=w3).dtype == np.result_type(y3, w3))
Example #19
Source File: matrix.py From ektelo with Apache License 2.0 | 5 votes |
def __init__(self, A, B): assert A.shape[1] == B.shape[0] self._A = A self._B = B self.shape = (A.shape[0], B.shape[1]) self.dtype = np.result_type(A.dtype, B.dtype)
Example #20
Source File: fastsparse.py From qiskit-aer with Apache License 2.0 | 5 votes |
def _inequality(self, other, op, op_name, bad_scalar_msg): # Scalar other. if isscalarlike(other): if other == 0 and op_name in ('_le_', '_ge_'): raise NotImplementedError(" >= and <= don't work with 0.") if op(0, other): warn(bad_scalar_msg, SparseEfficiencyWarning) other_arr = np.empty(self.shape, dtype=np.result_type(other)) other_arr.fill(other) other_arr = csr_matrix(other_arr) return self._binopt(other_arr, op_name) else: return self._scalar_binopt(other, op) # Dense other. elif isdense(other): return op(self.todense(), other) # Sparse other. elif isspmatrix(other): # TODO sparse broadcasting if self.shape != other.shape: raise ValueError("inconsistent shapes") if self.format != other.format: other = other.asformat(self.format) if op_name not in ('_ge_', '_le_'): return self._binopt(other, op_name) warn("Comparing sparse matrices using >= and <= is inefficient, " "using <, >, or !=, instead.", SparseEfficiencyWarning) all_true = _all_true(self.shape) res = self._binopt(other, '_gt_' if op_name == '_le_' else '_lt_') return all_true - res else: raise ValueError("Operands could not be compared.")
Example #21
Source File: matrix.py From ektelo with Apache License 2.0 | 5 votes |
def __init__(self, matrices): # all matrices must have same number of rows cols = [Q.shape[1] for Q in matrices] m = matrices[0].shape[0] n = sum(cols) assert all(Q.shape[0] == m for Q in matrices), 'dimension mismatch' self.shape = (m,n) self.matrices = matrices self.dtype = np.result_type(*[Q.dtype for Q in matrices]) self.split = np.cumsum(cols)[:-1]
Example #22
Source File: test_function_base.py From vnpy_crypto with MIT License | 5 votes |
def test_weights(self): y = np.arange(10) w = np.arange(10) actual = average(y, weights=w) desired = (np.arange(10) ** 2).sum() * 1. / np.arange(10).sum() assert_almost_equal(actual, desired) y1 = np.array([[1, 2, 3], [4, 5, 6]]) w0 = [1, 2] actual = average(y1, weights=w0, axis=0) desired = np.array([3., 4., 5.]) assert_almost_equal(actual, desired) w1 = [0, 0, 1] actual = average(y1, weights=w1, axis=1) desired = np.array([3., 6.]) assert_almost_equal(actual, desired) # This should raise an error. Can we test for that ? # assert_equal(average(y1, weights=w1), 9./2.) # 2D Case w2 = [[0, 0, 1], [0, 0, 2]] desired = np.array([3., 6.]) assert_array_equal(average(y1, weights=w2, axis=1), desired) assert_equal(average(y1, weights=w2), 5.) y3 = rand(5).astype(np.float32) w3 = rand(5).astype(np.float64) assert_(np.average(y3, weights=w3).dtype == np.result_type(y3, w3))
Example #23
Source File: test_numeric.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_result_type(self): self.check_promotion_cases(np.result_type) assert_(np.result_type(None) == np.dtype(None))
Example #24
Source File: test_base.py From mars with Apache License 2.0 | 5 votes |
def testResultType(self): x = tensor([2, 3], dtype='i4') y = 3 z = np.array([3, 4], dtype='f4') r = result_type(x, y, z) e = np.result_type(x.dtype, y, z) self.assertEqual(r, e)
Example #25
Source File: test_function_base.py From lambda-packs with MIT License | 5 votes |
def test_weights(self): y = np.arange(10) w = np.arange(10) actual = average(y, weights=w) desired = (np.arange(10) ** 2).sum() * 1. / np.arange(10).sum() assert_almost_equal(actual, desired) y1 = np.array([[1, 2, 3], [4, 5, 6]]) w0 = [1, 2] actual = average(y1, weights=w0, axis=0) desired = np.array([3., 4., 5.]) assert_almost_equal(actual, desired) w1 = [0, 0, 1] actual = average(y1, weights=w1, axis=1) desired = np.array([3., 6.]) assert_almost_equal(actual, desired) # This should raise an error. Can we test for that ? # assert_equal(average(y1, weights=w1), 9./2.) # 2D Case w2 = [[0, 0, 1], [0, 0, 2]] desired = np.array([3., 6.]) assert_array_equal(average(y1, weights=w2, axis=1), desired) assert_equal(average(y1, weights=w2), 5.) y3 = rand(5).astype(np.float32) w3 = rand(5).astype(np.float64) assert_(np.average(y3, weights=w3).dtype == np.result_type(y3, w3))
Example #26
Source File: _upfirdn.py From lambda-packs with MIT License | 5 votes |
def __init__(self, h, x_dtype, up, down): """Helper for resampling""" h = np.asarray(h) if h.ndim != 1 or h.size == 0: raise ValueError('h must be 1D with non-zero length') self._output_type = np.result_type(h.dtype, x_dtype, np.float32) h = np.asarray(h, self._output_type) self._up = int(up) self._down = int(down) if self._up < 1 or self._down < 1: raise ValueError('Both up and down must be >= 1') # This both transposes, and "flips" each phase for filtering self._h_trans_flip = _pad_h(h, self._up) self._h_trans_flip = np.ascontiguousarray(self._h_trans_flip)
Example #27
Source File: compressed.py From lambda-packs with MIT License | 5 votes |
def _inequality(self, other, op, op_name, bad_scalar_msg): # Scalar other. if isscalarlike(other): if 0 == other and op_name in ('_le_', '_ge_'): raise NotImplementedError(" >= and <= don't work with 0.") elif op(0, other): warn(bad_scalar_msg, SparseEfficiencyWarning) other_arr = np.empty(self.shape, dtype=np.result_type(other)) other_arr.fill(other) other_arr = self.__class__(other_arr) return self._binopt(other_arr, op_name) else: return self._scalar_binopt(other, op) # Dense other. elif isdense(other): return op(self.todense(), other) # Sparse other. elif isspmatrix(other): #TODO sparse broadcasting if self.shape != other.shape: raise ValueError("inconsistent shapes") elif self.format != other.format: other = other.asformat(self.format) if op_name not in ('_ge_', '_le_'): return self._binopt(other, op_name) warn("Comparing sparse matrices using >= and <= is inefficient, " "using <, >, or !=, instead.", SparseEfficiencyWarning) all_true = self.__class__(np.ones(self.shape)) res = self._binopt(other, '_gt_' if op_name == '_le_' else '_lt_') return all_true - res else: raise ValueError("Operands could not be compared.")
Example #28
Source File: edfs.py From westpa with MIT License | 5 votes |
def as_array(self): '''Return this EDF as a (N,2) array, where N is the number of unique values passed to the constructor. Numpy type casting rules are applied (so, for instance, integral abcissae are converted to floating-point values).''' result = numpy.empty((len(self.F),2), dtype=numpy.result_type(self.x, self.F)) result[:,0] = self.x result[:,1] = self.F return result
Example #29
Source File: _classification.py From mars with Apache License 2.0 | 5 votes |
def __call__(self, y_true, y_pred): type_true, y_true, y_pred = _check_targets(y_true, y_pred) self._type_true = type_true inputs = [y_true, y_pred, type_true] if isinstance(self._sample_weight, (Base, Entity)): inputs.append(self._sample_weight) dtype = np.dtype(float) if self._normalize else np.result_type(y_true, y_pred) return self.new_tileable(inputs, dtype=dtype, shape=(), order=TensorOrder.C_ORDER)
Example #30
Source File: numint.py From pyscf with Apache License 2.0 | 5 votes |
def _fxc_mat(self, cell, ao_kpts, wv, non0tab, xctype, ao_loc): nkpts = len(ao_kpts) nao = ao_kpts[0].shape[-1] dtype = numpy.result_type(*ao_kpts) mat = numpy.empty((nkpts,nao,nao), dtype=dtype) for k in range(nkpts): mat[k] = _fxc_mat(cell, ao_kpts[k], wv, non0tab, xctype, ao_loc) return mat