Blame view

src/Parameters/DataTypeMath.hh 30.5 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
/*
 * 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_

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

/**
 * @brief Operator And (&&) between LogicalData
 */
	inline AMDA::Parameters::LogicalData And(AMDA::Parameters::LogicalData a, AMDA::Parameters::LogicalData b) {
		AMDA::Parameters::LogicalData res = AMDA::Parameters::LogicalData::NaN;

		if ( a != AMDA::Parameters::LogicalData::NaN && b != AMDA::Parameters::LogicalData::NaN) {
			res = ((a==AMDA::Parameters::LogicalData::True) && (b==AMDA::Parameters::LogicalData::True))?AMDA::Parameters::LogicalData::True:AMDA::Parameters::LogicalData::False;
		}

		return res;
	}

	/**
	 * @brief Operator Or (||) between LogicalData
	 */
	inline AMDA::Parameters::LogicalData Or(AMDA::Parameters::LogicalData a, AMDA::Parameters::LogicalData b) {
		AMDA::Parameters::LogicalData res = AMDA::Parameters::LogicalData::NaN;

		if ( a != AMDA::Parameters::LogicalData::NaN && b != AMDA::Parameters::LogicalData::NaN) {
			res = (( a == AMDA::Parameters::LogicalData::True) || (b == 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;
	}


fbe3c2bb   Benjamin Renard   First commit
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
	/**
	 * @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;
	}



	/**
	 * @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;
	}

	/**
22493b20   Benjamin Renard   Add dot product f...
175
176
177
178
179
180
181
182
	 * @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;
        }

	/**
fbe3c2bb   Benjamin Renard   First commit
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	 * @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;
	}

2e74c5fa   Benjamin Renard   Define cross func...
204
205
206
207
208
209
210
	/**
	 * @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;
	}
fbe3c2bb   Benjamin Renard   First commit
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378

	/**
	 * @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;


//	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);
//	}

	// vector with scalar operation

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


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


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

	/**
	 * @brief Operator addition between a scalar and a  vector
	 * @details all member of vector are added with the scalar
	 */
	template <typename Type>
	std::vector<Type> operator +( double f, std::vector<Type> a) {
		return a + f;
	}

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

	/**
	 * @brief Operator product between a scalar and a vector
	 * @details all member of vector are added by the scalar
	 */
	template <typename Type>
	std::vector<Type> operator *( double f, std::vector<Type> a) {
		return a * f;
	}



	/**
	 * @return false if val is NaN or inf or -inf
	 */
	template <typename Type>
	inline bool isFinite(Type val) {
		return std::isfinite(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>
c6877b6f   Benjamin Renard   Fix average for v...
379
	inline Type average(std::list<Type> tab, int /*dim1*/, int /*dim2*/) {
fbe3c2bb   Benjamin Renard   First commit
380
381
		Type mean;
		if(tab.empty()) {
2800bdcc   Benjamin Renard   Fix average funct...
382
			mean << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
383
384
		}
		else {
2800bdcc   Benjamin Renard   Fix average funct...
385
			mean << ElemNull();
fbe3c2bb   Benjamin Renard   First commit
386
387
388
			auto it = tab.begin();
			if (it != tab.end()) {
				unsigned int nbNan = 0;
5e9f00aa   Benjamin Renard   Fix another bug w...
389
390
				Type sum;
				sum << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
391
				for (; it != tab.end(); ++it) {
2800bdcc   Benjamin Renard   Fix average funct...
392
					if(isFinite(*it) && !isNAN(*it)) {
5e9f00aa   Benjamin Renard   Fix another bug w...
393
394
395
396
						if (isNAN(sum))
							sum = *it;
						else
							sum = sum + *it;
fbe3c2bb   Benjamin Renard   First commit
397
398
399
400
401
					}
					else {
						++nbNan;
					}
				}
5e9f00aa   Benjamin Renard   Fix another bug w...
402
				if ((tab.size() - nbNan == 0) || isNAN(sum)) {
2800bdcc   Benjamin Renard   Fix average funct...
403
404
405
406
407
408
409
410
					mean << NotANumber();
				}
				else {
					mean = sum / (int)(tab.size() - nbNan);
				}
			}
			else {
				mean << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
411
412
413
414
415
			}
		}
		return mean;
	}

c6877b6f   Benjamin Renard   Fix average for v...
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
	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]);
				}
			}
		}

		return mean;
	}

