lucid.linalg.solve

lucid.linalg.solve(a: Tensor, b: Tensor, /) Tensor

The solve function solves a linear system of equations \(\mathbf{A} \mathbf{x} = \mathbf{b}\).

Function Signature

def solve(a: Tensor, b: Tensor) -> Tensor

Parameters

  • a (Tensor):

    The coefficient matrix \(\mathbf{A}\). It must be square (same number of rows and columns).

  • b (Tensor):

    The right-hand side matrix or vector \(\mathbf{b}\). Its number of rows must match the number of rows in \(\mathbf{A}\).

Returns

  • Tensor:

    A tensor containing the solution vector or matrix \(\mathbf{x}\) to the equation \(\mathbf{A} \mathbf{x} = \mathbf{b}\).

Forward Calculation

The forward calculation for solve finds the solution \(\mathbf{x}\) that satisfies:

\[\mathbf{A} \mathbf{x} = \mathbf{b}\]

Backward Gradient Calculation

For a linear system, the gradient calculations are based on perturbing the matrices \(\mathbf{A}\) and \(\mathbf{b}\).

If the solution is \(\mathbf{x}\), the gradients are computed as follows:

\[\frac{\partial \mathbf{x}}{\partial \mathbf{A}} = -\mathbf{A}^{-1} \cdot \mathbf{x} \cdot \frac{\partial \mathbf{b}}{\partial \mathbf{A}}\]
\[\frac{\partial \mathbf{x}}{\partial \mathbf{b}} = \mathbf{A}^{-1}\]

This leverages matrix differentiation and propagates gradients efficiently.

Raises

Attention

  • ValueError: If the matrix \(\mathbf{A}\) is not square or if the dimensions of \(\mathbf{b}\) do not align with \(\mathbf{A}\).

  • LinAlgError: If the matrix \(\mathbf{A}\) is singular and the system cannot be solved.

Example

>>> import lucid
>>> a = lucid.Tensor([[3.0, 1.0], [1.0, 2.0]])
>>> b = lucid.Tensor([9.0, 8.0])
>>> x = lucid.linalg.solve(a, b)
>>> print(x)
Tensor([2.0, 3.0])

Note

  • The input tensor \(\mathbf{A}\) must be invertible to ensure a unique solution exists.

  • If \(\mathbf{b}\) is a matrix, the function computes solutions for multiple right-hand sides.