-
Notifications
You must be signed in to change notification settings - Fork 24
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
Gradients2 #44
Gradients2 #44
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,3 +75,9 @@ Maximum | |
Quadratic | ||
SumPositive | ||
``` | ||
|
||
## Distances | ||
```@docs | ||
DistL2 | ||
SqrDistL2 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ is_convex(f::IndSOC) = true | |
is_set(f::IndSOC) = true | ||
|
||
function prox!{T <: Real}(y::AbstractArray{T,1}, f::IndSOC, x::AbstractArray{T,1}, gamma::T=one(T)) | ||
nx = norm(x[2:end]) | ||
@views nx = norm(x[2:end]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, I didn’t know this! |
||
t = x[1] | ||
if t <= -nx | ||
y[:] = 0.0 | ||
|
@@ -36,7 +36,7 @@ function prox!{T <: Real}(y::AbstractArray{T,1}, f::IndSOC, x::AbstractArray{T,1 | |
else | ||
r = 0.5 * (1 + t / nx) | ||
y[1] = r * nx | ||
y[2:end] = r * x[2:end] | ||
@views y[2:end] .= r .* x[2:end] | ||
end | ||
return 0.0 | ||
end | ||
|
@@ -57,7 +57,7 @@ function prox_naive{T <: Real}(f::IndSOC, x::AbstractArray{T,1}, gamma=1.0) | |
y = zeros(x) | ||
r = 0.5 * (1 + t / nx) | ||
y[1] = r * nx | ||
y[2:end] = r * x[2:end] | ||
y[2:end] .= r .* x[2:end] | ||
end | ||
return y, 0.0 | ||
end | ||
|
@@ -95,19 +95,19 @@ function prox!{T <: Real}(y::AbstractArray{T,1}, f::IndRotatedSOC, x::AbstractAr | |
x1 = 0.7071067811865475*x[1] + 0.7071067811865475*x[2] | ||
x2 = 0.7071067811865475*x[1] - 0.7071067811865475*x[2] | ||
# project rotated x onto SOC | ||
nx = sqrt(x2^2+norm(x[3:end])^2) | ||
@views nx = sqrt(x2^2+norm(x[3:end])^2) | ||
t = x1 | ||
if t <= -nx | ||
y[:] = 0.0 | ||
elseif t >= nx | ||
y[1] = x1 | ||
y[2] = x2 | ||
y[3:end] = x[3:end] | ||
@views y[3:end] .= x[3:end] | ||
else | ||
r = 0.5 * (1 + t / nx) | ||
y[1] = r * nx | ||
y[2] = r * x2 | ||
y[3:end] = r * x[3:end] | ||
@views y[3:end] = r .* x[3:end] | ||
end | ||
# rotate back y cw by pi/4 | ||
y1 = 0.7071067811865475*y[1] + 0.7071067811865475*y[2] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ end | |
|
||
Linear(c::A) where {R <: RealOrComplex, A <: AbstractArray{R}} = Linear{R, A}(c) | ||
|
||
is_separable(f::Linear) = true | ||
is_convex(f::Linear) = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add |
||
|
||
function (f::Linear{RC, A})(x::AbstractArray{RC}) where {R <: Real, RC <: Union{R, Complex{R}}, A <: AbstractArray{RC}} | ||
return vecdot(f.c, x) | ||
end | ||
|
@@ -28,7 +31,6 @@ function prox!(y::AbstractArray{RC}, f::Linear{RC, A}, x::AbstractArray{RC}, gam | |
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not changed in PR, but should be |
||
|
||
function gradient!(y::AbstractArray{RC}, f::Linear{RC, A}, x::AbstractArray{RC}) where {R <: Real, RC <: Union{R, Complex{R}}, A <: AbstractArray{RC}} | ||
A_mul_B!(y, f.Q, x) | ||
y .= f.c | ||
return vecdot(f.c, x) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ If `iterative=true`, then `prox!` is evaluated approximately using an iterative | |
|
||
abstract type Quadratic <: ProximableFunction end | ||
|
||
is_convex(f::Quadratic) = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This I have not previously put because one should really check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any thoughts on this @mfalt ? Of course checking the eigenvalues is not viable, but maybe the user should be able to assert whether the function is convex or not? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No ok, the docstring says we assume There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I added this because of the documentation, but you are right, maybe some keyword from the user, acknowledging if it is positive(semi)-definite would be good. |
||
is_smooth(f::Quadratic) = true | ||
is_quadratic(f::Quadratic) = true | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
Fast (non-allocating) version of vecnorm(x-y,2)^2 | ||
""" | ||
function vecnormdiff2(x,y) | ||
s = 0.0 | ||
for i in eachindex(x) | ||
s += abs2(x[i]-y[i]) | ||
end | ||
return s | ||
end | ||
|
||
""" | ||
Fast (non-allocating) version of vecnorm(x-y,2) | ||
""" | ||
vecnormdiff(x,y) = sqrt(vecnormdiff2(x,y)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This I have also put in the section describing calculus rules, since these are really modifiers of some other (indicator) functions; but I guess they can stay in both places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They were in the FUNCTIONS.md in the root, so I copied them here. But you are right, we should think of how we can unify the different documentations.