Blame view

src/Parameters/DataTypeMath.hh 46.3 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
16
17

#include <vector>
#include <cmath>
#include "ParamData.hh"

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
18
19
20
21
22
23
24
25
26
27
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...
28

fbe3c2bb   Benjamin Renard   First commit
29
30
31
/**
 * @brief Operator And (&&) between LogicalData
 */
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
32
33
34
35
36
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...
37

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
38
39
40
    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...
41

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
42
43
    return res;
}
384f25b6   Benjamin Renard   Implements operat...
44

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
45
46
47
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
/**
 * @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...
254

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
255
256
257
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
/**
 * @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...
283
Type magnitude(std::vector<Type> a) {
f2cf89d8   Hacene SI HADJ MOHAND   ok 7528
284
285
286
287
    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
288
289
290
291
}

template <typename Type1, typename Type2>
Type1 angle(std::vector<Type1> a, std::vector<Type2> b) {
f2cf89d8   Hacene SI HADJ MOHAND   ok 7528
292
293
294
295
    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
296
    if (dot(a, b) / magnitude(a) / magnitude(b) > 0.99999) {
f2cf89d8   Hacene SI HADJ MOHAND   ok 7528
297
        res = 0;
f02d93eb   Hacene SI HADJ MOHAND   alfven ok
298
299
    } else {
        res = std::acos(dot(a, b) / magnitude(a) / magnitude(b));
f2cf89d8   Hacene SI HADJ MOHAND   ok 7528
300
301
    }
    return res;
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
302
303
}

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

ed087564   Hacene SI HADJ MOHAND   function fairfiel...
327
328
329
330
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
331
}
3ff34f08   Hacene SI HADJ MOHAND   fairfield70 et fa...
332
333
334
335
336
337
338
339
340
 *  */
/**
 * @brief FAIRFIELD 1970 model of neutral sheet position  
 * @param xyz_sm SM coordinates of the sat
 * @param x_ss tilt Angle
 * @return delata Z 
 */
template <typename Type1, typename Type2>
Type1 fairfield70(std::vector<Type1>  xyz_sm, Type2 x_ss) {
c222fe3e   Hacene SI HADJ MOHAND   modif formules fa...
341
    Type1 val = xyz_sm[2]-std::sqrt(121.0 - 0.537778 * pow(xyz_sm[1], 2.0)) * std::sin(x_ss);
3ff34f08   Hacene SI HADJ MOHAND   fairfield70 et fa...
342
343
344
345
346
347
348
349
350
351
352
353
354
355
    return val;
}

/**
 * @brief FAIRFIELD 1980 model of neutral sheet position  
 * @param xyz_sm SM coordinates of the sat
 * @param x_ss tilt Angle
 * @return delata Z 
 */
template <typename Type1, typename Type2>
Type1 fairfield80(std::vector<Type1>  xyz_sm, Type2 x_ss) {
    Type1 val = xyz_sm[2]-10.5*std::sqrt(1-pow(xyz_sm[1], 2.0)/pow(17.0,2.0))* std::sin(x_ss);
    return val;
}
f02d93eb   Hacene SI HADJ MOHAND   alfven ok
356

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
357
/**
3699433c   Hacene SI HADJ MOHAND   adding functions
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
 * @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
380
381
382
383
384
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
385
386
387
388
    return val; 
}

/**
ed087564   Hacene SI HADJ MOHAND   function fairfiel...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
 * @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) {
    Type1 val = -(0.14*Kp+0.69)*std::pow(phi, 1.0/3.0)*(0.065*R-0.16)*DTA;
    return val;
}



/**
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
404
405
406
407
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
 * @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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484


//	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
485
486
487
488
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
// 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 ...
709
        }
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
710
711
712
713
    }

    return mean;
}
fbe3c2bb   Benjamin Renard   First commit
714

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
715
716
717
718
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
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...
758
        }
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
759
760
    }

7578a549   Benjamin Renard   Add boolean suppo...
761

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
762
763
    return mean;
}
fbe3c2bb   Benjamin Renard   First commit
764

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
765
766
767
768
769
770
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...
771

2f932bad   Benjamin Renard   Fix interpolation...
772
773
774
/**
 *
 */
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
775
776
777
778
779
780
781
782
783
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...
784

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
785
786
787
788
789
790
791
792
793
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...
794

