NOMAD Source  Version 4.0.0 Beta
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utils.hpp File Reference

Utility functions (headers) More...

#include <cmath>
#include <list>
#include <vector>
#include "../Util/defines.hpp"
#include "../nomad_nsbegin.hpp"
#include "../nomad_nsend.hpp"
Include dependency graph for utils.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

std::string itos (const int i)
 Transform an integer into a string. More...
 
std::string itos (const size_t i)
 Transform a unsigned long (size_t) into a string. More...
 
void toupper (std::string &s)
 Put a string into upper cases. More...
 
void toupper (std::list< std::string > &ls)
 Put a list of strings into upper cases. More...
 
bool atoi (const std::string &s, int &i)
 Convert a string into an integer. More...
 
bool atoi (const char c, int &i)
 Convert a character into an integer. More...
 
bool atost (const std::string &s, size_t &st)
 Convert a string into a size_t. More...
 
std::string enumStr (SuccessType success)
 Convert a success type to a string. More...
 
bool stringToBool (const std::string &string)
 Convert a string in {"YES","NO","Y","N","0","1","TRUE","FALSE"} to a boolean. More...
 
std::string boolToString (bool boolean)
 Convert a bool to "true" or "false". More...
 
std::size_t nbDecimals (const std::string &s)
 Return the number of decimals of a string representing a double. More...
 
void getFormat (const std::string &s, const size_t prec, size_t &width, size_t &spacePadding)
 
bool separateFormat (const std::string &s, std::string &format, std::string &tag)
 
bool validFormat (std::string &s)
 
bool SVD_decomposition (std::string &error_msg, double **M, double *W, double **V, int m, int n, int max_mpn=1500)
 SVD decomposition. More...
 
bool LU_decomposition (std::string &error_msg, double **M, int n, double &d, int max_mpn=1500)
 LU decomposition. More...
 
int getRank (double **M, size_t m, size_t n, double eps=SVD_EPS)
 
bool getDeterminant (double **M, double &det, size_t n)
 

Detailed Description

Utility functions (headers)

Author
Sebastien Le Digabel, modified by Viviane Rochon Montplaisir
Date
March 2017
See Also
utils.cpp

Definition in file utils.hpp.

Function Documentation

bool NOMAD::atoi ( const std::string &  s,
int &  i 
)

Convert a string into an integer.

Parameters
sThe string – IN.
iThe integer – OUT.
Returns
A boolean equal to true if the conversion was possible.

Definition at line 71 of file utils.cpp.

72 {
73  i = -1;
74  if ( s.empty() )
75  {
76  return false;
77  }
78 
79  size_t n = s.size();
80 
81  if ( s[0] == '-' )
82  {
83  if ( n > 1 && s[1] == '-' )
84  return false;
85  std::string ss = s;
86  ss.erase(ss.begin());
87  if ( NOMAD::atoi ( ss , i ) )
88  {
89  i = -i;
90  return true;
91  }
92  return false;
93  }
94 
95  std::string ts = s;
96  NOMAD::toupper(ts);
97  if ( ts == "INF" || ts == "+INF" )
98  {
99  i = NOMAD::P_INF_INT;
100  return true;
101  }
102  if ( ts == "-INF" )
103  {
104  i = NOMAD::M_INF_INT;
105  return true;
106  }
107 
108  for ( size_t k = 0 ; k < n ; ++k )
109  {
110  if ( !isdigit(s[k]) )
111  {
112  return false;
113  }
114  }
115  i = std::atoi(s.c_str());
116  return true;
117 }
bool atoi(const std::string &s, int &i)
Convert a string into an integer.
Definition: utils.cpp:71
const int M_INF_INT
minus infinity for int
Definition: defines.hpp:83
void toupper(std::string &s)
Put a string into upper cases.
Definition: utils.cpp:36
const int P_INF_INT
plus infinity for int
Definition: defines.hpp:82
bool NOMAD::atoi ( const char  c,
int &  i 
)

Convert a character into an integer.

Parameters
cThe character – IN.
iThe integer – OUT.
Returns
A boolean equal to true if the conversion was possible.

Definition at line 152 of file utils.cpp.

