Skip to content

Commit 441b4ff

Browse files
committed
try to be even smarter!
1 parent 9994038 commit 441b4ff

2 files changed

Lines changed: 59 additions & 18 deletions

File tree

‎src/implementations/exponential.jl‎

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,48 @@ end
112112
# ------------
113113
function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFunctionViaTaylor)
114114
check_input(exponential!, A, expA, alg)
115+
m = LinearAlgebra.checksquare(A)
115116
T = eltype(A)
116117
R = real(T)
117118
tol = convert(R, get(alg.kwargs, :tol, eps(R)))
118119

119120
dobalance = get(alg.kwargs, :balance, false)
120121
scale = dobalance ? balance!(A)[2] : nothing
121122

122-
θ = LinearAlgebra.opnorm(A, 1)
123-
iszero(θ) && return one!(expA)
123+
# Form a minimal set of powers of A up front and use them to sharpen the norm estimate
124+
# through the Al-Mohy–Higham quantities dₚ = ‖Aᵖ‖^(1/p).
125+
p₀ = min(convert(Int, get(alg.kwargs, :estimate_order, 4)), m)
126+
powers = Vector{typeof(A)}(undef, p₀)
127+
powers[1] = A
128+
d = Vector{R}(undef, p₀)
129+
d[1] = LinearAlgebra.opnorm(A, 1)
130+
iszero(d[1]) && return one!(expA)
131+
for p in 2:p₀
132+
powers[p] = powers[p - 1] * A
133+
d[p] = LinearAlgebra.opnorm(powers[p], 1)^(1 / p)
134+
end
135+
136+
order, squarings = taylor_order_and_squarings(d, tol)
137+
blocksize = ceil(Int, sqrt(order + 1))
138+
139+
# Rescale the (≤ p₀) powers we hold into powers of A/2ˢ
140+
resize!(powers, min(p₀, blocksize))
141+
if squarings > 0
142+
f = inv(convert(T, 2)^squarings)
143+
fk = one(T)
144+
for k in 1:length(powers)
145+
fk *= f
146+
rmul!(powers[k], fk) # powers[k] ← (A/2ˢ)ᵏ
147+
end
148+
end
149+
# Extend to `blocksize` powers
150+
for p in (length(powers) + 1):blocksize
151+
push!(powers, powers[p - 1] * powers[1])
152+
end
124153

125-
order, squarings = taylor_order_and_squarings(θ, tol)
126-
squarings > 0 && rmul!(A, inv(convert(T, 2)^squarings))
127154

128-
X = taylor_polynomial(A, order)
155+
# Paterson–Stockmeyer evaluation, followed by squaring
156+
X = taylor_polynomial(powers, order)
129157
Y = expA # can reuse this memory
130158
for _ in 1:squarings
131159
mul!(Y, X, X)
@@ -142,18 +170,33 @@ end
142170