2f932bad   Benjamin Renard   Fix interpolation...
795
796
797
798
/**
 * 
 */

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
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
825
826
827
828
829

/**
 * @brief Operator + between Tab2D and scalar
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
830
831
832
833
834
835
836
837
838
839
840
841
842
843
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
844
845
846
}

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

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

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
860
861
862
863
864
865
866
867
868
869
870
871
872
873
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
874
875
876
877
878
879
}

/**
 * @brief Operator * between Tab2D and scalar
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
880
881
882
883
884
885
886
887
888
889
890
891
892
893
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
894
895
896
}

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

/**
 * @brief Operator / between Tab2D and scalar
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
905
906
907
908
909
910
911
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
912
913
914
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
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
931
932
933
934
935
936
}

/*
 * @brief Function add between a Tab2D and a vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
937
938
939
940
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
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
974
975
976
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
977
978
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
979
980
981
982
983
984
}

/**
 * @brief Operator + between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
985
986
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
987
988
989
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
990
991
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
992
993
994
995
996
997
}

/**
 * @brief Function sub between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
998
999
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
1000
1001
1002
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1003
1004
1005
1006
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
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
1040
1041
1042
1043
1044
1045
}

/**
 * @brief Operator - between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1046
1047
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
1048
1049
1050
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1051
1052
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
1053
1054
1055
1056
1057
1058
}

/**
 * @brief Function mult between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1059
1060
1061
1062
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
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
1096
1097
1098
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1099
1100
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
1101
1102
1103
1104
1105
1106
}

/**
 * @brief Operator * between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1107
1108
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
1109
1110
1111
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1112
1113
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
1114
1115
1116
1117
1118
1119
}

/**
 * @brief Function div between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1120
1121
1122
1123
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
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
1157
1158
1159
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1160
1161
1162
1163
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
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
1197
1198
1199
1200
1201
1202
}

/**
 * @brief Operator / between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1203
1204
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
1205
1206
1207
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1208
1209
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
1210
1211
1212
1213
1214
1215
}

/**
 * @brief Operator + between two Tab2D
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
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
1231

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1232
    return r;
fbe3c2bb   Benjamin Renard   First commit
1233
1234
1235
1236
1237
1238
}

/**
 * @brief Operator - between two Tab2D
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
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
1255
1256
1257
1258
1259
1260
}

/**
 * @brief Operator * between two Tab2D
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
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
1277
1278
1279
1280
1281
1282
}

/**
 * @brief Operator + between two Tab2D
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
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
1299
1300
1301
}

/**
351058d5   Elena.Budnik   total() for vector
1302
1303
1304
 * @brief Function total on all components of a vector => result is a scalar
 */
template <typename Type>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
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
1318
1319
1320
}

/**
fbe3c2bb   Benjamin Renard   First commit
1321
1322
1323
 * @brief Function total on all components of a Tab2D => result is a scalar
 */
template <typename Type>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
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
1338
1339
1340
1341
1342
1343
1344
1345
}

/**
 * @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
1346
1347
1348
1349
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
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
1386
1387
}

9e4ae650   Benjamin Renard   Fix operators amb...
1388
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1389
1390
1391
1392
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...
1393
1394
1395
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1396
1397
1398
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...
1399
1400
1401
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1402
1403
1404
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...
1405
1406
1407
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1408
1409
1410
1411
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...
1412
1413
1414
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1415
1416
1417
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...
1418
1419
1420
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1421
1422
1423
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...
1424
1425
1426
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1427
1428
1429
1430
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...
1431
1432
1433
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1434
1435
1436
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...
1437
1438
1439
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1440
1441
1442
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...
1443
1444
1445
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1446
1447
1448
1449
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...
1450
1451
1452
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1453
1454
1455
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...
1456
1457
1458
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1459
1460
1461
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...
1462
}
fbe3c2bb   Benjamin Renard   First commit
1463
1464

#endif /* LOGICALDATATYPE_HH_ */