Skip to content

Commit 9c530de

Browse files
Provide a message when throwing DimensionMismatch (#501)
See JuliaLang/julia#30766 Co-authored-by: Peter Shin <[email protected]>
1 parent d37d2fd commit 9c530de

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

src/sparsevector.jl

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,8 @@ function _binarymap(f::Function,
14491449
R = Base.Broadcast.combine_eltypes(f, (x, y))
14501450
I = promote_type(eltype(nonzeroinds(x)), eltype(nonzeroinds(y)))
14511451
n = length(x)
1452-
length(y) == n || throw(DimensionMismatch())
1452+
length(y) == n || throw(DimensionMismatch(
1453+
"Vector x has a length $n but y has a length $(length(y))"))
14531454

14541455
xnzind = nonzeroinds(x)
14551456
xnzval = nonzeros(x)
@@ -1701,7 +1702,8 @@ adjoint(sv::AbstractCompressedVector) = Adjoint(sv)
17011702

17021703
function LinearAlgebra.axpy!(a::Number, x::SparseVectorUnion, y::AbstractVector)
17031704
require_one_based_indexing(x, y)
1704-
length(x) == length(y) || throw(DimensionMismatch())
1705+
length(x) == length(y) || throw(DimensionMismatch(
1706+
"Vector x has a length $(length(x)) but y has a length $(length(y))"))
17051707
nzind = nonzeroinds(x)
17061708
nzval = nonzeros(x)
17071709
m = length(nzind)
@@ -1758,7 +1760,8 @@ end
17581760
function dot(x::AbstractVector, y::SparseVectorUnion)
17591761
require_one_based_indexing(x, y)
17601762
n = length(x)
1761-
length(y) == n || throw(DimensionMismatch())
1763+
length(y) == n || throw(DimensionMismatch(
1764+
"Vector x has a length $n but y has a length $(length(y))"))
17621765
nzind = nonzeroinds(y)
17631766
nzval = nonzeros(y)
17641767
s = dot(zero(eltype(x)), zero(eltype(y)))
@@ -1771,7 +1774,8 @@ end
17711774
function dot(x::SparseVectorUnion, y::AbstractVector)
17721775
require_one_based_indexing(x, y)
17731776
n = length(y)
1774-
length(x) == n || throw(DimensionMismatch())
1777+
length(x) == n || throw(DimensionMismatch(
1778+
"Vector x has a length $(length(x)) but y has a length $n"))
17751779
nzind = nonzeroinds(x)
17761780
nzval = nonzeros(x)
17771781
s = dot(zero(eltype(x)), zero(eltype(y)))
@@ -1805,7 +1809,8 @@ end
18051809
function dot(x::SparseVectorUnion, y::SparseVectorUnion)
18061810
x === y && return sum(abs2, x)
18071811
n = length(x)
1808-
length(y) == n || throw(DimensionMismatch())
1812+
length(y) == n || throw(DimensionMismatch(
1813+
"Vector x has a length $n but y has a length $(length(y))"))
18091814

18101815
xnzind = nonzeroinds(x)
18111816
ynzind = nonzeroinds(y)
@@ -1846,7 +1851,8 @@ _fliptri(A::UnitLowerTriangular) = UnitUpperTriangular(parent(parent(A)))
18461851
function (*)(A::_StridedOrTriangularMatrix{Ta}, x::AbstractSparseVector{Tx}) where {Ta,Tx}
18471852
require_one_based_indexing(A, x)
18481853
m, n = size(A)
1849-
length(x) == n || throw(DimensionMismatch())
1854+
length(x) == n || throw(DimensionMismatch(
1855+
"Matrix A has $n columns, but vector x has a length $(length(x))"))
18501856
Ty = promote_op(matprod, eltype(A), eltype(x))
18511857
y = Vector{Ty}(undef, m)
18521858
mul!(y, A, x)
@@ -1881,7 +1887,10 @@ end
18811887
function _spmul!(y::AbstractVector, A::AbstractMatrix, x::AbstractSparseVector, α::Number, β::Number)
18821888
require_one_based_indexing(y, A, x)
18831889
m, n = size(A)
1884-
length(x) == n && length(y) == m || throw(DimensionMismatch())
1890+
length(x) == n || throw(DimensionMismatch(
1891+
"Matrix A has $n columns, but vector x has a length $(length(x))"))
1892+
length(y) == m || throw(DimensionMismatch(
1893+
"Matrix A has $m rows, but vector y has a length $(length(y))"))
18851894
m == 0 && return y
18861895
β != one(β) && LinearAlgebra._rmul_or_fill!(y, β)
18871896
α == zero(α) && return y
@@ -1906,7 +1915,10 @@ function _At_or_Ac_mul_B!(tfun::Function,
19061915
α::Number, β::Number)
19071916
require_one_based_indexing(y, A, x)
19081917
n, m = size(A)
1909-
length(x) == n && length(y) == m || throw(DimensionMismatch())
1918+
length(x) == n || throw(DimensionMismatch(
1919+
"Matrix A has $n rows, but vector x has a length $(length(x))"))
1920+
length(y) == m || throw(DimensionMismatch(
1921+
"Matrix A has $m columns, but vector y has a length $(length(y))"))
19101922
m == 0 && return y
19111923
β != one(β) && LinearAlgebra._rmul_or_fill!(y, β)
19121924
α == zero(α) && return y
@@ -1930,15 +1942,17 @@ end
19301942
function *(A::AdjOrTrans{<:Any,<:StridedMatrix}, x::AbstractSparseVector)
19311943
require_one_based_indexing(A, x)
19321944
m, n = size(A)
1933-
length(x) == n || throw(DimensionMismatch())
1945+
length(x) == n || throw(DimensionMismatch(
1946+
"Matrix A has $n columns, but vector x has a length $(length(x))"))
19341947
Ty = promote_op(matprod, eltype(A), eltype(x))
19351948
y = Vector{Ty}(undef, m)
19361949
mul!(y, A, x, true, false)
19371950
end
19381951
function *(A::LinearAlgebra.HermOrSym{<:Any,<:StridedMatrix}, x::AbstractSparseVector)
19391952
require_one_based_indexing(A, x)
19401953
m, n = size(A)
1941-
length(x) == n || throw(DimensionMismatch())
1954+
length(x) == n || throw(DimensionMismatch(
1955+
"Matrix A has $n columns, but vector x has a length $(length(x))"))
19421956
Ty = promote_op(matprod, eltype(A), eltype(x))
19431957
y = Vector{Ty}(undef, m)
19441958
mul!(y, A, x, true, false)
@@ -1953,12 +1967,15 @@ function densemv(A::AbstractSparseMatrixCSC, x::AbstractSparseVector; trans::Abs
19531967
m, n = size(A)
19541968
if trans == 'N' || trans == 'n'
19551969
xlen = n; ylen = m
1970+
xaxis = "columns"
19561971
elseif trans == 'T' || trans == 't' || trans == 'C' || trans == 'c'
19571972
xlen = m; ylen = n
1973+
xaxis = "rows"
19581974
else
19591975
throw(ArgumentError("Invalid trans character $trans"))
19601976
end
1961-
xlen == length(x) || throw(DimensionMismatch())
1977+
xlen == length(x) || throw(DimensionMismatch(
1978+
"Matrix A has $xlen $xaxis, but vector x has a length $(length(x))"))
19621979
T = promote_op(matprod, eltype(A), eltype(x))
19631980
y = Vector{T}(undef, ylen)
19641981
if trans == 'N' || trans == 'n'
@@ -1989,7 +2006,10 @@ end
19892006
function _spmul!(y::AbstractVector, A::AbstractSparseMatrixCSC, x::AbstractSparseVector, α::Number, β::Number)
19902007
require_one_based_indexing(y, A, x)
19912008
m, n = size(A)
1992-
length(x) == n && length(y) == m || throw(DimensionMismatch())
2009+
length(x) == n || throw(DimensionMismatch(
2010+
"Matrix A has $n columns, but vector x has a length $(length(x))"))
2011+
length(y) == m || throw(DimensionMismatch(
2012+
"Matrix A has $m rows, but vector y has a length $(length(y))"))
19932013
m == 0 && return y
19942014
β != one(β) && LinearAlgebra._rmul_or_fill!(y, β)
19952015
α == zero(α) && return y
@@ -2018,7 +2038,10 @@ function _At_or_Ac_mul_B!(tfun::Function,
20182038
α::Number, β::Number)
20192039
require_one_based_indexing(y, A, x)
20202040
m, n = size(A)
2021-
length(x) == m && length(y) == n || throw(DimensionMismatch())
2041+
length(x) == m || throw(DimensionMismatch(
2042+
"Matrix A has $n columns, but vector x has a length $(length(x))"))
2043+
length(y) == n || throw(DimensionMismatch(
2044+
"Matrix A has $m rows, but vector y has a length $(length(y))"))
20222045
n == 0 && return y
20232046
β != one(β) && LinearAlgebra._rmul_or_fill!(y, β)
20242047
α == zero(α) && return y
@@ -2056,7 +2079,8 @@ function _At_or_Ac_mul_B(tfun::Function, A::AbstractSparseMatrixCSC{TvA,TiA}, x:
20562079
Tv = promote_op(matprod, TvA, TvX)) where {TvA,TiA,TvX,TiX}
20572080
require_one_based_indexing(A, x)
20582081
m, n = size(A)
2059-
length(x) == m || throw(DimensionMismatch())
2082+
length(x) == m || throw(DimensionMismatch(
2083+
"Matrix A has $m rows, but vector x has a length $(length(x))"))
20602084
Ti = promote_type(TiA, TiX)
20612085

20622086
xnzind = nonzeroinds(x)

0 commit comments

Comments
 (0)