Skip to content

Commit e3bbd26

Browse files
Sacha0andreasnoack
authored andcommitted
Fixes and stronger tests for SymTridiagonal setindex!. (#20901)
Makes SymTridiagonal setindex! no longer support off-diagonal assignment of any form. Strengthens tests for SymTridiagonal setindex!. Cleans up associated tests for broadcast! with structured-matrix destination.
1 parent 080dcf4 commit e3bbd26

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

base/linalg/tridiag.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,13 @@ function getindex{T}(A::SymTridiagonal{T}, i::Integer, j::Integer)
375375
end
376376

377377
function setindex!(A::SymTridiagonal, x, i::Integer, j::Integer)
378+
@boundscheck checkbounds(A, i, j)
378379
if i == j
379-
A.dv[i] = x
380-
elseif abs(i - j) == 1
381-
A.ev[min(i,j)] = x
380+
@inbounds A.dv[i] = x
382381
else
383-
throw(ArgumentError("cannot set elements outside the sub, main, or super diagonals"))
382+
throw(ArgumentError("cannot set off-diagonal entry ($i, $j)"))
384383
end
384+
return x
385385
end
386386

387387
## Tridiagonal matrices ##

test/linalg/tridiag.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,12 @@ let n = 12 #Size of matrix problem to test
266266
@test A[1,1] == a[1]
267267

268268
debug && println("setindex!")
269-
@test_throws ArgumentError A[n,1] = 1
270-
@test_throws ArgumentError A[1,n] = 1
271-
A[3,3] = A[3,3]
272-
A[2,3] = A[2,3]
273-
A[3,2] = A[3,2]
274-
@test A == fA
269+
@test_throws BoundsError A[n + 1, 1] = 0 # test bounds check
270+
@test_throws BoundsError A[1, n + 1] = 0 # test bounds check
271+
@test ((A[3, 3] = A[3, 3]) == A[3, 3]; A == fA) # test assignment on the main diagonal
272+
@test_throws ArgumentError A[3, 2] = 1 # test assignment on the subdiagonal
273+
@test_throws ArgumentError A[2, 3] = 1 # test assignment on the superdiagonal
274+
@test_throws ArgumentError A[1, 3] = 1 # test assignment off the main/sub/super diagonal
275275

276276
debug && println("Diagonal extraction")
277277
@test diag(A,1) == b

test/sparse/higherorderfns.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,18 +424,14 @@ end
424424
D = Diagonal(rand(N))
425425
B = Bidiagonal(rand(N), rand(N - 1), true)
426426
T = Tridiagonal(rand(N - 1), rand(N), rand(N - 1))
427-
S = SymTridiagonal(rand(N), rand(N - 1))
428-
# why some of the tests below are broken:
429-
# Diagonal setindex! allows setting off-diagonal entries to zero. Subtypes of
430-
# AbstractTriangular allow analogs.
431427
@test broadcast!(sin, copy(D), D) == Diagonal(sin.(D))
432428
@test broadcast!(sin, copy(B), B) == Bidiagonal(sin.(B), true)
433429
@test broadcast!(sin, copy(T), T) == Tridiagonal(sin.(T))
434-
@test_broken broadcast!(sin, copy(S), S) == SymTridiagonal(sin.(S))
435430
@test broadcast!(*, copy(D), D, A) == Diagonal(broadcast(*, D, A))
436431
@test broadcast!(*, copy(B), B, A) == Bidiagonal(broadcast(*, B, A), true)
437432
@test broadcast!(*, copy(T), T, A) == Tridiagonal(broadcast(*, T, A))
438-
@test_broken broadcast!(*, copy(S), T, sA) == SymTridiagonal(broadcast(*, T, sA))
433+
# SymTridiagonal (and similar symmetric matrix types) do not support setindex!
434+
# off the diagonal, and so cannot serve as a destination for broadcast!
439435
end
440436

441437
@testset "map[!] over combinations of sparse and structured matrices" begin

0 commit comments

Comments
 (0)