Skip to content

Commit 86f8606

Browse files
committed
Operator constructor instead of convert
1 parent e3f9926 commit 86f8606

32 files changed

+330
-174
lines changed

src/Operators/Operator.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,3 +900,5 @@ diagindrow(S::SubOperator) = diagindrow(S,parentindices(S)[1],parentindices(S)[2
900900

901901
# Conversion between operator types
902902
convert(::Type{O}, c::Operator) where {O<:Operator} = c isa O ? c : O(c)::O
903+
convert(::Type{Operator{T}}, c::Operator{S}) where {S,T} = c isa Operator{T} ? c : Operator{T}(c)::Operator{T}
904+
Operator(A::Operator) = A

src/Operators/SubOperator.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,16 @@ SubOperator(A,inds,dims) = SubOperator(A,inds,dims,(dims[1]-1,dims[2]-1))
7878
SubOperator(A,inds) = SubOperator(A,inds,map(length,inds))
7979

8080

81-
convert(::Type{Operator{T}},SO::SubOperator) where {T} =
81+
Operator{T}(SO::SubOperator) where {T} =
8282
SubOperator(Operator{T}(SO.parent),SO.indexes,SO.dims,SO.bandwidths)::Operator{T}
83+
function SubOperator{T,B,I,DI,BI}(S::SubOperator) where {T,B,I,DI,BI}
84+
SubOperator{T,B,I,DI,BI}(
85+
strictconvert(B, S.parent),
86+
strictconvert(I, S.indexes),
87+
strictconvert(DI, S.dims),
88+
strictconvert(BI, S.bandwidths),
89+
)
90+
end
8391

8492
function view(A::Operator,kr::InfRanges,jr::InfRanges)
8593
@assert isinf(size(A,1)) && isinf(size(A,2))

src/Operators/almostbanded/LowRankOperator.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ LowRankOperator(B::AbstractVector,S...) = LowRankOperator(strictconvert(Vector{O
3939
LowRankOperator(A::Fun,B::Operator) = LowRankOperator([A],[B])
4040

4141

42-
convert(::Type{Operator{T}},L::LowRankOperator{S}) where {S,T} =
42+
function LowRankOperator{S,T}(L::LowRankOperator) where {S,T}
4343
LowRankOperator{S,T}(strictconvert(Vector{VFun{S,T}},L.U),
4444
strictconvert(Vector{Operator{T}},L.V))
45+
end
46+
47+
Operator{T}(L::LowRankOperator{S}) where {S,T} = LowRankOperator{S,T}(S)
4548

4649

4750
datasize(L::LowRankOperator,k) =

src/Operators/almostbanded/LowRankPertOperator.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ function LowRankPertOperator(Bin::Operator,Lin::LowRankOperator)
2020
end
2121

2222

23-
24-
convert(::Type{Operator{T}},L::LowRankPertOperator) where {T} =
25-
LowRankPertOperator(Operator{T}(L.op),Operator{T}(L.pert))::Operator{T}
23+
function LowRankPertOperator{OO,LR,T}(L::LowRankPertOperator) where {OO,LR,T}
24+
LowRankPertOperator{OO,LR,T}(strictconvert(OO, L.op), strictconvert(LR, L.pert))
25+
end
26+
function Operator{T}(L::LowRankPertOperator) where {T}
27+
LowRankPertOperator(Operator{T}(L.op),Operator{T}(L.pert))
28+
end
2629
convert(::Type{Operator},V::AbstractVector{OT}) where {OT<:Operator}=LowRankPertOperator(V)
2730

2831

src/Operators/banded/CalculusOperator.jl

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,18 @@ macro calculus_operator(Op)
5858
$Op(x...) = $DefaultOp(x...)
5959
$ConcOp(S::Space) = $ConcOp(S,1)
6060

61-
function Base.convert(::Type{Operator{T}},D::$ConcOp) where T
62-
if T==eltype(D)
63-
D
64-
else
65-
$ConcOp{typeof(D.space),typeof(D.order),T}(D.space, D.order)
66-
end
61+
function Operator{T}(D::$ConcOp) where {T}
62+
$ConcOp{typeof(D.space),typeof(D.order),T}(D.space, D.order)
6763
end
6864

6965
$WrappOp(op::Operator, order = 1, d = domainspace(op), r = rangespace(op)) =
7066
$WrappOp{typeof(op),typeof(d),typeof(r),typeof(order),eltype(op)}(op,order,d,r)
7167

72-
function Base.convert(::Type{Operator{T}},D::$WrappOp) where T
73-
if T==eltype(D)
74-
D
75-
else
76-
op=ApproxFunBase.strictconvert(Operator{T},D.op)
77-
S = domainspace(D)
78-
R = rangespace(D)
79-
$WrappOp(op,D.order,S,R)::Operator{T}
80-
end
68+
function Operator{T}(D::$WrappOp) where {T}
69+
op=ApproxFunBase.strictconvert(Operator{T},D.op)
70+
S = domainspace(D)
71+
R = rangespace(D)
72+
$WrappOp(op,D.order,S,R)::Operator{T}
8173
end
8274

8375
## Routines

src/Operators/banded/ConstantOperator.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export ConstantOperator, IdentityOperator, BasisFunctional
22

3-
struct ConstantOperator{T,DS} <: Operator{T}
3+
struct ConstantOperator{T,DS<:Space} <: Operator{T}
44
λ::T
55
space::DS
66
ConstantOperator{T,DS}(c::Number,sp::DS) where {T,DS} = new{T,DS}(strictconvert(T,c),sp)
@@ -36,12 +36,11 @@ getindex(C::ConstantOperator,k::Integer,j::Integer) =
3636

3737
==(C1::ConstantOperator, C2::ConstantOperator) = C1.λ==C2.λ
3838

39-
function convert(::Type{Operator{T}}, C::ConstantOperator) where T
40-
if T == eltype(C)
41-
C
42-
else
43-
ConstantOperator{T,typeof(C.space)}(C.λ,C.space)
44-
end
39+
function ConstantOperator{T,DS}(C::ConstantOperator) where {T,DS}
40+
ConstantOperator{T,DS}(strictconvert(T, C.λ), strictconvert(DS, C.space))
41+
end
42+
function Operator{T}(C::ConstantOperator) where T
43+
ConstantOperator{T,typeof(C.space)}(C.λ,C.space)
4544
end
4645

4746
# zero needs to be different since it can take a space to

src/Operators/banded/Conversion.jl

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ function ConcreteConversion(a::Space,b::Space)
1616
ConcreteConversion(T, a, b)
1717
end
1818

19-
function convert(::Type{Operator{T}}, C::ConcreteConversion) where {T}
20-
if T==eltype(C)
21-
C
22-
else
23-
ConcreteConversion(T, C.domainspace,C.rangespace)::Operator{T}
24-
end
19+
function ConcreteConversion{D,R,T}(C::ConcreteConversion) where {D<:Space,R<:Space,T}
20+
ConcreteConversion{D,R,T}(strictconvert(D,C.domainspace), strictconvert(R, C.rangetype))
21+
end
22+
23+
function Operator{T}(C::ConcreteConversion) where {T}
24+
ConcreteConversion(T, C.domainspace,C.rangespace)::Operator{T}
2525
end
2626

2727
domainspace(C::ConcreteConversion)=C.domainspace
@@ -126,15 +126,14 @@ Conversion(A::Space,B::Space,C::Space,D::Space...) =
126126

127127
==(A::ConversionWrapper,B::ConversionWrapper) = A.op==B.op
128128

129-
130-
function convert(::Type{Operator{T}},D::ConversionWrapper) where T
131-
if T==eltype(D)
132-
D
133-
else
134-
BO=strictconvert(Operator{T},D.op)
135-
d, r = domainspace(D), rangespace(D)
136-
ConversionWrapper(d, r, BO)::Operator{T}
137-
end
129+
function ConversionWrapper{D,R,T,O}(C::ConcreteConversion) where {D<:Space,R<:Space,T,O<:Operator{T}}
130+
ConversionWrapper{D,R,T,O}(strictconvert(D,C.domainspace), strictconvert(R,C.rangespace),
131+
strictconvert(O, C.op))
132+
end
133+
function Operator{T}(D::ConversionWrapper) where T
134+
BO=strictconvert(Operator{T},D.op)
135+
d, r = domainspace(D), rangespace(D)
136+
ConversionWrapper(d, r, BO)::Operator{T}
138137
end
139138

140139
convert(::Type{T}, C::ConversionWrapper) where {T<:Number} = strictconvert(T, C.op)

src/Operators/banded/Multiplication.jl

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,11 @@ Multiplication(c::Number) = Multiplication(Fun(c) )
5151
# This covers right multiplication unless otherwise specified.
5252
Multiplication(S::Space,f::Fun) = Multiplication(f,S)
5353

54-
55-
function convert(::Type{Operator{T}},C::ConcreteMultiplication{S,V}) where {S,V,T}
56-
if T==eltype(C)
57-
C
58-
else
59-
ConcreteMultiplication{S,V,T}(Fun{S,T}(C.f),C.space)
60-
end
54+
function ConcreteMultiplication{D,S,T}(C::ConcreteMultiplication) where {D<:Space,S<:Space,T}
55+
ConcreteMultiplication{D,S,T}(strictconvert(VFun{D,T}, C.f), strictconvert(S, C.space))
56+
end
57+
function Operator{T}(C::ConcreteMultiplication{S,V}) where {S,V,T}
58+
ConcreteMultiplication{S,V,T}(Fun{S,T}(C.f),C.space)
6159
end
6260

6361
domainspace(M::ConcreteMultiplication{D,S,T}) where {D,S,T} = M.space
@@ -113,12 +111,11 @@ domainspace(M::MultiplicationWrapper) = M.space
113111

114112
@wrapper MultiplicationWrapper false
115113

116-
function convert(::Type{Operator{TT}},C::MultiplicationWrapper{S,V,O,T}) where {TT,S,V,O,T}
117-
if TT==T
118-
C
119-
else
120-
MultiplicationWrapper(Fun{S,TT}(C.f),Operator{TT}(C.op), C.space)::Operator{TT}
121-
end
114+
function MultiplicationWrapper{D,S,T,O}(M::MultiplicationWrapper) where {D<:Space,S<:Space,T,O<:Operator{T}}
115+
MultiplicationWrapper{D,S,T,O}(strictconvert(VFun{D,T}, M.f), strictconvert(O, M.op), strictconvert(S, M.space))
116+
end
117+
function Operator{TT}(C::MultiplicationWrapper{S}) where {TT,S}
118+
MultiplicationWrapper(Fun{S,TT}(C.f),Operator{TT}(C.op), C.space)::Operator{TT}
122119
end
123120

124121

src/Operators/banded/PermutationOperator.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Creates a operator that permutes rows, in blocks of size
22
# length(perm)
3-
struct PermutationOperator{T,DS,RS} <: Operator{T}
3+
struct PermutationOperator{T,DS<:Space,RS<:Space} <: Operator{T}
44
perm::Vector{Int}
55
domainspace::DS
66
rangespace::RS
@@ -13,8 +13,10 @@ PermutationOperator(prm) = PermutationOperator(prm, ℓ⁰, ℓ⁰)
1313
domainspace(P::PermutationOperator) = P.domainspace
1414
rangespace(P::PermutationOperator) = P.rangespace
1515

16-
17-
convert(::Type{Operator{T}},P::PermutationOperator) where {T} =
16+
function PermutationOperator{T,DS,RS}(P::PermutationOperator) where {T,DS<:Space,RS<:Space}
17+
PermutationOperator{T,DS,RS}(P.perm, strictconvert(DS, V.domainspace), strictconvert(RS, V.rangespace))
18+
end
19+
Operator{T}(P::PermutationOperator) where {T} =
1820
PermutationOperator{T}(P.perm, P.domainspace, P.rangespace)
1921

2022
function bandwidths(P::PermutationOperator)
@@ -39,7 +41,7 @@ perm(a::Vector,b::Vector) = multiplyperm(invperm(sortperm(b)),sortperm(a))
3941
perm(a::Tuple,b::Tuple) = perm(collect(a),collect(b))
4042

4143

42-
struct NegateEven{T,DS,RS} <: Operator{T}
44+
struct NegateEven{T,DS<:Space,RS<:Space} <: Operator{T}
4345
domainspace::DS
4446
rangespace::RS
4547
end
@@ -52,7 +54,10 @@ NegateEven() = NegateEven(ℓ⁰,ℓ⁰)
5254
domainspace(P::NegateEven) = P.domainspace
5355
rangespace(P::NegateEven) = P.rangespace
5456

55-
convert(::Type{Operator{T}},P::NegateEven) where {T} =
57+
function NegateEven{T,DS,RS}(N::NegateEven) where {T,DS<:Space,RS<:Space}
58+
NegateEven{T,DS,RS}(strictconvert(DS, N.domainspace), strictconvert(RS, N.rangespace))
59+
end
60+
Operator{T}(P::NegateEven) where {T} =
5661
NegateEven{T}(P.domainspace, P.rangespace)
5762

5863
bandwidths(P::NegateEven) = (0,0)

src/Operators/banded/Reverse.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ for TYP in (:ReverseOrientation,:Reverse)
1010
end
1111

1212
$WRAP(op::Operator) = $WRAP{typeof(op),eltype(op)}(op)
13-
convert(::Type{Operator{T}},op::$TYP) where {T} = $TYP{T}()
14-
convert(::Type{Operator{T}},op::$WRAP) where {T} = $WRAP(Operator{T}(op.op))::Operator{T}
13+
$TYP{T}(op::$TYP) where {T} = $TYP{T}()
14+
Operator{T}(op::$TYP) where {T} = $TYP{T}()
15+
$WRAP{OS,T}(op::$WRAP) where {OS,T} = $WRAP{OS,T}(strictconvert(OS,op))
16+
Operator{T}(op::$WRAP) where {T} = $WRAP(Operator{T}(op.op))::Operator{T}
1517

1618
@wrapper $WRAP
1719
end

0 commit comments

Comments
 (0)