Xmipp  v3.23.11-Nereus
matrix1d.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include "matrix1d.h"
27 #include "xmipp_filename.h"
28 #include "numerical_recipes.h"
29 #include <fstream>
30 #include <algorithm>
31 
32 template<typename T>
34 {
35  if (&op1 != this)
36  {
37  resizeNoCopy(op1);
38  memcpy(vdata, op1.vdata, vdim * sizeof(T));
39  row = op1.row;
40  }
41 
42  return *this;
43 }
44 
45 template<typename T>
46 bool Matrix1D<T>::operator==(const Matrix1D<T>& op1) const
47 {
48  if (row != op1.row)
49  return false;
50 
51  for (size_t i = 0; i < vdim; ++i)
52  if (!XMIPP_EQUAL_REAL(vdata[i], op1.vdata[i]))
53  return false;
54  return true;
55 }
56 
57 template<typename T>
58 Matrix1D<T>& Matrix1D<T>::operator=(const std::vector<T>& op1)
59 {
60  resizeNoCopy(op1.size());
61  memcpy(&vdata[0],&(op1[0]),vdim*sizeof(T));
62  row = false;
63  return *this;
64 }
65 
66 template<typename T>
68 {
69  coreDeallocate();
70  coreInit();
71 }
72 
73 template<typename T>
75 {
76  vdim = 0;
77  row = false;
78  vdata = NULL;
79  destroyData = true;
80 }
81 
82 template<typename T>
84 {
85  for (size_t j = 0; j < vdim; j++)
86  vdata[j] = val;
87 }
88 
89 template<typename T>
90 void Matrix1D<T>::initConstant(size_t Xdim, T val)
91 {
92  if (vdim != Xdim)
93  resizeNoCopy(Xdim);
94  initConstant(val);
95 }
96 
97 template<typename T>
99 {
100  for (size_t j = 0; j < vdim; j++)
101  vdata[j] = j;
102 }
103 
104 template<typename T>
106 {
107  for (size_t j = 0; j < vdim; j++)
108  if (ISNAN(vdata[j]))
109  return true;
110  return false;
111 }
112 
113 template<typename T>
115 {
116  Matrix1D<T> tmp(*this);
117  T *ptr1 = &VEC_ELEM(tmp,0);
118  const T *ptr2 = &VEC_ELEM(*this,0);
119  size_t iBlockMax = vdim / 4;
120  for (size_t i = 0; i < iBlockMax; i++)
121  {
122  (*ptr1++) = (*ptr2++) * op1;
123  (*ptr1++) = (*ptr2++) * op1;
124  (*ptr1++) = (*ptr2++) * op1;
125  (*ptr1++) = (*ptr2++) * op1;
126  }
127  for (size_t i = iBlockMax * 4; i < vdim; ++i)
128  (*ptr1++) = (*ptr2++) * op1;
129  return tmp;
130 }
131 
132 template<typename T>
134 {
135  Matrix1D<T> tmp(*this);
136  T iop1 = 1 / op1;
137  T *ptr1 = &VEC_ELEM(tmp,0);
138  const T *ptr2 = &VEC_ELEM(*this,0);
139  size_t iBlockMax = vdim / 4;
140  for (size_t i = 0; i < iBlockMax; i++)
141  {
142  (*ptr1++) = (*ptr2++) * iop1;
143  (*ptr1++) = (*ptr2++) * iop1;
144  (*ptr1++) = (*ptr2++) * iop1;
145  (*ptr1++) = (*ptr2++) * iop1;
146  }
147  for (size_t i = iBlockMax * 4; i < vdim; ++i)
148  (*ptr1++) = (*ptr2++) * iop1;
149  return tmp;
150 }
151 
152 template<typename T>
154 {
155  Matrix1D<T> tmp(*this);
156  T *ptr1 = &VEC_ELEM(tmp,0);
157  const T *ptr2 = &VEC_ELEM(*this,0);
158  size_t iBlockMax = vdim / 4;
159  for (size_t i = 0; i < iBlockMax; i++)
160  {
161  (*ptr1++) = (*ptr2++) + op1;
162  (*ptr1++) = (*ptr2++) + op1;
163  (*ptr1++) = (*ptr2++) + op1;
164  (*ptr1++) = (*ptr2++) + op1;
165  }
166  for (size_t i = iBlockMax * 4; i < vdim; ++i)
167  (*ptr1++) = (*ptr2++) + op1;
168  return tmp;
169 }
170 
171 template<typename T>
173  {
174  Matrix1D<T> tmp(*this);
175  T *ptr1 = &VEC_ELEM(tmp,0);
176  const T *ptr2 = &VEC_ELEM(*this,0);
177  size_t iBlockMax = vdim / 4;
178  for (size_t i = 0; i < iBlockMax; i++)
179  {
180  (*ptr1++) = (*ptr2++) - op1;
181  (*ptr1++) = (*ptr2++) - op1;
182  (*ptr1++) = (*ptr2++) - op1;
183  (*ptr1++) = (*ptr2++) - op1;
184  }
185  for (size_t i = iBlockMax * 4; i < vdim; ++i)
186  (*ptr1++) = (*ptr2++) - op1;
187  return tmp;
188  }
189 
190 template<typename T>
191 void Matrix1D<T>::operator+=(const Matrix1D<T>& op1) const
192 {
193  if (vdim != op1.vdim)
194  REPORT_ERROR(ERR_MATRIX_SIZE, "Not same sizes in vector summation");
195 
196  T *ptr1 = &VEC_ELEM(*this,0);
197  const T *ptr2 = &VEC_ELEM(op1,0);
198  size_t iBlockMax = vdim / 4;
199  for (size_t i = 0; i < iBlockMax; i++)
200  {
201  (*ptr1++) += (*ptr2++);
202  (*ptr1++) += (*ptr2++);
203  (*ptr1++) += (*ptr2++);
204  (*ptr1++) += (*ptr2++);
205  }
206  for (size_t i = iBlockMax * 4; i < vdim; ++i)
207  (*ptr1++) += (*ptr2++);
208 }
209 
210 template<typename T>
211 void Matrix1D<T>::operator-=(const Matrix1D<T>& op1) const
212 {
213  if (vdim != op1.vdim)
214  REPORT_ERROR(ERR_MATRIX_SIZE, "Not same sizes in vector summation");
215 
216  T *ptr1 = &VEC_ELEM(*this,0);
217  const T *ptr2 = &VEC_ELEM(op1,0);
218  size_t iBlockMax = vdim / 4;
219  for (size_t i = 0; i < iBlockMax; i++)
220  {
221  (*ptr1++) -= (*ptr2++);
222  (*ptr1++) -= (*ptr2++);
223  (*ptr1++) -= (*ptr2++);
224  (*ptr1++) -= (*ptr2++);
225  }
226  for (size_t i = iBlockMax * 4; i < vdim; ++i)
227  (*ptr1++) -= (*ptr2++);
228 }
229 
230 template<typename T>
232 {
233  T *ptr1 = &VEC_ELEM(*this,0);
234  size_t iBlockMax = vdim / 4;
235  for (size_t i = 0; i < iBlockMax; i++)
236  {
237  (*ptr1++) *= op1;
238  (*ptr1++) *= op1;
239  (*ptr1++) *= op1;
240  (*ptr1++) *= op1;
241  }
242  for (size_t i = iBlockMax * 4; i < vdim; ++i)
243  (*ptr1++) *= op1;
244 }
245 
246 template<typename T>
248 {
249  T iop1 = 1 / op1;
250  T * ptr1 = &VEC_ELEM(*this,0);
251  size_t iBlockMax = vdim / 4;
252  for (size_t i = 0; i < iBlockMax; i++)
253  {
254  (*ptr1++) *= iop1;
255  (*ptr1++) *= iop1;
256  (*ptr1++) *= iop1;
257  (*ptr1++) *= iop1;
258  }
259  for (size_t i = iBlockMax * 4; i < vdim; ++i)
260  (*ptr1++) *= iop1;
261 }
262 
263 template<typename T>
265 {
266  T *ptr1 = &VEC_ELEM(*this,0);
267  size_t iBlockMax = vdim / 4;
268  for (size_t i = 0; i < iBlockMax; i++)
269  {
270  (*ptr1++) += op1;
271  (*ptr1++) += op1;
272  (*ptr1++) += op1;
273  (*ptr1++) += op1;
274  }
275  for (size_t i = iBlockMax * 4; i < vdim; ++i)
276  (*ptr1++) += op1;
277 }
278 
279 template<typename T>
281 {
282  T *ptr1 = &VEC_ELEM(*this,0);
283  size_t iBlockMax = vdim / 4;
284  for (size_t i = 0; i < iBlockMax; i++)
285  {
286  (*ptr1++) -= op1;
287  (*ptr1++) -= op1;
288  (*ptr1++) -= op1;
289  (*ptr1++) -= op1;
290  }
291  for (size_t i = iBlockMax * 4; i < vdim; ++i)
292  (*ptr1++) -= op1;
293 }
294 
295 template<typename T>
297 {
298  Matrix1D<T> tmp(op1);
299  T *ptr1 = &VEC_ELEM(tmp,0);
300  const T *ptr2 = &VEC_ELEM(*this,0);
301  const T *ptr3 = &VEC_ELEM(op1,0);
302  size_t iBlockMax = vdim / 4;
303  for (size_t i = 0; i < iBlockMax; i++)
304  {
305  (*ptr1++) = (*ptr2++) * (*ptr3++);
306  (*ptr1++) = (*ptr2++) * (*ptr3++);
307  (*ptr1++) = (*ptr2++) * (*ptr3++);
308  (*ptr1++) = (*ptr2++) * (*ptr3++);
309  }
310  for (size_t i = iBlockMax * 4; i < vdim; ++i)
311  (*ptr1++) = (*ptr2++) * (*ptr3++);
312  return tmp;
313 }
314 
315 template<typename T>
317 {
318  Matrix1D<T> tmp(op1);
319  T *ptr1 = &VEC_ELEM(tmp,0);
320  const T *ptr2 = &VEC_ELEM(*this,0);
321  const T *ptr3 = &VEC_ELEM(op1,0);
322  size_t iBlockMax = vdim / 4;
323  for (size_t i = 0; i < iBlockMax; i++)
324  {
325  (*ptr1++) = (*ptr2++) / (*ptr3++);
326  (*ptr1++) = (*ptr2++) / (*ptr3++);
327  (*ptr1++) = (*ptr2++) / (*ptr3++);
328  (*ptr1++) = (*ptr2++) / (*ptr3++);
329  }
330  for (size_t i = iBlockMax * 4; i < vdim; ++i)
331  (*ptr1++) = (*ptr2++) / (*ptr3++);
332  return tmp;
333 }
334 
335 template<typename T>
337 {
338  Matrix1D<T> tmp(op1);
339  T *ptr1 = &VEC_ELEM(tmp,0);
340  const T *ptr2 = &VEC_ELEM(*this,0);
341  const T *ptr3 = &VEC_ELEM(op1,0);
342  size_t iBlockMax = vdim / 4;
343  for (size_t i = 0; i < iBlockMax; i++)
344  {
345  (*ptr1++) = (*ptr2++) + (*ptr3++);
346  (*ptr1++) = (*ptr2++) + (*ptr3++);
347  (*ptr1++) = (*ptr2++) + (*ptr3++);
348  (*ptr1++) = (*ptr2++) + (*ptr3++);
349  }
350  for (size_t i = iBlockMax * 4; i < vdim; ++i)
351  (*ptr1++) = (*ptr2++) + (*ptr3++);
352  return tmp;
353 }
354 
355 template<typename T>
357 {
358  Matrix1D<T> tmp(op1);
359  T *ptr1 = &VEC_ELEM(tmp,0);
360  const T *ptr2 = &VEC_ELEM(*this,0);
361  const T *ptr3 = &VEC_ELEM(op1,0);
362  size_t iBlockMax = vdim / 4;
363  for (size_t i = 0; i < iBlockMax; i++)
364  {
365  (*ptr1++) = (*ptr2++) - (*ptr3++);
366  (*ptr1++) = (*ptr2++) - (*ptr3++);
367  (*ptr1++) = (*ptr2++) - (*ptr3++);
368  (*ptr1++) = (*ptr2++) - (*ptr3++);
369  }
370  for (size_t i = iBlockMax * 4; i < vdim; ++i)
371  (*ptr1++) = (*ptr2++) - (*ptr3++);
372  return tmp;
373 }
374 
375 template<typename T>
377 {
378  T *ptr1 = &VEC_ELEM(*this,0);
379  const T *ptr2 = &VEC_ELEM(op1,0);
380  size_t iBlockMax = vdim / 4;
381  for (size_t i = 0; i < iBlockMax; i++)
382  {
383  (*ptr1++) *= (*ptr2++);
384  (*ptr1++) *= (*ptr2++);
385  (*ptr1++) *= (*ptr2++);
386  (*ptr1++) *= (*ptr2++);
387  }
388  for (size_t i = iBlockMax * 4; i < vdim; ++i)
389  (*ptr1++) *= (*ptr2++);
390 }
391 
392 template<typename T>
394 {
395  T *ptr1 = &VEC_ELEM(*this,0);
396  const T *ptr2 = &VEC_ELEM(op1,0);
397  size_t iBlockMax = vdim / 4;
398  for (size_t i = 0; i < iBlockMax; i++)
399  {
400  (*ptr1++) /= (*ptr2++);
401  (*ptr1++) /= (*ptr2++);
402  (*ptr1++) /= (*ptr2++);
403  (*ptr1++) /= (*ptr2++);
404  }
405  for (size_t i = iBlockMax * 4; i < vdim; ++i)
406  (*ptr1++) /= (*ptr2++);
407 }
408 
409 template<typename T>
411  {
412  T *ptr1 = &VEC_ELEM(*this,0);
413  const T *ptr2 = &VEC_ELEM(op1,0);
414  size_t iBlockMax = vdim / 4;
415  for (size_t i = 0; i < iBlockMax; i++)
416  {
417  (*ptr1++) += (*ptr2++);
418  (*ptr1++) += (*ptr2++);
419  (*ptr1++) += (*ptr2++);
420  (*ptr1++) += (*ptr2++);
421  }
422  for (size_t i = iBlockMax * 4; i < vdim; ++i)
423  (*ptr1++) += (*ptr2++);
424  }
425 
426 template<typename T>
428  {
429  T *ptr1 = &VEC_ELEM(*this,0);
430  const T *ptr2 = &VEC_ELEM(op1,0);
431  size_t iBlockMax = vdim / 4;
432  for (size_t i = 0; i < iBlockMax; i++)
433  {
434  (*ptr1++) -= (*ptr2++);
435  (*ptr1++) -= (*ptr2++);
436  (*ptr1++) -= (*ptr2++);
437  (*ptr1++) -= (*ptr2++);
438  }
439  for (size_t i = iBlockMax * 4; i < vdim; ++i)
440  (*ptr1++) -= (*ptr2++);
441  }
442 
443 template<typename T>
445 {
446  Matrix1D<T> tmp(*this);
447  T *ptr1 = &VEC_ELEM(tmp,0);
448  const T *ptr2 = &VEC_ELEM(*this,0);
449  size_t iBlockMax = vdim / 4;
450  for (size_t i = 0; i < iBlockMax; i++)
451  {
452  if constexpr (std::is_signed_v<T> || std::is_floating_point_v<T>) {
453  (*ptr1++) = -(*ptr2++);
454  (*ptr1++) = -(*ptr2++);
455  (*ptr1++) = -(*ptr2++);
456  (*ptr1++) = -(*ptr2++);
457  } else {
458  REPORT_ERROR(
460  static_cast<std::string>("Can not use unitary minus on unsigned datatypes"));
461  }
462 
463  }
464  for (size_t i = iBlockMax * 4; i < vdim; ++i)
465  if constexpr (std::is_signed_v<T> || std::is_floating_point_v<T>) {
466  (*ptr1++) = -(*ptr2++);
467  } else {
468  REPORT_ERROR(
470  static_cast<std::string>("Can not use unitary minus on unsigned datatypes"));
471  }
472  return tmp;
473 }
474 
475 template<typename T>
477 {
478  T *ptr1 = &VEC_ELEM(*this,0);
479  size_t iBlockMax = vdim / 4;
480  for (size_t i = 0; i < iBlockMax; i++)
481  {
482  *ptr1 = ceil(*ptr1);
483  ++ptr1;
484  *ptr1 = ceil(*ptr1);
485  ++ptr1;
486  *ptr1 = ceil(*ptr1);
487  ++ptr1;
488  *ptr1 = ceil(*ptr1);
489  ++ptr1;
490  }
491  for (size_t i = iBlockMax * 4; i < vdim; ++i)
492  {
493  *ptr1 = ceil(*ptr1);
494  ++ptr1;
495  }
496 }
497 
498 template<typename T>
500 {
501  T *ptr1 = &VEC_ELEM(*this,0);
502  size_t iBlockMax = vdim / 4;
503  for (size_t i = 0; i < iBlockMax; i++)
504  {
505  *ptr1 = floor(*ptr1);
506  ++ptr1;
507  *ptr1 = floor(*ptr1);
508  ++ptr1;
509  *ptr1 = floor(*ptr1);
510  ++ptr1;
511  *ptr1 = floor(*ptr1);
512  ++ptr1;
513  }
514  for (size_t i = iBlockMax * 4; i < vdim; ++i)
515  {
516  *ptr1 = floor(*ptr1);
517  ++ptr1;
518  }
519 }
520 
521 template<typename T>
523 {
524  T *ptr1 = &VEC_ELEM(*this,0);
525  size_t iBlockMax = vdim / 4;
526  for (size_t i = 0; i < iBlockMax; i++)
527  {
528  *ptr1 = round(*ptr1);
529  ++ptr1;
530  *ptr1 = round(*ptr1);
531  ++ptr1;
532  *ptr1 = round(*ptr1);
533  ++ptr1;
534  *ptr1 = round(*ptr1);
535  ++ptr1;
536  }
537  for (size_t i = iBlockMax * 4; i < vdim; ++i)
538  {
539  *ptr1 = round(*ptr1);
540  ++ptr1;
541  }
542 }
543 
545 template<typename T>
547 {
548  if (vdim == 0)
549  return 0;
550 
551  double sum = 0;
552  for (size_t j = 0; j < vdim; ++j)
553  sum+=VEC_ELEM(*this,j);
554  return sum/vdim;
555 }
556 
557 template<typename T>
559 {
560  if (vdim == 0)
561  return 0;
562 
563  T maxval = VEC_ELEM(*this,0);
564  for (size_t j = 0; j < vdim; ++j)
565  if (VEC_ELEM(*this,j) > maxval)
566  maxval = VEC_ELEM(*this,j);
567  return maxval;
568 }
569 
570 template<typename T>
571 void Matrix1D<T>::computeMinMax(T &minval, T &maxval) const
572 {
573  if (vdim == 0)
574  return;
575 
576  maxval = minval = VEC_ELEM(*this,0);
577  for (size_t j = 0; j < vdim; ++j)
578  {
579  T val=VEC_ELEM(*this,j);
580  if (val > maxval)
581  maxval = val;
582  else if (val<minval)
583  minval = val;
584  }
585 }
586 
587 template<typename T>
588 void Matrix1D<T>::computeMeanAndStddev(double &mean, double &stddev) const
589 {
590  mean=stddev=0;
591  if (vdim == 0)
592  return;
593 
594  double sum = 0, sum2 = 0;
595  for (size_t j = 0; j < vdim; ++j)
596  {
597  double val=VEC_ELEM(*this,j);
598  sum+=val;
599  sum2+=val*val;
600  }
601  mean=sum/vdim;
602  stddev=sum2/vdim-mean*mean;
603  if (stddev<0)
604  stddev=0;
605  else
606  stddev=sqrt(stddev);
607 }
608 
609 template<typename T>
611 {
612  if (vdim == 0)
613  return -1;
614 
615  int jmax = 0;
616  T maxval = VEC_ELEM(*this, 0);
617  for (size_t j = 0; j < vdim; ++j)
618  if (VEC_ELEM(*this,j) > maxval)
619  {
620  jmax = j;
621  maxval = VEC_ELEM(*this,j);
622  }
623  return jmax;
624 }
625 
626 template<typename T>
628 {
629  if (vdim == 0)
630  return -1;
631 
632  int jmin = 0;
633  T minval = VEC_ELEM(*this, 0);
634  for (size_t j = 0; j < vdim; ++j)
635  if (VEC_ELEM(*this,j) < minval)
636  {
637  jmin = j;
638  minval = VEC_ELEM(*this,j);
639  }
640  return jmin;
641 }
642 
643 template<typename T>
645 {
646  Matrix1D<T> temp(*this);
647  temp.selfTranspose();
648  return temp;
649 }
650 
651 template<typename T>
652 double Matrix1D<T>::sum(bool average) const
653 {
654  double sum = 0;
655  const T *ptr1 = &VEC_ELEM(*this,0);
656  size_t iBlockMax = vdim / 4;
657  for (size_t i = 0; i < iBlockMax; i++)
658  {
659  sum += (*ptr1++);
660  sum += (*ptr1++);
661  sum += (*ptr1++);
662  sum += (*ptr1++);
663  }
664  for (size_t i = iBlockMax * 4; i < vdim; ++i)
665  sum += (*ptr1++);
666  if (average)
667  return sum / (double) vdim;
668  else
669  return sum;
670 }
671 
672 template<typename T>
673 double Matrix1D<T>::sum2() const
674 {
675  double sum = 0;
676  const T *ptr1 = &VEC_ELEM(*this,0);
677  double val;
678  size_t iBlockMax = vdim / 4;
679  for (size_t i = 0; i < iBlockMax; i++)
680  {
681  val = *ptr1;
682  sum += val * val;
683  ++ptr1;
684  val = *ptr1;
685  sum += val * val;
686  ++ptr1;
687  val = *ptr1;
688  sum += val * val;
689  ++ptr1;
690  val = *ptr1;
691  sum += val * val;
692  ++ptr1;
693  }
694  for (size_t i = iBlockMax * 4; i < vdim; ++i)
695  {
696  val = *ptr1;
697  sum += val * val;
698  ++ptr1;
699  }
700  return sum;
701 }
702 
703 template<typename T>
704 double Matrix1D<T>::dotProduct(const Matrix1D<T> &op1) const
705 {
706  double sum = 0;
707  const T *ptr1 = &VEC_ELEM(*this,0);
708  const T *ptr2 = &VEC_ELEM(op1,0);
709  size_t iBlockMax = vdim / 4;
710  for (size_t i = 0; i < iBlockMax; i++)
711  {
712  sum += (*ptr1++) * (*ptr2++);
713  sum += (*ptr1++) * (*ptr2++);
714  sum += (*ptr1++) * (*ptr2++);
715  sum += (*ptr1++) * (*ptr2++);
716  }
717  for (size_t i = iBlockMax * 4; i < vdim; ++i)
718  sum += (*ptr1++) * (*ptr2++);
719  return sum;
720 }
721 
722 template<typename T>
724 {
725  double m = module();
726  if (fabs(m) > XMIPP_EQUAL_ACCURACY)
727  {
728  T im = (T) (1.0 / m);
729  *this *= im;
730  }
731  else
732  initZeros();
733 }
734 
735 template<typename T>
737 {
738  for (int j = 0; j <= (int) (vdim - 1) / 2; j++)
739  {
740  T aux;
741  SWAP(vdata[j], vdata[vdim-1-j], aux);
742  }
743 }
744 
745 template<typename T>
747 {
748  const double i12 = 1.0 / 12.0;
749  result.initZeros(*this);
750  for (int i = 2; i <= vdim - 2; i++)
751  if constexpr (std::is_signed_v<T> || std::is_floating_point_v<T>) {
752  result(i) = i12
753  * (-(*this)(i + 2) + 8 * (*this)(i + 1) - 8 * (*this)(i - 1)
754  + (*this)(i + 2));
755  } else {
756  REPORT_ERROR(
758  static_cast<std::string>("Can not use unitary minus on unsigned datatypes"));
759  }
760 }
761 
762 template<typename T>
763 void Matrix1D<T>::showWithGnuPlot(const std::string& xlabel, const std::string& title)
764 {
765  FileName fn_tmp;
766  fn_tmp.initRandom(10);
767  Matrix1D<T>::write(static_cast<std::string>("PPP") + fn_tmp + ".txt");
768 
769  std::ofstream fh_gplot;
770  fh_gplot.open(
771  (static_cast<std::string>("PPP") + fn_tmp + ".gpl").c_str());
772  if (!fh_gplot)
773  REPORT_ERROR(
775  static_cast<std::string>("vector::showWithGnuPlot: Cannot open PPP") + fn_tmp + ".gpl for output");
776  fh_gplot << "set xlabel \"" + xlabel + "\"\n";
777  fh_gplot
778  << "plot \"PPP" + fn_tmp + ".txt\" title \"" + title
779  + "\" w l\n";
780  fh_gplot << "pause 300 \"\"\n";
781  fh_gplot.close();
782  auto res = system(
783  (static_cast<std::string>("(gnuplot PPP") + fn_tmp
784  + ".gpl; rm PPP" + fn_tmp + ".txt PPP" + fn_tmp
785  + ".gpl) &").c_str());
786  if (0 != res) {
787  REPORT_ERROR(
789  "Something went wrong when working with GNUPlot. Please report this error to developers.");
790  }
791 }
792 
793 template<typename T>
794 void Matrix1D<T>::write(const FileName& fn) const
795 {
796  std::ofstream out;
797  out.open(fn.c_str(), std::ios::out);
798  if (!out)
799  REPORT_ERROR(
801  static_cast< std::string >("Matrix1D::write: File " + fn + " cannot be opened for output"));
802 
803  out << *this;
804  out.close();
805 }
806 
807 template<typename T>
809 {
810  std::ifstream in;
811  in.open(fn.c_str(), std::ios::in);
812 
813  if (!in)
814  REPORT_ERROR(
816  static_cast< std::string >("MultidimArray::read: File " + fn + " not found"));
817 
818  in >> *this;
819  in.close();
820 }
821 
822 template<typename T>
824 {
825  FileName nam;
826  nam.initRandom(15);
827 
828  nam = static_cast<std::string>("PPP" + nam + ".txt");
829  write
830  (nam);
831 
832  auto res = system(
833  (static_cast<std::string>("xmipp_edit -i " + nam + " -remove &").c_str()));
834  if (0 != res) {
835  REPORT_ERROR(
837  "Something went wrong when working with xmipp_edit. Please report this error to developers.");
838  }
839 }
840 
841 template<typename T>
843 {
844  Matrix1D<T> aux;
845  aux=*this;
846  std::nth_element(aux.vdata,aux.vdata+vdim/2,aux.vdata+vdim);
847  return VEC_ELEM(aux,vdim/2);
848 }
849 template<typename T>
851 {
852  Matrix1D<T> temp(*this);
853  if (vdim == 0)
854  return temp;
855 
856  std::sort(temp.vdata, temp.vdata + temp.vdim);
857  return temp;
858 }
859 
860 template<typename T>
862 {
863  Matrix1D< double > temp;
864  indx.clear();
865 
866  if (VEC_XSIZE(*this) == 0)
867  return;
868 
869  if (VEC_XSIZE(*this) == 1)
870  {
871  indx.resizeNoCopy(1);
872  VEC_ELEM(indx,0) = 1;
873  return;
874  }
875 
876  // Initialise data
877  indx.resizeNoCopy(VEC_XSIZE(*this));
878  typeCast(*this, temp);
879 
880  // Sort indexes
881  indexx(VEC_XSIZE(*this), MATRIX1D_ARRAY(temp)-1, MATRIX1D_ARRAY(indx)-1);
882 }
883 
884 Matrix1D<double> vectorR2(double x, double y)
885 {
886  Matrix1D<double> result(2);
887  VEC_ELEM(result, 0) = x;
888  VEC_ELEM(result, 1) = y;
889  return result;
890 }
891 
892 Matrix1D<double> vectorR3(double x, double y, double z)
893 {
894  Matrix1D<double> result(3);
895  VEC_ELEM(result, 0) = x;
896  VEC_ELEM(result, 1) = y;
897  VEC_ELEM(result, 2) = z;
898  return result;
899 }
900 
901 Matrix1D<int> vectorR3(int x, int y, int z)
902 {
903  Matrix1D<int> result(3);
904  VEC_ELEM(result, 0) = x;
905  VEC_ELEM(result, 1) = y;
906  VEC_ELEM(result, 2) = z;
907  return result;
908 }
909 
910 // explicit instantiation
911 template class Matrix1D<double>;
912 template class Matrix1D<int>;
913 template class Matrix1D<float>;
914 template class Matrix1D<unsigned char>;
915 template class Matrix1D<short>;
916 template class Matrix1D<unsigned long>;
Just to locate unclassified errors.
Definition: xmipp_error.h:192
void selfROUND()
Definition: matrix1d.cpp:522
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
void coreInit()
Definition: matrix1d.cpp:74
__host__ __device__ float2 floor(const float2 v)
T computeMedian() const
Definition: matrix1d.cpp:842
void clear()
Definition: matrix1d.cpp:67
void write(std::ostream &os, const datablock &db)
Definition: cif2pdb.cpp:3747
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void computeMinMax(T &minval, T &maxval) const
Definition: matrix1d.cpp:571
Problem with matrix size.
Definition: xmipp_error.h:152
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
Matrix1D< T > operator-() const
Definition: matrix1d.cpp:444
void indexSort(Matrix1D< int > &indx) const
Definition: matrix1d.cpp:861
void sqrt(Image< double > &op)
Matrix1D< T > operator*(T op1) const
Definition: matrix1d.cpp:114
static double * y
Matrix1D< double > vectorR3(double x, double y, double z)
Definition: matrix1d.cpp:892
void showWithGnuPlot(const std::string &xlabel, const std::string &title)
Definition: matrix1d.cpp:763
void enumerate()
Definition: matrix1d.cpp:98
void selfNormalize()
Definition: matrix1d.cpp:723
void operator+=(const Matrix1D< T > &op1) const
Definition: matrix1d.cpp:191
void selfTranspose()
Definition: matrix1d.h:944
void write(const FileName &fn) const
Definition: matrix1d.cpp:794
doublereal * x
#define i
Matrix1D< T > transpose() const
Definition: matrix1d.cpp:644
int maxIndex() const
Definition: matrix1d.cpp:610
#define XMIPP_EQUAL_REAL(x, y)
Definition: xmipp_macros.h:122
void read(const FileName &fn)
Definition: matrix1d.cpp:808
void operator-=(const Matrix1D< T > &op1) const
Definition: matrix1d.cpp:211
double sum2() const
Definition: matrix1d.cpp:673
double computeMean() const
Definition: matrix1d.cpp:546
bool isAnyNaN()
Definition: matrix1d.cpp:105
T computeMax() const
Definition: matrix1d.cpp:558
double dotProduct(const Matrix1D< T > &op1) const
Definition: matrix1d.cpp:704
int in
#define XMIPP_EQUAL_ACCURACY
Definition: xmipp_macros.h:119
#define ISNAN(x)
Definition: xmipp_macros.h:64
void indexx(int n, double arrin[], int indx[])
bool row
<0=column vector (default), 1=row vector
Definition: matrix1d.h:267
double z
double sum(bool average=false) const
Definition: matrix1d.cpp:652
Matrix1D< double > vectorR2(double x, double y)
Definition: matrix1d.cpp:884
void initZeros()
Definition: matrix1d.h:592
Matrix1D< T > operator/(T op1) const
Definition: matrix1d.cpp:133
void sort(struct DCEL_T *dcel)
Definition: sorting.cpp:18
#define j
int m
Matrix1D< T > operator+(T op1) const
Definition: matrix1d.cpp:153
File cannot be open.
Definition: xmipp_error.h:137
void selfFLOOR()
Definition: matrix1d.cpp:499
void initConstant(std::vector< T > &V, T &value)
Definition: xmipp_funcs.h:390
void selfReverse()
Definition: matrix1d.cpp:736
int round(double x)
Definition: ap.cpp:7245
#define MATRIX1D_ARRAY(v)
Definition: matrix1d.h:58
Definition: ctf.h:38
void typeCast(const Matrix1D< T1 > &v1, Matrix1D< T2 > &v2)
Definition: matrix1d.h:1227
void edit()
Definition: matrix1d.cpp:823
void numericalDerivative(Matrix1D< double > &result) const
Definition: matrix1d.cpp:746
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458
void computeMeanAndStddev(double &mean, double &stddev) const
Definition: matrix1d.cpp:588
#define SWAP(a, b, tmp)
Definition: xmipp_macros.h:428
Matrix1D< T > sort() const
Definition: matrix1d.cpp:850
T * vdata
The array itself.
Definition: matrix1d.h:258
Incorrect type received.
Definition: xmipp_error.h:190
void operator/=(T op1)
Definition: matrix1d.cpp:247
bool operator==(const Matrix1D< T > &op1) const
Definition: matrix1d.cpp:46
void initConstant(T val)
Definition: matrix1d.cpp:83
Matrix1D< T > & operator=(const Matrix1D< T > &op1)
Definition: matrix1d.cpp:33
void operator*=(T op1)
Definition: matrix1d.cpp:231
void selfCEIL()
Definition: matrix1d.cpp:476
size_t vdim
Number of elements.
Definition: matrix1d.h:264
int minIndex() const
Definition: matrix1d.cpp:627
void initRandom(int length)