Skip to content

Commit 9994038

Browse files
committed
micro-optimize
1 parent 5085cfa commit 9994038

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
@@ -115,7 +115,9 @@ function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFuncti
115115
T = eltype(A)
116116
R = real(T)
117117
tol = convert(R, get(alg.kwargs, :tol, eps(R)))
118-
scale = get(alg.kwargs, :balance, true) ? balance!(A)[2] : fill!(similar(A, R, size(A, 1)), one(R))
118+
119+
dobalance = get(alg.kwargs, :balance, false)
120+
scale = dobalance ? balance!(A)[2] : nothing
119121

120122
θ = LinearAlgebra.opnorm(A, 1)
121123
iszero(θ) && return one!(expA)
@@ -124,15 +126,17 @@ function exponential!(A::AbstractMatrix, expA::AbstractMatrix, alg::MatrixFuncti
124126
squarings > 0 && rmul!(A, inv(convert(T, 2)^squarings))
125127

126128
X = taylor_polynomial(A, order)
127-
if squarings > 0
128-
Y = similar(X)
129-
for _ in 1:squarings
130-
mul!(Y, X, X)
131-
X, Y = Y, X
132-
end
129+
Y = expA # can reuse this memory
130+
for _ in 1:squarings
131+
mul!(Y, X, X)
132+
X, Y = Y, X
133133
end
134134

135-
expA .= scale .* X ./ transpose(scale)
135+
if dobalance
136+
expA .= scale .* X ./ transpose(scale)
137+
else
138+
X === expA || copyto!(expA, X)
139+
end
136140
return expA
137141
end
138142

0 commit comments

Comments
 (0)