本文整理汇总了Python中scipy.linalg.det方法的典型用法代码示例。如果您正苦于以下问题:Python linalg.det方法的具体用法?Python linalg.det怎么用?Python linalg.det使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.linalg
的用法示例。
在下文中一共展示了linalg.det方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: f_value_wilks_lambda
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def f_value_wilks_lambda(ER, EF, dfnum, dfden, a, b):
"""Calculation of Wilks lambda F-statistic for multivarite data, per
Maxwell & Delaney p.657.
"""
if isinstance(ER, (int, float)):
ER = array([[ER]])
if isinstance(EF, (int, float)):
EF = array([[EF]])
lmbda = linalg.det(EF) / linalg.det(ER)
if (a-1)**2 + (b-1)**2 == 5:
q = 1
else:
q = np.sqrt(((a-1)**2*(b-1)**2 - 2) / ((a-1)**2 + (b-1)**2 - 5))
n_um = (1 - lmbda**(1.0/q))*(a-1)*(b-1)
d_en = lmbda**(1.0/q) / (n_um*q - 0.5*(a-1)*(b-1) + 1)
return n_um / d_en
示例2: test_compute_log_det_cholesky
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def test_compute_log_det_cholesky():
n_features = 2
rand_data = RandomData(np.random.RandomState(0))
for covar_type in COVARIANCE_TYPE:
covariance = rand_data.covariances[covar_type]
if covar_type == 'full':
predected_det = np.array([linalg.det(cov) for cov in covariance])
elif covar_type == 'tied':
predected_det = linalg.det(covariance)
elif covar_type == 'diag':
predected_det = np.array([np.prod(cov) for cov in covariance])
elif covar_type == 'spherical':
predected_det = covariance ** n_features
# We compute the cholesky decomposition of the covariance matrix
expected_det = _compute_log_det_cholesky(_compute_precision_cholesky(
covariance, covar_type), covar_type, n_features=n_features)
assert_array_almost_equal(expected_det, - .5 * np.log(predected_det))
示例3: _params_zyz
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def _params_zyz(mat):
"""Return the euler angles and phase for the ZYZ basis."""
# We rescale the input matrix to be special unitary (det(U) = 1)
# This ensures that the quaternion representation is real
coeff = la.det(mat)**(-0.5)
phase = -np.angle(coeff)
su_mat = coeff * mat # U in SU(2)
# OpenQASM SU(2) parameterization:
# U[0, 0] = exp(-i(phi+lambda)/2) * cos(theta/2)
# U[0, 1] = -exp(-i(phi-lambda)/2) * sin(theta/2)
# U[1, 0] = exp(i(phi-lambda)/2) * sin(theta/2)
# U[1, 1] = exp(i(phi+lambda)/2) * cos(theta/2)
theta = 2 * math.atan2(abs(su_mat[1, 0]), abs(su_mat[0, 0]))
phiplambda = 2 * np.angle(su_mat[1, 1])
phimlambda = 2 * np.angle(su_mat[1, 0])
phi = (phiplambda + phimlambda) / 2.0
lam = (phiplambda - phimlambda) / 2.0
return theta, phi, lam, phase
示例4: check_one_qubit_euler_angles
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def check_one_qubit_euler_angles(self, operator, basis='U3',
tolerance=1e-12,
phase_equal=False):
"""Check euler_angles_1q works for the given unitary"""
decomposer = OneQubitEulerDecomposer(basis)
with self.subTest(operator=operator):
target_unitary = operator.data
decomp_unitary = Operator(decomposer(operator)).data
if not phase_equal:
target_unitary *= la.det(target_unitary)**(-0.5)
decomp_unitary *= la.det(decomp_unitary)**(-0.5)
maxdist = np.max(np.abs(target_unitary - decomp_unitary))
if not phase_equal and maxdist > 0.1:
maxdist = np.max(np.abs(target_unitary + decomp_unitary))
self.assertTrue(np.abs(maxdist) < tolerance, "Worst distance {}".format(maxdist))
# U3 basis
示例5: __init__
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def __init__(self, sigma, mu, seed=42):
self.sigma = sigma
self.mu = mu
if not (len(mu.shape) == 1):
raise Exception('mu has shape ' + str(mu.shape) +
' (it should be a vector)')
self.sigma_inv = solve(self.sigma, N.identity(mu.shape[0]),
sym_pos=True)
self.L = cholesky(self.sigma)
self.s_rng = make_theano_rng(seed, which_method='normal')
#Compute logZ
#log Z = log 1/( (2pi)^(-k/2) |sigma|^-1/2 )
# = log 1 - log (2pi^)(-k/2) |sigma|^-1/2
# = 0 - log (2pi)^(-k/2) - log |sigma|^-1/2
# = (k/2) * log(2pi) + (1/2) * log |sigma|
k = float(self.mu.shape[0])
self.logZ = 0.5 * (k * N.log(2. * N.pi) + N.log(det(sigma)))
示例6: f_value_wilks_lambda
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def f_value_wilks_lambda(ER, EF, dfnum, dfden, a, b):
"""Calculation of Wilks lambda F-statistic for multivarite data, per
Maxwell & Delaney p.657.
"""
if isinstance(ER, (int, float)):
ER = array([[ER]])
if isinstance(EF, (int, float)):
EF = array([[EF]])
lmbda = linalg.det(EF) / linalg.det(ER)
if (a-1)**2 + (b-1)**2 == 5:
q = 1
else:
q = np.sqrt(((a-1)**2*(b-1)**2 - 2) / ((a-1)**2 + (b-1)**2 - 5))
n_um = (1 - lmbda**(1.0/q))*(a-1)*(b-1)
d_en = lmbda**(1.0/q) / (n_um*q - 0.5*(a-1)*(b-1) + 1)
return n_um / d_en
示例7: berry_phase
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def berry_phase(h,nk=20,kpath=None):
""" Calculates the Berry phase of a Hamiltonian"""
if h.dimensionality==0: raise
elif h.dimensionality == 1:
ks = np.linspace(0.,1.,nk,endpoint=False) # list of kpoints
elif h.dimensionality > 1: # you must provide a kpath
if kpath is None:
print("You must provide a k-path")
raise # error
ks = kpath # continue
nk = len(kpath) # redefine
else: raise # otherwise
hkgen = h.get_hk_gen() # get Hamiltonian generator
wf0 = occupied_states(hkgen,ks[0]) # get occupied states, first k-point
wfold = wf0.copy() # copy
m = np.matrix(np.identity(len(wf0))) # initialize as the identity matrix
for ik in range(1,len(ks)): # loop over k-points, except first one
wf = occupied_states(hkgen,ks[ik]) # get waves
m = m@uij(wfold,wf) # get the uij and multiply
wfold = wf.copy() # this is the new old
m = m@uij(wfold,wf0) # last one
d = lg.det(m) # calculate determinant
phi = np.arctan2(d.imag,d.real)
open("BERRY_PHASE.OUT","w").write(str(phi/np.pi)+"\n")
return phi # return Berry phase
示例8: berry_curvature
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def berry_curvature(h,k,dk=0.01,window=None,max_waves=None):
""" Calculates the Berry curvature of a 2d hamiltonian"""
if h.dimensionality != 2: raise # only for 2d
k = np.array([k[0],k[1]])
dx = np.array([dk,0.])
dy = np.array([0.,dk])
# get the function that returns the occ states
occf = occ_states_generator(h,k,window=window,max_waves=max_waves)
# get the waves
# print("Doing k-point",k)
wf1 = occf(k-dx-dy)
wf2 = occf(k+dx-dy)
wf3 = occf(k+dx+dy)
wf4 = occf(k-dx+dy)
dims = [len(wf1),len(wf2),len(wf3),len(wf4)] # number of vectors
if max(dims)!=min(dims): # check that the dimensions are fine
# print("WARNING, skipping this k-point",k)
return 0.0 # if different number of vectors
# get the uij
m = uij(wf1,wf2)@uij(wf2,wf3)@uij(wf3,wf4)@uij(wf4,wf1)
d = lg.det(m) # calculate determinant
phi = np.arctan2(d.imag,d.real)/(4.*dk*dk)
return phi
示例9: is_rotation_matrix
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def is_rotation_matrix(mat, show_diff=False):
from scipy.linalg import det, norm
dim = mat.shape[0]
if dim != mat.shape[1]:
return False
determ = det(mat)
right_handed = (np.abs(determ - 1.) < 1E-10)
orthonorm_diff = mat * mat.T - np.eye(dim)
diff_norm = norm(orthonorm_diff, 2)
orthonormal = (diff_norm < 1E-10)
if not right_handed or not orthonormal:
if show_diff:
print('matrix S:\n', mat)
print('det(S): ', determ)
print('S*S.T - eye:\n', orthonorm_diff)
print('2-norm of difference: ', diff_norm)
return False
return True
示例10: logsqrtdet
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def logsqrtdet(self):
# Sigma = L^T L -- but be careful: only the lower triangle and
# diagonal of L are actually initialized; the upper triangle is
# garbage.
L, _lower = self._cholesky
# det Sigma = det L^T L = det L^T det L = (det L)^2. Since L is
# triangular, its determinant is the product of its diagonal. To
# compute log sqrt(det Sigma) = log det L, we sum the logs of its
# diagonal.
return np.sum(np.log(np.diag(L)))
示例11: logpdf
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def logpdf(X, Mu, Sigma):
"""Multivariate normal log pdf."""
# This is the multivariate normal log pdf for an array X of n
# outputs, an array Mu of n means, and an n-by-n positive-definite
# covariance matrix Sigma. The direct-space density is:
#
# P(X | Mu, Sigma)
# = ((2 pi)^n det Sigma)^(-1/2)
# exp((-1/2) (X - Mu)^T Sigma^-1 (X - Mu)),
#
# We want this in log-space, so we have
#
# log P(X | Mu, Sigma)
# = (-1/2) (X - Mu)^T Sigma^-1 (X - Mu) - log ((2 pi)^n det Sigma)^(1/2)
# = (-1/2) (X - Mu)^T Sigma^-1 (X - Mu)
# - (n/2) log (2 pi) - (1/2) log det Sigma.
#
n = len(X)
assert X.shape == (n,)
assert Mu.shape == (n,)
assert Sigma.shape == (n, n)
assert np.all(np.isfinite(X))
assert np.all(np.isfinite(Mu))
assert np.all(np.isfinite(Sigma))
X_ = X - Mu
covf = _covariance_factor(Sigma)
logp = -np.dot(X_.T, covf.solve(X_)/2.)
logp -= (n/2.)*np.log(2*np.pi)
logp -= covf.logsqrtdet()
# Convert 1x1 matrix to float.
return float(logp)
示例12: _compute_covariance
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def _compute_covariance(self):
"""Computes the covariance matrix for each Gaussian kernel using
covariance_factor().
"""
self.factor = self.covariance_factor()
# Cache covariance and inverse covariance of the data
if not hasattr(self, '_data_inv_cov'):
self._data_covariance = atleast_2d(np.cov(self.dataset, rowvar=1,
bias=False))
self._data_inv_cov = linalg.inv(self._data_covariance)
self.covariance = self._data_covariance * self.factor**2
self.inv_cov = self._data_inv_cov / self.factor**2
self._norm_factor = sqrt(linalg.det(2*pi*self.covariance)) * self.n
示例13: mdet
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def mdet(self):
return linalg.det(self.m)
示例14: mlogdet
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def mlogdet(self):
return np.log(linalg.det(self.m))
示例15: calcWeight
# 需要导入模块: from scipy import linalg [as 别名]
# 或者: from scipy.linalg import det [as 别名]
def calcWeight( jac ):
n = jac.shape
if n[0] == n[1]:
return abs(det(jac))
elif n[0] == 2 and n[1] == 1:
return sqrt(sum(sum(jac*jac)))
#------------------------------------------------------------------------------
#
#------------------------------------------------------------------------------