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 files via upload #1
base: master
Are you sure you want to change the base?
Conversation
On line 46 1 changed to one(x) On line 84 1. changed to one(z) for arbitrary precision,etc.
Thanks for editing and making the PR more readable; congratulations on your new position AA. Thanks to SJ for mentoring and encouragement. I can't benchmark against what does not exist: matlab, maxima, scipy.special, gsl do not have associated Legendre type 3 (z > 1). I have used SLegendreP3 (which I derived and used a long time ago) ; similarly SLegendreP2 gives reliable results compared to scipy.special for |x| <1 . function BenchmarkZZLegendreP2 , shows that ZZLegendreP2 is faster than scipy.special. ZZLegendreP3 comes from a modification of work published by Selezneva, etal. If the general direction of the PR is OK; then I can extract all the test... functions to test.jl and do additional work. All suggestions, comments,etc. are welcome. Thank you for helping me to learn Julia scientific computing.
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.
Thanks for editting and making the PR more readable; congratulations on your new position AA. Thanks to SJ for mentoring and encouragement.
I can't benchmark against what does not exist: matlab, maxima, scipy.special, gsl do not have associated Legendre type 3 (z > 1). I have used SLegendreP3 (which I derived and used a long time ago) ; similarly SLegendreP2 gives reliable results compared to scipy.special for |x| <1 . function BenchmarkZZLegendreP2 , shows that ZZLegendreP2 is faster than scipy.special. ZZLegendreP3 comes from a modification of work published by Selezneva, etal.
If the general direction of the PR is OK; then I can extract all the test... functions to test.jl and do additional work. All suggestions, comments,etc. are welcome. Thank you for helping me to learn Julia scientific computing.
|
||
function timePY() | ||
for i=1:100 | ||
s.lpmv(15,30,.6) |
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.
This benchmark is meaningless; when timing Python functions, you need to time computation of a whole vector at once, in order to eliminate the overhead of Python and PyCall. To minimize the overhead, you would want to do something like (using the BenchmarkTools package):
lpmv = pyimport("scipy.special")["lpmv"]
x = PyObject(collect(linspace(0.1,0.9,10^6))) # 10^6 points
m = PyObject(15)
v = PyObject(30)
@btime pycall($lpmv, PyObject, $x, $m, $v)
dblfac=j*dblfac | ||
end | ||
|
||
MXP=Int(trunc(((n+m)/2))) |
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.
Note that you can just do div(n+m,2)
solutions, w, of the differential equation areassociated Legendre functions(1-xx)d/dx(dw/dx)�-2�x�(dw/dx)+(n(n+1)-mm/(1-x*x))w=0#for m =0 they are a.k.a. Legendre polynomials #There are more than a handful of programs. Some fast and inaccurate SSLegendreP2 and 3 : The finite sum is arranged so thateach term is a multiple of the preceeding term. SLegendreP2 and 3are very slow and used for testing.The recursion equation 8.5.3 Abramowitz & Stegun (AS) Handbook ofMathematical Functions used in LegendreP2 is(n-m+1)P(n+1,m,z)=(2n+1)zP(n,m,z)- (n+m)P(n-1,m,z)ZZLegendreP2 and 3 uses a different recursionP(n,m+2,x)+2(m+1)x(s(1-x*x))^(-1/2)P(n,m+1,x)+s(n-m)(n+m+1)P(n,m,x)=0#from http://dlmf.nist.gov/14.10 equation 14.10.1 where s=sign(1-x*x) See function BenchmarkZZLegemdreP2() for speed.#These recursions are valid for Legendre functions of the first kind type 1 (?) is m=0, type 2 is -1<=x<=1 and type 3 is z> 1http://mpmath.org/doc/0.18/functions/orthogonal.html#Thus we define functions LegendreP2,....P3,....Q2,...Q3 for n and m positive integers 0<=m<=nrefer to http://dlmf.nist.gov/14.6#P(n,m,z) and Q(n,m,�z) (z> 1) are often referred to as the prolate We can define spherical harmonics (there are other definitions)Y(n,m,theta,phi)= LegendreP2(n,|m|,theta)exp(immphi)((-)^(|m|-m)/2))sqrt((2n+1)factorial(n-|m|)/(4pifactorial(n+|m|)))refer to https://en.wikipedia.org/wiki/Spherical_harmonics""" """ function LegendreP2(n::Integer,m::Integer,x::Number)
return pj1 #checking LegendreP2 against scipy.special for n< 18 the relative error is less than 2e-14the error depends on the n amd m values and x values to some extentusing PyCall @pyimport scipy.special as s """ function testLegendreP2()
end testLegendreP2() """ function SLegendreP2(n,m,x)#n,m integers
end I derived this finite sum using 8.6.6,8.6.18,8.2.5 (AS),#binomial expansion and differentiating """
end """ """ function SSLegendreP2(n::Integer,m::Integer,x::Number)#n,m integers
end #checking SSLegendreP2 against scipy.special for n< 17 the relative error is less than 3e-11the error depends on the n amd m values and x values to some extentusing PyCall @pyimport scipy.special as s """
end #checking SSLegendreP2 against Float64(SLegendreP2) for n< 17 the relative error is less than 3e-11the error depends on the n amd m values and x values to some extent""" """
end #print mylegenp3(2,1,2.)#agrees to 16 decimals I derived this finite sum using 8.6.6,8.6.18,8.2.5 (AS),#binomial expansion and differentiating function timeS2() function timeP2() the series is double speed when each term is a multiple of previous term""" """ function SSLegendreP3(n::Integer,m::Integer,x::Number)#n,m integers
end #checking SSLegendreP3 against Float64(SLegendreP3) for n< 17 the relative error is less than 4e-14the error depends on the n amd m values and x values to some extent""" based on code published ati. a. selezneva, yu. l. ratis, e. hernández, j. pérez-quiles#and p. fernández de córdoba: """ function ZZLegendreP2(n::Integer,m::Integer,z::Number) #Benchmarking ZZLegendreP2 against scipy.special using PyCall @pyimport scipy.special as s function timePY() function timeZ2() function BenchmarkZZLegendreP2() TPY= @Elapsed(timePY()) TZ2= @Elapsed(timeZ2()) #time s.lpmv(15,30,.6) 0.000674941 end #checking ZZLegendreP2 against scipy special for n< 51 the relative error is less than 6e-7the error depends on the n amd m values and x values to some extent""" #checking ZZLegendreP2 against Float64(SLegendreP2) for n< 51 the relative error is less than 6e-7the error depends on the n amd m values and x values to some extentlargest errors for n=m or n= m+1 (could be fixed?)""" derived from work published ati. a. selezneva, yu. l. ratis, e. hernández, j. pérez-quiles#and p. fernández de córdoba: """ function ZZLegendreP3(n::Integer,m::Integer,z::Number)
end #checking ZZLegendreP3 against Float64(SLegendreP3) for n< 18 the relative error is less than 4e-6the error depends on the n amd m values and x values to some extent#n=1 may have error |
M = (2*n -1) # M must be odd | ||
# (2m-1)!!= (2m)!/( m! 2^m) double factorial | ||
dblfac=1 | ||
for j=1:M |
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.
You can just do for j = 1:2:M
to loop over the odd j
.
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.
thanks
end | ||
|
||
MXP=Int(trunc(((n+m)/2))) | ||
sum= (x^(n+m))*dblfac/(((x*x - one(x))^(m/2))*prod(1:n-m)) |
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.
This could have spurious overflow (the numerator and/or denominator could overflow even though the ratio is in a representable range)...
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.
There could be a check for overflow before line 357
No description provided.