Skip to content

Commit ffa7033

Browse files
authored
contains constant trait (#486)
* containsconstant trait * traited Fun-Number promote_rule * remove unused method
1 parent e720dba commit ffa7033

File tree

6 files changed

+52
-19
lines changed

6 files changed

+52
-19
lines changed

Project.toml

Lines changed: 3 additions & 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.8.34"
3+
version = "0.8.35"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
@@ -20,6 +20,7 @@ IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
2020
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
2121
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
2222
LowRankApprox = "898213cb-b102-5a47-900c-97e73b919f73"
23+
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
2324
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2425
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
2526
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
@@ -45,6 +46,7 @@ Infinities = "0.1"
4546
IntervalSets = "0.5, 0.6, 0.7"
4647
LazyArrays = "0.20, 0.21, 0.22, 1"
4748
LowRankApprox = "0.2, 0.3, 0.4, 0.5"
49+
SimpleTraits = "0.9"
4850
SpecialFunctions = "0.10, 1.0, 2"
4951
StaticArrays = "0.12, 1.0"
5052
julia = "1.6"

src/ApproxFunBase.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ using Base: AnyDict
33
using Base, BlockArrays, BandedMatrices, BlockBandedMatrices, DomainSets,
44
IntervalSets, SpecialFunctions, AbstractFFTs, FFTW,
55
SpecialFunctions, DSP, DualNumbers, LinearAlgebra, SparseArrays,
6-
LowRankApprox, FillArrays, InfiniteArrays, InfiniteLinearAlgebra
6+
LowRankApprox, FillArrays, InfiniteArrays, InfiniteLinearAlgebra,
7+
SimpleTraits
78

89
import StaticArrays, Calculus
910
using StaticArrays: SVector, @SArray, SArray

src/Fun.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ one(::Type{Fun{S,T,VT}}) where {T,S<:Space,VT} = ones(T,S(AnyDomain()))
216216
zero(f::Fun) = zeros(cfstype(f), space(f))
217217
one(f::Fun) = ones(cfstype(f), space(f))
218218

219-
cfstype(::Fun{S,T}) where {S,T} = T
220-
cfstype(::Type{Fun{S,T,VT}}) where {S,T,VT} = T
219+
cfstype(f::Fun) = cfstype(typeof(f))
220+
cfstype(::Type{<:Fun{<:Any,T}}) where {T} = T
221221

222222
# Number and Array conform to the Fun interface
223223
cfstype(::Type{T}) where T<: Number = T

src/Space.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ end
631631
ConstantSpace(d::Domain) = ConstantSpace{typeof(d),real(prectype(d))}(d)
632632

633633
ConstantSpace(::Type{N},d::Domain) where {N<:Number} = ConstantSpace{typeof(d),real(N)}(d)
634-
ConstantSpace(::Type{N}) where {N<:Number} = ConstantSpace(N,AnyDomain())
634+
ConstantSpace(N::Type{<:Number}) = ConstantSpace(N,AnyDomain())
635635
ConstantSpace() = ConstantSpace(Float64)
636636

637637

src/Spaces/ConstantSpace.jl

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,48 @@ coefficients(cfs::AbstractVector,::SequenceSpace) = cfs # all vectors are conve
3131

3232
## Constant space defintions
3333

34+
@traitdef ContainsConstant{X}
35+
@traitimpl ContainsConstant{X} <- containsconstant(X)
36+
37+
containsconstant(@nospecialize(_)) = false
38+
39+
promote_rule(TS1::Type{<:ConstantSpace}, TS2::Type{<:Space}) =
40+
constspace_promote_rule(TS1, TS2)
41+
42+
@traitfn function constspace_promote_rule(::Type{<:ConstantSpace},
43+
B::Type{X}) where {X; !ContainsConstant{X}}
44+
Union{}
45+
end
46+
@traitfn function constspace_promote_rule(::Type{<:ConstantSpace},
47+
B::Type{X}) where {X; ContainsConstant{X}}
48+
B
49+
end
50+
51+
union_rule(A::ConstantSpace, B::Space) = constspace_union_rule(A, B)
52+
# TODO: this seems like it needs more thought
53+
@traitfn constspace_union_rule(A::ConstantSpace, B::X) where {X; !ContainsConstant{X}} =
54+
ConstantSpace(domain(B))B
55+
@traitfn constspace_union_rule(A::ConstantSpace, B::X) where {X; ContainsConstant{X}} = B
56+
57+
promote_rule(TF::Type{<:Fun}, TN::Type{<:Number}) = fun_promote_rule(TF, TN)
58+
59+
@traitfn fun_promote_rule(::Type{<:Fun{S,CT}},
60+
::Type{T}) where {T<:Number,CT,S; !ContainsConstant{S}} = Fun
61+
@traitfn function fun_promote_rule(::Type{<:Fun{S,CT}},
62+
::Type{T}) where {T<:Number,CT,S; ContainsConstant{S}}
63+
ApproxFunBase.VFun{S,promote_type(CT,T)}
64+
end
65+
@traitfn fun_promote_rule(::Type{Fun{S}},
66+
::Type{T}) where {T<:Number,S; !ContainsConstant{S}} = Fun
67+
@traitfn function fun_promote_rule(::Type{<:Fun{S}},
68+
::Type{T}) where {T<:Number,S; ContainsConstant{S}}
69+
ApproxFunBase.VFun{S,T}
70+
end
71+
3472
# setup conversions for spaces that contain constants
3573
macro containsconstants(SP)
3674
esc(quote
37-
ApproxFunBase.union_rule(A::(ApproxFunBase.ConstantSpace),B::$SP) = B
38-
Base.promote_rule(A::Type{<:(ApproxFunBase.ConstantSpace)},B::Type{<:($SP)}) = B
39-
40-
Base.promote_rule(::Type{ApproxFunBase.Fun{S,V,VV}},::Type{T}) where {T<:Number,S<:$SP,V,VV} =
41-
ApproxFunBase.VFun{S,promote_type(V,T)}
42-
Base.promote_rule(::Type{ApproxFunBase.Fun{S}},::Type{T}) where {T<:Number,S<:$SP} = ApproxFunBase.VFun{S,T}
43-
Base.promote_rule(::Type{ApproxFunBase.Fun{S,V,VV}},
44-
::Type{Fun{ApproxFunBase.ConstantSpace{ApproxFunBase.AnyDomain},T,VT}}) where {T,S<:$SP,V,VV,VT} =
45-
ApproxFunBase.VFun{S,promote_type(V,T)}
75+
ApproxFunBase.containsconstant(::Type{<:$SP}) = true
4676
end)
4777
end
4878

@@ -78,7 +108,6 @@ Number(f::Fun) = strictconvert(Number, f)
78108
Base.promote_rule(::Type{Fun{CS}},::Type{T}) where {CS<:ConstantSpace,T<:Number} = Fun{CS,T}
79109
Base.promote_rule(::Type{Fun{CS,V}},::Type{T}) where {CS<:ConstantSpace,T<:Number,V} =
80110
Fun{CS,promote_type(T,V)}
81-
Base.promote_rule(::Type{IF},::Type{T}) where {T<:Number,IF<:Fun} = Fun
82111

83112

84113
# we know multiplication by constants preserves types
@@ -107,10 +136,6 @@ maxspace_rule(A::ZeroSpace,B::Space) = B
107136
Conversion(A::ZeroSpace,B::ZeroSpace) = ConversionWrapper(ZeroOperator(A,B))
108137
Conversion(A::ZeroSpace,B::Space) = ConversionWrapper(ZeroOperator(A,B))
109138

110-
# TODO: this seems like it needs more thought
111-
union_rule(A::ConstantSpace,B::Space) = ConstantSpace(domain(B))B
112-
113-
114139
## Special Multiplication and Conversion for constantspace
115140

116141
# TODO: this is a special work around but really we want it to be blocks

test/SpacesTest.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ using LinearAlgebra
198198
@test fs[1] == Fun(f1, sp)
199199
@test fs[2] == Fun(f2, sp)
200200
end
201+
202+
@testset "promote_rule" begin
203+
F = Fun{typeof(PointSpace(1:3)), Float32}
204+
@test ApproxFunBase.cfstype(F) == Float32
205+
end
201206
end
202207

203208
@testset "DiracSpace" begin

0 commit comments

Comments
 (0)