-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxSortedL1.c
More file actions
89 lines (80 loc) · 2.53 KB
/
proxSortedL1.c
File metadata and controls
89 lines (80 loc) · 2.53 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
/*
* Copyright 2013, M. Bogdan, E. van den Berg, W. Su, and E.J. Candes
*
* This file is part of SLOPE Toolbox version 1.0.
*
* The SLOPE Toolbox is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* The SLOPE Toolbox is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the SLOPE Toolbox. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include "proxSortedL1.h"
/* ----------------------------------------------------------------------- */
int evaluateProx(double *y, double *lambda, double *x, size_t n, int *order)
/* ----------------------------------------------------------------------- */
{ double d;
double *s = NULL;
double *w = NULL;
size_t *idx_i = NULL;
size_t *idx_j = NULL;
size_t i,j,k;
int result = 0;
/* Allocate memory */
s = (double *)malloc(sizeof(double) * n);
w = (double *)malloc(sizeof(double) * n);
idx_i = (size_t *)malloc(sizeof(size_t) * n);
idx_j = (size_t *)malloc(sizeof(size_t) * n);
if ((s != NULL) && (w != NULL) && (idx_i != NULL) && (idx_j != NULL))
{
k = 0;
for (i = 0; i < n; i++)
{
idx_i[k] = i;
idx_j[k] = i;
s[k] = y[i] - lambda[i];
w[k] = s[k];
while ((k > 0) && (w[k-1] <= w[k]))
{ k --;
idx_j[k] = i;
s[k] += s[k+1];
w[k] = s[k] / (i - idx_i[k] + 1);
}
k++;
}
if (order == NULL)
{ for (j = 0; j < k; j++)
{ d = w[j]; if (d < 0) d = 0;
for (i = idx_i[j]; i <= idx_j[j]; i++)
{ x[i] = d;
}
}
}
else
{ for (j = 0; j < k; j++)
{ d = w[j]; if (d < 0) d = 0;
for (i = idx_i[j]; i <= idx_j[j]; i++)
{ x[order[i]] = d;
}
}
}
}
else
{ result = -1;
}
/* Deallocate memory */
if (s != NULL) free(s);
if (w != NULL) free(w);
if (idx_i != NULL) free(idx_i);
if (idx_j != NULL) free(idx_j);
return result;
}