Skip to content

Backport isconstant #637

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
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
8 changes: 5 additions & 3 deletions src/Fun.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ julia> coefficients(f, Legendre()) ≈ [0, 0, 1]
true
```
"""
function coefficients(f::Fun,msp::Space)
coefficients(f::Fun,msp::Space) = _coefficients(f::Fun,msp::Space)
function _coefficients(f::Fun,msp::Space)
#zero can always be converted
fc = f.coefficients
if ncoefficients(f) == 0 || (ncoefficients(f) == 1 && fc[1] == 0)
Expand Down Expand Up @@ -777,9 +778,10 @@ isapprox(g::Number, f::Fun; kw...) = isapprox(g*ones(space(f)), f; kw...)
isreal(f::Fun{<:RealSpace,<:Real}) = true
isreal(f::Fun) = false

iszero(f::Fun) = all(iszero,f.coefficients)

iszero(f::Fun) = all(iszero, coefficients(f)) || all(iszero, values(f))

# Deliberately not named isconst or isconstant to avoid conflicts with Base or DomainSets
isconstantfun(f::Fun) = iszero(f - first(f))

# sum, integrate, and idfferentiate are in CalculusOperator

Expand Down
2 changes: 1 addition & 1 deletion src/Operators/Operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ opissymmetric(::OperatorStyle, A) = false
for f in [:domainspace, :rangespace]
opf = Symbol(:op, f)
@eval begin
$opf(::OperatorStyle, A) = error("Override domainspace for $(typeof(A))")
$opf(::OperatorStyle, A) = error("Override $($f) for $(typeof(A))")
$opf(S::StyleConflict, A) = $opf(dominantstyle(S, $f, A), A)
end
end
Expand Down
10 changes: 9 additions & 1 deletion src/Spaces/ConstantSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ ones(S::Union{AnyDomain,UnsetSpace}) = ones(ConstantSpace())
zeros(S::AnyDomain) = zero(ConstantSpace())
zero(S::UnsetSpace) = zero(ConstantSpace())
_first_or_zero(f::AbstractVector) = get(f, 1, zero(eltype(f)))
function evaluate(f::AbstractVector,::ConstantSpace,x...)
function evaluate(f::AbstractVector, sp::ConstantSpace, x)
x in domain(sp) || return zero(eltype(f))
_first_or_zero(f)
end
evaluate(f::AbstractVector,::ZeroSpace,x...)=zero(eltype(f))
Expand Down Expand Up @@ -143,6 +144,10 @@ function getindex(C::ConcreteConversion{CS,S,T},k::Integer,j::Integer) where {CS
k ≤ ncoefficients(on) ? strictconvert(T,on.coefficients[k]) : zero(T)
end

function coefficients(f::Fun, msp::ConstantSpace)
isconstantfun(f) || throw(ArgumentError("cannot convert a non-constant Fun to ConstantSpace"))
_coefficients(f, msp)
end

coefficients(f::AbstractVector,sp::ConstantSpace{Segment{SVector{2,TT}}},
ts::TensorSpace{SV,DD}) where {TT,SV,DD<:EuclideanDomain{2}} =
Expand All @@ -155,6 +160,9 @@ coefficients(f::AbstractVector, sp::ConstantSpace{<:Domain{<:Number}}, ts::Space
coefficients(f::AbstractVector, sp::ConstantSpace, ts::Space) =
f[1]*ones(ts).coefficients

# a Fun{<:ConstantSpace} corresponds to a constant value within the domain, irrespective of the domain
first(f::Fun{<:ConstantSpace}) = convert(Number, f)
last(f::Fun{<:ConstantSpace}) = convert(Number, f)

########
# Evaluation
Expand Down
15 changes: 8 additions & 7 deletions src/Spaces/DiracSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,20 @@ Fun(::typeof(identity), S::DiracSpace) = Fun(PointSpace(S.points),S.points)
transform(S::PointSpace,v::AbstractVector,plan...) = v
values(f::Fun{S}) where S<:PointSpace = coefficient(f,:)

function evaluate(f::AbstractVector,PS::PointSpace,x::Number)
p = findfirst(y->isapprox(x,y),PS.points)
first(f::Fun{<:PointSpace}) = coefficients(f)[1]
last(f::Fun{<:PointSpace}) = coefficients(f)[end]

function evaluate(f::AbstractVector, PS::PointSpace, x)
p = findfirst(≈(convert(Number,x)), PS.points)
if p === nothing
zero(eltype(f))
else
f[p]
end
end

function evaluate(f::AbstractVector, PS::DiracSpace, x::Number)
x ∉ domain(PS) && return zero(eltype(f))

p = findfirst(y->isapprox(x,y), PS.points)
function evaluate(f::AbstractVector, PS::DiracSpace, x)
p = findfirst(≈(convert(Number,x)), PS.points)
if p === nothing
zero(eltype(f))
else
Expand All @@ -100,7 +101,7 @@ function evaluate(f::AbstractVector, PS::DiracSpace, x::Number)
end

Base.sum(f::Fun{DS}) where DS<:DiracSpace =
sum(f.coefficients[1:dimension(space(f))])
sum(@view f.coefficients[1:dimension(space(f))])

DiracDelta(x::Number)=Fun(DiracSpace(x),[1.])
DiracDelta()=DiracDelta(0.)
Expand Down
21 changes: 19 additions & 2 deletions test/SpacesTest.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using ApproxFunBase
using Test
using ApproxFunBase: PointSpace, HeavisideSpace, PiecewiseSegment, dimension, SVector, checkpoints, AnyDomain
using StaticArrays
using BandedMatrices: rowrange, colrange, BandedMatrix
using DomainSets: Point
using LinearAlgebra
using StaticArrays
using Test

@testset "Spaces" begin
@testset "PointSpace" begin
@test eltype(domain(PointSpace([0,0.1,1])) ) == Float64

f = @inferred Fun(x->(x-0.1),PointSpace([0,0.1,1]))
@test roots(f) == [0.1]
@test first(f) == -0.1
@test last(f) == 0.9
@test f(Point(0)) == -0.1

a = @inferred Fun(exp, space(f))
@test f/a == @inferred Fun(x->(x-0.1)*exp(-x),space(f))
Expand Down Expand Up @@ -204,6 +208,11 @@ using LinearAlgebra
F = Fun{typeof(PointSpace(1:3)), Float32}
@test ApproxFunBase.cfstype(F) == Float32
end

@testset "isconstantfun" begin
f = Fun(PointSpace(1:4), ones(4))
@test ApproxFunBase.isconstantfun(f)
end
end

@testset "DiracSpace" begin
Expand All @@ -212,6 +221,8 @@ using LinearAlgebra
@test f(0.5) == 0
@test f(5) == 0
@test isinf(f(0))
@test isinf(f(Point(0)))
@test sum(f) == 6
end

@testset "Derivative operator for HeavisideSpace" begin
Expand Down Expand Up @@ -317,6 +328,12 @@ using LinearAlgebra
@test g >= f
@test 1 < f < 3
@test differentiate(f) == Fun(0, ConstantSpace(0..1))
@test f(-1) == 0
@test first(f) == last(f) == 2
@test ApproxFunBase.isconstantfun(f)

f = Fun(2, ConstantSpace())
@test first(f) == last(f) == 2

@test maxspace(ConstantSpace(Point(1)), ConstantSpace(Point(2))) == ConstantSpace(Point(1) ∪ Point(2))
@test maxspace(ConstantSpace(Point(1)), ConstantSpace(AnyDomain())) == ConstantSpace(Point(1))
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -795,3 +795,10 @@ include("show.jl")

@test @inferred(chebyshev_clenshaw(BigInt[1], 1)) == 1
end

@testset "Unimplemented functionality errors" begin
struct MyOperator{T} <: ApproxFunBase.Operator{T} end
X = MyOperator{Float64}();
@test_throws ErrorException("Override domainspace for $(typeof(X))") domainspace(X)
@test_throws ErrorException("Override rangespace for $(typeof(X))") rangespace(X)
end