Skip to content

Commit e6b9dca

Browse files
Merge pull request #776 from ranocha/hr/isinplace
allow different number of out-of-place args for isinplace
2 parents 68f47f8 + 2d0b008 commit e6b9dca

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

src/alg_traits.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ Integral interpolation for the SDE solver algorithm. SDEs solutions depend on th
253253
the left-hand rule is taken, while Stratonovich calculus uses the right-hand rule. Unlike in standard Riemannian integration, these integral rules do
254254
not converge to the same answer. In the context of a stochastic differential equation, the underlying solution (and its mean, variance, etc.) is dependent
255255
on the integral rule that is chosen. This trait describes which interpretation the solver algorithm subscribes to, and thus whether the solution should
256-
be interpretated as the solution to the SDE under the Ito or Stratonovich interpretation.
256+
be interpreted as the solution to the SDE under the Ito or Stratonovich interpretation.
257257
258258
For more information, see https://oatml.cs.ox.ac.uk/blog/2022/03/22/ito-strat.html as a good high-level explanation.
259259

src/utils.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,18 @@ function Base.showerror(io::IO, e::FunctionArgumentsError)
216216
end
217217

218218
"""
219-
isinplace(f, inplace_param_number[,fname="f"])
219+
isinplace(f, inplace_param_number, fname = "f", iip_preferred = true;
220+
has_two_dispatches = true,
221+
outofplace_param_number = inplace_param_number - 1)
220222
isinplace(f::AbstractSciMLFunction[, inplace_param_number])
221223
222224
Check whether a function operates in place by comparing its number of arguments
223225
to the expected number. If `f` is an `AbstractSciMLFunction`, then the type
224226
parameter is assumed to be correct and is used. Otherwise `inplace_param_number`
225227
is checked against the methods table, where `inplace_param_number` is the number
226228
of arguments for the in-place dispatch. The out-of-place dispatch is assumed
227-
to have one less. If neither of these dispatches exist, an error is thrown.
229+
to have `outofplace_param_number` parameters (one less than the inplace version
230+
by default). If neither of these dispatches exist, an error is thrown.
228231
If the error is thrown, `fname` is used to tell the user which function has the
229232
incorrect dispatches.
230233
@@ -241,19 +244,20 @@ form is disabled and the 2-argument signature is ensured to be matched.
241244
- [`numargs`](@ref numargs)
242245
"""
243246
function isinplace(f, inplace_param_number, fname = "f", iip_preferred = true;
244-
has_two_dispatches = true, isoptimization = false)
247+
has_two_dispatches = true, isoptimization = false,
248+
outofplace_param_number = inplace_param_number - 1)
245249
nargs = numargs(f)
246250
iip_dispatch = any(x -> x == inplace_param_number, nargs)
247-
oop_dispatch = any(x -> x == inplace_param_number - 1, nargs)
251+
oop_dispatch = any(x -> x == outofplace_param_number, nargs)
248252

249253
if length(nargs) == 0
250254
throw(NoMethodsError(fname))
251255
end
252256

253257
if !iip_dispatch && !oop_dispatch && !isoptimization
254-
if all(x -> x > inplace_param_number, nargs)
258+
if all(>(inplace_param_number), nargs)
255259
throw(TooManyArgumentsError(fname, f))
256-
elseif all(x -> x < inplace_param_number - 1, nargs) && has_two_dispatches
260+
elseif all(<(outofplace_param_number), nargs) && has_two_dispatches
257261
# Possible extra safety?
258262
# Find if there's a `f(args...)` dispatch
259263
# If so, no error

test/function_building_error_messages.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ f = Foo{1}()
2424
(this::Foo{T})(args...) where {T} = 1
2525
@test SciMLBase.isinplace(Foo{Int}(), 4)
2626

27+
@testset "isinplace accepts an out-of-place version with different numbers of parameters " begin
28+
f1(u) = 2 * u
29+
@test !isinplace(f1, 2)
30+
@test_throws SciMLBase.TooFewArgumentsError SciMLBase.isinplace(f1, 4)
31+
@test !isinplace(f1, 4; outofplace_param_number = 1)
32+
end
33+
2734
## Problem argument tests
2835

2936
ftoomany(u, p, t, x, y) = 2u

0 commit comments

Comments
 (0)