Skip to content

Add supportsinplacetransform trait #379

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 1 commit into from
Feb 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
2 changes: 1 addition & 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.7.70"
version = "0.7.71"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
1 change: 1 addition & 0 deletions src/LinearAlgebra/helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ promote_rule(::Type{Bool}, ::Type{UnsetNumber}) = Bool

# Test the number of arguments a function takes
hasnumargs(f,k) = k == 1 ? applicable(f, 0.0) : applicable(f, (1.0:k)...)
hasonearg(f) = hasnumargs(f, 1)

# fast implementation of isapprox with atol a non-keyword argument in most cases
isapprox_atol(a,b,atol;kwds...) = isapprox(a,b;atol=atol,kwds...)
Expand Down
15 changes: 15 additions & 0 deletions src/Space.jl
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,21 @@ itransform(S::Space, cfs) = plan_itransform(S,cfs)*cfs
itransform!(S::Space,cfs) = plan_itransform!(S,cfs)*cfs
transform!(S::Space,cfs) = plan_transform!(S,cfs)*cfs

_transform!!(::Val{false}) = transform
_transform!!(::Val{true}) = transform!
_itransform!!(::Val{false}) = itransform
_itransform!!(::Val{true}) = itransform!

"""
supportsinplacetransform(s::Space)

Trait that states if an inplace transform is possible for the space `s`.
In general, this is possible if `transform(s, v)` has the same `eltype` and `size` as `v`.
By default this is assumed to be `false`.

New spaces may choose to extend this if the result is known statically.
"""
supportsinplacetransform(_) = false

_coefficients!!(::Val{true}) = coefficients!
_coefficients!!(::Val{false}) = coefficients
Expand Down
31 changes: 14 additions & 17 deletions src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ end

# default_Fun is the default constructor, based on evaluation and transforms
# last argument is whether to splat or not
default_Fun(T::Type,f,d::Space,pts::AbstractArray, shouldsplat::Val{true}) =
Fun(d,transform(d,T[f(x...) for x in pts]))
function default_Fun(T::Type, f, d::Space, pts::AbstractArray, shouldsplat::Val{true})
default_Fun(T, Base.splat(f), d, pts, Val(false))
end

default_Fun(T::Type,f,d::Space,pts::AbstractArray, shouldsplat::Val{false}) =
Fun(d,transform(d,broadcast!(f, similar(pts, T), pts)))
function default_Fun(T::Type, f, d::Space, pts::AbstractArray, shouldsplat::Val{false})
fv = broadcast!(f, similar(pts, T), pts)
tfn = _transform!!(Val(supportsinplacetransform(d)))
coeffs = tfn(d, fv)
Fun(d, coeffs)
end


function default_Fun(f,d::Space,n::Integer, shouldsplat::Val{false})
function default_Fun(f, d::Space, n::Integer, shouldsplat::Val{false})
pts=points(d, n)
f1=f(pts[1])
if isa(f1,AbstractArray) && size(d) ≠ size(f1)
Expand All @@ -57,19 +62,11 @@ function default_Fun(f,d::Space,n::Integer, shouldsplat::Val{false})
default_Fun(Tprom,f,d,pts,Val(false))
end

function default_Fun(f,d::Space,n::Integer, shouldsplat::Val{true})
pts=points(d, n)
f1=f(pts[1]...)
if isa(f1,AbstractArray) && size(d) ≠ size(f1)
return Fun(f,Space(fill(d,size(f1))),n)
end

# we need 3 eltype calls for the case Interval(Point([1.,1.]))
Tprom=choosefuncfstype(typeof(f1),prectype(domain(d)))
default_Fun(Tprom,f,d,pts,Val(true))
function default_Fun(f, d::Space, n::Integer, shouldsplat::Val{true})
default_Fun(Base.splat(f), d, n, Val(false))
end

default_Fun(f,d::Space,n::Integer) = default_Fun(f,d,n,Val(!hasnumargs(f,1)))
default_Fun(f,d::Space,n::Integer) = default_Fun(f,d,n,Val(!hasonearg(f)))

Fun(f,d::Space,n::Integer) = default_Fun(dynamic(f),d,n)

Expand All @@ -96,7 +93,7 @@ Fun(c::Number,d::Space) = c==0 ? c*zeros(prectype(d),d) : c*ones(prectype(d),d)

## Adaptive constructors
function default_Fun(f, d::Space)
_default_Fun(hasnumargs(f, 1) ? f : Base.splat(f), d)
_default_Fun(hasonearg(f) ? f : Base.splat(f), d)
end
# In _default_Fun, we know that the function takes a single argument
function _default_Fun(f, d::Space)
Expand Down