Skip to content

contains constant trait #486

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
Jun 16, 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: 3 additions & 1 deletion 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.34"
version = "0.8.35"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand All @@ -20,6 +20,7 @@ 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 All @@ -45,6 +46,7 @@ Infinities = "0.1"
IntervalSets = "0.5, 0.6, 0.7"
LazyArrays = "0.20, 0.21, 0.22, 1"
LowRankApprox = "0.2, 0.3, 0.4, 0.5"
SimpleTraits = "0.9"
SpecialFunctions = "0.10, 1.0, 2"
StaticArrays = "0.12, 1.0"
julia = "1.6"
Expand Down
3 changes: 2 additions & 1 deletion src/ApproxFunBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ using Base: AnyDict
using Base, BlockArrays, BandedMatrices, BlockBandedMatrices, DomainSets,
IntervalSets, SpecialFunctions, AbstractFFTs, FFTW,
SpecialFunctions, DSP, DualNumbers, LinearAlgebra, SparseArrays,
LowRankApprox, FillArrays, InfiniteArrays, InfiniteLinearAlgebra
LowRankApprox, FillArrays, InfiniteArrays, InfiniteLinearAlgebra,
SimpleTraits

import StaticArrays, Calculus
using StaticArrays: SVector, @SArray, SArray
Expand Down
4 changes: 2 additions & 2 deletions src/Fun.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ one(::Type{Fun{S,T,VT}}) where {T,S<:Space,VT} = ones(T,S(AnyDomain()))
zero(f::Fun) = zeros(cfstype(f), space(f))
one(f::Fun) = ones(cfstype(f), space(f))

cfstype(::Fun{S,T}) where {S,T} = T
cfstype(::Type{Fun{S,T,VT}}) where {S,T,VT} = T
cfstype(f::Fun) = cfstype(typeof(f))
cfstype(::Type{<:Fun{<:Any,T}}) where {T} = T

# Number and Array conform to the Fun interface
cfstype(::Type{T}) where T<: Number = T
Expand Down
2 changes: 1 addition & 1 deletion src/Space.jl
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ end
ConstantSpace(d::Domain) = ConstantSpace{typeof(d),real(prectype(d))}(d)

ConstantSpace(::Type{N},d::Domain) where {N<:Number} = ConstantSpace{typeof(d),real(N)}(d)
ConstantSpace(::Type{N}) where {N<:Number} = ConstantSpace(N,AnyDomain())
ConstantSpace(N::Type{<:Number}) = ConstantSpace(N,AnyDomain())
ConstantSpace() = ConstantSpace(Float64)


Expand Down
53 changes: 39 additions & 14 deletions src/Spaces/ConstantSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,48 @@ coefficients(cfs::AbstractVector,::SequenceSpace) = cfs # all vectors are conve

## Constant space defintions

@traitdef ContainsConstant{X}
@traitimpl ContainsConstant{X} <- containsconstant(X)

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{}
end
@traitfn function constspace_promote_rule(::Type{<:ConstantSpace},
B::Type{X}) where {X; ContainsConstant{X}}
B
end

union_rule(A::ConstantSpace, B::Space) = constspace_union_rule(A, 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)

@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}}
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}}
ApproxFunBase.VFun{S,T}
end

# setup conversions for spaces that contain constants
macro containsconstants(SP)
esc(quote
ApproxFunBase.union_rule(A::(ApproxFunBase.ConstantSpace),B::$SP) = B
Base.promote_rule(A::Type{<:(ApproxFunBase.ConstantSpace)},B::Type{<:($SP)}) = B

Base.promote_rule(::Type{ApproxFunBase.Fun{S,V,VV}},::Type{T}) where {T<:Number,S<:$SP,V,VV} =
ApproxFunBase.VFun{S,promote_type(V,T)}
Base.promote_rule(::Type{ApproxFunBase.Fun{S}},::Type{T}) where {T<:Number,S<:$SP} = ApproxFunBase.VFun{S,T}
Base.promote_rule(::Type{ApproxFunBase.Fun{S,V,VV}},
::Type{Fun{ApproxFunBase.ConstantSpace{ApproxFunBase.AnyDomain},T,VT}}) where {T,S<:$SP,V,VV,VT} =
ApproxFunBase.VFun{S,promote_type(V,T)}
ApproxFunBase.containsconstant(::Type{<:$SP}) = true
end)
end

Expand Down Expand Up @@ -78,7 +108,6 @@ Number(f::Fun) = strictconvert(Number, f)
Base.promote_rule(::Type{Fun{CS}},::Type{T}) where {CS<:ConstantSpace,T<:Number} = Fun{CS,T}
Base.promote_rule(::Type{Fun{CS,V}},::Type{T}) where {CS<:ConstantSpace,T<:Number,V} =
Fun{CS,promote_type(T,V)}
Base.promote_rule(::Type{IF},::Type{T}) where {T<:Number,IF<:Fun} = Fun


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

# TODO: this seems like it needs more thought
union_rule(A::ConstantSpace,B::Space) = ConstantSpace(domain(B))⊕B


## Special Multiplication and Conversion for constantspace

# TODO: this is a special work around but really we want it to be blocks
Expand Down
5 changes: 5 additions & 0 deletions test/SpacesTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ using LinearAlgebra
@test fs[1] == Fun(f1, sp)
@test fs[2] == Fun(f2, sp)
end

@testset "promote_rule" begin
F = Fun{typeof(PointSpace(1:3)), Float32}
@test ApproxFunBase.cfstype(F) == Float32
end
end

@testset "DiracSpace" begin
Expand Down