JuliaSmoothOptimizers / JuliaSmoothOptimizers/LinearOperators.jl
LinearOperator(Matrix) constructor defaults symmetric/hermitian to false even for symmetric input matrices
- Dominant language
- Julia
- Stars
- 209
- Forks
- 46
- Avg merge
- 6h 50m
- Merged PRs (30d)
- 1
Description
**Description**
When constructing a `LinearOperator` from a standard dense `Matrix` (or `AbstractMatrix`) that is symmetric or Hermitian, the resulting operator does not automatically detect or inherit these properties. It defaults `symmetric` and `hermitian` to `false`.
While explicit keywords (`symmetric=true`) can force these properties, users expect the constructor to inspect the input matrix properties or default to the correct state when `issymmetric(A)` is true, similar to how other Julia types might behave.
**Steps to Reproduce**
```julia
using LinearOperators, LinearAlgebra
n = 5
# Create a real symmetric matrix manually
H_dense = rand(n, n)
H_dense = H_dense + H_dense'
# Verify the matrix is actually Hermitian/Symmetric
println("Is input hermitian? ", ishermitian(H_dense)) # Output: true
# Wrap in LinearOperator
H_op = LinearOperator(H_dense)
# Inspect properties
display(H_op)
```
**Observed Behavior**
The resulting `LinearOperator` has both flags set to `false`:
```
Linear operator
nrow: 5
ncol: 5
eltype: Float64
symmetric: false
hermitian: false
nprod: 0
ntprod: 0
nctprod: 0
```
**Expected Behavior**
The `LinearOperator` should either:
1. Detect that the input matrix satisfies `issymmetric`/`ishermitian` and set the flags accordingly.
2. Or, if checking the matrix is avoided for performance reasons (since `issymmetric` is ), this behavior should be explicitly warned or documented, as it breaks downstream solvers that rely on these properties (e.g., Krylov methods choosing CG over GMRES).
**Potential Fix/Suggestion**
If checks are undesirable during construction, perhaps the constructor should check if the input is wrapped in `LinearAlgebra.Symmetric` or `LinearAlgebra.Hermitian` and inherit properties from those types automatically.
**Environment**
* Julia version: 1.11.x
* LinearOperators.jl version: (Current Main)
Contributor guide
Assessment
This issue has not been assessed yet.