Skip to content

Non-square matrices return errors - no default method implemented #546

Open
@l8l

Description

@l8l

Describe the bug 🐞

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions