Skip to content

Commit ceb351e

Browse files
authored
compact show for operators (#172)
* compact show for operators * reduce allocations * remove unnecessary comma * bump version to v0.6.15 * use show instead of print_array * tests for rowrange/colrange * bugfixes * revert to using print_array
1 parent c512b22 commit ceb351e

File tree

6 files changed

+86
-36
lines changed

6 files changed

+86
-36
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.6.14"
3+
version = "0.6.15"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/Operators/SubOperator.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,20 @@ function colstop(S::SubOperator{<:Any,<:Any,NTuple{2,UnitRange{Int}}},j::Integer
181181
min(n,findfirst(isequal(cs),kr))
182182
end
183183
end
184-
colstart(S::SubOperator{<:Any,<:Any,NTuple{2,UnitRange{Int}}},j::Integer) =
185-
max(findfirst(parentindices(S)[1],colstart(parent(S),parentindices(S)[2][j])),1)
186-
rowstart(S::SubOperator{<:Any,<:Any,NTuple{2,UnitRange{Int}}},j::Integer) =
187-
max(1,findfirst(parentindices(S)[2],rowstart(parent(S),parentindices(S)[1][j])))
188-
rowstop(S::SubOperator{<:Any,<:Any,NTuple{2,UnitRange{Int}}},j::Integer) =
189-
findfirst(parentindices(S)[2],rowstop(parent(S),parentindices(S)[1][j]))
184+
function colstart(S::SubOperator{<:Any,<:Any,NTuple{2,UnitRange{Int}}},j::Integer)
185+
cind = colstart(parent(S),parentindices(S)[2][j])
186+
ind = findfirst(==(cind), parentindices(S)[1])
187+
max(ind,1)
188+
end
189+
function rowstart(S::SubOperator{<:Any,<:Any,NTuple{2,UnitRange{Int}}},j::Integer)
190+
rind = rowstart(parent(S),parentindices(S)[1][j])
191+
ind = findfirst(==(rind), parentindices(S)[2])
192+
max(1,ind)
193+
end
194+
function rowstop(S::SubOperator{<:Any,<:Any,NTuple{2,UnitRange{Int}}},j::Integer)
195+
ind = rowstop(parent(S),parentindices(S)[1][j])
196+
findfirst(==(ind), parentindices(S)[2])
197+
end
190198

191199

192200
# blocks don't change

src/Operators/general/OperatorLayout.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ domain(P::AdjointOperator)=domain(P.op)
8282
bandwidths(P::AdjointOperator) = reverse(bandwidths(P.op))
8383

8484
getindex(P::AdjointOperator,k::Integer,j::Integer) = conj(P.op[j,k])
85+
getindex(P::AdjointOperator,inds...) = adjoint(P.op[reverse(inds)...])
8586

8687
function BandedMatrix(S::SubOperator{T,TO}) where {T,TO<:AdjointOperator}
8788
kr,jr=parentindices(S)

src/show.jl

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ Base.show(io::IO, N::PrintShow) = print(io, N.c)
4040

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

43-
function show(io::IO, ::MIME"text/plain", B::Operator;header::Bool=true)
43+
function show(io::IO, mimetype::MIME"text/plain", @nospecialize(B::Operator); header::Bool=true)
4444
header && summary(io, B)
4545
dsp = domainspace(B)
4646

47+
sz1_B, sz2_B = size(B)
48+
49+
iocompact = haskey(io, :compact) ? io : IOContext(io, :compact => true)
50+
4751
if !isambiguous(domainspace(B)) && (eltype(B) <: Number)
48-
println()
49-
if isbanded(B) && isinf(size(B,1)) && isinf(size(B,2))
52+
println(io)
53+
if isbanded(B) && isinf(sz1_B) && isinf(sz2_B)
5054
BM=B[1:10,1:10]
5155

5256
M=Matrix{Union{eltype(B), PrintShow}}(undef,11,11)
@@ -62,49 +66,56 @@ function show(io::IO, ::MIME"text/plain", B::Operator;header::Bool=true)
6266
M[end,j]=PrintShow('')
6367
end
6468

65-
print_array(io, M)
66-
elseif isinf(size(B,1)) && isinf(size(B,2))
69+
print_array(iocompact, M)
70+
elseif isinf(sz1_B) && isinf(sz2_B)
6771
BM=B[1:10,1:10]
6872

6973
M=Matrix{Union{eltype(B), PrintShow}}(undef,11,11)
70-
for k=1:10,j=1:10
71-
M[k,j]=BM[k,j]
74+
for I in CartesianIndices(axes(BM))
75+
M[I]=BM[Tuple(I)...] # not certain if indexing with CartesianIndex is implemented
7276
end
7377

7478
M[1,end]=PrintShow('')
7579
M[end,1]=PrintShow('')
7680

7781
for k=2:11
78-
M[k,end]=M[end,k]=PrintShow('')
82+
M[k,end]=PrintShow('')
83+
end
84+
for k=2:11
85+
M[end,k]=PrintShow('')
7986
end
8087

81-
print_array(io, M)
82-
elseif isinf(size(B,1))
83-
BM=B[1:10,1:size(B,2)]
88+
print_array(iocompact, M)
89+
elseif isinf(sz1_B)
90+
sz2int = Int(sz2_B)::Int
91+
BM=B[1:10,1:sz2int]
8492

85-
M=Matrix{Union{eltype(B), PrintShow}}(undef,11,size(B,2))
86-
for k=1:10,j=1:size(B,2)
87-
M[k,j]=BM[k,j]
93+
M=Matrix{Union{eltype(B), PrintShow}}(undef,11,sz2int)
94+
for I in CartesianIndices(axes(BM))
95+
M[I]=BM[Tuple(I)...]
8896
end
89-
for k=1:size(B,2)
97+
for k=1:sz2int
9098
M[end,k]=PrintShow('')
9199
end
92100

93-
print_array(io, M)
94-
elseif isinf(size(B,2))
95-
BM=B[1:size(B,1),1:10]
101+
print_array(iocompact, M)
102+
elseif isinf(sz2_B)
103+
sz1int = Int(sz1_B)::Int
104+
BM=B[1:sz1int,1:10]
96105

97-
M=Matrix{Union{eltype(B), PrintShow}}(undef,size(B,1),11)
98-
for k=1:size(B,1),j=1:10
99-
M[k,j]=BM[k,j]
106+
M=Matrix{Union{eltype(B), PrintShow}}(undef,sz1int,11)
107+
for I in CartesianIndices(axes(BM))
108+
M[I]=BM[Tuple(I)...]
100109
end
101-
for k=1:size(B,1)
110+
for k=1:sz1int
102111
M[k,end]=PrintShow('')
103112
end
104113

105-
print_array(io, M)
114+
print_array(iocompact, M)
106115
else
107-
print_array(io, AbstractMatrix(B)[1:size(B,1),1:size(B,2)])
116+
sz1int = Int(sz1_B)::Int
117+
sz2int = Int(sz2_B)::Int
118+
print_array(iocompact, AbstractMatrix(B)[1:sz1int,1:sz2int])
108119
end
109120
end
110121
end

test/SpacesTest.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using ApproxFunBase, Test
22
import ApproxFunBase: PointSpace, HeavisideSpace, PiecewiseSegment, dimension, Vec, checkpoints
33
using ApproxFunOrthogonalPolynomials
44
using StaticArrays
5+
using BandedMatrices: rowrange, colrange
56

67
@testset "Spaces" begin
78
@testset "PointSpace" begin
@@ -340,5 +341,12 @@ using StaticArrays
340341
@test Derivative(Chebyshev()) != Derivative(Chebyshev(), 2)
341342
@test Derivative(Chebyshev()) != Derivative(Legendre())
342343
end
344+
345+
@testset "SubOperator" begin
346+
D = Derivative(Chebyshev())
347+
S = @view D[1:10, 1:10]
348+
@test rowrange(S, 1) == 2:2
349+
@test colrange(S, 2) == 1:1
350+
end
343351
end
344352
end

test/show.jl

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@testset "show" begin
2+
io = IOBuffer()
23
@testset "Domain" begin
34
@testset "Segment" begin
45
s = Segment(0, 1)
@@ -31,16 +32,37 @@
3132
@testset "Operator" begin
3233
@testset "Derivative" begin
3334
D = Derivative()
34-
dsum = ApproxFunBase.summarystr(D)
35-
@test repr(D) == dsum
36-
io = IOBuffer()
35+
summarystr = ApproxFunBase.summarystr(D)
36+
@test repr(D) == summarystr
3737
show(io, MIME"text/plain"(), D)
38-
@test contains(String(take!(io)), dsum)
38+
@test contains(String(take!(io)), summarystr)
39+
40+
D = Derivative(Chebyshev())
41+
summarystr = ApproxFunBase.summarystr(D)
42+
show(io, MIME"text/plain"(), D)
43+
@test contains(String(take!(io)), summarystr)
44+
end
45+
@testset "SubOperator" begin
46+
D = Derivative(Chebyshev())
47+
S = @view D[1:10, 1:10]
48+
summarystr = ApproxFunBase.summarystr(S)
49+
show(io, MIME"text/plain"(), S)
50+
@test contains(String(take!(io)), summarystr)
51+
end
52+
@testset "Evaluation" begin
53+
E = Evaluation(Chebyshev(), 0)
54+
summarystr = ApproxFunBase.summarystr(E)
55+
show(io, MIME"text/plain"(), E)
56+
@test contains(String(take!(io)), summarystr)
57+
58+
EA = Evaluation(Chebyshev(), 0)'
59+
summarystr = ApproxFunBase.summarystr(EA)
60+
show(io, MIME"text/plain"(), EA)
61+
@test contains(String(take!(io)), summarystr)
3962
end
4063
@testset "QuotientSpace" begin
4164
Q = QuotientSpace(Dirichlet(ConstantSpace(0..1)))
4265
@test startswith(repr(Q), "ConstantSpace(0..1) /")
43-
io = IOBuffer()
4466
show(io, MIME"text/plain"(), Q)
4567
s = String(take!(io))
4668
@test startswith(s, "ConstantSpace(0..1) /")

0 commit comments

Comments
 (0)