fbe3c2bb   Benjamin Renard   First commit
461
462
463
464
	/**
	 * @return the average of std::list
	 */
	template <typename Type>
c6877b6f   Benjamin Renard   Fix average for v...
465
	inline std::vector<Type> average(std::list<std::vector<Type> > tab, int dim1, int /*dim2*/) {
fbe3c2bb   Benjamin Renard   First commit
466
		std::vector<Type> mean;
c6877b6f   Benjamin Renard   Fix average for v...
467
		mean.resize(dim1);
fbe3c2bb   Benjamin Renard   First commit
468
		std::vector<unsigned int >nbNan;
c6877b6f   Benjamin Renard   Fix average for v...
469
470
471
		nbNan.resize(dim1);
		std::vector<Type> sum;
		sum.resize(dim1);
fbe3c2bb   Benjamin Renard   First commit
472
		if(tab.empty()) {
2800bdcc   Benjamin Renard   Fix average funct...
473
474
			mean << NotANumber();
			return mean;
fbe3c2bb   Benjamin Renard   First commit
475
476
		}
		for(unsigned int index = 0; index < tab.begin()->size(); ++index) {
c6877b6f   Benjamin Renard   Fix average for v...
477
			nbNan[index] = 0;
fbe3c2bb   Benjamin Renard   First commit
478
		}
c6877b6f   Benjamin Renard   Fix average for v...
479
		sum << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
480
481
482
		for (auto it = tab.begin(); it != tab.end(); ++it) {
			for(unsigned int index = 0; index < it->size(); ++index) {
				Type val = it->at(index);
2800bdcc   Benjamin Renard   Fix average funct...
483
				if(std::isfinite(val) && !isNAN(val)) {
c6877b6f   Benjamin Renard   Fix average for v...
484
485
486
487
					if (isNAN(sum[index]))
						sum[index] = val;
					else
						sum[index] +=val;
fbe3c2bb   Benjamin Renard   First commit
488
489
490
491
492
493
				}
				else {
					++(nbNan[index]);
				}
			}
		}
fbe3c2bb   Benjamin Renard   First commit
494
		for(unsigned int index = 0; index < tab.begin()->size(); ++index) {
2800bdcc   Benjamin Renard   Fix average funct...
495
			if (tab.size() - nbNan[index] == 0) {
c6877b6f   Benjamin Renard   Fix average for v...
496
				mean[index] << NotANumber();
2800bdcc   Benjamin Renard   Fix average funct...
497
498
			}
			else {
c6877b6f   Benjamin Renard   Fix average for v...
499
				mean[index] = sum[index] / (int)(tab.size() - nbNan[index]);
2800bdcc   Benjamin Renard   Fix average funct...
500
			}
fbe3c2bb   Benjamin Renard   First commit
501
502
503
504
505
506
		}


		return mean;
	}

2f932bad   Benjamin Renard   Fix interpolation...
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
/**
 *
 */
	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;
        }

/**
 * 
 */

	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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
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
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
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
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
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
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087

/**
 * @brief Operator + between Tab2D and scalar
 */
template <typename Type1, typename Type2>
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;
}

template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type2> operator +(Type1 a, const AMDA::Parameters::Tab2DData<Type2>& b)
{
	return (b+a);
}

/**
 * @brief Operator - between Tab2D and scalar
 */
template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type1> operator -(const AMDA::Parameters::Tab2DData<Type1>& a, Type2 b)
{
	return (a+(-b));
}

template <typename Type1, typename Type2>
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;
}

/**
 * @brief Operator * between Tab2D and scalar
 */
template <typename Type1, typename Type2>
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;
}

template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type2> operator *(Type1 a, const AMDA::Parameters::Tab2DData<Type2>& b)
{
	return (b*a);
}

/**
 * @brief Operator / between Tab2D and scalar
 */
template <typename Type1, typename Type2>
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));
}

template <typename Type1, typename Type2>
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;
}

/*
 * @brief Function add between a Tab2D and a vector
 */
template <typename Type1, typename Type2>
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;
}

template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type2> add(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b, int mode)
{
	return add(b,a,mode);
}

