Skip to content

Homogenize use of Base functions #445

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
Apr 14, 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 src/ApproxFunBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Base: values, convert, getindex, setindex!, *, +, -, ==, <, <=, >, |, !,
setdiff, isless, union, angle, join, isnan, isapprox, isempty,
sort, merge, minimum, maximum, extrema, argmax,
argmin, findmax, findmin, isfinite,
zeros, zero, one, promote_rule, repeat, length, resize!, isinf,
zeros, zero, one, promote_rule, repeat, length, resize!, reshape, isinf,
getproperty, findfirst, unsafe_getindex, fld, div,
eachindex, firstindex, lastindex, isreal,
OneTo, Array, Vector, Matrix, view, ones, @propagate_inbounds,
Expand Down
4 changes: 2 additions & 2 deletions src/Multivariate/ProductFun.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ ProductFun(cfs::Vector{VFun{S,T}},sp::AbstractProductSpace{Tuple{W,V},DD}) where
W<:UnivariateSpace,T<:Number,DD} =
ProductFun{W,V,typeof(sp),T}(VFun{W,T}[Fun(cfs[k],columnspace(sp,k)) for k=1:length(cfs)],sp)

Base.size(f::ProductFun,k::Integer) =
size(f::ProductFun,k::Integer) =
k==1 ? mapreduce(ncoefficients,max,f.coefficients) : length(f.coefficients)
Base.size(f::ProductFun) = (size(f,1),size(f,2))
size(f::ProductFun) = (size(f,1),size(f,2))

## Construction in an AbstractProductSpace via a Matrix of coefficients

