Skip to content

Fix issymmetric for matrices with empty columns #606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
@@ -4074,9 +4074,9 @@ function _blockdiag(::Type{Tv}, ::Type{Ti}, X::AbstractSparseMatrixCSC...) where
end

## Structure query functions
issymmetric(A::AbstractSparseMatrixCSC) = is_hermsym(A, identity)
issymmetric(A::AbstractSparseMatrixCSC) = is_hermsym(A, transpose)

ishermitian(A::AbstractSparseMatrixCSC) = is_hermsym(A, conj)
ishermitian(A::AbstractSparseMatrixCSC) = is_hermsym(A, adjoint)

function is_hermsym(A::AbstractSparseMatrixCSC, check::Function)
m, n = size(A)
@@ -4112,6 +4112,12 @@ function is_hermsym(A::AbstractSparseMatrixCSC, check::Function)
return false
end
else
# if nzrange(A, row) is empty, then A[:, row] is all zeros.
# Specifically, A[col, row] is zero.
# However, we know at this point that A[row, col] is not zero
# This means that the matrix is not symmetric
isempty(nzrange(A, row)) && return false

offset = tracker[row]

# If the matrix is unsymmetric, there might not exist
21 changes: 21 additions & 0 deletions test/linalg.jl
Original file line number Diff line number Diff line change
@@ -425,6 +425,27 @@ end
@test issymmetric(sparse([1 0; 1 0])) == false
@test issymmetric(sparse([0 1; 1 0])) == true
@test issymmetric(sparse([1 1; 1 0])) == true

# test some non-trivial cases
local S
@testset "random matrices" begin
for sparsity in (0.1, 0.01, 0.0)
S = sparse(Symmetric(sprand(20, 20, sparsity)))
@test issymmetric(S)
@test ishermitian(S)
S = sparse(Symmetric(sprand(ComplexF64, 20, 20, sparsity)))
@test issymmetric(S)
@test !ishermitian(S) || isreal(S)
S = sparse(Hermitian(sprand(ComplexF64, 20, 20, sparsity)))
@test ishermitian(S)
@test !issymmetric(S) || isreal(S)
end
end

@testset "issue #605" begin
S = sparse([2, 3, 1], [1, 1, 3], [1, 1, 1], 3, 3)
@test !issymmetric(S)
end
end

@testset "rotations" begin