Xmipp  v3.23.11-Nereus
tools.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 // Various tools and auxiliary functions
28 //
29 #include <stdlib.h>
30 #include <iostream>
31 #include <time.h>
32 #include <string.h>
33 #include "tools.h"
34 #ifndef WIN32
35 #include <unistd.h>
36 #endif
37 
38 unsigned long choose( unsigned n, unsigned k )
39 {
40  const unsigned long uupSize = 100;
41  static unsigned long uup[uupSize];
42  unsigned long *up;
43  static unsigned long Nold = 0;
44  static unsigned long Kold = 0;
45  unsigned long l,m;
46  unsigned i,j;
47 
48  if ( (n < k) || !n ) return 0;
49 
50  if ( (n == k) || !k ) // includes n == 1
51  return 1;
52 
53  if ( k > (n >> 1) ) // Only lower half
54  k = n-k;
55 
56  if ( (Nold == n) && (k < Kold) ) // We did it last time ...
57  return *(uup + k - 1);
58 
59  if ( k > uupSize )
60  {
61  printf( "choose( unsigned, unsigned) : overflow\n");
62  getchar(); exit(-1);
63  }
64 
65  Nold=n; Kold=k;
66 
67  *(up=uup)=2;
68  for (i=2; i<n; i++) // Pascal's triangle
69  {
70  // todo: remove next line:
71  *(up+1)=1;
72  l=1;
73  m=*(up=uup);
74  for (j=0; j<mmin(i,k); j++)
75  {
76  *up=m+l;
77  l=m;
78  m=*(++up);
79  }
80  // todo: remove next line:
81  *up=1;
82  }
83 
84  return *(uup + k - 1);
85 }
86 
87 unsigned long mysrand;
88 double rand1()
89 {
90  mysrand=1664525*mysrand+1013904223L;
91  double r=((double)mysrand)/4294967297.0;
92  //if (r>.52)
93  //{
94  // printf("whoups\n");
95  //}
96  return r;
97 
98 }
99 
100 
101 void initRandom(int i)
102 {
103  if (i) { mysrand=i; return; }
104  mysrand=(unsigned long) (clock());
105 }
106 
107 void error(char *s)
108 {
109  printf("Error due to %s.", s);
110  getchar(); exit(255);
111 }
112 
113 double euclidianNorm(int i, double *xp)
114 {
115 // no tested
116 // same code for the Vector eucilidian norm and for the Matrix Froebenis norm
117 /*
118  double sum=0;
119  while (i--) sum+=sqr(*(xp++));
120  return sqrt(sum);
121 */
122  const double SMALL=5.422e-20, BIG=1.304e19/((double)i);
123  double s1=0,s2=0,s3=0, x1max=0, x3max=0, xabs;
124 
125  while (i--)
126  {
127  xabs=condorAbs(*(xp++));
128 
129  if (xabs>BIG)
130  {
131  if (xabs>x1max)
132  {
133  s1=1.0+s1*sqr(x1max/xabs);
134  x1max=xabs;
135  continue;
136  }
137  s1+=sqr(xabs/x1max);
138  continue;
139  }
140  if (xabs<SMALL)
141  {
142  if (xabs>x3max)
143  {
144  s3=1.0+s3*sqr(x3max/xabs);
145  x3max=xabs;
146  continue;
147  }
148  if (xabs!=0) s3+=sqr(xabs/x3max);
149  continue;
150  }
151  s2+=sqr(xabs);
152  };
153  if (s1!=0) return x1max*sqrt(s1+(s2/x1max)/x1max);
154  if (s2!=0)
155  {
156  if (s2>=x3max) return sqrt(s2*(1.0+(x3max/s2)*(x3max*s3)));
157  return sqrt(x3max*((s2/x3max)+(x3max*s3)));
158  }
159  return x3max*sqrt(s3);
160 }
161 
162 #define EOL1 13
163 #define EOL2 10
164 
165 char isEmpty(const char *line)
166 {
167  line=skipSpaces(line);
168  return (*line==0);
169 }
170 
171 const char *skipSpaces(const char *t)
172 {
173  while ((*t==' ')||(*t=='\t')) t++;
174  return t;
175 }
176 
177 char isEmptyline(const char *line)
178 {
179  if (*line==';') return 1;
180  line=skipSpaces(line);
181  if ((*line==EOL1)||(*line==EOL2)||(*line=='\0')) return 1;
182  return 0;
183 }
184 
185 char *GetRidOfTheEOL(char *tline)
186 {
187  char *t;
188  t=tline=(char*)skipSpaces(tline);
189  while ((*tline!=EOL1)&&(*tline!=EOL2)&&(*tline)) tline++;
190  *tline='\0';
191  return t;
192 }
193 
194 char *removeQuotes(char *t)
195 {
196  if ((*t=='\'')||(*t=='"'))
197  {
198  t[strlen(t)-1]=0;
199  return t+1;
200  }
201  return t;
202 }
203 
204 char **getNameTable(const char *line, int *ncolumn)
205 {
206  char *n=(char*)line;
207  int j=0,nc;
208  if (ncolumn) nc=*ncolumn;
209  if (nc==0)
210  {
211  while (*n)
212  {
213  while ((*n)&&(*n!='\t')&&(*n!=' ')&&(*n!=13)&&(*n!=10)) n++;
214  nc++;
215  while ((*n)&&((*n=='\t')||(*n==' ')||(*n==13)||(*n==10))) n++;
216  }
217  }
218  if (ncolumn) *ncolumn=nc;
219  char **names=(char**)malloc(nc*sizeof(char**));
220  n=(char*)malloc(strlen(line)+1);
221  strcpy(n,line);
222 
223  for (j=0; j<nc-1; j++)
224  {
225  names[j]=n;
226  while ((*n)&&(*n!='\t')&&(*n!=' ')&&(*n!=13)&&(*n!=10)) n++;
227  if (*n) { *n=0; n++; }
228  while ((*n)&&((*n=='\t')||(*n==' ')||(*n==13)||(*n==10))) n++;
229  }
230  names[j]=n;
231  return names;
232 }
233 
234 char *stringDuplicate(const char *line)
235 {
236  int l=(int)strlen(line);
237  // remove quotes:
238  if ((*line=='\'')||(*line=='"')) { l-=2; line++; }
239  char *t=(char*)malloc(l+1);
240  memcpy(t,line,l);
241  t[l]=0;
242  GetRidOfTheEOL(t);
243  return t;
244 }
245 
246 char *removeAllEOL(char *t)
247 {
248  char *t2=t;
249  while (*t)
250  {
251  if ((*t=='\r')||(*t=='\n')) *t=' ';
252  t++;
253  }
254  return t2;
255 }
256 
257 const char *skipLine(const char *t)
258 {
259  while ((*t!='\r')&&(*t!='\n')&&(*t)) t++;
260  if (*t=='\n') { if (*(t+1)=='\r') t++; }
261  else if (*t=='\r') { if (*(t+1)=='\n') t++; }
262  return t+1;
263 }
264 
265 char *loadFile(FILE *f)
266 {
267 // FILE *f=fopen(filename,"rb");
268  fseek(f,0,SEEK_END);
269  int l=ftell(f);
270  fseek(f,0,SEEK_SET);
271  char *buf=(char*)malloc(l+1);
272  if (fread(buf,l,1,f) != l) {
273  std::cerr << "error while loading file (no more info available)" << std::endl; exit(255); // FIXME implement properly
274  }
275  fclose(f);
276  buf[l]=0;
277  return buf;
278 }
279 
280 void deleteFile(const char *line)
281 {
282 #ifdef WIN32
283  remove(line);
284 #else
285  unlink(line);
286 #endif
287 }
288 
char * GetRidOfTheEOL(char *tline)
Definition: tools.cpp:185
void sqrt(Image< double > &op)
double mmin(const double t1, const double t2)
Definition: tools.h:69
void initRandom(int i)
Definition: tools.cpp:101
#define EOL1
Definition: tools.cpp:162
#define EOL2
Definition: tools.cpp:163
#define i
void deleteFile(const char *line)
Definition: tools.cpp:280
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
char * removeQuotes(char *t)
Definition: tools.cpp:194
char isEmptyline(const char *line)
Definition: tools.cpp:177
double sqr(const double &t)
Definition: tools.h:99
char * loadFile(FILE *f)
Definition: tools.cpp:265
double euclidianNorm(int i, double *xp)
Definition: tools.cpp:113
double * f
double condorAbs(const double t1)
Definition: tools.h:47
const char * skipLine(const char *t)
Definition: tools.cpp:257
double rand1()
Definition: tools.cpp:88
#define j
int m
void error(char *s)
Definition: tools.cpp:107
char isEmpty(const char *line)
Definition: tools.cpp:165
unsigned long choose(unsigned n, unsigned k)
Definition: tools.cpp:38
unsigned long mysrand
Definition: tools.cpp:87
const char * skipSpaces(const char *t)
Definition: tools.cpp:171
int * n
char ** getNameTable(const char *line, int *ncolumn)
Definition: tools.cpp:204
char * stringDuplicate(const char *line)
Definition: tools.cpp:234
char * removeAllEOL(char *t)
Definition: tools.cpp:246