-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
Conversation
Project.toml
Outdated
|
||
[deps] | ||
CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2" | ||
FastGaussQuadrature = "442a2c76-b920-505d-bb47-c5924d526838" |
There was a problem hiding this comment.
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")
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
Fantastic! |
This is an initial implementation of Gauss-Legendre quadrature, with support for composite quadrature, i.e. I implement both
and
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
with$W_k$ and $x_k$ the weights and nodes. Perhaps a type like
could be used, with$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
C
deciding if a composite rule should be used,A
deciding if the weight function has already been applied ontoN
andW
are for the nodes and weights, respectively. Maybe the weight function could be stored directly inF
. With this, for example, myGaussLegendre
struct could build aGaussianQuadrature
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)