本文整理汇总了Python中scipy.linalg.block_diag方法的典型用法代码示例。如果您正苦于以下问题:Python linalg.block_diag方法的具体用法?Python linalg.block_diag怎么用?Python linalg.block_diag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.linalg
的用法示例。
在下文中一共展示了linalg.block_diag方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mtx_fri2visi_ri_multiband
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def mtx_fri2visi_ri_multiband(M, p_mic_x_all, p_mic_y_all, D1, D2, aslist=False):
"""
build the matrix that maps the Fourier series to the visibility in terms of
REAL-VALUED entries only. (matrix size double)
:param M: the Fourier series expansion is limited from -M to M
:param p_mic_x_all: a matrix that contains microphones x coordinates
:param p_mic_y_all: a matrix that contains microphones y coordinates
:param D1: expansion matrix for the real-part
:param D2: expansion matrix for the imaginary-part
:return:
"""
num_bands = p_mic_x_all.shape[1]
if aslist:
return [mtx_fri2visi_ri(M, p_mic_x_all[:, band_count],
p_mic_y_all[:, band_count], D1, D2)
for band_count in range(num_bands)]
else:
return linalg.block_diag(*[mtx_fri2visi_ri(M, p_mic_x_all[:, band_count],
p_mic_y_all[:, band_count], D1, D2)
for band_count in range(num_bands)])
示例2: output_shrink
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def output_shrink(K, L):
"""
shrink the convolution output to half the size.
used when both the annihilating filter and the uniform samples of sinusoids satisfy
Hermitian symmetric.
:param K: the annihilating filter size: K + 1
:param L: length of the (complex-valued) b vector
:return:
"""
out_len = L - K
if out_len % 2 == 0:
half_out_len = np.int(out_len / 2.)
mtx_r = np.hstack((np.eye(half_out_len),
np.zeros((half_out_len, half_out_len))))
mtx_i = mtx_r
else:
half_out_len = np.int((out_len + 1) / 2.)
mtx_r = np.hstack((np.eye(half_out_len),
np.zeros((half_out_len, half_out_len - 1))))
mtx_i = np.hstack((np.eye(half_out_len - 1),
np.zeros((half_out_len - 1, half_out_len))))
return linalg.block_diag(mtx_r, mtx_i)
示例3: test_matrix_static_gain
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def test_matrix_static_gain(self):
"""Regression: can we create matrix static gains?"""
d1 = np.matrix([[1, 2, 3], [4, 5, 6]])
d2 = np.matrix([[7, 8], [9, 10], [11, 12]])
g1 = StateSpace([], [], [], d1)
# _remove_useless_states was making A = [[0]]
self.assertEqual((0, 0), g1.A.shape)
g2 = StateSpace([], [], [], d2)
g3 = StateSpace([], [], [], d2.T)
h1 = g1 * g2
np.testing.assert_array_equal(d1 * d2, h1.D)
h2 = g1 + g3
np.testing.assert_array_equal(d1 + d2.T, h2.D)
h3 = g1.feedback(g2)
np.testing.assert_array_almost_equal(
solve(np.eye(2) + d1 * d2, d1), h3.D)
h4 = g1.append(g2)
np.testing.assert_array_equal(block_diag(d1, d2), h4.D)
示例4: test_matrix_static_gain
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def test_matrix_static_gain(self):
"""Regression: can we create matrix static gains?"""
d1 = np.array([[1, 2, 3], [4, 5, 6]])
d2 = np.array([[7, 8], [9, 10], [11, 12]])
g1 = StateSpace([], [], [], d1)
# _remove_useless_states was making A = [[0]]
self.assertEqual((0, 0), g1.A.shape)
g2 = StateSpace([], [], [], d2)
g3 = StateSpace([], [], [], d2.T)
h1 = g1 * g2
np.testing.assert_array_equal(np.dot(d1, d2), h1.D)
h2 = g1 + g3
np.testing.assert_array_equal(d1 + d2.T, h2.D)
h3 = g1.feedback(g2)
np.testing.assert_array_almost_equal(
solve(np.eye(2) + np.dot(d1, d2), d1), h3.D)
h4 = g1.append(g2)
np.testing.assert_array_equal(block_diag(d1, d2), h4.D)
示例5: test_rotated_squeezed
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def test_rotated_squeezed(self, setup_eng, hbar, tol):
"""Testing decomposed rotated squeezed state"""
eng, prog = setup_eng(3)
r = 0.1
phi = 0.2312
v1 = (hbar / 2) * np.diag([np.exp(-r), np.exp(r)])
A = changebasis(3)
cov = A.T @ block_diag(*[rot(phi) @ v1 @ rot(phi).T] * 3) @ A
with prog.context as q:
ops.Gaussian(cov) | q
state = eng.run(prog).state
assert np.allclose(state.cov(), cov, atol=tol)
assert len(eng.run_progs[-1]) == 3
示例6: constr
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def constr(self):
def fun(x):
x_coord, y_coord, z_coord = self._get_cordinates(x)
return x_coord**2 + y_coord**2 + z_coord**2 - 1
def jac(x):
x_coord, y_coord, z_coord = self._get_cordinates(x)
Jx = 2 * np.diag(x_coord)
Jy = 2 * np.diag(y_coord)
Jz = 2 * np.diag(z_coord)
return csc_matrix(np.hstack((Jx, Jy, Jz)))
def hess(x, v):
D = 2 * np.diag(v)
return block_diag(D, D, D)
return NonlinearConstraint(fun, ("less",), jac, hess)
示例7: constr
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def constr(self):
def fun(x):
x_coord, y_coord, z_coord = self._get_cordinates(x)
return x_coord**2 + y_coord**2 + z_coord**2 - 1
if self.constr_jac is None:
def jac(x):
x_coord, y_coord, z_coord = self._get_cordinates(x)
Jx = 2 * np.diag(x_coord)
Jy = 2 * np.diag(y_coord)
Jz = 2 * np.diag(z_coord)
return csc_matrix(np.hstack((Jx, Jy, Jz)))
else:
jac = self.constr_jac
if self.constr_hess is None:
def hess(x, v):
D = 2 * np.diag(v)
return block_diag(D, D, D)
else:
hess = self.constr_hess
return NonlinearConstraint(fun, -np.inf, 0, jac, hess)
示例8: SO3_irreps
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def SO3_irreps(g, irreps):
global Jd
# First, compute sinusoids at all required frequencies, i.e.
# cos(n x) for n=0, ..., max(irreps)
# sin(n x) for n=-max(irreps), ..., max(irreps)
# where x ranges over the three parameters of SO(3).
# In theory, it may be faster to evaluate cos(x) once and then use
# Chebyshev polynomials to obtain cos(n*x), but in practice this appears
# to be slower than just evaluating cos(n*x).
dim = np.sum(2 * np.array(irreps) + 1)
T = np.empty((dim, dim, g.shape[1]))
for i in range(g.shape[1]):
T[:, :, i] = block_diag(*[rot_mat(g[0, i], g[1, i], g[2, i], l, Jd[l]) for l in irreps])
return T
示例9: set_wrench
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def set_wrench(self, wrench):
"""
Set contact wrench directly.
Parameters
----------
wrench : array, shape=(6,)
Wrench coordinates given in the contact frame.
Notes
-----
This function switches the contact to "managed" mode, as opposed to the
default "supporting" mode where the wrench distributor finds contact
wrenches by numerical optimization.
"""
if not type(wrench) is ndarray:
wrench = array(wrench)
if not self.is_managed:
self.set_color('b')
self.is_managed = True
self.wrench = dot(block_diag(self.R, self.R), wrench)
示例10: test_decompose
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def test_decompose(self, tol):
"""Test the two mode squeezing symplectic transform decomposes correctly."""
r = 0.543
phi = 0.123
S = symplectic.two_mode_squeezing(r, phi)
# test that S = B^\dagger(pi/4, 0) [S(z) x S(-z)] B(pi/4)
# fmt: off
B = np.array([[1, -1, 0, 0], [1, 1, 0, 0], [0, 0, 1, -1], [0, 0, 1, 1]])/np.sqrt(2)
Sq1 = np.array([[np.cosh(r)-np.cos(phi)*np.sinh(r), -np.sin(phi)*np.sinh(r)],
[-np.sin(phi)*np.sinh(r), np.cosh(r)+np.cos(phi)*np.sinh(r)]])
Sq2 = np.array([[np.cosh(-r)-np.cos(phi)*np.sinh(-r), -np.sin(phi)*np.sinh(-r)],
[-np.sin(phi)*np.sinh(-r), np.cosh(-r)+np.cos(phi)*np.sinh(-r)]])
# fmt: on
Sz = block_diag(Sq1, Sq2)[:, [0, 2, 1, 3]][[0, 2, 1, 3]]
expected = B.conj().T @ Sz @ B
assert np.allclose(S, expected, atol=tol, rtol=0)
示例11: _rotation
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def _rotation(phi, bare=False):
r"""Utility function, returns the Heisenberg transformation of a phase rotation gate.
The transformation matrix returned is:
.. math:: M = \begin{bmatrix}
1 & 0 & 0\\
0 & \cos\phi & -\sin\phi\\
0 & \sin\phi & \cos\phi
\end{bmatrix}
Args:
phi (float): rotation angle.
bare (bool): if True, return a simple 2d rotation matrix
Returns:
array[float]: transformation matrix
"""
c = math.cos(phi)
s = math.sin(phi)
temp = np.array([[c, -s], [s, c]])
if bare:
return temp
return block_diag(1, temp) # pylint: disable=no-member
示例12: test_two_mode_squeezing
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def test_two_mode_squeezing(self, tol):
"""Test the two mode squeezing symplectic transform."""
r = 0.543
phi = 0.123
S = two_mode_squeezing(r, phi)
# test that S = B^\dagger(pi/4, 0) [S(z) x S(-z)] B(pi/4)
B = beamsplitter(np.pi/4, 0)
Sz = block_diag(squeezing(r, phi), squeezing(-r, phi))[:, [0, 2, 1, 3]][[0, 2, 1, 3]]
expected = B.conj().T @ Sz @ B
assert S == pytest.approx(expected, abs=tol)
# test that S |a1, a2> = |ta1+ra2, ta2+ra1>
a1 = 0.23+0.12j
a2 = 0.23+0.12j
out = S @ np.array([a1.real, a2.real, a1.imag, a2.imag])*np.sqrt(2*hbar)
T = np.cosh(r)
R = np.exp(1j*phi)*np.sinh(r)
a1out = T*a1 + R*np.conj(a2)
a2out = T*a2 + R*np.conj(a1)
expected = np.array([a1out.real, a2out.real, a1out.imag, a2out.imag])*np.sqrt(2*hbar)
assert out == pytest.approx(expected, abs=tol)
示例13: test_controlled_addition
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def test_controlled_addition(self, tol):
"""Test the CX symplectic transform."""
s = 0.543
S = controlled_addition(s)
# test that S = B(theta+pi/2, 0) [S(z) x S(-z)] B(theta, 0)
r = np.arcsinh(-s/2)
theta = 0.5*np.arctan2(-1/np.cosh(r), -np.tanh(r))
Sz = block_diag(squeezing(r, 0), squeezing(-r, 0))[:, [0, 2, 1, 3]][[0, 2, 1, 3]]
expected = beamsplitter(theta+np.pi/2, 0) @ Sz @ beamsplitter(theta, 0)
assert S == pytest.approx(expected, abs=tol)
# test that S[x1, x2, p1, p2] -> [x1, x2+sx1, p1-sp2, p2]
x1 = 0.5432
x2 = -0.453
p1 = 0.154
p2 = -0.123
out = S @ np.array([x1, x2, p1, p2])*np.sqrt(2*hbar)
expected = np.array([x1, x2+s*x1, p1-s*p2, p2])*np.sqrt(2*hbar)
assert out == pytest.approx(expected, abs=tol)
示例14: test_controlled_phase
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def test_controlled_phase(self, tol):
"""Test the CZ symplectic transform."""
s = 0.543
S = controlled_phase(s)
# test that S = R_2(pi/2) CX(s) R_2(pi/2)^\dagger
R2 = block_diag(np.identity(2), rotation(np.pi/2))[:, [0, 2, 1, 3]][[0, 2, 1, 3]]
expected = R2 @ controlled_addition(s) @ R2.conj().T
assert S == pytest.approx(expected, abs=tol)
# test that S[x1, x2, p1, p2] -> [x1, x2, p1+sx2, p2+sx1]
x1 = 0.5432
x2 = -0.453
p1 = 0.154
p2 = -0.123
out = S @ np.array([x1, x2, p1, p2])*np.sqrt(2*hbar)
expected = np.array([x1, x2, p1+s*x2, p2+s*x1])*np.sqrt(2*hbar)
assert out == pytest.approx(expected, abs=tol)
示例15: Sa
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import block_diag [as 别名]
def Sa(self, x_surface, geom):
"""Covariance of prior distribution, calculated at state x. We find
the covariance in a normalized space (normalizing by z) and then un-
normalize the result for the calling function."""
lamb = self.calc_lamb(x_surface, geom)
lamb_ref = lamb[self.idx_ref]
ci = self.component(x_surface, geom)
Cov = self.components[ci][1]
Cov = Cov * (self.norm(lamb_ref)**2)
# If there are no other state vector elements, we're done.
if len(self.statevec_names) == len(self.idx_lamb):
return Cov
# Embed into a larger state vector covariance matrix
nprefix = self.idx_lamb[0]
nsuffix = len(self.statevec_names) - self.idx_lamb[-1] - 1
Cov_prefix = np.zeros((nprefix, nprefix))
Cov_suffix = np.zeros((nsuffix, nsuffix))
return block_diag(Cov_prefix, Cov, Cov_suffix)