tensor.slinalg
– Linear Algebra Ops Using Scipy#
Note
This module is not imported by default. You need to import it to use it.
API#
- pytensor.tensor.slinalg.block_diag(*matrices: TensorVariable)[source]#
Construct a block diagonal matrix from a sequence of input tensors.
Given the inputs
A
,B
andC
, the output will have these arrays arranged on the diagonal:- [[A, 0, 0],
[0, B, 0], [0, 0, C]]
- Parameters:
A (tensors) – Input tensors to form the block diagonal matrix. last two dimensions of the inputs will be used, and all inputs should have at least 2 dimensins.
B (tensors) – Input tensors to form the block diagonal matrix. last two dimensions of the inputs will be used, and all inputs should have at least 2 dimensins.
... (C) – Input tensors to form the block diagonal matrix. last two dimensions of the inputs will be used, and all inputs should have at least 2 dimensins.
- Returns:
out – The block diagonal matrix formed from the input matrices.
- Return type:
tensor
Examples
Create a block diagonal matrix from two 2x2 matrices:
..code-block:: python
import numpy as np from pytensor.tensor.linalg import block_diag
A = pt.as_tensor_variable(np.array([[1, 2], [3, 4]])) B = pt.as_tensor_variable(np.array([[5, 6], [7, 8]]))
result = block_diagonal(A, B, name=’X’) print(result.eval()) Out: array([[1, 2, 0, 0],
[3, 4, 0, 0], [0, 0, 5, 6], [0, 0, 7, 8]])
- pytensor.tensor.slinalg.cholesky(x: TensorLike, lower: bool = True, *, check_finite: bool = False, overwrite_a: bool = False, on_error: Literal['raise', 'nan'] = 'raise')[source]#
Return a triangular matrix square root of positive semi-definite
x
.L = cholesky(X, lower=True) implies dot(L, L.T) == X.
- Parameters:
x (tensor_like) –
lower (bool, default=True) – Whether to return the lower or upper cholesky factor
check_finite (bool, default=False) – Whether to check that the input matrix contains only finite numbers.
overwrite_a (bool, ignored) – Whether to use the same memory for the output as
a
. This argument is ignored, and is present here only for consistency with scipy.linalg.cholesky.on_error (['raise', 'nan']) – If on_error is set to ‘raise’, this Op will raise a
scipy.linalg.LinAlgError
if the matrix is not positive definite. If on_error is set to ‘nan’, it will return a matrix containing nans instead.
- Returns:
Lower or upper triangular Cholesky factor of
x
- Return type:
Example
import pytensor import pytensor.tensor as pt import numpy as np x = pt.tensor('x', shape=(5, 5), dtype='float64') L = pt.linalg.cholesky(x) f = pytensor.function([x], L) x_value = np.random.normal(size=(5, 5)) x_value = x_value @ x_value.T # Ensures x is positive definite L_value = f(x_value) assert np.allclose(L_value @ L_value.T, x_value)
- pytensor.tensor.slinalg.solve(a, b, *, assume_a='gen', lower=False, check_finite=True, b_ndim: Optional[int] = None)[source]#
Solves the linear equation set
a * x = b
for the unknownx
for squarea
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 aregeneric matrix
‘gen’
symmetric
‘sym’
hermitian
‘her’
positive definite
‘pos’
If omitted,
'gen'
is the default structure.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.
- Parameters:
a ((..., N, N) array_like) – Square input data
b ((..., N, NRHS) array_like) – Input data for the right hand side.
lower (bool, optional) – If True, only the data contained in the lower triangle of
a
. Default is to use upper triangle. (ignored for'gen'
)check_finite (bool, optional) – 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 explained above.
b_ndim (int) – Whether the core case of b is a vector (1) or matrix (2). This will influence how batched dimensions are interpreted.
- pytensor.tensor.slinalg.solve_continuous_lyapunov(A: Union[Variable, Sequence[Variable], ArrayLike], Q: Union[Variable, Sequence[Variable], ArrayLike]) TensorVariable [source]#
Solve the continuous Lyapunov equation \(A X + X A^H + Q = 0\).
- Parameters:
A (TensorLike) – Square matrix of shape
N x N
.Q (TensorLike) – Square matrix of shape
N x N
.
- Returns:
X – Square matrix of shape
N x N
- Return type:
- pytensor.tensor.slinalg.solve_discrete_are(A: Union[Variable, Sequence[Variable], ArrayLike], B: Union[Variable, Sequence[Variable], ArrayLike], Q: Union[Variable, Sequence[Variable], ArrayLike], R: Union[Variable, Sequence[Variable], ArrayLike], enforce_Q_symmetric: bool = False) TensorVariable [source]#
Solve the discrete Algebraic Riccati equation \(A^TXA - X - (A^TXB)(R + B^TXB)^{-1}(B^TXA) + Q = 0\).
Discrete-time Algebraic Riccati equations arise in the context of optimal control and filtering problems, as the solution to Linear-Quadratic Regulators (LQR), Linear-Quadratic-Guassian (LQG) control problems, and as the steady-state covariance of the Kalman Filter.
Such problems typically have many solutions, but we are generally only interested in the unique stabilizing solution. This stable solution, if it exists, will be returned by this function.
- Parameters:
A (TensorLike) – Square matrix of shape M x M
B (TensorLike) – Square matrix of shape M x M
Q (TensorLike) – Symmetric square matrix of shape M x M
R (TensorLike) – Square matrix of shape N x N
enforce_Q_symmetric (bool) – If True, the provided Q matrix is transformed to 0.5 * (Q + Q.T) to ensure symmetry
- Returns:
X – Square matrix of shape M x M, representing the solution to the DARE
- Return type:
- pytensor.tensor.slinalg.solve_discrete_lyapunov(A: Union[Variable, Sequence[Variable], ArrayLike], Q: Union[Variable, Sequence[Variable], ArrayLike], method: Literal['direct', 'bilinear'] = 'bilinear') TensorVariable [source]#
Solve the discrete Lyapunov equation \(A X A^H - X = Q\).
- Parameters:
A (TensorLike) – Square matrix of shape N x N
Q (TensorLike) – Square matrix of shape N x N
method (str, one of
"direct"
or"bilinear"
) –Solver method used, .
"direct"
solves the problem directly via matrix inversion. This has a pure PyTensor implementation and can thus be cross-compiled to supported backends, and should be preferred whenN
is not large. The direct method scales poorly with the size ofN
, and the bilinear can beused in these cases.
- Returns:
X – Square matrix of shape
N x N
. Solution to the Lyapunov equation- Return type:
- pytensor.tensor.slinalg.solve_triangular(a: TensorVariable, b: TensorVariable, *, trans: int | str = 0, lower: bool = False, unit_diagonal: bool = False, check_finite: bool = True, b_ndim: Optional[int] = None) TensorVariable [source]#
Solve the equation
a x = b
forx
, assuminga
is a triangular matrix.- Parameters:
a – Square input data
b – Input data for the right hand side.
lower (bool, optional) – Use only data contained in the lower triangle of
a
. Default is to use upper triangle.trans ({0, 1, 2, 'N', 'T', 'C'}, optional) – Type of system to solve: trans system 0 or ‘N’ a x = b 1 or ‘T’ a^T x = b 2 or ‘C’ a^H x = b
unit_diagonal (bool, optional) – If True, diagonal elements of
a
are assumed to be 1 and will not be referenced.check_finite (bool, optional) – 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.
b_ndim (int) – Whether the core case of b is a vector (1) or matrix (2). This will influence how batched dimensions are interpreted.