本文整理匯總了Python中numpy.block方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.block方法的具體用法?Python numpy.block怎麽用?Python numpy.block使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.block方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_k_ix
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def get_k_ix(self, item, like):
"""
Retrieves block indexes: row and column.
Args:
item (str): a string of 'mknj' letters;
like (tuple): a 2-tuple with sample pair of k-points;
Returns:
Row and column indexes of a sub-block with conserving momentum.
"""
item_i = numpy.argsort(mknj2i(item))
item_code = ''.join("++--"[i] for i in item_i)
if item_code[0] == item_code[1]:
kc = self.kconserv # ++-- --++
elif item_code[0] == item_code[2]:
kc = self.kconserv.swapaxes(1, 2) # +-+- -+-+
elif item_code[1] == item_code[2]:
kc = self.kconserv.transpose(2, 0, 1) # +--+ -++-
else:
raise RuntimeError("Unknown case: {}".format(item_code))
y = kc[like]
x = kc[0, y[0]]
return x, y
示例2: eri_mknj
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def eri_mknj(self, item, pair_row, pair_column):
"""
Retrieves the merged ERI block using 'mknj' notation with pairs of k-indexes (k1, k1, k2, k2).
Args:
item (str): a 4-character string of 'mknj' letters;
pair_row (Iterable): a k-point pair `k2 = pair_row[k1]` for each k1 (row indexes in the final matrix);
pair_column (Iterable): a k-point pair `k4 = pair_row[k3]` for each k3 (column indexes in the final matrix);
Returns:
The corresponding block of ERI (phys notation).
"""
return super(PhysERI, self).eri_mknj(
item,
pairs_row=enumerate(pair_row),
pairs_column=enumerate(pair_column),
)
示例3: tdhf_primary_form
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def tdhf_primary_form(self, k):
"""
A primary form of TDHF matrixes (full).
Args:
k (tuple, int): momentum transfer: either a pair of k-point indexes specifying the momentum transfer
vector or a single integer with the second index assuming the first index being zero;
Returns:
Output type: "full", and the corresponding matrix.
"""
r1, r2, c1, c2 = get_block_k_ix(self, k)
d1 = self.tdhf_diag(r1)
d2 = self.tdhf_diag(r2)
a = d1 + 2 * self["knmj", r1, c1] - self["knjm", r1, c1]
b = 2 * self["kjmn", r1, c2] - self["kjnm", r1, c2]
a_ = d2 + 2 * self["mjkn", r2, c2] - self["mjnk", r2, c2]
b_ = 2 * self["mnkj", r2, c1] - self["mnjk", r2, c1]
return "full", numpy.block([[a, b], [-b_, -a_]])
示例4: eri_mknj
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def eri_mknj(self, item, pairs_row=None, pairs_column=None):
"""
Retrieves the merged ERI block using 'mknj' notation with all k-indexes.
Args:
item (str): a 4-character string of 'mknj' letters;
pairs_row (Iterable): iterator for pairs of row k-points (first index in the output matrix);
pairs_column (Iterable): iterator for pairs of column k-points (second index in the output matrix);
Returns:
The corresponding block of ERI (phys notation).
"""
if pairs_row is None:
pairs_row = product(range(len(self.model.kpts)), range(len(self.model.kpts)))
if pairs_column is None:
pairs_column = product(range(len(self.model.kpts)), range(len(self.model.kpts)))
# Second index has to support re-iterations
pairs_column = tuple(pairs_column)
result = []
for k1, k2 in pairs_row:
result.append([])
for k3, k4 in pairs_column:
result[-1].append(self.eri_mknj_k(item, (k1, k2, k3, k4)))
r = numpy.block(result)
return r / len(self.model.kpts)
示例5: eri_mknj
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def eri_mknj(self, item, *args):
"""
Retrieves ERI block using 'mknj' notation.
Args:
item (str): a 4-character string of 'mknj' letters;
*args: other arguments passed to `get_block_ov_notation`;
Returns:
The corresponding block of ERI (matrix with paired dimensions).
"""
if len(item) != 4 or not isinstance(item, str) or set(item) != set('mknj'):
raise ValueError("Unknown item: {}".format(repr(item)))
item = mknj2i(item)
n_ov = ''.join('o' if i % 2 == 0 else 'v' for i in item)
args = tuple(
tuple(arg[i] for i in item)
for arg in args
)
result = self.eri_ov(n_ov, *args).transpose(*numpy.argsort(item))
i, j, k, l = result.shape
result = result.reshape((i * j, k * l))
return result
示例6: cascade
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def cascade(T: np.ndarray, U: np.ndarray) -> np.ndarray:
n = int(T.shape[1] / 2)
J = np.linalg.inv(np.eye(n) - np.matmul(U[0:n, 0:n], T[n : 2 * n, n : 2 * n]))
K = np.linalg.inv(np.eye(n) - np.matmul(T[n : 2 * n, n : 2 * n], U[0:n, 0:n]))
S = np.block(
[
[
T[0:n, 0:n]
+ np.matmul(
np.matmul(np.matmul(T[0:n, n : 2 * n], J), U[0:n, 0:n]),
T[n : 2 * n, 0:n],
),
np.matmul(np.matmul(T[0:n, n : 2 * n], J), U[0:n, n : 2 * n]),
],
[
np.matmul(np.matmul(U[n : 2 * n, 0:n], K), T[n : 2 * n, 0:n]),
U[n : 2 * n, n : 2 * n]
+ np.matmul(
np.matmul(np.matmul(U[n : 2 * n, 0:n], K), T[n : 2 * n, n : 2 * n]),
U[0:n, n : 2 * n],
),
],
]
)
return S # type: ignore
示例7: creneau
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def creneau(k0: float, a0: float, pol: float, e1: float, e2: float, a: float, n: int, x0: float) -> tp.Tuple[np.ndarray, np.ndarray]:
nmod = int(n / 2)
alpha = np.diag(a0 + 2 * np.pi * np.arange(-nmod, nmod + 1))
if pol == 0:
M = alpha * alpha - k0 * k0 * marche(e1, e2, a, n, x0)
L, E = np.linalg.eig(M)
L = np.sqrt(-L + 0j)
L = (1 - 2 * (np.imag(L) < -1e-15)) * L
P = np.block([[E], [np.matmul(E, np.diag(L))]])
else:
U = marche(1 / e1, 1 / e2, a, n, x0)
T = np.linalg.inv(U)
M = (
np.matmul(
np.matmul(np.matmul(T, alpha), np.linalg.inv(marche(e1, e2, a, n, x0))),
alpha,
)
- k0 * k0 * T
)
L, E = np.linalg.eig(M)
L = np.sqrt(-L + 0j)
L = (1 - 2 * (np.imag(L) < -1e-15)) * L
P = np.block([[E], [np.matmul(np.matmul(U, E), np.diag(L))]])
return P, L
示例8: test_block_complicated
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def test_block_complicated(self):
# a bit more complicated
one_2d = np.array([[1, 1, 1]])
two_2d = np.array([[2, 2, 2]])
three_2d = np.array([[3, 3, 3, 3, 3, 3]])
four_1d = np.array([4, 4, 4, 4, 4, 4])
five_0d = np.array(5)
six_1d = np.array([6, 6, 6, 6, 6])
zero_2d = np.zeros((2, 6))
expected = np.array([[1, 1, 1, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4],
[5, 6, 6, 6, 6, 6],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
result = block([[one_2d, two_2d],
[three_2d],
[four_1d],
[five_0d, six_1d],
[zero_2d]])
assert_equal(result, expected)
示例9: concatenate_state_matrices
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def concatenate_state_matrices(G):
"""
Takes a State() model as input and returns the A, B, C, D matrices
combined into a full matrix. For static gain models, the feedthrough
matrix D is returned.
Parameters
----------
G : State
Returns
-------
M : ndarray
"""
if not isinstance(G, State):
raise ValueError('concatenate_state_matrices() works on state '
'representations, but I found \"{0}\" object '
'instead.'.format(type(G).__name__))
if G._isgain:
return G.d
return np.block([[G.a, G.b], [G.c, G.d]])
示例10: test_bogoliubov_transform_quadratic_hamiltonian_inverse_is_dagger
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def test_bogoliubov_transform_quadratic_hamiltonian_inverse_is_dagger(
n_qubits, real, particle_conserving, atol):
quad_ham = random_quadratic_hamiltonian(
n_qubits,
real=real,
conserves_particle_number=particle_conserving,
seed=46533)
_, transformation_matrix, _ = quad_ham.diagonalizing_bogoliubov_transform()
qubits = cirq.LineQubit.range(n_qubits)
if transformation_matrix.shape == (n_qubits, n_qubits):
daggered_transformation_matrix = transformation_matrix.T.conj()
else:
left_block = transformation_matrix[:, :n_qubits]
right_block = transformation_matrix[:, n_qubits:]
daggered_transformation_matrix = numpy.block(
[left_block.T.conj(), right_block.T])
circuit1 = cirq.Circuit(
cirq.inverse(bogoliubov_transform(qubits, transformation_matrix)))
circuit2 = cirq.Circuit(
bogoliubov_transform(qubits, daggered_transformation_matrix))
cirq.testing.assert_allclose_up_to_global_phase(
circuit1.unitary(),
circuit2.unitary(),
atol=atol)
示例11: _gaussian_basis_change
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def _gaussian_basis_change(qubits: Sequence[cirq.Qid],
transformation_matrix: numpy.ndarray,
initially_occupied_orbitals: Optional[Sequence[int]]
) -> cirq.OP_TREE:
n_qubits = len(qubits)
# Rearrange the transformation matrix because the OpenFermion routine
# expects it to describe annihilation operators rather than creation
# operators
left_block = transformation_matrix[:, :n_qubits]
right_block = transformation_matrix[:, n_qubits:]
transformation_matrix = numpy.block(
[numpy.conjugate(right_block), numpy.conjugate(left_block)])
decomposition, left_decomposition, _, left_diagonal = (
fermionic_gaussian_decomposition(transformation_matrix))
if (initially_occupied_orbitals is not None and
len(initially_occupied_orbitals) == 0):
# Starting with the vacuum state yields additional symmetry
circuit_description = list(reversed(decomposition))
else:
if initially_occupied_orbitals is None:
# The initial state is not a computational basis state so the
# phases left on the diagonal in the Givens decomposition matter
yield (cirq.rz(rads=
numpy.angle(left_diagonal[j])).on(qubits[j])
for j in range(n_qubits))
circuit_description = list(reversed(decomposition + left_decomposition))
yield _ops_from_givens_rotations_circuit_description(
qubits, circuit_description)
示例12: eri_ov
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def eri_ov(self, item):
result = []
k = numpy.arange(self.nk)
for k1 in k:
result.append([])
for k2 in k:
result[-1].append([])
for k3 in k:
result[-1][-1].append([])
for k4 in k:
result[-1][-1][-1].append(self.eri.eri_ov(item, (k1, k2, k3, k4)))
r = numpy.block(result)
return r / len(k)
示例13: tdhf_diag
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def tdhf_diag(self, block):
"""
Retrieves the merged diagonal block only with specific pairs of k-indexes (k, block[k]).
Args:
block (Iterable): a k-point pair `k2 = pair[k1]` for each k1;
Returns:
The diagonal block.
"""
return super(PhysERI, self).tdhf_diag(pairs=enumerate(block))
示例14: tdhf_primary_form
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def tdhf_primary_form(self, k):
"""
A primary form of TD matrices (full).
Args:
k (tuple, int): momentum transfer: either a pair of k-point indexes specifying the momentum transfer
vector or a single integer with the second index assuming the first index being zero;
Returns:
Output type: "full", and the corresponding matrix.
"""
r1, r2, c1, c2 = krhf_slow.get_block_k_ix(self, k)
(a, _), (_, b), (_, b_star), (a_star, _) = self.proxy_response_ov_batch((r1, r1, r2, r2), (c1, c2, c1, c2))
return "full", numpy.block([[a, b], [-b_star.conj(), -a_star.conj()]])
示例15: tdhf_diag_k
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import block [as 別名]
def tdhf_diag_k(self, k1, k2):
"""
Retrieves the diagonal block.
Args:
k1 (int): first k-index (row);
k2 (int): second k-index (column);
Returns:
The diagonal block.
"""
# Everything is already implemented in molecular code
return super(PhysERI, self).tdhf_diag(k1, k2)