DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
utlCore.h
Go to the documentation of this file.
1 
18 #ifndef __utlCore_h
19 #define __utlCore_h
20 
21 
22 #include "utlCommandLineParser.h"
23 #include "utlSTDHeaders.h"
24 
25 #include "utlCoreMacro.h"
26 #include "utlSmartAssert.h"
27 
28 // Include OS-specific headers.
29 #if UTL_OS==1
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <unistd.h>
33 #elif UTL_OS==2
34 #include <windows.h>
35 #endif
36 
37 #include <iostream>
38 #include <fstream>
39 
40 #include <algorithm>
41 #include <utility>
42 #include <vector>
43 #include <cfloat>
44 #include <cstdlib>
45 #include <string>
46 #include <cstring>
47 #include <cassert>
48 #include <complex>
49 #include <cmath>
50 #include <limits>
51 #include <iomanip>
52 #include <ctime>
53 // #include <wordexp.h>
54 
55 #include "utlConstants.h"
56 #include "utlCoreMKL.h"
57 
58 #if __cplusplus >= 201103L
59 #include "utlCore11.h"
60 #endif
61 
65 namespace utl
66 {
67 
68 
70 
73 inline unsigned long
74 Time()
75 {
76 #if UTL_OS==1
77  struct timeval st_time;
78  gettimeofday(&st_time,0);
79  return (unsigned long)(st_time.tv_usec/1000 + st_time.tv_sec*1000);
80 #elif UTL_OS==2
81  SYSTEMTIME st_time;
82  GetSystemTime(&st_time);
83  return (unsigned long)(st_time.wMilliseconds + 1000*(st_time.wSecond + 60*(st_time.wMinute + 60*st_time.wHour)));
84 #else
85  return 0;
86 #endif
87 }
88 
90 inline unsigned long
91 TicToc(const bool is_tic, std::ostream& os=std::cout)
92 {
93  static unsigned long t0 = 0;
94  const unsigned long t = Time();
95  if (is_tic) return (t0 = t);
96  const unsigned long dt = t>=t0?(t - t0): std::numeric_limits<unsigned long>::max();
97  const unsigned int
98  edays = (unsigned int)(dt/86400000.0),
99  ehours = (unsigned int)((dt - edays*86400000.0)/3600000.0),
100  emin = (unsigned int)((dt - edays*86400000.0 - ehours*3600000.0)/60000.0),
101  esec = (unsigned int)((dt - edays*86400000.0 - ehours*3600000.0 - emin*60000.0)/1000.0),
102  ems = (unsigned int)(dt - edays*86400000.0 - ehours*3600000.0 - emin*60000.0 - esec*1000.0);
103  if (!edays && !ehours && !emin && !esec)
104  os << "[UTL] Elapsed time: " << ems << " ms" << std::endl << std::flush;
105  else {
106  if (!edays && !ehours && !emin)
107  os << "[UTL] Elapsed time: " << esec << " sec "<< ems << " ms" << std::endl << std::flush;
108  else {
109  if (!edays && !ehours)
110  os << "[UTL] Elapsed time: " << emin << " min " << esec << " sec "<< ems << " ms" << std::endl << std::flush;
111  else{
112  if (!edays)
113  os << "[UTL] Elapsed time: " << ehours << " hours "<< emin << " min " << esec << " sec "<< ems << "ms" << std::endl << std::flush;
114  else{
115  os << "[UTL] Elapsed time: " << edays << " dats " << ehours << " hours "<< emin << " min " << esec << " sec "<< ems << "ms" << std::endl << std::flush;
116  }
117  }
118  }
119  }
120  return t;
121 }
122 
124 
127 inline unsigned long
128 Tic(std::ostream& os=std::cout)
129 {
130  return TicToc(true, os);
131 }
132 
134 
137 inline unsigned long
138 Toc(std::ostream& os=std::cout)
139 {
140  return TicToc(false, os);
141 }
142 
143 template<class Parent>
145 {
146  template<class T>
147  class Help
148  {
149  bool is;
150 
151  public:
152  Help(const void* object) : is(false) { }
153  Help(const T* object) : is(true) { }
154 
155  operator bool() { return is; }
156  };
157 
159 
160 public:
161  template<class Object>
162  __InstanceOf(const Object& o) : is(Help<Parent>(&o)) { }
163 
164  operator bool() { return is; }
165 };
166 
167 template<class Parent, class Object>
168 inline bool
169 IsInstanceOf(const Object& o)
170 {
171  return __InstanceOf<Parent>(o);
172 }
173 
175 inline void
176 SwapBytes(void *ptr, const int sizePerElement, const int count)
177 {
178  char minibuf[32], *buffer=(char *)ptr;
179  for (int y=0; y<count; y++)
180  {
181  for (int x=0; x<sizePerElement; x++)
182  minibuf[x] = *(buffer++);
183  for (int x=0; x<sizePerElement; x++)
184  *(--buffer) = minibuf[x];
185  buffer+=sizePerElement;
186  }
187 }
188 
189 template <typename T>
190 inline T median_element(std::vector<T> values)
191 {
192  // values does not change after median
193  const unsigned int N = values.size();
194  std::nth_element(values.begin(),values.begin()+N/2,values.end());
195  return values[N/2];
196 }
197 
198 template <class T>
199 inline T min_element(const std::vector<T>& v)
200 {
201  return *std::min_element(v.begin(), v.end());
202 }
203 
204 template <class T>
205 inline T max_element(const std::vector<T>& v)
206 {
207  return *std::max_element(v.begin(), v.end());
208 }
209 
210 
211 template <class Iterator>
212 inline unsigned int argmin(Iterator i1, Iterator i2)
213 {
214  Iterator iter = std::min_element(i1, i2);
215  int n=0;
216  for ( Iterator ii = i1; ii!=iter; ++ii )
217  n++;
218  return n;
219 }
220 
221 template <class Iterator>
222 inline unsigned int argmax(Iterator i1, Iterator i2)
223 {
224  Iterator iter = std::max_element(i1, i2);
225  int n=0;
226  for ( Iterator ii = i1; ii!=iter; ++ii )
227  n++;
228  return n;
229 }
230 
232 template <class VectorType>
233 inline void
234 argmax2(const VectorType& vec, int size, int& index0, int& index1)
235 {
236  double max0 = -std::numeric_limits<double>::max();
237  double max1 = max0;
238  index0=0, index1=0;
239  for ( int i = 0; i < size; ++i )
240  {
241  if (vec[i]>max0)
242  {
243  max1 = max0;
244  index1 = index0;
245  max0 = vec[i];
246  index0 = i;
247  }
248  else if (vec[i] > max1)
249  {
250  max1 = vec[i];
251  index1 = i;
252  }
253  }
254 }
255 
257 template<typename T> inline const T& min(const T& a,const T& b) { return a<=b?a:b; }
259 template<typename T> inline const T& min(const T& a,const T& b,const T& c) { return min(min(a,b),c); }
261 template<typename T> inline const T& min(const T& a,const T& b,const T& c,const T& d) { return min(min(min(a,b),c),d); }
263 template<typename T> inline const T& max(const T& a,const T& b) { return a>=b?a:b; }
265 template<typename T> inline const T& max(const T& a,const T& b,const T& c) { return max(max(a,b),c); }
267 template<typename T> inline const T& max(const T& a,const T& b,const T& c,const T& d) { return max(max(a,b,c),d); }
268 
269 #define __utl_minmax(T0_, T1_, T2_) \
270  inline const T0_ min(const T1_ a, const T2_ b) {return a<=b?a:b; } \
271  inline const T0_ min(const T2_ a, const T1_ b) {return a<=b?a:b; } \
272  inline const T0_ max(const T1_ a, const T2_ b) {return a>=b?a:b; } \
273  inline const T0_ max(const T2_ a, const T1_ b) {return a>=b?a:b; }
274 
275 __utl_minmax(unsigned int, unsigned int, size_t);
276 __utl_minmax(int, unsigned int, int);
277 __utl_minmax(int, int, size_t);
278 __utl_minmax(double, double, float);
279 __utl_minmax(double, double, int);
280 __utl_minmax(double, float, int);
281 
282 #undef __utl_minmax
283 
285 template<typename T> inline int sign(const T& x) { return (x<0)?-1:(x==0?0:1); }
286 
287 template<typename T> inline T square(const T& x) { return x*x; }
288 
289 template<typename T> inline T cube(const T& x) { return x*x*x; }
290 
291 inline std::string
292 StringToLowerCase(const std::string& str)
293 {
294  std::string result(str);
295  for (std::string::iterator c = result.begin(); c != result.end(); ++c)
296  *c = tolower(*c);
297  return result;
298 }
299 
300 inline std::string
301 StringToUpperCase(const std::string& str)
302 {
303  std::string result(str);
304  for (std::string::iterator c = result.begin(); c != result.end(); ++c)
305  *c = toupper(*c);
306  return result;
307 }
308 
310 inline bool
311 StringCompareCaseIgnored(const std::string& str1, const std::string& str2)
312 {
313  return StringToLowerCase(str1)==StringToLowerCase(str2);
314 }
315 
316 // replace with as many times as it shows up in source.
317 // write the result into source.
318 inline void
319 ReplaceString(std::string& source, const char* replace, const char* with)
320 {
321  const char *src = source.c_str();
322  char *searchPos = const_cast<char *>(strstr(src,replace));
323 
324  // get out quick if string is not found
325  if (!searchPos)
326  {
327  return;
328  }
329 
330  // perform replacements until done
331  size_t replaceSize = strlen(replace);
332  // do while hangs if replaceSize is 0
333  if(replaceSize == 0)
334  {
335  return;
336  }
337  char *orig = strdup(src);
338  char *currentPos = orig;
339  searchPos = searchPos - src + orig;
340 
341  // initialize the result
342  source.erase(source.begin(),source.end());
343  do
344  {
345  *searchPos = '\0';
346  source += currentPos;
347  currentPos = searchPos + replaceSize;
348  // replace
349  source += with;
350  searchPos = strstr(currentPos,replace);
351  }
352  while (searchPos);
353 
354  // copy any trailing text
355  source += currentPos;
356  free(orig);
357 }
358 
360 inline std::string
361 ConvertToWindowsOutputPath(const char* path)
362 {
363  std::string ret;
364  // make it big enough for all of path and double quotes
365  ret.reserve(strlen(path)+3);
366  // put path into the string
367  ret.assign(path);
368  ret = path;
369  std::string::size_type pos = 0;
370  // first convert all of the slashes
371  while((pos = ret.find('/', pos)) != std::string::npos)
372  {
373  ret[pos] = '\\';
374  pos++;
375  }
376  // check for really small paths
377  if(ret.size() < 2)
378  {
379  return ret;
380  }
381  // now clean up a bit and remove double slashes
382  // Only if it is not the first position in the path which is a network
383  // path on windows
384  pos = 1; // start at position 1
385  if(ret[0] == '\"')
386  {
387  pos = 2; // if the string is already quoted then start at 2
388  if(ret.size() < 3)
389  {
390  return ret;
391  }
392  }
393  while((pos = ret.find("\\\\", pos)) != std::string::npos)
394  {
395  ret.erase(pos, 1);
396  }
397  // now double quote the path if it has spaces in it
398  // and is not already double quoted
399  if(ret.find(' ') != std::string::npos
400  && ret[0] != '\"')
401  {
402  ret.insert(static_cast<std::string::size_type>(0),
403  static_cast<std::string::size_type>(1), '\"');
404  ret.append(1, '\"');
405  }
406  return ret;
407 }
408 
410 inline std::string
411 ConvertToUnixOutputPath(const char* path)
412 {
413  std::string ret = path;
414 
415  // remove // except at the beginning might be a cygwin drive
416  std::string::size_type pos=1;
417  while((pos = ret.find("//", pos)) != std::string::npos)
418  {
419  ret.erase(pos, 1);
420  }
421  // escape spaces and () in the path
422  if(ret.find_first_of(" ") != std::string::npos)
423  {
424  std::string result = "";
425  char lastch = 1;
426  for(const char* ch = ret.c_str(); *ch != '\0'; ++ch)
427  {
428  // if it is already escaped then don't try to escape it again
429  if((*ch == ' ') && lastch != '\\')
430  {
431  result += '\\';
432  }
433  result += *ch;
434  lastch = *ch;
435  }
436  ret = result;
437  }
438  return ret;
439 }
440 
442 inline void ConvertToUnixSlashes(std::string& path)
443 {
444  const char* pathCString = path.c_str();
445  bool hasDoubleSlash = false;
446 
447  const char* pos0 = pathCString;
448  const char* pos1 = pathCString+1;
449  for (std::string::size_type pos = 0; *pos0; ++ pos )
450  {
451  // make sure we don't convert an escaped space to a unix slash
452  if ( *pos0 == '\\' && *pos1 != ' ' )
453  {
454  path[pos] = '/';
455  }
456 
457  // Also, reuse the loop to check for slash followed by another slash
458  if (*pos1 == '/' && *(pos1+1) == '/' && !hasDoubleSlash)
459  {
460 #ifdef _WIN32
461  // However, on windows if the first characters are both slashes,
462  // then keep them that way, so that network paths can be handled.
463  if ( pos > 0)
464  {
465  hasDoubleSlash = true;
466  }
467 #else
468  hasDoubleSlash = true;
469 #endif
470  }
471 
472  pos0 ++;
473  pos1 ++;
474  }
475 
476  if ( hasDoubleSlash )
477  {
478  ReplaceString(path, "//", "/");
479  }
480 
481  // remove any trailing slash
482  if(!path.empty())
483  {
484  // if there is a tilda ~ then replace it with HOME
485  pathCString = path.c_str();
486  if(pathCString[0] == '~' && (pathCString[1] == '/' || pathCString[1] == '\0'))
487  {
488  const char* homeEnv = getenv("HOME");
489  if (homeEnv)
490  {
491  path.replace(0,1,homeEnv);
492  }
493  }
494  // remove trailing slash if the path is more than
495  // a single /
496  pathCString = path.c_str();
497  if(path.size() > 1 && *(pathCString+(path.size()-1)) == '/')
498  {
499  // if it is c:/ then do not remove the trailing slash
500  if(!((path.size() == 3 && pathCString[1] == ':')))
501  {
502  path = path.substr(0, path.size()-1);
503  }
504  }
505  }
506 }
507 
508 inline std::string
509 CreateExpandedPath(const std::string& path)
510 {
511  // std::string result;
512  // wordexp_t exp_result;
513  // wordexp(path.c_str(), &exp_result, 0);
514  // result.assign(exp_result.we_wordv[0]);
515  // wordfree(&exp_result);
516 
517  std::string result(path);
518  ConvertToUnixSlashes(result);
519 
520 #ifdef _WIN32
521  return ConvertToWindowsOutputPath(result.c_str());
522 #else
523  return ConvertToUnixOutputPath(result.c_str());
524 #endif
525 }
526 
527 
528 inline bool
529 IsFileExist ( const std::string& file )
530 {
531  std::ifstream ii(file.c_str());
532  return ii.good();
533 }
534 
536 inline bool
537 IsEndingWith(const std::string& fullString, const std::string& ending )
538 {
539  if (fullString.length() >= ending.length())
540  return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
541  else
542  return false;
543 }
544 
549 inline void
550 GetPath(const std::string& fileNameAbsolute, std::string& path, std::string& file)
551 {
552  size_t found = fileNameAbsolute.find_last_of("/\\");
553  path = fileNameAbsolute.substr( 0, found + 1);
554  file = fileNameAbsolute.substr( found + 1);
555 }
556 
558 inline void
559 GetFileExtension(const std::string& fileNameAbsolute, std::string& ext, std::string& fileNoExt)
560 {
561  size_t found = fileNameAbsolute.find_last_of(".");
562  ext = fileNameAbsolute.substr(found + 1);
563  fileNoExt = fileNameAbsolute.substr(0,found);
564  // utlAssert(ext!=fileNoExt, "Logical Error! There is no ext! ext="<<ext <<", fileNoExt="<<fileNoExt);
565  if (ext==fileNoExt)
566  {
567  // no ".", no extension, ext is empty
568  ext = "";
569  }
570  else if (ext=="gz")
571  {
572  size_t found2 = fileNoExt.find_last_of(".");
573  std::string ext2 = fileNoExt.substr(found2 + 1);
574  if (ext2=="nii")
575  {
576  ext = "nii.gz";
577  fileNoExt = fileNoExt.substr(0,found2);
578  }
579  }
580 }
581 
585 inline std::string
586 ZeroPad(const unsigned int number, const unsigned int paddedLength)
587 {
588  std::stringstream padded;
589  padded << std::setfill('0') << std::setw(paddedLength) << number;
590  return padded.str();
591 }
592 
594 inline std::string
595 GetSequentialFileName(const std::string& filePrefix, const unsigned int iteration, const std::string& fileExtension, const unsigned int paddedLength=6)
596 {
597  std::stringstream padded;
598  padded << filePrefix << "_" << ZeroPad(iteration, paddedLength) << "." << fileExtension;
599  return padded.str();
600 }
601 
603 template <size_t M >
604 inline std::vector<std::string>
605 CovertChar2DArrayToStringArray(const char arr[][M], int N)
606 {
607  std::vector<std::string> vec;
608  for ( int i = 0; i < N; ++i )
609  {
610  std::string ss(arr[i]);
611  if (!ss.empty())
612  vec.push_back(ss);
613  }
614  return vec;
615 }
616 
617 template <size_t M >
618 inline void
619 CovertStringArrayToChar2DArray(const std::vector<std::string>& vec, char arr[][M], int N)
620 {
621  for ( int i = 0; i < N; ++i )
622  {
623  if (i<vec.size())
624  {
625  strcpy(arr[i], vec[i].c_str());
626  }
627  else
628  {
629  strcpy(arr[i], "");
630  }
631  }
632 }
633 
635 template<class VectorType>
636 inline VectorType
637 NormalizeMinMax(const VectorType & v, const int nSize)
638 {
639  VectorType norm(v);
642 
643  for(unsigned int i = 0; i < nSize; i++)
644  {
645  if(v[i] > max)
646  max = v[i];
647  if(v[i] < min)
648  min = v[i];
649  }
650 
651  for(unsigned int i = 0; i < v.size(); i++)
652  {
653  if((max-min) != 0)
654  norm[i] = ((v[i]-min)/(max-min));
655  else
656  norm[i] = 0;
657  }
658 
659  return norm;
660 }
661 
662 template<typename T>
663 inline std::vector<T>
664 NormalizeMinMax(std::vector<T> & v)
665 {
666  return NormalizeMinMax<std::vector<T> >(v, v.size());
667 }
668 
670 template<class VectorType>
671 inline VectorType
672 NormalizeUnitNorm(const VectorType & v, const int nSize)
673 {
674  VectorType v1(v);
675  double norm = 0.0;
676  for(unsigned int i = 0; i < nSize; i++)
677  norm += v1[i]*v1[i];
678  if (norm>1e-20)
679  {
680  double normsqrt = std::sqrt(norm);
681  for ( int i = 0; i < v.size(); i += 1 )
682  v1[i] /= normsqrt;
683  }
684  return v1;
685 }
686 
687 template<typename T>
688 inline std::vector<T>
689 NormalizeUnitNorm(const std::vector<T> & v)
690 {
691  return NormalizeUnitNorm<std::vector<T> >(v, v.size());
692 }
693 
695 template<class VectorType>
696 inline VectorType
697 NormalizeMax(const VectorType & v, const int nSize)
698 {
699  VectorType norm(v);
701 
702  for(unsigned int i = 0; i < nSize; i++)
703  {
704  if(v[i] > max)
705  max = v[i];
706  }
707 
708  for(unsigned int i = 0; i < nSize; i++)
709  {
710  if( max != 0)
711  norm[i] = v[i]/max;
712  }
713 
714  return norm;
715 }
716 
717 template<typename T>
718 inline std::vector<T>
719 NormalizeMax(const std::vector<T> & v)
720 {
721  return NormalizeMax<std::vector<T> >(v, v.size());
722 }
723 
725 inline bool
726 IsNumber( const std::string& input)
727 {
728  if (input.size()==0)
729  return false;
730  char *pend;
731  double number = strtod(input.c_str(),&pend);
732  if (*pend!='\0')
733  return false;
734  return true;
735 }
736 
738 template <class T>
739 inline std::string
740 ConvertNumberToString ( const T value, const int precision=6 )
741 {
742  std::ostringstream strs;
743  strs.precision(precision);
744  strs << value;
745  return strs.str();
746 }
747 
749 template <class T>
750 inline T
751 ConvertStringToNumber ( const std::string& input )
752 {
753  // // The first way also works
754  // utlException(input.size()==0, input << " is null");
755  // char *pend;
756  // T result = strtod(input.c_str(),&pend);
757  // utlException(*pend!='\0', input << " is not a number");
758  // return result;
759 
760  // the second way
761  std::istringstream ss(input);
762  T result;
763  ss >> result;
764  if (!ss)
765  utlException(true, "input="<<input);
766  return result;
767 }
768 
769 template <class VectorType>
770 std::string
771 ConvertVectorToString ( VectorType vec, const int N, const char* separate=" " )
772 {
773  std::ostringstream ss;
774  for ( int i = 0; i < N; ++i )
775  {
776  ss << vec[i];
777  if (i!=N-1)
778  ss << separate;
779  }
780  return ss.str();
781 }
782 
783 template <class T>
784 inline int
785 RoundNumber ( const T x )
786 {
787  return x>=0 ? std::floor(x+0.5) : std::ceil(x-0.5);
788 }
789 
790 
791 inline bool
792 IsInt( const std::string& input, const double epss=1e-10)
793 {
794  if (input.size()==0)
795  return false;
796  char *pend;
797  double dd = strtod(input.c_str(),&pend);
798  if (*pend!='\0')
799  return false;
800  double diff = dd - (long)(dd);
801  return diff<epss && diff>-epss;
802 }
803 
804 inline bool
805 IsInt( const double dd, const double epss=1e-10)
806 {
807  double diff = dd - (long)(dd);
808  return diff<epss && diff>-epss;
809 }
810 
811 inline bool
812 IsInt( const float dd, const double epss=1e-10)
813 {
814  double diff = dd - (long)(dd);
815  return diff<epss && diff>-epss;
816 }
817 
818 inline bool
819 IsEven(const int value)
820 {
821  return (value % 2 == 0);
822 }
823 
824 inline
825 bool IsOdd(const int value)
826 {
827  // NOTE: use value%2==1 is wrong, because it can be -1.
828  return !IsEven(value);
829 }
830 
831 template <class VectorType>
832 inline bool
833 IsContainsNaN(const VectorType& a, const int size)
834 {
835  for(unsigned int i = 0; i < size; ++i)
836  {
837  if(std::isnan(a[i]))
838  return true;
839  }
840  return false;
841 }
842 
844 inline void
845 SplitString (const std::string& str, std::vector<std::string>& strVec, const char* cc=" ")
846 {
847  strVec.clear();
848  char str2[str.size()+1];
849  for ( int i = 0; i < str.size(); i += 1 )
850  str2[i] = str[i];
851  str2[str.size()]='\0';
852  char *pch = strtok(str2, cc);
853  while (pch)
854  {
855  strVec.push_back(std::string(pch));
856  pch = strtok(NULL, cc);
857  }
858 }
859 
861 inline void
862 ReadLines ( const std::string& filename, std::vector < std::vector<std::string> >& strVec, const char* cc=" ")
863 {
864  strVec.clear();
865  std::string line;
866 
867  std::ifstream infile(filename.c_str(), std::ios_base::in);
868  utlGlobalAssert(infile, "failed to open input file " << filename);
869  while (std::getline(infile, line, '\n'))
870  {
871  std::vector<std::string> tmp;
872  SplitString(line, tmp, cc);
873  // char *pch = strtok((char *)line.c_str(), cc);
874  // while (pch)
875  // {
876  // tmp.push_back(std::string(pch));
877  // pch = strtok(NULL, cc);
878  // }
879  // do not add blank lines
880  if (tmp.size()>0)
881  strVec.push_back(tmp);
882  }
883 
884  // std::cout << "read " << strVec.size() << " lines from " << filename << ".\n";
885  infile.close();
886 }
887 
889 inline void
890 ReadLinesFirstlineCheck ( const std::string& filename, std::vector < std::vector<std::string> >& strVec, const char* cc=" " )
891 {
892  ReadLines(filename, strVec, cc);
893  if (strVec.size()>0 && strVec[0].size()==1 && IsInt(strVec[0][0]))
894  {
895  int nSize;
896  std::istringstream ( strVec[0][0] ) >> nSize;
897  if (nSize+1==strVec.size())
898  {
899  std::vector<std::vector<std::string> > strVec_back(strVec);
900  strVec.clear();
901  for ( int i = 1; i < strVec_back.size(); i += 1 )
902  strVec.push_back(strVec_back[i]);
903  }
904  }
905 }
906 
907 template <class TVectorType>
908 inline double
909 GetSumOfVector( const TVectorType& vec, const int NSize)
910 {
911  double sumTemp=0;
912  for ( int i = 0; i < NSize; i += 1 )
913  sumTemp += vec[i];
914  return sumTemp;
915 }
916 
917 
919 template <class IteratorType>
920 inline std::vector<double>
921 GetContainerStats ( IteratorType v1, IteratorType v2 )
922 {
923  if (v1==v2)
924  return std::vector<double>(4,0);
925 
926  double m = *v1, M = m;
927  double S = 0, S2 = 0;
928  int n=0;
929  for ( IteratorType ptr=v1; ptr!=v2; ++ptr )
930  {
931  const double val = *ptr;
932  if (val<m)
933  m = val;
934  if (val>M)
935  M = val;
936  S += val;
937  S2 += val*val;
938  n++;
939  }
940  double mean = S/(double)n;
941  double variance = n>1?(S2 - S*S/n)/(n-1):0;
942  variance = std::sqrt(variance);
943 
944  std::vector<double> result(4);
945  result[0]=m, result[1]=M, result[2]=mean, result[3]=variance;
946  return result;
947 }
948 
949 template <>
950 inline std::vector<double>
951 GetContainerStats<const std::complex<double>*> ( const std::complex<double>* v1, const std::complex<double>* v2 )
952 {
953  return std::vector<double>(4,0);
954 }
955 
956 template <>
957 inline std::vector<double>
958 GetContainerStats<std::complex<double>*> ( std::complex<double>* v1, std::complex<double>* v2 )
959 {
960  return std::vector<double>(4,0);
961 }
962 
963 template <>
964 inline std::vector<double>
965 GetContainerStats<std::complex<float>*> ( std::complex<float>* v1, std::complex<float>* v2 )
966 {
967  return std::vector<double>(4,0);
968 }
969 
970 template <>
971 inline std::vector<double>
972 GetContainerStats<const std::complex<float>*> ( const std::complex<float>* v1, const std::complex<float>* v2 )
973 {
974  return std::vector<double>(4,0);
975 }
976 
977 template <>
978 inline std::vector<double>
979 GetContainerStats<const std::string*> ( const std::string* v1, const std::string* v2 )
980 {
981  return std::vector<double>(4,0);
982 }
983 
984 template <class IteratorType>
985 int
986 GetNumberOfNonZeroValues ( IteratorType v, IteratorType v2, const double threshold=1e-6 )
987 {
988  if (v==v2)
989  return 0;
990 
991  int nnz = 0;
992  for ( IteratorType ptr=v; ptr != v2; ++ptr )
993  {
994  if (std::abs(*ptr)>threshold)
995  nnz++;
996  }
997  return nnz;
998 }
999 
1000 template <typename T>
1001 inline void
1002 PrintVector ( const std::vector<T>& vec, const std::string& str="", const char* separate=" ", std::ostream& os=std::cout, bool showStats=true)
1003 {
1004  char tmp[1024];
1005  if (showStats)
1006  {
1007  std::vector<double> st = GetContainerStats(vec.begin(), vec.end());
1008  sprintf(tmp, "%-8s(%p): size = %lu, stat = { %g, %g [%g], %g } : ", str==""?"vector":str.c_str(), &vec, vec.size(), st[0], st[2], st[3], st[1] );
1009  }
1010  else
1011  sprintf(tmp, "%-8s(%p): size = %lu : ", str==""?"vector":str.c_str(), &vec, vec.size() );
1012  std::string strr(tmp);
1013  if (vec.size()>0)
1014  {
1015  os << strr << " = [ ";
1016  for ( int i = 0; i < vec.size(); i += 1 )
1017  os << vec[i] << separate;
1018  os << "];\n" << std::flush;
1019  }
1020  else
1021  os << strr << " is an empty vector" << std::endl;
1022 }
1023 
1024 template <>
1025 inline void
1026 PrintVector<std::string> ( const std::vector<std::string>& vec, const std::string& str, const char* separate, std::ostream& os, bool showStats)
1027 {
1028  char tmp[1024];
1029  sprintf(tmp, "%-8s(%p): size = %lu, : ", str==""?"vector":str.c_str(), &vec, vec.size());
1030  std::string strr=tmp;
1031  if (vec.size()>0)
1032  {
1033  os << strr << " = [ ";
1034  for ( int i = 0; i < vec.size(); i += 1 )
1035  os << vec[i] << separate;
1036  os << "];\n" << std::flush;
1037  }
1038  else
1039  os << strr << " is an empty vector" << std::endl;
1040 }
1041 
1042 template <class VectorType>
1043 inline void
1044 PrintVector ( const VectorType& vec, const int NSize, const std::string& str="", const char* separate=" ", std::ostream& os=std::cout, bool showStats=true)
1045 {
1046  char tmp[1024];
1047  if (showStats)
1048  {
1049  std::vector<double> st = GetContainerStats(&vec[0], &vec[NSize]);
1050  sprintf(tmp, "%-8s(%p): size = %lu, stat = { %g, %g [%g], %g } : ", str==""?"vector":str.c_str(), &vec, (long unsigned int)NSize, st[0], st[2], st[3], st[1] );
1051  }
1052  else
1053  sprintf(tmp, "%-8s(%p): size = %lu : ", str==""?"vector":str.c_str(), &vec, (long unsigned int)NSize );
1054  std::string strr(tmp);
1055  if (NSize>0)
1056  {
1057  os << strr << " = [ ";
1058  for ( int i = 0; i < NSize; i += 1 )
1059  os << vec[i] << separate;
1060  os << "];\n" << std::flush;
1061  }
1062  else
1063  os << strr << " is an empty vector" << std::endl;
1064 }
1065 
1066 template <class IteratorType>
1067 inline void
1068 PrintContainer ( IteratorType v1, IteratorType v2, const std::string& str="", const char* separate=" ", std::ostream& os=std::cout, bool showStats=true)
1069 {
1070  long unsigned int NSize=0;
1071  IteratorType q = v1;
1072  for ( ; q!=v2; ++q )
1073  NSize++;
1074 
1075  char tmp[1024];
1076  if (showStats)
1077  {
1078  std::vector<double> st = GetContainerStats(v1, v2);
1079  sprintf(tmp, "%-8s(%p): size = %lu, stat = { %g, %g [%g], %g } : ", str==""?"vector":str.c_str(), v1, NSize, st[0], st[2], st[3], st[1] );
1080  }
1081  else
1082  sprintf(tmp, "%-8s(%p): size = %lu : ", str==""?"vector":str.c_str(), v1, NSize );
1083 
1084  std::string strr(tmp);
1085  if (v1!=v2)
1086  {
1087  os << strr << " = [ ";
1088  IteratorType q = v1;
1089  for ( ; q!=v2; ++q )
1090  os << (*q) << separate;
1091  os << "];\n" << std::flush;
1092  }
1093  else
1094  os << strr << " is an empty container" << std::endl;
1095 }
1096 
1097 inline int
1098 strfind(const char *s,const char c)
1099 {
1100  if (s)
1101  {
1102  int l;
1103  for (l=0; l<=strlen(s) && s[l]!=c; l++)
1104  ;
1105  if (l==strlen(s)+1)
1106  l = -1;
1107  return l;
1108  }
1109  return -1;
1110 }
1111 
1112 // to set std::vector<T> with variable length
1113 template <typename T>
1114  int
1115 SetVector(const char *s, std::vector<T>& vec, const int least_num=0, const char& c=',')
1116 {
1117  vec.clear();
1118  if (strlen(s)==0)
1119  return vec.size();
1120  if (s)
1121  {
1122  // std::cout << "strlen(s) = " << strlen(s) << std::endl;
1123  unsigned int l;
1124  char temp[1024];
1125  int k = 0;
1126  for (l=0; l<=strlen(s); l++)
1127  {
1128  if (s[l]==c || s[l]=='\0')
1129  {
1130  temp[k]='\0';
1131  k = 0;
1132  T ftmp = (T)atof(temp);
1133  vec.push_back(ftmp);
1134  }
1135  else
1136  {
1137  temp[k] = s[l];
1138  k++;
1139  }
1140  }
1141  if (l==strlen(s)+1)
1142  l = -1;
1143  utlAssert(least_num==0 || vec.size()>=least_num, "no enough numbers were imported in vector");
1144  return l;
1145  }
1146  utlAssert(least_num==0 || vec.size()>=least_num, "no enough numbers were imported in vector");
1147  return -1;
1148 }
1149 
1150 template <typename T>
1151  int
1152 SetVector(const std::string& s, std::vector<T>& vec, const int least_num=0, const char& c=',')
1153 {
1154  return SetVector(s.c_str(), vec, least_num, c);
1155 }
1156 
1157 template < class T >
1158 inline void
1159 ReadVector ( const std::string& vectorStr, std::vector<T>& vec, const char* cc=" ")
1160 {
1161  utlException(vectorStr=="", "need to set vectorStr");
1162  vec.clear();
1163 
1164  std::vector<std::vector<std::string> > strVec;
1165  ReadLinesFirstlineCheck(vectorStr, strVec, cc);
1166  utlException(strVec.size()==0, "wrong file");
1167 
1168  bool isRowVector = strVec.size()==1;
1169  if (isRowVector)
1170  {
1171  for ( int i = 0; i < strVec[0].size(); i += 1 )
1172  vec.push_back(ConvertStringToNumber<T>(strVec[0][i]));
1173  }
1174  else
1175  {
1176  for ( int i = 0; i < strVec.size(); i += 1 )
1177  {
1178  utlException(strVec[i].size()!=1, "should keep one float number in one line");
1179  vec.push_back(ConvertStringToNumber<T>(strVec[i][0]));
1180  }
1181  }
1182 }
1183 
1184 template <>
1185 inline void
1186 ReadVector<std::string> ( const std::string& vectorStr, std::vector<std::string>& vec, const char* cc)
1187 {
1188  utlException(vectorStr=="", "need to set vectorStr");
1189  vec.clear();
1190 
1191  std::vector<std::vector<std::string> > strVec;
1192  ReadLinesFirstlineCheck(vectorStr, strVec, cc);
1193  utlException(strVec.size()==0, "wrong file");
1194 
1195  bool isRowVector = strVec.size()==1;
1196  if (isRowVector)
1197  {
1198  for ( int i = 0; i < strVec[0].size(); i += 1 )
1199  vec.push_back(strVec[0][i]);
1200  }
1201  else
1202  {
1203  for ( int i = 0; i < strVec.size(); i += 1 )
1204  {
1205  utlException(strVec[i].size()!=1, "should keep one float number in one line");
1206  vec.push_back(strVec[i][0]);
1207  }
1208  }
1209 }
1210 
1211 template <typename VectorType>
1212 void
1213 SaveVector ( const VectorType& vv, const int NSize, const std::string& vectorStr, const bool is_save_number=false)
1214 {
1215  std::ofstream out; // create ofstream object
1216  out.open ( vectorStr.c_str() ); // open ofstream
1217  utlException(!out, "\nERROR : failed to open output file " << vectorStr );
1218 
1219  if (is_save_number)
1220  out << NSize << "\n";
1221  for ( int i = 0; i < NSize; i += 1 )
1222  out << vv[i] << "\n";
1223  out.close();
1224 }
1225 
1226 template <typename T>
1227 void
1228 SaveVector ( const std::vector<T>& vv, const std::string& vectorStr, const bool is_save_number=false)
1229 {
1230  int NSize = vv.size();
1231  SaveVector<std::vector<T> >(vv, NSize, vectorStr, is_save_number);
1232 }
1233 
1234 template <typename T>
1235 inline std::vector<int>
1236 FindVector ( const std::vector<T>& vec, const T& elem, const double gap=M_EPS )
1237 {
1238  std::vector<int> index;
1239  for ( int i = 0; i < vec.size(); i += 1 )
1240  {
1241  if (std::fabs(elem-vec[i])<gap)
1242  index.push_back(i);
1243  }
1244  return index;
1245 }
1246 
1247 template <>
1248 inline std::vector<int>
1249 FindVector<std::string> ( const std::vector<std::string>& vec, const std::string& elem, const double )
1250 {
1251  std::vector<int> index;
1252  for ( int i = 0; i < vec.size(); i += 1 )
1253  {
1254  if (elem==vec[i])
1255  index.push_back(i);
1256  }
1257  return index;
1258 }
1259 
1260 template <typename T>
1261 std::vector<std::vector<int> >
1262 SeparateVector ( const std::vector<T>& vec, std::vector<T>& vec_sep, const double gap=M_EPS )
1263 {
1264  std::vector<std::vector<int> > index;
1265  vec_sep.clear();
1266 
1267  for ( int i = 0; i < vec.size(); i += 1 )
1268  {
1269  bool is_test = false;
1270  for ( int i_ind = 0; i_ind < index.size(); i_ind += 1 )
1271  {
1272  for ( int j_ind = 0; j_ind < index[i_ind].size(); j_ind += 1 )
1273  {
1274  if (i==index[i_ind][j_ind])
1275  {
1276  is_test = true;
1277  break;
1278  }
1279  }
1280  if (is_test)
1281  break;
1282  }
1283 
1284  if (!is_test)
1285  {
1286  vec_sep.push_back(vec[i]);
1287  std::vector<int> index_tmp = FindVector(vec, vec[i], gap);
1288  index.push_back(index_tmp);
1289  }
1290  }
1291 
1292  for ( int m = 0; m < index.size(); m += 1 )
1293  {
1294  T sum = 0.0;
1295  for ( int n = 0; n < index[m].size(); n += 1 )
1296  sum += vec[index[m][n]];
1297  vec_sep[m] = sum/(double)index[m].size();
1298  }
1299  return index;
1300 }
1301 
1302 template <typename T>
1303 void
1304 ConnectVector ( std::vector<T>& vec1, const std::vector<T>& vec2)
1305 {
1306  for ( int i = 0; i < vec2.size(); i += 1 )
1307  vec1.push_back(vec2[i]);
1308 }
1309 
1310 template <typename T>
1311 std::vector<T>
1312 SelectVector ( const std::vector<T>& vec, const std::vector<int>& index )
1313 {
1314  std::vector<T> select;
1315  for ( int i = 0; i < index.size(); i += 1 )
1316  {
1317  select.push_back( vec[index[i]] );
1318  }
1319  return select;
1320 }
1321 
1322 template <typename T>
1323 std::vector<T>
1324 SelectVector ( const std::vector<T>& vec, const int startIndex, const int numberOfElement )
1325 {
1326  std::vector<T> select;
1327  for ( int i = 0; i < numberOfElement; i += 1 )
1328  {
1329  if (startIndex+i<vec.size())
1330  select.push_back( vec[startIndex+i] );
1331  }
1332  return select;
1333 }
1334 
1335 template <class T>
1336 inline bool
1337 IsSame ( const T& value, const T& v0, const double eps=1e-10 )
1338 {
1339  return std::fabs(value-v0)<eps;
1340 }
1341 
1342 template <>
1343 inline bool
1344 IsSame<int>(const int& value, const int& v0, const double)
1345 {
1346  return value==v0;
1347 }
1348 
1349 template <>
1350 inline bool
1351 IsSame<std::string>(const std::string& value, const std::string& v0, const double)
1352 {
1353  return value==v0;
1354 }
1355 
1356 template <>
1357 inline bool
1358 IsSame<char>(const char& value, const char& v0, const double)
1359 {
1360  return value==v0;
1361 }
1362 
1363 template <class T>
1364 inline bool
1365 IsSameVector(const std::vector<T>& vec1, const std::vector<T>& vec2, const double eps=1e-10)
1366 {
1367  if (vec1.size()!=vec2.size())
1368  return false;
1369  for ( int i = 0; i < vec1.size(); ++i )
1370  {
1371  if (!IsSame<T>(vec1[i], vec2[i], eps))
1372  return false;
1373  }
1374  return true;
1375 }
1376 
1377 
1378 template <class T, size_t N1, size_t N2 >
1379 inline bool
1380 IsSameArray (const T (&a1)[N1], const T (&a2)[N2], const double eps=1e-10 )
1381 {
1382  if (N1!=N2)
1383  return false;
1384  for ( int i = 0; i < N1; ++i )
1385  {
1386  if (!IsSame<T>(a1[i], a2[i], eps))
1387  return false;
1388  }
1389  return true;
1390 }
1391 
1392 template <class VectorType, class T>
1393 inline bool
1394 IsInVector ( const VectorType& vec, const int size, const T& num, const double eps=1e-10 )
1395 {
1396  for ( int i = 0; i < size; i += 1 )
1397  {
1398  if (IsSame<T>(vec[i], num, eps))
1399  return true;
1400  }
1401  return false;
1402 }
1403 
1404 template <typename T>
1405 inline bool
1406 IsInVector ( const std::vector<T>& vec, const T& num, const double eps=1e-10 )
1407 {
1408  return IsInVector<std::vector<T>, T>(vec, vec.size(), num, eps);
1409 }
1410 
1411 
1413 template < typename T >
1414 inline T
1415 Random( const T d1 = T(0.0), const T d2 = T(1.0) )
1416 {
1417  utlAssert(d2>d1, "wrong setting");
1418  static bool first_time = true;
1419  if (first_time)
1420  {
1421  std::srand(std::time(NULL));
1422  first_time = false;
1423  }
1424  T ra = 1e-10 + (1-2e-10) * std::rand() / RAND_MAX;
1425  return (d2 - d1)*ra + d1;
1426 } // ----- end random -----
1427 
1429 inline long
1430 RandomInt( const long d1 = 0, const long d2 = 1 )
1431 {
1432  if (d2==d1)
1433  return d1;
1434  utlAssert(d2>d1, "wrong setting, d1="<<d1 << ", d2="<<d2);
1435  static bool first_time = true;
1436  if (first_time)
1437  {
1438  std::srand(std::time(NULL));
1439  first_time = false;
1440  }
1441  int tmp = d2-d1;
1442  int rand_tmp = std::rand() % (tmp+1);
1443  return rand_tmp + d1;
1444 } // ----- end random -----
1445 
1446 template < typename T >
1447 inline std::vector<T>
1448 RandomVec( const std::vector<T>& vec, const int num, std::vector<int>& result_index)
1449 {
1450  utlAssert(vec.size()>num && num>0, "wrong size, vec.size()="<<vec.size() << ", num="<<num);
1451  result_index.clear();
1452  std::vector<T> result;
1453  static bool first_time = true;
1454  if (first_time)
1455  {
1456  std::srand(std::time(NULL));
1457  first_time = false;
1458  }
1459  for ( int nn = 0; nn < num; nn += 1 )
1460  {
1461  int i = RandomInt(0, vec.size()-num+nn);
1462  bool is_choose = false;
1463  for ( int n_i = 0; n_i < result_index.size(); n_i += 1 )
1464  {
1465  if (i==result_index[n_i])
1466  {
1467  is_choose=true;
1468  break;
1469  }
1470  }
1471  if (!is_choose)
1472  {
1473  result_index.push_back(i);
1474  result.push_back(vec[i]);
1475  }
1476  else
1477  {
1478  result_index.push_back(vec.size()-num+nn);
1479  result.push_back(vec[vec.size()-num+nn]);
1480  }
1481  }
1482  return result;
1483 } // ----- end random -----
1484 
1488 inline std::vector<double>
1489 RandomPointInSphere ( const bool hemis )
1490 {
1491  std::vector<double> result(3);
1492  double u = hemis ? Random(0.0,1.0) : Random(-1.0,1.0); // hemisphere
1493  double theta = Random(0.0,2*M_PI); // hemisphere
1494  result[0] = std::sqrt(1-u*u)*std::cos(theta);
1495  result[1] = std::sqrt(1-u*u)*std::sin(theta);
1496  result[2] = u;
1497  return result;
1498 }
1499 
1502 template < typename T >
1503 inline T
1504 GaussRand ( const T value, const double sigma )
1505 {
1506  if (sigma<=0)
1507  return value;
1508  static bool first_time = true;
1509  if (first_time)
1510  {
1511  std::srand(std::time(NULL));
1512  first_time = false;
1513  }
1514  T A;
1515  T U1,U2;
1516  U1 = 1e-10 + (1-2e-10) * std::rand() / RAND_MAX;
1517  U2 = 1e-10 + (1-2e-10) * std::rand() / RAND_MAX;
1518  A = std::sqrt(-2*std::log(U1))*std::cos(2*M_PI*U2);
1519  return (value + sigma*A);
1520 } // ----- end rician_rand -----
1521 
1524 template < typename T >
1525 inline T
1526 RicianRand ( const T value, const double sigma )
1527 {
1528  if (sigma<=0)
1529  return value;
1530  static bool first_time = true;
1531  if (first_time)
1532  {
1533  std::srand(std::time(NULL));
1534  first_time = false;
1535  }
1536 
1537  T real_part = value;
1538  T imag_part = 0;
1539 
1540  T n1 = GaussRand(T(0.0),sigma);
1541  T n2 = GaussRand(T(0.0),sigma);
1542 
1543  T noise_real = real_part + n1;
1544  T noise_imag = imag_part + n2;
1545 
1546  T noise = std::sqrt(noise_real*noise_real + noise_imag*noise_imag);
1547  return noise;
1548 } // ----- end rician_rand -----
1549 
1550 template < typename VectorType>
1551 inline VectorType
1552 AddNoise( const VectorType& signal, const int size, const double sigma, const bool is_rician=true)
1553 {
1554  if (sigma<=0)
1555  return signal;
1556 
1557  VectorType result(signal);
1558  for ( int i = 0; i < size; i += 1 )
1559  {
1560  if (is_rician)
1561  result[i] = RicianRand(signal[i],sigma);
1562  else
1563  result[i] = GaussRand(signal[i],sigma);
1564  }
1565  return result;
1566 }
1567 
1568 template < typename T >
1569 inline std::vector<T>
1570 AddNoise( const std::vector<T>& signal, const double sigma, const bool is_rician=true)
1571 {
1572  return AddNoise(signal, signal.size(), sigma, is_rician);
1573 }
1574 
1575 
1577 template < typename T >
1578 inline void
1579 cartesian2Spherical( const T x, const T y, const T z, T& r, T & theta, T & phi)
1580 {
1581  r = sqrt(x*x + y*y + z*z);
1582  if ( r >= M_EPS )
1583  {
1584  theta = std::acos( z / r );
1585  phi = std::atan2( y, x );
1586  }
1587  else
1588  theta = phi = 0.0;
1589 }
1590 
1591 template < typename T >
1592 inline void
1593 cartesian2Spherical( T& x, T& y, T& z)
1594 {
1595  T x1=x, y1=y, z1=z;
1596  cartesian2Spherical(x1,y1,z1, x,y,z);
1597 }
1598 
1600 template < typename T >
1601 inline void
1602 spherical2Cartesian( const T r, const T theta, const T phi, T& x, T & y, T & z)
1603 {
1604  x = r * std::sin(theta) * std::cos(phi);
1605  y = r * std::sin(theta) * std::sin(phi);
1606  z = r * std::cos(theta);
1607 }
1608 
1609 template < typename T >
1610 inline void
1611 spherical2Cartesian( T& x, T& y, T& z)
1612 {
1613  T x1=x, y1=y, z1=z;
1614  spherical2Cartesian(x1,y1,z1, x,y,z);
1615 }
1616 
1617 template <class IteratorType>
1618 inline void
1619 PowerVector ( IteratorType v1, IteratorType v2, const double poww )
1620 {
1621  for ( IteratorType ptr=v1; ptr != v2; ++ptr )
1622  *ptr = std::pow(*ptr, poww);
1623 }
1624 
1625 template <class VectorType>
1626 inline VectorType
1627 GetVectorLinspace( const double valueMin, const double valueMax, const int num )
1628 {
1629  VectorType result(num);
1630  if (num==1)
1631  result[0] = valueMin;
1632  else
1633  {
1634  double step = (valueMax-valueMin)/(double)(num-1);
1635  for ( int i = 0; i < num; i += 1 )
1636  result[i] = valueMin+step*i;
1637  }
1638  return result;
1639 }
1640 
1641 inline std::vector<int>
1642 GetRange ( const int start, const int end, const int space=1)
1643 {
1644  std::vector<int> result;
1645  int val = start;
1646  do
1647  {
1648  result.push_back(val);
1649  val += space;
1650  } while ( val < end );
1651  return result;
1652 }
1653 
1654 template <class VectorType>
1655 inline void
1656 VectorShrinkage( VectorType& vec, const int N, const double kappa )
1657 {
1658  for ( int i = 0; i < N; i += 1 )
1659  vec[i] = (vec[i]>=kappa) ? (vec[i]-kappa) : ( (vec[i]<=-kappa) ? (vec[i]+kappa) : 0.0 );
1660 }
1661 
1662 template <class VectorType>
1663 inline VectorType
1664 GetVectorShrinkage( const VectorType& vec, const int N, const double kappa )
1665 {
1666  VectorType result(vec);
1667  VectorShrinkage(result, N, kappa);
1668  return result;
1669 }
1670 
1671 template <class VectorType>
1672 inline void
1673 AbsoluteVector( VectorType& vec, const int N)
1674 {
1675  for ( int i = 0; i < N; i += 1 )
1676  vec[i] = std::abs(vec[i]);
1677 }
1678 
1679 template <class VectorType>
1680 inline VectorType
1681 GetAbsoluteVector( const VectorType& vec, const int N )
1682 {
1683  VectorType result(vec);
1684  AbsoluteVector(result, N);
1685  return result;
1686 }
1687 
1688 template <class T1, class T2>
1689 void
1690 FlipVector ( T1& vec, const T2& flip, const int N)
1691 {
1692  for ( int i = 0; i < N; i += 1 )
1693  vec[i] = flip[i]? -vec[i] : vec[i];
1694 }
1695 
1697 template <class T1, class T2>
1698 void
1699 VectorToVector ( const T1& v1, T2& v2, const int N)
1700 {
1701  for ( int i = 0; i < N; i += 1 )
1702  v2[i] = v1[i];
1703 }
1704 
1706 template <class T1, class T2>
1707 void
1708 MatrixToMatrix ( const T1& mat1, T2& mat2, const int NRows, const int NColumns)
1709 {
1710  for ( int i = 0; i < NRows; i += 1 )
1711  for ( int j = 0; j < NColumns; j += 1 )
1712  mat2(i,j) = mat1(i,j);
1713 }
1714 
1716 template <class TMatrixType>
1717 void
1718 PrintMatrixStats ( const TMatrixType& matrix, const int NumberRows, const int NumberColumns, const std::string& str="", const char* separate=" ", std::ostream& os=std::cout )
1719 {
1720  char tmp[1024];
1721  std::vector<double> st = NumberRows*NumberColumns>0 ? GetContainerStats(&matrix(0,0), &matrix(0,0)+NumberRows*NumberColumns) : std::vector<double>(4,0);
1722  sprintf(tmp, "%-8s(%p): size = (%d, %d), stat = { %g, %g [%g], %g } : ", str==""?"matrix":str.c_str(), &matrix, NumberRows, NumberColumns, st[0], st[2], st[3], st[1] );
1723  std::string strr(tmp);
1724  if (NumberRows>0 && NumberColumns>0)
1725  os << strr << std::endl;
1726 }
1727 
1728 template <class TMatrixType>
1729 void
1730 PrintMatrix ( const TMatrixType& matrix, const int NumberRows, const int NumberColumns, const std::string& str="", const char* separate=" ", std::ostream& os=std::cout, bool showStats=true )
1731 {
1732  char tmp[1024];
1733  if (showStats)
1734  {
1735  std::vector<double> st = NumberRows*NumberColumns>0 ? GetContainerStats(&matrix(0,0), &matrix(0,0)+NumberRows*NumberColumns) : std::vector<double>(4,0);
1736  sprintf(tmp, "%-8s(%p): size = (%d, %d), stat = { %g, %g [%g], %g } : ", str==""?"matrix":str.c_str(), &matrix, NumberRows, NumberColumns, st[0], st[2], st[3], st[1] );
1737  }
1738  else
1739  sprintf(tmp, "%-8s(%p): size = (%d, %d) : ", str==""?"matrix":str.c_str(), &matrix, NumberRows, NumberColumns );
1740  std::string strr(tmp);
1741 
1742  if (NumberRows>0 && NumberColumns>0)
1743  {
1744  os << strr << " = [ ";
1745  for ( int i = 0; i < NumberRows-1; i += 1 )
1746  {
1747  for ( int j = 0; j < NumberColumns-1; j += 1 )
1748  os << matrix(i,j)<< separate;
1749  os << matrix(i,NumberColumns-1) << "; ";
1750  }
1751  for ( int j = 0; j < NumberColumns-1; j += 1 )
1752  os << matrix(NumberRows-1,j)<< separate;
1753  os << matrix(NumberRows-1,NumberColumns-1) << "];\n" << std::flush;
1754  }
1755  else
1756  os << strr << " is an empty matrix" << std::endl;
1757  return;
1758 }
1759 
1760 template <class TMatrixType>
1761 inline double
1762 ArgmaxSymmetricMatrix(const TMatrixType matrix, const int size, int& row, int& colomn, const bool includeDiagonalElements)
1763 {
1764  double maxValue = -std::numeric_limits<double>::max();
1765  double value;
1766  for ( unsigned int i = 0; i < size; i += 1 )
1767  {
1768  for ( unsigned int j = 0; (includeDiagonalElements?(j<=i):(j<i)) ; j += 1 )
1769  {
1770  value = matrix(i,j);
1771  if (value > maxValue)
1772  {
1773  maxValue = value;
1774  row = i;
1775  colomn = j;
1776  }
1777  }
1778  }
1779  return maxValue;
1780 }
1781 
1782 template <class TMatrixType>
1783 inline double
1784 ArgminSymmetricMatrix(const TMatrixType& matrix, const int size, int& row, int& colomn, const bool includeDiagonalElements)
1785 {
1786  double minValue = std::numeric_limits<double>::max();
1787  double value;
1788  for ( unsigned int i = 0; i < size; i += 1 )
1789  {
1790  for ( unsigned int j = 0; (includeDiagonalElements?(j<=i):(j<i)) ; j += 1 )
1791  {
1792  value = matrix(i,j);
1793  if (value < minValue)
1794  {
1795  minValue = value;
1796  row = i;
1797  colomn = j;
1798  }
1799  }
1800  }
1801  return minValue;
1802 }
1803 
1804 template <class TMatrixType>
1805 inline double
1806 ArgmaxMatrix(const TMatrixType& matrix, const int rows, const int columns, int& row, int& colomn)
1807 {
1808  double maxValue = -std::numeric_limits<double>::max();
1809  double value;
1810  for ( unsigned int i = 0; i < rows; i += 1 )
1811  {
1812  for ( unsigned int j = 0; j < columns; j += 1 )
1813  {
1814  value = matrix(i,j);
1815  if (value > maxValue)
1816  {
1817  maxValue = value;
1818  row = i;
1819  colomn = j;
1820  }
1821  }
1822  }
1823  return maxValue;
1824 }
1825 
1826 template <class TMatrixType>
1827 inline double
1828 ArgminMatrix(const TMatrixType matrix, const int rows, const int columns, int& row, int& colomn)
1829 {
1830  double minValue = std::numeric_limits<double>::max();
1831  double value;
1832  for ( unsigned int i = 0; i < rows; i += 1 )
1833  {
1834  for ( unsigned int j = 0; j < columns; j += 1 )
1835  {
1836  value = matrix(i,j);
1837  if (value < minValue)
1838  {
1839  minValue = value;
1840  row = i;
1841  colomn = j;
1842  }
1843  }
1844  }
1845  return minValue;
1846 }
1847 
1848 template <typename Vector2D>
1849 void
1850 Save2DVector ( const Vector2D& vv, std::ostream& out=std::cout)
1851 {
1852  for ( int i = 0; i < vv.size(); i += 1 )
1853  {
1854  if (vv[i].size()==0)
1855  {
1856  out<<"\n" << std::flush;
1857  continue;
1858  }
1859  for ( int j = 0; j < vv[i].size()-1; j += 1 )
1860  out << vv[i][j]<< " ";
1861  out << vv[i][vv[i].size()-1] << "\n" << std::flush;
1862  }
1863  return;
1864 }
1865 
1866 template <typename Vector2D>
1867 void
1868 Save2DVector ( const Vector2D& vv, const std::string& file)
1869 {
1870  std::ofstream out; // create ofstream object
1871  out.open ( file.c_str() ); // open ofstream
1872  utlException (!out, "\nERROR : failed to open output file " << file );
1873  Save2DVector(vv, out);
1874  out.close();
1875  return;
1876 }
1877 
1878 template <class TMatrixType>
1879 void
1880 SaveMatrix ( const TMatrixType& matrix, const int NumberRows, const int NumberColumns, const std::string& file )
1881 {
1882  std::ofstream out; // create ofstream object
1883  out.open ( file.c_str() ); // open ofstream
1884  utlException (!out, "\nERROR : failed to open output file " << file );
1885  for ( int i = 0; i < NumberRows; i += 1 )
1886  {
1887  for ( int j = 0; j < NumberColumns-1; j += 1 )
1888  out << matrix(i,j)<< " ";
1889  out << matrix(i,NumberColumns-1) << "\n";
1890  }
1891  out.close();
1892  return;
1893 }
1894 
1895 template <class TMatrixType>
1896 void
1897 ReadMatrix ( const std::string& file, TMatrixType& matrix )
1898 {
1899  std::vector<std::vector<std::string> > matrixStr;
1900  utl::ReadLines(file, matrixStr);
1901  int NumberRows = matrixStr.size();
1902  utlException(NumberRows==0, "wrong file");
1903  int NumberColumns = matrixStr[0].size();
1904  matrix = TMatrixType(NumberRows, NumberColumns);
1905  for ( int i = 0; i < NumberRows; i += 1 )
1906  {
1907  utlException(NumberColumns!=matrixStr[i].size(), "wrong size");
1908  for ( int j = 0; j < NumberColumns; j += 1 )
1909  std::istringstream ( matrixStr[i][j] ) >> matrix(i,j);
1910  }
1911  return;
1912 }
1913 
1914 inline void
1915 ComputeOffsetTable(const std::vector<int>& size, std::vector<int>& offsetTable, const int storedWay)
1916 {
1917  int Dim = size.size();
1918  if (storedWay==ROW_MAJOR)
1919  {
1920  offsetTable.resize(Dim);
1921  for ( int i = Dim-1; i >= 0; i-- )
1922  {
1923  if (i==Dim-1)
1924  offsetTable[i]=size[i];
1925  else
1926  offsetTable[i]=size[i]*offsetTable[i+1];
1927  }
1928  }
1929  else if (storedWay==COLUMN_MAJOR)
1930  {
1931  offsetTable.resize(Dim+1);
1932  offsetTable[0]=1;
1933  int numberOfSamples = 1;
1934  for ( int i = 0; i < Dim; ++i )
1935  {
1936  numberOfSamples *= size[i];
1937  offsetTable[i+1] = numberOfSamples;
1938  }
1939  }
1940  else
1941  utlException(true, "wrong storedWay input");
1942 }
1943 
1944 
1945 inline int
1946 ComputeNDArrayOffset(const std::vector<int>& multiIndex, const std::vector<int>& size, const int storedWay, std::vector<int> offsetTable=std::vector<int>())
1947 {
1948  int Dim = multiIndex.size();
1949  if (Dim==1)
1950  return multiIndex[0];
1951 
1952  if (offsetTable.size()==0)
1953  ComputeOffsetTable(size, offsetTable, storedWay);
1954 
1955  int offset = 0;
1956  if (storedWay==ROW_MAJOR)
1957  {
1958  offset = 0;
1959  for ( int i = 0; i < Dim-1; ++i )
1960  offset += multiIndex[i]*offsetTable[i+1];
1961  offset += multiIndex[Dim-1];
1962  }
1963  else if (storedWay==COLUMN_MAJOR)
1964  {
1965  offset = 0;
1966  for ( int i = Dim-1; i > 0; i-- )
1967  offset += multiIndex[i] * offsetTable[i];
1968  offset += multiIndex[0];
1969  }
1970  else
1971  utlException(true, "wrong storedWay input");
1972 
1973  return offset;
1974 }
1975 
1976 inline void
1977 ComputeNDArrayIndex(const int offset, std::vector<int>& index, const std::vector<int>& size, const int storedWay, std::vector<int> offsetTable=std::vector<int>())
1978 {
1979  int Dim = size.size();
1980  if (Dim==1)
1981  {
1982  index.resize(1);
1983  index[0]=offset;
1984  return;
1985  }
1986 
1987  if (offsetTable.size()==0)
1988  ComputeOffsetTable(size, offsetTable, storedWay);
1989 
1990  index.resize(Dim);
1991  if (storedWay==ROW_MAJOR)
1992  {
1993  int nn = offset;
1994  for ( int i = 1; i < Dim; i++ )
1995  {
1996  index[i-1] = nn / offsetTable[i] ;
1997  nn -= index[i-1] * offsetTable[i];
1998  }
1999  index[Dim-1] = nn;
2000  }
2001  else if (storedWay==COLUMN_MAJOR)
2002  {
2003  int nn = offset;
2004  for ( int i = Dim-1; i > 0; i-- )
2005  {
2006  index[i] = nn / offsetTable[i] ;
2007  nn -= index[i] * offsetTable[i];
2008  }
2009  index[0] = nn;
2010  }
2011  else
2012  utlException(true, "wrong storedWay input");
2013 }
2014 
2015 
2016 }
2017 
2018 namespace std
2019 {
2020 
2021 template <typename T>
2022 std::ostream&
2023 operator<<(std::ostream& out, const std::vector<T>& vec )
2024 {
2025  if (vec.size()>0)
2026  {
2027  for ( int i = 0; i < vec.size()-1; i += 1 )
2028  {
2029  out << vec[i] << " ";
2030  }
2031  out << vec[vec.size()-1];
2032  }
2033  else
2034  out << "(empty vector)";
2035  return out;
2036 }
2037 
2038 template <typename T>
2039 std::ostream&
2040 operator<<(std::ostream& out, const std::complex<T>& val )
2041 {
2042  T a = val.real(), b = val.imag();
2043  if (a==0 && b==0)
2044  {
2045  out << "0";
2046  return out;
2047  }
2048 
2049  if (a!=0)
2050  out << a;
2051  if (b>0)
2052  out << "+" << b << "i";
2053  else if (b<0)
2054  out << b << "i";
2055  return out;
2056 }
2057 
2058 }
2059 
2062 #endif
void SwapBytes(void *ptr, const int sizePerElement, const int count)
Definition: utlCore.h:176
void PrintContainer(IteratorType v1, IteratorType v2, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout, bool showStats=true)
Definition: utlCore.h:1068
double ArgminMatrix(const TMatrixType matrix, const int rows, const int columns, int &row, int &colomn)
Definition: utlCore.h:1828
void ReadMatrix(const std::string &file, TMatrixType &matrix)
Definition: utlCore.h:1897
Help(const T *object)
Definition: utlCore.h:153
unsigned int argmin(Iterator i1, Iterator i2)
Definition: utlCore.h:212
bool IsOdd(const int value)
Definition: utlCore.h:825
void argmax2(const VectorType &vec, int size, int &index0, int &index1)
Definition: utlCore.h:234
void SaveVector(const VectorType &vv, const int NSize, const std::string &vectorStr, const bool is_save_number=false)
Definition: utlCore.h:1213
T min_element(const std::vector< T > &v)
Definition: utlCore.h:199
Help< Parent > is
Definition: utlCore.h:158
VectorType GetVectorShrinkage(const VectorType &vec, const int N, const double kappa)
Definition: utlCore.h:1664
int GetNumberOfNonZeroValues(IteratorType v, IteratorType v2, const double threshold=1e-6)
Definition: utlCore.h:986
void VectorToVector(const T1 &v1, T2 &v2, const int N)
Definition: utlCore.h:1699
bool IsNumber(const std::string &input)
Definition: utlCore.h:726
int strfind(const char *s, const char c)
Definition: utlCore.h:1098
std::string ConvertToWindowsOutputPath(const char *path)
Definition: utlCore.h:361
void SplitString(const std::string &str, std::vector< std::string > &strVec, const char *cc=" ")
Definition: utlCore.h:845
VectorType GetAbsoluteVector(const VectorType &vec, const int N)
Definition: utlCore.h:1681
void PrintMatrix(const TMatrixType &matrix, const int NumberRows, const int NumberColumns, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout, bool showStats=true)
Definition: utlCore.h:1730
VectorType AddNoise(const VectorType &signal, const int size, const double sigma, const bool is_rician=true)
Definition: utlCore.h:1552
bool IsEven(const int value)
Definition: utlCore.h:819
std::vector< double > GetContainerStats(IteratorType v1, IteratorType v2)
Definition: utlCore.h:921
double ArgmaxMatrix(const TMatrixType &matrix, const int rows, const int columns, int &row, int &colomn)
Definition: utlCore.h:1806
#define __utl_minmax(T0_, T1_, T2_)
Definition: utlCore.h:269
void ReplaceString(std::string &source, const char *replace, const char *with)
Definition: utlCore.h:319
std::vector< T > RandomVec(const std::vector< T > &vec, const int num, std::vector< int > &result_index)
Definition: utlCore.h:1448
std::string ConvertNumberToString(const T value, const int precision=6)
Definition: utlCore.h:740
void ConnectVector(std::vector< T > &vec1, const std::vector< T > &vec2)
Definition: utlCore.h:1304
#define utlException(cond, expout)
Definition: utlCoreMacro.h:548
bool IsFileExist(const std::string &file)
Definition: utlCore.h:529
void spherical2Cartesian(const T r, const T theta, const T phi, T &x, T &y, T &z)
Definition: utlCore.h:1602
STL namespace.
std::vector< std::vector< int > > SeparateVector(const std::vector< T > &vec, std::vector< T > &vec_sep, const double gap=1e-9)
Definition: utlCore.h:1262
void ReadLinesFirstlineCheck(const std::string &filename, std::vector< std::vector< std::string > > &strVec, const char *cc=" ")
Definition: utlCore.h:890
Created "09-22-2016.
T square(const T &x)
Definition: utlCore.h:287
bool IsSame< char >(const char &value, const char &v0, const double)
Definition: utlCore.h:1358
std::string StringToLowerCase(const std::string &str)
Definition: utlCore.h:292
const T & max(const T &a, const T &b)
Return the maximum between a and b.
Definition: utlCore.h:263
void ConvertToUnixSlashes(std::string &path)
Definition: utlCore.h:442
unsigned long TicToc(const bool is_tic, std::ostream &os=std::cout)
Definition: utlCore.h:91
void PowerVector(IteratorType v1, IteratorType v2, const double poww)
Definition: utlCore.h:1619
unsigned int argmax(Iterator i1, Iterator i2)
Definition: utlCore.h:222
std::vector< int > FindVector(const std::vector< T > &vec, const T &elem, const double gap=1e-9)
Definition: utlCore.h:1236
void FlipVector(T1 &vec, const T2 &flip, const int N)
Definition: utlCore.h:1690
void Save2DVector(const Vector2D &vv, std::ostream &out=std::cout)
Definition: utlCore.h:1850
std::string GetSequentialFileName(const std::string &filePrefix, const unsigned int iteration, const std::string &fileExtension, const unsigned int paddedLength=6)
Definition: utlCore.h:595
std::string StringToUpperCase(const std::string &str)
Definition: utlCore.h:301
const T & min(const T &a, const T &b)
Return the minimum between a and b.
Definition: utlCore.h:257
void SaveMatrix(const TMatrixType &matrix, const int NumberRows, const int NumberColumns, const std::string &file)
Definition: utlCore.h:1880
std::string CreateExpandedPath(const std::string &path)
Definition: utlCore.h:509
#define M_EPS
Definition: utlCoreMacro.h:54
void ComputeOffsetTable(const std::vector< int > &size, std::vector< int > &offsetTable, const int storedWay)
Definition: utlCore.h:1915
bool IsInstanceOf(const Object &o)
Definition: utlCore.h:169
std::string ConvertToUnixOutputPath(const char *path)
Definition: utlCore.h:411
bool IsContainsNaN(const VectorType &a, const int size)
Definition: utlCore.h:833
T abs(const T x)
template version of the fabs function
#define M_PI
Definition: utlCoreMacro.h:57
std::string ZeroPad(const unsigned int number, const unsigned int paddedLength)
Definition: utlCore.h:586
std::vector< std::string > CovertChar2DArrayToStringArray(const char arr[][M], int N)
Definition: utlCore.h:605
bool IsSame< int >(const int &value, const int &v0, const double)
Definition: utlCore.h:1344
T median_element(std::vector< T > values)
Definition: utlCore.h:190
#define utlGlobalAssert(cond, expout)
Definition: utlCoreMacro.h:381
Definition: utl.h:90
int ComputeNDArrayOffset(const std::vector< int > &multiIndex, const std::vector< int > &size, const int storedWay, std::vector< int > offsetTable=std::vector< int >())
Definition: utlCore.h:1946
bool IsInt(const std::string &input, const double epss=1e-10)
Definition: utlCore.h:792
unsigned long Toc(std::ostream &os=std::cout)
End tic/toc timer and displays elapsed time from last call to tic().
Definition: utlCore.h:138
int RoundNumber(const T x)
Definition: utlCore.h:785
std::vector< double > RandomPointInSphere(const bool hemis)
Definition: utlCore.h:1489
void CovertStringArrayToChar2DArray(const std::vector< std::string > &vec, char arr[][M], int N)
Definition: utlCore.h:619
T Random(const T d1=T(0.0), const T d2=T(1.0))
Definition: utlCore.h:1415
void AbsoluteVector(VectorType &vec, const int N)
Definition: utlCore.h:1673
bool IsSameVector(const std::vector< T > &vec1, const std::vector< T > &vec2, const double eps=1e-10)
Definition: utlCore.h:1365
void ComputeNDArrayIndex(const int offset, std::vector< int > &index, const std::vector< int > &size, const int storedWay, std::vector< int > offsetTable=std::vector< int >())
Definition: utlCore.h:1977
double ArgminSymmetricMatrix(const TMatrixType &matrix, const int size, int &row, int &colomn, const bool includeDiagonalElements)
Definition: utlCore.h:1784
unsigned long Time()
Return the value of a system timer, with a millisecond precision.
Definition: utlCore.h:74
utl functions using c++11 Created "07-02-2016
void ReadVector(const std::string &vectorStr, std::vector< T > &vec, const char *cc=" ")
Definition: utlCore.h:1159
void PrintVector(const std::vector< T > &vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout, bool showStats=true)
Definition: utlCore.h:1002
std::vector< T > SelectVector(const std::vector< T > &vec, const std::vector< int > &index)
Definition: utlCore.h:1312
VectorType GetVectorLinspace(const double valueMin, const double valueMax, const int num)
Definition: utlCore.h:1627
Help(const void *object)
Definition: utlCore.h:152
bool IsSameArray(const T(&a1)[N1], const T(&a2)[N2], const double eps=1e-10)
Definition: utlCore.h:1380
long RandomInt(const long d1=0, const long d2=1)
Definition: utlCore.h:1430
__InstanceOf(const Object &o)
Definition: utlCore.h:162
bool IsInVector(const VectorType &vec, const int size, const T &num, const double eps=1e-10)
Definition: utlCore.h:1394
bool IsSame(const T &value, const T &v0, const double eps=1e-10)
Definition: utlCore.h:1337
void ReadLines(const std::string &filename, std::vector< std::vector< std::string > > &strVec, const char *cc=" ")
Definition: utlCore.h:862
#define utlAssert(cond, expout)
Definition: utlCoreMacro.h:550
bool IsEndingWith(const std::string &fullString, const std::string &ending)
Definition: utlCore.h:537
VectorType NormalizeMinMax(const VectorType &v, const int nSize)
Definition: utlCore.h:637
std::string ConvertVectorToString(VectorType vec, const int N, const char *separate=" ")
Definition: utlCore.h:771
bool StringCompareCaseIgnored(const std::string &str1, const std::string &str2)
Definition: utlCore.h:311
void MatrixToMatrix(const T1 &mat1, T2 &mat2, const int NRows, const int NColumns)
Definition: utlCore.h:1708
void cartesian2Spherical(const T x, const T y, const T z, T &r, T &theta, T &phi)
Definition: utlCore.h:1579
T RicianRand(const T value, const double sigma)
Definition: utlCore.h:1526
VectorType NormalizeUnitNorm(const VectorType &v, const int nSize)
Definition: utlCore.h:672
void GetPath(const std::string &fileNameAbsolute, std::string &path, std::string &file)
Definition: utlCore.h:550
int SetVector(const char *s, std::vector< T > &vec, const int least_num=0, const char &c=',')
Definition: utlCore.h:1115
T ConvertStringToNumber(const std::string &input)
Definition: utlCore.h:751
macros for utlCore
std::vector< int > GetRange(const int start, const int end, const int space=1)
Definition: utlCore.h:1642
double GetSumOfVector(const TVectorType &vec, const int NSize)
Definition: utlCore.h:909
int sign(const T &x)
Return the sign of x.
Definition: utlCore.h:285
void PrintMatrixStats(const TMatrixType &matrix, const int NumberRows, const int NumberColumns, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
Definition: utlCore.h:1718
T cube(const T &x)
Definition: utlCore.h:289
T max_element(const std::vector< T > &v)
Definition: utlCore.h:205
void GetFileExtension(const std::string &fileNameAbsolute, std::string &ext, std::string &fileNoExt)
Definition: utlCore.h:559
unsigned long Tic(std::ostream &os=std::cout)
Start tic/toc timer for time measurement between code instructions.
Definition: utlCore.h:128
void VectorShrinkage(VectorType &vec, const int N, const double kappa)
Definition: utlCore.h:1656
small but powerful CMD parser, borrowed from CImg, http://cimg.sourceforge.net/
double ArgmaxSymmetricMatrix(const TMatrixType matrix, const int size, int &row, int &colomn, const bool includeDiagonalElements)
Definition: utlCore.h:1762
VectorType NormalizeMax(const VectorType &v, const int nSize)
Definition: utlCore.h:697
const double max(const int a, const float b)
Definition: utlCore.h:280
T GaussRand(const T value, const double sigma)
Definition: utlCore.h:1504