Blame view

src/Parameters/DataTypeMath.hh 46.6 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
/*
 * LogicalDataType.hh
 * @brief is a complement of ParamData.hh, contain mathematics function between ParamData element
 * @see paramData.hh
 *  Created on: Dec 21, 2012
 *      Author: f.casimir
 */

#ifndef LOGICALDATATYPE_HH_
#define LOGICALDATATYPE_HH_
f02d93eb   Hacene SI HADJ MOHAND   alfven ok
11
#define MU_0 1.25664e-06
3699433c   Hacene SI HADJ MOHAND   adding functions
12
#define RE 6.3712e+06
fbe3c2bb   Benjamin Renard   First commit
13
14
15

#include <vector>
#include <cmath>
c76233b2   Benjamin Renard   Check window time...
16
17
#include <boost/lexical_cast.hpp>

fbe3c2bb   Benjamin Renard   First commit
18
#include "ParamData.hh"
ab4d0b63   Elena.Budnik   AMDA_constants to...
19
#include "AMDA_constants.hh"
fbe3c2bb   Benjamin Renard   First commit
20

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
21
22
23
24
25
26
27
28
29
30
inline AMDA::Parameters::LogicalData castToLogicalData(int a) {
    if (isNAN(a)) {
        return AMDA::Parameters::LogicalData::NaN;
    }
    return (a >= 1) ? AMDA::Parameters::LogicalData::True : AMDA::Parameters::LogicalData::False;
}

inline AMDA::Parameters::LogicalData castToLogicalData(AMDA::Parameters::LogicalData a) {
    return a;
}
7578a549   Benjamin Renard   Add boolean suppo...
31

fbe3c2bb   Benjamin Renard   First commit
32
33
34
/**
 * @brief Operator And (&&) between LogicalData
 */
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
35
36
37
38
39
template<typename Type1, typename Type2>
inline AMDA::Parameters::LogicalData And(Type1 a, Type2 b) {
    AMDA::Parameters::LogicalData aa = castToLogicalData(a);
    AMDA::Parameters::LogicalData bb = castToLogicalData(b);
    AMDA::Parameters::LogicalData res = AMDA::Parameters::LogicalData::NaN;
384f25b6   Benjamin Renard   Implements operat...
40

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
41
42
43
    if (aa != AMDA::Parameters::LogicalData::NaN && bb != AMDA::Parameters::LogicalData::NaN) {
        res = ((aa == AMDA::Parameters::LogicalData::True) && (bb == AMDA::Parameters::LogicalData::True)) ? AMDA::Parameters::LogicalData::True : AMDA::Parameters::LogicalData::False;
    }
384f25b6   Benjamin Renard   Implements operat...
44

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
45
46
    return res;
}
384f25b6   Benjamin Renard   Implements operat...
47

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
 * @brief Operator Or (||) between LogicalData
 */
template<typename Type1, typename Type2>
inline AMDA::Parameters::LogicalData Or(Type1 a, Type2 b) {
    AMDA::Parameters::LogicalData aa = castToLogicalData(a);
    AMDA::Parameters::LogicalData bb = castToLogicalData(b);
    AMDA::Parameters::LogicalData res = AMDA::Parameters::LogicalData::NaN;

    if (aa != AMDA::Parameters::LogicalData::NaN && bb != AMDA::Parameters::LogicalData::NaN) {
        res = ((aa == AMDA::Parameters::LogicalData::True) || (bb == AMDA::Parameters::LogicalData::True)) ? AMDA::Parameters::LogicalData::True : AMDA::Parameters::LogicalData::False;
    }

    return res;
}

/**
 * @brief Operator greater_than (>) between LogicalData
 */
template <typename Type1, typename Type2>
AMDA::Parameters::LogicalData greater_than(Type1 a, Type2 b) {
    AMDA::Parameters::LogicalData res = AMDA::Parameters::LogicalData::True;
    if (std::isfinite(a) && std::isfinite(b)) {
        res = (a > b) ? AMDA::Parameters::LogicalData::True : AMDA::Parameters::LogicalData::False;
    } else {
        res = AMDA::Parameters::LogicalData::NaN;
    }
    return res;
}

/**
 * @brief Operator greater_than (>) between table of LogicalData
 */
