solve

Contents

solve#

class brainunit.linalg.solve(a, b, **kwargs)#

Solve a linear system of equations.

SaiUnit implementation of numpy.linalg.solve().

This solves a (batched) linear system of equations a @ x = b for x given a and b. The resulting unit is b.unit / a.unit.

Parameters:
  • a (Array | ndarray | bool | number | bool | int | float | complex | saiunit.Quantity) – Coefficient matrix of shape (..., N, N).

  • b (Array | ndarray | bool | number | bool | int | float | complex | saiunit.Quantity) – Right-hand side of shape (N,) (for a 1-dimensional right-hand side) or (..., N, M) (for batched 2-dimensional right-hand side).

Returns:

x – Solution array. The result has shape (..., N) if b is of shape (N,), and shape (..., N, M) otherwise. The resulting unit is b.unit / a.unit.

Return type:

Array | ndarray | bool | number | bool | int | float | complex | saiunit.Quantity

See also

saiunit.linalg.inv

Compute the inverse of a matrix.

saiunit.linalg.lstsq

Least-squares solution to a linear equation.

saiunit.linalg.tensorsolve

Solve the tensor equation a x = b.

Notes

Prefer solve over explicitly computing inv(a) @ b for better numerical precision and performance.

Examples

>>> import saiunit as u
>>> import jax.numpy as jnp
>>> A = jnp.array([[1., 2., 3.],
...                [2., 4., 2.],
...                [3., 2., 1.]]) * u.meter
>>> b = jnp.array([14., 16., 10.]) * u.second
>>> x = u.linalg.solve(A, b)
>>> x.unit
Unit("s / m")
>>> u.math.allclose(A @ x, b)
Array(True, dtype=bool)