Skip to content

Commit

Permalink
handle irrational constants by casting to float
Browse files Browse the repository at this point in the history
  • Loading branch information
birm authored Mar 11, 2020
1 parent e7def5f commit a67c657
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/finite_difference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ end
function finite_difference(f,
x::T,
dtype::Symbol = :central) where T <: Number
# handle irrationals
if T <: Irrational
x = float(x)
end
if dtype == :forward
@forwardrule x epsilon
xplusdx = x + epsilon
Expand Down Expand Up @@ -101,6 +105,10 @@ function finite_difference!(f,
x::AbstractVector{S},
g::AbstractVector{T},
dtype::Symbol) where {S <: Number, T <: Number}
# handle irrationals
if S <: Irrational
x = float.(x)
end
# What is the dimension of x?
n = length(x)

Expand Down Expand Up @@ -161,6 +169,10 @@ function finite_difference_jacobian!(f,
dtype::Symbol = :central) where {R <: Number,
S <: Number,
T <: Number}
# handle irrationals
if R <: Irrational
x = float.(x)
end
# What is the dimension of x?
m, n = size(J)

Expand Down Expand Up @@ -214,6 +226,10 @@ end

function finite_difference_hessian(f,
x::T) where T <: Number
# handle irrationals
if typeof(x) <: Irrational
x = float(x)
end
@hessianrule x epsilon
(f(x + epsilon) - 2*f(x) + f(x - epsilon))/epsilon^2
end
Expand All @@ -234,6 +250,10 @@ function finite_difference_hessian!(f,
x::AbstractVector{S},
H::Array{T}) where {S <: Number,
T <: Number}
# handle irrationals
if S <: Irrational
x = float.(x)
end
# What is the dimension of x?
n = length(x)

Expand Down

0 comments on commit a67c657

Please sign in to comment.