Skip to content

Construct LazyString in error paths for tridiag #54648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Changes from all commits
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
26 changes: 15 additions & 11 deletions stdlib/LinearAlgebra/src/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ issymmetric(S::SymTridiagonal) = true

tr(S::SymTridiagonal) = sum(S.dv)

@noinline function throw_diag_outofboundserror(n, sz)
sz1, sz2 = sz
throw(ArgumentError(LazyString(lazy"requested diagonal, $n, must be at least $(-sz1) ",
lazy"and at most $sz2 for an $(sz1)-by-$(sz2) matrix")))
end

function diag(M::SymTridiagonal{T}, n::Integer=0) where T<:Number
# every branch call similar(..., ::Int) to make sure the
# same vector type is returned independent of n
Expand All @@ -194,8 +200,7 @@ function diag(M::SymTridiagonal{T}, n::Integer=0) where T<:Number
elseif absn <= size(M,1)
return fill!(similar(M.dv, size(M,1)-absn), zero(T))
else
throw(ArgumentError(string(lazy"requested diagonal, $n, must be at least $(-size(M, 1)) ",
lazy"and at most $(size(M, 2)) for an $(size(M, 1))-by-$(size(M, 2)) matrix")))
throw_diag_outofboundserror(n, size(M))
end
end
function diag(M::SymTridiagonal, n::Integer=0)
Expand All @@ -210,8 +215,7 @@ function diag(M::SymTridiagonal, n::Integer=0)
elseif n <= size(M,1)
throw(ArgumentError("requested diagonal contains undefined zeros of an array type"))
else
throw(ArgumentError(string(lazy"requested diagonal, $n, must be at least $(-size(M, 1)) ",
lazy"and at most $(size(M, 2)) for an $(size(M, 1))-by-$(size(M, 2)) matrix")))
throw_diag_outofboundserror(n, size(M))
end
end

Expand Down Expand Up @@ -353,7 +357,7 @@ isdiag(M::SymTridiagonal) = iszero(_evview(M))
function tril!(M::SymTridiagonal{T}, k::Integer=0) where T
n = length(M.dv)
if !(-n - 1 <= k <= n - 1)
throw(ArgumentError(string(lazy"the requested diagonal, $k, must be at least ",
throw(ArgumentError(LazyString(lazy"the requested diagonal, $k, must be at least ",
lazy"$(-n - 1) and at most $(n - 1) in an $n-by-$n matrix")))
elseif k < -1
fill!(M.ev, zero(T))
Expand All @@ -372,7 +376,7 @@ end
function triu!(M::SymTridiagonal{T}, k::Integer=0) where T
n = length(M.dv)
if !(-n + 1 <= k <= n + 1)
throw(ArgumentError(string(lazy"the requested diagonal, $k, must be at least ",
throw(ArgumentError(LazyString(lazy"the requested diagonal, $k, must be at least ",
lazy"$(-n + 1) and at most $(n + 1) in an $n-by-$n matrix")))
elseif k > 1
fill!(M.ev, zero(T))
Expand Down Expand Up @@ -485,7 +489,7 @@ struct Tridiagonal{T,V<:AbstractVector{T}} <: AbstractMatrix{T}
require_one_based_indexing(dl, d, du)
n = length(d)
if (length(dl) != n-1 || length(du) != n-1) && !(length(d) == 0 && length(dl) == 0 && length(du) == 0)
throw(ArgumentError(string("cannot construct Tridiagonal from incompatible ",
throw(ArgumentError(LazyString("cannot construct Tridiagonal from incompatible ",
"lengths of subdiagonal, diagonal and superdiagonal: ",
lazy"($(length(dl)), $(length(d)), $(length(du)))")))
end
Expand Down Expand Up @@ -658,7 +662,7 @@ function diag(M::Tridiagonal{T}, n::Integer=0) where T
elseif abs(n) <= size(M,1)
return fill!(similar(M.d, size(M,1)-abs(n)), zero(T))
else
throw(ArgumentError(string(lazy"requested diagonal, $n, must be at least $(-size(M, 1)) ",
throw(ArgumentError(LazyString(lazy"requested diagonal, $n, must be at least $(-size(M, 1)) ",
lazy"and at most $(size(M, 2)) for an $(size(M, 1))-by-$(size(M, 2)) matrix")))
end
end
Expand Down Expand Up @@ -724,7 +728,7 @@ end
elseif j - i == 1
@inbounds A.du[i] = x
elseif !iszero(x)
throw(ArgumentError(string(lazy"cannot set entry ($i, $j) off ",
throw(ArgumentError(LazyString(lazy"cannot set entry ($i, $j) off ",
lazy"the tridiagonal band to a nonzero value ($x)")))
end
return x
Expand Down Expand Up @@ -780,7 +784,7 @@ isdiag(M::Tridiagonal) = iszero(M.dl) && iszero(M.du)
function tril!(M::Tridiagonal{T}, k::Integer=0) where T
n = length(M.d)
if !(-n - 1 <= k <= n - 1)
throw(ArgumentError(string(lazy"the requested diagonal, $k, must be at least ",
throw(ArgumentError(LazyString(lazy"the requested diagonal, $k, must be at least ",
lazy"$(-n - 1) and at most $(n - 1) in an $n-by-$n matrix")))
elseif k < -1
fill!(M.dl, zero(T))
Expand All @@ -798,7 +802,7 @@ end
function triu!(M::Tridiagonal{T}, k::Integer=0) where T
n = length(M.d)
if !(-n + 1 <= k <= n + 1)
throw(ArgumentError(string(lazy"the requested diagonal, $k, must be at least ",
throw(ArgumentError(LazyString(lazy"the requested diagonal, $k, must be at least ",
lazy"$(-n + 1) and at most $(n + 1) in an $n-by-$n matrix")))
elseif k > 1
fill!(M.dl, zero(T))
Expand Down