forked from JuliaFirstOrder/ProximalCore.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_functions.jl
More file actions
95 lines (73 loc) · 1.87 KB
/
base_functions.jl
File metadata and controls
95 lines (73 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Zero()
Constructs the zero function, i.e., the function that is zero everywhere.
# Example
```jldoctest
julia> f = ProximalCore.Zero()
ProximalCore.Zero()
julia> f(rand(3))
0.0
```
"""
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)
end
is_convex(::Type{Zero}) = true
is_generalized_quadratic(::Type{Zero}) = true
"""
IndZero()
Constructs the indicator function of the zero set, i.e., the function that is zero if the input is zero and infinity otherwise.
# Example
```jldoctest
julia> f = ProximalCore.IndZero()
ProximalCore.IndZero()
julia> f([1.0, 2.0, 3.0])
Inf
julia> f([0.0, 0.0, 0.0])
0.0
```
"""
struct IndZero end
function (::IndZero)(x)
R = real(eltype(x))
if iszero(x)
return R(0)
end
return R(Inf)
end
is_convex(::Type{IndZero}) = true
is_generalized_quadratic(::Type{IndZero}) = true
function prox!(y, ::IndZero, x, gamma)
R = real(eltype(x))
y .= R(0)
return R(0)
end
"""
ConvexConjugate(f)
Constructs the convex conjugate of the function `f`. The convex conjugate of a function `f` is defined as
`f*(y) = sup_x {<x, y> - f(x)}`.
"""
struct ConvexConjugate{T}
f::T
end
is_convex(::Type{<:ConvexConjugate}) = true
is_generalized_quadratic(::Type{ConvexConjugate{T}}) where T = is_generalized_quadratic(T)
function prox_conjugate!(y, u, f, x, gamma)
u .= x ./ gamma
v = prox!(y, f, u, 1 / gamma)
v = real(dot(x, y)) - gamma * real(dot(y, y)) - v
y .= x .- gamma .* y
return v
end
prox_conjugate!(y, f, x, gamma) = prox_conjugate!(y, similar(x), f, x, gamma)
prox!(y, g::ConvexConjugate, x, gamma) = prox_conjugate!(y, g.f, x, gamma)
convex_conjugate(f) = ConvexConjugate(f)
convex_conjugate(::Zero) = IndZero()
convex_conjugate(::IndZero) = Zero()