153 {
154  std::string s = "-";
155  s[0] = c;
156  return NOMAD::atoi(s,i);
157 }
bool atoi(const std::string &s, int &i)
Convert a string into an integer.
Definition: utils.cpp:71
bool NOMAD::atost ( const std::string &  s,
size_t &  st 
)

Convert a string into a size_t.

Parameters
sThe string – IN.
stThe size_t – OUT.
Returns
A boolean equal to true if the conversion was possible.

Definition at line 122 of file utils.cpp.

123 {
124  st = NOMAD::INF_SIZE_T;
125 
126  if ( s.empty() )
127  {
128  return false;
129  }
130 
131  std::string ts = s;
132  NOMAD::toupper(ts);
133  if ( ts == "INF" || ts == "+INF" )
134  {
135  st = NOMAD::INF_SIZE_T;
136  return true;
137  }
138 
139  int i;
140  bool success = NOMAD::atoi(s,i);
141  if ( success )
142  {
143  if ( i < 0 )
144  throw NOMAD::Exception(__FILE__, __LINE__, "Invalid value for size_t. Value must be >0");
145  st = static_cast<size_t>(i);
146  }
147 
148  return success;
149 }
bool atoi(const std::string &s, int &i)
Convert a string into an integer.
Definition: utils.cpp:71
void toupper(std::string &s)
Put a string into upper cases.
Definition: utils.cpp:36
const size_t INF_SIZE_T
The infinity for size_t.
Definition: defines.hpp:84
std::string NOMAD::boolToString ( bool  boolean)

Convert a bool to "true" or "false".

Definition at line 217 of file utils.cpp.

218 {
219  return (boolean) ? "true" : "false";
220 }
std::string enumStr ( SuccessType  success)

Convert a success type to a string.

bool NOMAD::getDeterminant ( double **  M,
double &  det,
size_t  n 
)
Parameters
MThe input nxn matrix – IN.
nNumber of row and columns in M – IN.
detThe determinant of the matrix M – OUT.
Returns
True/False if success or not.

Definition at line 406 of file utils.cpp.

409 {
410  std::string error_msg;
411 
412  double d=1 ;
413 
414  double ** LU = new double *[n];
415  for (size_t i = 0 ; i < n ; i++ )
416  {
417  LU[i]=new double [n];
418  for ( size_t j = 0; j < n ; j++ )
419  LU[i][j]=M[i][j];
420  }
421 
422  NOMAD::LU_decomposition ( error_msg , LU , static_cast<int>(n) , d );
423 
424 
425  if ( error_msg.empty() )
426  {
427  for (size_t i=0;i < n;i++)
428  d*=LU[i][i];
429  }
430 
431 
432  for (size_t i = 0 ; i < n ; i++ )
433  {
434  delete [] LU[i];
435  }
436  delete [] LU;
437 
438  det=d;
439 
440  if ( error_msg.empty() )
441  return true;
442  else
443  return false;
444 }
bool LU_decomposition(std::string &error_msg, double **M, int n, double &d, int max_mpn=1500)
LU decomposition.
Definition: utils.cpp:828
void NOMAD::getFormat ( const std::string &  s,
const size_t  prec,
size_t &  width,
size_t &  spacePadding 
)

Given a string s with precision prec after the decimal point, return the suggested width and the string padding needed for alignment.

Definition at line 251 of file utils.cpp.

252 {
253  // These values non modifiable for now.
254  const size_t nbDigitsBeforePoint = NOMAD::NB_DIGITS_BEFORE_POINT;
255  const size_t intWidth = NOMAD::INT_DISPLAY_WIDTH;
256 
257  if (0 == prec)
258  {
259  // Integer.
260  width = intWidth;
261  }
262  else
263  {
264  // Double
265  width = nbDigitsBeforePoint + 1 + prec;
266  size_t pointPos = s.find(".");
267  spacePadding = 1 + prec; // Including one space for the decimal point
268  if (pointPos != std::string::npos)
269  {
270  // Compute padding as needed.
271  // Ex. if precision is 6 but the string number is "27.03" -> pad to "27.03 ".
272  spacePadding -= (s.size() - pointPos);
273  if (spacePadding >= width)
274  {
275  spacePadding = 0;
276  }
277  }
278  }
279 }
const int INT_DISPLAY_WIDTH
Definition: defines.hpp:93
const int NB_DIGITS_BEFORE_POINT
Definition: defines.hpp:92
int NOMAD::getRank ( double **  M,
size_t  m,
size_t  n,
double  eps = SVD_EPS 
)
  • The mxn M matrix is decomposed into M=U.W.V'. The rank equals the size of W
    Parameters
    MThe input mxn matrix – IN.
    mNumber of rows in M – IN.
    nNumber of columns in M – IN.
    epsPrecision to detect rank from W – IN.
    Returns
    The rank>0 if the decomposition worked else 0.

