From 736ad75e623af88d28cdd46d4c4292b4218913ec Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 27 Jul 2023 12:16:58 +0530 Subject: [PATCH] Move SparseArrays and Statistics to extensions --- Project.toml | 8 +++++++ ext/ApproxFunBaseSparseArraysExt.jl | 36 +++++++++++++++++++++++++++++ ext/ApproxFunBaseStatisticsExt.jl | 9 ++++++++ src/ApproxFunBase.jl | 11 ++++----- src/Domains/Segment.jl | 1 + src/Operators/Operator.jl | 7 ------ src/Operators/systems.jl | 23 ------------------ src/Spaces/ProductSpaceOperators.jl | 3 --- 8 files changed, 59 insertions(+), 39 deletions(-) create mode 100644 ext/ApproxFunBaseSparseArraysExt.jl create mode 100644 ext/ApproxFunBaseStatisticsExt.jl diff --git a/Project.toml b/Project.toml index 2113f6e4..184f7d8f 100644 --- a/Project.toml +++ b/Project.toml @@ -24,6 +24,14 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" +[weakdeps] +SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" + +[extensions] +ApproxFunBaseSparseArraysExt = "SparseArrays" +ApproxFunBaseStatisticsExt = "Statistics" + [compat] AbstractFFTs = "0.5, 1" Aqua = "0.8" diff --git a/ext/ApproxFunBaseSparseArraysExt.jl b/ext/ApproxFunBaseSparseArraysExt.jl new file mode 100644 index 00000000..406cf3a2 --- /dev/null +++ b/ext/ApproxFunBaseSparseArraysExt.jl @@ -0,0 +1,36 @@ +module ApproxFunBaseSparseArraysExt + +using SparseArrays +import SparseArrays: blockdiag +using ApproxFunBase +using ApproxFunBase: promote_eltypeof + +##TODO: unify with other blockdiag +function blockdiag(d1::AbstractVector{T}, d2::AbstractVector{T}) where T<:Operator + if isempty(d1) && isempty(d2) + error("Empty blockdiag") + end + if isempty(d1) + TT=promote_eltypeof(d2) + elseif isempty(d2) + TT=promote_eltypeof(d1) + else + TT=promote_type(promote_eltypeof(d1), + promote_eltypeof(d2)) + end + + D=zeros(Operator{TT},length(d1)+length(d2),2) + D[1:length(d1),1]=d1 + D[length(d1)+1:end,2]=d2 + D +end + +function blockdiag(a::Operator, b::Operator) + blockdiag(Operator{promote_type(eltype(a),eltype(b))}[a], + Operator{promote_type(eltype(a),eltype(b))}[b]) +end + +blockdiag(A::PlusOperator) = mapreduce(blockdiag, +, A.ops) +blockdiag(A::TimesOperator) = mapreduce(blockdiag, .*, A.ops) + +end diff --git a/ext/ApproxFunBaseStatisticsExt.jl b/ext/ApproxFunBaseStatisticsExt.jl new file mode 100644 index 00000000..e1d60c8d --- /dev/null +++ b/ext/ApproxFunBaseStatisticsExt.jl @@ -0,0 +1,9 @@ +module ApproxFunBaseStatisticsExt + +import Statistics: mean +using ApproxFunBase +using ApproxFunBase: IntervalOrSegment + +mean(d::IntervalOrSegment) = ApproxFunBase.mean(d) + +end diff --git a/src/ApproxFunBase.jl b/src/ApproxFunBase.jl index 4cc49a31..16c4bfe8 100644 --- a/src/ApproxFunBase.jl +++ b/src/ApproxFunBase.jl @@ -15,10 +15,8 @@ using InfiniteArrays using IntervalSets using LinearAlgebra using LowRankMatrices -using SparseArrays using SpecialFunctions using StaticArrays: SVector, @SArray, SArray -import Statistics: mean import DomainSets: Domain, indomain, UnionDomain, ProductDomain, Point, ∂, SetdiffDomain, Interval, ChebyshevInterval, boundary, @@ -58,10 +56,6 @@ import LinearAlgebra: BlasInt, BlasFloat, norm, ldiv!, mul!, det, cross, nullspace, Hermitian, Symmetric, adjoint, transpose, char_uplo, axpy!, eigvals -import SparseArrays: blockdiag - -# import Arpack: eigs - # we need to import all special functions to use Calculus.symbolic_derivatives_1arg # we can't do importall Base as we replace some Base definitions import SpecialFunctions: airy, besselh, @@ -160,4 +154,9 @@ include("hacks.jl") include("specialfunctions.jl") include("show.jl") +if !isdefined(Base, :get_extension) + include("../ext/ApproxFunBaseSparseArraysExt.jl") + include("../ext/ApproxFunBaseStatisticsExt.jl") +end + end #module diff --git a/src/Domains/Segment.jl b/src/Domains/Segment.jl index 75fb2c54..ae9039c3 100644 --- a/src/Domains/Segment.jl +++ b/src/Domains/Segment.jl @@ -68,6 +68,7 @@ issubset(a::Segment,b::Segment) = leftendpoint(a)∈b && rightendpoint(a)∈b arclength(d::AbstractInterval) = width(d) arclength(d::Segment) = norm(complexlength(d)) complexlength(d::IntervalOrSegment) = rightendpoint(d)-leftendpoint(d) +# ApproxFunBase.mean != Statistics.mean, as the latter is defined in the Statistics extension mean(d::IntervalOrSegment) = (rightendpoint(d)+leftendpoint(d))/2 angle(d::IntervalOrSegment) = angle(complexlength(d)) sign(d::IntervalOrSegment) = sign(complexlength(d)) diff --git a/src/Operators/Operator.jl b/src/Operators/Operator.jl index 76da7648..04c6f515 100644 --- a/src/Operators/Operator.jl +++ b/src/Operators/Operator.jl @@ -332,13 +332,6 @@ istril(A::Operator) = bandwidth(A, 2) <= 0 include("SubOperator.jl") - -# -# sparse(B::Operator,n::Integer)=sparse(BandedMatrix(B,n)) -# sparse(B::Operator,n::AbstractRange,m::AbstractRange)=sparse(BandedMatrix(B,n,m)) -# sparse(B::Operator,n::Colon,m::AbstractRange)=sparse(BandedMatrix(B,n,m)) -# sparse(B::Operator,n::AbstractRange,m::Colon)=sparse(BandedMatrix(B,n,m)) - ## getindex diff --git a/src/Operators/systems.jl b/src/Operators/systems.jl index 914b1f25..bdd366f1 100644 --- a/src/Operators/systems.jl +++ b/src/Operators/systems.jl @@ -33,29 +33,6 @@ function diagm_container(size, kv::Pair{<:Integer,<:AbstractVector{<:Operator}}. zeros(Operator{T}, n, n) end -##TODO: unify with other blockdiag -function blockdiag(d1::AbstractVector{T},d2::AbstractVector{T}) where T<:Operator - if isempty(d1)&&isempty(d2) - error("Empty blockdiag") - end - if isempty(d1) - TT=promote_eltypeof(d2) - elseif isempty(d2) - TT=promote_eltypeof(d1) - else - TT=promote_type(promote_eltypeof(d1), - promote_eltypeof(d2)) - end - - D=zeros(Operator{TT},length(d1)+length(d2),2) - D[1:length(d1),1]=d1 - D[length(d1)+1:end,2]=d2 - D -end - -blockdiag(a::Operator,b::Operator) = blockdiag(Operator{promote_type(eltype(a),eltype(b))}[a], - Operator{promote_type(eltype(a),eltype(b))}[b]) - ## broadcase broadcast(::typeof(*),A::AbstractArray{N},D::Operator) where {N<:Number} = diff --git a/src/Spaces/ProductSpaceOperators.jl b/src/Spaces/ProductSpaceOperators.jl index 4d99f4cf..105da90a 100644 --- a/src/Spaces/ProductSpaceOperators.jl +++ b/src/Spaces/ProductSpaceOperators.jl @@ -55,9 +55,6 @@ end continuity(d::UnionDomain,k) = continuity(Space(d),k) continuity(d) = continuity(d,0) -blockdiag(A::PlusOperator) = mapreduce(blockdiag, +, A.ops) -blockdiag(A::TimesOperator) = mapreduce(blockdiag, .*, A.ops) - # TODO: general wrappers Evaluation(S::SumSpace,x,order) =