Skip to content
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

Unalias off-diagonals in Tridiagonal constructor #51763

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 0 additions & 3 deletions stdlib/LinearAlgebra/src/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,6 @@ end

# Initialize variables
info = 0
if dl === du
throw(ArgumentError("off-diagonals must not alias"))
end
fill!(du2, 0)

@inbounds begin
Expand Down
11 changes: 6 additions & 5 deletions stdlib/LinearAlgebra/src/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,13 @@ struct Tridiagonal{T,V<:AbstractVector{T}} <: AbstractMatrix{T}
"lengths of subdiagonal, diagonal and superdiagonal: ",
"($(length(dl)), $(length(d)), $(length(du)))")))
end
new{T,V}(dl, d, du)
new{T,V}(dl, d, Base.unalias(dl, du))
end
# constructor used in lu!
function Tridiagonal{T,V}(dl, d, du, du2) where {T,V<:AbstractVector{T}}
require_one_based_indexing(dl, d, du, du2)
# length checks?
new{T,V}(dl, d, du, du2)
new{T,V}(dl, d, Base.unalias(dl, du), du2)
end
end

Expand All @@ -491,6 +491,10 @@ solvers, but may be converted into a regular matrix with
[`convert(Array, _)`](@ref) (or `Array(_)` for short).
The lengths of `dl` and `du` must be one less than the length of `d`.

!!! note
The subdiagonal `dl` and the superdiagonal `du` must not be aliased to each other.
If aliasing is detected, the constructor will use a copy of `du` as its argument.

# Examples
```jldoctest
julia> dl = [1, 2, 3];
Expand Down Expand Up @@ -912,9 +916,6 @@ function ldiv!(A::Tridiagonal, B::AbstractVecOrMat)
dl = A.dl
d = A.d
du = A.du
if dl === du
throw(ArgumentError("off-diagonals of `A` must not alias"))
end

@inbounds begin
for i in 1:n-1
Expand Down
4 changes: 3 additions & 1 deletion stdlib/LinearAlgebra/test/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ dimg = randn(n)/2
dlu = convert.(eltya, [1, 1])
dia = convert.(eltya, [-2, -2, -2])
tri = Tridiagonal(dlu, dia, dlu)
@test_throws ArgumentError lu!(tri)
L = lu(tri)
@test lu!(tri) == L
@test UpperTriangular(tri) == L.U
end
end
@testset for eltyb in (Float32, Float64, ComplexF32, ComplexF64, Int)
Expand Down
10 changes: 9 additions & 1 deletion stdlib/LinearAlgebra/test/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ end
@test TT == Matrix(TT)
@test TT.dl === y
@test TT.d === x
@test TT.du === y
@test TT.du == y
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we document which off-diagonal is aliased to the argument of the constructor and which one may be just a copy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we should document that the off diagonals shouldn't be aliased, and leave the de-aliasing as an implementation detail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, as a Julia beginner and new user, I feel I might prefer to have a warning here along with the unaliasing if I alias dl and du (and not an error as earlier). The reason is that, in Julia, I always expect matrix assignments and calls to reuse the same memory. Silent unaliasing of dl and du would be (from my point of view) unexpected behaviour.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment to the docstring explaining that du is copied if aliasing is detected.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much, @jishnub. I'm not sure if it makes sense, in my comment above, I was actually referring to a runtime warning that would seem most correct to me as a beginner.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think runtime warnings may ruin performance. Moreover, we have the hint in the documentation, so it's not exactly "silent".

@test typeof(TT)(TT) === TT
end
ST = SymTridiagonal{elty}([1,2,3,4], [1,2,3])
Expand Down Expand Up @@ -521,6 +521,14 @@ end
@test Tridiagonal(4:5, 1:3, 1:2) == [1 1 0; 4 2 2; 0 5 3]
end

@testset "Prevent off-diagonal aliasing in Tridiagonal" begin
e = ones(4)
f = e[1:end-1]
T = Tridiagonal(f, 2e, f)
T ./= 10
@test all(==(0.1), f)
end

@testset "Issue #26994 (and the empty case)" begin
T = SymTridiagonal([1.0],[3.0])
x = ones(1)
Expand Down