Solve a linear matrix equation, or system of linear scalar equations. Computes the "exact" solution, `x`, of the well-determined, i.e., full rank, linear matrix equation `ax = b`. Parameters ---------- a : (..., M, M) array_like Coefficient matrix. b : {(M,), (
(a, b)
| 373 | |
| 374 | @array_function_dispatch(_solve_dispatcher) |
| 375 | def solve(a, b): |
| 376 | """ |
| 377 | Solve a linear matrix equation, or system of linear scalar equations. |
| 378 | |
| 379 | Computes the "exact" solution, `x`, of the well-determined, i.e., full |
| 380 | rank, linear matrix equation `ax = b`. |
| 381 | |
| 382 | Parameters |
| 383 | ---------- |
| 384 | a : (..., M, M) array_like |
| 385 | Coefficient matrix. |
| 386 | b : {(M,), (..., M, K)}, array_like |
| 387 | Ordinate or "dependent variable" values. |
| 388 | |
| 389 | Returns |
| 390 | ------- |
| 391 | x : {(..., M,), (..., M, K)} ndarray |
| 392 | Solution to the system a x = b. Returned shape is (..., M) if b is |
| 393 | shape (M,) and (..., M, K) if b is (..., M, K), where the "..." part is |
| 394 | broadcasted between a and b. |
| 395 | |
| 396 | Raises |
| 397 | ------ |
| 398 | LinAlgError |
| 399 | If `a` is singular or not square. |
| 400 | |
| 401 | See Also |
| 402 | -------- |
| 403 | scipy.linalg.solve : Similar function in SciPy. |
| 404 | |
| 405 | Notes |
| 406 | ----- |
| 407 | Broadcasting rules apply, see the `numpy.linalg` documentation for |
| 408 | details. |
| 409 | |
| 410 | The solutions are computed using LAPACK routine ``_gesv``. |
| 411 | |
| 412 | `a` must be square and of full-rank, i.e., all rows (or, equivalently, |
| 413 | columns) must be linearly independent; if either is not true, use |
| 414 | `lstsq` for the least-squares best "solution" of the |
| 415 | system/equation. |
| 416 | |
| 417 | .. versionchanged:: 2.0 |
| 418 | |
| 419 | The b array is only treated as a shape (M,) column vector if it is |
| 420 | exactly 1-dimensional. In all other instances it is treated as a stack |
| 421 | of (M, K) matrices. Previously b would be treated as a stack of (M,) |
| 422 | vectors if b.ndim was equal to a.ndim - 1. |
| 423 | |
| 424 | References |
| 425 | ---------- |
| 426 | .. [1] G. Strang, *Linear Algebra and Its Applications*, 2nd Ed., Orlando, |
| 427 | FL, Academic Press, Inc., 1980, pg. 22. |
| 428 | |
| 429 | Examples |
| 430 | -------- |
| 431 | Solve the system of equations: |
| 432 | ``x0 + 2 * x1 = 1`` and |
no test coverage detected
searching dependent graphs…