Skip to content

Commit f2ccad8

Browse files
authored
Add derivatives for ellipk and ellipe (#370)
* Add derivatives for `ellipk` and `ellipe` * Bump version * Fix derivatives at 0
1 parent 414385a commit f2ccad8

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "SpecialFunctions"
22
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
3-
version = "2.0.0"
3+
version = "2.1.0"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/chainrules.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ ChainRulesCore.@scalar_rule(expinti(x), exp(x) / x)
194194
ChainRulesCore.@scalar_rule(sinint(x), sinc(invπ * x))
195195
ChainRulesCore.@scalar_rule(cosint(x), cos(x) / x)
196196

197+
# elliptic integrals
198+
ChainRulesCore.@scalar_rule(
199+
ellipk(m),
200+
iszero(m) ? oftype(Ω, π) / 8 : (ellipe(m) / (1 - m) - Ω) / (2 * m),
201+
)
202+
ChainRulesCore.@scalar_rule(
203+
ellipe(m),
204+
iszero(m) ? -oftype(Ω, π) / 8 :- ellipk(m)) / (2 * m),
205+
)
206+
197207
# non-holomorphic functions
198208
function ChainRulesCore.frule((_, Δν, Δx), ::typeof(besselix), ν::Number, x::Number)
199209
# primal

src/ellip.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ For ``m<0``, followed by
3939
> <https://www.researchgate.net/publication/267330394>
4040
As suggested in this paper, the domain is restricted to ``(-\infty,1]``.
4141
"""
42-
function ellipk(m::Float64)
42+
ellipk(m::Real) = _ellipk(float(m))
43+
44+
function _ellipk(m::Float64)
4345
flag_is_m_neg = false
4446
if m < 0.0
4547
x = m / (m-1) #dealing with negative args
@@ -214,7 +216,9 @@ For ``m<0``, followed by
214216
> <https://www.researchgate.net/publication/267330394>
215217
As suggested in this paper, the domain is restricted to ``(-\infty,1]``.
216218
"""
217-
function ellipe(m::Float64)
219+
ellipe(m::Real) = _ellipe(float(m))
220+
221+
function _ellipe(m::Float64)
218222
flag_is_m_neg = false
219223
if m < 0.0
220224
x = m / (m-1) #dealing with negative args
@@ -346,11 +350,7 @@ function ellipe(m::Float64)
346350
end
347351
end
348352

349-
for f in (:ellipk,:ellipe)
350-
@eval begin
351-
($f)(x::Float16) = Float16(($f)(Float64(x)))
352-
($f)(x::Float32) = Float32(($f)(Float64(x)))
353-
($f)(x::Real) = ($f)(float(x))
354-
($f)(x::AbstractFloat) = throw(MethodError($f, (x, "")))
355-
end
353+
# Support for Float32 and Float16
354+
for internalf in (:_ellipk, :_ellipe), T in (:Float16, :Float32)
355+
@eval $internalf(x::$T) = $T($internalf(Float64(x)))
356356
end

test/chainrules.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
test_scalar(airyaiprimex, x)
3838
end
3939
end
40+
41+
if x isa Real && x < 1
42+
test_scalar(ellipk, x)
43+
test_scalar(ellipe, x)
44+
end
4045
end
4146
end
4247

0 commit comments

Comments
 (0)