Skip to content

Commit

Permalink
RFC: Added recursive transpose to Diagonal (#21019)
Browse files Browse the repository at this point in the history
* Added recursive transpose to `Diagonal`

`Diagonal` might be used to represent a block-diagonal matrix structure.

* Fixes plus expm, logm, sqrtm
  • Loading branch information
andyferris authored and andreasnoack committed Mar 16, 2017
1 parent 102800a commit b2795eb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
15 changes: 11 additions & 4 deletions base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ end
parent(D::Diagonal) = D.diag

ishermitian(D::Diagonal{<:Real}) = true
ishermitian(D::Diagonal) = isreal(D.diag)
issymmetric(D::Diagonal) = true
ishermitian(D::Diagonal{<:Number}) = isreal(D.diag)
ishermitian(D::Diagonal) = all(ishermitian, D.diag)
issymmetric(D::Diagonal{<:Number}) = true
issymmetric(D::Diagonal) = all(issymmetric, D.diag)
isposdef(D::Diagonal) = all(x -> x > 0, D.diag)

factorize(D::Diagonal) = D
Expand Down Expand Up @@ -260,8 +262,10 @@ end
@inline A_mul_Bc(D::Diagonal, rowvec::RowVector) = D*ctranspose(rowvec)

conj(D::Diagonal) = Diagonal(conj(D.diag))
transpose(D::Diagonal) = D
ctranspose(D::Diagonal) = conj(D)
transpose(D::Diagonal{<:Number}) = D
transpose(D::Diagonal) = Diagonal(transpose.(D.diag))
ctranspose(D::Diagonal{<:Number}) = conj(D)
ctranspose(D::Diagonal) = Diagonal(ctranspose.(D.diag))

diag(D::Diagonal) = D.diag
trace(D::Diagonal) = sum(D.diag)
Expand All @@ -275,8 +279,11 @@ end
eye{T}(::Type{Diagonal{T}}, n::Int) = Diagonal(ones(T,n))

expm(D::Diagonal) = Diagonal(exp.(D.diag))
expm(D::Diagonal{<:AbstractMatrix}) = Diagonal(expm.(D.diag))
logm(D::Diagonal) = Diagonal(log.(D.diag))
logm(D::Diagonal{<:AbstractMatrix}) = Diagonal(logm.(D.diag))
sqrtm(D::Diagonal) = Diagonal(sqrt.(D.diag))
sqrtm(D::Diagonal{<:AbstractMatrix}) = Diagonal(sqrtm.(D.diag))

#Linear solver
function A_ldiv_B!(D::Diagonal, B::StridedVecOrMat)
Expand Down
24 changes: 24 additions & 0 deletions test/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,27 @@ end
Q = qrfact(randn(5, 5))[:Q]
@test D * Q' == Array(D) * Q'
end

@testset "block diagonal matrices" begin
D = Diagonal([[1 2; 3 4], [1 2; 3 4]])
Dherm = Diagonal([[1 1+im; 1-im 1], [1 1+im; 1-im 1]])
Dsym = Diagonal([[1 1+im; 1+im 1], [1 1+im; 1+im 1]])
@test D' == Diagonal([[1 3; 2 4], [1 3; 2 4]])
@test D.' == Diagonal([[1 3; 2 4], [1 3; 2 4]])
@test Dherm' == Dherm
@test Dherm.' == Diagonal([[1 1-im; 1+im 1], [1 1-im; 1+im 1]])
@test Dsym' == Diagonal([[1 1-im; 1-im 1], [1 1-im; 1-im 1]])
@test Dsym.' == Dsym

@test issymmetric(D) == false
@test issymmetric(Dherm) == false
@test issymmetric(Dsym) == true

@test ishermitian(D) == false
@test ishermitian(Dherm) == true
@test ishermitian(Dsym) == false

@test expm(D) == Diagonal([expm([1 2; 3 4]), expm([1 2; 3 4])])
@test logm(D) == Diagonal([logm([1 2; 3 4]), logm([1 2; 3 4])])
@test sqrtm(D) == Diagonal([sqrtm([1 2; 3 4]), sqrtm([1 2; 3 4])])
end

0 comments on commit b2795eb

Please sign in to comment.