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  return;
499 
500  FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
501  FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
502  FormulaToken *pCur2 = vSubArguments[2]->GetFormulaToken();
503  FormulaToken *pCur3 = vSubArguments[3]->GetFormulaToken();
504  assert(pCur);
505  assert(pCur1);
506  assert(pCur2);
507  assert(pCur3);
508  if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
509  {
510  if(pCur2->GetType() == formula::svSingleVectorRef)
511  {
512  const formula::SingleVectorRefToken* pSVR =
513  static_cast< const formula::SingleVectorRefToken*>(pCur2);
514  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
515  ss << " {\n";
516  ss << " mode = " ;
517  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
518  ss << " if (isnan(mode))\n";
519  ss << " mode = 0.0;\n";
520  ss << " else\n";
521  ss << " mode = floor(mode);\n";
522  ss << " }\n";
523  }
524  else if(pCur2->GetType() == formula::svDouble)
525  {
526  ss << " mode = floor(convert_double(";
527  ss << pCur2->GetDouble() << "));\n";
528  }
529  else
530  {
531  ss << " return DBL_MAX;\n";
532  ss << "}\n";
533  return ;
534  }
535  }
536  else
537  {
538  ss << " mode = floor(" ;
539  ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ");\n";
540  }
541  ss << " if(!(mode == 1.0 || mode == 2.0))\n";
542  ss << " return DBL_MAX;\n";
543  if(ocPush==vSubArguments[3]->GetFormulaToken()->GetOpCode())
544  {
545  if(pCur3->GetType() == formula::svSingleVectorRef)
546  {
547  const formula::SingleVectorRefToken* pSVR =
548  static_cast< const formula::SingleVectorRefToken*>(pCur3);
549  assert(pSVR);
550  ss << " if (gid0 < " << pSVR->GetArrayLength() << ")\n";
551  ss << " {\n";
552  ss << " if (isnan(";
553  ss << vSubArguments[3]->GenSlidingWindowDeclRef() << "))\n";
554  ss << " type=0.0;\n";
555  ss << " else\n";
556  ss << " type=floor(";
557  ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ");\n";
558  ss << " }\n";
559  }
560  else if(pCur3->GetType() == formula::svDouble)
561  {
562  ss << " type = floor(convert_double(" << pCur3->GetDouble() <<
563  "));\n";
564  }
565  else
566  {
567  ss << " return DBL_MAX;\n";
568  ss << "}\n";
569  return ;
570  }
571  }
572  else
573  {
574  ss << " type=floor(";
575  ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ");\n";
576  }
577  ss << " if(!(type == 1.0||type == 2.0||type == 3.0))\n";
578  ss << " return DBL_MAX;\n";
579 
580  if(pCur->GetType() == formula::svDoubleVectorRef &&
582  {
583  const formula::DoubleVectorRefToken* pDVR =
584  static_cast<const formula::DoubleVectorRefToken *>(pCur);
585  const formula::DoubleVectorRefToken* pDVR1 =
586  static_cast<const formula::DoubleVectorRefToken *>(pCur1);
587 
588  size_t nCurWindowSize = pDVR->GetRefRowSize();
589  size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
590 
591  if(nCurWindowSize == nCurWindowSize1)
592  {
593  ss << " if(type == 1.0)\n";
594  ss << " {\n";
595  ss << " for (int i = ";
596  if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed()) &&
597  (!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
598  {
599  ss << "gid0; i < " << pDVR->GetArrayLength();
600  ss << " && i < " << nCurWindowSize << "; i++)\n";
601  ss << " {\n";
602  }
603  else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed()) &&
604  (pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
605  {
606  ss << "0; i < " << pDVR->GetArrayLength();
607  ss << " && i < gid0+"<< nCurWindowSize << "; i++)\n";
608  ss << " {\n";
609  }
610  else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed()) &&
611  (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
612  {
613  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
614  ss << " && i < " << nCurWindowSize << "; i++)\n";
615  ss << " {\n";
616  }
617  else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed()) &&
618  (pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
619  {
620  ss << "0; i < " << nCurWindowSize << "; i++)\n";
621  ss << " {\n";
622  }
623  else
624  {
625  ss << "0; i < " << nCurWindowSize << "; i++)\n";
626  ss << " {\n";
627  ss << " break;\n";
628  ss << " }";
629  ss << " return DBL_MAX;\n";
630  ss << " }\n";
631  ss << "}\n";
632  return ;
633  }
634 
635  ss << " arg1 = ";
636  ss << vSubArguments[0]->GenSlidingWindowDeclRef(true) << ";\n";
637  ss << " arg2 = ";
638  ss << vSubArguments[1]->GenSlidingWindowDeclRef(true) << ";\n";
639  ss << " if (isnan(arg1)||isnan(arg2))\n";
640  ss << " continue;\n";
641  ss << " fSum1 += arg1;\n";
642  ss << " fSum2 += arg2;\n";
643  ss << " fSumSqr1 += (arg1 - arg2)*(arg1 - arg2);\n";
644  ss << " fCount1 += 1;\n";
645  ss << " }\n";
646  ss << " if(fCount1 < 1.0)\n";
647  ss << " return DBL_MAX;\n";
648  ss << " fT = sqrt(fCount1-1.0) * fabs(fSum1 - fSum2)\n";
649  ss << " /sqrt(fCount1 * fSumSqr1 - (fSum1-fSum2)\n";
650  ss << " *(fSum1-fSum2));\n";
651  ss << " fF = fCount1 - 1.0;\n";
652  }
653  else
654  {
655  ss << " return DBL_MAX;\n";
656  ss << "}\n";
657  return ;
658  }
659  }
660  else
661  {
662  ss << " return DBL_MAX;\n";
663  ss << "}\n";
664  return ;
665  }
666  ss << " }\n";
667  ss << " if(type == 2.0 || type == 3.0)\n";
668  ss << " {\n";
669 
670  if(pCur->GetType() == formula::svDoubleVectorRef &&
672  {
673  const formula::DoubleVectorRefToken* pDVR =
674  static_cast<const formula::DoubleVectorRefToken *>(pCur);
675  const formula::DoubleVectorRefToken* pDVR1 =
676  static_cast<const formula::DoubleVectorRefToken *>(pCur1);
677 
678  size_t nCurWindowSize = pDVR->GetRefRowSize();
679  size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
680  ss << " for (int i = ";
681  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
682  {
683  ss << "gid0; i < " << pDVR->GetArrayLength();
684  ss << " && i < " << nCurWindowSize << "; i++)\n";
685  ss << " {\n";
686  }
687  else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
688  {
689  ss << "0; i < " << pDVR->GetArrayLength();
690  ss << " && i < gid0+"<< nCurWindowSize << "; i++)\n";
691  ss << " {\n";
692  }
693  else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
694  {
695  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
696  ss << " && i < " << nCurWindowSize << "; i++)\n";
697  ss << " {\n";
698  }
699  else
700  {
701  ss << "0; i < " << nCurWindowSize << "; i++)\n";
702  ss << " {\n";
703  }
704 
705  ss << " arg1 = ";
706  ss << vSubArguments[0]->GenSlidingWindowDeclRef(true) << ";\n";
707  ss << " if (isnan(arg1))\n";
708  ss << " continue;\n";
709  ss << " fSum1 += arg1;\n";
710  ss << " fSumSqr1 += arg1 * arg1;\n";
711  ss << " fCount1 += 1;\n";
712  ss << " }\n";
713 
714  ss << " for (int i = ";
715  if (!pDVR1->IsStartFixed() && pDVR1->IsEndFixed())
716  {
717  ss << "gid0; i < " << pDVR1->GetArrayLength();
718  ss << " && i < " << nCurWindowSize1 << "; i++)\n";
719  ss << " {\n";
720  }
721  else if (pDVR1->IsStartFixed() && !pDVR1->IsEndFixed())
722  {
723  ss << "0; i < " << pDVR1->GetArrayLength();
724  ss << " && i < gid0+"<< nCurWindowSize1 << "; i++)\n";
725  ss << " {\n";
726  }
727  else if (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed())
728  {
729  ss << "0; i + gid0 < " << pDVR1->GetArrayLength();
730  ss << " && i < " << nCurWindowSize1 << "; i++)\n";
731  ss << " {\n";
732  }
733  else
734  {
735  ss << "0; i < " << nCurWindowSize1 << "; i++)\n";
736  ss << " {\n";
737  }
738  ss << " arg2 = ";
739  ss << vSubArguments[1]->GenSlidingWindowDeclRef(true) << ";\n";
740  ss << " if (isnan(arg2))\n";
741  ss << " continue;\n";
742  ss << " fSum2 += arg2;\n";
743  ss << " fSumSqr2 += arg2 * arg2;\n";
744  ss << " fCount2 += 1;\n";
745  ss << " }\n";
746  }
747  else
748  {
749  ss << " return DBL_MAX;\n";
750  ss << " }\n";
751  ss << "}\n";
752  return ;
753  }
754  ss << " if (fCount1 < 2.0 || fCount2 < 2.0)\n";
755  ss << " return DBL_MAX;\n";
756  ss << " }\n";
757  ss << " if(type == 3.0)\n";
758  ss << " {\n";
759  ss << " double fS1 = (fSumSqr1-fSum1*fSum1/fCount1)\n";
760  ss << " /(fCount1-1.0)/fCount1;\n";
761  ss << " double fS2 = (fSumSqr2-fSum2*fSum2/fCount2)\n";
762  ss << " /(fCount2-1.0)/fCount2;\n";
763  ss << " if (fS1 + fS2 == 0.0)\n";
764  ss << " return DBL_MAX;\n";
765  ss << " fT = fabs(fSum1/fCount1 - fSum2/fCount2)\n";
766  ss << " /sqrt(fS1+fS2);\n";
767  ss << " double c = fS1/(fS1+fS2);\n";
768  ss << " fF = 1.0/(c*c/(fCount1-1.0)+(1.0-c)*(1.0-c)\n";
769  ss << " /(fCount2-1.0));\n";
770  ss << " }\n";
771  ss << " if(type == 2.0)\n";
772  ss << " {\n";
773  ss << " double fS1 = (fSumSqr1 - fSum1*fSum1/fCount1)\n";
774  ss << " /(fCount1 - 1.0);\n";
775  ss << " double fS2 = (fSumSqr2 - fSum2*fSum2/fCount2)\n";
776  ss << " /(fCount2 - 1.0);\n";
777  ss << " fT = fabs( fSum1/fCount1 - fSum2/fCount2 )\n";
778  ss << " /sqrt( (fCount1-1.0)*fS1 + (fCount2-1.0)*fS2 )\n";
779  ss << " *sqrt( fCount1*fCount2*(fCount1+fCount2-2)\n";
780  ss << " /(fCount1+fCount2) );\n";
781  ss << " fF = fCount1 + fCount2 - 2;\n";
782  ss << " }\n";
783 
784  ss << " double tdist=GetTDist(fT, fF);\n";
785  ss << " if (mode==1)\n";
786  ss << " return tdist;\n";
787  ss << " else\n";
788  ss << " return 2.0*tdist;\n";
789  ss << "}\n";
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  throw Unhandled(__FILE__, __LINE__);
2579  }
2580 
2581  const formula::DoubleVectorRefToken* pDVR =
2582  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2583  const formula::DoubleVectorRefToken* pDVR1 =
2584  static_cast<const formula::DoubleVectorRefToken *>(pCur1);
2585 
2586  size_t nCurWindowSize = pDVR->GetRefRowSize();
2587  size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
2588  size_t arrayLength = pDVR->GetArrayLength()<
2589  pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
2590  pDVR1->GetArrayLength();
2591  if(nCurWindowSize != nCurWindowSize1)
2592  throw Unhandled(__FILE__, __LINE__);
2593  ss << " for (int i = ";
2594  if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2595  &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2596  {
2597  ss << "gid0; i < " << arrayLength;
2598  ss << " && i < " << nCurWindowSize << "; i++)\n";
2599  ss << " {\n";
2600  }
2601  else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2602  &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2603  {
2604  ss << "0; i < " << arrayLength ;
2605  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2606  ss << " {\n";
2607  }
2608  else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2609  &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2610  {
2611  ss << "0; i + gid0 < " << arrayLength;
2612  ss << " && i < " << nCurWindowSize << "; i++)\n";
2613  ss << " {\n";
2614  }
2615  else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
2616  &&(pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2617  {
2618  ss << "0; i < " << arrayLength << "; i++)\n";
2619  ss << " {\n";
2620  }
2621  else
2622  {
2623  throw Unhandled(__FILE__, __LINE__);
2624  }
2625 
2626  ss << " argX = ";
2627  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2628  ss << " argY = ";
2629  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2630  ss << " if (isnan(argX) || isnan(argY))\n";
2631  ss << " continue;\n";
2632  ss << " fSumX += argX;\n";
2633  ss << " fSumY += argY;\n";
2634  ss << " fCount += 1.0;\n";
2635  ss << " }\n";
2636 
2637  ss << " if (fCount < 1.0)\n";
2638  ss << " return CreateDoubleError(NoValue);\n";
2639  ss << " else\n";
2640  ss << " {\n";
2641  ss << " fMeanX = fSumX * pow(fCount,-1.0);\n";
2642  ss << " fMeanY = fSumY * pow(fCount,-1.0);\n";
2643 
2644  ss << " for (int i = ";
2645  if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2646  &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2647  {
2648  ss << "gid0; i < " << arrayLength;
2649  ss << " && i < " << nCurWindowSize << "; i++)\n";
2650  ss << " {\n";
2651  }
2652  else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2653  &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2654  {
2655  ss << "0; i < " << arrayLength ;
2656  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2657  ss << " {\n";
2658  }
2659  else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2660  &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2661  {
2662  ss << "0; i + gid0 < " << arrayLength;
2663  ss << " && i < " << nCurWindowSize << "; i++)\n";
2664  ss << " {\n";
2665  }
2666  else
2667  {
2668  ss << "0; i < " << arrayLength << "; i++)\n";
2669  ss << " {\n";
2670  }
2671 
2672  ss << " argX = ";
2673  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2674  ss << " argY = ";
2675  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2676  ss << " if (isnan(argX) || isnan(argY))\n";
2677  ss << " continue;\n";
2678  ss << " fSumDeltaXDeltaY += (argX-fMeanX)*(argY-fMeanY);\n";
2679  ss << " fSumSqrDeltaX += (argX-fMeanX) * (argX-fMeanX);\n";
2680  ss << " }\n";
2681  ss << " if(fSumSqrDeltaX == 0.0)\n";
2682  ss << " return CreateDoubleError(DivisionByZero);\n";
2683  ss << " else\n";
2684  ss << " {\n";
2685  ss << " return fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0);\n";
2686  ss << " }\n";
2687  ss << " }\n";
2688  ss << "}\n";
2689 
2690 }
2691 void OpSTEYX::GenSlidingWindowFunction(std::stringstream &ss,
2692  const std::string &sSymName, SubArguments &vSubArguments)
2693 {
2694  ss << "\ndouble " << sSymName;
2695  ss << "_" << BinFuncName() << "(";
2696  for (size_t i = 0; i < vSubArguments.size(); i++)
2697  {
2698  if (i)
2699  ss << ",";
2700  vSubArguments[i]->GenSlidingWindowDecl(ss);
2701  }
2702  ss << "){\n";
2703  ss << " int gid0 = get_global_id(0);\n";
2704  ss << " double fSumX = 0.0;\n";
2705  ss << " double fSumY = 0.0;\n";
2706  ss << " double fMeanX = 0.0;\n";
2707  ss << " double fMeanY = 0.0;\n";
2708  ss << " double fSumDeltaXDeltaY = 0.0;\n";
2709  ss << " double fSumSqrDeltaX = 0.0;\n";
2710  ss << " double fSumSqrDeltaY = 0.0;\n";
2711  ss << " double fCount = 0.0;\n";
2712  ss << " double argX = 0.0;\n";
2713  ss << " double argY = 0.0;\n";
2714  FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
2715  FormulaToken *pCur1 = vSubArguments[0]->GetFormulaToken();
2716  assert(pCur);
2717  assert(pCur1);
2718  if (pCur->GetType() == formula::svDoubleVectorRef&&
2719  pCur1->GetType() == formula::svDoubleVectorRef)
2720  {
2721  const formula::DoubleVectorRefToken* pDVR =
2722  static_cast<const formula::DoubleVectorRefToken *>(pCur);
2723  const formula::DoubleVectorRefToken* pDVR1 =
2724  static_cast<const formula::DoubleVectorRefToken *>(pCur1);
2725  size_t nCurWindowSize = pDVR->GetRefRowSize();
2726  size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
2727  size_t arrayLength = pDVR->GetArrayLength()<
2728  pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
2729  pDVR1->GetArrayLength();
2730  if(nCurWindowSize != nCurWindowSize1)
2731  {
2732  ss << " return DBL_MAX;\n";
2733  ss << "}\n";
2734  return ;
2735  }
2736  ss << " for (int i = ";
2737  if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2738  &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2739  {
2740  ss << "gid0; i < " << arrayLength;
2741  ss << " && i < " << nCurWindowSize << "; i++)\n";
2742  ss << " {\n";
2743  }
2744  else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2745  &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2746  {
2747  ss << "0; i < " << arrayLength;
2748  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2749  ss << " {\n";
2750  }
2751  else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2752  &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2753  {
2754  ss << "0; i + gid0 < " << arrayLength;
2755  ss << " && i < " << nCurWindowSize << "; i++)\n";
2756  ss << " {\n";
2757  }
2758  else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
2759  &&(pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2760  {
2761  ss << "0; i < " << arrayLength << "; i++)\n";
2762  ss << " {\n";
2763  }
2764  else
2765  {
2766  ss << "0; i < " << nCurWindowSize << "; i++)\n";
2767  ss << " {\n";
2768  ss << " break;\n";
2769  ss << " }";
2770  ss << " return DBL_MAX;\n";
2771  ss << "}\n";
2772  return ;
2773  }
2774 
2775  ss << " argX = ";
2776  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2777  ss << " argY = ";
2778  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2779  ss << " if (isnan(argX) || isnan(argY))\n";
2780  ss << " continue;\n";
2781  ss << " fSumX += argX;\n";
2782  ss << " fSumY += argY;\n";
2783  ss << " fCount += 1.0;\n";
2784  ss << " }\n";
2785 
2786  ss << " if (fCount < 3.0)\n";
2787  ss << " return DBL_MAX;\n";
2788  ss << " else\n";
2789  ss << " {\n";
2790  ss << " fMeanX = fSumX * pow(fCount,-1.0);\n";
2791  ss << " fMeanY = fSumY * pow(fCount,-1.0);\n";
2792 
2793  ss << " for (int i = ";
2794  if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
2795  &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
2796  {
2797  ss << "gid0; i < " << arrayLength;
2798  ss << " && i < " << nCurWindowSize << "; i++)\n";
2799  ss << " {\n";
2800  }
2801  else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
2802  &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2803  {
2804  ss << "0; i < " << arrayLength ;
2805  ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
2806  ss << " {\n";
2807  }
2808  else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())&&
2809  (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
2810  {
2811  ss << "0; i + gid0 < " << arrayLength;
2812  ss << " && i < " << nCurWindowSize << "; i++)\n";
2813  ss << " {\n";
2814  }
2815  else
2816  {
2817  ss << "0; i < " << arrayLength << "; i++)\n";
2818  ss << " {\n";
2819  }
2820 
2821  ss << " argX = ";
2822  ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
2823  ss << " argY = ";
2824  ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
2825  ss << " if (isnan(argX)||isnan(argY))\n";
2826  ss << " continue;\n";
2827  ss << " fSumDeltaXDeltaY +=(argX-fMeanX)*(argY-fMeanY);\n";
2828  ss << " fSumSqrDeltaX += (argX-fMeanX)*(argX-fMeanX);\n";
2829  ss << " fSumSqrDeltaY += (argY-fMeanY)*(argY-fMeanY);\n";
2830  ss << " }\n";
2831  ss << " if(fSumSqrDeltaX == 0.0)\n";
2832  ss << " return DBL_MAX;\n";
2833  ss << " else\n";
2834  ss << " {\n";
2835  ss << " return sqrt((fSumSqrDeltaY - fSumDeltaXDeltaY * \n";
2836  ss << " fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0))\n";
2837  ss << " *pow(fCount - 2.0,-1.0));\n";
2838  ss << " }\n";
2839  ss << " }\n";
2840  ss << "}\n";
2841  }
2842  else
2843  {
2844  ss << " return DBL_MAX;\n";
2845  ss << "}\n";
2846  }
2847 }
2848 void OpFisher::GenSlidingWindowFunction(
2849  std::stringstream &ss, const std::string &sSymName, SubArguments &
2850 vSubArguments)
2851 {
2852  ss << "\ndouble " << sSymName;
2853  ss << "_"<< BinFuncName() <<"(";
2854  for (size_t i = 0; i < vSubArguments.size(); i++)
2855  {
2856  if (i)
2857  ss << ",";
2858  vSubArguments[i]->GenSlidingWindowDecl(ss);
2859  }
2860  ss << ") {\n";
2861  ss <<" int gid0=get_global_id(0);\n";
2862  ss <<" double arg0;\n";
2863  if(vSubArguments.size() != 1)
2864  {
2865  ss << " return DBL_MAX;\n";
2866  return ;
2867  }
2868  FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
2869  assert(pCur);
2870  if (pCur->GetType() == formula::svDoubleVectorRef)
2871  {
2872  ss << " return DBL_MAX;\n";
2873  return ;
2874  }
2875  else if (pCur->GetType() == formula::svSingleVectorRef)
2876  {
2877  const formula::SingleVectorRefToken* pSVR =
2878  static_cast< const formula::SingleVectorRefToken* >(pCur);
2879  ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2880  ss << ";\n";
2881  ss<< " if(isnan(arg0)||(gid0>=";
2882  ss<<pSVR->GetArrayLength();
2883  ss<<"))\n";
2884  ss<<" arg0 = 0;\n";
2885  }
2886  else if (pCur->GetType() == formula::svDouble)
2887  {
2888  ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2889  ss << ";\n";
2890  ss << " if(isnan(arg0))\n";
2891  ss << " return DBL_MAX;\n";
2892  }
2893  ss << " if (fabs(arg0) >= 1.0)\n";
2894  ss << " return DBL_MAX;\n";
2895  ss << " double tmp=0.5*log((1+arg0)*pow((1-arg0),-1));\n";
2896  ss << " return tmp;\n";
2897  ss << "}\n";
2898 }
2899 
2900 void OpFisherInv::GenSlidingWindowFunction(
2901  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
2902 {
2903  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
2904  const formula::SingleVectorRefToken*tmpCurDVR = static_cast<const
2905  formula::SingleVectorRefToken *>(tmpCur);
2906  ss << "\ndouble " << sSymName;
2907  ss << "_"<< BinFuncName() <<"(";
2908  for (size_t i = 0; i < vSubArguments.size(); i++)
2909  {
2910  if (i)
2911  ss << ",";
2912  vSubArguments[i]->GenSlidingWindowDecl(ss);
2913  }
2914  ss << ") {\n\t";
2915  ss <<"int gid0=get_global_id(0);\n\t";
2916  ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2917  ss << ";\n\t";
2918  ss<< "if(isnan(arg0)||(gid0>=";
2919  ss<<tmpCurDVR->GetArrayLength();
2920  ss<<"))\n\t\t";
2921  ss<<"arg0 = 0;\n\t";
2922  ss << "double tmp=tanh(arg0);\n\t";
2923  ss << "return tmp;\n";
2924  ss << "}\n";
2925 }
2926 
2927 void OpGamma::GenSlidingWindowFunction(
2928  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
2929 {
2930  ss << "\ndouble " << sSymName;
2931  ss << "_"<< BinFuncName() <<"(";
2932  for (size_t i = 0; i < vSubArguments.size(); i++)
2933  {
2934  if (i)
2935  ss << ",";
2936  vSubArguments[i]->GenSlidingWindowDecl(ss);
2937  }
2938  ss << ") {\n\t";
2939  ss <<"int gid0=get_global_id(0);\n\t";
2940  ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
2941  ss << ";\n\t";
2942  ss << "double tmp=tgamma(arg0);\n\t";
2943  ss << "return tmp;\n";
2944  ss << "}\n";
2945 }
2946 
2947 void OpCorrel::GenSlidingWindowFunction(
2948  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
2949 {
2950  if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
2951  ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
2952  ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
2954  throw Unhandled(__FILE__, __LINE__);
2955  const formula::DoubleVectorRefToken* pCurDVRX =
2956  static_cast<const formula::DoubleVectorRefToken *>(
2957  vSubArguments[0]->GetFormulaToken());
2958  const formula::DoubleVectorRefToken* pCurDVRY =
2959  static_cast<const formula::DoubleVectorRefToken *>(
2960  vSubArguments[1]->GetFormulaToken());
2961  if( pCurDVRX->GetRefRowSize() != pCurDVRY->GetRefRowSize() )
2962  throw Unhandled(__FILE__, __LINE__);
2963 
2964  ss << "\ndouble " << sSymName;
2965  ss << "_"<< BinFuncName() <<"(";
2966  for (size_t i = 0; i < vSubArguments.size(); i++)
2967  {
2968  if (i)
2969  ss << ",";
2970  vSubArguments[i]->GenSlidingWindowDecl(ss);
2971  }
2972  ss << ") {\n\t";
2973  ss << "double vSum = 0.0;\n\t";
2974  ss << "double vXSum = 0.0;\n\t";
2975  ss << "double vYSum = 0.0;\n\t";
2976  ss << "double vXMean = 0.0;\n\t";
2977  ss << "double vYMean = 0.0;\n\t";
2978 
2979  ss << "int gid0 = get_global_id(0);\n\t";
2980  ss << "double arg0 = 0.0;\n\t";
2981  ss << "double arg1 = 0.0;\n\t";
2982  ss << "int cnt = 0;\n\t";
2983 
2984  size_t nCurWindowSizeX = pCurDVRY->GetRefRowSize();
2985 
2986  ss << "for (int i = ";
2987  if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
2988  ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
2989  ss << "arg0 = " << vSubArguments[0]
2990  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
2991  ss << "arg1 = " << vSubArguments[1]
2992  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
2993  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
2994  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
2995  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
2996  ss << "arg0 = 0.0;\n\t\t\t";
2997  ss << "arg1 = 0.0;\n\t\t\t";
2998  ss << "--cnt;\n\t\t";
2999  ss << "}\n\t\t";
3000  ss << "++cnt;\n\t\t";
3001  ss << "vXSum += arg0;\n\t\t";
3002  ss << "vYSum += arg1;\n\t";
3003  ss << "}\n\t";
3004  } else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
3005  ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n\t\t";
3006  ss << "arg0 = " << vSubArguments[0]
3007  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3008  ss << "arg1 = " << vSubArguments[1]
3009  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3010  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
3011  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3012  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
3013  ss << "arg0 = 0.0;\n\t\t\t";
3014  ss << "arg1 = 0.0;\n\t\t\t";
3015  ss << "--cnt;\n\t\t";
3016  ss << "}\n\t\t";
3017  ss << "++cnt;\n\t\t";
3018  ss << "vXSum += arg0;\n\t\t";
3019  ss << "vYSum += arg1;\n\t";
3020  ss << "}\n\t";
3021  }
3022  else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
3023  ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
3024  ss << "arg0 = " << vSubArguments[0]
3025  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3026  ss << "arg1 = " << vSubArguments[1]
3027  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3028  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
3029  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3030  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
3031  ss << "arg0 = 0.0;\n\t\t\t";
3032  ss << "arg1 = 0.0;\n\t\t\t";
3033  ss << "--cnt;\n\t\t";
3034  ss << "}\n\t\t";
3035  ss << "++cnt;\n\t\t";
3036  ss << "vXSum += arg0;\n\t\t";
3037  ss << "vYSum += arg1;\n\t";
3038  ss << "}\n\t";
3039  } else {
3040  ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
3041  ss << "arg0 = " << vSubArguments[0]
3042  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3043  ss << "arg1 = " << vSubArguments[1]
3044  ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
3045  ss << "if(isnan(arg0) || isnan(arg1) || (i + gid0 >= ";
3046  ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >=";
3047  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
3048  ss << "arg0 = 0.0;\n\t\t\t";
3049  ss << "arg1 = 0.0;\n\t\t\t";
3050  ss << "--cnt;\n\t\t";
3051  ss << "}\n\t\t";
3052  ss << "++cnt;\n\t\t";
3053  ss << "vXSum += arg0;\n\t\t";
3054  ss << "vYSum += arg1;\n\t";
3055  ss << "}\n\t";
3056  }
3057 
3058  ss << "if(cnt < 1) {\n\t\t";
3059  ss << "return DBL_MIN;\n\t";
3060  ss << "}\n\t";
3061  ss << "else {\n\t\t";
3062  ss << "vXMean = vXSum/cnt;\n\t\t";
3063  ss << "vYMean = vYSum/cnt;\n\t\t";
3064  ss << "vXSum = 0.0;\n\t\t";
3065  ss << "vYSum = 0.0;\n\t\t";
3066 
3067  ss << "for (int i = ";
3068  if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
3069  ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3070  ss << "arg0 = " << vSubArguments[0]
3071  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3072  ss << "arg1 = " << vSubArguments[1]
3073  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3074  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
3075  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3076  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3077  ss << "arg0 = vXMean;\n\t\t\t\t";
3078  ss << "arg1 = vYMean;\n\t\t\t";
3079  ss << "}\n\t\t\t";
3080  ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
3081  ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
3082  ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3083  ss << "}\n\t\t";
3084  } else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
3085  ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3086  ss << "arg0 = " << vSubArguments[0]
3087  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3088  ss << "arg1 = " << vSubArguments[1]
3089  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3090  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
3091  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3092  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3093  ss << "arg0 = vXMean;\n\t\t\t\t";
3094  ss << "arg1 = vYMean;\n\t\t\t";
3095  ss << "}\n\t\t\t";
3096  ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
3097  ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
3098  ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3099  ss << "}\n\t\t";
3100  } else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
3101  ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3102  ss << "arg0 = " << vSubArguments[0]
3103  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3104  ss << "arg1 = " << vSubArguments[1]
3105  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3106  ss << "if(isnan(arg0) || isnan(arg1) || (i >= ";
3107  ss << pCurDVRX->GetArrayLength() << ") || (i >=";
3108  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3109  ss << "arg0 = vXMean;\n\t\t\t\t";
3110  ss << "arg1 = vYMean;\n\t\t\t";
3111  ss << "}\n\t\t\t";
3112  ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
3113  ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
3114  ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3115  ss << "}\n\t\t";
3116  } else {
3117  ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
3118  ss << "arg0 = " << vSubArguments[0]
3119  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3120  ss << "arg1 = " << vSubArguments[1]
3121  ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
3122  ss << "if(isnan(arg0) || isnan(arg1) || (i + gid0 >= ";
3123  ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >=";
3124  ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
3125  ss << "arg0 = vXMean;\n\t\t\t\t";
3126  ss << "arg1 = vYMean;\n\t\t\t";
3127  ss << "}\n\t\t\t";
3128  ss << "vXSum += ((arg0 - vXMean)*(arg0 - vXMean));\n\t\t\t";
3129  ss << "vYSum += ((arg1 - vYMean)*(arg1 - vYMean));\n\t\t\t";
3130  ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
3131  ss << "}\n\t\t";
3132  }
3133 
3134  ss << "if(vXSum == 0.0 || vYSum == 0.0) {\n\t\t\t";
3135  ss << "return NAN;\n\t\t";
3136  ss << "}\n\t\t";
3137  ss << "else {\n\t\t\t";
3138  ss << "return vSum/pow(vXSum*vYSum, 0.5);\n\t\t";
3139  ss << "}\n\t";
3140  ss << "}\n";
3141  ss << "}";
3142 }
3143 
3144 void OpNegbinomdist::GenSlidingWindowFunction(
3145  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3146 {
3147  ss << "\ndouble " << sSymName;
3148  ss << "_"<< BinFuncName() <<"(";
3149  for (size_t i = 0; i < vSubArguments.size(); i++)
3150  {
3151  if (i)
3152  ss << ",";
3153  vSubArguments[i]->GenSlidingWindowDecl(ss);
3154  }
3155  ss << ")\n";
3156  ss << "{\n\t";
3157  ss << "double f,s,p,tmp0,tmp1,tmp2;\n";
3158  ss << " int gid0=get_global_id(0);\n";
3159  ss <<"\n";
3160  for (size_t i = 0; i < vSubArguments.size(); i++)
3161  {
3162  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3163  assert(pCur);
3164  if (pCur->GetType() == formula::svSingleVectorRef)
3165  {
3166  const formula::SingleVectorRefToken* pSVR =
3167  static_cast< const formula::SingleVectorRefToken* >(pCur);
3168  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3169  }
3170  else if (pCur->GetType() == formula::svDouble)
3171  {
3172  ss << "{\n";
3173  }
3174 
3175  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3176  {
3177  ss << " if (isnan(";
3178  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3179  ss << "))\n";
3180  ss << " tmp"<<i<<"= 0;\n";
3181  ss << " else\n";
3182  ss << " tmp"<<i<<"=\n";
3183  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3184  ss << ";\n}\n";
3185  }
3186  else
3187  {
3188  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3189  ss <<";\n";
3190  }
3191  }
3192  ss << " p = tmp2;\n";
3193  ss << " s = tmp1;\n";
3194  ss << " f = tmp0;\n";
3195  ss << " double q = 1.0 - p;\n\t";
3196  ss << " double fFactor = pow(p,s);\n\t";
3197  ss << " for(int i=0; i<f; i++)\n\t";
3198  ss << " {\n\t";
3199  ss << " fFactor *= ((double)i+s)*pow(((double)i+1.0),-1.0)/pow(q,-1);\n";
3200  ss << " }\n\t";
3201  ss << " double temp=fFactor;\n\t";
3202  ss << " return temp;\n";
3203  ss << "}\n";
3204 }
3205 
3206 void OpPearson::GenSlidingWindowFunction(
3207  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3208 {
3209  if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
3210  ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
3211  ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
3213  throw Unhandled(__FILE__, __LINE__);
3214  const formula::DoubleVectorRefToken* pDVR =
3215  static_cast<const formula::DoubleVectorRefToken *>(
3216  vSubArguments[0]->GetFormulaToken());
3217  const formula::DoubleVectorRefToken* pCurDVRY =
3218  static_cast<const formula::DoubleVectorRefToken *>(
3219  vSubArguments[1]->GetFormulaToken());
3220  if( pDVR->GetRefRowSize() != pCurDVRY->GetRefRowSize() )
3221  throw Unhandled(__FILE__, __LINE__);
3222 
3223  size_t nCurWindowSize = pDVR->GetRefRowSize();
3224 
3225  ss << "\ndouble " << sSymName;
3226  ss << "_"<< BinFuncName() <<"(";
3227  for (size_t i = 0; i < vSubArguments.size(); i++)
3228  {
3229  if (i)
3230  ss << ",";
3231  vSubArguments[i]->GenSlidingWindowDecl(ss);
3232  }
3233  ss << ")\n";
3234  ss << "{\n";
3235  ss << " int gid0=get_global_id(0);\n";
3236  ss << " double fCount = 0.0;\n";
3237  ss << " double fSumX = 0.0;\n";
3238  ss << " double fSumY = 0.0;\n";
3239  ss << " double fSumDeltaXDeltaY = 0.0;\n";
3240  ss << " double fInx;\n";
3241  ss << " double fIny;\n";
3242  ss << "for (int i = ";
3243  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
3244  ss << "gid0; i < " << pDVR->GetArrayLength();
3245  ss << " && i < " << nCurWindowSize << "; i++){\n";
3246  } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
3247  ss << "0; i < " << pDVR->GetArrayLength();
3248  ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
3249  } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
3250  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3251  ss << " && i < "<< nCurWindowSize << "; i++){\n";
3252  }
3253  else {
3254  ss << "0; i < "<< nCurWindowSize << "; i++){\n";
3255  }
3256  ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef(true);
3257  ss << ";\n";
3258  ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef(true);
3259  ss << " ;\n";
3260  ss << " if(!isnan(fInx)&&!isnan(fIny)){\n";
3261  ss << " fSumX += fInx;\n";
3262  ss << " fSumY += fIny;\n";
3263  ss << " fCount = fCount + 1;\n";
3264  ss << " }\n";
3265  ss << " }\n";
3266  ss << " if(fCount < 1)\n";
3267  ss << " return CreateDoubleError(NoValue);\n";
3268  ss << " double fMeanX = fSumX / fCount;\n";
3269  ss << " double fMeanY = fSumY / fCount;\n";
3270  ss << " fSumX = 0.0;\n";
3271  ss << " fSumY = 0.0;\n";
3272  ss << "for (int i = ";
3273  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
3274  ss << "gid0; i < " << pDVR->GetArrayLength();
3275  ss << " && i < " << nCurWindowSize << "; i++){\n";
3276  } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
3277  ss << "0; i < " << pDVR->GetArrayLength();
3278  ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
3279  } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
3280  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3281  ss << " && i < "<< nCurWindowSize << "; i++){\n";
3282  }
3283  else {
3284  ss << "0; i < "<< nCurWindowSize << "; i++){\n";
3285  }
3286  ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef(true);
3287  ss << " ;\n";
3288  ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef(true);
3289  ss << " ;\n";
3290  ss << " if(!isnan(fInx)&&!isnan(fIny)){\n";
3291  ss << " fSumDeltaXDeltaY += (fInx - fMeanX) * (fIny - fMeanY);\n";
3292  ss << " fSumX += (fInx - fMeanX) * (fInx - fMeanX);\n";
3293  ss << " fSumY += (fIny - fMeanY) * (fIny - fMeanY);\n";
3294  ss << " }\n";
3295  ss << " }\n";
3296  ss << " if (fSumX == 0 || fSumY == 0)\n";
3297  ss << " return CreateDoubleError(DivisionByZero);\n";
3298  ss << " double tmp = ( fSumDeltaXDeltaY / sqrt( fSumX * fSumY));\n";
3299  ss << " return tmp;\n";
3300  ss << "}\n";
3301 }
3302 
3303 void OpGammaLn::GenSlidingWindowFunction(
3304  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3305 {
3306  FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
3307  const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
3309  ss << "\ndouble " << sSymName;
3310  ss << "_"<< BinFuncName() <<"(";
3311  for (size_t i = 0; i < vSubArguments.size(); i++)
3312  {
3313  if (i)
3314  ss << ",";
3315  vSubArguments[i]->GenSlidingWindowDecl(ss);
3316  }
3317  ss << ") {\n\t";
3318  ss <<"int gid0=get_global_id(0);\n\t";
3319  ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
3320  ss << ";\n\t";
3321  ss<< "if(isnan(arg0)||(gid0>=";
3322  ss<<tmpCurDVR->GetArrayLength();
3323  ss<<"))\n\t\t";
3324  ss<<"arg0 = 0;\n\t";
3325  ss << "double tmp=lgamma(arg0);\n\t";
3326  ss << "return tmp;\n";
3327  ss << "}\n";
3328 }
3329 void OpGauss::BinInlineFun(std::set<std::string>& decls,
3330  std::set<std::string>& funs)
3331 {
3332  decls.insert(taylorDecl);decls.insert(phiDecl);
3333  decls.insert(gaussDecl);
3334  funs.insert(taylor);funs.insert(phi);
3335  funs.insert(gauss);
3336 }
3337 
3338 void OpGauss::GenSlidingWindowFunction(
3339  std::stringstream &ss, const std::string &sSymName, SubArguments &
3340 vSubArguments)
3341 {
3342  ss << "\ndouble " << sSymName;
3343  ss << "_"<< BinFuncName() <<"(";
3344  for (size_t i = 0; i < vSubArguments.size(); i++)
3345  {
3346  if (i)
3347  ss << ",";
3348  vSubArguments[i]->GenSlidingWindowDecl(ss);
3349  }
3350  ss << ") {\n";
3351  ss <<" int gid0=get_global_id(0);\n";
3352  ss <<" double arg0;\n";
3353  if(vSubArguments.size() != 1)
3354  {
3355  ss << " return DBL_MAX;\n";
3356  return ;
3357  }
3358  FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
3359  assert(pCur);
3360  if (pCur->GetType() == formula::svDoubleVectorRef)
3361  {
3362  ss << " return DBL_MAX;\n";
3363  return ;
3364  }
3365  else if (pCur->GetType() == formula::svSingleVectorRef)
3366  {
3367  const formula::SingleVectorRefToken* pSVR =
3368  static_cast< const formula::SingleVectorRefToken* >(pCur);
3369  ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
3370  ss << ";\n";
3371  ss<< " if(isnan(arg0)||(gid0>=";
3372  ss<<pSVR->GetArrayLength();
3373  ss<<"))\n";
3374  ss<<" arg0 = 0;\n";
3375  }
3376  else if (pCur->GetType() == formula::svDouble)
3377  {
3378  ss << " arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
3379  ss << ";\n";
3380  ss << " if(isnan(arg0))\n";
3381  ss << " return DBL_MAX;\n";
3382  }
3383  ss << " double tmp=gauss(arg0);\n";
3384  ss << " return tmp;\n";
3385  ss << "}\n";
3386 }
3387 
3388 void OpGeoMean::GenSlidingWindowFunction(
3389  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3390 {
3391  ss << "__kernel void ";
3392  ss << "GeoMean_reduction( ";
3393  for (size_t i = 0; i < vSubArguments.size(); i++)
3394  {
3395  if (i)
3396  ss << ",";
3397  vSubArguments[i]->GenSlidingWindowDecl(ss);
3398  }
3399  ss << ", __global double *result)\n";
3400  ss << "{\n";
3401  ss << " double tmp =0;\n";
3402  ss << " int count = 0;\n";
3403  ss << " int i ;\n";
3404  GenTmpVariables(ss,vSubArguments);
3405  ss << " double current_sum = 0.0;\n";
3406  ss << " int windowSize;\n";
3407  ss << " int arrayLength;\n";
3408  ss << " int current_count = 0;\n";
3409  ss << " int writePos = get_group_id(1);\n";
3410  ss << " int lidx = get_local_id(0);\n";
3411  ss << " __local double shm_buf[256];\n";
3412  ss << " __local int count_buf[256];\n";
3413  ss << " int loop;\n";
3414  ss << " int offset;\n";
3415  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3416 
3417  for(const DynamicKernelArgumentRef & rArg : vSubArguments)
3418  {
3419  assert(rArg->GetFormulaToken());
3420 
3421  if(rArg->GetFormulaToken()->GetType() ==
3423  {
3424  FormulaToken *tmpCur = rArg->GetFormulaToken();
3425  const formula::DoubleVectorRefToken*pCurDVR= static_cast<const
3426  formula::DoubleVectorRefToken *>(tmpCur);
3427  size_t nCurWindowSize = pCurDVR->GetArrayLength() <
3428  pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
3429  pCurDVR->GetRefRowSize() ;
3430 
3431  if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
3432  ss << " offset = 0;\n";
3433  else if (!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
3434  ss << " offset = get_group_id(1);\n";
3435  else
3436  throw Unhandled(__FILE__, __LINE__);
3437  ss << " windowSize = ";
3438  ss << nCurWindowSize;
3439  ss << ";\n";
3440  ss << " arrayLength = ";
3441  ss << pCurDVR->GetArrayLength();
3442  ss << ";\n";
3443  ss << " loop = arrayLength/512 + 1;\n";
3444  ss << " for (int l=0; l<loop; l++){\n";
3445  ss << " tmp = 0.0;\n";
3446  ss << " count = 0;\n";
3447  ss << " int loopOffset = l*512;\n";
3448  ss << " int p1 = loopOffset + lidx + offset, p2 = p1 + 256;\n";
3449  ss << " if (p2 < min(offset + windowSize, arrayLength)) {\n";
3450  ss << " tmp0 = 0.0;\n";
3451  std::string p1 = "p1";
3452  std::string p2 = "p2";
3453 
3454  ss << " tmp0 =";
3455  rArg->GenDeclRef(ss);
3456  ss << "["<<p1.c_str()<<"];\n";
3457  ss << " if(!isnan(tmp0))\n";
3458  ss << " {\n";
3459  ss << " tmp += log(tmp0);\n";
3460  ss << " count++;\n";
3461  ss << " }\n";
3462 
3463  ss << " tmp0 =";
3464  rArg->GenDeclRef(ss);
3465  ss << "["<<p2.c_str()<<"];\n";
3466  ss << " if(!isnan(tmp0))\n";
3467  ss << " {\n";
3468  ss << " tmp += log(tmp0);\n";
3469  ss << " count++;\n";
3470  ss << " }\n";
3471 
3472  ss << " }\n";
3473  ss << " else if (p1 < min(arrayLength, offset + windowSize)) {\n";
3474 
3475  ss << " tmp0 =";
3476  rArg->GenDeclRef(ss);
3477  ss << "["<<p1.c_str()<<"];\n";
3478  ss << " if(!isnan(tmp0))\n";
3479  ss << " {\n";
3480  ss << " tmp += log(tmp0);\n";
3481  ss << " count++;\n";
3482  ss << " }\n";
3483 
3484  ss << " }\n";
3485  ss << " shm_buf[lidx] = tmp;\n";
3486  ss << " count_buf[lidx] = count;\n";
3487  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3488 
3489  ss << " for (int i = 128; i >0; i/=2) {\n";
3490  ss << " if (lidx < i)\n";
3491  ss << " {\n";
3492  ss << " shm_buf[lidx] += shm_buf[lidx + i];\n";
3493  ss << " count_buf[lidx] += count_buf[lidx + i];\n";
3494  ss << " }\n";
3495  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3496  ss << " }\n";
3497  ss << " if (lidx == 0)\n";
3498  ss << " {\n";
3499  ss << " current_sum += shm_buf[0];\n";
3500  ss << " current_count += count_buf[0];\n";
3501  ss << " }\n";
3502  // ss << "if(writePos == 14 && lidx ==0)\n";
3503  //ss <<"printf(\"\\n********************sum is %f, count is%d\",current_sum,current_count);\n";
3504  ss << " barrier(CLK_LOCAL_MEM_FENCE);\n";
3505  ss << " }\n";
3506  }else
3507  {
3508  ss << " if (lidx == 0)\n";
3509  ss << " {\n";
3510  ss << " tmp0 =";
3511  if(rArg->GetFormulaToken()->GetType() == formula::svSingleVectorRef)
3512  {
3513  rArg->GenDeclRef(ss);
3514  ss << "[writePos];\n";
3515  }
3516  else
3517  {
3518  rArg->GenDeclRef(ss);
3519  ss <<";\n";
3520  //ss <<"printf(\"\\n********************tmp0 is %f\",tmp0);\n";
3521  }
3522  ss << " if(!isnan(tmp0))\n";
3523  ss << " {\n";
3524  ss << " current_sum += log(tmp0);\n";
3525  ss << " current_count++;\n";
3526  ss << " }\n";
3527  ss << " }\n";
3528  }
3529  }
3530 
3531  ss << " if (lidx == 0)\n";
3532  ss << " result[writePos] = exp(current_sum/current_count);\n";
3533  ss << "}\n";
3534 
3535  ss << "\ndouble " << sSymName;
3536  ss << "_"<< BinFuncName() <<"(";
3537  for (size_t i = 0; i < vSubArguments.size(); i++)
3538  {
3539  if (i)
3540  ss << ",";
3541  vSubArguments[i]->GenSlidingWindowDecl(ss);
3542  }
3543  ss << ")\n {\n";
3544  ss <<" int gid0=get_global_id(0);\n";
3545  ss << " double tmp =0;\n";
3546  ss << " tmp =";
3547  vSubArguments[0]->GenDeclRef(ss);
3548  ss << "[gid0];\n";
3549  ss << " return tmp;\n";
3550  ss << "}";
3551 }
3552 
3553 void OpHarMean::GenSlidingWindowFunction(
3554  std::stringstream &ss, const std::string &sSymName, SubArguments &
3555 vSubArguments)
3556 {
3557 
3558  ss << "\ndouble " << sSymName;
3559  ss << "_"<< BinFuncName() <<"( ";
3560  for (size_t i = 0; i < vSubArguments.size(); i++)
3561  {
3562  if (i)
3563  ss << ",";
3564  vSubArguments[i]->GenSlidingWindowDecl(ss);
3565  }
3566  ss << ")\n";
3567  ss <<"{\n";
3568  ss << " int gid0 = get_global_id(0);\n";
3569  ss << " double nVal=0.0;\n";
3570  ss << " double tmp = 0;\n";
3571  ss << " int length;\n";
3572  ss << " int totallength=0;\n";
3573  for (size_t i = 0; i < vSubArguments.size(); i++)
3574  {
3575  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3576  assert(pCur);
3577  if (pCur->GetType() == formula::svDoubleVectorRef)
3578  {
3579  const formula::DoubleVectorRefToken* pDVR =
3580  static_cast<const formula::DoubleVectorRefToken *>(pCur);
3581  size_t nCurWindowSize = pDVR->GetRefRowSize();
3582  ss << " length="<<nCurWindowSize;
3583  ss << ";\n";
3584  ss << " for (int i = ";
3585  ss << "0; i < "<< nCurWindowSize << "; i++)\n";
3586  ss << " {\n";
3587  ss << " double arg"<<i<<" = ";
3588  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3589  ss << ";\n";
3590  ss << " if(isnan(arg"<<i<<")||((gid0+i)>=";
3591  ss << pDVR->GetArrayLength();
3592  ss << "))\n";
3593  ss << " {\n";
3594  ss << " length--;\n";
3595  ss << " continue;\n";
3596  ss << " }\n";
3597  ss << " nVal += (1.0 *pow(";
3598  ss << " arg"<<i<<",-1));\n";
3599  ss << " }\n";
3600  ss << " totallength +=length;\n";
3601  }
3602  else if (pCur->GetType() == formula::svSingleVectorRef)
3603  {
3604  ss << " tmp = ";
3605  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3606  ss << ";\n";
3607  ss << " if(!isnan(tmp))\n";
3608  ss << " {\n";
3609  ss << " nVal += (1.0 * pow( tmp,-1));\n";
3610  ss << " totallength +=1;\n";
3611  ss << " }\n";
3612  }
3613  else if (pCur->GetType() == formula::svDouble)
3614  {
3615  ss << " tmp = ";
3616  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3617  ss << ";\n";
3618  ss << " nVal += (1.0 *pow( tmp,-1));\n";
3619  ss << " totallength +=1;\n";
3620  }
3621  else
3622  {
3623  ss << " return DBL_MIN;\n";
3624  }
3625  }
3626  ss << " tmp = totallength*pow(nVal,-1);\n";
3627  ss << " return tmp;\n";
3628  ss << "}";
3629 }
3630 
3631 void OpConfidence::BinInlineFun(std::set<std::string>& decls,
3632  std::set<std::string>& funs)
3633 {
3634  decls.insert(gaussinvDecl);
3635  funs.insert(gaussinv);
3636 }
3637 
3638 void OpConfidence::GenSlidingWindowFunction(std::stringstream& ss,
3639  const std::string &sSymName, SubArguments& vSubArguments)
3640 {
3641  ss << "\ndouble " << sSymName;
3642  ss << "_"<< BinFuncName() <<"(";
3643  for (size_t i = 0; i < vSubArguments.size(); i++)
3644  {
3645  if (i)
3646  ss << ",";
3647  vSubArguments[i]->GenSlidingWindowDecl(ss);
3648  }
3649  ss << ") {\n";
3650  ss << " double tmp = " << GetBottom() <<";\n";
3651  ss << " int gid0 = get_global_id(0);\n";
3652  ss << " double alpha = " << GetBottom() <<";\n";
3653  ss << " double sigma = " << GetBottom() <<";\n";
3654  ss << " double size = " << GetBottom() <<";\n";
3655  ss << " double tmp0,tmp1,tmp2;\n";
3656  ss <<"\n";
3657  for (size_t i = 0; i < vSubArguments.size(); i++)
3658  {
3659  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3660  assert(pCur);
3661  if (pCur->GetType() == formula::svSingleVectorRef)
3662  {
3663  const formula::SingleVectorRefToken* pSVR =
3664  static_cast< const formula::SingleVectorRefToken* >(pCur);
3665  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3666  }
3667  else if (pCur->GetType() == formula::svDouble)
3668  {
3669  ss << "{\n";
3670  }
3671 
3672  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3673  {
3674  ss << " if (isnan(";
3675  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3676  ss << "))\n";
3677  ss << " tmp"<<i<<"= 0;\n";
3678  ss << " else\n";
3679  ss << " tmp"<<i<<"=\n";
3680  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3681  ss << ";\n}\n";
3682  }
3683  else
3684  {
3685  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3686  ss <<";\n";
3687  }
3688  }
3689  ss << " alpha = tmp0;\n";
3690  ss << " sigma = tmp1;\n";
3691  ss << " size = tmp2;\n";
3692  ss << " double rn = floor(size);\n";
3693  ss << " if(sigma <= 0.0 || alpha <= 0.0 || alpha >= 1.0";
3694  ss << "|| rn < 1.0)\n";
3695  ss << " tmp = -DBL_MAX;\n";
3696  ss << " else\n";
3697  ss << " tmp = gaussinv(1.0 - alpha * pow(2.0,-1.0)) * sigma ";
3698  ss << "* pow(sqrt( rn ),-1);\n";
3699  ss << " return tmp;\n";
3700  ss << "}";
3701 }
3702 
3703 void OpCritBinom::BinInlineFun(std::set<std::string>& decls,
3704  std::set<std::string>& funs)
3705 {
3706  decls.insert(MinDecl);
3707  funs.insert("");
3708 }
3709 
3710 void OpCritBinom::GenSlidingWindowFunction(std::stringstream& ss,
3711  const std::string &sSymName, SubArguments& vSubArguments)
3712 {
3713  ss << "\ndouble " << sSymName;
3714  ss << "_"<< BinFuncName() <<"(";
3715  for (size_t i = 0; i < vSubArguments.size(); i++)
3716  {
3717  if (i)
3718  ss << ",";
3719  vSubArguments[i]->GenSlidingWindowDecl(ss);
3720  }
3721  ss << ") {\n";
3722  ss << " double tmp = " << GetBottom() <<";\n";
3723  ss << " int gid0 = get_global_id(0);\n";
3724  ss << " double n = " << GetBottom() <<";\n";
3725  ss << " double p = " << GetBottom() <<";\n";
3726  ss << " double alpha = " << GetBottom() <<";\n";
3727  ss << " double tmp0 = 0.0,tmp1 = 0.0,tmp2 = 0.0;\n";
3728  ss <<"\n";
3729  for (size_t i = 0; i < vSubArguments.size(); i++)
3730  {
3731  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3732  assert(pCur);
3733  if (pCur->GetType() == formula::svSingleVectorRef)
3734  {
3735  const formula::SingleVectorRefToken* pSVR =
3736  static_cast< const formula::SingleVectorRefToken* >(pCur);
3737  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3738  }
3739  else if (pCur->GetType() == formula::svDouble)
3740  {
3741  ss << "{\n";
3742  }
3743 
3744  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3745  {
3746  ss << " if (isnan(";
3747  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3748  ss << "))\n";
3749  ss << " tmp"<<i<<"= 0;\n";
3750  ss << " else\n";
3751  ss << " tmp"<<i<<"=\n";
3752  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3753  ss << ";\n}\n";
3754  }
3755  else
3756  {
3757  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3758  ss <<";\n";
3759  }
3760  }
3761  ss << " n = tmp0;\n";
3762  ss << " p = tmp1;\n";
3763  ss << " alpha = tmp2;\n";
3764  ss << " double rn = floor(n);\n";
3765  ss << " if (rn < 0.0 || alpha <= 0.0 || alpha >= 1.0 || p < 0.0";
3766  ss << " || p > 1.0)\n";
3767  ss << " tmp = -DBL_MIN;\n";
3768  ss << " else\n";
3769  ss << " {\n";
3770  ss << " double rq = (0.5 - p) + 0.5;\n";
3771  ss << " double fFactor = pow(rq, rn);\n";
3772  ss << " if (fFactor <= Min)\n";
3773  ss << " {\n";
3774  ss << " fFactor = pow(p, rn);\n";
3775  ss << " if (fFactor <= Min)\n";
3776  ss << " tmp = -DBL_MAX;\n";
3777  ss << " else\n";
3778  ss << " {\n";
3779  ss << " double fSum = 1.0 - fFactor;\n";
3780  ss << " uint max =(uint)(rn), i;\n";
3781  ss << " for (i = 0; i < max && fSum >= alpha; i++)\n";
3782  ss << " {\n";
3783  ss << " fFactor *= (rn - i) * pow((double)(i + 1),-1.0) *";
3784  ss << " rq * pow(p, -1.0);\n";
3785  ss << " fSum -= fFactor;\n";
3786  ss << " }\n";
3787  ss << " tmp = (rn - i);\n";
3788  ss << " }\n";
3789  ss << " }\n";
3790  ss << " else\n";
3791  ss << " {\n";
3792  ss << " double fSum = fFactor;\n";
3793  ss << " uint max = (uint)(rn), i;\n";
3794  ss << " for (i = 0; i < max && fSum < alpha; i++)\n";
3795  ss << " {\n";
3796  ss << " fFactor *= (rn - i) * pow((double)(i + 1), -1.0) *";
3797  ss << " p * pow(rq, -1.0);\n";
3798  ss << " fSum += fFactor;\n";
3799  ss << " }\n";
3800  ss << " tmp = (i);\n";
3801  ss << " }\n";
3802  ss << " }\n";
3803  ss << " return tmp;\n";
3804  ss << "}";
3805 }
3806 
3807 void OpRsq::GenSlidingWindowFunction(
3808  std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
3809 {
3810  if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
3811  ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
3812  ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
3814  throw Unhandled(__FILE__, __LINE__);
3815  const formula::DoubleVectorRefToken* pCurDVR1 =
3816  static_cast<const formula::DoubleVectorRefToken *>(
3817  vSubArguments[0]->GetFormulaToken());
3818  const formula::DoubleVectorRefToken* pCurDVR2 =
3819  static_cast<const formula::DoubleVectorRefToken *>(
3820  vSubArguments[1]->GetFormulaToken());
3821  if( pCurDVR1->GetRefRowSize() != pCurDVR2->GetRefRowSize() )
3822  throw Unhandled(__FILE__, __LINE__);
3823 
3824  size_t nCurWindowSize = pCurDVR1->GetRefRowSize();
3825 
3826  ss << "\ndouble " << sSymName;
3827  ss << "_"<< BinFuncName() <<"(";
3828  for (size_t i = 0; i < vSubArguments.size(); i++)
3829  {
3830  if (i)
3831  ss << ",";
3832  vSubArguments[i]->GenSlidingWindowDecl(ss);
3833  }
3834  ss << ")\n";
3835  ss << "{\n";
3836  ss << " int gid0=get_global_id(0);\n";
3837  ss << " double fCount = 0.0;\n";
3838  ss << " double fSumX = 0.0;\n";
3839  ss << " double fSumY = 0.0;\n";
3840  ss << " double fSumDeltaXDeltaY = 0.0;\n";
3841  ss << " double fInx;\n";
3842  ss << " double fIny;\n";
3843  ss << " double tmp0,tmp1;\n";
3844 
3845  ss <<"\n";
3846 
3847  ss << " for(int i=0; i<"<<nCurWindowSize<<"; i++)\n";
3848  ss << " {\n";
3849  ss << " if(isnan(";
3850  ss << vSubArguments[0]->GenSlidingWindowDeclRef(true);
3851  ss << "))\n";
3852  ss << " fInx = 0;\n";
3853  ss << " else\n";
3854  ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
3855  ss << ";\n";
3856  ss << " if(isnan(";
3857  ss << vSubArguments[1]->GenSlidingWindowDeclRef(true);
3858  ss << "))\n";
3859  ss << " fIny = 0;\n";
3860  ss << " else\n";
3861  ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
3862  ss << " ;\n";
3863  ss << " fSumX += fInx;\n";
3864  ss << " fSumY += fIny;\n";
3865  ss << " fCount = fCount + 1;\n";
3866  ss << " }\n";
3867  ss << " double fMeanX = fSumX / fCount;\n";
3868  ss << " double fMeanY = fSumY / fCount;\n";
3869  ss << " fSumX = 0.0;\n";
3870  ss << " fSumY = 0.0;\n";
3871  ss << " for(int i=0; i<"<<nCurWindowSize<<"; i++)\n";
3872  ss << " {\n";
3873  ss << " if(isnan(";
3874  ss << vSubArguments[0]->GenSlidingWindowDeclRef(true);
3875  ss << "))\n";
3876  ss << " fInx = 0;\n";
3877  ss << " else\n";
3878  ss << " fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
3879  ss << ";\n";
3880  ss << " if(isnan(";
3881  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3882  ss << "))\n";
3883  ss << " fIny = 0;\n";
3884  ss << " else\n";
3885  ss << " fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
3886  ss << " ;\n";
3887  ss << " fSumDeltaXDeltaY += (fInx - fMeanX) * (fIny - fMeanY);\n";
3888  ss << " fSumX += pow(fInx - fMeanX,2);\n";
3889  ss << " fSumY += pow(fIny - fMeanY,2);\n";
3890  ss << " }\n";
3891  ss << " double tmp = pow( fSumDeltaXDeltaY,2) / (fSumX * fSumY);\n";
3892  ss << " return tmp ;\n";
3893  ss << "}\n";
3894 }
3895 
3896 void OpChiInv::BinInlineFun(std::set<std::string>& decls,
3897  std::set<std::string>& funs)
3898 {
3899  decls.insert(fMachEpsDecl);
3900  funs.insert("");
3901  decls.insert(fBigInvDecl);
3902  funs.insert("");
3903  decls.insert(fHalfMachEpsDecl);
3904  funs.insert("");
3905  decls.insert(lcl_IterateInverseChiInvDecl);
3906  funs.insert(lcl_IterateInverseChiInv);
3907  decls.insert(GetChiDistDecl);
3908  funs.insert(GetChiDist);
3909  decls.insert(lcl_HasChangeOfSignDecl);
3910  funs.insert(lcl_HasChangeOfSign);
3911  decls.insert(GetUpRegIGammaDecl);
3912  funs.insert(GetUpRegIGamma);
3913  decls.insert(GetGammaContFractionDecl);
3914  funs.insert(GetGammaContFraction);
3915  decls.insert(GetGammaSeriesDecl);
3916  funs.insert(GetGammaSeries);
3917 }
3918 void OpChiInv::GenSlidingWindowFunction(
3919  std::stringstream &ss,const std::string &sSymName,
3920  SubArguments &vSubArguments)
3921 {
3922  ss << "\ndouble " << sSymName;
3923  ss << "_"<< BinFuncName() <<"(";
3924  for (size_t i = 0; i < vSubArguments.size(); i++)
3925  {
3926  if (i)
3927  ss << ",";
3928  vSubArguments[i]->GenSlidingWindowDecl(ss);
3929  }
3930  ss << ")\n";
3931  ss << "{\n";
3932  ss << " double tmp0,tmp1,tmp;\n";
3933  ss << " int gid0=get_global_id(0);\n";
3934  ss <<"\n ";
3935  for (size_t i = 0; i < vSubArguments.size(); i++)
3936  {
3937  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3938  assert(pCur);
3939  if (pCur->GetType() == formula::svDoubleVectorRef)
3940  {
3941  const formula::DoubleVectorRefToken* pDVR =
3942  static_cast<const formula::DoubleVectorRefToken *>(pCur);
3943  size_t nCurWindowSize = pDVR->GetRefRowSize();
3944  ss << "for (int i = ";
3945  if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
3946  ss << "gid0; i < " << pDVR->GetArrayLength();
3947  ss << " && i < " << nCurWindowSize << "; i++){\n";
3948  } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
3949  ss << "0; i < " << pDVR->GetArrayLength();
3950  ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
3951  } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
3952  ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3953  ss << " && i < "<< nCurWindowSize << "; i++){\n";
3954  } else {
3955  ss << "0; i < "<< nCurWindowSize << "; i++){\n";
3956  }
3957  }
3958  else if (pCur->GetType() == formula::svSingleVectorRef)
3959  {
3960  const formula::SingleVectorRefToken* pSVR =
3961  static_cast< const formula::SingleVectorRefToken* >(pCur);
3962  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3963  }
3964  else if (pCur->GetType() == formula::svDouble)
3965  {
3966  ss << "{\n";
3967  }
3968 
3969  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3970  {
3971  ss << "if (isnan(";
3972  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3973  ss << "))\n";
3974  ss << " tmp"<<i<<"= 0;\n";
3975  ss << "else\n";
3976  ss <<"tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3977  ss << ";\n}\n";
3978  }
3979  else
3980  {
3981  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3982  ss << ";\n";
3983  }
3984  }
3985  ss << " tmp1 = floor(tmp1);";
3986  ss << " if (tmp1 < 1.0 || tmp0 <= 0.0 || tmp0 > 1.0 )\n";
3987  ss << " {\n";
3988  ss << " return DBL_MIN;\n";
3989  ss << " }\n";
3990  ss << " bool bConvError;\n";
3991  ss << " double fVal = lcl_IterateInverseChiInv";
3992  ss << "(tmp0, tmp1, tmp1*0.5, tmp1, &bConvError);\n";
3993  ss << " if(bConvError)\n";
3994  ss << " return DBL_MIN;\n";
3995  ss << " return fVal;\n";
3996  ss << "}\n";
3997 }
3998 void OpNormdist::GenSlidingWindowFunction(
3999  std::stringstream &ss, const std::string &sSymName,
4000  SubArguments &vSubArguments)
4001 {
4002  CHECK_PARAMETER_COUNT(3,4);
4003  ss << "\ndouble " << sSymName;
4004  ss << "_"<< BinFuncName() <<"(";
4005  for (size_t i = 0; i < vSubArguments.size(); i++)
4006  {
4007  if (i)
4008  ss << ",";
4009  vSubArguments[i]->GenSlidingWindowDecl(ss);
4010  }
4011  ss << ")\n";
4012  ss << "{\n";
4013  ss << " double x,mue,sigma,c;\n";
4014  ss << " int gid0=get_global_id(0);\n";
4015  ss << " double tmp0,tmp1,tmp2;\n";
4016  ss << " double tmp3 = 0;\n"; // optional argument
4017  ss <<"\n ";
4018  for (size_t i = 0; i < vSubArguments.size(); i++)
4019  {
4020  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4021  assert(pCur);
4022  if (pCur->GetType() == formula::svSingleVectorRef)
4023  {
4024  const formula::SingleVectorRefToken* pSVR =
4025  static_cast< const formula::SingleVectorRefToken* >(pCur);
4026  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4027  }
4028  else if (pCur->GetType() == formula::svDouble)
4029  {
4030  ss << "{\n";
4031  }
4032  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4033  {
4034  ss << " if (isnan(";
4035  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4036  ss << "))\n";
4037  ss << " tmp"<<i<<"= 0;\n";
4038  ss << " else\n";
4039  ss << " tmp"<<i<<"=\n";
4040  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4041  ss << ";\n}\n";
4042  }
4043  else
4044  {
4045  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4046  ss <<";\n";
4047  }
4048  }
4049  ss << "x = tmp0;\n";
4050  ss << "mue = tmp1;\n";
4051  ss << "sigma = tmp2;\n";
4052  ss << "c = tmp3;\n";
4053  ss << "if(sigma <= 0)\n";
4054  ss << " return CreateDoubleError(IllegalArgument);\n";
4055  ss << "double mid,tmp;\n";
4056  ss << "mid = (x - mue)/sigma;\n";
4057  ss << "if(c)\n";
4058  ss << " tmp = 0.5 *erfc(-mid * 0.7071067811865475);\n";
4059  ss << "else \n";
4060  ss <<" tmp=(0.39894228040143268*exp(-pow(mid,2)/2.0))/sigma;\n";
4061  ss << "return tmp;\n";
4062  ss << "}\n";
4063 }
4064 void OpNormsdist::GenSlidingWindowFunction(
4065  std::stringstream &ss,const std::string &sSymName,
4066  SubArguments &vSubArguments)
4067 {
4068  ss << "\ndouble " << sSymName;
4069  ss << "_"<< BinFuncName() <<"(";
4070  for (size_t i = 0; i < vSubArguments.size(); i++)
4071  {
4072  if (i)
4073  ss << ",";
4074  vSubArguments[i]->GenSlidingWindowDecl(ss);
4075  }
4076  ss << ")\n";
4077  ss << "{\n";
4078  ss << " double x = 0,tmp0 = 0;\n";
4079  ss << " int gid0=get_global_id(0);\n";
4080  ss <<"\n ";
4081  for (size_t i = 0; i < vSubArguments.size(); i++)
4082  {
4083  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4084  assert(pCur);
4085  if (pCur->GetType() == formula::svSingleVectorRef)
4086  {
4087  const formula::SingleVectorRefToken* pSVR =
4088  static_cast< const formula::SingleVectorRefToken* >(pCur);
4089  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4090  }
4091  else if (pCur->GetType() == formula::svDouble)
4092  {
4093  ss << "{\n";
4094  }
4095 
4096  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4097  {
4098  ss << " if (isnan(";
4099  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4100  ss << "))\n";
4101  ss << " tmp"<<i<<"= 0;\n";
4102  ss << " else\n";
4103  ss << " tmp"<<i<<"=\n";
4104  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4105  ss << ";\n}\n";
4106  }
4107  else
4108  {
4109  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4110  ss <<";\n";
4111  }
4112  }
4113  ss << " x = tmp0;\n";
4114  ss << " double tmp = 0.5 * erfc((-1)*x * 0.7071067811865475);\n";
4115  ss << " return tmp;\n";
4116  ss << "}\n";
4117 }
4118 
4119 void OpPermut::GenSlidingWindowFunction(
4120  std::stringstream &ss,const std::string &sSymName,
4121  SubArguments &vSubArguments)
4122 {
4123  ss << "\ndouble " << sSymName;
4124  ss << "_"<< BinFuncName() <<"(";
4125  for (size_t i = 0; i < vSubArguments.size(); i++)
4126  {
4127  if (i)
4128  ss << ",";
4129  vSubArguments[i]->GenSlidingWindowDecl(ss);
4130  }
4131  ss << ") {\n";
4132  ss <<" int gid0=get_global_id(0);\n";
4133  ss <<" double inA;\n";
4134  ss <<" double inB;\n";
4135  ss <<" double tmp0,tmp1;\n";
4136  ss <<" double tmp = 1 ;\n";
4137  ss <<"\n";
4138  for (size_t i = 0; i < vSubArguments.size(); i++)
4139  {
4140  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4141  assert(pCur);
4142  if (pCur->GetType() == formula::svSingleVectorRef)
4143  {
4144  const formula::SingleVectorRefToken* pSVR =
4145  static_cast< const formula::SingleVectorRefToken* >(pCur);
4146  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4147 
4148  }
4149  else if (pCur->GetType() == formula::svDouble)
4150  {
4151  ss << "{\n";
4152  }
4153 
4154  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4155  {
4156  ss << " if (isnan(";
4157  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4158  ss << "))\n";
4159  ss << " tmp"<<i<<"= 0;\n";
4160  ss << " else\n";
4161  ss << " tmp"<<i<<"=\n";
4162  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4163  ss << ";\n}\n";
4164  }
4165  else
4166  {
4167  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4168  ss <<";\n";
4169  }
4170  }
4171  ss << " inA = tmp0;\n";
4172  ss << " inB = tmp1;\n";
4173  ss << " for( int i =0; i<inB; i++)\n";
4174  ss << " {\n";
4175  ss << " tmp *= inA ;\n";
4176  ss << " inA = inA - 1.0;\n";
4177  ss << " }\n";
4178  ss << " return tmp;\n";
4179  ss << "}\n";
4180 }
4181 void OpPermutationA::GenSlidingWindowFunction(
4182  std::stringstream &ss,const std::string &sSymName,
4183  SubArguments &vSubArguments)
4184 {
4185  ss << "\ndouble " << sSymName;
4186  ss << "_"<< BinFuncName() <<"(";
4187  for (size_t i = 0; i < vSubArguments.size(); i++)
4188  {
4189  if (i)
4190  ss << ",";
4191  vSubArguments[i]->GenSlidingWindowDecl(ss);
4192  }
4193  ss << ") {\n";
4194  ss <<" int gid0=get_global_id(0);\n";
4195  ss <<" double inA;\n";
4196  ss <<" double inB;\n";
4197  ss <<" double tmp = 1.0;\n";
4198  FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
4199  const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
4200  formula::SingleVectorRefToken *>(tmpCur0);
4201  FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
4202  const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
4203  formula::SingleVectorRefToken *>(tmpCur1);
4204  ss << "int buffer_fIna_len = ";
4205  ss << tmpCurDVR0->GetArrayLength();
4206  ss << ";\n";
4207  ss << " int buffer_fInb_len = ";
4208  ss << tmpCurDVR1->GetArrayLength();
4209  ss << ";\n";
4210  ss << " if((gid0)>=buffer_fIna_len || isnan(";
4211  ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4212  ss << "))\n";
4213  ss << " inA = 0;\nelse \n";
4214  ss << " inA = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
4215  ss << ";\n";
4216  ss << "if((gid0)>=buffer_fInb_len || isnan(";
4217  ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4218  ss << "))\n";
4219  ss << "inB = 0;\nelse \n";
4220  ss << " inB = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
4221  ss << ";\n";
4222  ss << " for(int i=0; i<inB; i++)\n";
4223  ss << " {\n";
4224  ss << " tmp *= inA;\n";
4225  ss << " }\n";
4226  ss << " return tmp;\n";
4227  ss << "}\n";
4228 }
4229 
4230 void OpPhi::GenSlidingWindowFunction(
4231  std::stringstream &ss,const std::string &sSymName,
4232  SubArguments &vSubArguments)
4233 {
4234  ss << "\ndouble " << sSymName;
4235  ss << "_"<< BinFuncName() <<"(";
4236  for (size_t i = 0; i < vSubArguments.size(); i++)
4237  {
4238  if (i)
4239  ss << ",";
4240  vSubArguments[i]->GenSlidingWindowDecl(ss);
4241  }
4242  ss << ")\n";
4243  ss << "{\n";
4244  ss << " double x,tmp0;\n";
4245  ss << " int gid0=get_global_id(0);\n";
4246  ss <<"\n";
4247  for (size_t i = 0; i < vSubArguments.size(); i++)
4248  {
4249  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4250  assert(pCur);
4251  if (pCur->GetType() == formula::svSingleVectorRef)
4252  {
4253  const formula::SingleVectorRefToken* pSVR =
4254  static_cast< const formula::SingleVectorRefToken* >(pCur);
4255  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4256  }
4257  else if (pCur->GetType() == formula::svDouble)
4258  {
4259  ss << "{\n";
4260  }
4261 
4262  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4263  {
4264  ss << " if (isnan(";
4265  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4266  ss << "))\n";
4267  ss << " tmp"<<i<<"= 0;\n";
4268  ss << " else\n";
4269  ss << " tmp"<<i<<"=\n";
4270  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4271  ss << ";\n}\n";
4272  }
4273  else
4274  {
4275  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4276  ss <<";\n";
4277  }
4278  }
4279  ss << " x = tmp0;\n";
4280  ss << " double tmp = 0.39894228040143268 * exp((-1)*pow(x,2) / 2.0);\n";
4281  ss << " return tmp;\n";
4282  ss << "}\n";
4283 }
4284 
4285 void OpNorminv::GenSlidingWindowFunction(
4286  std::stringstream &ss,const std::string &sSymName,
4287  SubArguments &vSubArguments)
4288 {
4289  ss << "\ndouble " << sSymName;
4290  ss << "_"<< BinFuncName() <<"(";
4291  for (size_t i = 0; i < vSubArguments.size(); i++)
4292  {
4293  if (i)
4294  ss << ",";
4295  vSubArguments[i]->GenSlidingWindowDecl(ss);
4296  }
4297  ss << ")\n";
4298  ss << "{\n";
4299  ss <<" double q,t,z;\n";
4300  ss <<" double x,mue,sigma;\n";
4301  ss <<" double tmp0,tmp1,tmp2;\n";
4302  ss <<" int gid0=get_global_id(0);\n";
4303  ss <<"\n";
4304  for (size_t i = 0; i < vSubArguments.size(); i++)
4305  {
4306  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4307  assert(pCur);
4308  if (pCur->GetType() == formula::svSingleVectorRef)
4309  {
4310  const formula::SingleVectorRefToken* pSVR =
4311  static_cast< const formula::SingleVectorRefToken* >(pCur);
4312  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4313  }
4314  else if (pCur->GetType() == formula::svDouble)
4315  {
4316  ss << "{\n";
4317  }
4318 
4319  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4320  {
4321  ss << " if (isnan(";
4322  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4323  ss << "))\n";
4324  ss << " tmp"<<i<<"= 0;\n";
4325  ss << " else\n";
4326  ss << " tmp"<<i<<"=\n";
4327  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4328  ss << ";\n}\n";
4329  }
4330  else
4331  {
4332  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4333  ss <<";\n";
4334  }
4335  }
4336  ss <<" x = tmp0;\n";
4337  ss <<" mue = tmp1;\n";
4338  ss <<" sigma = tmp2;\n";
4339  ss <<" q = x -0.5;\n";
4340  ss <<" if(fabs(q)<=.425)\n";
4341  ss <<" {\n";
4342  ss <<" t=0.180625-pow(q,2);\n";
4343  ss <<" z=\n"
4344  "q*\n"
4345  "(\n"
4346  "(\n"
4347  "(\n"
4348  "(\n"
4349  "(\n"
4350  "(\n"
4351  "(\n"
4352  "t*2509.0809287301226727";
4353  ss <<"+33430.575583588128105\n"
4354  ")\n"
4355  "*t+67265.770927008700853\n"
4356  ")\n"
4357  "*t+45921.953931549871457\n"
4358  ")\n"
4359  "*t+13731.693765509461125\n"
4360  ")\n"
4361  "*t+1971.5909503065514427\n"
4362  ")\n"
4363  "*t+133.14166789178437745\n"
4364  ")\n"
4365  "*t+3.387132872796366608\n"
4366  ")\n"
4367  "/\n"
4368  "(\n"
4369  "(\n"
4370  "(\n"
4371  "(\n"
4372  "(\n"
4373  "(\n"
4374  "(\n"
4375  "t*5226.495278852854561";
4376  ss <<"+28729.085735721942674\n"
4377  ")\n"
4378  "*t+39307.89580009271061\n"
4379  ")\n"
4380  "*t+21213.794301586595867\n"
4381  ")\n"
4382  "*t+5394.1960214247511077\n"
4383  ")\n"
4384  "*t+687.1870074920579083\n"
4385  ")\n"
4386  "*t+42.313330701600911252\n"
4387  ")\n"
4388  "*t+1.0\n"
4389  ");\n";
4390  ss <<"}\nelse\n{\n";
4391  ss <<" if(q>0)\nt=1-x;\n";
4392  ss <<"else\nt=x;\n";
4393  ss <<"t=sqrt(-log(t));\n";
4394  ss <<"if(t<=5.0)\n{\n";
4395  ss <<"t+=-1.6;\n";
4396  ss <<"z=\n"
4397  "(\n"
4398  "(\n"
4399  "(\n"
4400  "(\n"
4401  "(\n"
4402  "(\n"
4403  "(\n"
4404  "t*7.7454501427834140764e-4";
4405  ss <<"+0.0227238449892691845833\n"
4406  ")\n"
4407  "*t+0.24178072517745061177\n"
4408  ")\n"
4409  "*t+1.27045825245236838258\n"
4410  ")\n"
4411  "*t+3.64784832476320460504\n"
4412  ")\n"
4413  "*t+5.7694972214606914055\n"
4414  ")\n"
4415  "*t+4.6303378461565452959\n"
4416  ")\n"
4417  "*t+1.42343711074968357734\n"
4418  ")\n"
4419  "/\n"
4420  "(\n"
4421  "(\n"
4422  "(\n"
4423  "(\n"
4424  "(\n"
4425  "(\n"
4426  "(\n"
4427  "t*1.05075007164441684324e-9";
4428  ss <<"+5.475938084995344946e-4\n"
4429  ")\n"
4430  "*t+0.0151986665636164571966\n"
4431  ")\n"
4432  "*t+0.14810397642748007459\n"
4433  ")\n"
4434  "*t+0.68976733498510000455\n"
4435  ")\n"
4436  "*t+1.6763848301838038494\n"
4437  ")\n"
4438  "*t+2.05319162663775882187\n"
4439  ")\n"
4440  "*t+1.0\n"
4441  ");\n}\n";
4442  ss <<"else\n{\n";
4443  ss <<"t+=-5.0;\n";
4444  ss <<"z=\n"
4445  "(\n"
4446  "(\n"
4447  "(\n"
4448  "(\n"
4449  "(\n"
4450  "(\n"
4451  "(\n"
4452  "t*2.01033439929228813265e-7";
4453  ss<<"+2.71155556874348757815e-5\n"
4454  ")\n"
4455  "*t+0.0012426609473880784386\n"
4456  ")\n"
4457  "*t+0.026532189526576123093\n"
4458  ")\n"
4459  "*t+0.29656057182850489123\n"
4460  ")\n"
4461  "*t+1.7848265399172913358\n"
4462  ")\n"
4463  "*t+5.4637849111641143699\n"
4464  ")\n"
4465  "*t+6.6579046435011037772\n"
4466  ")\n"
4467  "/\n"
4468  "(\n"
4469  "(\n"
4470  "(\n"
4471  "(\n"
4472  "(\n"
4473  "(\n"
4474  "(\n"
4475  "t*2.04426310338993978564e-15"
4476  "+1.4215117583164458887e-7\n"
4477  ")\n"
4478  "*t+1.8463183175100546818e-5\n"
4479  ")\n"
4480  "*t+7.868691311456132591e-4\n"
4481  ")\n"
4482  "*t+0.0148753612908506148525\n"
4483  ")\n"
4484  "*t+0.13692988092273580531\n"
4485  ")\n"
4486  "*t+0.59983220655588793769\n"
4487  ")\n"
4488  "*t+1.0\n"
4489  ");\n";
4490  ss<<"}\n";
4491  ss << "z = q < 0.0 ? (-1)*z : z;\n";
4492  ss<<"}\n";
4493  ss<<"double tmp = z*sigma + mue;\n";
4494  ss<<"return tmp;\n";
4495  ss<<"}\n";
4496 }
4497 void OpNormsinv:: GenSlidingWindowFunction
4498  (std::stringstream &ss,const std::string &sSymName,
4499  SubArguments &vSubArguments)
4500 {
4501  ss << "\ndouble " << sSymName;
4502  ss << "_"<< BinFuncName() <<"(";
4503  for (size_t i = 0; i < vSubArguments.size(); i++)
4504  {
4505  if (i)
4506  ss << ",";
4507  vSubArguments[i]->GenSlidingWindowDecl(ss);
4508  }
4509  ss << ")\n";
4510  ss << "{\n";
4511  ss << " double q,t,z,x,tmp0;\n";
4512  ss << " int gid0=get_global_id(0);\n";
4513  ss <<"\n";
4514  for (size_t i = 0; i < vSubArguments.size(); i++)
4515  {
4516  FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
4517  assert(pCur);
4518  if (pCur->GetType() == formula::svSingleVectorRef)
4519  {
4520  const formula::SingleVectorRefToken* pSVR =
4521  static_cast< const formula::SingleVectorRefToken* >(pCur);
4522  ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
4523  }
4524  else if (pCur->GetType() == formula::svDouble)
4525  {
4526  ss << "{\n";
4527  }
4528 
4529  if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
4530  {
4531  ss << " if (isnan(";
4532  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4533  ss << "))\n";
4534  ss << " tmp"<<i<<"= 0;\n";
4535  ss << " else\n";
4536  ss << " tmp"<<i<<"=\n";
4537  ss << vSubArguments[i]->GenSlidingWindowDeclRef();
4538  ss << ";\n}\n";
4539  }
4540  else
4541  {
4542  ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
4543  ss <<";\n";
4544  }
4545  }
4546  ss <<" x = tmp0;\n";
4547  ss <<" q = x -0.5;\n";
4548  ss <<" if(fabs(q)<=.425)\n";
4549  ss <<" {\n";
4550  ss <<" t=0.180625-pow(q,2);\n";
4551  ss <<" z=\n"
4552  "q*\n"
4553  "(\n"
4554  "(\n"
4555  "(\n"
4556  "(\n"
4557  "(\n"
4558  "(\n"
4559  "(\n"
4560  "t*2509.0809287301226727";
4561  ss <<"+33430.575583588128105\n"
4562  ")\n"
4563  "*t+67265.770927008700853\n"
4564  ")\n"
4565  "*t+45921.953931549871457\n"
4566  ")\n"
4567  "*t+13731.693765509461125\n"
4568  ")\n"
4569  "*t+1971.5909503065514427\n"
4570  ")\n"
4571  "*t+133.14166789178437745\n"
4572  ")\n"
4573  "*t+3.387132872796366608\n"
4574  ")\n"
4575  "/\n"
4576  "(\n"
4577  "(\n"
4578  "(\n"
4579  "(\n"
4580  "(\n"
4581  "(\n"
4582  "(\n"
4583  "t*5226.495278852854561";
4584  ss <<"+28729.085735721942674\n"
4585  ")\n"
4586  "*t+39307.89580009271061\n"
4587  ")\n"
4588  "*t+21213.794301586595867\n"
4589  ")\n"
4590  "*t+5394.1960214247511077\n"
4591  ")\n"
4592  "*t+687.1870074920579083\n"
4593  ")\n"
4594  "*t+42.313330701600911252\n"
4595  ")\n"
4596  "*t+1.0\n"
4597  ");\n";
4598  ss <<"}\nelse\n{\n";
4599  ss <<" if(q>0)\nt=1-x;\n";
4600  ss <<"else\nt=x;\n";
4601  ss <<"t=sqrt(-log(t));\n";
4602  ss <<"if(t<=5.0)\n{\n";
4603  ss <<"t+=-1.6;\n";
4604  ss <<"z=\n"
4605  "(\n"
4606  "(\n"
4607  "(\n"
4608  "(\n"
4609  "(\n"
4610  "(\n"
4611  "(\n"
4612  "t*7.7454501427834140764e-4";
4613  ss <<"+0.0227238449892691845833\n"
4614  ")\n"
4615  "*t+0.24178072517745061177\n"
4616  ")\n"
4617  "*t+1.27045825245236838258\n"
4618  ")\n"
4619  "*t+3.64784832476320460504\n"
4620  ")\n"
4621  "*t+5.7694972214606914055\n"
4622  ")\n"
4623  "*t+4.6303378461565452959\n"
4624  ")\n"
4625  "*t+1.42343711074968357734\n"
4626  ")\n"
4627  "/\n"
4628  "(\n"
4629  "(\n"
4630  "(\n"
4631  "(\n"
4632  "(\n"
4633  "(\n"
4634  "(\n"
4635  "t*1.05075007164441684324e-9";
4636  ss <<"+5.475938084995344946e-4\n"
4637  ")\n"
4638  "*t+0.0151986665636164571966\n"
4639  ")\n"
4640  "*t+0.14810397642748007459\n"
4641  ")\n"
4642  "*t+0.68976733498510000455\n"
4643  ")\n"
4644  "*t+1.6763848301838038494\n"
4645  ")\n"
4646  "*t+2.05319162663775882187\n"
4647  ")\n"
4648  "*t+1.0\n"
4649  ");\n}\n";
4650  ss <<