Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper for constructing block matrices #1100

Open
jessegrabowski opened this issue Nov 22, 2024 · 0 comments
Open

Add helper for constructing block matrices #1100

jessegrabowski opened this issue Nov 22, 2024 · 0 comments
Labels
enhancement New feature or request feature request help wanted Extra attention is needed linalg Linear algebra NumPy compatibility

Comments

@jessegrabowski
Copy link
Member

jessegrabowski commented Nov 22, 2024

Description

Numpy has np.block, which is gives a nice shorthand for repeated concatenations. For example, say I want to make a block lower-triangle matrix:

$$ D = \begin{bmatrix} A & 0 \\ B & C \end{bmatrix} $$

I can do:

import numpy 
A, B, C = np.ones((3, 4, 4))
B *= 2
C *= 3

zeros = np.zeros((4, 4))
D = np.block([[A, zeros], [B, C]])
print(D)

# Result:
[[1. 1. 1. 1. 0. 0. 0. 0.]
 [1. 1. 1. 1. 0. 0. 0. 0.]
 [1. 1. 1. 1. 0. 0. 0. 0.]
 [1. 1. 1. 1. 0. 0. 0. 0.]
 [2. 2. 2. 2. 3. 3. 3. 3.]
 [2. 2. 2. 2. 3. 3. 3. 3.]
 [2. 2. 2. 2. 3. 3. 3. 3.]
 [2. 2. 2. 2. 3. 3. 3. 3.]]

Obviously you can do this in a million different ways, with np.hstack, np.vstack, np.c_, np.r_r, np.concat, np.stack, etc, etc. But this one is concise and readable.

In the content of pytensor, it's related to rewrites in the same vein as #1044. We could very easily break apart these block matrices and do the matmul in chunks, especially if we see there are zero matrices among the blocks.

@jessegrabowski jessegrabowski added enhancement New feature or request help wanted Extra attention is needed feature request linalg Linear algebra labels Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature request help wanted Extra attention is needed linalg Linear algebra NumPy compatibility
Projects
None yet
Development

No branches or pull requests

2 participants