# Where can I find the most widely adopted implementation of Legendre polynomials?

**URL:** <https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183>\
**Category:** Numerics\
**Created:** [June 9, 2017, 4:20pm UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183 "2017-06-09T16:20:16Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![krcools](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krcools/32/10212_2.png) [@krcools](https://discourse.julialang.org/u/krcools)\
**Post date:** [June 9, 2017, 4:20pm UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/1 "2017-06-09T16:20:16Z")

</div>

Apart from an implementation hidden somewhere in Jacobi, I could not find where this special, but quite common, function was implemented. Am I missing the obvious location? Maybe it belongs in SpecialFunctions?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 9, 2017, 9:00pm UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/2 "2017-06-09T21:00:35Z")

</div>

do either of these help?

[https://github.com/milthorpe/SphericalHarmonics.jl](https://github.com/milthorpe/SphericalHarmonics.jl)

[https://github.com/jiahao/GSL.jl](https://github.com/jiahao/GSL.jl)

[https://github.com/pjabardo/Jacobi.jl](https://github.com/pjabardo/Jacobi.jl)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 9, 2017, 9:17pm UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/3 "2017-06-09T21:17:24Z")

</div>

I wouldn’t call the function `legendre` listed in the README of [https://github.com/pjabardo/Jacobi.jl](https://github.com/pjabardo/Jacobi.jl) “hidden away”.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 9, 2017, 9:44pm UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/4 "2017-06-09T21:44:22Z")

</div>

just re-found that – nice package … I added it above

---

<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:** [June 10, 2017, 2:05am UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/5 "2017-06-10T02:05:03Z")

</div>

It requires unnecessary allocations, but you can also use ApproxFun:

```julia
f = Fun(Legendre(), [zeros(k-1);1])
f(0.1)

```

---

<div class="post-metadata">

**Author:** ![krcools](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krcools/32/10212_2.png) [@krcools](https://discourse.julialang.org/u/krcools)\
**Post date:** [June 12, 2017, 5:10pm UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/6 "2017-06-12T17:10:19Z")

</div>

Thanks everyone for the overview. SphericalHarmonics comes closest to where I expected legendre to reside. In striving for a minimal amount of dependencies I would be a bit adverse to pulling in large complex packages like GSL and Jacobi if I only wanted a single function from them.

---

<div class="post-metadata">

**Author:** ![arturgower](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arturgower/32/3967_2.png) [@arturgower](https://discourse.julialang.org/u/arturgower)\
**Post date:** [July 24, 2019, 11:33am UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/7 "2019-07-24T11:33:41Z")

</div>

Is there now a standard package for associated Legendre polynomials in Julia? This package SphericalHarmonics.jl has not been updated in over 3 years, and the relationship between associated Legendre polynomials and Jacobi polynomials is [not exactly straightforward](https://link-springer-com.manchester.idm.oclc.org/article/10.1007/s00365-017-9403-5).

Associated Legendre polynomials are necessary for [Spherical Harmonics](https://en.wikipedia.org/wiki/Spherical_harmonics), which are a corner stone of the mathematics of waves.

---

<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 24, 2019, 5:09pm UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/8 "2019-07-24T17:09:22Z")

</div>

There’s an implementation buried here: [https://github.com/JuliaApproximation/FastTransforms.jl/blob/master/src/SphericalHarmonics/sphfunctions.jl](https://github.com/JuliaApproximation/FastTransforms.jl/blob/master/src/SphericalHarmonics/sphfunctions.jl)

---

<div class="post-metadata">

**Author:** ![arturgower](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arturgower/32/3967_2.png) [@arturgower](https://discourse.julialang.org/u/arturgower)\
**Post date:** [July 25, 2019, 3:37pm UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/9 "2019-07-25T15:37:40Z")

</div>

Thanks @dlfivefifty, that is quite buried!

For now, I’ve gone with [GSL.jl](https://github.com/JuliaMath/GSL.jl). Am writing some [basic tests](https://github.com/arturgower/EffectiveWaves.jl/blob/test3d/test/specialfunctions.jl) in my package [EffectiveWaves.jl](https://github.com/arturgower/EffectiveWaves.jl) if anyone wants to see how to use GSL.jl

---

<div class="post-metadata">

**Author:** ![LeoGalPras](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leogalpras/32/14616_2.png) [@LeoGalPras](https://discourse.julialang.org/u/LeoGalPras)\
**Post date:** [September 5, 2019, 7:30am UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/10 "2019-09-05T07:30:48Z")

</div>

The easiest to use implementation I’ve found is in CMB.jl at [https://github.com/jmert/CMB.jl/blob/master/src/legendre.jl](https://github.com/jmert/CMB.jl/blob/master/src/legendre.jl) , also it looks to be well written.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [September 5, 2019, 9:16am UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/11 "2019-09-05T09:16:52Z")

</div>

I’ve cleaned it up and put it into one of my codes, but was thinking of merging it back once I’ve optimised and stabilised it. Let me know if there is any interest. This would be a much more general Spherical Harmonics package though which also computes cg-coefficients, etc.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 8, 2019, 12:44am UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/12 "2019-09-08T00:44:56Z")

</div>

interested

---

<div class="post-metadata">

**Author:** ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)\
**Post date:** [September 8, 2019, 3:00am UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/13 "2019-09-08T03:00:15Z")

</div>

Maybe it’s a bit late, but if you are interested in evaluating Legendre polynomials (and other classical orthogonal polynomials), you can also use my package [PolynomialBases.jl](https://github.com/ranocha/PolynomialBases.jl). There is a brief tutorial given as [Notebook](https://nbviewer.jupyter.org/github/ranocha/PolynomialBases.jl/blob/master/notebooks/Tutorial.ipynb), showing how to evaluate some orthogonal polynomials. However, it does not contain general associated Legendre polynomials.

---

<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:** [September 11, 2019, 9:55am UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/14 "2019-09-11T09:55:34Z")

</div>

To add to the noise: there is the work-in-progress ContinuumArrays.jl which builds on QuasiArrays.jl. The idea is to treat bases as “quasi-arrays” where the first axis is continuous. These means we can bootstrap array terminology:

```julia
julia> using ContinuumArrays

julia> P = Legendre(); # Legendre polynomials

julia> size(P) # uncountable ∞ x countable ∞
(ContinuumArrays.AlephInfinity{1}(), ∞)

julia> axes(P) # essentially (-1..1, 1:∞)
(QuasiArrays.Inclusion{Float64,DomainSets.ChebyshevInterval{Float64}}(-1.0..1.0 (Chebyshev)), OneToInf())

julia> P[0.1,1:10] # [P_0(0.1), …, P_9(0.1)]
10-element Array{Float64,1}:
  1.0                
  0.1                
 -0.485              
 -0.14750000000000002
  0.3379375          
  0.17882875         
 -0.2488293125       
 -0.19949294375000004
  0.180320721484375  
  0.21138764183593753

julia> @time P[range(-1,1; length=10_000), 1:10_000]; # construct 10_000^2 Vandermonde matrix
  1.624796 seconds (10.02 k allocations: 1.491 GiB, 6.81% gc time)

```

This also works for associated Legendre polynomials as weighted Ultraspherical polynomials:

```julia
julia> associatedlegendre(m) = ((-1)^m*prod(1:2:(2m-1)))*(UltrasphericalWeight((m+1)/2).*Ultraspherical(m+1/2))
associatedlegendre (generic function with 1 method)

julia> associatedlegendre(2)[0.1,1:10]
10-element Array{Float64,1}:
   2.9699999999999998
   1.4849999999999999
  -6.9052500000000006
  -5.041575          
  10.697754375       
  10.8479361375      
 -13.334647528125    
 -18.735466024687497 
  13.885467170308594 
  28.220563705988674 

```

It’s very experimental but happy to have help. The long term aim is to replace the internals of ApproxFun to give better performance for spherical harmonics, etc.

---

<div class="post-metadata">

**Author:** ![krcools](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krcools/32/10212_2.png) [@krcools](https://discourse.julialang.org/u/krcools)\
**Post date:** [September 11, 2019, 10:01am UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/15 "2019-09-11T10:01:44Z")

</div>

Does this use recursion to save time when values for P\_l at consecutive l are asked for?

---

<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:** [September 11, 2019, 10:30am UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/16 "2019-09-11T10:30:01Z")

</div>

Yes of course: it would never make to 10k otherwise.

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [September 11, 2019, 3:58pm UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/17 "2019-09-11T15:58:47Z")

</div>

Also interested!

---

<div class="post-metadata">

**Author:** ![jmert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmert/32/3161_2.png) [@jmert](https://discourse.julialang.org/u/jmert)\
**Post date:** [September 11, 2019, 4:59pm UTC](https://discourse.julialang.org/t/where-can-i-find-the-most-widely-adopted-implementation-of-legendre-polynomials/4183/18 "2019-09-11T16:59:33Z")

</div>

> [@LeoGalPras](#):
>
> The easiest to use implementation I’ve found is in CMB.jl at [https://github.com/jmert/CMB.jl/blob/master/src/legendre.jl](https://github.com/jmert/CMB.jl/blob/master/src/legendre.jl) , also it looks to be well written.

Hah, I didn’t see this topic come through, but that’s my package! Thanks for the kind words!

I’m glad somebody has found it useful. Unfortunately, I haven’t made any improvements to it in quite some time — I’m trying to finish my PhD, and Julia is only a hobby for me, so development has taken a back seat until I graduate.
