Skip to content

Commit

Permalink
Fixed up bidiag too
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt committed Aug 13, 2015
1 parent 4d50e23 commit 095e0c4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
34 changes: 16 additions & 18 deletions base/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,42 +108,40 @@ ctranspose(M::Bidiagonal) = Bidiagonal(conj(M.dv), conj(M.ev), !M.isupper)
istriu(M::Bidiagonal) = M.isupper || all(M.ev .== 0)
istril(M::Bidiagonal) = !M.isupper || all(M.ev .== 0)

function tril(M::Bidiagonal, k::Integer=0)
function tril!(M::Bidiagonal, k::Integer=0)
n = length(M.dv)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif M.isupper && k < 0
return Bidiagonal(zeros(M.dv),zeros(M.ev),M.isupper)
fill!(M.dv,0)
fill!(M.ev,0)
elseif k < -1
return Bidiagonal(zeros(M.dv),zeros(M.ev),M.isupper)
elseif !M.isupper && k == 0
return M
fill!(M.dv,0)
fill!(M.ev,0)
elseif M.isupper && k == 0
return Bidiagonal(M.dv,zeros(M.ev),M.isupper)
fill!(M.ev,0)
elseif !M.isupper && k == -1
return Bidiagonal(zeros(M.dv),M.ev,M.isupper)
elseif k > 0
return M
fill!(M.dv,0)
end
return M
end

function triu(M::Bidiagonal, k::Integer=0)
function triu!(M::Bidiagonal, k::Integer=0)
n = length(M.dv)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif !M.isupper && k > 0
return Bidiagonal(zeros(M.dv),zeros(M.ev),M.isupper)
fill!(M.dv,0)
fill!(M.ev,0)
elseif k > 1
return Bidiagonal(zeros(M.dv),zeros(M.ev),M.isupper)
elseif M.isupper && k == 0
return M
fill!(M.dv,0)
fill!(M.ev,0)
elseif !M.isupper && k == 0
return Bidiagonal(M.dv,zeros(M.ev),M.isupper)
fill!(M.ev,0)
elseif M.isupper && k == 1
return Bidiagonal(zeros(M.dv),M.ev,M.isupper)
elseif k < 0
return M
fill!(M.dv,0)
end
return M
end

function diag{T}(M::Bidiagonal{T}, n::Integer=0)
Expand Down
43 changes: 23 additions & 20 deletions test/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,29 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
end

debug && println("triu and tril")
@test tril(Bidiagonal(dv,ev,'U'),-1) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test tril(Bidiagonal(dv,ev,'L'),-1) == Bidiagonal(zeros(dv),ev,'L')
@test tril(Bidiagonal(dv,ev,'U'),-2) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test tril(Bidiagonal(dv,ev,'L'),-2) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test tril(Bidiagonal(dv,ev,'U'),1) == Bidiagonal(dv,ev,'U')
@test tril(Bidiagonal(dv,ev,'L'),1) == Bidiagonal(dv,ev,'L')
@test tril(Bidiagonal(dv,ev,'U')) == Bidiagonal(dv,zeros(ev),'U')
@test tril(Bidiagonal(dv,ev,'L')) == Bidiagonal(dv,ev,'L')
@test_throws ArgumentError tril(Bidiagonal(dv,ev,'U'),n+1)

@test triu(Bidiagonal(dv,ev,'L'),1) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test triu(Bidiagonal(dv,ev,'U'),1) == Bidiagonal(zeros(dv),ev,'U')
@test triu(Bidiagonal(dv,ev,'U'),2) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test triu(Bidiagonal(dv,ev,'L'),2) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test triu(Bidiagonal(dv,ev,'U'),-1) == Bidiagonal(dv,ev,'U')
@test triu(Bidiagonal(dv,ev,'L'),-1) == Bidiagonal(dv,ev,'L')
@test triu(Bidiagonal(dv,ev,'L')) == Bidiagonal(dv,zeros(ev),'L')
@test triu(Bidiagonal(dv,ev,'U')) == Bidiagonal(dv,ev,'U')
@test_throws ArgumentError triu(Bidiagonal(dv,ev,'U'),n+1)

@test istril(Bidiagonal(dv,ev,'L'))
@test !istril(Bidiagonal(dv,ev,'U'))
@test tril!(Bidiagonal(dv,ev,'U'),-1) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test tril!(Bidiagonal(dv,ev,'L'),-1) == Bidiagonal(zeros(dv),ev,'L')
@test tril!(Bidiagonal(dv,ev,'U'),-2) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test tril!(Bidiagonal(dv,ev,'L'),-2) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test tril!(Bidiagonal(dv,ev,'U'),1) == Bidiagonal(dv,ev,'U')
@test tril!(Bidiagonal(dv,ev,'L'),1) == Bidiagonal(dv,ev,'L')
@test tril!(Bidiagonal(dv,ev,'U')) == Bidiagonal(dv,zeros(ev),'U')
@test tril!(Bidiagonal(dv,ev,'L')) == Bidiagonal(dv,ev,'L')
@test_throws ArgumentError tril!(Bidiagonal(dv,ev,'U'),n+1)

@test istriu(Bidiagonal(dv,ev,'U'))
@test !istriu(Bidiagonal(dv,ev,'L'))
@test triu!(Bidiagonal(dv,ev,'L'),1) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test triu!(Bidiagonal(dv,ev,'U'),1) == Bidiagonal(zeros(dv),ev,'U')
@test triu!(Bidiagonal(dv,ev,'U'),2) == Bidiagonal(zeros(dv),zeros(ev),'U')
@test triu!(Bidiagonal(dv,ev,'L'),2) == Bidiagonal(zeros(dv),zeros(ev),'L')
@test triu!(Bidiagonal(dv,ev,'U'),-1) == Bidiagonal(dv,ev,'U')
@test triu!(Bidiagonal(dv,ev,'L'),-1) == Bidiagonal(dv,ev,'L')
@test triu!(Bidiagonal(dv,ev,'L')) == Bidiagonal(dv,zeros(ev),'L')
@test triu!(Bidiagonal(dv,ev,'U')) == Bidiagonal(dv,ev,'U')
@test_throws ArgumentError triu!(Bidiagonal(dv,ev,'U'),n+1)

debug && println("Linear solver")
Tfull = full(T)
Expand Down

0 comments on commit 095e0c4

Please sign in to comment.