143171
# Truncation order `m` and number of squarings `s` minimizing the Paterson–Stockmeyer
144172
# matrix-multiplication count subject to the Taylor remainder bound `(θ/2ˢ)ᵐ⁺¹/(m+1)! ≤ tol`.
145-
function taylor_order_and_squarings(θ::Real, tol::Real)
146-
log2θ = log2(Float64(θ))
173+
#
174+
# The effective norm `θ` per candidate order is the sharpest Al-Mohy–Higham quantity
175+
# `αₚ = max(dₚ, dₚ₊₁)` (with `dₚ = ‖Aᵖ‖^(1/p)`) that is valid for that order: since every
176+
# `k ≥ p(p-1)` is a nonnegative combination of `p` and `p+1`, `‖Aᵏ‖ ≤ αₚᵏ` holds for `k ≥ p(p-1)`,
177+
# so `αₚ` bounds the degree-`m` remainder whenever `m+1 ≥ p(p-1)`. The `length(d)` powers already
178+
# formed count as free in the cost model.
179+
function taylor_order_and_squarings(d::AbstractVector{<:Real}, tol::Real)
180+
p₀ = length(d)
147181
log2tol = log2(Float64(tol))
148182
log2factorial = 0.0
149183
best_order = 1
150184
best_squarings = 0
151185
best_cost = typemax(Int)
152186
for order in 1:100
153187
log2factorial += log2(order + 1)
154-
squarings = max(0, ceil(Int, log2θ - (log2tol + log2factorial) / (order + 1)))
188+
# sharpest valid αₚ = min over p with p(p-1) ≤ order+1 and p+1 ≤ p₀
189+
θ = Float64(d[1])
190+
for p in 1:(p₀ - 1)
191+
p * (p - 1) ≤ order + 1 || break
192+
θ = min(θ, max(Float64(d[p]), Float64(d[p + 1])))
193+
end
194+
# `θ == 0` (a nilpotent Aᵖ) makes the remainder vanish exactly ⇒ no squarings needed;
195+
# guard the `log2(0) = -Inf` before it reaches `ceil(Int, ⋅)`.
196+
excess = log2(θ) - (log2tol + log2factorial) / (order + 1)
197+
squarings = excess > 0 ? ceil(Int, excess) : 0
155198
blocksize = ceil(Int, sqrt(order + 1))
156-
cost = (blocksize - 1) + (cld(order + 1, blocksize) - 1) + squarings
199+
cost = max(0, blocksize - p₀) + (cld(order + 1, blocksize) - 1) + squarings
157200
if cost < best_cost
158201
best_cost = cost
159202
best_order = order
@@ -164,22 +207,18 @@ function taylor_order_and_squarings(θ::Real, tol::Real)
164207
end
165208

166209
# Evaluate ∑ₖ₌₀ᵐ Aᵏ/k! via the Paterson–Stockmeyer scheme, returning a freshly allocated matrix.
167-
function taylor_polynomial(A::AbstractMatrix, order::Integer)
210+
# `powers` holds A, A², …, A^blocksize (already scaled), with blocksize = ceil(√(order+1)).
211+
function taylor_polynomial(powers::AbstractVector{<:AbstractMatrix}, order::Integer)
212+
A = powers[1]
168213
T = eltype(A)
169-
blocksize = ceil(Int, sqrt(order + 1))
214+
blocksize = length(powers)
170215

171216
invfactorial = Vector{T}(undef, order + 1)
172217
invfactorial[1] = one(T)
173218
for k in 1:order
174219
invfactorial[k + 1] = invfactorial[k] / k
175220
end
176221

177-
powers = Vector{typeof(A)}(undef, blocksize)
178-
powers[1] = A
179-
for p in 2:blocksize
180-
powers[p] = powers[p - 1] * A
181-
end
182-
183222
result = similar(A)
184223
block = similar(A)
185224
numblocks = fld(order, blocksize)

‎src/interface/matrixfunctions.jl‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ Algorithm type to denote finding the exponential of `A` via the implementation o
99
@algdef MatrixFunctionViaLA
1010

1111
"""
12-
MatrixFunctionViaTaylor(; tol=eps, balance=true)
12+
MatrixFunctionViaTaylor(; tol=eps, balance=true, estimate_order=4)
1313
1414
Algorithm type to denote finding the exponential of `A` through a pure-Julia scaling-and-squaring
1515
evaluation of its Taylor series, following Fasi & Higham (2018).
1616
The truncation order and the number of squarings are chosen to reach a relative accuracy `tol`,
1717
and the Taylor polynomial is evaluated with the Paterson–Stockmeyer scheme.
1818
When `balance` is `true`, `A` is first balanced by a diagonal similarity.
19+
`estimate_order` sets how many powers of `A` are formed up front to sharpen the norm estimate via the
20+
Al-Mohy–Higham quantities `‖Aᵖ‖^(1/p)`; these powers are reused by the Paterson–Stockmeyer evaluation.
1921
As this algorithm requires no LAPACK support, it also applies at arbitrary precision.
2022
"""
2123
@algdef MatrixFunctionViaTaylor

0 commit comments

Comments
 (0)