Skip to content

Commit 15eeec1

Browse files
committed
methods: change all instances of Real and AbstractFloat to Number
... in the hopes of supporting Unitful numbers, since they are <: Number. This alone does not work though.
1 parent 633ec9b commit 15eeec1

1 file changed

Lines changed: 46 additions & 46 deletions

File tree

src/methods.jl

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ end
107107
FiniteDifferenceMethod(
108108
grid::AbstractVector{Int},
109109
q::Int;
110-
condition::Real=DEFAULT_CONDITION,
111-
factor::Real=DEFAULT_FACTOR,
112-
max_range::Real=Inf
110+
condition::Number=DEFAULT_CONDITION,
111+
factor::Number=DEFAULT_FACTOR,
112+
max_range::Number=Inf
113113
)
114114
115115
Construct a finite difference method.
@@ -120,9 +120,9 @@ Construct a finite difference method.
120120
- `q::Int`: Order of the derivative to estimate.
121121
122122
# Keywords
123-
- `condition::Real`: Condition number. See [`DEFAULT_CONDITION`](@ref).
124-
- `factor::Real`: Factor number. See [`DEFAULT_FACTOR`](@ref).
125-
- `max_range::Real=Inf`: Maximum distance that a function is evaluated from the input at
123+
- `condition::Number`: Condition number. See [`DEFAULT_CONDITION`](@ref).
124+
- `factor::Number`: Factor number. See [`DEFAULT_FACTOR`](@ref).
125+
- `max_range::Number=Inf`: Maximum distance that a function is evaluated from the input at
126126
which the derivative is estimated.
127127
128128
# Returns
@@ -131,9 +131,9 @@ Construct a finite difference method.
131131
function FiniteDifferenceMethod(
132132
grid::SVector{P,Int},
133133
q::Int;
134-
condition::Real=DEFAULT_CONDITION,
135-
factor::Real=DEFAULT_FACTOR,
136-
max_range::Real=Inf,
134+
condition::Number=DEFAULT_CONDITION,
135+
factor::Number=DEFAULT_FACTOR,
136+
max_range::Number=Inf,
137137
) where P
138138
_check_p_q(P, q)
139139
coefs, coefs_neighbourhood, ∇f_magnitude_mult, f_error_mult = _coefs_mults(grid, q)
@@ -153,7 +153,7 @@ function FiniteDifferenceMethod(grid::AbstractVector{Int}, q::Int; kw_args...)
153153
end
154154

155155
"""
156-
(m::FiniteDifferenceMethod)(f, x::T) where T<:AbstractFloat
156+
(m::FiniteDifferenceMethod)(f, x::T) where T<:Number
157157
158158
Estimate the derivative of `f` at `x` using the finite differencing method `m` and an
159159
automatically determined step size.
@@ -188,12 +188,12 @@ julia> FiniteDifferences.estimate_step(fdm, sin, 1.0) # Computes step size and
188188
# We loop over all concrete subtypes of `FiniteDifferenceMethod` for Julia v1.0 compatibility.
189189
for T in (UnadaptedFiniteDifferenceMethod, AdaptedFiniteDifferenceMethod)
190190
@eval begin
191-
function (m::$T)(f::TF, x::Real) where TF
191+
function (m::$T)(f::TF, x::Number) where TF
192192
x = float(x) # Assume that converting to float is desired, if it isn't already.
193193
step = first(estimate_step(m, f, x))
194194
return m(f, x, step)
195195
end
196-
function (m::$T{P,0})(f::TF, x::Real) where {P,TF}
196+
function (m::$T{P,0})(f::TF, x::Number) where {P,TF}
197197
# The automatic step size calculation fails if `Q == 0`, so handle that edge
198198
# case.
199199
return f(x)
@@ -202,15 +202,15 @@ for T in (UnadaptedFiniteDifferenceMethod, AdaptedFiniteDifferenceMethod)
202202
end
203203

204204
"""
205-
(m::FiniteDifferenceMethod)(f, x::T, step::Real) where T<:AbstractFloat
205+
(m::FiniteDifferenceMethod)(f, x::T, step::Number) where T<:Number
206206
207207
Estimate the derivative of `f` at `x` using the finite differencing method `m` and a given
208208
step size.
209209
210210
# Arguments
211211
- `f`: Function to estimate derivative of.
212212
- `x::T`: Input to estimate derivative at.
213-
- `step::Real`: Step size.
213+
- `step::Number`: Step size.
214214
215215
# Returns
216216
- Estimate of the derivative.
@@ -235,7 +235,7 @@ julia> fdm(sin, 1, 1e-3) - cos(1) # Check the error.
235235
# We loop over all concrete subtypes of `FiniteDifferenceMethod` for 1.0 compatibility.
236236
for T in (UnadaptedFiniteDifferenceMethod, AdaptedFiniteDifferenceMethod)
237237
@eval begin
238-
function (m::$T{P,Q})(f::TF, x::Real, step::Real) where {P,Q,TF}
238+
function (m::$T{P,Q})(f::TF, x::Number, step::Number) where {P,Q,TF}
239239
x = float(x) # Assume that converting to float is desired, if it isn't already.
240240
fs = _eval_function(m, f, x, step)
241241
return _compute_estimate(m, fs, x, step, m.coefs)
@@ -244,18 +244,18 @@ for T in (UnadaptedFiniteDifferenceMethod, AdaptedFiniteDifferenceMethod)
244244
end
245245

