Skip to content

Commit 94f4a84

Browse files
authored
Merge pull request #34 from JuliaDiff/wb/order-derivative
Check that the derivative is nonnegative, and handle the case `q == 0`
2 parents 6e4b18c + 43fedf2 commit 94f4a84

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/methods.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ end
145145

146146
# Check the method and derivative orders for consistency
147147
function _check_p_q(p::Integer, q::Integer)
148+
q >= 0 || throw(ArgumentError("order of derivative must be nonnegative"))
148149
q < p || throw(ArgumentError("order of the method must be strictly greater than that " *
149150
"of the derivative"))
150151
# Check whether the method can be computed. We require the factorial of the
@@ -225,6 +226,10 @@ function fdm(
225226
bound > 0 || throw(ArgumentError("bound must be positive, got $bound"))
226227
0 <= adapt < 20 - m.p || throw(ArgumentError("can't perform $adapt adaptation steps"))
227228

229+
# The below calculation can fail for `m.q == 0`, because then `C₂` may be zero. We
230+
# therefore handle this edge case here.
231+
m.q == 0 && return m, f(x)
232+
228233
p = m.p
229234
q = m.q
230235
grid = m.grid

test/methods.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@ using FiniteDifferences: Forward, Backward, Central, Nonstandard
22

33
@testset "Methods" begin
44
for f in [:forward_fdm, :backward_fdm, :central_fdm]
5+
@eval @test $f(1, 0; bound=1)(sin, 1) == sin(1)
6+
@eval @test $f(2, 0; bound=1)(sin, 1) == sin(1)
7+
@eval @test $f(3, 0; bound=1)(sin, 1) == sin(1)
58
@eval @test $f(10, 1; bound=1)(sin, 1) cos(1)
69
@eval @test $f(10, 2; bound=1)(sin, 1) -sin(1)
710

11+
@eval @test $f(1, 0; bound=1)(exp, 1) == exp(1)
12+
@eval @test $f(2, 0; bound=1)(exp, 1) == exp(1)
13+
@eval @test $f(3, 0; bound=1)(exp, 1) == exp(1)
814
@eval @test $f(10, 1; bound=1)(exp, 1) exp(1)
915
@eval @test $f(10, 2; bound=1)(exp, 1) exp(1)
1016

17+
@eval @test $f(1, 0; bound=1)(abs2, 1) == 1
18+
@eval @test $f(2, 0; bound=1)(abs2, 1) == 1
19+
@eval @test $f(3, 0; bound=1)(abs2, 1) == 1
1120
@eval @test $f(10, 1; bound=1)(abs2, 1) 2
1221
@eval @test $f(10, 2; bound=1)(abs2, 1) 2
1322

23+
@eval @test $f(1, 0; bound=1)(sqrt, 1) == 1
24+
@eval @test $f(2, 0; bound=1)(sqrt, 1) == 1
25+
@eval @test $f(3, 0; bound=1)(sqrt, 1) == 1
1426
@eval @test $f(10, 1; bound=1)(sqrt, 1) .5
1527
@eval @test $f(10, 2; bound=1)(sqrt, 1) -.25
1628
end
@@ -64,6 +76,7 @@ using FiniteDifferences: Forward, Backward, Central, Nonstandard
6476
@testset "Types" begin
6577
@testset "$T" for T in (Forward, Backward, Central)
6678
@test T(5, 1)(sin, 1; adapt=4) cos(1)
79+
@test_throws ArgumentError T(3, 3)
6780
@test_throws ArgumentError T(3, 4)
6881
@test_throws ArgumentError T(40, 5)
6982
@test_throws ArgumentError T(5, 1)(sin, 1; adapt=200)

0 commit comments

Comments
 (0)