Xmipp  v3.23.11-Nereus
MultInd.cpp
Go to the documentation of this file.
1 /*
2 
3 CONDOR 1.06 - COnstrained, Non-linear, Direct, parallel Optimization
4  using trust Region method for high-computing load,
5  noisy functions
6 Copyright (C) 2004 Frank Vanden Berghen
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation version 2
11 of the License.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 
22 If you want to include this tools in any commercial product,
23 you can contact the author at fvandenb@iridia.ulb.ac.be
24 
25 */
26 
27 //
28 // Multiindex
29 //
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <memory.h>
33 
34 #define __INSIDE_MULTIND_CPP__
35 #include "MultInd.h"
36 #undef __INSIDE_MULTIND_CPP__
37 
38 #include "tools.h"
39 
40 unsigned MultInd::maxDim;
41 unsigned *MultInd::buffer;
43 
45 {
46  dim=p.dim; deg=p.deg; next=NULL;
47  lastChangesV=p.lastChangesV; indexesOfCoefInLexOrderV=p.indexesOfCoefInLexOrderV;
48  indexV=p.indexV;
49  standardInit();
50  if (deg==0) memcpy(coeffDeg,p.coeffDeg,dim*sizeof(unsigned));
51  return *this;
52 }
53 
54 void MultInd::standardInit()
55 {
56  if (deg==0)
57  {
58  coeffDeg=(unsigned*)malloc(dim*sizeof(unsigned));
59  coeffLex=NULL;
60  } else
61  {
62  coeffDeg=buffer;
63  coeffLex=buffer+dim;
64  };
65 }
66 
67 MultInd::MultInd( unsigned _dim, unsigned _deg):
68  dim(_dim), deg(_deg), next(NULL)
69 {
70  standardInit();
71  fullInit();
72  resetCounter();
73 }
74 
75 MultInd::MultInd(unsigned d): dim(d), deg(0), next(NULL)
76 {
77  standardInit();
78  resetCounter();
79 }
80 
82 {
83  if (deg==0) free(coeffDeg);
84 }
85 
87 {
88  printf("[");
89  if (!dim) { printf("]"); return; }
90 
91  unsigned N=dim,*up=coeffDeg;
92  while (--N) printf("%i,",*(up++));
93  printf("%i]",*up);
94 }
95 
96 unsigned MultInd::len()
97 {
98  unsigned l=0, *ccDeg=coeffDeg, j=dim;
99  while (j--) l+=*(ccDeg++);
100  return l;
101 }
102 
104 {
105  unsigned *p1=(*this), *p2=m, n=dim;
106  while (n--)
107  if (*(p1++)!=*(p2++)) return false;
108  return true;
109 }
110 
111 void MultInd::fullInit()
112 {
113  unsigned *ccLex, *ccDeg, degree=deg, n=choose(dim+deg,dim),i,k,sum, d=dim-1;
114  int j;
115 
116  lastChangesV.setSize(n-1);
117  indexesOfCoefInLexOrderV.setSize(n);
118 
119  memset(coeffLex+1,0,d*sizeof(int));
120  *coeffLex=deg;
121 
122  for (i=0; i<n; i++)
123  {
124  sum=0; ccLex=coeffLex; j=dim;
125  while (j--) sum+=*(ccLex++);
126  if (sum) k=choose( sum+d, dim ); else k=0;
127 
128  resetCounter();
129  *coeffDeg=sum;
130 
131  while(1)
132  {
133  ccLex=coeffLex; ccDeg=coeffDeg;
134  for ( j=d; j>0 ; j--, ccLex++, ccDeg++ ) if (*ccLex != *ccDeg) break;
135  if (*ccLex >= *ccDeg) break;
136  ++(*this); k++;
137  }
138 
139  indexesOfCoefInLexOrderV[i]=k;
140 
141  if (i==n-1) break;
142 
143  // lexical order ++ :
144  if (coeffLex[d])
145  {
146  lastChangesV[i]=d;
147  coeffLex[d]--;
148  } else
149  for (j=d-1; j>=0; j--)
150  {
151  if (coeffLex[j])
152  {
153  lastChangesV[i]=j;
154  sum=--coeffLex[j];
155  for (k=0; k<(unsigned)j; k++) sum+=coeffLex[k];
156  coeffLex[++j]=degree-sum;
157  for (k=j+1; k<=d; k++) coeffLex[k]=0;
158  break;
159  }
160  }
161  }
162 }
163 
165 {
166  indexV=0;
167  memset(coeffDeg,0,dim*sizeof(unsigned));
168 }
169 
171 {
172  unsigned *cc = coeffDeg;
173  int n=dim, pos, i;
174 
175  if (!n || !cc) return *this;
176 
177  for (pos = n-2; pos >= 0; pos--)
178  {
179  if (cc[pos]) // Gotcha
180  {
181  cc[pos]--;
182  cc[++pos]++;
183  for (i = pos+1; i < n;i++)
184  {
185  cc[pos] += cc[i];
186  cc[i] = 0;
187  }
188  indexV++;
189  return *this;
190  }
191  }
192 
193  (*cc)++;
194  for ( i = 1; i < n; i++)
195  {
196  *cc += cc[i];
197  cc[i] = 0;
198  }
199 
200  indexV++;
201  return *this;
202 }
203 
205 {
206  if (deg==0)
207  {
208  printf("use MultIndCache to instanciate MultInd");
209  getchar(); exit(252);
210  }
211  return (unsigned*)lastChangesV.d->p;
212 }
213 
215 {
216  if (deg==0)
217  {
218  printf("use MultIndCache to instanciate MultInd");
219  getchar(); exit(252);
220  }
221  return (unsigned*)indexesOfCoefInLexOrderV.d->p;
222 }
223 
225 {
226  MultInd::maxDim=100;
227  MultInd::buffer=(unsigned*)malloc(MultInd::maxDim*2*sizeof(unsigned));
228 }
229 
231 {
232  MultInd *d=head, *d1;
233  while (d)
234  {
235  d1=d->next;
236  delete d;
237  d=d1;
238  }
239  free(MultInd::buffer);
240 }
241 
242 MultInd *MultIndCache::get(unsigned _dim, unsigned _deg )
243 {
244  if (_deg==0)
245  {
246  printf("use normal constructor of MultiInd");
247  getchar(); exit(252);
248  }
249  if (_dim>MultInd::maxDim)
250  {
251  free(MultInd::buffer);
252  MultInd::maxDim=_dim;
253  MultInd::buffer=(unsigned*)malloc(_dim*2*sizeof(unsigned));
254  }
255  MultInd *d=head;
256  while (d)
257  {
258  if ((_dim==d->dim)&&(_deg==d->deg)) return d;
259  d=d->next;
260  }
261 
262  d=new MultInd(_dim,_deg);
263  d->next=head;
264  head=d;
265  return d;
266 }
unsigned long choose(unsigned n, unsigned k)
Definition: tools.cpp:38
MultIndCache cacheMultInd
Definition: MultInd.cpp:42
unsigned deg
Definition: MultInd.h:53
unsigned * lastChanges()
Definition: MultInd.cpp:204
MultInd * get(unsigned _dim, unsigned _deg)
Definition: MultInd.cpp:242
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
void setSize(int _n)
Definition: VectorInt.cpp:126
doublereal * d
bool operator==(const MultInd &m)
Definition: MultInd.cpp:103
MultInd & operator++()
Definition: MultInd.cpp:170
free((char *) ob)
unsigned len()
Definition: MultInd.cpp:96
~MultInd()
Definition: MultInd.cpp:81
VectorIntData * d
Definition: VectorInt.h:40
#define j
int m
unsigned * indexesOfCoefInLexOrder()
Definition: MultInd.cpp:214
MultInd & operator=(const MultInd &P)
Definition: MultInd.cpp:44
void resetCounter()
Definition: MultInd.cpp:164
unsigned dim
Definition: MultInd.h:53
void print()
Definition: MultInd.cpp:86
int * n
MultInd(unsigned d=0)
Definition: MultInd.cpp:75