246246
function _eval_function(
247-
m::FiniteDifferenceMethod, f::TF, x::T, step::Real,
248-
) where {TF,T<:AbstractFloat}
247+
m::FiniteDifferenceMethod, f::TF, x::T, step::Number,
248+
) where {TF,T<:Number}
249249
return f.(x .+ T(step) .* m.grid)
250250
end
251251

252252
function _compute_estimate(
253253
m::FiniteDifferenceMethod{P,Q},
254254
fs::SVector{P,TF},
255255
x::T,
256-
step::Real,
256+
step::Number,
257257
coefs::SVector{P,Float64},
258-
) where {P,Q,TF,T<:AbstractFloat}
258+
) where {P,Q,TF,T<:Number}
259259
# If we substitute `T.(coefs)` in the expression below, then allocations occur. We
260260
# therefore perform the broadcasting first. See
261261
# https://github.com/JuliaLang/julia/issues/39151.
@@ -338,7 +338,7 @@ end
338338
m::FiniteDifferenceMethod,
339339
f,
340340
x::T
341-
) where T<:AbstractFloat
341+
) where T<:Number
342342
343343
Estimate the step size for a finite difference method `m`. Also estimates the error of the
344344
estimate of the derivative.
@@ -349,19 +349,19 @@ estimate of the derivative.
349349
- `x::T`: Point to estimate the derivative at.
350350
351351
# Returns
352-
- `Tuple{<:AbstractFloat, <:AbstractFloat}`: Estimated step size and an estimate of the
352+
- `Tuple{<:Number, <:Number}`: Estimated step size and an estimate of the
353353
error of the finite difference estimate. The error will be `NaN` if the method failed
354354
to estimate the error.
355355
"""
356356
function estimate_step(
357357
m::UnadaptedFiniteDifferenceMethod, f::TF, x::T,
358-
) where {TF,T<:AbstractFloat}
358+
) where {TF,T<:Number}
359359
step, acc = _compute_step_acc_default(m, x)
360360
return _limit_step(m, x, step, acc)
361361
end
362362
function estimate_step(
363363
m::AdaptedFiniteDifferenceMethod{P,Q}, f::TF, x::T,
364-
) where {P,Q,TF,T<:AbstractFloat}
364+
) where {P,Q,TF,T<:Number}
365365
∇f_magnitude, f_magnitude = _estimate_magnitudes(m.bound_estimator, f, x)
366366
if ∇f_magnitude == 0.0 || f_magnitude == 0.0
367367
step, acc = _compute_step_acc_default(m, x)
@@ -373,7 +373,7 @@ end
373373

374374
function _estimate_magnitudes(
375375
m::FiniteDifferenceMethod{P,Q}, f::TF, x::T,
376-
) where {P,Q,TF,T<:AbstractFloat}
376+
) where {P,Q,TF,T<:Number}
377377
step = first(estimate_step(m, f, x))
378378
fs = _eval_function(m, f, x, step)
379379
# Estimate magnitude of `∇f` in a neighbourhood of `x`.
@@ -388,13 +388,13 @@ function _estimate_magnitudes(
388388
return ∇f_magnitude, f_magnitude
389389
end
390390

391-
function _compute_step_acc_default(m::FiniteDifferenceMethod, x::T) where {T<:AbstractFloat}
391+
function _compute_step_acc_default(m::FiniteDifferenceMethod, x::T) where {T<:Number}
392392
# Compute a default step size using a heuristic and [`DEFAULT_CONDITION`](@ref).
393393
return _compute_step_acc(m, m.condition, eps(T))
394394
end
395395

396396
function _compute_step_acc(
397-
m::FiniteDifferenceMethod{P,Q}, ∇f_magnitude::Real, f_error::Real,
397+
m::FiniteDifferenceMethod{P,Q}, ∇f_magnitude::Number, f_error::Number,
398398
) where {P,Q}
399399
# Set the step size by minimising an upper bound on the error of the estimate.
400400
C₁ = f_error * m.f_error_mult * m.factor
@@ -406,8 +406,8 @@ function _compute_step_acc(
406406
end
407407

408408
function _limit_step(
409-
m::FiniteDifferenceMethod, x::T, step::Real, acc::Real,
410-
) where {T<:AbstractFloat}
409+
m::FiniteDifferenceMethod, x::T, step::Number, acc::Number,
410+
) where {T<:Number}
411411
# First, limit the step size based on the maximum range.
412412
step_max = m.max_range / maximum(abs.(m.grid))
413413
if step > step_max
@@ -433,9 +433,9 @@ for direction in [:forward, :central, :backward]
433433
p::Int,
434434
q::Int;
435435
adapt::Int=1,
436-
condition::Real=DEFAULT_CONDITION,
437-
factor::Real=DEFAULT_FACTOR,
438-
max_range::Real=Inf,
436+
condition::Number=DEFAULT_CONDITION,
437+
factor::Number=DEFAULT_FACTOR,
438+
max_range::Number=Inf,
439439
geom::Bool=false
440440
)
441441
_check_p_q(p, q)
@@ -484,9 +484,9 @@ for direction in [:forward, :central, :backward]
484484
p::Int,
485485
q::Int;
486486
adapt::Int=1,
487-
condition::Real=DEFAULT_CONDITION,
488-
factor::Real=DEFAULT_FACTOR,
489-
max_range::Real=Inf,
487+
condition::Number=DEFAULT_CONDITION,
488+
factor::Number=DEFAULT_FACTOR,
489+
max_range::Number=Inf,
490490
geom::Bool=false
491491
)
492492
@@ -500,9 +500,9 @@ Construct a finite difference method at a $($(Meta.quot(direction))) grid of `p`
500500
- `adapt::Int=1`: Use another finite difference method to estimate the magnitude of the
501501
`p`th order derivative, which is important for the step size computation. Recurse
502502
this procedure `adapt` times.
503-
- `condition::Real`: Condition number. See [`DEFAULT_CONDITION`](@ref).
504-
- `factor::Real`: Factor number. See [`DEFAULT_FACTOR`](@ref).
505-
- `max_range::Real=Inf`: Maximum distance that a function is evaluated from the input at
503+
- `condition::Number`: Condition number. See [`DEFAULT_CONDITION`](@ref).
504+
- `factor::Number`: Factor number. See [`DEFAULT_FACTOR`](@ref).
505+
- `max_range::Number=Inf`: Maximum distance that a function is evaluated from the input at
506506
which the derivative is estimated.
507507
- `geom::Bool`: Use geometrically spaced points instead of linearly spaced points.
508508
@@ -552,10 +552,10 @@ end
552552
extrapolate_fdm(
553553
m::FiniteDifferenceMethod,
554554
f,
555-
x::Real,
556-
initial_step::Real=10,
555+
x::Number,
556+
initial_step::Number=10,
557557
power::Int=1,
558-
breaktol::Real=Inf,
558+
breaktol::Number=Inf,
559559
kw_args...
560560
)
561561
@@ -568,19 +568,19 @@ automatically sets `power = 2` if `m` is symmetric and `power = 1`. Moreover, it
568568
# Arguments
569569
- `m::FiniteDifferenceMethod`: Finite difference method to estimate the step size for.
570570
- `f`: Function to evaluate the derivative of.
571-
- `x::Real`: Point to estimate the derivative at.
572-
- `initial_step::Real=10`: Initial step size.
571+
- `x::Number`: Point to estimate the derivative at.
572+
- `initial_step::Number=10`: Initial step size.
573573
574574
# Returns
575-
- `Tuple{<:AbstractFloat, <:AbstractFloat}`: Estimate of the derivative and error.
575+
- `Tuple{<:Number, <:Number}`: Estimate of the derivative and error.
576576
"""
577577
function extrapolate_fdm(
578578
m::FiniteDifferenceMethod,
579579
f,
580-
x::Real,
581-
initial_step::Real=10;
580+
x::Number,
581+
initial_step::Number=10;
582582
power::Int=1,
583-
breaktol::Real=Inf,
583+
breaktol::Number=Inf,
584584
kw_args...
585585
)
586586
(power == 1 && _is_symmetric(m)) && (power = 2)

0 commit comments

Comments
 (0)