Skip to content

Commit 19b14b8

Browse files
committed
Define size unconditionally
All operators _must_ implement size. This is not part of the "matrix inerface"
1 parent afb8c4e commit 19b14b8

3 files changed

Lines changed: 15 additions & 37 deletions

File tree

src/gradgen_operator.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ end
5252
_exp_prop_convert_operator(::GradgenOperator) = Matrix{ComplexF64}
5353

5454
supports_inplace(::Type{GradgenOperator{N,GT,CGT}}) where {N,GT,CGT} =
55-
(supports_inplace(GT) && supports_inplace(CGT))
55+
supports_inplace(GT)
56+
# Note: `evaluate!` for `GradgenOperator` reassigns `control_deriv_ops[i]` via
57+
# vector element assignment, so the in-place capability of `CGT` is irrelevant.
58+
# Only the underlying operator `GT` needs to support in-place evaluation.
5659

5760
supports_matrix_interface(::Type{<:GradgenOperator{N,GT,CGT}}) where {N,GT,CGT} =
5861
supports_matrix_interface(GT) && supports_matrix_interface(CGT)

src/linalg.jl

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -241,52 +241,29 @@ function Base.fill!(Ψ::GradVector, v)
241241
end
242242

243243

244-
# === Matrix interface for GradgenOperator ===
245-
#
246-
# The following methods are part of the matrix interface and are only
247-
# meaningful when `supports_matrix_interface` is true for both component types.
248-
# Each method delegates to a private `_name(::Val{supports}, ...)` function:
249-
# the Val{true} method contains the implementation, and the Val{false} method
250-
# throws an error.
251-
252-
function _size(
253-
::Val{true},
254-
O::GradgenOperator{num_controls,GT,CGT}
255-
) where {num_controls,GT,CGT}
244+
function Base.size(O::GradgenOperator{num_controls,GT,CGT}) where {num_controls,GT,CGT}
256245
return (num_controls + 1) .* size(O.G)
257246
end
258247

259-
function _size(::Val{false}, O::GradgenOperator)
260-
error("$(typeof(O)) does not support the matrix interface")
261-
end
262-
263-
function Base.size(O::T) where {T<:GradgenOperator}
264-
return _size(Val(supports_matrix_interface(T)), O)
265-
end
266-
267-
268-
function _size(
269-
::Val{true},
270-
O::GradgenOperator{num_controls,GT,CGT},
271-
dim::Integer
272-
) where {num_controls,GT,CGT}
248+
function Base.size(O::GradgenOperator{num_controls,GT,CGT}, dim::Integer) where {num_controls,GT,CGT}
273249
return (num_controls + 1) * size(O.G, dim)
274250
end
275251

276-
function _size(::Val{false}, O::GradgenOperator, dim::Integer)
277-
error("$(typeof(O)) does not support the matrix interface")
278-
end
279252

280-
function Base.size(O::T, dim::Integer) where {T<:GradgenOperator}
281-
return _size(Val(supports_matrix_interface(T)), O, dim)
282-
end
253+
# === Matrix interface for GradgenOperator ===
254+
#
255+
# The following methods are part of the matrix interface and are only
256+
# meaningful when `supports_matrix_interface` is true for both component types.
257+
# Each method delegates to a private `_name(::Val{supports}, ...)` function:
258+
# the Val{true} method contains the implementation, and the Val{false} method
259+
# throws an error. Note that this does not include `size`, which must be
260+
# defined for _all_ operators, whether or not they define the full matrix
261+
# interface.
283262

284263

285264
# As for an `Operator`, we implement `similar` to return a standard `Array`
286265
# because `GradgenOperator` does not `setindex!`, so it's arguably not a
287266
# "mutable array" even if its components are mutable.
288-
# similar(O) and similar(O, S) call size(O), which will error if
289-
# !supports_matrix_interface. The dims-based variants need no guard.
290267
Base.similar(G::GradgenOperator) = Array{eltype(G)}(undef, size(G))
291268

292269
Base.similar(O::GradgenOperator, ::Type{S}) where {S} = Array{S}(undef, size(O))

test/test_interface.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,6 @@ end
301301

302302
# Matrix interface methods must throw an error when not supported
303303
@test_throws "does not support the matrix interface" op[1, 1]
304-
@test_throws "does not support the matrix interface" size(op)
305-
@test_throws "does not support the matrix interface" size(op, 1)
306304
@test_throws "does not support the matrix interface" length(op)
307305
@test_throws "does not support the matrix interface" iterate(op)
308306

0 commit comments

Comments
 (0)