Machine Learning Library
Macros.h
Go to the documentation of this file.
1 #ifndef MACROS_H
2 #define MACROS_H
3 #include <ctime>
4 #include <cstdlib>
5 #include <cmath>
6 #include <vector>
7 #include <stdio.h>
8 #include <iostream>
9 #include <CObject.h>
10 using namespace std;
11 #ifndef INFINITY
12 #define INFINITY HUGE_VAL
13 #endif
14 
15 #define MSG(x) {cout << x << endl;}
16 #define ABS(x) ( ((x) < 0) ? -(x) : (x))
17 #define SIGN(x) ( ((x) < 0) ? -1 : +1)
18 
19 #define MAX(x,y) ( ((x) > (y)) ? (x) : (y))
20 #define MIN(x,y) ( ((x) < (y)) ? (x) : (y))
21 
22 // template<class Type>
23 // Type MAX(Type x, Type y)
24 // {
25 // if(x>=y)
26 // return x;
27 // else
28 // return y;
29 // };
30 
31 
32 // template<class Type>
33 // Type MIN(Type x, Type y)
34 // {
35 // if(x<y) return x;
36 // else return y;
37 // };
38 
39 // template<class Type>
40 // Type SIGN(Type x)
41 // {
42 // if(x>0) return (Type)+1;
43 // else return (Type)-1;
44 // };
45 
46 // template<class Type>
47 // Type ABS(Type x)
48 // {
49 // if(x>0) return x;
50 // else return (-x);
51 // };
52 
53 
54 
55 
56 
57 
58 
59 template<class Type>
60 void readValueOfType(Type& rtValue, fstream& stream, DATATYPE tType)
61 {
62  if(tType == INT) {
63  int iTmp;
64  stream.read((char*) &iTmp, sizeof(int));
65  rtValue = (Type) iTmp;
66  } else if(tType == FLOAT) {
67  float fTmp;
68  stream.read((char*) &fTmp, sizeof(float));
69  rtValue = (Type) fTmp;
70  } else if(tType == DOUBLE) {
71  double dTmp;
72  stream.read((char*) &dTmp, sizeof(double));
73  rtValue = (Type) dTmp;
74  } else return;
75 }
76 
77 /*
78  return random (integer) number in [x,..,y]
79 */
80 #define IRAND(x,y) ((x) + (int)(((float) (y - x)) * rand()/(RAND_MAX+1.0)))
81 
86 template<class Type>
87 Type getRank(int iRank, int iDim, Type* pfVector);
88 
89 /******************************************************************************/
94 /*-NAN checks----------------------------------------------------------------*/
95 
96 #define ML_NAN_CHK(var) if(!isnan(var)) {cerr << "ML_NAN_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "!= NAN" << endl; abort();}
97 
98 #define ML_NOT_NAN_CHK(var) if(isnan(var)) {cerr << "ML_NOT_NAN_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "== NAN" << endl; abort();}
99 /*-NULL checks----------------------------------------------------------------*/
100 
101 #define ML_NULL_CHK(var) if((var) != NULL) {cerr << "ML_NULL_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "!= NULL" << endl; abort();}
102 
103 #define ML_NOT_NULL_CHK(var) if((var) == NULL) {cerr << "ML_NOT_NULL_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "== NULL" << endl; abort();}
104 
105 /*-BOOL check-----------------------------------------------------------------*/
106 
107 #define ML_BOOL_CHK(var) if(!(var)) {cerr << "ML_BOOL_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "=" << var << endl; abort();}
108 
109 /*-Less - Less equal----------------------------------------------------------*/
110 
111 #define ML_LS_CHK(var, val) if((var) >= (val)) {cerr << "ML_LS_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "=" << var << " >= " << val << endl; abort();}
112 
113 #define ML_LEQ_CHK(var, val) if((var) > (val)) {cerr << "ML_LEQ_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "=" << var << " > " << val << endl; abort();}
114 
115 /*-Greater - Greater equal----------------------------------------------------*/
116 
117 #define ML_GR_CHK(var, val) if((var) <= (val)) {cerr << "ML_GR_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "=" << var << " <= " << val << endl; abort();}
118 
119 #define ML_GEQ_CHK(var, val) if((var) < (val)) {cerr << "ML_GEQ_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "=" << var << " < " << val << endl; abort();}
120 
121 /*-Equal - Not equal----------------------------------------------------------*/
122 
123 #define ML_EQ_CHK(var, val) if((var) != (val)) {cerr << "ML_EQ_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "=" << var << " != " << val << endl; abort();}
124 
125 #define ML_NEQ_CHK(var, val) if((var) == (val)) {cerr << "ML_NEQ_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "=" << var << " == " << val << endl; abort();}
126 
127 /*-Out of range---------------------------------------------------------------*/
128 
129 #define ML_RANGE_CHK(var, lower, upper) if((var)<(lower) && (var)>(upper)) {cerr << "ML_RANGE_CHK failed in File: " << __FILE__ << " Line: " << __LINE__ << ". " << #var << "=" << var << " out of range ["<<lower <<","<<upper<<"]" << endl; abort();}
130 
131 /*- Warning to stderr - execution will continue ------------------------------*/
132 #define ML_WARNING_MSG(x) {cerr << "ML_WARNING_MSG in File: " << __FILE__ << " Line: " << __LINE__ << endl << x << endl << endl; cerr << endl;}
133 
134 
135 /*- Warning to stderr - program will exit ------------------------------------*/
136 #define ML_CRITICAL_MSG(x) {cerr << "ML_CRITICAL_MSG in File: " << __FILE__ << " Line: " << __LINE__ << endl << x << endl << endl; abort();}
137 
138 /*- Message to cout ----------------------------------------------------------*/
139 #define ML_MSG(x) {cerr << "ML_MSG in File: " << __FILE__ << " Line: " << __LINE__ << endl << x << endl << endl; cerr << endl;}
140 
141 
142 /******************************************************************************/
156 template<class Type>
157 void nDimFor(const vector<Type>& rvtStart, const vector<Type>& rvtEnd, vector<Type>& rvtIndices, bool bInit);
158 
159 template<class Type>
160 void nDimForReverse(const vector<Type>& rvtStart, const vector<Type>& rvtEnd, vector<Type>& rvtIndices, bool bInit);
161 
162 
163 
164 /******************************************************************************/
169 double gammln(double xx);
170 double factrl(int n);
171 double factln(int n);
172 double bico(int n,int k);
173 float gauss_rnd(void);
174 void init_rnd_seed();
175 
176 
177 #endif
DATATYPE
Definition: CObject.h:39
double factrl(int n)
void nDimFor(const vector< Type > &rvtStart, const vector< Type > &rvtEnd, vector< Type > &rvtIndices, bool bInit)
void init_rnd_seed()
double bico(int n, int k)
Definition: CObject.h:39
void readValueOfType(Type &rtValue, fstream &stream, DATATYPE tType)
Definition: Macros.h:60
void nDimForReverse(const vector< Type > &rvtStart, const vector< Type > &rvtEnd, vector< Type > &rvtIndices, bool bInit)
float gauss_rnd(void)
Type getRank(int iRank, int iDim, Type *pfVector)
Definition: CObject.h:39
Definition: CObject.h:39
double gammln(double xx)
double factln(int n)