Skip to content

Commit

Permalink
Add real,imag,abs for special matrix types
Browse files Browse the repository at this point in the history
(cherry picked from commit 44826cb)
ref #13049
  • Loading branch information
kshyatt authored and tkelman committed Sep 11, 2015
1 parent 4d6ca2f commit 89a7b95
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion base/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function size(M::Bidiagonal, d::Integer)
end

#Elementary operations
for func in (:conj, :copy, :round, :trunc, :floor, :ceil)
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :real, :imag, :abs)
@eval ($func)(M::Bidiagonal) = Bidiagonal(($func)(M.dv), ($func)(M.ev), M.isupper)
end
for func in (:round, :trunc, :floor, :ceil)
Expand Down
4 changes: 4 additions & 0 deletions base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ isposdef(D::Diagonal) = all(D.diag .> 0)

factorize(D::Diagonal) = D

abs(D::Diagonal) = Diagonal(abs(D.diag))
real(D::Diagonal) = Diagonal(real(D.diag))
imag(D::Diagonal) = Diagonal(imag(D.diag))

istriu(D::Diagonal) = true
istril(D::Diagonal) = true
function triu!(D::Diagonal,k::Integer=0)
Expand Down
7 changes: 6 additions & 1 deletion base/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ for t in (:LowerTriangular, :UnitLowerTriangular, :UpperTriangular, :UnitUpperTr

real{T<:Real}(A::$t{T}) = A
real{T<:Complex}(A::$t{T}) = (B = real(A.data); $t(B))

abs(A::$t) = $t(abs(A.data))
end
end

imag(A::UpperTriangular) = UpperTriangular(imag(A.data))
imag(A::LowerTriangular) = LowerTriangular(imag(A.data))
imag(A::UnitLowerTriangular) = LowerTriangular(tril!(imag(A.data),-1))
imag(A::UnitUpperTriangular) = UpperTriangular(triu!(imag(A.data),1))

full(A::AbstractTriangular) = convert(Matrix, A)

fill!(A::AbstractTriangular, x) = (fill!(A.data, x); A)
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function size(A::SymTridiagonal, d::Integer)
end

#Elementary operations
for func in (:conj, :copy, :round, :trunc, :floor, :ceil)
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :abs, :real, :imag)
@eval ($func)(M::SymTridiagonal) = SymTridiagonal(($func)(M.dv), ($func)(M.ev))
end
for func in (:round, :trunc, :floor, :ceil)
Expand Down Expand Up @@ -315,7 +315,7 @@ end
copy!(dest::Tridiagonal, src::Tridiagonal) = Tridiagonal(copy!(dest.dl, src.dl), copy!(dest.d, src.d), copy!(dest.du, src.du), copy!(dest.du2, src.du2))

#Elementary operations
for func in (:conj, :copy, :round, :trunc, :floor, :ceil)
for func in (:conj, :copy, :round, :trunc, :floor, :ceil, :abs, :real, :imag)
@eval function ($func)(M::Tridiagonal)
Tridiagonal(($func)(M.dl), ($func)(M.d), ($func)(M.du), ($func)(M.du2))
end
Expand Down
3 changes: 3 additions & 0 deletions test/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
@test full(T) == diagm(dv) + diagm(ev, isupper?1:-1)
@test Bidiagonal(full(T), isupper) == T
@test big(T) == T
@test full(abs(T)) == abs(diagm(dv)) + abs(diagm(ev, isupper?1:-1))
@test full(real(T)) == real(diagm(dv)) + real(diagm(ev, isupper?1:-1))
@test full(imag(T)) == imag(diagm(dv)) + imag(diagm(ev, isupper?1:-1))
z = zeros(elty, n)

debug && println("Idempotent tests")
Expand Down
4 changes: 4 additions & 0 deletions test/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
@test typeof(convert(Diagonal{Complex64},D)) == Diagonal{Complex64}
@test typeof(convert(AbstractMatrix{Complex64},D)) == Diagonal{Complex64}

@test full(real(D)) == real(DM)
@test full(abs(D)) == abs(DM)
@test full(imag(D)) == imag(DM)

debug && println("Linear solve")
@test_approx_eq_eps D*v DM*v n*eps(relty)*(elty<:Complex ? 2:1)
@test_approx_eq_eps D*U DM*U n^2*eps(relty)*(elty<:Complex ? 2:1)
Expand Down
2 changes: 2 additions & 0 deletions test/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ for elty1 in (Float32, Float64, Complex64, Complex128, BigFloat, Int)

# real
@test full(real(A1)) == real(full(A1))
@test full(imag(A1)) == imag(full(A1))
@test full(abs(A1)) == abs(full(A1))

# Unary operations
@test full(-A1) == -full(A1)
Expand Down
7 changes: 7 additions & 0 deletions test/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ for elty in (Float32, Float64, Complex64, Complex128, Int)
@test transpose(T) == Tridiagonal(du, d, dl)
@test ctranspose(T) == Tridiagonal(conj(du), conj(d), conj(dl))

@test abs(T) == Tridiagonal(abs(dl),abs(d),abs(du))
@test real(T) == Tridiagonal(real(dl),real(d),real(du))
@test imag(T) == Tridiagonal(imag(dl),imag(d),imag(du))
@test abs(Ts) == SymTridiagonal(abs(d),abs(dl))
@test real(Ts) == SymTridiagonal(real(d),real(dl))
@test imag(Ts) == SymTridiagonal(imag(d),imag(dl))

# test interconversion of Tridiagonal and SymTridiagonal
@test Tridiagonal(dl, d, dl) == SymTridiagonal(d, dl)
@test SymTridiagonal(d, dl) == Tridiagonal(dl, d, dl)
Expand Down

0 comments on commit 89a7b95

Please sign in to comment.