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

Remove gradient #3

Open
wants to merge 12 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ProximalCore"
uuid = "dc4f5ac2-75d1-4f31-931e-60435d74994b"
authors = ["Lorenzo Stella <lorenzostella@gmail.com>"]
version = "0.1.2"
version = "0.2.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
35 changes: 0 additions & 35 deletions src/ProximalCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,6 @@ is_convex(::T) where T = is_convex(T)
is_generalized_quadratic(::Type) = false
is_generalized_quadratic(::T) where T = is_generalized_quadratic(T)

"""
gradient!(y, f, x)

In-place gradient (and value) of `f` at `x`.

The gradient is written to the (pre-allocated) array `y`, which should have the same shape/size as `x`.

Returns the value `f` at `x`.

See also: [`gradient`](@ref).
"""
gradient!

"""
gradient(f, x)

Gradient (and value) of `f` at `x`.

Return a tuple `(y, fx)` consisting of
- `y`: the gradient of `f` at `x`
- `fx`: the value of `f` at `x`

See also: [`gradient!`](@ref).
"""
function gradient(f, x)
y = similar(x)
fx = gradient!(y, f, x)
return y, fx
end

"""
prox!(y, f, x, gamma=1)

Expand Down Expand Up @@ -83,11 +53,6 @@ struct Zero end

(::Zero)(x) = real(eltype(x))(0)

function gradient!(y, f::Zero, x)
y .= eltype(x)(0)
return f(x)
end

function prox!(y, ::Zero, x, gamma)
y .= x
return real(eltype(y))(0)
Expand Down
23 changes: 17 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ using Test
using Aqua
using LinearAlgebra
using ProximalCore
using ProximalCore: prox, gradient, convex_conjugate
using ProximalCore: Zero, IndZero
using ProximalCore: prox
using ProximalCore: Zero, IndZero, convex_conjugate
import ProximalCore: prox!, is_convex, is_generalized_quadratic

@testset "Aqua" begin
Expand Down Expand Up @@ -36,10 +36,10 @@ end
@test is_generalized_quadratic(Zero())

for T in [Float32, Float64]
@test let x = T[1.0, 2.0, 3.0]
prox(Zero(), x, T(42)) == (x, T(0))
gradient(Zero(), x) == (T[0, 0, 0], T(0))
end
x = T[1.0, 2.0, 3.0]
@test Zero()(x) == T(0)
@test prox(Zero(), x, T(42)) == (x, T(0))
@test prox(Zero(), x, ) == (x, T(0))
end

end
Expand All @@ -57,9 +57,20 @@ end
@test IndZero()(x) == T(Inf)
@test IndZero()(T[0, 0, 0]) == T(0)
@test prox(IndZero(), x, T(42)) == (T[0, 0, 0], T(0))
@test prox(IndZero(), x) == (T[0, 0, 0], T(0))
end

end

@testset "Others" begin

@inferred (f -> Val(is_convex(f)))(42)
@inferred (f -> Val(is_generalized_quadratic(f)))(42)

@test !is_convex(42)
@test !is_generalized_quadratic(42)

end

@testset "Conjugation" begin

Expand Down