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 and C, 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.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 unknown x for square a 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 are

generic 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: TensorLike, Q: TensorLike) TensorVariable[source]#

Solve the continuous Lyapunov equation \(A X + X A^H + Q = 0\).

Parameters:
  • A – Square matrix of shape N x N; must have the same shape as Q.

  • Q – Square matrix of shape N x N; must have the same shape as A.

Returns:

  • Square matrix of shape N x N, representing the solution to the

  • Lyapunov equation

pytensor.tensor.slinalg.solve_discrete_are(A, B, Q, R, enforce_Q_symmetric=False) TensorVariable[source]#

Solve the discrete Algebraic Riccati equation \(A^TXA - X - (A^TXB)(R + B^TXB)^{-1}(B^TXA) + Q = 0\).

Parameters:
  • A (ArrayLike) – Square matrix of shape M x M

  • B (ArrayLike) – Square matrix of shape M x M

  • Q (ArrayLike) – Symmetric square matrix of shape M x M

  • R (ArrayLike) – 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:

pt.matrix

pytensor.tensor.slinalg.solve_discrete_lyapunov(A: TensorLike, Q: TensorLike, method: Literal['direct', 'bilinear'] = 'direct') TensorVariable[source]#

Solve the discrete Lyapunov equation \(A X A^H - X = Q\).

Parameters:
  • A – Square matrix of shape N x N; must have the same shape as Q

  • Q – Square matrix of shape N x N; must have the same shape as A

  • method – Solver method used, one of "direct" or "bilinear". "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 when N is not large. The direct method scales poorly with the size of N, and the bilinear can be used in these cases.

Returns:

  • Square matrix of shape N x N, representing the solution to the

  • Lyapunov equation

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 for x, assuming a 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.