本文整理匯總了Python中scipy.linalg方法的典型用法代碼示例。如果您正苦於以下問題:Python scipy.linalg方法的具體用法?Python scipy.linalg怎麽用?Python scipy.linalg使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy
的用法示例。
在下文中一共展示了scipy.linalg方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_k
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def get_k(self,dm):
"""
Update Exact Exchange Matrix
Args:
dm: float or complex
AO density matrix.
Returns:
kmat: float or complex
Exact Exchange in AO basis
"""
naux = self.auxmol.nao_nr()
nao = self.ks.mol.nao_nr()
kpj = np.einsum("ijp,jk->ikp", self.eri3c, dm)
pik = np.linalg.solve(self.eri2c, kpj.reshape(-1,naux).T.conj())
kmat = np.einsum("pik,kjp->ij", pik.reshape(naux,nao,nao), self.eri3c)
return kmat
示例2: __init__
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def __init__(self, linear_operator, linear_operator_diagonal, options=None):
"""
Args:
linear_operator(scipy.sparse.linalg.LinearOperator): The linear
operator which defines a dot function when applying on a vector.
linear_operator_diagonal(numpy.ndarray): The linear operator's
diagonal elements.
options(DavidsonOptions): Iteration options.
"""
if options is None:
options = DavidsonOptions()
if not isinstance(linear_operator,
(scipy.sparse.linalg.LinearOperator,
scipy.sparse.spmatrix)):
raise ValueError(
'linear_operator is not a LinearOperator: {}.'.format(type(
linear_operator)))
self.linear_operator = linear_operator
self.linear_operator_diagonal = linear_operator_diagonal
self.options = options
self.options.set_dimension(len(linear_operator_diagonal))
示例3: generate_random_vectors
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def generate_random_vectors(row, col, real_only=False):
"""Generates orthonormal random vectors with col columns.
Args:
row(int): Number of rows for the vectors.
col(int): Number of columns for the vectors.
real_only(bool): Real vectors or complex ones.
Returns:
random_vectors(numpy.ndarray(complex)): Orthonormal random vectors.
"""
random_vectors = numpy.random.rand(row, col)
if not real_only:
random_vectors = random_vectors + numpy.random.rand(row, col) * 1.0j
random_vectors = scipy.linalg.orth(random_vectors)
return random_vectors
示例4: drss
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def drss(num_states, num_inputs, num_outputs):
"""Generates a discrete-time random state-space system.
Args:
``num_states``: Number of states.
``num_inputs``: Number of inputs.
``num_outputs``: Number of outputs.
Returns:
``A``, ``B``, ``C``: State-space arrays of discrete-time system.
By construction, all eigenvalues are real and stable.
"""
# eig_vals = np.linspace(.9, .95, num_states)
# eig_vecs = np.random.normal(0, 2., (num_states, num_states))
eig_vals = np.linspace(.2, .95, num_states)
eig_vecs = np.random.normal(0, 2., (num_states, num_states))
A = np.real(
np.linalg.inv(eig_vecs).dot(np.diag(eig_vals).dot(eig_vecs)))
B = np.random.normal(0, 1., (num_states, num_inputs))
C = np.random.normal(0, 1., (num_outputs, num_states))
return A, B, C
示例5: __call__
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def __call__(self, A):
errString = "can only call scipy.linalg.expm on square arrays"
if not isinstance(A, PureNumpy.PurePythonNumpyArray):
raise TypeError(errString)
if A.shape[0] != A.shape[1]:
raise TypeError(errString)
result = __inline_fora(
"""fun(@unnamed_args:(matrix), *args) {
return purePython.linalgModule.expm(
matrix
)
}"""
)(A)
return PureNumpy.PurePythonNumpyArray(
tuple(result[1]),
result[0]
)
示例6: set_corr
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def set_corr(self, R):
"""
Set the Correlation matrix of the copula.
Parameters
----------
R : numpy array (of size copula dimensions * copula dimension)
The definite positive correlation matrix. Note that you should check yourself if the matrix is definite positive.
"""
S = np.asarray(R)
if len(S.shape) > 2:
raise ValueError("2-dimensional array expected, get {0}-dimensional array.".format(len(S.shape)))
if S.shape[0] != S.shape[1]:
raise ValueError("Correlation matrix must be a squared matrix of dimension {0}".format(self.dim))
if not(np.array_equal(np.transpose(S), S)):
raise ValueError("Correlation matrix is not symmetric.")
self.R = S
self._R_det = np.linalg.det(S)
self._R_inv = np.linalg.inv(S)
示例7: test_eigsh_valid_init_operator_with_shape
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def test_eigsh_valid_init_operator_with_shape(dtype):
backend = numpy_backend.NumPyBackend()
D = 16
np.random.seed(10)
init = backend.randn((D,), dtype=dtype, seed=10)
tmp = backend.randn((D, D), dtype=dtype, seed=10)
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
def mv(x, mat):
return np.dot(mat, x)
eta1, U1 = backend.eigsh_lanczos(mv, [H], init)
eta2, U2 = np.linalg.eigh(H)
v2 = U2[:, 0]
v2 = v2 / sum(v2)
v1 = np.reshape(U1[0], (D))
v1 = v1 / sum(v1)
np.testing.assert_allclose(eta1[0], min(eta2))
np.testing.assert_allclose(v1, v2)
示例8: test_eigsh_lanczos_1
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def test_eigsh_lanczos_1(dtype):
backend = numpy_backend.NumPyBackend()
D = 16
np.random.seed(10)
init = backend.randn((D,), dtype=dtype, seed=10)
tmp = backend.randn((D, D), dtype=dtype, seed=10)
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
def mv(x, mat):
return np.dot(mat, x)
eta1, U1 = backend.eigsh_lanczos(mv, [H], init)
eta2, U2 = np.linalg.eigh(H)
v2 = U2[:, 0]
v2 = v2 / sum(v2)
v1 = np.reshape(U1[0], (D))
v1 = v1 / sum(v1)
np.testing.assert_allclose(eta1[0], min(eta2))
np.testing.assert_allclose(v1, v2)
示例9: test_eigsh_lanczos_2
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def test_eigsh_lanczos_2(dtype):
backend = numpy_backend.NumPyBackend()
D = 16
np.random.seed(10)
tmp = backend.randn((D, D), dtype=dtype, seed=10)
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
def mv(x, mat):
return np.dot(mat, x)
eta1, U1 = backend.eigsh_lanczos(mv, [H], shape=(D,), dtype=dtype)
eta2, U2 = np.linalg.eigh(H)
v2 = U2[:, 0]
v2 = v2 / sum(v2)
v1 = np.reshape(U1[0], (D))
v1 = v1 / sum(v1)
np.testing.assert_allclose(eta1[0], min(eta2))
np.testing.assert_allclose(v1, v2)
示例10: test_eigs
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def test_eigs(dtype, which):
backend = numpy_backend.NumPyBackend()
D = 16
np.random.seed(10)
init = backend.randn((D,), dtype=dtype, seed=10)
M = backend.randn((D, D), dtype=dtype, seed=10)
def mv(x, mat):
return np.dot(mat, x)
eta1, U1 = backend.eigs(mv, [M], init, numeig=1, which=which)
eta2, U2 = np.linalg.eig(M)
val, index = find(which, eta2)
v2 = U2[:, index]
v2 = v2 / sum(v2)
v1 = np.reshape(U1[0], (D))
v1 = v1 / sum(v1)
np.testing.assert_allclose(find(which, eta1)[0], val)
np.testing.assert_allclose(v1, v2)
示例11: test_eigs_no_init
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def test_eigs_no_init(which):
backend = numpy_backend.NumPyBackend()
dtype = np.complex128
D = 16
np.random.seed(10)
H = backend.randn((D, D), dtype=dtype, seed=10)
def mv(x, mat):
return np.dot(mat, x)
eta1, U1 = backend.eigs(
mv, [H], shape=(D,), dtype=dtype, numeig=1, which=which)
eta2, U2 = np.linalg.eig(H)
val, index = find(which, eta2)
v2 = U2[:, index]
v2 = v2 / np.sum(v2)
v1 = np.reshape(U1[0], (D))
v1 = v1 / np.sum(v1)
np.testing.assert_allclose(find(which, eta1)[0], val)
np.testing.assert_allclose(v1, v2)
示例12: test_eigs_init
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def test_eigs_init(dtype, which):
backend = numpy_backend.NumPyBackend()
D = 16
np.random.seed(10)
H = backend.randn((D, D), dtype=dtype, seed=10)
init = backend.randn((D,), dtype=dtype)
def mv(x, mat):
return np.dot(mat, x)
eta1, U1 = backend.eigs(mv, [H], initial_state=init, numeig=1, which=which)
eta2, U2 = np.linalg.eig(H)
val, index = find(which, eta2)
v2 = U2[:, index]
v2 = v2 / sum(v2)
v1 = np.reshape(U1[0], (D))
v1 = v1 / sum(v1)
np.testing.assert_allclose(find(which, eta1)[0], val)
np.testing.assert_allclose(v1, v2)
示例13: test_eigsh_valid_init_operator_with_shape
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def test_eigsh_valid_init_operator_with_shape(dtype):
backend = jax_backend.JaxBackend()
D = 16
np.random.seed(10)
init = backend.randn((D,), dtype=dtype, seed=10)
tmp = backend.randn((D, D), dtype=dtype, seed=10)
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
def mv(x, H):
return jax.numpy.dot(H, x)
eta1, U1 = backend.eigsh_lanczos(mv, [H], init)
eta2, U2 = np.linalg.eigh(H)
v2 = U2[:, 0]
v2 = v2 / sum(v2)
v1 = np.reshape(U1[0], (D))
v1 = v1 / sum(v1)
np.testing.assert_allclose(eta1[0], min(eta2))
np.testing.assert_allclose(v1, v2)
示例14: test_eigsh_lanczos_1
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def test_eigsh_lanczos_1(dtype):
backend = jax_backend.JaxBackend()
D = 16
np.random.seed(10)
init = backend.randn((D,), dtype=dtype, seed=10)
tmp = backend.randn((D, D), dtype=dtype, seed=10)
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
def mv(x, H):
return jax.numpy.dot(H, x)
eta1, U1 = backend.eigsh_lanczos(mv, [H], init)
eta2, U2 = np.linalg.eigh(H)
v2 = U2[:, 0]
v2 = v2 / sum(v2)
v1 = np.reshape(U1[0], (D))
v1 = v1 / sum(v1)
np.testing.assert_allclose(eta1[0], min(eta2))
np.testing.assert_allclose(v1, v2)
示例15: test_eigsh_lanczos_2
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import linalg [as 別名]
def test_eigsh_lanczos_2(dtype):
backend = jax_backend.JaxBackend()
D = 16
np.random.seed(10)
tmp = backend.randn((D, D), dtype=dtype, seed=10)
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
def mv(x, H):
return jax.numpy.dot(H, x)
eta1, U1 = backend.eigsh_lanczos(mv, [H], shape=(D,), dtype=dtype)
eta2, U2 = np.linalg.eigh(H)
v2 = U2[:, 0]
v2 = v2 / sum(v2)
v1 = np.reshape(U1[0], (D))
v1 = v1 / sum(v1)
np.testing.assert_allclose(eta1[0], min(eta2))
np.testing.assert_allclose(v1, v2)