Blame view

src/Parameters/DataTypeMath.hh 45.4 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) 
ed087564   Hacene SI HADJ MOHAND   function fairfiel...
326
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
332
}

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
333
/**
3699433c   Hacene SI HADJ MOHAND   adding functions
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
 * @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)
 */
template <typename Type1, typename Type2, typename Type3>
Type1 newell2007(Type1  mu, Type2 B_mag,Type3 theta) {
    Type1 val = std::pow(mu , 4./3.)*std::pow(B_mag , 2./3.)*pow(std::sin(theta/2),8./3.);
    return val; 
}

/**
ed087564   Hacene SI HADJ MOHAND   function fairfiel...
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
 * @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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
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
 * @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
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


//	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
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
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
// 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 ...
683
        }
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
684
685
686
687
    }

    return mean;
}
fbe3c2bb   Benjamin Renard   First commit
688

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
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...
732
        }
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
733
734
    }

7578a549   Benjamin Renard   Add boolean suppo...
735

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
736
737
    return mean;
}
fbe3c2bb   Benjamin Renard   First commit
738

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
739
740
741
742
743
744
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...
745

2f932bad   Benjamin Renard   Fix interpolation...
746
747
748
/**
 *
 */
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
749
750
751
752
753
754
755
756
757
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...
758

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
759
760
761
762
763
764
765
766
767
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...
768

2f932bad   Benjamin Renard   Fix interpolation...
769
770
771
772
/**
 * 
 */

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
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
799
800
801
802
803

/**
 * @brief Operator + between Tab2D and scalar
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
804
805
806
807
808
809
810
811
812
813
814
815
816
817
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
818
819
820
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
821
822
AMDA::Parameters::Tab2DData<Type2> operator+(Type1 a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    return (b + a);
fbe3c2bb   Benjamin Renard   First commit
823
824
825
826
827
828
}

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

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<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
848
849
850
851
852
853
}

/**
 * @brief Operator * between Tab2D and scalar
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
854
855
856
857
858
859
860
861
862
863
864
865
866
867
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
868
869
870
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
871
872
AMDA::Parameters::Tab2DData<Type2> operator*(Type1 a, const AMDA::Parameters::Tab2DData<Type2>& b) {
    return (b * a);
fbe3c2bb   Benjamin Renard   First commit
873
874
875
876
877
878
}

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

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
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
905
906
907
908
909
910
}

/*
 * @brief Function add between a Tab2D and a vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
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
948
949
950
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
951
952
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
953
954
955
956
957
958
}

/**
 * @brief Operator + between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
959
960
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
961
962
963
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
964
965
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
966
967
968
969
970
971
}

/**
 * @brief Function sub between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
972
973
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
974
975
976
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
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
1014
1015
1016
1017
1018
1019
}

/**
 * @brief Operator - between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1020
1021
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
1022
1023
1024
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1025
1026
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
1027
1028
1029
1030
1031
1032
}

/**
 * @brief Function mult between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
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
1070
1071
1072
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1073
1074
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
1075
1076
1077
1078
1079
1080
}

/**
 * @brief Operator * between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1081
1082
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
1083
1084
1085
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1086
1087
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
1088
1089
1090
1091
1092
1093
}

/**
 * @brief Function div between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
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
1131
1132
1133
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
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
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
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
1171
1172
1173
1174
1175
1176
}

/**
 * @brief Operator / between Tab2D and vector
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1177
1178
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
1179
1180
1181
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1182
1183
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
1184
1185
1186
1187
1188
1189
}

/**
 * @brief Operator + between two Tab2D
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
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
1205

68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1206
    return r;
fbe3c2bb   Benjamin Renard   First commit
1207
1208
1209
1210
1211
1212
}

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

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

/**
 * @brief Operator + between two Tab2D
 */
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
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
1273
1274
1275
}

/**
351058d5   Elena.Budnik   total() for vector
1276
1277
1278
 * @brief Function total on all components of a vector => result is a scalar
 */
template <typename Type>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
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
1292
1293
1294
}

/**
fbe3c2bb   Benjamin Renard   First commit
1295
1296
1297
 * @brief Function total on all components of a Tab2D => result is a scalar
 */
template <typename Type>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
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
1312
1313
1314
1315
1316
1317
1318
1319
}

/**
 * @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
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
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
1360
1361
}

9e4ae650   Benjamin Renard   Fix operators amb...
1362
template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1363
1364
1365
1366
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...
1367
1368
1369
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1370
1371
1372
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...
1373
1374
1375
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1376
1377
1378
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...
1379
1380
1381
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1382
1383
1384
1385
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...
1386
1387
1388
}

template <typename Type1, typename Type2>
68af7c4e   Hacene SI HADJ MOHAND   nOk 7528
1389
1390
1391
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...
1392
1393
1394
}

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

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

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

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

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

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

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

#endif /* LOGICALDATATYPE_HH_ */