update emoji autocorrect entries from po-files
[LibreOffice.git] / sc / source / core / opencl / op_statistical.cxx
blob6e38f843eed06e91d34dfe323b15e92bb6ad9029
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_statistical.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>
20 #include "opinlinefun_statistical.cxx"
22 using namespace formula;
24 namespace sc { namespace opencl {
25 void OpVar::GenSlidingWindowFunction(std::stringstream &ss,
26 const std::string &sSymName, SubArguments &vSubArguments)
28 ss << "\ndouble " << sSymName;
29 ss << "_" << BinFuncName() << "(";
30 for (unsigned i = 0; i < vSubArguments.size(); i++)
32 if (i)
33 ss << ",";
34 vSubArguments[i]->GenSlidingWindowDecl(ss);
36 ss << "){\n";
37 ss << " int gid0 = get_global_id(0);\n";
38 ss << " double fSum = 0.0;\n";
39 ss << " double fMean = 0.0;\n";
40 ss << " double vSum = 0.0;\n";
41 ss << " double fCount = 0.0;\n";
42 ss << " double arg = 0.0;\n";
43 unsigned i = vSubArguments.size();
44 while (i--)
46 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
47 assert(pCur);
48 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
50 if (pCur->GetType() == formula::svDoubleVectorRef)
52 const formula::DoubleVectorRefToken* pDVR =
53 static_cast<const formula::DoubleVectorRefToken *>(pCur);
54 size_t nCurWindowSize = pDVR->GetRefRowSize();
55 ss << " for (int i = ";
56 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
58 ss << "gid0; i < " << pDVR->GetArrayLength();
59 ss << " && i < " << nCurWindowSize << "; i++)\n";
60 ss << " {\n";
62 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
64 ss << "0; i < " << pDVR->GetArrayLength();
65 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
66 ss << " {\n";
68 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
70 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
71 ss << " && i < " << nCurWindowSize << "; i++)\n";
72 ss << " {\n";
74 else
76 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
77 ss << " {\n";
79 ss << " arg = ";
80 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
81 ss << ";\n";
82 ss << " if (isNan(arg))\n";
83 ss << " continue;\n";
84 ss << " fSum += arg;\n";
85 ss << " fCount += 1.0;\n";
86 ss << " }\n";
88 else if (pCur->GetType() == formula::svSingleVectorRef)
90 const formula::SingleVectorRefToken* pSVR =
91 static_cast< const formula::SingleVectorRefToken*>(pCur);
92 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
93 ss << " {\n";
94 ss << " arg = ";
95 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
96 ss << " if (!isNan(arg))\n";
97 ss << " {\n";
98 ss << " fSum += arg;\n";
99 ss << " fCount += 1.0;\n";
100 ss << " }\n";
101 ss << " }\n";
103 else
105 ss << " arg = " << pCur->GetDouble() << ";\n";
106 ss << " fSum += arg;\n";
107 ss << " fCount += 1.0;\n";
110 else
112 ss << " arg = ";
113 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
114 ss << " fSum += arg;\n";
115 ss << " fCount += 1.0;\n";
117 if (i == 0)
119 ss << " fMean = fSum * pow(fCount,-1.0);\n";
122 i = vSubArguments.size();
123 while (i--)
125 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
126 assert(pCur);
127 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
129 if (pCur->GetType() == formula::svDoubleVectorRef)
131 const formula::DoubleVectorRefToken* pDVR =
132 static_cast<const formula::DoubleVectorRefToken *>(pCur);
133 size_t nCurWindowSize = pDVR->GetRefRowSize();
134 ss << " for (int i = ";
135 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
137 ss << "gid0; i < " << pDVR->GetArrayLength();
138 ss << " && i < " << nCurWindowSize << "; i++)\n";
139 ss << " {\n";
141 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
143 ss << "0; i < " << pDVR->GetArrayLength();
144 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
145 ss << " {\n";
147 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
149 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
150 ss << " && i < " << nCurWindowSize << "; i++)\n";
151 ss << " {\n";
153 else
155 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
156 ss << " {\n";
159 ss << " arg = ";
160 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
161 ss << ";\n";
162 ss << " if (isNan(arg))\n";
163 ss << " continue;\n";
164 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
165 ss << " }\n";
167 else if (pCur->GetType() == formula::svSingleVectorRef)
169 const formula::SingleVectorRefToken* pSVR =
170 static_cast< const formula::SingleVectorRefToken*>(pCur);
171 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
172 ss << " {\n";
173 ss << " if (!isNan(";
174 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
175 ss << "))\n";
176 ss << " {\n";
177 ss << " arg = ";
178 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
179 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
180 ss << " }\n";
181 ss << " }\n";
183 else
185 ss << " arg=" << pCur->GetDouble() << ";\n";
186 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
189 else
191 ss << " arg = ";
192 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
193 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
196 ss << " if (fCount <= 1.0)\n";
197 ss << " return CreateDoubleError(errDivisionByZero);\n";
198 ss << " else\n";
199 ss << " return vSum * pow(fCount - 1.0,-1.0);\n";
200 ss << "}\n";
202 void OpZTest::BinInlineFun(std::set<std::string>& decls,
203 std::set<std::string>& funs)
205 decls.insert(phiDecl);
206 funs.insert(phi);
207 decls.insert(taylorDecl);
208 funs.insert(taylor);
209 decls.insert(gaussDecl);
210 funs.insert(gauss);
212 void OpZTest::GenSlidingWindowFunction(std::stringstream &ss,
213 const std::string &sSymName, SubArguments &vSubArguments)
215 ss << "\ndouble " << sSymName;
216 ss << "_" << BinFuncName() << "(";
217 for (unsigned i = 0; i < vSubArguments.size(); i++)
219 if (i)
220 ss << ",";
221 vSubArguments[i]->GenSlidingWindowDecl(ss);
223 ss << "){\n";
224 ss << " int gid0 = get_global_id(0);\n";
225 ss << " double fSum = 0.0;\n";
226 ss << " double fSumSqr = 0.0;\n";
227 ss << " double mue = 0.0;\n";
228 ss << " double fCount = 0.0;\n";
229 ss << " double arg = 0.0;\n";
230 ss << " double sigma = 0.0;\n";
231 ss << " double mu = 0.0;\n";
232 if(vSubArguments.size() == 1 || vSubArguments.size() == 0)
234 ss << " return DBL_MAX;\n";
235 ss << "}\n";
236 return ;
238 else if(vSubArguments.size() == 2)
240 FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
241 FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
242 assert(pCur);
243 assert(pCur1);
244 if(pCur->GetType() == formula::svDoubleVectorRef)
246 const formula::DoubleVectorRefToken* pDVR =
247 static_cast<const formula::DoubleVectorRefToken *>(pCur);
248 size_t nCurWindowSize = pDVR->GetRefRowSize();
249 ss << " for (int i = ";
250 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
252 ss << "gid0; i < " << pDVR->GetArrayLength();
253 ss << " && i < " << nCurWindowSize << "; i++)\n";
254 ss << " {\n";
256 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
258 ss << "0; i < " << pDVR->GetArrayLength();
259 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
260 ss << " {\n";
262 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
264 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
265 ss << " && i < " << nCurWindowSize << "; i++)\n";
266 ss << " {\n";
268 else
270 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
271 ss << " {\n";
274 ss << " arg = ";
275 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
276 ss << " if (isNan(arg))\n";
277 ss << " continue;\n";
278 ss << " fSum += arg;\n";
279 ss << " fSumSqr += arg * arg;\n";
280 ss << " fCount += 1.0;\n";
281 ss << " }\n";
282 ss << " if(fCount <= 1.0)\n";
283 ss << " return DBL_MAX;\n";
284 ss << " mue = fSum *pow(fCount,-1.0);\n";
285 ss << " sigma = (fSumSqr-fSum*fSum*";
286 ss << "pow(fCount,-1.0))*pow(fCount-1.0,-1.0);\n";
288 else
290 ss << " return DBL_MAX;\n";
291 ss << "}\n";
292 return ;
294 if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
296 if(pCur1->GetType() == formula::svSingleVectorRef)
298 const formula::SingleVectorRefToken* pSVR =
299 static_cast<const formula::SingleVectorRefToken* >(pCur1);
300 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
301 ss << " {\n";
302 ss << " mu = " ;
303 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
304 ss << " if (isNan(mu))\n";
305 ss << " mu = 0.0;\n";
306 ss << " }\n";
309 else if(pCur1->GetType() == formula::svDouble)
311 ss << " mu = " << pCur1->GetDouble() << ";\n";
313 else
315 ss << " return DBL_MAX;\n";
316 ss << "}\n";
317 return ;
320 else
322 ss << " mu = " ;
323 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
325 ss << " return 0.5 - gauss((mue-mu)/sqrt(sigma/fCount));\n";
326 ss << "}\n";
327 return ;
329 else
331 FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
332 FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
333 FormulaToken *pCur2 = vSubArguments[2]->GetFormulaToken();
334 assert(pCur);
335 assert(pCur1);
336 assert(pCur2);
337 if(pCur->GetType() == formula::svDoubleVectorRef)
339 const formula::DoubleVectorRefToken* pDVR =
340 static_cast<const formula::DoubleVectorRefToken *>(pCur);
341 size_t nCurWindowSize = pDVR->GetRefRowSize();
342 ss << " for (int i = ";
343 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
345 ss << "gid0; i < " << pDVR->GetArrayLength();
346 ss << " && i < " << nCurWindowSize << "; i++)\n";
347 ss << " {\n";
349 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
351 ss << "0; i < " << pDVR->GetArrayLength();
352 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
353 ss << " {\n";
355 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
357 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
358 ss << " && i < " << nCurWindowSize << "; i++)\n";
359 ss << " {\n";
361 else
363 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
364 ss << " {\n";
366 ss << " arg = ";
367 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
368 ss << " if (isNan(arg))\n";
369 ss << " continue;\n";
370 ss << " fSum += arg;\n";
371 ss << " fSumSqr += arg * arg;\n";
372 ss << " fCount += 1.0;\n";
373 ss << " }\n";
374 ss << " if(fCount <= 1.0)\n";
375 ss << " return DBL_MAX;\n";
376 ss << " mue = fSum * pow(fCount,-1.0);\n";
378 else
380 ss << " return DBL_MAX;\n";
381 ss << "}\n";
382 return ;
384 if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
386 if(pCur1->GetType() == formula::svSingleVectorRef)
388 const formula::SingleVectorRefToken* pSVR1 =
389 static_cast<const formula::SingleVectorRefToken* >(pCur1);
390 ss << " if (gid0 < " << pSVR1->GetArrayLength() << ")\n";
391 ss << " {\n";
392 ss << " mu = " ;
393 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
394 ss << " if (isNan(mu))\n";
395 ss << " mu = 0.0;\n";
396 ss << " }\n";
398 else if(pCur1->GetType() == formula::svDouble)
400 ss << " mu = " << pCur1->GetDouble() << ";\n";
402 else
404 ss << " return DBL_MAX;\n";
405 ss << "}\n";
406 return ;
409 else
411 ss << " mu=" ;
412 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
414 if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
416 if(pCur2->GetType() == formula::svSingleVectorRef)
418 const formula::SingleVectorRefToken* pSVR2 =
419 static_cast<const formula::SingleVectorRefToken* >(pCur2);
420 ss << " if (gid0 < " << pSVR2->GetArrayLength() << ")\n";
421 ss << " {\n";
422 ss << " sigma = " ;
423 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
424 ss << " if (isNan(sigma))\n";
425 ss << " sigma = 0.0;\n";
426 ss << " }\n";
428 else if(pCur2->GetType() == formula::svDouble)
430 ss << " sigma = " << pCur2->GetDouble() << ";\n";
432 else
434 ss << " return DBL_MAX;\n";
435 ss << "}\n";
436 return ;
439 else
441 ss << " sigma = " ;
442 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
444 ss << " return 0.5 - gauss((mue-mu)*sqrt(fCount)/sigma);\n";
445 ss << "}\n";
449 void OpTTest::BinInlineFun(std::set<std::string>& decls,
450 std::set<std::string>& funs)
452 decls.insert(fMachEpsDecl);
453 funs.insert("");
454 decls.insert(fMaxGammaArgumentDecl);
455 funs.insert("");
456 decls.insert(lcl_getLanczosSumDecl);
457 funs.insert(lcl_getLanczosSum);
458 decls.insert(GetBetaDecl);
459 funs.insert(GetBeta);
460 decls.insert(GetLogBetaDecl);
461 funs.insert(GetLogBeta);
462 decls.insert(GetBetaDistPDFDecl);
463 funs.insert(GetBetaDistPDF);
464 decls.insert(lcl_GetBetaHelperContFracDecl);
465 funs.insert(lcl_GetBetaHelperContFrac);
466 decls.insert(GetBetaDistDecl);
467 funs.insert(GetBetaDist);
468 decls.insert(GetTDistDecl);
469 funs.insert(GetTDist);
472 void OpTTest::GenSlidingWindowFunction(std::stringstream &ss,
473 const std::string &sSymName, SubArguments &vSubArguments)
475 ss << "\ndouble " << sSymName;
476 ss << "_" << BinFuncName() << "(";
477 for (unsigned i = 0; i < vSubArguments.size(); i++)
479 if (i)
480 ss << ",";
481 vSubArguments[i]->GenSlidingWindowDecl(ss);
483 ss << "){\n";
484 ss << " int gid0 = get_global_id(0);\n";
485 ss << " double fSum1 = 0.0;\n";
486 ss << " double fSum2 = 0.0;\n";
487 ss << " double fSumSqr1 = 0.0;\n";
488 ss << " double fSumSqr2 = 0.0;\n";
489 ss << " double fCount1 = 0.0;\n";
490 ss << " double fCount2 = 0.0;\n";
491 ss << " double arg1 = 0.0;\n";
492 ss << " double arg2 = 0.0;\n";
493 ss << " double mode = 0.0;\n";
494 ss << " double type = 0.0;\n";
495 ss << " double fT = 0.0;\n";
496 ss << " double fF = 0.0;\n";
497 if(vSubArguments.size() != 4)
499 ss << " return DBL_MAX;\n";
500 ss << "}\n";
501 return ;
503 if(vSubArguments.size() == 4)
505 FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
506 FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
507 FormulaToken *pCur2 = vSubArguments[2]->GetFormulaToken();
508 FormulaToken *pCur3 = vSubArguments[3]->GetFormulaToken();
509 assert(pCur);
510 assert(pCur1);
511 assert(pCur2);
512 assert(pCur3);
513 if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
515 if(pCur2->GetType() == formula::svSingleVectorRef)
517 const formula::SingleVectorRefToken* pSVR =
518 static_cast< const formula::SingleVectorRefToken*>(pCur2);
519 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
520 ss << " {\n";
521 ss << " mode = " ;
522 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
523 ss << " if (isNan(mode))\n";
524 ss << " mode = 0.0;\n";
525 ss << " else\n";
526 ss << " mode = floor(mode);\n";
527 ss << " }\n";
529 else if(pCur2->GetType() == formula::svDouble)
531 ss << " mode = floor(convert_double(";
532 ss << pCur2->GetDouble() << "));\n";
534 else
536 ss << " return DBL_MAX;\n";
537 ss << "}\n";
538 return ;
541 else
543 ss << " mode = floor(" ;
544 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ");\n";
546 ss << " if(!(mode == 1.0 || mode == 2.0))\n";
547 ss << " return DBL_MAX;\n";
548 if(ocPush==vSubArguments[3]->GetFormulaToken()->GetOpCode())
550 if(pCur3->GetType() == formula::svSingleVectorRef)
552 const formula::SingleVectorRefToken* pSVR =
553 static_cast< const formula::SingleVectorRefToken*>(pCur3);
554 assert(pSVR);
555 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
556 ss << " {\n";
557 ss << " if (isNan(";
558 ss << vSubArguments[3]->GenSlidingWindowDeclRef() << "))\n";
559 ss << " type=0.0;\n";
560 ss << " else\n";
561 ss << " type=floor(";
562 ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ");\n";
563 ss << " }\n";
565 else if(pCur3->GetType() == formula::svDouble)
567 ss << " type = floor(convert_double(" << pCur3->GetDouble() <<
568 "));\n";
570 else
572 ss << " return DBL_MAX;\n";
573 ss << "}\n";
574 return ;
577 else
579 ss << " type=floor(";
580 ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ");\n";
582 ss << " if(!(type == 1.0||type == 2.0||type == 3.0))\n";
583 ss << " return DBL_MAX;\n";
585 if(pCur->GetType() == formula::svDoubleVectorRef &&
586 pCur1->GetType() == formula::svDoubleVectorRef)
588 const formula::DoubleVectorRefToken* pDVR =
589 static_cast<const formula::DoubleVectorRefToken *>(pCur);
590 const formula::DoubleVectorRefToken* pDVR1 =
591 static_cast<const formula::DoubleVectorRefToken *>(pCur1);
593 size_t nCurWindowSize = pDVR->GetRefRowSize();
594 size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
596 if(nCurWindowSize == nCurWindowSize1)
598 ss << " if(type == 1.0)\n";
599 ss << " {\n";
600 ss << " for (int i = ";
601 if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed()) &&
602 (!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
604 ss << "gid0; i < " << pDVR->GetArrayLength();
605 ss << " && i < " << nCurWindowSize << "; i++)\n";
606 ss << " {\n";
608 else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed()) &&
609 (pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
611 ss << "0; i < " << pDVR->GetArrayLength();
612 ss << " && i < gid0+"<< nCurWindowSize << "; i++)\n";
613 ss << " {\n";
615 else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed()) &&
616 (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
618 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
619 ss << " && i < " << nCurWindowSize << "; i++)\n";
620 ss << " {\n";
622 else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed()) &&
623 (pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
625 ss << "0; i < " << nCurWindowSize << "; i++)\n";
626 ss << " {\n";
628 else
630 ss << "0; i < " << nCurWindowSize << "; i++)\n";
631 ss << " {\n";
632 ss << " break;\n";
633 ss << " }";
634 ss << " return DBL_MAX;\n";
635 ss << " }\n";
636 ss << "}\n";
637 return ;
640 ss << " arg1 = ";
641 ss << vSubArguments[0]->GenSlidingWindowDeclRef(true) << ";\n";
642 ss << " arg2 = ";
643 ss << vSubArguments[1]->GenSlidingWindowDeclRef(true) << ";\n";
644 ss << " if (isNan(arg1)||isNan(arg2))\n";
645 ss << " continue;\n";
646 ss << " fSum1 += arg1;\n";
647 ss << " fSum2 += arg2;\n";
648 ss << " fSumSqr1 += (arg1 - arg2)*(arg1 - arg2);\n";
649 ss << " fCount1 += 1;\n";
650 ss << " }\n";
651 ss << " if(fCount1 < 1.0)\n";
652 ss << " return DBL_MAX;\n";
653 ss << " fT = sqrt(fCount1-1.0) * fabs(fSum1 - fSum2)\n";
654 ss << " /sqrt(fCount1 * fSumSqr1 - (fSum1-fSum2)\n";
655 ss << " *(fSum1-fSum2));\n";
656 ss << " fF = fCount1 - 1.0;\n";
658 else
660 ss << " return DBL_MAX;\n";
661 ss << "}\n";
662 return ;
665 else
667 ss << " return DBL_MAX;\n";
668 ss << "}\n";
669 return ;
671 ss << " }\n";
672 ss << " if(type == 2.0 || type == 3.0)\n";
673 ss << " {\n";
675 if(pCur->GetType() == formula::svDoubleVectorRef &&
676 pCur1->GetType() == formula::svDoubleVectorRef)
678 const formula::DoubleVectorRefToken* pDVR =
679 static_cast<const formula::DoubleVectorRefToken *>(pCur);
680 const formula::DoubleVectorRefToken* pDVR1 =
681 static_cast<const formula::DoubleVectorRefToken *>(pCur1);
683 size_t nCurWindowSize = pDVR->GetRefRowSize();
684 size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
685 ss << " for (int i = ";
686 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
688 ss << "gid0; i < " << pDVR->GetArrayLength();
689 ss << " && i < " << nCurWindowSize << "; i++)\n";
690 ss << " {\n";
692 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
694 ss << "0; i < " << pDVR->GetArrayLength();
695 ss << " && i < gid0+"<< nCurWindowSize << "; i++)\n";
696 ss << " {\n";
698 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
700 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
701 ss << " && i < " << nCurWindowSize << "; i++)\n";
702 ss << " {\n";
704 else
706 ss << "0; i < " << nCurWindowSize << "; i++)\n";
707 ss << " {\n";
710 ss << " arg1 = ";
711 ss << vSubArguments[0]->GenSlidingWindowDeclRef(true) << ";\n";
712 ss << " if (isNan(arg1))\n";
713 ss << " continue;\n";
714 ss << " fSum1 += arg1;\n";
715 ss << " fSumSqr1 += arg1 * arg1;\n";
716 ss << " fCount1 += 1;\n";
717 ss << " }\n";
719 ss << " for (int i = ";
720 if (!pDVR1->IsStartFixed() && pDVR1->IsEndFixed())
722 ss << "gid0; i < " << pDVR1->GetArrayLength();
723 ss << " && i < " << nCurWindowSize1 << "; i++)\n";
724 ss << " {\n";
726 else if (pDVR1->IsStartFixed() && !pDVR1->IsEndFixed())
728 ss << "0; i < " << pDVR1->GetArrayLength();
729 ss << " && i < gid0+"<< nCurWindowSize1 << "; i++)\n";
730 ss << " {\n";
732 else if (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed())
734 ss << "0; i + gid0 < " << pDVR1->GetArrayLength();
735 ss << " && i < " << nCurWindowSize1 << "; i++)\n";
736 ss << " {\n";
738 else
740 ss << "0; i < " << nCurWindowSize1 << "; i++)\n";
741 ss << " {\n";
743 ss << " arg2 = ";
744 ss << vSubArguments[1]->GenSlidingWindowDeclRef(true) << ";\n";
745 ss << " if (isNan(arg2))\n";
746 ss << " continue;\n";
747 ss << " fSum2 += arg2;\n";
748 ss << " fSumSqr2 += arg2 * arg2;\n";
749 ss << " fCount2 += 1;\n";
750 ss << " }\n";
752 else
754 ss << " return DBL_MAX;\n";
755 ss << " }\n";
756 ss << "}\n";
757 return ;
759 ss << " if (fCount1 < 2.0 || fCount2 < 2.0)\n";
760 ss << " return DBL_MAX;\n";
761 ss << " }\n";
762 ss << " if(type == 3.0)\n";
763 ss << " {\n";
764 ss << " double fS1 = (fSumSqr1-fSum1*fSum1/fCount1)\n";
765 ss << " /(fCount1-1.0)/fCount1;\n";
766 ss << " double fS2 = (fSumSqr2-fSum2*fSum2/fCount2)\n";
767 ss << " /(fCount2-1.0)/fCount2;\n";
768 ss << " if (fS1 + fS2 == 0.0)\n";
769 ss << " return DBL_MAX;\n";
770 ss << " fT = fabs(fSum1/fCount1 - fSum2/fCount2)\n";
771 ss << " /sqrt(fS1+fS2);\n";
772 ss << " double c = fS1/(fS1+fS2);\n";
773 ss << " fF = 1.0/(c*c/(fCount1-1.0)+(1.0-c)*(1.0-c)\n";
774 ss << " /(fCount2-1.0));\n";
775 ss << " }\n";
776 ss << " if(type == 2.0)\n";
777 ss << " {\n";
778 ss << " double fS1 = (fSumSqr1 - fSum1*fSum1/fCount1)\n";
779 ss << " /(fCount1 - 1.0);\n";
780 ss << " double fS2 = (fSumSqr2 - fSum2*fSum2/fCount2)\n";
781 ss << " /(fCount2 - 1.0);\n";
782 ss << " fT = fabs( fSum1/fCount1 - fSum2/fCount2 )\n";
783 ss << " /sqrt( (fCount1-1.0)*fS1 + (fCount2-1.0)*fS2 )\n";
784 ss << " *sqrt( fCount1*fCount2*(fCount1+fCount2-2)\n";
785 ss << " /(fCount1+fCount2) );\n";
786 ss << " fF = fCount1 + fCount2 - 2;\n";
787 ss << " }\n";
789 ss << " double tdist=GetTDist(fT, fF);\n";
790 ss << " if (mode==1)\n";
791 ss << " return tdist;\n";
792 ss << " else\n";
793 ss << " return 2.0*tdist;\n";
794 ss << "}\n";
797 void OpVarP::GenSlidingWindowFunction(std::stringstream &ss,
798 const std::string &sSymName, SubArguments &vSubArguments)
800 ss << "\ndouble " << sSymName;
801 ss << "_" << BinFuncName() << "(";
802 for (unsigned i = 0; i < vSubArguments.size(); i++)
804 if (i)
805 ss << ",";
806 vSubArguments[i]->GenSlidingWindowDecl(ss);
808 ss << "){\n";
809 ss << " int gid0 = get_global_id(0);\n";
810 ss << " double fSum = 0.0;\n";
811 ss << " double fMean = 0.0;\n";
812 ss << " double vSum = 0.0;\n";
813 ss << " double fCount = 0.0;\n";
814 ss << " double arg = 0.0;\n";
815 unsigned i = vSubArguments.size();
816 while (i--)
818 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
819 assert(pCur);
820 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
822 if (pCur->GetType() == formula::svDoubleVectorRef)
824 const formula::DoubleVectorRefToken* pDVR =
825 static_cast<const formula::DoubleVectorRefToken *>(pCur);
826 size_t nCurWindowSize = pDVR->GetRefRowSize();
827 ss << " for (int i = ";
828 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
830 ss << "gid0; i < " << pDVR->GetArrayLength();
831 ss << " && i < " << nCurWindowSize << "; i++)\n";
832 ss << " {\n";
834 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
836 ss << "0; i < " << pDVR->GetArrayLength();
837 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
838 ss << " {\n";
840 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
842 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
843 ss << " && i < " << nCurWindowSize << "; i++)\n";
844 ss << " {\n";
846 else
848 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
849 ss << " {\n";
851 ss << " arg = ";
852 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
853 ss << ";\n";
854 ss << " if (isNan(arg))\n";
855 ss << " continue;\n";
856 ss << " fSum += arg;\n";
857 ss << " fCount += 1.0;\n";
858 ss << " }\n";
860 else if (pCur->GetType() == formula::svSingleVectorRef)
862 const formula::SingleVectorRefToken* pSVR =
863 static_cast< const formula::SingleVectorRefToken*>(pCur);
864 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
865 ss << " {\n";
866 ss << " arg = ";
867 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
868 ss << " if (!isNan(arg))\n";
869 ss << " {\n";
870 ss << " fSum += arg;\n";
871 ss << " fCount += 1.0;\n";
872 ss << " }\n";
873 ss << " }\n";
875 else
877 ss << " arg = " << pCur->GetDouble() << ";\n";
878 ss << " fSum += arg;\n";
879 ss << " fCount += 1.0;\n";
882 else
884 ss << " arg = ";
885 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
886 ss << " fSum += arg;\n";
887 ss << " fCount += 1.0;\n";
889 if (i == 0)
891 ss << " fMean = fSum * pow(fCount,-1.0);\n";
894 i = vSubArguments.size();
895 while (i--)
897 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
898 assert(pCur);
899 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
901 if (pCur->GetType() == formula::svDoubleVectorRef)
903 const formula::DoubleVectorRefToken* pDVR =
904 static_cast<const formula::DoubleVectorRefToken *>(pCur);
905 size_t nCurWindowSize = pDVR->GetRefRowSize();
906 ss << " for (int i = ";
907 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
909 ss << "gid0; i < " << pDVR->GetArrayLength();
910 ss << " && i < " << nCurWindowSize << "; i++)\n";
911 ss << " {\n";
913 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
915 ss << "0; i < " << pDVR->GetArrayLength();
916 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
917 ss << " {\n";
919 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
921 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
922 ss << " && i < " << nCurWindowSize << "; i++)\n";
923 ss << " {\n";
925 else
927 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
928 ss << " {\n";
931 ss << " arg = ";
932 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
933 ss << ";\n";
934 ss << " if (isNan(arg))\n";
935 ss << " continue;\n";
936 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
937 ss << " }\n";
939 else if (pCur->GetType() == formula::svSingleVectorRef)
941 const formula::SingleVectorRefToken* pSVR =
942 static_cast< const formula::SingleVectorRefToken*>(pCur);
943 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
944 ss << " {\n";
945 ss << " arg = ";
946 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
947 ss << " if (!isNan(arg))\n";
948 ss << " {\n";
949 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
950 ss << " }\n";
951 ss << " }\n";
953 else
955 ss << " arg = " << pCur->GetDouble() << ";\n";
956 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
959 else
961 ss << " arg = ";
962 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
963 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
966 ss << " if (fCount == 0.0)\n";
967 ss << " return DBL_MAX;\n";
968 ss << " else\n";
969 ss << " return vSum * pow(fCount,-1.0);\n";
970 ss << "}\n";
973 void OpTDist::BinInlineFun(std::set<std::string>& decls,
974 std::set<std::string>& funs)
976 decls.insert(fMachEpsDecl);
977 funs.insert("");
978 decls.insert(fMaxGammaArgumentDecl);
979 funs.insert("");
980 decls.insert(lcl_getLanczosSumDecl);
981 funs.insert(lcl_getLanczosSum);
982 decls.insert(GetBetaDecl);
983 funs.insert(GetBeta);
984 decls.insert(GetLogBetaDecl);
985 funs.insert(GetLogBeta);
986 decls.insert(GetBetaDistPDFDecl);
987 funs.insert(GetBetaDistPDF);
988 decls.insert(lcl_GetBetaHelperContFracDecl);
989 funs.insert(lcl_GetBetaHelperContFrac);
990 decls.insert(GetBetaDistDecl);
991 funs.insert(GetBetaDist);
992 decls.insert(GetTDistDecl);
993 funs.insert(GetTDist);
995 void OpTDist::GenSlidingWindowFunction(std::stringstream &ss,
996 const std::string &sSymName, SubArguments &vSubArguments)
998 ss << "\ndouble " << sSymName;
999 ss << "_" << BinFuncName() << "(";
1000 for (unsigned i = 0; i < vSubArguments.size(); i++)
1002 if (i)
1003 ss << ",";
1004 vSubArguments[i]->GenSlidingWindowDecl(ss);
1006 ss << ") {\n";
1007 ss << " int gid0 = get_global_id(0);\n";
1008 ss << " double x = 0.0;\n";
1009 ss << " double fDF = 0.0;\n";
1010 ss << " double fFlag = 0.0;\n";
1011 if(vSubArguments.size() != 3)
1013 ss << " return DBL_MAX;\n}\n";
1014 return ;
1016 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1017 assert(tmpCur0);
1018 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1020 if(tmpCur0->GetType() == formula::svSingleVectorRef)
1022 const formula::SingleVectorRefToken*tmpCurDVR0 =
1023 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1024 ss << " if(gid0 < ";
1025 ss << tmpCurDVR0->GetArrayLength() << ")\n";
1026 ss << " {\n";
1027 ss << " x = ";
1028 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1029 ss << " if(isNan(x))\n";
1030 ss << " x = 0.0;\n";
1031 ss << " }\n";
1033 else if(tmpCur0->GetType() == formula::svDouble)
1035 ss << " x = " << tmpCur0->GetDouble() << ";\n";
1037 else
1039 ss << " return DBL_MAX;\n}\n";
1040 return ;
1043 else
1045 ss << " x = ";
1046 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1048 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1049 assert(tmpCur1);
1050 if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
1052 if(tmpCur1->GetType() == formula::svSingleVectorRef)
1054 const formula::SingleVectorRefToken*tmpCurDVR1 =
1055 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
1056 ss << " if(gid0 < ";
1057 ss << tmpCurDVR1->GetArrayLength() << ")\n";
1058 ss << " {\n";
1059 ss << " fDF = ";
1060 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1061 ss << " if(isNan(fDF))\n";
1062 ss << " fDF = 0.0;\n";
1063 ss << " else\n";
1064 ss << " fDF = floor(fDF);\n";
1065 ss << " }\n";
1067 else if(tmpCur1->GetType() == formula::svDouble)
1069 ss << " fDF = floor(convert_double(";
1070 ss << tmpCur1->GetDouble() << "));\n";
1072 else
1074 ss << " return DBL_MAX;\n}\n";
1075 return ;
1078 else
1080 ss << " fDF = floor(";
1081 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
1084 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1085 assert(tmpCur2);
1086 if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
1088 if(tmpCur2->GetType() == formula::svSingleVectorRef)
1090 const formula::SingleVectorRefToken*tmpCurDVR2 =
1091 static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
1092 ss << " if(gid0 < ";
1093 ss << tmpCurDVR2->GetArrayLength() << ")\n";
1094 ss << " {\n";
1095 ss << " fFlag = ";
1096 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
1097 ss << " if(isNan(fFlag))\n";
1098 ss << " fFlag = 0.0;\n";
1099 ss << " else\n";
1100 ss << " fFlag = floor(fFlag);\n";
1101 ss << " }\n";
1104 else if(tmpCur2->GetType() == formula::svDouble)
1106 ss << " fFlag = floor(convert_double(";
1107 ss << tmpCur2->GetDouble() << "));\n";
1109 else
1111 ss << " return DBL_MAX;\n}\n";
1112 return ;
1115 else
1117 ss << " fFlag = floor(";
1118 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ");\n";
1120 ss << " if(fDF < 1.0 || x < 0.0 || (fFlag != 1.0 && fFlag != 2.0))\n";
1121 ss << " return DBL_MAX;\n";
1122 ss << " double R = GetTDist(x, fDF);\n";
1123 ss << " if (fFlag == 1.0)\n";
1124 ss << " return R;\n";
1125 ss << " else\n";
1126 ss << " return 2.0 * R;\n";
1127 ss << "}\n";
1129 void OpExponDist::GenSlidingWindowFunction(std::stringstream &ss,
1130 const std::string &sSymName, SubArguments &vSubArguments)
1132 ss << "\ndouble " << sSymName;
1133 ss << "_"<< BinFuncName() <<"(";
1134 for (unsigned i = 0; i < vSubArguments.size(); i++)
1136 if (i)
1137 ss << ",";
1138 vSubArguments[i]->GenSlidingWindowDecl(ss);
1140 ss << ") {\n";
1141 ss << " double tmp = 0,tmp0=0,tmp1=0,tmp2=0;\n";
1142 ss << " int gid0 = get_global_id(0);\n";
1143 ss << " double rx,rlambda,rkum;\n";
1144 size_t i = vSubArguments.size();
1145 ss <<"\n";
1146 for (i = 0; i < vSubArguments.size(); i++)
1148 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
1149 assert(pCur);
1150 if (pCur->GetType() == formula::svSingleVectorRef)
1152 const formula::SingleVectorRefToken* pSVR =
1153 static_cast< const formula::SingleVectorRefToken* >(pCur);
1154 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
1156 else if (pCur->GetType() == formula::svDouble)
1158 ss << "{\n";
1160 else
1163 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
1165 ss << " if (isNan(";
1166 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1167 ss << "))\n";
1168 ss << " tmp"<<i<<"= 0;\n";
1169 ss << " else\n";
1170 ss << " tmp"<<i<<"=\n";
1171 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1172 ss << ";\n}\n";
1174 else
1176 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
1177 ss <<";\n";
1180 ss << " rx = tmp0;\n";
1181 ss << " rlambda = tmp1;\n";
1182 ss << " rkum = tmp2;\n";
1183 ss <<" if(rlambda <= 0.0)\n";
1184 ss <<" {\n";
1185 ss <<" tmp = -DBL_MAX;\n";
1186 ss <<" }\n";
1187 ss <<" else if(rkum == 0)\n";
1188 ss <<" {\n";
1189 ss <<" if(rx >= 0)\n";
1190 ss <<" tmp = rlambda*exp(-rlambda*rx);\n";
1191 ss <<" else\n";
1192 ss <<" tmp = 0.0;\n";
1193 ss <<" }\n";
1194 ss <<" else\n";
1195 ss <<" {\n";
1196 ss <<" if(rx > 0)\n";
1197 ss <<" tmp = 1.0 - exp(-rlambda*rx);\n";
1198 ss <<" else\n";
1199 ss <<" tmp = 0.0;\n";
1200 ss <<" }\n";
1201 ss <<" return tmp;\n";
1202 ss <<"}";
1204 void OpFdist::BinInlineFun(std::set<std::string>& decls,
1205 std::set<std::string>& funs)
1207 decls.insert(GetFDistDecl);decls.insert(GetBetaDistDecl);
1208 decls.insert(GetBetaDecl);decls.insert(fMaxGammaArgumentDecl);
1209 decls.insert(lcl_GetBetaHelperContFracDecl);
1210 decls.insert(GetBetaDistPDFDecl);
1211 decls.insert(GetLogBetaDecl);decls.insert(lcl_getLanczosSumDecl);
1212 decls.insert(fMachEpsDecl);
1213 funs.insert(GetFDist);funs.insert(GetBetaDist);
1214 funs.insert(GetBeta);
1215 funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetBetaDistPDF);
1216 funs.insert(GetLogBeta);
1217 funs.insert(lcl_getLanczosSum);
1219 void OpFdist::GenSlidingWindowFunction(std::stringstream &ss,
1220 const std::string &sSymName, SubArguments &vSubArguments)
1222 ss << "\ndouble " << sSymName;
1223 ss << "_"<< BinFuncName() <<"(";
1224 for (unsigned i = 0; i < vSubArguments.size(); i++)
1226 if (i)
1227 ss << ",";
1228 vSubArguments[i]->GenSlidingWindowDecl(ss);
1230 ss << ") {\n";
1231 ss << " double tmp = 0,tmp0=0,tmp1=0,tmp2=0;\n";
1232 ss << " int gid0 = get_global_id(0);\n";
1233 ss << " double rF1,rF2,rX;\n";
1234 size_t i = vSubArguments.size();
1235 ss <<"\n";
1236 for (i = 0; i < vSubArguments.size(); i++)
1238 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
1239 assert(pCur);
1240 if (pCur->GetType() == formula::svSingleVectorRef)
1242 const formula::SingleVectorRefToken* pSVR =
1243 static_cast< const formula::SingleVectorRefToken* >(pCur);
1244 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
1246 else if (pCur->GetType() == formula::svDouble)
1248 ss << "{\n";
1250 else
1253 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
1255 ss << " if (isNan(";
1256 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1257 ss << "))\n";
1258 ss << " tmp"<<i<<"= 0;\n";
1259 ss << " else\n";
1260 ss << " tmp"<<i<<"=\n";
1261 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1262 ss << ";\n}\n";
1264 else
1266 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
1267 ss <<";\n";
1270 ss << " rX = tmp0;\n";
1271 ss << " rF1 = floor(tmp1);\n";
1272 ss << " rF2 = floor(tmp2);\n";
1273 ss <<" if (rX < 0.0 || rF1 < 1.0 || rF2 < 1.0 || rF1 >= 1.0E10 ||";
1274 ss <<"rF2 >= 1.0E10)\n";
1275 ss <<" {\n";
1276 ss <<" tmp = -DBL_MAX;\n";
1277 ss <<" }\n";
1278 ss <<" tmp = GetFDist(rX, rF1, rF2);\n";
1279 ss <<" return tmp;\n";
1280 ss <<"}";
1283 void OpStandard::GenSlidingWindowFunction(std::stringstream &ss,
1284 const std::string &sSymName, SubArguments &vSubArguments)
1286 ss << "\ndouble " << sSymName;
1287 ss << "_" << BinFuncName() << "(";
1288 for (unsigned i = 0; i < vSubArguments.size(); i++)
1290 if (i)
1291 ss << ",";
1292 vSubArguments[i]->GenSlidingWindowDecl(ss);
1294 ss << ") {\n";
1295 ss << " int gid0 = get_global_id(0);\n";
1296 ss << " double x = 0.0;\n";
1297 ss << " double mu = 0.0;\n";
1298 ss << " double sigma = 0.0;\n";
1299 if(vSubArguments.size() != 3)
1301 ss << " return DBL_MAX;\n}\n";
1302 return ;
1304 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1305 assert(tmpCur0);
1306 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1308 if(tmpCur0->GetType() == formula::svSingleVectorRef)
1310 const formula::SingleVectorRefToken*tmpCurSVR0 =
1311 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1312 ss << " if (gid0 < " << tmpCurSVR0->GetArrayLength() << ")\n";
1313 ss << " {\n";
1314 ss << " x = ";
1315 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1316 ss << " if (isNan(x))\n";
1317 ss << " x = 0.0;\n";
1318 ss << " }\n";
1320 else if(tmpCur0->GetType() == formula::svDouble)
1322 ss << " x = " << tmpCur0->GetDouble() << ";\n";
1324 else
1326 ss << " return DBL_MAX;\n}\n";
1327 return ;
1330 else
1332 ss << " x = ";
1333 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1336 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1337 assert(tmpCur1);
1338 if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
1340 if(tmpCur1->GetType() == formula::svSingleVectorRef)
1342 const formula::SingleVectorRefToken*tmpCurSVR1 =
1343 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
1344 ss << " if (gid0 < " << tmpCurSVR1->GetArrayLength() << ")\n";
1345 ss << " {\n";
1346 ss << " mu = ";
1347 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1348 ss << " if (isNan(mu))\n";
1349 ss << " mu = 0.0;\n";
1350 ss << " }\n";
1352 else if(tmpCur1->GetType() == formula::svDouble)
1354 ss << " mu = " << tmpCur1->GetDouble() << ";\n";
1356 else
1358 ss << " return DBL_MAX;\n}\n";
1359 return ;
1362 else
1364 ss << " mu = ";
1365 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1368 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1369 assert(tmpCur2);
1370 if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
1372 if(tmpCur2->GetType() == formula::svSingleVectorRef)
1374 const formula::SingleVectorRefToken*tmpCurSVR2 =
1375 static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
1376 ss << " if (gid0 < " << tmpCurSVR2->GetArrayLength() << ")\n";
1377 ss << " {\n";
1378 ss << " sigma = ";
1379 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
1380 ss << " if (isNan(sigma))\n";
1381 ss << " sigma = 0.0;\n";
1382 ss << " }\n";
1384 else if(tmpCur2->GetType() == formula::svDouble)
1386 ss << " sigma = " << tmpCur2->GetDouble() << ";\n";
1388 else
1390 ss << " return DBL_MAX;\n";
1391 ss << "}\n";
1392 return ;
1395 else
1397 ss << " sigma = ";
1398 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
1401 ss << " if(sigma <= 0.0)\n";
1402 ss << " return DBL_MAX;\n";
1403 ss << " else\n";
1404 ss << " return (x - mu)*pow(sigma,-1.0);\n";
1405 ss << "}";
1408 void OpWeibull::GenSlidingWindowFunction(std::stringstream &ss,
1409 const std::string &sSymName, SubArguments &vSubArguments)
1411 ss << "\ndouble " << sSymName;
1412 ss << "_" << BinFuncName() << "(";
1413 for (unsigned i = 0; i < vSubArguments.size(); i++)
1415 if (i)
1416 ss << ",";
1417 vSubArguments[i]->GenSlidingWindowDecl(ss);
1419 ss << ") {\n";
1420 ss << " int gid0 = get_global_id(0);\n";
1421 ss << " double x = 0.0;\n";
1422 ss << " double alpha = 0.0;\n";
1423 ss << " double beta = 0.0;\n";
1424 ss << " double kum = 0.0;\n";
1425 if(vSubArguments.size() != 4)
1427 ss << " return DBL_MAX;\n}\n";
1428 return ;
1430 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1431 assert(tmpCur0);
1432 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1434 if(tmpCur0->GetType() == formula::svSingleVectorRef)
1436 const formula::SingleVectorRefToken*tmpCurSVR0 =
1437 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1438 ss << " if (gid0 < " << tmpCurSVR0->GetArrayLength() << ")\n";
1439 ss << " {\n";
1440 ss << " x = ";
1441 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1442 ss << " if (isNan(x))\n";
1443 ss << " x = 0.0;\n";
1444 ss << " }\n";
1446 else if(tmpCur0->GetType() == formula::svDouble)
1448 ss << " x = " << tmpCur0->GetDouble() << ";\n";
1450 else
1452 ss << " return DBL_MAX;\n}\n";
1453 return ;
1456 else
1458 ss << " x = ";
1459 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1462 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1463 assert(tmpCur1);
1464 if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
1466 if(tmpCur1->GetType() == formula::svSingleVectorRef)
1468 const formula::SingleVectorRefToken*tmpCurSVR1 =
1469 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
1470 ss << " if (gid0 < " << tmpCurSVR1->GetArrayLength() << ")\n";
1471 ss << " {\n";
1472 ss << " alpha = ";
1473 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1474 ss << " if (isNan(alpha))\n";
1475 ss << " alpha = 0.0;\n";
1476 ss << " }\n";
1478 else if(tmpCur1->GetType() == formula::svDouble)
1480 ss << " alpha = " << tmpCur1->GetDouble() << ";\n";
1482 else
1484 ss << " return DBL_MAX;\n}\n";
1485 return ;
1488 else
1490 ss << " alpha = ";
1491 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1494 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1495 assert(tmpCur2);
1496 if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
1498 if(tmpCur2->GetType() == formula::svSingleVectorRef)
1500 const formula::SingleVectorRefToken*tmpCurSVR2 =
1501 static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
1502 ss << " if (gid0 < " << tmpCurSVR2->GetArrayLength() << ")\n";
1503 ss << " {\n";
1504 ss << " beta = ";
1505 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
1506 ss << " if (isNan(beta))\n";
1507 ss << " beta = 0.0;\n";
1508 ss << " }\n";
1510 else if(tmpCur2->GetType() == formula::svDouble)
1512 ss << " beta = " << tmpCur2->GetDouble() << ";\n";
1514 else
1516 ss << " return DBL_MAX;\n}\n";
1517 return ;
1520 else
1522 ss << " beta = ";
1523 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
1526 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
1527 assert(tmpCur3);
1528 if(ocPush == vSubArguments[3]->GetFormulaToken()->GetOpCode())
1530 if(tmpCur3->GetType() == formula::svSingleVectorRef)
1532 const formula::SingleVectorRefToken*tmpCurSVR3 =
1533 static_cast<const formula::SingleVectorRefToken *>(tmpCur3);
1534 ss << " if (gid0 < " << tmpCurSVR3->GetArrayLength() << ")\n";
1535 ss << " {\n";
1536 ss << " kum = ";
1537 ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ";\n";
1538 ss << " if (isNan(kum))\n";
1539 ss << " kum = 0.0;\n";
1540 ss << " }\n";
1542 else if(tmpCur3->GetType() == formula::svDouble)
1544 ss << " kum = " << tmpCur3->GetDouble() << ";\n";
1546 else
1548 ss << " return DBL_MAX;\n}\n";
1549 return ;
1552 else
1554 ss << " kum = ";
1555 ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ";\n";
1558 ss << " if(alpha <= 0.0 || beta <=0.0 || kum < 0.0)\n";
1559 ss << " return DBL_MAX;\n";
1560 ss << " else if(kum == 0.0)\n";
1561 ss << " {\n";
1562 ss << " return alpha*pow(pow(beta,alpha),-1.0)*pow(x,alpha-1.0)";
1563 ss << "*exp(-pow(x*pow(beta,-1.0),alpha));\n";
1564 ss << " }\n";
1565 ss << " else\n";
1566 ss << " return 1.0-exp(-pow(x*pow(beta,-1.0),alpha));\n";
1567 ss << "}\n";
1570 void OpSkew::GenSlidingWindowFunction(std::stringstream &ss,
1571 const std::string &sSymName, SubArguments &vSubArguments)
1573 ss << "\ndouble " << sSymName;
1574 ss << "_" << BinFuncName() << "(";
1575 for (unsigned i = 0; i < vSubArguments.size(); i++)
1577 if (i)
1578 ss << ",";
1579 vSubArguments[i]->GenSlidingWindowDecl(ss);
1581 ss << "){\n";
1582 ss << " int gid0 = get_global_id(0);\n";
1583 ss << " double fSum = 0.0;\n";
1584 ss << " double fMean = 0.0;\n";
1585 ss << " double vSum = 0.0;\n";
1586 ss << " double fCount = 0.0;\n";
1587 ss << " double arg = 0.0;\n";
1588 unsigned i = vSubArguments.size();
1589 while (i--)
1591 FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
1592 assert(pCur);
1593 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1595 if (pCur->GetType() == formula::svDoubleVectorRef)
1597 const formula::DoubleVectorRefToken* pDVR =
1598 static_cast<const formula::DoubleVectorRefToken *>(pCur);
1599 size_t nCurWindowSize = pDVR->GetRefRowSize();
1600 ss << " for (int i = ";
1601 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1603 ss << "gid0; i < " << pDVR->GetArrayLength();
1604 ss << " && i < " << nCurWindowSize << "; i++)\n";
1605 ss << " {\n";
1607 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1609 ss << "0; i < " << pDVR->GetArrayLength();
1610 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
1611 ss << " {\n";
1613 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1615 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
1616 ss << " && i < " << nCurWindowSize << "; i++)\n";
1617 ss << " {\n";
1619 else
1621 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
1622 ss << " {\n";
1625 ss << " arg = ";
1626 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1627 ss << " if (isNan(arg))\n";
1628 ss << " continue;\n";
1629 ss << " fSum += arg;\n";
1630 ss << " fCount += 1.0;\n";
1631 ss << " }\n";
1633 else if (pCur->GetType() == formula::svSingleVectorRef)
1635 const formula::SingleVectorRefToken* pSVR =
1636 static_cast< const formula::SingleVectorRefToken* >(pCur);
1637 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
1638 ss << " {\n";
1639 ss << " arg = ";
1640 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1641 ss << " if (!isNan(arg))\n";
1642 ss << " {\n";
1643 ss << " fSum += arg;\n";
1644 ss << " fCount += 1.0;\n";
1645 ss << " }\n";
1646 ss << " }\n";
1648 else
1650 ss << " arg = " << pCur->GetDouble() << ";\n";
1651 ss << " fSum += arg;\n";
1652 ss << " fCount += 1.0;\n";
1655 else
1657 ss << " arg = ";
1658 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1659 ss << " fSum += arg;\n";
1660 ss << " fCount += 1.0;\n";
1663 if(i == 0)
1665 ss << " if(fCount <= 2.0)\n";
1666 ss << " return DBL_MAX;\n";
1667 ss << " else\n";
1668 ss << " fMean = fSum * pow(fCount,-1.0);\n";
1671 i = vSubArguments.size();
1672 while (i--)
1674 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
1675 assert(pCur);
1676 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1678 if (pCur->GetType() == formula::svDoubleVectorRef)
1680 const formula::DoubleVectorRefToken* pDVR =
1681 static_cast<const formula::DoubleVectorRefToken *>(pCur);
1682 size_t nCurWindowSize = pDVR->GetRefRowSize();
1683 ss << " for (int i = ";
1684 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1686 ss << "gid0; i < " << pDVR->GetArrayLength();
1687 ss << " && i < " << nCurWindowSize << "; i++)\n";
1688 ss << " {\n";
1690 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1692 ss << "0; i < " << pDVR->GetArrayLength();
1693 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
1694 ss << " {\n";
1696 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1698 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
1699 ss << " && i < " << nCurWindowSize << "; i++)\n";
1700 ss << " {\n";
1702 else
1704 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
1705 ss << " {\n";
1708 ss << " arg = ";
1709 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1710 ss << " if (isNan(arg))\n";
1711 ss << " continue;\n";
1712 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1713 ss << " }\n";
1715 else if (pCur->GetType() == formula::svSingleVectorRef)
1717 const formula::SingleVectorRefToken* pSVR =
1718 static_cast< const formula::SingleVectorRefToken* >(pCur);
1719 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
1720 ss << " {\n";
1721 ss << " arg = ";
1722 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1723 ss << " if (!isNan(arg))\n";
1724 ss << " {\n";
1725 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1726 ss << " }\n";
1727 ss << " }\n";
1729 else
1731 ss << " arg = " << pCur->GetDouble() << ";\n";
1732 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1735 else
1737 ss << " arg = ";
1738 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1739 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1742 ss << " double fStdDev = sqrt(vSum * pow(fCount - 1.0,-1.0));\n";
1743 ss << " double dx = 0.0;\n";
1744 ss << " double xcube = 0.0;\n";
1745 ss << " if(fStdDev == 0.0)\n";
1746 ss << " return DBL_MAX;\n";
1747 i = vSubArguments.size();
1748 while (i--)
1750 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
1751 assert(pCur);
1752 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1754 if (pCur->GetType() == formula::svDoubleVectorRef)
1756 const formula::DoubleVectorRefToken* pDVR =
1757 static_cast<const formula::DoubleVectorRefToken *>(pCur);
1758 size_t nCurWindowSize = pDVR->GetRefRowSize();
1759 ss << " for (int i = ";
1760 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1762 ss << "gid0; i < " << pDVR->GetArrayLength();
1763 ss << " && i < " << nCurWindowSize << "; i++)\n";
1764 ss << " {\n";
1766 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1768 ss << "0; i < " << pDVR->GetArrayLength();
1769 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
1770 ss << " {\n";
1772 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1774 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
1775 ss << " && i < " << nCurWindowSize << "; i++)\n";
1776 ss << " {\n";
1778 else
1780 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
1781 ss << " {\n";
1784 ss << " arg = ";
1785 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1786 ss << " if (isNan(arg))\n";
1787 ss << " continue;\n";
1788 ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
1789 ss << " xcube = xcube + dx * dx * dx;\n";
1790 ss << " }\n";
1792 else if (pCur->GetType() == formula::svSingleVectorRef)
1794 const formula::SingleVectorRefToken* pSVR =
1795 static_cast< const formula::SingleVectorRefToken* >(pCur);
1796 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
1797 ss << " {\n";
1798 ss << " arg = ";
1799 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1800 ss << " if (!isNan(arg))\n";
1801 ss << " {\n";
1802 ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
1803 ss << " xcube = xcube + dx * dx * dx;\n";
1804 ss << " }\n";
1805 ss << " }\n";
1807 else
1809 ss << " arg = " << pCur->GetDouble() << ";\n";
1810 ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
1811 ss << " xcube = xcube + dx * dx * dx;\n";
1814 else
1816 ss << " arg = ";
1817 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1818 ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
1819 ss << " xcube = xcube + dx * dx * dx;\n";
1822 ss << " return ((xcube * fCount) * pow(fCount - 1.0,-1.0))";
1823 ss << " * pow(fCount - 2.0,-1.0);\n";
1824 ss << "}\n";
1827 void OpSkewp::GenSlidingWindowFunction(std::stringstream &ss,
1828 const std::string &sSymName, SubArguments &vSubArguments)
1830 ss << "\ndouble " << sSymName;
1831 ss << "_" << BinFuncName() << "(";
1832 for (unsigned i = 0; i < vSubArguments.size(); i++)
1834 if (i)
1835 ss << ",";
1836 vSubArguments[i]->GenSlidingWindowDecl(ss);
1838 ss << "){\n";
1839 ss << " int gid0 = get_global_id(0);\n";
1840 ss << " double fSum = 0.0;\n";
1841 ss << " double fMean = 0.0;\n";
1842 ss << " double vSum = 0.0;\n";
1843 ss << " double fCount = 0.0;\n";
1844 ss << " double arg = 0.0;\n";
1845 unsigned i = vSubArguments.size();
1846 while (i--)
1848 FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
1849 assert(pCur);
1850 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1852 if (pCur->GetType() == formula::svDoubleVectorRef)
1854 const formula::DoubleVectorRefToken* pDVR =
1855 static_cast<const formula::DoubleVectorRefToken *>(pCur);
1856 size_t nCurWindowSize = pDVR->GetRefRowSize();
1857 ss << " for (int i = ";
1858 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1860 ss << "gid0; i < " << pDVR->GetArrayLength();
1861 ss << " && i < " << nCurWindowSize << "; i++)\n";
1862 ss << " {\n";
1864 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1866 ss << "0; i < " << pDVR->GetArrayLength();
1867 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
1868 ss << " {\n";
1870 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1872 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
1873 ss << " && i < " << nCurWindowSize << "; i++)\n";
1874 ss << " {\n";
1876 else
1878 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
1879 ss << " {\n";
1882 ss << " arg = ";
1883 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1884 ss << " if (isNan(arg))\n";
1885 ss << " continue;\n";
1886 ss << " fSum += arg;\n";
1887 ss << " fCount += 1.0;\n";
1888 ss << " }\n";
1890 else if (pCur->GetType() == formula::svSingleVectorRef)
1892 const formula::SingleVectorRefToken* pSVR =
1893 static_cast< const formula::SingleVectorRefToken* >(pCur);
1894 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
1895 ss << " {\n";
1896 ss << " arg = ";
1897 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1898 ss << " if (!isNan(arg))\n";
1899 ss << " {\n";
1900 ss << " fSum += arg;\n";
1901 ss << " fCount += 1.0;\n";
1902 ss << " }\n";
1903 ss << " }\n";
1905 else
1907 ss << " arg = " << pCur->GetDouble() << ";\n";
1908 ss << " fSum += arg;\n";
1909 ss << " fCount += 1.0;\n";
1912 else
1914 ss << " arg = ";
1915 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1916 ss << " fSum += arg;\n";
1917 ss << " fCount += 1.0;\n";
1920 if(i == 0)
1922 ss << " if(fCount <= 2.0)\n";
1923 ss << " return DBL_MAX;\n";
1924 ss << " else\n";
1925 ss << " fMean = fSum * pow(fCount,-1.0);\n";
1928 i = vSubArguments.size();
1929 while (i--)
1931 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
1932 assert(pCur);
1933 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1935 if (pCur->GetType() == formula::svDoubleVectorRef)
1937 const formula::DoubleVectorRefToken* pDVR =
1938 static_cast<const formula::DoubleVectorRefToken *>(pCur);
1939 size_t nCurWindowSize = pDVR->GetRefRowSize();
1940 ss << " for (int i = ";
1941 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1943 ss << "gid0; i < " << pDVR->GetArrayLength();
1944 ss << " && i < " << nCurWindowSize << "; i++)\n";
1945 ss << " {\n";
1947 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1949 ss << "0; i < " << pDVR->GetArrayLength();
1950 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
1951 ss << " {\n";
1953 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1955 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
1956 ss << " && i < " << nCurWindowSize << "; i++)\n";
1957 ss << " {\n";
1959 else
1961 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
1962 ss << " {\n";
1965 ss << " arg = ";
1966 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1967 ss << " if (isNan(arg))\n";
1968 ss << " continue;\n";
1969 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1970 ss << " }\n";
1972 else if (pCur->GetType() == formula::svSingleVectorRef)
1974 const formula::SingleVectorRefToken* pSVR =
1975 static_cast< const formula::SingleVectorRefToken* >(pCur);
1976 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
1977 ss << " {\n";
1978 ss << " arg = ";
1979 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1980 ss << " if (!isNan(arg))\n";
1981 ss << " {\n";
1982 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1983 ss << " }\n";
1984 ss << " }\n";
1986 else
1988 ss << " arg = " << pCur->GetDouble() << ";\n";
1989 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1992 else
1994 ss << " arg = ";
1995 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1996 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1999 ss << " double fStdDev = sqrt(vSum * pow(fCount,-1.0));\n";
2000 ss << " double dx = 0.0;\n";
2001 ss << " double xcube = 0.0;\n";
2002 ss << " if(fStdDev == 0.0)\n";
2003 ss << " return DBL_MAX;\n";
2004 i = vSubArguments.size();
2005 while (i--)
2007 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2008 assert(pCur);
2009 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2011 if (pCur->GetType() == formula::svDoubleVectorRef)
2013 const formula::DoubleVectorRefToken* pDVR =
2014 static_cast<const formula::DoubleVectorRefToken *>(pCur);
2015 size_t nCurWindowSize = pDVR->GetRefRowSize();
2016 ss << " for (int i = ";
2017 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2019 ss << "gid0; i < " << pDVR->GetArrayLength();
2020 ss << " && i < " << nCurWindowSize << "; i++)\n";
2021 ss << " {\n";
2023 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2025 ss << "0; i < " << pDVR->GetArrayLength();
2026 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2027 ss << " {\n";
2029 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2031 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2032 ss << " && i < " << nCurWindowSize << "; i++)\n";
2033 ss << " {\n";
2035 else
2037 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
2038 ss << " {\n";
2041 ss << " arg = ";
2042 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2043 ss << " if (isNan(arg))\n";
2044 ss << " continue;\n";
2045 ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
2046 ss << " xcube = xcube + dx * dx * dx;\n";
2047 ss << " }\n";
2049 else if (pCur->GetType() == formula::svSingleVectorRef)
2051 const formula::SingleVectorRefToken* pSVR =
2052 static_cast< const formula::SingleVectorRefToken* >(pCur);
2053 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
2054 ss << " {\n";
2055 ss << " arg = ";
2056 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2057 ss << " if (!isNan(arg))\n";
2058 ss << " {\n";
2059 ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
2060 ss << " xcube = xcube + dx * dx * dx;\n";
2061 ss << " }\n";
2062 ss << " }\n";
2064 else
2066 ss << " arg = " << pCur->GetDouble() << ";\n";
2067 ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
2068 ss << " xcube = xcube + dx * dx * dx;\n";
2071 else
2073 ss << " arg = ";
2074 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2075 ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
2076 ss << " xcube = xcube + dx * dx * dx;\n";
2079 ss << " return xcube * pow(fCount,-1.0);\n";
2080 ss << "}\n";
2083 void OpTInv::BinInlineFun(std::set<std::string>& decls,
2084 std::set<std::string>& funs)
2086 decls.insert(fMachEpsDecl);
2087 funs.insert("");
2088 decls.insert(fMaxGammaArgumentDecl);
2089 funs.insert("");
2090 decls.insert(lcl_getLanczosSumDecl);
2091 funs.insert(lcl_getLanczosSum);
2092 decls.insert(GetBetaDecl);
2093 funs.insert(GetBeta);
2094 decls.insert(GetLogBetaDecl);
2095 funs.insert(GetLogBeta);
2096 decls.insert(GetBetaDistPDFDecl);
2097 funs.insert(GetBetaDistPDF);
2098 decls.insert(lcl_GetBetaHelperContFracDecl);
2099 funs.insert(lcl_GetBetaHelperContFrac);
2100 decls.insert(GetBetaDistDecl);
2101 funs.insert(GetBetaDist);
2102 decls.insert(GetTDistDecl);
2103 funs.insert(GetTDist);
2104 decls.insert(GetValueDecl);
2105 funs.insert(GetValue);
2106 decls.insert(lcl_HasChangeOfSignDecl);
2107 funs.insert(lcl_HasChangeOfSign);
2108 decls.insert(lcl_IterateInverseDecl);
2109 funs.insert(lcl_IterateInverse);
2112 void OpTInv::GenSlidingWindowFunction(std::stringstream &ss,
2113 const std::string &sSymName, SubArguments &vSubArguments)
2115 ss << "\ndouble " << sSymName;
2116 ss << "_" << BinFuncName() << "(";
2117 for (unsigned i = 0; i < vSubArguments.size(); i++)
2119 if (i)
2120 ss << ",";
2121 vSubArguments[i]->GenSlidingWindowDecl(ss);
2123 ss << ") {\n";
2124 ss << " int gid0 = get_global_id(0);\n";
2125 ss << " double x = 0.0;\n";
2126 ss << " double fDF = 0.0;\n";
2127 if(vSubArguments.size() != 2)
2129 ss << " return DBL_MAX;\n}\n";
2130 return ;
2132 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
2133 assert(tmpCur0);
2134 if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
2136 if(tmpCur0->GetType() == formula::svSingleVectorRef)
2138 const formula::SingleVectorRefToken*tmpCurDVR0 =
2139 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
2140 ss << " if(gid0 < ";
2141 ss << tmpCurDVR0->GetArrayLength() << ")\n";
2142 ss << " {\n";
2143 ss << " x = ";
2144 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2145 ss << " if(isNan(x))\n";
2146 ss << " x = 0.0;\n";
2147 ss << " }\n";
2149 else if(tmpCur0->GetType() == formula::svDouble)
2151 ss << " x = " << tmpCur0->GetDouble() << ";\n";
2153 else
2155 ss << " return DBL_MAX;\n}\n";
2156 return ;
2159 else
2161 ss << " x = ";
2162 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2164 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
2165 assert(tmpCur1);
2166 if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
2168 if(tmpCur1->GetType() == formula::svSingleVectorRef)
2170 const formula::SingleVectorRefToken*tmpCurDVR1 =
2171 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
2172 ss << " if(gid0 < ";
2173 ss << tmpCurDVR1->GetArrayLength() << ")\n";
2174 ss << " {\n";
2175 ss << " fDF = ";
2176 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2177 ss << " if(isNan(fDF))\n";
2178 ss << " fDF = 0.0;\n";
2179 ss << " else\n";
2180 ss << " fDF = floor(fDF);\n";
2181 ss << " }\n";
2183 else if(tmpCur1->GetType() == formula::svDouble)
2185 ss << " fDF = floor(convert_double(";
2186 ss << tmpCur1->GetDouble() << "));\n";
2188 else
2190 ss << " return DBL_MAX;\n}\n";
2191 return ;
2194 else
2196 ss << " fDF = floor(";
2197 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
2199 ss << " if (x > 1.0||fDF < 1.0 || fDF > 1.0E10 || x <= 0.0)\n";
2200 ss << " return DBL_MAX;\n";
2201 ss << " bool bConvError;\n";
2202 ss << " double fVal = lcl_IterateInverse(\n";
2203 ss << " fDF*0.5, fDF, &bConvError,x,fDF );\n";
2204 ss << " if (bConvError)\n";
2205 ss << " return DBL_MAX;\n";
2206 ss << " return fVal;\n";
2207 ss << "}\n";
2210 void OpStDev::GenSlidingWindowFunction(std::stringstream &ss,
2211 const std::string &sSymName, SubArguments &vSubArguments)
2213 ss << "\ndouble " << sSymName;
2214 ss << "_" << BinFuncName() << "(";
2215 for (unsigned i = 0; i < vSubArguments.size(); i++)
2217 if (i)
2218 ss << ",";
2219 vSubArguments[i]->GenSlidingWindowDecl(ss);
2221 ss << "){\n";
2222 ss << " int gid0 = get_global_id(0);\n";
2223 ss << " double fSum = 0.0;\n";
2224 ss << " double vSum = 0.0;\n";
2225 ss << " double fMean = 0.0;\n";
2226 ss << " double fCount = 0.0;\n";
2227 ss << " double arg = 0.0;\n";
2228 unsigned i = vSubArguments.size();
2229 while (i--)
2231 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2232 assert(pCur);
2233 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2235 if (pCur->GetType() == formula::svDoubleVectorRef)
2237 const formula::DoubleVectorRefToken* pDVR =
2238 static_cast<const formula::DoubleVectorRefToken *>(pCur);
2239 size_t nCurWindowSize = pDVR->GetRefRowSize();
2240 ss << " for (int i = ";
2241 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2243 ss << "gid0; i < " << pDVR->GetArrayLength();
2244 ss << " && i < " << nCurWindowSize << "; i++)\n";
2245 ss << " {\n";
2247 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2249 ss << "0; i < " << pDVR->GetArrayLength();
2250 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2251 ss << " {\n";
2253 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2255 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2256 ss << " && i < " << nCurWindowSize << "; i++)\n";
2257 ss << " {\n";
2259 else
2261 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
2262 ss << " {\n";
2265 ss << " arg = ";
2266 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2267 ss << " if (isNan(arg))\n";
2268 ss << " continue;\n";
2269 ss << " fSum += arg;\n";
2270 ss << " fCount += 1.0;\n";
2271 ss << " }\n";
2273 else if (pCur->GetType() == formula::svSingleVectorRef)
2275 const formula::SingleVectorRefToken* pSVR =
2276 static_cast< const formula::SingleVectorRefToken* >(pCur);
2277 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
2278 ss << " {\n";
2279 ss << " arg = ";
2280 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2281 ss << " if (!isNan(arg))\n";
2282 ss << " {\n";
2283 ss << " fSum += arg;\n";
2284 ss << " fCount += 1.0;\n";
2285 ss << " }\n";
2286 ss << " }\n";
2288 else
2290 ss << " arg = " << pCur->GetDouble() << ";\n";
2291 ss << " fSum += arg;\n";
2292 ss << " fCount += 1.0;\n";
2295 else
2297 ss << " arg = ";
2298 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2299 ss << " fSum += arg;\n";
2300 ss << " fCount += 1.0;\n";
2302 if (i == 0)
2304 ss << " fMean = fSum * pow(fCount,-1.0);\n";
2307 i = vSubArguments.size();
2308 while (i--)
2310 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2311 assert(pCur);
2312 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2314 if (pCur->GetType() == formula::svDoubleVectorRef)
2316 const formula::DoubleVectorRefToken* pDVR =
2317 static_cast<const formula::DoubleVectorRefToken *>(pCur);
2318 size_t nCurWindowSize = pDVR->GetRefRowSize();
2319 ss << " for (int i = ";
2320 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2322 ss << "gid0; i < " << pDVR->GetArrayLength();
2323 ss << " && i < " << nCurWindowSize << "; i++)\n";
2324 ss << " {\n";
2326 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2328 ss << "0; i < " << pDVR->GetArrayLength();
2329 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2330 ss << " {\n";
2332 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2334 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2335 ss << " && i < " << nCurWindowSize << "; i++)\n";
2336 ss << " {\n";
2338 else
2340 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
2341 ss << " {\n";
2343 ss << " arg = ";
2344 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2345 ss << " if (isNan(arg))\n";
2346 ss << " continue;\n";
2347 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2348 ss << " }\n";
2350 else if (pCur->GetType() == formula::svSingleVectorRef)
2352 const formula::SingleVectorRefToken* pSVR =
2353 static_cast< const formula::SingleVectorRefToken* >(pCur);
2354 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
2355 ss << " {\n";
2356 ss << " arg = ";
2357 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2358 ss << " if (!isNan(arg))\n";
2359 ss << " {\n";
2360 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2361 ss << " }\n";
2362 ss << " }\n";
2364 else
2366 ss << " arg = " << pCur->GetDouble() << ";\n";
2367 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2370 else
2372 ss << " arg = ";
2373 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2374 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2377 ss << " if (fCount <= 1.0)\n";
2378 ss << " return DBL_MAX;\n";
2379 ss << " else\n";
2380 ss << " return sqrt(vSum * pow(fCount - 1.0,-1.0));\n";
2381 ss << "}\n";
2384 void OpStDevP::GenSlidingWindowFunction(std::stringstream &ss,
2385 const std::string &sSymName, SubArguments &vSubArguments)
2387 ss << "\ndouble " << sSymName;
2388 ss << "_" << BinFuncName() << "(";
2389 for (unsigned i = 0; i < vSubArguments.size(); i++)
2391 if (i)
2392 ss << ",";
2393 vSubArguments[i]->GenSlidingWindowDecl(ss);
2395 ss << "){\n";
2396 ss << " int gid0 = get_global_id(0);\n";
2397 ss << " double fSum = 0.0;\n";
2398 ss << " double fMean = 0.0;\n";
2399 ss << " double vSum = 0.0;\n";
2400 ss << " double fCount = 0.0;\n";
2401 ss << " double arg = 0.0;\n";
2402 unsigned i = vSubArguments.size();
2403 while (i--)
2405 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2406 assert(pCur);
2407 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2409 if (pCur->GetType() == formula::svDoubleVectorRef)
2411 const formula::DoubleVectorRefToken* pDVR =
2412 static_cast<const formula::DoubleVectorRefToken *>(pCur);
2413 size_t nCurWindowSize = pDVR->GetRefRowSize();
2414 ss << " for (int i = ";
2415 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2417 ss << "gid0; i < " << pDVR->GetArrayLength();
2418 ss << " && i < " << nCurWindowSize << "; i++)\n";
2419 ss << " {\n";
2421 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2423 ss << "0; i < " << pDVR->GetArrayLength();
2424 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2425 ss << " {\n";
2427 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2429 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2430 ss << " && i < " << nCurWindowSize << "; i++)\n";
2431 ss << " {\n";
2433 else
2435 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
2436 ss << " {\n";
2439 ss << " arg = ";
2440 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2441 ss << " if (isNan(arg))\n";
2442 ss << " continue;\n";
2443 ss << " fSum += arg;\n";
2444 ss << " fCount += 1.0;\n";
2445 ss << " }\n";
2447 else if (pCur->GetType() == formula::svSingleVectorRef)
2449 const formula::SingleVectorRefToken* pSVR =
2450 static_cast< const formula::SingleVectorRefToken* >(pCur);
2451 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
2452 ss << " {\n";
2453 ss << " arg = ";
2454 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2455 ss << " if (!isNan(arg))\n";
2456 ss << " {\n";
2457 ss << " fSum += arg;\n";
2458 ss << " fCount += 1.0;\n";
2459 ss << " }\n";
2460 ss << " }\n";
2462 else
2464 ss << " arg = " << pCur->GetDouble() << ";\n";
2465 ss << " fSum += arg;\n";
2466 ss << " fCount += 1.0;\n";
2469 else
2471 ss << " arg = ";
2472 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2473 ss << " fSum += arg;\n";
2474 ss << " fCount += 1.0;\n";
2477 if (i == 0)
2479 ss << " fMean = fSum * pow(fCount,-1.0);\n";
2482 i = vSubArguments.size();
2483 while (i--)
2485 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2486 assert(pCur);
2487 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2489 if (pCur->GetType() == formula::svDoubleVectorRef)
2491 const formula::DoubleVectorRefToken* pDVR =
2492 static_cast<const formula::DoubleVectorRefToken *>(pCur);
2493 size_t nCurWindowSize = pDVR->GetRefRowSize();
2494 ss << " for (int i = ";
2495 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2497 ss << "gid0; i < " << pDVR->GetArrayLength();
2498 ss << " && i < " << nCurWindowSize << "; i++)\n";
2499 ss << " {\n";
2501 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2503 ss << "0; i < " << pDVR->GetArrayLength();
2504 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2505 ss << " {\n";
2507 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2509 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2510 ss << " && i < " << nCurWindowSize << "; i++)\n";
2511 ss << " {\n";
2513 else
2515 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
2516 ss << " {\n";
2519 ss << " arg = ";
2520 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2521 ss << " if (isNan(arg))\n";
2522 ss << " continue;\n";
2523 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2524 ss << " }\n";
2526 else if (pCur->GetType() == formula::svSingleVectorRef)
2528 const formula::SingleVectorRefToken* pSVR =
2529 static_cast< const formula::SingleVectorRefToken* >(pCur);
2530 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
2531 ss << " {\n";
2532 ss << " arg = ";
2533 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2534 ss << " if (!isNan(arg))\n";
2535 ss << " {\n";
2536 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2537 ss << " }\n";
2538 ss << " }\n";
2540 else
2542 ss << " arg = " << pCur->GetDouble() << ";\n";
2543 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2546 else
2548 ss << " arg = ";
2549 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2550 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2553 ss << " if (fCount == 0.0)\n";
2554 ss << " return DBL_MAX;\n";
2555 ss << " else\n";
2556 ss << " return sqrt(vSum * pow(fCount,-1.0));\n";
2557 ss << "}\n";
2560 void OpSlope::GenSlidingWindowFunction(std::stringstream &ss,
2561 const std::string &sSymName, SubArguments &vSubArguments)
2563 ss << "\ndouble " << sSymName;
2564 ss << "_" << BinFuncName() << "(";
2565 for (unsigned i = 0; i < vSubArguments.size(); i++)
2567 if (i)
2568 ss << ",";
2569 vSubArguments[i]->GenSlidingWindowDecl(ss);
2571 ss << "){\n";
2572 ss << " int gid0 = get_global_id(0);\n";
2573 ss << " double fSumX = 0.0;\n";
2574 ss << " double fSumY = 0.0;\n";
2575 ss << " double fMeanX = 0.0;\n";
2576 ss << " double fMeanY = 0.0;\n";
2577 ss << " double fSumDeltaXDeltaY = 0.0;\n";
2578 ss << " double fSumSqrDeltaX = 0.0;\n";
2579 ss << " double fCount = 0.0;\n";
2580 ss << " double argX = 0.0;\n";
2581 ss << " double argY = 0.0;\n";
2582 if(vSubArguments.size() != 2)
2584 ss << " return NAN;\n";
2585 ss << "}\n";
2586 return ;
2588 FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
2589 FormulaToken *pCur1 = vSubArguments[0]->GetFormulaToken();
2590 assert(pCur);
2591 assert(pCur1);
2592 if (pCur->GetType() == formula::svDoubleVectorRef&&
2593 pCur1->GetType() == formula::svDoubleVectorRef)
2595 const formula::DoubleVectorRefToken* pDVR =
2596 static_cast<const formula::DoubleVectorRefToken *>(pCur);
2597 const formula::DoubleVectorRefToken* pDVR1 =
2598 static_cast<const formula::DoubleVectorRefToken *>(pCur1);
2600 size_t nCurWindowSize = pDVR->GetRefRowSize();
2601 size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
2602 size_t arrayLength = pDVR->GetArrayLength()<
2603 pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
2604 pDVR1->GetArrayLength();
2605 if(nCurWindowSize != nCurWindowSize1)
2607 ss << " return NAN;\n";
2608 ss << "}\n";
2609 return ;
2611 ss << " for (int i = ";
2612 if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2613 &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2615 ss << "gid0; i < " << arrayLength;
2616 ss << " && i < " << nCurWindowSize << "; i++)\n";
2617 ss << " {\n";
2619 else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2620 &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2622 ss << "0; i < " << arrayLength ;
2623 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2624 ss << " {\n";
2626 else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2627 &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2629 ss << "0; i + gid0 < " << arrayLength;
2630 ss << " && i < " << nCurWindowSize << "; i++)\n";
2631 ss << " {\n";
2633 else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
2634 &&(pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2636 ss << "0; i < " << arrayLength << "; i++)\n";
2637 ss << " {\n";
2639 else
2641 ss << "0; i < " << nCurWindowSize << "; i++)\n";
2642 ss << " {\n";
2643 ss << " break;\n";
2644 ss << " }";
2645 ss << " return NAN;\n";
2646 ss << "}\n";
2647 return ;
2650 ss << " argX = ";
2651 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2652 ss << " argY = ";
2653 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2654 ss << " if (isNan(argX) || isNan(argY))\n";
2655 ss << " continue;\n";
2656 ss << " fSumX += argX;\n";
2657 ss << " fSumY += argY;\n";
2658 ss << " fCount += 1.0;\n";
2659 ss << " }\n";
2661 ss << " if (fCount < 1.0)\n";
2662 ss << " return CreateDoubleError(errNoValue);\n";
2663 ss << " else\n";
2664 ss << " {\n";
2665 ss << " fMeanX = fSumX * pow(fCount,-1.0);\n";
2666 ss << " fMeanY = fSumY * pow(fCount,-1.0);\n";
2668 ss << " for (int i = ";
2669 if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2670 &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2672 ss << "gid0; i < " << arrayLength;
2673 ss << " && i < " << nCurWindowSize << "; i++)\n";
2674 ss << " {\n";
2676 else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2677 &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2679 ss << "0; i < " << arrayLength ;
2680 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2681 ss << " {\n";
2683 else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2684 &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2686 ss << "0; i + gid0 < " << arrayLength;
2687 ss << " && i < " << nCurWindowSize << "; i++)\n";
2688 ss << " {\n";
2690 else
2692 ss << "0; i < " << arrayLength << "; i++)\n";
2693 ss << " {\n";
2696 ss << " argX = ";
2697 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2698 ss << " argY = ";
2699 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2700 ss << " if (isNan(argX) || isNan(argY))\n";
2701 ss << " continue;\n";
2702 ss << " fSumDeltaXDeltaY += (argX-fMeanX)*(argY-fMeanY);\n";
2703 ss << " fSumSqrDeltaX += (argX-fMeanX) * (argX-fMeanX);\n";
2704 ss << " }\n";
2705 ss << " if(fSumSqrDeltaX == 0.0)\n";
2706 ss << " return CreateDoubleError(errDivisionByZero);\n";
2707 ss << " else\n";
2708 ss << " {\n";
2709 ss << " return fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0);\n";
2710 ss << " }\n";
2711 ss << " }\n";
2712 ss << "}\n";
2714 else
2716 ss << " return NAN;\n";
2717 ss << "}\n";
2720 void OpSTEYX::GenSlidingWindowFunction(std::stringstream &ss,
2721 const std::string &sSymName, SubArguments &vSubArguments)
2723 ss << "\ndouble " << sSymName;
2724 ss << "_" << BinFuncName() << "(";
2725 for (unsigned i = 0; i < vSubArguments.size(); i++)
2727 if (i)
2728 ss << ",";
2729 vSubArguments[i]->GenSlidingWindowDecl(ss);
2731 ss << "){\n";
2732 ss << " int gid0 = get_global_id(0);\n";
2733 ss << " double fSumX = 0.0;\n";
2734 ss << " double fSumY = 0.0;\n";
2735 ss << " double fMeanX = 0.0;\n";
2736 ss << " double fMeanY = 0.0;\n";
2737 ss << " double fSumDeltaXDeltaY = 0.0;\n";
2738 ss << " double fSumSqrDeltaX = 0.0;\n";
2739 ss << " double fSumSqrDeltaY = 0.0;\n";
2740 ss << " double fCount = 0.0;\n";
2741 ss << " double argX = 0.0;\n";
2742 ss << " double argY = 0.0;\n";
2743 FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
2744 FormulaToken *pCur1 = vSubArguments[0]->GetFormulaToken();
2745 assert(pCur);
2746 assert(pCur1);
2747 if (pCur->GetType() == formula::svDoubleVectorRef&&
2748 pCur1->GetType() == formula::svDoubleVectorRef)
2750 const formula::DoubleVectorRefToken* pDVR =
2751 static_cast<const formula::DoubleVectorRefToken *>(pCur);
2752 const formula::DoubleVectorRefToken* pDVR1 =
2753 static_cast<const formula::DoubleVectorRefToken *>(pCur1);
2754 size_t nCurWindowSize = pDVR->GetRefRowSize();
2755 size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
2756 size_t arrayLength = pDVR->GetArrayLength()<
2757 pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
2758 pDVR1->GetArrayLength();
2759 if(nCurWindowSize != nCurWindowSize1)
2761 ss << " return DBL_MAX;\n";
2762 ss << "}\n";
2763 return ;
2765 ss << " for (int i = ";
2766 if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2767 &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2769 ss << "gid0; i < " << arrayLength;
2770 ss << " && i < " << nCurWindowSize << "; i++)\n";
2771 ss << " {\n";
2773 else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2774 &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2776 ss << "0; i < " << arrayLength;
2777 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2778 ss << " {\n";
2780 else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2781 &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2783 ss << "0; i + gid0 < " << arrayLength;
2784 ss << " && i < " << nCurWindowSize << "; i++)\n";
2785 ss << " {\n";
2787 else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
2788 &&(pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2790 ss << "0; i < " << arrayLength << "; i++)\n";
2791 ss << " {\n";
2793 else
2795 ss << "0; i < " << nCurWindowSize << "; i++)\n";
2796 ss << " {\n";
2797 ss << " break;\n";
2798 ss << " }";
2799 ss << " return DBL_MAX;\n";
2800 ss << "}\n";
2801 return ;
2804 ss << " argX = ";
2805 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2806 ss << " argY = ";
2807 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2808 ss << " if (isNan(argX) || isNan(argY))\n";
2809 ss << " continue;\n";
2810 ss << " fSumX += argX;\n";
2811 ss << " fSumY += argY;\n";
2812 ss << " fCount += 1.0;\n";
2813 ss << " }\n";
2815 ss << " if (fCount < 3.0)\n";
2816 ss << " return DBL_MAX;\n";
2817 ss << " else\n";
2818 ss << " {\n";
2819 ss << " fMeanX = fSumX * pow(fCount,-1.0);\n";
2820 ss << " fMeanY = fSumY * pow(fCount,-1.0);\n";
2822 ss << " for (int i = ";
2823 if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2824 &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2826 ss << "gid0; i < " << arrayLength;
2827 ss << " && i < " << nCurWindowSize << "; i++)\n";
2828 ss << " {\n";
2830 else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2831 &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2833 ss << "0; i < " << arrayLength ;
2834 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2835 ss << " {\n";
2837 else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())&&
2838 (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2840 ss << "0; i + gid0 < " << arrayLength;
2841 ss << " && i < " << nCurWindowSize << "; i++)\n";
2842 ss << " {\n";
2844 else
2846 ss << "0; i < " << arrayLength << "; i++)\n";
2847 ss << " {\n";
2850 ss << " argX = ";
2851 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2852 ss << " argY = ";
2853 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2854 ss << " if (isNan(argX)||isNan(argY))\n";
2855 ss << " continue;\n";
2856 ss << " fSumDeltaXDeltaY +=(argX-fMeanX)*(argY-fMeanY);\n";
2857 ss << " fSumSqrDeltaX += (argX-fMeanX)*(argX-fMeanX);\n";
2858 ss << " fSumSqrDeltaY += (argY-fMeanY)*(argY-fMeanY);\n";
2859 ss << " }\n";
2860 ss << " if(fSumSqrDeltaX == 0.0)\n";
2861 ss << " return DBL_MAX;\n";
2862 ss << " else\n";
2863 ss << " {\n";
2864 ss << " return sqrt((fSumSqrDeltaY - fSumDeltaXDeltaY * \n";
2865 ss << " fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0))\n";
2866 ss << " *pow(fCount - 2.0,-1.0));\n";
2867 ss << " }\n";
2868 ss << " }\n";
2869 ss << "}\n";
2871 else
2873 ss << " return DBL_MAX;\n";
2874 ss << "}\n";
2877 void OpFisher::GenSlidingWindowFunction(
2878 std::stringstream &ss, const std::string &sSymName, SubArguments &
2879 vSubArguments)
2881 ss << "\ndouble " << sSymName;
2882 ss << "_"<< BinFuncName() <<"(";
2883 for (unsigned i = 0; i < vSubArguments.size(); i++)
2885 if (i)
2886 ss << ",";
2887 vSubArguments[i]->GenSlidingWindowDecl(ss);
2889 ss << ") {\n";
2890 ss <<" int gid0=get_global_id(0);\n";
2891 ss <<" double arg0;\n";
2892 if(vSubArguments.size() != 1)
2894 ss << " return DBL_MAX;\n";
2895 return ;
2897 FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
2898 assert(pCur);
2899 if (pCur->GetType() == formula::svDoubleVectorRef)
2901 ss << " return DBL_MAX;\n";
2902 return ;
2904 else if (pCur->GetType() == formula::svSingleVectorRef)
2906 const formula::SingleVectorRefToken* pSVR =
2907 static_cast< const formula::SingleVectorRefToken* >(pCur);
2908 ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2909 ss << ";\n";
2910 ss<< " if(isNan(arg0)||(gid0>=";
2911 ss<<pSVR->GetArrayLength();
2912 ss<<"))\n";
2913 ss<<" arg0 = 0;\n";
2915 else if (pCur->GetType() == formula::svDouble)
2917 ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2918 ss << ";\n";
2919 ss << " if(isNan(arg0))\n";
2920 ss << " return DBL_MAX;\n";
2922 ss << " if (fabs(arg0) >= 1.0)\n";
2923 ss << " return DBL_MAX;\n";
2924 ss << " double tmp=0.5*log((1+arg0)*pow((1-arg0),-1));\n";
2925 ss << " return tmp;\n";
2926 ss << "}\n";
2929 void OpFisherInv::GenSlidingWindowFunction(
2930 std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
2932 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
2933 const formula::SingleVectorRefToken*tmpCurDVR = static_cast<const
2934 formula::SingleVectorRefToken *>(tmpCur);
2935 ss << "\ndouble " << sSymName;
2936 ss << "_"<< BinFuncName() <<"(";
2937 for (unsigned i = 0; i < vSubArguments.size(); i++)
2939 if (i)
2940 ss << ",";
2941 vSubArguments[i]->GenSlidingWindowDecl(ss);
2943 ss << ") {\n\t";
2944 ss <<"int gid0=get_global_id(0);\n\t";
2945 ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2946 ss << ";\n\t";
2947 ss<< "if(isNan(arg0)||(gid0>=";
2948 ss<<tmpCurDVR->GetArrayLength();
2949 ss<<"))\n\t\t";
2950 ss<<"arg0 = 0;\n\t";
2951 ss << "double tmp=tanh(arg0);\n\t";
2952 ss << "return tmp;\n";
2953 ss << "}\n";
2956 void OpGamma::GenSlidingWindowFunction(
2957 std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
2959 ss << "\ndouble " << sSymName;
2960 ss << "_"<< BinFuncName() <<"(";
2961 for (unsigned i = 0; i < vSubArguments.size(); i++)
2963 if (i)
2964 ss << ",";
2965 vSubArguments[i]->GenSlidingWindowDecl(ss);
2967 ss << ") {\n\t";
2968 ss <<"int gid0=get_global_id(0);\n\t";
2969 ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2970 ss << ";\n\t";
2971 ss << "double tmp=tgamma(arg0);\n\t";
2972 ss << "return tmp;\n";
2973 ss << "}\n";
2976 void OpCorrel::GenSlidingWindowFunction(
2977 std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
2979 if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
2980 ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
2981 ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
2982 ///only support DoubleVector in OpCorrelfor GPU calculating.
2983 throw Unhandled();
2984 const formula::DoubleVectorRefToken* pCurDVRX =
2985 static_cast<const formula::DoubleVectorRefToken *>(
2986 vSubArguments[0]->GetFormulaToken());
2987 const formula::DoubleVectorRefToken* pCurDVRY =
2988 static_cast<const formula::DoubleVectorRefToken *>(
2989 vSubArguments[1]->GetFormulaToken());
2990 if( pCurDVRX->GetRefRowSize() != pCurDVRY->GetRefRowSize() )
2991 throw Unhandled();
2993 ss << "\ndouble " << sSymName;
2994 ss << "_"<< BinFuncName() <<"(";
2995 for (unsigned i = 0; i < vSubArguments.size(); i++)
2997 if (i)
2998 ss << ",";
2999 vSubArguments[i]->GenSlidingWindowDecl(ss);
3001 ss << ") {\n\t";
3002 ss << "double vSum = 0.0;\n\t";
3003 ss << "double vXSum = 0.0;\n\t";
3004 ss << "double vYSum = 0.0;\n\t";
3005 ss << "double vXMean = 0.0;\n\t";
3006 ss << "double vYMean = 0.0;\n\t";
3008 ss << "int gid0 = get_global_id(0);\n\t";
3009 ss << "double arg0 = 0.0;\n\t";
3010 ss << "double arg1 = 0.0;\n\t";
3011 ss << "int cnt = 0;\n\t";
3013 size_t nCurWindowSizeX = pCurDVRY->GetRefRowSize();
3015 ss << "for (int i = ";
3016 if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
3017 ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
3018 ss << "arg0 = " << vSubArguments[0]
3019 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3020 ss << "arg1 = " << vSubArguments[1]
3021 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3022 ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
3023 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3024 ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
3025 ss << "arg0 = 0.0;\n\t\t\t";
3026 ss << "arg1 = 0.0;\n\t\t\t";
3027 ss << "--cnt;\n\t\t";
3028 ss << "}\n\t\t";
3029 ss << "++cnt;\n\t\t";
3030 ss << "vXSum += arg0;\n\t\t";
3031 ss << "vYSum += arg1;\n\t";
3032 ss << "}\n\t";
3033 } else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
3034 ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n\t\t";
3035 ss << "arg0 = " << vSubArguments[0]
3036 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3037 ss << "arg1 = " << vSubArguments[1]
3038 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3039 ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
3040 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3041 ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
3042 ss << "arg0 = 0.0;\n\t\t\t";
3043 ss << "arg1 = 0.0;\n\t\t\t";
3044 ss << "--cnt;\n\t\t";
3045 ss << "}\n\t\t";
3046 ss << "++cnt;\n\t\t";
3047 ss << "vXSum += arg0;\n\t\t";
3048 ss << "vYSum += arg1;\n\t";
3049 ss << "}\n\t";
3051 else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
3052 ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
3053 ss << "arg0 = " << vSubArguments[0]
3054 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3055 ss << "arg1 = " << vSubArguments[1]
3056 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3057 ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
3058 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3059 ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
3060 ss << "arg0 = 0.0;\n\t\t\t";
3061 ss << "arg1 = 0.0;\n\t\t\t";
3062 ss << "--cnt;\n\t\t";
3063 ss << "}\n\t\t";
3064 ss << "++cnt;\n\t\t";
3065 ss << "vXSum += arg0;\n\t\t";
3066 ss << "vYSum += arg1;\n\t";
3067 ss << "}\n\t";
3068 } else {
3069 ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
3070 ss << "arg0 = " << vSubArguments[0]
3071 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3072 ss << "arg1 = " << vSubArguments[1]
3073 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3074 ss << "if(isNan(arg0) || isNan(arg1) || (i + gid0 >= ";
3075 ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >=";
3076 ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
3077 ss << "arg0 = 0.0;\n\t\t\t";
3078 ss << "arg1 = 0.0;\n\t\t\t";
3079 ss << "--cnt;\n\t\t";
3080 ss << "}\n\t\t";
3081 ss << "++cnt;\n\t\t";
3082 ss << "vXSum += arg0;\n\t\t";
3083 ss << "vYSum += arg1;\n\t";
3084 ss << "}\n\t";
3087 ss << "if(cnt < 1) {\n\t\t";
3088 ss << "return DBL_MIN;\n\t";
3089 ss << "}\n\t";
3090 ss << "else {\n\t\t";
3091 ss << "vXMean = vXSum/cnt;\n\t\t";
3092 ss << "vYMean = vYSum/cnt;\n\t\t";
3093 ss << "vXSum = 0.0;\n\t\t";
3094 ss << "vYSum = 0.0;\n\t\t";
3096 ss << "for (int i = ";
3097 if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
3098 ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3099 ss << "arg0 = " << vSubArguments[0]
3100 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3101 ss << "arg1 = " << vSubArguments[1]
3102 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3103 ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
3104 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3105 ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3106 ss << "arg0 = vXMean;\n\t\t\t\t";
3107 ss << "arg1 = vYMean;\n\t\t\t";
3108 ss << "}\n\t\t\t";
3109 ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
3110 ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
3111 ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3112 ss << "}\n\t\t";
3113 } else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
3114 ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3115 ss << "arg0 = " << vSubArguments[0]
3116 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3117 ss << "arg1 = " << vSubArguments[1]
3118 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3119 ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
3120 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3121 ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3122 ss << "arg0 = vXMean;\n\t\t\t\t";
3123 ss << "arg1 = vYMean;\n\t\t\t";
3124 ss << "}\n\t\t\t";
3125 ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
3126 ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
3127 ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3128 ss << "}\n\t\t";
3129 } else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
3130 ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3131 ss << "arg0 = " << vSubArguments[0]
3132 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3133 ss << "arg1 = " << vSubArguments[1]
3134 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3135 ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
3136 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3137 ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3138 ss << "arg0 = vXMean;\n\t\t\t\t";
3139 ss << "arg1 = vYMean;\n\t\t\t";
3140 ss << "}\n\t\t\t";
3141 ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
3142 ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
3143 ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3144 ss << "}\n\t\t";
3145 } else {
3146 ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3147 ss << "arg0 = " << vSubArguments[0]
3148 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3149 ss << "arg1 = " << vSubArguments[1]
3150 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3151 ss << "if(isNan(arg0) || isNan(arg1) || (i + gid0 >= ";
3152 ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >=";
3153 ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3154 ss << "arg0 = vXMean;\n\t\t\t\t";
3155 ss << "arg1 = vYMean;\n\t\t\t";
3156 ss << "}\n\t\t\t";
3157 ss << "vXSum += ((arg0 - vXMean)*(arg0 - vXMean));\n\t\t\t";
3158 ss << "vYSum += ((arg1 - vYMean)*(arg1 - vYMean));\n\t\t\t";
3159 ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3160 ss << "}\n\t\t";
3163 ss << "if(vXSum == 0.0 || vYSum == 0.0) {\n\t\t\t";
3164 ss << "return NAN;\n\t\t";
3165 ss << "}\n\t\t";
3166 ss << "else {\n\t\t\t";
3167 ss << "return vSum/pow(vXSum*vYSum, 0.5);\n\t\t";
3168 ss << "}\n\t";
3169 ss << "}\n";
3170 ss << "}";
3173 void OpNegbinomdist::GenSlidingWindowFunction(
3174 std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3176 ss << "\ndouble " << sSymName;
3177 ss << "_"<< BinFuncName() <<"(";
3178 for (unsigned i = 0; i < vSubArguments.size(); i++)
3180 if (i)
3181 ss << ",";
3182 vSubArguments[i]->GenSlidingWindowDecl(ss);
3184 ss << ")\n";
3185 ss << "{\n\t";
3186 ss << "double f,s,p,tmp0,tmp1,tmp2;\n";
3187 ss << " int gid0=get_global_id(0);\n";
3188 size_t i = vSubArguments.size();
3189 ss <<"\n";
3190 for (i = 0; i < vSubArguments.size(); i++)
3192 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3193 assert(pCur);
3194 if (pCur->GetType() == formula::svSingleVectorRef)
3196 const formula::SingleVectorRefToken* pSVR =
3197 static_cast< const formula::SingleVectorRefToken* >(pCur);
3198 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3200 else if (pCur->GetType() == formula::svDouble)
3202 ss << "{\n";
3204 else
3207 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3209 ss << " if (isNan(";
3210 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3211 ss << "))\n";
3212 ss << " tmp"<<i<<"= 0;\n";
3213 ss << " else\n";
3214 ss << " tmp"<<i<<"=\n";
3215 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3216 ss << ";\n}\n";
3218 else
3220 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3221 ss <<";\n";
3224 ss << " p = tmp2;\n";
3225 ss << " s = tmp1;\n";
3226 ss << " f = tmp0;\n";
3227 ss << " double q = 1.0 - p;\n\t";
3228 ss << " double fFactor = pow(p,s);\n\t";
3229 ss << " for(int i=0; i<f; i++)\n\t";
3230 ss << " {\n\t";
3231 ss << " fFactor *= ((double)i+s)*pow(((double)i+1.0),-1.0)/pow(q,-1);\n";
3232 ss << " }\n\t";
3233 ss << " double temp=fFactor;\n\t";
3234 ss << " return temp;\n";
3235 ss << "}\n";
3238 void OpPearson::GenSlidingWindowFunction(
3239 std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3241 if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
3242 ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
3243 ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
3244 ///only support DoubleVector in OpPearson for GPU calculating.
3245 throw Unhandled();
3246 const formula::DoubleVectorRefToken* pDVR =
3247 static_cast<const formula::DoubleVectorRefToken *>(
3248 vSubArguments[0]->GetFormulaToken());
3249 const formula::DoubleVectorRefToken* pCurDVRY =
3250 static_cast<const formula::DoubleVectorRefToken *>(
3251 vSubArguments[1]->GetFormulaToken());
3252 if( pDVR->GetRefRowSize() != pCurDVRY->GetRefRowSize() )
3253 throw Unhandled();
3255 size_t nCurWindowSize = pDVR->GetRefRowSize();
3257 ss << "\ndouble " << sSymName;
3258 ss << "_"<< BinFuncName() <<"(";
3259 for (unsigned i = 0; i < vSubArguments.size(); i++)
3261 if (i)
3262 ss << ",";
3263 vSubArguments[i]->GenSlidingWindowDecl(ss);
3265 ss << ")\n";
3266 ss << "{\n";
3267 ss << " int gid0=get_global_id(0);\n";
3268 ss << " double fCount = 0.0;\n";
3269 ss << " double fSumX = 0.0;\n";
3270 ss << " double fSumY = 0.0;\n";
3271 ss << " double fSumDeltaXDeltaY = 0.0;\n";
3272 ss << " double fInx;\n";
3273 ss << " double fIny;\n";
3274 ss << "for (int i = ";
3275 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
3276 ss << "gid0; i < " << pDVR->GetArrayLength();
3277 ss << " && i < " << nCurWindowSize << "; i++){\n";
3278 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
3279 ss << "0; i < " << pDVR->GetArrayLength();
3280 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
3281 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
3282 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3283 ss << " && i < "<< nCurWindowSize << "; i++){\n";
3285 else {
3286 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
3288 ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef(true);
3289 ss << ";\n";
3290 ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef(true);
3291 ss << " ;\n";
3292 ss << " if(isNan(fInx)||isNan(fIny)){fInx=0.0;fIny=0.0;fCount = fCount-1;}\n";
3293 ss << " fSumX += fInx;\n";
3294 ss << " fSumY += fIny;\n";
3295 ss << " fCount = fCount + 1;\n";
3296 ss << " }\n";
3297 ss << " double fMeanX = fSumX / fCount;\n";
3298 ss << " double fMeanY = fSumY / fCount;\n";
3299 ss << " fSumX = 0.0;\n";
3300 ss << " fSumY = 0.0;\n";
3301 ss << "for (int i = ";
3302 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
3303 ss << "gid0; i < " << pDVR->GetArrayLength();
3304 ss << " && i < " << nCurWindowSize << "; i++){\n";
3305 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
3306 ss << "0; i < " << pDVR->GetArrayLength();
3307 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
3308 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
3309 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3310 ss << " && i < "<< nCurWindowSize << "; i++){\n";
3312 else {
3313 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
3315 ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef(true);
3316 ss << " ;\n";
3317 ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef(true);
3318 ss << " ;\n";
3319 ss << " if(isNan(fInx)||isNan(fIny)){fInx=0.0;fIny=0.0;}\n";
3320 ss << " fSumDeltaXDeltaY += (fInx - fMeanX) * (fIny - fMeanY);\n";
3321 ss << " fSumX += pow(fInx - fMeanX,2);\n";
3322 ss << " fSumY += pow(fIny - fMeanY,2);\n";
3323 ss << " }\n";
3324 ss << " double tmp = ( fSumDeltaXDeltaY / ";
3325 ss << "sqrt( fSumX * fSumY));\n\t";
3326 ss << " if (isnan(tmp))\n";
3327 ss << " return CreateDoubleError(errNoValue);\n";
3328 ss << " return tmp;\n";
3329 ss << "}\n";
3332 void OpGammaLn::GenSlidingWindowFunction(
3333 std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3335 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
3336 const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
3337 formula::SingleVectorRefToken *>(tmpCur);
3338 ss << "\ndouble " << sSymName;
3339 ss << "_"<< BinFuncName() <<"(";
3340 for (unsigned i = 0; i < vSubArguments.size(); i++)
3342 if (i)
3343 ss << ",";
3344 vSubArguments[i]->GenSlidingWindowDecl(ss);
3346 ss << ") {\n\t";
3347 ss <<"int gid0=get_global_id(0);\n\t";
3348 ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
3349 ss << ";\n\t";
3350 ss<< "if(isNan(arg0)||(gid0>=";
3351 ss<<tmpCurDVR->GetArrayLength();
3352 ss<<"))\n\t\t";
3353 ss<<"arg0 = 0;\n\t";
3354 ss << "double tmp=lgamma(arg0);\n\t";
3355 ss << "return tmp;\n";
3356 ss << "}\n";
3358 void OpGauss::BinInlineFun(std::set<std::string>& decls,
3359 std::set<std::string>& funs)
3361 decls.insert(taylorDecl);decls.insert(phiDecl);
3362 decls.insert(gaussDecl);
3363 funs.insert(taylor);funs.insert(phi);
3364 funs.insert(gauss);
3367 void OpGauss::GenSlidingWindowFunction(
3368 std::stringstream &ss, const std::string &sSymName, SubArguments &
3369 vSubArguments)
3371 ss << "\ndouble " << sSymName;
3372 ss << "_"<< BinFuncName() <<"(";
3373 for (unsigned i = 0; i < vSubArguments.size(); i++)
3375 if (i)
3376 ss << ",";
3377 vSubArguments[i]->GenSlidingWindowDecl(ss);
3379 ss << ") {\n";
3380 ss <<" int gid0=get_global_id(0);\n";
3381 ss <<" double arg0;\n";
3382 if(vSubArguments.size() != 1)
3384 ss << " return DBL_MAX;\n";
3385 return ;
3387 FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
3388 assert(pCur);
3389 if (pCur->GetType() == formula::svDoubleVectorRef)
3391 ss << " return DBL_MAX;\n";
3392 return ;
3394 else if (pCur->GetType() == formula::svSingleVectorRef)
3396 const formula::SingleVectorRefToken* pSVR =
3397 static_cast< const formula::SingleVectorRefToken* >(pCur);
3398 ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
3399 ss << ";\n";
3400 ss<< " if(isNan(arg0)||(gid0>=";
3401 ss<<pSVR->GetArrayLength();
3402 ss<<"))\n";
3403 ss<<" arg0 = 0;\n";
3405 else if (pCur->GetType() == formula::svDouble)
3407 ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
3408 ss << ";\n";
3409 ss << " if(isNan(arg0))\n";
3410 ss << " return DBL_MAX;\n";
3412 ss << " double tmp=gauss(arg0);\n";
3413 ss << " return tmp;\n";
3414 ss << "}\n";
3417 void OpGeoMean::GenSlidingWindowFunction(
3418 std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3420 ss << "__kernel void ";
3421 ss << "GeoMean_reduction( ";
3422 for (unsigned i = 0; i < vSubArguments.size(); i++)
3424 if (i)
3425 ss << ",";
3426 vSubArguments[i]->GenSlidingWindowDecl(ss);
3428 ss << ", __global double *result)\n";
3429 ss << "{\n";
3430 ss << " double tmp =0;\n";
3431 ss << " int count = 0;\n";
3432 ss << " int i ;\n";
3433 GenTmpVariables(ss,vSubArguments);
3434 ss << " double current_sum = 0.0;\n";
3435 ss << " int windowSize;\n";
3436 ss << " int arrayLength;\n";
3437 ss << " int current_count = 0;\n";
3438 ss << " int writePos = get_group_id(1);\n";
3439 ss << " int lidx = get_local_id(0);\n";
3440 ss << " __local double shm_buf[256];\n";
3441 ss << " __local int count_buf[256];\n";
3442 ss << " int loop;\n";
3443 ss << " int offset;\n";
3444 ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3446 for(unsigned i=0;i<vSubArguments.size();i++)
3448 assert(vSubArguments[i]->GetFormulaToken());
3450 if(vSubArguments[i]->GetFormulaToken()->GetType() ==
3451 formula::svDoubleVectorRef)
3453 FormulaToken *tmpCur = vSubArguments[i]->GetFormulaToken();
3454 const formula::DoubleVectorRefToken*pCurDVR= static_cast<const
3455 formula::DoubleVectorRefToken *>(tmpCur);
3456 size_t nCurWindowSize = pCurDVR->GetArrayLength() <
3457 pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
3458 pCurDVR->GetRefRowSize() ;
3460 if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
3461 ss << " offset = 0;\n";
3462 else if (!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
3463 ss << " offset = get_group_id(1);\n";
3464 else
3465 throw Unhandled();
3466 ss << " windowSize = ";
3467 ss << nCurWindowSize;
3468 ss << ";\n";
3469 ss << " arrayLength = ";
3470 ss << pCurDVR->GetArrayLength();
3471 ss << ";\n";
3472 ss << " loop = arrayLength/512 + 1;\n";
3473 ss << " for (int l=0; l<loop; l++){\n";
3474 ss << " tmp = 0.0;\n";
3475 ss << " count = 0;\n";
3476 ss << " int loopOffset = l*512;\n";
3477 ss << " int p1 = loopOffset + lidx + offset, p2 = p1 + 256;\n";
3478 ss << " if (p2 < min(offset + windowSize, arrayLength)) {\n";
3479 ss << " tmp0 = 0.0;\n";
3480 std::string p1 = "p1";
3481 std::string p2 = "p2";
3483 ss << " tmp0 =";
3484 vSubArguments[i]->GenDeclRef(ss);
3485 ss << "["<<p1.c_str()<<"];\n";
3486 ss << " if(!isNan(tmp0))\n";
3487 ss << " {\n";
3488 ss << " tmp += log(tmp0);\n";
3489 ss << " count++;\n";
3490 ss << " }\n";
3492 ss << " tmp0 =";
3493 vSubArguments[i]->GenDeclRef(ss);
3494 ss << "["<<p2.c_str()<<"];\n";
3495 ss << " if(!isNan(tmp0))\n";
3496 ss << " {\n";
3497 ss << " tmp += log(tmp0);\n";
3498 ss << " count++;\n";
3499 ss << " }\n";
3501 ss << " }\n";
3502 ss << " else if (p1 < min(arrayLength, offset + windowSize)) {\n";
3504 ss << " tmp0 =";
3505 vSubArguments[i]->GenDeclRef(ss);
3506 ss << "["<<p1.c_str()<<"];\n";
3507 ss << " if(!isNan(tmp0))\n";
3508 ss << " {\n";
3509 ss << " tmp += log(tmp0);\n";
3510 ss << " count++;\n";
3511 ss << " }\n";
3513 ss << " }\n";
3514 ss << " shm_buf[lidx] = tmp;\n";
3515 ss << " count_buf[lidx] = count;\n";
3516 ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3518 ss << " for (int i = 128; i >0; i/=2) {\n";
3519 ss << " if (lidx < i)\n";
3520 ss << " {\n";
3521 ss << " shm_buf[lidx] += shm_buf[lidx + i];\n";
3522 ss << " count_buf[lidx] += count_buf[lidx + i];\n";
3523 ss << " }\n";
3524 ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3525 ss << " }\n";
3526 ss << " if (lidx == 0)\n";
3527 ss << " {\n";
3528 ss << " current_sum += shm_buf[0];\n";
3529 ss << " current_count += count_buf[0];\n";
3530 ss << " }\n";
3531 // ss << "if(writePos == 14 && lidx ==0)\n";
3532 //ss <<"printf(\"\\n********************sum is is %f, count is%d\",current_sum,current_count);\n";
3533 ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3534 ss << " }\n";
3535 }else
3537 ss << " if (lidx == 0)\n";
3538 ss << " {\n";
3539 ss << " tmp0 =";
3540 if(vSubArguments[i]->GetFormulaToken()->GetType() ==
3541 formula::svSingleVectorRef)
3543 vSubArguments[i]->GenDeclRef(ss);
3544 ss << "[writePos];\n";
3546 else
3548 vSubArguments[i]->GenDeclRef(ss);
3549 ss <<";\n";
3550 //ss <<"printf(\"\\n********************tmp0 is %f\",tmp0);\n";
3552 ss << " if(!isNan(tmp0))\n";
3553 ss << " {\n";
3554 ss << " current_sum += log(tmp0);\n";
3555 ss << " current_count++;\n";
3556 ss << " }\n";
3557 ss << " }\n";
3561 ss << " if (lidx == 0)\n";
3562 ss << " result[writePos] = exp(current_sum/current_count);\n";
3563 ss << "}\n";
3565 ss << "\ndouble " << sSymName;
3566 ss << "_"<< BinFuncName() <<"(";
3567 for (unsigned i = 0; i < vSubArguments.size(); i++)
3569 if (i)
3570 ss << ",";
3571 vSubArguments[i]->GenSlidingWindowDecl(ss);
3573 ss << ")\n {\n";
3574 ss <<" int gid0=get_global_id(0);\n";
3575 ss << " double tmp =0;\n";
3576 ss << " tmp =";
3577 vSubArguments[0]->GenDeclRef(ss);
3578 ss << "[gid0];\n";
3579 ss << " return tmp;\n";
3580 ss << "}";
3583 void OpHarMean::GenSlidingWindowFunction(
3584 std::stringstream &ss, const std::string &sSymName, SubArguments &
3585 vSubArguments)
3588 ss << "\ndouble " << sSymName;
3589 ss << "_"<< BinFuncName() <<"( ";
3590 for (unsigned i = 0; i < vSubArguments.size(); i++)
3592 if (i)
3593 ss << ",";
3594 vSubArguments[i]->GenSlidingWindowDecl(ss);
3596 ss << ")\n";
3597 ss <<"{\n";
3598 ss << " int gid0 = get_global_id(0);\n";
3599 ss << " double nVal=0.0;\n";
3600 ss << " double tmp = 0;\n";
3601 ss << " int length;\n";
3602 ss << " int totallength=0;\n";
3603 for (unsigned i = 0; i < vSubArguments.size(); i++)
3605 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3606 assert(pCur);
3607 if (pCur->GetType() == formula::svDoubleVectorRef)
3609 const formula::DoubleVectorRefToken* pDVR =
3610 static_cast<const formula::DoubleVectorRefToken *>(pCur);
3611 size_t nCurWindowSize = pDVR->GetRefRowSize();
3612 ss << " length="<<nCurWindowSize;
3613 ss << ";\n";
3614 ss << " for (int i = ";
3615 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
3616 ss << " {\n";
3617 ss << " double arg"<<i<<" = ";
3618 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3619 ss << ";\n";
3620 ss << " if(isNan(arg"<<i<<")||((gid0+i)>=";
3621 ss << pDVR->GetArrayLength();
3622 ss << "))\n";
3623 ss << " {\n";
3624 ss << " length--;\n";
3625 ss << " continue;\n";
3626 ss << " }\n";
3627 ss << " nVal += (1.0 *pow(";
3628 ss << " arg"<<i<<",-1));\n";
3629 ss << " }\n";
3630 ss << " totallength +=length;\n";
3632 else if (pCur->GetType() == formula::svSingleVectorRef)
3634 ss << " tmp = ";
3635 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3636 ss << ";\n";
3637 ss << " if(!isNan(tmp))\n";
3638 ss << " {\n";
3639 ss << " nVal += (1.0 * pow( tmp,-1));\n";
3640 ss << " totallength +=1;\n";
3641 ss << " }\n";
3643 else if (pCur->GetType() == formula::svDouble)
3645 ss << " tmp = ";
3646 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3647 ss << ";\n";
3648 ss << " nVal += (1.0 *pow( tmp,-1));\n";
3649 ss << " totallength +=1;\n";
3651 else
3653 ss << " return DBL_MIN;\n";
3656 ss << " tmp = totallength*pow(nVal,-1);\n";
3657 ss << " return tmp;\n";
3658 ss << "}";
3661 void OpConfidence::BinInlineFun(std::set<std::string>& decls,
3662 std::set<std::string>& funs)
3664 decls.insert(gaussinvDecl);
3665 funs.insert(gaussinv);
3668 void OpConfidence::GenSlidingWindowFunction(std::stringstream& ss,
3669 const std::string &sSymName, SubArguments& vSubArguments)
3671 ss << "\ndouble " << sSymName;
3672 ss << "_"<< BinFuncName() <<"(";
3673 for (unsigned i = 0; i < vSubArguments.size(); i++)
3675 if (i)
3676 ss << ",";
3677 vSubArguments[i]->GenSlidingWindowDecl(ss);
3679 ss << ") {\n";
3680 ss << " double tmp = " << GetBottom() <<";\n";
3681 ss << " int gid0 = get_global_id(0);\n";
3682 ss << " double alpha = " << GetBottom() <<";\n";
3683 ss << " double sigma = " << GetBottom() <<";\n";
3684 ss << " double size = " << GetBottom() <<";\n";
3685 ss << " double tmp0,tmp1,tmp2;\n";
3686 size_t i = vSubArguments.size();
3687 ss <<"\n";
3688 for (i = 0; i < vSubArguments.size(); i++)
3690 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3691 assert(pCur);
3692 if (pCur->GetType() == formula::svSingleVectorRef)
3694 const formula::SingleVectorRefToken* pSVR =
3695 static_cast< const formula::SingleVectorRefToken* >(pCur);
3696 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3698 else if (pCur->GetType() == formula::svDouble)
3700 ss << "{\n";
3702 else
3705 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3707 ss << " if (isNan(";
3708 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3709 ss << "))\n";
3710 ss << " tmp"<<i<<"= 0;\n";
3711 ss << " else\n";
3712 ss << " tmp"<<i<<"=\n";
3713 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3714 ss << ";\n}\n";
3716 else
3718 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3719 ss <<";\n";
3722 ss << " alpha = tmp0;\n";
3723 ss << " sigma = tmp1;\n";
3724 ss << " size = tmp2;\n";
3725 ss << " double rn = floor(size);\n";
3726 ss << " if(sigma <= 0.0 || alpha <= 0.0 || alpha >= 1.0";
3727 ss << "|| rn < 1.0)\n";
3728 ss << " tmp = -DBL_MAX;\n";
3729 ss << " else\n";
3730 ss << " tmp = gaussinv(1.0 - alpha * pow(2.0,-1.0)) * sigma ";
3731 ss << "* pow(sqrt( rn ),-1);\n";
3732 ss << " return tmp;\n";
3733 ss << "}";
3736 void OpCritBinom::BinInlineFun(std::set<std::string>& decls,
3737 std::set<std::string>& funs)
3739 decls.insert(MinDecl);
3740 funs.insert("");
3743 void OpCritBinom::GenSlidingWindowFunction(std::stringstream& ss,
3744 const std::string &sSymName, SubArguments& vSubArguments)
3746 ss << "\ndouble " << sSymName;
3747 ss << "_"<< BinFuncName() <<"(";
3748 for (unsigned i = 0; i < vSubArguments.size(); i++)
3750 if (i)
3751 ss << ",";
3752 vSubArguments[i]->GenSlidingWindowDecl(ss);
3754 ss << ") {\n";
3755 ss << " double tmp = " << GetBottom() <<";\n";
3756 ss << " int gid0 = get_global_id(0);\n";
3757 ss << " double n = " << GetBottom() <<";\n";
3758 ss << " double p = " << GetBottom() <<";\n";
3759 ss << " double alpha = " << GetBottom() <<";\n";
3760 ss << " double tmp0 = 0.0,tmp1 = 0.0,tmp2 = 0.0;\n";
3761 size_t i = vSubArguments.size();
3762 ss <<"\n";
3763 for (i = 0; i < vSubArguments.size(); i++)
3765 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3766 assert(pCur);
3767 if (pCur->GetType() == formula::svSingleVectorRef)
3769 const formula::SingleVectorRefToken* pSVR =
3770 static_cast< const formula::SingleVectorRefToken* >(pCur);
3771 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3773 else if (pCur->GetType() == formula::svDouble)
3775 ss << "{\n";
3777 else
3780 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3782 ss << " if (isNan(";
3783 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3784 ss << "))\n";
3785 ss << " tmp"<<i<<"= 0;\n";
3786 ss << " else\n";
3787 ss << " tmp"<<i<<"=\n";
3788 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3789 ss << ";\n}\n";
3791 else
3793 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3794 ss <<";\n";
3797 ss << " n = tmp0;\n";
3798 ss << " p = tmp1;\n";
3799 ss << " alpha = tmp2;\n";
3800 ss << " double rn = floor(n);\n";
3801 ss << " if (rn < 0.0 || alpha <= 0.0 || alpha >= 1.0 || p < 0.0";
3802 ss << " || p > 1.0)\n";
3803 ss << " tmp = -DBL_MIN;\n";
3804 ss << " else\n";
3805 ss << " {\n";
3806 ss << " double rq = (0.5 - p) + 0.5;\n";
3807 ss << " double fFactor = pow(rq, rn);\n";
3808 ss << " if (fFactor <= Min)\n";
3809 ss << " {\n";
3810 ss << " fFactor = pow(p, rn);\n";
3811 ss << " if (fFactor <= Min)\n";
3812 ss << " tmp = -DBL_MAX;\n";
3813 ss << " else\n";
3814 ss << " {\n";
3815 ss << " double fSum = 1.0 - fFactor;\n";
3816 ss << " uint max =(uint)(rn), i;\n";
3817 ss << " for (i = 0; i < max && fSum >= alpha; i++)\n";
3818 ss << " {\n";
3819 ss << " fFactor *= (rn - i) * pow((double)(i + 1),-1.0) *";
3820 ss << " rq * pow(p, -1.0);\n";
3821 ss << " fSum -= fFactor;\n";
3822 ss << " }\n";
3823 ss << " tmp = (rn - i);\n";
3824 ss << " }\n";
3825 ss << " }\n";
3826 ss << " else\n";
3827 ss << " {\n";
3828 ss << " double fSum = fFactor;\n";
3829 ss << " uint max = (uint)(rn), i;\n";
3830 ss << " for (i = 0; i < max && fSum < alpha; i++)\n";
3831 ss << " {\n";
3832 ss << " fFactor *= (rn - i) * pow((double)(i + 1), -1.0) *";
3833 ss << " p * pow(rq, -1.0);\n";
3834 ss << " fSum += fFactor;\n";
3835 ss << " }\n";
3836 ss << " tmp = (i);\n";
3837 ss << " }\n";
3838 ss << " }\n";
3839 ss << " return tmp;\n";
3840 ss << "}";
3843 void OpRsq::GenSlidingWindowFunction(
3844 std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3846 if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
3847 ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
3848 ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
3849 ///only support DoubleVector in OpRsq for GPU calculating.
3850 throw Unhandled();
3851 const formula::DoubleVectorRefToken* pCurDVR1 =
3852 static_cast<const formula::DoubleVectorRefToken *>(
3853 vSubArguments[0]->GetFormulaToken());
3854 const formula::DoubleVectorRefToken* pCurDVR2 =
3855 static_cast<const formula::DoubleVectorRefToken *>(
3856 vSubArguments[1]->GetFormulaToken());
3857 if( pCurDVR1->GetRefRowSize() != pCurDVR2->GetRefRowSize() )
3858 throw Unhandled();
3860 size_t nCurWindowSize = pCurDVR1->GetRefRowSize();
3862 ss << "\ndouble " << sSymName;
3863 ss << "_"<< BinFuncName() <<"(";
3864 for (unsigned i = 0; i < vSubArguments.size(); i++)
3866 if (i)
3867 ss << ",";
3868 vSubArguments[i]->GenSlidingWindowDecl(ss);
3870 ss << ")\n";
3871 ss << "{\n";
3872 ss << " int gid0=get_global_id(0);\n";
3873 ss << " double fCount = 0.0;\n";
3874 ss << " double fSumX = 0.0;\n";
3875 ss << " double fSumY = 0.0;\n";
3876 ss << " double fSumDeltaXDeltaY = 0.0;\n";
3877 ss << " double fInx;\n";
3878 ss << " double fIny;\n";
3879 ss << " double tmp0,tmp1;\n";
3881 ss <<"\n";
3883 ss << " for(int i=0; i<"<<nCurWindowSize<<"; i++)\n";
3884 ss << " {\n";
3885 ss << " if(isNan(";
3886 ss << vSubArguments[0]->GenSlidingWindowDeclRef(true);
3887 ss << "))\n";
3888 ss << " fInx = 0;\n";
3889 ss << " else\n";
3890 ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
3891 ss << ";\n";
3892 ss << " if(isNan(";
3893 ss << vSubArguments[1]->GenSlidingWindowDeclRef(true);
3894 ss << "))\n";
3895 ss << " fIny = 0;\n";
3896 ss << " else\n";
3897 ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
3898 ss << " ;\n";
3899 ss << " fSumX += fInx;\n";
3900 ss << " fSumY += fIny;\n";
3901 ss << " fCount = fCount + 1;\n";
3902 ss << " }\n";
3903 ss << " double fMeanX = fSumX / fCount;\n";
3904 ss << " double fMeanY = fSumY / fCount;\n";
3905 ss << " fSumX = 0.0;\n";
3906 ss << " fSumY = 0.0;\n";
3907 ss << " for(int i=0; i<"<<nCurWindowSize<<"; i++)\n";
3908 ss << " {\n";
3909 ss << " if(isNan(";
3910 ss << vSubArguments[0]->GenSlidingWindowDeclRef(true);
3911 ss << "))\n";
3912 ss << " fInx = 0;\n";
3913 ss << " else\n";
3914 ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
3915 ss << ";\n";
3916 ss << " if(isNan(";
3917 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3918 ss << "))\n";
3919 ss << " fIny = 0;\n";
3920 ss << " else\n";
3921 ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
3922 ss << " ;\n";
3923 ss << " fSumDeltaXDeltaY += (fInx - fMeanX) * (fIny - fMeanY);\n";
3924 ss << " fSumX += pow(fInx - fMeanX,2);\n";
3925 ss << " fSumY += pow(fIny - fMeanY,2);\n";
3926 ss << " }\n";
3927 ss << " double tmp = pow( fSumDeltaXDeltaY,2) / (fSumX * fSumY);\n";
3928 ss << " return tmp ;\n";
3929 ss << "}\n";
3932 void OpChiInv::BinInlineFun(std::set<std::string>& decls,
3933 std::set<std::string>& funs)
3935 decls.insert(fMachEpsDecl);
3936 funs.insert("");
3937 decls.insert(fBigInvDecl);
3938 funs.insert("");
3939 decls.insert(fHalfMachEpsDecl);
3940 funs.insert("");
3941 decls.insert(lcl_IterateInverseChiInvDecl);
3942 funs.insert(lcl_IterateInverseChiInv);
3943 decls.insert(GetChiDistDecl);
3944 funs.insert(GetChiDist);
3945 decls.insert(lcl_HasChangeOfSignDecl);
3946 funs.insert(lcl_HasChangeOfSign);
3947 decls.insert(GetUpRegIGammaDecl);
3948 funs.insert(GetUpRegIGamma);
3949 decls.insert(GetGammaContFractionDecl);
3950 funs.insert(GetGammaContFraction);
3951 decls.insert(GetGammaSeriesDecl);
3952 funs.insert(GetGammaSeries);
3954 void OpChiInv::GenSlidingWindowFunction(
3955 std::stringstream &ss,const std::string &sSymName,
3956 SubArguments &vSubArguments)
3958 ss << "\ndouble " << sSymName;
3959 ss << "_"<< BinFuncName() <<"(";
3960 for (unsigned i = 0; i < vSubArguments.size(); i++)
3962 if (i)
3963 ss << ",";
3964 vSubArguments[i]->GenSlidingWindowDecl(ss);
3966 ss << ")\n";
3967 ss << "{\n";
3968 ss << " double tmp0,tmp1,tmp;\n";
3969 ss << " int gid0=get_global_id(0);\n";
3970 size_t i = vSubArguments.size();
3971 ss <<"\n ";
3972 for (i = 0; i < vSubArguments.size(); i++)
3974 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3975 assert(pCur);
3976 if (pCur->GetType() == formula::svDoubleVectorRef)
3978 const formula::DoubleVectorRefToken* pDVR =
3979 static_cast<const formula::DoubleVectorRefToken *>(pCur);
3980 size_t nCurWindowSize = pDVR->GetRefRowSize();
3981 ss << "for (int i = ";
3982 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
3983 ss << "gid0; i < " << pDVR->GetArrayLength();
3984 ss << " && i < " << nCurWindowSize << "; i++){\n";
3985 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
3986 ss << "0; i < " << pDVR->GetArrayLength();
3987 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
3988 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
3989 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3990 ss << " && i < "<< nCurWindowSize << "; i++){\n";
3992 else {
3993 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
3996 else if (pCur->GetType() == formula::svSingleVectorRef)
3998 const formula::SingleVectorRefToken* pSVR =
3999 static_cast< const formula::SingleVectorRefToken* >(pCur);
4000 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4002 else if (pCur->GetType() == formula::svDouble)
4004 ss << "{\n";
4006 else
4009 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4011 ss << "if (isNan(";
4012 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4013 ss << "))\n";
4014 ss << " tmp"<<i<<"= 0;\n";
4015 ss << "else\n";
4016 ss <<"tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4017 ss << ";\n}\n";
4019 else
4021 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4022 ss << ";\n";
4025 ss << " tmp1 = floor(tmp1);";
4026 ss << " if (tmp1 < 1.0 || tmp0 <= 0.0 || tmp0 > 1.0 )\n";
4027 ss << " {\n";
4028 ss << " return DBL_MIN;\n";
4029 ss << " }\n";
4030 ss << " bool bConvError;\n";
4031 ss << " double fVal = lcl_IterateInverseChiInv";
4032 ss << "(tmp0, tmp1, tmp1*0.5, tmp1, &bConvError);\n";
4033 ss << " if(bConvError)\n";
4034 ss << " return DBL_MIN;\n";
4035 ss << " return fVal;\n";
4036 ss << "}\n";
4038 void OpNormdist::GenSlidingWindowFunction(
4039 std::stringstream &ss, const std::string &sSymName,
4040 SubArguments &vSubArguments)
4042 ss << "\ndouble " << sSymName;
4043 ss << "_"<< BinFuncName() <<"(";
4044 for (unsigned i = 0; i < vSubArguments.size(); i++)
4046 if (i)
4047 ss << ",";
4048 vSubArguments[i]->GenSlidingWindowDecl(ss);
4050 ss << ")\n";
4051 ss << "{\n";
4052 ss << " double x,mue,sigma,c;\n";
4053 ss << " int gid0=get_global_id(0);\n";
4054 ss << " double tmp0,tmp1,tmp2,tmp3;\n";
4055 size_t i = vSubArguments.size();
4056 ss <<"\n ";
4057 for (i = 0; i < vSubArguments.size(); i++)
4059 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4060 assert(pCur);
4061 if (pCur->GetType() == formula::svSingleVectorRef)
4063 const formula::SingleVectorRefToken* pSVR =
4064 static_cast< const formula::SingleVectorRefToken* >(pCur);
4065 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4067 else if (pCur->GetType() == formula::svDouble)
4069 ss << "{\n";
4071 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4073 ss << " if (isNan(";
4074 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4075 ss << "))\n";
4076 ss << " tmp"<<i<<"= 0;\n";
4077 ss << " else\n";
4078 ss << " tmp"<<i<<"=\n";
4079 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4080 ss << ";\n}\n";
4082 else
4084 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4085 ss <<";\n";
4088 ss << "x = tmp0;\n";
4089 ss << "mue = tmp1;\n";
4090 ss << "sigma = tmp2;\n";
4091 ss << "c = tmp3;\n";
4092 ss << "double mid,tmp;\n";
4093 ss << "mid = (x - mue)/sigma;\n";
4094 ss << "if(c)\n";
4095 ss << " tmp = 0.5 *erfc(-mid * 0.7071067811865475);\n";
4096 ss << "else \n";
4097 ss <<" tmp=(0.39894228040143268*exp(-pow(mid,2)/2.0))/sigma;\n";
4098 ss << "return tmp;\n";
4099 ss << "}\n";
4101 void OpNormsdist::BinInlineFun(std::set<std::string>& decls,
4102 std::set<std::string>& funs)
4104 decls.insert(lcl_Erfc0600Decl);
4105 funs.insert(lcl_Erfc0600);
4106 decls.insert(lcl_Erfc2654Decl);
4107 funs.insert(lcl_Erfc2654);
4108 decls.insert(lcl_Erf0065Decl);
4109 funs.insert(lcl_Erf0065);
4110 decls.insert(rtl_math_erf_rdDecl);
4111 funs.insert(rtl_math_erf_rd);
4112 decls.insert(rtl_math_erfc_rdDecl);
4113 funs.insert(rtl_math_erfc_rd);
4116 void OpNormsdist::GenSlidingWindowFunction(
4117 std::stringstream &ss,const std::string &sSymName,
4118 SubArguments &vSubArguments)
4120 ss << "\ndouble " << sSymName;
4121 ss << "_"<< BinFuncName() <<"(";
4122 for (unsigned i = 0; i < vSubArguments.size(); i++)
4124 if (i)
4125 ss << ",";
4126 vSubArguments[i]->GenSlidingWindowDecl(ss);
4128 ss << ")\n";
4129 ss << "{\n";
4130 ss << " double x = 0,tmp0 = 0;\n";
4131 ss << " int gid0=get_global_id(0);\n";
4132 size_t i = vSubArguments.size();
4133 ss <<"\n ";
4134 for (i = 0; i < vSubArguments.size(); i++)
4136 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4137 assert(pCur);
4138 if (pCur->GetType() == formula::svSingleVectorRef)
4140 const formula::SingleVectorRefToken* pSVR =
4141 static_cast< const formula::SingleVectorRefToken* >(pCur);
4142 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4144 else if (pCur->GetType() == formula::svDouble)
4146 ss << "{\n";
4148 else
4151 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4153 ss << " if (isNan(";
4154 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4155 ss << "))\n";
4156 ss << " tmp"<<i<<"= 0;\n";
4157 ss << " else\n";
4158 ss << " tmp"<<i<<"=\n";
4159 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4160 ss << ";\n}\n";
4162 else
4164 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4165 ss <<";\n";
4168 ss << " x = tmp0;\n";
4169 ss << " double tmp = 0.5 * rtl_math_erfc_rd((-1)*x * 0.7071067811865475);\n";
4170 ss << " return tmp;\n";
4171 ss << "}\n";
4174 void OpPermut::GenSlidingWindowFunction(
4175 std::stringstream &ss,const std::string &sSymName,
4176 SubArguments &vSubArguments)
4178 ss << "\ndouble " << sSymName;
4179 ss << "_"<< BinFuncName() <<"(";
4180 for (unsigned i = 0; i < vSubArguments.size(); i++)
4182 if (i)
4183 ss << ",";
4184 vSubArguments[i]->GenSlidingWindowDecl(ss);
4186 ss << ") {\n";
4187 ss <<" int gid0=get_global_id(0);\n";
4188 ss <<" double inA;\n";
4189 ss <<" double inB;\n";
4190 ss <<" double tmp0,tmp1;\n";
4191 ss <<" double tmp = 1 ;\n";
4192 size_t i = vSubArguments.size();
4193 ss <<"\n";
4194 for (i = 0; i < vSubArguments.size(); i++)
4196 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4197 assert(pCur);
4198 if (pCur->GetType() == formula::svSingleVectorRef)
4200 const formula::SingleVectorRefToken* pSVR =
4201 static_cast< const formula::SingleVectorRefToken* >(pCur);
4202 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4205 else if (pCur->GetType() == formula::svDouble)
4207 ss << "{\n";
4209 else
4212 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4214 ss << " if (isNan(";
4215 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4216 ss << "))\n";
4217 ss << " tmp"<<i<<"= 0;\n";
4218 ss << " else\n";
4219 ss << " tmp"<<i<<"=\n";
4220 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4221 ss << ";\n}\n";
4223 else
4225 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4226 ss <<";\n";
4229 ss << " inA = tmp0;\n";
4230 ss << " inB = tmp1;\n";
4231 ss << " for( int i =0; i<inB; i++)\n";
4232 ss << " {\n";
4233 ss << " tmp *= inA ;\n";
4234 ss << " inA = inA - 1.0;\n";
4235 ss << " }\n";
4236 ss << " return tmp;\n";
4237 ss << "}\n";
4239 void OpPermutationA::GenSlidingWindowFunction(
4240 std::stringstream &ss,const std::string &sSymName,
4241 SubArguments &vSubArguments)
4243 ss << "\ndouble " << sSymName;
4244 ss << "_"<< BinFuncName() <<"(";
4245 for (unsigned i = 0; i < vSubArguments.size(); i++)
4247 if (i)
4248 ss << ",";
4249 vSubArguments[i]->GenSlidingWindowDecl(ss);
4251 ss << ") {\n";
4252 ss <<" int gid0=get_global_id(0);\n";
4253 ss <<" double inA;\n";
4254 ss <<" double inB;\n";
4255 ss <<" double tmp = 1.0;\n";
4256 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
4257 const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
4258 formula::SingleVectorRefToken *>(tmpCur0);
4259 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
4260 const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
4261 formula::SingleVectorRefToken *>(tmpCur1);
4262 ss << "int buffer_fIna_len = ";
4263 ss << tmpCurDVR0->GetArrayLength();
4264 ss << ";\n";
4265 ss << " int buffer_fInb_len = ";
4266 ss << tmpCurDVR1->GetArrayLength();
4267 ss << ";\n";
4268 ss << " if((gid0)>=buffer_fIna_len || isNan(";
4269 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4270 ss << "))\n";
4271 ss << " inA = 0;\nelse \n";
4272 ss << " inA = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
4273 ss << ";\n";
4274 ss << "if((gid0)>=buffer_fInb_len || isNan(";
4275 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4276 ss << "))\n";
4277 ss << "inB = 0;\nelse \n";
4278 ss << " inB = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
4279 ss << ";\n";
4280 ss << " for(int i=0; i<inB; i++)\n";
4281 ss << " {\n";
4282 ss << " tmp *= inA;\n";
4283 ss << " }\n";
4284 ss << " return tmp;\n";
4285 ss << "}\n";
4288 void OpPhi::GenSlidingWindowFunction(
4289 std::stringstream &ss,const std::string &sSymName,
4290 SubArguments &vSubArguments)
4292 ss << "\ndouble " << sSymName;
4293 ss << "_"<< BinFuncName() <<"(";
4294 for (unsigned i = 0; i < vSubArguments.size(); i++)
4296 if (i)
4297 ss << ",";
4298 vSubArguments[i]->GenSlidingWindowDecl(ss);
4300 ss << ")\n";
4301 ss << "{\n";
4302 ss << " double x,tmp0;\n";
4303 ss << " int gid0=get_global_id(0);\n";
4304 size_t i = vSubArguments.size();
4305 ss <<"\n";
4306 for (i = 0; i < vSubArguments.size(); i++)
4308 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4309 assert(pCur);
4310 if (pCur->GetType() == formula::svSingleVectorRef)
4312 const formula::SingleVectorRefToken* pSVR =
4313 static_cast< const formula::SingleVectorRefToken* >(pCur);
4314 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4316 else if (pCur->GetType() == formula::svDouble)
4318 ss << "{\n";
4320 else
4323 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4325 ss << " if (isNan(";
4326 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4327 ss << "))\n";
4328 ss << " tmp"<<i<<"= 0;\n";
4329 ss << " else\n";
4330 ss << " tmp"<<i<<"=\n";
4331 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4332 ss << ";\n}\n";
4334 else
4336 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4337 ss <<";\n";
4340 ss << " x = tmp0;\n";
4341 ss << " double tmp = 0.39894228040143268 * exp((-1)*pow(x,2) / 2.0);\n";
4342 ss << " return tmp;\n";
4343 ss << "}\n";
4346 void OpNorminv::GenSlidingWindowFunction(
4347 std::stringstream &ss,const std::string &sSymName,
4348 SubArguments &vSubArguments)
4350 ss << "\ndouble " << sSymName;
4351 ss << "_"<< BinFuncName() <<"(";
4352 for (unsigned i = 0; i < vSubArguments.size(); i++)
4354 if (i)
4355 ss << ",";
4356 vSubArguments[i]->GenSlidingWindowDecl(ss);
4358 ss << ")\n";
4359 ss << "{\n";
4360 ss <<" double q,t,z;\n";
4361 ss <<" double x,mue,sigma;\n";
4362 ss <<" double tmp0,tmp1,tmp2;\n";
4363 ss <<" int gid0=get_global_id(0);\n";
4364 size_t i = vSubArguments.size();
4365 ss <<"\n";
4366 for (i = 0; i < vSubArguments.size(); i++)
4368 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4369 assert(pCur);
4370 if (pCur->GetType() == formula::svSingleVectorRef)
4372 const formula::SingleVectorRefToken* pSVR =
4373 static_cast< const formula::SingleVectorRefToken* >(pCur);
4374 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4376 else if (pCur->GetType() == formula::svDouble)
4378 ss << "{\n";
4380 else
4383 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4385 ss << " if (isNan(";
4386 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4387 ss << "))\n";
4388 ss << " tmp"<<i<<"= 0;\n";
4389 ss << " else\n";
4390 ss << " tmp"<<i<<"=\n";
4391 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4392 ss << ";\n}\n";
4394 else
4396 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4397 ss <<";\n";
4400 ss <<" x = tmp0;\n";
4401 ss <<" mue = tmp1;\n";
4402 ss <<" sigma = tmp2;\n";
4403 ss <<" q = x -0.5;\n";
4404 ss <<" if(fabs(q)<=.425)\n";
4405 ss <<" {\n";
4406 ss <<" t=0.180625-pow(q,2);\n";
4407 ss <<" z=\n"
4408 "q*\n"
4409 "(\n"
4410 "(\n"
4411 "(\n"
4412 "(\n"
4413 "(\n"
4414 "(\n"
4415 "(\n"
4416 "t*2509.0809287301226727";
4417 ss <<"+33430.575583588128105\n"
4418 ")\n"
4419 "*t+67265.770927008700853\n"
4420 ")\n"
4421 "*t+45921.953931549871457\n"
4422 ")\n"
4423 "*t+13731.693765509461125\n"
4424 ")\n"
4425 "*t+1971.5909503065514427\n"
4426 ")\n"
4427 "*t+133.14166789178437745\n"
4428 ")\n"
4429 "*t+3.387132872796366608\n"
4430 ")\n"
4431 "/\n"
4432 "(\n"
4433 "(\n"
4434 "(\n"
4435 "(\n"
4436 "(\n"
4437 "(\n"
4438 "(\n"
4439 "t*5226.495278852854561";
4440 ss <<"+28729.085735721942674\n"
4441 ")\n"
4442 "*t+39307.89580009271061\n"
4443 ")\n"
4444 "*t+21213.794301586595867\n"
4445 ")\n"
4446 "*t+5394.1960214247511077\n"
4447 ")\n"
4448 "*t+687.1870074920579083\n"
4449 ")\n"
4450 "*t+42.313330701600911252\n"
4451 ")\n"
4452 "*t+1.0\n"
4453 ");\n";
4454 ss <<"}\nelse\n{\n";
4455 ss <<" if(q>0)\nt=1-x;\n";
4456 ss <<"else\nt=x;\n";
4457 ss <<"t=sqrt(-log(t));\n";
4458 ss <<"if(t<=5.0)\n{\n";
4459 ss <<"t+=-1.6;\n";
4460 ss <<"z=\n"
4461 "(\n"
4462 "(\n"
4463 "(\n"
4464 "(\n"
4465 "(\n"
4466 "(\n"
4467 "(\n"
4468 "t*7.7454501427834140764e-4";
4469 ss <<"+0.0227238449892691845833\n"
4470 ")\n"
4471 "*t+0.24178072517745061177\n"
4472 ")\n"
4473 "*t+1.27045825245236838258\n"
4474 ")\n"
4475 "*t+3.64784832476320460504\n"
4476 ")\n"
4477 "*t+5.7694972214606914055\n"
4478 ")\n"
4479 "*t+4.6303378461565452959\n"
4480 ")\n"
4481 "*t+1.42343711074968357734\n"
4482 ")\n"
4483 "/\n"
4484 "(\n"
4485 "(\n"
4486 "(\n"
4487 "(\n"
4488 "(\n"
4489 "(\n"
4490 "(\n"
4491 "t*1.05075007164441684324e-9";
4492 ss <<"+5.475938084995344946e-4\n"
4493 ")\n"
4494 "*t+0.0151986665636164571966\n"
4495 ")\n"
4496 "*t+0.14810397642748007459\n"
4497 ")\n"
4498 "*t+0.68976733498510000455\n"
4499 ")\n"
4500 "*t+1.6763848301838038494\n"
4501 ")\n"
4502 "*t+2.05319162663775882187\n"
4503 ")\n"
4504 "*t+1.0\n"
4505 ");\n}\n";
4506 ss <<"else\n{\n";
4507 ss <<"t+=-5.0;\n";
4508 ss <<"z=\n"
4509 "(\n"
4510 "(\n"
4511 "(\n"
4512 "(\n"
4513 "(\n"
4514 "(\n"
4515 "(\n"
4516 "t*2.01033439929228813265e-7";
4517 ss<<"+2.71155556874348757815e-5\n"
4518 ")\n"
4519 "*t+0.0012426609473880784386\n"
4520 ")\n"
4521 "*t+0.026532189526576123093\n"
4522 ")\n"
4523 "*t+0.29656057182850489123\n"
4524 ")\n"
4525 "*t+1.7848265399172913358\n"
4526 ")\n"
4527 "*t+5.4637849111641143699\n"
4528 ")\n"
4529 "*t+6.6579046435011037772\n"
4530 ")\n"
4531 "/\n"
4532 "(\n"
4533 "(\n"
4534 "(\n"
4535 "(\n"
4536 "(\n"
4537 "(\n"
4538 "(\n"
4539 "t*2.04426310338993978564e-15"
4540 "+1.4215117583164458887e-7\n"
4541 ")\n"
4542 "*t+1.8463183175100546818e-5\n"
4543 ")\n"
4544 "*t+7.868691311456132591e-4\n"
4545 ")\n"
4546 "*t+0.0148753612908506148525\n"
4547 ")\n"
4548 "*t+0.13692988092273580531\n"
4549 ")\n"
4550 "*t+0.59983220655588793769\n"
4551 ")\n"
4552 "*t+1.0\n"
4553 ");\n";
4554 ss<<"}\n";
4555 ss << "z = q < 0.0 ? (-1)*z : z;\n";
4556 ss<<"}\n";
4557 ss<<"double tmp = z*sigma + mue;\n";
4558 ss<<"return tmp;\n";
4559 ss<<"}\n";
4561 void OpNormsinv:: GenSlidingWindowFunction
4562 (std::stringstream &ss,const std::string &sSymName,
4563 SubArguments &vSubArguments)
4565 ss << "\ndouble " << sSymName;
4566 ss << "_"<< BinFuncName() <<"(";
4567 for (unsigned i = 0; i < vSubArguments.size(); i++)
4569 if (i)
4570 ss << ",";
4571 vSubArguments[i]->GenSlidingWindowDecl(ss);
4573 ss << ")\n";
4574 ss << "{\n";
4575 ss << " double q,t,z,x,tmp0;\n";
4576 ss << " int gid0=get_global_id(0);\n";
4577 size_t i = vSubArguments.size();
4578 ss <<"\n";
4579 for (i = 0; i < vSubArguments.size(); i++)
4581 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4582 assert(pCur);
4583 if (pCur->GetType() == formula::svSingleVectorRef)
4585 const formula::SingleVectorRefToken* pSVR =
4586 static_cast< const formula::SingleVectorRefToken* >(pCur);
4587 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4589 else if (pCur->GetType() == formula::svDouble)
4591 ss << "{\n";
4593 else
4596 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4598 ss << " if (isNan(";
4599 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4600 ss << "))\n";
4601 ss << " tmp"<<i<<"= 0;\n";
4602 ss << " else\n";
4603 ss << " tmp"<<i<<"=\n";
4604 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4605 ss << ";\n}\n";
4607 else
4609 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4610 ss <<";\n";
4613 ss <<" x = tmp0;\n";
4614 ss <<" q = x -0.5;\n";
4615 ss <<" if(fabs(q)<=.425)\n";
4616 ss <<" {\n";
4617 ss <<" t=0.180625-pow(q,2);\n";
4618 ss <<" z=\n"
4619 "q*\n"
4620 "(\n"
4621 "(\n"
4622 "(\n"
4623 "(\n"
4624 "(\n"
4625 "(\n"
4626 "(\n"
4627 "t*2509.0809287301226727";
4628 ss <<"+33430.575583588128105\n"
4629 ")\n"
4630 "*t+67265.770927008700853\n"
4631 ")\n"
4632 "*t+45921.953931549871457\n"
4633 ")\n"
4634 "*t+13731.693765509461125\n"
4635 ")\n"
4636 "*t+1971.5909503065514427\n"
4637 ")\n"
4638 "*t+133.14166789178437745\n"
4639 ")\n"
4640 "*t+3.387132872796366608\n"
4641 ")\n"
4642 "/\n"
4643 "(\n"
4644 "(\n"
4645 "(\n"
4646 "(\n"
4647 "(\n"
4648 "(\n"
4649 "(\n"
4650 "t*5226.495278852854561";
4651 ss <<"+28729.085735721942674\n"
4652 ")\n"
4653 "*t+39307.89580009271061\n"
4654 ")\n"
4655 "*t+21213.794301586595867\n"
4656 ")\n"
4657 "*t+5394.1960214247511077\n"
4658 ")\n"
4659 "*t+687.1870074920579083\n"
4660 ")\n"
4661 "*t+42.313330701600911252\n"
4662 ")\n"
4663 "*t+1.0\n"
4664 ");\n";
4665 ss <<"}\nelse\n{\n";
4666 ss <<" if(q>0)\nt=1-x;\n";
4667 ss <<"else\nt=x;\n";
4668 ss <<"t=sqrt(-log(t));\n";
4669 ss <<"if(t<=5.0)\n{\n";
4670 ss <<"t+=-1.6;\n";
4671 ss <<"z=\n"
4672 "(\n"
4673 "(\n"
4674 "(\n"
4675 "(\n"
4676 "(\n"
4677 "(\n"
4678 "(\n"
4679 "t*7.7454501427834140764e-4";
4680 ss <<"+0.0227238449892691845833\n"
4681 ")\n"
4682 "*t+0.24178072517745061177\n"
4683 ")\n"
4684 "*t+1.27045825245236838258\n"
4685 ")\n"
4686 "*t+3.64784832476320460504\n"
4687 ")\n"
4688 "*t+5.7694972214606914055\n"
4689 ")\n"
4690 "*t+4.6303378461565452959\n"
4691 ")\n"
4692 "*t+1.42343711074968357734\n"
4693 ")\n"
4694 "/\n"
4695 "(\n"
4696 "(\n"
4697 "(\n"
4698 "(\n"
4699 "(\n"
4700 "(\n"
4701 "(\n"
4702 "t*1.05075007164441684324e-9";
4703 ss <<"+5.475938084995344946e-4\n"
4704 ")\n"
4705 "*t+0.0151986665636164571966\n"
4706 ")\n"
4707 "*t+0.14810397642748007459\n"
4708 ")\n"
4709 "*t+0.68976733498510000455\n"
4710 ")\n"
4711 "*t+1.6763848301838038494\n"
4712 ")\n"
4713 "*t+2.05319162663775882187\n"
4714 ")\n"
4715 "*t+1.0\n"
4716 ");\n}\n";
4717 ss <<"else\n{\n";
4718 ss <<"t+=-5.0;\n";
4719 ss <<"z=\n"
4720 "(\n"
4721 "(\n"
4722 "(\n"
4723 "(\n"
4724 "(\n"
4725 "(\n"
4726 "(\n"
4727 "t*2.01033439929228813265e-7";
4728 ss <<"+2.71155556874348757815e-5\n"
4729 ")\n"
4730 "*t+0.0012426609473880784386\n"
4731 ")\n"
4732 "*t+0.026532189526576123093\n"
4733 ")\n"
4734 "*t+0.29656057182850489123\n"
4735 ")\n"
4736 "*t+1.7848265399172913358\n"
4737 ")\n"
4738 "*t+5.4637849111641143699\n"
4739 ")\n"
4740 "*t+6.6579046435011037772\n"
4741 ")\n"
4742 "/\n"
4743 "(\n"
4744 "(\n"
4745 "(\n"
4746 "(\n"
4747 "(\n"
4748 "(\n"
4749 "(\n"
4750 "t*2.04426310338993978564e-15"
4751 "+1.4215117583164458887e-7\n"
4752 ")\n"
4753 "*t+1.8463183175100546818e-5\n"
4754 ")\n"
4755 "*t+7.868691311456132591e-4\n"
4756 ")\n"
4757 "*t+0.0148753612908506148525\n"
4758 ")\n"
4759 "*t+0.13692988092273580531\n"
4760 ")\n"
4761 "*t+0.59983220655588793769\n"
4762 ")\n"
4763 "*t+1.0\n"
4764 ");\n";
4765 ss <<"}\n";
4766 ss << "z = q < 0.0 ? (-1)*z : z;\n";
4767 ss <<"}\n";
4768 ss <<"if (isnan(z))\n";
4769 ss <<" return CreateDoubleError(errNoValue);\n";
4770 ss <<"return z;\n";
4771 ss <<"}\n";
4773 void OpMedian::GenSlidingWindowFunction(
4774 std::stringstream &ss, const std::string &sSymName,
4775 SubArguments &vSubArguments)
4777 ss << "\ndouble " << sSymName;
4778 ss << "_"<< BinFuncName() <<"(";
4779 for (unsigned i = 0; i < vSubArguments.size(); i++)
4781 if (i)
4782 ss << ",";
4783 vSubArguments[i]->GenSlidingWindowDecl(ss);
4785 ss << ") {\n";
4786 ss << " int gid0 = get_global_id(0);\n";
4787 ss << " double tmp = 0;\n";
4788 ss << " int i;\n";
4789 ss << " unsigned int startFlag = 0;\n";
4790 ss << " unsigned int endFlag = 0;\n";
4791 ss << " double dataIna;\n";
4792 for (unsigned i = 0; i < vSubArguments.size(); i++)
4794 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4795 assert(pCur);
4796 if (const formula::DoubleVectorRefToken* pCurDVR =
4797 dynamic_cast<const formula::DoubleVectorRefToken *>(pCur))
4799 size_t nCurWindowSize = pCurDVR->GetRefRowSize();
4800 ss << "startFlag = ";
4801 if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
4803 ss << "gid0; endFlag = "<< nCurWindowSize <<"-gid0;\n";
4805 ss << "gid0; endFlag = gid0+"<< nCurWindowSize <<";\n";
4807 else
4809 ss<<"startFlag=gid0;endFlag=gid0;\n";
4812 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
4813 const formula::DoubleVectorRefToken*tmpCurDVR0= static_cast<const
4814 formula::DoubleVectorRefToken *>(tmpCur0);
4815 ss << "int buffer_fIna_len = ";
4816 ss << tmpCurDVR0->GetArrayLength();
4817 ss << ";\n";
4818 ss<<"if((i+gid0)>=buffer_fIna_len || isNan(";
4819 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4820 ss<<"))\n";
4821 ss<<" dataIna = 0;\n";
4822 ss << " int nSize =endFlag- startFlag ;\n";
4823 ss << " if (nSize & 1)\n";
4824 ss << " {\n";
4825 ss << " tmp = "<<vSubArguments[0]->GetName();
4826 ss << " [startFlag+nSize/2];\n";
4827 ss << " }\n";
4828 ss << " else\n";
4829 ss << " {\n";
4830 ss << " tmp =("<<vSubArguments[0]->GetName();
4831 ss << " [startFlag+nSize/2]+";
4832 ss << vSubArguments[0]->GetName();
4833 ss << " [startFlag+nSize/2-1])/2;\n";
4834 ss << " }\n";
4835 ss <<" return tmp;\n";
4836 ss << "}\n";
4838 void OpKurt:: GenSlidingWindowFunction(std::stringstream &ss,
4839 const std::string &sSymName, SubArguments &vSubArguments)
4841 ss << "\ndouble " << sSymName;
4842 ss << "_"<< BinFuncName() <<"( ";
4843 for (unsigned i = 0; i < vSubArguments.size(); i++)
4845 if (i)
4846 ss << ",";
4847 vSubArguments[i]->GenSlidingWindowDecl(ss);
4849 ss << ")\n";
4850 ss <<"{\n";
4851 ss << " int gid0 = get_global_id(0);\n";
4852 ss << " double fSum = 0.0;\n";
4853 ss << " double vSum = 0.0;\n";
4854 ss << " double length;\n";
4855 ss << " double totallength=0;\n";
4856 ss << " double tmp = 0;\n";
4857 for (unsigned i = 0; i < vSubArguments.size(); i++)
4859 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4860 assert(pCur);
4861 if (pCur->GetType() == formula::svDoubleVectorRef)
4863 const formula::DoubleVectorRefToken* pDVR =
4864 static_cast<const formula::DoubleVectorRefToken *>(pCur);
4865 size_t nCurWindowSize = pDVR->GetRefRowSize();
4866 ss << " length="<<nCurWindowSize;
4867 ss << ";\n";
4868 ss << " for (int i = ";
4869 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
4870 ss << " {\n";
4871 ss << " double arg"<<i<<" = ";
4872 ss << vSubArguments[i]->GenSlidingWindowDeclRef(true);
4873 ss << ";\n";
4874 ss << " if(isNan(arg"<<i<<")||((gid0+i)>=";
4875 ss << pDVR->GetArrayLength();
4876 ss << "))\n";
4877 ss << " {\n";
4878 ss << " length-=1.0;\n";
4879 ss << " continue;\n";
4880 ss << " }\n";
4881 ss << " fSum += arg"<<i<<";\n";
4882 ss << " }\n";
4883 ss << " totallength +=length;\n";
4885 else if (pCur->GetType() == formula::svSingleVectorRef)
4887 ss << " tmp = ";
4888 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4889 ss << ";\n";
4890 ss << " if(!isNan(tmp))\n";
4891 ss << " {\n";
4892 ss << " fSum += tmp;\n";
4893 ss << " totallength +=1;\n";
4894 ss << " }\n";
4896 else if (pCur->GetType() == formula::svDouble)
4898 ss << " tmp = ";
4899 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4900 ss << ";\n";
4901 ss << " fSum += tmp;\n";
4902 ss << " totallength +=1;\n";
4904 else
4906 ss << " return DBL_MIN;\n";
4909 ss << " double fMean = fSum * pow(totallength,-1);\n";
4910 for (unsigned i = 0; i < vSubArguments.size(); i++)
4912 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4913 assert(pCur);
4914 if (pCur->GetType() == formula::svDoubleVectorRef)
4916 const formula::DoubleVectorRefToken* pDVR =
4917 static_cast<const formula::DoubleVectorRefToken *>(pCur);
4918 size_t nCurWindowSize = pDVR->GetRefRowSize();
4919 ss << " for (int i = ";
4920 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
4921 ss << " {\n";
4922 ss << " double arg"<<i<<" = ";
4923 ss << vSubArguments[i]->GenSlidingWindowDeclRef(true);
4924 ss << ";\n";
4925 ss << " if(isNan(arg"<<i<<")||((gid0+i)>=";
4926 ss << pDVR->GetArrayLength();
4927 ss << "))\n";
4928 ss << " {\n";
4929 ss << " continue;\n";
4930 ss << " }\n";
4931 ss << " vSum += (arg"<<i<<"-fMean)*(arg"<<i<<"-fMean);\n";
4932 ss << " }\n";
4934 else if (pCur->GetType() == formula::svSingleVectorRef)
4936 ss << " tmp = ";
4937 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4938 ss << ";\n";
4939 ss << " if(!isNan(tmp))\n";
4940 ss << " {\n";
4941 ss << " vSum += (tmp-fMean)*(tmp-fMean);\n";
4942 ss << " }\n";
4944 else if (pCur->GetType() == formula::svDouble)
4946 ss << " tmp = ";
4947 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4948 ss << ";\n";
4949 ss << " vSum += (tmp-fMean)*(tmp-fMean);\n";
4952 ss << " double fStdDev = sqrt(vSum / (totallength - 1.0));\n";
4953 ss << " double dx = 0.0;\n";
4954 ss << " double xpower4 = 0.0;\n";
4955 for (unsigned i = 0; i < vSubArguments.size(); i++)
4957 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4958 assert(pCur);
4959 if (pCur->GetType() == formula::svDoubleVectorRef)
4961 const formula::DoubleVectorRefToken* pDVR =
4962 static_cast<const formula::DoubleVectorRefToken *>(pCur);
4963 size_t nCurWindowSize = pDVR->GetRefRowSize();
4964 ss << " for (int i = ";
4965 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
4966 ss << " {\n";
4967 ss << " double arg"<<i<<" = ";
4968 ss << vSubArguments[i]->GenSlidingWindowDeclRef(true);
4969 ss << ";\n";
4970 ss << " if(isNan(arg"<<i<<")||((gid0+i)>=";
4971 ss << pDVR->GetArrayLength();
4972 ss << "))\n";
4973 ss << " {\n";
4974 ss << " continue;\n";
4975 ss << " }\n";
4976 ss<< " dx = (arg"<<i<<" -fMean) / fStdDev;\n";
4977 ss<< " xpower4 = xpower4 + (dx * dx * dx * dx);\n";
4978 ss << " }\n";
4980 else if (pCur->GetType() == formula::svSingleVectorRef)
4982 ss << " tmp = ";
4983 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4984 ss << ";\n";
4985 ss << " if(!isNan(tmp))\n";
4986 ss << " {\n";
4987 ss<< " dx = (tmp -fMean) / fStdDev;\n";
4988 ss<< " xpower4 = xpower4 + (dx * dx * dx * dx);\n";
4989 ss << " }\n";
4991 else if (pCur->GetType() == formula::svDouble)
4993 ss << " tmp = ";
4994 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4995 ss << ";\n";
4996 ss<< " dx = (tmp -fMean) / fStdDev;\n";
4997 ss<< " xpower4 = xpower4 + (dx * dx * dx * dx);\n";
5000 ss<< " double k_d = (totallength - 2.0) * (totallength - 3.0);\n";
5001 ss<< " double k_l = totallength * (totallength + 1.0) /";
5002 ss<< "((totallength - 1.0) * k_d);\n";
5003 ss<< " double k_t = 3.0 * (totallength - 1.0) * ";
5004 ss<< "(totallength - 1.0) / k_d;\n";
5005 ss<< " tmp = xpower4 * k_l - k_t;\n";
5006 ss<< " return tmp;\n";
5007 ss << "}";
5010 void OpIntercept::GenSlidingWindowFunction(std::stringstream &ss,
5011 const std::string &sSymName, SubArguments &vSubArguments)
5013 ss << "\ndouble " << sSymName;
5014 ss << "_" << BinFuncName() << "(";
5015 for (unsigned i = 0; i < vSubArguments.size(); i++)
5017 if (i)
5018 ss << ",";
5019 vSubArguments[i]->GenSlidingWindowDecl(ss);
5021 ss << "){\n";
5022 ss << " int gid0 = get_global_id(0);\n";
5023 ss << " double fSumX = 0.0;\n";
5024 ss << " double fSumY = 0.0;\n";
5025 ss << " double fMeanX = 0.0;\n";
5026 ss << " double fMeanY = 0.0;\n";
5027 ss << " double fSumDeltaXDeltaY = 0.0;\n";
5028 ss << " double fSumSqrDeltaX = 0.0;\n";
5029 ss << " double fCount = 0.0;\n";
5030 ss << " double argX = 0.0;\n";
5031 ss << " double argY = 0.0;\n";
5032 if(vSubArguments.size() != 2)
5034 ss << " return NAN;\n";
5035 ss << "}\n";
5036 return ;
5038 FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
5039 FormulaToken *pCur1 = vSubArguments[0]->GetFormulaToken();
5040 assert(pCur);
5041 assert(pCur1);
5042 if (pCur->GetType() == formula::svDoubleVectorRef&&
5043 pCur1->GetType() == formula::svDoubleVectorRef)
5045 const formula::DoubleVectorRefToken* pDVR =
5046 static_cast<const formula::DoubleVectorRefToken *>(pCur);
5047 const formula::DoubleVectorRefToken* pDVR1 =
5048 static_cast<const formula::DoubleVectorRefToken *>(pCur1);
5050 size_t nCurWindowSize = pDVR->GetRefRowSize();
5051 size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
5052 size_t arrayLength = pDVR->GetArrayLength()<
5053 pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
5054 pDVR1->GetArrayLength();
5055 if(nCurWindowSize != nCurWindowSize1)
5057 ss << " return NAN;\n";
5058 ss << "}\n";
5059 return ;
5061 ss << " for (int i = ";
5062 if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
5063 &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
5065 ss << "gid0; i < " << arrayLength;
5066 ss << " && i < " << nCurWindowSize << "; i++)\n";
5067 ss << " {\n";
5069 else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
5070 &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
5072 ss << "0; i < " << arrayLength ;
5073 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
5074 ss << " {\n";
5076 else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
5077 &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
5079 ss << "0; i + gid0 < " << arrayLength;
5080 ss << " && i < " << nCurWindowSize << "; i++)\n";
5081 ss << " {\n";
5083 else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
5084 &&(pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
5086 ss << "0; i < " << arrayLength << "; i++)\n";
5087 ss << " {\n";
5089 else
5091 ss << "0; i < " << nCurWindowSize << "; i++)\n";
5092 ss << " {\n";
5093 ss << " break;\n";
5094 ss << " }";
5095 ss << " return NAN;\n";
5096 ss << "}\n";
5097 return ;
5100 ss << " argX = ";
5101 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
5102 ss << " argY = ";
5103 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
5104 ss << " if (isNan(argX) || isNan(argY))\n";
5105 ss << " continue;\n";
5106 ss << " fSumX += argX;\n";
5107 ss << " fSumY += argY;\n";
5108 ss << " fCount += 1.0;\n";
5109 ss << " }\n";
5111 ss << " if (fCount < 1.0)\n";
5112 ss << " return NAN;\n";
5113 ss << " else\n";
5114 ss << " {\n";
5115 ss << " fMeanX = fSumX * pow(fCount,-1.0);\n";
5116 ss << " fMeanY = fSumY * pow(fCount,-1.0);\n";
5118 ss << " for (int i = ";
5119 if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
5120 &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
5122 ss << "gid0; i < " << arrayLength;
5123 ss << " && i < " << nCurWindowSize << "; i++)\n";
5124 ss << " {\n";
5126 else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
5127 &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
5129 ss << "0; i < " << arrayLength ;
5130 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
5131 ss << " {\n";
5133 else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
5134 &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
5136 ss << "0; i + gid0 < " << arrayLength;
5137 ss << " && i < " << nCurWindowSize << "; i++)\n";
5138 ss << " {\n";
5140 else
5142 ss << "0; i < " << arrayLength << "; i++)\n";
5143 ss << " {\n";
5146 ss << " argX = ";
5147 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
5148 ss << " argY = ";
5149 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
5150 ss << " if (isNan(argX) || isNan(argY))\n";
5151 ss << " continue;\n";
5152 ss << " fSumDeltaXDeltaY += (argX-fMeanX)*(argY-fMeanY);\n";
5153 ss << " fSumSqrDeltaX += (argX-fMeanX) * (argX-fMeanX);\n";
5154 ss << " }\n";
5155 ss << " if(fSumSqrDeltaX == 0.0)\n";
5156 ss << " return NAN;\n";
5157 ss << " else\n";
5158 ss << " {\n";
5159 ss << " return fMeanY -";
5160 ss << " (fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0))*fMeanX;\n";
5161 ss << " }\n";
5162 ss << " }\n";
5163 ss << "}\n";
5165 else
5167 ss << " return NAN;\n";
5168 ss << "}\n";
5171 void OpLogInv:: GenSlidingWindowFunction(std::stringstream &ss,
5172 const std::string &sSymName, SubArguments &vSubArguments)
5174 ss << "\ndouble " << sSymName;
5175 ss << "_"<< BinFuncName() <<"(";
5176 for (unsigned i = 0; i < vSubArguments.size(); i++)
5178 if (i)
5179 ss << ",";
5180 vSubArguments[i]->GenSlidingWindowDecl(ss);
5182 ss << ") {\n";
5183 ss << " int gid0=get_global_id(0);\n";
5184 ss << " double tmp;\n";
5185 ss << " double arg0,arg1,arg2,arg3;\n";
5186 size_t i = vSubArguments.size();
5187 for (i = 0; i < vSubArguments.size(); i++)
5189 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
5190 assert(pCur);
5191 if (pCur->GetType() == formula::svDoubleVectorRef)
5193 const formula::DoubleVectorRefToken* pDVR =
5194 static_cast<const formula::DoubleVectorRefToken *>(pCur);
5195 size_t nCurWindowSize = pDVR->GetRefRowSize();
5196 ss << "for (int i = ";
5197 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
5198 ss << "gid0; i < " << pDVR->GetArrayLength();
5199 ss << " && i < " << nCurWindowSize << "; i++){\n";
5200 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
5201 ss << "0; i < " << pDVR->GetArrayLength();
5202 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
5203 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
5204 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
5205 ss << " && i < "<< nCurWindowSize << "; i++){\n ";
5207 else {
5208 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
5211 else if (pCur->GetType() == formula::svSingleVectorRef)
5213 const formula::SingleVectorRefToken* pSVR =
5214 static_cast< const formula::SingleVectorRefToken* >(pCur);
5215 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
5216 ss << " {\n";
5217 ss << " if (isNan(";
5218 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
5219 ss << "))\n";
5220 ss << " arg"<<i<<"= 0;\n";
5221 ss << " else\n";
5222 ss << " arg"<<i<<"=";
5223 ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
5224 ss << ";\n";
5225 ss << " }\n";
5226 ss << " else\n";
5227 ss << " arg"<<i<<"= 0;\n";
5229 else if (pCur->GetType() == formula::svDouble)
5231 ss << " if (isNan(";
5232 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
5233 ss << "))\n";
5234 ss << " arg"<<i<<"= 0;\n";
5235 ss << " else\n";
5236 ss << " arg"<<i<<"=";
5237 ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
5238 ss << ";\n";
5241 ss<< " double q,t,z;\n";
5242 ss<< " q = arg0 -0.5;\n";
5243 ss<< " if(fabs(q)<=.425)\n";
5244 ss<< " {\n";
5245 ss<< " t=0.180625-pow(q, 2);\n";
5246 ss<< " z=\n"
5247 " q*\n"
5248 " (\n"
5249 " (\n"
5250 " (\n"
5251 " (\n"
5252 " (\n"
5253 " (\n"
5254 " (\n"
5255 " t*2509.0809287301226727";
5256 ss<<"+33430.575583588128105\n"
5257 " )\n"
5258 " *t+67265.770927008700853\n"
5259 " )\n"
5260 " *t+45921.953931549871457\n"
5261 " )\n"
5262 " *t+13731.693765509461125\n"
5263 " )\n"
5264 " *t+1971.5909503065514427\n"
5265 " )\n"
5266 " *t+133.14166789178437745\n"
5267 " )\n"
5268 " *t+3.387132872796366608\n"
5269 " )\n"
5270 " /\n"
5271 " (\n"
5272 " (\n"
5273 " (\n"
5274 " (\n"
5275 " (\n"
5276 " (\n"
5277 " (\n"
5278 " t*5226.495278852854561";
5279 ss<<"+28729.085735721942674\n"
5280 " )\n"
5281 " *t+39307.89580009271061\n"
5282 " )\n"
5283 " *t+21213.794301586595867\n"
5284 " )\n"
5285 " *t+5394.1960214247511077\n"
5286 " )\n"
5287 " *t+687.1870074920579083\n"
5288 " )\n"
5289 " *t+42.313330701600911252\n"
5290 " )\n"
5291 " *t+1.0\n"
5292 " );\n";
5293 ss<<" }\n";
5294 ss<<" else\n";
5295 ss<<" {\n";
5296 ss<<" t = q > 0 ? 1 - arg0 : arg0;\n";
5297 ss<<" t=sqrt(-log(t));\n";
5298 ss<<" if(t<=5.0)\n";
5299 ss<<" {\n";
5300 ss<<" t+=-1.6;\n";
5301 ss<<" z=\n"
5302 " (\n"
5303 " (\n"
5304 " (\n"
5305 " (\n"
5306 " (\n"
5307 " (\n"
5308 " (\n"
5309 " t*7.7454501427834140764e-4";
5310 ss<<"+0.0227238449892691845833\n"
5311 " )\n"
5312 " *t+0.24178072517745061177\n"
5313 " )\n"
5314 " *t+1.27045825245236838258\n"
5315 " )\n"
5316 " *t+3.64784832476320460504\n"
5317 " )\n"
5318 " *t+5.7694972214606914055\n"
5319 " )\n"
5320 " *t+4.6303378461565452959\n"
5321 " )\n"
5322 " *t+1.42343711074968357734\n"
5323 " )\n"
5324 " /\n"
5325 " (\n"
5326 " (\n"
5327 " (\n"
5328 " (\n"
5329 " (\n"
5330 " (\n"
5331 " (\n"
5332 " t*1.05075007164441684324e-9";
5333 ss<<"+5.475938084995344946e-4\n"
5334 " )\n"
5335 " *t+0.0151986665636164571966\n"
5336 " )\n"
5337 " *t+0.14810397642748007459\n"
5338 " )\n"
5339 " *t+0.68976733498510000455\n"
5340 " )\n"
5341 " *t+1.6763848301838038494\n"
5342 " )\n"
5343 " *t+2.05319162663775882187\n"
5344 " )\n"
5345 " *t+1.0\n"
5346 " );\n";
5347 ss<<" }\n";
5348 ss<<" else\n";
5349 ss<<" {\n";
5350 ss<<" t+=-5.0;\n";
5351 ss<<" z=\n"
5352 " (\n"
5353 " (\n"
5354 " (\n"
5355 " (\n"
5356 " (\n"
5357 " (\n"
5358 " (\n"
5359 " t*2.01033439929228813265e-7";
5360 ss<<"+2.71155556874348757815e-5\n"
5361 " )\n"
5362 " *t+0.0012426609473880784386\n"
5363 " )\n"
5364 " *t+0.026532189526576123093\n"
5365 " )\n"
5366 " *t+0.29656057182850489123\n"
5367 " )\n"
5368 " *t+1.7848265399172913358\n"
5369 " )\n"
5370 " *t+5.4637849111641143699\n"
5371 " )\n"
5372 " *t+6.6579046435011037772\n"
5373 " )\n"
5374 " /\n"
5375 " (\n"
5376 " (\n"
5377 " (\n"
5378 " (\n"
5379 " (\n"
5380 " (\n"
5381 " (\n"
5382 " t*2.04426310338993978564e-15"
5383 " +1.4215117583164458887e-7\n"
5384 " )\n"
5385 " *t+1.8463183175100546818e-5\n"
5386 " )\n"
5387 " *t+7.868691311456132591e-4\n"
5388 " )\n"
5389 " *t+0.0148753612908506148525\n"
5390 " )\n"
5391 " *t+0.13692988092273580531\n"
5392 " )\n"
5393 " *t+0.59983220655588793769\n"
5394 " )\n"
5395 " *t+1.0\n"
5396 " );\n";
5397 ss << " }\n";
5398 ss << " z = q < 0.0 ? (-1)*z : z;\n";
5399 ss << " }\n";
5400 ss << " tmp = exp(arg1+arg2*z);\n";
5401 ss << " return tmp;\n";
5402 ss << "}\n";
5405 void OpForecast::GenSlidingWindowFunction(std::stringstream &ss,
5406 const std::string &sSymName, SubArguments &vSubArguments)
5408 FormulaToken *pCur0 = vSubArguments[0]->GetFormulaToken();
5409 assert(pCur0);
5410 const formula::SingleVectorRefToken*pCurDVR0= static_cast<const
5411 formula::SingleVectorRefToken *>(pCur0);
5412 FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
5413 assert(pCur1);
5414 const formula::DoubleVectorRefToken* pCurDVR1 =
5415 static_cast<const formula::DoubleVectorRefToken *>(pCur1);
5416 size_t nCurWindowSize = pCurDVR1->GetRefRowSize();
5417 FormulaToken *pCur2 = vSubArguments[2]->GetFormulaToken();
5418 assert(pCur2);
5419 const formula::DoubleVectorRefToken* pCurDVR2 =
5420 static_cast<const formula::DoubleVectorRefToken *>(pCur2);
5421 size_t nCurWindowSize1 = pCurDVR2->GetRefRowSize();
5422 ss << "\ndouble " << sSymName;
5423 ss << "_"<< BinFuncName() <<"( ";
5424 for (unsigned i = 0; i < vSubArguments.size(); i++)
5426 if (i)
5427 ss << ",";
5428 vSubArguments[i]->GenSlidingWindowDecl(ss);
5430 ss << ") {\n";
5431 ss << " int gid0 = get_global_id(0);\n";
5432 ss << " double fSumX = 0.0;\n";
5433 ss << " double fSumY = 0.0;\n";
5434 ss << " double fSumDeltaXDeltaY = 0.0;\n";
5435 ss << " double fSumSqrDeltaX = 0.0;\n";
5436 if(pCur0->GetType()== formula::svDouble ||
5437 pCur0->GetType() == formula::svSingleVectorRef)
5439 ss << " double arg0 = ";
5440 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
5441 ss << ";\n";
5443 else
5444 ss << "return HUGE_VAL";
5445 if(pCur1->GetType() != formula::svDoubleVectorRef ||
5446 pCur2->GetType() != formula::svDoubleVectorRef)
5447 ss << "return HUGE_VAL";
5448 else
5450 ss<< " if(isNan(arg0)||(gid0>=";
5451 ss<<pCurDVR0->GetArrayLength();
5452 ss<<"))\n";
5453 ss<<" arg0 = 0;\n";
5454 ss << " int length="<<nCurWindowSize;
5455 ss << ";\n";
5456 ss << " int length1= "<<nCurWindowSize1;
5457 ss << ";\n";
5458 ss << " if(length!=length1)\n";
5459 ss << " return 0;\n";
5460 ss << " double tmp = 0;\n";
5461 ss << " for (int i = 0; i <" << nCurWindowSize << "; i++)\n";
5462 ss << " {\n";
5463 ss << " double arg1 = ";
5464 ss << vSubArguments[1]->GenSlidingWindowDeclRef(true);
5465 ss << ";\n";
5466 ss << " double arg2 = ";
5467 ss << vSubArguments[2]->GenSlidingWindowDeclRef(true);
5468 ss << ";\n";
5469 ss << " if(isNan(arg1)||((gid0+i)>=";
5470 ss << pCurDVR1->GetArrayLength();
5471 ss << "))\n";
5472 ss << " {\n";
5473 ss << " length--;\n";
5474 ss << " continue;\n";
5475 ss << " }\n";
5476 ss << " if(isNan(arg2)||((gid0+i)>=";
5477 ss << pCurDVR2->GetArrayLength();
5478 ss << "))\n";
5479 ss << " {\n";
5480 ss << " length--;\n";
5481 ss << " continue;\n";
5482 ss << " }\n";
5483 ss << " fSumY+=arg1;\n";
5484 ss << " fSumX+=arg2;\n";
5485 ss << " }\n";
5486 ss << " double fMeanX = fSumX / length;\n";
5487 ss << " double fMeanY = fSumY / length;\n";
5488 ss << " for (int i = 0; i <" << nCurWindowSize << "; i++)\n";
5489 ss << " {\n";
5490 ss << " double arg1 = ";
5491 ss << vSubArguments[1]->GenSlidingWindowDeclRef(true);
5492 ss << ";\n";
5493 ss << " double arg2 = ";
5494 ss << vSubArguments[2]->GenSlidingWindowDeclRef(true);
5495 ss << ";\n";
5496 ss << " if(isNan(arg1)||((gid0+i)>=";
5497 ss <<pCurDVR1->GetArrayLength();
5498 ss <<"))\n";
5499 ss <<" {\n";
5500 ss <<" continue;\n";
5501 ss <<" }\n";
5502 ss << " if(isNan(arg2)||((gid0+i)>=";
5503 ss <<pCurDVR2->GetArrayLength();
5504 ss <<"))\n";
5505 ss <<" {\n";
5506 ss <<" continue;\n";
5507 ss <<" }\n";
5508 ss <<" fSumDeltaXDeltaY+=(arg2 - fMeanX) * (arg1 - fMeanY);\n";
5509 ss <<" fSumSqrDeltaX+=pow(arg2 - fMeanX, 2);\n";
5510 ss <<" }\n";
5511 ss <<" tmp =fMeanY + fSumDeltaXDeltaY / fSumSqrDeltaX *";
5512 ss <<" (arg0 - fMeanX);\n";
5513 ss <<" return tmp;\n";
5514 ss << "}";
5517 void OpLogNormDist::GenSlidingWindowFunction(std::stringstream &ss,
5518 const std::string &sSymName, SubArguments &vSubArguments)
5520 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
5521 const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
5522 formula::SingleVectorRefToken *>(tmpCur0);
5523 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
5524 const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
5525 formula::SingleVectorRefToken *>(tmpCur1);
5526 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
5527 const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
5528 formula::SingleVectorRefToken *>(tmpCur2);
5529 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
5530 const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
5531 formula::SingleVectorRefToken *>(tmpCur3);
5532 ss << "\ndouble " << sSymName;
5533 ss << "_"<< BinFuncName() <<"(";
5534 for (unsigned i = 0; i < vSubArguments.size(); i++)
5536 if (i)
5537 ss << ",";
5538 vSubArguments[i]->GenSlidingWindowDecl(ss);
5540 ss << ") {\n";
5541 ss << " int gid0=get_global_id(0);\n";
5542 ss << " double arg0,arg1,arg2,arg3;\n";
5543 size_t i = vSubArguments.size();
5544 for (i = 0; i < vSubArguments.size(); i++)
5546 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
5547 assert(pCur);
5548 if (pCur->GetType() == formula::svDoubleVectorRef)
5550 const formula::DoubleVectorRefToken* pDVR =
5551 static_cast<const formula::DoubleVectorRefToken *>(pCur);
5552 size_t nCurWindowSize = pDVR->GetRefRowSize();
5553 ss << "for (int i = ";
5554 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
5555 ss << "gid0; i < " << pDVR->GetArrayLength();
5556 ss << " && i < " << nCurWindowSize << "; i++){\n";
5557 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
5558 ss << "0; i < " << pDVR->GetArrayLength();
5559 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
5560 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
5561 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
5562 ss << " && i < "<< nCurWindowSize << "; i++){\n ";
5564 else {
5565 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
5568 else if (pCur->GetType() == formula::svSingleVectorRef)
5570 const formula::SingleVectorRefToken* pSVR =
5571 static_cast< const formula::SingleVectorRefToken* >(pCur);
5572 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
5573 ss << " {\n";
5574 ss << " if (isNan(";
5575 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
5576 ss << "))\n";
5577 ss << " arg"<<i<<"= 0;\n";
5578 ss << " else\n";
5579 ss << " arg"<<i<<"=";
5580 ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
5581 ss << ";\n";
5582 ss << " }\n";
5583 ss << " else\n";
5584 ss << " arg"<<i<<"= 0;\n";
5586 else if (pCur->GetType() == formula::svDouble)
5588 ss << " if (isNan(";
5589 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
5590 ss << "))\n";
5591 ss << " arg"<<i<<"= 0;\n";
5592 ss << " else\n";
5593 ss << " arg"<<i<<"=";
5594 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
5595 ss << ";\n";
5598 ss << " double tmp;\n";
5599 ss << " if(isNan(arg0)||(gid0>=";
5600 ss << tmpCurDVR0->GetArrayLength();
5601 ss << "))\n";
5602 ss << " arg0 = 0;\n";
5603 ss << " if(isNan(arg1)||(gid0>=";
5604 ss << tmpCurDVR1->GetArrayLength();
5605 ss << "))\n";
5606 ss << " arg1 = 0;\n";
5607 ss << " if(isNan(arg2)||(gid0>=";
5608 ss << tmpCurDVR2->GetArrayLength();
5609 ss << "))\n";
5610 ss << " arg2 = 0;\n";
5611 ss << " if(isNan(arg3)||(gid0>=";
5612 ss << tmpCurDVR3->GetArrayLength();
5613 ss << "))\n";
5614 ss << " arg3 = 0;\n";
5615 ss << " double temp = (log(arg0)-arg1)/arg2;\n";
5616 ss << " if(arg3)\n";
5617 ss << " {\n";
5618 ss << " if(arg0<=0)\n";
5619 ss << " tmp = 0.0;\n";
5620 ss << " else\n";
5621 ss << " tmp = 0.5 * erfc(-temp * 0.7071067811865475);\n";
5622 ss << " }\n";
5623 ss << " else\n";
5624 ss << " tmp = (0.39894228040143268 * exp((-1)*pow(temp, 2)";
5625 ss << " / 2.0))/(arg2*arg0);\n";
5626 ss << " return tmp;\n";
5627 ss << "}\n";
5630 void OpGammaDist::BinInlineFun(std::set<std::string>& decls,
5631 std::set<std::string>& funs)
5633 decls.insert(fBigInvDecl);decls.insert(fLogDblMaxDecl);
5634 decls.insert(fHalfMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
5635 decls.insert(GetGammaSeriesDecl);decls.insert(GetGammaContFractionDecl);
5636 decls.insert(GetLowRegIGammaDecl);decls.insert(GetGammaDistDecl);
5637 decls.insert(GetGammaDistPDFDecl);
5638 funs.insert(GetGammaSeries);funs.insert(GetGammaContFraction);
5639 funs.insert(GetLowRegIGamma);funs.insert(GetGammaDist);
5640 funs.insert(GetGammaDistPDF);
5643 void OpGammaDist::GenSlidingWindowFunction(std::stringstream &ss,
5644 const std::string &sSymName, SubArguments &vSubArguments)
5646 ss << "\ndouble " << sSymName;
5647 ss << "_"<< BinFuncName() <<"(";
5648 for (unsigned i = 0; i < vSubArguments.size(); i++)
5650 if (i)
5651 ss << ",";
5652 vSubArguments[i]->GenSlidingWindowDecl(ss);
5654 ss << ") {\n";
5655 ss << " int gid0=get_global_id(0);\n";
5656 ss << " double tmp;\n";
5657 ss << " double arg0,arg1,arg2,arg3;\n";
5658 size_t i = vSubArguments.size();
5660 for (i = 0; i < vSubArguments.size(); i++)
5662 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
5663 assert(pCur);
5664 if (pCur->GetType() == formula::svDoubleVectorRef)
5666 const formula::DoubleVectorRefToken* pDVR =
5667 static_cast<const formula::DoubleVectorRefToken *>(pCur);
5668 size_t nCurWindowSize = pDVR->GetRefRowSize();
5669 ss << "for (int i = ";
5670 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
5671 ss << "gid0; i < " << pDVR->GetArrayLength();
5672 ss << " && i < " << nCurWindowSize << "; i++){\n";
5673 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
5674 ss << "0; i < " << pDVR->GetArrayLength();
5675 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
5676 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
5677 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
5678 ss << " && i < "<< nCurWindowSize << "; i++){\n ";
5680 else {
5681 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
5684 else if (pCur->GetType() == formula::svSingleVectorRef)
5686 const formula::SingleVectorRefToken* pSVR =
5687 static_cast< const formula::SingleVectorRefToken* >(pCur);
5688 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
5689 ss << " {\n";
5690 ss << " if (isNan(";
5691 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
5692 ss << "))\n";
5693 ss << " arg"<<i<<"= 0;\n";
5694 ss << " else\n";
5695 ss << " arg"<<i<<"=";
5696 ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
5697 ss << ";\n";
5698 ss << " }\n";
5699 ss << " else\n";
5700 ss << " arg"<<i<<"= 0;\n";
5702 else if (pCur->GetType() == formula::svDouble)
5704 ss << " if (isNan(";
5705 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
5706 ss << "))\n";
5707 ss << " arg"<<i<<"= 0;\n";
5708 ss << " else\n";
5709 ss << " arg"<<i<<"=";
5710 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
5711 ss << ";\n";
5714 ss << " if (arg3)\n";
5715 ss << " tmp=GetGammaDist( arg0, arg1, arg2);\n";
5716 ss << " else\n";
5717 ss << " tmp=GetGammaDistPDF( arg0, arg1, arg2);\n";
5718 ss << " return tmp;\n";
5719 ss << "}\n";
5721 void OpChiDist::BinInlineFun(std::set<std::string>& decls,
5722 std::set<std::string>& funs)
5724 decls.insert(fBigInvDecl);
5725 funs.insert("");
5726 decls.insert(fHalfMachEpsDecl);
5727 funs.insert("");
5728 decls.insert(GetUpRegIGammaDecl);
5729 funs.insert(GetUpRegIGamma);
5730 decls.insert(GetGammaSeriesDecl);
5731 funs.insert(GetGammaSeries);
5732 decls.insert(GetGammaContFractionDecl);
5733 funs.insert(GetGammaContFraction);
5734 decls.insert(GetChiDistDecl);
5735 funs.insert(GetChiDist);
5737 void OpChiDist::GenSlidingWindowFunction(
5738 std::stringstream &ss,const std::string &sSymName,
5739 SubArguments &vSubArguments)
5741 ss << "\ndouble " << sSymName;
5742 ss << "_"<< BinFuncName() <<"(";
5743 for (unsigned i = 0; i < vSubArguments.size(); i++)
5745 if (i)
5746 ss << ",";
5747 vSubArguments[i]->GenSlidingWindowDecl(ss);
5749 ss << ")\n";
5750 ss << "{\n";
5751 ss << " double fx,fDF,tmp=0,tmp0=0,tmp1=0;\n";
5752 ss << " int gid0=get_global_id(0);\n";
5754 size_t i = vSubArguments.size();
5755 ss <<"\n";
5756 for (i = 0; i < vSubArguments.size(); i++)
5758 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
5759 assert(pCur);
5760 if (pCur->GetType() == formula::svDoubleVectorRef)
5762 const formula::DoubleVectorRefToken* pDVR =
5763 static_cast<const formula::DoubleVectorRefToken *>(pCur);
5764 size_t nCurWindowSize = pDVR->GetRefRowSize();
5765 ss << "for (int i = ";
5766 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
5768 ss << "gid0; i < " << pDVR->GetArrayLength();
5769 ss << " && i < " << nCurWindowSize << "; i++){\n";
5771 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
5773 ss << "0; i < " << pDVR->GetArrayLength();
5774 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
5776 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
5778 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
5779 ss << " && i < "<< nCurWindowSize << "; i++){\n";
5781 else
5783 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
5786 else if (pCur->GetType() == formula::svSingleVectorRef)
5788 const formula::SingleVectorRefToken* pSVR =
5789 static_cast< const formula::SingleVectorRefToken* >(pCur);
5790 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
5792 else if (pCur->GetType() == formula::svDouble)
5794 ss << "{\n";
5796 else
5799 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
5801 ss << " if (isNan(";
5802 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
5803 ss << "))\n";
5804 ss << " tmp"<<i<<"= 0;\n";
5805 ss << " else\n";
5806 ss << " tmp"<<i<<"=\n";
5807 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
5808 ss << ";\n}\n";
5811 ss << " fx = tmp0;\n";
5812 ss << " fDF = floor(tmp1);\n";
5813 ss << " if(fDF < 1.0)\n";
5814 ss << " {\n";
5815 ss << " return DBL_MIN;\n";
5816 ss << " }\n";
5817 ss << " tmp = GetChiDist( fx, fDF);\n";
5818 ss << " return tmp;\n";
5819 ss << "}\n";
5821 void OpBinomdist::BinInlineFun(std::set<std::string>& decls,
5822 std::set<std::string>& funs)
5824 decls.insert(fMachEpsDecl);
5825 funs.insert("");
5826 decls.insert(MinDecl);
5827 funs.insert("");
5828 decls.insert(fMaxGammaArgumentDecl);
5829 funs.insert("");
5830 decls.insert(GetBinomDistPMFDecl);
5831 funs.insert(GetBinomDistPMF);
5832 decls.insert(GetBetaDistDecl);
5833 funs.insert(GetBetaDist);
5834 decls.insert(lcl_GetBinomDistRangeDecl);
5835 funs.insert(lcl_GetBinomDistRange);
5836 decls.insert(lcl_GetBetaHelperContFracDecl);
5837 funs.insert(lcl_GetBetaHelperContFrac);
5838 decls.insert(GetBetaDistPDFDecl);
5839 funs.insert(GetBetaDistPDF);
5840 decls.insert(GetLogBetaDecl);
5841 funs.insert(GetLogBeta);
5842 decls.insert(GetBetaDecl);
5843 funs.insert(GetBeta);
5844 decls.insert(lcl_getLanczosSumDecl);
5845 funs.insert(lcl_getLanczosSum);
5847 void OpBinomdist::GenSlidingWindowFunction(
5848 std::stringstream &ss,const std::string &sSymName,
5849 SubArguments &vSubArguments)
5851 ss << "\ndouble " << sSymName;
5852 ss << "_"<< BinFuncName() <<"(";
5853 for (unsigned i = 0; i < vSubArguments.size(); i++)
5855 if (i)
5856 ss << ",";
5857 vSubArguments[i]->GenSlidingWindowDecl(ss);
5859 ss << ")\n";
5860 ss << "{\n";
5861 ss << " double tmp0,tmp1,tmp2,tmp3;\n";
5862 ss << " int gid0=get_global_id(0);\n";
5863 size_t i = vSubArguments.size();
5865 ss <<"\n ";
5866 //while (i-- > 1)
5867 for (i = 0; i < vSubArguments.size(); i++)
5869 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
5870 assert(pCur);
5871 if (pCur->GetType() == formula::svDoubleVectorRef)
5873 const formula::DoubleVectorRefToken* pDVR =
5874 static_cast<const formula::DoubleVectorRefToken *>(pCur);
5875 size_t nCurWindowSize = pDVR->GetRefRowSize();
5876 ss << "for (int i = ";
5877 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
5879 ss << "gid0; i < " << pDVR->GetArrayLength();
5880 ss << " && i < " << nCurWindowSize << "; i++){\n";
5882 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
5884 ss << "0; i < " << pDVR->GetArrayLength();
5885 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
5887 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
5889 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
5890 ss << " && i < "<< nCurWindowSize << "; i++){\n";
5892 else
5894 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
5897 else if (pCur->GetType() == formula::svSingleVectorRef)
5899 const formula::SingleVectorRefToken* pSVR =
5900 static_cast< const formula::SingleVectorRefToken* >(pCur);
5901 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
5903 else if (pCur->GetType() == formula::svDouble)
5905 ss << "{\n";
5907 else
5910 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
5912 ss << " if (isNan(";
5913 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
5914 ss << "))\n";
5915 ss << " tmp"<<i<<"= 0;\n";
5916 ss << " else\n";
5917 ss << " tmp"<<i<<"=\n";
5918 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
5919 ss << ";\n}\n";
5921 else
5923 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
5924 ss <<";\n";
5927 ss << " tmp0 = floor(tmp0);\n";
5928 ss << " tmp1 = floor(tmp1);\n";
5929 ss << " double rq = (0.5 - tmp2) + 0.5;\n";
5930 ss << " if (tmp1 < 0.0 || tmp0 < 0.0 || tmp0 > tmp1 ||";
5931 ss << "tmp2 < 0.0 || tmp2 > 1.0)\n";
5932 ss << " {\n";
5933 ss << " return DBL_MIN;\n";
5934 ss << " }\n";
5935 ss << " if(tmp2 == 0.0)\n";
5936 ss << " return ( (tmp0 == 0.0 || tmp3) ? 1.0 : 0.0 );\n";
5937 ss << " if(tmp2 == 1.0)\n";
5938 ss << " return ( (tmp0 == tmp1) ? 1.0 : 0.0);\n";
5939 ss << " if(!tmp3)\n";
5940 ss << " return ( GetBinomDistPMF(tmp0, tmp1, tmp2));\n";
5941 ss << " else \n";
5942 ss << " {\n";
5943 ss << " if(tmp0 == tmp1)\n";
5944 ss << " return 1.0;\n";
5945 ss << " else\n";
5946 ss << " {\n";
5947 ss << " double fFactor = pow(rq,tmp1);\n";
5948 ss << " if(tmp0 == 0.0)\n";
5949 ss << " return (fFactor);\n";
5950 ss << " else if(fFactor <= Min)\n";
5951 ss << " {\n";
5952 ss << " fFactor = pow(tmp2,tmp1);\n";
5953 ss << " if(fFactor <= Min)\n";
5954 ss << " return GetBetaDist";
5955 ss << "(rq, tmp1 - tmp0, tmp0 + 1.0);\n";
5956 ss << " else\n";
5957 ss << " {\n";
5958 ss << " if(fFactor > fMachEps)\n";
5959 ss << " {\n";
5960 ss << " double fSum = 1.0 - fFactor;\n";
5961 ss << " unsigned int max = ";
5962 ss << "(unsigned int)((tmp1 - tmp0)-1);\n";
5963 ss << " for (uint i = 0; i < max && fFactor > 0.0;";
5964 ss << " i++)\n";
5965 ss << " {\n";
5966 ss << " fFactor *= (tmp1 - i)*pow((i + 1),-1.0)*";
5967 ss << "rq*pow(tmp2,-1.0);\n";
5968 ss << " fSum -= fFactor;\n";
5969 ss << " }\n";
5970 ss << " return ( (fSum < 0.0) ? 0.0 : fSum );\n";
5971 ss << " }\n";
5972 ss << " else \n";
5973 ss << " return (lcl_GetBinomDistRange";
5974 ss << "(tmp1, tmp1 - tmp0, tmp1, fFactor, rq, tmp2));\n";
5975 ss << " }\n";
5976 ss << " }\n";
5977 ss << " else\n";
5978 ss << " {\n";
5979 ss << " double rtmp = ( lcl_GetBinomDistRange";
5980 ss << "(tmp1, 0.0, tmp0, fFactor, tmp2, rq));\n";
5981 ss << " return rtmp;\n";
5982 ss << " }\n";
5983 ss << " }\n";
5984 ss << " }\n";
5985 ss << "}\n";
5988 void OpChiSqDist::BinInlineFun(std::set<std::string>& decls,
5989 std::set<std::string>& funs)
5991 decls.insert(fMaxGammaArgumentDecl);decls.insert(GetChiSqDistCDFDecl);
5992 decls.insert(GetChiSqDistPDFDecl);decls.insert(GetLowRegIGammaDecl);
5993 decls.insert(GetGammaContFractionDecl);decls.insert(GetGammaSeriesDecl);
5994 decls.insert(fHalfMachEpsDecl);decls.insert(F_PIDecl);
5995 decls.insert(fBigInvDecl);
5997 funs.insert(GetGammaContFraction);funs.insert(GetChiSqDistCDF);
5998 funs.insert(GetChiSqDistPDF);funs.insert(GetLowRegIGamma);
5999 funs.insert(GetGammaSeries);
6002 void OpChiSqDist::GenSlidingWindowFunction(
6003 std::stringstream &ss, const std::string &sSymName, SubArguments &
6004 vSubArguments)
6006 ss << "\ndouble " << sSymName;
6007 ss << "_"<< BinFuncName() <<"(";
6008 for (unsigned i = 0; i < vSubArguments.size(); i++)
6010 if (i)
6011 ss << ",";
6012 vSubArguments[i]->GenSlidingWindowDecl(ss);
6014 ss << ") {\n";
6015 ss << " int gid0 = get_global_id(0);\n";
6016 ss << " int singleIndex = gid0;\n";
6017 ss << " double result = 0;\n";
6018 if(vSubArguments.size()<2)
6020 ss << " result = -DBL_MAX;\n";
6021 ss << " return result;\n";
6022 }else
6024 GenTmpVariables(ss,vSubArguments);
6025 CheckAllSubArgumentIsNan(ss,vSubArguments);
6026 if(vSubArguments.size() == 2)
6028 ss << " int tmp2 = 1;\n";
6031 size_t i = vSubArguments.size();
6032 ss <<"\n";
6033 for (i = 0; i < vSubArguments.size(); i++)
6035 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
6036 assert(pCur);
6037 if (pCur->GetType() == formula::svSingleVectorRef)
6039 const formula::SingleVectorRefToken* pSVR =
6040 static_cast< const formula::SingleVectorRefToken* >(pCur);
6041 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
6043 else if (pCur->GetType() == formula::svDouble)
6045 ss << "{\n";
6048 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
6050 ss << " if (isNan(";
6051 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6052 ss << "))\n";
6053 ss << " tmp"<<i<<"= 0;\n";
6054 ss << " else\n";
6055 ss << " tmp"<<i<<"=\n";
6056 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6057 ss << ";\n}\n";
6059 else
6061 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
6062 ss <<";\n";
6065 ss << " tmp1 = floor(tmp1);\n";
6066 ss << " if(tmp1 < 1.0)\n";
6067 ss << " result = -DBL_MAX;\n";
6068 ss << " else\n";
6069 ss << " {\n";
6070 ss << " if(tmp2)\n";
6071 ss << " result =GetChiSqDistCDF(tmp0,tmp1);\n";
6072 ss << " else\n";
6073 ss << " result =GetChiSqDistPDF(tmp0,tmp1);\n";
6074 ss << " }\n";
6075 ss << " return result;\n";
6076 ss << "}";
6079 void OpChiSqInv::BinInlineFun(std::set<std::string>& decls,
6080 std::set<std::string>& funs)
6082 decls.insert(fMaxGammaArgumentDecl);decls.insert(GetChiSqDistCDFDecl);
6083 decls.insert(GetLowRegIGammaDecl);decls.insert(lcl_IterateInverseChiSQInvDecl);
6084 decls.insert(GetGammaContFractionDecl);decls.insert(GetGammaSeriesDecl);
6085 decls.insert(fHalfMachEpsDecl);decls.insert(F_PIDecl);
6086 decls.insert(fBigInvDecl);decls.insert(lcl_HasChangeOfSignDecl);
6087 decls.insert(fMachEpsDecl);
6089 funs.insert(GetGammaContFraction);funs.insert(GetChiSqDistCDF);
6090 funs.insert(GetLowRegIGamma);funs.insert(lcl_HasChangeOfSign);
6091 funs.insert(GetGammaSeries);funs.insert(lcl_IterateInverseChiSQInv);
6094 void OpChiSqInv::GenSlidingWindowFunction(
6095 std::stringstream &ss, const std::string &sSymName, SubArguments &
6096 vSubArguments)
6098 ss << "\ndouble " << sSymName;
6099 ss << "_"<< BinFuncName() <<"(";
6100 for (unsigned i = 0; i < vSubArguments.size(); i++)
6102 if (i)
6103 ss << ",";
6104 vSubArguments[i]->GenSlidingWindowDecl(ss);
6106 ss << ") {\n";
6107 ss << " int gid0 = get_global_id(0);\n";
6108 ss << " int singleIndex = gid0;\n";
6109 ss << " double result = 0;\n";
6110 if(vSubArguments.size()!=2)
6112 ss << " result = -DBL_MAX;\n";
6113 ss << " return result;\n";
6115 else
6117 GenTmpVariables(ss,vSubArguments);
6118 CheckAllSubArgumentIsNan(ss,vSubArguments);
6119 size_t i = vSubArguments.size();
6120 ss <<"\n";
6121 for (i = 0; i < vSubArguments.size(); i++)
6123 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
6124 assert(pCur);
6125 if (pCur->GetType() == formula::svSingleVectorRef)
6127 const formula::SingleVectorRefToken* pSVR =
6128 static_cast< const formula::SingleVectorRefToken* >(pCur);
6129 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
6131 else if (pCur->GetType() == formula::svDouble)
6133 ss << "{\n";
6136 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
6138 ss << " if (isNan(";
6139 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6140 ss << "))\n";
6141 ss << " tmp"<<i<<"= 0;\n";
6142 ss << " else\n";
6143 ss << " tmp"<<i<<"=\n";
6144 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6145 ss << ";\n}\n";
6147 else
6149 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
6150 ss <<";\n";
6153 ss << " tmp1 = floor(tmp1);\n";
6154 ss << " bool bConvError;\n";
6155 ss << " if(tmp1 < 1.0 || tmp0 < 0 || tmp0>=1.0)\n";
6156 ss << " result = -DBL_MAX;\n";
6157 ss << " else\n";
6158 ss << " {\n";
6159 ss << " result =lcl_IterateInverseChiSQInv( tmp0, tmp1,";
6160 ss << "tmp1*0.5, tmp1, &bConvError );\n";
6161 ss << " }\n";
6162 ss << " if(bConvError)\n";
6163 ss << " result = -DBL_MAX;\n";
6164 ss << " return result;\n";
6165 ss << "}";
6169 void OpGammaInv::BinInlineFun(std::set<std::string>& decls,
6170 std::set<std::string>& funs)
6172 decls.insert(fBigInvDecl);decls.insert(fHalfMachEpsDecl);
6173 decls.insert(GetGammaSeriesDecl);decls.insert(GetGammaContFractionDecl);
6174 decls.insert(GetGammaInvValueDecl);
6175 funs.insert(GetGammaSeries);funs.insert(GetGammaContFraction);
6176 funs.insert(GetGammaInvValue);
6179 void OpGammaInv::GenSlidingWindowFunction(std::stringstream &ss,
6180 const std::string &sSymName, SubArguments &vSubArguments)
6182 ss << "\ndouble " << sSymName;
6183 ss << "_"<< BinFuncName() <<"(";
6184 for (unsigned i = 0; i < vSubArguments.size(); i++)
6186 if (i)
6187 ss << ",";
6188 vSubArguments[i]->GenSlidingWindowDecl(ss);
6190 ss << ") {\n";
6191 ss << " int gid0=get_global_id(0);\n";
6192 ss << " double tmp;\n";
6193 ss << " double arg0,arg1,arg2;\n";
6194 size_t i = vSubArguments.size();
6196 for (i = 0; i < vSubArguments.size(); i++)
6198 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
6199 assert(pCur);
6200 if (pCur->GetType() == formula::svDoubleVectorRef)
6202 const formula::DoubleVectorRefToken* pDVR =
6203 static_cast<const formula::DoubleVectorRefToken *>(pCur);
6204 size_t nCurWindowSize = pDVR->GetRefRowSize();
6205 ss << "for (int i = ";
6206 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
6207 ss << "gid0; i < " << pDVR->GetArrayLength();
6208 ss << " && i < " << nCurWindowSize << "; i++){\n";
6209 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
6210 ss << "0; i < " << pDVR->GetArrayLength();
6211 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
6212 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
6213 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
6214 ss << " && i < "<< nCurWindowSize << "; i++){\n ";
6216 else {
6217 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
6220 else if (pCur->GetType() == formula::svSingleVectorRef)
6222 const formula::SingleVectorRefToken* pSVR =
6223 static_cast< const formula::SingleVectorRefToken* >(pCur);
6224 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
6225 ss << " {\n";
6226 ss << " if (isNan(";
6227 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6228 ss << "))\n";
6229 ss << " arg"<<i<<"= 0;\n";
6230 ss << " else\n";
6231 ss << " arg"<<i<<"=";
6232 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
6233 ss << ";\n";
6234 ss << " }\n";
6235 ss << " else\n";
6236 ss << " arg"<<i<<"= 0;\n";
6238 else if (pCur->GetType() == formula::svDouble)
6240 ss << " if (isNan(";
6241 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6242 ss << "))\n";
6243 ss << " arg"<<i<<"= 0;\n";
6244 ss << " else\n";
6245 ss << " arg"<<i<<"=";
6246 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
6247 ss << ";\n";
6250 ss << " if (arg0 == 0.0)\n"
6251 " {\n"
6252 " tmp=0.0;\n"
6253 " return tmp;\n"
6254 " }\n"
6255 " else\n"
6256 " {\n"
6257 " bool bConvError;\n"
6258 " double fStart = arg1 * arg2;\n"
6259 " double fAx=fStart*0.5;\n"
6260 " double fBx=fStart;\n"
6261 " bConvError = false;\n"
6262 " double fYEps = 1.0E-307;\n"
6263 " double fXEps = 2.22045e-016;\n"
6264 " double fAy = arg0-GetGammaInvValue(arg1,arg2,fAx);\n"
6265 " double fBy = arg0-GetGammaInvValue(arg1,arg2,fBx);\n"
6266 " double fTemp;\n"
6267 " unsigned short nCount;\n"
6268 " for (nCount = 0; nCount < 1000 && !((fAy < 0.0 && fBy > 0.0)"
6269 " || (fAy > 0.0 && fBy < 0.0)); nCount++)\n"
6270 " {\n"
6271 " if (fabs(fAy) <= fabs(fBy))\n"
6272 " {\n"
6273 " fTemp = fAx;\n"
6274 " fAx += 2.0 * (fAx - fBx);\n"
6275 " if (fAx < 0.0)\n"
6276 " fAx = 0.0;\n"
6277 " fBx = fTemp;\n"
6278 " fBy = fAy;\n"
6279 " fAy = arg0-GetGammaInvValue(arg1,arg2,fAx);\n"
6280 " }\n"
6281 " else\n"
6282 " {\n"
6283 " fTemp = fBx;\n"
6284 " fBx += 2.0 * (fBx - fAx);\n"
6285 " fAx = fTemp;\n"
6286 " fAy = fBy;\n"
6287 " fBy = arg0-GetGammaInvValue(arg1,arg2,fBx);\n"
6288 " }\n"
6289 " }\n"
6290 " if (fAy == 0.0)\n"
6291 " {\n"
6292 " tmp = fAx;\n"
6293 " return tmp;\n"
6294 " }\n"
6295 " if (fBy == 0.0)\n"
6296 " {\n"
6297 " tmp = fBx;\n"
6298 " return tmp;\n"
6299 " }\n"
6300 " if (!((fAy < 0.0 && fBy > 0.0) || (fAy > 0.0 && fBy < 0.0)))\n"
6301 " {\n"
6302 " bConvError = true;\n"
6303 " tmp = 0.0;\n"
6304 " return tmp;\n"
6305 " }\n"
6306 " double fPx = fAx;\n"
6307 " double fPy = fAy;\n"
6308 " double fQx = fBx;\n"
6309 " double fQy = fBy;\n"
6310 " double fRx = fAx;\n"
6311 " double fRy = fAy;\n"
6312 " double fSx = 0.5 * (fAx + fBx);\n"
6313 " bool bHasToInterpolate = true;\n"
6314 " nCount = 0;\n"
6315 " while ( nCount < 500 && fabs(fRy) > fYEps &&"
6316 "(fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
6317 " {\n"
6318 " if (bHasToInterpolate)\n"
6319 " {\n"
6320 " if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
6321 " {\n"
6322 " fSx = fPx * fRy * fQy *pow( (fRy-fPy),-1) *pow"
6323 "( (fQy-fPy),-1)"
6324 "+ fRx * fQy * fPy *pow( (fQy-fRy),-1) *pow( (fPy-fRy),-1)"
6325 "+ fQx * fPy * fRy *pow( (fPy-fQy),-1) *pow( (fRy-fQy),-1);\n"
6326 " bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
6327 " }\n"
6328 " else\n"
6329 " bHasToInterpolate = false;\n"
6330 " }\n"
6331 " if(!bHasToInterpolate)\n"
6332 " {\n"
6333 " fSx = 0.5 * (fAx + fBx);\n"
6334 " fPx = fAx; fPy = fAy;\n"
6335 " fQx = fBx; fQy = fBy;\n"
6336 " bHasToInterpolate = true;\n"
6337 " }\n"
6338 " fPx = fQx; fQx = fRx; fRx = fSx;\n"
6339 " fPy = fQy; fQy = fRy;\n"
6340 " fRy = arg0-GetGammaInvValue(arg1,arg2,fSx);\n"
6341 " if ((fAy < 0.0 && fRy > 0.0) || (fAy > 0.0 && fRy < 0.0))\n"
6342 " {\n"
6343 " fBx = fRx;\n"
6344 " fBy = fRy;\n"
6345 " }\n"
6346 " else\n"
6347 " {\n"
6348 " fAx = fRx;\n"
6349 " fAy = fRy;\n"
6350 " }\n"
6351 " bHasToInterpolate = bHasToInterpolate && (fabs(fRy)"
6352 " * 2.0 <= fabs(fQy));\n"
6353 " ++nCount;\n"
6354 " }\n"
6355 " tmp = fRx;\n"
6356 " return tmp;\n"
6357 " }\n"
6358 "}\n";
6360 void OpFInv::BinInlineFun(std::set<std::string>& decls,
6361 std::set<std::string>& funs)
6363 decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
6364 decls.insert(lcl_getLanczosSumDecl);decls.insert(GetBetaDecl);
6365 decls.insert(GetLogBetaDecl);decls.insert(GetBetaDistPDFDecl);
6366 decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetFInvValueDecl);
6367 funs.insert(lcl_getLanczosSum);funs.insert(GetBeta);
6368 funs.insert(GetLogBeta);funs.insert(GetBetaDistPDF);
6369 funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetFInvValue);
6372 void OpFInv::GenSlidingWindowFunction(std::stringstream &ss,
6373 const std::string &sSymName, SubArguments &vSubArguments)
6375 ss << "\ndouble " << sSymName;
6376 ss << "_"<< BinFuncName() <<"(";
6377 for (unsigned i = 0; i < vSubArguments.size(); i++)
6379 if (i)
6380 ss << ",";
6381 vSubArguments[i]->GenSlidingWindowDecl(ss);
6383 ss << ") {\n";
6384 ss << " int gid0=get_global_id(0);\n";
6385 ss << " double tmp;\n";
6386 ss << " double arg0,arg1,arg2;\n";
6387 size_t i = vSubArguments.size();
6389 for (i = 0; i < vSubArguments.size(); i++)
6391 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
6392 assert(pCur);
6393 if (pCur->GetType() == formula::svDoubleVectorRef)
6395 const formula::DoubleVectorRefToken* pDVR =
6396 static_cast<const formula::DoubleVectorRefToken *>(pCur);
6397 size_t nCurWindowSize = pDVR->GetRefRowSize();
6398 ss << "for (int i = ";
6399 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
6400 ss << "gid0; i < " << pDVR->GetArrayLength();
6401 ss << " && i < " << nCurWindowSize << "; i++){\n";
6402 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
6403 ss << "0; i < " << pDVR->GetArrayLength();
6404 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
6405 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
6406 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
6407 ss << " && i < "<< nCurWindowSize << "; i++){\n ";
6409 else {
6410 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
6413 else if (pCur->GetType() == formula::svSingleVectorRef)
6415 const formula::SingleVectorRefToken* pSVR =
6416 static_cast< const formula::SingleVectorRefToken* >(pCur);
6417 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
6418 ss << " {\n";
6419 ss << " if (isNan(";
6420 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6421 ss << "))\n";
6422 ss << " arg"<<i<<"= 0;\n";
6423 ss << " else\n";
6424 ss << " arg"<<i<<"="<<vSubArguments[i]->
6425 GenSlidingWindowDeclRef();
6426 ss << ";\n";
6427 ss << " }\n";
6428 ss << " else\n";
6429 ss << " arg"<<i<<"= 0;\n";
6431 else if (pCur->GetType() == formula::svDouble)
6433 ss << " if (isNan(";
6434 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6435 ss << "))\n";
6436 ss << " arg"<<i<<"= 0;\n";
6437 ss << " else\n";
6438 ss << " arg"<<i<<"="<<vSubArguments[i]->
6439 GenSlidingWindowDeclRef();
6440 ss << ";\n";
6443 ss << " double fF2=floor(arg2);\n"
6444 " double fF1=floor(arg1);\n"
6445 " bool bConvError;\n"
6446 " double fAx=fF1*0.5;\n"
6447 " double fBx=fF1;\n"
6448 " bConvError = false;\n"
6449 " const double fYEps = 1.0E-307;\n"
6450 " const double fXEps = 2.22045e-016;\n"
6451 " double fAy = arg0-GetFInvValue(fF1,fF2,fAx);\n"
6452 " double fBy = arg0-GetFInvValue(fF1,fF2,fBx);\n"
6453 " double fTemp;\n"
6454 " unsigned short nCount;\n"
6455 " for (nCount = 0; nCount < 1000 && !((fAy < 0.0 && fBy > 0.0)"
6456 " || (fAy > 0.0 && fBy < 0.0)); nCount++)\n"
6457 " {\n"
6458 " if (fabs(fAy) <= fabs(fBy))\n"
6459 " {\n"
6460 " fTemp = fAx;\n"
6461 " fAx += 2.0 * (fAx - fBx);\n"
6462 " if (fAx < 0.0)\n"
6463 " fAx = 0.0;\n"
6464 " fBx = fTemp;\n"
6465 " fBy = fAy;\n"
6466 " fAy = arg0-GetFInvValue(fF1,fF2,fAx);\n"
6467 " }\n"
6468 " else\n"
6469 " {\n"
6470 " fTemp = fBx;\n"
6471 " fBx += 2.0 * (fBx - fAx);\n"
6472 " fAx = fTemp;\n"
6473 " fAy = fBy;\n"
6474 " fBy = arg0-GetFInvValue(fF1,fF2,fBx);\n"
6475 " }\n"
6476 " }\n"
6477 " if (fAy == 0.0)\n"
6478 " {\n"
6479 " tmp = fAx;\n"
6480 " return tmp;\n"
6481 " }\n"
6482 " if (fBy == 0.0)\n"
6483 " {\n"
6484 " tmp = fBx;\n"
6485 " return tmp;\n"
6486 " }\n"
6487 " if (!((fAy < 0.0 && fBy > 0.0) || (fAy > 0.0 && fBy < 0.0)))\n"
6488 " {\n"
6489 " bConvError = true;\n"
6490 " tmp = 0.0;\n"
6491 " return tmp;\n"
6492 " }\n"
6493 " double fPx = fAx;\n"
6494 " double fPy = fAy;\n"
6495 " double fQx = fBx;\n"
6496 " double fQy = fBy;\n"
6497 " double fRx = fAx;\n"
6498 " double fRy = fAy;\n"
6499 " double fSx = 0.5 * (fAx + fBx);\n"
6500 " bool bHasToInterpolate = true;\n"
6501 " nCount = 0;\n"
6502 " while ( nCount < 500 && fabs(fRy) > fYEps &&"
6503 "(fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
6504 " {\n"
6505 " if (bHasToInterpolate)\n"
6506 " {\n"
6507 " if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
6508 " {\n"
6509 " fSx = fPx * fRy * fQy *pow( (fRy-fPy),-1)"
6510 " *pow( (fQy-fPy),-1)+fRx * fQy * fPy*pow( (fQy-fRy),-1) *"
6511 "pow( (fPy-fRy),-1)+ fQx * fPy * fRy *pow( (fPy-fQy),-1)"
6512 " *pow((fRy-fQy),-1);\n"
6513 " bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
6514 " }\n"
6515 " else\n"
6516 " bHasToInterpolate = false;\n"
6517 " }\n"
6518 " if(!bHasToInterpolate)\n"
6519 " {\n"
6520 " fSx = 0.5 * (fAx + fBx);\n"
6521 " fPx = fAx; fPy = fAy;\n"
6522 " fQx = fBx; fQy = fBy;\n"
6523 " bHasToInterpolate = true;\n"
6524 " }\n"
6525 " fPx = fQx; fQx = fRx; fRx = fSx;\n"
6526 " fPy = fQy; fQy = fRy;\n"
6527 " fRy = arg0-GetFInvValue(fF1,fF2,fSx);\n"
6528 " if ((fAy < 0.0 && fRy > 0.0) || (fAy > 0.0 && fRy < 0.0))\n"
6529 " {\n"
6530 " fBx = fRx; fBy = fRy;\n"
6531 " }\n"
6532 " else\n"
6533 " {\n"
6534 " fAx = fRx; fAy = fRy;\n"
6535 " }\n"
6536 " bHasToInterpolate = bHasToInterpolate && (fabs(fRy)"
6537 " * 2.0 <= fabs(fQy));\n"
6538 " ++nCount;\n"
6539 " }\n"
6540 " tmp = fRx;\n"
6541 " return tmp;"
6542 "}";
6544 void OpFTest::BinInlineFun(std::set<std::string>& decls,
6545 std::set<std::string>& funs)
6547 decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
6548 decls.insert(lcl_getLanczosSumDecl);decls.insert(GetBetaDecl);
6549 decls.insert(GetLogBetaDecl);decls.insert(GetBetaDistPDFDecl);
6550 decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetBetaDistDecl);
6551 decls.insert(GetFDistDecl);
6552 funs.insert(lcl_getLanczosSum);funs.insert(GetBeta);
6553 funs.insert(GetLogBeta);funs.insert(GetBetaDistPDF);
6554 funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetBetaDist);
6555 funs.insert(GetFDist);
6557 void OpFTest::GenSlidingWindowFunction(std::stringstream &ss,
6558 const std::string &sSymName, SubArguments &vSubArguments)
6560 FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
6561 assert(pCur);
6562 const formula::DoubleVectorRefToken* pCurDVR =
6563 static_cast<const formula::DoubleVectorRefToken *>(pCur);
6564 size_t nCurWindowSize = pCurDVR->GetRefRowSize();
6565 FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
6566 assert(pCur1);
6567 const formula::DoubleVectorRefToken* pCurDVR1 =
6568 static_cast<const formula::DoubleVectorRefToken *>(pCur1);
6569 size_t nCurWindowSize1 = pCurDVR1->GetRefRowSize();
6570 ss << "\ndouble " << sSymName;
6571 ss << "_"<< BinFuncName() <<"( ";
6572 for (unsigned i = 0; i < vSubArguments.size(); i++)
6574 if (i)
6575 ss << ",";
6576 vSubArguments[i]->GenSlidingWindowDecl(ss);
6578 ss << ") {\n";
6579 ss << " int gid0 = get_global_id(0);\n";
6580 ss << " double fSum1 = 0.0;\n";
6581 ss << " double fSumSqr1 = 0.0;\n";
6582 ss << " double fSum2 = 0.0;\n";
6583 ss << " double fSumSqr2 = 0.0;\n";
6584 ss << " int length0="<<nCurWindowSize;
6585 ss << ";\n";
6586 ss << " int length1= "<<nCurWindowSize1;
6587 ss << ";\n";
6588 ss << " double tmp = 0;\n";
6589 for (unsigned i = 0; i < vSubArguments.size(); i++)
6591 FormulaToken *pCurSub = vSubArguments[i]->GetFormulaToken();
6592 assert(pCurSub);
6593 if (pCurSub->GetType() == formula::svDoubleVectorRef)
6595 const formula::DoubleVectorRefToken* pDVR =
6596 static_cast<const formula::DoubleVectorRefToken *>(pCurSub);
6597 ss << " for (int i = ";
6598 ss << "0; i < "<< pDVR->GetRefRowSize() << "; i++){\n";
6599 ss << " double arg"<<i<<" = ";
6600 ss << vSubArguments[i]->GenSlidingWindowDeclRef(true);
6601 ss << ";\n";
6602 ss << " if(isNan(arg"<<i<<")||((gid0+i)>=";
6603 ss << pDVR->GetArrayLength();
6604 ss << "))\n";
6605 ss << " {\n";
6606 ss << " length"<<i<<"--;\n";
6607 ss << " continue;\n";
6608 ss << " }\n";
6609 ss << " fSum"<<i+1<<" += arg"<<i<<";\n";
6610 ss << " fSumSqr"<<i+1<<" += arg"<<i;
6611 ss << " * arg"<<i<<";\n";
6612 ss << " }\n";
6614 else if (pCurSub->GetType() == formula::svSingleVectorRef)
6616 ss << "return HUGE_VAL";
6618 else if (pCurSub->GetType() == formula::svDouble)
6620 ss << "return HUGE_VAL";
6623 ss << " double fS1 = (fSumSqr1-fSum1*fSum1/length0)/(length0-1.0);\n"
6624 " double fS2 = (fSumSqr2-fSum2*fSum2/length1)/(length1-1.0);\n"
6625 " double fF, fF1, fF2;\n"
6626 " if (fS1 > fS2)\n"
6627 " {\n"
6628 " fF = fS1/fS2;\n"
6629 " fF1 = length0-1.0;\n"
6630 " fF2 = length1-1.0;\n"
6631 " }\n"
6632 " else\n"
6633 " {\n"
6634 " fF = fS2/fS1;\n"
6635 " fF1 = length1-1.0;\n"
6636 " fF2 = length0-1.0;\n"
6637 " }\n"
6638 " tmp = 2.0*GetFDist(fF, fF1, fF2);\n";
6639 ss << " return tmp;\n";
6640 ss << "}";
6642 void OpB::BinInlineFun(std::set<std::string>& decls,
6643 std::set<std::string>& funs)
6645 //decls.insert(fBigInvDecl);decls.insert(fLogDblMaxDecl);
6646 decls.insert(GetBinomDistPMFDecl);decls.insert(MinDecl);
6647 decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
6648 decls.insert(GetBetaDistDecl);decls.insert(GetBetaDistPDFDecl);
6649 decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetLogBetaDecl);
6650 decls.insert(lcl_getLanczosSumDecl); decls.insert(GetBetaDecl);
6651 funs.insert(GetBinomDistPMF);funs.insert(lcl_GetBinomDistRange);
6652 funs.insert(GetBetaDist);funs.insert(GetBetaDistPDF);
6653 funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetLogBeta);
6654 funs.insert(lcl_getLanczosSum);funs.insert(GetBeta);
6657 void OpB::GenSlidingWindowFunction(std::stringstream &ss,
6658 const std::string &sSymName, SubArguments &vSubArguments)
6660 ss << "\ndouble " << sSymName;
6661 ss << "_"<< BinFuncName() <<"(";
6662 for (unsigned i = 0; i < vSubArguments.size(); i++)
6664 if (i)
6665 ss << ",";
6666 vSubArguments[i]->GenSlidingWindowDecl(ss);
6668 ss << ") {\n";
6669 ss << " int gid0=get_global_id(0);\n";
6670 ss << " double min = 2.22507e-308;\n";
6671 ss << " double tmp;\n";
6672 ss << " double arg0,arg1,arg2,arg3;\n";
6673 size_t i = vSubArguments.size();
6674 for (i = 0; i < vSubArguments.size(); i++)
6676 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
6677 assert(pCur);
6678 if (pCur->GetType() == formula::svDoubleVectorRef)
6680 const formula::DoubleVectorRefToken* pDVR =
6681 static_cast<const formula::DoubleVectorRefToken *>(pCur);
6682 size_t nCurWindowSize = pDVR->GetRefRowSize();
6683 ss << "for (int i = ";
6684 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
6685 ss << "gid0; i < " << pDVR->GetArrayLength();
6686 ss << " && i < " << nCurWindowSize << "; i++){\n";
6687 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
6688 ss << "0; i < " << pDVR->GetArrayLength();
6689 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
6690 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
6691 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
6692 ss << " && i < "<< nCurWindowSize << "; i++){\n ";
6694 else {
6695 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
6698 else if (pCur->GetType() == formula::svSingleVectorRef)
6700 const formula::SingleVectorRefToken* pSVR =
6701 static_cast< const formula::SingleVectorRefToken* >(pCur);
6702 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
6703 ss << " {\n";
6704 ss << " if (isNan(";
6705 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6706 ss << "))\n";
6707 ss << " arg"<<i<<"= 0;\n";
6708 ss << " else\n";
6709 ss << " arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
6710 ss << ";\n";
6711 ss << " }\n";
6712 ss << " else\n";
6713 ss << " arg"<<i<<"= 0;\n";
6715 else if (pCur->GetType() == formula::svDouble)
6717 ss << " if (isNan(";
6718 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6719 ss << "))\n";
6720 ss << " arg"<<i<<"= 0;\n";
6721 ss << " else\n";
6722 ss << " arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
6723 ss << ";\n";
6726 ss << " double rxs = floor(arg2);\n"
6727 " double rxe = floor(arg3);\n"
6728 " double rn = floor(arg0);\n"
6729 " double rq = (0.5 - arg1) + 0.5;\n"
6730 " bool bIsValidX = (0.0 <= rxs && rxs <= rxe && rxe <= rn);\n"
6731 " if (bIsValidX && 0.0 < arg1 && arg1 < 1.0)\n"
6732 " {\n"
6733 " if (rxs == rxe)\n"
6734 " tmp = GetBinomDistPMF(rxs, rn, arg1);\n"
6735 " else\n"
6736 " {\n"
6737 " double fFactor = pow(rq, rn);\n"
6738 " if (fFactor > min)\n"
6739 " tmp ="
6740 " lcl_GetBinomDistRange(rn, rxs, rxe, fFactor, arg1, rq);\n"
6741 " else\n"
6742 " {\n"
6743 " fFactor = pow(arg1, rn);\n"
6744 " if (fFactor > min)\n"
6745 " {\n"
6746 " tmp ="
6747 "lcl_GetBinomDistRange(rn, rn - rxe, rn - rxs, fFactor, rq, arg1);\n"
6748 " }\n"
6749 " else\n"
6750 " tmp ="
6751 "GetBetaDist(rq, rn - rxe, rxe + 1.0)"
6752 "- GetBetaDist(rq, rn - rxs + 1, rxs);\n"
6753 " }\n"
6754 " }\n"
6755 " }\n"
6756 " else\n"
6757 " {\n"
6758 " if (bIsValidX)\n"
6759 " {\n"
6760 " if (arg1 == 0.0)\n"
6761 " tmp = (rxs == 0.0 ? 1.0 : 0.0);\n"
6762 " else if (arg1 == 1.0)\n"
6763 " tmp = (rxe == rn ? 1.0 : 0.0);\n"
6764 " else\n"
6765 " {\n"
6766 " tmp = DBL_MIN;\n"
6767 " }\n"
6768 " }\n"
6769 " else\n"
6770 " {\n"
6771 " tmp = DBL_MIN;\n"
6772 " }\n"
6773 " }\n"
6774 " return tmp;"
6775 "}\n";
6777 void OpBetaDist::BinInlineFun(std::set<std::string>& decls,
6778 std::set<std::string>& funs)
6780 decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
6781 decls.insert(GetBetaDistDecl);decls.insert(GetBetaDistPDFDecl);
6782 decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetLogBetaDecl);
6783 decls.insert(GetBetaDecl);decls.insert(lcl_getLanczosSumDecl);
6784 funs.insert(GetBetaDist);funs.insert(GetBetaDistPDF);
6785 funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetLogBeta);
6786 funs.insert(GetBeta);funs.insert(lcl_getLanczosSum);
6788 void OpPoisson::BinInlineFun(std::set<std::string>& decls,
6789 std::set<std::string>& funs)
6791 decls.insert(fHalfMachEpsDecl);
6792 funs.insert("");
6793 decls.insert(fMaxGammaArgumentDecl);
6794 funs.insert("");
6795 decls.insert(fBigInvDecl);
6796 funs.insert("");
6797 decls.insert(GetLogGammaDecl);
6798 funs.insert(GetLogGamma);
6799 decls.insert(lcl_GetLogGammaHelperDecl);
6800 funs.insert(lcl_GetLogGammaHelper);
6801 decls.insert(lcl_GetGammaHelperDecl);
6802 funs.insert(lcl_GetGammaHelper);
6803 decls.insert(lcl_getLanczosSumDecl);
6804 funs.insert(lcl_getLanczosSum);
6805 decls.insert(GetUpRegIGammaDecl);
6806 funs.insert(GetUpRegIGamma);
6807 decls.insert(GetGammaContFractionDecl);
6808 funs.insert(GetGammaContFraction);
6809 decls.insert(GetGammaSeriesDecl);
6810 funs.insert(GetGammaSeries);
6812 void OpPoisson::GenSlidingWindowFunction(
6813 std::stringstream &ss,const std::string &sSymName,
6814 SubArguments &vSubArguments)
6816 ss << "\ndouble " << sSymName;
6817 ss << "_"<< BinFuncName() <<"(";
6818 for (unsigned i = 0; i < vSubArguments.size(); i++)
6820 if (i)
6821 ss << ",";
6822 vSubArguments[i]->GenSlidingWindowDecl(ss);
6824 ss << ")\n";
6825 ss << "{\n";
6826 ss << " double x,lambda,tmp,tmp0,tmp1,tmp2;\n";
6827 ss << " int bCumulative;\n";
6828 ss << " int gid0=get_global_id(0);\n";
6829 size_t i = vSubArguments.size();
6830 ss <<"\n ";
6831 for (i = 0; i < vSubArguments.size(); i++)
6833 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
6834 assert(pCur);
6835 if (pCur->GetType() == formula::svSingleVectorRef)
6837 const formula::SingleVectorRefToken* pSVR =
6838 static_cast< const formula::SingleVectorRefToken* >(pCur);
6839 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
6841 else if (pCur->GetType() == formula::svDouble)
6843 ss << "{\n";
6845 else
6848 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
6850 ss << " if (isNan(";
6851 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6852 ss << "))\n";
6853 ss << " tmp"<<i<<"= 0;\n";
6854 ss << " else\n";
6855 ss << " tmp"<<i<<"=\n";
6856 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
6857 ss << ";\n}\n";
6859 else
6861 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
6862 ss <<";\n";
6865 ss << " x = floor(tmp0);\n";
6866 ss << " lambda = tmp1;\n";
6867 ss << " bCumulative = tmp2;\n ";
6868 ss << " if (!bCumulative)\n";
6869 ss << " {\n";
6870 ss << " if(lambda == 0.0)\n";
6871 ss << " {\n";
6872 ss << " return 0;\n";
6873 ss << " }\n";
6874 ss << " else\n";
6875 ss << " {\n";
6876 ss << " if (lambda >712)\n";
6877 ss << " {\n";
6878 ss << " tmp = (exp(x*log(lambda)-lambda-GetLogGamma(x+1.0)));\n";
6879 ss << " return tmp;\n";
6880 ss << " }\n";
6881 ss << " else\n";
6882 ss << " {\n";
6883 ss << " double fPoissonVar = 1.0;\n";
6884 ss << " for ( int f = 0; f < x; ++f )\n";
6885 ss << " fPoissonVar *= lambda * pow(( (double)f + 1.0 ),-1);\n";
6886 ss << " tmp = ( fPoissonVar * exp( -lambda ) );\n";
6887 ss << " return tmp;\n";
6888 ss << " }\n";
6889 ss << " }\n";
6890 ss << " } \n";
6891 ss << " else\n";
6892 ss << " {\n";
6893 ss << " if (lambda == 0.0)\n";
6894 ss << " {\n";
6895 ss << " return 1;\n";
6896 ss << " }\n";
6897 ss << " else\n";
6898 ss << " {\n";
6899 ss << " if (lambda > 712 )\n";
6900 ss << " {\n";
6901 ss << " tmp = (GetUpRegIGamma(x+1.0,lambda));\n";
6902 ss << " return tmp;\n";
6903 ss << " }\n";
6904 ss << " else\n";
6905 ss << " {\n";
6906 ss << " if (x >= 936.0)\n";
6907 ss << " {\n";
6908 ss << " return 1;\n";
6909 ss << " }\n";
6910 ss << " else\n";
6911 ss << " {\n";
6912 ss << " double fSummand = exp(-lambda);\n";
6913 ss << " double fSum = fSummand;\n";
6914 ss << " int nEnd = (int) (x + 0.5);\n";
6915 ss << " for (int i = 1; i <= nEnd; i++)\n";
6916 ss << " {\n";
6917 ss << " fSummand = (fSummand*lambda)*pow((double)i,-1);\n";
6918 ss << " fSum += fSummand;\n";
6919 ss << " }\n";
6920 ss << " tmp = fSum;\n";
6921 ss << " return tmp;\n";
6922 ss << " }\n";
6923 ss << " }\n";
6924 ss << " }\n";
6925 ss << " }\n";
6926 ss << "}\n";
6928 void OpCovar::GenSlidingWindowFunction(std::stringstream& ss,
6929 const std::string &sSymName, SubArguments& vSubArguments)
6931 ss << "\ndouble " << sSymName;
6932 ss << "_"<< BinFuncName() <<"(";
6933 for (unsigned i = 0; i < vSubArguments.size(); i++)
6935 if (i)
6936 ss << ",";
6937 vSubArguments[i]->GenSlidingWindowDecl(ss);
6939 ss << ") {\n";
6940 ss << " int gid0 = get_global_id(0);\n";
6941 ss << " double vSum = 0.0;\n";
6942 ss << " double vSum0 = 0.0;\n";
6943 ss << " double vSum1 = 0.0;\n";
6944 ss << " double vMean0 = 0.0;\n";
6945 ss << " double vMean1 = 0.0;\n";
6946 ss << " double arg0 = 0.0;\n";
6947 ss << " double arg1 = 0.0;\n";
6948 FormulaToken* pCurX = vSubArguments[0]->GetFormulaToken();
6949 FormulaToken* pCurY = vSubArguments[1]->GetFormulaToken();
6950 if ((pCurX->GetType() == formula::svDoubleVectorRef)&&
6951 (pCurY->GetType() == formula::svDoubleVectorRef)){
6952 ss << " int cnt = 0;\n";
6953 const formula::DoubleVectorRefToken* pCurDVRX =
6954 static_cast<const formula::DoubleVectorRefToken* >(pCurX);
6955 const formula::DoubleVectorRefToken* pCurDVRY =
6956 static_cast<const formula::DoubleVectorRefToken* >(pCurY);
6957 size_t nCurWindowSizeX = pCurDVRX->GetRefRowSize();
6958 size_t nCurWindowSizeY = pCurDVRY->GetRefRowSize();
6959 if(nCurWindowSizeX == nCurWindowSizeY)
6961 ss << " for( ";
6962 if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
6963 ss << "int i = gid0; i < " << nCurWindowSizeX;
6964 ss << " && i < " << pCurDVRX->GetArrayLength() << " && i < ";
6965 ss << pCurDVRY->GetArrayLength() << "; i++){\n";
6966 ss << " if(isNan(";
6967 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") ||";
6968 ss << " isNan("<< vSubArguments[1]->GenSlidingWindowDeclRef();
6969 ss << ")) {\n";
6970 ss << " arg0 = 0.0;\n";
6971 ss << " arg1 = 0.0;\n";
6972 ss << " --cnt;\n";
6973 ss << " }\n";
6974 ss << "else{\n";
6975 ss << " arg0 = ";
6976 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
6977 ss << " arg1 = ";
6978 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
6979 ss << "}\n";
6980 ss << " ++cnt;\n";
6981 ss << " vSum0 += arg0;\n";
6982 ss << " vSum1 += arg1;\n";
6983 ss << " }\n";
6985 else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
6986 ss << "int i = 0; i < gid0 + " << nCurWindowSizeX << " && ";
6987 ss << " i < " << pCurDVRX->GetArrayLength() << " && ";
6988 ss << " i < " << pCurDVRY->GetArrayLength() << "; i++) {\n";
6989 ss << " if(isNan(";
6990 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") ||";
6991 ss << " isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
6992 ss << ")) {\n";
6993 ss << " arg0 = 0.0;\n";
6994 ss << " arg1 = 0.0;\n";
6995 ss << " --cnt;\n";
6996 ss << " }\n";
6997 ss << "else{\n";
6998 ss << " arg0 = ";
6999 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
7000 ss << " arg1 = ";
7001 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";}\n";
7002 ss << " ++cnt;\n";
7003 ss << " vSum0 += arg0;\n";
7004 ss << " vSum1 += arg1;\n";
7005 ss << " }\n";
7007 else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
7008 ss << "int i = 0; i < " << nCurWindowSizeX << " && i < ";
7009 ss << pCurDVRX->GetArrayLength() << " && i < ";
7010 ss << pCurDVRY->GetArrayLength() << "; i++) {\n";
7011 ss << " if(isNan(";
7012 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") ||";
7013 ss << " isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
7014 ss << ")) {\n";
7015 ss << " arg0 = 0.0;\n";
7016 ss << " arg1 = 0.0;\n";
7017 ss << " --cnt;\n";
7018 ss << " }\n";
7019 ss << "else{\n";
7020 ss << " arg0 = ";
7021 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
7022 ss << " arg1 = ";
7023 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";}\n";
7024 ss << " ++cnt;\n";
7025 ss << " vSum0 += arg0;\n";
7026 ss << " vSum1 += arg1;\n";
7027 ss << " }\n";
7029 else {
7030 ss << "int i = 0; i < " << nCurWindowSizeX << " && ";
7031 ss << " i + gid0 < " << pCurDVRX->GetArrayLength();
7032 ss << " && i + gid0 < " << pCurDVRX->GetArrayLength();
7033 ss << "; i++) {\n";
7034 ss << "if ((isNan(";
7035 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ")) || ";
7036 ss << "(isNan("<< vSubArguments[1]->GenSlidingWindowDeclRef();
7037 ss << "))) {\n";
7038 ss << " arg0 = 0.0;\n";
7039 ss << " arg1 = 0.0;\n";
7040 ss << " --cnt;\n";
7041 ss << " }\n";
7042 ss << " else {\n";
7043 ss << " arg0 = ";
7044 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
7045 ss << " arg1 = ";
7046 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
7047 ss << " }\n";
7048 ss << " ++cnt;\n";
7049 ss << " vSum0 += arg0;\n";
7050 ss << " vSum1 += arg1;\n";
7051 ss << " }\n";
7053 ss << " if(cnt < 1) {\n";
7054 ss << " return CreateDoubleError(errNoValue);\n";
7055 ss << " }\n";
7056 ss << " else {\n";
7057 ss << " vMean0 = vSum0 / cnt;\n";
7058 ss << " vMean1 = vSum1 / cnt;\n";
7059 ss << " for(";
7060 if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
7061 ss << "int i = gid0; i < " << nCurWindowSizeX;
7062 ss << " && i < " << pCurDVRX->GetArrayLength() << " && i < ";
7063 ss << pCurDVRY->GetArrayLength() << "; i++){\n";
7064 ss << " if(isNan(";
7065 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") ||";
7066 ss << " isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
7067 ss << ")){\n";
7068 ss << " arg0 = vMean0;\n";
7069 ss << " arg1 = vMean1;\n";
7070 ss << " }\n";
7071 ss << " else{\n";
7072 ss << " arg0 = ";
7073 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
7074 ss << " arg1 = ";
7075 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";}\n";
7076 ss << " vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
7077 ss << " }\n";
7079 else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
7080 ss << "int i = 0; i < gid0 + " << nCurWindowSizeX << " && ";
7081 ss << " i < " << pCurDVRX->GetArrayLength() << " && ";
7082 ss << " i < " << pCurDVRY->GetArrayLength() << "; i++) {\n";
7083 ss << " if(isNan(";
7084 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") || ";
7085 ss << "isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
7086 ss << ")) {\n";
7087 ss << " arg0 = vMean0;\n";
7088 ss << " arg1 = vMean1;\n";
7089 ss << " }\n";
7090 ss << "else{\n";
7091 ss << " arg0 = ";
7092 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
7093 ss << " arg1 = ";
7094 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";}\n";
7095 ss << " vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
7096 ss << " }\n";
7098 else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
7099 ss << "int i = 0; i < " << nCurWindowSizeX << " && i < ";
7100 ss << pCurDVRX->GetArrayLength() << " && i < ";
7101 ss << pCurDVRY->GetArrayLength() << "; i++) {\n";
7102 ss << " if(isNan(";
7103 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") || ";
7104 ss << "isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
7105 ss << ")) {\n";
7106 ss << " arg0 = vMean0;\n";
7107 ss << " arg1 = vMean1;\n";
7108 ss << " }\n";
7109 ss << "else{\n";
7110 ss << " arg0 = ";
7111 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
7112 ss << " arg1 = ";
7113 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";}\n";
7114 ss << " vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
7115 ss << " }\n";
7117 else {
7118 ss << "int i = 0; i < " << nCurWindowSizeX << " && ";
7119 ss << " i + gid0 < " << pCurDVRX->GetArrayLength();
7120 ss << " && i + gid0 < " << pCurDVRX->GetArrayLength();
7121 ss << "; i++) {\n";
7122 ss << "if((isNan(";
7123 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ")) || ";
7124 ss << "(isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
7125 ss << "))) {\n";
7126 ss << " arg0 = vMean0;\n";
7127 ss << " arg1 = vMean1;\n";
7128 ss << " }\n";
7129 ss << " else{\n";
7130 ss << " arg0 = ";
7131 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
7132 ss << " arg1 = ";
7133 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
7134 ss << " }\n";
7135 ss << " vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
7136 ss << " }\n";
7138 ss << " return vSum / cnt;\n";
7139 ss << " }\n";
7140 ss << "}";
7143 else {
7144 ss << " int cnt0 = 0,cnt1 = 0;\n";
7145 for (unsigned i = 0; i < vSubArguments.size(); i++)
7147 FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
7148 if (pCur->GetType() == formula::svSingleVectorRef){
7149 const formula::SingleVectorRefToken* pTVR =
7150 static_cast< const formula::SingleVectorRefToken* >(pCur);
7151 ss << " if(isNan(";
7152 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7153 ss << ") || gid0 >= " << pTVR->GetArrayLength() << ")\n";
7154 ss << " arg" << i << " = 0;\n else\n";
7155 ss << " arg" << i << " = ";
7156 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7157 ss << " cnt" << i << "++;\n";
7158 ss << " vSum" << i << " += arg" << i << ";\n";
7160 else if (pCur->GetType() == formula::svDouble){
7161 ss << " if(isNan ( ";
7162 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << "))\n";
7163 ss << " arg" << i << " = 0;\n else\n";
7164 ss << " arg" << i << " = ";
7165 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7166 ss << " cnt" << i << "++;\n";
7167 ss << " vSum" << i << " += arg" << i << ";\n";
7169 else {
7170 ss << " arg" << i << " = ";
7171 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7172 ss << " cnt" << i << "++;\n";
7173 ss << " vSum" << i << " += arg" << i << ";\n";
7176 ss << " vMean0 = vSum0 / cnt0;\n";
7177 ss << " vMean1 = vSum0 / cnt1;\n";
7178 for(unsigned i = 0; i < vSubArguments.size(); i++ ) {
7179 FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
7180 if (pCur->GetType() == formula::svSingleVectorRef) {
7181 const formula::SingleVectorRefToken* pTVR =
7182 static_cast< const formula::SingleVectorRefToken* >(pCur);
7183 ss << " if(isNan(";
7184 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7185 ss << ") || gid0 >= " << pTVR->GetArrayLength() << ")\n";
7186 ss << " arg" << i << " = vMean" << i << ";\n";
7187 ss << " else\n";
7188 ss << " arg" << i << " = ";
7189 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7191 else if (pCur->GetType() == formula::svDouble) {
7192 ss << " arg" << i << " = ";
7193 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7194 ss << " if(isNan(arg" << i << "))\n";
7195 ss << " arg" << i << " = vMean" << i << ";\n";
7197 else {
7198 ss << " arg" << i << " = ";
7199 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7202 ss << " vSum += (arg0 - vMean0) * ( arg1 - vMean1 );\n";
7203 ss << " return vSum / cnt0;\n";
7204 ss << "}";
7207 void OpBetaDist::GenSlidingWindowFunction(std::stringstream &ss,
7208 const std::string &sSymName, SubArguments &vSubArguments)
7210 ss << "\ndouble " << sSymName;
7211 ss << "_"<< BinFuncName() <<"(";
7212 for (unsigned i = 0; i < vSubArguments.size(); i++)
7214 if (i)
7215 ss << ",";
7216 vSubArguments[i]->GenSlidingWindowDecl(ss);
7218 ss << ") {\n";
7219 ss << " int gid0=get_global_id(0);\n";
7220 ss << " double tmp;\n";
7221 ss << " double arg0,arg1,arg2,arg3,arg4,arg5;\n";
7222 size_t i = vSubArguments.size();
7224 for (i = 0; i < vSubArguments.size(); i++)
7226 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
7227 assert(pCur);
7228 if (pCur->GetType() == formula::svDoubleVectorRef)
7230 const formula::DoubleVectorRefToken* pDVR =
7231 static_cast<const formula::DoubleVectorRefToken *>(pCur);
7232 size_t nCurWindowSize = pDVR->GetRefRowSize();
7233 ss << "for (int i = ";
7234 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
7235 ss << "gid0; i < " << pDVR->GetArrayLength();
7236 ss << " && i < " << nCurWindowSize << "; i++){\n";
7237 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
7238 ss << "0; i < " << pDVR->GetArrayLength();
7239 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
7240 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
7241 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
7242 ss << " && i < "<< nCurWindowSize << "; i++){\n ";
7244 else {
7245 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
7249 else if (pCur->GetType() == formula::svSingleVectorRef)
7251 const formula::SingleVectorRefToken* pSVR =
7252 static_cast< const formula::SingleVectorRefToken* >(pCur);
7253 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
7254 ss << " {\n";
7255 ss << " if (isNan(";
7256 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7257 ss << "))\n";
7258 ss << " arg"<<i<<"= 0;\n";
7259 ss << " else\n";
7260 ss << " arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
7261 ss << ";\n";
7262 ss << " }\n";
7263 ss << " else\n";
7264 ss << " arg"<<i<<"= 0;\n";
7266 else if (pCur->GetType() == formula::svDouble)
7268 ss << " if (isNan(";
7269 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7270 ss << "))\n";
7271 ss << " arg"<<i<<"= 0;\n";
7272 ss << " else\n";
7273 ss << " arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
7274 ss << ";\n";
7277 ss << " double fScale = arg4 - arg3;\n"
7278 " if (fScale <= 0.0 || arg1 <= 0.0 || arg2 <= 0.0)\n"
7279 " {\n"
7280 " tmp = DBL_MIN;\n"
7281 " return tmp;\n"
7282 " }\n"
7283 " if (arg5)\n"
7284 " {\n"
7285 " if (arg0< arg3)\n"
7286 " {\n"
7287 " tmp = 0.0;\n"
7288 " return tmp;\n"
7289 " }\n"
7290 " if (arg0 > arg4)\n"
7291 " {\n"
7292 " tmp = 1.0;\n"
7293 " return tmp;\n"
7294 " }\n"
7295 " arg0 = (arg0-arg3)*pow(fScale,-1);\n"
7296 " tmp = GetBetaDist(arg0, arg1, arg2);\n"
7297 " }\n"
7298 " else\n"
7299 " {\n"
7300 " if (arg0 < arg3 || arg0 > arg4 )\n"
7301 " {\n"
7302 " tmp = 0.0;\n"
7303 " return tmp;\n"
7304 " }\n"
7305 " arg0 = (arg0 - arg3)*pow(fScale,-1);\n"
7306 " tmp = GetBetaDistPDF(arg0, arg1, arg2)*pow(fScale,-1);\n"
7307 " }\n";
7308 ss << " return tmp;\n";
7309 ss << "}\n";
7311 void OpBetainv::BinInlineFun(std::set<std::string>& decls,
7312 std::set<std::string>& funs)
7314 decls.insert(fMachEpsDecl);
7315 funs.insert("");
7316 decls.insert(fMaxGammaArgumentDecl);
7317 funs.insert("");
7318 decls.insert(lcl_IterateInverseBetaInvDecl);
7319 funs.insert(lcl_IterateInverseBetaInv);
7320 decls.insert(GetBetaDistDecl);
7321 funs.insert(GetBetaDist);
7322 decls.insert(lcl_HasChangeOfSignDecl);
7323 funs.insert(lcl_HasChangeOfSign);
7324 decls.insert(lcl_HasChangeOfSignDecl);
7325 funs.insert(lcl_HasChangeOfSign);
7326 decls.insert(lcl_HasChangeOfSignDecl);
7327 funs.insert(lcl_HasChangeOfSign);
7328 decls.insert(lcl_GetBetaHelperContFracDecl);
7329 funs.insert(lcl_GetBetaHelperContFrac);
7330 decls.insert(GetBetaDistPDFDecl);
7331 funs.insert(GetBetaDistPDF);
7332 decls.insert(GetLogBetaDecl);
7333 funs.insert(GetLogBeta);
7334 decls.insert(GetBetaDecl);
7335 funs.insert(GetBeta);
7336 decls.insert(lcl_getLanczosSumDecl);
7337 funs.insert(lcl_getLanczosSum);
7339 void OpBetainv::GenSlidingWindowFunction(
7340 std::stringstream &ss,const std::string &sSymName,
7341 SubArguments &vSubArguments)
7343 ss << "\ndouble " << sSymName;
7344 ss << "_"<< BinFuncName() <<"(";
7345 for (unsigned i = 0; i < vSubArguments.size(); i++)
7347 if (i)
7348 ss << ",";
7349 vSubArguments[i]->GenSlidingWindowDecl(ss);
7351 ss << ")\n";
7352 ss << "{\n";
7353 ss << " double tmp0,tmp1,tmp2,tmp3,tmp4;\n";
7354 ss << " int gid0=get_global_id(0);\n";
7355 size_t i = vSubArguments.size();
7356 ss <<"\n ";
7357 //while (i-- > 1)
7358 for (i = 0; i < vSubArguments.size(); i++)
7360 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
7361 assert(pCur);
7362 if (pCur->GetType() == formula::svDoubleVectorRef)
7364 const formula::DoubleVectorRefToken* pDVR =
7365 static_cast<const formula::DoubleVectorRefToken *>(pCur);
7366 size_t nCurWindowSize = pDVR->GetRefRowSize();
7367 ss << "for (int i = ";
7368 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
7369 ss << "gid0; i < " << pDVR->GetArrayLength();
7370 ss << " && i < " << nCurWindowSize << "; i++){\n";
7371 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
7372 ss << "0; i < " << pDVR->GetArrayLength();
7373 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
7374 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
7375 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
7376 ss << " && i < "<< nCurWindowSize << "; i++){\n";
7378 else {
7379 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
7382 else if (pCur->GetType() == formula::svSingleVectorRef)
7384 const formula::SingleVectorRefToken* pSVR =
7385 static_cast< const formula::SingleVectorRefToken* >(pCur);
7386 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
7388 else if (pCur->GetType() == formula::svDouble)
7390 ss << "{\n";
7392 else
7396 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
7398 ss << " if (isNan(";
7399 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7400 ss << "))\n";
7401 ss << " tmp"<<i<<"= 0;\n";
7402 ss << " else\n";
7403 ss << " tmp"<<i<<"=\n";
7404 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7405 ss << ";\n}\n";
7407 else
7409 ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
7410 ss <<";\n";
7413 ss << " if (tmp0 < 0.0 || tmp0 >= 1.0 ||";
7414 ss << "tmp3 == tmp4 || tmp1 <= 0.0 || tmp2 <= 0.0)\n";
7415 ss << " {\n";
7416 ss << " return DBL_MIN;\n";
7417 ss << " }\n";
7418 ss << " if (tmp0 == 0.0)\n";
7419 ss << " return 0.0;\n";
7420 ss << " else\n";
7421 ss << " {";
7422 ss << " bool bConvError;";
7423 ss << " double fVal = lcl_IterateInverseBetaInv";
7424 ss << "(tmp0, tmp1, tmp2, 0.0, 1.0, &bConvError);\n";
7425 ss << " if(bConvError)\n";
7426 ss << " return DBL_MIN;\n";
7427 ss << " else\n";
7428 ss << " return (tmp3 + fVal*(tmp4 - tmp3));\n";
7429 ss << " }";
7430 ss << "}\n";
7432 void OpDevSq::GenSlidingWindowFunction(std::stringstream& ss,
7433 const std::string &sSymName, SubArguments& vSubArguments)
7435 ss << "\ndouble " << sSymName;
7436 ss << "_"<< BinFuncName() <<"(";
7437 for (unsigned i = 0; i < vSubArguments.size(); i++)
7439 if (i)
7440 ss << ",";
7441 vSubArguments[i]->GenSlidingWindowDecl(ss);
7443 ss << ") {\n";
7444 ss << " int gid0 = get_global_id(0);\n";
7445 ss << " double vSum = 0.0;\n";
7446 ss << " double vMean = 0.0;\n";
7447 ss << " int cnt = 0;\n";
7448 for(unsigned i = 0; i < vSubArguments.size(); i++ )
7450 ss << " double arg" << i << " = 0.0;\n";
7451 FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
7452 if (pCur->GetType() == formula::svDoubleVectorRef)
7454 const formula::DoubleVectorRefToken* pCurDVR =
7455 static_cast<const formula::DoubleVectorRefToken* >(pCur);
7456 size_t nCurWindowSize = pCurDVR->GetRefRowSize();
7457 ss << " for(int i = ";
7458 if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
7459 ss << "gid0; i < " << nCurWindowSize << "; i++) {\n";
7460 ss << " arg" << i << " = ";
7461 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7462 ss << " if(isNan(arg" << i << ") || (i >= ";
7463 ss << pCurDVR->GetArrayLength() << ")) {\n";
7464 ss << " arg" << i << " = 0.0;\n";
7465 ss << " --cnt;\n";
7466 ss << " }\n";
7467 ss << " ++cnt;\n";
7468 ss << " vSum += arg" << i << ";\n";
7469 ss << " }\n";
7470 } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
7471 ss << "0; i < gid0 + " << nCurWindowSize << "; i++) {\n";
7472 ss << " arg" << i << " = ";
7473 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7474 ss << " if(isNan(arg" << i << ") || (i >= ";
7475 ss << pCurDVR->GetArrayLength() << ")) {\n";
7476 ss << " arg" << i << " = 0.0;\n";
7477 ss << " --cnt;\n";
7478 ss << " }\n";
7479 ss << " ++cnt;\n";
7480 ss << " vSum += arg" << i << ";\n";
7481 ss << " }\n";
7482 } else if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
7483 ss << "0; i < " << nCurWindowSize << "; i++) {\n";
7484 ss << " arg" << i << " = ";
7485 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7486 ss << " if(isNan(arg" << i << ") || (i >= ";
7487 ss << pCurDVR->GetArrayLength() << ")) {\n";
7488 ss << " arg" << i << " = 0.0;\n";
7489 ss << " --cnt;\n";
7490 ss << " }\n";
7491 ss << " ++cnt;\n";
7492 ss << " vSum += arg" << i << ";\n";
7493 ss << " }\n";
7494 } else {
7495 ss << "0; i < " << nCurWindowSize << "; i++) {\n";
7496 ss << " arg" << i << " = ";
7497 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7498 ss << " if(isNan(arg" << i << ") || (i + gid0 >= ";
7499 ss << pCurDVR->GetArrayLength() << ")) {\n";
7500 ss << " arg" << i << " = 0.0;\n";
7501 ss << " --cnt;\n";
7502 ss << " }\n";
7503 ss << " ++cnt;\n";
7504 ss << " vSum += arg" << i << ";\n";
7505 ss << " }\n";
7508 else if (pCur->GetType() == formula::svSingleVectorRef)
7510 const formula::SingleVectorRefToken* pTVR =
7511 static_cast< const formula::SingleVectorRefToken* >(pCur);
7512 ss << " if(isNan(";
7513 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7514 ss << ") || gid0 >= " << pTVR->GetArrayLength() << ")\n";
7515 ss << " arg" << i << " = 0;\n else\n";
7516 ss << " arg" << i << " = ";
7517 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7518 ss << " cnt++;\n";
7519 ss << " vSum += arg" << i << ";\n";
7521 else if (pCur->GetType() == formula::svDouble)
7523 ss << " if(isNan ( ";
7524 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << "))\n";
7525 ss << " arg" << i << " = 0;\n else\n";
7526 ss << " arg" << i << " = ";
7527 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7528 ss << " cnt++;\n";
7529 ss << " vSum += arg" << i << ";\n";
7531 else
7533 ss << " arg" << i << " = ";
7534 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
7535 ss << " cnt++;\n";
7536 ss << " vSum += arg" << i << ";\n";
7539 ss << " vMean = vSum / cnt;\n";
7540 ss << " vSum = 0.0;\n";
7541 for(unsigned k = 0; k < vSubArguments.size(); k++ )
7543 FormulaToken* pCur = vSubArguments[k]->GetFormulaToken();
7544 if (pCur->GetType() == formula::svDoubleVectorRef)
7546 const formula::DoubleVectorRefToken* pCurDVR =
7547 static_cast<const formula::DoubleVectorRefToken* >(pCur);
7548 size_t nCurWindowSize = pCurDVR->GetRefRowSize();
7549 ss << " for(int i = ";
7550 if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
7551 ss << "gid0; i < " << nCurWindowSize << "; i++) {\n";
7552 ss << " arg" << k << " = ";
7553 ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
7554 ss << " if(isNan( arg" << k << " ) || (i >= ";
7555 ss << pCurDVR->GetArrayLength() << ")) {\n";
7556 ss << " arg" << k << " = vXMean;\n";
7557 ss << " }\n";
7558 ss << " vSum += pow( arg" << k << " - vMean, 2 );\n";
7559 ss << " }\n";
7560 } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
7561 ss << "0; i < gid0 + " << nCurWindowSize << "; i++) {\n";
7562 ss << " arg" << k << " = ";
7563 ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
7564 ss << " if(isNan( arg" << k << ") || (i >= ";
7565 ss << pCurDVR->GetArrayLength() << ")) {\n";
7566 ss << " arg" << k << " = vMean;\n";
7567 ss << " }\n";
7568 ss << " vSum += pow( arg" << k << " - vMean, 2 );\n";
7569 ss << " }\n";
7570 } else if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
7571 ss << "0; i < " << nCurWindowSize << "; i++) {\n";
7572 ss << " arg" << k << " = ";
7573 ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
7574 ss << " if(isNan(arg" << k << ") || (i >= ";
7575 ss << pCurDVR->GetArrayLength() << ")) {\n";
7576 ss << " arg" << k << " = vMean;\n";
7577 ss << " }\n";
7578 ss << " vSum += pow( arg" << k << " - vMean, 2 );\n";
7579 ss << " }\n";
7580 } else {
7581 ss << "0; i < " << nCurWindowSize << "; i++) {\n";
7582 ss << " arg" << k << " = ";
7583 ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
7584 ss << " if(isNan(arg" << k << ") || (i + gid0 >= ";
7585 ss << pCurDVR->GetArrayLength() << ")) {\n";
7586 ss << " arg" << k << " = vMean;\n";
7587 ss << " }\n";
7588 ss << " vSum += pow( arg" << k << " - vMean, 2 );\n";
7589 ss << " }\n";
7592 else if (pCur->GetType() == formula::svSingleVectorRef)
7594 const formula::SingleVectorRefToken* pTVR =
7595 static_cast< const formula::SingleVectorRefToken* >(pCur);
7596 ss << " if(isNan(";
7597 ss << vSubArguments[k]->GenSlidingWindowDeclRef();
7598 ss << ") || gid0 >= " << pTVR->GetArrayLength() << ")\n";
7599 ss << " arg" << k << " = vMean;\n else\n";
7600 ss << " arg" << k << " = ";
7601 ss << vSubArguments[k]->GenSlidingWindowDeclRef()<<";\n";
7602 ss << " vSum += pow( arg" << k << " - vMean, 2 );\n";
7604 else if (pCur->GetType() == formula::svDouble)
7606 ss << " arg" << k << " = ";
7607 ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
7608 ss << " if(isNan(arg" << k << "))\n";
7609 ss << " arg" << k << " = vMean;\n";
7610 ss << " vSum += pow( arg" << k << " - vMean, 2 );\n";
7612 else
7614 ss << " arg" << k << " = ";
7615 ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
7616 ss << " vSum += pow( arg" << k << " - vMean, 2 );\n";
7619 ss << " return vSum;\n";
7620 ss << "}";
7622 void OpHypGeomDist::GenSlidingWindowFunction(std::stringstream &ss,
7623 const std::string &sSymName, SubArguments &vSubArguments)
7625 ss << "\ndouble " << sSymName;
7626 ss << "_"<< BinFuncName() <<"(";
7627 for (unsigned i = 0; i < vSubArguments.size(); i++)
7629 if (i)
7630 ss << ",";
7631 vSubArguments[i]->GenSlidingWindowDecl(ss);
7633 ss << ") {\n";
7634 ss << " int gid0=get_global_id(0);\n";
7635 ss << " double arg0,arg1,arg2,arg3;\n";
7636 size_t i = vSubArguments.size();
7638 for (i = 0; i < vSubArguments.size(); i++)
7640 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
7641 assert(pCur);
7642 if (pCur->GetType() == formula::svDoubleVectorRef)
7644 const formula::DoubleVectorRefToken* pDVR =
7645 static_cast<const formula::DoubleVectorRefToken *>(pCur);
7646 size_t nCurWindowSize = pDVR->GetRefRowSize();
7647 ss << "for (int i = ";
7648 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
7649 ss << "gid0; i < " << pDVR->GetArrayLength();
7650 ss << " && i < " << nCurWindowSize << "; i++){\n";
7651 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
7652 ss << "0; i < " << pDVR->GetArrayLength();
7653 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
7654 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
7655 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
7656 ss << " && i < "<< nCurWindowSize << "; i++){\n ";
7658 else {
7659 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
7662 else if (pCur->GetType() == formula::svSingleVectorRef)
7664 const formula::SingleVectorRefToken* pSVR =
7665 static_cast< const formula::SingleVectorRefToken* >(pCur);
7666 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
7667 ss << " {\n";
7668 ss << " if (isNan(";
7669 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7670 ss << "))\n";
7671 ss << " arg"<<i<<"= 0;\n";
7672 ss << " else\n";
7673 ss << " arg"<<i<<"=";
7674 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
7675 ss << ";\n";
7676 ss << " }\n";
7677 ss << " else\n";
7678 ss << " arg"<<i<<"= 0;\n";
7680 else if (pCur->GetType() == formula::svDouble)
7682 ss << " if (isNan(";
7683 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7684 ss << "))\n";
7685 ss << " arg"<<i<<"= 0;\n";
7686 ss << " else\n";
7687 ss << " arg"<<i<<"=";
7688 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
7689 ss << ";\n";
7692 ss << " double N1=floor(arg3);\n"
7693 " double M1=floor(arg2);\n"
7694 " double n1=floor(arg1);\n"
7695 " double x1=floor(arg0);\n"
7696 " double num[9];\n"
7697 " double PI = 3.1415926535897932384626433832795;\n"
7698 " double tmp;\n"
7699 " if( (x1 < 0.0) || (n1 < x1) || (M1 < x1) || (N1 < n1) ||"
7700 "(N1 < M1) || (x1 < n1 - N1 + M1) )\n"
7701 " {\n"
7702 " tmp = DBL_MIN;\n"
7703 " return tmp;\n"
7704 " }\n"
7705 " num[0]=M1;\n"
7706 " num[1]=x1;\n"
7707 " num[2]=M1-x1;\n"
7708 " num[3]=N1-M1;\n"
7709 " num[4]=n1-x1;\n"
7710 " num[5]=N1-M1-n1+x1;\n"
7711 " num[6]=N1;\n"
7712 " num[7]=n1;\n"
7713 " num[8]=N1-n1;\n"
7714 " for(int i=0;i<9;i++)\n"
7715 " {\n"
7716 " if(num[i]<171)\n"
7717 " {\n"
7718 " if(num[i]==0)\n"
7719 " num[i]=0;\n"
7720 " else\n"
7721 " num[i]=log(tgamma(num[i])*num[i]);\n"
7722 " }\n"
7723 " else\n"
7724 " num[i]=0.5*log(2.0*PI)+(num[i]+0.5)*log(num[i])-num[i]+"
7725 "(1.0*pow(12.0*num[i],-1)-1.0*pow(360*pow(num[i],3),-1));\n"
7726 " }\n";
7727 ss << " tmp=pow(M_E,(num[0]+num[3]+num[7]+num[8]";
7728 ss << "-num[1]-num[2]-num[4]-num[5]-num[6]));\n";
7729 ss << " return tmp;\n";
7730 ss << "}\n";
7733 enum MixDoubleString
7735 svDoubleVectorRefDoubleString,
7736 svDoubleVectorRefDouble,
7737 svDoubleVectorRefString,
7738 svDoubleVectorRefNULL,
7739 svSingleVectorRefDoubleString,
7740 svSingleVectorRefDouble,
7741 svSingleVectorRefString,
7742 svSingleVectorRefNULL,
7743 svDoubleDouble
7746 void OpMinA::GenSlidingWindowFunction(
7747 std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
7749 int isMixed = 0;
7750 ss << "\ndouble " << sSymName;
7751 ss << "_"<< BinFuncName() <<"(";
7752 for (unsigned i = 0; i < vSubArguments.size(); i++)
7754 if (i)
7755 ss << ",";
7756 vSubArguments[i]->GenSlidingWindowDecl(ss);
7758 ss << ")\n";
7759 ss << "{\n";
7760 ss << " int gid0=get_global_id(0);\n";
7761 ss << " double tmp0 = 1.79769e+308;\n";
7762 size_t i = vSubArguments.size();
7763 ss <<"\n";
7764 for (i = 0; i < vSubArguments.size(); i++)
7766 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
7767 assert(pCur);
7768 if (pCur->GetType() == formula::svDoubleVectorRef)
7770 const formula::DoubleVectorRefToken* pDVR =
7771 static_cast<const formula::DoubleVectorRefToken *>(pCur);
7772 if(pDVR->GetArrays()[0].mpNumericArray
7773 && pDVR->GetArrays()[0].mpStringArray)
7774 isMixed = svDoubleVectorRefDoubleString;
7775 else if(pDVR->GetArrays()[0].mpNumericArray)
7776 isMixed = svDoubleVectorRefDouble;
7777 else if(pDVR->GetArrays()[0].mpStringArray)
7778 isMixed = svDoubleVectorRefString;
7779 else
7780 isMixed = svDoubleVectorRefNULL;
7781 size_t nCurWindowSize = pDVR->GetRefRowSize();
7782 ss << " for (int i = ";
7783 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
7784 ss << "gid0; i < " << pDVR->GetArrayLength();
7785 ss << " && i < " << nCurWindowSize << "; i++){\n";
7786 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
7787 ss << "0; i < " << pDVR->GetArrayLength();
7788 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
7789 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
7790 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
7791 ss << " && i < "<< nCurWindowSize << "; i++){\n";
7793 else {
7794 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
7797 else if (pCur->GetType() == formula::svSingleVectorRef)
7799 const formula::SingleVectorRefToken* pSVR =
7800 static_cast< const formula::SingleVectorRefToken* >(pCur);
7802 if(pSVR->GetArray().mpNumericArray
7803 && pSVR->GetArray().mpStringArray)
7804 isMixed = svSingleVectorRefDoubleString;
7805 else if(pSVR->GetArray().mpNumericArray)
7806 isMixed = svSingleVectorRefDouble;
7807 else if(pSVR->GetArray().mpStringArray)
7808 isMixed = svSingleVectorRefString;
7809 else
7810 isMixed = svSingleVectorRefNULL;
7811 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
7813 else if (pCur->GetType() == formula::svDouble)
7815 ss << " {\n";
7816 isMixed = svDoubleDouble;
7818 else
7822 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
7824 if(isMixed == svDoubleVectorRefDoubleString
7825 || isMixed == svSingleVectorRefDoubleString)
7827 ss << " if (!isNan(";
7828 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
7829 ss << "))\n";
7830 ss << " tmp0 = tmp0 > ";
7831 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
7832 ss << " ? ";
7833 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
7834 ss << " : tmp0;\n";
7835 ss << " else if(isNan(";
7836 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
7837 ss << ") && ";
7838 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
7839 ss << " != 0)\n";
7840 ss << " tmp0 = tmp0 > 0.0 ? 0.0 : tmp0;\n";
7841 ss << " }\n";
7843 else if(isMixed == svDoubleVectorRefDouble
7844 || isMixed == svSingleVectorRefDouble)
7846 ss << " if (!isNan(";
7847 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7848 ss << "))\n";
7849 ss << " tmp0 = tmp0 > ";
7850 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7851 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
7852 ss << " : tmp0;";
7853 ss <<"\n }\n";
7855 else if(isMixed == svDoubleVectorRefString)
7857 ss << " if(";
7858 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7859 ss << " == 0)\n continue;\n";
7860 ss << " tmp0 = tmp0 > 0.0 ? 0.0 : tmp0;\n";
7861 ss << " }\n";
7863 else if(isMixed == svSingleVectorRefString)
7865 ss << " if(";
7866 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7867 ss << " != 0)\n";
7868 ss << " tmp0 = tmp0 > 0.0 ? 0.0 : tmp0;\n";
7869 ss << " }\n";
7871 else if(isMixed == svDoubleDouble)
7873 ss << " tmp0 = tmp0 > ";
7874 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7875 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
7876 ss << " : tmp0;\n }\n";
7878 else
7880 ss << " }\n";
7883 else
7885 ss << " tmp0 = tmp0 > ";
7886 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7887 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
7888 ss << " : tmp0;";
7889 ss <<"\n }\n";
7892 ss << " return tmp0 == 1.79769e+308 ? 0.0 : tmp0;\n";
7893 ss << "}\n";
7895 void OpCountA::GenSlidingWindowFunction(
7896 std::stringstream &ss, const std::string &sSymName, SubArguments &
7897 vSubArguments)
7899 int isMixed = 0;
7900 ss << "\ndouble " << sSymName;
7901 ss << "_"<< BinFuncName() <<"(";
7902 for (unsigned i = 0; i < vSubArguments.size(); i++)
7904 if (i)
7905 ss << ",";
7906 vSubArguments[i]->GenSlidingWindowDecl(ss);
7908 ss << ")\n";
7909 ss << "{\n";
7910 ss << " int gid0=get_global_id(0);\n";
7911 ss << " double nCount = 0.0;\n";
7912 size_t i = vSubArguments.size();
7914 ss <<"\n";
7915 for (i = 0; i < vSubArguments.size(); i++)
7917 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
7918 assert(pCur);
7919 if (pCur->GetType() == formula::svDoubleVectorRef)
7921 const formula::DoubleVectorRefToken* pDVR =
7922 static_cast<const formula::DoubleVectorRefToken *>(pCur);
7923 if(pDVR->GetArrays()[0].mpNumericArray
7924 && pDVR->GetArrays()[0].mpStringArray)
7925 isMixed = svDoubleVectorRefDoubleString;
7926 else if(pDVR->GetArrays()[0].mpNumericArray)
7927 isMixed = svDoubleVectorRefDouble;
7928 else if(pDVR->GetArrays()[0].mpStringArray)
7929 isMixed = svDoubleVectorRefString;
7930 else
7931 isMixed = svDoubleVectorRefNULL;
7932 size_t nCurWindowSize = pDVR->GetRefRowSize();
7933 ss << " for (int i = ";
7934 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
7935 ss << "gid0; i < " << pDVR->GetArrayLength();
7936 ss << " && i < " << nCurWindowSize << "; i++){\n";
7937 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
7938 ss << "0; i < " << pDVR->GetArrayLength();
7939 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
7940 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
7941 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
7942 ss << " && i < "<< nCurWindowSize << "; i++){\n";
7944 else {
7945 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
7948 else if (pCur->GetType() == formula::svSingleVectorRef)
7950 const formula::SingleVectorRefToken* pSVR =
7951 static_cast< const formula::SingleVectorRefToken* >(pCur);
7953 if(pSVR->GetArray().mpNumericArray
7954 && pSVR->GetArray().mpStringArray)
7955 isMixed = svSingleVectorRefDoubleString;
7956 else if(pSVR->GetArray().mpNumericArray)
7957 isMixed = svSingleVectorRefDouble;
7958 else if(pSVR->GetArray().mpStringArray)
7959 isMixed = svSingleVectorRefString;
7960 else
7961 isMixed = svSingleVectorRefNULL;
7962 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
7964 else if (pCur->GetType() == formula::svDouble)
7966 ss << " {\n";
7967 isMixed = svDoubleDouble;
7969 else
7973 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
7975 if(isMixed == svDoubleVectorRefDoubleString
7976 || isMixed == svSingleVectorRefDoubleString)
7978 ss << " if (!isNan(";
7979 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
7980 ss << ")){\n";
7981 ss << " nCount+=1.0;\n";
7982 ss << " }\n";
7983 ss << " else if(isNan(";
7984 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
7985 ss << ") && ";
7986 ss<< vSubArguments[i]->GenStringSlidingWindowDeclRef();
7987 ss << " != 0)\n";
7988 ss << " nCount+=1.0;\n";
7989 ss << " }\n";
7991 else if(isMixed == svDoubleVectorRefDouble
7992 || isMixed == svSingleVectorRefDouble)
7994 ss << " if (!isNan(";
7995 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
7996 ss << ")){\n";
7997 ss << " nCount+=1.0;\n";
7998 ss <<"}\n }\n";
8000 else if(isMixed == svDoubleVectorRefString)
8002 ss << " if (!isNan(";
8003 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8004 ss << "))\n";
8005 ss << " nCount+=1.0;\n";
8006 ss <<"\n }\n";
8008 else if(isMixed == svSingleVectorRefString)
8010 ss << " if(";
8011 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8012 ss << " != 0)\n";
8013 ss << " nCount+=1.0;\n";
8014 ss << " }\n";
8016 else if(isMixed == svDoubleDouble)
8018 ss << " nCount+=1.0;\n";
8019 ss << " }\n";
8021 else
8023 ss << " }\n";
8026 else
8028 ss << " nCount+=1.0;\n";
8029 ss << " }\n";
8032 ss << " return nCount;\n";
8033 ss << "}\n";
8035 void OpMaxA::GenSlidingWindowFunction(
8036 std::stringstream &ss, const std::string &sSymName, SubArguments &
8037 vSubArguments)
8039 int isMixed = 0;
8040 ss << "\ndouble " << sSymName;
8041 ss << "_"<< BinFuncName() <<"(";
8042 for (unsigned i = 0; i < vSubArguments.size(); i++)
8044 if (i)
8045 ss << ",";
8046 vSubArguments[i]->GenSlidingWindowDecl(ss);
8048 ss << ")\n";
8049 ss << "{\n";
8050 ss << " int gid0=get_global_id(0);\n";
8051 ss << " double tmp0 = 2.22507e-308;\n";
8052 size_t i = vSubArguments.size();
8054 ss <<"\n";
8055 for (i = 0; i < vSubArguments.size(); i++)
8057 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
8058 assert(pCur);
8059 if (pCur->GetType() == formula::svDoubleVectorRef)
8061 const formula::DoubleVectorRefToken* pDVR =
8062 static_cast<const formula::DoubleVectorRefToken *>(pCur);
8063 if(pDVR->GetArrays()[0].mpNumericArray
8064 && pDVR->GetArrays()[0].mpStringArray)
8065 isMixed = svDoubleVectorRefDoubleString;
8066 else if(pDVR->GetArrays()[0].mpNumericArray)
8067 isMixed = svDoubleVectorRefDouble;
8068 else if(pDVR->GetArrays()[0].mpStringArray)
8069 isMixed = svDoubleVectorRefString;
8070 else
8071 isMixed = svDoubleVectorRefNULL;
8072 size_t nCurWindowSize = pDVR->GetRefRowSize();
8073 ss << " for (int i = ";
8074 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
8075 ss << "gid0; i < " << pDVR->GetArrayLength();
8076 ss << " && i < " << nCurWindowSize << "; i++){\n";
8077 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
8078 ss << "0; i < " << pDVR->GetArrayLength();
8079 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
8080 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
8081 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
8082 ss << " && i < "<< nCurWindowSize << "; i++){\n";
8084 else {
8085 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
8088 else if (pCur->GetType() == formula::svSingleVectorRef)
8090 const formula::SingleVectorRefToken* pSVR =
8091 static_cast< const formula::SingleVectorRefToken* >(pCur);
8093 if(pSVR->GetArray().mpNumericArray
8094 && pSVR->GetArray().mpStringArray)
8095 isMixed = svSingleVectorRefDoubleString;
8096 else if(pSVR->GetArray().mpNumericArray)
8097 isMixed = svSingleVectorRefDouble;
8098 else if(pSVR->GetArray().mpStringArray)
8099 isMixed = svSingleVectorRefString;
8100 else
8101 isMixed = svSingleVectorRefNULL;
8102 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
8104 else if (pCur->GetType() == formula::svDouble)
8106 ss << " {\n";
8107 isMixed = svDoubleDouble;
8109 else
8113 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
8115 if(isMixed == svDoubleVectorRefDoubleString
8116 || isMixed == svSingleVectorRefDoubleString)
8118 ss << " if (!isNan(";
8119 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8120 ss << "))\n";
8121 ss << " tmp0 = tmp0 < ";
8122 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8123 ss << " ? ";
8124 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8125 ss << " : tmp0;\n";
8126 ss << " else if(isNan(";
8127 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8128 ss << ") && ";
8129 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8130 ss << " != 0)\n";
8131 ss << " tmp0 = tmp0 < 0.0 ? 0.0 : tmp0;\n";
8132 ss << " }\n";
8134 else if(isMixed == svDoubleVectorRefDouble
8135 || isMixed == svSingleVectorRefDouble)
8137 ss << " if (!isNan(";
8138 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8139 ss << "))\n";
8140 ss << " tmp0 = tmp0 < ";
8141 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8142 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
8143 ss << " : tmp0;";
8144 ss <<"\n }\n";
8146 else if(isMixed == svDoubleVectorRefString)
8148 ss << " if(";
8149 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8150 ss << " == 0)\n continue;\n";
8151 ss << " tmp0 = tmp0 < 0.0 ? 0.0 : tmp0;\n";
8152 ss << " }\n";
8154 else if(isMixed == svSingleVectorRefString)
8156 ss << " if(";
8157 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8158 ss << " != 0)\n";
8159 ss << " tmp0 = tmp0 < 0.0 ? 0.0 : tmp0;\n";
8160 ss << " }\n";
8162 else if(isMixed == svDoubleDouble)
8164 ss << " tmp0 = tmp0 < ";
8165 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8166 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
8167 ss << " : tmp0;\n }\n";
8169 else
8171 ss << " }\n";
8174 else
8176 ss << " tmp0 = tmp0 < ";
8177 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8178 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
8179 ss << " : tmp0;";
8180 ss <<"\n }\n";
8183 ss << " return tmp0 == 2.22507e-308 ? 0.0 : tmp0;\n";
8184 ss << "}\n";
8186 void OpAverageA::GenSlidingWindowFunction(
8187 std::stringstream &ss, const std::string &sSymName, SubArguments &
8188 vSubArguments)
8190 int isMixed = 0;
8191 ss << "\ndouble " << sSymName;
8192 ss << "_"<< BinFuncName() <<"(";
8193 for (unsigned i = 0; i < vSubArguments.size(); i++)
8195 if (i)
8196 ss << ",";
8197 vSubArguments[i]->GenSlidingWindowDecl(ss);
8199 ss << ")\n";
8200 ss << "{\n";
8201 ss << " int gid0=get_global_id(0);\n";
8202 ss << " double tmp0 = 0.0;\n";
8203 ss << " double nCount = 0.0;\n";
8204 size_t i = vSubArguments.size();
8205 ss <<"\n";
8206 for (i = 0; i < vSubArguments.size(); i++)
8208 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
8209 assert(pCur);
8210 if (pCur->GetType() == formula::svDoubleVectorRef)
8212 const formula::DoubleVectorRefToken* pDVR =
8213 static_cast<const formula::DoubleVectorRefToken *>(pCur);
8214 if(pDVR->GetArrays()[0].mpNumericArray
8215 && pDVR->GetArrays()[0].mpStringArray)
8216 isMixed = svDoubleVectorRefDoubleString;
8217 else if(pDVR->GetArrays()[0].mpNumericArray)
8218 isMixed = svDoubleVectorRefDouble;
8219 else if(pDVR->GetArrays()[0].mpStringArray)
8220 isMixed = svDoubleVectorRefString;
8221 else
8222 isMixed = svDoubleVectorRefNULL;
8223 size_t nCurWindowSize = pDVR->GetRefRowSize();
8224 ss << " for (int i = ";
8225 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
8226 ss << "gid0; i < " << pDVR->GetArrayLength();
8227 ss << " && i < " << nCurWindowSize << "; i++){\n";
8228 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
8229 ss << "0; i < " << pDVR->GetArrayLength();
8230 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
8231 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
8232 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
8233 ss << " && i < "<< nCurWindowSize << "; i++){\n";
8235 else {
8236 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
8239 else if (pCur->GetType() == formula::svSingleVectorRef)
8241 const formula::SingleVectorRefToken* pSVR =
8242 static_cast< const formula::SingleVectorRefToken* >(pCur);
8244 if(pSVR->GetArray().mpNumericArray
8245 && pSVR->GetArray().mpStringArray)
8246 isMixed = svSingleVectorRefDoubleString;
8247 else if(pSVR->GetArray().mpNumericArray)
8248 isMixed = svSingleVectorRefDouble;
8249 else if(pSVR->GetArray().mpStringArray)
8250 isMixed = svSingleVectorRefString;
8251 else
8252 isMixed = svSingleVectorRefNULL;
8253 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
8255 else if (pCur->GetType() == formula::svDouble)
8257 ss << " {\n";
8258 isMixed = svDoubleDouble;
8260 else
8264 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
8266 if(isMixed == svDoubleVectorRefDoubleString
8267 || isMixed == svSingleVectorRefDoubleString)
8269 ss << " if (!isNan(";
8270 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8271 ss << ")){\n";
8272 ss << " tmp0 +=";
8273 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8274 ss << ";\n";
8275 ss << " nCount+=1.0;\n";
8276 ss << " }\n";
8277 ss << " else if(isNan(";
8278 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8279 ss << ") && ";
8280 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8281 ss << " != 0)\n";
8282 ss << " nCount+=1.0;\n";
8283 ss << " }\n";
8285 else if(isMixed == svDoubleVectorRefDouble
8286 || isMixed == svSingleVectorRefDouble)
8288 ss << " if (!isNan(";
8289 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8290 ss << ")){\n";
8291 ss << " tmp0 +=";
8292 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8293 ss << ";\n";
8294 ss << " nCount+=1.0;\n";
8295 ss <<"}\n }\n";
8297 else if(isMixed == svDoubleVectorRefString)
8299 ss << " if (!isNan(";
8300 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8301 ss << "))\n";
8302 ss << " nCount+=1.0;\n";
8303 ss <<"\n }\n";
8305 else if(isMixed == svSingleVectorRefString)
8307 ss << " if(";
8308 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8309 ss << " != 0)\n";
8310 ss << " nCount+=1.0;\n";
8311 ss << " }\n";
8313 else if(isMixed == svDoubleDouble)
8315 ss << " tmp0 +=";
8316 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8317 ss << ";\n";
8318 ss << " nCount+=1.0;\n";
8319 ss << " }\n";
8321 else
8323 ss << " }\n";
8326 else
8328 ss << " tmp0 +=";
8329 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8330 ss << ";\n";
8331 ss << " nCount+=1.0;\n";
8332 ss << " }\n";
8335 ss << " return tmp0*pow(nCount,-1);\n";
8336 ss << "}\n";
8338 void OpVarA::GenSlidingWindowFunction(std::stringstream &ss,
8339 const std::string &sSymName, SubArguments &vSubArguments)
8341 int isMixedDV = 0;
8342 int isMixedSV = 0;
8343 ss << "\ndouble " << sSymName;
8344 ss << "_" << BinFuncName() << "(";
8345 for (unsigned i = 0; i < vSubArguments.size(); i++)
8347 if (i)
8348 ss << ",";
8349 vSubArguments[i]->GenSlidingWindowDecl(ss);
8351 ss << "){\n";
8352 ss << " int gid0 = get_global_id(0);\n";
8353 ss << " double fSum = 0.0;\n";
8354 ss << " double fMean = 0.0;\n";
8355 ss << " double vSum = 0.0;\n";
8356 ss << " double fCount = 0.0;\n";
8357 ss << " double arg = 0.0;\n";
8358 unsigned i = vSubArguments.size();
8359 while (i--)
8361 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
8362 assert(pCur);
8364 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
8366 if (pCur->GetType() == formula::svDoubleVectorRef)
8368 const formula::DoubleVectorRefToken* pDVR =
8369 static_cast<const formula::DoubleVectorRefToken *>(pCur);
8370 assert(pDVR);
8371 if(pDVR->GetArrays()[0].mpNumericArray
8372 && pDVR->GetArrays()[0].mpStringArray)
8373 isMixedDV = svDoubleVectorRefDoubleString;
8374 else if(pDVR->GetArrays()[0].mpNumericArray)
8375 isMixedDV = svDoubleVectorRefDouble;
8376 else if(pDVR->GetArrays()[0].mpStringArray)
8377 isMixedDV = svDoubleVectorRefString;
8378 else
8379 isMixedDV = svDoubleVectorRefNULL;
8381 size_t nCurWindowSize = pDVR->GetRefRowSize();
8382 ss << " for (int i = ";
8383 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
8385 ss << "gid0; i < " << pDVR->GetArrayLength();
8386 ss << " && i < " << nCurWindowSize << "; i++)\n";
8387 ss << " {\n";
8389 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
8391 ss << "0; i < " << pDVR->GetArrayLength();
8392 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
8393 ss << " {\n";
8395 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
8397 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
8398 ss << " && i < " << nCurWindowSize << "; i++)\n";
8399 ss << " {\n";
8401 else
8403 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
8404 ss << " {\n";
8407 if(isMixedDV == svDoubleVectorRefDoubleString)
8409 ss << " arg = ";
8410 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8411 ss << ";\n";
8412 ss << " if (isNan(arg) && ";
8413 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8414 ss << " == 0)\n";
8415 ss << " continue;\n";
8416 ss << " if(isNan(arg) && ";
8417 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8418 ss << " != 0)\n";
8419 ss << " {\n";
8420 ss << " fCount = fCount + 1.0;\n";
8421 ss << " continue;\n";
8422 ss << " }\n";
8423 ss << " fSum += arg;\n";
8424 ss << " fCount = fCount + 1.0;\n";
8425 ss << " }\n";
8427 else if(isMixedDV == svDoubleVectorRefDouble)
8429 ss << " arg = ";
8430 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8431 ss << ";\n";
8432 ss << " if (isNan(arg))\n";
8433 ss << " continue;\n";
8434 ss << " fSum += arg;\n";
8435 ss << " fCount = fCount + 1.0;\n";
8436 ss << " }\n";
8438 else if(isMixedDV == svDoubleVectorRefString)
8440 ss << " if (";
8441 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8442 ss << " == 0)\n";
8443 ss << " continue;\n";
8444 ss << " fCount = fCount + 1.0;\n";
8445 ss << " }\n";
8447 else
8449 ss << " continue;\n";
8450 ss << " }\n";
8454 else if (pCur->GetType() == formula::svSingleVectorRef)
8456 const formula::SingleVectorRefToken* pSVR =
8457 static_cast< const formula::SingleVectorRefToken* >(pCur);
8458 assert(pSVR);
8459 if(pSVR->GetArray().mpNumericArray
8460 && pSVR->GetArray().mpStringArray)
8461 isMixedSV = svSingleVectorRefDoubleString;
8462 else if(pSVR->GetArray().mpNumericArray)
8463 isMixedSV = svSingleVectorRefDouble;
8464 else if(pSVR->GetArray().mpStringArray)
8465 isMixedSV = svSingleVectorRefString;
8466 else
8467 isMixedSV = svSingleVectorRefNULL;
8469 if(isMixedSV == svSingleVectorRefDoubleString)
8471 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
8472 ss << " {\n";
8473 ss << " arg = ";
8474 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8475 ss << ";\n";
8476 ss << " if (!isNan(arg))\n";
8477 ss << " {\n";
8478 ss << " fSum += arg;\n";
8479 ss << " fCount = fCount + 1.0;\n";
8480 ss << " }\n";
8481 ss << " if (isNan(arg) && ";
8482 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8483 ss << " != 0)\n";
8484 ss << " fCount = fCount + 1.0;\n";
8485 ss << " }\n";
8487 else if(isMixedSV == svSingleVectorRefDouble)
8489 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
8490 ss << " {\n";
8491 ss << " arg = ";
8492 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
8493 ss << " if (!isNan(arg))\n";
8494 ss << " {\n";
8495 ss << " fSum += arg;\n";
8496 ss << " fCount += 1.0;\n";
8497 ss << " }\n";
8498 ss << " }\n";
8500 else if(isMixedSV == svSingleVectorRefString)
8503 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
8504 ss << " {\n";
8505 ss << " if (";
8506 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8507 ss << " != 0)\n";
8508 ss << " fCount = fCount + 1.0;\n";
8509 ss << " }\n";
8511 else
8513 ss << " arg =0.0;\n";
8516 else
8518 ss << " arg = " << pCur->GetDouble() << ";\n";
8519 ss << " fSum += arg;\n";
8520 ss << " fCount = fCount + 1.0;\n";
8523 else
8525 ss << " arg = ";
8526 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
8527 ss << " fSum += arg;\n";
8528 ss << " fCount = fCount + 1.0;\n";
8530 if (i == 0)
8532 ss << " fMean = fSum * pow(fCount,-1.0);\n";
8535 i = vSubArguments.size();
8536 while (i--)
8538 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
8539 assert(pCur);
8541 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
8543 if (pCur->GetType() == formula::svDoubleVectorRef)
8545 const formula::DoubleVectorRefToken* pDVR =
8546 static_cast<const formula::DoubleVectorRefToken *>(pCur);
8547 if(pDVR->GetArrays()[0].mpNumericArray
8548 && pDVR->GetArrays()[0].mpStringArray)
8549 isMixedDV = svDoubleVectorRefDoubleString;
8550 else if(pDVR->GetArrays()[0].mpNumericArray)
8551 isMixedDV = svDoubleVectorRefDouble;
8552 else if(pDVR->GetArrays()[0].mpStringArray)
8553 isMixedDV = svDoubleVectorRefString;
8554 else
8555 isMixedDV = svDoubleVectorRefNULL;
8557 size_t nCurWindowSize = pDVR->GetRefRowSize();
8558 ss << " for (int i = ";
8559 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
8561 ss << "gid0; i < " << pDVR->GetArrayLength();
8562 ss << " && i < " << nCurWindowSize << "; i++)\n";
8563 ss << " {\n";
8565 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
8567 ss << "0; i < " << pDVR->GetArrayLength();
8568 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
8569 ss << " {\n";
8571 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
8573 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
8574 ss << " && i < " << nCurWindowSize << "; i++)\n";
8575 ss << " {\n";
8577 else
8579 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
8580 ss << " {\n";
8582 if(isMixedDV == svDoubleVectorRefDoubleString)
8584 ss << " arg = ";
8585 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8586 ss << ";\n";
8587 ss << " if (isNan(arg) && ";
8588 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8589 ss << " == 0)\n";
8590 ss << " continue;\n";
8591 ss << " if(isNan(arg) && ";
8592 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8593 ss << " != 0)\n";
8594 ss << " arg = 0.0;\n";
8595 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
8596 ss << " }\n";
8599 else if(isMixedDV == svDoubleVectorRefDouble)
8601 ss << " arg = ";
8602 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8603 ss << ";\n";
8604 ss << " if (isNan(arg))\n";
8605 ss << " continue;\n";
8606 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
8607 ss << " }\n";
8610 else if(isMixedDV == svDoubleVectorRefString)
8612 ss << " if (";
8613 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8614 ss << " == 0)\n";
8615 ss << " continue;\n";
8616 ss << " arg = 0.0;\n";
8617 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
8618 ss << " }\n";
8620 else
8622 ss << " continue;\n";
8623 ss << " }\n";
8626 else if (pCur->GetType() == formula::svSingleVectorRef)
8628 const formula::SingleVectorRefToken* pSVR =
8629 static_cast< const formula::SingleVectorRefToken* >(pCur);
8630 if(pSVR->GetArray().mpNumericArray
8631 && pSVR->GetArray().mpStringArray)
8632 isMixedSV = svSingleVectorRefDoubleString;
8633 else if(pSVR->GetArray().mpNumericArray)
8634 isMixedSV = svSingleVectorRefDouble;
8635 else if(pSVR->GetArray().mpStringArray)
8636 isMixedSV = svSingleVectorRefString;
8637 else
8638 isMixedSV = svSingleVectorRefNULL;
8640 if(isMixedSV == svSingleVectorRefDoubleString)
8642 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
8643 ss << " {\n";
8644 ss << " arg = ";
8645 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8646 ss << ";\n";
8647 ss << " if (!isNan(arg))\n";
8648 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
8649 ss << " if (isNan(arg) && ";
8650 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8651 ss << " != 0)\n";
8652 ss << " {\n";
8653 ss << " arg = 0.0;\n";
8654 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
8655 ss << " }\n";
8656 ss << " }\n";
8658 else if(isMixedSV == svSingleVectorRefDouble)
8660 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
8661 ss << " {\n";
8662 ss << " arg = ";
8663 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
8664 ss << " if (!isNan(arg))\n";
8665 ss << " {\n";
8666 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
8667 ss << " }\n";
8668 ss << " }\n";
8670 else if(isMixedSV == svSingleVectorRefString)
8672 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
8673 ss << " {\n";
8674 ss << " if (";
8675 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8676 ss << " != 0)\n";
8677 ss << " {\n";
8678 ss << " arg = 0.0;\n";
8679 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
8680 ss << " }\n";
8681 ss << " }\n";
8683 else
8685 ss << " arg = 0.0;\n";
8688 else
8690 ss << " arg = " << pCur->GetDouble() << ";\n";
8691 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
8694 else
8696 ss << " arg = ";
8697 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
8698 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
8701 ss << " if (fCount <= 1.0)\n";
8702 ss << " return DBL_MAX;\n";
8703 ss << " else\n";
8704 ss << " return vSum * pow(fCount - 1.0,-1.0);\n";
8705 ss << "}\n";
8708 void OpVarPA::GenSlidingWindowFunction(std::stringstream &ss,
8709 const std::string &sSymName, SubArguments &vSubArguments)
8711 int isMixedDV = 0;
8712 int isMixedSV = 0;
8713 ss << "\ndouble " << sSymName;
8714 ss << "_" << BinFuncName() << "(";
8715 for (unsigned i = 0; i < vSubArguments.size(); i++)
8717 if (i)
8718 ss << ",";
8719 vSubArguments[i]->GenSlidingWindowDecl(ss);
8721 ss << "){\n";
8722 ss << " int gid0 = get_global_id(0);\n";
8723 ss << " double fSum = 0.0;\n";
8724 ss << " double fMean = 0.0;\n";
8725 ss << " double vSum = 0.0;\n";
8726 ss << " double fCount = 0.0;\n";
8727 ss << " double arg = 0.0;\n";
8728 unsigned i = vSubArguments.size();
8729 while (i--)
8731 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
8732 assert(pCur);
8734 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
8736 if (pCur->GetType() == formula::svDoubleVectorRef)
8738 const formula::DoubleVectorRefToken* pDVR =
8739 static_cast<const formula::DoubleVectorRefToken *>(pCur);
8740 if(pDVR->GetArrays()[0].mpNumericArray
8741 && pDVR->GetArrays()[0].mpStringArray)
8742 isMixedDV = svDoubleVectorRefDoubleString;
8743 else if(pDVR->GetArrays()[0].mpNumericArray)
8744 isMixedDV = svDoubleVectorRefDouble;
8745 else if(pDVR->GetArrays()[0].mpStringArray)
8746 isMixedDV = svDoubleVectorRefString;
8747 else
8748 isMixedDV = svDoubleVectorRefNULL;
8750 size_t nCurWindowSize = pDVR->GetRefRowSize();
8751 ss << " for (int i = ";
8752 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
8754 ss << "gid0; i < " << pDVR->GetArrayLength();
8755 ss << " && i < " << nCurWindowSize << "; i++)\n";
8756 ss << " {\n";
8758 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
8760 ss << "0; i < " << pDVR->GetArrayLength();
8761 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
8762 ss << " {\n";
8764 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
8766 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
8767 ss << " && i < " << nCurWindowSize << "; i++)\n";
8768 ss << " {\n";
8770 else
8772 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
8773 ss << " {\n";
8776 if(isMixedDV == svDoubleVectorRefDoubleString)
8778 ss << " arg = ";
8779 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8780 ss << ";\n";
8781 ss << " if (isNan(arg) && ";
8782 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8783 ss << " == 0)\n";
8784 ss << " continue;\n";
8785 ss << " if(isNan(arg) && ";
8786 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8787 ss << " != 0)\n";
8788 ss << " {\n";
8789 ss << " fCount = fCount + 1.0;\n";
8790 ss << " continue;\n";
8791 ss << " }\n";
8792 ss << " fSum += arg;\n";
8793 ss << " fCount = fCount + 1.0;\n";
8794 ss << " }\n";
8796 else if(isMixedDV == svDoubleVectorRefDouble)
8798 ss << " arg = ";
8799 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8800 ss << ";\n";
8801 ss << " if (isNan(arg))\n";
8802 ss << " continue;\n";
8803 ss << " fSum += arg;\n";
8804 ss << " fCount = fCount + 1.0;\n";
8805 ss << " }\n";
8807 else if(isMixedDV == svDoubleVectorRefString)
8809 ss << " if (";
8810 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8811 ss << " == 0)\n";
8812 ss << " continue;\n";
8813 ss << " fCount = fCount + 1.0;\n";
8814 ss << " }\n";
8816 else
8818 ss << " continue;\n";
8819 ss << " }\n";
8823 else if (pCur->GetType() == formula::svSingleVectorRef)
8825 const formula::SingleVectorRefToken* pSVR =
8826 static_cast< const formula::SingleVectorRefToken* >(pCur);
8827 if(pSVR->GetArray().mpNumericArray
8828 && pSVR->GetArray().mpStringArray)
8829 isMixedSV = svSingleVectorRefDoubleString;
8830 else if(pSVR->GetArray().mpNumericArray)
8831 isMixedSV = svSingleVectorRefDouble;
8832 else if(pSVR->GetArray().mpStringArray)
8833 isMixedSV = svSingleVectorRefString;
8834 else
8835 isMixedSV = svSingleVectorRefNULL;
8837 if(isMixedSV == svSingleVectorRefDoubleString)
8839 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
8840 ss << " {\n";
8841 ss << " arg = ";
8842 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8843 ss << ";\n";
8844 ss << " if (!isNan(arg))\n";
8845 ss << " {\n";
8846 ss << " fSum += arg;\n";
8847 ss << " fCount = fCount + 1.0;\n";
8848 ss << " }\n";
8849 ss << " if (isNan(arg) && ";
8850 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8851 ss << " != 0)\n";
8852 ss << " fCount = fCount + 1.0;\n";
8853 ss << " }\n";
8855 else if(isMixedSV == svSingleVectorRefDouble)
8857 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
8858 ss << " {\n";
8859 ss << " arg = ";
8860 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
8861 ss << " if (!isNan(arg))\n";
8862 ss << " {\n";
8863 ss << " fSum += arg;\n";
8864 ss << " fCount += 1.0;\n";
8865 ss << " }\n";
8866 ss << " }\n";
8868 else if(isMixedSV == svSingleVectorRefString)
8871 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
8872 ss << " {\n";
8873 ss << " if (";
8874 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8875 ss << " != 0)\n";
8876 ss << " fCount = fCount + 1.0;\n";
8877 ss << " }\n";
8879 else
8881 ss << " arg =0.0;\n";
8884 else
8886 ss << " arg = " << pCur->GetDouble() << ";\n";
8887 ss << " fSum += arg;\n";
8888 ss << " fCount = fCount + 1.0;\n";
8891 else
8893 ss << " arg = ";
8894 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
8895 ss << " fSum += arg;\n";
8896 ss << " fCount = fCount + 1.0;\n";
8898 if (i == 0)
8900 ss << " fMean = fSum * pow(fCount,-1.0);\n";
8903 i = vSubArguments.size();
8904 while (i--)
8906 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
8907 assert(pCur);
8909 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
8911 if (pCur->GetType() == formula::svDoubleVectorRef)
8913 const formula::DoubleVectorRefToken* pDVR =
8914 static_cast<const formula::DoubleVectorRefToken *>(pCur);
8915 if(pDVR->GetArrays()[0].mpNumericArray
8916 && pDVR->GetArrays()[0].mpStringArray)
8917 isMixedDV = svDoubleVectorRefDoubleString;
8918 else if(pDVR->GetArrays()[0].mpNumericArray)
8919 isMixedDV = svDoubleVectorRefDouble;
8920 else if(pDVR->GetArrays()[0].mpStringArray)
8921 isMixedDV = svDoubleVectorRefString;
8922 else
8923 isMixedDV = svDoubleVectorRefNULL;
8925 size_t nCurWindowSize = pDVR->GetRefRowSize();
8926 ss << " for (int i = ";
8927 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
8929 ss << "gid0; i < " << pDVR->GetArrayLength();
8930 ss << " && i < " << nCurWindowSize << "; i++)\n";
8931 ss << " {\n";
8933 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
8935 ss << "0; i < " << pDVR->GetArrayLength();
8936 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
8937 ss << " {\n";
8939 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
8941 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
8942 ss << " && i < " << nCurWindowSize << "; i++)\n";
8943 ss << " {\n";
8945 else
8947 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
8948 ss << " {\n";
8950 if(isMixedDV == svDoubleVectorRefDoubleString)
8952 ss << " arg = ";
8953 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
8954 ss << ";\n";
8955 ss << " if (isNan(arg) && ";
8956 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8957 ss << " == 0)\n";
8958 ss << " continue;\n";
8959 ss << " if(isNan(arg) && ";
8960 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
8961 ss << " != 0)\n";
8962 ss << " arg = 0.0;\n";
8963 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
8964 ss << " }\n";
8967 else if(isMixedDV == svDoubleVectorRefDouble)
8969 ss << " arg = ";
8970 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8971 ss << ";\n";
8972 ss << " if (isNan(arg))\n";
8973 ss << " continue;\n";
8974 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
8975 ss << " }\n";
8978 else if(isMixedDV == svDoubleVectorRefString)
8980 ss << " if (";
8981 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
8982 ss << " == 0)\n";
8983 ss << " continue;\n";
8984 ss << " arg = 0.0;\n";
8985 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
8986 ss << " }\n";
8988 else
8990 ss << " continue;\n";
8991 ss << " }\n";
8994 else if (pCur->GetType() == formula::svSingleVectorRef)
8996 const formula::SingleVectorRefToken* pSVR =
8997 static_cast< const formula::SingleVectorRefToken* >(pCur);
8998 if(pSVR->GetArray().mpNumericArray
8999 && pSVR->GetArray().mpStringArray)
9000 isMixedSV = svSingleVectorRefDoubleString;
9001 else if(pSVR->GetArray().mpNumericArray)
9002 isMixedSV = svSingleVectorRefDouble;
9003 else if(pSVR->GetArray().mpStringArray)
9004 isMixedSV = svSingleVectorRefString;
9005 else
9006 isMixedSV = svSingleVectorRefNULL;
9008 if(isMixedSV == svSingleVectorRefDoubleString)
9010 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9011 ss << " {\n";
9012 ss << " arg = ";
9013 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
9014 ss << ";\n";
9015 ss << " if (!isNan(arg))\n";
9016 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9017 ss << " if (isNan(arg) && ";
9018 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9019 ss << " != 0)\n";
9020 ss << " {\n";
9021 ss << " arg = 0.0;\n";
9022 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9023 ss << " }\n";
9024 ss << " }\n";
9026 else if(isMixedSV == svSingleVectorRefDouble)
9028 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9029 ss << " {\n";
9030 ss << " arg = ";
9031 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
9032 ss << " if (!isNan(arg))\n";
9033 ss << " {\n";
9034 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9035 ss << " }\n";
9036 ss << " }\n";
9038 else if(isMixedSV == svSingleVectorRefString)
9040 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9041 ss << " {\n";
9042 ss << " if (";
9043 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9044 ss << " != 0)\n";
9045 ss << " {\n";
9046 ss << " arg = 0.0;\n";
9047 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9048 ss << " }\n";
9049 ss << " }\n";
9051 else
9053 ss << " arg = 0.0;\n";
9056 else
9058 ss << " arg = " << pCur->GetDouble() << ";\n";
9059 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9062 else
9064 ss << " arg = ";
9065 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
9066 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9069 ss << " if (fCount == 0.0)\n";
9070 ss << " return DBL_MAX;\n";
9071 ss << " else\n";
9072 ss << " return vSum * pow(fCount,-1.0);\n";
9073 ss << "}\n";
9075 void OpStDevA::GenSlidingWindowFunction(std::stringstream &ss,
9076 const std::string &sSymName, SubArguments &vSubArguments)
9078 int isMixedDV = 0;
9079 int isMixedSV = 0;
9080 ss << "\ndouble " << sSymName;
9081 ss << "_" << BinFuncName() << "(";
9082 for (unsigned i = 0; i < vSubArguments.size(); i++)
9084 if (i)
9085 ss << ",";
9086 vSubArguments[i]->GenSlidingWindowDecl(ss);
9088 ss << "){\n";
9089 ss << " int gid0 = get_global_id(0);\n";
9090 ss << " double fSum = 0.0;\n";
9091 ss << " double fMean = 0.0;\n";
9092 ss << " double vSum = 0.0;\n";
9093 ss << " double fCount = 0.0;\n";
9094 ss << " double arg = 0.0;\n";
9095 unsigned i = vSubArguments.size();
9096 while (i--)
9098 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
9099 assert(pCur);
9101 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
9103 if (pCur->GetType() == formula::svDoubleVectorRef)
9105 const formula::DoubleVectorRefToken* pDVR =
9106 static_cast<const formula::DoubleVectorRefToken *>(pCur);
9107 if(pDVR->GetArrays()[0].mpNumericArray
9108 && pDVR->GetArrays()[0].mpStringArray)
9109 isMixedDV = svDoubleVectorRefDoubleString;
9110 else if(pDVR->GetArrays()[0].mpNumericArray)
9111 isMixedDV = svDoubleVectorRefDouble;
9112 else if(pDVR->GetArrays()[0].mpStringArray)
9113 isMixedDV = svDoubleVectorRefString;
9114 else
9115 isMixedDV = svDoubleVectorRefNULL;
9117 size_t nCurWindowSize = pDVR->GetRefRowSize();
9118 ss << " for (int i = ";
9119 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
9121 ss << "gid0; i < " << pDVR->GetArrayLength();
9122 ss << " && i < " << nCurWindowSize << "; i++)\n";
9123 ss << " {\n";
9125 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
9127 ss << "0; i < " << pDVR->GetArrayLength();
9128 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
9129 ss << " {\n";
9131 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
9133 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
9134 ss << " && i < " << nCurWindowSize << "; i++)\n";
9135 ss << " {\n";
9137 else
9139 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
9140 ss << " {\n";
9143 if(isMixedDV == svDoubleVectorRefDoubleString)
9145 ss << " arg = ";
9146 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
9147 ss << ";\n";
9148 ss << " if (isNan(arg) && ";
9149 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9150 ss << " == 0)\n";
9151 ss << " continue;\n";
9152 ss << " if(isNan(arg) && ";
9153 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9154 ss << " != 0)\n";
9155 ss << " {\n";
9156 ss << " fCount = fCount + 1.0;\n";
9157 ss << " continue;\n";
9158 ss << " }\n";
9159 ss << " fSum += arg;\n";
9160 ss << " fCount = fCount + 1.0;\n";
9161 ss << " }\n";
9163 else if(isMixedDV == svDoubleVectorRefDouble)
9165 ss << " arg = ";
9166 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9167 ss << ";\n";
9168 ss << " if (isNan(arg))\n";
9169 ss << " continue;\n";
9170 ss << " fSum += arg;\n";
9171 ss << " fCount = fCount + 1.0;\n";
9172 ss << " }\n";
9174 else if(isMixedDV == svDoubleVectorRefString)
9176 ss << " if (";
9177 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9178 ss << " == 0)\n";
9179 ss << " continue;\n";
9180 ss << " fCount = fCount + 1.0;\n";
9181 ss << " }\n";
9183 else
9185 ss << " continue;\n";
9186 ss << " }\n";
9190 else if (pCur->GetType() == formula::svSingleVectorRef)
9192 const formula::SingleVectorRefToken* pSVR =
9193 static_cast< const formula::SingleVectorRefToken* >(pCur);
9194 if(pSVR->GetArray().mpNumericArray
9195 && pSVR->GetArray().mpStringArray)
9196 isMixedSV = svSingleVectorRefDoubleString;
9197 else if(pSVR->GetArray().mpNumericArray)
9198 isMixedSV = svSingleVectorRefDouble;
9199 else if(pSVR->GetArray().mpStringArray)
9200 isMixedSV = svSingleVectorRefString;
9201 else
9202 isMixedSV = svSingleVectorRefNULL;
9204 if(isMixedSV == svSingleVectorRefDoubleString)
9206 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9207 ss << " {\n";
9208 ss << " arg = ";
9209 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
9210 ss << ";\n";
9211 ss << " if (!isNan(arg))\n";
9212 ss << " {\n";
9213 ss << " fSum += arg;\n";
9214 ss << " fCount = fCount + 1.0;\n";
9215 ss << " }\n";
9216 ss << " if (isNan(arg) && ";
9217 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9218 ss << " != 0)\n";
9219 ss << " fCount = fCount + 1.0;\n";
9220 ss << " }\n";
9222 else if(isMixedSV == svSingleVectorRefDouble)
9224 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9225 ss << " {\n";
9226 ss << " arg = ";
9227 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
9228 ss << " if (!isNan(arg))\n";
9229 ss << " {\n";
9230 ss << " fSum += arg;\n";
9231 ss << " fCount += 1.0;\n";
9232 ss << " }\n";
9233 ss << " }\n";
9235 else if(isMixedSV == svSingleVectorRefString)
9238 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9239 ss << " {\n";
9240 ss << " if (";
9241 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9242 ss << " != 0)\n";
9243 ss << " fCount = fCount + 1.0;\n";
9244 ss << " }\n";
9246 else
9248 ss << " arg =0.0;\n";
9251 else
9253 ss << " arg = " << pCur->GetDouble() << ";\n";
9254 ss << " fSum += arg;\n";
9255 ss << " fCount = fCount + 1.0;\n";
9258 else
9260 ss << " arg = ";
9261 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
9262 ss << " fSum += arg;\n";
9263 ss << " fCount = fCount + 1.0;\n";
9265 if (i == 0)
9267 ss << " fMean = fSum * pow(fCount,-1.0);\n";
9270 i = vSubArguments.size();
9271 while (i--)
9273 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
9274 assert(pCur);
9276 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
9278 if (pCur->GetType() == formula::svDoubleVectorRef)
9280 const formula::DoubleVectorRefToken* pDVR =
9281 static_cast<const formula::DoubleVectorRefToken *>(pCur);
9282 if(pDVR->GetArrays()[0].mpNumericArray
9283 && pDVR->GetArrays()[0].mpStringArray)
9284 isMixedDV = svDoubleVectorRefDoubleString;
9285 else if(pDVR->GetArrays()[0].mpNumericArray)
9286 isMixedDV = svDoubleVectorRefDouble;
9287 else if(pDVR->GetArrays()[0].mpStringArray)
9288 isMixedDV = svDoubleVectorRefString;
9289 else
9290 isMixedDV = svDoubleVectorRefNULL;
9292 size_t nCurWindowSize = pDVR->GetRefRowSize();
9293 ss << " for (int i = ";
9294 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
9296 ss << "gid0; i < " << pDVR->GetArrayLength();
9297 ss << " && i < " << nCurWindowSize << "; i++)\n";
9298 ss << " {\n";
9300 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
9302 ss << "0; i < " << pDVR->GetArrayLength();
9303 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
9304 ss << " {\n";
9306 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
9308 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
9309 ss << " && i < " << nCurWindowSize << "; i++)\n";
9310 ss << " {\n";
9312 else
9314 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
9315 ss << " {\n";
9317 if(isMixedDV == svDoubleVectorRefDoubleString)
9319 ss << " arg = ";
9320 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
9321 ss << ";\n";
9322 ss << " if (isNan(arg) && ";
9323 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9324 ss << " == 0)\n";
9325 ss << " continue;\n";
9326 ss << " if(isNan(arg) && ";
9327 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9328 ss << " != 0)\n";
9329 ss << " arg = 0.0;\n";
9330 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9331 ss << " }\n";
9334 else if(isMixedDV == svDoubleVectorRefDouble)
9336 ss << " arg = ";
9337 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9338 ss << ";\n";
9339 ss << " if (isNan(arg))\n";
9340 ss << " continue;\n";
9341 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9342 ss << " }\n";
9345 else if(isMixedDV == svDoubleVectorRefString)
9347 ss << " if (";
9348 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9349 ss << " == 0)\n";
9350 ss << " continue;\n";
9351 ss << " arg = 0.0;\n";
9352 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9353 ss << " }\n";
9355 else
9357 ss << " continue;\n";
9358 ss << " }\n";
9361 else if (pCur->GetType() == formula::svSingleVectorRef)
9363 const formula::SingleVectorRefToken* pSVR =
9364 static_cast< const formula::SingleVectorRefToken* >(pCur);
9365 if(pSVR->GetArray().mpNumericArray
9366 && pSVR->GetArray().mpStringArray)
9367 isMixedSV = svSingleVectorRefDoubleString;
9368 else if(pSVR->GetArray().mpNumericArray)
9369 isMixedSV = svSingleVectorRefDouble;
9370 else if(pSVR->GetArray().mpStringArray)
9371 isMixedSV = svSingleVectorRefString;
9372 else
9373 isMixedSV = svSingleVectorRefNULL;
9375 if(isMixedSV == svSingleVectorRefDoubleString)
9377 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9378 ss << " {\n";
9379 ss << " arg = ";
9380 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
9381 ss << ";\n";
9382 ss << " if (!isNan(arg))\n";
9383 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9384 ss << " if (isNan(arg) && ";
9385 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9386 ss << " != 0)\n";
9387 ss << " {\n";
9388 ss << " arg = 0.0;\n";
9389 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9390 ss << " }\n";
9391 ss << " }\n";
9393 else if(isMixedSV == svSingleVectorRefDouble)
9395 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9396 ss << " {\n";
9397 ss << " arg = ";
9398 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
9399 ss << " if (!isNan(arg))\n";
9400 ss << " {\n";
9401 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9402 ss << " }\n";
9403 ss << " }\n";
9405 else if(isMixedSV == svSingleVectorRefString)
9407 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9408 ss << " {\n";
9409 ss << " if (";
9410 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9411 ss << " != 0)\n";
9412 ss << " {\n";
9413 ss << " arg = 0.0;\n";
9414 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9415 ss << " }\n";
9416 ss << " }\n";
9418 else
9420 ss << " arg = 0.0;\n";
9423 else
9425 ss << " arg = " << pCur->GetDouble() << ";\n";
9426 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9429 else
9431 ss << " arg = ";
9432 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
9433 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9436 ss << " if (fCount <= 1.0)\n";
9437 ss << " return DBL_MAX;\n";
9438 ss << " else\n";
9439 ss << " return sqrt(vSum * pow(fCount - 1.0,-1.0));\n";
9440 ss << "}\n";
9443 void OpStDevPA::GenSlidingWindowFunction(std::stringstream &ss,
9444 const std::string &sSymName, SubArguments &vSubArguments)
9446 int isMixedDV = 0;
9447 int isMixedSV = 0;
9448 ss << "\ndouble " << sSymName;
9449 ss << "_" << BinFuncName() << "(";
9450 for (unsigned i = 0; i < vSubArguments.size(); i++)
9452 if (i)
9453 ss << ",";
9454 vSubArguments[i]->GenSlidingWindowDecl(ss);
9456 ss << "){\n";
9457 ss << " int gid0 = get_global_id(0);\n";
9458 ss << " double fSum = 0.0;\n";
9459 ss << " double fMean = 0.0;\n";
9460 ss << " double vSum = 0.0;\n";
9461 ss << " double fCount = 0.0;\n";
9462 ss << " double arg = 0.0;\n";
9463 unsigned i = vSubArguments.size();
9464 while (i--)
9466 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
9467 assert(pCur);
9469 if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
9471 if (pCur->GetType() == formula::svDoubleVectorRef)
9473 const formula::DoubleVectorRefToken* pDVR =
9474 static_cast<const formula::DoubleVectorRefToken *>(pCur);
9475 if(pDVR->GetArrays()[0].mpNumericArray
9476 && pDVR->GetArrays()[0].mpStringArray)
9477 isMixedDV = svDoubleVectorRefDoubleString;
9478 else if(pDVR->GetArrays()[0].mpNumericArray)
9479 isMixedDV = svDoubleVectorRefDouble;
9480 else if(pDVR->GetArrays()[0].mpStringArray)
9481 isMixedDV = svDoubleVectorRefString;
9482 else
9483 isMixedDV = svDoubleVectorRefNULL;
9485 size_t nCurWindowSize = pDVR->GetRefRowSize();
9486 ss << " for (int i = ";
9487 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
9489 ss << "gid0; i < " << pDVR->GetArrayLength();
9490 ss << " && i < " << nCurWindowSize << "; i++)\n";
9491 ss << " {\n";
9493 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
9495 ss << "0; i < " << pDVR->GetArrayLength();
9496 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
9497 ss << " {\n";
9499 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
9501 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
9502 ss << " && i < " << nCurWindowSize << "; i++)\n";
9503 ss << " {\n";
9505 else
9507 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
9508 ss << " {\n";
9511 if(isMixedDV == svDoubleVectorRefDoubleString)
9513 ss << " arg = ";
9514 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
9515 ss << ";\n";
9516 ss << " if (isNan(arg) && ";
9517 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9518 ss << " == 0)\n";
9519 ss << " continue;\n";
9520 ss << " if(isNan(arg) && ";
9521 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9522 ss << " != 0)\n";
9523 ss << " {\n";
9524 ss << " fCount = fCount + 1.0;\n";
9525 ss << " continue;\n";
9526 ss << " }\n";
9527 ss << " fSum += arg;\n";
9528 ss << " fCount = fCount + 1.0;\n";
9529 ss << " }\n";
9531 else if(isMixedDV == svDoubleVectorRefDouble)
9533 ss << " arg = ";
9534 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9535 ss << ";\n";
9536 ss << " if (isNan(arg))\n";
9537 ss << " continue;\n";
9538 ss << " fSum += arg;\n";
9539 ss << " fCount = fCount + 1.0;\n";
9540 ss << " }\n";
9542 else if(isMixedDV == svDoubleVectorRefString)
9544 ss << " if (";
9545 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9546 ss << " == 0)\n";
9547 ss << " continue;\n";
9548 ss << " fCount = fCount + 1.0;\n";
9549 ss << " }\n";
9551 else
9553 ss << " continue;\n";
9554 ss << " }\n";
9558 else if (pCur->GetType() == formula::svSingleVectorRef)
9560 const formula::SingleVectorRefToken* pSVR =
9561 static_cast< const formula::SingleVectorRefToken* >(pCur);
9562 if(pSVR->GetArray().mpNumericArray
9563 && pSVR->GetArray().mpStringArray)
9564 isMixedSV = svSingleVectorRefDoubleString;
9565 else if(pSVR->GetArray().mpNumericArray)
9566 isMixedSV = svSingleVectorRefDouble;
9567 else if(pSVR->GetArray().mpStringArray)
9568 isMixedSV = svSingleVectorRefString;
9569 else
9570 isMixedSV = svSingleVectorRefNULL;
9572 if(isMixedSV == svSingleVectorRefDoubleString)
9574 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9575 ss << " {\n";
9576 ss << " arg = ";
9577 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
9578 ss << ";\n";
9579 ss << " if (!isNan(arg))\n";
9580 ss << " {\n";
9581 ss << " fSum += arg;\n";
9582 ss << " fCount = fCount + 1.0;\n";
9583 ss << " }\n";
9584 ss << " if (isNan(arg) && ";
9585 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9586 ss << " != 0)\n";
9587 ss << " fCount = fCount + 1.0;\n";
9588 ss << " }\n";
9590 else if(isMixedSV == svSingleVectorRefDouble)
9592 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9593 ss << " {\n";
9594 ss << " arg = ";
9595 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
9596 ss << " if (!isNan(arg))\n";
9597 ss << " {\n";
9598 ss << " fSum += arg;\n";
9599 ss << " fCount += 1.0;\n";
9600 ss << " }\n";
9601 ss << " }\n";
9603 else if(isMixedSV == svSingleVectorRefString)
9606 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9607 ss << " {\n";
9608 ss << " if (";
9609 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9610 ss << " != 0)\n";
9611 ss << " fCount = fCount + 1.0;\n";
9612 ss << " }\n";
9614 else
9616 ss << " arg =0.0;\n";
9619 else
9621 ss << " arg = " << pCur->GetDouble() << ";\n";
9622 ss << " fSum += arg;\n";
9623 ss << " fCount = fCount + 1.0;\n";
9626 else
9628 ss << " arg = ";
9629 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
9630 ss << " fSum += arg;\n";
9631 ss << " fCount = fCount + 1.0;\n";
9633 if (i == 0)
9635 ss << " fMean = fSum * pow(fCount,-1.0);\n";
9638 i = vSubArguments.size();
9639 while (i--)
9641 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
9642 assert(pCur);
9644 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
9646 if (pCur->GetType() == formula::svDoubleVectorRef)
9648 const formula::DoubleVectorRefToken* pDVR =
9649 static_cast<const formula::DoubleVectorRefToken *>(pCur);
9650 if(pDVR->GetArrays()[0].mpNumericArray
9651 && pDVR->GetArrays()[0].mpStringArray)
9652 isMixedDV = svDoubleVectorRefDoubleString;
9653 else if(pDVR->GetArrays()[0].mpNumericArray)
9654 isMixedDV = svDoubleVectorRefDouble;
9655 else if(pDVR->GetArrays()[0].mpStringArray)
9656 isMixedDV = svDoubleVectorRefString;
9657 else
9658 isMixedDV = svDoubleVectorRefNULL;
9660 size_t nCurWindowSize = pDVR->GetRefRowSize();
9661 ss << " for (int i = ";
9662 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
9664 ss << "gid0; i < " << pDVR->GetArrayLength();
9665 ss << " && i < " << nCurWindowSize << "; i++)\n";
9666 ss << " {\n";
9668 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
9670 ss << "0; i < " << pDVR->GetArrayLength();
9671 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
9672 ss << " {\n";
9674 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
9676 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
9677 ss << " && i < " << nCurWindowSize << "; i++)\n";
9678 ss << " {\n";
9680 else
9682 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
9683 ss << " {\n";
9685 if(isMixedDV == svDoubleVectorRefDoubleString)
9687 ss << " arg = ";
9688 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
9689 ss << ";\n";
9690 ss << " if (isNan(arg) && ";
9691 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9692 ss << " == 0)\n";
9693 ss << " continue;\n";
9694 ss << " if(isNan(arg) && ";
9695 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9696 ss << " != 0)\n";
9697 ss << " arg = 0.0;\n";
9698 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9699 ss << " }\n";
9702 else if(isMixedDV == svDoubleVectorRefDouble)
9704 ss << " arg = ";
9705 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9706 ss << ";\n";
9707 ss << " if (isNan(arg))\n";
9708 ss << " continue;\n";
9709 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9710 ss << " }\n";
9713 else if(isMixedDV == svDoubleVectorRefString)
9715 ss << " if (";
9716 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9717 ss << " == 0)\n";
9718 ss << " continue;\n";
9719 ss << " arg = 0.0;\n";
9720 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9721 ss << " }\n";
9723 else
9725 ss << " continue;\n";
9726 ss << " }\n";
9729 else if (pCur->GetType() == formula::svSingleVectorRef)
9731 const formula::SingleVectorRefToken* pSVR =
9732 static_cast< const formula::SingleVectorRefToken* >(pCur);
9733 if(pSVR->GetArray().mpNumericArray
9734 && pSVR->GetArray().mpStringArray)
9735 isMixedSV = svSingleVectorRefDoubleString;
9736 else if(pSVR->GetArray().mpNumericArray)
9737 isMixedSV = svSingleVectorRefDouble;
9738 else if(pSVR->GetArray().mpStringArray)
9739 isMixedSV = svSingleVectorRefString;
9740 else
9741 isMixedSV = svSingleVectorRefNULL;
9743 if(isMixedSV == svSingleVectorRefDoubleString)
9745 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9746 ss << " {\n";
9747 ss << " arg = ";
9748 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
9749 ss << ";\n";
9750 ss << " if (!isNan(arg))\n";
9751 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9752 ss << " if (isNan(arg) && ";
9753 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
9754 ss << " != 0)\n";
9755 ss << " {\n";
9756 ss << " arg = 0.0;\n";
9757 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9758 ss << " }\n";
9759 ss << " }\n";
9761 else if(isMixedSV == svSingleVectorRefDouble)
9763 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9764 ss << " {\n";
9765 ss << " arg = ";
9766 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
9767 ss << " if (!isNan(arg))\n";
9768 ss << " {\n";
9769 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9770 ss << " }\n";
9771 ss << " }\n";
9773 else if(isMixedSV == svSingleVectorRefString)
9775 ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
9776 ss << " {\n";
9777 ss << " if (";
9778 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9779 ss << " != 0)\n";
9780 ss << " {\n";
9781 ss << " arg = 0.0;\n";
9782 ss << " vSum += (arg - fMean)*(arg - fMean);\n";
9783 ss << " }\n";
9784 ss << " }\n";
9786 else
9788 ss << " arg = 0.0;\n";
9791 else
9793 ss << " arg = " << pCur->GetDouble() << ";\n";
9794 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9797 else
9799 ss << " arg = ";
9800 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
9801 ss << " vSum += (arg - fMean) * (arg - fMean);\n";
9804 ss << " if (fCount == 1.0)\n";
9805 ss << " return DBL_MAX;\n";
9806 ss << " else\n";
9807 ss << " return sqrt(vSum * pow(fCount,-1.0));\n";
9808 ss << "}\n";
9811 void OpAveDev:: GenSlidingWindowFunction(std::stringstream &ss,
9812 const std::string &sSymName, SubArguments &vSubArguments)
9814 ss << "\ndouble " << sSymName;
9815 ss << "_"<< BinFuncName() <<"( ";
9816 for (unsigned i = 0; i < vSubArguments.size(); i++)
9818 if (i)
9819 ss << ",";
9820 vSubArguments[i]->GenSlidingWindowDecl(ss);
9822 ss << ")\n";
9823 ss <<"{\n";
9824 ss << " int gid0 = get_global_id(0);\n";
9825 ss << " double sum=0.0;\n";
9826 ss << " double length;\n";
9827 ss << " double totallength=0;\n";
9828 ss << " double tmp = 0;\n";
9829 for (unsigned i = 0; i < vSubArguments.size(); i++)
9831 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
9832 assert(pCur);
9833 if (pCur->GetType() == formula::svDoubleVectorRef)
9835 const formula::DoubleVectorRefToken* pDVR =
9836 static_cast<const formula::DoubleVectorRefToken *>(pCur);
9837 size_t nCurWindowSize = pDVR->GetRefRowSize();
9838 ss << " length="<<nCurWindowSize;
9839 ss << ";\n";
9840 ss << " for (int i = ";
9841 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
9842 ss << " {\n";
9843 ss << " double arg"<<i<<" = ";
9844 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9845 ss << ";\n";
9846 ss << " if(isNan(arg"<<i<<")||((gid0+i)>=";
9847 ss << pDVR->GetArrayLength();
9848 ss << "))\n";
9849 ss << " {\n";
9850 ss << " length-=1.0;\n";
9851 ss << " continue;\n";
9852 ss << " }\n";
9853 ss << " sum += arg"<<i<<";\n";
9854 ss << " }\n";
9855 ss << " totallength +=length;\n";
9857 else if (pCur->GetType() == formula::svSingleVectorRef)
9859 ss << " tmp = ";
9860 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9861 ss << ";\n";
9862 ss << " if(!isNan(tmp))\n";
9863 ss << " {\n";
9864 ss << " sum += tmp;\n";
9865 ss << " totallength +=1;\n";
9866 ss << " }\n";
9868 else if (pCur->GetType() == formula::svDouble)
9870 ss << " tmp = ";
9871 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9872 ss << ";\n";
9873 ss << " sum += tmp;\n";
9874 ss << " totallength +=1;\n";
9877 ss << " double mean = sum * pow(totallength,-1);\n";
9878 ss << " sum = 0.0;\n";
9879 for (unsigned i = 0; i < vSubArguments.size(); i++)
9881 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
9882 assert(pCur);
9883 if (pCur->GetType() == formula::svDoubleVectorRef)
9885 const formula::DoubleVectorRefToken* pDVR =
9886 static_cast<const formula::DoubleVectorRefToken *>(pCur);
9887 size_t nCurWindowSize = pDVR->GetRefRowSize();
9888 ss << " for (int i = ";
9889 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
9890 ss << " {\n";
9891 ss << " double arg"<<i<<" = ";
9892 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9893 ss << ";\n";
9894 ss << " if(isNan(arg"<<i<<")||((gid0+i)>=";
9895 ss << pDVR->GetArrayLength();
9896 ss << "))\n";
9897 ss << " {\n";
9898 ss << " continue;\n";
9899 ss << " }\n";
9900 ss << " sum += fabs(arg"<<i<<"-mean);\n";
9901 ss << " }\n";
9903 else if (pCur->GetType() == formula::svSingleVectorRef)
9905 ss << " tmp = ";
9906 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9907 ss << ";\n";
9908 ss << " if(!isNan(tmp))\n";
9909 ss << " {\n";
9910 ss << " sum += fabs(tmp-mean);\n";
9911 ss << " }\n";
9913 else if (pCur->GetType() == formula::svDouble)
9915 ss << " tmp = ";
9916 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
9917 ss << ";\n";
9918 ss << " sum += fabs(tmp-mean);\n";
9921 ss << " tmp=sum*pow(totallength,-1);\n";
9922 ss << " return tmp;\n";
9923 ss << "}";
9927 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */