Xmipp  v3.23.11-Nereus
Vector.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 #include <iostream>
27 #include <fstream>
28 #include <memory.h>
29 #include <string.h> // for memmove: microsoft bug
30 #include "Vector.h"
31 #include "Matrix.h"
32 #include "tools.h"
33 
35 
36 void Vector::alloc(int _n, int _ext)
37 {
38  d=new VectorData();
39  d->n=_n;
40  d->extention=_ext;
41  d->ref_count=1;
42 
43  if (_ext==0) { d->p=NULL; return; };
44 
45  d->p=(double*)malloc(_ext*sizeof(double));
46  if (d->p==NULL) { printf("memory allocation error\n"); getchar(); exit(253); }
47 }
48 
50 {
51  alloc(n,n);
52  zero();
53 }
54 
55 Vector::Vector(int n, int ext)
56 {
57  alloc(n,ext);
58  zero();
59 }
60 
61 Vector::Vector(int n, double *dd, char _exte)
62 {
63  alloc(n,n);
64  if (dd)
65  {
66  if (_exte) { d->externalData=1; d->p=dd; }
67  else memcpy(d->p,dd,n*sizeof(double));
68  }
69  else zero();
70 }
71 
73 {
74  int n=a.sz()+b.sz();
75  alloc(n,n);
76  memcpy(d->p ,a,a.sz()*sizeof(double));
77  memcpy(d->p+a.sz(),b,b.sz()*sizeof(double));
78 }
79 
80 void Vector::setExternalData(int _n, double *dd)
81 {
82  if ((d->extention==_n)||(!d->extention))
83  {
84  d->n=_n; d->extention=_n; d->externalData=1; d->p=dd;
85  } else
86  {
87  printf("do not use this function ('setExternalData'): it's too dangerous.\n");
88  getchar(); exit(255);
89  }
90 }
91 
92 
93 void Vector::zero(int i, int _n)
94 {
95  if (_n==0) _n=d->n-i;
96  if (d->p) memset(d->p+i,0,_n*sizeof(double));
97 }
98 
99 void Vector::prepareExtend(int new_extention)
100 {
101  if (d->extention<new_extention)
102  {
103  d->p=(double*)realloc(d->p,new_extention*sizeof(double));
104  if (d->p==NULL) { printf("memory allocation error\n"); getchar(); exit(253); }
105 
106  // not really necessary (fill with zero's):
107  memset(d->p+d->extention,0,(new_extention-d->extention)*sizeof(double));
108  d->extention=new_extention;
109  };
110 }
111 
112 void Vector::setSize(int _n)
113 {
114  d->n=_n;
115  if (_n==0) { if (d->p) free(d->p); d->p=NULL; d->extention=0; return; }
116  prepareExtend(_n);
117 }
118 
120 {
121  d->n++;
122  if (d->n>d->extention) prepareExtend(d->extention+100);
123 }
124 
126 {
127  if (d->extention!=d->n)
128  {
129  d->p=(double*)realloc(d->p,d->n*sizeof(double));
130  if (d->p==NULL) { printf("memory allocation error\n"); getchar(); exit(253); }
131  d->extention=d->n;
132  };
133 }
134 
136 {
137  if (Q.d==d) return 1;
138  if (Q.d==emptyVector.d)
139  {
140  double *cP=d->p;
141  int i=sz();
142  while (i--) if (*(cP++)) return 0;
143  return 1;
144  }
145 
146  if (sz() != Q.sz()) return 0;
147 
148  double *cP = d->p, *cQ = Q.d->p;
149  int i = sz();
150 
151  while( i-- )
152  {
153  if (*cP!=*cQ) return 0;
154  cP++; cQ++;
155  }
156 
157  return 1;
158 }
159 
160 //ostream& Vector::PrintToStream( ostream& out ) const
162 {
163  int N=sz();
164  printf("[");
165  if (!N || !d->p) { printf("]\n"); return; }
166 
167  double *up=d->p;
168  while (--N) printf("%f,",*(up++));
169  printf("%f]\n",*up);
170 }
171 
173 {
175 }
176 
178 {
179  if (!d) return;
180  (d->ref_count) --;
181  if (d->ref_count==0)
182  {
183  if ((d->p)&&(!d->externalData)) free(d->p);
184  delete d;
185  };
186 }
187 
189 {
190  // shallow copy
191  d=A.d;
192  (d->ref_count)++ ;
193 }
194 
196 {
197  // shallow copy
198  if (this != &A)
199  {
201  d=A.d;
202  (d->ref_count) ++ ;
203  }
204  return *this;
205 }
206 
208 {
209  // a deep copy
210  Vector r(sz());
211  r.copyFrom(*this);
212  return r;
213 }
214 
215 void Vector::copyFrom(Vector r, int _n)
216 {
217  if (_n==0) _n=r.sz();
218  setSize(_n);
219  if (_n) memcpy(d->p,r.d->p,_n*sizeof(double));
220 }
221 
223 {
225 }
226 
228 {
229  if (sz()==0) return 0;
230  double *x=d->p, sum=0;
231  int ni=sz();
232  while (ni--) sum+=condorAbs(*(x++));
233  return sum;
234 }
235 
237 {
238  double *xp=d->p, sum=0;
239  int ni=sz();
240  while (ni--) sum+=sqr(*(xp++));
241  return sum;
242 }
243 
245 {
246  Vector t=(*this)-v;
247  return ::euclidianNorm(sz(), t.d->p);
248 /*
249  double *xp1=d->p, *xp2=v.d->p, sum=0;
250  int ni=sz();
251  while (ni--) sum+=sqr(*(xp1++)-*(xp2++));
252  return sqrt(sum);
253 */
254 }
255 
257 {
258  double *xp1=d->p, *xp2=v.d->p, sum=-1.0;
259  int ni=sz();
260  while (ni--) sum=::mmax(sum, condorAbs(*(xp1++)-*(xp2++)));
261  return sum;
262 }
263 
265 {
266  double *xp1=d->p, sum=-1.0;
267  int ni=sz();
268  while (ni--) sum=::mmax(sum, condorAbs(*(xp1++)));
269  return sum;
270 }
271 
273 {
274  if (sz()==0) return 0;
275  double *xp1=d->p, *xp2=v.d->p, sum=0;
276  int ni=sz();
277  while (ni--) sum+=condorAbs(*(xp1++)-*(xp2++));
278  return sum;
279 }
280 
281 void Vector::multiply(double a)
282 {
283  double *xp=d->p;
284  int ni=sz();
285  while (ni--) *(xp++)*=a;
286 }
287 
288 void Vector::multiply(Vector R, double a)
289 {
290  int ni=sz();
291  R.setSize(ni);
292  double *xs=d->p, *xd=R;
293  while (ni--) *(xd++)=a * (*(xs++));
294 }
295 
296 
298 {
299  if ((int)sz()!=M.nLine())
300  {
301  printf("error in V^t * M.\n"); getchar(); exit(254);
302  }
303  int n=sz(), szr=M.nColumn(), i;
304  vR.setSize(szr);
305  double sum, *dv=(*this), **dm=M, *dd=vR;
306 
307  while (szr--)
308  {
309  sum=0.0;
310  i=n;
311  while (i--) sum+=dv[i]*dm[i][szr];
312  dd[szr]=sum;
313  }
314 }
315 
317 {
318  int i,j,nl=M.nLine(),nc=M.nColumn();
319  if ((int)sz()!=nl)
320  {
321  printf("(matrix_diagonal * matrix) error");
322  getchar(); exit(249);
323  }
324  double **p1=M,*p2=(*this);
325 
326  for (i=0; i<nl; i++)
327  for (j=0; j<nc; j++)
328  p1[i][j]*=p2[i];
329 
330 }
331 
333 {
334  double *xp1=d->p, *xp2=v.d->p, sum=0;
335  int ni=sz();
336  while (ni--) { sum+=*(xp1++) * *(xp2++); };
337  return sum;
338 }
339 
340 double Vector::mmin()
341 {
342  if (sz()==0) return 0;
343  double *xp=d->p, m=INF;
344  int ni=sz();
345  while (ni--) m=::mmin(m,*(xp++));
346  return m;
347 }
348 
349 double Vector::mmax()
350 {
351  if (sz()==0) return 0;
352  double *xp=d->p, m=-INF;
353  int ni=sz();
354  while (ni--) m=::mmax(m,*(xp++));
355  return m;
356 }
357 
359 {
360  double *xp=d->p;
361  int ni=sz();
362  while (ni--) if (*(xp++)!=0) return false;
363  return true;
364 }
365 
367 {
368  int ni=sz();
369  Vector r(sz());
370  double *xp1=r.d->p, *xp2=v.d->p, *xp3=d->p;
371  while (ni--)
372  *(xp1++)+=*(xp3++)-*(xp2++);
373  return r;
374 }
375 
377 {
378  int i=::mmin(sz(),rescaling.sz());
379  double *xb=(*this), *r=rescaling;
380  while (i--) xb[i]*=r[i];
381 }
382 
384 {
385  int i=sz();
386  double *xb=(*this);
387  while (i--) xb[i]=1/xb[i];
388 }
389 
391 {
392  int ni=sz();
393  Vector r(sz());
394  double *xp1=r.d->p, *xp2=v.d->p, *xp3=d->p;
395  while (ni--)
396  *(xp1++)+=*(xp3++)+*(xp2++);
397  return r;
398 }
399 
401 {
402  int ni=sz();
403  double *xp1=d->p, *xp2=v.d->p;
404  while (ni--) *(xp1++)-=*(xp2++);
405  return *this;
406 }
407 
409 {
410  int ni=sz();
411  double *xp1=d->p, *xp2=v.d->p;
412  while (ni--) *(xp1++)+=*(xp2++);
413  return *this;
414 }
415 
416 void Vector ::addInPlace(double a, Vector v)
417 {
418  int ni=sz();
419  double *xp1=d->p, *xp2=v.d->p;
420  if (a==1.0) while (ni--) *(xp1++)+= *(xp2++);
421  else while (ni--) *(xp1++)+=a * (*(xp2++));
422 }
423 
424 void Vector::addInPlace(double a, int i, Matrix m)
425 {
426  int ni=sz();
427  double *xp1=d->p, *xp2=((double**)m)[i];
428  while (ni--) *(xp1++)+=a * (*(xp2++));
429 }
430 
431 Vector::Vector(char *filename)
432 {
433  unsigned _n;
434  std::ifstream ifp(filename, std::ios::in | std::ios::binary);
435  ifp.read(reinterpret_cast<char*>(&_n), sizeof(int));
436  alloc(_n,_n);
437  ifp.read(reinterpret_cast<char*>(d->p), d->n*sizeof(double));
438  ifp.close();
439 }
440 
441 void Vector::save(char *filename, char ascii)
442 {
443  FILE *f;
444  if (ascii) f=fopen(filename,"w"); else f=fopen(filename,"wb");
445  if (f==NULL)
446  {
447  printf("Cannot write to '%s'\n",filename);
448  exit(255);
449  }
450  save(f,ascii);
451  fclose(f);
452 }
453 
454 void Vector::save(FILE *f, char ascii)
455 {
456  char header[32]="CONDORVBv1.0";
457  if (ascii)
458  {
459  unsigned i;
460  double *pp=d->p;
461  if (ascii!=2) fprintf(f,"CONDORVAv1.0\n");
462  if (sz())
463  {
464  for (i=0; i<sz()-1; i++) fprintf(f,"%.16e\t",pp[i]);
465  fprintf(f,"%.16e\n",pp[i]);
466  }
467  return;
468  }
469  fwrite(header,sizeof(char),13,f);
470  fwrite(&d->n, sizeof(int),1, f);
471  fwrite(d->p, d->n*sizeof(double),1, f);
472 }
473 
474 
475 void Vector::setPart(int i, Vector v, int n, int ii)
476 {
477  if (n==0) n=v.sz()-ii;
478  n=::mmin((int)n,(int)sz()-i);
479  memcpy(d->p+i, ((double*)v)+ii, n*sizeof(double));
480 }
481 
482 void Vector::set(double dd)
483 {
484  double *p=(*this);
485  if (!p) return;
486  int n=sz();
487  while (n--) *(p++)=dd;
488 }
489 
490 void Vector::shift(int s)
491 {
492  int n=sz();
493  if (!n) return;
494  double *ps=(*this), *pd=ps; // pointer source / destination
495  if (s==0) return;
496  if (s>0) { n-=s; pd+=s; }
497  else { n+=s; ps+=s; }
498  memmove(pd,ps,n*sizeof(double));
499 }
500 
502 {
503  int i,n=sz(), *ii=viP;
504  if (!n) return;
505  if (n!=viP.sz())
506  {
507  printf("error in permutation IN: sizes don't agree.\n"); getchar(); exit(255);
508  }
509  vR.setSize(n);
510  double *ps=(*this), *pd=vR; // pointer source / destination
511  for (i=0; i<n; i++)
512 // *(pd++)=ps[ii[i]];
513  pd[ii[i]]=*(ps++);
514 }
515 
517 {
518  int i,n=sz(), *ii=viP;
519  if (!n) return;
520  if (n!=viP.sz())
521  {
522  printf("error in permutation IN: sizes don't agree.\n"); getchar(); exit(255);
523  }
524  vR.setSize(n);
525  double *ps=(*this), *pd=vR; // pointer source / destination
526  for (i=0; i<n; i++)
527 // pd[ii[i]]=*(ps++);
528  *(pd++)=ps[ii[i]];
529 }
530 
531 #define EOL1 13
532 #define EOL2 10
533 
534 char isANumber(char c)
535 {
536  return (((c>='0')&&(c<='9'))||
537  (c=='.')||
538  (c=='e')||
539  (c=='E')||
540  (c=='+')||
541  (c=='-'));
542 }
543 Vector::Vector(char *line, int gn)
544 {
545  char *tline=line,*oldtline=NULL;
546 
547  if (gn==0)
548  {
549  while ((*tline!=EOL1)&&(*tline!=EOL2))
550  {
551  while ((*tline==' ')||
552  (*tline=='\t'))tline++;
553  if ((*tline==EOL1)||(*tline==EOL2)||(*tline==0)) break;
554  while (isANumber(*tline)) tline++;
555  gn++;
556  if (oldtline==tline)
557  {
558  alloc(gn-1,gn-1);
559  return;
560  //tline[10]=0;
561  //printf("Error in simulation output file. The full line is:\n"
562  // "%s\n"
563  // "There is an error here:\n"
564  // "%s\n",line,tline);
565  }
566  oldtline=tline;
567  };
568  };
569 
570  if (gn==0) { alloc(0,0); return; };
571  alloc(gn,gn);
572  getFromLine(line);
573 }
574 
575 char *Vector::getFromLine(char *line)
576 {
577  double *dp=d->p;
578  int n=sz(),k;
579  char *tline=line, *oldtline;
580  for (k=0; k<n; k++)
581  {
582  while ((*tline==' ')||
583  (*tline=='\t'))tline++;
584  if ((*tline==EOL1)||(*tline==EOL2))
585  {
586  setSize(k);
587  return tline;
588  }
589  oldtline=tline;
590  while(isANumber(*tline)) tline++;
591  if (!isANumber(*(tline-1)))
592  {
593  setSize(k);
594  return tline;
595  //tline[10]=0;
596  //printf( "Error in simulation output file. The full line is:\n"
597  // "%s\n"
598  // "There is an error here:\n"
599  // "%s\n",line,tline);
600  }
601  if (oldtline==tline)
602  {
603  setSize(k);
604  return tline;
605  };
606  if (*tline) { *tline='\0'; tline++; }
607  dp[k]=atof(oldtline);
608  }
609  return tline;
610 }
611 
612 void Vector::appendToMatrixFile(char *saveFileName, char **names)
613 {
614  FILE *f=fopen(saveFileName,"rb+");
615  if (f==NULL)
616  {
617  Matrix t(*this);
618  if (names) t.setColNames(names);
619  t.save(saveFileName,0);
620  return;
621  }
622  int nc=sz();
623  fseek(f,0,SEEK_END);
624  unsigned l=ftell(f);
625  if (l==0)
626  {
627  Matrix t(*this);
628  if (names) t.setColNames(names);
629  t.save(saveFileName,0);
630  return;
631  }
632  fwrite(d->p,sizeof(double)*nc,1,f);
633  unsigned nlfile;
634  fseek(f,13,SEEK_SET);
635  if (fread(&nlfile,sizeof(int),1,f) != sizeof(int)) {
636  std::cerr << "error while appending to file " << saveFileName << std::endl; exit(255);
637  }
638  nlfile++;
639  fseek(f,13,SEEK_SET);
640  fwrite(&nlfile, sizeof(unsigned), 1, f);
641  fclose(f);
642 }
void permutIn(Vector vR, VectorInt viP)
Definition: Vector.cpp:501
int sz()
Definition: VectorInt.h:63
void appendToMatrixFile(char *saveFileName, char **names=NULL)
Definition: Vector.cpp:612
void setColNames(char **c, int nc=0)
Definition: Matrix.cpp:1180
double LnftyNorm()
Definition: Vector.cpp:264
doublereal * c
#define pp(s, x)
Definition: ml2d.cpp:473
#define EOL2
Definition: Vector.cpp:532
Definition: Vector.h:37
void save(char *filename, char ascii)
Definition: Vector.cpp:441
int equals(const Vector Q)
Definition: Vector.cpp:135
double LnftyDistance(Vector v)
Definition: Vector.cpp:256
double euclidianNorm()
Definition: Vector.cpp:222
void prepareExtend(int new_extention)
Definition: Vector.cpp:99
Vector operator-=(Vector v)
Definition: Vector.cpp:400
void print()
Definition: Vector.cpp:161
double euclidianDistance(Vector v)
Definition: Vector.cpp:244
void oneByOneMutiply(Vector r)
Definition: Vector.cpp:376
int nColumn()
Definition: Matrix.h:84
Vector clone()
Definition: Vector.cpp:207
doublereal * x
#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: Vector.cpp:112
void setExternalData(int _n, double *dd)
Definition: Vector.cpp:80
char * getFromLine(char *line)
Definition: Vector.cpp:575
double mmin()
Definition: Vector.cpp:340
unsigned sz()
Definition: Vector.h:79
int nLine()
Definition: Matrix.h:83
double sqr(const double &t)
Definition: tools.h:99
doublereal * b
double L1Norm()
Definition: Vector.cpp:227
Vector(int _n=0)
Definition: Vector.cpp:49
double euclidianNorm(int i, double *xp)
Definition: tools.cpp:113
void shift(int s)
Definition: Vector.cpp:490
int in
double * f
void zero(int _i=0, int _n=0)
Definition: Vector.cpp:93
Vector operator+=(Vector v)
Definition: Vector.cpp:408
Vector operator+(Vector v)
Definition: Vector.cpp:390
~Vector()
Definition: Vector.cpp:172
double condorAbs(const double t1)
Definition: tools.h:47
free((char *) ob)
void alloc(int n, int ext)
Definition: Vector.cpp:36
double square()
Definition: Vector.cpp:236
void exactshape()
Definition: Vector.cpp:125
Definition: Matrix.h:38
void oneByOneInvert()
Definition: Vector.cpp:383
void transposeAndMultiply(Vector vR, Matrix M)
Definition: Vector.cpp:297
Vector operator-(Vector v)
Definition: Vector.cpp:366
VectorData * d
Definition: Vector.h:50
int ni
void multiply(double a)
Definition: Vector.cpp:281
double L1Distance(Vector v)
Definition: Vector.cpp:272
bool isNull()
Definition: Vector.cpp:358
#define j
int m
void addInPlace(double a, Vector v)
Definition: Vector.cpp:416
void diagonalizeAndMultiply(Matrix M)
Definition: Vector.cpp:316
void save(char *filename, char ascii)
Definition: Matrix.cpp:282
double mmax()
Definition: Vector.cpp:349
#define EOL1
Definition: Vector.cpp:531
double scalarProduct(Vector v)
Definition: Vector.cpp:332
void setPart(int i, Vector v, int n=0, int ii=0)
Definition: Vector.cpp:475
void permutOut(Vector vR, VectorInt viP)
Definition: Vector.cpp:516
struct Vector::VectorDataTag VectorData
void destroyCurrentBuffer()
Definition: Vector.cpp:177
static Vector emptyVector
Definition: Vector.h:119
void extend()
Definition: Vector.cpp:119
void set(double dd)
Definition: Vector.cpp:482
Vector & operator=(const Vector &P)
Definition: Vector.cpp:195
fprintf(glob_prnt.io, "\)
#define INF
Definition: svm.cpp:43
char isANumber(char c)
Definition: Vector.cpp:534
void copyFrom(Vector r, int _n=0)
Definition: Vector.cpp:215
int * n
doublereal * a