Expand Down
4 changes: 2 additions & 2 deletions src/Multivariate/TensorSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,10 @@ components(sp::TensorSpace{Tuple{S1,S2}}) where {S1<:Space{D,R},S2} where {D,R<:
components(sp::TensorSpace{Tuple{S1,S2}}) where {S1,S2<:Space{D,R}} where {D,R<:AbstractArray} =
[sp.spaces[1] ⊗ s for s in components(sp.spaces[2])]

Base.size(sp::TensorSpace{Tuple{S1,S2}}) where {S1<:Space{D,R},S2} where {D,R<:AbstractArray} =
size(sp::TensorSpace{Tuple{S1,S2}}) where {S1<:Space{D,R},S2} where {D,R<:AbstractArray} =
size(sp.spaces[1])

Base.size(sp::TensorSpace{Tuple{S1,S2}}) where {S1,S2<:Space{D,R}} where {D,R<:AbstractArray} =
size(sp::TensorSpace{Tuple{S1,S2}}) where {S1,S2<:Space{D,R}} where {D,R<:AbstractArray} =
size(sp.spaces[2])

# TODO: Generalize to higher dimensions
Expand Down
6 changes: 3 additions & 3 deletions src/Operators/Operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ blocksize(A::Operator,k) = k==1 ? length(blocklengths(rangespace(A))) : length(b
blocksize(A::Operator) = (blocksize(A,1),blocksize(A,2))


Base.size(A::Operator) = (size(A,1),size(A,2))
Base.size(A::Operator,k::Integer) = k==1 ? dimension(rangespace(A)) : dimension(domainspace(A))
Base.length(A::Operator) = size(A,1) * size(A,2)
size(A::Operator) = (size(A,1),size(A,2))
size(A::Operator,k::Integer) = k==1 ? dimension(rangespace(A)) : dimension(domainspace(A))
length(A::Operator) = size(A,1) * size(A,2)


# used to compute "end" for last index
Expand Down
2 changes: 1 addition & 1 deletion src/Operators/almostbanded/LowRankOperator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ rangespace(L::LowRankOperator) = space(first(L.U))
promoterangespace(L::LowRankOperator,sp::Space) = LowRankOperator(map(u->Fun(u,sp),L.U),L.V)
promotedomainspace(L::LowRankOperator,sp::Space) = LowRankOperator(L.U,map(v->promotedomainspace(v,sp),L.V))

function Base.getindex(L::LowRankOperator, k::Integer,j::Integer)
function getindex(L::LowRankOperator, k::Integer,j::Integer)
ret=zero(eltype(L))
for (p, LUp) in enumerate(L.U)
if k ≤ ncoefficients(LUp)
Expand Down
8 changes: 4 additions & 4 deletions src/Operators/banded/ConstantOperator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ domainspace(B::BasisFunctional) = B.space

convert(::Type{Operator{T}},B::BasisFunctional) where {T} = BasisFunctional{T,typeof(B.space)}(B.k,B.space)

Base.getindex(op::BasisFunctional{T},k::Integer) where {T} = (k==op.k) ? one(T) : zero(T)
Base.getindex(op::BasisFunctional{T},k::AbstractRange) where {T} = strictconvert(Vector{T},k.==op.k)
getindex(op::BasisFunctional{T},k::Integer) where {T} = (k==op.k) ? one(T) : zero(T)
getindex(op::BasisFunctional{T},k::AbstractRange) where {T} = strictconvert(Vector{T},k.==op.k)

struct FillFunctional{T} <: Operator{T}
λ::T
Expand All @@ -105,8 +105,8 @@ end

domainspace(B::FillFunctional) = ℓ⁰

Base.getindex(op::FillFunctional,k::Integer)=op.λ
Base.getindex(op::FillFunctional,k::AbstractRange)=fill(op.λ,length(k))
getindex(op::FillFunctional,k::Integer)=op.λ
getindex(op::FillFunctional,k::AbstractRange)=fill(op.λ,length(k))

## Zero is a special operator: it makes sense on all spaces, and between all spaces

Expand Down
2 changes: 1 addition & 1 deletion src/Operators/banded/ToeplitzOperator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ bandwidths(T::ToeplitzOperator)=(length(T.negative),length(T.nonnegative)-1)

# slice of a ToeplitzOPerator is a ToeplitzOperator

function Base.getindex(T::ToeplitzOperator,kr::InfRanges,jr::InfRanges)
function getindex(T::ToeplitzOperator,kr::InfRanges,jr::InfRanges)
sh=first(jr)-first(kr)
st=step(jr)
@assert st==step(kr)
Expand Down
10 changes: 5 additions & 5 deletions src/Operators/general/CachedOperator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ blockbandwidths(C::CachedOperator{T,BM,M}) where {T<:Number,BM<:BandedMatrix,M<:
@wrapperstructure CachedOperator


@propagate_inbounds function Base.getindex(B::CachedOperator,k::Integer,j::Integer)
@propagate_inbounds function getindex(B::CachedOperator,k::Integer,j::Integer)
resizedata!(B,k,j)
if k ≤ size(B.data,1) && j ≤ size(B.data,2)
B.data[k,j]
Expand All @@ -82,7 +82,7 @@ blockbandwidths(C::CachedOperator{T,BM,M}) where {T<:Number,BM<:BandedMatrix,M<:
end
end

function Base.getindex(B::CachedOperator,k::AbstractRange,j::AbstractRange)
function getindex(B::CachedOperator,k::AbstractRange,j::AbstractRange)
if !isempty(k) && !isempty(j)
resizedata!(B,maximum(k),maximum(j))
B.data[k,j]
Expand All @@ -91,7 +91,7 @@ function Base.getindex(B::CachedOperator,k::AbstractRange,j::AbstractRange)
end
end

function Base.getindex(B::CachedOperator,k::Integer)
function getindex(B::CachedOperator,k::Integer)
if size(B,1)==1
B[1,k]
elseif size(B,2)==1
Expand All @@ -101,12 +101,12 @@ function Base.getindex(B::CachedOperator,k::Integer)
end
end

@propagate_inbounds function Base.setindex!(B::CachedOperator,v,k,j)
@propagate_inbounds function setindex!(B::CachedOperator,v,k,j)
resizedata!(B,k,j)
B.data[k,j] = v
end

@propagate_inbounds function Base.setindex!(B::CachedOperator,v,k::Integer)
@propagate_inbounds function setindex!(B::CachedOperator,v,k::Integer)
if size(B,1)==1
B[1,k] = v
elseif size(B,2)==1
Expand Down
8 changes: 4 additions & 4 deletions src/PDE/KroneckerOperator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,18 @@ function mul_coefficients(A::SubOperator{T,KKO,NTuple{2,UnitRange{Int}}}, b) whe
M*pad(b, size(M,2))
end

Base.getindex(A::KroneckerOperator, B::MultivariateFun) = A[Fun(B)]
function Base.getindex(K::KroneckerOperator, f::LowRankFun)
getindex(A::KroneckerOperator, B::MultivariateFun) = A[Fun(B)]
function getindex(K::KroneckerOperator, f::LowRankFun)
op1, op2 = K.ops
sum(zip(f.A, f.B)) do (A,B)
op1[A] ⊗ op2[B]
end
end
Base.getindex(K::KroneckerOperator, B::ProductFun) = K[LowRankFun(B)]
getindex(K::KroneckerOperator, B::ProductFun) = K[LowRankFun(B)]

for F in [:MultivariateFun, :ProductFun, :LowRankFun]
for O in WrapperList
@eval Base.getindex(K::$O{<:KroneckerOperator}, f::$F) = K.op[f]
@eval getindex(K::$O{<:KroneckerOperator}, f::$F) = K.op[f]
@eval (*)(A::$O{<:KroneckerOperator}, B::$F) = A.op * B
@eval (*)(A::$F, B::$O{<:KroneckerOperator}) = A * B.op
end
Expand Down
12 changes: 6 additions & 6 deletions src/Spaces/ArraySpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ getindex(f::ArraySpace,k...) = Space(component(f,k...))
iterate(f::Fun{<:ArraySpace}) = iterate(components(f))


Base.reshape(AS::ArraySpace,k...) = ArraySpace(reshape(AS.spaces,k...))
reshape(AS::ArraySpace,k...) = ArraySpace(reshape(AS.spaces,k...))
dimension(AS::ArraySpace) = mapreduce(dimension,+,AS.spaces,init=0)

# TODO: union domain
Expand Down Expand Up @@ -112,14 +112,14 @@ repeat(A::ArraySpace,n,m) = ArraySpace(repeat(A.spaces,n,m))

component(A::MatrixSpace,k::Integer,j::Integer) = A.spaces[k,j]

Base.getindex(f::Fun{DSS},k::Integer) where {DSS<:ArraySpace} = component(f,k)
getindex(f::Fun{DSS},k::Integer) where {DSS<:ArraySpace} = component(f,k)


Base.getindex(f::Fun{<:MatrixSpace},k::Integer,j::Integer) =
getindex(f::Fun{<:MatrixSpace},k::Integer,j::Integer) =
f[k+stride(f,2)*(j-1)]

Base.getindex(f::Fun{DSS},kj::CartesianIndex{1}) where {DSS<:ArraySpace} = f[kj[1]]
Base.getindex(f::Fun{DSS},kj::CartesianIndex{2}) where {DSS<:ArraySpace} = f[kj[1],kj[2]]
getindex(f::Fun{DSS},kj::CartesianIndex{1}) where {DSS<:ArraySpace} = f[kj[1]]
getindex(f::Fun{DSS},kj::CartesianIndex{2}) where {DSS<:ArraySpace} = f[kj[1],kj[2]]


function Fun(A::AbstractMatrix{<:Fun{<:VectorSpace{S},V,VV}}) where {S,V,VV}
Expand Down Expand Up @@ -189,7 +189,7 @@ function choosedomainspace(A::VectorInterlaceOperator, sp::ArraySpace)
end


Base.reshape(f::Fun{AS},k...) where {AS<:ArraySpace} = Fun(reshape(space(f),k...),f.coefficients)
reshape(f::Fun{AS},k...) where {AS<:ArraySpace} = Fun(reshape(space(f),k...),f.coefficients)

Base.diff(f::Fun{AS,T},n...) where {AS<:ArraySpace,T} = Fun(diff(Array(f),n...))

Expand Down
4 changes: 2 additions & 2 deletions src/Spaces/ProductSpaceOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ DefiniteLineIntegral(sp::PiecewiseSpace) =

## TensorSpace of two PiecewiseSpaces

Base.getindex(d::TensorSpace{Tuple{PWS1,PWS2}},i::Integer,j::Integer) where {PWS1<:PiecewiseSpace,PWS2<:PiecewiseSpace} =
getindex(d::TensorSpace{Tuple{PWS1,PWS2}},i::Integer,j::Integer) where {PWS1<:PiecewiseSpace,PWS2<:PiecewiseSpace} =
d[1][i]⊗d[2][j]
Base.getindex(d::TensorSpace{Tuple{PWS1,PWS2}},i::AbstractRange,j::AbstractRange) where {PWS1<:PiecewiseSpace,PWS2<:PiecewiseSpace} =
getindex(d::TensorSpace{Tuple{PWS1,PWS2}},i::AbstractRange,j::AbstractRange) where {PWS1<:PiecewiseSpace,PWS2<:PiecewiseSpace} =
PiecewiseSpace(d[1][i])⊗PiecewiseSpace(d[2][j])

## ProductFun
Expand Down