Skip to content

Simplify containsconstant trait #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ApproxFunBase"
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
version = "0.8.37"
version = "0.8.38"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand All @@ -20,7 +20,6 @@ IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LowRankApprox = "898213cb-b102-5a47-900c-97e73b919f73"
SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Expand Down Expand Up @@ -50,7 +49,6 @@ IntervalSets = "0.5, 0.6, 0.7"
LazyArrays = "0.20, 0.21, 0.22, 1"
LowRankApprox = "0.2, 0.3, 0.4, 0.5"
OddEvenIntegers = "0.1.8"
SimpleTraits = "0.9"
SpecialFunctions = "0.10, 1.0, 2"
Static = "0.8"
StaticArrays = "0.12, 1.0"
Expand Down
5 changes: 1 addition & 4 deletions src/ApproxFunBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ using Base: AnyDict
using Base, BlockArrays, BandedMatrices, BlockBandedMatrices, DomainSets,
IntervalSets, SpecialFunctions, AbstractFFTs, FFTW,
SpecialFunctions, DSP, DualNumbers, LinearAlgebra, SparseArrays,
LowRankApprox, FillArrays, InfiniteArrays, InfiniteLinearAlgebra,
SimpleTraits
LowRankApprox, FillArrays, InfiniteArrays, InfiniteLinearAlgebra

import StaticArrays, Calculus
using StaticArrays: SVector, @SArray, SArray
Expand Down Expand Up @@ -96,8 +95,6 @@ import DomainSets: dimension

import IntervalSets: (..), endpoints

using SimpleTraits

const Vec{d,T} = SVector{d,T}

export pad!, pad, chop!, sample,
Expand Down
59 changes: 23 additions & 36 deletions src/Spaces/ConstantSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,40 @@ coefficients(cfs::AbstractVector,::SequenceSpace) = cfs # all vectors are conve

## Constant space defintions

@traitdef ContainsConstant{X}
@traitimpl ContainsConstant{X} <- containsconstant(X)
containsconstant(A::Space) = containsconstant(typeof(A))
containsconstant(@nospecialize(_)) = Val(false)

containsconstant(@nospecialize(_)) = false

promote_rule(TS1::Type{<:ConstantSpace}, TS2::Type{<:Space}) =
constspace_promote_rule(TS1, TS2)

@traitfn function constspace_promote_rule(::Type{<:ConstantSpace},
B::Type{X}) where {X; !ContainsConstant{X}}
Union{}
# setup conversions for spaces that contain constants
macro containsconstants(SP)
esc(quote
ApproxFunBase.containsconstant(::Type{<:$SP}) = Val(true)
end)
end
@traitfn function constspace_promote_rule(::Type{<:ConstantSpace},
B::Type{X}) where {X; ContainsConstant{X}}
B

function promote_rule(TS1::Type{<:ConstantSpace}, ::Type{TS2}) where {TS2<:Space}
constspace_promote_rule(TS1, TS2, containsconstant(TS2))
end
constspace_promote_rule(::Type{<:ConstantSpace}, ::Type{<:Space}, ::Val{false}) = Union{}
constspace_promote_rule(::Type{<:ConstantSpace}, ::Type{B}, ::Val{true}) where {B<:Space} = B

union_rule(A::ConstantSpace, B::Space) = constspace_union_rule(A, B)
union_rule(A::ConstantSpace, B::Space) = constspace_union_rule(A, B, containsconstant(B))
# TODO: this seems like it needs more thought
@traitfn constspace_union_rule(A::ConstantSpace, B::X) where {X; !ContainsConstant{X}} =
ConstantSpace(domain(B))⊕B
@traitfn constspace_union_rule(A::ConstantSpace, B::X) where {X; ContainsConstant{X}} = B

promote_rule(TF::Type{<:Fun}, TN::Type{<:Number}) = fun_promote_rule(TF, TN)
function constspace_union_rule(@nospecialize(_::ConstantSpace), B::Space, ::Val{false})
ConstantSpace(domain(B)) ⊕ B
end
constspace_union_rule(@nospecialize(_::ConstantSpace), B::Space, ::Val{true}) = B

@traitfn fun_promote_rule(::Type{<:Fun{S,CT}},
::Type{T}) where {T<:Number,CT,S; !ContainsConstant{S}} = Fun
@traitfn function fun_promote_rule(::Type{<:Fun{S,CT}},
::Type{T}) where {T<:Number,CT,S; ContainsConstant{S}}
function promote_rule(TF::Type{<:Fun{S}}, TN::Type{<:Number}) where {S}
fun_promote_rule(TF, TN, containsconstant(S))
end
fun_promote_rule(::Type{<:Fun}, ::Type{<:Number}, ::Val{false}) = Fun
function fun_promote_rule(::Type{<:Fun{S,CT}}, ::Type{T}, ::Val{true}) where {T<:Number,CT,S}
ApproxFunBase.VFun{S,promote_type(CT,T)}
end
@traitfn fun_promote_rule(::Type{Fun{S}},
::Type{T}) where {T<:Number,S; !ContainsConstant{S}} = Fun
@traitfn function fun_promote_rule(::Type{<:Fun{S}},
::Type{T}) where {T<:Number,S; ContainsConstant{S}}
function fun_promote_rule(::Type{<:Fun{S}}, ::Type{T}, ::Val{true}) where {T<:Number,S}
ApproxFunBase.VFun{S,T}
end

# setup conversions for spaces that contain constants
macro containsconstants(SP)
esc(quote
ApproxFunBase.containsconstant(::Type{<:$SP}) = true
end)
end



Fun(c::Number) = Fun(ConstantSpace(typeof(c)),[c])
Fun(c::Number,d::ConstantSpace) = Fun(d,[c])

Expand Down
20 changes: 19 additions & 1 deletion test/PolynomialSpacesTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ApproxFunBase.domainscompatible(a::UniqueInterval, b::UniqueInterval) = a == b

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

@testset "ApproxFunBase" begin
@testset "PolynomialSpaces" begin
@testset "Constructor" begin
@test (@inferred Fun()) == Fun(x->x, Chebyshev())
@test (@inferred norm(Fun())) ≈ norm(Fun(), 2) ≈ √(2/3) # √∫x^2 dx over -1..1
Expand Down Expand Up @@ -367,6 +367,24 @@ Base.:(==)(a::UniqueInterval, b::UniqueInterval) = (@assert a.parentinterval ==
A = @inferred Derivative() * Multiplication(x, Chebyshev())
@test A * x ≈ 2x
end

@testset "ConstantSpace" begin
S = Chebyshev()
d = domain(S)
C = ConstantSpace(d)
@test promote_type(typeof(S), typeof(C)) == typeof(S)
@test promote_type(typeof(S|(2:3)), typeof(C)) <: Space{typeof(d)}

@test union(S, C) == S
# space doesn't contain constant
cmps = union(S|(2:4), C)
@test C in components(cmps)
@test S|(2:4) in components(cmps)

@test promote_type(typeof(Fun()), Float64) == typeof(Fun())
@test [Fun(), 1] isa Vector{typeof(Fun())}
@test promote_type(Fun{typeof(Chebyshev())}, Float64) == typeof(Fun())
end
end

end # module