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

Gaussian quadrature #141

Merged
merged 76 commits into from
Apr 19, 2023
Merged

Gaussian quadrature #141

merged 76 commits into from
Apr 19, 2023

Conversation

elastufka
Copy link
Contributor

Add Gaussian quadrature methods

Base Gaussian quadrature class created

Several Gaussian quadrature methods implemented, for integration over [a,b], [0,inf] and [-inf,inf]

Minor changes to BaseIntegrator to allow for args for input functions

Resolved Issues

How Has This Been Tested?

  • See examples in docstrings

How Has This Not Been Tested?

All except Gauss-legendre have not been tested on the GPU. Most have also not been tested in multiple dimensions.

No backends besides Torch and numpy have been tested

Known issues

Finding the roots of some polynomials (Jacobi, Laguerre) use scipy functions. I don't know if/how this is compatible with autoray.do()

Transforming points and weights for integration can be tricky with the GPU involved - need to be able to check/enforce that all tensors are on the same device

Gauss-Jacobi integration does not currently give the correct solution outside the interval [-1,1]

Not yet implemented

other Gaussian methods including Gauss-Kronrod / QUADPACK

@gomezzz gomezzz changed the base branch from main to develop April 22, 2022 07:27
@gomezzz
Copy link
Collaborator

gomezzz commented Apr 22, 2022

I changed PR base as new features go into develop first 🙏

@gomezzz
Copy link
Collaborator

gomezzz commented Apr 22, 2022

Just request a review when I should review this!

@elastufka
Copy link
Contributor Author

Hi,

I can't seem to be able to request a review through the usual method. However, please feel free to take a look at your convenience.

All results for 1-dimensional integrals are correct now. You can see the tests that I did in this Colab.

Probably the integration does not work correctly for higher dimensions. When I first looked at this I was interested in vectorized integration - integrating over vectors of limits in one dimension - which is just the result of treating dims as the number of limits and not summing over all dimensions in the end. This is what quadpy does as well, and since it turns out that doing fixed-point and iterative integration in Torch is very slow even with large numbers of points, I won't be looking into it much more, or into high-dimension integration at all. (Also with the particular integral I wanted to do quickly, it looks like tensor-based backends won't help either since it is bogged down by a high-order cross-section calculation.)

Hopefully someone who has a better knowledge of how high-dimensional integration should work can quickly get it working based on the start that's been made here.

@gomezzz gomezzz self-requested a review April 28, 2022 15:10
@ilan-gold
Copy link
Collaborator

Yay!

@ilan-gold ilan-gold requested a review from gomezzz March 16, 2023 11:20
@ilan-gold
Copy link
Collaborator

I do not know what is going on with that test.

@gomezzz
Copy link
Collaborator

gomezzz commented Mar 17, 2023

I do not know what is going on with that test.

I had this problem in another repo as well, it's only the posting of the coverage comment, not sure if the thing I implemented is quite as robust as I had hoped 🥲 Will investigate soon if it continues. In any event not related to this PR, I think.

Copy link
Collaborator

@gomezzz gomezzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of mostly minor comments :)

torchquad/integration/grid_integrator.py Outdated Show resolved Hide resolved
torchquad/tests/gauss_test.py Outdated Show resolved Hide resolved
torchquad/tests/gauss_test.py Outdated Show resolved Hide resolved
torchquad/tests/gauss_test.py Outdated Show resolved Hide resolved
torchquad/tests/gauss_test.py Show resolved Hide resolved
torchquad/integration/base_integrator.py Outdated Show resolved Hide resolved
torchquad/integration/gaussian.py Show resolved Hide resolved
torchquad/integration/gaussian.py Show resolved Hide resolved
return f

def _weights(self, N, dim, backend, requires_grad=False):
return None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should never be called, right? If so, then we might as well raise an exception in case it somehow does?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this an implemented method in the base class because the Gaussian integral inherits from this and I've implemented everything here around generating the grid etc. which requires this method.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I am just thinking practically speaking this function should always be overwritten by any derived class, right? And if not, then it at least shouldn't be called. So instead of returning None and potentially failing silently if either it wasn't overwritten or called by accident we should raise an exception?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is that some integration methods have weights and some do not, but the GridIntegrator should be able to handle both. So if you prefer I can make the base class method abstract here instead of having this default behavior and then add this implementation to the NewtonCotes class instead tor return None. Let me know :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is that some integration methods have weights and some do not, but the GridIntegrator should be able to handle both. So if you prefer I can make the base class method abstract here instead of having this default behavior and then add this implementation to the NewtonCotes class instead tor return None. Let me know :)

Yes, let's do that then :)

torchquad/integration/grid_integrator.py Show resolved Hide resolved
@ilan-gold
Copy link
Collaborator

@gomezzz I generally cleaned up the tests. I also addressed the other comments and left the ones unresolved where we might have a discussion.

@gomezzz gomezzz mentioned this pull request Mar 21, 2023
@gomezzz
Copy link
Collaborator

gomezzz commented Mar 21, 2023

@ilan-gold I am trying to fix the CI in #169 but on that PR it works. Could you try to merge the branch into this one to see if I fixed it? Otherwise, my suspicion is that it maybe because the PR comes from a fork? There seems to be some permission problems as described here MishaKav/pytest-coverage-comment#68 (the mentioned repo setting is, however, correct, but maybe the one in the fork also affects this? Not quite sure yet what is going on :) )

@ilan-gold
Copy link
Collaborator

A class MC failure @gomezzz - I will re-run but posting for posterity: https://github.com/esa/torchquad/actions/runs/4530799642/jobs/7980177519?pr=141

Not really sure what to do about these MC failures in the single-dimension integral domain case. It seems like they are generally against the concept of unit-testing (since they are random). Maybe we could set a seed?

@gomezzz
Copy link
Collaborator

gomezzz commented Mar 27, 2023

Not really sure what to do about these MC failures in the single-dimension integral domain case. It seems like they are generally against the concept of unit-testing (since they are random). Maybe we could set a seed?

I think they are already seeded, but not all the frameworks provide deterministic seeding on different machines, iirc? I am afraid we may just need to increase the bounds instead 🤷

@ilan-gold
Copy link
Collaborator

@gomezzz I increased the bounds. I think everything is passing now. I have one more small PR based on https://github.com/ilan-gold/torchquad/tree/special_grid to make - basically allowing the user to disable a check and then generalizing the usage of integration_domain in a few places to use it directly instead of the two separate bounds i.e., [a,b] instead of passing a and b

@ilan-gold
Copy link
Collaborator

don't know what to say here @gomezzz about this coverage business...

@gomezzz
Copy link
Collaborator

gomezzz commented Apr 19, 2023

don't know what to say here @gomezzz about this coverage business...

Let's ignore on this PR for now, I will try to fix it later. 🙈

(logged in #171 )

@ilan-gold
Copy link
Collaborator

So we are good to merge @gomezzz?

Copy link
Collaborator

@gomezzz gomezzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! :) 💪

@ilan-gold ilan-gold merged commit cd53251 into esa:develop Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Gaussian quadrature methods
3 participants