Skip to content

Commit

Permalink
Type asesrtions in BandedMatrix conversion (#159)
Browse files Browse the repository at this point in the history
* return type assertions in BandedMatrix

* concrete printshow

* assertions with variables
  • Loading branch information
jishnub authored Aug 14, 2022
1 parent 55d0e2b commit fbf61e3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
10 changes: 7 additions & 3 deletions src/Operators/general/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ function getindex(P::TimesOperator,k::AbstractVector)
vec(Matrix(P[1:1,k]))
end

_rettype(::Type{BandedMatrix{T}}) where {T} = BandedMatrix{T,Matrix{T},Base.OneTo{Int}}
_rettype(T) = T
for TYP in (:Matrix, :BandedMatrix, :RaggedMatrix)
@eval function $TYP(V::SubOperator{T,TO,Tuple{UnitRange{Int},UnitRange{Int}}}) where {T,TO<:TimesOperator}
P = parent(V)
Expand Down Expand Up @@ -406,12 +408,14 @@ for TYP in (:Matrix, :BandedMatrix, :RaggedMatrix)

# The following returns a banded Matrix with all rows
# for large k its upper triangular
BA = convert($TYP{T}, P.ops[end][krl[end,1]:krl[end,2],jr])
RT = $TYP{T}
RT2 = _rettype(RT)
BA::RT2 = convert(RT, P.ops[end][krl[end,1]:krl[end,2],jr])::RT
for m = (length(P.ops)-1):-1:1
BA = convert($TYP{T}, P.ops[m][krl[m,1]:krl[m,2],krl[m+1,1]:krl[m+1,2]])*BA
BA = convert(RT, P.ops[m][krl[m,1]:krl[m,2],krl[m+1,1]:krl[m+1,2]])::RT * BA
end

$TYP{T}(BA)
RT(BA)::RT2
end
end

Expand Down
28 changes: 14 additions & 14 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ summarystr(B::Operator) = string(typeof(B).name.name, " : ", domainspace(B), "
summary(io::IO, B::Operator) = print(io, summarystr(B))

struct PrintShow
str
c::Char
end
Base.show(io::IO,N::PrintShow) = print(io,N.str)
Base.show(io::IO, N::PrintShow) = print(io, N.c)

show(io::IO, B::Operator; kw...) = summary(io, B)

Expand All @@ -49,57 +49,57 @@ function show(io::IO, ::MIME"text/plain", B::Operator;header::Bool=true)
if isbanded(B) && isinf(size(B,1)) && isinf(size(B,2))
BM=B[1:10,1:10]

M=Matrix{Any}(undef,11,11)
fill!(M,PrintShow(""))
M=Matrix{Union{eltype(B), PrintShow}}(undef,11,11)
fill!(M,PrintShow(''))
for j = 1:size(BM,2),k = colrange(BM,j)
M[k,j]=BM[k,j]
end

for k=max(1,11-bandwidth(B,2)):11
M[k,end]=PrintShow("")
M[k,end]=PrintShow('')
end
for j=max(1,11-bandwidth(B,1)):10
M[end,j]=PrintShow("")
M[end,j]=PrintShow('')
end

print_array(io, M)
elseif isinf(size(B,1)) && isinf(size(B,2))
BM=B[1:10,1:10]

M=Matrix{Any}(undef,11,11)
M=Matrix{Union{eltype(B), PrintShow}}(undef,11,11)
for k=1:10,j=1:10
M[k,j]=BM[k,j]
end

M[1,end]=PrintShow("")
M[end,1]=PrintShow("")
M[1,end]=PrintShow('')
M[end,1]=PrintShow('')

for k=2:11
M[k,end]=M[end,k]=PrintShow("")
M[k,end]=M[end,k]=PrintShow('')
end

print_array(io, M)
elseif isinf(size(B,1))
BM=B[1:10,1:size(B,2)]

M=Matrix{Any}(undef,11,size(B,2))
M=Matrix{Union{eltype(B), PrintShow}}(undef,11,size(B,2))
for k=1:10,j=1:size(B,2)
M[k,j]=BM[k,j]
end
for k=1:size(B,2)
M[end,k]=PrintShow("")
M[end,k]=PrintShow('')
end

print_array(io, M)
elseif isinf(size(B,2))
BM=B[1:size(B,1),1:10]

M=Matrix{Any}(undef,size(B,1),11)
M=Matrix{Union{eltype(B), PrintShow}}(undef,size(B,1),11)
for k=1:size(B,1),j=1:10
M[k,j]=BM[k,j]
end
for k=1:size(B,1)
M[k,end]=PrintShow("")
M[k,end]=PrintShow('')
end

print_array(io, M)
Expand Down

2 comments on commit fbf61e3

@jishnub
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/66216

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.9 -m "<description of version>" fbf61e307450dd1ddf9e7419b9f301880ae4fb6d
git push origin v0.6.9

Please sign in to comment.