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 Gauss-Legendre quadrature #147

Merged
merged 5 commits into from
Feb 19, 2023
Merged

Add Gauss-Legendre quadrature #147

merged 5 commits into from
Feb 19, 2023

Conversation

DanielVandH
Copy link
Member

@DanielVandH DanielVandH commented Feb 17, 2023

This is an initial implementation of Gauss-Legendre quadrature, with support for composite quadrature, i.e. I implement both

$$ \int_a^b f(x)~\mathrm{dx} \approx \frac{b-a}{2}\sum_{k=1}^n A_kf\left(\frac{b-a}{2}x_k + \frac{b+a}{2}\right) $$

and

$$ \int_a^b f(x)~\mathrm{dx} \approx \frac{h}{2}\sum_{j=2}^m \sum_{k=1}^n A_k f\left(\frac{h}{2}\xi_k + \frac{x_{j-1} + x_j}{2}\right), $$

where $A_k = 2/[(1-\xi_k^2)[P_n'(\xi_k)]^2]$ are the quadrature weights and $\xi_k$ are the roots of the nth degree Legendre polynomial $P_n$. In the latter case, $[a, b]$ is split into $m-1$ subintervals $[x_{j-1}, x_j]$, $j=2,\ldots,m$, each of equal width $h = (b-a)/(m-1)$ and Gauss-Legendre quadrature is applied on each interval separately.

Some other thoughts for more types of Gaussian quadrature are below, but the above is perhaps enough for now to get a feel for what it should look like.

I don't think it would be too difficult to make this more general for other types of Gauss Quadrature, defining an approach e.g. for evaluating

$$ \int_a^b w(x) f(x)~\mathrm{dx} \approx \sum_{k=1}^n W_k f(x_k) $$

with $W_k$ and $x_k$ the weights and nodes. Perhaps a type like

abstract type AbstractGaussianQuadrature{C,A,F,N,W} end

could be used, with C deciding if a composite rule should be used, A deciding if the weight function has already been applied onto $f$ (if not, we need to define $g(x) = f(x)/w(x)$ so that we have an integrand of the form $w(x) g(x)$), and N and W are for the nodes and weights, respectively. Maybe the weight function could be stored directly in F. With this, for example, my GaussLegendre struct could build a GaussianQuadrature type directly - the actual code for applying the rule is the same in all cases. Could obviously keep going with e.g. Newton-Cotes rules and error estimates but that's not for this PR..

An important issue to consider would be avoiding mapping away from infinity if the quadrature rule calls for it, or mapping into infinity if needed (e.g. Gauss-Hermite or Gauss-Laguerre)

Project.toml Outdated

[deps]
CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
FastGaussQuadrature = "442a2c76-b920-505d-bb47-c5924d526838"
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be made into an extension package (like the other solvers will be "soon")

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I've now addressed this. I've never worked with extensions before so maybe I've done some things in a weird way..

I commented out Lines 3-21 of the new test file gaussian_quadrature_tests.jl since I couldn't see how to get the functions gauss_legendre and composite_gauss_legendre out of the extension file. They are covered by Lines 23 onward though, so maybe it's OK?

function gauss_legendre(f, p, lb, ub, nodes, weights)
scale = (ub - lb) / 2
shift = (lb + ub) / 2
scaled_f = s -> f(scale * s + shift, p)
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to define the closure here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've changed it now - is this what you mean to have instead?

Project.toml Outdated Show resolved Hide resolved
@ChrisRackauckas
Copy link
Member

Fantastic!

@ChrisRackauckas ChrisRackauckas merged commit f7a60d9 into SciML:master Feb 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.

3 participants