Skip to content

Commit

Permalink
Merge pull request #19 from dlfivefifty/pull-request/a52672d1
Browse files Browse the repository at this point in the history
Add more convert methods
  • Loading branch information
andreasnoack committed Aug 29, 2016
2 parents af17cb0 + 327c6f5 commit d26e2c7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/ToeplitzMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ abstract AbstractToeplitz{T<:Number} <: AbstractMatrix{T}
size(A::AbstractToeplitz) = (size(A, 1), size(A, 2))
getindex(A::AbstractToeplitz, i::Integer) = A[mod(i, size(A,1)), div(i, size(A,1)) + 1]


convert(::Type{Matrix}, S::AbstractToeplitz) = full(S)
convert{T}(::Type{AbstractMatrix{T}}, S::AbstractToeplitz) = convert(AbstractToeplitz{T}, S)
convert{T}(::Type{AbstractArray{T}}, S::AbstractToeplitz) = convert(AbstractToeplitz{T}, S)


# Convert an abstract Toeplitz matrix to a full matrix
function full{T}(A::AbstractToeplitz{T})
m, n = size(A)
Expand Down Expand Up @@ -145,6 +151,10 @@ function Toeplitz(vc::Vector, vr::Vector)
return Toeplitz(vcp, vrp, dft*tmp, similar(tmp), dft)
end

convert{T}(::Type{AbstractToeplitz{T}},A::Toeplitz) = convert(Toeplitz{T},A)
convert{T}(::Type{Toeplitz{T}},A::Toeplitz) = Toeplitz(convert(Vector{T},A.vc),
convert(Vector{T},A.vr))

# Size of a general Toeplitz matrix
function size(A::Toeplitz, dim::Int)
if dim == 1
Expand Down Expand Up @@ -217,6 +227,9 @@ function SymmetricToeplitz{T<:BlasReal}(vc::Vector{T})
return SymmetricToeplitz(vc, dft*tmp, similar(tmp), dft)
end

convert{T}(::Type{AbstractToeplitz{T}},A::SymmetricToeplitz) = convert(SymmetricToeplitz{T},A)
convert{T}(::Type{SymmetricToeplitz{T}},A::SymmetricToeplitz) = SymmetricToeplitz(convert(Vector{T},A.vc))

function size(A::SymmetricToeplitz, dim::Int)
if 1 <= dim <= 2
return length(A.vc)
Expand Down Expand Up @@ -249,6 +262,10 @@ function Circulant(vc::Vector)
return Circulant(vc, fft(vc), tmp, plan_fft!(tmp))
end

convert{T}(::Type{AbstractToeplitz{T}},A::Circulant) = convert(Circulant{T},A)
convert{T}(::Type{Circulant{T}},A::Circulant) = Circulant(convert(Vector{T},A.vc))


function size(C::Circulant, dim::Integer)
if 1 <= dim <= 2
return length(C.vc)
Expand Down Expand Up @@ -367,6 +384,11 @@ function convert(::Type{Toeplitz}, A::TriangularToeplitz)
end
end

convert{T}(::Type{AbstractToeplitz{T}},A::TriangularToeplitz) = convert(TriangularToeplitz{T},A)
convert{T}(::Type{TriangularToeplitz{T}},A::TriangularToeplitz) =
TriangularToeplitz(convert(Vector{T},A.ve),A.uplo=='U'?(:U):(:L))


function size(A::TriangularToeplitz, dim::Int)
if dim == 1 || dim == 2
return length(A.ve)
Expand Down Expand Up @@ -509,6 +531,14 @@ function Hankel(vc,vr)
Hankel(Toeplitz(p[n:end],p[n:-1:1]))
end

convert(::Type{Array},A::Hankel) = convert(Matrix,A)
convert(::Type{Matrix},A::Hankel) = full(A)

convert{T}(::Type{AbstractMatrix{T}},A::Hankel) = convert(Hankel{T},A)
convert{T}(::Type{Hankel{T}},A::Hankel) = Hankel(convert(Toeplitz{T},A.T))



# Size
size(H::Hankel,k...) = size(H.T,k...)

Expand Down
43 changes: 43 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,46 @@ if isdir(Pkg.dir("FastTransforms"))
@test_approx_eq T*ones(BigFloat,n) full(T)*ones(BigFloat,n)
println("OK!")
end


println("Convert")

T = Toeplitz(ones(2),ones(2))

@test isa(convert(Matrix{Complex128},T),Matrix{Complex128})
@test isa(convert(AbstractMatrix{Complex128},T),Toeplitz{Complex128})
@test isa(convert(AbstractArray{Complex128},T),Toeplitz{Complex128})
@test isa(convert(ToeplitzMatrices.AbstractToeplitz{Complex128},T),Toeplitz{Complex128})
@test isa(convert(ToeplitzMatrices.Toeplitz{Complex128},T),Toeplitz{Complex128})

T = SymmetricToeplitz(ones(2))

@test isa(convert(Matrix{Float32},T),Matrix{Float32})
@test isa(convert(AbstractMatrix{Float32},T),SymmetricToeplitz{Float32})
@test isa(convert(AbstractArray{Float32},T),SymmetricToeplitz{Float32})
@test isa(convert(ToeplitzMatrices.AbstractToeplitz{Float32},T),SymmetricToeplitz{Float32})
@test isa(convert(ToeplitzMatrices.SymmetricToeplitz{Float32},T),SymmetricToeplitz{Float32})

T = Circulant(ones(2))

@test isa(convert(Matrix{Complex128},T),Matrix{Complex128})
@test isa(convert(AbstractMatrix{Complex128},T),Circulant{Complex128})
@test isa(convert(AbstractArray{Complex128},T),Circulant{Complex128})
@test isa(convert(ToeplitzMatrices.AbstractToeplitz{Complex128},T),Circulant{Complex128})
@test isa(convert(ToeplitzMatrices.Circulant{Complex128},T),Circulant{Complex128})

T = TriangularToeplitz(ones(2),:U)

@test isa(convert(Matrix{Complex128},T),Matrix{Complex128})
@test isa(convert(AbstractMatrix{Complex128},T),TriangularToeplitz{Complex128})
@test isa(convert(AbstractArray{Complex128},T),TriangularToeplitz{Complex128})
@test isa(convert(ToeplitzMatrices.AbstractToeplitz{Complex128},T),TriangularToeplitz{Complex128})
@test isa(convert(ToeplitzMatrices.TriangularToeplitz{Complex128},T),TriangularToeplitz{Complex128})


T = Hankel(ones(2),ones(2))

@test isa(convert(Matrix{Complex128},T),Matrix{Complex128})
@test isa(convert(AbstractMatrix{Complex128},T),Hankel{Complex128})
@test isa(convert(AbstractArray{Complex128},T),Hankel{Complex128})
@test isa(convert(ToeplitzMatrices.Hankel{Complex128},T),Hankel{Complex128})

0 comments on commit d26e2c7

Please sign in to comment.