# Fast integration of log(2^(P(x)exp(-x^2/2)+1)

**URL:** <https://discourse.julialang.org/t/fast-integration-of-log-2-p-x-exp-x-2-2-1/42762>\
**Category:** Numerics\
**Created:** [July 8, 2020, 11:39pm UTC](https://discourse.julialang.org/t/fast-integration-of-log-2-p-x-exp-x-2-2-1/42762 "2020-07-08T23:39:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [July 8, 2020, 11:39pm UTC](https://discourse.julialang.org/t/fast-integration-of-log-2-p-x-exp-x-2-2-1/42762/1 "2020-07-08T23:39:00Z")

</div>

Hello,

I would like to compute \int\_0^{x\_i}log(2^{P\_i(t)exp(-t^2/2)} + 1)dt for a large number of values x\_i where P\_i is a different polynomial (degree \<6) for each x\_i value.

The naive approach is to use the function `quadgk` provided in `QuadGK.jl` for each value x\_i and polynomial P\_i. Is there a way to speed-up these multiple integrations by reusing computation from one case to the other?

If I would like to use Gaussian quadrature (`FastGaussianQuadrature.jl`), what type of nodes would you recommend?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 9, 2020, 12:37am UTC](https://discourse.julialang.org/t/fast-integration-of-log-2-p-x-exp-x-2-2-1/42762/2 "2020-07-09T00:37:31Z")

</div>

See my answer here: [Julia integral calculation - community module or own module? - #15 by stevengj](https://discourse.julialang.org/t/julia-integral-calculation-community-module-or-own-module/24278/15)

…though if your polynomials change matters are more difficult.

Could you explain more of the context here? Where do your integrals and polynomials come from?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 9, 2020, 2:07am UTC](https://discourse.julialang.org/t/fast-integration-of-log-2-p-x-exp-x-2-2-1/42762/3 "2020-07-09T02:07:13Z")

</div>

How many is the large number and how broad is their distribution? If you get them all ahead of time, a decent approach would be to sort them, compute the integrals from `x[i-1]` to `x[i]` and add them back to get the full intervals.

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [July 9, 2020, 2:56am UTC](https://discourse.julialang.org/t/fast-integration-of-log-2-p-x-exp-x-2-2-1/42762/4 "2020-07-09T02:56:49Z")

</div>

Sorry, I should have mentioned that the samples x\_i as well as the polynomials P\_i are independent of each other. Large is about 300-400 different values of x\_i.

---

<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:** [July 9, 2020, 8:49am UTC](https://discourse.julialang.org/t/fast-integration-of-log-2-p-x-exp-x-2-2-1/42762/5 "2020-07-09T08:49:33Z")

</div>

I guess you never hit the singularity, so if you want to use Gauss quadrature then `gausslegendre ` would probably make the most sense, though it will be a different grid for each `x_i`
