Skip to content
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

Work around world age error for Duals in exp_generic #43

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ authors = ["Chris Rackauckas <[email protected]>"]
version = "1.7.0"

[deps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
ForwardDiff = "0.10"
Requires = "1.0"
julia = "1"

Expand Down
2 changes: 1 addition & 1 deletion src/ExponentialUtilities.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ExponentialUtilities
using LinearAlgebra, SparseArrays, Printf, Requires
using LinearAlgebra, SparseArrays, Printf, Requires, ForwardDiff

"""
@diagview(A,d) -> view of the `d`th diagonal of `A`.
Expand Down
16 changes: 10 additions & 6 deletions src/exp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,22 @@ function _exp!(A::StridedMatrix{T}; caches=nothing) where T <: LinearAlgebra.Bla
end

"""
exp(x, vk=Val{13}())
Generic exponential function, working on any `x` for which the functions
`LinearAlgebra.opnorm`, `+`, `*`, `^`, and `/` (including addition with
UniformScaling objects) are defined. Use the argument `vk` to adjust the
exp(x, vk=Val{13}())
Generic exponential function, working on any `x` for which the functions
`LinearAlgebra.opnorm`, `+`, `*`, `^`, and `/` (including addition with
UniformScaling objects) are defined. Use the argument `vk` to adjust the
number of terms used in the Pade approximants at compile time.

See "The Scaling and Squaring Method for the Matrix Exponential Revisited"
by Higham, Nicholas J. in 2005 for algorithm details.
"""
function exp_generic(x, vk=Val{13}())
function exp_generic(x, vk=Val{13}())
nx = opnorm(x, 1)
s = ceil(Int, log2(nx))
nxl2 = log2(nx)
if iszero(nx)
return x + oneunit(x)
end
s = ceil(Int, nxl2)
if s >= 1
exp_generic(x/(2^s), vk)^(2^s)
else
Expand Down
17 changes: 15 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,34 @@ using ExponentialUtilities: getH, getV, _exp!
@test A2 ≈ expA2
end

@testset "exp_generic" begin
@testset "exp_generic" begin
for n in [5, 10, 30, 50, 100, 500]
M = rand(n, n)
@test exp(M) ≈ exp_generic(M)

M′ = M / 10opnorm(M, 1)
@test exp(M′) ≈ exp_generic(M′)

N = randn(n, n)
@test exp(N) ≈ exp_generic(N)

exp(n) ≈ exp_generic(n)
end
end

# The issue was only triggered if exp_generic had been called before loading
Copy link

Choose a reason for hiding this comment

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

This should be incorrect. What matters is the load order, while it's a bug if the call order is observable.

Copy link
Member

Choose a reason for hiding this comment

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

how'd you find this? 😆 I think we can just straight avoid the issue for the most common case (#45) and handle the more general thing later.

Copy link

Choose a reason for hiding this comment

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

Someone put out the bat signal 🙃

# ForwardDiff so it's importnat that the loading happens after the "exp_generic"
# testset
using ForwardDiff
@testset "Issue 41" begin
@test ForwardDiff.derivative(exp_generic, 0.1) == exp_generic(0.1)
end

@testset "Issue 42" begin
@test exp_generic(0.0) == 1
@test ForwardDiff.derivative(exp_generic, 0.0) == 1
end

@testset "Phi" begin
# Scalar phi
K = 4
Expand Down