Skip to main content
`Ctrl`+`K`
scipy.linalg.

# solve

scipy.linalg.solve(a, b, lower=False, overwrite_a=False, overwrite_b=False, check_finite=True, assume_a=None, transposed=False)

  Solve the equation `a @ x = b` for `x`, where a is a square 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 are

  diagonal | ‘diagonal’
  tridiagonal | ‘tridiagonal’
  banded | ‘banded’
  upper triangular | ‘upper triangular’
  lower triangular | ‘lower triangular’
  symmetric | ‘symmetric’ (or ‘sym’)
  hermitian | ‘hermitian’ (or ‘her’)
  symmetric positive definite | ‘positive definite’ (or ‘pos’)
  general | ‘general’ (or ‘gen’)

  Array argument(s) of this function may have additional “batch” dimensions prepended to the core shape. In this case, the array is treated as a batch of lower-dimensional slices; see Batched Linear Operations for details.

  Parameters:
    a array_like, shape (…, N, N)

      Square left-hand side matrix or a batch of matrices.

    b(…, N, NRHS) array_like

      Input data for the right hand side or a batch of right-hand sides.

    lower bool, default: False

      Ignored unless `assume_a` is one of `'sym'`, `'her'`, or `'pos'`. If True, the calculation uses only the data in the lower triangle of a; entries above the diagonal are ignored. If False (default), the calculation uses only the data in the upper triangle of a; entries below the diagonal are ignored.

    overwrite_a bool, optional

      Allow overwriting data in a (may enhance performance). Default is False. See overwrite_* arguments for details.

    overwrite_b bool, optional

      Allow overwriting data in b (may enhance performance). Default is False. See overwrite_* arguments for details.

    check_finite bool, default: True

      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_a str, optional

      Valid entries are described above. If omitted or `None`, checks are performed to identify structure so the appropriate solver can be called.

    transposed bool, default: False

      If True, solve `a.T @ x == b`. Raises NotImplementedError for complex a.

  Returns:
    x ndarray, shape (N, NRHS) or (…, N)

      The solution array.

  Raises:
    ValueError

      If size mismatches detected or input a is not square.

    LinAlgError

      If the computation fails because of matrix singularity.

    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 general, symmetric, Hermitian and positive definite solutions are obtained via calling ?GETRF/?GETRS, ?SYSV, ?HESV, and ?POTRF/?POTRS routines of LAPACK respectively.

  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.

  Examples

  Given a and b, solve for x:

      >>> import numpy as np
      >>> a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]])
      >>> b = np.array([2, 4, -1])
      >>> from scipy.linalg import solve
      >>> x = solve(a, b)
      >>> x
      array([ 2., -2.,  9.])
      >>> a @ x == b
      array([ True,  True,  True], dtype=bool)

  Batches of matrices are supported, with and without structure detection: (See Batched Linear Operations for further details of handling batched inputs.)

      >>> a = np.arange(12).reshape(3, 2, 2)   # a batch of 3 2x2 matrices
      >>> A = a.transpose(0, 2, 1) @ a    # A is a batch of 3 positive definite matrices
      >>> b = np.ones(2)
      >>> solve(A, b)      # this automatically detects that A is pos.def.
      array([[ 1. , -0.5],
             [ 3. , -2.5],
             [ 5. , -4.5]])
      >>> solve(A, b, assume_a='pos')   # bypass structucture detection
      array([[ 1. , -0.5],
             [ 3. , -2.5],
             [ 5. , -4.5]])

  Note that the structure detection runs per-slice: in the example above, each of the two slices will be independently discovered as being positive definite. Setting an explicit `assume_a` argument bypasses structure detection and uses the provided value without checking:

      >>> a = np.stack((np.eye(2), np.arange(1, 5).reshape(2, 2)))
      >>> b = [1, 1]
      >>> solve(a, b, assume_a="diagonal")
      array([[1.  , 1.  ],
             [1.  , 0.25]])   # the second row is incorrect