Skip to content

Commit 00ab0d5

Browse files
committed
micro-optimize
1 parent fb5a1cf commit 00ab0d5

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

‎src/implementations/exponential.jl‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFuncti
122122
T = eltype(A)
123123
R = real(T)
124124
tol = convert(R, get(alg.kwargs, :tol, eps(R)))
125-
scale = get(alg.kwargs, :balance, true) ? balance!(A)[2] : fill!(similar(A, R, size(A, 1)), one(R))
125+
126+
dobalance = get(alg.kwargs, :balance, false)
127+
scale = dobalance ? balance!(A)[2] : nothing
126128

127129
θ = LinearAlgebra.opnorm(A, 1)
128130
iszero(θ) && return one!(expA)
@@ -131,15 +133,17 @@ function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFuncti
131133
squarings > 0 && rmul!(A, inv(convert(T, 2)^squarings))
132134

133135
X = taylor_polynomial(A, order)
134-
if squarings > 0
135-
Y = similar(X)
136-
for _ in 1:squarings
137-
mul!(Y, X, X)
138-
X, Y = Y, X
139-
end
136+
Y = expA # can reuse this memory
137+
for _ in 1:squarings
138+
mul!(Y, X, X)
139+
X, Y = Y, X
140140
end
141141

142-
expA .= scale .* X ./ transpose(scale)
142+
if dobalance
143+
expA .= scale .* X ./ transpose(scale)
144+
else
145+
X === expA || copyto!(expA, X)
146+
end
143147
return expA
144148
end
145149

0 commit comments

Comments
 (0)