本文整理匯總了Python中scipy.linalg.lu_factor方法的典型用法代碼示例。如果您正苦於以下問題:Python linalg.lu_factor方法的具體用法?Python linalg.lu_factor怎麽用?Python linalg.lu_factor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy.linalg
的用法示例。
在下文中一共展示了linalg.lu_factor方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def __init__(self, inv_array, inv_lu_and_piv, inv_lu_transposed):
"""
Args:
inv_array (array): 2D array specifying inverse matrix entries.
inv_lu_and_piv (Tuple[array, array]): Pivoted LU factorisation
represented as a tuple with first element a 2D array containing
the lower and upper triangular factors (with the unit diagonal
of the lower triangular factor not stored) and the second
element a 1D array containing the pivot indices. Corresponds
to the output of `scipy.linalg.lu_factor` and input to
`scipy.linalg.lu_solve`.
inv_lu_transposed (bool): Whether LU factorisation is of inverse of
array or transpose of inverse of array.
"""
super().__init__(inv_array.shape)
self._inv_array = inv_array
self._inv_lu_and_piv = inv_lu_and_piv
self._inv_lu_transposed = inv_lu_transposed
示例2: lu_factor
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def lu_factor(sigma, A):
"""
LU Factorisation wrapper of:
.. math:: LU = (\sigma \mathbf{I} - \mathbf{A})
In the case of ``A`` being a sparse matrix, the sparse methods in scipy are employed
Args:
sigma (float): Expansion frequency
A (csc_matrix or np.ndarray): Dynamics matrix
Returns:
tuple or SuperLU: tuple (dense) or SuperLU (sparse) objects containing the LU factorisation
"""
n = A.shape[0]
if type(A) == libsp.csc_matrix:
return scsp.linalg.splu(sigma * scsp.identity(n, dtype=complex, format='csc') - A)
else:
return sclalg.lu_factor(sigma * np.eye(n) - A)
示例3: build_krylov_space
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def build_krylov_space(frequency, r, side, a, b):
if frequency == np.inf or frequency.real == np.inf:
approx_type = 'partial_realisation'
lu_a = a
else:
approx_type = 'Pade'
lu_a = lu_factor(frequency, a)
try:
nu = b.shape[1]
except IndexError:
nu = 1
if nu == 1:
krylov_function = construct_krylov
else:
krylov_function = construct_mimo_krylov
v = krylov_function(r, lu_a, b, approx_type, side)
return v
示例4: __init__
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def __init__(self, M):
self.M_lu = lu_factor(M)
self.shape = M.shape
self.dtype = M.dtype
示例5: __init__
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def __init__(self, M):
self.M_lu = lu_factor(M)
LinearOperator.__init__(self, M.shape, self._matvec, dtype=M.dtype)
示例6: solve_nonlinear
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def solve_nonlinear(self, inputs, outputs):
self.lu = lu_factor(inputs['mtx'])
outputs['circulations'] = lu_solve(self.lu, inputs['rhs'])
示例7: linearize
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def linearize(self, inputs, outputs, partials):
system_size = self.system_size
self.lu = lu_factor(inputs['mtx'])
partials['circulations', 'circulations'] = inputs['mtx'].flatten()
partials['circulations', 'mtx'] = \
np.outer(np.ones(system_size), outputs['circulations']).flatten()
示例8: statdist
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def statdist(generator):
"""Compute the stationary distribution of a Markov chain.
Parameters
----------
generator : array_like
Infinitesimal generator matrix of the Markov chain.
Returns
-------
dist : numpy.ndarray
The unnormalized stationary distribution of the Markov chain.
Raises
------
ValueError
If the Markov chain does not have a unique stationary distribution.
"""
generator = np.asarray(generator)
n = generator.shape[0]
with warnings.catch_warnings():
# The LU decomposition raises a warning when the generator matrix is
# singular (which it, by construction, is!).
warnings.filterwarnings("ignore")
lu, piv = spl.lu_factor(generator.T, check_finite=False)
# The last row contains 0's only.
left = lu[:-1,:-1]
right = -lu[:-1,-1]
# Solves system `left * x = right`. Assumes that `left` is
# upper-triangular (ignores lower triangle).
try:
res = spl.solve_triangular(left, right, check_finite=False)
except:
# Ideally we would like to catch `spl.LinAlgError` only, but there seems
# to be a bug in scipy, in the code that raises the LinAlgError (!!).
raise ValueError(
"stationary distribution could not be computed. "
"Perhaps the Markov chain has more than one absorbing class?")
res = np.append(res, 1.0)
return (n / res.sum()) * res
示例9: factorise_interp_matrix
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def factorise_interp_matrix(self):
if not self.factorisation_current:
A, self.left_scaling, self.right_scaling = self.interpolation_matrix()
self.lu, self.piv = LA.lu_factor(A)
self.factorisation_current = True
return
示例10: factored
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def factored(self):
"Caches the LU factorisation of the matrix"
try:
return self.lu_factored
except AttributeError:
self.lu_factored = la.lu_factor(self.val().simple_view())
return self.lu_factored
示例11: lu_and_piv
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def lu_and_piv(self):
"""Pivoted LU factorisation of matrix."""
if self._lu_and_piv is None:
self._lu_and_piv = sla.lu_factor(self._array, check_finite=False)
self._lu_transposed = False
return self._lu_and_piv
示例12: __init__
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def __init__(self):
matrix_pairs = {}
rng = np.random.RandomState(SEED)
for sz in SIZES:
for transposed in [True, False]:
inverse_array = rng.standard_normal((sz, sz))
inverse_lu_and_piv = sla.lu_factor(
inverse_array.T if transposed else inverse_array)
array = nla.inv(inverse_array)
matrix_pairs[(sz, transposed)] = (
matrices.InverseLUFactoredSquareMatrix(
inverse_array, inverse_lu_and_piv, transposed), array)
super().__init__(matrix_pairs, rng)
示例13: lu_factor
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def lu_factor(A, rho):
r"""
Compute LU factorisation of either :math:`A^T A + \rho I` or
:math:`A A^T + \rho I`, depending on which matrix is smaller.
Parameters
----------
A : array_like
Array :math:`A`
rho : float
Scalar :math:`\rho`
Returns
-------
lu : ndarray
Matrix containing U in its upper triangle, and L in its lower triangle,
as returned by :func:`scipy.linalg.lu_factor`
piv : ndarray
Pivot indices representing the permutation matrix P, as returned by
:func:`scipy.linalg.lu_factor`
"""
N, M = A.shape
# If N < M it is cheaper to factorise A*A^T + rho*I and then use the
# matrix inversion lemma to compute the inverse of A^T*A + rho*I
if N >= M:
lu, piv = linalg.lu_factor(A.T.dot(A) + rho*np.identity(M,
dtype=A.dtype))
else:
lu, piv = linalg.lu_factor(A.dot(A.T) + rho*np.identity(N,
dtype=A.dtype))
return lu, piv
示例14: lu_solve_ATAI
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def lu_solve_ATAI(A, rho, b, lu, piv):
r"""
Solve the linear system :math:`(A^T A + \rho I)\mathbf{x} = \mathbf{b}`
or :math:`(A^T A + \rho I)X = B` using :func:`scipy.linalg.lu_solve`.
Parameters
----------
A : array_like
Matrix :math:`A`
rho : float
Scalar :math:`\rho`
b : array_like
Vector :math:`\mathbf{b}` or matrix :math:`B`
lu : array_like
Matrix containing U in its upper triangle, and L in its lower triangle,
as returned by :func:`scipy.linalg.lu_factor`
piv : array_like
Pivot indices representing the permutation matrix P, as returned by
:func:`scipy.linalg.lu_factor`
Returns
-------
x : ndarray
Solution to the linear system.
"""
N, M = A.shape
if N >= M:
x = linalg.lu_solve((lu, piv), b)
else:
x = (b - A.T.dot(linalg.lu_solve((lu, piv), A.dot(b), 1))) / rho
return x
示例15: lu_solve_AATI
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import lu_factor [as 別名]
def lu_solve_AATI(A, rho, b, lu, piv):
r"""
Solve the linear system :math:`(A A^T + \rho I)\mathbf{x} = \mathbf{b}`
or :math:`(A A^T + \rho I)X = B` using :func:`scipy.linalg.lu_solve`.
Parameters
----------
A : array_like
Matrix :math:`A`
rho : float
Scalar :math:`\rho`
b : array_like
Vector :math:`\mathbf{b}` or matrix :math:`B`
lu : array_like
Matrix containing U in its upper triangle, and L in its lower triangle,
as returned by :func:`scipy.linalg.lu_factor`
piv : array_like
Pivot indices representing the permutation matrix P, as returned by
:func:`scipy.linalg.lu_factor`
Returns
-------
x : ndarray
Solution to the linear system.
"""
N, M = A.shape
if N >= M:
x = (b - linalg.lu_solve((lu, piv), b.dot(A).T).T.dot(A.T)) / rho
else:
x = linalg.lu_solve((lu, piv), b.T).T
return x