Skip to content

Commit 8604233

Browse files
raghav9-97ViralBShah
authored andcommitted
Fix #18742: Added check for / DimensionMismatch error (#30446)
* Added check for / DimensionMismatch error * Added tests
1 parent e7e33cc commit 8604233

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

stdlib/LinearAlgebra/src/generic.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,10 @@ function (\)(A::AbstractMatrix, B::AbstractVecOrMat)
908908
end
909909

910910
(\)(a::AbstractVector, b::AbstractArray) = pinv(a) * b
911-
(/)(A::AbstractVecOrMat, B::AbstractVecOrMat) = copy(adjoint(adjoint(B) \ adjoint(A)))
911+
function (/)(A::AbstractVecOrMat, B::AbstractVecOrMat)
912+
size(A,2) != size(B,2) && throw(DimensionMismatch("Both inputs should have the same number of columns"))
913+
return copy(adjoint(adjoint(B) \ adjoint(A)))
914+
end
912915
# \(A::StridedMatrix,x::Number) = inv(A)*x Should be added at some point when the old elementwise version has been deprecated long enough
913916
# /(x::Number,A::StridedMatrix) = x*inv(A)
914917
/(x::Number, v::AbstractVector) = x*pinv(v)

stdlib/LinearAlgebra/src/qr.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ function (\)(A::Union{QR{TA},QRCompactWY{TA},QRPivoted{TA}}, B::AbstractVecOrMat
854854
@assert !has_offset_axes(B)
855855
S = promote_type(TA,TB)
856856
m, n = size(A)
857-
m == size(B,1) || throw(DimensionMismatch("left hand side has $m rows, but right hand side has $(size(B,1)) rows"))
857+
m == size(B,1) || throw(DimensionMismatch("Both inputs should have the same number of rows"))
858858

859859
AA = Factorization{S}(A)
860860

stdlib/LinearAlgebra/test/generic.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ LinearAlgebra.Transpose(a::ModInt{n}) where {n} = transpose(a)
299299
@test A*(lu(A, Val(true))\b) == b
300300
end
301301

302+
@testset "Issue 18742" begin
303+
@test_throws DimensionMismatch ones(4,5)/zeros(3,6)
304+
@test_throws DimensionMismatch ones(4,5)\zeros(3,6)
305+
end
302306
@testset "fallback throws properly for AbstractArrays with dimension > 2" begin
303307
@test_throws ErrorException adjoint(rand(2,2,2,2))
304308
@test_throws ErrorException transpose(rand(2,2,2,2))

0 commit comments

Comments
 (0)