@@ -119,20 +119,48 @@ end
119119# ------------
120120function exponential! (A:: AbstractMatrix , expA:: AbstractMatrix , alg:: MatrixFunctionViaTaylor )
121121 check_input (exponential!, A, expA, alg)
122+ m = LinearAlgebra. checksquare (A)
122123 T = eltype (A)
123124 R = real (T)
124125 tol = convert (R, get (alg. kwargs, :tol , eps (R)))
125126
126127 dobalance = get (alg. kwargs, :balance , false )
127128 scale = dobalance ? balance! (A)[2 ] : nothing
128129
129- θ = LinearAlgebra. opnorm (A, 1 )
130- iszero (θ) && return one! (expA)
130+ # Form a minimal set of powers of A up front and use them to sharpen the norm estimate
131+ # through the Al-Mohy–Higham quantities dₚ = ‖Aᵖ‖^(1/p).
132+ p₀ = min (convert (Int, get (alg. kwargs, :estimate_order , 4 )), m)
133+ powers = Vector {typeof(A)} (undef, p₀)
134+ powers[1 ] = A
135+ d = Vector {R} (undef, p₀)
136+ d[1 ] = LinearAlgebra. opnorm (A, 1 )
137+ iszero (d[1 ]) && return one! (expA)
138+ for p in 2 : p₀
139+ powers[p] = powers[p - 1 ] * A
140+ d[p] = LinearAlgebra. opnorm (powers[p], 1 )^ (1 / p)
141+ end
142+
143+ order, squarings = taylor_order_and_squarings (d, tol)
144+ blocksize = ceil (Int, sqrt (order + 1 ))
145+
146+ # Rescale the (≤ p₀) powers we hold into powers of A/2ˢ
147+ resize! (powers, min (p₀, blocksize))
148+ if squarings > 0
149+ f = inv (convert (T, 2 )^ squarings)
150+ fk = one (T)
151+ for k in 1 : length (powers)
152+ fk *= f
153+ rmul! (powers[k], fk) # powers[k] ← (A/2ˢ)ᵏ
154+ end
155+ end
156+ # Extend to `blocksize` powers
157+ for p in (length (powers) + 1 ): blocksize
158+ push! (powers, powers[p - 1 ] * powers[1 ])
159+ end
131160
132- order, squarings = taylor_order_and_squarings (θ, tol)
133- squarings > 0 && rmul! (A, inv (convert (T, 2 )^ squarings))
134161
135- X = taylor_polynomial (A, order)
162+ # Paterson–Stockmeyer evaluation, followed by squaring
163+ X = taylor_polynomial (powers, order)
136164 Y = expA # can reuse this memory
137165 for _ in 1 : squarings
138166 mul! (Y, X, X)
@@ -149,18 +177,33 @@ end
149177
150178# Truncation order `m` and number of squarings `s` minimizing the Paterson–Stockmeyer
151179# matrix-multiplication count subject to the Taylor remainder bound `(θ/2ˢ)ᵐ⁺¹/(m+1)! ≤ tol`.
152- function taylor_order_and_squarings (θ:: Real , tol:: Real )
153- log2θ = log2 (Float64 (θ))
180+ #
181+ # The effective norm `θ` per candidate order is the sharpest Al-Mohy–Higham quantity
182+ # `αₚ = max(dₚ, dₚ₊₁)` (with `dₚ = ‖Aᵖ‖^(1/p)`) that is valid for that order: since every
183+ # `k ≥ p(p-1)` is a nonnegative combination of `p` and `p+1`, `‖Aᵏ‖ ≤ αₚᵏ` holds for `k ≥ p(p-1)`,
184+ # so `αₚ` bounds the degree-`m` remainder whenever `m+1 ≥ p(p-1)`. The `length(d)` powers already
185+ # formed count as free in the cost model.
186+ function taylor_order_and_squarings (d:: AbstractVector{<:Real} , tol:: Real )
187+ p₀ = length (d)
154188 log2tol = log2 (Float64 (tol))
155189 log2factorial = 0.0
156190 best_order = 1
157191 best_squarings = 0
158192 best_cost = typemax (Int)
159193 for order in 1 : 100
160194 log2factorial += log2 (order + 1 )
161- squarings = max (0 , ceil (Int, log2θ - (log2tol + log2factorial) / (order + 1 )))
195+ # sharpest valid αₚ = min over p with p(p-1) ≤ order+1 and p+1 ≤ p₀
196+ θ = Float64 (d[1 ])
197+ for p in 1 : (p₀ - 1 )
198+ p * (p - 1 ) ≤ order + 1 || break
199+ θ = min (θ, max (Float64 (d[p]), Float64 (d[p + 1 ])))
200+ end
201+ # `θ == 0` (a nilpotent Aᵖ) makes the remainder vanish exactly ⇒ no squarings needed;
202+ # guard the `log2(0) = -Inf` before it reaches `ceil(Int, ⋅)`.
203+ excess = log2 (θ) - (log2tol + log2factorial) / (order + 1 )
204+ squarings = excess > 0 ? ceil (Int, excess) : 0
162205 blocksize = ceil (Int, sqrt (order + 1 ))
163- cost = ( blocksize - 1 ) + (cld (order + 1 , blocksize) - 1 ) + squarings
206+ cost = max ( 0 , blocksize - p₀ ) + (cld (order + 1 , blocksize) - 1 ) + squarings
164207 if cost < best_cost
165208 best_cost = cost
166209 best_order = order
@@ -171,22 +214,18 @@ function taylor_order_and_squarings(θ::Real, tol::Real)
171214end
172215
173216# Evaluate ∑ₖ₌₀ᵐ Aᵏ/k! via the Paterson–Stockmeyer scheme, returning a freshly allocated matrix.
174- function taylor_polynomial (A:: AbstractMatrix , order:: Integer )
217+ # `powers` holds A, A², …, A^blocksize (already scaled), with blocksize = ceil(√(order+1)).
218+ function taylor_polynomial (powers:: AbstractVector{<:AbstractMatrix} , order:: Integer )
219+ A = powers[1 ]
175220 T = eltype (A)
176- blocksize = ceil (Int, sqrt (order + 1 ) )
221+ blocksize = length (powers )
177222
178223 invfactorial = Vector {T} (undef, order + 1 )
179224 invfactorial[1 ] = one (T)
180225 for k in 1 : order
181226 invfactorial[k + 1 ] = invfactorial[k] / k
182227 end
183228
184- powers = Vector {typeof(A)} (undef, blocksize)
185- powers[1 ] = A
186- for p in 2 : blocksize
187- powers[p] = powers[p - 1 ] * A
188- end
189-
190229 result = similar (A)
191230 block = similar (A)
192231 numblocks = fld (order, blocksize)
0 commit comments