Xmipp  v3.23.11-Nereus
Macros | Functions
tools.h File Reference
#include <math.h>
#include <iosfwd>
#include "Vector.h"
#include "Matrix.h"
Include dependency graph for tools.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define maxDWORD   4294967295
 
#define INF   1.7E+308
 
#define EOL   10
 
#define PI   3.1415926535897932384626433832795
 
#define ROOT2   1.41421356
 

Functions

double condorAbs (const double t1)
 
double sign (const double a)
 
double sign (const double t1, const double t2)
 
double isInt (const double a)
 
double mmin (const double t1, const double t2)
 
unsigned mmin (const unsigned t1, const unsigned t2)
 
int mmin (const int t1, const int t2)
 
double mmax (const double t1, const double t2)
 
unsigned mmax (const unsigned t1, const unsigned t2)
 
int mmax (int t1, int t2)
 
double sqr (const double &t)
 
double CondorRound (double a)
 
unsigned long choose (unsigned n, unsigned k)
 
double rand1 ()
 
void initRandom (int i=0)
 
double euclidianNorm (int i, double *xp)
 
char emptyline (const char *line)
 
char * GetRidOfTheEOL (char *tline)
 
char * removeQuotes (char *t)
 
char ** getNameTable (const char *line, int *ncolumn)
 
char * stringDuplicate (const char *l)
 
char isEmpty (const char *line)
 
char * removeAllEOL (char *t)
 
char * loadFile (FILE *f)
 
const char * skipLine (const char *l)
 
const char * skipSpaces (const char *l)
 
void deleteFile (const char *l)
 

Macro Definition Documentation

◆ EOL

#define EOL   10

Definition at line 41 of file tools.h.

◆ INF

#define INF   1.7E+308

Definition at line 40 of file tools.h.

◆ maxDWORD

#define maxDWORD   4294967295

Definition at line 39 of file tools.h.

◆ PI

#define PI   3.1415926535897932384626433832795

Definition at line 43 of file tools.h.

◆ ROOT2

#define ROOT2   1.41421356

Definition at line 45 of file tools.h.

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

◆ condorAbs()

double condorAbs ( const double  t1)
inline

Definition at line 47 of file tools.h.

48 {
49  return t1 > 0.0 ? t1 : -t1;
50 }

◆ CondorRound()

double CondorRound ( double  a)
inline

Definition at line 104 of file tools.h.

105 {
106  return (int)(a+.5);
107 }
doublereal * a

◆ deleteFile()

void deleteFile ( const char *  l)

Definition at line 280 of file tools.cpp.

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

◆ emptyline()

char emptyline ( const char *  line)

◆ 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 = 0)

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

◆ isInt()

double isInt ( const double  a)
inline

Definition at line 64 of file tools.h.

65 {
66  return condorAbs(a-floor( a + 0.5 ))<1e-4;
67 }
__host__ __device__ float2 floor(const float2 v)
double condorAbs(const double t1)
Definition: tools.h:47
doublereal * a

◆ 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

◆ mmax() [1/3]

double mmax ( const double  t1,
const double  t2 
)
inline

Definition at line 84 of file tools.h.

85 {
86  return t1 > t2 ? t1 : t2;
87 }

◆ mmax() [2/3]

unsigned mmax ( const unsigned  t1,
const unsigned  t2 
)
inline

Definition at line 89 of file tools.h.

90 {
91  return t1 > t2 ? t1 : t2;
92 }

◆ mmax() [3/3]

int mmax ( int  t1,
int  t2 
)
inline

Definition at line 94 of file tools.h.

95 {
96  return t1 > t2 ? t1 : t2;
97 }

◆ mmin() [1/3]

double mmin ( const double  t1,
const double  t2 
)
inline

Definition at line 69 of file tools.h.

70 {
71  return t1 < t2 ? t1 : t2;
72 }

◆ mmin() [2/3]

unsigned mmin ( const unsigned  t1,
const unsigned  t2 
)
inline

Definition at line 74 of file tools.h.

75 {
76  return t1 < t2 ? t1 : t2;
77 }

◆ mmin() [3/3]

int mmin ( const int  t1,
const int  t2 
)
inline

Definition at line 79 of file tools.h.

80 {
81  return t1 < t2 ? t1 : t2;
82 }

◆ 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 }

◆ sign() [1/2]

double sign ( const double  a)
inline

Definition at line 52 of file tools.h.

54 {
55  return a<0?-1:1;
56 }
doublereal * a

◆ sign() [2/2]

double sign ( const double  t1,
const double  t2 
)
inline

Definition at line 58 of file tools.h.

59 {
60  if(t2>=0) return condorAbs(t1);
61  return -condorAbs(t1);
62 }
double condorAbs(const double t1)
Definition: tools.h:47

◆ skipLine()

const char* skipLine ( const char *  l)

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 *  l)

Definition at line 171 of file tools.cpp.

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

◆ sqr()

double sqr ( const double &  t)
inline

Definition at line 99 of file tools.h.

100 {
101  return t*t;
102 }

◆ stringDuplicate()

char* stringDuplicate ( const char *  l)

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