本文整理汇总了Python中numpy.result_type方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.result_type方法的具体用法?Python numpy.result_type怎么用?Python numpy.result_type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.result_type方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cartesian_prod
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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
示例2: _conc_mos
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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
示例3: _unsigned_subtract
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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)
示例4: __array__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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
示例5: _unsigned_subtract
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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)
示例6: _align
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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
示例7: _filter_special_cases
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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
示例8: _align
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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
示例9: _promote_like_lnp
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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
示例10: testBitwiseOp
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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)
示例11: testDot
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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)
示例12: testQuantile
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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)
示例13: eval_mat
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
def eval_mat(self, cell, ao_kpts, weight, rho, vxc,
non0tab=None, xctype='LDA', spin=0, verbose=None):
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] = eval_mat(cell, ao_kpts[k], weight, rho, vxc,
non0tab, xctype, spin, verbose)
return mat
示例14: _fxc_mat
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
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
示例15: _rotate_mo
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import result_type [as 别名]
def _rotate_mo(mo_coeff, mo_occ, dx):
mo = []
p1 = 0
dtype = numpy.result_type(dx, *mo_coeff)
for k, occ in enumerate(mo_occ):
nmo = occ.size
no = numpy.count_nonzero(occ > 0)
nv = nmo - no
p0, p1 = p1, p1 + nv * no
dr = numpy.zeros((nmo,nmo), dtype=dtype)
dr[no:,:no] = dx[p0:p1].reshape(nv,no)
dr[:no,no:] =-dx[p0:p1].reshape(nv,no).conj().T
mo.append(numpy.dot(mo_coeff[k], scipy.linalg.expm(dr)))
return mo