]> Creatis software - bbtk.git/blob - packages/std/src/bbstdMathOperationVector.cxx
#2808, #2807, #2806, #2805, #2804 BBTK Feature New Normal Add new functionality MathO...
[bbtk.git] / packages / std / src / bbstdMathOperationVector.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbstdMathOperationVector.h"
5 #include "bbstdPackage.h"
6
7 #include <math.h>
8 #include <stdlib.h>
9 #include <time.h>
10
11 namespace bbstd
12 {
13
14 BBTK_ADD_BLACK_BOX_TO_PACKAGE(std,MathOperationVector)
15 BBTK_BLACK_BOX_IMPLEMENTATION(MathOperationVector,bbtk::AtomicBlackBox);
16
17 double MathOperationVector::g(std::vector<double>*v,unsigned int i,double defValue )
18 {
19         double result=defValue;
20         if (i < (*v).size()) 
21         {
22                 result= (*v)[i];
23         }
24         return result;
25 }
26
27 void MathOperationVector::SumVector(std::vector<double> *v,std::vector<double> *vr)
28 {
29         unsigned int i;
30         double tmpResult=0;
31         unsigned int sizeVec=v->size(); 
32         if ( sizeVec>0) 
33         { 
34                 for (i=0;i<sizeVec;i++) 
35                 { 
36                         tmpResult = (*v)[i] + tmpResult; 
37                 } // for i 
38                 vr->push_back( tmpResult ); 
39         } // if sizeVec
40 }
41
42
43 void MathOperationVector::MinVector(std::vector<double> *v,std::vector<double> *vr)
44 {
45         unsigned int i;
46         double tmpResult;               
47         unsigned int sizeVec=v->size(); 
48         if ( sizeVec>0) 
49         { 
50                 tmpResult=(*v)[0];
51                 for (i=1;i<sizeVec;i++) 
52                 { 
53                         tmpResult = fmin ( (*v)[i] , tmpResult ) ; 
54                 } // for i 
55                 vr->push_back( tmpResult ); 
56         } // if sizeVec
57 }
58
59
60 void MathOperationVector::MaxVector(std::vector<double> *v,std::vector<double> *vr)
61 {
62         unsigned int i;
63         double tmpResult;               
64         unsigned int sizeVec=v->size(); 
65         if ( sizeVec>0) 
66         { 
67                 tmpResult=(*v)[0];
68                 for (i=1;i<sizeVec;i++) 
69                 { 
70                         tmpResult = fmax ( (*v)[i] , tmpResult ) ; 
71                 } // for i 
72                 vr->push_back( tmpResult ); 
73         } // if sizeVec
74 }
75
76 //===== 
77 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
78 //===== 
79 void MathOperationVector::Process()
80 {
81 // THE MAIN PROCESSING METHOD BODY
82 //   Here we simply set the input 'In' value to the output 'Out'
83 //   And print out the output value
84 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
85 //    void bbSet{Input|Output}NAME(const TYPE&)
86 //    const TYPE& bbGet{Input|Output}NAME() const 
87 //    Where :
88 //    * NAME is the name of the input/output
89 //      (the one provided in the attribute 'name' of the tag 'input')
90 //    * TYPE is the C++ type of the input/output
91 //      (the one provided in the attribute 'type' of the tag 'input')
92
93         bool error;
94         unsigned int i;
95         unsigned int sizeVec = bbGetInputIn0().size();
96         double tmpResult;
97         std::vector<double> resultVec;
98         std::vector<double> _v0=bbGetInputIn0();
99         std::vector<double> _v1=bbGetInputIn1();
100         std::vector<double> _v2=bbGetInputIn2();
101         std::vector<double> _v3=bbGetInputIn3();
102         std::vector<double> _v4=bbGetInputIn4();
103         std::vector<double> _v5=bbGetInputIn5();
104         std::vector<double> _v6=bbGetInputIn6();
105         std::vector<double> _v7=bbGetInputIn7();
106         std::vector<double> _v8=bbGetInputIn8();
107         std::vector<double> _v9=bbGetInputIn9();
108
109         std::vector<double> *v0 = &_v0;
110         std::vector<double> *v1 = &_v1;
111         std::vector<double> *v2 = &_v2;
112         std::vector<double> *v3 = &_v3;
113         std::vector<double> *v4 = &_v4;
114         std::vector<double> *v5 = &_v5;
115         std::vector<double> *v6 = &_v6;
116         std::vector<double> *v7 = &_v7;
117         std::vector<double> *v8 = &_v8;
118         std::vector<double> *v9 = &_v9;
119
120         double minTmp;          
121         double maxTmp;          
122
123          if (firsttime==true)
124          {
125                 firsttime=false;
126          /* initialize random seed: */ 
127         srand (time(NULL));
128                 if (bbGetInputType()==7) 
129                 {
130                         for (i=0;i<sizeVec;i++)
131                         {
132                                 acum.push_back(bbGetInputIn0()[i]);
133                         } // for
134                 } // if 7
135          } // if firsttime
136
137
138     if (bbGetInputType()==0) 
139         {
140                 for (i=0;i<sizeVec;i++)
141                 {
142                         tmpResult = g(v0,i,0) + g(v1,i,0) + g(v2,i,0) + g(v3,i,0) +
143                                                 g(v4,i,0) + g(v5,i,0) + g(v6,i,0) + 
144                                                 g(v7,i,0) + g(v8,i,0) + g(v9,i,0) ;
145                         resultVec.push_back( tmpResult );
146                 } // for i
147                 bbSetOutputOut( resultVec );
148         } // if Type 0
149
150     if (bbGetInputType()==-99) 
151         {
152                 SumVector(v0,&resultVec);
153                 SumVector(v1,&resultVec);
154                 SumVector(v2,&resultVec);
155                 SumVector(v3,&resultVec);
156                 SumVector(v4,&resultVec);
157                 SumVector(v5,&resultVec);
158                 SumVector(v6,&resultVec);
159                 SumVector(v7,&resultVec);
160                 SumVector(v8,&resultVec);
161                 SumVector(v9,&resultVec);
162
163                 bbSetOutputOut( resultVec );
164         } // if Type -99
165
166
167
168     if (bbGetInputType()==1)
169         {
170                 for (i=0;i<sizeVec;i++)
171                 {
172                         tmpResult = g(v0,i,0) - g(v1,i,0) - g(v2,i,0) - g(v3,i,0) -
173                                                 g(v4,i,0) - g(v5,i,0) - g(v6,i,0) -
174                                                 g(v7,i,0) - g(v8,i,0) - g(v9,i,0) ;
175                         resultVec.push_back( tmpResult );
176                 } // for i
177                 bbSetOutputOut( resultVec );
178         } // if Type 1  
179
180
181     if (bbGetInputType()==2) 
182         {
183                 for (i=0;i<sizeVec;i++)
184                 {
185                         tmpResult = g(v0,i,0) * g(v1,i,1) * g(v2,i,1) * g(v3,i,1) *
186                                                 g(v4,i,1) * g(v5,i,1) * g(v6,i,1) *
187                                                 g(v7,i,1) * g(v8,i,1) * g(v9,i,1) ;
188                         resultVec.push_back( tmpResult );
189                 } // for i
190                 bbSetOutputOut( resultVec );
191         } // if Type 2
192
193
194
195     if (bbGetInputType()==3)
196     {
197                 error = false;
198                 for (i=0;i<sizeVec;i++)
199                 {
200             if (g(v1,i,1)!=0) tmpResult = g(v0,i,0) / g(v1,i,1); else error=true;
201             if (g(v2,i,1)!=0) tmpResult = tmpResult / g(v2,i,1); else error=true;
202             if (g(v3,i,1)!=0) tmpResult = tmpResult / g(v3,i,1); else error=true;
203             if (g(v4,i,1)!=0) tmpResult = tmpResult / g(v4,i,1); else error=true;
204             if (g(v5,i,1)!=0) tmpResult = tmpResult / g(v5,i,1); else error=true;
205             if (g(v6,i,1)!=0) tmpResult = tmpResult / g(v6,i,1); else error=true;
206             if (g(v7,i,1)!=0) tmpResult = tmpResult / g(v7,i,1); else error=true;
207             if (g(v8,i,1)!=0) tmpResult = tmpResult / g(v8,i,1); else error=true;
208             if (g(v9,i,1)!=0) tmpResult = tmpResult / g(v9,i,1); else error=true;
209                         if (error==true)  tmpResult = 99999;
210                         resultVec.push_back( tmpResult );
211                 } // for i
212                 bbSetOutputOut( resultVec );
213     } // if Type 3
214
215
216     if (bbGetInputType()==4)
217         { 
218                 for (i=0;i<sizeVec;i++)
219                 {
220                 if (bbGetInputIn0()[i]>=0) tmpResult = sqrt( bbGetInputIn0()[i] ) ;
221                else  tmpResult = 0 ;
222                         resultVec.push_back( tmpResult );
223                 } // for i
224                 bbSetOutputOut( resultVec );
225         } // if Type 4
226
227
228     if (bbGetInputType()==5)
229          {
230                 for (i=0;i<sizeVec;i++)
231                 {
232                 if (bbGetInputIn0()[i]>0)  tmpResult = log( bbGetInputIn0()[i] ) ;
233                else  tmpResult = -99999 ; 
234                         resultVec.push_back( tmpResult );
235                 } // for i
236                 bbSetOutputOut( resultVec );
237          } // if Type 5
238
239
240
241     if (bbGetInputType()==6)
242         {
243                 for (i=0;i<sizeVec;i++)
244                 {
245                         resultVec.push_back(  exp( bbGetInputIn0()[i] )  );
246                 } // for i
247                 bbSetOutputOut( resultVec );
248         } // Type 6
249
250
251     if (bbGetInputType()==7)
252         {
253                 bbSetOutputOut( acum );
254                 for (i=0;i<sizeVec;i++)
255                 {
256                         acum[i]++;
257                 if (acum[i]>g(v1,i,0)) acum[i]=g(v0,i,0);
258                 } // for
259         } // if Type 7
260
261     if (bbGetInputType()==8 )
262         {
263                 for (i=0;i<sizeVec;i++)
264                 {
265                         resultVec.push_back(  rand() % (int)( g(v1,i,0)-g(v0,i,0) ) + g(v0,i,0) );
266                 } // for i
267                 bbSetOutputOut( resultVec );
268         } // Type 8
269
270     if (bbGetInputType()==9)
271         {
272                 for (i=0;i<sizeVec;i++)
273                 {
274                         resultVec.push_back(  sin(  bbGetInputIn0()[i] )  );
275                 } // for i
276                 bbSetOutputOut( resultVec );
277         } // Type 9
278
279     if (bbGetInputType()==10)
280         {
281                 for (i=0;i<sizeVec;i++)
282                 {
283                         resultVec.push_back(  cos(  bbGetInputIn0()[i] )  );
284                 } // for i
285                 bbSetOutputOut( resultVec );
286         } // Type 10
287
288     if (bbGetInputType()==11)
289         {
290                 for (i=0;i<sizeVec;i++)
291                 {
292                         resultVec.push_back(  tan(  bbGetInputIn0()[i] )  );
293                 } // for i
294                 bbSetOutputOut( resultVec );
295         } // Type 11
296
297     if (bbGetInputType()==12)
298         {
299                 for (i=0;i<sizeVec;i++)
300                 {
301                         resultVec.push_back(  asin(  bbGetInputIn0()[i] )  );
302                 } // for i
303                 bbSetOutputOut( resultVec );
304         } // Type 12
305
306     if (bbGetInputType()==13)
307         {
308                 for (i=0;i<sizeVec;i++)
309                 {
310                         resultVec.push_back(  acos(  bbGetInputIn0()[i] )  );
311                 } // for i
312                 bbSetOutputOut( resultVec );
313         } // Type 13
314
315     if (bbGetInputType()==14)
316         {
317                 for (i=0;i<sizeVec;i++)
318                 {
319                         resultVec.push_back(  atan(  bbGetInputIn0()[i] )  );
320                 } // for i
321                 bbSetOutputOut( resultVec );
322         } // Type 14
323
324     if (bbGetInputType()==15)
325         {
326                 for (i=0;i<sizeVec;i++)
327                 {
328                         resultVec.push_back(  atan2(  g(v0,i,0) , g(v1,i,0) )  );
329                 } // for i
330                 bbSetOutputOut( resultVec );
331         } // Type 15
332
333
334     if (bbGetInputType()==16)
335         {
336                 for (i=0;i<sizeVec;i++)
337                 {
338                         minTmp = fmin(  g(v0,i,999999) , g(v1,i,999999) );
339                         minTmp = fmin(  g(v2,i,999999) , minTmp );
340                         minTmp = fmin(  g(v3,i,999999) , minTmp );
341                         minTmp = fmin(  g(v4,i,999999) , minTmp );
342                         minTmp = fmin(  g(v5,i,999999) , minTmp );
343                         minTmp = fmin(  g(v6,i,999999) , minTmp );
344                         minTmp = fmin(  g(v7,i,999999) , minTmp );
345                         minTmp = fmin(  g(v8,i,999999) , minTmp );
346                         minTmp = fmin(  g(v9,i,999999) , minTmp );
347                         resultVec.push_back( minTmp );
348                 } // for i
349                 bbSetOutputOut( resultVec );
350         } // Type 16
351
352     if (bbGetInputType()==-16)
353         {
354                 MinVector(v0,&resultVec);
355                 MinVector(v1,&resultVec);
356                 MinVector(v2,&resultVec);
357                 MinVector(v3,&resultVec);
358                 MinVector(v4,&resultVec);
359                 MinVector(v5,&resultVec);
360                 MinVector(v6,&resultVec);
361                 MinVector(v7,&resultVec);
362                 MinVector(v8,&resultVec);
363                 MinVector(v9,&resultVec);
364
365                 bbSetOutputOut( resultVec );
366         } // Type -16
367
368     if (bbGetInputType()==17)
369         {
370                 for (i=0;i<sizeVec;i++)
371                 {
372                         maxTmp = fmax(  g(v0,i,-999999) , g(v1,i,-999999) );
373                         maxTmp = fmax(  g(v2,i,-999999) , maxTmp );
374                         maxTmp = fmax(  g(v3,i,-999999) , maxTmp );
375                         maxTmp = fmax(  g(v4,i,-999999) , maxTmp );
376                         maxTmp = fmax(  g(v5,i,-999999) , maxTmp );
377                         maxTmp = fmax(  g(v6,i,-999999) , maxTmp );
378                         maxTmp = fmax(  g(v7,i,-999999) , maxTmp );
379                         maxTmp = fmax(  g(v8,i,-999999) , maxTmp );
380                         maxTmp = fmax(  g(v9,i,-999999) , maxTmp );
381                         resultVec.push_back(  maxTmp  );
382                 } // for i
383                 bbSetOutputOut( resultVec );
384         } // Type 17
385
386
387     if (bbGetInputType()==-17)
388         {
389                 MaxVector(v0,&resultVec);
390                 MaxVector(v1,&resultVec);
391                 MaxVector(v2,&resultVec);
392                 MaxVector(v3,&resultVec);
393                 MaxVector(v4,&resultVec);
394                 MaxVector(v5,&resultVec);
395                 MaxVector(v6,&resultVec);
396                 MaxVector(v7,&resultVec);
397                 MaxVector(v8,&resultVec);
398                 MaxVector(v9,&resultVec);
399                 bbSetOutputOut( resultVec );
400         } // Type -17
401
402
403     if (bbGetInputType()==18)
404         {
405                 for (i=0;i<sizeVec;i++)
406                 {
407                         resultVec.push_back(  fabs( bbGetInputIn0()[i] )  );
408                 } // for i
409                 bbSetOutputOut( resultVec );
410         } // Type 18
411
412
413 }
414
415 //===== 
416 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
417 //===== 
418 void MathOperationVector::bbUserSetDefaultValues()
419 {
420
421 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
422 //    Here we initialize the input 'In' to 0
423
424    bbSetInputType(0);
425    firsttime=true;
426   
427
428 }
429 //===== 
430 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
431 //===== 
432 void MathOperationVector::bbUserInitializeProcessing()
433 {
434
435 //  THE INITIALIZATION METHOD BODY :
436 //    Here does nothing 
437 //    but this is where you should allocate the internal/output pointers 
438 //    if any 
439
440   
441 }
442 //===== 
443 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
444 //===== 
445 void MathOperationVector::bbUserFinalizeProcessing()
446 {
447
448 //  THE FINALIZATION METHOD BODY :
449 //    Here does nothing 
450 //    but this is where you should desallocate the internal/output pointers 
451 //    if any
452   
453 }
454 }
455 // EO namespace bbstd
456
457