Skip to content

fixes and stronger tests for Tridiagonal setindex! #20892

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 1 commit into from
Mar 6, 2017
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
13 changes: 8 additions & 5 deletions base/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -559,15 +559,18 @@ function getindex{T}(A::Tridiagonal{T}, i::Integer, j::Integer)
end

function setindex!(A::Tridiagonal, x, i::Integer, j::Integer)
@boundscheck checkbounds(A, i, j)
if i == j
A.d[i] = x
@inbounds A.d[i] = x
elseif i - j == 1
A.dl[j] = x
@inbounds A.dl[j] = x
elseif j - i == 1
A.du[i] = x
else
throw(ArgumentError("cannot set elements outside the sub, main, or super diagonals"))
@inbounds A.du[i] = x
elseif !iszero(x)
throw(ArgumentError(string("cannot set entry ($i, $j) off ",
"the tridiagonal band to a nonzero value ($x)")))
end
return x
end

## structured matrix methods ##
Expand Down
13 changes: 7 additions & 6 deletions test/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,13 @@ let n = 12 #Size of matrix problem to test
@test_throws BoundsError A[1,n+1]

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

Expand Down