Skip to content

Commit 04ced10

Browse files
committed
Fixes and stronger tests for Tridiagonal setindex!
Makes Tridiagonal setindex! support assigning zero off the main, sub, and super diagonals. Strengthens tests for Tridiagonal setindex!.
1 parent 145eae8 commit 04ced10

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

base/linalg/tridiag.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,15 +559,18 @@ function getindex{T}(A::Tridiagonal{T}, i::Integer, j::Integer)
559559
end
560560

561561
function setindex!(A::Tridiagonal, x, i::Integer, j::Integer)
562+
@boundscheck checkbounds(A, i, j)
562563
if i == j
563-
A.d[i] = x
564+
@inbounds A.d[i] = x
564565
elseif i - j == 1
565-
A.dl[j] = x
566+
@inbounds A.dl[j] = x
566567
elseif j - i == 1
567-
A.du[i] = x
568-
else
569-
throw(ArgumentError("cannot set elements outside the sub, main, or super diagonals"))
568+
@inbounds A.du[i] = x
569+
elseif !iszero(x)
570+
throw(ArgumentError(string("cannot set entry ($i, $j) off ",
571+
"the tridiagonal band to a nonzero value ($x)")))
570572
end
573+
return x
571574
end
572575

573576
## structured matrix methods ##

test/linalg/tridiag.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -459,12 +459,13 @@ let n = 12 #Size of matrix problem to test
459459
@test_throws BoundsError A[1,n+1]
460460

461461
debug && println("setindex!")
462-
@test_throws ArgumentError A[n,1] = 1
463-
@test_throws ArgumentError A[1,n] = 1
464-
A[3,3] = A[3,3]
465-
A[2,3] = A[2,3]
466-
A[3,2] = A[3,2]
467-
@test A == fA
462+
@test_throws BoundsError A[n + 1, 1] = 0 # test bounds check
463+
@test_throws BoundsError A[1, n + 1] = 0 # test bounds check
464+
@test (A[3, 3] = A[3, 3]; A == fA) # test assignment on the main diagonal
465+
@test (A[3, 2] = A[3, 2]; A == fA) # test assignment on the subdiagonal
466+
@test (A[2, 3] = A[2, 3]; A == fA) # test assignment on the superdiagonal
467+
@test ((A[1, 3] = 0) == 0; A == fA) # test zero assignment off the main/sub/super diagonal
468+
@test_throws ArgumentError A[1, 3] = 1 # test non-zero assignment off the main/sub/super diagonal
468469
end
469470
end
470471

0 commit comments

Comments
 (0)