Assume I want to solve Ax = b for a bunch of different b's, but keeping A fixed. Furthermore, assume A is a tall matrix. The question is, what are the pros and cons of precomputing the pseudoinverse of A and precomputing the QR factorization of A?
In the documentation of LinearSolve.jl, for example, one mentions the problem of repeated solve but the pseudoinverse appers to not be used. In the book Fundamentals of Numerical Computation it is also said that the pseudoinverse βis rarely necessary in practiceβ.
Nonetheless, in the following benchmark, I appear to get faster and nonallocating results with the pseudoinverse approach:
using LinearAlgebra
A = rand(Float32, 10^4, 10^2)
invA = pinv(A)
b = rand(Float32, 10^4)
F = qr(A)
dest1 = Vector{Float32}(undef, 10^2)
dest2 = Vector{Float32}(undef, 10^2)
ldiv!(dest1, F, b)
mul!(dest2, invA, b)
dest1 β dest2 # true
@benchmark ldiv!($dest1, $F, $b)
@benchmark mul!($dest2, $invA, $b)
The output of the benchmark of ldiv!
is
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min β¦ max): 277.379 ΞΌs β¦ 30.607 ms β GC (min β¦ max): 0.00% β¦ 98.50%
Time (median): 432.062 ΞΌs β GC (median): 0.00%
Time (mean Β± Ο): 394.850 ΞΌs Β± 311.694 ΞΌs β GC (mean Β± Ο): 0.87% Β± 1.54%
β β βββ
β
ββ
βββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
277 ΞΌs Histogram: frequency by time 490 ΞΌs <
Memory estimate: 39.62 KiB, allocs estimate: 4.
while that of mul!
is
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min β¦ max): 82.456 ΞΌs β¦ 259.985 ΞΌs β GC (min β¦ max): 0.00% β¦ 0.00%
Time (median): 92.826 ΞΌs β GC (median): 0.00%
Time (mean Β± Ο): 93.672 ΞΌs Β± 5.320 ΞΌs β GC (mean Β± Ο): 0.00% Β± 0.00%
βββββββββββββββ
βββ
βββββββββββββββββββββββββββββββββ
β
β
βββββββββββββββββββββββββ β
82.5 ΞΌs Histogram: frequency by time 110 ΞΌs <
Memory estimate: 0 bytes, allocs estimate: 0.
This result seems to be in contradiction with the general advice, at least those contained in the references that I found. Am I missing something here?
Thanks in advance for any help!