Skip to content

Commit

Permalink
LinearAlgebra: LazyString in interpolated error messages (#53976)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Apr 9, 2024
1 parent 26070ca commit 7099bdd
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 271 deletions.
22 changes: 11 additions & 11 deletions stdlib/LinearAlgebra/src/abstractq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ end
# generically, treat AbstractQ like a matrix with its definite size
qsize_check(Q::AbstractQ, B::AbstractVecOrMat) =
size(Q, 2) == size(B, 1) ||
throw(DimensionMismatch("second dimension of Q, $(size(Q,2)), must coincide with first dimension of B, $(size(B,1))"))
throw(DimensionMismatch(lazy"second dimension of Q, $(size(Q,2)), must coincide with first dimension of B, $(size(B,1))"))
qsize_check(A::AbstractVecOrMat, Q::AbstractQ) =
size(A, 2) == size(Q, 1) ||
throw(DimensionMismatch("second dimension of A, $(size(A,2)), must coincide with first dimension of Q, $(size(Q,1))"))
throw(DimensionMismatch(lazy"second dimension of A, $(size(A,2)), must coincide with first dimension of Q, $(size(Q,1))"))
qsize_check(Q::AbstractQ, P::AbstractQ) =
size(Q, 2) == size(P, 1) ||
throw(DimensionMismatch("second dimension of A, $(size(Q,2)), must coincide with first dimension of B, $(size(P,1))"))
throw(DimensionMismatch(lazy"second dimension of A, $(size(Q,2)), must coincide with first dimension of B, $(size(P,1))"))

# mimic the AbstractArray fallback
*(Q::AbstractQ{<:Number}) = Q
Expand Down Expand Up @@ -317,7 +317,7 @@ function lmul!(A::QRPackedQ, B::AbstractVecOrMat)
mA, nA = size(A.factors)
mB, nB = size(B,1), size(B,2)
if mA != mB
throw(DimensionMismatch("matrix A has dimensions ($mA,$nA) but B has dimensions ($mB, $nB)"))
throw(DimensionMismatch(lazy"matrix A has dimensions ($mA,$nA) but B has dimensions ($mB, $nB)"))
end
Afactors = A.factors
@inbounds begin
Expand Down Expand Up @@ -353,7 +353,7 @@ function lmul!(adjA::AdjointQ{<:Any,<:QRPackedQ}, B::AbstractVecOrMat)
mA, nA = size(A.factors)
mB, nB = size(B,1), size(B,2)
if mA != mB
throw(DimensionMismatch("matrix A has dimensions ($mA,$nA) but B has dimensions ($mB, $nB)"))
throw(DimensionMismatch(lazy"matrix A has dimensions ($mA,$nA) but B has dimensions ($mB, $nB)"))
end
Afactors = A.factors
@inbounds begin
Expand Down Expand Up @@ -384,7 +384,7 @@ function rmul!(A::AbstractVecOrMat, Q::QRPackedQ)
mQ, nQ = size(Q.factors)
mA, nA = size(A,1), size(A,2)
if nA != mQ
throw(DimensionMismatch("matrix A has dimensions ($mA,$nA) but matrix Q has dimensions ($mQ, $nQ)"))
throw(DimensionMismatch(lazy"matrix A has dimensions ($mA,$nA) but matrix Q has dimensions ($mQ, $nQ)"))
end
Qfactors = Q.factors
@inbounds begin
Expand Down Expand Up @@ -420,7 +420,7 @@ function rmul!(A::AbstractVecOrMat, adjQ::AdjointQ{<:Any,<:QRPackedQ})
mQ, nQ = size(Q.factors)
mA, nA = size(A,1), size(A,2)
if nA != mQ
throw(DimensionMismatch("matrix A has dimensions ($mA,$nA) but matrix Q has dimensions ($mQ, $nQ)"))
throw(DimensionMismatch(lazy"matrix A has dimensions ($mA,$nA) but matrix Q has dimensions ($mQ, $nQ)"))
end
Qfactors = Q.factors
@inbounds begin
Expand Down Expand Up @@ -521,10 +521,10 @@ rmul!(X::Adjoint{T,<:StridedVecOrMat{T}}, adjQ::AdjointQ{<:Any,<:HessenbergQ{T}}
# flexible left-multiplication (and adjoint right-multiplication)
qsize_check(Q::Union{QRPackedQ,QRCompactWYQ,HessenbergQ}, B::AbstractVecOrMat) =
size(B, 1) in size(Q.factors) ||
throw(DimensionMismatch("first dimension of B, $(size(B,1)), must equal one of the dimensions of Q, $(size(Q.factors))"))
throw(DimensionMismatch(lazy"first dimension of B, $(size(B,1)), must equal one of the dimensions of Q, $(size(Q.factors))"))
qsize_check(A::AbstractVecOrMat, adjQ::AdjointQ{<:Any,<:Union{QRPackedQ,QRCompactWYQ,HessenbergQ}}) =
(Q = adjQ.Q; size(A, 2) in size(Q.factors) ||
throw(DimensionMismatch("second dimension of A, $(size(A,2)), must equal one of the dimensions of Q, $(size(Q.factors))")))
throw(DimensionMismatch(lazy"second dimension of A, $(size(A,2)), must equal one of the dimensions of Q, $(size(Q.factors))")))

det(Q::HessenbergQ) = _det_tau(Q.τ)

Expand Down Expand Up @@ -560,10 +560,10 @@ size(Q::LQPackedQ) = (n = size(Q.factors, 2); return n, n)

qsize_check(adjQ::AdjointQ{<:Any,<:LQPackedQ}, B::AbstractVecOrMat) =
size(B, 1) in size(adjQ.Q.factors) ||
throw(DimensionMismatch("first dimension of B, $(size(B,1)), must equal one of the dimensions of Q, $(size(adjQ.Q.factors))"))
throw(DimensionMismatch(lazy"first dimension of B, $(size(B,1)), must equal one of the dimensions of Q, $(size(adjQ.Q.factors))"))
qsize_check(A::AbstractVecOrMat, Q::LQPackedQ) =
size(A, 2) in size(Q.factors) ||
throw(DimensionMismatch("second dimension of A, $(size(A,2)), must equal one of the dimensions of Q, $(size(Q.factors))"))
throw(DimensionMismatch(lazy"second dimension of A, $(size(A,2)), must equal one of the dimensions of Q, $(size(Q.factors))"))

# in-place right-application of LQPackedQs
# these methods require that the applied-to matrix's (A's) number of columns
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ tr(A::Transpose) = transpose(tr(parent(A)))
function _dot_nonrecursive(u, v)
lu = length(u)
if lu != length(v)
throw(DimensionMismatch("first array has length $(lu) which does not match the length of the second, $(length(v))."))
throw(DimensionMismatch(lazy"first array has length $(lu) which does not match the length of the second, $(length(v))."))
end
if lu == 0
zero(eltype(u)) * zero(eltype(v))
Expand Down
20 changes: 10 additions & 10 deletions stdlib/LinearAlgebra/src/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct Bidiagonal{T,V<:AbstractVector{T}} <: AbstractMatrix{T}
function Bidiagonal{T,V}(dv, ev, uplo::AbstractChar) where {T,V<:AbstractVector{T}}
require_one_based_indexing(dv, ev)
if length(ev) != max(length(dv)-1, 0)
throw(DimensionMismatch("length of diagonal vector is $(length(dv)), length of off-diagonal vector is $(length(ev))"))
throw(DimensionMismatch(lazy"length of diagonal vector is $(length(dv)), length of off-diagonal vector is $(length(ev))"))
end
(uplo != 'U' && uplo != 'L') && throw_uplo()
new{T,V}(dv, ev, uplo)
Expand Down Expand Up @@ -438,11 +438,11 @@ function check_A_mul_B!_sizes(C, A, B)
mB, nB = size(B)
mC, nC = size(C)
if mA != mC
throw(DimensionMismatch("first dimension of A, $mA, and first dimension of output C, $mC, must match"))
throw(DimensionMismatch(lazy"first dimension of A, $mA, and first dimension of output C, $mC, must match"))
elseif nA != mB
throw(DimensionMismatch("second dimension of A, $nA, and first dimension of B, $mB, must match"))
throw(DimensionMismatch(lazy"second dimension of A, $nA, and first dimension of B, $mB, must match"))
elseif nB != nC
throw(DimensionMismatch("second dimension of output C, $nC, and second dimension of B, $nB, must match"))
throw(DimensionMismatch(lazy"second dimension of output C, $nC, and second dimension of B, $nB, must match"))
end
end

Expand Down Expand Up @@ -562,10 +562,10 @@ function _mul!(C::AbstractVecOrMat, A::BiTriSym, B::AbstractVecOrMat, _add::MulA
nA = size(A,1)
nB = size(B,2)
if !(size(C,1) == size(B,1) == nA)
throw(DimensionMismatch("A has first dimension $nA, B has $(size(B,1)), C has $(size(C,1)) but all must match"))
throw(DimensionMismatch(lazy"A has first dimension $nA, B has $(size(B,1)), C has $(size(C,1)) but all must match"))
end
if size(C,2) != nB
throw(DimensionMismatch("A has second dimension $nA, B has $(size(B,2)), C has $(size(C,2)) but all must match"))
throw(DimensionMismatch(lazy"A has second dimension $nA, B has $(size(B,2)), C has $(size(C,2)) but all must match"))
end
iszero(nA) && return C
iszero(_add.alpha) && return _rmul_or_fill!(C, _add.beta)
Expand Down Expand Up @@ -763,11 +763,11 @@ function ldiv!(c::AbstractVecOrMat, A::Bidiagonal, b::AbstractVecOrMat)
N = size(A, 2)
mb, nb = size(b, 1), size(b, 2)
if N != mb
throw(DimensionMismatch("second dimension of A, $N, does not match first dimension of b, $mb"))
throw(DimensionMismatch(lazy"second dimension of A, $N, does not match first dimension of b, $mb"))
end
mc, nc = size(c, 1), size(c, 2)
if mc != mb || nc != nb
throw(DimensionMismatch("size of result, ($mc, $nc), does not match the size of b, ($mb, $nb)"))
throw(DimensionMismatch(lazy"size of result, ($mc, $nc), does not match the size of b, ($mb, $nb)"))
end

if N == 0
Expand Down Expand Up @@ -833,11 +833,11 @@ function _rdiv!(C::AbstractMatrix, A::AbstractMatrix, B::Bidiagonal)
require_one_based_indexing(C, A, B)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
throw(DimensionMismatch(lazy"right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
mc, nc = size(C)
if mc != m || nc != n
throw(DimensionMismatch("expect output to have size ($m, $n), but got ($mc, $nc)"))
throw(DimensionMismatch(lazy"expect output to have size ($m, $n), but got ($mc, $nc)"))
end

zi = findfirst(iszero, B.dv)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ function diagm_size(size::Tuple{Int,Int}, kv::Pair{<:Integer,<:AbstractVector}..
mmax = mapreduce(x -> length(x.second) - min(0,Int(x.first)), max, kv; init=0)
nmax = mapreduce(x -> length(x.second) + max(0,Int(x.first)), max, kv; init=0)
m, n = size
(m mmax && n nmax) || throw(DimensionMismatch("invalid size=$size"))
(m mmax && n nmax) || throw(DimensionMismatch(lazy"invalid size=$size"))
return m, n
end
function diagm_container(size, kv::Pair{<:Integer,<:AbstractVector}...)
Expand Down Expand Up @@ -1645,7 +1645,7 @@ function cond(A::AbstractMatrix, p::Real=2)
end
end
end
throw(ArgumentError("p-norm must be 1, 2 or Inf, got $p"))
throw(ArgumentError(lazy"p-norm must be 1, 2 or Inf, got $p"))
end

## Lyapunov and Sylvester equation
Expand Down
34 changes: 17 additions & 17 deletions stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function setindex!(D::Diagonal, v, i::Int, j::Int)
if i == j
@inbounds D.diag[i] = v
elseif !iszero(v)
throw(ArgumentError("cannot set off-diagonal entry ($i, $j) to a nonzero value ($v)"))
throw(ArgumentError(lazy"cannot set off-diagonal entry ($i, $j) to a nonzero value ($v)"))
end
return v
end
Expand Down Expand Up @@ -280,13 +280,13 @@ Base.literal_pow(::typeof(^), D::Diagonal, ::Val{-1}) = inv(D) # for disambiguat
function _muldiag_size_check(A, B)
nA = size(A, 2)
mB = size(B, 1)
@noinline throw_dimerr(::AbstractMatrix, nA, mB) = throw(DimensionMismatch("second dimension of A, $nA, does not match first dimension of B, $mB"))
@noinline throw_dimerr(::AbstractVector, nA, mB) = throw(DimensionMismatch("second dimension of D, $nA, does not match length of V, $mB"))
@noinline throw_dimerr(::AbstractMatrix, nA, mB) = throw(DimensionMismatch(lazy"second dimension of A, $nA, does not match first dimension of B, $mB"))
@noinline throw_dimerr(::AbstractVector, nA, mB) = throw(DimensionMismatch(lazy"second dimension of D, $nA, does not match length of V, $mB"))
nA == mB || throw_dimerr(B, nA, mB)
return nothing
end
# the output matrix should have the same size as the non-diagonal input matrix or vector
@noinline throw_dimerr(szC, szA) = throw(DimensionMismatch("output matrix has size: $szC, but should have size $szA"))
@noinline throw_dimerr(szC, szA) = throw(DimensionMismatch(lazy"output matrix has size: $szC, but should have size $szA"))
_size_check_out(C, ::Diagonal, A) = _size_check_out(C, A)
_size_check_out(C, A, ::Diagonal) = _size_check_out(C, A)
_size_check_out(C, A::Diagonal, ::Diagonal) = _size_check_out(C, A)
Expand Down Expand Up @@ -432,7 +432,7 @@ function _rdiv!(B::AbstractVecOrMat, A::AbstractVecOrMat, D::Diagonal)
dd = D.diag
m, n = size(A, 1), size(A, 2)
if (k = length(dd)) != n
throw(DimensionMismatch("left hand side has $n columns but D is $k by $k"))
throw(DimensionMismatch(lazy"left hand side has $n columns but D is $k by $k"))
end
@inbounds for j in 1:n
ddj = dd[j]
Expand All @@ -458,8 +458,8 @@ function ldiv!(B::AbstractVecOrMat, D::Diagonal, A::AbstractVecOrMat)
d = length(dd)
m, n = size(A, 1), size(A, 2)
m′, n′ = size(B, 1), size(B, 2)
m == d || throw(DimensionMismatch("right hand side has $m rows but D is $d by $d"))
(m, n) == (m′, n′) || throw(DimensionMismatch("expect output to be $m by $n, but got $m′ by $n′"))
m == d || throw(DimensionMismatch(lazy"right hand side has $m rows but D is $d by $d"))
(m, n) == (m′, n′) || throw(DimensionMismatch(lazy"expect output to be $m by $n, but got $m′ by $n′"))
j = findfirst(iszero, D.diag)
isnothing(j) || throw(SingularException(j))
@inbounds for j = 1:n, i = 1:m
Expand All @@ -470,7 +470,7 @@ end

function _rdiv!(Dc::Diagonal, Db::Diagonal, Da::Diagonal)
n, k = length(Db.diag), length(Da.diag)
n == k || throw(DimensionMismatch("left hand side has $n columns but D is $k by $k"))
n == k || throw(DimensionMismatch(lazy"left hand side has $n columns but D is $k by $k"))
j = findfirst(iszero, Da.diag)
isnothing(j) || throw(SingularException(j))
Dc.diag .= Db.diag ./ Da.diag
Expand Down Expand Up @@ -498,10 +498,10 @@ function ldiv!(T::Tridiagonal, D::Diagonal, S::Union{SymTridiagonal,Tridiagonal}
m = size(S, 1)
dd = D.diag
if (k = length(dd)) != m
throw(DimensionMismatch("diagonal matrix is $k by $k but right hand side has $m rows"))
throw(DimensionMismatch(lazy"diagonal matrix is $k by $k but right hand side has $m rows"))
end
if length(T.d) != m
throw(DimensionMismatch("target matrix size $(size(T)) does not match input matrix size $(size(S))"))
throw(DimensionMismatch(lazy"target matrix size $(size(T)) does not match input matrix size $(size(S))"))
end
m == 0 && return T
j = findfirst(iszero, dd)
Expand Down Expand Up @@ -535,10 +535,10 @@ function _rdiv!(T::Tridiagonal, S::Union{SymTridiagonal,Tridiagonal}, D::Diagona
n = size(S, 2)
dd = D.diag
if (k = length(dd)) != n
throw(DimensionMismatch("left hand side has $n columns but D is $k by $k"))
throw(DimensionMismatch(lazy"left hand side has $n columns but D is $k by $k"))
end
if length(T.d) != n
throw(DimensionMismatch("target matrix size $(size(T)) does not match input matrix size $(size(S))"))
throw(DimensionMismatch(lazy"target matrix size $(size(T)) does not match input matrix size $(size(S))"))
end
n == 0 && return T
j = findfirst(iszero, dd)
Expand Down Expand Up @@ -608,7 +608,7 @@ end
valB = B.diag; nB = length(valB)
nC = checksquare(C)
@boundscheck nC == nA*nB ||
throw(DimensionMismatch("expect C to be a $(nA*nB)x$(nA*nB) matrix, got size $(nC)x$(nC)"))
throw(DimensionMismatch(lazy"expect C to be a $(nA*nB)x$(nA*nB) matrix, got size $(nC)x$(nC)"))
isempty(A) || isempty(B) || fill!(C, zero(A[1,1] * B[1,1]))
@inbounds for i = 1:nA, j = 1:nB
idx = (i-1)*nB+j
Expand Down Expand Up @@ -639,7 +639,7 @@ end
(mB, nB) = size(B)
(mC, nC) = size(C)
@boundscheck (mC, nC) == (mA * mB, nA * nB) ||
throw(DimensionMismatch("expect C to be a $(mA * mB)x$(nA * nB) matrix, got size $(mC)x$(nC)"))
throw(DimensionMismatch(lazy"expect C to be a $(mA * mB)x$(nA * nB) matrix, got size $(mC)x$(nC)"))
isempty(A) || isempty(B) || fill!(C, zero(A[1,1] * B[1,1]))
m = 1
@inbounds for j = 1:nA
Expand All @@ -662,7 +662,7 @@ end
(mB, nB) = size(B)
(mC, nC) = size(C)
@boundscheck (mC, nC) == (mA * mB, nA * nB) ||
throw(DimensionMismatch("expect C to be a $(mA * mB)x$(nA * nB) matrix, got size $(mC)x$(nC)"))
throw(DimensionMismatch(lazy"expect C to be a $(mA * mB)x$(nA * nB) matrix, got size $(mC)x$(nC)"))
isempty(A) || isempty(B) || fill!(C, zero(A[1,1] * B[1,1]))
m = 1
@inbounds for j = 1:nA
Expand Down Expand Up @@ -875,15 +875,15 @@ dot(x::AbstractVector, D::Diagonal, y::AbstractVector) = _mapreduce_prod(dot, x,

dot(A::Diagonal, B::Diagonal) = dot(A.diag, B.diag)
function dot(D::Diagonal, B::AbstractMatrix)
size(D) == size(B) || throw(DimensionMismatch("Matrix sizes $(size(D)) and $(size(B)) differ"))
size(D) == size(B) || throw(DimensionMismatch(lazy"Matrix sizes $(size(D)) and $(size(B)) differ"))
return dot(D.diag, view(B, diagind(B, IndexStyle(B))))
end

dot(A::AbstractMatrix, B::Diagonal) = conj(dot(B, A))

function _mapreduce_prod(f, x, D::Diagonal, y)
if !(length(x) == length(D.diag) == length(y))
throw(DimensionMismatch("x has length $(length(x)), D has size $(size(D)), and y has $(length(y))"))
throw(DimensionMismatch(lazy"x has length $(length(x)), D has size $(size(D)), and y has $(length(y))"))
end
if isempty(x) && isempty(D) && isempty(y)
return zero(promote_op(f, eltype(x), eltype(D), eltype(y)))
Expand Down
Loading

0 comments on commit 7099bdd

Please sign in to comment.