Definition at line 446 of file utils.cpp.

450 {
451  double * W = new double [n];
452  double ** V = new double *[n];
453  for (size_t i = 0 ; i < n ; ++i )
454  {
455  V[i]=new double [n];
456  }
457 
458  std::string error_msg;
459  NOMAD::SVD_decomposition ( error_msg , M , W , V , static_cast<int>(m) , static_cast<int>(n) );
460 
461  for (size_t i=0;i<n;++i)
462  delete [] V[i];
463  delete [] V;
464 
465 
466  if (! error_msg.empty())
467  {
468  delete [] W;
469  return -1;
470  }
471 
472  int rank=0;
473  for (size_t i=0;i<n;i++)
474  {
475  if (fabs(W[i]) > eps)
476  rank++;
477  }
478 
479  delete [] W;
480  return rank;
481 
482 }
bool SVD_decomposition(std::string &error_msg, double **M, double *W, double **V, int m, int n, int max_mpn=1500)
SVD decomposition.
Definition: utils.cpp:509
std::string NOMAD::itos ( const int  i)

Transform an integer into a string.

Parameters
iThe integer – IN.
Returns
The string.

Definition at line 15 of file utils.cpp.

16 {
17  std::ostringstream oss;
18  oss << i;
19  return oss.str();
20 }
std::string NOMAD::itos ( const size_t  i)

Transform a unsigned long (size_t) into a string.

Parameters
iThe unsigned long – IN.
Returns
The string.

Definition at line 25 of file utils.cpp.

26 {
27  std::ostringstream oss;
28  oss << i;
29  return oss.str();
30 }
bool NOMAD::LU_decomposition ( std::string &  error_msg,
double **  M,
int  n,
double &  d,
int  max_mpn = 1500 
)

LU decomposition.

  • The nxn M matrix is decomposed into M=L.U.
    Parameters
    error_msgError message when the function returns falseOUT.
    MThe input nxn matrix; Will be replaced by LUIN/OUT.
    nNumber of columns and rows in M – IN.
    dUsed by determinant – OUT.
    max_mpnMaximum allowed value for n; ignored if <=0IN (Opt) (default = 50).
    Returns
    A boolean equal to true if the decomposition worked.

Definition at line 828 of file utils.cpp.

833 {
834  error_msg.clear();
835 
836  if ( max_n > 0 && n > max_n )
837  {
838  error_msg = "LU_decomposition() error: n > " + NOMAD::itos ( max_n );
839  return false;
840  }
841 
842  const double TINY =1E-40;
843  int i,imax,j,k;
844 
845  double big,temp;
846 
847  double * vv = new double[n]; // stores the implicit scaling of each row
848  int *indx = new int[n]; // stores the permutations
849 
850  d =1; // No row interchange yet
851 
852  for (i = 0; i < n ; i++ ) // Loop over row to get implicit scaling information
853  {
854  big = 0.0;
855  for ( j = 0 ; j < n ; j++ )
856  {
857  if ( (temp=fabs(LU[i][j])) > big )
858  big = temp;
859  }
860  if ( big == 0 )
861  {
862  error_msg = "LU_decomposition() error: no nonzero largest element";
863  delete [] vv;
864  delete [] indx;
865 
866  return false;
867  }
868  vv[i]= 1.0/big; // Saves the scaling
869  }
870  for ( k = 0; k < n ; k++) // This is the outermost kij loop
871  {
872  big = 0.0; // Initialized the search for largest pivot element
873  imax = k;
874  for ( i = k ; i < n ; i++ )
875  {
876  temp=vv[i]*fabs(LU[i][k]);
877  if ( temp > big )
878  {
879  big = temp;
880  imax=i;
881  }
882  }
883  if ( k != imax )
884  {
885  for ( j = 0; j < n ; j++ )
886  {
887  temp = LU[imax][j];
888  LU[imax][j]=LU[k][j];
889  LU[k][j]=temp;
890  }
891  d = -d;
892  vv[imax] = vv[k];
893  }
894  indx[k] = imax;
895  if ( LU[k][k] == 0.0) // TINY for zero
896  LU[k][k] = TINY;
897  for ( i = k+1 ; i < n ; i++ )
898  {
899  temp = LU[i][k] /= LU[k][k]; // Divide by pivot element
900  for ( j = k+1 ; j < n ; j++ ) // Innermos matrix: reduce remaining submatrix
901  LU[i][j] -= temp*LU[k][j];
902  }
903  }
904 
905  delete [] vv;
906  delete [] indx;
907  return true;
908 
909 }
std::string itos(const int i)
Transform an integer into a string.
Definition: utils.cpp:15
std::size_t nbDecimals ( const std::string &  s)

