LibreOffice Module sc (master)  1
op_statistical.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  */
9 
10 #include "op_statistical.hxx"
11 
12 #include <formula/vectortoken.hxx>
13 #include <sstream>
15 
16 using namespace formula;
17 
18 namespace sc::opencl {
19 void OpVar::GenSlidingWindowFunction(std::stringstream &ss,
20  const std::string &sSymName, SubArguments &vSubArguments)
21 {
22  ss << "\ndouble " << sSymName;
23  ss << "_" << BinFuncName() << "(";
24  for (size_t i = 0; i < vSubArguments.size(); i++)
25  {
26  if (i)
27  ss << ",";
28  vSubArguments[i]->GenSlidingWindowDecl(ss);
29  }
30  ss << "){\n";
31  ss << " int gid0 = get_global_id(0);\n";
32  ss << " double fSum = 0.0;\n";
33  ss << " double fMean = 0.0;\n";
34  ss << " double vSum = 0.0;\n";
35  ss << " double fCount = 0.0;\n";
36  ss << " double arg = 0.0;\n";
37  unsigned i = vSubArguments.size();
38  while (i--)
39  {
40  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
41  assert(pCur);
42  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
43  {
44  if (pCur->GetType() == formula::svDoubleVectorRef)
45  {
46  const formula::DoubleVectorRefToken* pDVR =
47  static_cast<const formula::DoubleVectorRefToken *>(pCur);
48  size_t nCurWindowSize = pDVR->GetRefRowSize();
49  ss << " for (int i = ";
50  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
51  {
52  ss << "gid0; i < " << pDVR->GetArrayLength();
53  ss << " && i < " << nCurWindowSize << "; i++)\n";
54  ss << " {\n";
55  }
56  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
57  {
58  ss << "0; i < " << pDVR->GetArrayLength();
59  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
60  ss << " {\n";
61  }
62  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
63  {
64  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
65  ss << " && i < " << nCurWindowSize << "; i++)\n";
66  ss << " {\n";
67  }
68  else
69  {
70  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
71  ss << " {\n";
72  }
73  ss << " arg = ";
74  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
75  ss << ";\n";
76  ss << " if (isnan(arg))\n";
77  ss << " continue;\n";
78  ss << " fSum += arg;\n";
79  ss << " fCount += 1.0;\n";
80  ss << " }\n";
81  }
82  else if (pCur->GetType() == formula::svSingleVectorRef)
83  {
84  const formula::SingleVectorRefToken* pSVR =
85  static_cast< const formula::SingleVectorRefToken*>(pCur);
86  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
87  ss << " {\n";
88  ss << " arg = ";
89  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
90  ss << " if (!isnan(arg))\n";
91  ss << " {\n";
92  ss << " fSum += arg;\n";
93  ss << " fCount += 1.0;\n";
94  ss << " }\n";
95  ss << " }\n";
96  }
97  else
98  {
99  ss << " arg = " << pCur->GetDouble() << ";\n";
100  ss << " fSum += arg;\n";
101  ss << " fCount += 1.0;\n";
102  }
103  }
104  else
105  {
106  ss << " arg = ";
107  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
108  ss << " fSum += arg;\n";
109  ss << " fCount += 1.0;\n";
110  }
111  if (i == 0)
112  {
113  ss << " fMean = fSum * pow(fCount,-1.0);\n";
114  }
115  }
116  i = vSubArguments.size();
117  while (i--)
118  {
119  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
120  assert(pCur);
121  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
122  {
123  if (pCur->GetType() == formula::svDoubleVectorRef)
124  {
125  const formula::DoubleVectorRefToken* pDVR =
126  static_cast<const formula::DoubleVectorRefToken *>(pCur);
127  size_t nCurWindowSize = pDVR->GetRefRowSize();
128  ss << " for (int i = ";
129  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
130  {
131  ss << "gid0; i < " << pDVR->GetArrayLength();
132  ss << " && i < " << nCurWindowSize << "; i++)\n";
133  ss << " {\n";
134  }
135  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
136  {
137  ss << "0; i < " << pDVR->GetArrayLength();
138  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
139  ss << " {\n";
140  }
141  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
142  {
143  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
144  ss << " && i < " << nCurWindowSize << "; i++)\n";
145  ss << " {\n";
146  }
147  else
148  {
149  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
150  ss << " {\n";
151  }
152 
153  ss << " arg = ";
154  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
155  ss << ";\n";
156  ss << " if (isnan(arg))\n";
157  ss << " continue;\n";
158  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
159  ss << " }\n";
160  }
161  else if (pCur->GetType() == formula::svSingleVectorRef)
162  {
163  const formula::SingleVectorRefToken* pSVR =
164  static_cast< const formula::SingleVectorRefToken*>(pCur);
165  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
166  ss << " {\n";
167  ss << " if (!isnan(";
168  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
169  ss << "))\n";
170  ss << " {\n";
171  ss << " arg = ";
172  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
173  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
174  ss << " }\n";
175  ss << " }\n";
176  }
177  else
178  {
179  ss << " arg=" << pCur->GetDouble() << ";\n";
180  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
181  }
182  }
183  else
184  {
185  ss << " arg = ";
186  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
187  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
188  }
189  }
190  ss << " if (fCount <= 1.0)\n";
191  ss << " return CreateDoubleError(DivisionByZero);\n";
192  ss << " else\n";
193  ss << " return vSum * pow(fCount - 1.0,-1.0);\n";
194  ss << "}\n";
195 }
196 void OpZTest::BinInlineFun(std::set<std::string>& decls,
197  std::set<std::string>& funs)
198 {
199  decls.insert(phiDecl);
200  funs.insert(phi);
201  decls.insert(taylorDecl);
202  funs.insert(taylor);
203  decls.insert(gaussDecl);
204  funs.insert(gauss);
205 }
206 void OpZTest::GenSlidingWindowFunction(std::stringstream &ss,
207  const std::string &sSymName, SubArguments &vSubArguments)
208 {
209  ss << "\ndouble " << sSymName;
210  ss << "_" << BinFuncName() << "(";
211  for (size_t i = 0; i < vSubArguments.size(); i++)
212  {
213  if (i)
214  ss << ",";
215  vSubArguments[i]->GenSlidingWindowDecl(ss);
216  }
217  ss << "){\n";
218  ss << " int gid0 = get_global_id(0);\n";
219  ss << " double fSum = 0.0;\n";
220  ss << " double fSumSqr = 0.0;\n";
221  ss << " double mue = 0.0;\n";
222  ss << " double fCount = 0.0;\n";
223  ss << " double arg = 0.0;\n";
224  ss << " double sigma = 0.0;\n";
225  ss << " double mu = 0.0;\n";
226  if(vSubArguments.size() == 1 || vSubArguments.empty())
227  {
228  ss << " return DBL_MAX;\n";
229  ss << "}\n";
230  return ;
231  }
232  else if(vSubArguments.size() == 2)
233  {
234  FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
235  FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
236  assert(pCur);
237  assert(pCur1);
238  if(pCur->GetType() == formula::svDoubleVectorRef)
239  {
240  const formula::DoubleVectorRefToken* pDVR =
241  static_cast<const formula::DoubleVectorRefToken *>(pCur);
242  size_t nCurWindowSize = pDVR->GetRefRowSize();
243  ss << " for (int i = ";
244  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
245  {
246  ss << "gid0; i < " << pDVR->GetArrayLength();
247  ss << " && i < " << nCurWindowSize << "; i++)\n";
248  ss << " {\n";
249  }
250  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
251  {
252  ss << "0; i < " << pDVR->GetArrayLength();
253  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
254  ss << " {\n";
255  }
256  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
257  {
258  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
259  ss << " && i < " << nCurWindowSize << "; i++)\n";
260  ss << " {\n";
261  }
262  else
263  {
264  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
265  ss << " {\n";
266  }
267 
268  ss << " arg = ";
269  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
270  ss << " if (isnan(arg))\n";
271  ss << " continue;\n";
272  ss << " fSum += arg;\n";
273  ss << " fSumSqr += arg * arg;\n";
274  ss << " fCount += 1.0;\n";
275  ss << " }\n";
276  ss << " if(fCount <= 1.0)\n";
277  ss << " return DBL_MAX;\n";
278  ss << " mue = fSum *pow(fCount,-1.0);\n";
279  ss << " sigma = (fSumSqr-fSum*fSum*";
280  ss << "pow(fCount,-1.0))*pow(fCount-1.0,-1.0);\n";
281  }
282  else
283  {
284  ss << " return DBL_MAX;\n";
285  ss << "}\n";
286  return ;
287  }
288  if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
289  {
290  if(pCur1->GetType() == formula::svSingleVectorRef)
291  {
292  const formula::SingleVectorRefToken* pSVR =
293  static_cast<const formula::SingleVectorRefToken* >(pCur1);
294  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
295  ss << " {\n";
296  ss << " mu = " ;
297  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
298  ss << " if (isnan(mu))\n";
299  ss << " mu = 0.0;\n";
300  ss << " }\n";
301 
302  }
303  else if(pCur1->GetType() == formula::svDouble)
304  {
305  ss << " mu = " << pCur1->GetDouble() << ";\n";
306  }
307  else
308  {
309  ss << " return DBL_MAX;\n";
310  ss << "}\n";
311  return ;
312  }
313  }
314  else
315  {
316  ss << " mu = " ;
317  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
318  }
319  ss << " return 0.5 - gauss((mue-mu)/sqrt(sigma/fCount));\n";
320  ss << "}\n";
321  return ;
322  }
323  else
324  {
325  FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
326  FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
327  FormulaToken *pCur2 = vSubArguments[2]->GetFormulaToken();
328  assert(pCur);
329  assert(pCur1);
330  assert(pCur2);
331  if(pCur->GetType() == formula::svDoubleVectorRef)
332  {
333  const formula::DoubleVectorRefToken* pDVR =
334  static_cast<const formula::DoubleVectorRefToken *>(pCur);
335  size_t nCurWindowSize = pDVR->GetRefRowSize();
336  ss << " for (int i = ";
337  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
338  {
339  ss << "gid0; i < " << pDVR->GetArrayLength();
340  ss << " && i < " << nCurWindowSize << "; i++)\n";
341  ss << " {\n";
342  }
343  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
344  {
345  ss << "0; i < " << pDVR->GetArrayLength();
346  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
347  ss << " {\n";
348  }
349  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
350  {
351  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
352  ss << " && i < " << nCurWindowSize << "; i++)\n";
353  ss << " {\n";
354  }
355  else
356  {
357  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
358  ss << " {\n";
359  }
360  ss << " arg = ";
361  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
362  ss << " if (isnan(arg))\n";
363  ss << " continue;\n";
364  ss << " fSum += arg;\n";
365  ss << " fSumSqr += arg * arg;\n";
366  ss << " fCount += 1.0;\n";
367  ss << " }\n";
368  ss << " if(fCount <= 1.0)\n";
369  ss << " return DBL_MAX;\n";
370  ss << " mue = fSum * pow(fCount,-1.0);\n";
371  }
372  else
373  {
374  ss << " return DBL_MAX;\n";
375  ss << "}\n";
376  return ;
377  }
378  if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
379  {
380  if(pCur1->GetType() == formula::svSingleVectorRef)
381  {
382  const formula::SingleVectorRefToken* pSVR1 =
383  static_cast<const formula::SingleVectorRefToken* >(pCur1);
384  ss << " if (gid0 < " << pSVR1->GetArrayLength() << ")\n";
385  ss << " {\n";
386  ss << " mu = " ;
387  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
388  ss << " if (isnan(mu))\n";
389  ss << " mu = 0.0;\n";
390  ss << " }\n";
391  }
392  else if(pCur1->GetType() == formula::svDouble)
393  {
394  ss << " mu = " << pCur1->GetDouble() << ";\n";
395  }
396  else
397  {
398  ss << " return DBL_MAX;\n";
399  ss << "}\n";
400  return ;
401  }
402  }
403  else
404  {
405  ss << " mu=" ;
406  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
407  }
408  if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
409  {
410  if(pCur2->GetType() == formula::svSingleVectorRef)
411  {
412  const formula::SingleVectorRefToken* pSVR2 =
413  static_cast<const formula::SingleVectorRefToken* >(pCur2);
414  ss << " if (gid0 < " << pSVR2->GetArrayLength() << ")\n";
415  ss << " {\n";
416  ss << " sigma = " ;
417  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
418  ss << " if (isnan(sigma))\n";
419  ss << " sigma = 0.0;\n";
420  ss << " }\n";
421  }
422  else if(pCur2->GetType() == formula::svDouble)
423  {
424  ss << " sigma = " << pCur2->GetDouble() << ";\n";
425  }
426  else
427  {
428  ss << " return DBL_MAX;\n";
429  ss << "}\n";
430  return ;
431  }
432  }
433  else
434  {
435  ss << " sigma = " ;
436  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
437  }
438  ss << " return 0.5 - gauss((mue-mu)*sqrt(fCount)/sigma);\n";
439  ss << "}\n";
440  }
441 }
442 
443 void OpTTest::BinInlineFun(std::set<std::string>& decls,
444  std::set<std::string>& funs)
445 {
446  decls.insert(fMachEpsDecl);
447  funs.insert("");
448  decls.insert(fMaxGammaArgumentDecl);
449  funs.insert("");
450  decls.insert(lcl_getLanczosSumDecl);
451  funs.insert(lcl_getLanczosSum);
452  decls.insert(GetBetaDecl);
453  funs.insert(GetBeta);
454  decls.insert(GetLogBetaDecl);
455  funs.insert(GetLogBeta);
456  decls.insert(GetBetaDistPDFDecl);
457  funs.insert(GetBetaDistPDF);
458  decls.insert(lcl_GetBetaHelperContFracDecl);
459  funs.insert(lcl_GetBetaHelperContFrac);
460  decls.insert(GetBetaDistDecl);
461  funs.insert(GetBetaDist);
462  decls.insert(GetTDistDecl);
463  funs.insert(GetTDist);
464 }
465 
466 void OpTTest::GenSlidingWindowFunction(std::stringstream &ss,
467  const std::string &sSymName, SubArguments &vSubArguments)
468 {
469  ss << "\ndouble " << sSymName;
470  ss << "_" << BinFuncName() << "(";
471  for (size_t i = 0; i < vSubArguments.size(); i++)
472  {
473  if (i)
474  ss << ",";
475  vSubArguments[i]->GenSlidingWindowDecl(ss);
476  }
477  ss << "){\n";
478  ss << " int gid0 = get_global_id(0);\n";
479  ss << " double fSum1 = 0.0;\n";
480  ss << " double fSum2 = 0.0;\n";
481  ss << " double fSumSqr1 = 0.0;\n";
482  ss << " double fSumSqr2 = 0.0;\n";
483  ss << " double fCount1 = 0.0;\n";
484  ss << " double fCount2 = 0.0;\n";
485  ss << " double arg1 = 0.0;\n";
486  ss << " double arg2 = 0.0;\n";
487  ss << " double mode = 0.0;\n";
488  ss << " double type = 0.0;\n";
489  ss << " double fT = 0.0;\n";
490  ss << " double fF = 0.0;\n";
491  if(vSubArguments.size() != 4)
492  {
493  ss << " return DBL_MAX;\n";
494  ss << "}\n";
495  return ;
496  }
497  if(vSubArguments.size() == 4)
498  {
499  FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
500  FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
501  FormulaToken *pCur2 = vSubArguments[2]->GetFormulaToken();
502  FormulaToken *pCur3 = vSubArguments[3]->GetFormulaToken();
503  assert(pCur);
504  assert(pCur1);
505  assert(pCur2);
506  assert(pCur3);
507  if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
508  {
509  if(pCur2->GetType() == formula::svSingleVectorRef)
510  {
511  const formula::SingleVectorRefToken* pSVR =
512  static_cast< const formula::SingleVectorRefToken*>(pCur2);
513  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
514  ss << " {\n";
515  ss << " mode = " ;
516  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
517  ss << " if (isnan(mode))\n";
518  ss << " mode = 0.0;\n";
519  ss << " else\n";
520  ss << " mode = floor(mode);\n";
521  ss << " }\n";
522  }
523  else if(pCur2->GetType() == formula::svDouble)
524  {
525  ss << " mode = floor(convert_double(";
526  ss << pCur2->GetDouble() << "));\n";
527  }
528  else
529  {
530  ss << " return DBL_MAX;\n";
531  ss << "}\n";
532  return ;
533  }
534  }
535  else
536  {
537  ss << " mode = floor(" ;
538  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ");\n";
539  }
540  ss << " if(!(mode == 1.0 || mode == 2.0))\n";
541  ss << " return DBL_MAX;\n";
542  if(ocPush==vSubArguments[3]->GetFormulaToken()->GetOpCode())
543  {
544  if(pCur3->GetType() == formula::svSingleVectorRef)
545  {
546  const formula::SingleVectorRefToken* pSVR =
547  static_cast< const formula::SingleVectorRefToken*>(pCur3);
548  assert(pSVR);
549  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
550  ss << " {\n";
551  ss << " if (isnan(";
552  ss << vSubArguments[3]->GenSlidingWindowDeclRef() << "))\n";
553  ss << " type=0.0;\n";
554  ss << " else\n";
555  ss << " type=floor(";
556  ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ");\n";
557  ss << " }\n";
558  }
559  else if(pCur3->GetType() == formula::svDouble)
560  {
561  ss << " type = floor(convert_double(" << pCur3->GetDouble() <<
562  "));\n";
563  }
564  else
565  {
566  ss << " return DBL_MAX;\n";
567  ss << "}\n";
568  return ;
569  }
570  }
571  else
572  {
573  ss << " type=floor(";
574  ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ");\n";
575  }
576  ss << " if(!(type == 1.0||type == 2.0||type == 3.0))\n";
577  ss << " return DBL_MAX;\n";
578 
579  if(pCur->GetType() == formula::svDoubleVectorRef &&
581  {
582  const formula::DoubleVectorRefToken* pDVR =
583  static_cast<const formula::DoubleVectorRefToken *>(pCur);
584  const formula::DoubleVectorRefToken* pDVR1 =
585  static_cast<const formula::DoubleVectorRefToken *>(pCur1);
586 
587  size_t nCurWindowSize = pDVR->GetRefRowSize();
588  size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
589 
590  if(nCurWindowSize == nCurWindowSize1)
591  {
592  ss << " if(type == 1.0)\n";
593  ss << " {\n";
594  ss << " for (int i = ";
595  if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed()) &&
596  (!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
597  {
598  ss << "gid0; i < " << pDVR->GetArrayLength();
599  ss << " && i < " << nCurWindowSize << "; i++)\n";
600  ss << " {\n";
601  }
602  else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed()) &&
603  (pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
604  {
605  ss << "0; i < " << pDVR->GetArrayLength();
606  ss << " && i < gid0+"<< nCurWindowSize << "; i++)\n";
607  ss << " {\n";
608  }
609  else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed()) &&
610  (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
611  {
612  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
613  ss << " && i < " << nCurWindowSize << "; i++)\n";
614  ss << " {\n";
615  }
616  else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed()) &&
617  (pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
618  {
619  ss << "0; i < " << nCurWindowSize << "; i++)\n";
620  ss << " {\n";
621  }
622  else
623  {
624  ss << "0; i < " << nCurWindowSize << "; i++)\n";
625  ss << " {\n";
626  ss << " break;\n";
627  ss << " }";
628  ss << " return DBL_MAX;\n";
629  ss << " }\n";
630  ss << "}\n";
631  return ;
632  }
633 
634  ss << " arg1 = ";
635  ss << vSubArguments[0]->GenSlidingWindowDeclRef(true) << ";\n";
636  ss << " arg2 = ";
637  ss << vSubArguments[1]->GenSlidingWindowDeclRef(true) << ";\n";
638  ss << " if (isnan(arg1)||isnan(arg2))\n";
639  ss << " continue;\n";
640  ss << " fSum1 += arg1;\n";
641  ss << " fSum2 += arg2;\n";
642  ss << " fSumSqr1 += (arg1 - arg2)*(arg1 - arg2);\n";
643  ss << " fCount1 += 1;\n";
644  ss << " }\n";
645  ss << " if(fCount1 < 1.0)\n";
646  ss << " return DBL_MAX;\n";
647  ss << " fT = sqrt(fCount1-1.0) * fabs(fSum1 - fSum2)\n";
648  ss << " /sqrt(fCount1 * fSumSqr1 - (fSum1-fSum2)\n";
649  ss << " *(fSum1-fSum2));\n";
650  ss << " fF = fCount1 - 1.0;\n";
651  }
652  else
653  {
654  ss << " return DBL_MAX;\n";
655  ss << "}\n";
656  return ;
657  }
658  }
659  else
660  {
661  ss << " return DBL_MAX;\n";
662  ss << "}\n";
663  return ;
664  }
665  ss << " }\n";
666  ss << " if(type == 2.0 || type == 3.0)\n";
667  ss << " {\n";
668 
669  if(pCur->GetType() == formula::svDoubleVectorRef &&
671  {
672  const formula::DoubleVectorRefToken* pDVR =
673  static_cast<const formula::DoubleVectorRefToken *>(pCur);
674  const formula::DoubleVectorRefToken* pDVR1 =
675  static_cast<const formula::DoubleVectorRefToken *>(pCur1);
676 
677  size_t nCurWindowSize = pDVR->GetRefRowSize();
678  size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
679  ss << " for (int i = ";
680  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
681  {
682  ss << "gid0; i < " << pDVR->GetArrayLength();
683  ss << " && i < " << nCurWindowSize << "; i++)\n";
684  ss << " {\n";
685  }
686  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
687  {
688  ss << "0; i < " << pDVR->GetArrayLength();
689  ss << " && i < gid0+"<< nCurWindowSize << "; i++)\n";
690  ss << " {\n";
691  }
692  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
693  {
694  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
695  ss << " && i < " << nCurWindowSize << "; i++)\n";
696  ss << " {\n";
697  }
698  else
699  {
700  ss << "0; i < " << nCurWindowSize << "; i++)\n";
701  ss << " {\n";
702  }
703 
704  ss << " arg1 = ";
705  ss << vSubArguments[0]->GenSlidingWindowDeclRef(true) << ";\n";
706  ss << " if (isnan(arg1))\n";
707  ss << " continue;\n";
708  ss << " fSum1 += arg1;\n";
709  ss << " fSumSqr1 += arg1 * arg1;\n";
710  ss << " fCount1 += 1;\n";
711  ss << " }\n";
712 
713  ss << " for (int i = ";
714  if (!pDVR1->IsStartFixed() && pDVR1->IsEndFixed())
715  {
716  ss << "gid0; i < " << pDVR1->GetArrayLength();
717  ss << " && i < " << nCurWindowSize1 << "; i++)\n";
718  ss << " {\n";
719  }
720  else if (pDVR1->IsStartFixed() && !pDVR1->IsEndFixed())
721  {
722  ss << "0; i < " << pDVR1->GetArrayLength();
723  ss << " && i < gid0+"<< nCurWindowSize1 << "; i++)\n";
724  ss << " {\n";
725  }
726  else if (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed())
727  {
728  ss << "0; i + gid0 < " << pDVR1->GetArrayLength();
729  ss << " && i < " << nCurWindowSize1 << "; i++)\n";
730  ss << " {\n";
731  }
732  else
733  {
734  ss << "0; i < " << nCurWindowSize1 << "; i++)\n";
735  ss << " {\n";
736  }
737  ss << " arg2 = ";
738  ss << vSubArguments[1]->GenSlidingWindowDeclRef(true) << ";\n";
739  ss << " if (isnan(arg2))\n";
740  ss << " continue;\n";
741  ss << " fSum2 += arg2;\n";
742  ss << " fSumSqr2 += arg2 * arg2;\n";
743  ss << " fCount2 += 1;\n";
744  ss << " }\n";
745  }
746  else
747  {
748  ss << " return DBL_MAX;\n";
749  ss << " }\n";
750  ss << "}\n";
751  return ;
752  }
753  ss << " if (fCount1 < 2.0 || fCount2 < 2.0)\n";
754  ss << " return DBL_MAX;\n";
755  ss << " }\n";
756  ss << " if(type == 3.0)\n";
757  ss << " {\n";
758  ss << " double fS1 = (fSumSqr1-fSum1*fSum1/fCount1)\n";
759  ss << " /(fCount1-1.0)/fCount1;\n";
760  ss << " double fS2 = (fSumSqr2-fSum2*fSum2/fCount2)\n";
761  ss << " /(fCount2-1.0)/fCount2;\n";
762  ss << " if (fS1 + fS2 == 0.0)\n";
763  ss << " return DBL_MAX;\n";
764  ss << " fT = fabs(fSum1/fCount1 - fSum2/fCount2)\n";
765  ss << " /sqrt(fS1+fS2);\n";
766  ss << " double c = fS1/(fS1+fS2);\n";
767  ss << " fF = 1.0/(c*c/(fCount1-1.0)+(1.0-c)*(1.0-c)\n";
768  ss << " /(fCount2-1.0));\n";
769  ss << " }\n";
770  ss << " if(type == 2.0)\n";
771  ss << " {\n";
772  ss << " double fS1 = (fSumSqr1 - fSum1*fSum1/fCount1)\n";
773  ss << " /(fCount1 - 1.0);\n";
774  ss << " double fS2 = (fSumSqr2 - fSum2*fSum2/fCount2)\n";
775  ss << " /(fCount2 - 1.0);\n";
776  ss << " fT = fabs( fSum1/fCount1 - fSum2/fCount2 )\n";
777  ss << " /sqrt( (fCount1-1.0)*fS1 + (fCount2-1.0)*fS2 )\n";
778  ss << " *sqrt( fCount1*fCount2*(fCount1+fCount2-2)\n";
779  ss << " /(fCount1+fCount2) );\n";
780  ss << " fF = fCount1 + fCount2 - 2;\n";
781  ss << " }\n";
782 
783  ss << " double tdist=GetTDist(fT, fF);\n";
784  ss << " if (mode==1)\n";
785  ss << " return tdist;\n";
786  ss << " else\n";
787  ss << " return 2.0*tdist;\n";
788  ss << "}\n";
789  }
790 }
791 void OpVarP::GenSlidingWindowFunction(std::stringstream &ss,
792  const std::string &sSymName, SubArguments &vSubArguments)
793 {
794  ss << "\ndouble " << sSymName;
795  ss << "_" << BinFuncName() << "(";
796  for (size_t i = 0; i < vSubArguments.size(); i++)
797  {
798  if (i)
799  ss << ",";
800  vSubArguments[i]->GenSlidingWindowDecl(ss);
801  }
802  ss << "){\n";
803  ss << " int gid0 = get_global_id(0);\n";
804  ss << " double fSum = 0.0;\n";
805  ss << " double fMean = 0.0;\n";
806  ss << " double vSum = 0.0;\n";
807  ss << " double fCount = 0.0;\n";
808  ss << " double arg = 0.0;\n";
809  unsigned i = vSubArguments.size();
810  while (i--)
811  {
812  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
813  assert(pCur);
814  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
815  {
816  if (pCur->GetType() == formula::svDoubleVectorRef)
817  {
818  const formula::DoubleVectorRefToken* pDVR =
819  static_cast<const formula::DoubleVectorRefToken *>(pCur);
820  size_t nCurWindowSize = pDVR->GetRefRowSize();
821  ss << " for (int i = ";
822  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
823  {
824  ss << "gid0; i < " << pDVR->GetArrayLength();
825  ss << " && i < " << nCurWindowSize << "; i++)\n";
826  ss << " {\n";
827  }
828  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
829  {
830  ss << "0; i < " << pDVR->GetArrayLength();
831  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
832  ss << " {\n";
833  }
834  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
835  {
836  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
837  ss << " && i < " << nCurWindowSize << "; i++)\n";
838  ss << " {\n";
839  }
840  else
841  {
842  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
843  ss << " {\n";
844  }
845  ss << " arg = ";
846  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
847  ss << ";\n";
848  ss << " if (isnan(arg))\n";
849  ss << " continue;\n";
850  ss << " fSum += arg;\n";
851  ss << " fCount += 1.0;\n";
852  ss << " }\n";
853  }
854  else if (pCur->GetType() == formula::svSingleVectorRef)
855  {
856  const formula::SingleVectorRefToken* pSVR =
857  static_cast< const formula::SingleVectorRefToken*>(pCur);
858  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
859  ss << " {\n";
860  ss << " arg = ";
861  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
862  ss << " if (!isnan(arg))\n";
863  ss << " {\n";
864  ss << " fSum += arg;\n";
865  ss << " fCount += 1.0;\n";
866  ss << " }\n";
867  ss << " }\n";
868  }
869  else
870  {
871  ss << " arg = " << pCur->GetDouble() << ";\n";
872  ss << " fSum += arg;\n";
873  ss << " fCount += 1.0;\n";
874  }
875  }
876  else
877  {
878  ss << " arg = ";
879  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
880  ss << " fSum += arg;\n";
881  ss << " fCount += 1.0;\n";
882  }
883  if (i == 0)
884  {
885  ss << " fMean = fSum * pow(fCount,-1.0);\n";
886  }
887  }
888  i = vSubArguments.size();
889  while (i--)
890  {
891  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
892  assert(pCur);
893  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
894  {
895  if (pCur->GetType() == formula::svDoubleVectorRef)
896  {
897  const formula::DoubleVectorRefToken* pDVR =
898  static_cast<const formula::DoubleVectorRefToken *>(pCur);
899  size_t nCurWindowSize = pDVR->GetRefRowSize();
900  ss << " for (int i = ";
901  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
902  {
903  ss << "gid0; i < " << pDVR->GetArrayLength();
904  ss << " && i < " << nCurWindowSize << "; i++)\n";
905  ss << " {\n";
906  }
907  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
908  {
909  ss << "0; i < " << pDVR->GetArrayLength();
910  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
911  ss << " {\n";
912  }
913  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
914  {
915  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
916  ss << " && i < " << nCurWindowSize << "; i++)\n";
917  ss << " {\n";
918  }
919  else
920  {
921  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
922  ss << " {\n";
923  }
924 
925  ss << " arg = ";
926  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
927  ss << ";\n";
928  ss << " if (isnan(arg))\n";
929  ss << " continue;\n";
930  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
931  ss << " }\n";
932  }
933  else if (pCur->GetType() == formula::svSingleVectorRef)
934  {
935  const formula::SingleVectorRefToken* pSVR =
936  static_cast< const formula::SingleVectorRefToken*>(pCur);
937  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
938  ss << " {\n";
939  ss << " arg = ";
940  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
941  ss << " if (!isnan(arg))\n";
942  ss << " {\n";
943  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
944  ss << " }\n";
945  ss << " }\n";
946  }
947  else
948  {
949  ss << " arg = " << pCur->GetDouble() << ";\n";
950  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
951  }
952  }
953  else
954  {
955  ss << " arg = ";
956  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
957  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
958  }
959  }
960  ss << " if (fCount == 0.0)\n";
961  ss << " return DBL_MAX;\n";
962  ss << " else\n";
963  ss << " return vSum * pow(fCount,-1.0);\n";
964  ss << "}\n";
965 }
966 
967 void OpTDist::BinInlineFun(std::set<std::string>& decls,
968  std::set<std::string>& funs)
969 {
970  decls.insert(fMachEpsDecl);
971  funs.insert("");
972  decls.insert(fMaxGammaArgumentDecl);
973  funs.insert("");
974  decls.insert(lcl_getLanczosSumDecl);
975  funs.insert(lcl_getLanczosSum);
976  decls.insert(GetBetaDecl);
977  funs.insert(GetBeta);
978  decls.insert(GetLogBetaDecl);
979  funs.insert(GetLogBeta);
980  decls.insert(GetBetaDistPDFDecl);
981  funs.insert(GetBetaDistPDF);
982  decls.insert(lcl_GetBetaHelperContFracDecl);
983  funs.insert(lcl_GetBetaHelperContFrac);
984  decls.insert(GetBetaDistDecl);
985  funs.insert(GetBetaDist);
986  decls.insert(GetTDistDecl);
987  funs.insert(GetTDist);
988 }
989 void OpTDist::GenSlidingWindowFunction(std::stringstream &ss,
990  const std::string &sSymName, SubArguments &vSubArguments)
991 {
992  ss << "\ndouble " << sSymName;
993  ss << "_" << BinFuncName() << "(";
994  for (size_t i = 0; i < vSubArguments.size(); i++)
995  {
996  if (i)
997  ss << ",";
998  vSubArguments[i]->GenSlidingWindowDecl(ss);
999  }
1000  ss << ") {\n";
1001  ss << " int gid0 = get_global_id(0);\n";
1002  ss << " double x = 0.0;\n";
1003  ss << " double fDF = 0.0;\n";
1004  ss << " double fFlag = 0.0;\n";
1005  if(vSubArguments.size() != 3)
1006  {
1007  ss << " return DBL_MAX;\n}\n";
1008  return ;
1009  }
1010  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1011  assert(tmpCur0);
1012  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1013  {
1014  if(tmpCur0->GetType() == formula::svSingleVectorRef)
1015  {
1016  const formula::SingleVectorRefToken*tmpCurDVR0 =
1017  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1018  ss << " if(gid0 < ";
1019  ss << tmpCurDVR0->GetArrayLength() << ")\n";
1020  ss << " {\n";
1021  ss << " x = ";
1022  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1023  ss << " if(isnan(x))\n";
1024  ss << " x = 0.0;\n";
1025  ss << " }\n";
1026  }
1027  else if(tmpCur0->GetType() == formula::svDouble)
1028  {
1029  ss << " x = " << tmpCur0->GetDouble() << ";\n";
1030  }
1031  else
1032  {
1033  ss << " return DBL_MAX;\n}\n";
1034  return ;
1035  }
1036  }
1037  else
1038  {
1039  ss << " x = ";
1040  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1041  }
1042  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1043  assert(tmpCur1);
1044  if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
1045  {
1046  if(tmpCur1->GetType() == formula::svSingleVectorRef)
1047  {
1048  const formula::SingleVectorRefToken*tmpCurDVR1 =
1049  static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
1050  ss << " if(gid0 < ";
1051  ss << tmpCurDVR1->GetArrayLength() << ")\n";
1052  ss << " {\n";
1053  ss << " fDF = ";
1054  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1055  ss << " if(isnan(fDF))\n";
1056  ss << " fDF = 0.0;\n";
1057  ss << " else\n";
1058  ss << " fDF = floor(fDF);\n";
1059  ss << " }\n";
1060  }
1061  else if(tmpCur1->GetType() == formula::svDouble)
1062  {
1063  ss << " fDF = floor(convert_double(";
1064  ss << tmpCur1->GetDouble() << "));\n";
1065  }
1066  else
1067  {
1068  ss << " return DBL_MAX;\n}\n";
1069  return ;
1070  }
1071  }
1072  else
1073  {
1074  ss << " fDF = floor(";
1075  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
1076  }
1077 
1078  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1079  assert(tmpCur2);
1080  if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
1081  {
1082  if(tmpCur2->GetType() == formula::svSingleVectorRef)
1083  {
1084  const formula::SingleVectorRefToken*tmpCurDVR2 =
1085  static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
1086  ss << " if(gid0 < ";
1087  ss << tmpCurDVR2->GetArrayLength() << ")\n";
1088  ss << " {\n";
1089  ss << " fFlag = ";
1090  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
1091  ss << " if(isnan(fFlag))\n";
1092  ss << " fFlag = 0.0;\n";
1093  ss << " else\n";
1094  ss << " fFlag = floor(fFlag);\n";
1095  ss << " }\n";
1096 
1097  }
1098  else if(tmpCur2->GetType() == formula::svDouble)
1099  {
1100  ss << " fFlag = floor(convert_double(";
1101  ss << tmpCur2->GetDouble() << "));\n";
1102  }
1103  else
1104  {
1105  ss << " return DBL_MAX;\n}\n";
1106  return ;
1107  }
1108  }
1109  else
1110  {
1111  ss << " fFlag = floor(";
1112  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ");\n";
1113  }
1114  ss << " if(fDF < 1.0 || x < 0.0 || (fFlag != 1.0 && fFlag != 2.0))\n";
1115  ss << " return DBL_MAX;\n";
1116  ss << " double R = GetTDist(x, fDF);\n";
1117  ss << " if (fFlag == 1.0)\n";
1118  ss << " return R;\n";
1119  ss << " else\n";
1120  ss << " return 2.0 * R;\n";
1121  ss << "}\n";
1122 }
1123 void OpExponDist::GenSlidingWindowFunction(std::stringstream &ss,
1124  const std::string &sSymName, SubArguments &vSubArguments)
1125 {
1126  ss << "\ndouble " << sSymName;
1127  ss << "_"<< BinFuncName() <<"(";
1128  for (size_t i = 0; i < vSubArguments.size(); i++)
1129  {
1130  if (i)
1131  ss << ",";
1132  vSubArguments[i]->GenSlidingWindowDecl(ss);
1133  }
1134  ss << ") {\n";
1135  ss << " double tmp = 0,tmp0=0,tmp1=0,tmp2=0;\n";
1136  ss << " int gid0 = get_global_id(0);\n";
1137  ss << " double rx,rlambda,rkum;\n";
1138  ss <<"\n";
1139  for (size_t i = 0; i < vSubArguments.size(); i++)
1140  {
1141  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
1142  assert(pCur);
1143  if (pCur->GetType() == formula::svSingleVectorRef)
1144  {
1145  const formula::SingleVectorRefToken* pSVR =
1146  static_cast< const formula::SingleVectorRefToken* >(pCur);
1147  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
1148  }
1149  else if (pCur->GetType() == formula::svDouble)
1150  {
1151  ss << "{\n";
1152  }
1153 
1154  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
1155  {
1156  ss << " if (isnan(";
1157  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1158  ss << "))\n";
1159  ss << " tmp"<<i<<"= 0;\n";
1160  ss << " else\n";
1161  ss << " tmp"<<i<<"=\n";
1162  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1163  ss << ";\n}\n";
1164  }
1165  else
1166  {
1167  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
1168  ss <<";\n";
1169  }
1170  }
1171  ss << " rx = tmp0;\n";
1172  ss << " rlambda = tmp1;\n";
1173  ss << " rkum = tmp2;\n";
1174  ss <<" if(rlambda <= 0.0)\n";
1175  ss <<" {\n";
1176  ss <<" tmp = -DBL_MAX;\n";
1177  ss <<" }\n";
1178  ss <<" else if(rkum == 0)\n";
1179  ss <<" {\n";
1180  ss <<" if(rx >= 0)\n";
1181  ss <<" tmp = rlambda*exp(-rlambda*rx);\n";
1182  ss <<" else\n";
1183  ss <<" tmp = 0.0;\n";
1184  ss <<" }\n";
1185  ss <<" else\n";
1186  ss <<" {\n";
1187  ss <<" if(rx > 0)\n";
1188  ss <<" tmp = 1.0 - exp(-rlambda*rx);\n";
1189  ss <<" else\n";
1190  ss <<" tmp = 0.0;\n";
1191  ss <<" }\n";
1192  ss <<" return tmp;\n";
1193  ss <<"}";
1194 }
1195 void OpFdist::BinInlineFun(std::set<std::string>& decls,
1196  std::set<std::string>& funs)
1197 {
1198  decls.insert(GetFDistDecl);decls.insert(GetBetaDistDecl);
1199  decls.insert(GetBetaDecl);decls.insert(fMaxGammaArgumentDecl);
1200  decls.insert(lcl_GetBetaHelperContFracDecl);
1201  decls.insert(GetBetaDistPDFDecl);
1202  decls.insert(GetLogBetaDecl);decls.insert(lcl_getLanczosSumDecl);
1203  decls.insert(fMachEpsDecl);
1204  funs.insert(GetFDist);funs.insert(GetBetaDist);
1205  funs.insert(GetBeta);
1206  funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetBetaDistPDF);
1207  funs.insert(GetLogBeta);
1208  funs.insert(lcl_getLanczosSum);
1209 }
1210 void OpFdist::GenSlidingWindowFunction(std::stringstream &ss,
1211  const std::string &sSymName, SubArguments &vSubArguments)
1212 {
1213  ss << "\ndouble " << sSymName;
1214  ss << "_"<< BinFuncName() <<"(";
1215  for (size_t i = 0; i < vSubArguments.size(); i++)
1216  {
1217  if (i)
1218  ss << ",";
1219  vSubArguments[i]->GenSlidingWindowDecl(ss);
1220  }
1221  ss << ") {\n";
1222  ss << " double tmp = 0,tmp0=0,tmp1=0,tmp2=0;\n";
1223  ss << " int gid0 = get_global_id(0);\n";
1224  ss << " double rF1,rF2,rX;\n";
1225  ss <<"\n";
1226  for (size_t i = 0; i < vSubArguments.size(); i++)
1227  {
1228  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
1229  assert(pCur);
1230  if (pCur->GetType() == formula::svSingleVectorRef)
1231  {
1232  const formula::SingleVectorRefToken* pSVR =
1233  static_cast< const formula::SingleVectorRefToken* >(pCur);
1234  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
1235  }
1236  else if (pCur->GetType() == formula::svDouble)
1237  {
1238  ss << "{\n";
1239  }
1240 
1241  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
1242  {
1243  ss << " if (isnan(";
1244  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1245  ss << "))\n";
1246  ss << " tmp"<<i<<"= 0;\n";
1247  ss << " else\n";
1248  ss << " tmp"<<i<<"=\n";
1249  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1250  ss << ";\n}\n";
1251  }
1252  else
1253  {
1254  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
1255  ss <<";\n";
1256  }
1257  }
1258  ss << " rX = tmp0;\n";
1259  ss << " rF1 = floor(tmp1);\n";
1260  ss << " rF2 = floor(tmp2);\n";
1261  ss <<" if (rX < 0.0 || rF1 < 1.0 || rF2 < 1.0 || rF1 >= 1.0E10 ||";
1262  ss <<"rF2 >= 1.0E10)\n";
1263  ss <<" {\n";
1264  ss <<" tmp = -DBL_MAX;\n";
1265  ss <<" }\n";
1266  ss <<" tmp = GetFDist(rX, rF1, rF2);\n";
1267  ss <<" return tmp;\n";
1268  ss <<"}";
1269 }
1270 
1271 void OpStandard::GenSlidingWindowFunction(std::stringstream &ss,
1272  const std::string &sSymName, SubArguments &vSubArguments)
1273 {
1274  ss << "\ndouble " << sSymName;
1275  ss << "_" << BinFuncName() << "(";
1276  for (size_t i = 0; i < vSubArguments.size(); i++)
1277  {
1278  if (i)
1279  ss << ",";
1280  vSubArguments[i]->GenSlidingWindowDecl(ss);
1281  }
1282  ss << ") {\n";
1283  ss << " int gid0 = get_global_id(0);\n";
1284  ss << " double x = 0.0;\n";
1285  ss << " double mu = 0.0;\n";
1286  ss << " double sigma = 0.0;\n";
1287  if(vSubArguments.size() != 3)
1288  {
1289  ss << " return DBL_MAX;\n}\n";
1290  return ;
1291  }
1292  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1293  assert(tmpCur0);
1294  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1295  {
1296  if(tmpCur0->GetType() == formula::svSingleVectorRef)
1297  {
1298  const formula::SingleVectorRefToken*tmpCurSVR0 =
1299  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1300  ss << " if (gid0 < " << tmpCurSVR0->GetArrayLength() << ")\n";
1301  ss << " {\n";
1302  ss << " x = ";
1303  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1304  ss << " if (isnan(x))\n";
1305  ss << " x = 0.0;\n";
1306  ss << " }\n";
1307  }
1308  else if(tmpCur0->GetType() == formula::svDouble)
1309  {
1310  ss << " x = " << tmpCur0->GetDouble() << ";\n";
1311  }
1312  else
1313  {
1314  ss << " return DBL_MAX;\n}\n";
1315  return ;
1316  }
1317  }
1318  else
1319  {
1320  ss << " x = ";
1321  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1322  }
1323 
1324  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1325  assert(tmpCur1);
1326  if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
1327  {
1328  if(tmpCur1->GetType() == formula::svSingleVectorRef)
1329  {
1330  const formula::SingleVectorRefToken*tmpCurSVR1 =
1331  static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
1332  ss << " if (gid0 < " << tmpCurSVR1->GetArrayLength() << ")\n";
1333  ss << " {\n";
1334  ss << " mu = ";
1335  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1336  ss << " if (isnan(mu))\n";
1337  ss << " mu = 0.0;\n";
1338  ss << " }\n";
1339  }
1340  else if(tmpCur1->GetType() == formula::svDouble)
1341  {
1342  ss << " mu = " << tmpCur1->GetDouble() << ";\n";
1343  }
1344  else
1345  {
1346  ss << " return DBL_MAX;\n}\n";
1347  return ;
1348  }
1349  }
1350  else
1351  {
1352  ss << " mu = ";
1353  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1354  }
1355 
1356  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1357  assert(tmpCur2);
1358  if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
1359  {
1360  if(tmpCur2->GetType() == formula::svSingleVectorRef)
1361  {
1362  const formula::SingleVectorRefToken*tmpCurSVR2 =
1363  static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
1364  ss << " if (gid0 < " << tmpCurSVR2->GetArrayLength() << ")\n";
1365  ss << " {\n";
1366  ss << " sigma = ";
1367  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
1368  ss << " if (isnan(sigma))\n";
1369  ss << " sigma = 0.0;\n";
1370  ss << " }\n";
1371  }
1372  else if(tmpCur2->GetType() == formula::svDouble)
1373  {
1374  ss << " sigma = " << tmpCur2->GetDouble() << ";\n";
1375  }
1376  else
1377  {
1378  ss << " return DBL_MAX;\n";
1379  ss << "}\n";
1380  return ;
1381  }
1382  }
1383  else
1384  {
1385  ss << " sigma = ";
1386  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
1387  }
1388 
1389  ss << " if(sigma <= 0.0)\n";
1390  ss << " return DBL_MAX;\n";
1391  ss << " else\n";
1392  ss << " return (x - mu)*pow(sigma,-1.0);\n";
1393  ss << "}";
1394 }
1395 
1396 void OpWeibull::GenSlidingWindowFunction(std::stringstream &ss,
1397  const std::string &sSymName, SubArguments &vSubArguments)
1398 {
1399  ss << "\ndouble " << sSymName;
1400  ss << "_" << BinFuncName() << "(";
1401  for (size_t i = 0; i < vSubArguments.size(); i++)
1402  {
1403  if (i)
1404  ss << ",";
1405  vSubArguments[i]->GenSlidingWindowDecl(ss);
1406  }
1407  ss << ") {\n";
1408  ss << " int gid0 = get_global_id(0);\n";
1409  ss << " double x = 0.0;\n";
1410  ss << " double alpha = 0.0;\n";
1411  ss << " double beta = 0.0;\n";
1412  ss << " double kum = 0.0;\n";
1413  if(vSubArguments.size() != 4)
1414  {
1415  ss << " return DBL_MAX;\n}\n";
1416  return ;
1417  }
1418  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1419  assert(tmpCur0);
1420  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
1421  {
1422  if(tmpCur0->GetType() == formula::svSingleVectorRef)
1423  {
1424  const formula::SingleVectorRefToken*tmpCurSVR0 =
1425  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
1426  ss << " if (gid0 < " << tmpCurSVR0->GetArrayLength() << ")\n";
1427  ss << " {\n";
1428  ss << " x = ";
1429  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1430  ss << " if (isnan(x))\n";
1431  ss << " x = 0.0;\n";
1432  ss << " }\n";
1433  }
1434  else if(tmpCur0->GetType() == formula::svDouble)
1435  {
1436  ss << " x = " << tmpCur0->GetDouble() << ";\n";
1437  }
1438  else
1439  {
1440  ss << " return DBL_MAX;\n}\n";
1441  return ;
1442  }
1443  }
1444  else
1445  {
1446  ss << " x = ";
1447  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1448  }
1449 
1450  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1451  assert(tmpCur1);
1452  if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
1453  {
1454  if(tmpCur1->GetType() == formula::svSingleVectorRef)
1455  {
1456  const formula::SingleVectorRefToken*tmpCurSVR1 =
1457  static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
1458  ss << " if (gid0 < " << tmpCurSVR1->GetArrayLength() << ")\n";
1459  ss << " {\n";
1460  ss << " alpha = ";
1461  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1462  ss << " if (isnan(alpha))\n";
1463  ss << " alpha = 0.0;\n";
1464  ss << " }\n";
1465  }
1466  else if(tmpCur1->GetType() == formula::svDouble)
1467  {
1468  ss << " alpha = " << tmpCur1->GetDouble() << ";\n";
1469  }
1470  else
1471  {
1472  ss << " return DBL_MAX;\n}\n";
1473  return ;
1474  }
1475  }
1476  else
1477  {
1478  ss << " alpha = ";
1479  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1480  }
1481 
1482  FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1483  assert(tmpCur2);
1484  if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
1485  {
1486  if(tmpCur2->GetType() == formula::svSingleVectorRef)
1487  {
1488  const formula::SingleVectorRefToken*tmpCurSVR2 =
1489  static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
1490  ss << " if (gid0 < " << tmpCurSVR2->GetArrayLength() << ")\n";
1491  ss << " {\n";
1492  ss << " beta = ";
1493  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
1494  ss << " if (isnan(beta))\n";
1495  ss << " beta = 0.0;\n";
1496  ss << " }\n";
1497  }
1498  else if(tmpCur2->GetType() == formula::svDouble)
1499  {
1500  ss << " beta = " << tmpCur2->GetDouble() << ";\n";
1501  }
1502  else
1503  {
1504  ss << " return DBL_MAX;\n}\n";
1505  return ;
1506  }
1507  }
1508  else
1509  {
1510  ss << " beta = ";
1511  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
1512  }
1513 
1514  FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
1515  assert(tmpCur3);
1516  if(ocPush == vSubArguments[3]->GetFormulaToken()->GetOpCode())
1517  {
1518  if(tmpCur3->GetType() == formula::svSingleVectorRef)
1519  {
1520  const formula::SingleVectorRefToken*tmpCurSVR3 =
1521  static_cast<const formula::SingleVectorRefToken *>(tmpCur3);
1522  ss << " if (gid0 < " << tmpCurSVR3->GetArrayLength() << ")\n";
1523  ss << " {\n";
1524  ss << " kum = ";
1525  ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ";\n";
1526  ss << " if (isnan(kum))\n";
1527  ss << " kum = 0.0;\n";
1528  ss << " }\n";
1529  }
1530  else if(tmpCur3->GetType() == formula::svDouble)
1531  {
1532  ss << " kum = " << tmpCur3->GetDouble() << ";\n";
1533  }
1534  else
1535  {
1536  ss << " return DBL_MAX;\n}\n";
1537  return ;
1538  }
1539  }
1540  else
1541  {
1542  ss << " kum = ";
1543  ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ";\n";
1544  }
1545 
1546  ss << " if(alpha <= 0.0 || beta <=0.0 || kum < 0.0)\n";
1547  ss << " return DBL_MAX;\n";
1548  ss << " else if(kum == 0.0)\n";
1549  ss << " {\n";
1550  ss << " return alpha*pow(pow(beta,alpha),-1.0)*pow(x,alpha-1.0)";
1551  ss << "*exp(-pow(x*pow(beta,-1.0),alpha));\n";
1552  ss << " }\n";
1553  ss << " else\n";
1554  ss << " return 1.0-exp(-pow(x*pow(beta,-1.0),alpha));\n";
1555  ss << "}\n";
1556 }
1557 
1558 void OpSkew::GenSlidingWindowFunction(std::stringstream &ss,
1559  const std::string &sSymName, SubArguments &vSubArguments)
1560 {
1561  ss << "\ndouble " << sSymName;
1562  ss << "_" << BinFuncName() << "(";
1563  for (size_t i = 0; i < vSubArguments.size(); i++)
1564  {
1565  if (i)
1566  ss << ",";
1567  vSubArguments[i]->GenSlidingWindowDecl(ss);
1568  }
1569  ss << "){\n";
1570  ss << " int gid0 = get_global_id(0);\n";
1571  ss << " double fSum = 0.0;\n";
1572  ss << " double fMean = 0.0;\n";
1573  ss << " double vSum = 0.0;\n";
1574  ss << " double fCount = 0.0;\n";
1575  ss << " double arg = 0.0;\n";
1576  unsigned i = vSubArguments.size();
1577  while (i--)
1578  {
1579  FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
1580  assert(pCur);
1581  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1582  {
1583  if (pCur->GetType() == formula::svDoubleVectorRef)
1584  {
1585  const formula::DoubleVectorRefToken* pDVR =
1586  static_cast<const formula::DoubleVectorRefToken *>(pCur);
1587  size_t nCurWindowSize = pDVR->GetRefRowSize();
1588  ss << " for (int i = ";
1589  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1590  {
1591  ss << "gid0; i < " << pDVR->GetArrayLength();
1592  ss << " && i < " << nCurWindowSize << "; i++)\n";
1593  ss << " {\n";
1594  }
1595  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1596  {
1597  ss << "0; i < " << pDVR->GetArrayLength();
1598  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
1599  ss << " {\n";
1600  }
1601  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1602  {
1603  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
1604  ss << " && i < " << nCurWindowSize << "; i++)\n";
1605  ss << " {\n";
1606  }
1607  else
1608  {
1609  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
1610  ss << " {\n";
1611  }
1612 
1613  ss << " arg = ";
1614  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1615  ss << " if (isnan(arg))\n";
1616  ss << " continue;\n";
1617  ss << " fSum += arg;\n";
1618  ss << " fCount += 1.0;\n";
1619  ss << " }\n";
1620  }
1621  else if (pCur->GetType() == formula::svSingleVectorRef)
1622  {
1623  const formula::SingleVectorRefToken* pSVR =
1624  static_cast< const formula::SingleVectorRefToken* >(pCur);
1625  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
1626  ss << " {\n";
1627  ss << " arg = ";
1628  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1629  ss << " if (!isnan(arg))\n";
1630  ss << " {\n";
1631  ss << " fSum += arg;\n";
1632  ss << " fCount += 1.0;\n";
1633  ss << " }\n";
1634  ss << " }\n";
1635  }
1636  else
1637  {
1638  ss << " arg = " << pCur->GetDouble() << ";\n";
1639  ss << " fSum += arg;\n";
1640  ss << " fCount += 1.0;\n";
1641  }
1642  }
1643  else
1644  {
1645  ss << " arg = ";
1646  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1647  ss << " fSum += arg;\n";
1648  ss << " fCount += 1.0;\n";
1649  }
1650 
1651  if(i == 0)
1652  {
1653  ss << " if(fCount <= 2.0)\n";
1654  ss << " return DBL_MAX;\n";
1655  ss << " else\n";
1656  ss << " fMean = fSum * pow(fCount,-1.0);\n";
1657  }
1658  }
1659  i = vSubArguments.size();
1660  while (i--)
1661  {
1662  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
1663  assert(pCur);
1664  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1665  {
1666  if (pCur->GetType() == formula::svDoubleVectorRef)
1667  {
1668  const formula::DoubleVectorRefToken* pDVR =
1669  static_cast<const formula::DoubleVectorRefToken *>(pCur);
1670  size_t nCurWindowSize = pDVR->GetRefRowSize();
1671  ss << " for (int i = ";
1672  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1673  {
1674  ss << "gid0; i < " << pDVR->GetArrayLength();
1675  ss << " && i < " << nCurWindowSize << "; i++)\n";
1676  ss << " {\n";
1677  }
1678  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1679  {
1680  ss << "0; i < " << pDVR->GetArrayLength();
1681  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
1682  ss << " {\n";
1683  }
1684  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1685  {
1686  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
1687  ss << " && i < " << nCurWindowSize << "; i++)\n";
1688  ss << " {\n";
1689  }
1690  else
1691  {
1692  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
1693  ss << " {\n";
1694  }
1695 
1696  ss << " arg = ";
1697  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1698  ss << " if (isnan(arg))\n";
1699  ss << " continue;\n";
1700  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1701  ss << " }\n";
1702  }
1703  else if (pCur->GetType() == formula::svSingleVectorRef)
1704  {
1705  const formula::SingleVectorRefToken* pSVR =
1706  static_cast< const formula::SingleVectorRefToken* >(pCur);
1707  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
1708  ss << " {\n";
1709  ss << " arg = ";
1710  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1711  ss << " if (!isnan(arg))\n";
1712  ss << " {\n";
1713  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1714  ss << " }\n";
1715  ss << " }\n";
1716  }
1717  else
1718  {
1719  ss << " arg = " << pCur->GetDouble() << ";\n";
1720  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1721  }
1722  }
1723  else
1724  {
1725  ss << " arg = ";
1726  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1727  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1728  }
1729  }
1730  ss << " double fStdDev = sqrt(vSum * pow(fCount - 1.0,-1.0));\n";
1731  ss << " double dx = 0.0;\n";
1732  ss << " double xcube = 0.0;\n";
1733  ss << " if(fStdDev == 0.0)\n";
1734  ss << " return DBL_MAX;\n";
1735  i = vSubArguments.size();
1736  while (i--)
1737  {
1738  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
1739  assert(pCur);
1740  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1741  {
1742  if (pCur->GetType() == formula::svDoubleVectorRef)
1743  {
1744  const formula::DoubleVectorRefToken* pDVR =
1745  static_cast<const formula::DoubleVectorRefToken *>(pCur);
1746  size_t nCurWindowSize = pDVR->GetRefRowSize();
1747  ss << " for (int i = ";
1748  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1749  {
1750  ss << "gid0; i < " << pDVR->GetArrayLength();
1751  ss << " && i < " << nCurWindowSize << "; i++)\n";
1752  ss << " {\n";
1753  }
1754  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1755  {
1756  ss << "0; i < " << pDVR->GetArrayLength();
1757  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
1758  ss << " {\n";
1759  }
1760  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1761  {
1762  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
1763  ss << " && i < " << nCurWindowSize << "; i++)\n";
1764  ss << " {\n";
1765  }
1766  else
1767  {
1768  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
1769  ss << " {\n";
1770  }
1771 
1772  ss << " arg = ";
1773  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1774  ss << " if (isnan(arg))\n";
1775  ss << " continue;\n";
1776  ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
1777  ss << " xcube = xcube + dx * dx * dx;\n";
1778  ss << " }\n";
1779  }
1780  else if (pCur->GetType() == formula::svSingleVectorRef)
1781  {
1782  const formula::SingleVectorRefToken* pSVR =
1783  static_cast< const formula::SingleVectorRefToken* >(pCur);
1784  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
1785  ss << " {\n";
1786  ss << " arg = ";
1787  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1788  ss << " if (!isnan(arg))\n";
1789  ss << " {\n";
1790  ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
1791  ss << " xcube = xcube + dx * dx * dx;\n";
1792  ss << " }\n";
1793  ss << " }\n";
1794  }
1795  else
1796  {
1797  ss << " arg = " << pCur->GetDouble() << ";\n";
1798  ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
1799  ss << " xcube = xcube + dx * dx * dx;\n";
1800  }
1801  }
1802  else
1803  {
1804  ss << " arg = ";
1805  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1806  ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
1807  ss << " xcube = xcube + dx * dx * dx;\n";
1808  }
1809  }
1810  ss << " return ((xcube * fCount) * pow(fCount - 1.0,-1.0))";
1811  ss << " * pow(fCount - 2.0,-1.0);\n";
1812  ss << "}\n";
1813 }
1814 
1815 void OpSkewp::GenSlidingWindowFunction(std::stringstream &ss,
1816  const std::string &sSymName, SubArguments &vSubArguments)
1817 {
1818  ss << "\ndouble " << sSymName;
1819  ss << "_" << BinFuncName() << "(";
1820  for (size_t i = 0; i < vSubArguments.size(); i++)
1821  {
1822  if (i)
1823  ss << ",";
1824  vSubArguments[i]->GenSlidingWindowDecl(ss);
1825  }
1826  ss << "){\n";
1827  ss << " int gid0 = get_global_id(0);\n";
1828  ss << " double fSum = 0.0;\n";
1829  ss << " double fMean = 0.0;\n";
1830  ss << " double vSum = 0.0;\n";
1831  ss << " double fCount = 0.0;\n";
1832  ss << " double arg = 0.0;\n";
1833  unsigned i = vSubArguments.size();
1834  while (i--)
1835  {
1836  FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
1837  assert(pCur);
1838  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1839  {
1840  if (pCur->GetType() == formula::svDoubleVectorRef)
1841  {
1842  const formula::DoubleVectorRefToken* pDVR =
1843  static_cast<const formula::DoubleVectorRefToken *>(pCur);
1844  size_t nCurWindowSize = pDVR->GetRefRowSize();
1845  ss << " for (int i = ";
1846  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1847  {
1848  ss << "gid0; i < " << pDVR->GetArrayLength();
1849  ss << " && i < " << nCurWindowSize << "; i++)\n";
1850  ss << " {\n";
1851  }
1852  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1853  {
1854  ss << "0; i < " << pDVR->GetArrayLength();
1855  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
1856  ss << " {\n";
1857  }
1858  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1859  {
1860  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
1861  ss << " && i < " << nCurWindowSize << "; i++)\n";
1862  ss << " {\n";
1863  }
1864  else
1865  {
1866  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
1867  ss << " {\n";
1868  }
1869 
1870  ss << " arg = ";
1871  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1872  ss << " if (isnan(arg))\n";
1873  ss << " continue;\n";
1874  ss << " fSum += arg;\n";
1875  ss << " fCount += 1.0;\n";
1876  ss << " }\n";
1877  }
1878  else if (pCur->GetType() == formula::svSingleVectorRef)
1879  {
1880  const formula::SingleVectorRefToken* pSVR =
1881  static_cast< const formula::SingleVectorRefToken* >(pCur);
1882  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
1883  ss << " {\n";
1884  ss << " arg = ";
1885  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1886  ss << " if (!isnan(arg))\n";
1887  ss << " {\n";
1888  ss << " fSum += arg;\n";
1889  ss << " fCount += 1.0;\n";
1890  ss << " }\n";
1891  ss << " }\n";
1892  }
1893  else
1894  {
1895  ss << " arg = " << pCur->GetDouble() << ";\n";
1896  ss << " fSum += arg;\n";
1897  ss << " fCount += 1.0;\n";
1898  }
1899  }
1900  else
1901  {
1902  ss << " arg = ";
1903  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1904  ss << " fSum += arg;\n";
1905  ss << " fCount += 1.0;\n";
1906  }
1907 
1908  if(i == 0)
1909  {
1910  ss << " if(fCount <= 2.0)\n";
1911  ss << " return DBL_MAX;\n";
1912  ss << " else\n";
1913  ss << " fMean = fSum * pow(fCount,-1.0);\n";
1914  }
1915  }
1916  i = vSubArguments.size();
1917  while (i--)
1918  {
1919  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
1920  assert(pCur);
1921  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1922  {
1923  if (pCur->GetType() == formula::svDoubleVectorRef)
1924  {
1925  const formula::DoubleVectorRefToken* pDVR =
1926  static_cast<const formula::DoubleVectorRefToken *>(pCur);
1927  size_t nCurWindowSize = pDVR->GetRefRowSize();
1928  ss << " for (int i = ";
1929  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
1930  {
1931  ss << "gid0; i < " << pDVR->GetArrayLength();
1932  ss << " && i < " << nCurWindowSize << "; i++)\n";
1933  ss << " {\n";
1934  }
1935  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1936  {
1937  ss << "0; i < " << pDVR->GetArrayLength();
1938  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
1939  ss << " {\n";
1940  }
1941  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
1942  {
1943  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
1944  ss << " && i < " << nCurWindowSize << "; i++)\n";
1945  ss << " {\n";
1946  }
1947  else
1948  {
1949  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
1950  ss << " {\n";
1951  }
1952 
1953  ss << " arg = ";
1954  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1955  ss << " if (isnan(arg))\n";
1956  ss << " continue;\n";
1957  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1958  ss << " }\n";
1959  }
1960  else if (pCur->GetType() == formula::svSingleVectorRef)
1961  {
1962  const formula::SingleVectorRefToken* pSVR =
1963  static_cast< const formula::SingleVectorRefToken* >(pCur);
1964  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
1965  ss << " {\n";
1966  ss << " arg = ";
1967  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1968  ss << " if (!isnan(arg))\n";
1969  ss << " {\n";
1970  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1971  ss << " }\n";
1972  ss << " }\n";
1973  }
1974  else
1975  {
1976  ss << " arg = " << pCur->GetDouble() << ";\n";
1977  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1978  }
1979  }
1980  else
1981  {
1982  ss << " arg = ";
1983  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1984  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
1985  }
1986  }
1987  ss << " double fStdDev = sqrt(vSum * pow(fCount,-1.0));\n";
1988  ss << " double dx = 0.0;\n";
1989  ss << " double xcube = 0.0;\n";
1990  ss << " if(fStdDev == 0.0)\n";
1991  ss << " return DBL_MAX;\n";
1992  i = vSubArguments.size();
1993  while (i--)
1994  {
1995  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
1996  assert(pCur);
1997  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
1998  {
1999  if (pCur->GetType() == formula::svDoubleVectorRef)
2000  {
2001  const formula::DoubleVectorRefToken* pDVR =
2002  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2003  size_t nCurWindowSize = pDVR->GetRefRowSize();
2004  ss << " for (int i = ";
2005  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2006  {
2007  ss << "gid0; i < " << pDVR->GetArrayLength();
2008  ss << " && i < " << nCurWindowSize << "; i++)\n";
2009  ss << " {\n";
2010  }
2011  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2012  {
2013  ss << "0; i < " << pDVR->GetArrayLength();
2014  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2015  ss << " {\n";
2016  }
2017  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2018  {
2019  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2020  ss << " && i < " << nCurWindowSize << "; i++)\n";
2021  ss << " {\n";
2022  }
2023  else
2024  {
2025  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
2026  ss << " {\n";
2027  }
2028 
2029  ss << " arg = ";
2030  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2031  ss << " if (isnan(arg))\n";
2032  ss << " continue;\n";
2033  ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
2034  ss << " xcube = xcube + dx * dx * dx;\n";
2035  ss << " }\n";
2036  }
2037  else if (pCur->GetType() == formula::svSingleVectorRef)
2038  {
2039  const formula::SingleVectorRefToken* pSVR =
2040  static_cast< const formula::SingleVectorRefToken* >(pCur);
2041  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
2042  ss << " {\n";
2043  ss << " arg = ";
2044  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2045  ss << " if (!isnan(arg))\n";
2046  ss << " {\n";
2047  ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
2048  ss << " xcube = xcube + dx * dx * dx;\n";
2049  ss << " }\n";
2050  ss << " }\n";
2051  }
2052  else
2053  {
2054  ss << " arg = " << pCur->GetDouble() << ";\n";
2055  ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
2056  ss << " xcube = xcube + dx * dx * dx;\n";
2057  }
2058  }
2059  else
2060  {
2061  ss << " arg = ";
2062  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2063  ss << " dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
2064  ss << " xcube = xcube + dx * dx * dx;\n";
2065  }
2066  }
2067  ss << " return xcube * pow(fCount,-1.0);\n";
2068  ss << "}\n";
2069 }
2070 
2071 void OpTInv::BinInlineFun(std::set<std::string>& decls,
2072  std::set<std::string>& funs)
2073 {
2074  decls.insert(fMachEpsDecl);
2075  funs.insert("");
2076  decls.insert(fMaxGammaArgumentDecl);
2077  funs.insert("");
2078  decls.insert(lcl_getLanczosSumDecl);
2079  funs.insert(lcl_getLanczosSum);
2080  decls.insert(GetBetaDecl);
2081  funs.insert(GetBeta);
2082  decls.insert(GetLogBetaDecl);
2083  funs.insert(GetLogBeta);
2084  decls.insert(GetBetaDistPDFDecl);
2085  funs.insert(GetBetaDistPDF);
2086  decls.insert(lcl_GetBetaHelperContFracDecl);
2087  funs.insert(lcl_GetBetaHelperContFrac);
2088  decls.insert(GetBetaDistDecl);
2089  funs.insert(GetBetaDist);
2090  decls.insert(GetTDistDecl);
2091  funs.insert(GetTDist);
2092  decls.insert(GetValueDecl);
2093  funs.insert(GetValue);
2094  decls.insert(lcl_HasChangeOfSignDecl);
2095  funs.insert(lcl_HasChangeOfSign);
2096  decls.insert(lcl_IterateInverseDecl);
2097  funs.insert(lcl_IterateInverse);
2098 }
2099 
2100 void OpTInv::GenSlidingWindowFunction(std::stringstream &ss,
2101  const std::string &sSymName, SubArguments &vSubArguments)
2102 {
2103  ss << "\ndouble " << sSymName;
2104  ss << "_" << BinFuncName() << "(";
2105  for (size_t i = 0; i < vSubArguments.size(); i++)
2106  {
2107  if (i)
2108  ss << ",";
2109  vSubArguments[i]->GenSlidingWindowDecl(ss);
2110  }
2111  ss << ") {\n";
2112  ss << " int gid0 = get_global_id(0);\n";
2113  ss << " double x = 0.0;\n";
2114  ss << " double fDF = 0.0;\n";
2115  if(vSubArguments.size() != 2)
2116  {
2117  ss << " return DBL_MAX;\n}\n";
2118  return ;
2119  }
2120  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
2121  assert(tmpCur0);
2122  if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
2123  {
2124  if(tmpCur0->GetType() == formula::svSingleVectorRef)
2125  {
2126  const formula::SingleVectorRefToken*tmpCurDVR0 =
2127  static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
2128  ss << " if(gid0 < ";
2129  ss << tmpCurDVR0->GetArrayLength() << ")\n";
2130  ss << " {\n";
2131  ss << " x = ";
2132  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2133  ss << " if(isnan(x))\n";
2134  ss << " x = 0.0;\n";
2135  ss << " }\n";
2136  }
2137  else if(tmpCur0->GetType() == formula::svDouble)
2138  {
2139  ss << " x = " << tmpCur0->GetDouble() << ";\n";
2140  }
2141  else
2142  {
2143  ss << " return DBL_MAX;\n}\n";
2144  return ;
2145  }
2146  }
2147  else
2148  {
2149  ss << " x = ";
2150  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2151  }
2152  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
2153  assert(tmpCur1);
2154  if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
2155  {
2156  if(tmpCur1->GetType() == formula::svSingleVectorRef)
2157  {
2158  const formula::SingleVectorRefToken*tmpCurDVR1 =
2159  static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
2160  ss << " if(gid0 < ";
2161  ss << tmpCurDVR1->GetArrayLength() << ")\n";
2162  ss << " {\n";
2163  ss << " fDF = ";
2164  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2165  ss << " if(isnan(fDF))\n";
2166  ss << " fDF = 0.0;\n";
2167  ss << " else\n";
2168  ss << " fDF = floor(fDF);\n";
2169  ss << " }\n";
2170  }
2171  else if(tmpCur1->GetType() == formula::svDouble)
2172  {
2173  ss << " fDF = floor(convert_double(";
2174  ss << tmpCur1->GetDouble() << "));\n";
2175  }
2176  else
2177  {
2178  ss << " return DBL_MAX;\n}\n";
2179  return ;
2180  }
2181  }
2182  else
2183  {
2184  ss << " fDF = floor(";
2185  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
2186  }
2187  ss << " if (x > 1.0||fDF < 1.0 || fDF > 1.0E10 || x <= 0.0)\n";
2188  ss << " return DBL_MAX;\n";
2189  ss << " bool bConvError;\n";
2190  ss << " double fVal = lcl_IterateInverse(\n";
2191  ss << " fDF*0.5, fDF, &bConvError,x,fDF );\n";
2192  ss << " if (bConvError)\n";
2193  ss << " return DBL_MAX;\n";
2194  ss << " return fVal;\n";
2195  ss << "}\n";
2196 }
2197 
2198 void OpStDev::GenSlidingWindowFunction(std::stringstream &ss,
2199  const std::string &sSymName, SubArguments &vSubArguments)
2200 {
2201  ss << "\ndouble " << sSymName;
2202  ss << "_" << BinFuncName() << "(";
2203  for (size_t i = 0; i < vSubArguments.size(); i++)
2204  {
2205  if (i)
2206  ss << ",";
2207  vSubArguments[i]->GenSlidingWindowDecl(ss);
2208  }
2209  ss << "){\n";
2210  ss << " int gid0 = get_global_id(0);\n";
2211  ss << " double fSum = 0.0;\n";
2212  ss << " double vSum = 0.0;\n";
2213  ss << " double fMean = 0.0;\n";
2214  ss << " double fCount = 0.0;\n";
2215  ss << " double arg = 0.0;\n";
2216  unsigned i = vSubArguments.size();
2217  while (i--)
2218  {
2219  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2220  assert(pCur);
2221  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2222  {
2223  if (pCur->GetType() == formula::svDoubleVectorRef)
2224  {
2225  const formula::DoubleVectorRefToken* pDVR =
2226  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2227  size_t nCurWindowSize = pDVR->GetRefRowSize();
2228  ss << " for (int i = ";
2229  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2230  {
2231  ss << "gid0; i < " << pDVR->GetArrayLength();
2232  ss << " && i < " << nCurWindowSize << "; i++)\n";
2233  ss << " {\n";
2234  }
2235  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2236  {
2237  ss << "0; i < " << pDVR->GetArrayLength();
2238  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2239  ss << " {\n";
2240  }
2241  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2242  {
2243  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2244  ss << " && i < " << nCurWindowSize << "; i++)\n";
2245  ss << " {\n";
2246  }
2247  else
2248  {
2249  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
2250  ss << " {\n";
2251  }
2252 
2253  ss << " arg = ";
2254  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2255  ss << " if (isnan(arg))\n";
2256  ss << " continue;\n";
2257  ss << " fSum += arg;\n";
2258  ss << " fCount += 1.0;\n";
2259  ss << " }\n";
2260  }
2261  else if (pCur->GetType() == formula::svSingleVectorRef)
2262  {
2263  const formula::SingleVectorRefToken* pSVR =
2264  static_cast< const formula::SingleVectorRefToken* >(pCur);
2265  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
2266  ss << " {\n";
2267  ss << " arg = ";
2268  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2269  ss << " if (!isnan(arg))\n";
2270  ss << " {\n";
2271  ss << " fSum += arg;\n";
2272  ss << " fCount += 1.0;\n";
2273  ss << " }\n";
2274  ss << " }\n";
2275  }
2276  else
2277  {
2278  ss << " arg = " << pCur->GetDouble() << ";\n";
2279  ss << " fSum += arg;\n";
2280  ss << " fCount += 1.0;\n";
2281  }
2282  }
2283  else
2284  {
2285  ss << " arg = ";
2286  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2287  ss << " fSum += arg;\n";
2288  ss << " fCount += 1.0;\n";
2289  }
2290  if (i == 0)
2291  {
2292  ss << " fMean = fSum * pow(fCount,-1.0);\n";
2293  }
2294  }
2295  i = vSubArguments.size();
2296  while (i--)
2297  {
2298  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2299  assert(pCur);
2300  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2301  {
2302  if (pCur->GetType() == formula::svDoubleVectorRef)
2303  {
2304  const formula::DoubleVectorRefToken* pDVR =
2305  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2306  size_t nCurWindowSize = pDVR->GetRefRowSize();
2307  ss << " for (int i = ";
2308  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2309  {
2310  ss << "gid0; i < " << pDVR->GetArrayLength();
2311  ss << " && i < " << nCurWindowSize << "; i++)\n";
2312  ss << " {\n";
2313  }
2314  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2315  {
2316  ss << "0; i < " << pDVR->GetArrayLength();
2317  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2318  ss << " {\n";
2319  }
2320  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2321  {
2322  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2323  ss << " && i < " << nCurWindowSize << "; i++)\n";
2324  ss << " {\n";
2325  }
2326  else
2327  {
2328  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
2329  ss << " {\n";
2330  }
2331  ss << " arg = ";
2332  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2333  ss << " if (isnan(arg))\n";
2334  ss << " continue;\n";
2335  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2336  ss << " }\n";
2337  }
2338  else if (pCur->GetType() == formula::svSingleVectorRef)
2339  {
2340  const formula::SingleVectorRefToken* pSVR =
2341  static_cast< const formula::SingleVectorRefToken* >(pCur);
2342  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
2343  ss << " {\n";
2344  ss << " arg = ";
2345  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2346  ss << " if (!isnan(arg))\n";
2347  ss << " {\n";
2348  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2349  ss << " }\n";
2350  ss << " }\n";
2351  }
2352  else
2353  {
2354  ss << " arg = " << pCur->GetDouble() << ";\n";
2355  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2356  }
2357  }
2358  else
2359  {
2360  ss << " arg = ";
2361  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2362  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2363  }
2364  }
2365  ss << " if (fCount <= 1.0)\n";
2366  ss << " return DBL_MAX;\n";
2367  ss << " else\n";
2368  ss << " return sqrt(vSum * pow(fCount - 1.0,-1.0));\n";
2369  ss << "}\n";
2370 }
2371 
2372 void OpStDevP::GenSlidingWindowFunction(std::stringstream &ss,
2373  const std::string &sSymName, SubArguments &vSubArguments)
2374 {
2375  ss << "\ndouble " << sSymName;
2376  ss << "_" << BinFuncName() << "(";
2377  for (size_t i = 0; i < vSubArguments.size(); i++)
2378  {
2379  if (i)
2380  ss << ",";
2381  vSubArguments[i]->GenSlidingWindowDecl(ss);
2382  }
2383  ss << "){\n";
2384  ss << " int gid0 = get_global_id(0);\n";
2385  ss << " double fSum = 0.0;\n";
2386  ss << " double fMean = 0.0;\n";
2387  ss << " double vSum = 0.0;\n";
2388  ss << " double fCount = 0.0;\n";
2389  ss << " double arg = 0.0;\n";
2390  unsigned i = vSubArguments.size();
2391  while (i--)
2392  {
2393  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2394  assert(pCur);
2395  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2396  {
2397  if (pCur->GetType() == formula::svDoubleVectorRef)
2398  {
2399  const formula::DoubleVectorRefToken* pDVR =
2400  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2401  size_t nCurWindowSize = pDVR->GetRefRowSize();
2402  ss << " for (int i = ";
2403  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2404  {
2405  ss << "gid0; i < " << pDVR->GetArrayLength();
2406  ss << " && i < " << nCurWindowSize << "; i++)\n";
2407  ss << " {\n";
2408  }
2409  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2410  {
2411  ss << "0; i < " << pDVR->GetArrayLength();
2412  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2413  ss << " {\n";
2414  }
2415  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2416  {
2417  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2418  ss << " && i < " << nCurWindowSize << "; i++)\n";
2419  ss << " {\n";
2420  }
2421  else
2422  {
2423  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
2424  ss << " {\n";
2425  }
2426 
2427  ss << " arg = ";
2428  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2429  ss << " if (isnan(arg))\n";
2430  ss << " continue;\n";
2431  ss << " fSum += arg;\n";
2432  ss << " fCount += 1.0;\n";
2433  ss << " }\n";
2434  }
2435  else if (pCur->GetType() == formula::svSingleVectorRef)
2436  {
2437  const formula::SingleVectorRefToken* pSVR =
2438  static_cast< const formula::SingleVectorRefToken* >(pCur);
2439  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
2440  ss << " {\n";
2441  ss << " arg = ";
2442  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2443  ss << " if (!isnan(arg))\n";
2444  ss << " {\n";
2445  ss << " fSum += arg;\n";
2446  ss << " fCount += 1.0;\n";
2447  ss << " }\n";
2448  ss << " }\n";
2449  }
2450  else
2451  {
2452  ss << " arg = " << pCur->GetDouble() << ";\n";
2453  ss << " fSum += arg;\n";
2454  ss << " fCount += 1.0;\n";
2455  }
2456  }
2457  else
2458  {
2459  ss << " arg = ";
2460  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2461  ss << " fSum += arg;\n";
2462  ss << " fCount += 1.0;\n";
2463 
2464  }
2465  if (i == 0)
2466  {
2467  ss << " fMean = fSum * pow(fCount,-1.0);\n";
2468  }
2469  }
2470  i = vSubArguments.size();
2471  while (i--)
2472  {
2473  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2474  assert(pCur);
2475  if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
2476  {
2477  if (pCur->GetType() == formula::svDoubleVectorRef)
2478  {
2479  const formula::DoubleVectorRefToken* pDVR =
2480  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2481  size_t nCurWindowSize = pDVR->GetRefRowSize();
2482  ss << " for (int i = ";
2483  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2484  {
2485  ss << "gid0; i < " << pDVR->GetArrayLength();
2486  ss << " && i < " << nCurWindowSize << "; i++)\n";
2487  ss << " {\n";
2488  }
2489  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2490  {
2491  ss << "0; i < " << pDVR->GetArrayLength();
2492  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2493  ss << " {\n";
2494  }
2495  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2496  {
2497  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2498  ss << " && i < " << nCurWindowSize << "; i++)\n";
2499  ss << " {\n";
2500  }
2501  else
2502  {
2503  ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
2504  ss << " {\n";
2505  }
2506 
2507  ss << " arg = ";
2508  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2509  ss << " if (isnan(arg))\n";
2510  ss << " continue;\n";
2511  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2512  ss << " }\n";
2513  }
2514  else if (pCur->GetType() == formula::svSingleVectorRef)
2515  {
2516  const formula::SingleVectorRefToken* pSVR =
2517  static_cast< const formula::SingleVectorRefToken* >(pCur);
2518  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
2519  ss << " {\n";
2520  ss << " arg = ";
2521  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2522  ss << " if (!isnan(arg))\n";
2523  ss << " {\n";
2524  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2525  ss << " }\n";
2526  ss << " }\n";
2527  }
2528  else
2529  {
2530  ss << " arg = " << pCur->GetDouble() << ";\n";
2531  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2532  }
2533  }
2534  else
2535  {
2536  ss << " arg = ";
2537  ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
2538  ss << " vSum += (arg - fMean) * (arg - fMean);\n";
2539  }
2540  }
2541  ss << " if (fCount == 0.0)\n";
2542  ss << " return DBL_MAX;\n";
2543  ss << " else\n";
2544  ss << " return sqrt(vSum * pow(fCount,-1.0));\n";
2545  ss << "}\n";
2546 }
2547 
2548 void OpSlope::GenSlidingWindowFunction(std::stringstream &ss,
2549  const std::string &sSymName, SubArguments &vSubArguments)
2550 {
2551  CHECK_PARAMETER_COUNT(2,2);
2552  ss << "\ndouble " << sSymName;
2553  ss << "_" << BinFuncName() << "(";
2554  for (size_t i = 0; i < vSubArguments.size(); i++)
2555  {
2556  if (i)
2557  ss << ",";
2558  vSubArguments[i]->GenSlidingWindowDecl(ss);
2559  }
2560  ss << "){\n";
2561  ss << " int gid0 = get_global_id(0);\n";
2562  ss << " double fSumX = 0.0;\n";
2563  ss << " double fSumY = 0.0;\n";
2564  ss << " double fMeanX = 0.0;\n";
2565  ss << " double fMeanY = 0.0;\n";
2566  ss << " double fSumDeltaXDeltaY = 0.0;\n";
2567  ss << " double fSumSqrDeltaX = 0.0;\n";
2568  ss << " double fCount = 0.0;\n";
2569  ss << " double argX = 0.0;\n";
2570  ss << " double argY = 0.0;\n";
2571  FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
2572  FormulaToken *pCur1 = vSubArguments[0]->GetFormulaToken();
2573  assert(pCur);
2574  assert(pCur1);
2575  if (pCur->GetType() == formula::svDoubleVectorRef&&
2576  pCur1->GetType() == formula::svDoubleVectorRef)
2577  {
2578  const formula::DoubleVectorRefToken* pDVR =
2579  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2580  const formula::DoubleVectorRefToken* pDVR1 =
2581  static_cast<const formula::DoubleVectorRefToken *>(pCur1);
2582 
2583  size_t nCurWindowSize = pDVR->GetRefRowSize();
2584  size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
2585  size_t arrayLength = pDVR->GetArrayLength()<
2586  pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
2587  pDVR1->GetArrayLength();
2588  if(nCurWindowSize != nCurWindowSize1)
2589  throw Unhandled(__FILE__, __LINE__);
2590  ss << " for (int i = ";
2591  if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2592  &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2593  {
2594  ss << "gid0; i < " << arrayLength;
2595  ss << " && i < " << nCurWindowSize << "; i++)\n";
2596  ss << " {\n";
2597  }
2598  else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2599  &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2600  {
2601  ss << "0; i < " << arrayLength ;
2602  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2603  ss << " {\n";
2604  }
2605  else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2606  &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2607  {
2608  ss << "0; i + gid0 < " << arrayLength;
2609  ss << " && i < " << nCurWindowSize << "; i++)\n";
2610  ss << " {\n";
2611  }
2612  else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
2613  &&(pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2614  {
2615  ss << "0; i < " << arrayLength << "; i++)\n";
2616  ss << " {\n";
2617  }
2618  else
2619  {
2620  throw Unhandled(__FILE__, __LINE__);
2621  }
2622 
2623  ss << " argX = ";
2624  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2625  ss << " argY = ";
2626  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2627  ss << " if (isnan(argX) || isnan(argY))\n";
2628  ss << " continue;\n";
2629  ss << " fSumX += argX;\n";
2630  ss << " fSumY += argY;\n";
2631  ss << " fCount += 1.0;\n";
2632  ss << " }\n";
2633 
2634  ss << " if (fCount < 1.0)\n";
2635  ss << " return CreateDoubleError(NoValue);\n";
2636  ss << " else\n";
2637  ss << " {\n";
2638  ss << " fMeanX = fSumX * pow(fCount,-1.0);\n";
2639  ss << " fMeanY = fSumY * pow(fCount,-1.0);\n";
2640 
2641  ss << " for (int i = ";
2642  if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2643  &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2644  {
2645  ss << "gid0; i < " << arrayLength;
2646  ss << " && i < " << nCurWindowSize << "; i++)\n";
2647  ss << " {\n";
2648  }
2649  else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2650  &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2651  {
2652  ss << "0; i < " << arrayLength ;
2653  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2654  ss << " {\n";
2655  }
2656  else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2657  &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2658  {
2659  ss << "0; i + gid0 < " << arrayLength;
2660  ss << " && i < " << nCurWindowSize << "; i++)\n";
2661  ss << " {\n";
2662  }
2663  else
2664  {
2665  ss << "0; i < " << arrayLength << "; i++)\n";
2666  ss << " {\n";
2667  }
2668 
2669  ss << " argX = ";
2670  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2671  ss << " argY = ";
2672  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2673  ss << " if (isnan(argX) || isnan(argY))\n";
2674  ss << " continue;\n";
2675  ss << " fSumDeltaXDeltaY += (argX-fMeanX)*(argY-fMeanY);\n";
2676  ss << " fSumSqrDeltaX += (argX-fMeanX) * (argX-fMeanX);\n";
2677  ss << " }\n";
2678  ss << " if(fSumSqrDeltaX == 0.0)\n";
2679  ss << " return CreateDoubleError(DivisionByZero);\n";
2680  ss << " else\n";
2681  ss << " {\n";
2682  ss << " return fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0);\n";
2683  ss << " }\n";
2684  ss << " }\n";
2685  ss << "}\n";
2686  }
2687  else
2688  {
2689  throw Unhandled(__FILE__, __LINE__);
2690  }
2691 }
2692 void OpSTEYX::GenSlidingWindowFunction(std::stringstream &ss,
2693  const std::string &sSymName, SubArguments &vSubArguments)
2694 {
2695  ss << "\ndouble " << sSymName;
2696  ss << "_" << BinFuncName() << "(";
2697  for (size_t i = 0; i < vSubArguments.size(); i++)
2698  {
2699  if (i)
2700  ss << ",";
2701  vSubArguments[i]->GenSlidingWindowDecl(ss);
2702  }
2703  ss << "){\n";
2704  ss << " int gid0 = get_global_id(0);\n";
2705  ss << " double fSumX = 0.0;\n";
2706  ss << " double fSumY = 0.0;\n";
2707  ss << " double fMeanX = 0.0;\n";
2708  ss << " double fMeanY = 0.0;\n";
2709  ss << " double fSumDeltaXDeltaY = 0.0;\n";
2710  ss << " double fSumSqrDeltaX = 0.0;\n";
2711  ss << " double fSumSqrDeltaY = 0.0;\n";
2712  ss << " double fCount = 0.0;\n";
2713  ss << " double argX = 0.0;\n";
2714  ss << " double argY = 0.0;\n";
2715  FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
2716  FormulaToken *pCur1 = vSubArguments[0]->GetFormulaToken();
2717  assert(pCur);
2718  assert(pCur1);
2719  if (pCur->GetType() == formula::svDoubleVectorRef&&
2720  pCur1->GetType() == formula::svDoubleVectorRef)
2721  {
2722  const formula::DoubleVectorRefToken* pDVR =
2723  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2724  const formula::DoubleVectorRefToken* pDVR1 =
2725  static_cast<const formula::DoubleVectorRefToken *>(pCur1);
2726  size_t nCurWindowSize = pDVR->GetRefRowSize();
2727  size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
2728  size_t arrayLength = pDVR->GetArrayLength()<
2729  pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
2730  pDVR1->GetArrayLength();
2731  if(nCurWindowSize != nCurWindowSize1)
2732  {
2733  ss << " return DBL_MAX;\n";
2734  ss << "}\n";
2735  return ;
2736  }
2737  ss << " for (int i = ";
2738  if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2739  &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2740  {
2741  ss << "gid0; i < " << arrayLength;
2742  ss << " && i < " << nCurWindowSize << "; i++)\n";
2743  ss << " {\n";
2744  }
2745  else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2746  &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2747  {
2748  ss << "0; i < " << arrayLength;
2749  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2750  ss << " {\n";
2751  }
2752  else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2753  &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2754  {
2755  ss << "0; i + gid0 < " << arrayLength;
2756  ss << " && i < " << nCurWindowSize << "; i++)\n";
2757  ss << " {\n";
2758  }
2759  else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
2760  &&(pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2761  {
2762  ss << "0; i < " << arrayLength << "; i++)\n";
2763  ss << " {\n";
2764  }
2765  else
2766  {
2767  ss << "0; i < " << nCurWindowSize << "; i++)\n";
2768  ss << " {\n";
2769  ss << " break;\n";
2770  ss << " }";
2771  ss << " return DBL_MAX;\n";
2772  ss << "}\n";
2773  return ;
2774  }
2775 
2776  ss << " argX = ";
2777  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2778  ss << " argY = ";
2779  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2780  ss << " if (isnan(argX) || isnan(argY))\n";
2781  ss << " continue;\n";
2782  ss << " fSumX += argX;\n";
2783  ss << " fSumY += argY;\n";
2784  ss << " fCount += 1.0;\n";
2785  ss << " }\n";
2786 
2787  ss << " if (fCount < 3.0)\n";
2788  ss << " return DBL_MAX;\n";
2789  ss << " else\n";
2790  ss << " {\n";
2791  ss << " fMeanX = fSumX * pow(fCount,-1.0);\n";
2792  ss << " fMeanY = fSumY * pow(fCount,-1.0);\n";
2793 
2794  ss << " for (int i = ";
2795  if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2796  &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2797  {
2798  ss << "gid0; i < " << arrayLength;
2799  ss << " && i < " << nCurWindowSize << "; i++)\n";
2800  ss << " {\n";
2801  }
2802  else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2803  &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2804  {
2805  ss << "0; i < " << arrayLength ;
2806  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2807  ss << " {\n";
2808  }
2809  else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())&&
2810  (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2811  {
2812  ss << "0; i + gid0 < " << arrayLength;
2813  ss << " && i < " << nCurWindowSize << "; i++)\n";
2814  ss << " {\n";
2815  }
2816  else
2817  {
2818  ss << "0; i < " << arrayLength << "; i++)\n";
2819  ss << " {\n";
2820  }
2821 
2822  ss << " argX = ";
2823  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2824  ss << " argY = ";
2825  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2826  ss << " if (isnan(argX)||isnan(argY))\n";
2827  ss << " continue;\n";
2828  ss << " fSumDeltaXDeltaY +=(argX-fMeanX)*(argY-fMeanY);\n";
2829  ss << " fSumSqrDeltaX += (argX-fMeanX)*(argX-fMeanX);\n";
2830  ss << " fSumSqrDeltaY += (argY-fMeanY)*(argY-fMeanY);\n";
2831  ss << " }\n";
2832  ss << " if(fSumSqrDeltaX == 0.0)\n";
2833  ss << " return DBL_MAX;\n";
2834  ss << " else\n";
2835  ss << " {\n";
2836  ss << " return sqrt((fSumSqrDeltaY - fSumDeltaXDeltaY * \n";
2837  ss << " fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0))\n";
2838  ss << " *pow(fCount - 2.0,-1.0));\n";
2839  ss << " }\n";
2840  ss << " }\n";
2841  ss << "}\n";
2842  }
2843  else
2844  {
2845  ss << " return DBL_MAX;\n";
2846  ss << "}\n";
2847  }
2848 }
2849 void OpFisher::GenSlidingWindowFunction(
2850  std::stringstream &ss, const std::string &sSymName, SubArguments &
2851 vSubArguments)
2852 {
2853  ss << "\ndouble " << sSymName;
2854  ss << "_"<< BinFuncName() <<"(";
2855  for (size_t i = 0; i < vSubArguments.size(); i++)
2856  {
2857  if (i)
2858  ss << ",";
2859  vSubArguments[i]->GenSlidingWindowDecl(ss);
2860  }
2861  ss << ") {\n";
2862  ss <<" int gid0=get_global_id(0);\n";
2863  ss <<" double arg0;\n";
2864  if(vSubArguments.size() != 1)
2865  {
2866  ss << " return DBL_MAX;\n";
2867  return ;
2868  }
2869  FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
2870  assert(pCur);
2871  if (pCur->GetType() == formula::svDoubleVectorRef)
2872  {
2873  ss << " return DBL_MAX;\n";
2874  return ;
2875  }
2876  else if (pCur->GetType() == formula::svSingleVectorRef)
2877  {
2878  const formula::SingleVectorRefToken* pSVR =
2879  static_cast< const formula::SingleVectorRefToken* >(pCur);
2880  ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2881  ss << ";\n";
2882  ss<< " if(isnan(arg0)||(gid0>=";
2883  ss<<pSVR->GetArrayLength();
2884  ss<<"))\n";
2885  ss<<" arg0 = 0;\n";
2886  }
2887  else if (pCur->GetType() == formula::svDouble)
2888  {
2889  ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2890  ss << ";\n";
2891  ss << " if(isnan(arg0))\n";
2892  ss << " return DBL_MAX;\n";
2893  }
2894  ss << " if (fabs(arg0) >= 1.0)\n";
2895  ss << " return DBL_MAX;\n";
2896  ss << " double tmp=0.5*log((1+arg0)*pow((1-arg0),-1));\n";
2897  ss << " return tmp;\n";
2898  ss << "}\n";
2899 }
2900 
2901 void OpFisherInv::GenSlidingWindowFunction(
2902  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
2903 {
2904  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
2905  const formula::SingleVectorRefToken*tmpCurDVR = static_cast<const
2906  formula::SingleVectorRefToken *>(tmpCur);
2907  ss << "\ndouble " << sSymName;
2908  ss << "_"<< BinFuncName() <<"(";
2909  for (size_t i = 0; i < vSubArguments.size(); i++)
2910  {
2911  if (i)
2912  ss << ",";
2913  vSubArguments[i]->GenSlidingWindowDecl(ss);
2914  }
2915  ss << ") {\n\t";
2916  ss <<"int gid0=get_global_id(0);\n\t";
2917  ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2918  ss << ";\n\t";
2919  ss<< "if(isnan(arg0)||(gid0>=";
2920  ss<<tmpCurDVR->GetArrayLength();
2921  ss<<"))\n\t\t";
2922  ss<<"arg0 = 0;\n\t";
2923  ss << "double tmp=tanh(arg0);\n\t";
2924  ss << "return tmp;\n";
2925  ss << "}\n";
2926 }
2927 
2928 void OpGamma::GenSlidingWindowFunction(
2929  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
2930 {
2931  ss << "\ndouble " << sSymName;
2932  ss << "_"<< BinFuncName() <<"(";
2933  for (size_t i = 0; i < vSubArguments.size(); i++)
2934  {
2935  if (i)
2936  ss << ",";
2937  vSubArguments[i]->GenSlidingWindowDecl(ss);
2938  }
2939  ss << ") {\n\t";
2940  ss <<"int gid0=get_global_id(0);\n\t";
2941  ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2942  ss << ";\n\t";
2943  ss << "double tmp=tgamma(arg0);\n\t";
2944  ss << "return tmp;\n";
2945  ss << "}\n";
2946 }
2947 
2948 void OpCorrel::GenSlidingWindowFunction(
2949  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
2950 {
2951  if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
2952  ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
2953  ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
2955  throw Unhandled(__FILE__, __LINE__);
2956  const formula::DoubleVectorRefToken* pCurDVRX =
2957  static_cast<const formula::DoubleVectorRefToken *>(
2958  vSubArguments[0]->GetFormulaToken());
2959  const formula::DoubleVectorRefToken* pCurDVRY =
2960  static_cast<const formula::DoubleVectorRefToken *>(
2961  vSubArguments[1]->GetFormulaToken());
2962  if( pCurDVRX->GetRefRowSize() != pCurDVRY->GetRefRowSize() )
2963  throw Unhandled(__FILE__, __LINE__);
2964 
2965  ss << "\ndouble " << sSymName;
2966  ss << "_"<< BinFuncName() <<"(";
2967  for (size_t i = 0; i < vSubArguments.size(); i++)
2968  {
2969  if (i)
2970  ss << ",";
2971  vSubArguments[i]->GenSlidingWindowDecl(ss);
2972  }
2973  ss << ") {\n\t";
2974  ss << "double vSum = 0.0;\n\t";
2975  ss << "double vXSum = 0.0;\n\t";
2976  ss << "double vYSum = 0.0;\n\t";
2977  ss << "double vXMean = 0.0;\n\t";
2978  ss << "double vYMean = 0.0;\n\t";
2979 
2980  ss << "int gid0 = get_global_id(0);\n\t";
2981  ss << "double arg0 = 0.0;\n\t";
2982  ss << "double arg1 = 0.0;\n\t";
2983  ss << "int cnt = 0;\n\t";
2984 
2985  size_t nCurWindowSizeX = pCurDVRY->GetRefRowSize();
2986 
2987  ss << "for (int i = ";
2988  if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
2989  ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
2990  ss << "arg0 = " << vSubArguments[0]
2991  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
2992  ss << "arg1 = " << vSubArguments[1]
2993  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
2994  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
2995  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
2996  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
2997  ss << "arg0 = 0.0;\n\t\t\t";
2998  ss << "arg1 = 0.0;\n\t\t\t";
2999  ss << "--cnt;\n\t\t";
3000  ss << "}\n\t\t";
3001  ss << "++cnt;\n\t\t";
3002  ss << "vXSum += arg0;\n\t\t";
3003  ss << "vYSum += arg1;\n\t";
3004  ss << "}\n\t";
3005  } else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
3006  ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n\t\t";
3007  ss << "arg0 = " << vSubArguments[0]
3008  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3009  ss << "arg1 = " << vSubArguments[1]
3010  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3011  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
3012  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3013  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
3014  ss << "arg0 = 0.0;\n\t\t\t";
3015  ss << "arg1 = 0.0;\n\t\t\t";
3016  ss << "--cnt;\n\t\t";
3017  ss << "}\n\t\t";
3018  ss << "++cnt;\n\t\t";
3019  ss << "vXSum += arg0;\n\t\t";
3020  ss << "vYSum += arg1;\n\t";
3021  ss << "}\n\t";
3022  }
3023  else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
3024  ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
3025  ss << "arg0 = " << vSubArguments[0]
3026  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3027  ss << "arg1 = " << vSubArguments[1]
3028  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3029  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
3030  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3031  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
3032  ss << "arg0 = 0.0;\n\t\t\t";
3033  ss << "arg1 = 0.0;\n\t\t\t";
3034  ss << "--cnt;\n\t\t";
3035  ss << "}\n\t\t";
3036  ss << "++cnt;\n\t\t";
3037  ss << "vXSum += arg0;\n\t\t";
3038  ss << "vYSum += arg1;\n\t";
3039  ss << "}\n\t";
3040  } else {
3041  ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
3042  ss << "arg0 = " << vSubArguments[0]
3043  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3044  ss << "arg1 = " << vSubArguments[1]
3045  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3046  ss << "if(isnan(arg0) || isnan(arg1) || (i + gid0 >= ";
3047  ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >=";
3048  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
3049  ss << "arg0 = 0.0;\n\t\t\t";
3050  ss << "arg1 = 0.0;\n\t\t\t";
3051  ss << "--cnt;\n\t\t";
3052  ss << "}\n\t\t";
3053  ss << "++cnt;\n\t\t";
3054  ss << "vXSum += arg0;\n\t\t";
3055  ss << "vYSum += arg1;\n\t";
3056  ss << "}\n\t";
3057  }
3058 
3059  ss << "if(cnt < 1) {\n\t\t";
3060  ss << "return DBL_MIN;\n\t";
3061  ss << "}\n\t";
3062  ss << "else {\n\t\t";
3063  ss << "vXMean = vXSum/cnt;\n\t\t";
3064  ss << "vYMean = vYSum/cnt;\n\t\t";
3065  ss << "vXSum = 0.0;\n\t\t";
3066  ss << "vYSum = 0.0;\n\t\t";
3067 
3068  ss << "for (int i = ";
3069  if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
3070  ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3071  ss << "arg0 = " << vSubArguments[0]
3072  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3073  ss << "arg1 = " << vSubArguments[1]
3074  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3075  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
3076  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3077  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3078  ss << "arg0 = vXMean;\n\t\t\t\t";
3079  ss << "arg1 = vYMean;\n\t\t\t";
3080  ss << "}\n\t\t\t";
3081  ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
3082  ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
3083  ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3084  ss << "}\n\t\t";
3085  } else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
3086  ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3087  ss << "arg0 = " << vSubArguments[0]
3088  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3089  ss << "arg1 = " << vSubArguments[1]
3090  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3091  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
3092  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3093  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3094  ss << "arg0 = vXMean;\n\t\t\t\t";
3095  ss << "arg1 = vYMean;\n\t\t\t";
3096  ss << "}\n\t\t\t";
3097  ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
3098  ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
3099  ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3100  ss << "}\n\t\t";
3101  } else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
3102  ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3103  ss << "arg0 = " << vSubArguments[0]
3104  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3105  ss << "arg1 = " << vSubArguments[1]
3106  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3107  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
3108  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3109  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3110  ss << "arg0 = vXMean;\n\t\t\t\t";
3111  ss << "arg1 = vYMean;\n\t\t\t";
3112  ss << "}\n\t\t\t";
3113  ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
3114  ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
3115  ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3116  ss << "}\n\t\t";
3117  } else {
3118  ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3119  ss << "arg0 = " << vSubArguments[0]
3120  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3121  ss << "arg1 = " << vSubArguments[1]
3122  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3123  ss << "if(isnan(arg0) || isnan(arg1) || (i + gid0 >= ";
3124  ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >=";
3125  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3126  ss << "arg0 = vXMean;\n\t\t\t\t";
3127  ss << "arg1 = vYMean;\n\t\t\t";
3128  ss << "}\n\t\t\t";
3129  ss << "vXSum += ((arg0 - vXMean)*(arg0 - vXMean));\n\t\t\t";
3130  ss << "vYSum += ((arg1 - vYMean)*(arg1 - vYMean));\n\t\t\t";
3131  ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3132  ss << "}\n\t\t";
3133  }
3134 
3135  ss << "if(vXSum == 0.0 || vYSum == 0.0) {\n\t\t\t";
3136  ss << "return NAN;\n\t\t";
3137  ss << "}\n\t\t";
3138  ss << "else {\n\t\t\t";
3139  ss << "return vSum/pow(vXSum*vYSum, 0.5);\n\t\t";
3140  ss << "}\n\t";
3141  ss << "}\n";
3142  ss << "}";
3143 }
3144 
3145 void OpNegbinomdist::GenSlidingWindowFunction(
3146  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3147 {
3148  ss << "\ndouble " << sSymName;
3149  ss << "_"<< BinFuncName() <<"(";
3150  for (size_t i = 0; i < vSubArguments.size(); i++)
3151  {
3152  if (i)
3153  ss << ",";
3154  vSubArguments[i]->GenSlidingWindowDecl(ss);
3155  }
3156  ss << ")\n";
3157  ss << "{\n\t";
3158  ss << "double f,s,p,tmp0,tmp1,tmp2;\n";
3159  ss << " int gid0=get_global_id(0);\n";
3160  ss <<"\n";
3161  for (size_t i = 0; i < vSubArguments.size(); i++)
3162  {
3163  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3164  assert(pCur);
3165  if (pCur->GetType() == formula::svSingleVectorRef)
3166  {
3167  const formula::SingleVectorRefToken* pSVR =
3168  static_cast< const formula::SingleVectorRefToken* >(pCur);
3169  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3170  }
3171  else if (pCur->GetType() == formula::svDouble)
3172  {
3173  ss << "{\n";
3174  }
3175 
3176  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3177  {
3178  ss << " if (isnan(";
3179  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3180  ss << "))\n";
3181  ss << " tmp"<<i<<"= 0;\n";
3182  ss << " else\n";
3183  ss << " tmp"<<i<<"=\n";
3184  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3185  ss << ";\n}\n";
3186  }
3187  else
3188  {
3189  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3190  ss <<";\n";
3191  }
3192  }
3193  ss << " p = tmp2;\n";
3194  ss << " s = tmp1;\n";
3195  ss << " f = tmp0;\n";
3196  ss << " double q = 1.0 - p;\n\t";
3197  ss << " double fFactor = pow(p,s);\n\t";
3198  ss << " for(int i=0; i<f; i++)\n\t";
3199  ss << " {\n\t";
3200  ss << " fFactor *= ((double)i+s)*pow(((double)i+1.0),-1.0)/pow(q,-1);\n";
3201  ss << " }\n\t";
3202  ss << " double temp=fFactor;\n\t";
3203  ss << " return temp;\n";
3204  ss << "}\n";
3205 }
3206 
3207 void OpPearson::GenSlidingWindowFunction(
3208  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3209 {
3210  if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
3211  ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
3212  ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
3214  throw Unhandled(__FILE__, __LINE__);
3215  const formula::DoubleVectorRefToken* pDVR =
3216  static_cast<const formula::DoubleVectorRefToken *>(
3217  vSubArguments[0]->GetFormulaToken());
3218  const formula::DoubleVectorRefToken* pCurDVRY =
3219  static_cast<const formula::DoubleVectorRefToken *>(
3220  vSubArguments[1]->GetFormulaToken());
3221  if( pDVR->GetRefRowSize() != pCurDVRY->GetRefRowSize() )
3222  throw Unhandled(__FILE__, __LINE__);
3223 
3224  size_t nCurWindowSize = pDVR->GetRefRowSize();
3225 
3226  ss << "\ndouble " << sSymName;
3227  ss << "_"<< BinFuncName() <<"(";
3228  for (size_t i = 0; i < vSubArguments.size(); i++)
3229  {
3230  if (i)
3231  ss << ",";
3232  vSubArguments[i]->GenSlidingWindowDecl(ss);
3233  }
3234  ss << ")\n";
3235  ss << "{\n";
3236  ss << " int gid0=get_global_id(0);\n";
3237  ss << " double fCount = 0.0;\n";
3238  ss << " double fSumX = 0.0;\n";
3239  ss << " double fSumY = 0.0;\n";
3240  ss << " double fSumDeltaXDeltaY = 0.0;\n";
3241  ss << " double fInx;\n";
3242  ss << " double fIny;\n";
3243  ss << "for (int i = ";
3244  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
3245  ss << "gid0; i < " << pDVR->GetArrayLength();
3246  ss << " && i < " << nCurWindowSize << "; i++){\n";
3247  } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
3248  ss << "0; i < " << pDVR->GetArrayLength();
3249  ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
3250  } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
3251  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3252  ss << " && i < "<< nCurWindowSize << "; i++){\n";
3253  }
3254  else {
3255  ss << "0; i < "<< nCurWindowSize << "; i++){\n";
3256  }
3257  ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef(true);
3258  ss << ";\n";
3259  ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef(true);
3260  ss << " ;\n";
3261  ss << " if(!isnan(fInx)&&!isnan(fIny)){\n";
3262  ss << " fSumX += fInx;\n";
3263  ss << " fSumY += fIny;\n";
3264  ss << " fCount = fCount + 1;\n";
3265  ss << " }\n";
3266  ss << " }\n";
3267  ss << " if(fCount < 1)\n";
3268  ss << " return CreateDoubleError(NoValue);\n";
3269  ss << " double fMeanX = fSumX / fCount;\n";
3270  ss << " double fMeanY = fSumY / fCount;\n";
3271  ss << " fSumX = 0.0;\n";
3272  ss << " fSumY = 0.0;\n";
3273  ss << "for (int i = ";
3274  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
3275  ss << "gid0; i < " << pDVR->GetArrayLength();
3276  ss << " && i < " << nCurWindowSize << "; i++){\n";
3277  } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
3278  ss << "0; i < " << pDVR->GetArrayLength();
3279  ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
3280  } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
3281  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3282  ss << " && i < "<< nCurWindowSize << "; i++){\n";
3283  }
3284  else {
3285  ss << "0; i < "<< nCurWindowSize << "; i++){\n";
3286  }
3287  ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef(true);
3288  ss << " ;\n";
3289  ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef(true);
3290  ss << " ;\n";
3291  ss << " if(!isnan(fInx)&&!isnan(fIny)){\n";
3292  ss << " fSumDeltaXDeltaY += (fInx - fMeanX) * (fIny - fMeanY);\n";
3293  ss << " fSumX += (fInx - fMeanX) * (fInx - fMeanX);\n";
3294  ss << " fSumY += (fIny - fMeanY) * (fIny - fMeanY);\n";
3295  ss << " }\n";
3296  ss << " }\n";
3297  ss << " if (fSumX == 0 || fSumY == 0)\n";
3298  ss << " return CreateDoubleError(DivisionByZero);\n";
3299  ss << " double tmp = ( fSumDeltaXDeltaY / sqrt( fSumX * fSumY));\n";
3300  ss << " return tmp;\n";
3301  ss << "}\n";
3302 }
3303 
3304 void OpGammaLn::GenSlidingWindowFunction(
3305  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3306 {
3307  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
3308  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
3310  ss << "\ndouble " << sSymName;
3311  ss << "_"<< BinFuncName() <<"(";
3312  for (size_t i = 0; i < vSubArguments.size(); i++)
3313  {
3314  if (i)
3315  ss << ",";
3316  vSubArguments[i]->GenSlidingWindowDecl(ss);
3317  }
3318  ss << ") {\n\t";
3319  ss <<"int gid0=get_global_id(0);\n\t";
3320  ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
3321  ss << ";\n\t";
3322  ss<< "if(isnan(arg0)||(gid0>=";
3323  ss<<tmpCurDVR->GetArrayLength();
3324  ss<<"))\n\t\t";
3325  ss<<"arg0 = 0;\n\t";
3326  ss << "double tmp=lgamma(arg0);\n\t";
3327  ss << "return tmp;\n";
3328  ss << "}\n";
3329 }
3330 void OpGauss::BinInlineFun(std::set<std::string>& decls,
3331  std::set<std::string>& funs)
3332 {
3333  decls.insert(taylorDecl);decls.insert(phiDecl);
3334  decls.insert(gaussDecl);
3335  funs.insert(taylor);funs.insert(phi);
3336  funs.insert(gauss);
3337 }
3338 
3339 void OpGauss::GenSlidingWindowFunction(
3340  std::stringstream &ss, const std::string &sSymName, SubArguments &
3341 vSubArguments)
3342 {
3343  ss << "\ndouble " << sSymName;
3344  ss << "_"<< BinFuncName() <<"(";
3345  for (size_t i = 0; i < vSubArguments.size(); i++)
3346  {
3347  if (i)
3348  ss << ",";
3349  vSubArguments[i]->GenSlidingWindowDecl(ss);
3350  }
3351  ss << ") {\n";
3352  ss <<" int gid0=get_global_id(0);\n";
3353  ss <<" double arg0;\n";
3354  if(vSubArguments.size() != 1)
3355  {
3356  ss << " return DBL_MAX;\n";
3357  return ;
3358  }
3359  FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
3360  assert(pCur);
3361  if (pCur->GetType() == formula::svDoubleVectorRef)
3362  {
3363  ss << " return DBL_MAX;\n";
3364  return ;
3365  }
3366  else if (pCur->GetType() == formula::svSingleVectorRef)
3367  {
3368  const formula::SingleVectorRefToken* pSVR =
3369  static_cast< const formula::SingleVectorRefToken* >(pCur);
3370  ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
3371  ss << ";\n";
3372  ss<< " if(isnan(arg0)||(gid0>=";
3373  ss<<pSVR->GetArrayLength();
3374  ss<<"))\n";
3375  ss<<" arg0 = 0;\n";
3376  }
3377  else if (pCur->GetType() == formula::svDouble)
3378  {
3379  ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
3380  ss << ";\n";
3381  ss << " if(isnan(arg0))\n";
3382  ss << " return DBL_MAX;\n";
3383  }
3384  ss << " double tmp=gauss(arg0);\n";
3385  ss << " return tmp;\n";
3386  ss << "}\n";
3387 }
3388 
3389 void OpGeoMean::GenSlidingWindowFunction(
3390  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3391 {
3392  ss << "__kernel void ";
3393  ss << "GeoMean_reduction( ";
3394  for (size_t i = 0; i < vSubArguments.size(); i++)
3395  {
3396  if (i)
3397  ss << ",";
3398  vSubArguments[i]->GenSlidingWindowDecl(ss);
3399  }
3400  ss << ", __global double *result)\n";
3401  ss << "{\n";
3402  ss << " double tmp =0;\n";
3403  ss << " int count = 0;\n";
3404  ss << " int i ;\n";
3405  GenTmpVariables(ss,vSubArguments);
3406  ss << " double current_sum = 0.0;\n";
3407  ss << " int windowSize;\n";
3408  ss << " int arrayLength;\n";
3409  ss << " int current_count = 0;\n";
3410  ss << " int writePos = get_group_id(1);\n";
3411  ss << " int lidx = get_local_id(0);\n";
3412  ss << " __local double shm_buf[256];\n";
3413  ss << " __local int count_buf[256];\n";
3414  ss << " int loop;\n";
3415  ss << " int offset;\n";
3416  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3417 
3418  for(const DynamicKernelArgumentRef & rArg : vSubArguments)
3419  {
3420  assert(rArg->GetFormulaToken());
3421 
3422  if(rArg->GetFormulaToken()->GetType() ==
3424  {
3425  FormulaToken *tmpCur = rArg->GetFormulaToken();
3426  const formula::DoubleVectorRefToken*pCurDVR= static_cast<const
3427  formula::DoubleVectorRefToken *>(tmpCur);
3428  size_t nCurWindowSize = pCurDVR->GetArrayLength() <
3429  pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
3430  pCurDVR->GetRefRowSize() ;
3431 
3432  if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
3433  ss << " offset = 0;\n";
3434  else if (!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
3435  ss << " offset = get_group_id(1);\n";
3436  else
3437  throw Unhandled(__FILE__, __LINE__);
3438  ss << " windowSize = ";
3439  ss << nCurWindowSize;
3440  ss << ";\n";
3441  ss << " arrayLength = ";
3442  ss << pCurDVR->GetArrayLength();
3443  ss << ";\n";
3444  ss << " loop = arrayLength/512 + 1;\n";
3445  ss << " for (int l=0; l<loop; l++){\n";
3446  ss << " tmp = 0.0;\n";
3447  ss << " count = 0;\n";
3448  ss << " int loopOffset = l*512;\n";
3449  ss << " int p1 = loopOffset + lidx + offset, p2 = p1 + 256;\n";
3450  ss << " if (p2 < min(offset + windowSize, arrayLength)) {\n";
3451  ss << " tmp0 = 0.0;\n";
3452  std::string p1 = "p1";
3453  std::string p2 = "p2";
3454 
3455  ss << " tmp0 =";
3456  rArg->GenDeclRef(ss);
3457  ss << "["<<p1.c_str()<<"];\n";
3458  ss << " if(!isnan(tmp0))\n";
3459  ss << " {\n";
3460  ss << " tmp += log(tmp0);\n";
3461  ss << " count++;\n";
3462  ss << " }\n";
3463 
3464  ss << " tmp0 =";
3465  rArg->GenDeclRef(ss);
3466  ss << "["<<p2.c_str()<<"];\n";
3467  ss << " if(!isnan(tmp0))\n";
3468  ss << " {\n";
3469  ss << " tmp += log(tmp0);\n";
3470  ss << " count++;\n";
3471  ss << " }\n";
3472 
3473  ss << " }\n";
3474  ss << " else if (p1 < min(arrayLength, offset + windowSize)) {\n";
3475 
3476  ss << " tmp0 =";
3477  rArg->GenDeclRef(ss);
3478  ss << "["<<p1.c_str()<<"];\n";
3479  ss << " if(!isnan(tmp0))\n";
3480  ss << " {\n";
3481  ss << " tmp += log(tmp0);\n";
3482  ss << " count++;\n";
3483  ss << " }\n";
3484 
3485  ss << " }\n";
3486  ss << " shm_buf[lidx] = tmp;\n";
3487  ss << " count_buf[lidx] = count;\n";
3488  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3489 
3490  ss << " for (int i = 128; i >0; i/=2) {\n";
3491  ss << " if (lidx < i)\n";
3492  ss << " {\n";
3493  ss << " shm_buf[lidx] += shm_buf[lidx + i];\n";
3494  ss << " count_buf[lidx] += count_buf[lidx + i];\n";
3495  ss << " }\n";
3496  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3497  ss << " }\n";
3498  ss << " if (lidx == 0)\n";
3499  ss << " {\n";
3500  ss << " current_sum += shm_buf[0];\n";
3501  ss << " current_count += count_buf[0];\n";
3502  ss << " }\n";
3503  // ss << "if(writePos == 14 && lidx ==0)\n";
3504  //ss <<"printf(\"\\n********************sum is %f, count is%d\",current_sum,current_count);\n";
3505  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3506  ss << " }\n";
3507  }else
3508  {
3509  ss << " if (lidx == 0)\n";
3510  ss << " {\n";
3511  ss << " tmp0 =";
3512  if(rArg->GetFormulaToken()->GetType() == formula::svSingleVectorRef)
3513  {
3514  rArg->GenDeclRef(ss);
3515  ss << "[writePos];\n";
3516  }
3517  else
3518  {
3519  rArg->GenDeclRef(ss);
3520  ss <<";\n";
3521  //ss <<"printf(\"\\n********************tmp0 is %f\",tmp0);\n";
3522  }
3523  ss << " if(!isnan(tmp0))\n";
3524  ss << " {\n";
3525  ss << " current_sum += log(tmp0);\n";
3526  ss << " current_count++;\n";
3527  ss << " }\n";
3528  ss << " }\n";
3529  }
3530  }
3531 
3532  ss << " if (lidx == 0)\n";
3533  ss << " result[writePos] = exp(current_sum/current_count);\n";
3534  ss << "}\n";
3535 
3536  ss << "\ndouble " << sSymName;
3537  ss << "_"<< BinFuncName() <<"(";
3538  for (size_t i = 0; i < vSubArguments.size(); i++)
3539  {
3540  if (i)
3541  ss << ",";
3542  vSubArguments[i]->GenSlidingWindowDecl(ss);
3543  }
3544  ss << ")\n {\n";
3545  ss <<" int gid0=get_global_id(0);\n";
3546  ss << " double tmp =0;\n";
3547  ss << " tmp =";
3548  vSubArguments[0]->GenDeclRef(ss);
3549  ss << "[gid0];\n";
3550  ss << " return tmp;\n";
3551  ss << "}";
3552 }
3553 
3554 void OpHarMean::GenSlidingWindowFunction(
3555  std::stringstream &ss, const std::string &sSymName, SubArguments &
3556 vSubArguments)
3557 {
3558 
3559  ss << "\ndouble " << sSymName;
3560  ss << "_"<< BinFuncName() <<"( ";
3561  for (size_t i = 0; i < vSubArguments.size(); i++)
3562  {
3563  if (i)
3564  ss << ",";
3565  vSubArguments[i]->GenSlidingWindowDecl(ss);
3566  }
3567  ss << ")\n";
3568  ss <<"{\n";
3569  ss << " int gid0 = get_global_id(0);\n";
3570  ss << " double nVal=0.0;\n";
3571  ss << " double tmp = 0;\n";
3572  ss << " int length;\n";
3573  ss << " int totallength=0;\n";
3574  for (size_t i = 0; i < vSubArguments.size(); i++)
3575  {
3576  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3577  assert(pCur);
3578  if (pCur->GetType() == formula::svDoubleVectorRef)
3579  {
3580  const formula::DoubleVectorRefToken* pDVR =
3581  static_cast<const formula::DoubleVectorRefToken *>(pCur);
3582  size_t nCurWindowSize = pDVR->GetRefRowSize();
3583  ss << " length="<<nCurWindowSize;
3584  ss << ";\n";
3585  ss << " for (int i = ";
3586  ss << "0; i < "<< nCurWindowSize << "; i++)\n";
3587  ss << " {\n";
3588  ss << " double arg"<<i<<" = ";
3589  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3590  ss << ";\n";
3591  ss << " if(isnan(arg"<<i<<")||((gid0+i)>=";
3592  ss << pDVR->GetArrayLength();
3593  ss << "))\n";
3594  ss << " {\n";
3595  ss << " length--;\n";
3596  ss << " continue;\n";
3597  ss << " }\n";
3598  ss << " nVal += (1.0 *pow(";
3599  ss << " arg"<<i<<",-1));\n";
3600  ss << " }\n";
3601  ss << " totallength +=length;\n";
3602  }
3603  else if (pCur->GetType() == formula::svSingleVectorRef)
3604  {
3605  ss << " tmp = ";
3606  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3607  ss << ";\n";
3608  ss << " if(!isnan(tmp))\n";
3609  ss << " {\n";
3610  ss << " nVal += (1.0 * pow( tmp,-1));\n";
3611  ss << " totallength +=1;\n";
3612  ss << " }\n";
3613  }
3614  else if (pCur->GetType() == formula::svDouble)
3615  {
3616  ss << " tmp = ";
3617  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3618  ss << ";\n";
3619  ss << " nVal += (1.0 *pow( tmp,-1));\n";
3620  ss << " totallength +=1;\n";
3621  }
3622  else
3623  {
3624  ss << " return DBL_MIN;\n";
3625  }
3626  }
3627  ss << " tmp = totallength*pow(nVal,-1);\n";
3628  ss << " return tmp;\n";
3629  ss << "}";
3630 }
3631 
3632 void OpConfidence::BinInlineFun(std::set<std::string>& decls,
3633  std::set<std::string>& funs)
3634 {
3635  decls.insert(gaussinvDecl);
3636  funs.insert(gaussinv);
3637 }
3638 
3639 void OpConfidence::GenSlidingWindowFunction(std::stringstream& ss,
3640  const std::string &sSymName, SubArguments& vSubArguments)
3641 {
3642  ss << "\ndouble " << sSymName;
3643  ss << "_"<< BinFuncName() <<"(";
3644  for (size_t i = 0; i < vSubArguments.size(); i++)
3645  {
3646  if (i)
3647  ss << ",";
3648  vSubArguments[i]->GenSlidingWindowDecl(ss);
3649  }
3650  ss << ") {\n";
3651  ss << " double tmp = " << GetBottom() <<";\n";
3652  ss << " int gid0 = get_global_id(0);\n";
3653  ss << " double alpha = " << GetBottom() <<";\n";
3654  ss << " double sigma = " << GetBottom() <<";\n";
3655  ss << " double size = " << GetBottom() <<";\n";
3656  ss << " double tmp0,tmp1,tmp2;\n";
3657  ss <<"\n";
3658  for (size_t i = 0; i < vSubArguments.size(); i++)
3659  {
3660  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3661  assert(pCur);
3662  if (pCur->GetType() == formula::svSingleVectorRef)
3663  {
3664  const formula::SingleVectorRefToken* pSVR =
3665  static_cast< const formula::SingleVectorRefToken* >(pCur);
3666  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3667  }
3668  else if (pCur->GetType() == formula::svDouble)
3669  {
3670  ss << "{\n";
3671  }
3672 
3673  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3674  {
3675  ss << " if (isnan(";
3676  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3677  ss << "))\n";
3678  ss << " tmp"<<i<<"= 0;\n";
3679  ss << " else\n";
3680  ss << " tmp"<<i<<"=\n";
3681  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3682  ss << ";\n}\n";
3683  }
3684  else
3685  {
3686  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3687  ss <<";\n";
3688  }
3689  }
3690  ss << " alpha = tmp0;\n";
3691  ss << " sigma = tmp1;\n";
3692  ss << " size = tmp2;\n";
3693  ss << " double rn = floor(size);\n";
3694  ss << " if(sigma <= 0.0 || alpha <= 0.0 || alpha >= 1.0";
3695  ss << "|| rn < 1.0)\n";
3696  ss << " tmp = -DBL_MAX;\n";
3697  ss << " else\n";
3698  ss << " tmp = gaussinv(1.0 - alpha * pow(2.0,-1.0)) * sigma ";
3699  ss << "* pow(sqrt( rn ),-1);\n";
3700  ss << " return tmp;\n";
3701  ss << "}";
3702 }
3703 
3704 void OpCritBinom::BinInlineFun(std::set<std::string>& decls,
3705  std::set<std::string>& funs)
3706 {
3707  decls.insert(MinDecl);
3708  funs.insert("");
3709 }
3710 
3711 void OpCritBinom::GenSlidingWindowFunction(std::stringstream& ss,
3712  const std::string &sSymName, SubArguments& vSubArguments)
3713 {
3714  ss << "\ndouble " << sSymName;
3715  ss << "_"<< BinFuncName() <<"(";
3716  for (size_t i = 0; i < vSubArguments.size(); i++)
3717  {
3718  if (i)
3719  ss << ",";
3720  vSubArguments[i]->GenSlidingWindowDecl(ss);
3721  }
3722  ss << ") {\n";
3723  ss << " double tmp = " << GetBottom() <<";\n";
3724  ss << " int gid0 = get_global_id(0);\n";
3725  ss << " double n = " << GetBottom() <<";\n";
3726  ss << " double p = " << GetBottom() <<";\n";
3727  ss << " double alpha = " << GetBottom() <<";\n";
3728  ss << " double tmp0 = 0.0,tmp1 = 0.0,tmp2 = 0.0;\n";
3729  ss <<"\n";
3730  for (size_t i = 0; i < vSubArguments.size(); i++)
3731  {
3732  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3733  assert(pCur);
3734  if (pCur->GetType() == formula::svSingleVectorRef)
3735  {
3736  const formula::SingleVectorRefToken* pSVR =
3737  static_cast< const formula::SingleVectorRefToken* >(pCur);
3738  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3739  }
3740  else if (pCur->GetType() == formula::svDouble)
3741  {
3742  ss << "{\n";
3743  }
3744 
3745  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3746  {
3747  ss << " if (isnan(";
3748  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3749  ss << "))\n";
3750  ss << " tmp"<<i<<"= 0;\n";
3751  ss << " else\n";
3752  ss << " tmp"<<i<<"=\n";
3753  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3754  ss << ";\n}\n";
3755  }
3756  else
3757  {
3758  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3759  ss <<";\n";
3760  }
3761  }
3762  ss << " n = tmp0;\n";
3763  ss << " p = tmp1;\n";
3764  ss << " alpha = tmp2;\n";
3765  ss << " double rn = floor(n);\n";
3766  ss << " if (rn < 0.0 || alpha <= 0.0 || alpha >= 1.0 || p < 0.0";
3767  ss << " || p > 1.0)\n";
3768  ss << " tmp = -DBL_MIN;\n";
3769  ss << " else\n";
3770  ss << " {\n";
3771  ss << " double rq = (0.5 - p) + 0.5;\n";
3772  ss << " double fFactor = pow(rq, rn);\n";
3773  ss << " if (fFactor <= Min)\n";
3774  ss << " {\n";
3775  ss << " fFactor = pow(p, rn);\n";
3776  ss << " if (fFactor <= Min)\n";
3777  ss << " tmp = -DBL_MAX;\n";
3778  ss << " else\n";
3779  ss << " {\n";
3780  ss << " double fSum = 1.0 - fFactor;\n";
3781  ss << " uint max =(uint)(rn), i;\n";
3782  ss << " for (i = 0; i < max && fSum >= alpha; i++)\n";
3783  ss << " {\n";
3784  ss << " fFactor *= (rn - i) * pow((double)(i + 1),-1.0) *";
3785  ss << " rq * pow(p, -1.0);\n";
3786  ss << " fSum -= fFactor;\n";
3787  ss << " }\n";
3788  ss << " tmp = (rn - i);\n";
3789  ss << " }\n";
3790  ss << " }\n";
3791  ss << " else\n";
3792  ss << " {\n";
3793  ss << " double fSum = fFactor;\n";
3794  ss << " uint max = (uint)(rn), i;\n";
3795  ss << " for (i = 0; i < max && fSum < alpha; i++)\n";
3796  ss << " {\n";
3797  ss << " fFactor *= (rn - i) * pow((double)(i + 1), -1.0) *";
3798  ss << " p * pow(rq, -1.0);\n";
3799  ss << " fSum += fFactor;\n";
3800  ss << " }\n";
3801  ss << " tmp = (i);\n";
3802  ss << " }\n";
3803  ss << " }\n";
3804  ss << " return tmp;\n";
3805  ss << "}";
3806 }
3807 
3808 void OpRsq::GenSlidingWindowFunction(
3809  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3810 {
3811  if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
3812  ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
3813  ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
3815  throw Unhandled(__FILE__, __LINE__);
3816  const formula::DoubleVectorRefToken* pCurDVR1 =
3817  static_cast<const formula::DoubleVectorRefToken *>(
3818  vSubArguments[0]->GetFormulaToken());
3819  const formula::DoubleVectorRefToken* pCurDVR2 =
3820  static_cast<const formula::DoubleVectorRefToken *>(
3821  vSubArguments[1]->GetFormulaToken());
3822  if( pCurDVR1->GetRefRowSize() != pCurDVR2->GetRefRowSize() )
3823  throw Unhandled(__FILE__, __LINE__);
3824 
3825  size_t nCurWindowSize = pCurDVR1->GetRefRowSize();
3826 
3827  ss << "\ndouble " << sSymName;
3828  ss << "_"<< BinFuncName() <<"(";
3829  for (size_t i = 0; i < vSubArguments.size(); i++)
3830  {
3831  if (i)
3832  ss << ",";
3833  vSubArguments[i]->GenSlidingWindowDecl(ss);
3834  }
3835  ss << ")\n";
3836  ss << "{\n";
3837  ss << " int gid0=get_global_id(0);\n";
3838  ss << " double fCount = 0.0;\n";
3839  ss << " double fSumX = 0.0;\n";
3840  ss << " double fSumY = 0.0;\n";
3841  ss << " double fSumDeltaXDeltaY = 0.0;\n";
3842  ss << " double fInx;\n";
3843  ss << " double fIny;\n";
3844  ss << " double tmp0,tmp1;\n";
3845 
3846  ss <<"\n";
3847 
3848  ss << " for(int i=0; i<"<<nCurWindowSize<<"; i++)\n";
3849  ss << " {\n";
3850  ss << " if(isnan(";
3851  ss << vSubArguments[0]->GenSlidingWindowDeclRef(true);
3852  ss << "))\n";
3853  ss << " fInx = 0;\n";
3854  ss << " else\n";
3855  ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
3856  ss << ";\n";
3857  ss << " if(isnan(";
3858  ss << vSubArguments[1]->GenSlidingWindowDeclRef(true);
3859  ss << "))\n";
3860  ss << " fIny = 0;\n";
3861  ss << " else\n";
3862  ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
3863  ss << " ;\n";
3864  ss << " fSumX += fInx;\n";
3865  ss << " fSumY += fIny;\n";
3866  ss << " fCount = fCount + 1;\n";
3867  ss << " }\n";
3868  ss << " double fMeanX = fSumX / fCount;\n";
3869  ss << " double fMeanY = fSumY / fCount;\n";
3870  ss << " fSumX = 0.0;\n";
3871  ss << " fSumY = 0.0;\n";
3872  ss << " for(int i=0; i<"<<nCurWindowSize<<"; i++)\n";
3873  ss << " {\n";
3874  ss << " if(isnan(";
3875  ss << vSubArguments[0]->GenSlidingWindowDeclRef(true);
3876  ss << "))\n";
3877  ss << " fInx = 0;\n";
3878  ss << " else\n";
3879  ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
3880  ss << ";\n";
3881  ss << " if(isnan(";
3882  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3883  ss << "))\n";
3884  ss << " fIny = 0;\n";
3885  ss << " else\n";
3886  ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
3887  ss << " ;\n";
3888  ss << " fSumDeltaXDeltaY += (fInx - fMeanX) * (fIny - fMeanY);\n";
3889  ss << " fSumX += pow(fInx - fMeanX,2);\n";
3890  ss << " fSumY += pow(fIny - fMeanY,2);\n";
3891  ss << " }\n";
3892  ss << " double tmp = pow( fSumDeltaXDeltaY,2) / (fSumX * fSumY);\n";
3893  ss << " return tmp ;\n";
3894  ss << "}\n";
3895 }
3896 
3897 void OpChiInv::BinInlineFun(std::set<std::string>& decls,
3898  std::set<std::string>& funs)
3899 {
3900  decls.insert(fMachEpsDecl);
3901  funs.insert("");
3902  decls.insert(fBigInvDecl);
3903  funs.insert("");
3904  decls.insert(fHalfMachEpsDecl);
3905  funs.insert("");
3906  decls.insert(lcl_IterateInverseChiInvDecl);
3907  funs.insert(lcl_IterateInverseChiInv);
3908  decls.insert(GetChiDistDecl);
3909  funs.insert(GetChiDist);
3910  decls.insert(lcl_HasChangeOfSignDecl);
3911  funs.insert(lcl_HasChangeOfSign);
3912  decls.insert(GetUpRegIGammaDecl);
3913  funs.insert(GetUpRegIGamma);
3914  decls.insert(GetGammaContFractionDecl);
3915  funs.insert(GetGammaContFraction);
3916  decls.insert(GetGammaSeriesDecl);
3917  funs.insert(GetGammaSeries);
3918 }
3919 void OpChiInv::GenSlidingWindowFunction(
3920  std::stringstream &ss,const std::string &sSymName,
3921  SubArguments &vSubArguments)
3922 {
3923  ss << "\ndouble " << sSymName;
3924  ss << "_"<< BinFuncName() <<"(";
3925  for (size_t i = 0; i < vSubArguments.size(); i++)
3926  {
3927  if (i)
3928  ss << ",";
3929  vSubArguments[i]->GenSlidingWindowDecl(ss);
3930  }
3931  ss << ")\n";
3932  ss << "{\n";
3933  ss << " double tmp0,tmp1,tmp;\n";
3934  ss << " int gid0=get_global_id(0);\n";
3935  ss <<"\n ";
3936  for (size_t i = 0; i < vSubArguments.size(); i++)
3937  {
3938  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3939  assert(pCur);
3940  if (pCur->GetType() == formula::svDoubleVectorRef)
3941  {
3942  const formula::DoubleVectorRefToken* pDVR =
3943  static_cast<const formula::DoubleVectorRefToken *>(pCur);
3944  size_t nCurWindowSize = pDVR->GetRefRowSize();
3945  ss << "for (int i = ";
3946  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
3947  ss << "gid0; i < " << pDVR->GetArrayLength();
3948  ss << " && i < " << nCurWindowSize << "; i++){\n";
3949  } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
3950  ss << "0; i < " << pDVR->GetArrayLength();
3951  ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
3952  } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
3953  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3954  ss << " && i < "<< nCurWindowSize << "; i++){\n";
3955  } else {
3956  ss << "0; i < "<< nCurWindowSize << "; i++){\n";
3957  }
3958  }
3959  else if (pCur->GetType() == formula::svSingleVectorRef)
3960  {
3961  const formula::SingleVectorRefToken* pSVR =
3962  static_cast< const formula::SingleVectorRefToken* >(pCur);
3963  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3964  }
3965  else if (pCur->GetType() == formula::svDouble)
3966  {
3967  ss << "{\n";
3968  }
3969 
3970  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3971  {
3972  ss << "if (isnan(";
3973  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3974  ss << "))\n";
3975  ss << " tmp"<<i<<"= 0;\n";
3976  ss << "else\n";
3977  ss <<"tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3978  ss << ";\n}\n";
3979  }
3980  else
3981  {
3982  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3983  ss << ";\n";
3984  }
3985  }
3986  ss << " tmp1 = floor(tmp1);";
3987  ss << " if (tmp1 < 1.0 || tmp0 <= 0.0 || tmp0 > 1.0 )\n";
3988  ss << " {\n";
3989  ss << " return DBL_MIN;\n";
3990  ss << " }\n";
3991  ss << " bool bConvError;\n";
3992  ss << " double fVal = lcl_IterateInverseChiInv";
3993  ss << "(tmp0, tmp1, tmp1*0.5, tmp1, &bConvError);\n";
3994  ss << " if(bConvError)\n";
3995  ss << " return DBL_MIN;\n";
3996  ss << " return fVal;\n";
3997  ss << "}\n";
3998 }
3999 void OpNormdist::GenSlidingWindowFunction(
4000  std::stringstream &ss, const std::string &sSymName,
4001  SubArguments &vSubArguments)
4002 {
4003  CHECK_PARAMETER_COUNT(3,4);
4004  ss << "\ndouble " << sSymName;
4005  ss << "_"<< BinFuncName() <<"(";
4006  for (size_t i = 0; i < vSubArguments.size(); i++)
4007  {
4008  if (i)
4009  ss << ",";
4010  vSubArguments[i]->GenSlidingWindowDecl(ss);
4011  }
4012  ss << ")\n";
4013  ss << "{\n";
4014  ss << " double x,mue,sigma,c;\n";
4015  ss << " int gid0=get_global_id(0);\n";
4016  ss << " double tmp0,tmp1,tmp2;\n";
4017  ss << " double tmp3 = 0;\n"; // optional argument
4018  ss <<"\n ";
4019  for (size_t i = 0; i < vSubArguments.size(); i++)
4020  {
4021  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4022  assert(pCur);
4023  if (pCur->GetType() == formula::svSingleVectorRef)
4024  {
4025  const formula::SingleVectorRefToken* pSVR =
4026  static_cast< const formula::SingleVectorRefToken* >(pCur);
4027  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4028  }
4029  else if (pCur->GetType() == formula::svDouble)
4030  {
4031  ss << "{\n";
4032  }
4033  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4034  {
4035  ss << " if (isnan(";
4036  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4037  ss << "))\n";
4038  ss << " tmp"<<i<<"= 0;\n";
4039  ss << " else\n";
4040  ss << " tmp"<<i<<"=\n";
4041  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4042  ss << ";\n}\n";
4043  }
4044  else
4045  {
4046  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4047  ss <<";\n";
4048  }
4049  }
4050  ss << "x = tmp0;\n";
4051  ss << "mue = tmp1;\n";
4052  ss << "sigma = tmp2;\n";
4053  ss << "c = tmp3;\n";
4054  ss << "if(sigma <= 0)\n";
4055  ss << " return CreateDoubleError(IllegalArgument);\n";
4056  ss << "double mid,tmp;\n";
4057  ss << "mid = (x - mue)/sigma;\n";
4058  ss << "if(c)\n";
4059  ss << " tmp = 0.5 *erfc(-mid * 0.7071067811865475);\n";
4060  ss << "else \n";
4061  ss <<" tmp=(0.39894228040143268*exp(-pow(mid,2)/2.0))/sigma;\n";
4062  ss << "return tmp;\n";
4063  ss << "}\n";
4064 }
4065 void OpNormsdist::GenSlidingWindowFunction(
4066  std::stringstream &ss,const std::string &sSymName,
4067  SubArguments &vSubArguments)
4068 {
4069  ss << "\ndouble " << sSymName;
4070  ss << "_"<< BinFuncName() <<"(";
4071  for (size_t i = 0; i < vSubArguments.size(); i++)
4072  {
4073  if (i)
4074  ss << ",";
4075  vSubArguments[i]->GenSlidingWindowDecl(ss);
4076  }
4077  ss << ")\n";
4078  ss << "{\n";
4079  ss << " double x = 0,tmp0 = 0;\n";
4080  ss << " int gid0=get_global_id(0);\n";
4081  ss <<"\n ";
4082  for (size_t i = 0; i < vSubArguments.size(); i++)
4083  {
4084  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4085  assert(pCur);
4086  if (pCur->GetType() == formula::svSingleVectorRef)
4087  {
4088  const formula::SingleVectorRefToken* pSVR =
4089  static_cast< const formula::SingleVectorRefToken* >(pCur);
4090  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4091  }
4092  else if (pCur->GetType() == formula::svDouble)
4093  {
4094  ss << "{\n";
4095  }
4096 
4097  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4098  {
4099  ss << " if (isnan(";
4100  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4101  ss << "))\n";
4102  ss << " tmp"<<i<<"= 0;\n";
4103  ss << " else\n";
4104  ss << " tmp"<<i<<"=\n";
4105  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4106  ss << ";\n}\n";
4107  }
4108  else
4109  {
4110  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4111  ss <<";\n";
4112  }
4113  }
4114  ss << " x = tmp0;\n";
4115  ss << " double tmp = 0.5 * erfc((-1)*x * 0.7071067811865475);\n";
4116  ss << " return tmp;\n";
4117  ss << "}\n";
4118 }
4119 
4120 void OpPermut::GenSlidingWindowFunction(
4121  std::stringstream &ss,const std::string &sSymName,
4122  SubArguments &vSubArguments)
4123 {
4124  ss << "\ndouble " << sSymName;
4125  ss << "_"<< BinFuncName() <<"(";
4126  for (size_t i = 0; i < vSubArguments.size(); i++)
4127  {
4128  if (i)
4129  ss << ",";
4130  vSubArguments[i]->GenSlidingWindowDecl(ss);
4131  }
4132  ss << ") {\n";
4133  ss <<" int gid0=get_global_id(0);\n";
4134  ss <<" double inA;\n";
4135  ss <<" double inB;\n";
4136  ss <<" double tmp0,tmp1;\n";
4137  ss <<" double tmp = 1 ;\n";
4138  ss <<"\n";
4139  for (size_t i = 0; i < vSubArguments.size(); i++)
4140  {
4141  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4142  assert(pCur);
4143  if (pCur->GetType() == formula::svSingleVectorRef)
4144  {
4145  const formula::SingleVectorRefToken* pSVR =
4146  static_cast< const formula::SingleVectorRefToken* >(pCur);
4147  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4148 
4149  }
4150  else if (pCur->GetType() == formula::svDouble)
4151  {
4152  ss << "{\n";
4153  }
4154 
4155  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4156  {
4157  ss << " if (isnan(";
4158  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4159  ss << "))\n";
4160  ss << " tmp"<<i<<"= 0;\n";
4161  ss << " else\n";
4162  ss << " tmp"<<i<<"=\n";
4163  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4164  ss << ";\n}\n";
4165  }
4166  else
4167  {
4168  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4169  ss <<";\n";
4170  }
4171  }
4172  ss << " inA = tmp0;\n";
4173  ss << " inB = tmp1;\n";
4174  ss << " for( int i =0; i<inB; i++)\n";
4175  ss << " {\n";
4176  ss << " tmp *= inA ;\n";
4177  ss << " inA = inA - 1.0;\n";
4178  ss << " }\n";
4179  ss << " return tmp;\n";
4180  ss << "}\n";
4181 }
4182 void OpPermutationA::GenSlidingWindowFunction(
4183  std::stringstream &ss,const std::string &sSymName,
4184  SubArguments &vSubArguments)
4185 {
4186  ss << "\ndouble " << sSymName;
4187  ss << "_"<< BinFuncName() <<"(";
4188  for (size_t i = 0; i < vSubArguments.size(); i++)
4189  {
4190  if (i)
4191  ss << ",";
4192  vSubArguments[i]->GenSlidingWindowDecl(ss);
4193  }
4194  ss << ") {\n";
4195  ss <<" int gid0=get_global_id(0);\n";
4196  ss <<" double inA;\n";
4197  ss <<" double inB;\n";
4198  ss <<" double tmp = 1.0;\n";
4199  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
4200  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
4201  formula::SingleVectorRefToken *>(tmpCur0);
4202  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
4203  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
4204  formula::SingleVectorRefToken *>(tmpCur1);
4205  ss << "int buffer_fIna_len = ";
4206  ss << tmpCurDVR0->GetArrayLength();
4207  ss << ";\n";
4208  ss << " int buffer_fInb_len = ";
4209  ss << tmpCurDVR1->GetArrayLength();
4210  ss << ";\n";
4211  ss << " if((gid0)>=buffer_fIna_len || isnan(";
4212  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4213  ss << "))\n";
4214  ss << " inA = 0;\nelse \n";
4215  ss << " inA = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
4216  ss << ";\n";
4217  ss << "if((gid0)>=buffer_fInb_len || isnan(";
4218  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4219  ss << "))\n";
4220  ss << "inB = 0;\nelse \n";
4221  ss << " inB = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
4222  ss << ";\n";
4223  ss << " for(int i=0; i<inB; i++)\n";
4224  ss << " {\n";
4225  ss << " tmp *= inA;\n";
4226  ss << " }\n";
4227  ss << " return tmp;\n";
4228  ss << "}\n";
4229 }
4230 
4231 void OpPhi::GenSlidingWindowFunction(
4232  std::stringstream &ss,const std::string &sSymName,
4233  SubArguments &vSubArguments)
4234 {
4235  ss << "\ndouble " << sSymName;
4236  ss << "_"<< BinFuncName() <<"(";
4237  for (size_t i = 0; i < vSubArguments.size(); i++)
4238  {
4239  if (i)
4240  ss << ",";
4241  vSubArguments[i]->GenSlidingWindowDecl(ss);
4242  }
4243  ss << ")\n";
4244  ss << "{\n";
4245  ss << " double x,tmp0;\n";
4246  ss << " int gid0=get_global_id(0);\n";
4247  ss <<"\n";
4248  for (size_t i = 0; i < vSubArguments.size(); i++)
4249  {
4250  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4251  assert(pCur);
4252  if (pCur->GetType() == formula::svSingleVectorRef)
4253  {
4254  const formula::SingleVectorRefToken* pSVR =
4255  static_cast< const formula::SingleVectorRefToken* >(pCur);
4256  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4257  }
4258  else if (pCur->GetType() == formula::svDouble)
4259  {
4260  ss << "{\n";
4261  }
4262 
4263  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4264  {
4265  ss << " if (isnan(";
4266  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4267  ss << "))\n";
4268  ss << " tmp"<<i<<"= 0;\n";
4269  ss << " else\n";
4270  ss << " tmp"<<i<<"=\n";
4271  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4272  ss << ";\n}\n";
4273  }
4274  else
4275  {
4276  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4277  ss <<";\n";
4278  }
4279  }
4280  ss << " x = tmp0;\n";
4281  ss << " double tmp = 0.39894228040143268 * exp((-1)*pow(x,2) / 2.0);\n";
4282  ss << " return tmp;\n";
4283  ss << "}\n";
4284 }
4285 
4286 void OpNorminv::GenSlidingWindowFunction(
4287  std::stringstream &ss,const std::string &sSymName,
4288  SubArguments &vSubArguments)
4289 {
4290  ss << "\ndouble " << sSymName;
4291  ss << "_"<< BinFuncName() <<"(";
4292  for (size_t i = 0; i < vSubArguments.size(); i++)
4293  {
4294  if (i)
4295  ss << ",";
4296  vSubArguments[i]->GenSlidingWindowDecl(ss);
4297  }
4298  ss << ")\n";
4299  ss << "{\n";
4300  ss <<" double q,t,z;\n";
4301  ss <<" double x,mue,sigma;\n";
4302  ss <<" double tmp0,tmp1,tmp2;\n";
4303  ss <<" int gid0=get_global_id(0);\n";
4304  ss <<"\n";
4305  for (size_t i = 0; i < vSubArguments.size(); i++)
4306  {
4307  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4308  assert(pCur);
4309  if (pCur->GetType() == formula::svSingleVectorRef)
4310  {
4311  const formula::SingleVectorRefToken* pSVR =
4312  static_cast< const formula::SingleVectorRefToken* >(pCur);
4313  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4314  }
4315  else if (pCur->GetType() == formula::svDouble)
4316  {
4317  ss << "{\n";
4318  }
4319 
4320  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4321  {
4322  ss << " if (isnan(";
4323  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4324  ss << "))\n";
4325  ss << " tmp"<<i<<"= 0;\n";
4326  ss << " else\n";
4327  ss << " tmp"<<i<<"=\n";
4328  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4329  ss << ";\n}\n";
4330  }
4331  else
4332  {
4333  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4334  ss <<";\n";
4335  }
4336  }
4337  ss <<" x = tmp0;\n";
4338  ss <<" mue = tmp1;\n";
4339  ss <<" sigma = tmp2;\n";
4340  ss <<" q = x -0.5;\n";
4341  ss <<" if(fabs(q)<=.425)\n";
4342  ss <<" {\n";
4343  ss <<" t=0.180625-pow(q,2);\n";
4344  ss <<" z=\n"
4345  "q*\n"
4346  "(\n"
4347  "(\n"
4348  "(\n"
4349  "(\n"
4350  "(\n"
4351  "(\n"
4352  "(\n"
4353  "t*2509.0809287301226727";
4354  ss <<"+33430.575583588128105\n"
4355  ")\n"
4356  "*t+67265.770927008700853\n"
4357  ")\n"
4358  "*t+45921.953931549871457\n"
4359  ")\n"
4360  "*t+13731.693765509461125\n"
4361  ")\n"
4362  "*t+1971.5909503065514427\n"
4363  ")\n"
4364  "*t+133.14166789178437745\n"
4365  ")\n"
4366  "*t+3.387132872796366608\n"
4367  ")\n"
4368  "/\n"
4369  "(\n"
4370  "(\n"
4371  "(\n"
4372  "(\n"
4373  "(\n"
4374  "(\n"
4375  "(\n"
4376  "t*5226.495278852854561";
4377  ss <<"+28729.085735721942674\n"
4378  ")\n"
4379  "*t+39307.89580009271061\n"
4380  ")\n"
4381  "*t+21213.794301586595867\n"
4382  ")\n"
4383  "*t+5394.1960214247511077\n"
4384  ")\n"
4385  "*t+687.1870074920579083\n"
4386  ")\n"
4387  "*t+42.313330701600911252\n"
4388  ")\n"
4389  "*t+1.0\n"
4390  ");\n";
4391  ss <<"}\nelse\n{\n";
4392  ss <<" if(q>0)\nt=1-x;\n";
4393  ss <<"else\nt=x;\n";
4394  ss <<"t=sqrt(-log(t));\n";
4395  ss <<"if(t<=5.0)\n{\n";
4396  ss <<"t+=-1.6;\n";
4397  ss <<"z=\n"
4398  "(\n"
4399  "(\n"
4400  "(\n"
4401  "(\n"
4402  "(\n"
4403  "(\n"
4404  "(\n"
4405  "t*7.7454501427834140764e-4";
4406  ss <<"+0.0227238449892691845833\n"
4407  ")\n"
4408  "*t+0.24178072517745061177\n"
4409  ")\n"
4410  "*t+1.27045825245236838258\n"
4411  ")\n"
4412  "*t+3.64784832476320460504\n"
4413  ")\n"
4414  "*t+5.7694972214606914055\n"
4415  ")\n"
4416  "*t+4.6303378461565452959\n"
4417  ")\n"
4418  "*t+1.42343711074968357734\n"
4419  ")\n"
4420  "/\n"
4421  "(\n"
4422  "(\n"
4423  "(\n"
4424  "(\n"
4425  "(\n"
4426  "(\n"
4427  "(\n"
4428  "t*1.05075007164441684324e-9";
4429  ss <<"+5.475938084995344946e-4\n"
4430  ")\n"
4431  "*t+0.0151986665636164571966\n"
4432  ")\n"
4433  "*t+0.14810397642748007459\n"
4434  ")\n"
4435  "*t+0.68976733498510000455\n"
4436  ")\n"
4437  "*t+1.6763848301838038494\n"
4438  ")\n"
4439  "*t+2.05319162663775882187\n"
4440  ")\n"
4441  "*t+1.0\n"
4442  ");\n}\n";
4443  ss <<"else\n{\n";
4444  ss <<"t+=-5.0;\n";
4445  ss <<"z=\n"
4446  "(\n"
4447  "(\n"
4448  "(\n"
4449  "(\n"
4450  "(\n"
4451  "(\n"
4452  "(\n"
4453  "t*2.01033439929228813265e-7";
4454  ss<<"+2.71155556874348757815e-5\n"
4455  ")\n"
4456  "*t+0.0012426609473880784386\n"
4457  ")\n"
4458  "*t+0.026532189526576123093\n"
4459  ")\n"
4460  "*t+0.29656057182850489123\n"
4461  ")\n"
4462  "*t+1.7848265399172913358\n"
4463  ")\n"
4464  "*t+5.4637849111641143699\n"
4465  ")\n"
4466  "*t+6.6579046435011037772\n"
4467  ")\n"
4468  "/\n"
4469  "(\n"
4470  "(\n"
4471  "(\n"
4472  "(\n"
4473  "(\n"
4474  "(\n"
4475  "(\n"
4476  "t*2.04426310338993978564e-15"
4477  "+1.4215117583164458887e-7\n"
4478  ")\n"
4479  "*t+1.8463183175100546818e-5\n"
4480  ")\n"
4481  "*t+7.868691311456132591e-4\n"
4482  ")\n"
4483  "*t+0.0148753612908506148525\n"
4484  ")\n"
4485  "*t+0.13692988092273580531\n"
4486  ")\n"
4487  "*t+0.59983220655588793769\n"
4488  ")\n"
4489  "*t+1.0\n"
4490  ");\n";
4491  ss<<"}\n";
4492  ss << "z = q < 0.0 ? (-1)*z : z;\n";
4493  ss<<"}\n";
4494  ss<<"double tmp = z*sigma + mue;\n";
4495  ss<<"return tmp;\n";
4496  ss<<"}\n";
4497 }
4498 void OpNormsinv:: GenSlidingWindowFunction
4499  (std::stringstream &ss,const std::string &sSymName,
4500  SubArguments &vSubArguments)
4501 {
4502  ss << "\ndouble " << sSymName;
4503  ss << "_"<< BinFuncName() <<"(";
4504  for (size_t i = 0; i < vSubArguments.size(); i++)
4505  {
4506  if (i)
4507  ss << ",";
4508  vSubArguments[i]->GenSlidingWindowDecl(ss);
4509  }
4510  ss << ")\n";
4511  ss << "{\n";
4512  ss << " double q,t,z,x,tmp0;\n";
4513  ss << " int gid0=get_global_id(0);\n";
4514  ss <<"\n";
4515  for (size_t i = 0; i < vSubArguments.size(); i++)
4516  {
4517  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4518  assert(pCur);
4519  if (pCur->GetType() == formula::svSingleVectorRef)
4520  {
4521  const formula::SingleVectorRefToken* pSVR =
4522  static_cast< const formula::SingleVectorRefToken* >(pCur);
4523  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4524  }
4525  else if (pCur->GetType() == formula::svDouble)
4526  {
4527  ss << "{\n";
4528  }
4529 
4530  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4531  {
4532  ss << " if (isnan(";
4533  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4534  ss << "))\n";
4535  ss << " tmp"<<i<<"= 0;\n";
4536  ss << " else\n";
4537  ss << " tmp"<<i<<"=\n";
4538  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4539  ss << ";\n}\n";
4540  }
4541  else
4542  {
4543  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4544  ss <<";\n";
4545  }
4546  }
4547  ss <<" x = tmp0;\n";
4548  ss <<" q = x -0.5;\n";
4549  ss <<" if(fabs(q)<=.425)\n";
4550  ss <<" {\n";
4551  ss <<" t=0.180625-pow(q,2);\n";
4552  ss <<" z=\n"
4553  "q*\n"
4554  "(\n"
4555  "(\n"
4556  "(\n"
4557  "(\n"
4558  "(\n"
4559  "(\n"
4560  "(\n"
4561  "t*2509.0809287301226727";
4562  ss <<"+33430.575583588128105\n"
4563  ")\n"
4564  "*t+67265.770927008700853\n"
4565  ")\n"
4566  "*t+45921.953931549871457\n"
4567  ")\n"
4568  "*t+13731.693765509461125\n"
4569  ")\n"
4570  "*t+1971.5909503065514427\n"
4571  ")\n"
4572  "*t+133.14166789178437745\n"
4573  ")\n"
4574  "*t+3.387132872796366608\n"
4575  ")\n"
4576  "/\n"
4577  "(\n"
4578  "(\n"
4579  "(\n"
4580  "(\n"
4581  "(\n"
4582  "(\n"
4583  "(\n"
4584  "t*5226.495278852854561";
4585  ss <<"+28729.085735721942674\n"
4586  ")\n"
4587  "*t+39307.89580009271061\n"
4588  ")\n"
4589  "*t+21213.794301586595867\n"
4590  ")\n"
4591  "*t+5394.1960214247511077\n"
4592  ")\n"
4593  "*t+687.1870074920579083\n"
4594  ")\n"
4595  "*t+42.313330701600911252\n"
4596  ")\n"
4597  "*t+1.0\n"
4598  ");\n";
4599  ss <<"}\nelse\n{\n";
4600  ss <<" if(q>0)\nt=1-x;\n";
4601  ss <<"else\nt=x;\n";
4602  ss <<"t=sqrt(-log(t));\n";
4603  ss <<"if(t<=5.0)\n{\n";
4604  ss <<"t+=-1.6;\n";
4605  ss <<"z=\n"
4606  "(\n"
4607  "(\n"
4608  "(\n"
4609  "(\n"
4610  "(\n"
4611  "(\n"
4612  "(\n"
4613  "t*7.7454501427834140764e-4";
4614  ss <<"+0.0227238449892691845833\n"
4615  ")\n"
4616  "*t+0.24178072517745061177\n"
4617  ")\n"
4618  "*t+1.27045825245236838258\n"
4619  ")\n"
4620  "*t+3.64784832476320460504\n"
4621  ")\n"
4622  "*t+5.7694972214606914055\n"
4623  ")\n"
4624  "*t+4.6303378461565452959\n"
4625  ")\n"
4626  "*t+1.42343711074968357734\n"
4627  ")\n"
4628  "/\n"
4629  "(\n"
4630  "(\n"
4631  "(\n"
4632  "(\n"
4633  "(\n"
4634  "(\n"
4635  "(\n"
4636  "t*1.05075007164441684324e-9";
4637  ss <<"+5.475938084995344946e-4\n"
4638  ")\n"
4639  "*t+0.0151986665636164571966\n"
4640  ")\n"
4641