Skip to content

Commit 6fee35d

Browse files
authored
Numbers as derivative orders (#297)
* Numbers as derivative orders * restore unsetspace method
1 parent 6eb9e64 commit 6fee35d

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
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.7.42"
3+
version = "0.7.43"
44

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

src/ApproxFunBase.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,22 @@ uniontypedvec(A, B) = Union{typeof(A), typeof(B)}[A, B]
112112
convert_vector(v::AbstractVector) = convert(Vector, v)
113113
convert_vector(t::Tuple) = [t...]
114114

115+
convert_vector_or_svector(v::AbstractVector) = convert(Vector, v)
116+
convert_vector_or_svector(t::Tuple) = SVector(t)
117+
115118
promote_eltypeof(As...) = promote_eltypeof(As)
116119
# Avoid mapreduce for common cases, as it often suffers from poor type inference
117120
promote_eltypeof(As::Tuple{Any}) = eltype(As[1])
118121
promote_eltypeof(As::Tuple{Any,Any}) = promote_type(eltype(As[1]), eltype(As[2]))
119122
promote_eltypeof(As::Tuple{Any,Any,Any}) = promote_type(eltype(As[1]), eltype(As[2]), eltype(As[3]))
120123
promote_eltypeof(As::Union{AbstractArray, Tuple}) = mapfoldl(eltype, promote_type, As)
121124

125+
assert_integer(::Integer) = nothing
126+
function assert_integer(k::Number)
127+
@assert Integer(k) == k "order must be an integer"
128+
return nothing
129+
end
130+
122131
include("LinearAlgebra/LinearAlgebra.jl")
123132
include("Fun.jl")
124133
include("onehotvector.jl")

src/Operators/banded/CalculusOperator.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ macro calculus_operator(Op)
3030
$ConcOp(sp::Space,k) = $ConcOp{typeof(sp),typeof(k),ApproxFunBase.prectype(sp)}(sp,k)
3131

3232
$Op(sp::ApproxFunBase.UnsetSpace,k) = $ConcOp(sp,k)
33+
$Op(sp::ApproxFunBase.UnsetSpace,k::Number) = $ConcOp(sp,k)
3334
$Op(sp::ApproxFunBase.UnsetSpace,k::Real) = $ConcOp(sp,k)
3435
$Op(sp::ApproxFunBase.UnsetSpace,k::Integer) = $ConcOp(sp,k)
3536

@@ -183,7 +184,8 @@ end
183184

184185

185186
## Map to canonical
186-
function DefaultDerivative(sp::Space,k::Integer)
187+
function DefaultDerivative(sp::Space, k::Number)
188+
assert_integer(k)
187189
if typeof(canonicaldomain(sp)).name==typeof(domain(sp)).name
188190
# this is the normal default constructor
189191
csp=canonicalspace(sp)
@@ -212,7 +214,8 @@ function DefaultDerivative(sp::Space,k::Integer)
212214
end
213215

214216

215-
function DefaultIntegral(sp::Space,k::Integer)
217+
function DefaultIntegral(sp::Space, k::Number)
218+
assert_integer(k)
216219
if typeof(canonicaldomain(sp)).name==typeof(domain(sp)).name
217220
# this is the normal default constructor
218221
csp=canonicalspace(sp)

src/Operators/general/InterlaceOperator.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function rangespace(A::VectorOrTupleOfOp)
4747
error("Cannot construct rangespace for $A as domain spaces are not compatible")
4848
end
4949
spl=map(rangespace, A)
50-
ArraySpace(_convert_vector_or_svector(spl), first(spl))
50+
ArraySpace(_convert_vector_promotetypes(spl), first(spl))
5151
end
5252

5353
promotespaces(A::AbstractMatrix{<:Operator}) = promotespaces(Matrix(A))
@@ -178,12 +178,12 @@ InterlaceOperator(opsin::AbstractMatrix{<:Operator},::Type{DS}) where {DS<:Space
178178
InterlaceOperator(opsin::AbstractMatrix,S...) =
179179
InterlaceOperator(Matrix{Operator{promote_eltypeof(opsin)}}(promotespaces(opsin)),S...)
180180

181-
_convert_vector_or_svector(v::AbstractVector) = convert_vector(v)
181+
_convert_vector_promotetypes(v::AbstractVector) = convert_vector(v)
182182
_uniontypes_svector(t) = SVector{length(t), mapfoldl(typeof, (x,y)->Union{x,y}, t)}(t)
183-
_convert_vector_or_svector(t::NTuple{2,Any}) = _uniontypes_svector(t)
184-
_convert_vector_or_svector(t::NTuple{3,Any}) = _uniontypes_svector(t)
185-
_convert_vector_or_svector(t::NTuple{4,Any}) = _uniontypes_svector(t)
186-
_convert_vector_or_svector(t::Tuple) = SVector{length(t), mapreduce(typeof, typejoin, t)}(t)
183+
_convert_vector_promotetypes(t::NTuple{2,Any}) = _uniontypes_svector(t)
184+
_convert_vector_promotetypes(t::NTuple{3,Any}) = _uniontypes_svector(t)
185+
_convert_vector_promotetypes(t::NTuple{4,Any}) = _uniontypes_svector(t)
186+
_convert_vector_promotetypes(t::Tuple) = SVector{length(t), mapreduce(typeof, typejoin, t)}(t)
187187

188188
function InterlaceOperator(opsin::AbstractVector{<:Operator})
189189
ops = convert_vector(promotedomainspace(opsin))

src/Spaces/ProductSpaceOperators.jl

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,7 @@ for TYP in (:SumSpace,:PiecewiseSpace)
154154
end
155155
end
156156

157-
function hasconversion(a::SumSpace, b::Space)
158-
for n=1:length(a.spaces)
159-
if hasconversion(a.spaces[n],b) true
160-
return false
161-
end
162-
end
163-
return true
164-
end
157+
hasconversion(a::SumSpace, b::Space) = all(s -> hasconversion(s, b), a.spaces)
165158

166159
function Conversion(a::SumSpace, b::Space)
167160
if !hasconversion(a, b)
@@ -230,20 +223,32 @@ end
230223

231224
for (Op,OpWrap) in ((:Derivative,:DerivativeWrapper),(:Integral,:IntegralWrapper))
232225
@eval begin
233-
$Op(S::PiecewiseSpace,k::Integer) =
234-
$OpWrap(InterlaceOperator(Diagonal([map(s->$Op(s,k),components(S))...]),PiecewiseSpace),k)
235-
function $Op(S::ArraySpace,k::Integer)
226+
function $Op(S::PiecewiseSpace, k::Number)
227+
assert_integer(k)
228+
t = map(s->$Op(s,k),components(S))
229+
D = Diagonal(convert_vector_or_svector(t))
230+
O = InterlaceOperator(D, PiecewiseSpace)
231+
$OpWrap(O,k)
232+
end
233+
function $Op(S::ArraySpace, k::Number)
234+
assert_integer(k)
236235
ops = map(s->$Op(s,k),S)
237-
$OpWrap(InterlaceOperator(Diagonal(ops),S,ArraySpace(reshape(rangespace.(ops),size(S)))),k)
236+
RS = ArraySpace(reshape(map(rangespace, ops), size(S)))
237+
O = InterlaceOperator(Diagonal(ops), S, RS)
238+
$OpWrap(O,k)
238239
end
239240
end
240241
end
241242

242-
function Derivative(S::SumSpace,k::Integer)
243+
function Derivative(S::SumSpace, k::Number)
244+
assert_integer(k)
243245
# we want to map before we decompose, as the map may introduce
244246
# mixed bases.
245247
if typeof(canonicaldomain(S))==typeof(domain(S))
246-
DerivativeWrapper(InterlaceOperator(Diagonal([map(s->Derivative(s,k),components(S))...]),SumSpace),k)
248+
t = map(s->Derivative(s,k),components(S))
249+
D = Diagonal(convert_vector_or_svector(t))
250+
O = InterlaceOperator(D, SumSpace)
251+
DerivativeWrapper(O,k)
247252
else
248253
DefaultDerivative(S,k)
249254
end
@@ -268,17 +273,24 @@ end
268273

269274
## Multiply components
270275

271-
function Multiplication(f::Fun{PW},sp::PiecewiseSpace) where PW<:PiecewiseSpace
276+
function Multiplication(f::Fun{<:PiecewiseSpace}, sp::PiecewiseSpace)
272277
p=perm(domain(f).domains,domain(sp).domains) # sort f
273278
vf=components(f)[p]
274-
275-
MultiplicationWrapper(f,InterlaceOperator(Diagonal([map(Multiplication,vf,sp.spaces)...]),PiecewiseSpace))
279+
t = map(Multiplication,vf,sp.spaces)
280+
D = Diagonal(convert_vector_or_svector(t))
281+
O = InterlaceOperator(D, PiecewiseSpace)
282+
MultiplicationWrapper(f, O)
276283
end
277284

278285
Multiplication(f::Fun{SumSpace{SV1,D,R1}},sp::SumSpace{SV2,D,R2}) where {SV1,SV2,D,R1,R2} =
279286
MultiplicationWrapper(f,mapreduce(g->Multiplication(g,sp),+,components(f)))
280-
Multiplication(f::Fun,sp::SumSpace) =
281-
MultiplicationWrapper(f,InterlaceOperator(Diagonal([map(s->Multiplication(f,s),components(sp))...]),SumSpace))
287+
288+
function Multiplication(f::Fun, sp::SumSpace)
289+
t = map(s->Multiplication(f,s),components(sp))
290+
D = Diagonal(convert_vector_or_svector(t))
291+
O = InterlaceOperator(D, SumSpace)
292+
MultiplicationWrapper(f, O)
293+
end
282294

283295
Multiplication(f::Fun, sp::PiecewiseSpace) = MultiplicationWrapper(f, Multiplication(Fun(f,sp),sp))
284296

0 commit comments

Comments
 (0)