Skip to content

Commit 023b1a4

Browse files
authored
Add isconstantfun and check while converting to ConstantSpace (#632)
* Add isconstantfun and check while converting to ConstantSpace * Don't check if constant is supported
1 parent b2c626b commit 023b1a4

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

Project.toml

+1-1
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.9.23"
3+
version = "0.9.24"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/Fun.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ julia> coefficients(f, Legendre()) ≈ [0, 0, 1]
107107
true
108108
```
109109
"""
110-
function coefficients(f::Fun,msp::Space)
110+
coefficients(f::Fun,msp::Space) = _coefficients(f::Fun,msp::Space)
111+
function _coefficients(f::Fun,msp::Space)
111112
#zero can always be converted
112113
fc = f.coefficients
113114
if ncoefficients(f) == 0 || (ncoefficients(f) == 1 && fc[1] == 0)
@@ -774,7 +775,8 @@ isreal(f::Fun) = false
774775

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

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

779781
# sum, integrate, and idfferentiate are in CalculusOperator
780782

src/Spaces/ConstantSpace.jl

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ convert(::Type{T}, f::Fun{CS}) where {CS<:ConstantSpace,T<:Number} =
9393

9494
Number(f::Fun) = strictconvert(Number, f)
9595

96+
isconstantfun(f::Fun{<:ConstantSpace}) = true
9697

9798
# promoting numbers to Fun
9899
# override promote_rule if the space type can represent constants
@@ -144,6 +145,10 @@ function getindex(C::ConcreteConversion{CS,S,T},k::Integer,j::Integer) where {CS
144145
k ncoefficients(on) ? strictconvert(T,on.coefficients[k]) : zero(T)
145146
end
146147

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

148153
coefficients(f::AbstractVector,sp::ConstantSpace{Segment{SVector{2,TT}}},
149154
ts::TensorSpace{SV,DD}) where {TT,SV,DD<:EuclideanDomain{2}} =

test/SpacesTest.jl

+1
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ using Test
325325
@test differentiate(f) == Fun(0, ConstantSpace(0..1))
326326
@test f(-1) == 0
327327
@test first(f) == last(f) == 2
328+
@test ApproxFunBase.isconstantfun(f)
328329

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

0 commit comments

Comments
 (0)