template<typename Type1, typename Type2>
std::vector<AMDA::Parameters::LogicalData> greater_than(std::vector<Type1> a,
        std::vector<Type2> b) {
    std::vector<AMDA::Parameters::LogicalData> r;
    if (a.size() != b.size()) {
        BOOST_THROW_EXCEPTION(
                AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Logical comparison between two vector of different size"));
    }

    for (unsigned int i = 0; i < a.size(); ++i) {
        r.push_back(greater_than(a[i], b[i]));
    }
    return r;
}

/**
 * @brief Operator lower_than (<) between table of LogicalData
 */
template <typename Type1, typename Type2>
AMDA::Parameters::LogicalData lower_than(Type1 a, Type2 b) {
    AMDA::Parameters::LogicalData res = AMDA::Parameters::LogicalData::True;
    if (std::isfinite(a) && std::isfinite(b)) {
        res = (a < b) ? AMDA::Parameters::LogicalData::True : AMDA::Parameters::LogicalData::False;
    } else {
        res = AMDA::Parameters::LogicalData::NaN;
    }
    return res;
}

/**
 * @brief Operator lower_than (<) between table of LogicalData
 */
template<typename Type1, typename Type2>
std::vector<AMDA::Parameters::LogicalData> lower_than(std::vector<Type1> a,
        std::vector<Type2> b) {
    std::vector<AMDA::Parameters::LogicalData> r;
    if (a.size() != b.size()) {
        BOOST_THROW_EXCEPTION(
                AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Logical comparison between two vector of different size"));
    }

    for (unsigned int i = 0; i < a.size(); ++i) {
        r.push_back(lower_than(a[i], b[i]));
    }
    return r;
}

/**
 * @brief Operator * between two scalars, one is LogicalData
 */
template <typename Type>
Type operator*(Type a, AMDA::Parameters::LogicalData b) {
    Type r;
    r << NotANumber();
    if (isNAN(a) || isNAN(b)) {
        return r;
    }
    r = a * (Type) b;
    return r;
}

template <typename Type>
Type operator*(AMDA::Parameters::LogicalData a, Type b) {
    return b * a;
}

/**
 * @brief Operator / between two scalars, one is LogicalData
 */
template <typename Type>
Type operator/(Type a, AMDA::Parameters::LogicalData b) {
    Type r;
    r << NotANumber();
    if (isNAN(a) || isNAN(b) || (b == AMDA::Parameters::LogicalData::False)) {
        return r;
    }
    r = a / (Type) b;
    return r;
}

template <typename Type>
Type operator/(AMDA::Parameters::LogicalData a, Type b) {
    Type r;
    r << NotANumber();
    if (isNAN(a) || isNAN(b) || (b == 0)) {
        return r;
    }
    r = (Type) a / b;
    return r;
}

/**
 * @brief Operator + between two scalars, one is LogicalData
 */
template <typename Type>
Type operator+(Type a, AMDA::Parameters::LogicalData b) {
    Type r;
    r << NotANumber();
    if (isNAN(a) || isNAN(b)) {
        return r;
    }
    r = a + (Type) b;
    return r;
}

template <typename Type>
Type operator+(AMDA::Parameters::LogicalData a, Type b) {
    return b + a;
}

/**
 * @brief Operator - between two scalars, one is LogicalData
 */
template <typename Type>
Type operator-(Type a, AMDA::Parameters::LogicalData b) {
    Type r;
    r << NotANumber();
    if (isNAN(a) || isNAN(b)) {
        return r;
    }
    r = a - (Type) b;
    return r;
}

template <typename Type>
Type operator-(AMDA::Parameters::LogicalData a, Type b) {
    Type r;
    r << NotANumber();
    if (isNAN(a) || isNAN(b)) {
        return r;
    }
    r = (Type) a - b;
    return r;
}

/**
 * @brief Operator addition between  two  vector
 * @details
 * 		r[0] = a[0] + b[0]
 * 		r[1] = a[1] + b[1]
 * 		r[2] = a[2] + b[2]
 * 	@return r
 */
template <typename Type1, typename Type2>
std::vector<Type1> operator+(std::vector<Type1> a, std::vector<Type2> b) {
    std::vector<Type1> r;
    if (a.size() != b.size()) {
        BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Add two vector of different size"));
    }

    for (unsigned int i = 0; i < a.size(); ++i) {
        r.push_back((Type1) (a[i] + b[i]));
    }
    return r;
}

/**
 * @brief Operator subtraction between  two  vector
 * @details
 * 		r[0] = a[0] - b[0]
 * 		r[1] = a[1] - b[1]
 * 		r[2] = a[2] - b[2]
 * 	@return r
 */
template <typename Type1, typename Type2>
std::vector<Type1> operator-(std::vector<Type1> a, std::vector<Type2> b) {
    std::vector<Type1> r;
    if (a.size() != b.size()) {
        BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Subtract two vector of different size"));
    }

    for (unsigned int i = 0; i < a.size(); ++i) {
        r.push_back(a[i] - b[i]);
    }
    return r;
}
22493b20   Benjamin Renard   Add dot product f...
257

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
 * @brief Operator dot product between  two  vector
 * @details
 * 		r = a[0]*b[0] + a[1]*b[1] + a[2]+b[2]
 * 	@return the scalar r
 */
template <typename Type1, typename Type2>
Type1 operator*(std::vector<Type1> a, std::vector<Type2> b) {
    Type1 r = 0;
    if (a.size() != b.size()) {
        BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Multiply two vector of different size"));
    }

    for (unsigned int i = 0; i < a.size(); ++i) {
        r += a[i] * b[i];
    }
    return r;
}

/**
 * @brief Function dot between two vectors can be used for dot product
 */
template <typename Type1, typename Type2>
Type1 dot(std::vector<Type1> a, std::vector<Type2> b) {
    return a*b;
}

template <typename Type>
677cc39b   Hacene SI HADJ MOHAND   removing plugin M...
286
Type magnitude(std::vector<Type> a) {
f2cf89d8   Hacene SI HADJ MOHAND   ok 7528
287
288
289
290
    Type mag = 0;
    for (unsigned int i = 0; i < a.size(); ++i)
        mag += a[i] * a[i];
    return std::sqrt(mag);
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
291
292
293
294
}

template <typename Type1, typename Type2>
Type1 angle(std::vector<Type1> a, std::vector<Type2> b) {
f2cf89d8   Hacene SI HADJ MOHAND   ok 7528
295
296
297
298
    if (magnitude(a) == 0 || magnitude(b) == 0) {
        BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("One of the two vectors is NULL"));
    }
    Type1 res;
f02d93eb   Hacene SI HADJ MOHAND   alfven ok
299
    if (dot(a, b) / magnitude(a) / magnitude(b) > 0.99999) {
f2cf89d8   Hacene SI HADJ MOHAND   ok 7528
300
        res = 0;
f02d93eb   Hacene SI HADJ MOHAND   alfven ok
301
302
    } else {
        res = std::acos(dot(a, b) / magnitude(a) / magnitude(b));
f2cf89d8   Hacene SI HADJ MOHAND   ok 7528
303
304
    }
    return res;
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
305
306
}

ed087564   Hacene SI HADJ MOHAND   function fairfiel...
307
308
309
310
311
312
/**
 * @brief alfvenVelocity : args magnetic field magnitude and density
 * @details
 * 
 * V= mag / std::sqrt(MU_0 * density)
 */
f02d93eb   Hacene SI HADJ MOHAND   alfven ok
313
314
template <typename Type1, typename Type2>
Type1 alfvenVelocity(Type1 density, Type2 mag) {
ed087564   Hacene SI HADJ MOHAND   function fairfiel...
315
    Type1 val;
f02d93eb   Hacene SI HADJ MOHAND   alfven ok
316
317
318
    if (density <= 0) {
        val << NotANumber();
    } else {
ed087564   Hacene SI HADJ MOHAND   function fairfiel...
319
        val = mag / std::sqrt(MU_0 * density);
f02d93eb   Hacene SI HADJ MOHAND   alfven ok
320
    }
ed087564   Hacene SI HADJ MOHAND   function fairfiel...
321
322
323
324
325
326
327
    return val;
}

/**
 * @brief  FAIRFIELD 1970 model of neutral sheet position 
 * @details
 * z_sm-std::sqrt(pow(11.0, 2.0) - pow(11.0, 2.0) / pow(15.0, 2.0) * pow(y_sm, 2.0)) * std::sin(x_ss);
38bebd9c   Hacene SI HADJ MOHAND   lib Jupeter field...
328
 * where y_sm and z_sm  are the solar magnetospheric coordinates of the spacecraft and x_ss is geomagnetic latitude of the sun  (tilt angle) 
3ff34f08   Hacene SI HADJ MOHAND   fairfield70 et fa...
329

ed087564   Hacene SI HADJ MOHAND   function fairfiel...
330
331
332
333
template <typename Type1, typename Type2, typename Type3>
Type1 fairfield70(Type1  y_sm, Type2 z_sm,Type3 x_ss) {
    Type1 val = z_sm-std::sqrt(pow(11.0, 2.0) - pow(11.0, 2.0) / pow(15.0, 2.0) * pow(y_sm, 2.0)) * std::sin(x_ss);
    return val;
f02d93eb   Hacene SI HADJ MOHAND   alfven ok
334
}
3ff34f08   Hacene SI HADJ MOHAND   fairfield70 et fa...
335
 *  */
24effbb7   Elena.Budnik   Fairfield angle i...
336

3ff34f08   Hacene SI HADJ MOHAND   fairfield70 et fa...
337
/**
24effbb7   Elena.Budnik   Fairfield angle i...
338
 * @brief FAIRFIELD 1970 model of neutral sheet position : ..sqrt(11^2 - 11^2/15^2*y^2)... 
3ff34f08   Hacene SI HADJ MOHAND   fairfield70 et fa...
339
 * @param xyz_sm SM coordinates of the sat
24effbb7   Elena.Budnik   Fairfield angle i...
340
341
 * @param x_ss tilt Angle ( deg )
 * @return delta Z 
3ff34f08   Hacene SI HADJ MOHAND   fairfield70 et fa...
342
343
344
 */
template <typename Type1, typename Type2>
Type1 fairfield70(std::vector<Type1>  xyz_sm, Type2 x_ss) {
ab4d0b63   Elena.Budnik   AMDA_constants to...
345
    Type1 val = xyz_sm[2]-std::sqrt(121.0 - 0.537778 * pow(xyz_sm[1], 2.0)) * std::sin(x_ss*DEG2RAD);
3ff34f08   Hacene SI HADJ MOHAND   fairfield70 et fa...
346
347
348
349
350
351
352
    return val;
}

/**
 * @brief FAIRFIELD 1980 model of neutral sheet position  
 * @param xyz_sm SM coordinates of the sat
 * @param x_ss tilt Angle
24effbb7   Elena.Budnik   Fairfield angle i...
353
 * @return delta Z 
3ff34f08   Hacene SI HADJ MOHAND   fairfield70 et fa...
354
355
356
 */
template <typename Type1, typename Type2>
Type1 fairfield80(std::vector<Type1>  xyz_sm, Type2 x_ss) {
ab4d0b63   Elena.Budnik   AMDA_constants to...
357
    Type1 val = xyz_sm[2]-10.5*std::sqrt(1.0-pow(xyz_sm[1], 2.0)/289.0)* std::sin(x_ss * DEG2RAD);
3ff34f08   Hacene SI HADJ MOHAND   fairfield70 et fa...
358
359
    return val;
}
f02d93eb   Hacene SI HADJ MOHAND   alfven ok
360

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
361
/**
3699433c   Hacene SI HADJ MOHAND   adding functions
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
 * @brief  Perrault and Akasofu 1978 geomagnetic storms
 * @details
 *     u(t) = E_mag(t)*B_mag(t)/4pi(7RE sin²(theta_p(t)/2))²
 * where u(t) is total energy dissipation
 *            E_mag(t) The  magnitude of  the solar-wind electric field
 *            theta_p is a measure of the angle between the geomagnetic field vector and the IMF vector at the front of the 
 *            B_mag(t) The magnitude of the IMF magnetosphere in the equatorial plane. theta_p = pi/2 -theta where theta 
 *            denotes the latitudinal angle of the IMF vector measured from the solar equatorial plane.
 */
template <typename Type1, typename Type2, typename Type3>
Type1 perreault78(Type1  E_mag, Type2 B_mag,Type3 theta_p) {
    Type1 val = E_mag*B_mag/(4*std::atan(1)*4)*std::pow((7*RE*std::pow(sin(theta_p/2),2)),2);
    return val;
}

/**
 * @brief  NEWELL et al 2007 solar wind-magnetosphere coupling function 
 * @details d_phi/d_t = mu^4/3*Bt^2/3*sin(theta/2)^8/3
 * where mu is rate IMF field lines approach the magnetopause
 *            B_mag interplanetary field magnitude,
 *            theta IMF clock angle arctan(By/Bz)
 */
1d171be3   Hacene SI HADJ MOHAND   newell
384
385
386
387
388
template <typename Type1, typename Type2>
Type1 newell2007(Type1  mu, std::vector<Type2> Bxyz) {
    Type2 B_mag = std::sqrt(Bxyz[0]*Bxyz[0] + Bxyz[1]*Bxyz[1] + Bxyz[2]*Bxyz[2]);
    Type2 theta = std::atan2(Bxyz[1]/Bxyz[2]);
    Type1 val = std::pow(mu , 4.0/3.0)*std::pow(B_mag , 2.0/3.0)*pow(std::sin(theta/2),8.0/3.0);
3699433c   Hacene SI HADJ MOHAND   adding functions
389
390
391
392
    return val; 
}

/**
ed087564   Hacene SI HADJ MOHAND   function fairfiel...
393
394
395
396
397
398
399
400
 * @brief  Lopez 1990 model of magnetic latitude of the neutral sheet
 * @details
 *MLAT = -(0.14*Kp+0.69)*std::pow(phi, 1.0/3.0)*(0.065*R-0.16)*DTA;
 * where Kp is the 3-hour magnetic index, phi the magnetic local time in degrees (phi = 0° at midnight)
 * R is the radial distance in RE and DTA is the dipole tilt angle 
 */
template <typename Type1, typename Type2, typename Type3, typename Type4>
Type1 lopez90(Type1  Kp, Type2 phi,Type3 R, Type4  DTA) {
ab4d0b63   Elena.Budnik   AMDA_constants to...
401
    Type1 val = -(0.14*Kp+0.69)*std::pow(phi, 0.3333)*(0.065*R-0.16)*DTA*DEG2RAD;
ed087564   Hacene SI HADJ MOHAND   function fairfiel...
402
403
404
405
406
407
    return val;
}



/**
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
 * @brief Operator cross product between  two  vector
 * @details
 * 		r[0] = a[1]*b[2] - a[2]*b[1]
 * 		r[1] = a[2]*b[0] - a[0]*b[2]
 * 		r[2] = a[0]*b[1] - a[1]*b[0]
 * 	@return r
 */
template <typename Type1, typename Type2>
std::vector<Type1> operator ^(std::vector<Type1> a, std::vector<Type2> b) {
    std::vector<Type1> r;
    if (a.size() != b.size() || b.size() != 3) {
        BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vector product on two vector of different size"));
    }

    r.push_back(a[1] * b[2] - a[2] * b[1]);
    r.push_back(a[2] * b[0] - a[0] * b[2]);
    r.push_back(a[0] * b[1] - a[1] * b[0]);

    return r;
}

/**
 * @brief Function cross between two vectors can be used for cross product
 */
template <typename Type1, typename Type2>
std::vector<Type1> cross(std::vector<Type1> a, std::vector<Type2> b) {
    return a^b;
}

/**
 * @brief div each coordinate one to one
 * @details
 * 		r[0] = a[0] / b[0]
 * 		r[1] = a[1] / b[1]
 * 		r[2] = a[2] /  b[2]
 */
template <typename Type1, typename Type2>
std::vector<Type1> div(std::vector<Type1> a, std::vector<Type2> b) {
    std::vector<Type1> r;
    if (a.size() != b.size()) {
        BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vector div on two vector of different size"));

    }

    for (unsigned int i = 0; i < a.size(); ++i) {
        r.push_back(a[i] / b[i]);
    }

    return r;
}

//
//Idea but more complex: template <typename Type1, typename Type2>
//auto operator ^(std::vector<Type1> a, std::vector<Type2> b)  -> std::vector<decltype(Type1()*Type2())> {
//	std::vector<decltype(Type1()*Type2())> r;
fbe3c2bb   Benjamin Renard   First commit
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488


//	Idea but more complex: template <typename Type1, typename Type2>
//	double operator ^(Type1 a, Type2 b) {
//		return pow(a, b);
//	}
//	class Integer {
//		public:
//		Integer(int i) : _i(i) {};
//		int _i;
//	};
//
//	inline double operator ^(double a, Integer b) {
//		return pow(a, b._i);
//	}
//
//
//	class Double {
//		public:
//		Double(double i) : _i(i) {};
//		double _i;
//	};
//	inline double operator ^(double a, Double b) {
//		return pow(a, b._i);
//	}

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
// vector with scalar operation

/**
 * @brief Operator division between  a  vector and a scalar
 * @details all member of vector are divided by the scalar
 */
template <typename Type1, typename Type2>
std::vector<Type1> operator/(std::vector<Type1> a, Type2 b) {
    std::vector<Type1> r;
    for (typename std::vector<Type1>::iterator it = a.begin(); it != a.end(); ++it) {
        r.push_back((Type1) (*it / b));
    }
    return r;
}

/**
 * @brief Operator division between a scalar and a vector
 * @details apply the division on each vector component
 */
template <typename Type1, typename Type2>
std::vector<Type2> operator/(Type1 a, std::vector<Type2> b) {
    std::vector<Type2> r;
    for (typename std::vector<Type2>::iterator it = b.begin(); it != b.end(); ++it) {
        r.push_back((Type2) (a / *it));
    }
    return r;
}

/**
 * @brief Operator addition between  a  vector and a scalar
 * @details all member of vector are added with the scalar
 */
template <typename Type1, typename Type2>
std::vector<Type1> operator+(std::vector<Type1> a, Type2 b) {
    std::vector<Type1> r;
    for (typename std::vector<Type1>::iterator it = a.begin(); it != a.end(); ++it) {
        r.push_back((Type1) (*it + b));
    }
    return r;
}

/**
 * @brief Operator addition between a scalar and a  vector
 * @details all member of vector are added with the scalar
 */
template <typename Type1, typename Type2>
std::vector<Type2> operator+(Type1 a, std::vector<Type2> b) {
    std::vector<Type2> r;
    for (typename std::vector<Type2>::iterator it = b.begin(); it != b.end(); ++it) {
        r.push_back((Type2) (a + *it));
    }
    return r;
}

/**
 * @brief Operator subtraction  between a vector and a scalar
 * @details scalar is substracted to all vector components
 */
template <typename Type1, typename Type2>
std::vector<Type1> operator-(std::vector<Type1> a, Type2 b) {
    std::vector<Type1> r;
    for (typename std::vector<Type1>::iterator it = a.begin(); it != a.end(); ++it) {
        r.push_back((Type1) (*it - b));
    }
    return r;
}

/**
 * @brief Operator subtraction  between a scalar and a  vector
 * @details all member of vector are subtracted with the scalar
 */
template <typename Type1, typename Type2>
std::vector<Type2> operator-(Type1 a, std::vector<Type2> b) {
    std::vector<Type2> r;
    for (typename std::vector<Type2>::iterator it = b.begin(); it != b.end(); ++it) {
        r.push_back((Type2) (a - *it));
    }
    return r;
}

/**
 * @brief Operator product between a vector and a scalar
 * @details product applied in all member of vector
 */
template <typename Type1, typename Type2>
std::vector<Type1> operator*(std::vector<Type1> a, Type2 b) {
    std::vector<Type1> r;
    for (typename std::vector<Type1>::iterator it = a.begin(); it != a.end(); ++it) {
        r.push_back((Type1) ((*it) * b));
    }
    return r;
}

/**
 * @brief Operator product between a scalar and a vector
 * @details all member of vector are added by the scalar
 */
template <typename Type1, typename Type2>
std::vector<Type2> operator*(Type1 a, std::vector<Type2> b) {
    std::vector<Type2> r;
    for (typename std::vector<Type2>::iterator it = b.begin(); it != b.end(); ++it) {
        r.push_back((Type2) (a * (*it)));
    }
    return r;
}

/**
 * @return false if val is NaN or inf or -inf
 */
template <typename Type>
inline bool isFinite(Type val) {
    return std::isfinite(val);
}

inline bool isFinite(AMDA::Parameters::LogicalData val) {
    return !isNAN(val);
}

/**
 * @return false if elem of val is NaN or inf or -inf
 */
template <typename Type>
inline bool isFinite(std::vector<Type> val) {
    bool ret = false;
    auto it = val.begin();
    while (it != val.end() && !ret) {
        ret &= std::isfinite(*it);
        ++it;
    }
    return ret;
}

/**
 * @return false if elem of val is NaN or inf or -inf
 */
template <typename Type>
inline bool isFinite(const AMDA::Parameters::Tab2DData<Type>& val) {
    bool ret = false;
    for (int i = 0; i < val.getDim1Size(); ++i)
        for (int j = 0; j < val.getDim2Size(); ++j)
            ret &= std::isfinite(val[i][j]);
    return ret;
}

/**
 * @return the average of std::list
 */
template <typename Type>
inline Type average(std::list<Type> tab, int /*dim1*/, int /*dim2*/) {
    Type mean;
    if (tab.empty()) {
        mean << NotANumber();
    } else {
        mean << ElemNull();
        auto it = tab.begin();
        if (it != tab.end()) {
            unsigned int nbNan = 0;
            Type sum;
            sum << NotANumber();
            for (; it != tab.end(); ++it) {
                if (isFinite(*it) && !isNAN(*it)) {
                    if (isNAN(sum))
                        sum = *it;
                    else
                        sum = sum + *it;
                } else {
                    ++nbNan;
                }
            }
            if ((tab.size() - nbNan == 0) || isNAN(sum)) {
                mean << NotANumber();
            } else {
                mean = sum / (int) (tab.size() - nbNan);
            }
        } else {
            mean << NotANumber();
        }
    }
    return mean;
}

inline AMDA::Parameters::LogicalData average(std::list<AMDA::Parameters::LogicalData> /*tab*/, int /*dim1*/, int /*dim2*/) {
    AMDA::Parameters::LogicalData mean;
    mean << NotANumber();
    return mean;
}

template <typename Type>
inline AMDA::Parameters::Tab2DData<Type> average(std::list<AMDA::Parameters::Tab2DData<Type> > tab, int dim1, int dim2) {
    AMDA::Parameters::Tab2DData<Type> mean(dim1, dim2);
    AMDA::Parameters::Tab2DData<unsigned int > nbNan(dim1, dim2);
    AMDA::Parameters::Tab2DData<Type> sum(dim1, dim2);
    if (tab.empty()) {
        mean << NotANumber();
        return mean;
    }
    for (int index1 = 0; index1 < dim1; ++index1) {
        for (int index2 = 0; index2 < dim2; ++index2) {
            nbNan[index1][index2] = 0;
        }
    }
    sum << NotANumber();
    for (auto it = tab.begin(); it != tab.end(); ++it) {
        for (int index1 = 0; index1 < dim1; ++index1) {
            for (int index2 = 0; index2 < dim2; ++index2) {
                Type val = (*it)[index1][index2];
                if (std::isfinite(val) && !isNAN(val)) {
                    if (isNAN(sum[index1][index2]))
                        sum[index1][index2] = val;
                    else
                        sum[index1][index2] += val;
                } else {
                    ++(nbNan[index1][index2]);
                }
            }
        }
    }
    for (int index1 = 0; index1 < dim1; ++index1) {
        for (int index2 = 0; index2 < dim2; ++index2) {
            if (tab.size() - nbNan[index1][index2] == 0) {
                mean[index1][index2] << NotANumber();
            } else {
                mean[index1][index2] = sum[index1][index2] / (int) (tab.size() - nbNan[index1][index2]);
            }
8aa97375   Benjamin Renard   Add some missing ...
713
        }
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
714
715
716
717
    }

    return mean;
}
fbe3c2bb   Benjamin Renard   First commit
718

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
inline AMDA::Parameters::Tab2DData<AMDA::Parameters::LogicalData> average(std::list<AMDA::Parameters::Tab2DData<AMDA::Parameters::LogicalData>> /*tab*/, int dim1, int dim2) {
    AMDA::Parameters::Tab2DData<AMDA::Parameters::LogicalData> mean(dim1, dim2);
    mean << NotANumber();
    return mean;
}

/**
 * @return the average of std::list
 */
template <typename Type>
inline std::vector<Type> average(std::list<std::vector<Type> > tab, int dim1, int /*dim2*/) {
    std::vector<Type> mean;
    mean.resize(dim1);
    std::vector<unsigned int >nbNan;
    nbNan.resize(dim1);
    std::vector<Type> sum;
    sum.resize(dim1);
    if (tab.empty()) {
        mean << NotANumber();
        return mean;
    }
    for (unsigned int index = 0; index < tab.begin()->size(); ++index) {
        nbNan[index] = 0;
    }
    sum << NotANumber();
    for (auto it = tab.begin(); it != tab.end(); ++it) {
        for (unsigned int index = 0; index < it->size(); ++index) {
            Type val = it->at(index);
            if (std::isfinite(val) && !isNAN(val)) {
                if (isNAN(sum[index]))
                    sum[index] = val;
                else
                    sum[index] += val;
            } else {
                ++(nbNan[index]);
            }
        }
    }
    for (unsigned int index = 0; index < tab.begin()->size(); ++index) {
        if (tab.size() - nbNan[index] == 0) {
            mean[index] << NotANumber();
        } else {
            mean[index] = sum[index] / (int) (tab.size() - nbNan[index]);
7578a549   Benjamin Renard   Add boolean suppo...
762
        }
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
763
764
    }

7578a549   Benjamin Renard   Add boolean suppo...
765

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
766
767
    return mean;
}
fbe3c2bb   Benjamin Renard   First commit
768

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
769
770
771
772
773
774
inline std::vector<AMDA::Parameters::LogicalData> average(std::list<std::vector<AMDA::Parameters::LogicalData>> /*tab*/, int dim1, int /*dim2*/) {
    std::vector<AMDA::Parameters::LogicalData> mean;
    mean.resize(dim1);
    mean << NotANumber();
    return mean;
}
7578a549   Benjamin Renard   Add boolean suppo...
775

2f932bad   Benjamin Renard   Fix interpolation...
776
777
778
/**
 *
 */
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
779
780
781
782
783
784
785
786
787
template <typename Type>
inline Type interpolation(const Type& leftVal, const Type& rightVal, double coef, int /*dim1*/, int /*dim2*/) {
    Type val;
    val << NotANumber();
    if (isNAN(leftVal) || isNAN(rightVal) || coef < 0. || coef > 1.)
        return val;
    val = leftVal + (rightVal - leftVal) * coef;
    return val;
}
2f932bad   Benjamin Renard   Fix interpolation...
788

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
789
790
791
792
793
794
795
796
797
inline AMDA::Parameters::LogicalData interpolation(const AMDA::Parameters::LogicalData& leftVal, const AMDA::Parameters::LogicalData& rightVal, double /*coef*/, int /*dim1*/, int /*dim2*/) {
    AMDA::Parameters::LogicalData val;
    if (leftVal == rightVal) {
        val = leftVal;
    } else {
        val << NotANumber();
    }
    return val;
}
7578a549   Benjamin Renard   Add boolean suppo...
798

2f932bad   Benjamin Renard   Fix interpolation...
799
800
801
802
/**
 * 
 */

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
template <typename Type>
inline AMDA::Parameters::Tab2DData<Type> interpolation(const AMDA::Parameters::Tab2DData<Type>& leftVal, const AMDA::Parameters::Tab2DData<Type>& rightVal, double coef, int dim1, int dim2) {
    AMDA::Parameters::Tab2DData<Type> val(dim1, dim2);
    val << NotANumber();
    for (int index1 = 0; index1 < dim1; ++index1) {
        for (int index2 = 0; index2 < dim2; ++index2) {
            Type leftValComp = leftVal[index1][index2];
            Type rightValComp = rightVal[index1][index2];
            val[index1][index2] = interpolation(leftValComp, rightValComp, coef, 1, 1);
        }
    }
    return val;
}

template <typename Type>
inline std::vector<Type> interpolation(const std::vector<Type>& leftVal, const std::vector<Type>& rightVal, double coef, int dim1, int /*dim2*/) {
    std::vector<Type> val;
    val.resize(dim1);
    val << NotANumber();
    for (int index = 0; index < dim1; ++index) {
        Type leftValComp = leftVal[index];
        Type rightValComp = rightVal[index];
        val[index] = interpolation(leftValComp, rightValComp, coef, 1, 1);
    }
    return val;
}
fbe3c2bb   Benjamin Renard   First commit
829
830
831
832
833

/**
 * @brief Operator + between Tab2D and scalar
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
834
835
836
837
838
839
840
841
842
843
844
845
846
847
AMDA::Parameters::Tab2DData<Type1> operator+(const AMDA::Parameters::Tab2DData<Type1>& a, Type2 b) {
    AMDA::Parameters::Tab2DData<Type1> r(a);

    if (isNAN(b)) {
        r << NotANumber();
        return r;
    }

    for (int i = 0; i < r.getDim1Size(); ++i)
        for (int j = 0; j < r.getDim2Size(); ++j) {
            if (!isNAN(r[i][j]))
                r[i][j] += b;
        }
    return r;
fbe3c2bb   Benjamin Renard   First commit
848
849
850
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
851
852
AMDA::Parameters::Tab2DData<Type2> operator+(Type1 a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    return (b + a);
fbe3c2bb   Benjamin Renard   First commit
853
854
855
856
857
858
}

/**
 * @brief Operator - between Tab2D and scalar
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
859
860
AMDA::Parameters::Tab2DData<Type1> operator-(const AMDA::Parameters::Tab2DData<Type1>& a, Type2 b) {
    return (a + (-b));
fbe3c2bb   Benjamin Renard   First commit
861
862
863
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
864
865
866
867
868
869
870
871
872
873
874
875
876
877
AMDA::Parameters::Tab2DData<Type2> operator-(Type1 a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    AMDA::Parameters::Tab2DData<Type2> r(b);

    if (isNAN(a)) {
        r << NotANumber();
        return r;
    }

    for (int i = 0; i < r.getDim1Size(); ++i)
        for (int j = 0; j < r.getDim2Size(); ++j) {
            if (!isNAN(r[i][j]))
                r[i][j] = a - r[i][j];
        }
    return r;
fbe3c2bb   Benjamin Renard   First commit
878
879
880
881
882
883
}

/**
 * @brief Operator * between Tab2D and scalar
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
884
885
886
887
888
889
890
891
892
893
894
895
896
897
AMDA::Parameters::Tab2DData<Type1> operator*(const AMDA::Parameters::Tab2DData<Type1>& a, Type2 b) {
    AMDA::Parameters::Tab2DData<Type1> r(a);

    if (isNAN(b)) {
        r << NotANumber();
        return r;
    }

    for (int i = 0; i < r.getDim1Size(); ++i)
        for (int j = 0; j < r.getDim2Size(); ++j) {
            if (!isNAN(r[i][j]))
                r[i][j] *= b;
        }
    return r;
fbe3c2bb   Benjamin Renard   First commit
898
899
900
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
901
902
AMDA::Parameters::Tab2DData<Type2> operator*(Type1 a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    return (b * a);
fbe3c2bb   Benjamin Renard   First commit
903
904
905
906
907
908
}

/**
 * @brief Operator / between Tab2D and scalar
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
909
910
911
912
913
914
915
AMDA::Parameters::Tab2DData<Type1> operator/(const AMDA::Parameters::Tab2DData<Type1>& a, Type2 b) {
    if (b == 0) {
        AMDA::Parameters::Tab2DData<Type1> r(a);
        r << NotANumber();
        return r;
    }
    return (a * (1. / b));
fbe3c2bb   Benjamin Renard   First commit
916
917
918
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
AMDA::Parameters::Tab2DData<Type2> operator/(Type1 a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    AMDA::Parameters::Tab2DData<Type2> r(b);

    if (isNAN(a)) {
        r << NotANumber();
        return r;
    }

    for (int i = 0; i < r.getDim1Size(); ++i)
        for (int j = 0; j < r.getDim2Size(); ++j) {
            if (isNAN(r[i][j]) || (r[i][j] == 0))
                r[i][j] << NotANumber();
            else
                r[i][j] = a / r[i][j];
        }
    return r;
fbe3c2bb   Benjamin Renard   First commit
935
936
937
938
939
940
}

/*
 * @brief Function add between a Tab2D and a vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
AMDA::Parameters::Tab2DData<Type1> add(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b, int mode) {
    AMDA::Parameters::Tab2DData<Type1> r(a);

    switch (mode) {
        case 1:
            if (r.getDim1Size() != b.size()) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
            }

            for (int i = 0; i < r.getDim1Size(); ++i)
                for (int j = 0; j < r.getDim2Size(); ++j) {
                    if (isNAN(r[i][j]) || isNAN(b[i]))
                        r[i][j] << NotANumber();
                    else
                        r[i][j] += b[i];
                }
            break;
        case 2:
            if (r.getDim2Size() != b.size()) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
            }

            for (int i = 0; i < r.getDim1Size(); ++i)
                for (int j = 0; j < r.getDim2Size(); ++j) {
                    if (isNAN(r[i][j]) || isNAN(b[j]))
                        r[i][j] << NotANumber();
                    else
                        r[i][j] += b[j];
                }
            break;
        default:
            BOOST_THROW_EXCEPTION(
                    AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due a not implemented mode"));
    }
    return r;
fbe3c2bb   Benjamin Renard   First commit
978
979
980
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
981
982
AMDA::Parameters::Tab2DData<Type2> add(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b, int mode) {
    return add(b, a, mode);
fbe3c2bb   Benjamin Renard   First commit
983
984
985
986
987
988
}

/**
 * @brief Operator + between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
989
990
AMDA::Parameters::Tab2DData<Type1> operator+(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b) {
    return add(a, b, 1);
fbe3c2bb   Benjamin Renard   First commit
991
992
993
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
994
995
AMDA::Parameters::Tab2DData<Type2> operator+(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    return add(a, b, 1);
fbe3c2bb   Benjamin Renard   First commit
996
997
998
999
1000
1001
}

/**
 * @brief Function sub between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1002
1003
AMDA::Parameters::Tab2DData<Type1> sub(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b, int mode) {
    return add(a, (-1) * b, mode);
fbe3c2bb   Benjamin Renard   First commit
1004
1005
1006
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
AMDA::Parameters::Tab2DData<Type2> sub(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b, int mode) {
    AMDA::Parameters::Tab2DData<Type2> r(b);

    switch (mode) {
        case 1:
            if (r.getDim1Size() != a.size()) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
            }

            for (int i = 0; i < r.getDim1Size(); ++i)
                for (int j = 0; j < r.getDim2Size(); ++j) {
                    if (isNAN(r[i][j]) || isNAN(a[i]))
                        r[i][j] << NotANumber();
                    else
                        r[i][j] = a[i] - r[i][j];
                }
            break;
        case 2:
            if (r.getDim2Size() != a.size()) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
            }

            for (int i = 0; i < r.getDim1Size(); ++i)
                for (int j = 0; j < r.getDim2Size(); ++j) {
                    if (isNAN(r[i][j]) || isNAN(a[j]))
                        r[i][j] << NotANumber();
                    else
                        r[i][j] = a[j] - r[i][j];
                }
            break;
        default:
            BOOST_THROW_EXCEPTION(
                    AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due a not implemented mode"));
    }
    return r;
fbe3c2bb   Benjamin Renard   First commit
1044
1045
1046
1047
1048
1049
}

/**
 * @brief Operator - between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1050
1051
AMDA::Parameters::Tab2DData<Type1> operator-(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b) {
    return sub(a, b, 1);
fbe3c2bb   Benjamin Renard   First commit
1052
1053
1054
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1055
1056
AMDA::Parameters::Tab2DData<Type2> operator-(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    return sub(a, b, 1);
fbe3c2bb   Benjamin Renard   First commit
1057
1058
1059
1060
1061
1062
}

/**
 * @brief Function mult between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
AMDA::Parameters::Tab2DData<Type1> mult(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b, int mode) {
    AMDA::Parameters::Tab2DData<Type1> r(a);

    switch (mode) {
        case 1:
            if (r.getDim1Size() != b.size()) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
            }

            for (int i = 0; i < r.getDim1Size(); ++i)
                for (int j = 0; j < r.getDim2Size(); ++j) {
                    if (isNAN(r[i][j]) || isNAN(b[i]))
                        r[i][j] << NotANumber();
                    else
                        r[i][j] *= b[i];
                }
            break;
        case 2:
            if (r.getDim2Size() != b.size()) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
            }

            for (int i = 0; i < r.getDim1Size(); ++i)
                for (int j = 0; j < r.getDim2Size(); ++j) {
                    if (isNAN(r[i][j]) || isNAN(b[j]))
                        r[i][j] << NotANumber();
                    else
                        r[i][j] *= b[j];
                }
            break;
        default:
            BOOST_THROW_EXCEPTION(
                    AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due a not implemented mode"));
    }
    return r;
fbe3c2bb   Benjamin Renard   First commit
1100
1101
1102
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1103
1104
AMDA::Parameters::Tab2DData<Type2> mult(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b, int mode) {
    return mult(b, a, mode);
fbe3c2bb   Benjamin Renard   First commit
1105
1106
1107
1108
1109
1110
}

/**
 * @brief Operator * between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1111
1112
AMDA::Parameters::Tab2DData<Type1> operator*(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b) {
    return mult(a, b, 1);
fbe3c2bb   Benjamin Renard   First commit
1113
1114
1115
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1116
1117
AMDA::Parameters::Tab2DData<Type2> operator*(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    return mult(b, a, 1);
fbe3c2bb   Benjamin Renard   First commit
1118
1119
1120
1121
1122
1123
}

/**
 * @brief Function div between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
AMDA::Parameters::Tab2DData<Type1> div(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b, int mode) {
    AMDA::Parameters::Tab2DData<Type1> r(a);

    switch (mode) {
        case 1:
            if (r.getDim1Size() != b.size()) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
            }

            for (int i = 0; i < r.getDim1Size(); ++i)
                for (int j = 0; j < r.getDim2Size(); ++j) {
                    if (isNAN(r[i][j]) || isNAN(b[i]) || (b[i] == 0))
                        r[i][j] << NotANumber();
                    else
                        r[i][j] = r[i][j] / b[i];
                }
            break;
        case 2:
            if (r.getDim2Size() != b.size()) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
            }

            for (int i = 0; i < r.getDim1Size(); ++i)
                for (int j = 0; j < r.getDim2Size(); ++j) {
                    if (isNAN(r[i][j]) || isNAN(b[j]) || (b[j] == 0))
                        r[i][j] << NotANumber();
                    else
                        r[i][j] = r[i][j] / b[j];
                }
            break;
        default:
            BOOST_THROW_EXCEPTION(
                    AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due a not implemented mode"));
    }
    return r;
fbe3c2bb   Benjamin Renard   First commit
1161
1162
1163
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
AMDA::Parameters::Tab2DData<Type2> div(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b, int mode) {
    AMDA::Parameters::Tab2DData<Type2> r(b);

    switch (mode) {
        case 1:
            if (r.getDim1Size() != a.size()) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
            }

            for (int i = 0; i < r.getDim1Size(); ++i)
                for (int j = 0; j < r.getDim2Size(); ++j) {
                    if (isNAN(r[i][j]) || isNAN(a[i]) || (r[i][j] == 0))
                        r[i][j] << NotANumber();
                    else
                        r[i][j] = a[i] / r[i][j];
                }
            break;
        case 2:
            if (r.getDim2Size() != a.size()) {
                BOOST_THROW_EXCEPTION(
                        AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
            }

            for (int i = 0; i < r.getDim1Size(); ++i)
                for (int j = 0; j < r.getDim2Size(); ++j) {
                    if (isNAN(r[i][j]) || isNAN(a[j]) || (r[i][j] == 0))
                        r[i][j] << NotANumber();
                    else
                        r[i][j] = a[j] / r[i][j];
                }
            break;
        default:
            BOOST_THROW_EXCEPTION(
                    AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due a not implemented mode"));
    }
    return r;
fbe3c2bb   Benjamin Renard   First commit
1201
1202
1203
1204
1205
1206
}

/**
 * @brief Operator / between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1207
1208
AMDA::Parameters::Tab2DData<Type1> operator/(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b) {
    return div(a, b, 1);
fbe3c2bb   Benjamin Renard   First commit
1209
1210
1211
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1212
1213
AMDA::Parameters::Tab2DData<Type2> operator/(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    return div(b, a, 1);
fbe3c2bb   Benjamin Renard   First commit
1214
1215
1216
1217
1218
1219
}

/**
 * @brief Operator + between two Tab2D
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
AMDA::Parameters::Tab2DData<Type1> operator+(const AMDA::Parameters::Tab2DData<Type1>& a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    if ((a.getDim1Size() != b.getDim1Size()) || (a.getDim2Size() != b.getDim2Size())) {
        BOOST_THROW_EXCEPTION(
                AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
    }

    AMDA::Parameters::Tab2DData<Type1> r(a);

    for (int i = 0; i < r.getDim1Size(); ++i)
        for (int j = 0; j < r.getDim2Size(); ++j) {
            if (isNAN(a[i][j]) || isNAN(b[i][j]))
                r[i][j] << NotANumber();
            else
                r[i][j] = a[i][j] + b[i][j];
        }
fbe3c2bb   Benjamin Renard   First commit
1235

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1236
    return r;
fbe3c2bb   Benjamin Renard   First commit
1237
1238
1239
1240
1241
1242
}

/**
 * @brief Operator - between two Tab2D
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
AMDA::Parameters::Tab2DData<Type1> operator-(const AMDA::Parameters::Tab2DData<Type1>& a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    if ((a.getDim1Size() != b.getDim1Size()) || (a.getDim2Size() != b.getDim2Size())) {
        BOOST_THROW_EXCEPTION(
                AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
    }

    AMDA::Parameters::Tab2DData<Type1> r(a);

    for (int i = 0; i < r.getDim1Size(); ++i)
        for (int j = 0; j < r.getDim2Size(); ++j) {
            if (isNAN(a[i][j]) || isNAN(b[i][j]))
                r[i][j] << NotANumber();
            else
                r[i][j] = a[i][j] - b[i][j];
        }
    return r;
fbe3c2bb   Benjamin Renard   First commit
1259
1260
1261
1262
1263
1264
}

/**
 * @brief Operator * between two Tab2D
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
AMDA::Parameters::Tab2DData<Type1> operator*(const AMDA::Parameters::Tab2DData<Type1>& a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    if ((a.getDim1Size() != b.getDim1Size()) || (a.getDim2Size() != b.getDim2Size())) {
        BOOST_THROW_EXCEPTION(
                AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
    }

    AMDA::Parameters::Tab2DData<Type1> r(a);

    for (int i = 0; i < r.getDim1Size(); ++i)
        for (int j = 0; j < r.getDim2Size(); ++j) {
            if (isNAN(a[i][j]) || isNAN(b[i][j]))
                r[i][j] << NotANumber();
            else
                r[i][j] = a[i][j] * b[i][j];
        }
    return r;
fbe3c2bb   Benjamin Renard   First commit
1281
1282
1283
1284
1285
1286
}

/**
 * @brief Operator + between two Tab2D
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
AMDA::Parameters::Tab2DData<Type1> operator/(const AMDA::Parameters::Tab2DData<Type1>& a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    if ((a.getDim1Size() != b.getDim1Size()) || (a.getDim2Size() != b.getDim2Size())) {
        BOOST_THROW_EXCEPTION(
                AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due to incompatibility in data dimensions"));
    }

    AMDA::Parameters::Tab2DData<Type1> r(a);

    for (int i = 0; i < r.getDim1Size(); ++i)
        for (int j = 0; j < r.getDim2Size(); ++j) {
            if (isNAN(a[i][j]) || isNAN(b[i][j]) || (b[i][j] == 0))
                r[i][j] << NotANumber();
            else
                r[i][j] = a[i][j] / b[i][j];
        }
    return r;
fbe3c2bb   Benjamin Renard   First commit
1303
1304
1305
}

/**
351058d5   Elena.Budnik   total() for vector
1306
1307
1308
 * @brief Function total on all components of a vector => result is a scalar
 */
template <typename Type>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
Type total(const std::vector<Type>& a) {
    Type sum;
    sum << NotANumber();

    for (int j = 0; j < a.size(); ++j) {
        if (isFinite(a[j])) {
            if (isNAN(sum))
                sum = a[j];
            else
                sum += a[j];
        }
    }
    return sum;
351058d5   Elena.Budnik   total() for vector
1322
1323
1324
}

/**
fbe3c2bb   Benjamin Renard   First commit
1325
1326
1327
 * @brief Function total on all components of a Tab2D => result is a scalar
 */
template <typename Type>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
Type total(const AMDA::Parameters::Tab2DData<Type>& a) {
    Type sum;
    sum << NotANumber();

    for (int i = 0; i < a.getDim1Size(); ++i)
        for (int j = 0; j < a.getDim2Size(); ++j) {
            if (isFinite(a[i][j])) {
                if (isNAN(sum))
                    sum = a[i][j];
                else
                    sum += a[i][j];
            }
        }
    return sum;
fbe3c2bb   Benjamin Renard   First commit
1342
1343
1344
1345
1346
1347
1348
1349
}

/**
 * @brief Function total on lines or columns of a Tab2D => result is a vector
 * mode = 1 => sum on lines
 * mode = 2 => sum on columns
 */
template <typename Type>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
std::vector<Type> total(const AMDA::Parameters::Tab2DData<Type>& a, int mode) {
    std::vector<Type> sum;

    switch (mode) {
        case 1:
            for (int j = 0; j < a.getDim2Size(); ++j) {
                Type elem;
                elem << NotANumber();
                for (int i = 0; i < a.getDim1Size(); ++i) {
                    if (isFinite(a[i][j])) {
                        if (isNAN(elem))
                            elem = a[i][j];
                        else
                            elem += a[i][j];
                    }
                }
                sum.push_back(elem);
            }
            break;
        case 2:
            for (int i = 0; i < a.getDim1Size(); ++i) {
                Type elem;
                elem << NotANumber();
                for (int j = 0; j < a.getDim2Size(); ++j) {
                    if (isFinite(a[i][j])) {
                        if (isNAN(elem))
                            elem = a[i][j];
                        else
                            elem += a[i][j];
                    }
                }
                sum.push_back(elem);
            }
            break;
        default:
            BOOST_THROW_EXCEPTION(
                    AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Operation not allowed due a not implemented mode"));
    }

    return sum;
fbe3c2bb   Benjamin Renard   First commit
1390
1391
}

9e4ae650   Benjamin Renard   Fix operators amb...
1392
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1393
1394
1395
1396
std::vector<Type1> operator+(AMDA::Parameters::TabRow<Type1> a, AMDA::Parameters::TabRow<Type2> b) {
    std::vector<Type1> av(a);
    std::vector<Type1> bv(b);
    return av + bv;
9e4ae650   Benjamin Renard   Fix operators amb...
1397
1398
1399
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1400
1401
1402
std::vector<Type1> operator+(std::vector<Type1> a, AMDA::Parameters::TabRow<Type2> b) {
    std::vector<Type2> bv(b);
    return a + bv;
9e4ae650   Benjamin Renard   Fix operators amb...
1403
1404
1405
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1406
1407
1408
std::vector<Type1> operator+(AMDA::Parameters::TabRow<Type1> a, std::vector<Type2> b) {
    std::vector<Type1> av(a);
    return av + b;
9e4ae650   Benjamin Renard   Fix operators amb...
1409
1410
1411
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1412
1413
1414
1415
std::vector<Type1> operator*(AMDA::Parameters::TabRow<Type1> a, AMDA::Parameters::TabRow<Type2> b) {
    std::vector<Type1> av(a);
    std::vector<Type1> bv(b);
    return av*bv;
9e4ae650   Benjamin Renard   Fix operators amb...
1416
1417
1418
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1419
1420
1421
std::vector<Type1> operator*(std::vector<Type1> a, AMDA::Parameters::TabRow<Type2> b) {
    std::vector<Type2> bv(b);
    return a*bv;
9e4ae650   Benjamin Renard   Fix operators amb...
1422
1423
1424
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1425
1426
1427
std::vector<Type1> operator*(AMDA::Parameters::TabRow<Type1> a, std::vector<Type2> b) {
    std::vector<Type1> av(a);
    return av*b;
9e4ae650   Benjamin Renard   Fix operators amb...
1428
1429
1430
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1431
1432
1433
1434
std::vector<Type1> operator-(AMDA::Parameters::TabRow<Type1> a, AMDA::Parameters::TabRow<Type2> b) {
    std::vector<Type1> av(a);
    std::vector<Type1> bv(b);
    return av - bv;
9e4ae650   Benjamin Renard   Fix operators amb...
1435
1436
1437
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1438
1439
1440
std::vector<Type1> operator-(std::vector<Type1> a, AMDA::Parameters::TabRow<Type2> b) {
    std::vector<Type2> bv(b);
    return a - bv;
9e4ae650   Benjamin Renard   Fix operators amb...
1441
1442
1443
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1444
1445
1446
std::vector<Type1> operator-(AMDA::Parameters::TabRow<Type1> a, std::vector<Type2> b) {
    std::vector<Type1> av(a);
    return av - b;
9e4ae650   Benjamin Renard   Fix operators amb...
1447
1448
1449
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1450
1451
1452
1453
std::vector<Type1> operator/(AMDA::Parameters::TabRow<Type1> a, AMDA::Parameters::TabRow<Type2> b) {
    std::vector<Type1> av(a);
    std::vector<Type1> bv(b);
    return av / bv;
9e4ae650   Benjamin Renard   Fix operators amb...
1454
1455
1456
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1457
1458
1459
std::vector<Type1> operator/(std::vector<Type1> a, AMDA::Parameters::TabRow<Type2> b) {
    std::vector<Type2> bv(b);
    return a / bv;
9e4ae650   Benjamin Renard   Fix operators amb...
1460
1461
1462
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1463
1464
1465
std::vector<Type1> operator/(AMDA::Parameters::TabRow<Type1> a, std::vector<Type2> b) {
    std::vector<Type1> av(a);
    return av / b;
9e4ae650   Benjamin Renard   Fix operators amb...
1466
}
fbe3c2bb   Benjamin Renard   First commit
1467

c76233b2   Benjamin Renard   Check window time...
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
template<typename Type>
bool isValid(const std::string& num) { 
   bool flag = true; 
   try { 
      Type tmp = boost::lexical_cast<Type>(num); 
   } 
   catch (boost::bad_lexical_cast &e) { 
      flag = false; 
   } 
   return flag; 
} 

fbe3c2bb   Benjamin Renard   First commit
1480
#endif /* LOGICALDATATYPE_HH_ */