Return the number of decimals of a string representing a double.

bool NOMAD::separateFormat ( const std::string &  s,
std::string &  format,
std::string &  tag 
)

Given a string starting with '', separate the actual formatting from the tag. Ex. "%5.2fOBJ" -> "%5.2f", "OBJ" Ex. "%dBBO" -> "%d", "BBO" Ex. "%12BBE" -> "%12f", "BBE" "TIME" returns false. "%4.2" returns false.

Returns
true if a valid format was found, false otherwise.

Definition at line 282 of file utils.cpp.

283 {
284  // TODO Use the formatting we found here, like in NOMAD_3.
285  // TODO Add a temporary warning that we do not treat formatting - but
286  // don't show it every time
287  //std::cerr << "Warning: Formated output is not yet supported (starting with %)." << std::endl;
288 
289  format = "";
290  tag = s;
291  const std::string formatLetters = "eEfgGdi";
292  const std::string allLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
293  bool doSeparate = false;
294 
295  if ('%' == s[0])
296  {
297  std::size_t firstLetter = s.find_first_of(allLetters, 1);
298  std::size_t firstFormatLetter = s.find_first_of(formatLetters, 1);
299  std::size_t cutIndex = firstLetter;
300  if (std::string::npos != firstFormatLetter
301  && firstLetter == firstFormatLetter)
302  {
303  cutIndex += 1;
304  }
305  if (std::string::npos != cutIndex)
306  {
307  std::string tempFormat = s.substr(0, cutIndex);
308  doSeparate = validFormat(tempFormat);
309  if (doSeparate)
310  {
311  format = tempFormat;
312  tag = s.substr(cutIndex, s.length() - cutIndex);
313  }
314  }
315  }
316 
317  return doSeparate;
318 }
bool validFormat(std::string &s)
Definition: utils.cpp:338
bool NOMAD::stringToBool ( const std::string &  string)

Convert a string in {"YES","NO","Y","N","0","1","TRUE","FALSE"} to a boolean.

Definition at line 193 of file utils.cpp.

194 {
195  bool ret = false;
196  std::string s = string;
197  NOMAD::toupper(s);
198 
199  if ( s == "Y" || s == "YES" || s == "1" || s == "TRUE" )
200  {
201  ret = true;
202  }
203  else if ( s == "N" || s == "NO" || s == "0" || s == "FALSE" )
204  {
205  ret = false;
206  }
207  else
208  {
209  throw NOMAD::Exception(__FILE__, __LINE__, "Unrecognized string for bool: " + s);
210  }
211 
212  return ret;
213 }
void toupper(std::string &s)
Put a string into upper cases.
Definition: utils.cpp:36
bool NOMAD::SVD_decomposition ( std::string &  error_msg,
double **  M,
double *  W,
double **  V,
int  m,
int  n,
int  max_mpn = 1500 
)

SVD decomposition.

  • The mxn M matrix is decomposed into M=U.W.V'.
    Parameters
    error_msgError message when the function returns falseOUT.
    MThe input mxn matrix; Will be replaced by UIN/OUT.
    WThe output nxn diagonal matrix – OUT.
    VThe output nxn matrix – OUT.
    mNumber of rows in M – IN.
    nNumber of columns in M – IN.
    max_mpnMaximum allowed value for m+n; ignored if <=0IN (Opt) (default = 1500).
    Returns
    A boolean equal to true if the decomposition worked.

