fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / core / opencl / op_database.cxx
blob285da07002faf0031a528d74c8e1fbe1ec268ed6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include "op_database.hxx"
12 #include "formulagroup.hxx"
13 #include "document.hxx"
14 #include "formulacell.hxx"
15 #include "tokenarray.hxx"
16 #include "compiler.hxx"
17 #include "interpre.hxx"
18 #include <formula/vectortoken.hxx>
19 #include <sstream>
21 namespace sc { namespace opencl {
23 void OpDmax::GenSlidingWindowFunction(std::stringstream &ss,
24 const std::string &sSymName, SubArguments &vSubArguments)
26 ss << "\ndouble " << sSymName;
27 ss << "_"<< BinFuncName() <<"(";
28 for (unsigned i = 0; i < vSubArguments.size(); i++)
30 if (i)
31 ss << ",";
32 vSubArguments[i]->GenSlidingWindowDecl(ss);
34 ss << ")\n {\n";
35 ss << " int gid0=get_global_id(0);\n";
36 ss << " double max = -1000000000000;\n";
37 ss << " double value=0.0;\n";
38 GenTmpVariables(ss,vSubArguments);
39 int dataCol = 0;
40 int dataRow = 0;
41 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
42 formula::svDoubleVectorRef)
44 formula::FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
45 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
46 formula::DoubleVectorRefToken *>(tmpCur);
47 dataCol = pCurDVR1->GetArrays().size();
48 dataRow = pCurDVR1->GetArrayLength();
50 else
51 throw Unhandled();
52 if(vSubArguments[dataCol]->GetFormulaToken()->GetType() ==
53 formula::svSingleVectorRef)
55 const formula::SingleVectorRefToken*pTmpDVR1= static_cast<const
56 formula::SingleVectorRefToken *>(vSubArguments[dataCol]->
57 GetFormulaToken());
58 ss << " tmp"<<dataCol<<"=";
59 ss << vSubArguments[dataCol]->GenSlidingWindowDeclRef()<<";\n";
60 ss << " if(gid0>="<<pTmpDVR1->GetArrayLength()<<" ||isNan(";
61 ss << "tmp"<<dataCol<<"))\n";
62 ss << " tmp"<<dataCol<<"=0;\n";
64 else
65 throw Unhandled();
66 int conditionCol = 0;
67 int conditionRow = 0;
68 if(vSubArguments[dataCol + 1]->GetFormulaToken()->GetType() ==
69 formula::svDoubleVectorRef)
71 formula::FormulaToken *tmpCur = vSubArguments[dataCol + 1]->
72 GetFormulaToken();
73 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
74 formula::DoubleVectorRefToken *>(tmpCur);
75 conditionCol = pCurDVR2->GetArrays().size();
76 conditionRow = pCurDVR2->GetArrayLength();
78 else{
79 throw Unhandled();
81 if(dataCol!=conditionCol)
82 throw Unhandled();
83 if(dataCol > 0 && dataRow > 0)
85 formula::FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
86 formula::FormulaToken *tmpCur2 = vSubArguments[dataCol + 1]->
87 GetFormulaToken();
88 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
89 formula::DoubleVectorRefToken *>(tmpCur1);
90 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
91 formula::DoubleVectorRefToken *>(tmpCur2);
93 if(pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed() &&
94 pCurDVR2->IsStartFixed() && pCurDVR2->IsEndFixed())
96 ss << " int i,j,p;\n";
97 ss << " bool flag;\n";
98 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
99 ss << " {\n";
100 ss << " i = p;\n";
101 for(int i = 0; i < dataCol; ++i){
102 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
103 formula::svDoubleVectorRef)
104 throw Unhandled();
105 ss << " tmp"<<i<<"=";
106 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
107 ss << " if(isNan(tmp"<<i<<"))\n";
108 ss <<" tmp"<<i<<" = 0;\n";
110 ss << " flag = false;\n";
111 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
112 ss << " {\n";
113 ss << " i = j;\n";
114 ss << " if (flag)\n";
115 ss << " break;\n";
116 ss << " else{\n";
117 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
118 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
119 formula::svDoubleVectorRef)
120 throw Unhandled();
121 ss << " tmp"<<i<<"=";
122 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
123 ss << " if(!isNan(tmp"<<i<<")){\n";
124 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
125 ss << i<<"){\n";
126 ss << " continue;\n";
127 ss << " }\n";
128 ss << " }\n";
130 ss << " flag=true;\n";
131 ss << " }\n";
132 ss << " }\n";
133 ss << " if (flag){\n";
134 for(int i = 0; i < dataCol; ++i){
135 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
136 ss << " value=tmp"<<i<<";\n";
137 ss << " }\n";
139 ss << " if(max<value)\n";
140 ss << " max=value;";
141 ss << " }\n";
142 ss << " }\n";
144 else
145 ss << "max = -1;\n";
147 else
148 ss << "max = -1;\n";
149 ss << " return max;\n";
150 ss << "}";
153 void OpDmin::GenSlidingWindowFunction(std::stringstream &ss,
154 const std::string &sSymName, SubArguments &vSubArguments)
156 ss << "\ndouble " << sSymName;
157 ss << "_"<< BinFuncName() <<"(";
158 for (unsigned i = 0; i < vSubArguments.size(); i++)
160 if (i)
161 ss << ",";
162 vSubArguments[i]->GenSlidingWindowDecl(ss);
164 ss << ")\n {\n";
165 ss << " int gid0=get_global_id(0);\n";
166 ss << " double min = 1000000000000;\n";
167 ss << " double value=0.0;\n";
168 GenTmpVariables(ss,vSubArguments);
169 int dataCol = 0;
170 int dataRow = 0;
171 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
172 formula::svDoubleVectorRef)
174 formula::FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
175 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
176 formula::DoubleVectorRefToken *>(tmpCur);
177 dataCol = pCurDVR1->GetArrays().size();
178 dataRow = pCurDVR1->GetArrayLength();
180 else
181 throw Unhandled();
182 if(vSubArguments[dataCol]->GetFormulaToken()->GetType() ==
183 formula::svSingleVectorRef)
185 const formula::SingleVectorRefToken*pTmpDVR1= static_cast<const
186 formula::SingleVectorRefToken *>(vSubArguments[dataCol]->
187 GetFormulaToken());
188 ss << " tmp"<<dataCol<<"=";
189 ss << vSubArguments[dataCol]->GenSlidingWindowDeclRef()<<";\n";
190 ss << " if(gid0>="<<pTmpDVR1->GetArrayLength()<<" ||isNan(";
191 ss << "tmp"<<dataCol<<"))\n";
192 ss << " tmp"<<dataCol<<"=0;\n";
194 else
195 throw Unhandled();
196 int conditionCol = 0;
197 int conditionRow = 0;
198 if(vSubArguments[dataCol + 1]->GetFormulaToken()->GetType() ==
199 formula::svDoubleVectorRef)
201 formula::FormulaToken *tmpCur = vSubArguments[dataCol + 1]->
202 GetFormulaToken();
203 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
204 formula::DoubleVectorRefToken *>(tmpCur);
205 conditionCol = pCurDVR2->GetArrays().size();
206 conditionRow = pCurDVR2->GetArrayLength();
208 else{
209 throw Unhandled();
211 if(dataCol!=conditionCol)
212 throw Unhandled();
213 if(dataCol > 0 && dataRow > 0)
215 formula::FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
216 formula::FormulaToken *tmpCur2 = vSubArguments[dataCol + 1]->
217 GetFormulaToken();
218 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
219 formula::DoubleVectorRefToken *>(tmpCur1);
220 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
221 formula::DoubleVectorRefToken *>(tmpCur2);
223 if(pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed() &&
224 pCurDVR2->IsStartFixed() && pCurDVR2->IsEndFixed())
226 ss << " int i,j,p;\n";
227 ss << " bool flag;\n";
229 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
230 ss << " {\n";
231 ss << " i = p;\n";
232 for(int i = 0; i < dataCol; ++i){
233 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
234 formula::svDoubleVectorRef)
235 throw Unhandled();
236 ss << " tmp"<<i<<"=";
237 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
238 ss << " if(isNan(tmp"<<i<<"))\n";
239 ss <<" tmp"<<i<<" = 0;\n";
241 ss << " flag = false;\n";
242 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
243 ss << " {\n";
244 ss << " i = j;\n";
245 ss << " if (flag)\n";
246 ss << " break;\n";
247 ss << " else{\n";
248 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
249 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
250 formula::svDoubleVectorRef)
251 throw Unhandled();
252 ss << " tmp"<<i<<"=";
253 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
254 ss << " if(!isNan(tmp"<<i<<")){\n";
255 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
256 ss << i<<"){\n";
257 ss << " continue;\n";
258 ss << " }\n";
259 ss << " }\n";
261 ss << " flag=true;\n";
262 ss << " }\n";
263 ss << " }\n";
264 ss << " if (flag){\n";
265 for(int i = 0; i < dataCol; ++i){
266 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
267 ss << " value=tmp"<<i<<";\n";
268 ss << " }\n";
270 ss << " if(min>value)\n";
271 ss << " min=value;";
272 ss << " }\n";
273 ss << " }\n";
275 else
276 ss << "min = -1;\n";
278 else
279 ss << "min = -1;\n";
280 ss << " return min;\n";
281 ss << "}";
284 void OpDproduct::GenSlidingWindowFunction(std::stringstream &ss,
285 const std::string &sSymName, SubArguments &vSubArguments)
287 ss << "\ndouble " << sSymName;
288 ss << "_"<< BinFuncName() <<"(";
289 for (unsigned i = 0; i < vSubArguments.size(); i++)
291 if (i)
292 ss << ",";
293 vSubArguments[i]->GenSlidingWindowDecl(ss);
295 ss << ")\n {\n";
296 ss << " int gid0=get_global_id(0);\n";
297 ss << " double product = 1;\n";
298 ss << " double value =0;\n";
299 GenTmpVariables(ss,vSubArguments);
300 int dataCol = 0;
301 int dataRow = 0;
302 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
303 formula::svDoubleVectorRef)
305 formula::FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
306 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
307 formula::DoubleVectorRefToken *>(tmpCur);
308 dataCol = pCurDVR1->GetArrays().size();
309 dataRow = pCurDVR1->GetArrayLength();
311 else
312 throw Unhandled();
313 if(vSubArguments[dataCol]->GetFormulaToken()->GetType() ==
314 formula::svSingleVectorRef)
316 const formula::SingleVectorRefToken*pTmpDVR1= static_cast<const
317 formula::SingleVectorRefToken *>(vSubArguments[dataCol]->
318 GetFormulaToken());
319 ss << " tmp"<<dataCol<<"=";
320 ss << vSubArguments[dataCol]->GenSlidingWindowDeclRef()<<";\n";
321 ss << " if(gid0>="<<pTmpDVR1->GetArrayLength()<<" ||isNan(";
322 ss << "tmp"<<dataCol<<"))\n";
323 ss << " tmp"<<dataCol<<"=0;\n";
325 else
326 throw Unhandled();
327 int conditionCol = 0;
328 int conditionRow = 0;
329 if(vSubArguments[dataCol + 1]->GetFormulaToken()->GetType() ==
330 formula::svDoubleVectorRef)
332 formula::FormulaToken *tmpCur = vSubArguments[dataCol + 1]->
333 GetFormulaToken();
334 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
335 formula::DoubleVectorRefToken *>(tmpCur);
336 conditionCol = pCurDVR2->GetArrays().size();
337 conditionRow = pCurDVR2->GetArrayLength();
339 else{
340 throw Unhandled();
342 if(dataCol!=conditionCol)
343 throw Unhandled();
344 if(dataCol > 0 && dataRow > 0)
346 formula::FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
347 formula::FormulaToken *tmpCur2 = vSubArguments[dataCol + 1]->
348 GetFormulaToken();
349 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
350 formula::DoubleVectorRefToken *>(tmpCur1);
351 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
352 formula::DoubleVectorRefToken *>(tmpCur2);
354 if(pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed() &&
355 pCurDVR2->IsStartFixed() && pCurDVR2->IsEndFixed())
357 ss << " int i,j,p;\n";
358 ss << " bool flag;\n";
359 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
360 ss << " {\n";
361 ss << " i = p;\n";
362 for(int i = 0; i < dataCol; ++i){
363 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
364 formula::svDoubleVectorRef)
365 throw Unhandled();
366 ss << " tmp"<<i<<"=";
367 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
368 ss << " if(isNan(tmp"<<i<<"))\n";
369 ss <<" tmp"<<i<<" = 0;\n";
371 ss << " flag = false;\n";
372 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
373 ss << " {\n";
374 ss << " i = j;\n";
375 ss << " if (flag)\n";
376 ss << " break;\n";
377 ss << " else{\n";
378 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
379 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
380 formula::svDoubleVectorRef)
381 throw Unhandled();
382 ss << " tmp"<<i<<"=";
383 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
384 ss << " if(!isNan(tmp"<<i<<")){\n";
385 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
386 ss << i<<"){\n";
387 ss << " continue;\n";
388 ss << " }\n";
389 ss << " }\n";
391 ss << " flag=true;\n";
392 ss << " }\n";
393 ss << " }\n";
394 ss << " if (flag){\n";
395 for(int i = 0; i < dataCol; ++i){
396 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
397 ss << " value=tmp"<<i<<";\n";
398 ss << " }\n";
400 ss << " product*=value;\n";
401 ss << " }\n";
402 ss << " }\n";
404 else
405 ss << "product = -1;\n";
407 else
408 ss << "product = -1;\n";
409 ss << " return product;\n";
410 ss << "}";
413 void OpDaverage::GenSlidingWindowFunction(std::stringstream &ss,
414 const std::string &sSymName, SubArguments &vSubArguments)
416 ss << "\ndouble " << sSymName;
417 ss << "_"<< BinFuncName() <<"(";
418 for (unsigned i = 0; i < vSubArguments.size(); i++)
420 if (i)
421 ss << ",";
422 vSubArguments[i]->GenSlidingWindowDecl(ss);
424 ss << ")\n {\n";
425 ss << " int gid0=get_global_id(0);\n";
426 ss << " double sum = 0;\n";
427 ss << " int count = 0;\n";
428 ss << " double value =0;\n";
429 GenTmpVariables(ss,vSubArguments);
430 int dataCol = 0;
431 int dataRow = 0;
432 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
433 formula::svDoubleVectorRef)
435 formula::FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
436 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
437 formula::DoubleVectorRefToken *>(tmpCur);
438 dataCol = pCurDVR1->GetArrays().size();
439 dataRow = pCurDVR1->GetArrayLength();
441 else
442 throw Unhandled();
443 if(vSubArguments[dataCol]->GetFormulaToken()->GetType() ==
444 formula::svSingleVectorRef)
446 const formula::SingleVectorRefToken*pTmpDVR1= static_cast<const
447 formula::SingleVectorRefToken *>(vSubArguments[dataCol]->
448 GetFormulaToken());
449 ss << " tmp"<<dataCol<<"=";
450 ss << vSubArguments[dataCol]->GenSlidingWindowDeclRef()<<";\n";
451 ss << " if(gid0>="<<pTmpDVR1->GetArrayLength()<<" ||isNan(";
452 ss << "tmp"<<dataCol<<"))\n";
453 ss << " tmp"<<dataCol<<"=0;\n";
455 else
456 throw Unhandled();
457 int conditionCol = 0;
458 int conditionRow = 0;
459 if(vSubArguments[dataCol + 1]->GetFormulaToken()->GetType() ==
460 formula::svDoubleVectorRef)
462 formula::FormulaToken *tmpCur = vSubArguments[dataCol + 1]->
463 GetFormulaToken();
464 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
465 formula::DoubleVectorRefToken *>(tmpCur);
466 conditionCol = pCurDVR2->GetArrays().size();
467 conditionRow = pCurDVR2->GetArrayLength();
469 else{
470 throw Unhandled();
472 if(dataCol!=conditionCol)
473 throw Unhandled();
474 if(dataCol > 0 && dataRow > 0)
476 formula::FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
477 formula::FormulaToken *tmpCur2 = vSubArguments[dataCol + 1]->
478 GetFormulaToken();
479 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
480 formula::DoubleVectorRefToken *>(tmpCur1);
481 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
482 formula::DoubleVectorRefToken *>(tmpCur2);
484 if(pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed() &&
485 pCurDVR2->IsStartFixed() && pCurDVR2->IsEndFixed())
487 ss << " int i,j,p;\n";
488 ss << " bool flag;\n";
490 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
491 ss << " {\n";
492 ss << " i = p;\n";
493 for(int i = 0; i < dataCol; ++i){
494 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
495 formula::svDoubleVectorRef)
496 throw Unhandled();
497 ss << " tmp"<<i<<"=";
498 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
499 ss << " if(isNan(tmp"<<i<<"))\n";
500 ss <<" tmp"<<i<<" = 0;\n";
502 ss << " flag = false;\n";
503 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
504 ss << " {\n";
505 ss << " i = j;\n";
506 ss << " if (flag)\n";
507 ss << " break;\n";
508 ss << " else{\n";
509 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
510 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
511 formula::svDoubleVectorRef)
512 throw Unhandled();
513 ss << " tmp"<<i<<"=";
514 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
515 ss << " if(!isNan(tmp"<<i<<")){\n";
516 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
517 ss << i<<"){\n";
518 ss << " continue;\n";
519 ss << " }\n";
520 ss << " }\n";
522 ss << " flag=true;\n";
523 ss << " }\n";
524 ss << " }\n";
525 ss << " if (flag){\n";
526 ss << " count++;\n";
527 for(int i = 0; i < dataCol; ++i){
528 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
529 ss << " value=tmp"<<i<<";\n";
530 ss << " }\n";
532 ss << " sum+=value;\n";
533 ss << " }\n";
534 ss << " }\n";
536 else
537 ss << "sum = -1;\n";
539 else
540 ss << "sum = -1;\n";
541 ss << " if(count==0)\n";
542 ss << " return 0;\n";
543 ss << " return sum/count;\n";
544 ss << "}";
547 void OpDstdev::GenSlidingWindowFunction(std::stringstream &ss,
548 const std::string &sSymName, SubArguments &vSubArguments)
550 ss << "\ndouble " << sSymName;
551 ss << "_"<< BinFuncName() <<"(";
552 for (unsigned i = 0; i < vSubArguments.size(); i++)
554 if (i)
555 ss << ",";
556 vSubArguments[i]->GenSlidingWindowDecl(ss);
558 ss << ")\n {\n";
559 ss << " int gid0=get_global_id(0);\n";
560 ss << " double var = 0;\n";
561 ss << " double mean = 0;\n";
562 ss << " double value =0;\n";
563 ss << " int count = 0;\n";
564 GenTmpVariables(ss,vSubArguments);
565 int dataCol = 0;
566 int dataRow = 0;
567 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
568 formula::svDoubleVectorRef)
570 formula::FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
571 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
572 formula::DoubleVectorRefToken *>(tmpCur);
573 dataCol = pCurDVR1->GetArrays().size();
574 dataRow = pCurDVR1->GetArrayLength();
576 else
577 throw Unhandled();
578 if(vSubArguments[dataCol]->GetFormulaToken()->GetType() ==
579 formula::svSingleVectorRef)
581 const formula::SingleVectorRefToken*pTmpDVR1= static_cast<const
582 formula::SingleVectorRefToken *>(vSubArguments[dataCol]->
583 GetFormulaToken());
584 ss << " tmp"<<dataCol<<"=";
585 ss << vSubArguments[dataCol]->GenSlidingWindowDeclRef()<<";\n";
586 ss << " if(gid0>="<<pTmpDVR1->GetArrayLength()<<" ||isNan(";
587 ss << "tmp"<<dataCol<<"))\n";
588 ss << " tmp"<<dataCol<<"=0;\n";
590 else
591 throw Unhandled();
592 int conditionCol = 0;
593 int conditionRow = 0;
594 if(vSubArguments[dataCol + 1]->GetFormulaToken()->GetType() ==
595 formula::svDoubleVectorRef)
597 formula::FormulaToken *tmpCur = vSubArguments[dataCol + 1]->
598 GetFormulaToken();
599 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
600 formula::DoubleVectorRefToken *>(tmpCur);
601 conditionCol = pCurDVR2->GetArrays().size();
602 conditionRow = pCurDVR2->GetArrayLength();
604 else{
605 throw Unhandled();
607 if(dataCol!=conditionCol)
608 throw Unhandled();
609 if(dataCol > 0 && dataRow > 0)
611 formula::FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
612 formula::FormulaToken *tmpCur2 = vSubArguments[dataCol + 1]->
613 GetFormulaToken();
614 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
615 formula::DoubleVectorRefToken *>(tmpCur1);
616 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
617 formula::DoubleVectorRefToken *>(tmpCur2);
619 if(pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed() &&
620 pCurDVR2->IsStartFixed() && pCurDVR2->IsEndFixed())
622 ss << " int i,j,p;\n";
623 ss << " bool flag;\n";
625 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
626 ss << " {\n";
627 ss << " i = p;\n";
628 for(int i = 0; i < dataCol; ++i){
629 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
630 formula::svDoubleVectorRef)
631 throw Unhandled();
632 ss << " tmp"<<i<<"=";
633 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
634 ss << " if(isNan(tmp"<<i<<"))\n";
635 ss <<" tmp"<<i<<" = 0;\n";
637 ss << " flag = false;\n";
638 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
639 ss << " {\n";
640 ss << " i = j;\n";
641 ss << " if (flag)\n";
642 ss << " break;\n";
643 ss << " else{\n";
644 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
645 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
646 formula::svDoubleVectorRef)
647 throw Unhandled();
648 ss << " tmp"<<i<<"=";
649 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
650 ss << " if(!isNan(tmp"<<i<<")){\n";
651 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
652 ss << i<<"){\n";
653 ss << " continue;\n";
654 ss << " }\n";
655 ss << " }\n";
657 ss << " flag=true;\n";
658 ss << " }\n";
659 ss << " }\n";
660 ss << " if (flag){\n";
661 ss << " count++;\n";
662 for(int i = 0; i < dataCol; ++i){
663 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
664 ss << " value=tmp"<<i<<";\n";
665 ss << " }\n";
667 ss << " mean+=value;\n";
668 ss << " }\n";
669 ss << " }\n";
671 ss << " if(count<=1)\n";
672 ss << " return 0;\n";
674 ss << " mean/=count;\n";
676 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
677 ss << " {\n";
678 ss << " i = p;\n";
679 for(int i = 0; i < dataCol; ++i){
680 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
681 formula::svDoubleVectorRef)
682 throw Unhandled();
683 ss << " tmp"<<i<<"=";
684 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
685 ss << " if(isNan(tmp"<<i<<"))\n";
686 ss <<" tmp"<<i<<" = 0;\n";
688 ss << " flag = false;\n";
689 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
690 ss << " {\n";
691 ss << " i = j;\n";
692 ss << " if (flag)\n";
693 ss << " break;\n";
694 ss << " else{\n";
695 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
696 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
697 formula::svDoubleVectorRef)
698 throw Unhandled();
699 ss << " tmp"<<i<<"=";
700 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
701 ss << " if(!isNan(tmp"<<i<<")){\n";
702 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
703 ss << i<<"){\n";
704 ss << " continue;\n";
705 ss << " }\n";
706 ss << " }\n";
708 ss << " flag=true;\n";
709 ss << " }\n";
710 ss << " }\n";
711 ss << " if (flag){\n";
712 for(int i = 0; i < dataCol; ++i){
713 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
714 ss << " value=tmp"<<i<<";\n";
715 ss << " }\n";
717 ss << " var+=pow(mean-value,2);\n";
718 ss << " }\n";
719 ss << " }\n";
721 ss << " var = sqrt( var/(count-1) );\n";
723 else
724 ss << "var = -1;\n";
726 else
727 ss << "var = -1;\n";
728 ss << " return var;\n";
729 ss << "}";
732 void OpDstdevp::GenSlidingWindowFunction(std::stringstream &ss,
733 const std::string &sSymName, SubArguments &vSubArguments)
735 ss << "\ndouble " << sSymName;
736 ss << "_"<< BinFuncName() <<"(";
737 for (unsigned i = 0; i < vSubArguments.size(); i++)
739 if (i)
740 ss << ",";
741 vSubArguments[i]->GenSlidingWindowDecl(ss);
743 ss << ")\n {\n";
744 ss << " int gid0=get_global_id(0);\n";
745 ss << " double var = 0;\n";
746 ss << " double mean = 0;\n";
747 ss << " double value =0;\n";
748 ss << " int count = 0;\n";
749 GenTmpVariables(ss,vSubArguments);
750 int dataCol = 0;
751 int dataRow = 0;
752 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
753 formula::svDoubleVectorRef)
755 formula::FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
756 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
757 formula::DoubleVectorRefToken *>(tmpCur);
758 dataCol = pCurDVR1->GetArrays().size();
759 dataRow = pCurDVR1->GetArrayLength();
761 else
762 throw Unhandled();
763 if(vSubArguments[dataCol]->GetFormulaToken()->GetType() ==
764 formula::svSingleVectorRef)
766 const formula::SingleVectorRefToken*pTmpDVR1= static_cast<const
767 formula::SingleVectorRefToken *>(vSubArguments[dataCol]->
768 GetFormulaToken());
769 ss << " tmp"<<dataCol<<"=";
770 ss << vSubArguments[dataCol]->GenSlidingWindowDeclRef()<<";\n";
771 ss << " if(gid0>="<<pTmpDVR1->GetArrayLength()<<" ||isNan(";
772 ss << "tmp"<<dataCol<<"))\n";
773 ss << " tmp"<<dataCol<<"=0;\n";
775 else
776 throw Unhandled();
777 int conditionCol = 0;
778 int conditionRow = 0;
779 if(vSubArguments[dataCol + 1]->GetFormulaToken()->GetType() ==
780 formula::svDoubleVectorRef)
782 formula::FormulaToken *tmpCur = vSubArguments[dataCol + 1]->
783 GetFormulaToken();
784 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
785 formula::DoubleVectorRefToken *>(tmpCur);
786 conditionCol = pCurDVR2->GetArrays().size();
787 conditionRow = pCurDVR2->GetArrayLength();
789 else{
790 throw Unhandled();
792 if(dataCol!=conditionCol)
793 throw Unhandled();
794 if(dataCol > 0 && dataRow > 0)
796 formula::FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
797 formula::FormulaToken *tmpCur2 = vSubArguments[dataCol + 1]->
798 GetFormulaToken();
799 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
800 formula::DoubleVectorRefToken *>(tmpCur1);
801 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
802 formula::DoubleVectorRefToken *>(tmpCur2);
804 if(pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed() &&
805 pCurDVR2->IsStartFixed() && pCurDVR2->IsEndFixed())
807 ss << " int i,j,p;\n";
808 ss << " bool flag;\n";
810 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
811 ss << " {\n";
812 ss << " i = p;\n";
813 for(int i = 0; i < dataCol; ++i){
814 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
815 formula::svDoubleVectorRef)
816 throw Unhandled();
817 ss << " tmp"<<i<<"=";
818 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
819 ss << " if(isNan(tmp"<<i<<"))\n";
820 ss <<" tmp"<<i<<" = 0;\n";
822 ss << " flag = false;\n";
823 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
824 ss << " {\n";
825 ss << " i = j;\n";
826 ss << " if (flag)\n";
827 ss << " break;\n";
828 ss << " else{\n";
829 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
830 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
831 formula::svDoubleVectorRef)
832 throw Unhandled();
833 ss << " tmp"<<i<<"=";
834 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
835 ss << " if(!isNan(tmp"<<i<<")){\n";
836 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
837 ss << i<<"){\n";
838 ss << " continue;\n";
839 ss << " }\n";
840 ss << " }\n";
842 ss << " flag=true;\n";
843 ss << " }\n";
844 ss << " }\n";
845 ss << " if (flag){\n";
846 ss << " count++;\n";
847 for(int i = 0; i < dataCol; ++i){
848 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
849 ss << " value=tmp"<<i<<";\n";
850 ss << " }\n";
852 ss << " mean+=value;\n";
853 ss << " }\n";
854 ss << " }\n";
856 ss << " if(count<=1)\n";
857 ss << " return 0;\n";
859 ss << " mean/=count;\n";
861 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
862 ss << " {\n";
863 ss << " i = p;\n";
864 for(int i = 0; i < dataCol; ++i){
865 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
866 formula::svDoubleVectorRef)
867 throw Unhandled();
868 ss << " tmp"<<i<<"=";
869 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
870 ss << " if(isNan(tmp"<<i<<"))\n";
871 ss <<" tmp"<<i<<" = 0;\n";
873 ss << " flag = false;\n";
874 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
875 ss << " {\n";
876 ss << " i = j;\n";
877 ss << " if (flag)\n";
878 ss << " break;\n";
879 ss << " else{\n";
880 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
881 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
882 formula::svDoubleVectorRef)
883 throw Unhandled();
884 ss << " tmp"<<i<<"=";
885 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
886 ss << " if(!isNan(tmp"<<i<<")){\n";
887 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
888 ss << i<<"){\n";
889 ss << " continue;\n";
890 ss << " }\n";
891 ss << " }\n";
893 ss << " flag=true;\n";
894 ss << " }\n";
895 ss << " }\n";
896 ss << " if (flag){\n";
897 for(int i = 0; i < dataCol; ++i){
898 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
899 ss << " value=tmp"<<i<<";\n";
900 ss << " }\n";
902 ss << " var+=pow(mean-value,2);\n";
903 ss << " }\n";
904 ss << " }\n";
906 ss << " var = sqrt( var/count );\n";
908 else
909 ss << "var = -1;\n";
911 else
912 ss << "var = -1;\n";
913 ss << " return var;\n";
914 ss << "}";
917 void OpDsum::GenSlidingWindowFunction(std::stringstream &ss,
918 const std::string &sSymName, SubArguments &vSubArguments)
920 ss << "\ndouble " << sSymName;
921 ss << "_"<< BinFuncName() <<"(";
922 for (unsigned i = 0; i < vSubArguments.size(); i++)
924 if (i)
925 ss << ",";
926 vSubArguments[i]->GenSlidingWindowDecl(ss);
928 ss << ")\n {\n";
929 ss << " int gid0=get_global_id(0);\n";
930 ss << " double sum = 0;\n";
931 ss << " double value =0;\n";
932 GenTmpVariables(ss,vSubArguments);
933 int dataCol = 0;
934 int dataRow = 0;
935 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
936 formula::svDoubleVectorRef)
938 formula::FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
939 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
940 formula::DoubleVectorRefToken *>(tmpCur);
941 dataCol = pCurDVR1->GetArrays().size();
942 dataRow = pCurDVR1->GetArrayLength();
944 else
945 throw Unhandled();
946 if(vSubArguments[dataCol]->GetFormulaToken()->GetType() ==
947 formula::svSingleVectorRef)
949 const formula::SingleVectorRefToken*pTmpDVR1= static_cast<const
950 formula::SingleVectorRefToken *>(vSubArguments[dataCol]->
951 GetFormulaToken());
952 ss << " tmp"<<dataCol<<"=";
953 ss << vSubArguments[dataCol]->GenSlidingWindowDeclRef()<<";\n";
954 ss << " if(gid0>="<<pTmpDVR1->GetArrayLength()<<" ||isNan(";
955 ss << "tmp"<<dataCol<<"))\n";
956 ss << " tmp"<<dataCol<<"=0;\n";
958 else
959 throw Unhandled();
960 int conditionCol = 0;
961 int conditionRow = 0;
962 if(vSubArguments[dataCol + 1]->GetFormulaToken()->GetType() ==
963 formula::svDoubleVectorRef)
965 formula::FormulaToken *tmpCur = vSubArguments[dataCol + 1]->
966 GetFormulaToken();
967 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
968 formula::DoubleVectorRefToken *>(tmpCur);
969 conditionCol = pCurDVR2->GetArrays().size();
970 conditionRow = pCurDVR2->GetArrayLength();
972 else{
973 throw Unhandled();
975 if(dataCol!=conditionCol)
976 throw Unhandled();
977 if(dataCol > 0 && dataRow > 0)
979 formula::FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
980 formula::FormulaToken *tmpCur2 = vSubArguments[dataCol + 1]->
981 GetFormulaToken();
982 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
983 formula::DoubleVectorRefToken *>(tmpCur1);
984 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
985 formula::DoubleVectorRefToken *>(tmpCur2);
987 if(pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed() &&
988 pCurDVR2->IsStartFixed() && pCurDVR2->IsEndFixed())
990 ss << " int i,j,p;\n";
991 ss << " bool flag;\n";
993 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
994 ss << " {\n";
995 ss << " i = p;\n";
996 for(int i = 0; i < dataCol; ++i){
997 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
998 formula::svDoubleVectorRef)
999 throw Unhandled();
1000 ss << " tmp"<<i<<"=";
1001 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1002 ss << " if(isNan(tmp"<<i<<"))\n";
1003 ss <<" tmp"<<i<<" = 0;\n";
1005 ss << " flag = false;\n";
1006 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
1007 ss << " {\n";
1008 ss << " i = j;\n";
1009 ss << " if (flag)\n";
1010 ss << " break;\n";
1011 ss << " else{\n";
1012 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
1013 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1014 formula::svDoubleVectorRef)
1015 throw Unhandled();
1016 ss << " tmp"<<i<<"=";
1017 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1018 ss << " if(!isNan(tmp"<<i<<")){\n";
1019 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
1020 ss << i<<"){\n";
1021 ss << " continue;\n";
1022 ss << " }\n";
1023 ss << " }\n";
1025 ss << " flag=true;\n";
1026 ss << " }\n";
1027 ss << " }\n";
1028 ss << " if (flag){\n";
1029 for(int i = 0; i < dataCol; ++i){
1030 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
1031 ss << " value=tmp"<<i<<";\n";
1032 ss << " }\n";
1034 ss << " sum+=value;\n";
1035 ss << " }\n";
1036 ss << " }\n";
1038 else
1039 ss << "sum = -1;\n";
1041 else
1042 ss << "sum = -1;\n";
1043 ss << " return sum;\n";
1044 ss << "}";
1047 void OpDvar::GenSlidingWindowFunction(std::stringstream &ss,
1048 const std::string &sSymName, SubArguments &vSubArguments)
1050 ss << "\ndouble " << sSymName;
1051 ss << "_"<< BinFuncName() <<"(";
1052 for (unsigned i = 0; i < vSubArguments.size(); i++)
1054 if (i)
1055 ss << ",";
1056 vSubArguments[i]->GenSlidingWindowDecl(ss);
1058 ss << ")\n {\n";
1059 ss << " int gid0=get_global_id(0);\n";
1060 ss << " double var = 0;\n";
1061 ss << " double mean = 0;\n";
1062 ss << " double value =0;\n";
1063 ss << " int count = 0;\n";
1064 GenTmpVariables(ss,vSubArguments);
1065 int dataCol = 0;
1066 int dataRow = 0;
1067 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
1068 formula::svDoubleVectorRef)
1070 formula::FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1071 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
1072 formula::DoubleVectorRefToken *>(tmpCur);
1073 dataCol = pCurDVR1->GetArrays().size();
1074 dataRow = pCurDVR1->GetArrayLength();
1076 else
1077 throw Unhandled();
1078 if(vSubArguments[dataCol]->GetFormulaToken()->GetType() ==
1079 formula::svSingleVectorRef)
1081 const formula::SingleVectorRefToken*pTmpDVR1= static_cast<const
1082 formula::SingleVectorRefToken *>(vSubArguments[dataCol]->
1083 GetFormulaToken());
1084 ss << " tmp"<<dataCol<<"=";
1085 ss << vSubArguments[dataCol]->GenSlidingWindowDeclRef()<<";\n";
1086 ss << " if(gid0>="<<pTmpDVR1->GetArrayLength()<<" ||isNan(";
1087 ss << "tmp"<<dataCol<<"))\n";
1088 ss << " tmp"<<dataCol<<"=0;\n";
1090 else
1091 throw Unhandled();
1092 int conditionCol = 0;
1093 int conditionRow = 0;
1094 if(vSubArguments[dataCol + 1]->GetFormulaToken()->GetType() ==
1095 formula::svDoubleVectorRef)
1097 formula::FormulaToken *tmpCur = vSubArguments[dataCol + 1]->
1098 GetFormulaToken();
1099 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
1100 formula::DoubleVectorRefToken *>(tmpCur);
1101 conditionCol = pCurDVR2->GetArrays().size();
1102 conditionRow = pCurDVR2->GetArrayLength();
1104 else{
1105 throw Unhandled();
1107 if(dataCol!=conditionCol)
1108 throw Unhandled();
1109 if(dataCol > 0 && dataRow > 0)
1111 formula::FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
1112 formula::FormulaToken *tmpCur2 = vSubArguments[dataCol + 1]->
1113 GetFormulaToken();
1114 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
1115 formula::DoubleVectorRefToken *>(tmpCur1);
1116 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
1117 formula::DoubleVectorRefToken *>(tmpCur2);
1119 if(pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed() &&
1120 pCurDVR2->IsStartFixed() && pCurDVR2->IsEndFixed())
1122 ss << " int i,j,p;\n";
1123 ss << " bool flag;\n";
1125 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
1126 ss << " {\n";
1127 ss << " i = p;\n";
1128 for(int i = 0; i < dataCol; ++i){
1129 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1130 formula::svDoubleVectorRef)
1131 throw Unhandled();
1132 ss << " tmp"<<i<<"=";
1133 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1134 ss << " if(isNan(tmp"<<i<<"))\n";
1135 ss <<" tmp"<<i<<" = 0;\n";
1137 ss << " flag = false;\n";
1138 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
1139 ss << " {\n";
1140 ss << " i = j;\n";
1141 ss << " if (flag)\n";
1142 ss << " break;\n";
1143 ss << " else{\n";
1144 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
1145 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1146 formula::svDoubleVectorRef)
1147 throw Unhandled();
1148 ss << " tmp"<<i<<"=";
1149 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1150 ss << " if(!isNan(tmp"<<i<<")){\n";
1151 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
1152 ss << i<<"){\n";
1153 ss << " continue;\n";
1154 ss << " }\n";
1155 ss << " }\n";
1157 ss << " flag=true;\n";
1158 ss << " }\n";
1159 ss << " }\n";
1160 ss << " if (flag){\n";
1161 ss << " count++;\n";
1162 for(int i = 0; i < dataCol; ++i){
1163 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
1164 ss << " value=tmp"<<i<<";\n";
1165 ss << " }\n";
1167 ss << " mean+=value;\n";
1168 ss << " }\n";
1169 ss << " }\n";
1171 ss << " if(count<=1)\n";
1172 ss << " return 0;\n";
1174 ss << " mean/=count;\n";
1176 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
1177 ss << " {\n";
1178 ss << " i = p;\n";
1179 for(int i = 0; i < dataCol; ++i){
1180 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1181 formula::svDoubleVectorRef)
1182 throw Unhandled();
1183 ss << " tmp"<<i<<"=";
1184 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1185 ss << " if(isNan(tmp"<<i<<"))\n";
1186 ss <<" tmp"<<i<<" = 0;\n";
1188 ss << " flag = false;\n";
1189 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
1190 ss << " {\n";
1191 ss << " i = j;\n";
1192 ss << " if (flag)\n";
1193 ss << " break;\n";
1194 ss << " else{\n";
1195 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
1196 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1197 formula::svDoubleVectorRef)
1198 throw Unhandled();
1199 ss << " tmp"<<i<<"=";
1200 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1201 ss << " if(!isNan(tmp"<<i<<")){\n";
1202 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
1203 ss << i<<"){\n";
1204 ss << " continue;\n";
1205 ss << " }\n";
1206 ss << " }\n";
1208 ss << " flag=true;\n";
1209 ss << " }\n";
1210 ss << " }\n";
1211 ss << " if (flag){\n";
1212 for(int i = 0; i < dataCol; ++i){
1213 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
1214 ss << " value=tmp"<<i<<";\n";
1215 ss << " }\n";
1217 ss << " var+=pow(mean-value,2);\n";
1218 ss << " }\n";
1219 ss << " }\n";
1221 ss << " var = var/(count-1);\n";
1223 else
1224 ss << "var = -1;\n";
1226 else
1227 ss << "var = -1;\n";
1228 ss << " return var;\n";
1229 ss << "}";
1232 void OpDvarp::GenSlidingWindowFunction(std::stringstream &ss,
1233 const std::string &sSymName, SubArguments &vSubArguments)
1235 ss << "\ndouble " << sSymName;
1236 ss << "_"<< BinFuncName() <<"(";
1237 for (unsigned i = 0; i < vSubArguments.size(); i++)
1239 if (i)
1240 ss << ",";
1241 vSubArguments[i]->GenSlidingWindowDecl(ss);
1243 ss << ")\n {\n";
1244 ss << " int gid0=get_global_id(0);\n";
1245 ss << " double var = 0;\n";
1246 ss << " double mean = 0;\n";
1247 ss << " double value =0;\n";
1248 ss << " int count = 0;\n";
1249 GenTmpVariables(ss,vSubArguments);
1250 int dataCol = 0;
1251 int dataRow = 0;
1252 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
1253 formula::svDoubleVectorRef)
1255 formula::FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1256 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
1257 formula::DoubleVectorRefToken *>(tmpCur);
1258 dataCol = pCurDVR1->GetArrays().size();
1259 dataRow = pCurDVR1->GetArrayLength();
1261 else
1262 throw Unhandled();
1263 if(vSubArguments[dataCol]->GetFormulaToken()->GetType() ==
1264 formula::svSingleVectorRef)
1266 const formula::SingleVectorRefToken*pTmpDVR1= static_cast<const
1267 formula::SingleVectorRefToken *>(vSubArguments[dataCol]->
1268 GetFormulaToken());
1269 ss << " tmp"<<dataCol<<"=";
1270 ss << vSubArguments[dataCol]->GenSlidingWindowDeclRef()<<";\n";
1271 ss << " if(gid0>="<<pTmpDVR1->GetArrayLength()<<" ||isNan(";
1272 ss << "tmp"<<dataCol<<"))\n";
1273 ss << " tmp"<<dataCol<<"=0;\n";
1275 else
1276 throw Unhandled();
1277 int conditionCol = 0;
1278 int conditionRow = 0;
1279 if(vSubArguments[dataCol + 1]->GetFormulaToken()->GetType() ==
1280 formula::svDoubleVectorRef)
1282 formula::FormulaToken *tmpCur = vSubArguments[dataCol + 1]->
1283 GetFormulaToken();
1284 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
1285 formula::DoubleVectorRefToken *>(tmpCur);
1286 conditionCol = pCurDVR2->GetArrays().size();
1287 conditionRow = pCurDVR2->GetArrayLength();
1289 else{
1290 throw Unhandled();
1292 if(dataCol!=conditionCol)
1293 throw Unhandled();
1294 if(dataCol > 0 && dataRow > 0)
1296 formula::FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
1297 formula::FormulaToken *tmpCur2 = vSubArguments[dataCol + 1]->
1298 GetFormulaToken();
1299 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
1300 formula::DoubleVectorRefToken *>(tmpCur1);
1301 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
1302 formula::DoubleVectorRefToken *>(tmpCur2);
1304 if(pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed() &&
1305 pCurDVR2->IsStartFixed() && pCurDVR2->IsEndFixed())
1307 ss << " int i,j,p;\n";
1308 ss << " bool flag;\n";
1310 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
1311 ss << " {\n";
1312 ss << " i = p;\n";
1313 for(int i = 0; i < dataCol; ++i){
1314 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1315 formula::svDoubleVectorRef)
1316 throw Unhandled();
1317 ss << " tmp"<<i<<"=";
1318 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1319 ss << " if(isNan(tmp"<<i<<"))\n";
1320 ss <<" tmp"<<i<<" = 0;\n";
1322 ss << " flag = false;\n";
1323 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
1324 ss << " {\n";
1325 ss << " i = j;\n";
1326 ss << " if (flag)\n";
1327 ss << " break;\n";
1328 ss << " else{\n";
1329 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
1330 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1331 formula::svDoubleVectorRef)
1332 throw Unhandled();
1333 ss << " tmp"<<i<<"=";
1334 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1335 ss << " if(!isNan(tmp"<<i<<")){\n";
1336 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
1337 ss << i<<"){\n";
1338 ss << " continue;\n";
1339 ss << " }\n";
1340 ss << " }\n";
1342 ss << " flag=true;\n";
1343 ss << " }\n";
1344 ss << " }\n";
1345 ss << " if (flag){\n";
1346 ss << " count++;\n";
1347 for(int i = 0; i < dataCol; ++i){
1348 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
1349 ss << " value=tmp"<<i<<";\n";
1350 ss << " }\n";
1352 ss << " mean+=value;\n";
1353 ss << " }\n";
1354 ss << " }\n";
1356 ss << " if(count<=0)\n";
1357 ss << " return 0;\n";
1359 ss << " mean/=count;\n";
1361 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
1362 ss << " {\n";
1363 ss << " i = p;\n";
1364 for(int i = 0; i < dataCol; ++i){
1365 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1366 formula::svDoubleVectorRef)
1367 throw Unhandled();
1368 ss << " tmp"<<i<<"=";
1369 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1370 ss << " if(isNan(tmp"<<i<<"))\n";
1371 ss <<" tmp"<<i<<" = 0;\n";
1373 ss << " flag = false;\n";
1374 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
1375 ss << " {\n";
1376 ss << " i = j;\n";
1377 ss << " if (flag)\n";
1378 ss << " break;\n";
1379 ss << " else{\n";
1380 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
1381 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1382 formula::svDoubleVectorRef)
1383 throw Unhandled();
1384 ss << " tmp"<<i<<"=";
1385 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1386 ss << " if(!isNan(tmp"<<i<<")){\n";
1387 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
1388 ss << i<<"){\n";
1389 ss << " continue;\n";
1390 ss << " }\n";
1391 ss << " }\n";
1393 ss << " flag=true;\n";
1394 ss << " }\n";
1395 ss << " }\n";
1396 ss << " if (flag){\n";
1397 for(int i = 0; i < dataCol; ++i){
1398 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
1399 ss << " value=tmp"<<i<<";\n";
1400 ss << " }\n";
1402 ss << " var+=pow(mean-value,2);\n";
1403 ss << " }\n";
1404 ss << " }\n";
1406 ss << " var = var/count;\n";
1408 else
1409 ss << "var = -1;\n";
1411 else
1412 ss << "var = -1;\n";
1413 ss << " return var;\n";
1414 ss << "}";
1417 void OpDcount::GenSlidingWindowFunction(std::stringstream &ss,
1418 const std::string &sSymName, SubArguments &vSubArguments)
1420 ss << "\ndouble " << sSymName;
1421 ss << "_"<< BinFuncName() <<"(";
1422 for (unsigned i = 0; i < vSubArguments.size(); i++)
1424 if (i)
1425 ss << ",";
1426 vSubArguments[i]->GenSlidingWindowDecl(ss);
1428 ss << ")\n {\n";
1429 ss << " int gid0=get_global_id(0);\n";
1430 ss << " double value=0;\n";
1431 ss << " int count = 0;\n";
1432 GenTmpVariables(ss,vSubArguments);
1433 int dataCol = 0;
1434 int dataRow = 0;
1435 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
1436 formula::svDoubleVectorRef)
1438 formula::FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1439 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
1440 formula::DoubleVectorRefToken *>(tmpCur);
1441 dataCol = pCurDVR1->GetArrays().size();
1442 dataRow = pCurDVR1->GetArrayLength();
1444 else
1445 throw Unhandled();
1446 if(vSubArguments[dataCol]->GetFormulaToken()->GetType() ==
1447 formula::svSingleVectorRef)
1449 const formula::SingleVectorRefToken*pTmpDVR1= static_cast<const
1450 formula::SingleVectorRefToken *>(vSubArguments[dataCol]
1451 ->GetFormulaToken());
1452 ss << " tmp"<<dataCol<<"=";
1453 ss << vSubArguments[dataCol]->GenSlidingWindowDeclRef()<<";\n";
1454 ss << " if(gid0>="<<pTmpDVR1->GetArrayLength()<<" ||isNan(";
1455 ss << "tmp"<<dataCol<<"))\n";
1456 ss << " tmp"<<dataCol<<"=DBL_MIN;\n";
1458 else
1459 throw Unhandled();
1460 int conditionCol = 0;
1461 int conditionRow = 0;
1462 if(vSubArguments[dataCol + 1]->GetFormulaToken()->GetType() ==
1463 formula::svDoubleVectorRef)
1465 formula::FormulaToken *tmpCur = vSubArguments[dataCol + 1]
1466 ->GetFormulaToken();
1467 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
1468 formula::DoubleVectorRefToken *>(tmpCur);
1469 conditionCol = pCurDVR2->GetArrays().size();
1470 conditionRow = pCurDVR2->GetArrayLength();
1472 else{
1473 throw Unhandled();
1475 if(dataCol!=conditionCol)
1476 throw Unhandled();
1477 if(dataCol > 0 && dataRow > 0)
1479 formula::FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
1480 formula::FormulaToken *tmpCur2 = vSubArguments[dataCol + 1]
1481 ->GetFormulaToken();
1482 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
1483 formula::DoubleVectorRefToken *>(tmpCur1);
1484 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
1485 formula::DoubleVectorRefToken *>(tmpCur2);
1487 if(pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed() &&
1488 pCurDVR2->IsStartFixed() && pCurDVR2->IsEndFixed())
1490 ss << " int i,j,p;\n";
1491 ss << " bool flag;\n";
1493 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
1494 ss << " {\n";
1495 ss << " i = p;\n";
1496 for(int i = 0; i < dataCol; ++i){
1497 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1498 formula::svDoubleVectorRef)
1499 throw Unhandled();
1500 ss << " tmp"<<i<<"=";
1501 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1502 ss << " if(isNan(tmp"<<i<<"))\n";
1503 ss <<" tmp"<<i<<" = 0;\n";
1505 ss << " flag = false;\n";
1506 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
1507 ss << " {\n";
1508 ss << " i = j;\n";
1509 ss << " if (flag)\n";
1510 ss << " break;\n";
1511 ss << " else{\n";
1512 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
1513 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1514 formula::svDoubleVectorRef)
1515 throw Unhandled();
1516 ss << " tmp"<<i<<"=";
1517 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1518 ss << " if(!isNan(tmp"<<i<<")){\n";
1519 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
1520 ss << i<<"){\n";
1521 ss << " continue;\n";
1522 ss << " }\n";
1523 ss << " }\n";
1525 ss << " flag=true;\n";
1526 ss << " }\n";
1527 ss << " }\n";
1528 ss << " if (flag){\n";
1529 for(int i = 0; i < dataCol; ++i){
1530 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
1531 ss << " value=tmp"<<i<<";\n";
1532 ss << " }\n";
1534 ss << " if(value > DBL_MIN)\n";
1535 ss << " count++;\n";
1536 ss << " }\n";
1537 ss << " }\n";
1539 else
1540 ss << "count = -1;\n";
1542 else
1543 ss << "count = -1;\n";
1544 ss << " return count;\n";
1545 ss << "}";
1548 void OpDcount2::GenSlidingWindowFunction(std::stringstream &ss,
1549 const std::string &sSymName, SubArguments &vSubArguments)
1551 ss << "\ndouble " << sSymName;
1552 ss << "_"<< BinFuncName() <<"(";
1553 for (unsigned i = 0; i < vSubArguments.size(); i++)
1555 if (i)
1556 ss << ",";
1557 vSubArguments[i]->GenSlidingWindowDecl(ss);
1559 ss << ")\n {\n";
1560 ss << " int gid0=get_global_id(0);\n";
1561 ss << " double value=0;\n";
1562 ss << " int count = 0;\n";
1563 GenTmpVariables(ss,vSubArguments);
1564 int dataCol = 0;
1565 int dataRow = 0;
1566 if(vSubArguments[0]->GetFormulaToken()->GetType() ==
1567 formula::svDoubleVectorRef)
1569 formula::FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
1570 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
1571 formula::DoubleVectorRefToken *>(tmpCur);
1572 dataCol = pCurDVR1->GetArrays().size();
1573 dataRow = pCurDVR1->GetArrayLength();
1575 else
1576 throw Unhandled();
1577 if(vSubArguments[dataCol]->GetFormulaToken()->GetType() ==
1578 formula::svSingleVectorRef)
1580 const formula::SingleVectorRefToken*pTmpDVR1= static_cast<const
1581 formula::SingleVectorRefToken *>(vSubArguments[dataCol]
1582 ->GetFormulaToken());
1583 ss << " tmp"<<dataCol<<"=";
1584 ss << vSubArguments[dataCol]->GenSlidingWindowDeclRef()<<";\n";
1585 ss << " if(gid0>="<<pTmpDVR1->GetArrayLength()<<" ||isNan(";
1586 ss << "tmp"<<dataCol<<"))\n";
1587 ss << " tmp"<<dataCol<<"=DBL_MIN;\n";
1589 else
1590 throw Unhandled();
1591 int conditionCol = 0;
1592 int conditionRow = 0;
1593 if(vSubArguments[dataCol + 1]->GetFormulaToken()->GetType() ==
1594 formula::svDoubleVectorRef)
1596 formula::FormulaToken *tmpCur = vSubArguments[dataCol + 1]->
1597 GetFormulaToken();
1598 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
1599 formula::DoubleVectorRefToken *>(tmpCur);
1600 conditionCol = pCurDVR2->GetArrays().size();
1601 conditionRow = pCurDVR2->GetArrayLength();
1603 else{
1604 throw Unhandled();
1606 if(dataCol!=conditionCol)
1607 throw Unhandled();
1608 if(dataCol > 0 && dataRow > 0)
1610 formula::FormulaToken *tmpCur1 = vSubArguments[0]->GetFormulaToken();
1611 formula::FormulaToken *tmpCur2 = vSubArguments[dataCol + 1]->
1612 GetFormulaToken();
1613 const formula::DoubleVectorRefToken*pCurDVR1= static_cast<const
1614 formula::DoubleVectorRefToken *>(tmpCur1);
1615 const formula::DoubleVectorRefToken*pCurDVR2= static_cast<const
1616 formula::DoubleVectorRefToken *>(tmpCur2);
1618 if(pCurDVR1->IsStartFixed() && pCurDVR1->IsEndFixed() &&
1619 pCurDVR2->IsStartFixed() && pCurDVR2->IsEndFixed())
1621 ss << " int i,j,p;\n";
1622 ss << " bool flag;\n";
1624 ss << " for(p = 1;p < " << dataRow << ";++p)\n";
1625 ss << " {\n";
1626 ss << " i = p;\n";
1627 for(int i = 0; i < dataCol; ++i){
1628 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1629 formula::svDoubleVectorRef)
1630 throw Unhandled();
1631 ss << " tmp"<<i<<"=";
1632 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1633 ss << " if(isNan(tmp"<<i<<"))\n";
1634 ss <<" tmp"<<i<<" = 0;\n";
1636 ss << " flag = false;\n";
1637 ss << " for(j = 1; j < " << conditionRow << ";++j)\n";
1638 ss << " {\n";
1639 ss << " i = j;\n";
1640 ss << " if (flag)\n";
1641 ss << " break;\n";
1642 ss << " else{\n";
1643 for(int i = dataCol + 1; i < dataCol + 1 + conditionCol; ++i){
1644 if(vSubArguments[i]->GetFormulaToken()->GetType() !=
1645 formula::svDoubleVectorRef)
1646 throw Unhandled();
1647 ss << " tmp"<<i<<"=";
1648 ss << vSubArguments[i]->GenSlidingWindowDeclRef()<<";\n";
1649 ss << " if(!isNan(tmp"<<i<<")){\n";
1650 ss << " if(tmp"<<(i-dataCol-1)<<"!=tmp";
1651 ss << i<<"){\n";
1652 ss << " continue;\n";
1653 ss << " }\n";
1654 ss << " }\n";
1656 ss << " flag=true;\n";
1657 ss << " }\n";
1658 ss << " }\n";
1659 ss << " if (flag){\n";
1660 for(int i = 0; i < dataCol; ++i){
1661 ss << " if(tmp"<<dataCol<<"=="<<(i+1)<<"){\n";
1662 ss << " value=tmp"<<i<<";\n";
1663 ss << " }\n";
1665 ss << " if(value > DBL_MIN)\n";
1666 ss << " count++;\n";
1667 ss << " }\n";
1668 ss << " }\n";
1670 else
1671 ss << "count = -1;\n";
1673 else
1674 ss << "count = -1;\n";
1675 ss << " return count;\n";
1676 ss << "}";
1681 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */