Skip to content

Commit

Permalink
Add derivatives for ellipk and ellipe (#370)
Browse files Browse the repository at this point in the history
* Add derivatives for `ellipk` and `ellipe`

* Bump version

* Fix derivatives at 0
  • Loading branch information
devmotion authored Jan 26, 2022
1 parent 414385a commit f2ccad8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "SpecialFunctions"
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
version = "2.0.0"
version = "2.1.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
10 changes: 10 additions & 0 deletions src/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ ChainRulesCore.@scalar_rule(expinti(x), exp(x) / x)
ChainRulesCore.@scalar_rule(sinint(x), sinc(invπ * x))
ChainRulesCore.@scalar_rule(cosint(x), cos(x) / x)

# elliptic integrals
ChainRulesCore.@scalar_rule(
ellipk(m),
iszero(m) ? oftype(Ω, π) / 8 : (ellipe(m) / (1 - m) - Ω) / (2 * m),
)
ChainRulesCore.@scalar_rule(
ellipe(m),
iszero(m) ? -oftype(Ω, π) / 8 :- ellipk(m)) / (2 * m),
)

# non-holomorphic functions
function ChainRulesCore.frule((_, Δν, Δx), ::typeof(besselix), ν::Number, x::Number)
# primal
Expand Down
18 changes: 9 additions & 9 deletions src/ellip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ For ``m<0``, followed by
> <https://www.researchgate.net/publication/267330394>
As suggested in this paper, the domain is restricted to ``(-\infty,1]``.
"""
function ellipk(m::Float64)
ellipk(m::Real) = _ellipk(float(m))

function _ellipk(m::Float64)
flag_is_m_neg = false
if m < 0.0
x = m / (m-1) #dealing with negative args
Expand Down Expand Up @@ -214,7 +216,9 @@ For ``m<0``, followed by
> <https://www.researchgate.net/publication/267330394>
As suggested in this paper, the domain is restricted to ``(-\infty,1]``.
"""
function ellipe(m::Float64)
ellipe(m::Real) = _ellipe(float(m))

function _ellipe(m::Float64)
flag_is_m_neg = false
if m < 0.0
x = m / (m-1) #dealing with negative args
Expand Down Expand Up @@ -346,11 +350,7 @@ function ellipe(m::Float64)
end
end

for f in (:ellipk,:ellipe)
@eval begin
($f)(x::Float16) = Float16(($f)(Float64(x)))
($f)(x::Float32) = Float32(($f)(Float64(x)))
($f)(x::Real) = ($f)(float(x))
($f)(x::AbstractFloat) = throw(MethodError($f, (x, "")))
end
# Support for Float32 and Float16
for internalf in (:_ellipk, :_ellipe), T in (:Float16, :Float32)
@eval $internalf(x::$T) = $T($internalf(Float64(x)))
end
5 changes: 5 additions & 0 deletions test/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
test_scalar(airyaiprimex, x)
end
end

if x isa Real && x < 1
test_scalar(ellipk, x)
test_scalar(ellipe, x)
end
end
end

Expand Down

2 comments on commit f2ccad8

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/53219

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.1.0 -m "<description of version>" f2ccad81b957f295ed209f973e61fd8b13882c5d
git push origin v2.1.0

Please sign in to comment.