Definition at line 509 of file utils.cpp.

516 {
517  error_msg.clear();
518 
519  if ( max_mpn > 0 && m+n > max_mpn )
520  {
521  error_msg = "SVD_decomposition() error: m+n > " + NOMAD::itos ( max_mpn );
522  return false;
523  }
524 
525  double * rv1 = new double[n];
526  double scale = 0.0;
527  double g = 0.0;
528  double norm = 0.0;
529 
530  int nm1 = n - 1;
531 
532  bool flag;
533  int i , j , k , l = 0, its , jj , nm = 0;
534  double s , f , h , tmp , c , x , y , z , absf , absg , absh;
535 
536  const int NITER = 30;
537 
538  // Initialization W and V
539  for (i=0; i < n; ++i)
540  {
541  W[i]=0.0;
542  for (j=0; j < n ; ++j)
543  V[i][j]=0.0;
544  }
545 
546  // Householder reduction to bidiagonal form:
547  for ( i = 0 ; i < n ; ++i )
548  {
549  l = i + 1;
550  rv1[i] = scale * g;
551  g = s = scale = 0.0;
552  if ( i < m )
553  {
554  for ( k = i ; k < m ; ++k )
555  scale += fabs ( M[k][i] );
556  if ( scale )
557  {
558  for ( k = i ; k < m ; ++k )
559  {
560  M[k][i] /= scale;
561  s += M[k][i] * M[k][i];
562  }
563  f = M[i][i];
564  g = ( f >= 0.0 ) ? -fabs(sqrt(s)) : fabs(sqrt(s));
565  h = f * g - s;
566  M[i][i] = f - g;
567  for ( j = l ; j < n ; ++j )
568  {
569  for ( s = 0.0 , k = i ; k < m ; ++k )
570  s += M[k][i] * M[k][j];
571  f = s / h;
572  for ( k = i ; k < m ; ++k )
573  M[k][j] += f * M[k][i];
574  }
575  for ( k = i ; k < m ; ++k )
576  M[k][i] *= scale;
577  }
578  }
579  W[i] = scale * g;
580  g = s = scale = 0.0;
581  if ( i < m && i != nm1 )
582  {
583  for ( k = l ; k < n ; ++k )
584  scale += fabs ( M[i][k] );
585  if ( scale )
586  {
587  for ( k = l ; k < n ; ++k )
588  {
589  M[i][k] /= scale;
590  s += M[i][k] * M[i][k];
591  }
592  f = M[i][l];
593  g = ( f >= 0.0 ) ? -fabs(sqrt(s)) : fabs(sqrt(s));
594  h = f * g - s;
595  M[i][l] = f - g;
596  for ( k = l ; k < n ; ++k )
597  rv1[k] = M[i][k] / h;
598  for ( j = l ; j < m ; ++j )
599  {
600  for ( s=0.0,k=l ; k < n ; ++k )
601  s += M[j][k] * M[i][k];
602  for ( k=l ; k < n ; ++k )
603  M[j][k] += s * rv1[k];
604  }
605  for ( k = l ; k < n ; ++k )
606  M[i][k] *= scale;
607  }
608  }
609  tmp = fabs ( W[i] ) + fabs ( rv1[i] );
610  norm = ( norm > tmp ) ? norm : tmp;
611  }
612 
613  // accumulation of right-hand transformations:
614  for ( i = nm1 ; i >= 0 ; --i )
615  {
616  if ( i < nm1 )
617  {
618  if ( g )
619  {
620  for ( j = l ; j < n ; ++j )
621  V[j][i] = ( M[i][j] / M[i][l] ) / g;
622  for ( j = l ; j < n ; ++j )
623  {
624  for ( s = 0.0 , k = l ; k < n ; ++k )
625  s += M[i][k] * V[k][j];
626  for ( k = l ; k < n ; ++k )
627  V[k][j] += s * V[k][i];
628  }
629  }
630  for ( j = l ; j < n ; ++j )
631  V[i][j] = V[j][i] = 0.0;
632  }
633  V[i][i] = 1.0;
634  g = rv1[i];
635  l = i;
636  }
637 
638  // accumulation of left-hand transformations:
639  for ( i = ( ( m < n ) ? m : n ) - 1 ; i >= 0 ; --i )
640  {
641  l = i + 1;
642  g = W[i];
643  for ( j = l ; j < n ; ++j )
644  M[i][j] = 0.0;
645  if ( g )
646  {
647  g = 1.0 / g;
648  for ( j = l ; j < n ; ++j )
649  {
650  for ( s = 0.0 , k = l ; k < m ; ++k )
651  s += M[k][i] * M[k][j];
652  f = ( s / M[i][i] ) * g;
653  for ( k = i ; k < m ; ++k )
654  M[k][j] += f * M[k][i];
655  }
656  for ( j = i ; j < m ; ++j )
657  M[j][i] *= g;
658  }
659  else
660  for ( j = i ; j < m ; ++j )
661  M[j][i] = 0.0;
662  ++M[i][i];
663  }
664 
665  // diagonalization of the bidiagonal form:
666  for ( k = nm1 ; k >= 0 ; --k )
667  {
668  for ( its = 1 ; its <= NITER ; its++ )
669  {
670  flag = true;
671  for ( l = k ; l >= 0 ; l-- )
672  {
673  nm = l - 1;
674  if ( nm < 0 || fabs ( rv1[l]) + norm == norm )
675  {
676  flag = false;
677  break;
678  }
679  if ( fabs ( W[nm] ) + norm == norm )
680  break;
681  }
682  if ( flag )
683  {
684  c = 0.0;
685  s = 1.0;
686  for ( i = l ; i <= k ; i++ )
687  {
688  f = s * rv1[i];
689  rv1[i] = c * rv1[i];
690  if ( fabs(f) + norm == norm )
691  break;
692  g = W[i];
693 
694  absf = fabs(f);
695  absg = fabs(g);
696  h = ( absf > absg ) ?
697  absf * sqrt ( 1.0 + pow ( absg/absf , 2.0 ) ) :
698  ( ( absg==0 ) ? 0.0 : absg * sqrt ( 1.0 + pow ( absf/absg , 2.0 ) ) );
699 
700  W[i] = h;
701  h = 1.0 / h;
702  c = g * h;
703  s = -f * h;
704  for ( j = 0 ; j < m ; ++j )
705  {
706  y = M[j][nm];
707  z = M[j][ i];
708  M[j][nm] = y * c + z * s;
709  M[j][ i] = z * c - y * s;
710  }
711  }
712  }
713  z = W[k];
714  if ( l == k)
715  {
716  if ( z < 0.0 )
717  {
718  W[k] = -z;
719  for ( j = 0 ; j < n ; j++ )
720  V[j][k] = -V[j][k];
721  }
722  break; // this 'break' is always active if k==0
723  }
724  if ( its == NITER )
725  {
726  error_msg = "SVD_decomposition() error: no convergence in " +
727  NOMAD::itos ( NITER ) + " iterations";
728  delete [] rv1;
729  return false;
730  }
731  x = W[l];
732  nm = k - 1;
733  y = W[nm];
734  g = rv1[nm];
735  h = rv1[k];
736  f = ( (y-z) * (y+z) + (g-h) * (g+h) ) / ( 2.0 * h * y );
737 
738  absf = fabs(f);
739  g = ( absf > 1.0 ) ?
740  absf * sqrt ( 1.0 + pow ( 1.0/absf , 2.0 ) ) :
741  sqrt ( 1.0 + pow ( absf , 2.0 ) );
742 
743  f = ( (x-z) * (x+z) +
744  h * ( ( y / ( f + ( (f >= 0)? fabs(g) : -fabs(g) ) ) ) - h ) ) / x;
745  c = s = 1.0;
746 
747  for ( j = l ; j <= nm ; ++j )
748  {
749  i = j + 1;
750  g = rv1[i];
751  y = W[i];
752  h = s * g;
753  g = c * g;
754 
755  absf = fabs(f);
756  absh = fabs(h);
757  z = ( absf > absh ) ?
758  absf * sqrt ( 1.0 + pow ( absh/absf , 2.0 ) ) :
759  ( ( absh==0 ) ? 0.0 : absh * sqrt ( 1.0 + pow ( absf/absh , 2.0 ) ) );
760 
761  rv1[j] = z;
762  c = f / z;
763  s = h / z;
764  f = x * c + g * s;
765  g = g * c - x * s;
766  h = y * s;
767  y *= c;
768  for ( jj = 0 ; jj < n ; ++jj )
769  {
770  x = V[jj][j];
771  z = V[jj][i];
772  V[jj][j] = x * c + z * s;
773  V[jj][i] = z * c - x * s;
774  }
775 
776  absf = fabs(f);
777  absh = fabs(h);
778  z = ( absf > absh ) ?
779  absf * sqrt ( 1.0 + pow ( absh/absf , 2.0 ) ) :
780  ( ( absh==0 ) ? 0.0 : absh * sqrt ( 1.0 + pow ( absf/absh , 2.0 ) ) );
781 
782  W[j] = z;
783 
784  if ( z )
785  {
786  z = 1.0 / z;
787  c = f * z;
788  s = h * z;
789  }
790  f = c * g + s * y;
791  x = c * y - s * g;
792  for ( jj = 0 ; jj < m ; ++jj )
793  {
794  y = M[jj][j];
795  z = M[jj][i];
796  M[jj][j] = y * c + z * s;
797  M[jj][i] = z * c - y * s;
798  }
799  }
800  rv1[l] = 0.0;
801  rv1[k] = f;
802  W [k] = x;
803  }
804  }
805 
806  delete [] rv1;
807  return true;
808 }
std::string itos(const int i)
Transform an integer into a string.
Definition: utils.cpp:15
void NOMAD::toupper ( std::string &  s)

