You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A \ b works when A is of dimension $n\times p$ and $b$ is of dimension $n$. However, for LinearSolve, the code
using LinearSolve
lin_prob =LinearProblem(A, b)
solution = LinearSolve.solve(lin_prob)
returns the error
ERROR: DimensionMismatch: matrix is not square:...
In the documentation of LinearSolve.jl, I also did not find examples of non-square matrices, yet it is advertised that LinearSolve.jl is supposed to replace the \ - Operator, which works fine in my example.
This discussion on julia discourse revealed that " A \ b , for a “tall” matrix, automatically switches to finding the least-square solution (by pivoted QR), while for a “wide” matrix, it switches to finding the minimum-norm solution."
I found out, that least square and least norm solutions are at least available with the following Krylov methods:
using LinearSolve
lin_prob =LinearProblem(A, b)
x_least_squares = LinearSolve.solve(lin_prob,LinearSolve.KrylovJL_LSMR())
x_least_norm = LinearSolve.solve(lin_prob,LinearSolve.KrylovJL_CRAIGMR())
but there is no method that does, what \ seems to do in this case (first check whether A is tall or wide, then act accordingly) and maybe there is an even more optimal behavior, or better heuristics?
Expected behavior
The solver should check automatically if A is a square matrix, or not, and if not, should apply appropriate methods, that follow appropriate heuristics. For example, one could follow the strategy, that A \ b implements for non-square matrices. In the end,
using LinearSolve
lin_prob =LinearProblem(A, b)
solution = LinearSolve.solve(lin_prob)
should just work for non-square matrices (possibly with a note on the chosen solver-strategy, or a remark that A is not square, if desired).
Minimal Reproducible Example 👇
See above.
Error & Stacktrace ⚠️
ERROR: DimensionMismatch: matrix is not square:...
Environment (please complete the following information):
Output of using Pkg; Pkg.status()
LinearSolve v2.35.0
LinearAlgebra v1.11.0
Output of versioninfo()
Julia Version 1.11.0
Build Info:
Official https://julialang.org/ release
Platform Info:
OS: Linux (x86_64-linux-gnu)
CPU:Intel(R) Xeon(R) CPU @ 3.50GHz
WORD_SIZE:64
LLVM: libLLVM-16.0.6 (ORCJIT, skylake-avx512)
The text was updated successfully, but these errors were encountered:
Okay, thanks for this lightning-fast reply Chris! Interesting. You are right! It seems that the error occurred in my case because the matrix came from an adjoint:
julia> A =rand(2,3)'; b =rand(3);
julia> LinearSolve.solve(LinearProblem(A, b))
ERROR: DimensionMismatch: matrix is not square: dimensions are (3, 2)
...
julia> LinearSolve.solve(LinearProblem(A, b),LinearSolve.KrylovJL_LSMR())
retcode: Success
u:2-element Vector{Float64}:-0.37744811678157461.1609616348557
julia> LinearSolve.solve(LinearProblem(Matrix(A), b))
retcode: Default
u:2-element Vector{Float64}:-0.37744811678157671.1609616348557
julia> A
3×2adjoint(::Matrix{Float64}) with eltype Float64:0.2588060.2746450.4465310.9226080.5209270.446123
But shouldn't the default method also just work for adjoints?
Describe the bug 🐞
A \ b
works whenA
is of dimensionLinearSolve
, the codereturns the error
In the documentation of
LinearSolve.jl
, I also did not find examples of non-square matrices, yet it is advertised thatLinearSolve.jl
is supposed to replace the\
- Operator, which works fine in my example.This discussion on julia discourse revealed that "
A \ b
, for a “tall” matrix, automatically switches to finding the least-square solution (by pivoted QR), while for a “wide” matrix, it switches to finding the minimum-norm solution."I found out, that least square and least norm solutions are at least available with the following Krylov methods:
but there is no method that does, what
\
seems to do in this case (first check whetherA
is tall or wide, then act accordingly) and maybe there is an even more optimal behavior, or better heuristics?Expected behavior
The solver should check automatically if
A
is a square matrix, or not, and if not, should apply appropriate methods, that follow appropriate heuristics. For example, one could follow the strategy, thatA \ b
implements for non-square matrices. In the end,should just work for non-square matrices (possibly with a note on the chosen solver-strategy, or a remark that
A
is not square, if desired).Minimal Reproducible Example 👇
See above.
Error & Stacktrace⚠️
Environment (please complete the following information):
using Pkg; Pkg.status()
versioninfo()
The text was updated successfully, but these errors were encountered: