# Evaluate integral on many points (Cubature.jl ?)

**URL:** <https://discourse.julialang.org/t/evaluate-integral-on-many-points-cubature-jl/1723>\
**Category:** Numerics\
**Created:** [January 27, 2017, 6:48pm UTC](https://discourse.julialang.org/t/evaluate-integral-on-many-points-cubature-jl/1723 "2017-01-27T18:48:42Z")\
**Posts on this page:** 5\
**Page:** 3

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 19, 2017, 6:31pm UTC](https://discourse.julialang.org/t/evaluate-integral-on-many-points-cubature-jl/1723/41 "2017-02-19T18:31:33Z")

</div>

> [@dlfivefifty](#):
>
> Not necessarily for non-polynomial functions:

The article you link compares Clenshaw-Curtis to GQ, while I was comparing GQ to the naive approach of “simply evaluate the function a hundred or so times along an interval, and then fit a high order polynomial via regression”. The article you link is super-interesting, but I still think that GQ dominates the solution suggested (even though Clenshaw-Curtis may be better).

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [February 19, 2017, 8:46pm UTC](https://discourse.julialang.org/t/evaluate-integral-on-many-points-cubature-jl/1723/42 "2017-02-19T20:46:08Z")

</div>

> [@Tamas\_Papp](#):
>
> he article you link is super-interesting, but I still think that GQ dominates the solution suggested (even though Clenshaw-Curtis may be better).

The article just says the errors are comparable. Since FastGaussQuadrature.jl is O(n) while Clenshaw-Curtis is O(n\*log n), which one is better depends on n.

(ApproxFun is essentially doing adaptive Clenshaw-Curtis when you use it for integrals. Note that when you have a `Fun` The transform step has been done so it goes down to O(n).)

---

<div class="post-metadata">

**Author:** ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)\
**Post date:** [February 21, 2017, 1:56pm UTC](https://discourse.julialang.org/t/evaluate-integral-on-many-points-cubature-jl/1723/43 "2017-02-21T13:56:49Z")

</div>

Thank you very much! Will try to read.

---

<div class="post-metadata">

**Author:** ![MikaelSlevinsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikaelslevinsky/32/38545_2.png) [@MikaelSlevinsky](https://discourse.julialang.org/u/MikaelSlevinsky)\
**Post date:** [July 23, 2017, 10:22pm UTC](https://discourse.julialang.org/t/evaluate-integral-on-many-points-cubature-jl/1723/44 "2017-07-23T22:22:38Z")

</div>

> [@stevengj](#):
>
> It sounds like the exponential length increase comes because you are including the same expression more than once, which also hurts performance (unless the compiler can eliminate the common subexpressions). One thing you could do is to assign each expression to a temporary variable before using it. Alternatively, just overwrite the variables exactly as you would in a loop (but without the runtime loop, just an unrolled sequence of statements).

I realize I’m a little late to the conversation, but I just thought I’d add that I was aware that the @clenshaw macro in ApproxFun grew the intermediate expressions exponentially (before the compiler untangles it and produces nice O(n) machine code). I [posted](https://groups.google.com/d/topic/julia-users/d6rD23ySBN0/discussion) in the old julia-users group to seek help as it was just a personal experiment with meta-programming.

If a good solution is available, a PR in ApproxFun would be appreciated!

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [July 26, 2017, 2:55pm UTC](https://discourse.julialang.org/t/evaluate-integral-on-many-points-cubature-jl/1723/45 "2017-07-26T14:55:54Z")

</div>

Sure, I believe the code snippet I posted above works and is faster than a simple function, I’ll test a bit more and make a PR.

EDIT: [here](https://github.com/JuliaApproximation/ApproxFun.jl/pull/514) it is.

[Previous page](https://discourse.julialang.org/t/evaluate-integral-on-many-points-cubature-jl/1723.md?page=2)