Put a string into upper cases.

Parameters
sThe string – IN/OUT.

Definition at line 36 of file utils.cpp.

37 {
38  // Warning: strings like "é" do not get converted.
39  // Even when using locale.
40  // This is a widechar issue, not investigating in it right now.
41  // There is no easy way to show a clear warning, so do not do
42  // anything about it - just file it is as a known issue.
43 
44 // So 98
45 // size_t ns = s.size();
46 // for ( size_t i = 0 ; i < ns ; ++i )
47 // {
48 // s[i] = std::toupper(s[i]);
49 // }
50 
51 // modern obfuscated C++
52  for_each(s.begin(), s.end(), [](char& in){ in = std::toupper(in); });
53 }
void toupper(std::string &s)
Put a string into upper cases.
Definition: utils.cpp:36
void NOMAD::toupper ( std::list< std::string > &  ls)

Put a list of strings into upper cases.

Parameters
lsThe list of strings – IN/OUT.

Definition at line 58 of file utils.cpp.

59 {
60  std::list<std::string>::iterator it;
61  std::list<std::string>::const_iterator end = ls.end();
62  for ( it = ls.begin() ; it != end ; ++it )
63  {
64  NOMAD::toupper(*it);
65  }
66 }
void toupper(std::string &s)
Put a string into upper cases.
Definition: utils.cpp:36
bool NOMAD::validFormat ( std::string &  s)