/**
 * @brief Operator + between Tab2D and vector
 */
template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type1> operator +(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b)
{
	return add(a,b,1);
}

template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type2> operator +(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b)
{
	return add(a,b,1);
}

/**
 * @brief Function sub between Tab2D and vector
 */
template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type1> sub(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b, int mode)
{
	return add(a,(-1)*b,mode);
}

template <typename Type1, typename Type2>
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;
}

/**
 * @brief Operator - between Tab2D and vector
 */
template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type1> operator -(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b)
{
	return sub(a,b,1);
}

template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type2> operator -(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b)
{
	return sub(a,b,1);
}

/**
 * @brief Function mult between Tab2D and vector
 */
template <typename Type1, typename Type2>
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;
}

template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type2> mult(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b, int mode)
{
	return mult(b,a,mode);
}

/**
 * @brief Operator * between Tab2D and vector
 */
template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type1> operator *(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b)
{
	return mult(a,b,1);
}

template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type2> operator *(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b)
{
	return mult(b,a,1);
}

/**
 * @brief Function div between Tab2D and vector
 */
template <typename Type1, typename Type2>
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;
}

template <typename Type1, typename Type2>
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;
}

/**
 * @brief Operator / between Tab2D and vector
 */
template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type1> operator /(const AMDA::Parameters::Tab2DData<Type1>& a, std::vector<Type2> b)
{
	return div(a,b,1);
}

template <typename Type1, typename Type2>
AMDA::Parameters::Tab2DData<Type2> operator /(std::vector<Type1> a, const AMDA::Parameters::Tab2DData<Type2>& b)
{
	return div(b,a,1);
}

/**
 * @brief Operator + between two Tab2D
 */
template <typename Type1, typename Type2>
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;
}

/**
 * @brief Operator - between two Tab2D
 */
template <typename Type1, typename Type2>
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;
}

/**
 * @brief Operator * between two Tab2D
 */
template <typename Type1, typename Type2>
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;
}

/**
 * @brief Operator + between two Tab2D
 */
template <typename Type1, typename Type2>
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;
}

/**
 * @brief Function total on all components of a Tab2D => result is a scalar
 */
template <typename Type>
Type total(const AMDA::Parameters::Tab2DData<Type>& a)
{
65c661e8   Benjamin Renard   Table definition ...
1088
1089
	Type sum;
	sum << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
1090
1091
1092

	for(int i = 0; i < a.getDim1Size(); ++i)
		for(int j = 0; j < a.getDim2Size(); ++j)
65c661e8   Benjamin Renard   Table definition ...
1093
1094
1095
1096
1097
1098
1099
1100
1101
		{
			if (isFinite(a[i][j]))
			{
				if (isNAN(sum))
					sum = a[i][j];
				else
					sum += a[i][j];
			}
		}
fbe3c2bb   Benjamin Renard   First commit
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
	return sum;
}

/**
 * @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>
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)
		{
65c661e8   Benjamin Renard   Table definition ...
1120
1121
			Type elem;
			elem << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
1122
			for(int i = 0; i < a.getDim1Size(); ++i)
65c661e8   Benjamin Renard   Table definition ...
1123
1124
1125
1126
1127
1128
1129
1130
1131
			{
				if (isFinite(a[i][j]))
				{
					if (isNAN(elem))
						elem = a[i][j];
					else
						elem += a[i][j];
				}
			}
fbe3c2bb   Benjamin Renard   First commit
1132
1133
1134
1135
1136
1137
			sum.push_back(elem);
		}
		break;
	case 2 :
		for(int i = 0; i < a.getDim1Size(); ++i)
		{
65c661e8   Benjamin Renard   Table definition ...
1138
1139
			Type elem;
			elem << NotANumber();
fbe3c2bb   Benjamin Renard   First commit
1140
			for(int j = 0; j < a.getDim2Size(); ++j)
65c661e8   Benjamin Renard   Table definition ...
1141
1142
1143
1144
1145
1146
1147
1148
1149
			{
				if (isFinite(a[i][j]))
				{
					if (isNAN(elem))
						elem = a[i][j];
					else
						elem += a[i][j];
				}
			}
fbe3c2bb   Benjamin Renard   First commit
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
			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;
}



#endif /* LOGICALDATATYPE_HH_ */