Skip to content

Commit 4b1bb01

Browse files
authored
Simplify containsconstant trait (#492)
* Simplify containsconstant trait * Version bump to v0.8.38 * promote_rule test for constantless space
1 parent 4310c6e commit 4b1bb01

File tree

4 files changed

+44
-44
lines changed

4 files changed

+44
-44
lines changed

Project.toml

Lines changed: 1 addition & 3 deletions
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.37"
3+
version = "0.8.38"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
@@ -20,7 +20,6 @@ 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"
2423
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2524
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
2625
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
@@ -50,7 +49,6 @@ IntervalSets = "0.5, 0.6, 0.7"
5049
LazyArrays = "0.20, 0.21, 0.22, 1"
5150
LowRankApprox = "0.2, 0.3, 0.4, 0.5"
5251
OddEvenIntegers = "0.1.8"
53-
SimpleTraits = "0.9"
5452
SpecialFunctions = "0.10, 1.0, 2"
5553
Static = "0.8"
5654
StaticArrays = "0.12, 1.0"

src/ApproxFunBase.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ 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,
7-
SimpleTraits
6+
LowRankApprox, FillArrays, InfiniteArrays, InfiniteLinearAlgebra
87

98
import StaticArrays, Calculus
109
using StaticArrays: SVector, @SArray, SArray
@@ -96,8 +95,6 @@ import DomainSets: dimension
9695

9796
import IntervalSets: (..), endpoints
9897

99-
using SimpleTraits
100-
10198
const Vec{d,T} = SVector{d,T}
10299

103100
export pad!, pad, chop!, sample,

src/Spaces/ConstantSpace.jl

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,53 +31,40 @@ 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)
34+
containsconstant(A::Space) = containsconstant(typeof(A))
35+
containsconstant(@nospecialize(_)) = Val(false)
3636

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{}
37+
# setup conversions for spaces that contain constants
38+
macro containsconstants(SP)
39+
esc(quote
40+
ApproxFunBase.containsconstant(::Type{<:$SP}) = Val(true)
41+
end)
4542
end
46-
@traitfn function constspace_promote_rule(::Type{<:ConstantSpace},
47-
B::Type{X}) where {X; ContainsConstant{X}}
48-
B
43+
44+
function promote_rule(TS1::Type{<:ConstantSpace}, ::Type{TS2}) where {TS2<:Space}
45+
constspace_promote_rule(TS1, TS2, containsconstant(TS2))
4946
end
47+
constspace_promote_rule(::Type{<:ConstantSpace}, ::Type{<:Space}, ::Val{false}) = Union{}
48+
constspace_promote_rule(::Type{<:ConstantSpace}, ::Type{B}, ::Val{true}) where {B<:Space} = B
5049

51-
union_rule(A::ConstantSpace, B::Space) = constspace_union_rule(A, B)
50+
union_rule(A::ConstantSpace, B::Space) = constspace_union_rule(A, B, containsconstant(B))
5251
# 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)
52+
function constspace_union_rule(@nospecialize(_::ConstantSpace), B::Space, ::Val{false})
53+
ConstantSpace(domain(B)) B
54+
end
55+
constspace_union_rule(@nospecialize(_::ConstantSpace), B::Space, ::Val{true}) = B
5856

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}}
57+
function promote_rule(TF::Type{<:Fun{S}}, TN::Type{<:Number}) where {S}
58+
fun_promote_rule(TF, TN, containsconstant(S))
59+
end
60+
fun_promote_rule(::Type{<:Fun}, ::Type{<:Number}, ::Val{false}) = Fun
61+
function fun_promote_rule(::Type{<:Fun{S,CT}}, ::Type{T}, ::Val{true}) where {T<:Number,CT,S}
6362
ApproxFunBase.VFun{S,promote_type(CT,T)}
6463
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}}
64+
function fun_promote_rule(::Type{<:Fun{S}}, ::Type{T}, ::Val{true}) where {T<:Number,S}
6965
ApproxFunBase.VFun{S,T}
7066
end
7167

72-
# setup conversions for spaces that contain constants
73-
macro containsconstants(SP)
74-
esc(quote
75-
ApproxFunBase.containsconstant(::Type{<:$SP}) = true
76-
end)
77-
end
78-
79-
80-
8168
Fun(c::Number) = Fun(ConstantSpace(typeof(c)),[c])
8269
Fun(c::Number,d::ConstantSpace) = Fun(d,[c])
8370

test/PolynomialSpacesTests.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ApproxFunBase.domainscompatible(a::UniqueInterval, b::UniqueInterval) = a == b
2727

2828
Base.:(==)(a::UniqueInterval, b::UniqueInterval) = (@assert a.parentinterval == b.parentinterval; true)
2929

30-
@testset "ApproxFunBase" begin
30+
@testset "PolynomialSpaces" begin
3131
@testset "Constructor" begin
3232
@test (@inferred Fun()) == Fun(x->x, Chebyshev())
3333
@test (@inferred norm(Fun())) norm(Fun(), 2) (2/3) # √∫x^2 dx over -1..1
@@ -367,6 +367,24 @@ Base.:(==)(a::UniqueInterval, b::UniqueInterval) = (@assert a.parentinterval ==
367367
A = @inferred Derivative() * Multiplication(x, Chebyshev())
368368
@test A * x 2x
369369
end
370+
371+
@testset "ConstantSpace" begin
372+
S = Chebyshev()
373+
d = domain(S)
374+
C = ConstantSpace(d)
375+
@test promote_type(typeof(S), typeof(C)) == typeof(S)
376+
@test promote_type(typeof(S|(2:3)), typeof(C)) <: Space{typeof(d)}
377+
378+
@test union(S, C) == S
379+
# space doesn't contain constant
380+
cmps = union(S|(2:4), C)
381+
@test C in components(cmps)
382+
@test S|(2:4) in components(cmps)
383+
384+
@test promote_type(typeof(Fun()), Float64) == typeof(Fun())
385+
@test [Fun(), 1] isa Vector{typeof(Fun())}
386+
@test promote_type(Fun{typeof(Chebyshev())}, Float64) == typeof(Fun())
387+
end
370388
end
371389

372390
end # module

0 commit comments

Comments
 (0)