[d8e26d]: / SLEP_package_4.1 / SLEP / CFiles / q1 / ep1R.c

Download this file

69 lines (48 with data), 1.3 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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <mex.h>
#include <math.h>
#include "matrix.h"
/*
Euclidean Projection onto l_{2,1} Ball
min 1/2 ||x- u||_2^2 + 1/2 ||t- v||_2^2
s.t. |x|<=t
Usage:
[x, t]=ep1R(u, v, n);
*/
void ep1R(double * x, double *t, double * u, double * v, int n)
{
int j;
for(j=0;j<n;j++){
if(fabs(u[j]) > fabs(v[j])){
t[j]=(fabs(u[j]) + v[j])/2;
if (u[j] >0)
x[j]=t[j];
else
x[j]=-t[j];
}
else
if(fabs(u[j]) <= v[j]){
t[j]=v[j];
x[j]=u[j];
}
else{
t[j]=x[j]=0;
}
}
}
void mexFunction (int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
/*set up input arguments */
double* u= mxGetPr(prhs[0]);
double* v= mxGetPr(prhs[1]);
int n= (int) mxGetScalar(prhs[2]);
double *x, *t;
/* set up output arguments */
plhs[0] = mxCreateDoubleMatrix(n,1,mxREAL);
plhs[1] = mxCreateDoubleMatrix(n,1,mxREAL);
x=mxGetPr(plhs[0]);
t=mxGetPr(plhs[1]);
ep1R(x, t, u, v, n);
}