torchquad icon indicating copy to clipboard operation
torchquad copied to clipboard

Add Gaussian quadrature methods

Open gomezzz opened this issue 2 years ago • 4 comments

Feature

Desired Behavior / Functionality

Currently, torchquad only implements Newton-Cotes formulas as deterministic integration methods. A logical next step would be Gaussian quadrature methods.

What Needs to Be Done

  • [ ] Decide on integrators to implement (e.g. Gauss-Legendre
  • [ ] Implement integrator in integration folder, should inherit from BaseIntegrator
  • [ ] Add docs for it
  • [ ] Add tests for it, orient at Newton-Cotes tests

How Can It Be Tested

With new tests!

gomezzz avatar Aug 20 '21 13:08 gomezzz

Hello, I'm interested in a Torch implementation of Gauss-Legendre quadrature. The code here seems clear enough so I'll see what I can do based on the examples of the existing integrators.

If you have any thoughts on potential pitfalls in such an implementation, or just remarks on things to look out for in general, I'd appreciate it! Or if you know of someone who's already started work on this, even better.

elastufka avatar Apr 07 '22 12:04 elastufka

@elastufka Great to hear! I would suggest to branch out from #137 because this introduces quite some changes in the codebase (and by following the other integrators there your implementation will also support torch, TF, jax and numpy :) )

We'll be merging #137 shortly. Nobody has started work on this to my knowledge. If you want more feedback on implementing feel free to post some thoughts where you want to add things etc.

Thanks!

gomezzz avatar Apr 07 '22 23:04 gomezzz

@gomezzz Okay, I will do that.

So far everything seems pretty straightforward. One thing that wasn't quite clear from the documentation/code - are the inputs x to the integrals preferentially tensors, or numpy arrays?

elastufka avatar Apr 08 '22 14:04 elastufka

One thing that wasn't quite clear from the documentation/code - are the inputs x to the integrals preferentially tensors, or numpy arrays?

Well the integrand function is user-defined, so inside the user could convert (will break gradient flow though). Internally, torchquad will in the new version generate the data type matching the framework. so torch.tensor , np.array etc.

There are new examples in the docs to illustrate how it works with other frameworks

Making it cross-framework compatible with autoray is very similar to just writing torch. Have a look here, e.g. https://github.com/FHof/torchquad/blob/develop/torchquad/integration/boole.py

Basically you just use anp.sum(...) instead of torch.sum(...) and from autoray import numpy as anp

gomezzz avatar Apr 08 '22 18:04 gomezzz