# Special functions , associated Legendre type 3

**URL:** https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163
**Category:** Numerics
**Created:** [February 18, 2017, 1:06am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163 "2017-02-18T01:06:12Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![elaine](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@elaine](https://discourse.julialang.org/u/elaine)
#### Post date: [February 18, 2017, 1:06am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/1 "2017-02-18T01:06:12Z")

</div>

I have some special functions. I would like to deposit them into someone’s existing / new projects. An example:

```julia
function mylegenp3(n,m,z)# n,m integers , z > 1,associated Legendre
    v=(z*z - big(1.))^(-m*big(1)/2)
      v=v*factorial(big(n+m))/(((big(2))^n)* factorial(big(n)));
    term = big(0.);
     MXP=Int(trunc(((n+m)/2)))
    for p=0:MXP
        term= term + 
        ((-big(1.))^p)*binomial(big(2*(n-p)),big(n-m))*
        (z^(big(1)*(n + m -2*p)))*
        binomial(big(n),big(p)) 
        end
    return term * v
end 

```

#🙂

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 18, 2017, 2:00am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/2 "2017-02-18T02:00:07Z")

</div>

[https://github.com/JuliaMath/SpecialFunctions.jl](https://github.com/JuliaMath/SpecialFunctions.jl) ?

---

<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: [February 18, 2017, 2:01am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/3 "2017-02-18T02:01:46Z")

</div>

Which functions? Why not put them somewhere and post the link?

---

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [February 18, 2017, 7:30am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/4 "2017-02-18T07:30:34Z")

</div>

Could you please give some references to your implementation? For instance: was any stability analysis done for this algorithm? `term` has contributions with alternating signs.

And for those like me who are not familiar with associated Legendre of type 3, what are they?

---

<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: [February 18, 2017, 12:53pm UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/5 "2017-02-18T12:53:25Z")

</div>

[GitHub - JuliaMath/SpecialFunctions.jl: Special mathematical functions in Julia](https://github.com/JuliaMath/SpecialFunctions.jl) is becoming the main repo for special functions.

Note, however, that it looks like your code can be substantially improved. For on thing, the integer type used should depend on the type of the arguments—you shouldn’t unconditionally convert to BigInt. Also, when you’re computing polynomial series, you almost never want to compute each term in the series independently (with calls to `^`, `factorial`, `binomial` etcetera). Instead, you want to compute each term as a recurrence from the previous term, analogous to Horner’s method. (This also helps to avoid overflow from the individual terms in ratios of factorials.)

See, for example, how I compute the Taylor series for the exponential integral in this notebook: [https://github.com/mitmath/18S096/blob/iap2017/pset3/pset3-solutions.ipynb](https://github.com/mitmath/18S096/blob/iap2017/pset3/pset3-solutions.ipynb)

---

<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: [February 18, 2017, 12:56pm UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/6 "2017-02-18T12:56:45Z")

</div>

And you should always look at the literature on specific special functions to see if there are better ways to compute them than just plugging in one of the definitions. For example, for associated Legendre polynomials there are [recurrence relations](https://en.wikipedia.org/wiki/Associated_Legendre_polynomials#Recurrence_formula) that I’m guessing are a better method to evaluate them.

Another good thing to do is to compare to existing implementations, like the ones in GSL or SciPy. If your code is much slower then you are probably making a mistake.

---

<div class="post-metadata">

### Author: ![elaine](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@elaine](https://discourse.julialang.org/u/elaine)
#### Post date: [February 19, 2017, 6:49am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/7 "2017-02-19T06:49:39Z")

</div>

References are Abramowitz and Stegun, Handbook of Mathematical Functions, also online Mathematica function reference, wikipedia, and python implementation.

---

<div class="post-metadata">

### Author: ![elaine](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@elaine](https://discourse.julialang.org/u/elaine)
#### Post date: [February 19, 2017, 6:49am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/8 "2017-02-19T06:49:41Z")

</div>

I did unconditionally convert to BigInt because when dealing with factorials, things can get humungous. I used arbitrary precision for accuracy, not speed. Faster computation would use recurrence and your methods.

---

<div class="post-metadata">

### Author: ![elaine](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@elaine](https://discourse.julialang.org/u/elaine)
#### Post date: [February 19, 2017, 6:49am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/9 "2017-02-19T06:49:42Z")

</div>

Thanks for your input and guidance. My code is only accurate but never checked for speed.

---

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [February 19, 2017, 8:07am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/10 "2017-02-19T08:07:47Z")

</div>

I could not find any reference to “type 3” or the third kind. Do you have a link?

---

<div class="post-metadata">

### Author: ![elaine](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@elaine](https://discourse.julialang.org/u/elaine)
#### Post date: [February 19, 2017, 9:24am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/11 "2017-02-19T09:24:51Z")

</div>

[http://mpmath.org/doc/0.18/functions/orthogonal.html](http://mpmath.org/doc/0.18/functions/orthogonal.html)

---

<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: [February 19, 2017, 9:16pm UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/12 "2017-02-19T21:16:36Z")

</div>

> [@elaine](#):
>
> I did unconditionally convert to BigInt because when dealing with factorials, things can get humungous.

The trick is to arrange a recurrence carefully so that you can avoid overflow without resorting to bignum arithmetic. (Even though the individual factorial terms can be huge, the overall term in the series is usually easily within floating-point range.)

Performance is a big concern with special function implementations, because the whole point of using special functions is usually to gain performance over generic methods like quadrature etc. So it is really useful to compare your implementation to other implementations.

---

<div class="post-metadata">

### Author: ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)
#### Post date: [February 21, 2017, 12:31pm UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/13 "2017-02-21T12:31:39Z")

</div>

I have a package that uses recurrence relations to calculate Jacobi, Legendre and Chebyshev polynomials.

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

I think that they would be more useful in the SpecialFunctions.jl package.

---

<div class="post-metadata">

### Author: ![elaine](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@elaine](https://discourse.julialang.org/u/elaine)
#### Post date: [February 27, 2017, 5:53am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/14 "2017-02-27T05:53:55Z")

</div>

Matlab doesn’t calculate type 3 (z\>1) function but does type 2 (-1\<=x\<.=1) function ; timing is approximately .0007sec compared to average .00014sec recNM3(n,m,z) function  
function recNM3(n,m,z) #n,m positive integers, z number  
#reliable for values of n \<= 17 for z=2.  
# 0 \<= m \<= n  
# z \> 1.  
M = (2_m -1) # M must be odd  
# (2m-1)!!= (2m)!/( m! 2^m) double factorial  
dblfac=1  
for j=1:M  
if iseven(j)  
continue  
end  
dblfac=j_dblfac  
end  
if n == m  
return( dblfac\*(z_z -1)^(m/2))  
elseif n == m+1  
return((2.m +1.)zdblfac(z_z -1)^(m/2))  
end  
pj2=dblfac\*(z_z -1.)^(m/2)  
pj1=z_(2.\*m+1.)_pj2  
for j = m+2 :n  
pjj=(z_(2.\*j-1.)_pj1 - pj2_(j +m-1.)) /(j-m)  
pj2=pj1  
pj1=pjj  
end  
return pj1  
end

---

<div class="post-metadata">

### Author: ![elaine](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@elaine](https://discourse.julialang.org/u/elaine)
#### Post date: [April 2, 2017, 8:34am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/15 "2017-04-02T08:34:48Z")

</div>

[https://github.com/elaineVRC/SpecialFunctions.jl/pull/1/files#diff-dd20ac38491c559ae133f7d9b22648ce](https://github.com/elaineVRC/SpecialFunctions.jl/pull/1/files#diff-dd20ac38491c559ae133f7d9b22648ce)

---

<div class="post-metadata">

### Author: ![elaine](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@elaine](https://discourse.julialang.org/u/elaine)
#### Post date: [May 23, 2017, 12:51am UTC](https://discourse.julialang.org/t/special-functions-associated-legendre-type-3/2163/16 "2017-05-23T00:51:33Z")

</div>

I combined 8.6.6,.8.6.18, 8.2.5 (Abramaowitz & Stegun), used binomial expansion and differentiated to get a Finite sum.  
Markdown :  
A representation of P|σ|μ(z)(z2−1)|σ|/2 is used by combining 8.6.6,8.6.18,8.2.5 (Ref. 14),  
P|σ|μ(z)=(μ+|σ|)!(z2−1)−|σ|/22μμ!(μ−|σ|)!dμ−|σ|dzμ−|σ|(z2−1)μ using binomial expansion and differentiating} ;  
P|σ|μ(z)(z2−1)|σ|/2=(μ+|σ|)!2μμ!∑[μ+|σ|2]p=0(−)p(2μ−2pμ−|σ|)(μp)zμ+|σ|−2p
