Skip to content

Commit 690acfd

Browse files
jishnubKristofferC
authored andcommitted
LinearAlgebra: LazyString in interpolated error messages (#53976)
(cherry picked from commit 7099bdd)
1 parent 08e1fc0 commit 690acfd

File tree

17 files changed

+271
-271
lines changed

17 files changed

+271
-271
lines changed

stdlib/LinearAlgebra/src/abstractq.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ end
149149
# generically, treat AbstractQ like a matrix with its definite size
150150
qsize_check(Q::AbstractQ, B::AbstractVecOrMat) =
151151
size(Q, 2) == size(B, 1) ||
152-
throw(DimensionMismatch("second dimension of Q, $(size(Q,2)), must coincide with first dimension of B, $(size(B,1))"))
152+
throw(DimensionMismatch(lazy"second dimension of Q, $(size(Q,2)), must coincide with first dimension of B, $(size(B,1))"))
153153
qsize_check(A::AbstractVecOrMat, Q::AbstractQ) =
154154
size(A, 2) == size(Q, 1) ||
155-
throw(DimensionMismatch("second dimension of A, $(size(A,2)), must coincide with first dimension of Q, $(size(Q,1))"))
155+
throw(DimensionMismatch(lazy"second dimension of A, $(size(A,2)), must coincide with first dimension of Q, $(size(Q,1))"))
156156
qsize_check(Q::AbstractQ, P::AbstractQ) =
157157
size(Q, 2) == size(P, 1) ||
158-
throw(DimensionMismatch("second dimension of A, $(size(Q,2)), must coincide with first dimension of B, $(size(P,1))"))
158+
throw(DimensionMismatch(lazy"second dimension of A, $(size(Q,2)), must coincide with first dimension of B, $(size(P,1))"))
159159

160160
# mimic the AbstractArray fallback
161161
*(Q::AbstractQ{<:Number}) = Q
@@ -317,7 +317,7 @@ function lmul!(A::QRPackedQ, B::AbstractVecOrMat)
317317
mA, nA = size(A.factors)
318318
mB, nB = size(B,1), size(B,2)
319319
if mA != mB
320-
throw(DimensionMismatch("matrix A has dimensions ($mA,$nA) but B has dimensions ($mB, $nB)"))
320+
throw(DimensionMismatch(lazy"matrix A has dimensions ($mA,$nA) but B has dimensions ($mB, $nB)"))
321321
end
322322
Afactors = A.factors
323323
@inbounds begin
@@ -353,7 +353,7 @@ function lmul!(adjA::AdjointQ{<:Any,<:QRPackedQ}, B::AbstractVecOrMat)
353353
mA, nA = size(A.factors)
354354
mB, nB = size(B,1), size(B,2)
355355
if mA != mB
356-
throw(DimensionMismatch("matrix A has dimensions ($mA,$nA) but B has dimensions ($mB, $nB)"))
356+
throw(DimensionMismatch(lazy"matrix A has dimensions ($mA,$nA) but B has dimensions ($mB, $nB)"))
357357
end
358358
Afactors = A.factors
359359
@inbounds begin
@@ -384,7 +384,7 @@ function rmul!(A::AbstractVecOrMat, Q::QRPackedQ)
384384
mQ, nQ = size(Q.factors)
385385
mA, nA = size(A,1), size(A,2)
386386
if nA != mQ
387-
throw(DimensionMismatch("matrix A has dimensions ($mA,$nA) but matrix Q has dimensions ($mQ, $nQ)"))
387+
throw(DimensionMismatch(lazy"matrix A has dimensions ($mA,$nA) but matrix Q has dimensions ($mQ, $nQ)"))
388388
end
389389
Qfactors = Q.factors
390390
@inbounds begin
@@ -420,7 +420,7 @@ function rmul!(A::AbstractVecOrMat, adjQ::AdjointQ{<:Any,<:QRPackedQ})
420420
mQ, nQ = size(Q.factors)
421421
mA, nA = size(A,1), size(A,2)
422422
if nA != mQ
423-
throw(DimensionMismatch("matrix A has dimensions ($mA,$nA) but matrix Q has dimensions ($mQ, $nQ)"))
423+
throw(DimensionMismatch(lazy"matrix A has dimensions ($mA,$nA) but matrix Q has dimensions ($mQ, $nQ)"))
424424
end
425425
Qfactors = Q.factors
426426
@inbounds begin
@@ -521,10 +521,10 @@ rmul!(X::Adjoint{T,<:StridedVecOrMat{T}}, adjQ::AdjointQ{<:Any,<:HessenbergQ{T}}
521521
# flexible left-multiplication (and adjoint right-multiplication)
522522
qsize_check(Q::Union{QRPackedQ,QRCompactWYQ,HessenbergQ}, B::AbstractVecOrMat) =
523523
size(B, 1) in size(Q.factors) ||
524-
throw(DimensionMismatch("first dimension of B, $(size(B,1)), must equal one of the dimensions of Q, $(size(Q.factors))"))
524+
throw(DimensionMismatch(lazy"first dimension of B, $(size(B,1)), must equal one of the dimensions of Q, $(size(Q.factors))"))
525525
qsize_check(A::AbstractVecOrMat, adjQ::AdjointQ{<:Any,<:Union{QRPackedQ,QRCompactWYQ,HessenbergQ}}) =
526526
(Q = adjQ.Q; size(A, 2) in size(Q.factors) ||
527-
throw(DimensionMismatch("second dimension of A, $(size(A,2)), must equal one of the dimensions of Q, $(size(Q.factors))")))
527+
throw(DimensionMismatch(lazy"second dimension of A, $(size(A,2)), must equal one of the dimensions of Q, $(size(Q.factors))")))
528528

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

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

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

568568
# in-place right-application of LQPackedQs
569569
# these methods require that the applied-to matrix's (A's) number of columns

stdlib/LinearAlgebra/src/adjtrans.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ tr(A::Transpose) = transpose(tr(parent(A)))
465465
function _dot_nonrecursive(u, v)
466466
lu = length(u)
467467
if lu != length(v)
468-
throw(DimensionMismatch("first array has length $(lu) which does not match the length of the second, $(length(v))."))
468+
throw(DimensionMismatch(lazy"first array has length $(lu) which does not match the length of the second, $(length(v))."))
469469
end
470470
if lu == 0
471471
zero(eltype(u)) * zero(eltype(v))

stdlib/LinearAlgebra/src/bidiag.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct Bidiagonal{T,V<:AbstractVector{T}} <: AbstractMatrix{T}
88
function Bidiagonal{T,V}(dv, ev, uplo::AbstractChar) where {T,V<:AbstractVector{T}}
99
require_one_based_indexing(dv, ev)
1010
if length(ev) != max(length(dv)-1, 0)
11-
throw(DimensionMismatch("length of diagonal vector is $(length(dv)), length of off-diagonal vector is $(length(ev))"))
11+
throw(DimensionMismatch(lazy"length of diagonal vector is $(length(dv)), length of off-diagonal vector is $(length(ev))"))
1212
end
1313
(uplo != 'U' && uplo != 'L') && throw_uplo()
1414
new{T,V}(dv, ev, uplo)
@@ -443,11 +443,11 @@ function check_A_mul_B!_sizes(C, A, B)
443443
mB, nB = size(B)
444444
mC, nC = size(C)
445445
if mA != mC
446-
throw(DimensionMismatch("first dimension of A, $mA, and first dimension of output C, $mC, must match"))
446+
throw(DimensionMismatch(lazy"first dimension of A, $mA, and first dimension of output C, $mC, must match"))
447447
elseif nA != mB
448-
throw(DimensionMismatch("second dimension of A, $nA, and first dimension of B, $mB, must match"))
448+
throw(DimensionMismatch(lazy"second dimension of A, $nA, and first dimension of B, $mB, must match"))
449449
elseif nB != nC
450-
throw(DimensionMismatch("second dimension of output C, $nC, and second dimension of B, $nB, must match"))
450+
throw(DimensionMismatch(lazy"second dimension of output C, $nC, and second dimension of B, $nB, must match"))
451451
end
452452
end
453453

@@ -567,10 +567,10 @@ function _mul!(C::AbstractVecOrMat, A::BiTriSym, B::AbstractVecOrMat, _add::MulA
567567
nA = size(A,1)
568568
nB = size(B,2)
569569
if !(size(C,1) == size(B,1) == nA)
570-
throw(DimensionMismatch("A has first dimension $nA, B has $(size(B,1)), C has $(size(C,1)) but all must match"))
570+
throw(DimensionMismatch(lazy"A has first dimension $nA, B has $(size(B,1)), C has $(size(C,1)) but all must match"))
571571
end
572572
if size(C,2) != nB
573-
throw(DimensionMismatch("A has second dimension $nA, B has $(size(B,2)), C has $(size(C,2)) but all must match"))
573+
throw(DimensionMismatch(lazy"A has second dimension $nA, B has $(size(B,2)), C has $(size(C,2)) but all must match"))
574574
end
575575
iszero(nA) && return C
576576
iszero(_add.alpha) && return _rmul_or_fill!(C, _add.beta)
@@ -768,11 +768,11 @@ function ldiv!(c::AbstractVecOrMat, A::Bidiagonal, b::AbstractVecOrMat)
768768
N = size(A, 2)
769769
mb, nb = size(b, 1), size(b, 2)
770770
if N != mb
771-
throw(DimensionMismatch("second dimension of A, $N, does not match first dimension of b, $mb"))
771+
throw(DimensionMismatch(lazy"second dimension of A, $N, does not match first dimension of b, $mb"))
772772
end
773773
mc, nc = size(c, 1), size(c, 2)
774774
if mc != mb || nc != nb
775-
throw(DimensionMismatch("size of result, ($mc, $nc), does not match the size of b, ($mb, $nb)"))
775+
throw(DimensionMismatch(lazy"size of result, ($mc, $nc), does not match the size of b, ($mb, $nb)"))
776776
end
777777

778778
if N == 0
@@ -838,11 +838,11 @@ function _rdiv!(C::AbstractMatrix, A::AbstractMatrix, B::Bidiagonal)
838838
require_one_based_indexing(C, A, B)
839839
m, n = size(A)
840840
if size(B, 1) != n
841-
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
841+
throw(DimensionMismatch(lazy"right hand side B needs first dimension of size $n, has size $(size(B,1))"))
842842
end
843843
mc, nc = size(C)
844844
if mc != m || nc != n
845-
throw(DimensionMismatch("expect output to have size ($m, $n), but got ($mc, $nc)"))
845+
throw(DimensionMismatch(lazy"expect output to have size ($m, $n), but got ($mc, $nc)"))
846846
end
847847

848848
zi = findfirst(iszero, B.dv)

stdlib/LinearAlgebra/src/dense.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ function diagm_size(size::Tuple{Int,Int}, kv::Pair{<:Integer,<:AbstractVector}..
336336
mmax = mapreduce(x -> length(x.second) - min(0,Int(x.first)), max, kv; init=0)
337337
nmax = mapreduce(x -> length(x.second) + max(0,Int(x.first)), max, kv; init=0)
338338
m, n = size
339-
(m mmax && n nmax) || throw(DimensionMismatch("invalid size=$size"))
339+
(m mmax && n nmax) || throw(DimensionMismatch(lazy"invalid size=$size"))
340340
return m, n
341341
end
342342
function diagm_container(size, kv::Pair{<:Integer,<:AbstractVector}...)
@@ -1645,7 +1645,7 @@ function cond(A::AbstractMatrix, p::Real=2)
16451645
end
16461646
end
16471647
end
1648-
throw(ArgumentError("p-norm must be 1, 2 or Inf, got $p"))
1648+
throw(ArgumentError(lazy"p-norm must be 1, 2 or Inf, got $p"))
16491649
end
16501650

16511651
## Lyapunov and Sylvester equation

stdlib/LinearAlgebra/src/diagonal.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function setindex!(D::Diagonal, v, i::Int, j::Int)
190190
if i == j
191191
@inbounds D.diag[i] = v
192192
elseif !iszero(v)
193-
throw(ArgumentError("cannot set off-diagonal entry ($i, $j) to a nonzero value ($v)"))
193+
throw(ArgumentError(lazy"cannot set off-diagonal entry ($i, $j) to a nonzero value ($v)"))
194194
end
195195
return v
196196
end
@@ -279,13 +279,13 @@ Base.literal_pow(::typeof(^), D::Diagonal, ::Val{-1}) = inv(D) # for disambiguat
279279
function _muldiag_size_check(A, B)
280280
nA = size(A, 2)
281281
mB = size(B, 1)
282-
@noinline throw_dimerr(::AbstractMatrix, nA, mB) = throw(DimensionMismatch("second dimension of A, $nA, does not match first dimension of B, $mB"))
283-
@noinline throw_dimerr(::AbstractVector, nA, mB) = throw(DimensionMismatch("second dimension of D, $nA, does not match length of V, $mB"))
282+
@noinline throw_dimerr(::AbstractMatrix, nA, mB) = throw(DimensionMismatch(lazy"second dimension of A, $nA, does not match first dimension of B, $mB"))
283+
@noinline throw_dimerr(::AbstractVector, nA, mB) = throw(DimensionMismatch(lazy"second dimension of D, $nA, does not match length of V, $mB"))
284284
nA == mB || throw_dimerr(B, nA, mB)
285285
return nothing
286286
end
287287
# the output matrix should have the same size as the non-diagonal input matrix or vector
288-
@noinline throw_dimerr(szC, szA) = throw(DimensionMismatch("output matrix has size: $szC, but should have size $szA"))
288+
@noinline throw_dimerr(szC, szA) = throw(DimensionMismatch(lazy"output matrix has size: $szC, but should have size $szA"))
289289
_size_check_out(C, ::Diagonal, A) = _size_check_out(C, A)
290290
_size_check_out(C, A, ::Diagonal) = _size_check_out(C, A)
291291
_size_check_out(C, A::Diagonal, ::Diagonal) = _size_check_out(C, A)
@@ -432,7 +432,7 @@ function _rdiv!(B::AbstractVecOrMat, A::AbstractVecOrMat, D::Diagonal)
432432
dd = D.diag
433433
m, n = size(A, 1), size(A, 2)
434434
if (k = length(dd)) != n
435-
throw(DimensionMismatch("left hand side has $n columns but D is $k by $k"))
435+
throw(DimensionMismatch(lazy"left hand side has $n columns but D is $k by $k"))
436436
end
437437
@inbounds for j in 1:n
438438
ddj = dd[j]
@@ -459,8 +459,8 @@ function ldiv!(B::AbstractVecOrMat, D::Diagonal, A::AbstractVecOrMat)
459459
d = length(dd)
460460
m, n = size(A, 1), size(A, 2)
461461
m′, n′ = size(B, 1), size(B, 2)
462-
m == d || throw(DimensionMismatch("right hand side has $m rows but D is $d by $d"))
463-
(m, n) == (m′, n′) || throw(DimensionMismatch("expect output to be $m by $n, but got $m′ by $n′"))
462+
m == d || throw(DimensionMismatch(lazy"right hand side has $m rows but D is $d by $d"))
463+
(m, n) == (m′, n′) || throw(DimensionMismatch(lazy"expect output to be $m by $n, but got $m′ by $n′"))
464464
j = findfirst(iszero, D.diag)
465465
isnothing(j) || throw(SingularException(j))
466466
@inbounds for j = 1:n, i = 1:m
@@ -474,7 +474,7 @@ end
474474
/(A::Diagonal, D::Diagonal) = _rdiv!(matprod_dest(A, D, promote_op(/, eltype(A), eltype(D))), A, D)
475475
function _rdiv!(Dc::Diagonal, Db::Diagonal, Da::Diagonal)
476476
n, k = length(Db.diag), length(Da.diag)
477-
n == k || throw(DimensionMismatch("left hand side has $n columns but D is $k by $k"))
477+
n == k || throw(DimensionMismatch(lazy"left hand side has $n columns but D is $k by $k"))
478478
j = findfirst(iszero, Da.diag)
479479
isnothing(j) || throw(SingularException(j))
480480
Dc.diag .= Db.diag ./ Da.diag
@@ -502,10 +502,10 @@ function ldiv!(T::Tridiagonal, D::Diagonal, S::Union{SymTridiagonal,Tridiagonal}
502502
m = size(S, 1)
503503
dd = D.diag
504504
if (k = length(dd)) != m
505-
throw(DimensionMismatch("diagonal matrix is $k by $k but right hand side has $m rows"))
505+
throw(DimensionMismatch(lazy"diagonal matrix is $k by $k but right hand side has $m rows"))
506506
end
507507
if length(T.d) != m
508-
throw(DimensionMismatch("target matrix size $(size(T)) does not match input matrix size $(size(S))"))
508+
throw(DimensionMismatch(lazy"target matrix size $(size(T)) does not match input matrix size $(size(S))"))
509509
end
510510
m == 0 && return T
511511
j = findfirst(iszero, dd)
@@ -539,10 +539,10 @@ function _rdiv!(T::Tridiagonal, S::Union{SymTridiagonal,Tridiagonal}, D::Diagona
539539
n = size(S, 2)
540540
dd = D.diag
541541
if (k = length(dd)) != n
542-
throw(DimensionMismatch("left hand side has $n columns but D is $k by $k"))
542+
throw(DimensionMismatch(lazy"left hand side has $n columns but D is $k by $k"))
543543
end
544544
if length(T.d) != n
545-
throw(DimensionMismatch("target matrix size $(size(T)) does not match input matrix size $(size(S))"))
545+
throw(DimensionMismatch(lazy"target matrix size $(size(T)) does not match input matrix size $(size(S))"))
546546
end
547547
n == 0 && return T
548548
j = findfirst(iszero, dd)
@@ -612,7 +612,7 @@ end
612612
valB = B.diag; nB = length(valB)
613613
nC = checksquare(C)
614614
@boundscheck nC == nA*nB ||
615-
throw(DimensionMismatch("expect C to be a $(nA*nB)x$(nA*nB) matrix, got size $(nC)x$(nC)"))
615+
throw(DimensionMismatch(lazy"expect C to be a $(nA*nB)x$(nA*nB) matrix, got size $(nC)x$(nC)"))
616616
isempty(A) || isempty(B) || fill!(C, zero(A[1,1] * B[1,1]))
617617
@inbounds for i = 1:nA, j = 1:nB
618618
idx = (i-1)*nB+j
@@ -643,7 +643,7 @@ end
643643
(mB, nB) = size(B)
644644
(mC, nC) = size(C)
645645
@boundscheck (mC, nC) == (mA * mB, nA * nB) ||
646-
throw(DimensionMismatch("expect C to be a $(mA * mB)x$(nA * nB) matrix, got size $(mC)x$(nC)"))
646+
throw(DimensionMismatch(lazy"expect C to be a $(mA * mB)x$(nA * nB) matrix, got size $(mC)x$(nC)"))
647647
isempty(A) || isempty(B) || fill!(C, zero(A[1,1] * B[1,1]))
648648
m = 1
649649
@inbounds for j = 1:nA
@@ -666,7 +666,7 @@ end
666666
(mB, nB) = size(B)
667667
(mC, nC) = size(C)
668668
@boundscheck (mC, nC) == (mA * mB, nA * nB) ||
669-
throw(DimensionMismatch("expect C to be a $(mA * mB)x$(nA * nB) matrix, got size $(mC)x$(nC)"))
669+
throw(DimensionMismatch(lazy"expect C to be a $(mA * mB)x$(nA * nB) matrix, got size $(mC)x$(nC)"))
670670
isempty(A) || isempty(B) || fill!(C, zero(A[1,1] * B[1,1]))
671671
m = 1
672672
@inbounds for j = 1:nA
@@ -879,15 +879,15 @@ dot(x::AbstractVector, D::Diagonal, y::AbstractVector) = _mapreduce_prod(dot, x,
879879

880880
dot(A::Diagonal, B::Diagonal) = dot(A.diag, B.diag)
881881
function dot(D::Diagonal, B::AbstractMatrix)
882-
size(D) == size(B) || throw(DimensionMismatch("Matrix sizes $(size(D)) and $(size(B)) differ"))
882+
size(D) == size(B) || throw(DimensionMismatch(lazy"Matrix sizes $(size(D)) and $(size(B)) differ"))
883883
return dot(D.diag, view(B, diagind(B, IndexStyle(B))))
884884
end
885885

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

888888
function _mapreduce_prod(f, x, D::Diagonal, y)
889889
if !(length(x) == length(D.diag) == length(y))
890-
throw(DimensionMismatch("x has length $(length(x)), D has size $(size(D)), and y has $(length(y))"))
890+
throw(DimensionMismatch(lazy"x has length $(length(x)), D has size $(size(D)), and y has $(length(y))"))
891891
end
892892
if isempty(x) && isempty(D) && isempty(y)
893893
return zero(promote_op(f, eltype(x), eltype(D), eltype(y)))

0 commit comments

Comments
 (0)