A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.pytorch.org/docs/stable/generated/torch.linalg.solve.html below:

torch.linalg.solve — PyTorch 2.7 documentation

torch.linalg.solve
torch.linalg.solve(A, B, *, left=True, out=None) Tensor

Computes the solution of a square system of linear equations with a unique solution.

Letting K \mathbb{K} K be R \mathbb{R} R or C \mathbb{C} C, this function computes the solution X ∈ K n × k X \in \mathbb{K}^{n \times k} XKn×k of the linear system associated to A ∈ K n × n , B ∈ K n × k A \in \mathbb{K}^{n \times n}, B \in \mathbb{K}^{n \times k} AKn×n,BKn×k, which is defined as

A X = B AX = B AX=B

If left= False, this function returns the matrix X ∈ K n × k X \in \mathbb{K}^{n \times k} XKn×k that solves the system

X A = B A ∈ K k × k , B ∈ K n × k . XA = B\mathrlap{\qquad A \in \mathbb{K}^{k \times k}, B \in \mathbb{K}^{n \times k}.} XA=BAKk×k,BKn×k.

This system of linear equations has one solution if and only if A A A is invertible. This function assumes that A A A is invertible.

Supports inputs of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if the inputs are batches of matrices then the output has the same batch dimensions.

Letting * be zero or more batch dimensions,

Note

This function computes X = A.inverse() @ B in a faster and more numerically stable way than performing the computations separately.

Note

It is possible to compute the solution of the system X A = B XA = B XA=B by passing the inputs A and B transposed and transposing the output returned by this function.

Note

A is allowed to be a non-batched torch.sparse_csr_tensor, but only with left=True.

Note

When inputs are on a CUDA device, this function synchronizes that device with the CPU. For a version of this function that does not synchronize, see torch.linalg.solve_ex().

Parameters
  • A (Tensor) – tensor of shape (*, n, n) where * is zero or more batch dimensions.

  • B (Tensor) – right-hand side tensor of shape (*, n) or (*, n, k) or (n,) or (n, k) according to the rules described above

Keyword Arguments
  • left (bool, optional) – whether to solve the system A X = B AX=B AX=B or X A = B XA = B XA=B. Default: True.

  • out (Tensor, optional) – output tensor. Ignored if None. Default: None.

Raises

RuntimeError – if the A matrix is not invertible or any matrix in a batched A is not invertible.

Examples:

>>> A = torch.randn(3, 3)
>>> b = torch.randn(3)
>>> x = torch.linalg.solve(A, b)
>>> torch.allclose(A @ x, b)
True
>>> A = torch.randn(2, 3, 3)
>>> B = torch.randn(2, 3, 4)
>>> X = torch.linalg.solve(A, B)
>>> X.shape
torch.Size([2, 3, 4])
>>> torch.allclose(A @ X, B)
True

>>> A = torch.randn(2, 3, 3)
>>> b = torch.randn(3, 1)
>>> x = torch.linalg.solve(A, b) # b is broadcasted to size (2, 3, 1)
>>> x.shape
torch.Size([2, 3, 1])
>>> torch.allclose(A @ x, b)
True
>>> b = torch.randn(3)
>>> x = torch.linalg.solve(A, b) # b is broadcasted to size (2, 3)
>>> x.shape
torch.Size([2, 3])
>>> Ax = A @ x.unsqueeze(-1)
>>> torch.allclose(Ax, b.unsqueeze(-1).expand_as(Ax))
True

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4