Skip to content

Commit

Permalink
Loosen type annotations (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored and pkofod committed Sep 22, 2017
1 parent f89e171 commit 514e083
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/objective_types.jl
Expand Up @@ -3,41 +3,41 @@ real_to_complex(d::AbstractObjective, x) = iscomplex(d) ? real_to_complex(x) : x
complex_to_real(d::AbstractObjective, x) = iscomplex(d) ? complex_to_real(x) : x

# Used for objectives and solvers where no gradient is available/exists
mutable struct NonDifferentiable{T,Tcplx} <: AbstractObjective where {T<:Real,
mutable struct NonDifferentiable{T,A<:AbstractArray{T},Tcplx} <: AbstractObjective where {T<:Real,
Tcplx<:Union{Val{true},Val{false}} #if true, must convert back on every f call
}
f
f_x::T
last_x_f::Array{T}
last_x_f::A
f_calls::Vector{Int}
end
iscomplex(obj::NonDifferentiable{T,Val{true}}) where {T} = true
iscomplex(obj::NonDifferentiable{T,Val{false}}) where {T} = false
NonDifferentiable(f,f_x::T, last_x_f::Array, f_calls::Vector{Int}) where {T} = NonDifferentiable{T,Val{false}}(f,f_x,last_x_f,f_calls) #compatibility with old constructor
iscomplex(obj::NonDifferentiable{T,A,Val{true}}) where {T,A} = true
iscomplex(obj::NonDifferentiable{T,A,Val{false}}) where {T,A} = false
NonDifferentiable(f,f_x::T, last_x_f::AbstractArray{T}, f_calls::Vector{Int}) where {T} = NonDifferentiable{T,typeof(last_x_f),Val{false}}(f,f_x,last_x_f,f_calls) #compatibility with old constructor

function NonDifferentiable(f, x_seed::AbstractArray)
iscomplex = eltype(x_seed) <: Complex
if iscomplex
x_seed = complex_to_real(x_seed)
end
NonDifferentiable{eltype(x_seed),Val{iscomplex}}(f, f(x_seed), copy(x_seed), [1])
NonDifferentiable{eltype(x_seed),typeof(x_seed),Val{iscomplex}}(f, f(x_seed), copy(x_seed), [1])
end

# Used for objectives and solvers where the gradient is available/exists
mutable struct OnceDifferentiable{T, Tgrad, Tcplx} <: AbstractObjective where {T<:Real, Tgrad, Tcplx<:Union{Val{true},Val{false}}}
mutable struct OnceDifferentiable{T, Tgrad, A<:AbstractArray{T}, Tcplx} <: AbstractObjective where {T<:Real, Tgrad, Tcplx<:Union{Val{true},Val{false}}}
f
g!
fg!
f_x::T
g::Tgrad
last_x_f::Array{T}
last_x_g::Array{T}
last_x_f::A
last_x_g::A
f_calls::Vector{Int}
g_calls::Vector{Int}
end
iscomplex(obj::OnceDifferentiable{T,Tgrad,Val{true}}) where {T,Tgrad} = true
iscomplex(obj::OnceDifferentiable{T,Tgrad,Val{false}}) where {T,Tgrad} = false
OnceDifferentiable(f,g!,fg!,f_x::T, g::Tgrad, last_x_f::Array, last_x_g::Array, f_calls::Vector{Int}, g_calls::Vector{Int}) where {T, Tgrad} = OnceDifferentiable{T,Tgrad,Val{false}}(f,g!,fg!,f_x, g, last_x_f, last_x_g, f_calls, g_calls) #compatibility with old constructor
iscomplex(obj::OnceDifferentiable{T,Tgrad,A,Val{true}}) where {T,Tgrad,A} = true
iscomplex(obj::OnceDifferentiable{T,Tgrad,A,Val{false}}) where {T,Tgrad,A} = false
OnceDifferentiable(f,g!,fg!,f_x::T, g::Tgrad, last_x_f::A, last_x_g::A, f_calls::Vector{Int}, g_calls::Vector{Int}) where {T, Tgrad, A<:AbstractArray{T}} = OnceDifferentiable{T,Tgrad,A,Val{false}}(f,g!,fg!,f_x, g, last_x_f, last_x_g, f_calls, g_calls) #compatibility with old constructor

# The user friendly/short form OnceDifferentiable constructor
function OnceDifferentiable(f, g!, fg!, x_seed::AbstractArray)
Expand All @@ -49,7 +49,7 @@ function OnceDifferentiable(f, g!, fg!, x_seed::AbstractArray)
x_seed = complex_to_real(x_seed)
g = complex_to_real(g)
end
OnceDifferentiable{eltype(x_seed),typeof(x_seed),Val{iscomplex}}(f, g!, fg!, f_val, g, copy(x_seed), copy(x_seed), [1], [1])
OnceDifferentiable{eltype(x_seed),typeof(g),typeof(x_seed),Val{iscomplex}}(f, g!, fg!, f_val, g, copy(x_seed), copy(x_seed), [1], [1])
end
# Automatically create the fg! helper function if only f and g! is provided
function OnceDifferentiable(f, g!, x_seed::AbstractArray)
Expand All @@ -61,17 +61,17 @@ function OnceDifferentiable(f, g!, x_seed::AbstractArray)
end

# Used for objectives and solvers where the gradient and Hessian is available/exists
mutable struct TwiceDifferentiable{T<:Real} <: AbstractObjective
mutable struct TwiceDifferentiable{T<:Real,Tgrad,A<:AbstractArray{T}} <: AbstractObjective
f
g!
fg!
h!
f_x::T
g::Vector{T}
g::Tgrad
H::Matrix{T}
last_x_f::Vector{T}
last_x_g::Vector{T}
last_x_h::Vector{T}
last_x_f::A
last_x_g::A
last_x_h::A
f_calls::Vector{Int}
g_calls::Vector{Int}
h_calls::Vector{Int}
Expand All @@ -84,7 +84,7 @@ function TwiceDifferentiable(td::TwiceDifferentiable, x::AbstractArray)
td
end

function TwiceDifferentiable(f, g!, fg!, h!, x_seed::Array{T}) where T
function TwiceDifferentiable(f, g!, fg!, h!, x_seed::AbstractArray{T}) where T
n_x = length(x_seed)
g = similar(x_seed)
H = Array{T}(n_x, n_x)
Expand All @@ -100,8 +100,8 @@ end
function TwiceDifferentiable(f,
g!,
h!,
x_seed::Array{T}) where T
function fg!(storage::Vector, x::Vector)
x_seed::AbstractArray{T}) where T
function fg!(storage, x)
g!(storage, x)
return f(x)
end
Expand Down

0 comments on commit 514e083

Please sign in to comment.