Definition at line 338 of file utils.cpp.

339 {
340  const std::string formatLetters = "eEfgGdi";
341  bool isValid = true;
342 
343  // Verify size
344  if (s.length() < 2)
345  {
346  isValid = false;
347  }
348 
349  else
350  {
351  // Append f if last char is a digit.
352  if (std::isdigit(s[s.length()-1]))
353  {
354  s = s + "f";
355  }
356  size_t indexFormatLetter = s.find_first_of(formatLetters, 1);
357 
358  // Verify we do have a format letter
359  if (std::string::npos == indexFormatLetter)
360  {
361  isValid = false;
362  }
363  // Verify first character is '%'
364  else if (s[0] != '%')
365  {
366  isValid = false;
367  }
368  // Verify last character is a formatting character
369  else if (indexFormatLetter < s.length()-1)
370  {
371  isValid = false;
372  }
373  // Verify in between we have a decimal number
374  else
375  {
376  bool pointEncountered = false;
377  for (size_t i = 1; i < indexFormatLetter; i++)
378  {
379  if (std::isdigit(s[i]))
380  {
381  // ok
382  }
383  else if (s[i] == '.')
384  {
385  if (!pointEncountered)
386  {
387  pointEncountered = true;
388  }
389  else
390  {
391  // more than one point
392  isValid = false;
393  }
394  }
395  else
396  {
397  isValid = false;
398  }
399  }
400  }
401  }
402 
403  return isValid;
404 }