Skip to content

Check for elementwise equality when comparing with UniformScaling #1369

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
Jun 16, 2025
Merged
Show file tree
Hide file tree
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
18 changes: 10 additions & 8 deletions src/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,17 @@ end
function ==(A::AbstractMatrix, J::UniformScaling)
require_one_based_indexing(A)
size(A, 1) == size(A, 2) || return false
iszero(J.λ) && return iszero(A)
isone(J.λ) && return isone(A)
isdiag(A) || return false
return all(==(J.λ), diagview(A))
isempty(A) && return true
# Check that the elements of A are equal to those of J,
# this ensures that if A == J, their elements are equal as well
iszero(J.λ) && return first(A) == J.λ && iszero(A)
isone(J.λ) && return first(A) == J.λ && isone(A)
return _isequalto_uniformscaling(A, J)
end
function ==(A::StridedMatrix, J::UniformScaling)
size(A, 1) == size(A, 2) || return false
iszero(J.λ) && return iszero(A)
isone(J.λ) && return isone(A)
function _isequalto_uniformscaling(A::AbstractMatrix, J::UniformScaling)
return isdiag(A) && all(==(J.λ), diagview(A))
end
function _isequalto_uniformscaling(A::StridedMatrix, J::UniformScaling)
for j in axes(A, 2), i in axes(A, 1)
ifelse(i == j, A[i, j] == J.λ, iszero(A[i, j])) || return false
end
Expand Down
7 changes: 7 additions & 0 deletions test/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,11 @@ end
@test A\J ≈ cA\J
end

@testset "block matrix equality" begin
A = Diagonal(fill(I(2), 4))
@test isone(A)
@test A != I
@test 0*A != 0*I
end

end # module TestUniformscaling