From 0b1cdf8edf91f6d6eff6acfe3470e58ae977d031 Mon Sep 17 00:00:00 2001 From: Patrick Kofod Mogensen Date: Thu, 19 Oct 2017 17:52:00 +0200 Subject: [PATCH] Fix #121 by removing type annotations. (#122) * Fix #121 by removing type annotations. * Add test. --- src/differentiable_functions.jl | 10 +++++----- src/utils.jl | 2 +- test/api.jl | 23 +++++++++++++++++++++++ test/runtests.jl | 3 ++- 4 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 test/api.jl diff --git a/src/differentiable_functions.jl b/src/differentiable_functions.jl index a176a36..4679e8e 100644 --- a/src/differentiable_functions.jl +++ b/src/differentiable_functions.jl @@ -21,7 +21,7 @@ function DifferentiableMultivariateFunction(f!, g!, fg!, initial_x::AbstractArra end function DifferentiableMultivariateFunction(f!, g!) - function fg!(x::AbstractVector, fx::AbstractVector, gx::AbstractMatrix) + function fg!(x, fx, gx) f!(x, fx) g!(x, gx) end @@ -38,7 +38,7 @@ function DifferentiableMultivariateFunction(f!; dtype::Symbol=:central) end finite_difference_jacobian!(f, x, fx, gx, dtype) end - function g!(x::AbstractVector, gx::AbstractMatrix) + function g!(x, gx) fx = similar(x) fg!(x, fx, gx) end @@ -52,7 +52,7 @@ end # Helper for the case where only f! and fg! are available function only_f!_and_fg!(f!, fg!) - function g!(x::AbstractVector, gx::AbstractMatrix) + function g!(x, gx) fx = similar(x) fg!(x, fx, gx) end @@ -66,11 +66,11 @@ end # Helper for the case where only fg! is available function only_fg!(fg!) - function f!(x::AbstractVector, fx::AbstractVector) + function f!(x, fx) gx = Array{eltype(x)}(length(x), length(x)) fg!(x, fx, gx) end - function g!(x::AbstractVector, gx::AbstractMatrix) + function g!(x, gx) fx = similar(x) fg!(x, fx, gx) end diff --git a/src/utils.jl b/src/utils.jl index 5ea31be..8eb7982 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -38,7 +38,7 @@ end # Helpers for functions that do not modify arguments in place but return function not_in_place(f) - function f!(x::AbstractVector, fx::AbstractVector) + function f!(x, fx) copy!(fx, f(x)) end end diff --git a/test/api.jl b/test/api.jl new file mode 100644 index 0000000..970d123 --- /dev/null +++ b/test/api.jl @@ -0,0 +1,23 @@ +# taken from https://github.com/JuliaNLSolvers/NLsolve.jl/issues/121 + +function f(x) + eq = Array{Float64}(length(x)) + + eq[1] = 5 .* x[1] - x[2].^2 + eq[2] = 4 .* x[2] - x[1] + + return eq +end + +function J(x) + jmat = Array{Float64}(length(x),length(x)) + + jmat[1,1] = 5 + jmat[1,2] = -2.*x[2] + jmat[2,1] = -1 + jmat[2,2] = 4 + + return jmat +end + +x0 = [3.0, 12.0] diff --git a/test/runtests.jl b/test/runtests.jl index b48d2ba..2c66796 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -13,7 +13,8 @@ Base.reshape(A::WrappedArray, dims::Int...) = WrappedArray(reshape(A.x, dims...) if length(ARGS) > 0 tests = map(add_jl, ARGS) else - tests = ["2by2.jl", + tests = ["api.jl", + "2by2.jl", "singular.jl", "finite_difference.jl", "minpack.jl",