From 8e21b21ad0ca09e8758039ba0d66a5c1931c29ea Mon Sep 17 00:00:00 2001 From: Zentrik Date: Mon, 18 Sep 2023 13:50:13 +0100 Subject: [PATCH] Remove boxing in pinv (#51351) As discussed in https://discourse.julialang.org/t/pinv-not-type-stable/103885/14, the `tol` variable is captured which leads to it being boxed, as suggested can be fixed by having two separate variables. --- stdlib/LinearAlgebra/src/dense.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/LinearAlgebra/src/dense.jl b/stdlib/LinearAlgebra/src/dense.jl index b8a44159de8bd..8d5a3e6ea8de2 100644 --- a/stdlib/LinearAlgebra/src/dense.jl +++ b/stdlib/LinearAlgebra/src/dense.jl @@ -1483,10 +1483,10 @@ function pinv(A::AbstractMatrix{T}; atol::Real = 0.0, rtol::Real = (eps(real(flo return B end SVD = svd(A) - tol = max(rtol*maximum(SVD.S), atol) + tol2 = max(rtol*maximum(SVD.S), atol) Stype = eltype(SVD.S) Sinv = fill!(similar(A, Stype, length(SVD.S)), 0) - index = SVD.S .> tol + index = SVD.S .> tol2 Sinv[index] .= pinv.(view(SVD.S, index)) return SVD.Vt' * (Diagonal(Sinv) * SVD.U') end