Ajout de l'ensemble du workspace.
[GPU.git] / WCudaMSE / BilatTools_CPP / src / core / tools / cpp / MathTools.cpp
1 #include <iostream>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <math.h>
5
6 #include "MathTools.h"
7
8
9 using std::cout;
10 using std::endl;
11
12 #ifndef MIN
13 #define MIN(a,b) (((a)<(b))?(a):(b))
14 #endif
15
16 #ifndef MAX
17 #define MAX(a,b) (((a)>(b))?(a):(b))
18 #endif
19
20 #ifndef ABS
21 #define ABS(x) ((x)>0?(x):-(x))
22 #endif
23
24 #ifndef MAXABS
25 #define MAXABS(a,b) (ABS((a))>ABS((b)) ? ABS((a)): ABS((b)))
26 #endif
27
28
29 /*----------------------------------------------------------------------*\
30 |* Implementation *|
31 \*---------------------------------------------------------------------*/
32
33 /*--------------------------------------*\
34 |* Constructor *|
35 \*-------------------------------------*/
36
37 MathTools::MathTools()
38 {
39 // rien
40 }
41
42 MathTools::~MathTools()
43 {
44 // rien
45 }
46
47 /*--------------------------------------*\
48 |* Methodes static *|
49 \*-------------------------------------*/
50
51 /*----------------------*\
52 |* static *|
53 \*---------------------*/
54
55 /*---------------*\
56 |* Float *|
57 \*---------------*/
58
59 bool MathTools::isEquals(float x1, float x2, float epsilon)
60 {
61 bool isOk;
62 double delta;
63
64 if (x1 == 0 || x2 == 0)
65 {
66 delta = fabs(x1 - x2);
67 isOk = delta <= epsilon;
68 }
69 else
70 {
71 delta = fabs((x1 - x2) / MAXABS(x1,x2));
72 isOk = delta <= epsilon;
73 }
74
75 if (!isOk)
76 {
77 cout << "isEgale Float: (x1,x2)=(" << x1 << "," << x2 << ") : delta (Relatif) = " << delta << endl;
78 }
79
80 return isOk;
81 }
82
83
84 bool MathTools::isEquals(float a, float b, float reference, float epsilon)
85 {
86 return fabs((a - b) / reference) <= epsilon;
87 }
88
89 bool MathTools::isEquals(float* tabA, float* tabB, int n, float epsilon)
90 {
91 for (int i = 0; i < n; i++)
92 {
93 if (!isEquals(tabA[i], tabB[i], epsilon))
94 {
95 return false;
96 }
97 }
98 return true;
99 }
100
101 bool MathTools::isEqualsRelatifMax(float* tabA, float* tabB, int n, float epsilon)
102 {
103 float max = maxAbs(tabA, tabB, n);
104
105 if (max != 0)
106 {
107 for (int i = 0; i < n; i++)
108 {
109 if (!isEquals(tabA[i], tabB[i], max, epsilon))
110 {
111 return false;
112 }
113 }
114 return true;
115 }
116 else
117 {
118 return true;// tous à zero
119 //return isEquals(tabA, tabB, n, epsilon);
120 }
121 }
122
123 /*---------------*\
124 |* Double *|
125 \*---------------*/
126
127 bool MathTools::isEquals(double x1, double x2, double epsilon)
128 {
129 bool isOk;
130 double delta;
131 if (x1 == 0 || x2 == 0)
132 {
133 delta = fabs(x1 - x2);
134 isOk = delta <= epsilon;
135 }
136 else
137 {
138 delta = fabs((x1 - x2) / MAXABS(x1,x2));
139 isOk = delta <= epsilon;
140 }
141
142 if (!isOk)
143 {
144 cout << "isEgale Double : (x1,x2)=(" << x1 << "," << x2 << ") : delta (Relatif) = " << delta << endl;
145 }
146
147 return isOk;
148 }
149
150 /*---------------*\
151 |* Long *|
152 \*---------------*/
153
154 bool MathTools::isEquals(long x1, long x2)
155 {
156 long delta = labs(x1 - x2);
157 bool isOk = (delta == 0);
158
159 if (!isOk)
160 {
161 cout << "isEgale Long: (x1,x2)=(" << x1 << "," << x2 << ") : delta (Relatif) = " << delta << endl;
162 }
163
164 return isOk;
165 }
166
167 bool MathTools::isPower2(long i)
168 {
169 while (i >= 2)
170 {
171 if (i % 2 != 0)
172 {
173 return false;
174 }
175 i /= 2;
176 }
177 return true;
178 }
179
180 /*---------------*\
181 |* isPower2 *|
182 \*---------------*/
183
184 bool MathTools::isPower2(int i) // TODO operateur bit
185 {
186 while (i >= 2)
187 {
188 if (i % 2 != 0)
189 {
190 return false;
191 }
192 i /= 2;
193 }
194 return true;
195 }
196
197 bool MathTools::isPower2(unsigned int i) // TODO operateur bit
198 {
199 while (i >= 2)
200 {
201 if (i % 2 != 0)
202 {
203 return false;
204 }
205 i /= 2;
206 }
207 return true;
208 }
209
210 /*--------------------------------------*\
211 |* Methode static private *|
212 \*-------------------------------------*/
213
214 float MathTools::maxAbs(float a, float b)
215 {
216 return MAXABS(fabs(a), fabs(b));
217 }
218
219 float MathTools::maxAbs(float* tabA, float* tabB, int n)
220 {
221 float maxAbs = 0;
222
223 for (int i = 0; i < n; i++)
224 {
225 if (fabs(tabA[i]) > maxAbs)
226 {
227 maxAbs = fabs(tabA[i]);
228 }
229 if (fabs(tabB[i]) > maxAbs)
230 {
231 maxAbs = fabs(tabB[i]);
232 }
233 }
234
235 return maxAbs;
236 }
237
238
239 /*----------------------------------------------------------------------*\
240 |* End *|
241 \*---------------------------------------------------------------------*/
242