本文整理汇总了Python中numpy.find_common_type方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.find_common_type方法的具体用法?Python numpy.find_common_type怎么用?Python numpy.find_common_type使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.find_common_type方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def __init__(self,
sites,
Ws,
bc='finite',
IdL=None,
IdR=None,
max_range=None,
explicit_plus_hc=False):
self.sites = list(sites)
self.chinfo = self.sites[0].leg.chinfo
self.dtype = dtype = np.find_common_type([W.dtype for W in Ws], [])
self._W = [W.astype(dtype, copy=True) for W in Ws]
self.IdL = self._get_Id(IdL, len(sites))
self.IdR = self._get_Id(IdR, len(sites))
self.grouped = 1
self.bc = bc
self.max_range = max_range
self.explicit_plus_hc = explicit_plus_hc
self.test_sanity()
示例2: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def __init__(self, sites, Bs, SVs, bc='finite', form='B', norm=1.):
self.sites = list(sites)
self.chinfo = self.sites[0].leg.chinfo
self.dtype = dtype = np.find_common_type([B.dtype for B in Bs], [])
self.form = self._parse_form(form)
self.bc = bc # one of ``'finite', 'periodic', 'segment'``.
self.norm = norm
self.grouped = 1
# make copies of Bs and SVs
self._B = [B.astype(dtype, copy=True).itranspose(self._B_labels) for B in Bs]
self._S = [None] * (self.L + 1)
for i in range(self.L + 1)[self.nontrivial_bonds]:
self._S[i] = np.array(SVs[i], dtype=np.float)
if self.bc == 'infinite':
self._S[-1] = self._S[0]
elif self.bc == 'finite':
self._S[0] = self._S[-1] = np.ones([1])
self._transfermatrix_keep = 1
self.test_sanity()
示例3: astype
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def astype(self, dtype, copy=True):
"""Return copy with new dtype, upcasting all blocks in ``_data``.
Parameters
----------
dtype : convertible to a np.dtype
The new data type.
If None, deduce the new dtype as common type of ``self._data``.
copy : bool
Whether to make a copy of the blocks even if the type didn't change.
Returns
-------
copy : :class:`Array`
Deep copy of self with new dtype.
"""
cp = self.copy(deep=False) # manual deep copy: don't copy every block twice
cp._qdata = cp._qdata.copy()
if dtype is None:
dtype = np.find_common_type([d.dtype for d in self._data], [])
cp.dtype = dtype = np.dtype(dtype)
if copy or dtype != self.dtype:
cp._data = [d.astype(dtype, copy=copy) for d in self._data]
return cp
示例4: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def __init__(self, *args, **kwargs):
self._structure = kwargs.get('structure', None)
for A in args:
if len(A.shape) != 2 or A.shape[0] != A.shape[1]:
raise ValueError(
'For now, the ProductOperator implementation is '
'limited to the product of multiple square matrices.')
if args:
n = args[0].shape[0]
for A in args:
for d in A.shape:
if d != n:
raise ValueError(
'The square matrices of the ProductOperator '
'must all have the same shape.')
self.shape = (n, n)
self.ndim = len(self.shape)
self.dtype = np.find_common_type([x.dtype for x in args], [])
self._operator_sequence = args
示例5: _nbool_correspond_all
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def _nbool_correspond_all(u, v, w=None):
if u.dtype == v.dtype == bool and w is None:
not_u = ~u
not_v = ~v
nff = (not_u & not_v).sum()
nft = (not_u & v).sum()
ntf = (u & not_v).sum()
ntt = (u & v).sum()
else:
dtype = np.find_common_type([int], [u.dtype, v.dtype])
u = u.astype(dtype)
v = v.astype(dtype)
not_u = 1.0 - u
not_v = 1.0 - v
if w is not None:
not_u = w * not_u
u = w * u
nff = (not_u * not_v).sum()
nft = (not_u * v).sum()
ntf = (u * not_v).sum()
ntt = (u * v).sum()
return (nff, nft, ntf, ntt)
示例6: _nbool_correspond_ft_tf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def _nbool_correspond_ft_tf(u, v, w=None):
if u.dtype == v.dtype == bool and w is None:
not_u = ~u
not_v = ~v
nft = (not_u & v).sum()
ntf = (u & not_v).sum()
else:
dtype = np.find_common_type([int], [u.dtype, v.dtype])
u = u.astype(dtype)
v = v.astype(dtype)
not_u = 1.0 - u
not_v = 1.0 - v
if w is not None:
not_u = w * not_u
u = w * u
nft = (not_u * v).sum()
ntf = (u * not_v).sum()
return (nft, ntf)
示例7: test_where_type
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def test_where_type(self):
# Test the type conservation with where
x = np.arange(4, dtype=np.int32)
y = np.arange(4, dtype=np.float32) * 2.2
test = where(x > 1.5, y, x).dtype
control = np.find_common_type([np.int32, np.float32], [])
assert_equal(test, control)
示例8: test_scalar_loses1
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def test_scalar_loses1(self):
res = np.find_common_type(['f4', 'f4', 'i2'], ['f8'])
assert_(res == 'f4')
示例9: test_scalar_loses2
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def test_scalar_loses2(self):
res = np.find_common_type(['f4', 'f4'], ['i8'])
assert_(res == 'f4')
示例10: test_scalar_wins
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def test_scalar_wins(self):
res = np.find_common_type(['f4', 'f4', 'i2'], ['c8'])
assert_(res == 'c8')
示例11: test_scalar_wins2
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def test_scalar_wins2(self):
res = np.find_common_type(['u4', 'i4', 'i4'], ['f4'])
assert_(res == 'f8')
示例12: test_find_common_type_boolean
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def test_find_common_type_boolean(self):
# Ticket #1695
assert_(np.find_common_type([], ['?', '?']) == '?')
示例13: from_hdf5
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def from_hdf5(cls, hdf5_loader, h5gr, subpath):
"""Load instance from a HDF5 file.
This method reconstructs a class instance from the data saved with :meth:`save_hdf5`.
Parameters
----------
hdf5_loader : :class:`~tenpy.tools.hdf5_io.Hdf5Loader`
Instance of the loading engine.
h5gr : :class:`Group`
HDF5 group which is represent the object to be constructed.
subpath : str
The `name` of `h5gr` with a ``'/'`` in the end.
Returns
-------
obj : cls
Newly generated class instance containing the required data.
"""
obj = cls.__new__(cls) # create class instance, no __init__() call
hdf5_loader.memorize_load(h5gr, obj)
obj.sites = hdf5_loader.load(subpath + "sites")
obj.chinfo = hdf5_loader.load(subpath + "chinfo")
obj._W = hdf5_loader.load(subpath + "tensors")
obj.dtype = np.find_common_type([W.dtype for W in obj._W], [])
obj.IdL = hdf5_loader.load(subpath + "index_identity_left")
obj.IdR = hdf5_loader.load(subpath + "index_identity_right")
obj.grouped = hdf5_loader.get_attr(h5gr, "grouped")
obj.bc = hdf5_loader.load(subpath + "boundary_condition")
obj.max_range = hdf5_loader.load(subpath + "max_range")
obj.explicit_plus_hc = h5gr.attrs.get("explicit_plus_hc", False)
obj.test_sanity()
return obj
示例14: from_hdf5
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def from_hdf5(cls, hdf5_loader, h5gr, subpath):
"""Load instance from a HDF5 file.
This method reconstructs a class instance from the data saved with :meth:`save_hdf5`.
Parameters
----------
hdf5_loader : :class:`~tenpy.tools.hdf5_io.Hdf5Loader`
Instance of the loading engine.
h5gr : :class:`Group`
HDF5 group which is represent the object to be constructed.
subpath : str
The `name` of `h5gr` with a ``'/'`` in the end.
Returns
-------
obj : cls
Newly generated class instance containing the required data.
"""
obj = cls.__new__(cls) # create class instance, no __init__() call
hdf5_loader.memorize_load(h5gr, obj)
obj.sites = hdf5_loader.load(subpath + "sites")
obj._B = hdf5_loader.load(subpath + "tensors")
obj._S = hdf5_loader.load(subpath + "singular_values")
obj.bc = hdf5_loader.load(subpath + "boundary_condition")
form = hdf5_loader.load(subpath + "canonical_form")
obj.form = [tuple(f) for f in form]
obj.norm = hdf5_loader.get_attr(h5gr, "norm")
obj.grouped = hdf5_loader.get_attr(h5gr, "grouped")
obj._transfermatrix_keep = hdf5_loader.get_attr(h5gr, "transfermatrix_keep")
obj.chinfo = hdf5_loader.load(subpath + "chinfo")
obj.dtype = np.find_common_type([B.dtype for B in obj._B], [])
obj.test_sanity()
return obj
示例15: set_svd_theta
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import find_common_type [as 别名]
def set_svd_theta(self, i, theta, trunc_par=None, update_norm=False):
"""SVD a two-site wave function `theta` and save it in `self`.
Parameters
----------
i : int
`theta` is the wave function on sites `i`, `i`+1.
theta : :class:`~tenpy.linalg.np_conserved.Array`
The two-site wave function with labels combined into ``"(vL.p0)", "(p1.vR)"``,
ready for svd.
trunc_par : None | dict
Parameters for truncation, see :cfg:config:`truncation`.
If ``None``, no truncation is done.
update_norm : bool
If ``True``, multiply the norm of `theta` into :attr:`norm`.
"""
i0 = self._to_valid_index(i)
i1 = self._to_valid_index(i0 + 1)
self.dtype = np.find_common_type([self.dtype, theta.dtype], [])
qtotal_LR = [self._B[i0].qtotal, None]
if trunc_par is None:
U, S, VH = npc.svd(theta, qtotal_LR=qtotal_LR, inner_labels=['vR', 'vL'])
renorm = np.linalg.norm(S)
S /= renorm
err = None
if update_norm:
self.norm *= renorm
else:
U, S, VH, err, renorm = svd_theta(theta, trunc_par, qtotal_LR)
if update_norm:
self.norm *= renorm
U = U.split_legs().ireplace_label('p0', 'p')
VH = VH.split_legs().ireplace_label('p1', 'p')
self._B[i0] = U.itranspose(self._B_labels)
self.form[i0] = self._valid_forms['A']
self._B[i1] = VH.itranspose(self._B_labels)
self.form[i1] = self._valid_forms['B']
self.set_SR(i, S)
return err