本文整理汇总了Python中numpy.tril_indices方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.tril_indices方法的具体用法?Python numpy.tril_indices怎么用?Python numpy.tril_indices使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.tril_indices方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: vector_to_amplitudes_ea
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def vector_to_amplitudes_ea(vector, kshift, nkpts, nmo, nocc, kconserv):
nvir = nmo - nocc
r1 = vector[:nvir].copy()
r2_tril = vector[nvir:].copy().reshape(nocc*nkpts*nvir*(nkpts*nvir-1)//2)
r2 = np.zeros((nkpts,nkpts,nocc,nvir,nvir), dtype=vector.dtype)
index = 0
for kj, ka in itertools.product(range(nkpts), repeat=2):
kb = kconserv[kshift,ka,kj]
if ka < kb:
idx, idy = np.tril_indices(nvir, 0)
else:
idx, idy = np.tril_indices(nvir, -1)
tmp = r2_tril[index:index + nocc*len(idy)].reshape(-1,nocc)
r2[kj,ka,:,idx,idy] = tmp
r2[kj,kb,:,idy,idx] = -tmp
index = index + nocc*len(idy)
return [r1,r2]
示例2: reorder4irrep
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def reorder4irrep(eri, norb, link_index, orbsym, offdiag=0):
if orbsym is None:
return eri, link_index, numpy.array(norb, dtype=numpy.int32)
orbsym = numpy.asarray(orbsym)
# map irrep IDs of Dooh or Coov to D2h, C2v
# see symm.basis.linearmole_symm_descent
orbsym = orbsym % 10
# irrep of (ij| pair
trilirrep = (orbsym[:,None]^orbsym)[numpy.tril_indices(norb, offdiag)]
# and the number of occurence for each irrep
dimirrep = numpy.array(numpy.bincount(trilirrep), dtype=numpy.int32)
# we sort the irreps of (ij| pair, to group the pairs which have same irreps
# "order" is irrep-id-sorted index. The (ij| paired is ordered that the
# pair-id given by order[0] comes first in the sorted pair
# "rank" is a sorted "order". Given nth (ij| pair, it returns the place(rank)
# of the sorted pair
order = numpy.asarray(trilirrep.argsort(), dtype=numpy.int32)
rank = numpy.asarray(order.argsort(), dtype=numpy.int32)
eri = lib.take_2d(eri, order, order)
link_index_irrep = link_index.copy()
link_index_irrep[:,:,0] = rank[link_index[:,:,0]]
return numpy.asarray(eri, order='C'), link_index_irrep, dimirrep
示例3: reorder_eri
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def reorder_eri(eri, norb, orbsym):
if orbsym is None:
return [eri], numpy.arange(norb), numpy.zeros(norb,dtype=numpy.int32)
# map irrep IDs of Dooh or Coov to D2h, C2v
# see symm.basis.linearmole_symm_descent
orbsym = numpy.asarray(orbsym) % 10
# irrep of (ij| pair
trilirrep = (orbsym[:,None]^orbsym)[numpy.tril_indices(norb)]
# and the number of occurence for each irrep
dimirrep = numpy.asarray(numpy.bincount(trilirrep), dtype=numpy.int32)
# we sort the irreps of (ij| pair, to group the pairs which have same irreps
# "order" is irrep-id-sorted index. The (ij| paired is ordered that the
# pair-id given by order[0] comes first in the sorted pair
# "rank" is a sorted "order". Given nth (ij| pair, it returns the place(rank)
# of the sorted pair
old_eri_irrep = numpy.asarray(trilirrep, dtype=numpy.int32)
rank_in_irrep = numpy.empty_like(old_eri_irrep)
p0 = 0
eri_irs = [numpy.zeros((0,0))] * TOTIRREPS
for ir, nnorb in enumerate(dimirrep):
idx = numpy.asarray(numpy.where(trilirrep == ir)[0], dtype=numpy.int32)
rank_in_irrep[idx] = numpy.arange(nnorb, dtype=numpy.int32)
eri_irs[ir] = lib.take_2d(eri, idx, idx)
p0 += nnorb
return eri_irs, rank_in_irrep, old_eri_irrep
示例4: amplitudes_to_vector_eomsf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def amplitudes_to_vector_eomsf(t1, t2, out=None):
t1ab, t1ba = t1
t2baaa, t2aaba, t2abbb, t2bbab = t2
nocca, nvirb = t1ab.shape
noccb, nvira = t1ba.shape
otrila = np.tril_indices(nocca, k=-1)
otrilb = np.tril_indices(noccb, k=-1)
vtrila = np.tril_indices(nvira, k=-1)
vtrilb = np.tril_indices(nvirb, k=-1)
baaa = np.take(t2baaa.reshape(noccb*nocca,nvira*nvira),
vtrila[0]*nvira+vtrila[1], axis=1)
abbb = np.take(t2abbb.reshape(nocca*noccb,nvirb*nvirb),
vtrilb[0]*nvirb+vtrilb[1], axis=1)
vector = np.hstack((t1ab.ravel(), t1ba.ravel(),
baaa.ravel(), t2aaba[otrila].ravel(),
abbb.ravel(), t2bbab[otrilb].ravel()))
return vector
示例5: my_ao2mo
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def my_ao2mo(mo):
nao, nmo = mo.shape
orbspin = mo.orbspin
# eris = ao2mo.kernel(mygcc._scf._eri, mo_a + mo_b)
# sym_forbid = (orbspin[:,None] != orbspin)[np.tril_indices(nmo)]
# eris[sym_forbid,:] = 0
# eris[:,sym_forbid] = 0
# eris = ao2mo.restore(1, eris, nao)
# return eris
eris =(np.random.random((nmo,nmo,nmo,nmo)) +
np.random.random((nmo,nmo,nmo,nmo)) * 1j)
eris = eris + np.cos(eris)*1j
eris = eris + eris.transpose(1, 0, 3, 2)
eris = eris + eris.conj().transpose(2, 3, 0, 1)
eris[orbspin[:,None] != orbspin] = 0
eris[:,:,orbspin[:,None] != orbspin] = 0
return eris
示例6: vector_to_amplitudes_eomsf
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def vector_to_amplitudes_eomsf(vector, nmo, nocc):
nvir = nmo - nocc
t1 = vector[:nocc*nvir].reshape(nocc,nvir).copy()
pvec = vector[t1.size:]
nbaaa = nocc*nocc*nvir*(nvir-1)//2
naaba = nocc*(nocc-1)//2*nvir*nvir
t2baaa = np.zeros((nocc*nocc,nvir*nvir), dtype=vector.dtype)
t2aaba = np.zeros((nocc*nocc,nvir*nvir), dtype=vector.dtype)
otril = np.tril_indices(nocc, k=-1)
vtril = np.tril_indices(nvir, k=-1)
v = pvec[:nbaaa].reshape(nocc*nocc,nvir*(nvir-1)//2)
t2baaa[:,vtril[0]*nvir+vtril[1]] = v
t2baaa[:,vtril[1]*nvir+vtril[0]] = -v
v = pvec[nbaaa:nbaaa+naaba].reshape(-1,nvir*nvir)
t2aaba[otril[0]*nocc+otril[1]] = v
t2aaba[otril[1]*nocc+otril[0]] = -v
t2baaa = t2baaa.reshape(nocc,nocc,nvir,nvir)
t2aaba = t2aaba.reshape(nocc,nocc,nvir,nvir)
return t1, (t2baaa, t2aaba)
示例7: update_l1l2
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def update_l1l2(mf, t1, t2, l1, l2, orbspin):
mol = mf.mol
nao,nmo = mf.mo_coeff[0].shape
nelec = mol.nelectron
nso = nmo * 2
hcore = mf.get_hcore()
mo = np.zeros((nao,nso))
mo[:,orbspin==0] = mf.mo_coeff[0]
mo[:,orbspin==1] = mf.mo_coeff[1]
h1e = reduce(np.dot, (mo.T, hcore, mo))
h1e[orbspin[:,None]!=orbspin] = 0
int2e = ao2mo.full(mf._eri, mo)
sym_forbid = (orbspin[:,None] != orbspin)[np.tril_indices(nso)]
int2e[sym_forbid] = 0
int2e[:,sym_forbid] = 0
int2e = ao2mo.restore(1, int2e, nso)
int2e = int2e.transpose(0,2,1,3)
int2e = int2e - int2e.transpose(0,1,3,2)
mycc = ccsd(nso,nelec,h1e,int2e,h1e_is_fock=False)
l1, l2 = update_l1l2_sub(mycc, t1, t2, l1, l2)
return l1, l2
示例8: unpack_eri_2s
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def unpack_eri_2s(eri, norb):
n_oo = norb * (norb + 1) // 2
ind_oo = np.tril_indices(norb)
eri_ = None
if len(eri.shape) == 2:
if (eri.shape[0] != n_oo or eri.shape[1] != n_oo):
raise TypeError("ERI dimensions don't match")
temp = np.zeros((n_oo, norb, norb))
temp[:, ind_oo[0], ind_oo[1]] = eri
temp[:, ind_oo[1], ind_oo[0]] = eri
eri_ = np.zeros((norb, norb, norb, norb))
eri_[ind_oo[0], ind_oo[1]] = temp
eri_[ind_oo[1], ind_oo[0]] = temp
else:
raise RuntimeError("ERI does not have a correct dimension")
return eri_
示例9: unpack_eri_2
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def unpack_eri_2(eri, norb1, norb2):
n_oo1 = norb1 * (norb1 + 1) // 2
ind_oo1 = np.tril_indices(norb1)
n_oo2 = norb2 * (norb2 + 1) // 2
ind_oo2 = np.tril_indices(norb2)
eri_ = None
if len(eri.shape) == 2:
if (eri.shape[0] != n_oo1 or eri.shape[1] != n_oo2):
raise TypeError("ERI dimensions don't match")
temp = np.zeros((n_oo1, norb2, norb2))
temp[:, ind_oo2[0], ind_oo2[1]] = eri
temp[:, ind_oo2[1], ind_oo2[0]] = eri
eri_ = np.zeros((norb1, norb1, norb2, norb2))
eri_[ind_oo1[0], ind_oo1[1]] = temp
eri_[ind_oo1[1], ind_oo1[0]] = temp
else:
raise RuntimeError("ERI does not have a correct dimension")
return eri_
示例10: test_fill_k
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def test_fill_k(self):
fill = 'PBCnr3c_fill_ks2'
out = run3c(fill, kband)
self.assertAlmostEqual(finger(out), 6.6227481557912071-0.80306690266835279j, 9)
out0 = run3c('PBCnr3c_fill_kks2', kband)
idx = numpy.tril_indices(cell.nao_nr())
self.assertTrue(numpy.allclose(out0[0][idx], out[0]))
self.assertTrue(numpy.allclose(out0[2][idx], out[1]))
fill = 'PBCnr3c_fill_ks1'
out = run3c(fill, kband)
self.assertAlmostEqual(finger(out), -14.883220617173009-3.9101277614911112j, 9)
self.assertTrue(numpy.allclose(out0[0], out[0]))
self.assertTrue(numpy.allclose(out0[2], out[1]))
fill = 'PBCnr3c_fill_ks2'
out = run3c(fill, numpy.zeros((1,3)))
self.assertAlmostEqual(finger(out), -1.3258082818877739, 9)
示例11: amplitudes_to_cisdvec
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def amplitudes_to_cisdvec(c0, c1, c2):
c1a, c1b = c1
c2aa, c2ab, c2bb = c2
nocca, nvira = c1a.shape
noccb, nvirb = c1b.shape
def trilidx(n):
idx = numpy.tril_indices(n, -1)
return idx[0] * n + idx[1]
ooidxa = trilidx(nocca)
vvidxa = trilidx(nvira)
ooidxb = trilidx(noccb)
vvidxb = trilidx(nvirb)
size = (1, nocca*nvira, noccb*nvirb, nocca*noccb*nvira*nvirb,
len(ooidxa)*len(vvidxa), len(ooidxb)*len(vvidxb))
loc = numpy.cumsum(size)
civec = numpy.empty(loc[-1], dtype=c2ab.dtype)
civec[0] = c0
civec[loc[0]:loc[1]] = c1a.ravel()
civec[loc[1]:loc[2]] = c1b.ravel()
civec[loc[2]:loc[3]] = c2ab.ravel()
lib.take_2d(c2aa.reshape(nocca**2,nvira**2), ooidxa, vvidxa, out=civec[loc[3]:loc[4]])
lib.take_2d(c2bb.reshape(noccb**2,nvirb**2), ooidxb, vvidxb, out=civec[loc[4]:loc[5]])
return civec
示例12: _exact_paaa
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def _exact_paaa(self, mo, u, out=None):
nmo = mo.shape[1]
ncore = self.ncore
ncas = self.ncas
nocc = ncore + ncas
mo1 = numpy.dot(mo, u)
mo1_cas = mo1[:,ncore:nocc]
mos = (mo1_cas, mo1_cas, mo1, mo1_cas)
if self._scf._eri is None:
aapa = ao2mo.general(self.mol, mos)
else:
aapa = ao2mo.general(self._scf._eri, mos)
paaa = numpy.empty((nmo*ncas,ncas*ncas))
buf = numpy.empty((ncas,ncas,nmo*ncas))
for ij, (i, j) in enumerate(zip(*numpy.tril_indices(ncas))):
buf[i,j] = buf[j,i] = aapa[ij]
paaa = lib.transpose(buf.reshape(ncas*ncas,-1), out=out)
return paaa.reshape(nmo,ncas,ncas,ncas)
示例13: test_lower_triangular
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def test_lower_triangular(self, index_class):
# make an index for each dataframe with a new index name
index_a = pd.Index(self.a.index, name='index')
df_a = pd.DataFrame(self.a, index=index_a)
pairs = index_class.index(df_a)
# expected
levels = [df_a.index.values, df_a.index.values]
codes = np.tril_indices(len(df_a.index), k=-1)
full_pairs = pd.MultiIndex(levels=levels,
codes=codes,
verify_integrity=False)
# all pairs are in the lower triangle of the matrix.
assert len(pairs.difference(full_pairs)) == 0
示例14: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def __init__(self, features, using_cache=False, identity_init=True, eps=1e-3):
super().__init__(features, using_cache)
self.eps = eps
self.lower_indices = np.tril_indices(features, k=-1)
self.upper_indices = np.triu_indices(features, k=1)
self.diag_indices = np.diag_indices(features)
n_triangular_entries = ((features - 1) * features) // 2
self.lower_entries = nn.Parameter(torch.zeros(n_triangular_entries))
self.upper_entries = nn.Parameter(torch.zeros(n_triangular_entries))
self.unconstrained_upper_diag = nn.Parameter(torch.zeros(features))
self._initialize(identity_init)
示例15: _initialize_error_cov_unstructured
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import tril_indices [as 别名]
def _initialize_error_cov_unstructured(self):
# Initialize the parameters
k_endog = self.k_endog
self.parameters['error_cov'] = int(k_endog * (k_endog + 1) / 2)
# Setup fixed components of state space matrices
# Setup indices of state space matrices
self._idx_lower_error_cov = np.tril_indices(self.k_endog)
if self.error_order > 0:
start = self.k_factors
end = self.k_factors + self.k_endog
self._idx_error_cov = (
np.s_['state_cov', start:end, start:end])
else:
self._idx_error_cov = np.s_['obs_cov', :, :]