Skip to content

Commit fc6f19d

Browse files
authored
domain promotion for cached operator (#186)
1 parent ffdfef2 commit fc6f19d

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

src/Caching/matrix.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CachedOperator(::Type{Matrix},op::Operator;padding::Bool=false) =
2-
CachedOperator(op,Array{eltype(op)}(0,0),padding)
2+
CachedOperator(op,Array{eltype(op)}(undef,0,0),padding)
33

44

55
# Grow cached operator

src/LinearAlgebra/helper.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function unsafe_resize!(W::AbstractMatrix,n::Integer,::Colon)
200200
W[1:n,:]
201201
else
202202
m=size(W,2)
203-
ret=Matrix{eltype(W)}(n,m)
203+
ret=Matrix{eltype(W)}(undef, n,m)
204204
ret[1:N,:] = W
205205
ret
206206
end

src/Operators/general/CachedOperator.jl

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
2-
31
export cache
42

5-
6-
7-
8-
93
## CachedOperator
104

115
mutable struct CachedOperator{T<:Number,DM<:AbstractMatrix,M<:Operator,DS,RS,BI} <: Operator{T}
@@ -85,7 +79,7 @@ function Base.getindex(B::CachedOperator,k::AbstractRange,j::AbstractRange)
8579
resizedata!(B,maximum(k),maximum(j))
8680
B.data[k,j]
8781
else
88-
Matrix{eltype(B)}(length(k),length(j))
82+
Matrix{eltype(B)}(undef, length(k),length(j))
8983
end
9084
end
9185

@@ -114,17 +108,21 @@ end
114108
end
115109
end
116110

117-
118-
119-
120-
121-
122111
resizedata!(B::CachedOperator,::Colon,m::Integer) = resizedata!(B,size(B,1),m)
123112
resizedata!(B::CachedOperator,n::Integer,::Colon) = resizedata!(B,n,size(B,2))
124113

125-
126114
function mul_coefficients(B::CachedOperator,v::AbstractVector{T}) where T<:Number
127115
resizedata!(B,:,length(v))
128116

129117
B.data*pad(v,size(B.data,2))
130118
end
119+
120+
function promotedomainspace(C::CachedOperator,sp::Space)
121+
C2 = promotedomainspace(C.op, sp)
122+
cache(C2)
123+
end
124+
125+
function promoterangespace(C::CachedOperator,sp::Space)
126+
C2 = promoterangespace(C.op, sp)
127+
cache(C2)
128+
end

test/SpacesTest.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ApproxFunBase: PointSpace, HeavisideSpace, PiecewiseSegment, dimension, V
33
using ApproxFunOrthogonalPolynomials
44
using StaticArrays
55
using BandedMatrices: rowrange, colrange, BandedMatrix
6+
using LinearAlgebra
67

78
@testset "Spaces" begin
89
@testset "PointSpace" begin
@@ -125,6 +126,16 @@ using BandedMatrices: rowrange, colrange, BandedMatrix
125126
@test f(1) == 1
126127
@test f(4) == 0
127128
end
129+
130+
@testset "CachedOperator" begin
131+
s = PointSpace(1:3)
132+
Mop = Multiplication(Fun(s, 1:3))
133+
C = cache(Mop) : s s
134+
@test C isa ApproxFunBase.CachedOperator
135+
CM = AbstractMatrix(C[:, :])
136+
@test CM == Diagonal(1:3)
137+
@test size(C[1:0, 1:0]) == (0, 0)
138+
end
128139
end
129140

130141
@testset "DiracSpace" begin
@@ -387,6 +398,16 @@ using BandedMatrices: rowrange, colrange, BandedMatrix
387398
@test (@inferred BandedMatrix(S)) == (@inferred Matrix(S))
388399
end
389400

401+
@testset "CachedOperator" begin
402+
C = cache(Derivative())
403+
C = C : Chebyshev() Ultraspherical(2)
404+
D = Derivative() : Chebyshev() Ultraspherical(2)
405+
@test C[1:2, 1:0] == D[1:2, 1:0]
406+
@test C[1:10, 1:10] == D[1:10, 1:10]
407+
for col in 1:5, row in 1:5
408+
@test C[row, col] == D[row, col]
409+
end
410+
end
390411

391412
@testset "istriu/istril" begin
392413
for D in Any[Derivative(Chebyshev()),

0 commit comments

Comments
 (0)