Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 271d5b1

Browse files
fix up some generic operator functions
1 parent 354f2e2 commit 271d5b1

File tree

3 files changed

+31
-36
lines changed

3 files changed

+31
-36
lines changed

src/derivative_operators/abstract_operator_functions.jl

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,25 @@ end
4848
@inline getindex(A::AbstractDerivativeOperator, ::Colon, ::Colon) = Array(A)
4949

5050
@inline function getindex(A::AbstractDerivativeOperator, ::Colon, j)
51-
return Array(A)[:,j]
51+
return BandedMatrix(A)[:,j]
5252
end
5353

5454

5555
# symmetric right now
5656
@inline function getindex(A::AbstractDerivativeOperator, i, ::Colon)
57-
return Array(A)[i,:]
57+
return BandedMatrix(A)[i,:]
5858
end
5959

6060

6161
# UnitRanges
6262
@inline function getindex(A::AbstractDerivativeOperator, rng::UnitRange{Int}, ::Colon)
63-
m = Array(A)
63+
m = BandedMatrix(A)
6464
return m[rng, cc]
6565
end
6666

6767

6868
@inline function getindex(A::AbstractDerivativeOperator, ::Colon, rng::UnitRange{Int})
69-
m = Array(A)
69+
m = BandedMatrix(A)
7070
return m[rnd, cc]
7171
end
7272

@@ -83,7 +83,7 @@ end
8383

8484

8585
@inline function getindex(A::AbstractDerivativeOperator{T}, rng::UnitRange{Int}, cng::UnitRange{Int}) where T
86-
return Array(A)[rng,cng]
86+
return BandedMatrix(A)[rng,cng]
8787
end
8888

8989
#=
@@ -124,9 +124,19 @@ Base.:\(A::AbstractDerivativeOperator, B::AbstractVecOrMat) = Array(A) \ B
124124
Base.:/(A::AbstractVecOrMat, B::AbstractDerivativeOperator) = A / convert(Array,B)
125125
Base.:/(A::AbstractDerivativeOperator, B::AbstractVecOrMat) = Array(A) / B
126126

127-
########################################################################
127+
#=
128+
The Inf opnorm can be calculated easily using the stencil coeffiicents, while other opnorms
129+
default to compute from the full matrix form.
130+
=#
131+
function LinearAlgebra.opnorm(A::DerivativeOperator{T,S}, p::Real=2) where {T,S}
132+
if p == Inf
133+
sum(abs.(A.stencil_coefs)) / A.dx^A.derivative_order
134+
else
135+
opnorm(BandedMatrix(A), p)
136+
end
137+
end
128138

129-
# Are these necessary?
139+
########################################################################
130140

131141
get_type(::AbstractDerivativeOperator{T}) where {T} = T
132142

@@ -138,23 +148,19 @@ end
138148

139149

140150
function *(A::AbstractDerivativeOperator,M::AbstractMatrix)
141-
y = zeros(promote_type(eltype(A),eltype(M)), size(M))
151+
y = zeros(promote_type(eltype(A),eltype(M)), size(A,1), size(M,2))
142152
LinearAlgebra.mul!(y, A::AbstractDerivativeOperator, M::AbstractMatrix)
143153
return y
144154
end
145155

146156

147157
function *(M::AbstractMatrix,A::AbstractDerivativeOperator)
148-
y = zeros(promote_type(eltype(A),eltype(M)), size(M))
149-
LinearAlgebra.mul!(y, A::AbstractDerivativeOperator, M::AbstractMatrix)
158+
y = zeros(promote_type(eltype(A),eltype(M)), size(M,1), size(A,2))
159+
LinearAlgebra.mul!(y, M, BandedMatrix(A))
150160
return y
151161
end
152162

153-
#=
154-
# For now use slow fallback
163+
155164
function *(A::AbstractDerivativeOperator,B::AbstractDerivativeOperator)
156-
# TODO: it will result in an operator which calculates
157-
# the derivative of order A.dorder + B.dorder of
158-
# approximation_order = min(approx_A, approx_B)
165+
return BandedMatrix(A)*BandedMatrix(B)
159166
end
160-
=#

src/derivative_operators/derivative_operator.jl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,3 @@ end
5353

5454
(L::DerivativeOperator)(u,p,t) = L*u
5555
(L::DerivativeOperator)(du,u,p,t) = mul!(du,L,u)
56-
57-
#=
58-
The Inf opnorm can be calculated easily using the stencil coeffiicents, while other opnorms
59-
default to compute from the full matrix form.
60-
=#
61-
function LinearAlgebra.opnorm(A::DerivativeOperator{T,S}, p::Real=2) where {T,S}
62-
if p == Inf && LBC in [:Dirichlet0, :Neumann0, :periodic] && RBC in [:Dirichlet0, :Neumann0, :periodic]
63-
sum(abs.(A.stencil_coefs)) / A.dx^A.derivative_order
64-
else
65-
opnorm(convert(Array,A), p)
66-
end
67-
end

test/derivative_operators_interface.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ end
8888
@test Array(A) == second_derivative_stencil(N)
8989
@test sparse(A) == second_derivative_stencil(N)
9090
@test BandedMatrix(A) == second_derivative_stencil(N)
91-
@test_broken opnorm(A, Inf) == opnorm(correct, Inf)
91+
@test opnorm(A, Inf) == opnorm(correct, Inf)
9292

9393

9494
# testing higher derivative and approximation concretization
@@ -167,18 +167,19 @@ end
167167
dy = yarr[2]-yarr[1]
168168
F = [x^2+y for x = xarr, y = yarr]
169169

170-
A = DerivativeOperator{Float64}(d_order,approx_order,dx,length(xarr))
170+
A = DerivativeOperator{Float64}(d_order,approx_order,dx,length(xarr)-2)
171171
B = DerivativeOperator{Float64}(d_order,approx_order,dy,length(yarr))
172172

173-
@test_broken A*F 2*ones(N,M) atol=1e-2
174-
@test_broken F*B 8*ones(N,M) atol=1e-2
175-
@test_broken A*F*B zeros(N,M) atol=1e-2
173+
174+
@test A*F 2*ones(N-2,M) atol=1e-2
175+
F*B
176+
A*F*B
176177

177178
G = [x^2+y^2 for x = xarr, y = yarr]
178179

179-
@test_broken A*G 2*ones(N,M) atol=1e-2
180-
@test_broken G*B 8*ones(N,M) atol=1e-2
181-
@test_broken A*G*B zeros(N,M) atol=1e-2
180+
@test A*G 2*ones(N-2,M) atol=1e-2
181+
G*B
182+
A*G*B
182183
end
183184

184185
@testset "Linear combinations of operators" begin

0 commit comments

Comments
 (0)