Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions src/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ StridedMatrixStride1{T} = StridedArrayStride1{T,2}
"""
LinearAlgebra.checksquare(A)

Check that a matrix is square, then return its common dimension.
Checks whether a matrix is square, returning its common dimension if it is the case, or throwing a DimensionMismatch error otherwise.
For multiple arguments, return a vector.

# Examples
Expand All @@ -340,16 +340,16 @@ julia> LinearAlgebra.checksquare(A, B)
```
"""
function checksquare(A)
m,n = size(A)
m == n || throw(DimensionMismatch(lazy"matrix is not square: dimensions are $(size(A))"))
m
sizeA = size(A)
length(sizeA) == 2 || throw(DimensionMismatch(lazy"input is not a matrix: dimensions are $sizeA"))
sizeA[1] == sizeA[2] || throw(DimensionMismatch(lazy"matrix is not square: dimensions are $sizeA"))
return sizeA[1]
end

function checksquare(A...)
sizes = Int[]
for a in A
size(a,1)==size(a,2) || throw(DimensionMismatch(lazy"matrix is not square: dimensions are $(size(a))"))
push!(sizes, size(a,1))
sizes = Vector{Int}(undef, length(A))
for i in eachindex(A)
sizes[i] = checksquare(A[i])
end
return sizes
end
Expand Down
11 changes: 11 additions & 0 deletions test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1430,4 +1430,15 @@ end
@test log(D) log(UpperTriangular(D))
end

@testset "issue 1362" begin
A = zeros(2,2)
B = zeros(2,3)
C = zeros(2,2,1)
@test LinearAlgebra.checksquare(A) == 2
@test LinearAlgebra.checksquare(A,A) == [2, 2]
@test_throws DimensionMismatch LinearAlgebra.checksquare(B)
@test_throws DimensionMismatch LinearAlgebra.checksquare(C)
@test_throws DimensionMismatch LinearAlgebra.checksquare(A,B)
end

end # module TestDense