# Solving a matrix least-squares with LinearSolve.jl

**URL:** <https://discourse.julialang.org/t/solving-a-matrix-least-squares-with-linearsolve-jl/126367>\
**Category:** Numerics\
**Created:** [February 27, 2025, 1:54am UTC](https://discourse.julialang.org/t/solving-a-matrix-least-squares-with-linearsolve-jl/126367 "2025-02-27T01:54:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smallpondtom](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@smallpondtom](https://discourse.julialang.org/u/smallpondtom)\
**Post date:** [February 27, 2025, 1:54am UTC](https://discourse.julialang.org/t/solving-a-matrix-least-squares-with-linearsolve-jl/126367/1 "2025-02-27T01:54:26Z")

</div>

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:

```julia
# 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

```julia
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!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 28, 2025, 12:25am UTC](https://discourse.julialang.org/t/solving-a-matrix-least-squares-with-linearsolve-jl/126367/2 "2025-02-28T00:25:26Z")

</div>

> [@smallpondtom](#):
>
> 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](https://juliaregistries.github.io/General/packages/redirect_to_repo/LinearSolve).

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

---

<div class="post-metadata">

**Author:** ![smallpondtom](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@smallpondtom](https://discourse.julialang.org/u/smallpondtom)\
**Post date:** [February 28, 2025, 2:28pm UTC](https://discourse.julialang.org/t/solving-a-matrix-least-squares-with-linearsolve-jl/126367/3 "2025-02-28T14:28:51Z")

</div>

@ChrisRackauckas Thanks! Good to know!
