Skip to content

Commit

Permalink
relax type requirement on TwiceDifferentiable (#140)
Browse files Browse the repository at this point in the history
* relax type requirement on TwiceDifferentiable

* added tests for ComponentArrays

Co-authored-by: amrods <11388389+amrods@users.noreply.github.com>
  • Loading branch information
longemen3000 and amrods authored Jul 26, 2021
1 parent 62d2199 commit 2072671
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ FiniteDiff = "2.0"
julia = "1.5"

[extras]
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
OptimTestProblems = "cec144fc-5a64-5bc6-99fb-dde8f63e154c"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -23,4 +24,4 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["LinearAlgebra", "OptimTestProblems", "Random", "RecursiveArrayTools", "SparseArrays", "Test"]
test = ["ComponentArrays", "LinearAlgebra", "OptimTestProblems", "Random", "RecursiveArrayTools", "SparseArrays", "Test"]
4 changes: 2 additions & 2 deletions src/objective_types/twicedifferentiable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ function TwiceDifferentiable(f, x::AbstractArray, F::Real = real(zero(eltype(x))
FiniteDiff.finite_difference_gradient!(storage, f, x, gcache)
return
end
function fg!(storage::Vector, x::Vector)
function fg!(storage, x)
g!(storage, x)
return f(x)
end

function h!(storage::Matrix, x::Vector)
function h!(storage, x)
FiniteDiff.finite_difference_hessian!(storage, f, x)
return
end
Expand Down
23 changes: 23 additions & 0 deletions test/abstractarrays.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
@testset "ComponentArrays" begin
x_seed_1 = [0.0]
x_seed_2 = [0.0]
x_seed = ComponentArray(x_seed_1=x_seed_1, x_seed_2=x_seed_2)
g_seed_1 = [0.0]
g_seed_2 = [0.0]
g_seed = ComponentArray(g_seed_1=g_seed_1, g_seed_2=g_seed_2)
f_x_seed = 8157.682077608529

nd = NonDifferentiable(exponential, x_seed)
@test nd.f == exponential
@test value(nd) == 0.0
@test nd.f_calls == [0]
od = OnceDifferentiable(exponential, exponential_gradient!, nothing, x_seed, 0.0, g_seed)
@test od.f == exponential
@test od.df == exponential_gradient!
@test value(od) == 0.0
@test od.f_calls == [0]
@test od.df_calls == [0]
@test typeof(od.DF) <: ComponentArray
@test typeof(od.x_f) <: ComponentArray
@test typeof(od.x_df) <: ComponentArray
end
@testset "Matrix OnceDifferentiable" begin
x_seed = fill(0.0, 1, 2)
g_seed = fill(0.0, 1, 2)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NLSolversBase, Test
using Random
using LinearAlgebra: Diagonal, I
using ComponentArrays
using SparseArrays
using OptimTestProblems
using RecursiveArrayTools
Expand Down

0 comments on commit 2072671

Please sign in to comment.