24 void altra(
double *x,
double *v,
int n,
double *ind,
int nodes,
double mult)
27 double lambda,twoNorm, ratio;
32 if ((
int) ind[0]==-1){
37 if ((
int) ind[1]!=-1){
38 printf(
"\n Error! \n Check ind");
57 memcpy(x, v,
sizeof(
double) * n);
70 for(j=(
int) ind[3*i]-1;j< (int) ind[3*i+1];j++)
71 twoNorm += x[j] * x[j];
72 twoNorm=sqrt(twoNorm);
74 lambda=mult*ind[3*i+2];
76 ratio=(twoNorm-lambda)/twoNorm;
81 for(j=(
int) ind[3*i]-1;j<(int) ind[3*i+1];j++)
88 for(j=(
int) ind[3*i]-1;j<(int) ind[3*i+1];j++)
94 void altra_mt(
double *X,
double *V,
int n,
int k,
double *ind,
int nodes,
double mult)
98 double *x=(
double *)malloc(
sizeof(
double)*k);
99 double *v=(
double *)malloc(
sizeof(
double)*k);
109 altra(x, v, k, ind, nodes, mult);
122 void computeLambda2Max(
double *lambda2_max,
double *x,
int n,
double *ind,
int nodes)
129 for(i=0;i < nodes; i++){
134 for(j=(
int) ind[3*i]-1;j< (int) ind[3*i+1];j++)
135 twoNorm += x[j] * x[j];
136 twoNorm=sqrt(twoNorm);
138 twoNorm=twoNorm/ind[3*i+2];
140 if (twoNorm >*lambda2_max )
141 *lambda2_max=twoNorm;
145 double treeNorm(
double *x,
int ldx,
int n,
double *ind,
int nodes){
148 double twoNorm, lambda;
150 double tree_norm = 0;
155 if ((
int) ind[0]==-1){
160 if ((
int) ind[1]!=-1){
161 printf(
"\n Error! \n Check ind");
167 for(j=0;j<n*ldx;j+=ldx){
168 tree_norm+=fabs(x[j]);
171 tree_norm = tree_norm * lambda;
183 for(;i < nodes; i++){
189 int n_in_node = (int) ind[3*i+1] - (
int) ind[3*i]-1;
190 for(j=(
int) ind[3*i]-1;j< (int) ind[3*i]-1 + n_in_node*ldx;j+=ldx)
191 twoNorm += x[j] * x[j];
192 twoNorm=sqrt(twoNorm);
196 tree_norm = tree_norm + lambda*twoNorm;
202 double findLambdaMax(
double *v,
int n,
double *ind,
int nodes){
205 double lambda=0,squaredWeight=0, lambda1,lambda2;
206 double *x=(
double *)malloc(
sizeof(
double)*n);
207 double *ind2=(
double *)malloc(
sizeof(
double)*nodes*3);
214 if ( (
int)ind[0]==-1 )
215 squaredWeight=n*ind[2]*ind[2];
217 squaredWeight=ind[2]*ind[2];
219 for (i=1;i<nodes;i++){
220 squaredWeight+=ind[3*i+2]*ind[3*i+2];
225 lambda=sqrt(lambda/squaredWeight);
235 for(i=0;i<nodes;i++){
237 ind2[3*i+1]=ind[3*i+1];
238 ind2[3*i+2]=ind[3*i+2]*lambda;
243 altra(x, v, n, ind2, nodes);
263 for(i=0;i<nodes;i++){
264 ind2[3*i+2]=ind[3*i+2]*lambda1;
269 altra(x, v, n, ind2, nodes);
297 for(i=0;i<nodes;i++){
298 ind2[3*i+2]=ind[3*i+2]*lambda2;
303 altra(x, v, n, ind2, nodes);
322 while ( fabs(lambda2-lambda1) > lambda2 * 1e-10 ){
326 lambda=(lambda1+lambda2)/2;
330 for(i=0;i<nodes;i++){
331 ind2[3*i+2]=ind[3*i+2]*lambda;
336 altra(x, v, n, ind2, nodes);
367 double findLambdaMax_mt(
double *V,
int n,
int k,
double *ind,
int nodes)
371 double *v=(
double *)malloc(
sizeof(
double)*k);
384 lambda = findLambdaMax(v, k, ind, nodes);
390 if (lambda>lambdaMax)
402 #endif //USE_GPL_SHOGUN