An intelligent block matrix library for numpy, PyTorch, and beyond.

bamos, updated 🕥 2022-01-22 00:44:28

Block • License PyPi

An intelligent block matrix library for numpy, PyTorch, and beyond. Crafted by Brandon Amos with significant contributions by Eric Wong.


Why do we need an intelligent block matrix library?

Let's try to construct the KKT matrix from Mattingley and Boyd's CVXGEN paper in numpy and PyTorch:

Without block, there is no way to infer the appropriate sizes of the zero and identity matrix blocks. It is an inconvenience to think about what size these matrices should be.

What does block do?

Block acts a lot like np.bmat and replaces:

  • Any constant with an appropriately shaped block matrix filled with that constant.
  • The string 'I' with an appropriately shaped identity matrix.
  • The string '-I' with an appropriately shaped negated identity matrix.
  • [Request more features.]

Isn't constructing large block matrices with a lot of zeros inefficient?

Yes, block is meant to be a quick prototyping tool and there's probably a more efficient way to solve your system if it has a lot of zeros or identity elements.

How does block handle numpy and PyTorch with the same interface?

I wrote the logic to handle matrix sizing to be agnostic of the matrix library being used. numpy and PyTorch are just backends. More backends can easily be added for your favorite Python matrix library.

```Python class Backend(metaclass=ABCMeta):

@abstractmethod
def extract_shape(self, x): pass

@abstractmethod
def build_eye(self, n): pass

@abstractmethod
def build_full(self, shape, fill_val): pass

@abstractmethod
def build(self, rows): pass

@abstractmethod
def is_complete(self, rows): pass

```

Getting Started

  • Install: pip install block
  • Usage: from block import block
  • Run tests in test.py: nosetests test.py

Issues and Contributions

I'd be happy to hear from you about any issues or features you add, please file an issue or send in a PR.

Licensing

This repository is Apache-licensed.

Issues

Request Support For the Newest Pytorch Version

opened on 2020-03-24 17:02:55 by rychong

This package pytorch_fft is very usefull and important to me. The fft of pytorch torch.fft ignore the batch dims, thus it run very slowly. While i need apply fft to many matrix, only this package can do this.

But when i install this package, it show that "torch.utils.ffi is deprecated. Please use cpp extensions instead". It can run in Pythorch 0.4, but it doesn't suppert the newest version of Pytorch, I hope you can fixed this problem, thus we can replace torch.fft with this package in the newest version of pytorch.

Add a `dim`/`axis` arg to support batched tensors

opened on 2019-04-24 21:47:41 by bamos None

Treat 1d numpy arrays as row or column vectors depending on use

opened on 2018-01-19 15:32:10 by bamos None

Infer more information from identity blocks.

opened on 2017-02-17 01:19:00 by bamos
  • block(((A,'I'),(0,0))) currently returns A

CVXPY

opened on 2017-02-10 09:05:36 by take98

Supporting for the bmat of CVXPY (http://www.cvxpy.org/en/latest/tutorial/functions/index.html?highlight=bmat) would be very useful.

Brandon Amos
GitHub Repository

pytorch numpy deep-learning linear-algebra