scipy.linalg.solve#
- scipy.linalg.solve(a, b, sym_pos=False, lower=False, overwrite_a=False, overwrite_b=False, check_finite=True, assume_a='gen', transposed=False)[source]#
Solves the linear equation set
a * x = b
for the unknownx
for squarea
matrix.If the data matrix is known to be a particular type then supplying the corresponding string to
assume_a
key chooses the dedicated solver. The available options aregeneric matrix
‘gen’
symmetric
‘sym’
hermitian
‘her’
positive definite
‘pos’
If omitted,
'gen'
is the default structure.The datatype of the arrays define which solver is called regardless of the values. In other words, even when the complex array entries have precisely zero imaginary parts, the complex solver will be called based on the data type of the array.
- Parameters
- a(N, N) array_like
Square input data
- b(N, NRHS) array_like
Input data for the right hand side.
- sym_posbool, optional, deprecated
Assume a is symmetric and positive definite.
Deprecated since version 0.19.0: This keyword is deprecated and should be replaced by using
assume_a = 'pos'
. sym_pos will be removed in SciPy 1.11.0.- lowerbool, optional
If True, only the data contained in the lower triangle of a. Default is to use upper triangle. (ignored for
'gen'
)- overwrite_abool, optional
Allow overwriting data in a (may enhance performance). Default is False.
- overwrite_bbool, optional
Allow overwriting data in b (may enhance performance). Default is False.
- check_finitebool, optional
Whether to check that the input matrices contain only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs.
- assume_astr, optional
Valid entries are explained above.
- transposedbool, optional
If True,
a^T x = b
for real matrices, raises NotImplementedError for complex matrices (only for True).
- Returns
- x(N, NRHS) ndarray
The solution array.
- Raises
- ValueError
If size mismatches detected or input a is not square.
- LinAlgError
If the matrix is singular.
- LinAlgWarning
If an ill-conditioned input a is detected.
- NotImplementedError
If transposed is True and input a is a complex matrix.
Notes
If the input b matrix is a 1-D array with N elements, when supplied together with an NxN input a, it is assumed as a valid column vector despite the apparent size mismatch. This is compatible with the numpy.dot() behavior and the returned result is still 1-D array.
The generic, symmetric, Hermitian and positive definite solutions are obtained via calling ?GESV, ?SYSV, ?HESV, and ?POSV routines of LAPACK respectively.
Examples
Given a and b, solve for x:
>>> a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]]) >>> b = np.array([2, 4, -1]) >>> from scipy import linalg >>> x = linalg.solve(a, b) >>> x array([ 2., -2., 9.]) >>> np.dot(a, x) == b array([ True, True, True], dtype=bool)