Skip to content

Commit a67c657

Browse files
authored
handle irrational constants by casting to float
1 parent e7def5f commit a67c657

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/finite_difference.jl

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ end
4545
function finite_difference(f,
4646
x::T,
4747
dtype::Symbol = :central) where T <: Number
48+
# handle irrationals
49+
if T <: Irrational
50+
x = float(x)
51+
end
4852
if dtype == :forward
4953
@forwardrule x epsilon
5054
xplusdx = x + epsilon
@@ -101,6 +105,10 @@ function finite_difference!(f,
101105
x::AbstractVector{S},
102106
g::AbstractVector{T},
103107
dtype::Symbol) where {S <: Number, T <: Number}
108+
# handle irrationals
109+
if S <: Irrational
110+
x = float.(x)
111+
end
104112
# What is the dimension of x?
105113
n = length(x)
106114

@@ -161,6 +169,10 @@ function finite_difference_jacobian!(f,
161169
dtype::Symbol = :central) where {R <: Number,
162170
S <: Number,
163171
T <: Number}
172+
# handle irrationals
173+
if R <: Irrational
174+
x = float.(x)
175+
end
164176
# What is the dimension of x?
165177
m, n = size(J)
166178

@@ -214,6 +226,10 @@ end
214226

215227
function finite_difference_hessian(f,
216228
x::T) where T <: Number
229+
# handle irrationals
230+
if typeof(x) <: Irrational
231+
x = float(x)
232+
end
217233
@hessianrule x epsilon
218234
(f(x + epsilon) - 2*f(x) + f(x - epsilon))/epsilon^2
219235
end
@@ -234,6 +250,10 @@ function finite_difference_hessian!(f,
234250
x::AbstractVector{S},
235251
H::Array{T}) where {S <: Number,
236252
T <: Number}
253+
# handle irrationals
254+
if S <: Irrational
255+
x = float.(x)
256+
end
237257
# What is the dimension of x?
238258
n = length(x)
239259

0 commit comments

Comments
 (0)