Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle irrational constants by casting to float #148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions test/finite_difference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@test norm(Calculus.finite_difference(x -> sin(x), 1.0, :forward) - cos(1.0)) < 10e-4
@test norm(Calculus.finite_difference(x -> sin(x), 1.0, :central) - cos(1.0)) < 10e-4
@test norm(Calculus.finite_difference(x -> sin(x), 1.0) - cos(1.0)) < 10e-4
@test norm(Calculus.finite_difference(x -> sin(x), pi) + 1) < 10e-4

@test norm(Calculus.finite_difference(x -> exp(-x), 1.0, :forward) - (-exp(-1.0))) < 10e-4
@test norm(Calculus.finite_difference(x -> exp(-x), 1.0, :central) - (-exp(-1.0))) < 10e-4
Expand All @@ -21,6 +22,7 @@
@test norm(Calculus.finite_difference(x -> x[1]^2, [1.0], :forward) - [2.0]) < 10e-4
@test norm(Calculus.finite_difference(x -> x[1]^2, [1.0], :central) - [2.0]) < 10e-4
@test norm(Calculus.finite_difference(x -> x[1]^2, [1.0]) - [2.0]) < 10e-4
@test norm(Calculus.finite_difference(x -> x[1]^2, [float(pi)]) - [2*pi]) < 10e-4

@test norm(Calculus.finite_difference(x -> sin(x[1]), [1.0], :forward) - [cos(1.0)]) < 10e-4
@test norm(Calculus.finite_difference(x -> sin(x[1]), [1.0], :central) - [cos(1.0)]) < 10e-4
Expand Down