Solving a matrix least-squares with LinearSolve.jl

Hi. I’ve been trying out LinearSolve.jl for a bit, and I happened to notice that this package is generally for

\min \| \mathbf{Ax} - \mathbf{b} \|_2^2

where \mathbf{b},\mathbf{x} are vectors. My problem, on the other hand, is a matrix least-squares

\min \| \mathbf{AX} - \mathbf{B} \|_F^2

which can be solved using \. I also know I can simply solve the least-squares for each columns of \mathbf{X}. But I’m curious to know if there’s a nice memory and time efficient way to solve the matrix least-squares using LinearSolve.jl.

A simple example:

# Backslash
using LinearAlgebra
A = rand(10,5)
B = rand(10,3)
sol1 = A \ B

# LinearSolve.jl (does not work)
using LinearSolve
prob = LinearSolve.LinearProblem(A, B)
sol2 = LinearSolve.solve(prob)

where I’m met with the error message

ERROR: MethodError: no method matching ldiv!(::Vector{Float64}, ::LinearAlgebra.QRCompactWY{Float64, Matrix{Float64}, Matrix{Float64}}, ::Matrix{Float64})
The function `ldiv!` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  ldiv!(::AbstractVecOrMat, ::IdentityOperator, ::AbstractVecOrMat)
   @ SciMLOperators C:\Users\anon\.julia\packages\SciMLOperators\KVzmP\src\basic.jl:62
  ldiv!(::AbstractVecOrMat, ::Bidiagonal, ::AbstractVecOrMat)
   @ LinearAlgebra C:\Users\anon\.julia\juliaup\julia-1.11.3+0.x64.w64.mingw32\share\julia\stdlib\v1.11\LinearAlgebra\src\bidiag.jl:834       
  ldiv!(::AbstractVecOrMat, ::InvertibleOperator, ::AbstractVecOrMat)
   @ SciMLOperators C:\Users\anon\.julia\packages\SciMLOperators\KVzmP\src\matrix.jl:384

Any advice would be great. Thanks!

No, not at this time. It just focuses on the linear solve and not the batch linear solve or least squares. While QR and GMRES do this, it is slightly different as an interface interpretation

1 Like

@ChrisRackauckas Thanks! Good to know!