Xmipp  v3.23.11-Nereus
Macros | Functions | Variables
tools.cpp File Reference
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <string.h>
#include "tools.h"
#include <unistd.h>
Include dependency graph for tools.cpp:

Go to the source code of this file.

Macros

#define EOL1   13
 
#define EOL2   10
 

Functions

unsigned long choose (unsigned n, unsigned k)
 
double rand1 ()
 
void initRandom (int i)
 
void error (char *s)
 
double euclidianNorm (int i, double *xp)
 
char isEmpty (const char *line)
 
const char * skipSpaces (const char *t)
 
char isEmptyline (const char *line)
 
char * GetRidOfTheEOL (char *tline)
 
char * removeQuotes (char *t)
 
char ** getNameTable (const char *line, int *ncolumn)
 
char * stringDuplicate (const char *line)
 
char * removeAllEOL (char *t)
 
const char * skipLine (const char *t)
 
char * loadFile (FILE *f)
 
void deleteFile (const char *line)
 

Variables

unsigned long mysrand
 

Macro Definition Documentation

◆ EOL1

#define EOL1   13

Definition at line 162 of file tools.cpp.

◆ EOL2

#define EOL2   10

Definition at line 163 of file tools.cpp.

Function Documentation

◆ choose()

unsigned long choose ( unsigned  n,
unsigned  k 
)

Definition at line 38 of file tools.cpp.

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 }
double mmin(const double t1, const double t2)
Definition: tools.h:69
#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
#define j
int m
int * n

◆ deleteFile()

void deleteFile ( const char *  line)

Definition at line 280 of file tools.cpp.

281 {
282 #ifdef WIN32
283  remove(line);
284 #else
285  unlink(line);
286 #endif
287 }

◆ error()

void error ( char *  s)

Definition at line 107 of file tools.cpp.

108 {
109  printf("Error due to %s.", s);
110  getchar(); exit(255);
111 }

◆ euclidianNorm()

double euclidianNorm ( int  i,
double *  xp 
)

Definition at line 113 of file tools.cpp.

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 }
void sqrt(Image< double > &op)
#define i
double sqr(const double &t)
Definition: tools.h:99
double condorAbs(const double t1)
Definition: tools.h:47

◆ getNameTable()

char** getNameTable ( const char *  line,
int *  ncolumn 
)

Definition at line 204 of file tools.cpp.

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 }
#define j
int * n

◆ GetRidOfTheEOL()

char* GetRidOfTheEOL ( char *  tline)

Definition at line 185 of file tools.cpp.

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 }
#define EOL1
Definition: tools.cpp:162
#define EOL2
Definition: tools.cpp:163
const char * skipSpaces(const char *t)
Definition: tools.cpp:171

◆ initRandom()

void initRandom ( int  i)

Definition at line 101 of file tools.cpp.

102 {
103  if (i) { mysrand=i; return; }
104  mysrand=(unsigned long) (clock());
105 }
#define i
unsigned long mysrand
Definition: tools.cpp:87

◆ isEmpty()

char isEmpty ( const char *  line)

Definition at line 165 of file tools.cpp.

166 {
167  line=skipSpaces(line);
168  return (*line==0);
169 }
const char * skipSpaces(const char *t)
Definition: tools.cpp:171

◆ isEmptyline()

char isEmptyline ( const char *  line)

Definition at line 177 of file tools.cpp.

178 {
179  if (*line==';') return 1;
180  line=skipSpaces(line);
181  if ((*line==EOL1)||(*line==EOL2)||(*line=='\0')) return 1;
182  return 0;
183 }
#define EOL1
Definition: tools.cpp:162
#define EOL2
Definition: tools.cpp:163
const char * skipSpaces(const char *t)
Definition: tools.cpp:171

◆ loadFile()

char* loadFile ( FILE *  f)

Definition at line 265 of file tools.cpp.

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 }
double * f

◆ rand1()

double rand1 ( )

Definition at line 88 of file tools.cpp.

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 }
unsigned long mysrand
Definition: tools.cpp:87

◆ removeAllEOL()

char* removeAllEOL ( char *  t)

Definition at line 246 of file tools.cpp.

247 {
248  char *t2=t;
249  while (*t)
250  {
251  if ((*t=='\r')||(*t=='\n')) *t=' ';
252  t++;
253  }
254  return t2;
255 }

◆ removeQuotes()

char* removeQuotes ( char *  t)

Definition at line 194 of file tools.cpp.

195 {
196  if ((*t=='\'')||(*t=='"'))
197  {
198  t[strlen(t)-1]=0;
199  return t+1;
200  }
201  return t;
202 }

◆ skipLine()

const char* skipLine ( const char *  t)

Definition at line 257 of file tools.cpp.

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 }

◆ skipSpaces()

const char* skipSpaces ( const char *  t)

Definition at line 171 of file tools.cpp.

172 {
173  while ((*t==' ')||(*t=='\t')) t++;
174  return t;
175 }

◆ stringDuplicate()

char* stringDuplicate ( const char *  line)

Definition at line 234 of file tools.cpp.

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 }
char * GetRidOfTheEOL(char *tline)
Definition: tools.cpp:185

Variable Documentation

◆ mysrand

unsigned long mysrand

Definition at line 87 of file tools.cpp.