Skip to content

Commit

Permalink
fixed diagonal and added istrix
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt committed Aug 13, 2015
1 parent 7465943 commit 4d50e23
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 23 additions & 2 deletions base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,29 @@ isposdef(D::Diagonal) = all(D.diag .> 0)

factorize(D::Diagonal) = D

tril(D::Diagonal,i::Integer=0) = i == 0 ? D : zeros(D)
triu(D::Diagonal,i::Integer=0) = i == 0 ? D : zeros(D)
istriu(D::Diagonal) = true
istril(D::Diagonal) = true
function triu!(D::Diagonal,k::Integer=0)
n = size(D,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k != 0
fill!(D.diag,0)
end
return D
end
triu(D::Diagonal,k::Integer=0) = triu!(copy(D),k)

function tril!(D::Diagonal,k::Integer=0)
n = size(D,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k != 0
fill!(D.diag,0)
end
return D
end
tril(D::Diagonal,k::Integer=0) = tril!(copy(D),k)

==(Da::Diagonal, Db::Diagonal) = Da.diag == Db.diag
-(A::Diagonal)=Diagonal(-A.diag)
Expand Down
2 changes: 2 additions & 0 deletions test/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
@test_approx_eq D/D2 Diagonal(D.diag./D2.diag)

# test triu/tril
@test istriu(D)
@test istril(D)
@test triu(D,1) == zeros(D)
@test triu(D,0) == D
@test tril(D,1) == zeros(D)
Expand Down

0 comments on commit 4d50e23

Please sign in to comment.