Skip to content

Commit d8a0d0e

Browse files
committed
Fixes and stronger tests for SymTridiagonal setindex!.
Makes SymTridiagonal setindex! no longer support off-diagonal assignment of any form. Strengthens tests for SymTridiagonal setindex!.
1 parent ea8db45 commit d8a0d0e

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
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

0 commit comments

Comments
 (0)