LCOV - code coverage report
Current view: top level - sc/source/core/opencl - op_statistical.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 6611 0.1 %
Date: 2014-11-03 Functions: 2 91 2.2 %
Legend: Lines: hit not hit

          Line data    Source code
       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 "formulagroup.hxx"
      13             : #include "document.hxx"
      14             : #include "formulacell.hxx"
      15             : #include "tokenarray.hxx"
      16             : #include "compiler.hxx"
      17             : #include "interpre.hxx"
      18             : #include <formula/vectortoken.hxx>
      19             : #include <sstream>
      20             : #include "opinlinefun_statistical.cxx"
      21             : 
      22             : using namespace formula;
      23             : 
      24             : namespace sc { namespace opencl {
      25           0 : void OpVar::GenSlidingWindowFunction(std::stringstream &ss,
      26             :             const std::string &sSymName, SubArguments &vSubArguments)
      27             : {
      28           0 :     ss << "\ndouble " << sSymName;
      29           0 :     ss << "_" << BinFuncName() << "(";
      30           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
      31             :     {
      32           0 :         if (i)
      33           0 :             ss << ",";
      34           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
      35             :     }
      36           0 :     ss << "){\n";
      37           0 :     ss << "    int gid0 = get_global_id(0);\n";
      38           0 :     ss << "    double fSum = 0.0;\n";
      39           0 :     ss << "    double fMean = 0.0;\n";
      40           0 :     ss << "    double vSum = 0.0;\n";
      41           0 :     ss << "    double fCount = 0.0;\n";
      42           0 :     ss << "    double arg = 0.0;\n";
      43           0 :     unsigned i = vSubArguments.size();
      44           0 :     while (i--)
      45             :     {
      46           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
      47             :         assert(pCur);
      48           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
      49             :         {
      50           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
      51             :             {
      52             :                 const formula::DoubleVectorRefToken* pDVR =
      53           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
      54           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
      55           0 :                 ss << "    for (int i = ";
      56           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
      57             :                 {
      58             : #ifdef  ISNAN
      59           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
      60           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
      61           0 :                     ss << "    {\n";
      62             : #else
      63             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
      64             :                     ss << "    {\n";
      65             : #endif
      66             :                 }
      67           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
      68             :                 {
      69             : #ifdef  ISNAN
      70           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
      71           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
      72           0 :                     ss << "    {\n";
      73             : #else
      74             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
      75             :                     ss << "    {\n";
      76             : #endif
      77             :                 }
      78           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
      79             :                 {
      80             : #ifdef  ISNAN
      81           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
      82           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
      83           0 :                     ss << "    {\n";
      84             : #else
      85             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
      86             :                     ss << "    {\n";
      87             : #endif
      88             :                 }
      89             :                 else
      90             :                 {
      91             : #ifdef  ISNAN
      92           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
      93           0 :                     ss << "    {\n";
      94             : #else
      95             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
      96             :                     ss << "    {\n";
      97             : #endif
      98             :                 }
      99           0 :                 ss << "        arg = ";
     100           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
     101           0 :                 ss << ";\n";
     102             : #ifdef  ISNAN
     103           0 :                 ss << "        if (isNan(arg))\n";
     104           0 :                 ss << "            continue;\n";
     105             : #endif
     106           0 :                 ss << "        fSum += arg;\n";
     107           0 :                 ss << "        fCount += 1.0;\n";
     108           0 :                 ss << "    }\n";
     109             :             }
     110           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
     111             :             {
     112             :                 const formula::SingleVectorRefToken* pSVR =
     113           0 :                     static_cast< const formula::SingleVectorRefToken*>(pCur);
     114             : #ifdef  ISNAN
     115           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
     116           0 :                 ss << "    {\n";
     117             : #endif
     118           0 :                 ss << "        arg = ";
     119           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
     120             : #ifdef ISNAN
     121           0 :                 ss << "        if (!isNan(arg))\n";
     122           0 :                 ss << "        {\n";
     123             : #endif
     124           0 :                 ss << "            fSum += arg;\n";
     125           0 :                 ss << "            fCount += 1.0;\n";
     126             : #ifdef ISNAN
     127           0 :                 ss << "        }\n";
     128           0 :                 ss << "    }\n";
     129             : #endif
     130             :             }
     131             :             else
     132             :             {
     133           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
     134           0 :                 ss << "    fSum += arg;\n";
     135           0 :                 ss << "    fCount += 1.0;\n";
     136             :             }
     137             :         }
     138             :         else
     139             :         {
     140           0 :             ss << "    arg = ";
     141           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
     142           0 :             ss << "    fSum += arg;\n";
     143           0 :             ss << "    fCount += 1.0;\n";
     144             :         }
     145           0 :         if (i == 0)
     146             :         {
     147           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
     148             :         }
     149             :     }
     150           0 :     i = vSubArguments.size();
     151           0 :     while (i--)
     152             :     {
     153           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
     154             :         assert(pCur);
     155           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
     156             :         {
     157           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
     158             :             {
     159             :                 const formula::DoubleVectorRefToken* pDVR =
     160           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
     161           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
     162           0 :                 ss << "    for (int i = ";
     163           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
     164             :                 {
     165             : #ifdef  ISNAN
     166           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
     167           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
     168           0 :                     ss << "    {\n";
     169             : #else
     170             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
     171             :                     ss << "    {\n";
     172             : #endif
     173             :                 }
     174           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
     175             :                 {
     176             : #ifdef  ISNAN
     177           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
     178           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
     179           0 :                     ss << "    {\n";
     180             : #else
     181             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
     182             :                     ss << "    {\n";
     183             : #endif
     184             :                 }
     185           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
     186             :                 {
     187             : #ifdef  ISNAN
     188           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
     189           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
     190           0 :                     ss << "    {\n";
     191             : #else
     192             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
     193             :                     ss << "    {\n";
     194             : #endif
     195             :                 }
     196             :                 else
     197             :                 {
     198             : #ifdef  ISNAN
     199           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
     200           0 :                     ss << "    {\n";
     201             : #else
     202             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
     203             :                     ss << "    {\n";
     204             : #endif
     205             :                 }
     206             : 
     207           0 :                 ss << "        arg = ";
     208           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
     209           0 :                 ss << ";\n";
     210             : #ifdef  ISNAN
     211           0 :                 ss << "        if (isNan(arg))\n";
     212           0 :                 ss << "            continue;\n";
     213             : #endif
     214           0 :                 ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
     215           0 :                 ss << "    }\n";
     216             :             }
     217           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
     218             :             {
     219             :                 const formula::SingleVectorRefToken* pSVR =
     220           0 :                     static_cast< const formula::SingleVectorRefToken*>(pCur);
     221             : #ifdef  ISNAN
     222           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
     223           0 :                 ss << "    {\n";
     224           0 :                 ss << "        if (!isNan(";
     225           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
     226           0 :                 ss << "))\n";
     227           0 :                 ss << "        {\n";
     228             : #endif
     229           0 :                 ss << "            arg = ";
     230           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
     231           0 :                 ss << "            vSum += (arg - fMean) * (arg - fMean);\n";
     232             : #ifdef ISNAN
     233           0 :                 ss << "        }\n";
     234           0 :                 ss << "    }\n";
     235             : #endif
     236             :             }
     237             :             else
     238             :             {
     239           0 :                 ss << "    arg=" << pCur->GetDouble() << ";\n";
     240           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
     241             :             }
     242             :         }
     243             :         else
     244             :         {
     245           0 :             ss << "    arg = ";
     246           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
     247           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
     248             :         }
     249             :     }
     250           0 :     ss << "    if (fCount <= 1.0)\n";
     251           0 :     ss << "        return DBL_MAX;\n";
     252           0 :     ss << "    else\n";
     253           0 :     ss << "        return vSum * pow(fCount - 1.0,-1.0);\n";
     254           0 :     ss << "}\n";
     255           0 : }
     256           0 : void OpZTest::BinInlineFun(std::set<std::string>& decls,
     257             :     std::set<std::string>& funs)
     258             : {
     259           0 :     decls.insert(phiDecl);
     260           0 :     funs.insert(phi);
     261           0 :     decls.insert(taylorDecl);
     262           0 :     funs.insert(taylor);
     263           0 :     decls.insert(gaussDecl);
     264           0 :     funs.insert(gauss);
     265           0 : }
     266           0 : void OpZTest::GenSlidingWindowFunction(std::stringstream &ss,
     267             :             const std::string &sSymName, SubArguments &vSubArguments)
     268             : {
     269           0 :     ss << "\ndouble " << sSymName;
     270           0 :     ss << "_" << BinFuncName() << "(";
     271           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
     272             :     {
     273           0 :         if (i)
     274           0 :             ss << ",";
     275           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
     276             :     }
     277           0 :     ss << "){\n";
     278           0 :     ss << "    int gid0 = get_global_id(0);\n";
     279           0 :     ss << "    double fSum = 0.0;\n";
     280           0 :     ss << "    double fSumSqr = 0.0;\n";
     281           0 :     ss << "    double mue = 0.0;\n";
     282           0 :     ss << "    double fCount = 0.0;\n";
     283           0 :     ss << "    double arg = 0.0;\n";
     284           0 :     ss << "    double sigma = 0.0;\n";
     285           0 :     ss << "    double mu = 0.0;\n";
     286           0 :     if(vSubArguments.size() == 1 || vSubArguments.size() == 0)
     287             :     {
     288           0 :         ss << "    return DBL_MAX;\n";
     289           0 :         ss << "}\n";
     290           0 :         return ;
     291             :     }
     292           0 :     else if(vSubArguments.size() == 2)
     293             :     {
     294           0 :         FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
     295           0 :         FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
     296             :         assert(pCur);
     297             :         assert(pCur1);
     298           0 :         if(pCur->GetType() == formula::svDoubleVectorRef)
     299             :         {
     300             :             const formula::DoubleVectorRefToken* pDVR =
     301           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
     302           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
     303           0 :             ss << "    for (int i = ";
     304           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
     305             :             {
     306             : #ifdef  ISNAN
     307           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
     308           0 :                 ss << " && i < " << nCurWindowSize  << "; i++)\n";
     309           0 :                 ss << "    {\n";
     310             : #else
     311             :                 ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
     312             :                 ss << "    {\n";
     313             : #endif
     314             :             }
     315           0 :             else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
     316             :             {
     317             : #ifdef  ISNAN
     318           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
     319           0 :                 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
     320           0 :                 ss << "    {\n";
     321             : #else
     322             :                 ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
     323             :                 ss << "    {\n";
     324             : #endif
     325             :             }
     326           0 :             else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
     327             :             {
     328             : #ifdef  ISNAN
     329           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
     330           0 :                 ss << " &&  i < " << nCurWindowSize << "; i++)\n";
     331           0 :                 ss << "    {\n";
     332             : #else
     333             :                 ss << "0; i < " << nCurWindowSize << "; i++)\n";
     334             :                 ss << "    {\n";
     335             : #endif
     336             :             }
     337             :             else
     338             :             {
     339             : #ifdef  ISNAN
     340           0 :                 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
     341           0 :                 ss << "    {\n";
     342             : #else
     343             :                 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
     344             :                 ss << "    {\n";
     345             : #endif
     346             :             }
     347             : 
     348           0 :             ss << "        arg = ";
     349           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
     350             : #ifdef  ISNAN
     351           0 :             ss << "        if (isNan(arg))\n";
     352           0 :             ss << "            continue;\n";
     353             : #endif
     354           0 :             ss << "        fSum += arg;\n";
     355           0 :             ss << "        fSumSqr += arg * arg;\n";
     356           0 :             ss << "        fCount += 1.0;\n";
     357           0 :             ss << "    }\n";
     358           0 :             ss << "    if(fCount <= 1.0)\n";
     359           0 :             ss << "        return DBL_MAX;\n";
     360           0 :             ss << "    mue = fSum *pow(fCount,-1.0);\n";
     361           0 :             ss << "    sigma = (fSumSqr-fSum*fSum*";
     362           0 :             ss << "pow(fCount,-1.0))*pow(fCount-1.0,-1.0);\n";
     363             :         }
     364             :         else
     365             :         {
     366           0 :             ss << "    return DBL_MAX;\n";
     367           0 :             ss << "}\n";
     368           0 :             return ;
     369             :         }
     370           0 :         if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
     371             :         {
     372           0 :             if(pCur1->GetType() == formula::svSingleVectorRef)
     373             :             {
     374             :                 const formula::SingleVectorRefToken* pSVR =
     375           0 :                     static_cast<const formula::SingleVectorRefToken* >(pCur1);
     376             : #ifdef  ISNAN
     377           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
     378           0 :                 ss << "    {\n";
     379             : #endif
     380           0 :                 ss << "        mu = " ;
     381           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
     382             : #ifdef ISNAN
     383           0 :                 ss << "        if (isNan(mu))\n";
     384           0 :                 ss << "            mu = 0.0;\n";
     385           0 :                 ss << "    }\n";
     386             : #endif
     387             : 
     388             :             }
     389           0 :             else if(pCur1->GetType() == formula::svDouble)
     390             :             {
     391           0 :                 ss << "    mu = " << pCur1->GetDouble() << ";\n";
     392             :             }
     393             :             else
     394             :             {
     395           0 :                 ss << "    return DBL_MAX;\n";
     396           0 :                 ss << "}\n";
     397           0 :                 return ;
     398             :             }
     399             :         }
     400             :         else
     401             :         {
     402           0 :             ss << "    mu = " ;
     403           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
     404             :         }
     405           0 :         ss << "    return 0.5 - gauss((mue-mu)/sqrt(sigma/fCount));\n";
     406           0 :         ss << "}\n";
     407           0 :         return ;
     408             :     }
     409             :     else
     410             :     {
     411           0 :         FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
     412           0 :         FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
     413           0 :         FormulaToken *pCur2 = vSubArguments[2]->GetFormulaToken();
     414             :         assert(pCur);
     415             :         assert(pCur1);
     416             :         assert(pCur2);
     417           0 :         if(pCur->GetType() == formula::svDoubleVectorRef)
     418             :         {
     419             :             const formula::DoubleVectorRefToken* pDVR =
     420           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
     421           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
     422           0 :             ss << "    for (int i = ";
     423           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
     424             :             {
     425             : #ifdef  ISNAN
     426           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
     427           0 :                 ss << " && i < " << nCurWindowSize  << "; i++)\n";
     428           0 :                 ss << "    {\n";
     429             : #else
     430             :                 ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
     431             :                 ss << "    {\n";
     432             : #endif
     433             :             }
     434           0 :             else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
     435             :             {
     436             : #ifdef  ISNAN
     437           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
     438           0 :                 ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
     439           0 :                 ss << "    {\n";
     440             : #else
     441             :                 ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
     442             :                 ss << "    {\n";
     443             : #endif
     444             :             }
     445           0 :             else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
     446             :             {
     447             : #ifdef  ISNAN
     448           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
     449           0 :                 ss << " &&  i < " << nCurWindowSize << "; i++)\n";
     450           0 :                 ss << "    {\n";
     451             : #else
     452             :                 ss << "0; i < " << nCurWindowSize << "; i++)\n";
     453             :                 ss << "    {\n";
     454             : #endif
     455             :             }
     456             :             else
     457             :             {
     458             : #ifdef  ISNAN
     459           0 :                 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
     460           0 :                 ss << "    {\n";
     461             : #else
     462             :                 ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
     463             :                 ss << "    {\n";
     464             : #endif
     465             :             }
     466           0 :             ss << "        arg = ";
     467           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
     468             : #ifdef  ISNAN
     469           0 :             ss << "        if (isNan(arg))\n";
     470           0 :             ss << "            continue;\n";
     471             : #endif
     472           0 :             ss << "        fSum += arg;\n";
     473           0 :             ss << "        fSumSqr += arg * arg;\n";
     474           0 :             ss << "        fCount += 1.0;\n";
     475           0 :             ss << "    }\n";
     476           0 :             ss << "    if(fCount <= 1.0)\n";
     477           0 :             ss << "        return DBL_MAX;\n";
     478           0 :             ss << "    mue = fSum * pow(fCount,-1.0);\n";
     479             :         }
     480             :         else
     481             :         {
     482           0 :             ss << "    return DBL_MAX;\n";
     483           0 :             ss << "}\n";
     484           0 :             return ;
     485             :         }
     486           0 :         if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
     487             :         {
     488           0 :             if(pCur1->GetType() == formula::svSingleVectorRef)
     489             :             {
     490             :                 const formula::SingleVectorRefToken* pSVR1 =
     491           0 :                     static_cast<const formula::SingleVectorRefToken* >(pCur1);
     492             : #ifdef  ISNAN
     493           0 :                 ss << "    if (gid0 < " << pSVR1->GetArrayLength() << ")\n";
     494           0 :                 ss << "    {\n";
     495             : #endif
     496           0 :                 ss << "        mu = " ;
     497           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
     498             : #ifdef ISNAN
     499           0 :                 ss << "        if (isNan(mu))\n";
     500           0 :                 ss << "            mu = 0.0;\n";
     501           0 :                 ss << "    }\n";
     502             : #endif
     503             :             }
     504           0 :             else if(pCur1->GetType() == formula::svDouble)
     505             :             {
     506           0 :                 ss << "    mu = " << pCur1->GetDouble() << ";\n";
     507             :             }
     508             :             else
     509             :             {
     510           0 :                 ss << "    return DBL_MAX;\n";
     511           0 :                 ss << "}\n";
     512           0 :                 return ;
     513             :             }
     514             :         }
     515             :         else
     516             :         {
     517           0 :             ss << "    mu=" ;
     518           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
     519             :         }
     520           0 :         if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
     521             :         {
     522           0 :             if(pCur2->GetType() == formula::svSingleVectorRef)
     523             :             {
     524             :                 const formula::SingleVectorRefToken* pSVR2 =
     525           0 :                     static_cast<const formula::SingleVectorRefToken* >(pCur2);
     526             : #ifdef  ISNAN
     527           0 :                 ss << "    if (gid0 < " << pSVR2->GetArrayLength() << ")\n";
     528           0 :                 ss << "    {\n";
     529             : #endif
     530           0 :                 ss << "        sigma = " ;
     531           0 :                 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
     532             : #ifdef ISNAN
     533           0 :                 ss << "        if (isNan(sigma))\n";
     534           0 :                 ss << "            sigma = 0.0;\n";
     535           0 :                 ss << "    }\n";
     536             : #endif
     537             :             }
     538           0 :             else if(pCur2->GetType() == formula::svDouble)
     539             :             {
     540           0 :                 ss << "    sigma = " << pCur2->GetDouble() << ";\n";
     541             :             }
     542             :             else
     543             :             {
     544           0 :                 ss << "    return DBL_MAX;\n";
     545           0 :                 ss << "}\n";
     546           0 :                 return ;
     547             :             }
     548             :         }
     549             :         else
     550             :         {
     551           0 :             ss << "    sigma = " ;
     552           0 :             ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
     553             :         }
     554           0 :         ss << "    return 0.5 - gauss((mue-mu)*sqrt(fCount)/sigma);\n";
     555           0 :         ss << "}\n";
     556             :     }
     557             : }
     558             : 
     559           0 : void OpTTest::BinInlineFun(std::set<std::string>& decls,
     560             :     std::set<std::string>& funs)
     561             : {
     562           0 :     decls.insert(fMachEpsDecl);
     563           0 :     funs.insert("");
     564           0 :     decls.insert(fMaxGammaArgumentDecl);
     565           0 :     funs.insert("");
     566           0 :     decls.insert(lcl_getLanczosSumDecl);
     567           0 :     funs.insert(lcl_getLanczosSum);
     568           0 :     decls.insert(GetBetaDecl);
     569           0 :     funs.insert(GetBeta);
     570           0 :     decls.insert(GetLogBetaDecl);
     571           0 :     funs.insert(GetLogBeta);
     572           0 :     decls.insert(GetBetaDistPDFDecl);
     573           0 :     funs.insert(GetBetaDistPDF);
     574           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);
     575           0 :     funs.insert(lcl_GetBetaHelperContFrac);
     576           0 :     decls.insert(GetBetaDistDecl);
     577           0 :     funs.insert(GetBetaDist);
     578           0 :     decls.insert(GetTDistDecl);
     579           0 :     funs.insert(GetTDist);
     580           0 : }
     581             : 
     582           0 : void OpTTest::GenSlidingWindowFunction(std::stringstream &ss,
     583             :             const std::string &sSymName, SubArguments &vSubArguments)
     584             : {
     585           0 :     ss << "\ndouble " << sSymName;
     586           0 :     ss << "_" << BinFuncName() << "(";
     587           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
     588             :     {
     589           0 :         if (i)
     590           0 :             ss << ",";
     591           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
     592             :     }
     593           0 :     ss << "){\n";
     594           0 :     ss << "    int gid0 = get_global_id(0);\n";
     595           0 :     ss << "    double fSum1 = 0.0;\n";
     596           0 :     ss << "    double fSum2 = 0.0;\n";
     597           0 :     ss << "    double fSumSqr1 = 0.0;\n";
     598           0 :     ss << "    double fSumSqr2 = 0.0;\n";
     599           0 :     ss << "    double fCount1 = 0.0;\n";
     600           0 :     ss << "    double fCount2 = 0.0;\n";
     601           0 :     ss << "    double arg1 = 0.0;\n";
     602           0 :     ss << "    double arg2 = 0.0;\n";
     603           0 :     ss << "    double mode = 0.0;\n";
     604           0 :     ss << "    double type = 0.0;\n";
     605           0 :     ss << "    double fT = 0.0;\n";
     606           0 :     ss << "    double fF = 0.0;\n";
     607           0 :     if(vSubArguments.size() != 4)
     608             :     {
     609           0 :         ss << "    return DBL_MAX;\n";
     610           0 :         ss << "}\n";
     611           0 :         return ;
     612             :     }
     613           0 :     if(vSubArguments.size() == 4)
     614             :     {
     615           0 :         FormulaToken *pCur  = vSubArguments[0]->GetFormulaToken();
     616           0 :         FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
     617           0 :         FormulaToken *pCur2 = vSubArguments[2]->GetFormulaToken();
     618           0 :         FormulaToken *pCur3 = vSubArguments[3]->GetFormulaToken();
     619             :         assert(pCur);
     620             :         assert(pCur1);
     621             :         assert(pCur2);
     622             :         assert(pCur3);
     623           0 :         if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
     624             :         {
     625           0 :             if(pCur2->GetType() == formula::svSingleVectorRef)
     626             :             {
     627             :                 const formula::SingleVectorRefToken* pSVR =
     628           0 :                     static_cast< const formula::SingleVectorRefToken*>(pCur2);
     629             : #ifdef  ISNAN
     630           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
     631           0 :                 ss << "    {\n";
     632             : #endif
     633           0 :                 ss << "        mode = " ;
     634           0 :                 ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
     635             : #ifdef ISNAN
     636           0 :                 ss << "        if (isNan(mode))\n";
     637           0 :                 ss << "            mode = 0.0;\n";
     638           0 :                 ss << "        else\n";
     639             : #endif
     640           0 :                 ss << "            mode = floor(mode);\n";
     641             : #ifdef ISNAN
     642           0 :                 ss << "    }\n";
     643             : #endif
     644             :             }
     645           0 :             else if(pCur2->GetType() == formula::svDouble)
     646             :             {
     647           0 :                 ss << "    mode = floor(convert_double(";
     648           0 :                 ss << pCur2->GetDouble() << "));\n";
     649             :             }
     650             :             else
     651             :             {
     652           0 :                 ss << "    return DBL_MAX;\n";
     653           0 :                 ss << "}\n";
     654           0 :                 return ;
     655             :             }
     656             :         }
     657             :         else
     658             :         {
     659           0 :             ss << "    mode = floor(" ;
     660           0 :             ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ");\n";
     661             :         }
     662           0 :         ss << "    if(!(mode == 1.0 || mode == 2.0))\n";
     663           0 :         ss << "        return DBL_MAX;\n";
     664           0 :         if(ocPush==vSubArguments[3]->GetFormulaToken()->GetOpCode())
     665             :         {
     666           0 :             if(pCur3->GetType() == formula::svSingleVectorRef)
     667             :             {
     668             :                 const formula::SingleVectorRefToken* pSVR =
     669           0 :                     static_cast< const formula::SingleVectorRefToken*>(pCur3);
     670             :                 assert(pSVR);
     671             : #ifdef  ISNAN
     672           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
     673           0 :                 ss << "    {\n";
     674           0 :                 ss << "        if (isNan(";
     675           0 :                 ss << vSubArguments[3]->GenSlidingWindowDeclRef() << "))\n";
     676           0 :                 ss << "            type=0.0;\n";
     677           0 :                 ss << "        else\n";
     678             : #endif
     679           0 :                 ss << "            type=floor(";
     680           0 :                 ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ");\n";
     681             : #ifdef ISNAN
     682           0 :                 ss << "    }\n";
     683             : #endif
     684             :             }
     685           0 :             else if(pCur3->GetType() == formula::svDouble)
     686             :             {
     687           0 :                 ss << "    type = floor(convert_double(" << pCur3->GetDouble() <<
     688           0 :                 "));\n";
     689             :             }
     690             :             else
     691             :             {
     692           0 :                 ss << "    return DBL_MAX;\n";
     693           0 :                 ss << "}\n";
     694           0 :                 return ;
     695             :             }
     696             :         }
     697             :         else
     698             :         {
     699           0 :             ss << "    type=floor(";
     700           0 :             ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ");\n";
     701             :         }
     702           0 :         ss << "    if(!(type == 1.0||type == 2.0||type == 3.0))\n";
     703           0 :         ss << "        return DBL_MAX;\n";
     704             : 
     705           0 :         if(pCur->GetType() == formula::svDoubleVectorRef &&
     706           0 :                pCur1->GetType() == formula::svDoubleVectorRef)
     707             :         {
     708             :             const formula::DoubleVectorRefToken* pDVR =
     709           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
     710             :             const formula::DoubleVectorRefToken* pDVR1 =
     711           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur1);
     712             : 
     713           0 :             size_t nCurWindowSize  = pDVR->GetRefRowSize();
     714           0 :             size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
     715             : 
     716           0 :             if(nCurWindowSize == nCurWindowSize1)
     717             :             {
     718           0 :                 ss << "    if(type == 1.0)\n";
     719           0 :                 ss << "    {\n";
     720           0 :                 ss << "        for (int i = ";
     721           0 :                 if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed()) &&
     722           0 :                          (!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
     723             :                 {
     724             : #ifdef  ISNAN
     725           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
     726           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
     727           0 :                     ss << "        {\n";
     728             : #else
     729             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
     730             :                     ss << "        {\n";
     731             : #endif
     732             :                 }
     733           0 :                 else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed()) &&
     734           0 :                              (pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
     735             :                 {
     736             : #ifdef  ISNAN
     737           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
     738           0 :                     ss << " && i < gid0+"<< nCurWindowSize << "; i++)\n";
     739           0 :                     ss << "        {\n";
     740             : #else
     741             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
     742             :                     ss << "        {\n";
     743             : #endif
     744             :                 }
     745           0 :                 else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed()) &&
     746           0 :                              (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
     747             :                 {
     748             : #ifdef  ISNAN
     749           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
     750           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
     751           0 :                     ss << "        {\n";
     752             : #else
     753             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
     754             :                     ss << "        {\n";
     755             : #endif
     756             :                 }
     757           0 :                 else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed()) &&
     758           0 :                              (pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
     759             :                 {
     760             : #ifdef  ISNAN
     761           0 :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
     762           0 :                     ss << "        {\n";
     763             : #else
     764             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
     765             :                     ss << "        {\n";
     766             : #endif
     767             :                 }
     768             :                 else
     769             :                 {
     770             : #ifdef  ISNAN
     771           0 :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
     772           0 :                     ss << "        {\n";
     773             : #else
     774             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
     775             :                     ss << "        {\n";
     776             : #endif
     777           0 :                     ss << "            break;\n";
     778           0 :                     ss << "        }";
     779           0 :                     ss << "        return DBL_MAX;\n";
     780           0 :                     ss << "    }\n";
     781           0 :                     ss << "}\n";
     782           0 :                     return ;
     783             :                 }
     784             : 
     785           0 :                 ss << "            arg1 = ";
     786           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef(true) << ";\n";
     787           0 :                 ss << "            arg2 = ";
     788           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef(true) << ";\n";
     789             : #ifdef  ISNAN
     790           0 :                 ss << "            if (isNan(arg1)||isNan(arg2))\n";
     791           0 :                 ss << "                continue;\n";
     792             : #endif
     793           0 :                 ss << "            fSum1 += arg1;\n";
     794           0 :                 ss << "            fSum2 += arg2;\n";
     795           0 :                 ss << "            fSumSqr1 += (arg1 - arg2)*(arg1 - arg2);\n";
     796           0 :                 ss << "            fCount1 += 1;\n";
     797           0 :                 ss << "        }\n";
     798           0 :                 ss << "        if(fCount1 < 1.0)\n";
     799           0 :                 ss << "            return DBL_MAX;\n";
     800           0 :                 ss << "        fT = sqrt(fCount1-1.0) * fabs(fSum1 - fSum2)\n";
     801           0 :                 ss << "            /sqrt(fCount1 * fSumSqr1 - (fSum1-fSum2)\n";
     802           0 :                 ss << "             *(fSum1-fSum2));\n";
     803           0 :                 ss << "        fF = fCount1 - 1.0;\n";
     804             :             }
     805             :             else
     806             :             {
     807           0 :                 ss << "    return DBL_MAX;\n";
     808           0 :                 ss << "}\n";
     809           0 :                 return ;
     810             :             }
     811             :         }
     812             :         else
     813             :         {
     814           0 :             ss << "    return DBL_MAX;\n";
     815           0 :             ss << "}\n";
     816           0 :             return ;
     817             :         }
     818           0 :         ss << "    }\n";
     819           0 :         ss << "    if(type == 2.0 || type == 3.0)\n";
     820           0 :         ss << "    {\n";
     821             : 
     822           0 :         if(pCur->GetType() == formula::svDoubleVectorRef &&
     823           0 :                pCur1->GetType() == formula::svDoubleVectorRef)
     824             :         {
     825             :             const formula::DoubleVectorRefToken* pDVR =
     826           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
     827             :             const formula::DoubleVectorRefToken* pDVR1 =
     828           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur1);
     829             : 
     830           0 :             size_t nCurWindowSize  = pDVR->GetRefRowSize();
     831           0 :             size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
     832           0 :             ss << "        for (int i = ";
     833           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
     834             :             {
     835             : #ifdef  ISNAN
     836           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
     837           0 :                 ss << " && i < " << nCurWindowSize  << "; i++)\n";
     838           0 :                 ss << "        {\n";
     839             : #else
     840             :                 ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
     841             :                 ss << "        {\n";
     842             : #endif
     843             :             }
     844           0 :             else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
     845             :             {
     846             : #ifdef  ISNAN
     847           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
     848           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++)\n";
     849           0 :                 ss << "        {\n";
     850             : #else
     851             :                 ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
     852             :                 ss << "        {\n";
     853             : #endif
     854             :             }
     855           0 :             else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
     856             :             {
     857             : #ifdef  ISNAN
     858           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
     859           0 :                 ss << " &&  i < " << nCurWindowSize << "; i++)\n";
     860           0 :                 ss << "        {\n";
     861             : #else
     862             :                 ss << "0; i < " << nCurWindowSize << "; i++)\n";
     863             :                 ss << "        {\n";
     864             : #endif
     865             :             }
     866             :             else
     867             :             {
     868             : #ifdef  ISNAN
     869           0 :                 ss << "0; i < " << nCurWindowSize << "; i++)\n";
     870           0 :                 ss << "        {\n";
     871             : #else
     872             :                 ss << "0; i < " << nCurWindowSize << "; i++)\n";
     873             :                 ss << "        {\n";
     874             : #endif
     875             :             }
     876             : 
     877           0 :             ss << "            arg1 = ";
     878           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef(true) << ";\n";
     879             : #ifdef  ISNAN
     880           0 :             ss << "            if (isNan(arg1))\n";
     881           0 :             ss << "                continue;\n";
     882             : #endif
     883           0 :             ss << "            fSum1 += arg1;\n";
     884           0 :             ss << "            fSumSqr1 += arg1 * arg1;\n";
     885           0 :             ss << "            fCount1 += 1;\n";
     886           0 :             ss << "        }\n";
     887             : 
     888           0 :             ss << "        for (int i = ";
     889           0 :             if (!pDVR1->IsStartFixed() && pDVR1->IsEndFixed())
     890             :             {
     891             : #ifdef  ISNAN
     892           0 :                 ss << "gid0; i < " << pDVR1->GetArrayLength();
     893           0 :                 ss << " && i < " << nCurWindowSize1  << "; i++)\n";
     894           0 :                 ss << "        {\n";
     895             : #else
     896             :                 ss << "gid0; i < " << nCurWindowSize1 << "; i++)\n";
     897             :                 ss << "        {\n";
     898             : #endif
     899             :             }
     900           0 :             else if (pDVR1->IsStartFixed() && !pDVR1->IsEndFixed())
     901             :             {
     902             : #ifdef  ISNAN
     903           0 :                 ss << "0; i < " << pDVR1->GetArrayLength();
     904           0 :                 ss << " && i < gid0+"<< nCurWindowSize1 << "; i++)\n";
     905           0 :                 ss << "        {\n";
     906             : #else
     907             :                 ss << "0; i < gid0+" << nCurWindowSize1 << "; i++)\n";
     908             :                 ss << "        {\n";
     909             : #endif
     910             :             }
     911           0 :             else if (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed())
     912             :             {
     913             : #ifdef  ISNAN
     914           0 :                 ss << "0; i + gid0 < " << pDVR1->GetArrayLength();
     915           0 :                 ss << " &&  i < " << nCurWindowSize1 << "; i++)\n";
     916           0 :                 ss << "        {\n";
     917             : #else
     918             :                 ss << "0; i < " << nCurWindowSize1 << "; i++)\n";
     919             :                 ss << "        {\n";
     920             : #endif
     921             :             }
     922             :             else
     923             :             {
     924             : #ifdef  ISNAN
     925           0 :                 ss << "0; i < " << nCurWindowSize1 << "; i++)\n";
     926           0 :                 ss << "        {\n";
     927             : #else
     928             :                 ss << "0; i < " << nCurWindowSize1 << "; i++)\n";
     929             :                 ss << "        {\n";
     930             : #endif
     931             :             }
     932           0 :             ss << "            arg2 = ";
     933           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef(true) << ";\n";
     934             : #ifdef  ISNAN
     935           0 :             ss << "            if (isNan(arg2))\n";
     936           0 :             ss << "                continue;\n";
     937             : #endif
     938           0 :             ss << "            fSum2 += arg2;\n";
     939           0 :             ss << "            fSumSqr2 += arg2 * arg2;\n";
     940           0 :             ss << "            fCount2 += 1;\n";
     941           0 :             ss << "        }\n";
     942             :         }
     943             :         else
     944             :         {
     945           0 :             ss << "        return DBL_MAX;\n";
     946           0 :             ss << "    }\n";
     947           0 :             ss << "}\n";
     948           0 :             return ;
     949             :         }
     950           0 :         ss << "        if (fCount1 < 2.0 || fCount2 < 2.0)\n";
     951           0 :         ss << "            return DBL_MAX;\n";
     952           0 :         ss << "    }\n";
     953           0 :         ss << "    if(type == 3.0)\n";
     954           0 :         ss << "    {\n";
     955           0 :         ss << "        double fS1 = (fSumSqr1-fSum1*fSum1/fCount1)\n";
     956           0 :         ss << "            /(fCount1-1.0)/fCount1;\n";
     957           0 :         ss << "        double fS2 = (fSumSqr2-fSum2*fSum2/fCount2)\n";
     958           0 :         ss << "            /(fCount2-1.0)/fCount2;\n";
     959           0 :         ss << "        if (fS1 + fS2 == 0.0)\n";
     960           0 :         ss << "            return DBL_MAX;\n";
     961           0 :         ss << "        fT = fabs(fSum1/fCount1 - fSum2/fCount2)\n";
     962           0 :         ss << "             /sqrt(fS1+fS2);\n";
     963           0 :         ss << "        double c = fS1/(fS1+fS2);\n";
     964           0 :         ss << "        fF = 1.0/(c*c/(fCount1-1.0)+(1.0-c)*(1.0-c)\n";
     965           0 :         ss << "             /(fCount2-1.0));\n";
     966           0 :         ss << "    }\n";
     967           0 :         ss << "    if(type == 2.0)\n";
     968           0 :         ss << "    {\n";
     969           0 :         ss << "        double fS1 = (fSumSqr1 - fSum1*fSum1/fCount1)\n";
     970           0 :         ss << "             /(fCount1 - 1.0);\n";
     971           0 :         ss << "        double fS2 = (fSumSqr2 - fSum2*fSum2/fCount2)\n";
     972           0 :         ss << "             /(fCount2 - 1.0);\n";
     973           0 :         ss << "        fT = fabs( fSum1/fCount1 - fSum2/fCount2 )\n";
     974           0 :         ss << "            /sqrt( (fCount1-1.0)*fS1 + (fCount2-1.0)*fS2 )\n";
     975           0 :         ss << "            *sqrt( fCount1*fCount2*(fCount1+fCount2-2)\n";
     976           0 :         ss << "            /(fCount1+fCount2) );\n";
     977           0 :         ss << "        fF = fCount1 + fCount2 - 2;\n";
     978           0 :         ss << "    }\n";
     979             : 
     980           0 :         ss << "    double tdist=GetTDist(fT, fF);\n";
     981           0 :         ss << "    if (mode==1)\n";
     982           0 :         ss << "        return tdist;\n";
     983           0 :         ss << "    else\n";
     984           0 :         ss << "        return 2.0*tdist;\n";
     985           0 :         ss << "}\n";
     986             :     }
     987             : }
     988           0 : void OpVarP::GenSlidingWindowFunction(std::stringstream &ss,
     989             :             const std::string &sSymName, SubArguments &vSubArguments)
     990             : {
     991           0 :     ss << "\ndouble " << sSymName;
     992           0 :     ss << "_" << BinFuncName() << "(";
     993           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
     994             :     {
     995           0 :         if (i)
     996           0 :             ss << ",";
     997           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
     998             :     }
     999           0 :     ss << "){\n";
    1000           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1001           0 :     ss << "    double fSum = 0.0;\n";
    1002           0 :     ss << "    double fMean = 0.0;\n";
    1003           0 :     ss << "    double vSum = 0.0;\n";
    1004           0 :     ss << "    double fCount = 0.0;\n";
    1005           0 :     ss << "    double arg = 0.0;\n";
    1006           0 :     unsigned i = vSubArguments.size();
    1007           0 :     while (i--)
    1008             :     {
    1009           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    1010             :         assert(pCur);
    1011           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    1012             :         {
    1013           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    1014             :             {
    1015             :                 const formula::DoubleVectorRefToken* pDVR =
    1016           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    1017           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    1018           0 :                 ss << "    for (int i = ";
    1019           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    1020             :                 {
    1021             : #ifdef  ISNAN
    1022           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    1023           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    1024           0 :                     ss << "    {\n";
    1025             : #else
    1026             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    1027             :                     ss << "    {\n";
    1028             : #endif
    1029             :                 }
    1030           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1031             :                 {
    1032             : #ifdef  ISNAN
    1033           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    1034           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    1035           0 :                     ss << "    {\n";
    1036             : #else
    1037             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    1038             :                     ss << "    {\n";
    1039             : #endif
    1040             :                 }
    1041           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1042             :                 {
    1043             : #ifdef  ISNAN
    1044           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    1045           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    1046           0 :                     ss << "    {\n";
    1047             : #else
    1048             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    1049             :                     ss << "    {\n";
    1050             : #endif
    1051             :                 }
    1052             :                 else
    1053             :                 {
    1054             : #ifdef  ISNAN
    1055           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1056           0 :                     ss << "    {\n";
    1057             : #else
    1058             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1059             :                     ss << "    {\n";
    1060             : #endif
    1061             :                 }
    1062           0 :                 ss << "        arg = ";
    1063           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1064           0 :                 ss << ";\n";
    1065             : #ifdef  ISNAN
    1066           0 :                 ss << "        if (isNan(arg))\n";
    1067           0 :                 ss << "            continue;\n";
    1068             : #endif
    1069           0 :                 ss << "        fSum += arg;\n";
    1070           0 :                 ss << "        fCount += 1.0;\n";
    1071           0 :                 ss << "    }\n";
    1072             :             }
    1073           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    1074             :             {
    1075             :                 const formula::SingleVectorRefToken* pSVR =
    1076           0 :                     static_cast< const formula::SingleVectorRefToken*>(pCur);
    1077             : #ifdef  ISNAN
    1078           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    1079           0 :                 ss << "    {\n";
    1080             : #endif
    1081           0 :                 ss << "        arg = ";
    1082           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1083             : #ifdef  ISNAN
    1084           0 :                 ss << "        if (!isNan(arg))\n";
    1085           0 :                 ss << "        {\n";
    1086             : #endif
    1087           0 :                 ss << "            fSum += arg;\n";
    1088           0 :                 ss << "            fCount += 1.0;\n";
    1089             : #ifdef ISNAN
    1090           0 :                 ss << "        }\n";
    1091           0 :                 ss << "    }\n";
    1092             : #endif
    1093             :             }
    1094             :             else
    1095             :             {
    1096           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    1097           0 :                 ss << "    fSum += arg;\n";
    1098           0 :                 ss << "    fCount += 1.0;\n";
    1099             :             }
    1100             :         }
    1101             :         else
    1102             :         {
    1103           0 :             ss << "    arg = ";
    1104           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1105           0 :             ss << "    fSum += arg;\n";
    1106           0 :             ss << "    fCount += 1.0;\n";
    1107             :         }
    1108           0 :         if (i == 0)
    1109             :         {
    1110           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
    1111             :         }
    1112             :     }
    1113           0 :     i = vSubArguments.size();
    1114           0 :     while (i--)
    1115             :     {
    1116           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    1117             :         assert(pCur);
    1118           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    1119             :         {
    1120           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    1121             :             {
    1122             :                 const formula::DoubleVectorRefToken* pDVR =
    1123           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    1124           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    1125           0 :                 ss << "    for (int i = ";
    1126           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    1127             :                 {
    1128             : #ifdef  ISNAN
    1129           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    1130           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    1131           0 :                     ss << "    {\n";
    1132             : #else
    1133             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    1134             :                     ss << "    {\n";
    1135             : #endif
    1136             :                 }
    1137           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1138             :                 {
    1139             : #ifdef  ISNAN
    1140           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    1141           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    1142           0 :                     ss << "    {\n";
    1143             : #else
    1144             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    1145             :                     ss << "    {\n";
    1146             : #endif
    1147             :                 }
    1148           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1149             :                 {
    1150             : #ifdef  ISNAN
    1151           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    1152           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    1153           0 :                     ss << "    {\n";
    1154             : #else
    1155             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    1156             :                     ss << "    {\n";
    1157             : #endif
    1158             :                 }
    1159             :                 else
    1160             :                 {
    1161             : #ifdef  ISNAN
    1162           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1163           0 :                     ss << "    {\n";
    1164             : #else
    1165             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1166             :                     ss << "    {\n";
    1167             : #endif
    1168             :                 }
    1169             : 
    1170           0 :                 ss << "        arg = ";
    1171           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1172           0 :                 ss << ";\n";
    1173             : #ifdef  ISNAN
    1174           0 :                 ss << "        if (isNan(arg))\n";
    1175           0 :                 ss << "            continue;\n";
    1176             : #endif
    1177           0 :                 ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
    1178           0 :                 ss << "    }\n";
    1179             :             }
    1180           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    1181             :             {
    1182             :                 const formula::SingleVectorRefToken* pSVR =
    1183           0 :                     static_cast< const formula::SingleVectorRefToken*>(pCur);
    1184             : #ifdef  ISNAN
    1185           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    1186           0 :                 ss << "    {\n";
    1187             : #endif
    1188           0 :                 ss << "        arg = ";
    1189           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1190             : #ifdef  ISNAN
    1191           0 :                 ss << "        if (!isNan(arg))\n";
    1192           0 :                 ss << "        {\n";
    1193             : #endif
    1194           0 :                 ss << "            vSum += (arg - fMean) * (arg - fMean);\n";
    1195             : #ifdef ISNAN
    1196           0 :                 ss << "        }\n";
    1197           0 :                 ss << "    }\n";
    1198             : #endif
    1199             :             }
    1200             :             else
    1201             :             {
    1202           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    1203           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    1204             :             }
    1205             :         }
    1206             :         else
    1207             :         {
    1208           0 :             ss << "    arg = ";
    1209           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1210           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    1211             :         }
    1212             :     }
    1213           0 :     ss << "    if (fCount == 0.0)\n";
    1214           0 :     ss << "        return DBL_MAX;\n";
    1215           0 :     ss << "    else\n";
    1216           0 :     ss << "        return vSum * pow(fCount,-1.0);\n";
    1217           0 :     ss << "}\n";
    1218           0 : }
    1219             : 
    1220           0 : void OpTDist::BinInlineFun(std::set<std::string>& decls,
    1221             :     std::set<std::string>& funs)
    1222             : {
    1223           0 :     decls.insert(fMachEpsDecl);
    1224           0 :     funs.insert("");
    1225           0 :     decls.insert(fMaxGammaArgumentDecl);
    1226           0 :     funs.insert("");
    1227           0 :     decls.insert(lcl_getLanczosSumDecl);
    1228           0 :     funs.insert(lcl_getLanczosSum);
    1229           0 :     decls.insert(GetBetaDecl);
    1230           0 :     funs.insert(GetBeta);
    1231           0 :     decls.insert(GetLogBetaDecl);
    1232           0 :     funs.insert(GetLogBeta);
    1233           0 :     decls.insert(GetBetaDistPDFDecl);
    1234           0 :     funs.insert(GetBetaDistPDF);
    1235           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);
    1236           0 :     funs.insert(lcl_GetBetaHelperContFrac);
    1237           0 :     decls.insert(GetBetaDistDecl);
    1238           0 :     funs.insert(GetBetaDist);
    1239           0 :     decls.insert(GetTDistDecl);
    1240           0 :     funs.insert(GetTDist);
    1241           0 : }
    1242           0 : void OpTDist::GenSlidingWindowFunction(std::stringstream &ss,
    1243             :             const std::string &sSymName, SubArguments &vSubArguments)
    1244             : {
    1245           0 :     ss << "\ndouble " << sSymName;
    1246           0 :     ss << "_" << BinFuncName() << "(";
    1247           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1248             :     {
    1249           0 :         if (i)
    1250           0 :             ss << ",";
    1251           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1252             :     }
    1253           0 :     ss << ") {\n";
    1254           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1255           0 :     ss << "    double x = 0.0;\n";
    1256           0 :     ss << "    double fDF = 0.0;\n";
    1257           0 :     ss << "    double fFlag = 0.0;\n";
    1258           0 :     if(vSubArguments.size() != 3)
    1259             :     {
    1260           0 :         ss << "    return DBL_MAX;\n}\n";
    1261           0 :         return ;
    1262             :     }
    1263           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    1264             :     assert(tmpCur0);
    1265           0 :     if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
    1266             :     {
    1267           0 :         if(tmpCur0->GetType() == formula::svSingleVectorRef)
    1268             :         {
    1269             :             const formula::SingleVectorRefToken*tmpCurDVR0 =
    1270           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
    1271             : #ifdef ISNAN
    1272           0 :             ss << "    if(gid0 < ";
    1273           0 :             ss << tmpCurDVR0->GetArrayLength() << ")\n";
    1274           0 :             ss << "    {\n";
    1275             : #endif
    1276           0 :             ss << "        x = ";
    1277           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1278             : #ifdef ISNAN
    1279           0 :             ss << "        if(isNan(x))\n";
    1280           0 :             ss << "            x = 0.0;\n";
    1281           0 :             ss << "    }\n";
    1282             : #endif
    1283             :         }
    1284           0 :         else if(tmpCur0->GetType() == formula::svDouble)
    1285             :         {
    1286           0 :             ss << "    x = " << tmpCur0->GetDouble() << ";\n";
    1287             :         }
    1288             :         else
    1289             :         {
    1290           0 :             ss << "    return DBL_MAX;\n}\n";
    1291           0 :             return ;
    1292             :         }
    1293             :     }
    1294             :     else
    1295             :     {
    1296           0 :         ss << "    x = ";
    1297           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1298             :     }
    1299           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    1300             :     assert(tmpCur1);
    1301           0 :     if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
    1302             :     {
    1303           0 :         if(tmpCur1->GetType() == formula::svSingleVectorRef)
    1304             :         {
    1305             :             const formula::SingleVectorRefToken*tmpCurDVR1 =
    1306           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
    1307             : #ifdef ISNAN
    1308           0 :             ss << "    if(gid0 < ";
    1309           0 :             ss << tmpCurDVR1->GetArrayLength() << ")\n";
    1310           0 :             ss << "    {\n";
    1311             : #endif
    1312           0 :             ss << "        fDF = ";
    1313           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    1314             : #ifdef ISNAN
    1315           0 :             ss << "        if(isNan(fDF))\n";
    1316           0 :             ss << "            fDF = 0.0;\n";
    1317           0 :             ss << "        else\n";
    1318             : #endif
    1319           0 :             ss << "            fDF = floor(fDF);\n";
    1320             : #ifdef ISNAN
    1321           0 :             ss << "    }\n";
    1322             : #endif
    1323             :         }
    1324           0 :         else if(tmpCur1->GetType() == formula::svDouble)
    1325             :         {
    1326           0 :             ss << "    fDF = floor(convert_double(";
    1327           0 :             ss << tmpCur1->GetDouble() << "));\n";
    1328             :         }
    1329             :         else
    1330             :         {
    1331           0 :             ss << "    return DBL_MAX;\n}\n";
    1332           0 :             return ;
    1333             :         }
    1334             :     }
    1335             :     else
    1336             :     {
    1337           0 :         ss << "    fDF = floor(";
    1338           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
    1339             :     }
    1340             : 
    1341           0 :     FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
    1342             :     assert(tmpCur2);
    1343           0 :     if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
    1344             :     {
    1345           0 :         if(tmpCur2->GetType() == formula::svSingleVectorRef)
    1346             :         {
    1347             :             const formula::SingleVectorRefToken*tmpCurDVR2 =
    1348           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
    1349             : #ifdef ISNAN
    1350           0 :             ss << "    if(gid0 < ";
    1351           0 :             ss << tmpCurDVR2->GetArrayLength() << ")\n";
    1352           0 :             ss << "    {\n";
    1353             : #endif
    1354           0 :             ss << "        fFlag = ";
    1355           0 :             ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
    1356             : #ifdef ISNAN
    1357           0 :             ss << "        if(isNan(fFlag))\n";
    1358           0 :             ss << "            fFlag = 0.0;\n";
    1359           0 :             ss << "        else\n";
    1360             : #endif
    1361           0 :             ss << "            fFlag = floor(fFlag);\n";
    1362             : #ifdef ISNAN
    1363           0 :             ss << "    }\n";
    1364             : #endif
    1365             : 
    1366             :         }
    1367           0 :         else if(tmpCur2->GetType() == formula::svDouble)
    1368             :         {
    1369           0 :             ss << "    fFlag = floor(convert_double(";
    1370           0 :             ss << tmpCur2->GetDouble() << "));\n";
    1371             :         }
    1372             :         else
    1373             :         {
    1374           0 :             ss << "    return DBL_MAX;\n}\n";
    1375           0 :             return ;
    1376             :         }
    1377             :     }
    1378             :     else
    1379             :     {
    1380           0 :         ss << "    fFlag = floor(";
    1381           0 :         ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ");\n";
    1382             :     }
    1383           0 :     ss << "    if(fDF < 1.0 || x < 0.0 || (fFlag != 1.0 && fFlag != 2.0))\n";
    1384           0 :     ss << "        return DBL_MAX;\n";
    1385           0 :     ss << "    double R = GetTDist(x, fDF);\n";
    1386           0 :     ss << "    if (fFlag == 1.0)\n";
    1387           0 :     ss << "        return R;\n";
    1388           0 :     ss << "    else\n";
    1389           0 :     ss << "        return 2.0 * R;\n";
    1390           0 :     ss << "}\n";
    1391             : }
    1392           0 : void OpExponDist::GenSlidingWindowFunction(std::stringstream &ss,
    1393             :         const std::string &sSymName, SubArguments &vSubArguments)
    1394             : {
    1395           0 :     ss << "\ndouble " << sSymName;
    1396           0 :     ss << "_"<< BinFuncName() <<"(";
    1397           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1398             :     {
    1399           0 :         if (i)
    1400           0 :             ss << ",";
    1401           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1402             :     }
    1403           0 :     ss << ") {\n";
    1404           0 :     ss << "    double tmp = 0,tmp0=0,tmp1=0,tmp2=0;\n";
    1405           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1406           0 :     ss << "    double rx,rlambda,rkum;\n";
    1407           0 :     size_t i = vSubArguments.size();
    1408           0 :     ss <<"\n";
    1409           0 :     for (i = 0; i < vSubArguments.size(); i++)
    1410             :     {
    1411           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    1412             :         assert(pCur);
    1413           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    1414             :         {
    1415             : #ifdef  ISNAN
    1416             :             const formula::SingleVectorRefToken* pSVR =
    1417           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    1418           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    1419             : #endif
    1420             :         }
    1421           0 :         else if (pCur->GetType() == formula::svDouble)
    1422             :         {
    1423             : #ifdef  ISNAN
    1424           0 :             ss << "{\n";
    1425             : #endif
    1426             :         }
    1427             :         else
    1428             :         {
    1429             : #ifdef  ISNAN
    1430             : #endif
    1431             :         }
    1432             : #ifdef  ISNAN
    1433           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    1434             :         {
    1435           0 :             ss << "    if (isNan(";
    1436           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1437           0 :             ss << "))\n";
    1438           0 :             ss << "        tmp"<<i<<"= 0;\n";
    1439           0 :             ss << "    else\n";
    1440           0 :             ss << "        tmp"<<i<<"=\n";
    1441           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1442           0 :             ss << ";\n}\n";
    1443             :         }
    1444             :         else
    1445             :         {
    1446           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    1447           0 :             ss <<";\n";
    1448             :         }
    1449             : #endif
    1450             :     }
    1451           0 :     ss << "   rx = tmp0;\n";
    1452           0 :     ss << "   rlambda = tmp1;\n";
    1453           0 :     ss << "   rkum = tmp2;\n";
    1454           0 :     ss <<"    if(rlambda <= 0.0)\n";
    1455           0 :     ss <<"    {\n";
    1456           0 :     ss <<"        tmp = -DBL_MAX;\n";
    1457           0 :     ss <<"    }\n";
    1458           0 :     ss <<"    else if(rkum == 0)\n";
    1459           0 :     ss <<"    {\n";
    1460           0 :     ss <<"        if(rx >= 0)\n";
    1461           0 :     ss <<"            tmp = rlambda*exp(-rlambda*rx);\n";
    1462           0 :     ss <<"        else\n";
    1463           0 :     ss <<"            tmp = 0.0;\n";
    1464           0 :     ss <<"    }\n";
    1465           0 :     ss <<"    else\n";
    1466           0 :     ss <<"    {\n";
    1467           0 :     ss <<"        if(rx > 0)\n";
    1468           0 :     ss <<"            tmp = 1.0 - exp(-rlambda*rx);\n";
    1469           0 :     ss <<"        else\n";
    1470           0 :     ss <<"            tmp = 0.0;\n";
    1471           0 :     ss <<"    }\n";
    1472           0 :     ss <<"    return tmp;\n";
    1473           0 :     ss <<"}";
    1474           0 : }
    1475           0 : void OpFdist::BinInlineFun(std::set<std::string>& decls,
    1476             :     std::set<std::string>& funs)
    1477             : {
    1478           0 :     decls.insert(GetFDistDecl);decls.insert(GetBetaDistDecl);
    1479           0 :     decls.insert(GetBetaDecl);decls.insert(fMaxGammaArgumentDecl);
    1480           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);
    1481           0 :     decls.insert(GetBetaDistPDFDecl);
    1482           0 :     decls.insert(GetLogBetaDecl);decls.insert(lcl_getLanczosSumDecl);
    1483           0 :     decls.insert(fMachEpsDecl);
    1484           0 :     funs.insert(GetFDist);funs.insert(GetBetaDist);
    1485           0 :     funs.insert(GetBeta);
    1486           0 :     funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetBetaDistPDF);
    1487           0 :     funs.insert(GetLogBeta);
    1488           0 :     funs.insert(lcl_getLanczosSum);
    1489           0 : }
    1490           0 : void OpFdist::GenSlidingWindowFunction(std::stringstream &ss,
    1491             :         const std::string &sSymName, SubArguments &vSubArguments)
    1492             : {
    1493           0 :     ss << "\ndouble " << sSymName;
    1494           0 :     ss << "_"<< BinFuncName() <<"(";
    1495           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1496             :     {
    1497           0 :         if (i)
    1498           0 :             ss << ",";
    1499           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1500             :     }
    1501           0 :     ss << ") {\n";
    1502           0 :     ss << "    double tmp = 0,tmp0=0,tmp1=0,tmp2=0;\n";
    1503           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1504           0 :     ss << "    double rF1,rF2,rX;\n";
    1505           0 :     size_t i = vSubArguments.size();
    1506           0 :     ss <<"\n";
    1507           0 :     for (i = 0; i < vSubArguments.size(); i++)
    1508             :     {
    1509           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    1510             :         assert(pCur);
    1511           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    1512             :         {
    1513             : #ifdef  ISNAN
    1514             :             const formula::SingleVectorRefToken* pSVR =
    1515           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    1516           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    1517             : #endif
    1518             :         }
    1519           0 :         else if (pCur->GetType() == formula::svDouble)
    1520             :         {
    1521             : #ifdef  ISNAN
    1522           0 :             ss << "{\n";
    1523             : #endif
    1524             :         }
    1525             :         else
    1526             :         {
    1527             : #ifdef  ISNAN
    1528             : #endif
    1529             :         }
    1530             : #ifdef  ISNAN
    1531           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    1532             :         {
    1533           0 :             ss << "    if (isNan(";
    1534           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1535           0 :             ss << "))\n";
    1536           0 :             ss << "        tmp"<<i<<"= 0;\n";
    1537           0 :             ss << "    else\n";
    1538           0 :             ss << "        tmp"<<i<<"=\n";
    1539           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1540           0 :             ss << ";\n}\n";
    1541             :         }
    1542             :         else
    1543             :         {
    1544           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    1545           0 :             ss <<";\n";
    1546             :         }
    1547             : #endif
    1548             :     }
    1549           0 :     ss << " rX  = tmp0;\n";
    1550           0 :     ss << " rF1 = floor(tmp1);\n";
    1551           0 :     ss << " rF2 = floor(tmp2);\n";
    1552           0 :     ss <<"    if (rX < 0.0 || rF1 < 1.0 || rF2 < 1.0 || rF1 >= 1.0E10 ||";
    1553           0 :     ss <<"rF2 >= 1.0E10)\n";
    1554           0 :     ss <<"    {\n";
    1555           0 :     ss <<"        tmp = -DBL_MAX;\n";
    1556           0 :     ss <<"    }\n";
    1557           0 :     ss <<"    tmp = GetFDist(rX, rF1, rF2);\n";
    1558           0 :     ss <<"    return tmp;\n";
    1559           0 :     ss <<"}";
    1560           0 : }
    1561             : 
    1562           0 : void OpStandard::GenSlidingWindowFunction(std::stringstream &ss,
    1563             :             const std::string &sSymName, SubArguments &vSubArguments)
    1564             : {
    1565           0 :     ss << "\ndouble " << sSymName;
    1566           0 :     ss << "_" << BinFuncName() << "(";
    1567           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1568             :     {
    1569           0 :         if (i)
    1570           0 :             ss << ",";
    1571           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1572             :     }
    1573           0 :     ss << ") {\n";
    1574           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1575           0 :     ss << "    double x = 0.0;\n";
    1576           0 :     ss << "    double mu = 0.0;\n";
    1577           0 :     ss << "    double sigma = 0.0;\n";
    1578           0 :     if(vSubArguments.size() != 3)
    1579             :     {
    1580           0 :         ss << "    return DBL_MAX;\n}\n";
    1581           0 :         return ;
    1582             :     }
    1583           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    1584             :     assert(tmpCur0);
    1585           0 :     if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
    1586             :     {
    1587           0 :         if(tmpCur0->GetType() == formula::svSingleVectorRef)
    1588             :         {
    1589             :             const formula::SingleVectorRefToken*tmpCurSVR0 =
    1590           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
    1591             : #ifdef  ISNAN
    1592           0 :             ss << "    if (gid0 < " << tmpCurSVR0->GetArrayLength() << ")\n";
    1593           0 :             ss << "    {\n";
    1594             : #endif
    1595           0 :             ss << "        x = ";
    1596           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1597             : #ifdef  ISNAN
    1598           0 :             ss << "        if (isNan(x))\n";
    1599           0 :             ss << "            x = 0.0;\n";
    1600           0 :             ss << "    }\n";
    1601             : #endif
    1602             :         }
    1603           0 :         else if(tmpCur0->GetType() == formula::svDouble)
    1604             :         {
    1605           0 :             ss << "    x = " << tmpCur0->GetDouble() << ";\n";
    1606             :         }
    1607             :         else
    1608             :         {
    1609           0 :             ss << "    return DBL_MAX;\n}\n";
    1610           0 :             return ;
    1611             :         }
    1612             :     }
    1613             :     else
    1614             :     {
    1615           0 :         ss << "    x = ";
    1616           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1617             :     }
    1618             : 
    1619           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    1620             :     assert(tmpCur1);
    1621           0 :     if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
    1622             :     {
    1623           0 :         if(tmpCur1->GetType() == formula::svSingleVectorRef)
    1624             :         {
    1625             :             const formula::SingleVectorRefToken*tmpCurSVR1 =
    1626           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
    1627             : #ifdef  ISNAN
    1628           0 :             ss << "    if (gid0 < " << tmpCurSVR1->GetArrayLength() << ")\n";
    1629           0 :             ss << "    {\n";
    1630             : #endif
    1631           0 :             ss << "        mu = ";
    1632           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    1633             : #ifdef  ISNAN
    1634           0 :             ss << "        if (isNan(mu))\n";
    1635           0 :             ss << "            mu = 0.0;\n";
    1636           0 :             ss << "    }\n";
    1637             : #endif
    1638             :         }
    1639           0 :         else if(tmpCur1->GetType() == formula::svDouble)
    1640             :         {
    1641           0 :             ss << "    mu = " << tmpCur1->GetDouble() << ";\n";
    1642             :         }
    1643             :         else
    1644             :         {
    1645           0 :             ss << "    return DBL_MAX;\n}\n";
    1646           0 :             return ;
    1647             :         }
    1648             :     }
    1649             :     else
    1650             :     {
    1651           0 :         ss << "    mu = ";
    1652           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    1653             :     }
    1654             : 
    1655           0 :     FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
    1656             :     assert(tmpCur2);
    1657           0 :     if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
    1658             :     {
    1659           0 :         if(tmpCur2->GetType() == formula::svSingleVectorRef)
    1660             :         {
    1661             :             const formula::SingleVectorRefToken*tmpCurSVR2 =
    1662           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
    1663             : #ifdef  ISNAN
    1664           0 :             ss << "    if (gid0 < " << tmpCurSVR2->GetArrayLength() << ")\n";
    1665           0 :             ss << "    {\n";
    1666             : #endif
    1667           0 :             ss << "        sigma = ";
    1668           0 :             ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
    1669             : #ifdef  ISNAN
    1670           0 :             ss << "        if (isNan(sigma))\n";
    1671           0 :             ss << "            sigma = 0.0;\n";
    1672           0 :             ss << "    }\n";
    1673             : #endif
    1674             :         }
    1675           0 :         else if(tmpCur2->GetType() == formula::svDouble)
    1676             :         {
    1677           0 :             ss << "    sigma = " << tmpCur2->GetDouble() << ";\n";
    1678             :         }
    1679             :         else
    1680             :         {
    1681           0 :             ss << "    return DBL_MAX;\n";
    1682           0 :             ss << "}\n";
    1683           0 :             return ;
    1684             :         }
    1685             :     }
    1686             :     else
    1687             :     {
    1688           0 :         ss << "    sigma = ";
    1689           0 :         ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
    1690             :     }
    1691             : 
    1692           0 :     ss << "    if(sigma <= 0.0)\n";
    1693           0 :     ss << "        return DBL_MAX;\n";
    1694           0 :     ss << "    else\n";
    1695           0 :     ss << "        return (x - mu)*pow(sigma,-1.0);\n";
    1696           0 :     ss << "}";
    1697             : }
    1698             : 
    1699           0 : void OpWeibull::GenSlidingWindowFunction(std::stringstream &ss,
    1700             :             const std::string &sSymName, SubArguments &vSubArguments)
    1701             : {
    1702           0 :     ss << "\ndouble " << sSymName;
    1703           0 :     ss << "_" << BinFuncName() << "(";
    1704           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1705             :     {
    1706           0 :         if (i)
    1707           0 :             ss << ",";
    1708           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1709             :     }
    1710           0 :     ss << ") {\n";
    1711           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1712           0 :     ss << "    double x = 0.0;\n";
    1713           0 :     ss << "    double alpha = 0.0;\n";
    1714           0 :     ss << "    double beta = 0.0;\n";
    1715           0 :     ss << "    double kum = 0.0;\n";
    1716           0 :     if(vSubArguments.size() != 4)
    1717             :     {
    1718           0 :         ss << "    return DBL_MAX;\n}\n";
    1719           0 :         return ;
    1720             :     }
    1721           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    1722             :     assert(tmpCur0);
    1723           0 :     if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
    1724             :     {
    1725           0 :         if(tmpCur0->GetType() == formula::svSingleVectorRef)
    1726             :         {
    1727             :             const formula::SingleVectorRefToken*tmpCurSVR0 =
    1728           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
    1729             : #ifdef  ISNAN
    1730           0 :             ss << "    if (gid0 < " << tmpCurSVR0->GetArrayLength() << ")\n";
    1731           0 :             ss << "    {\n";
    1732             : #endif
    1733           0 :             ss << "        x = ";
    1734           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1735             : #ifdef  ISNAN
    1736           0 :             ss << "        if (isNan(x))\n";
    1737           0 :             ss << "            x = 0.0;\n";
    1738           0 :             ss << "    }\n";
    1739             : #endif
    1740             :         }
    1741           0 :         else if(tmpCur0->GetType() == formula::svDouble)
    1742             :         {
    1743           0 :             ss << "    x = " << tmpCur0->GetDouble() << ";\n";
    1744             :         }
    1745             :         else
    1746             :         {
    1747           0 :             ss << "    return DBL_MAX;\n}\n";
    1748           0 :             return ;
    1749             :         }
    1750             :     }
    1751             :     else
    1752             :     {
    1753           0 :         ss << "    x = ";
    1754           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1755             :     }
    1756             : 
    1757           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    1758             :     assert(tmpCur1);
    1759           0 :     if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
    1760             :     {
    1761           0 :         if(tmpCur1->GetType() == formula::svSingleVectorRef)
    1762             :         {
    1763             :             const formula::SingleVectorRefToken*tmpCurSVR1 =
    1764           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
    1765             : #ifdef  ISNAN
    1766           0 :             ss << "    if (gid0 < " << tmpCurSVR1->GetArrayLength() << ")\n";
    1767           0 :             ss << "    {\n";
    1768             : #endif
    1769           0 :             ss << "        alpha = ";
    1770           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    1771             : #ifdef  ISNAN
    1772           0 :             ss << "        if (isNan(alpha))\n";
    1773           0 :             ss << "            alpha = 0.0;\n";
    1774           0 :             ss << "    }\n";
    1775             : #endif
    1776             :         }
    1777           0 :         else if(tmpCur1->GetType() == formula::svDouble)
    1778             :         {
    1779           0 :             ss << "    alpha = " << tmpCur1->GetDouble() << ";\n";
    1780             :         }
    1781             :         else
    1782             :         {
    1783           0 :             ss << "    return DBL_MAX;\n}\n";
    1784           0 :             return ;
    1785             :         }
    1786             :     }
    1787             :     else
    1788             :     {
    1789           0 :         ss << "    alpha = ";
    1790           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    1791             :     }
    1792             : 
    1793           0 :     FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
    1794             :     assert(tmpCur2);
    1795           0 :     if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
    1796             :     {
    1797           0 :         if(tmpCur2->GetType() == formula::svSingleVectorRef)
    1798             :         {
    1799             :             const formula::SingleVectorRefToken*tmpCurSVR2 =
    1800           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
    1801             : #ifdef  ISNAN
    1802           0 :             ss << "    if (gid0 < " << tmpCurSVR2->GetArrayLength() << ")\n";
    1803           0 :             ss << "    {\n";
    1804             : #endif
    1805           0 :             ss << "        beta = ";
    1806           0 :             ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
    1807             : #ifdef  ISNAN
    1808           0 :             ss << "        if (isNan(beta))\n";
    1809           0 :             ss << "            beta = 0.0;\n";
    1810           0 :             ss << "    }\n";
    1811             : #endif
    1812             :         }
    1813           0 :         else if(tmpCur2->GetType() == formula::svDouble)
    1814             :         {
    1815           0 :             ss << "    beta = " << tmpCur2->GetDouble() << ";\n";
    1816             :         }
    1817             :         else
    1818             :         {
    1819           0 :             ss << "    return DBL_MAX;\n}\n";
    1820           0 :             return ;
    1821             :         }
    1822             :     }
    1823             :     else
    1824             :     {
    1825           0 :         ss << "    beta = ";
    1826           0 :         ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
    1827             :     }
    1828             : 
    1829           0 :     FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
    1830             :     assert(tmpCur3);
    1831           0 :     if(ocPush == vSubArguments[3]->GetFormulaToken()->GetOpCode())
    1832             :     {
    1833           0 :         if(tmpCur3->GetType() == formula::svSingleVectorRef)
    1834             :         {
    1835             :             const formula::SingleVectorRefToken*tmpCurSVR3 =
    1836           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur3);
    1837             : #ifdef  ISNAN
    1838           0 :             ss << "    if (gid0 < " << tmpCurSVR3->GetArrayLength() << ")\n";
    1839           0 :             ss << "    {\n";
    1840             : #endif
    1841           0 :             ss << "        kum = ";
    1842           0 :             ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ";\n";
    1843             : #ifdef  ISNAN
    1844           0 :             ss << "        if (isNan(kum))\n";
    1845           0 :             ss << "            kum = 0.0;\n";
    1846           0 :             ss << "    }\n";
    1847             : #endif
    1848             :         }
    1849           0 :         else if(tmpCur3->GetType() == formula::svDouble)
    1850             :         {
    1851           0 :             ss << "    kum = " << tmpCur3->GetDouble() << ";\n";
    1852             :         }
    1853             :         else
    1854             :         {
    1855           0 :             ss << "    return DBL_MAX;\n}\n";
    1856           0 :             return ;
    1857             :         }
    1858             :     }
    1859             :     else
    1860             :     {
    1861           0 :         ss << "    kum = ";
    1862           0 :         ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ";\n";
    1863             :     }
    1864             : 
    1865           0 :     ss << "    if(alpha <= 0.0 || beta <=0.0 || kum < 0.0)\n";
    1866           0 :     ss << "        return DBL_MAX;\n";
    1867           0 :     ss << "    else if(kum == 0.0)\n";
    1868           0 :     ss << "    {\n";
    1869           0 :     ss << "        return alpha*pow(pow(beta,alpha),-1.0)*pow(x,alpha-1.0)";
    1870           0 :     ss << "*exp(-pow(x*pow(beta,-1.0),alpha));\n";
    1871           0 :     ss << "    }\n";
    1872           0 :     ss << "    else\n";
    1873           0 :     ss << "        return 1.0-exp(-pow(x*pow(beta,-1.0),alpha));\n";
    1874           0 :     ss << "}\n";
    1875             : }
    1876             : 
    1877           0 : void OpSkew::GenSlidingWindowFunction(std::stringstream &ss,
    1878             :             const std::string &sSymName, SubArguments &vSubArguments)
    1879             : {
    1880           0 :     ss << "\ndouble " << sSymName;
    1881           0 :     ss << "_" << BinFuncName() << "(";
    1882           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1883             :     {
    1884           0 :         if (i)
    1885           0 :             ss << ",";
    1886           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1887             :     }
    1888           0 :     ss << "){\n";
    1889           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1890           0 :     ss << "    double fSum = 0.0;\n";
    1891           0 :     ss << "    double fMean = 0.0;\n";
    1892           0 :     ss << "    double vSum = 0.0;\n";
    1893           0 :     ss << "    double fCount = 0.0;\n";
    1894           0 :     ss << "    double arg = 0.0;\n";
    1895           0 :     unsigned i = vSubArguments.size();
    1896           0 :     while (i--)
    1897             :     {
    1898           0 :         FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
    1899             :         assert(pCur);
    1900           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    1901             :         {
    1902           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    1903             :             {
    1904             :                 const formula::DoubleVectorRefToken* pDVR =
    1905           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    1906           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    1907           0 :                 ss << "    for (int i = ";
    1908           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    1909             :                 {
    1910             : #ifdef  ISNAN
    1911           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    1912           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    1913           0 :                     ss << "    {\n";
    1914             : #else
    1915             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    1916             :                     ss << "    {\n";
    1917             : #endif
    1918             :                 }
    1919           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1920             :                 {
    1921             : #ifdef  ISNAN
    1922           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    1923           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    1924           0 :                     ss << "    {\n";
    1925             : #else
    1926             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    1927             :                     ss << "    {\n";
    1928             : #endif
    1929             :                 }
    1930           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1931             :                 {
    1932             : #ifdef  ISNAN
    1933           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    1934           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    1935           0 :                     ss << "    {\n";
    1936             : #else
    1937             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    1938             :                     ss << "    {\n";
    1939             : #endif
    1940             :                 }
    1941             :                 else
    1942             :                 {
    1943             : #ifdef  ISNAN
    1944           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1945           0 :                     ss << "    {\n";
    1946             : #else
    1947             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1948             :                     ss << "    {\n";
    1949             : #endif
    1950             :                 }
    1951             : 
    1952           0 :                 ss << "        arg = ";
    1953           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1954             : #ifdef  ISNAN
    1955           0 :                 ss << "        if (isNan(arg))\n";
    1956           0 :                 ss << "            continue;\n";
    1957             : #endif
    1958           0 :                 ss << "        fSum += arg;\n";
    1959           0 :                 ss << "        fCount += 1.0;\n";
    1960           0 :                 ss << "    }\n";
    1961             :             }
    1962           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    1963             :             {
    1964             :                 const formula::SingleVectorRefToken* pSVR =
    1965           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    1966             : #ifdef  ISNAN
    1967           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    1968           0 :                 ss << "    {\n";
    1969             : #endif
    1970           0 :                 ss << "        arg = ";
    1971           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1972             : #ifdef  ISNAN
    1973           0 :                 ss << "        if (!isNan(arg))\n";
    1974           0 :                 ss << "        {\n";
    1975             : #endif
    1976           0 :                 ss << "            fSum += arg;\n";
    1977           0 :                 ss << "            fCount += 1.0;\n";
    1978             : #ifdef ISNAN
    1979           0 :                 ss << "        }\n";
    1980           0 :                 ss << "    }\n";
    1981             : #endif
    1982             :             }
    1983             :             else
    1984             :             {
    1985           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    1986           0 :                 ss << "    fSum += arg;\n";
    1987           0 :                 ss << "    fCount += 1.0;\n";
    1988             :             }
    1989             :         }
    1990             :         else
    1991             :         {
    1992           0 :             ss << "    arg = ";
    1993           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1994           0 :             ss << "    fSum += arg;\n";
    1995           0 :             ss << "    fCount += 1.0;\n";
    1996             :         }
    1997             : 
    1998           0 :         if(i == 0)
    1999             :         {
    2000           0 :             ss << "    if(fCount <= 2.0)\n";
    2001           0 :             ss << "        return DBL_MAX;\n";
    2002           0 :             ss << "    else\n";
    2003           0 :             ss << "        fMean = fSum * pow(fCount,-1.0);\n";
    2004             :         }
    2005             :     }
    2006           0 :     i = vSubArguments.size();
    2007           0 :     while (i--)
    2008             :     {
    2009           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2010             :         assert(pCur);
    2011           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2012             :         {
    2013           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2014             :             {
    2015             :                 const formula::DoubleVectorRefToken* pDVR =
    2016           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2017           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2018           0 :                 ss << "    for (int i = ";
    2019           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2020             :                 {
    2021             : #ifdef  ISNAN
    2022           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2023           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2024           0 :                     ss << "    {\n";
    2025             : #else
    2026             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2027             :                     ss << "    {\n";
    2028             : #endif
    2029             :                 }
    2030           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2031             :                 {
    2032             : #ifdef  ISNAN
    2033           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2034           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2035           0 :                     ss << "    {\n";
    2036             : #else
    2037             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2038             :                     ss << "    {\n";
    2039             : #endif
    2040             :                 }
    2041           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2042             :                 {
    2043             : #ifdef  ISNAN
    2044           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2045           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2046           0 :                     ss << "    {\n";
    2047             : #else
    2048             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2049             :                     ss << "    {\n";
    2050             : #endif
    2051             :                 }
    2052             :                 else
    2053             :                 {
    2054             : #ifdef  ISNAN
    2055           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2056           0 :                     ss << "    {\n";
    2057             : #else
    2058             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2059             :                     ss << "    {\n";
    2060             : #endif
    2061             :                 }
    2062             : 
    2063           0 :                 ss << "        arg = ";
    2064           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2065             : #ifdef  ISNAN
    2066           0 :                 ss << "        if (isNan(arg))\n";
    2067           0 :                 ss << "            continue;\n";
    2068             : #endif
    2069           0 :                 ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
    2070           0 :                 ss << "    }\n";
    2071             :             }
    2072           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2073             :             {
    2074             :                 const formula::SingleVectorRefToken* pSVR =
    2075           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2076             : #ifdef  ISNAN
    2077           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2078           0 :                 ss << "    {\n";
    2079             : #endif
    2080           0 :                 ss << "        arg = ";
    2081           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2082             : #ifdef  ISNAN
    2083           0 :                 ss << "        if (!isNan(arg))\n";
    2084           0 :                 ss << "        {\n";
    2085             : #endif
    2086           0 :                 ss << "            vSum += (arg - fMean) * (arg - fMean);\n";
    2087             : #ifdef ISNAN
    2088           0 :                 ss << "        }\n";
    2089           0 :                 ss << "    }\n";
    2090             : #endif
    2091             :             }
    2092             :             else
    2093             :             {
    2094           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2095           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2096             :             }
    2097             :         }
    2098             :         else
    2099             :         {
    2100           0 :             ss << "    arg = ";
    2101           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2102           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2103             :         }
    2104             :     }
    2105           0 :     ss << "    double fStdDev = sqrt(vSum * pow(fCount - 1.0,-1.0));\n";
    2106           0 :     ss << "    double dx = 0.0;\n";
    2107           0 :     ss << "    double xcube = 0.0;\n";
    2108           0 :     ss << "    if(fStdDev == 0.0)\n";
    2109           0 :     ss << "        return DBL_MAX;\n";
    2110           0 :     i = vSubArguments.size();
    2111           0 :     while (i--)
    2112             :     {
    2113           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2114             :         assert(pCur);
    2115           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2116             :         {
    2117           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2118             :             {
    2119             :                 const formula::DoubleVectorRefToken* pDVR =
    2120           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2121           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2122           0 :                 ss << "    for (int i = ";
    2123           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2124             :                 {
    2125             : #ifdef  ISNAN
    2126           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2127           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2128           0 :                     ss << "    {\n";
    2129             : #else
    2130             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2131             :                     ss << "    {\n";
    2132             : #endif
    2133             :                 }
    2134           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2135             :                 {
    2136             : #ifdef  ISNAN
    2137           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2138           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2139           0 :                     ss << "    {\n";
    2140             : #else
    2141             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2142             :                     ss << "    {\n";
    2143             : #endif
    2144             :                 }
    2145           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2146             :                 {
    2147             : #ifdef  ISNAN
    2148           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2149           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2150           0 :                     ss << "    {\n";
    2151             : #else
    2152             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2153             :                     ss << "    {\n";
    2154             : #endif
    2155             :                 }
    2156             :                 else
    2157             :                 {
    2158             : #ifdef  ISNAN
    2159           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2160           0 :                     ss << "    {\n";
    2161             : #else
    2162             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2163             :                     ss << "    {\n";
    2164             : #endif
    2165             :                 }
    2166             : 
    2167           0 :                 ss << "        arg = ";
    2168           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2169             : #ifdef  ISNAN
    2170           0 :                 ss << "        if (isNan(arg))\n";
    2171           0 :                 ss << "            continue;\n";
    2172             : #endif
    2173           0 :                 ss << "        dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2174           0 :                 ss << "        xcube = xcube + dx * dx * dx;\n";
    2175           0 :                 ss << "    }\n";
    2176             :             }
    2177           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2178             :             {
    2179             :                 const formula::SingleVectorRefToken* pSVR =
    2180           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2181             : #ifdef  ISNAN
    2182           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2183           0 :                 ss << "    {\n";
    2184             : #endif
    2185           0 :                 ss << "        arg = ";
    2186           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2187             : #ifdef  ISNAN
    2188           0 :                 ss << "        if (!isNan(arg))\n";
    2189           0 :                 ss << "        {\n";
    2190             : #endif
    2191           0 :                 ss << "            dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2192           0 :                 ss << "            xcube = xcube + dx * dx * dx;\n";
    2193             : #ifdef ISNAN
    2194           0 :                 ss << "        }\n";
    2195           0 :                 ss << "    }\n";
    2196             : #endif
    2197             :             }
    2198             :             else
    2199             :             {
    2200           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2201           0 :                 ss << "    dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2202           0 :                 ss << "    xcube = xcube + dx * dx * dx;\n";
    2203             :             }
    2204             :         }
    2205             :         else
    2206             :         {
    2207           0 :             ss << "    arg = ";
    2208           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2209           0 :             ss << "    dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2210           0 :             ss << "    xcube = xcube + dx * dx * dx;\n";
    2211             :         }
    2212             :     }
    2213           0 :     ss << "    return ((xcube * fCount) * pow(fCount - 1.0,-1.0))";
    2214           0 :     ss << " * pow(fCount - 2.0,-1.0);\n";
    2215           0 :     ss << "}\n";
    2216           0 : }
    2217             : 
    2218           0 : void OpSkewp::GenSlidingWindowFunction(std::stringstream &ss,
    2219             :             const std::string &sSymName, SubArguments &vSubArguments)
    2220             : {
    2221           0 :     ss << "\ndouble " << sSymName;
    2222           0 :     ss << "_" << BinFuncName() << "(";
    2223           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    2224             :     {
    2225           0 :         if (i)
    2226           0 :             ss << ",";
    2227           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    2228             :     }
    2229           0 :     ss << "){\n";
    2230           0 :     ss << "    int gid0 = get_global_id(0);\n";
    2231           0 :     ss << "    double fSum = 0.0;\n";
    2232           0 :     ss << "    double fMean = 0.0;\n";
    2233           0 :     ss << "    double vSum = 0.0;\n";
    2234           0 :     ss << "    double fCount = 0.0;\n";
    2235           0 :     ss << "    double arg = 0.0;\n";
    2236           0 :     unsigned i = vSubArguments.size();
    2237           0 :     while (i--)
    2238             :     {
    2239           0 :         FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
    2240             :         assert(pCur);
    2241           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2242             :         {
    2243           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2244             :             {
    2245             :                 const formula::DoubleVectorRefToken* pDVR =
    2246           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2247           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2248           0 :                 ss << "    for (int i = ";
    2249           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2250             :                 {
    2251             : #ifdef  ISNAN
    2252           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2253           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2254           0 :                     ss << "    {\n";
    2255             : #else
    2256             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2257             :                     ss << "    {\n";
    2258             : #endif
    2259             :                 }
    2260           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2261             :                 {
    2262             : #ifdef  ISNAN
    2263           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2264           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2265           0 :                     ss << "    {\n";
    2266             : #else
    2267             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2268             :                     ss << "    {\n";
    2269             : #endif
    2270             :                 }
    2271           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2272             :                 {
    2273             : #ifdef  ISNAN
    2274           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2275           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2276           0 :                     ss << "    {\n";
    2277             : #else
    2278             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2279             :                     ss << "    {\n";
    2280             : #endif
    2281             :                 }
    2282             :                 else
    2283             :                 {
    2284             : #ifdef  ISNAN
    2285           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2286           0 :                     ss << "    {\n";
    2287             : #else
    2288             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2289             :                     ss << "    {\n";
    2290             : #endif
    2291             :                 }
    2292             : 
    2293           0 :                 ss << "        arg = ";
    2294           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2295             : #ifdef  ISNAN
    2296           0 :                 ss << "        if (isNan(arg))\n";
    2297           0 :                 ss << "            continue;\n";
    2298             : #endif
    2299           0 :                 ss << "        fSum += arg;\n";
    2300           0 :                 ss << "        fCount += 1.0;\n";
    2301           0 :                 ss << "    }\n";
    2302             :             }
    2303           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2304             :             {
    2305             :                 const formula::SingleVectorRefToken* pSVR =
    2306           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2307             : #ifdef  ISNAN
    2308           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2309           0 :                 ss << "    {\n";
    2310             : #endif
    2311           0 :                 ss << "        arg = ";
    2312           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2313             : #ifdef  ISNAN
    2314           0 :                 ss << "        if (!isNan(arg))\n";
    2315           0 :                 ss << "        {\n";
    2316             : #endif
    2317           0 :                 ss << "            fSum += arg;\n";
    2318           0 :                 ss << "            fCount += 1.0;\n";
    2319             : #ifdef ISNAN
    2320           0 :                 ss << "        }\n";
    2321           0 :                 ss << "    }\n";
    2322             : #endif
    2323             :             }
    2324             :             else
    2325             :             {
    2326           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2327           0 :                 ss << "    fSum += arg;\n";
    2328           0 :                 ss << "    fCount += 1.0;\n";
    2329             :             }
    2330             :         }
    2331             :         else
    2332             :         {
    2333           0 :             ss << "    arg = ";
    2334           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2335           0 :             ss << "    fSum += arg;\n";
    2336           0 :             ss << "    fCount += 1.0;\n";
    2337             :         }
    2338             : 
    2339           0 :         if(i == 0)
    2340             :         {
    2341           0 :             ss << "    if(fCount <= 2.0)\n";
    2342           0 :             ss << "        return DBL_MAX;\n";
    2343           0 :             ss << "    else\n";
    2344           0 :             ss << "        fMean = fSum * pow(fCount,-1.0);\n";
    2345             :         }
    2346             :     }
    2347           0 :     i = vSubArguments.size();
    2348           0 :     while (i--)
    2349             :     {
    2350           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2351             :         assert(pCur);
    2352           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2353             :         {
    2354           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2355             :             {
    2356             :                 const formula::DoubleVectorRefToken* pDVR =
    2357           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2358           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2359           0 :                 ss << "    for (int i = ";
    2360           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2361             :                 {
    2362             : #ifdef  ISNAN
    2363           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2364           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2365           0 :                     ss << "    {\n";
    2366             : #else
    2367             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2368             :                     ss << "    {\n";
    2369             : #endif
    2370             :                 }
    2371           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2372             :                 {
    2373             : #ifdef  ISNAN
    2374           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2375           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2376           0 :                     ss << "    {\n";
    2377             : #else
    2378             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2379             :                     ss << "    {\n";
    2380             : #endif
    2381             :                 }
    2382           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2383             :                 {
    2384             : #ifdef  ISNAN
    2385           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2386           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2387           0 :                     ss << "    {\n";
    2388             : #else
    2389             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2390             :                     ss << "    {\n";
    2391             : #endif
    2392             :                 }
    2393             :                 else
    2394             :                 {
    2395             : #ifdef  ISNAN
    2396           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2397           0 :                     ss << "    {\n";
    2398             : #else
    2399             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2400             :                     ss << "    {\n";
    2401             : #endif
    2402             :                 }
    2403             : 
    2404           0 :                 ss << "        arg = ";
    2405           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2406             : #ifdef  ISNAN
    2407           0 :                 ss << "        if (isNan(arg))\n";
    2408           0 :                 ss << "            continue;\n";
    2409             : #endif
    2410           0 :                 ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
    2411           0 :                 ss << "    }\n";
    2412             :             }
    2413           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2414             :             {
    2415             :                 const formula::SingleVectorRefToken* pSVR =
    2416           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2417             : #ifdef  ISNAN
    2418           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2419           0 :                 ss << "    {\n";
    2420             : #endif
    2421           0 :                 ss << "        arg = ";
    2422           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2423             : #ifdef  ISNAN
    2424           0 :                 ss << "        if (!isNan(arg))\n";
    2425           0 :                 ss << "        {\n";
    2426             : #endif
    2427           0 :                 ss << "            vSum += (arg - fMean) * (arg - fMean);\n";
    2428             : #ifdef ISNAN
    2429           0 :                 ss << "        }\n";
    2430           0 :                 ss << "    }\n";
    2431             : #endif
    2432             :             }
    2433             :             else
    2434             :             {
    2435           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2436           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2437             :             }
    2438             :         }
    2439             :         else
    2440             :         {
    2441           0 :             ss << "    arg = ";
    2442           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2443           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2444             :         }
    2445             :     }
    2446           0 :     ss << "    double fStdDev = sqrt(vSum * pow(fCount,-1.0));\n";
    2447           0 :     ss << "    double dx = 0.0;\n";
    2448           0 :     ss << "    double xcube = 0.0;\n";
    2449           0 :     ss << "    if(fStdDev == 0.0)\n";
    2450           0 :     ss << "        return DBL_MAX;\n";
    2451           0 :     i = vSubArguments.size();
    2452           0 :     while (i--)
    2453             :     {
    2454           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2455             :         assert(pCur);
    2456           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2457             :         {
    2458           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2459             :             {
    2460             :                 const formula::DoubleVectorRefToken* pDVR =
    2461           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2462           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2463           0 :                 ss << "    for (int i = ";
    2464           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2465             :                 {
    2466             : #ifdef  ISNAN
    2467           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2468           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2469           0 :                     ss << "    {\n";
    2470             : #else
    2471             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2472             :                     ss << "    {\n";
    2473             : #endif
    2474             :                 }
    2475           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2476             :                 {
    2477             : #ifdef  ISNAN
    2478           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2479           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2480           0 :                     ss << "    {\n";
    2481             : #else
    2482             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2483             :                     ss << "    {\n";
    2484             : #endif
    2485             :                 }
    2486           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2487             :                 {
    2488             : #ifdef  ISNAN
    2489           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2490           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2491           0 :                     ss << "    {\n";
    2492             : #else
    2493             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2494             :                     ss << "    {\n";
    2495             : #endif
    2496             :                 }
    2497             :                 else
    2498             :                 {
    2499             : #ifdef  ISNAN
    2500           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2501           0 :                     ss << "    {\n";
    2502             : #else
    2503             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2504             :                     ss << "    {\n";
    2505             : #endif
    2506             :                 }
    2507             : 
    2508           0 :                 ss << "        arg = ";
    2509           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2510             : #ifdef  ISNAN
    2511           0 :                 ss << "        if (isNan(arg))\n";
    2512           0 :                 ss << "            continue;\n";
    2513             : #endif
    2514           0 :                 ss << "        dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2515           0 :                 ss << "        xcube = xcube + dx * dx * dx;\n";
    2516           0 :                 ss << "    }\n";
    2517             :             }
    2518           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2519             :             {
    2520             :                 const formula::SingleVectorRefToken* pSVR =
    2521           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2522             : #ifdef  ISNAN
    2523           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2524           0 :                 ss << "    {\n";
    2525             : #endif
    2526           0 :                 ss << "        arg = ";
    2527           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2528             : #ifdef  ISNAN
    2529           0 :                 ss << "        if (!isNan(arg))\n";
    2530           0 :                 ss << "        {\n";
    2531             : #endif
    2532           0 :                 ss << "            dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2533           0 :                 ss << "            xcube = xcube + dx * dx * dx;\n";
    2534             : #ifdef ISNAN
    2535           0 :                 ss << "        }\n";
    2536           0 :                 ss << "    }\n";
    2537             : #endif
    2538             :             }
    2539             :             else
    2540             :             {
    2541           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2542           0 :                 ss << "    dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2543           0 :                 ss << "    xcube = xcube + dx * dx * dx;\n";
    2544             :             }
    2545             :         }
    2546             :         else
    2547             :         {
    2548           0 :             ss << "    arg = ";
    2549           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2550           0 :             ss << "    dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2551           0 :             ss << "    xcube = xcube + dx * dx * dx;\n";
    2552             :         }
    2553             :     }
    2554           0 :     ss << "    return xcube * pow(fCount,-1.0);\n";
    2555           0 :     ss << "}\n";
    2556           0 : }
    2557             : 
    2558           0 : void OpTInv::BinInlineFun(std::set<std::string>& decls,
    2559             :     std::set<std::string>& funs)
    2560             : {
    2561           0 :     decls.insert(fMachEpsDecl);
    2562           0 :     funs.insert("");
    2563           0 :     decls.insert(fMaxGammaArgumentDecl);
    2564           0 :     funs.insert("");
    2565           0 :     decls.insert(lcl_getLanczosSumDecl);
    2566           0 :     funs.insert(lcl_getLanczosSum);
    2567           0 :     decls.insert(GetBetaDecl);
    2568           0 :     funs.insert(GetBeta);
    2569           0 :     decls.insert(GetLogBetaDecl);
    2570           0 :     funs.insert(GetLogBeta);
    2571           0 :     decls.insert(GetBetaDistPDFDecl);
    2572           0 :     funs.insert(GetBetaDistPDF);
    2573           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);
    2574           0 :     funs.insert(lcl_GetBetaHelperContFrac);
    2575           0 :     decls.insert(GetBetaDistDecl);
    2576           0 :     funs.insert(GetBetaDist);
    2577           0 :     decls.insert(GetTDistDecl);
    2578           0 :     funs.insert(GetTDist);
    2579           0 :     decls.insert(GetValueDecl);
    2580           0 :     funs.insert(GetValue);
    2581           0 :     decls.insert(lcl_HasChangeOfSignDecl);
    2582           0 :     funs.insert(lcl_HasChangeOfSign);
    2583           0 :     decls.insert(lcl_IterateInverseDecl);
    2584           0 :     funs.insert(lcl_IterateInverse);
    2585           0 : }
    2586             : 
    2587           0 : void OpTInv::GenSlidingWindowFunction(std::stringstream &ss,
    2588             :             const std::string &sSymName, SubArguments &vSubArguments)
    2589             : {
    2590           0 :     ss << "\ndouble " << sSymName;
    2591           0 :     ss << "_" << BinFuncName() << "(";
    2592           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    2593             :     {
    2594           0 :         if (i)
    2595           0 :             ss << ",";
    2596           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    2597             :     }
    2598           0 :     ss << ") {\n";
    2599           0 :     ss << "    int gid0 = get_global_id(0);\n";
    2600           0 :     ss << "    double x = 0.0;\n";
    2601           0 :     ss << "    double fDF = 0.0;\n";
    2602           0 :     if(vSubArguments.size() != 2)
    2603             :     {
    2604           0 :         ss << "    return DBL_MAX;\n}\n";
    2605           0 :         return ;
    2606             :     }
    2607           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    2608             :     assert(tmpCur0);
    2609           0 :     if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
    2610             :     {
    2611           0 :         if(tmpCur0->GetType() == formula::svSingleVectorRef)
    2612             :         {
    2613             :             const formula::SingleVectorRefToken*tmpCurDVR0 =
    2614           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
    2615             : #ifdef ISNAN
    2616           0 :             ss << "    if(gid0 < ";
    2617           0 :             ss << tmpCurDVR0->GetArrayLength() << ")\n";
    2618           0 :             ss << "    {\n";
    2619             : #endif
    2620           0 :             ss << "        x = ";
    2621           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    2622             : #ifdef ISNAN
    2623           0 :             ss << "        if(isNan(x))\n";
    2624           0 :             ss << "            x = 0.0;\n";
    2625           0 :             ss << "    }\n";
    2626             : #endif
    2627             :         }
    2628           0 :         else if(tmpCur0->GetType() == formula::svDouble)
    2629             :         {
    2630           0 :             ss << "    x = " << tmpCur0->GetDouble() << ";\n";
    2631             :         }
    2632             :         else
    2633             :         {
    2634           0 :             ss << "    return DBL_MAX;\n}\n";
    2635           0 :             return ;
    2636             :         }
    2637             :     }
    2638             :     else
    2639             :     {
    2640           0 :         ss << "    x = ";
    2641           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    2642             :     }
    2643           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    2644             :     assert(tmpCur1);
    2645           0 :     if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
    2646             :     {
    2647           0 :         if(tmpCur1->GetType() == formula::svSingleVectorRef)
    2648             :         {
    2649             :             const formula::SingleVectorRefToken*tmpCurDVR1 =
    2650           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
    2651             : #ifdef ISNAN
    2652           0 :             ss << "    if(gid0 < ";
    2653           0 :             ss << tmpCurDVR1->GetArrayLength() << ")\n";
    2654           0 :             ss << "    {\n";
    2655             : #endif
    2656           0 :             ss << "        fDF = ";
    2657           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    2658             : #ifdef ISNAN
    2659           0 :             ss << "        if(isNan(fDF))\n";
    2660           0 :             ss << "            fDF = 0.0;\n";
    2661           0 :             ss << "        else\n";
    2662             : #endif
    2663           0 :             ss << "            fDF = floor(fDF);\n";
    2664             : #ifdef ISNAN
    2665           0 :             ss << "    }\n";
    2666             : #endif
    2667             :         }
    2668           0 :         else if(tmpCur1->GetType() == formula::svDouble)
    2669             :         {
    2670           0 :             ss << "    fDF = floor(convert_double(";
    2671           0 :             ss << tmpCur1->GetDouble() << "));\n";
    2672             :         }
    2673             :         else
    2674             :         {
    2675           0 :             ss << "    return DBL_MAX;\n}\n";
    2676           0 :             return ;
    2677             :         }
    2678             :     }
    2679             :     else
    2680             :     {
    2681           0 :         ss << "    fDF = floor(";
    2682           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
    2683             :     }
    2684           0 :     ss << "    if (x > 1.0||fDF < 1.0 || fDF > 1.0E10 || x <= 0.0)\n";
    2685           0 :     ss << "        return DBL_MAX;\n";
    2686           0 :     ss << "    bool bConvError;\n";
    2687           0 :     ss << "    double fVal = lcl_IterateInverse(\n";
    2688           0 :     ss << "        fDF*0.5, fDF, &bConvError,x,fDF );\n";
    2689           0 :     ss << "    if (bConvError)\n";
    2690           0 :     ss << "        return DBL_MAX;\n";
    2691           0 :     ss << "    return fVal;\n";
    2692           0 :     ss << "}\n";
    2693             : }
    2694             : 
    2695           0 : void OpStDev::GenSlidingWindowFunction(std::stringstream &ss,
    2696             :             const std::string &sSymName, SubArguments &vSubArguments)
    2697             : {
    2698           0 :     ss << "\ndouble " << sSymName;
    2699           0 :     ss << "_" << BinFuncName() << "(";
    2700           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    2701             :     {
    2702           0 :         if (i)
    2703           0 :             ss << ",";
    2704           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    2705             :     }
    2706           0 :     ss << "){\n";
    2707           0 :     ss << "    int gid0 = get_global_id(0);\n";
    2708           0 :     ss << "    double fSum = 0.0;\n";
    2709           0 :     ss << "    double vSum = 0.0;\n";
    2710           0 :     ss << "    double fMean = 0.0;\n";
    2711           0 :     ss << "    double fCount = 0.0;\n";
    2712           0 :     ss << "    double arg = 0.0;\n";
    2713           0 :     unsigned i = vSubArguments.size();
    2714           0 :     while (i--)
    2715             :     {
    2716           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2717             :         assert(pCur);
    2718           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2719             :         {
    2720           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2721             :             {
    2722             :                 const formula::DoubleVectorRefToken* pDVR =
    2723           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2724           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2725           0 :                 ss << "    for (int i = ";
    2726           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2727             :                 {
    2728             : #ifdef  ISNAN
    2729           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2730           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2731           0 :                     ss << "    {\n";
    2732             : #else
    2733             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2734             :                     ss << "    {\n";
    2735             : #endif
    2736             :                 }
    2737           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2738             :                 {
    2739             : #ifdef  ISNAN
    2740           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2741           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2742           0 :                     ss << "    {\n";
    2743             : #else
    2744             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2745             :                     ss << "    {\n";
    2746             : #endif
    2747             :                 }
    2748           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2749             :                 {
    2750             : #ifdef  ISNAN
    2751           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2752           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2753           0 :                     ss << "    {\n";
    2754             : #else
    2755             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2756             :                     ss << "    {\n";
    2757             : #endif
    2758             :                 }
    2759             :                 else
    2760             :                 {
    2761             : #ifdef  ISNAN
    2762           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2763           0 :                     ss << "    {\n";
    2764             : #else
    2765             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2766             :                     ss << "    {\n";
    2767             : #endif
    2768             :                 }
    2769             : 
    2770           0 :                 ss << "        arg = ";
    2771           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2772             : #ifdef  ISNAN
    2773           0 :                 ss << "        if (isNan(arg))\n";
    2774           0 :                 ss << "            continue;\n";
    2775             : #endif
    2776           0 :                 ss << "        fSum += arg;\n";
    2777           0 :                 ss << "        fCount += 1.0;\n";
    2778           0 :                 ss << "    }\n";
    2779             :             }
    2780           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2781             :             {
    2782             :                 const formula::SingleVectorRefToken* pSVR =
    2783           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2784             : #ifdef  ISNAN
    2785           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2786           0 :                 ss << "    {\n";
    2787             : #endif
    2788           0 :                 ss << "        arg = ";
    2789           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2790             : #ifdef  ISNAN
    2791           0 :                 ss << "        if (!isNan(arg))\n";
    2792           0 :                 ss << "        {\n";
    2793             : #endif
    2794           0 :                 ss << "            fSum += arg;\n";
    2795           0 :                 ss << "            fCount += 1.0;\n";
    2796             : #ifdef ISNAN
    2797           0 :                 ss << "        }\n";
    2798           0 :                 ss << "    }\n";
    2799             : #endif
    2800             :             }
    2801             :             else
    2802             :             {
    2803           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2804           0 :                 ss << "    fSum += arg;\n";
    2805           0 :                 ss << "    fCount += 1.0;\n";
    2806             :             }
    2807             :         }
    2808             :         else
    2809             :         {
    2810           0 :             ss << "    arg = ";
    2811           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2812           0 :             ss << "    fSum += arg;\n";
    2813           0 :             ss << "    fCount += 1.0;\n";
    2814             :         }
    2815           0 :         if (i == 0)
    2816             :         {
    2817           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
    2818             :         }
    2819             :     }
    2820           0 :     i = vSubArguments.size();
    2821           0 :     while (i--)
    2822             :     {
    2823           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2824             :         assert(pCur);
    2825           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2826             :         {
    2827           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2828             :             {
    2829             :                 const formula::DoubleVectorRefToken* pDVR =
    2830           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2831           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2832           0 :                 ss << "    for (int i = ";
    2833           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2834             :                 {
    2835             : #ifdef  ISNAN
    2836           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2837           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2838           0 :                     ss << "    {\n";
    2839             : #else
    2840             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2841             :                     ss << "    {\n";
    2842             : #endif
    2843             :                 }
    2844           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2845             :                 {
    2846             : #ifdef  ISNAN
    2847           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2848           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2849           0 :                     ss << "    {\n";
    2850             : #else
    2851             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2852             :                     ss << "    {\n";
    2853             : #endif
    2854             :                 }
    2855           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2856             :                 {
    2857             : #ifdef  ISNAN
    2858           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2859           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2860           0 :                     ss << "    {\n";
    2861             : #else
    2862             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2863             :                     ss << "    {\n";
    2864             : #endif
    2865             :                 }
    2866             :                 else
    2867             :                 {
    2868             : #ifdef  ISNAN
    2869           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2870           0 :                     ss << "    {\n";
    2871             : #else
    2872             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2873             :                     ss << "    {\n";
    2874             : #endif
    2875             :                 }
    2876           0 :                 ss << "        arg = ";
    2877           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2878             : #ifdef  ISNAN
    2879           0 :                 ss << "        if (isNan(arg))\n";
    2880           0 :                 ss << "            continue;\n";
    2881             : #endif
    2882           0 :                 ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
    2883           0 :                 ss << "    }\n";
    2884             :             }
    2885           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2886             :             {
    2887             :                 const formula::SingleVectorRefToken* pSVR =
    2888           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2889             : #ifdef  ISNAN
    2890           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2891           0 :                 ss << "    {\n";
    2892             : #endif
    2893           0 :                 ss << "        arg = ";
    2894           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2895             : #ifdef  ISNAN
    2896           0 :                 ss << "        if (!isNan(arg))\n";
    2897           0 :                 ss << "        {\n";
    2898             : #endif
    2899           0 :                 ss << "            vSum += (arg - fMean) * (arg - fMean);\n";
    2900             : #ifdef ISNAN
    2901           0 :                 ss << "        }\n";
    2902           0 :                 ss << "    }\n";
    2903             : #endif
    2904             :             }
    2905             :             else
    2906             :             {
    2907           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2908           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2909             :             }
    2910             :         }
    2911             :         else
    2912             :         {
    2913           0 :             ss << "    arg = ";
    2914           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2915           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2916             :         }
    2917             :     }
    2918           0 :     ss << "    if (fCount <= 1.0)\n";
    2919           0 :     ss << "        return DBL_MAX;\n";
    2920           0 :     ss << "    else\n";
    2921           0 :     ss << "        return sqrt(vSum * pow(fCount - 1.0,-1.0));\n";
    2922           0 :     ss << "}\n";
    2923           0 : }
    2924             : 
    2925           0 : void OpStDevP::GenSlidingWindowFunction(std::stringstream &ss,
    2926             :             const std::string &sSymName, SubArguments &vSubArguments)
    2927             : {
    2928           0 :     ss << "\ndouble " << sSymName;
    2929           0 :     ss << "_" << BinFuncName() << "(";
    2930           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    2931             :     {
    2932           0 :         if (i)
    2933           0 :             ss << ",";
    2934           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    2935             :     }
    2936           0 :     ss << "){\n";
    2937           0 :     ss << "    int gid0 = get_global_id(0);\n";
    2938           0 :     ss << "    double fSum = 0.0;\n";
    2939           0 :     ss << "    double fMean = 0.0;\n";
    2940           0 :     ss << "    double vSum = 0.0;\n";
    2941           0 :     ss << "    double fCount = 0.0;\n";
    2942           0 :     ss << "    double arg = 0.0;\n";
    2943           0 :     unsigned i = vSubArguments.size();
    2944           0 :     while (i--)
    2945             :     {
    2946           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2947             :         assert(pCur);
    2948           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2949             :         {
    2950           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2951             :             {
    2952             :                 const formula::DoubleVectorRefToken* pDVR =
    2953           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2954           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2955           0 :                 ss << "    for (int i = ";
    2956           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2957             :                 {
    2958             : #ifdef  ISNAN
    2959           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2960           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2961           0 :                     ss << "    {\n";
    2962             : #else
    2963             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2964             :                     ss << "    {\n";
    2965             : #endif
    2966             :                 }
    2967           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2968             :                 {
    2969             : #ifdef  ISNAN
    2970           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2971           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2972           0 :                     ss << "    {\n";
    2973             : #else
    2974             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2975             :                     ss << "    {\n";
    2976             : #endif
    2977             :                 }
    2978           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2979             :                 {
    2980             : #ifdef  ISNAN
    2981           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2982           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2983           0 :                     ss << "    {\n";
    2984             : #else
    2985             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2986             :                     ss << "    {\n";
    2987             : #endif
    2988             :                 }
    2989             :                 else
    2990             :                 {
    2991             : #ifdef  ISNAN
    2992           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2993           0 :                     ss << "    {\n";
    2994             : #else
    2995             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2996             :                     ss << "    {\n";
    2997             : #endif
    2998             :                 }
    2999             : 
    3000           0 :                 ss << "        arg = ";
    3001           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3002             : #ifdef  ISNAN
    3003           0 :                 ss << "        if (isNan(arg))\n";
    3004           0 :                 ss << "            continue;\n";
    3005             : #endif
    3006           0 :                 ss << "        fSum += arg;\n";
    3007           0 :                 ss << "        fCount += 1.0;\n";
    3008           0 :                 ss << "    }\n";
    3009             :             }
    3010           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    3011             :             {
    3012             :                 const formula::SingleVectorRefToken* pSVR =
    3013           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    3014             : #ifdef  ISNAN
    3015           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    3016           0 :                 ss << "    {\n";
    3017             : #endif
    3018           0 :                 ss << "        arg = ";
    3019           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3020             : #ifdef ISNAN
    3021           0 :                 ss << "        if (!isNan(arg))\n";
    3022           0 :                 ss << "        {\n";
    3023             : #endif
    3024           0 :                 ss << "            fSum += arg;\n";
    3025           0 :                 ss << "            fCount += 1.0;\n";
    3026             : #ifdef ISNAN
    3027           0 :                 ss << "        }\n";
    3028           0 :                 ss << "    }\n";
    3029             : #endif
    3030             :             }
    3031             :             else
    3032             :             {
    3033           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    3034           0 :                 ss << "    fSum += arg;\n";
    3035           0 :                 ss << "    fCount += 1.0;\n";
    3036             :             }
    3037             :         }
    3038             :         else
    3039             :         {
    3040           0 :             ss << "    arg = ";
    3041           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3042           0 :             ss << "    fSum += arg;\n";
    3043           0 :             ss << "    fCount += 1.0;\n";
    3044             : 
    3045             :         }
    3046           0 :         if (i == 0)
    3047             :         {
    3048           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
    3049             :         }
    3050             :     }
    3051           0 :     i = vSubArguments.size();
    3052           0 :     while (i--)
    3053             :     {
    3054           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    3055             :         assert(pCur);
    3056           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    3057             :         {
    3058           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    3059             :             {
    3060             :                 const formula::DoubleVectorRefToken* pDVR =
    3061           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    3062           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    3063           0 :                 ss << "    for (int i = ";
    3064           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3065             :                 {
    3066             : #ifdef  ISNAN
    3067           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    3068           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    3069           0 :                     ss << "    {\n";
    3070             : #else
    3071             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    3072             :                     ss << "    {\n";
    3073             : #endif
    3074             :                 }
    3075           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3076             :                 {
    3077             : #ifdef  ISNAN
    3078           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    3079           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    3080           0 :                     ss << "    {\n";
    3081             : #else
    3082             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    3083             :                     ss << "    {\n";
    3084             : #endif
    3085             :                 }
    3086           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3087             :                 {
    3088             : #ifdef  ISNAN
    3089           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    3090           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    3091           0 :                     ss << "    {\n";
    3092             : #else
    3093             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3094             :                     ss << "    {\n";
    3095             : #endif
    3096             :                 }
    3097             :                 else
    3098             :                 {
    3099             : #ifdef  ISNAN
    3100           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    3101           0 :                     ss << "    {\n";
    3102             : #else
    3103             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    3104             :                     ss << "    {\n";
    3105             : #endif
    3106             :                 }
    3107             : 
    3108           0 :                 ss << "        arg = ";
    3109           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3110             : #ifdef  ISNAN
    3111           0 :                 ss << "        if (isNan(arg))\n";
    3112           0 :                 ss << "            continue;\n";
    3113             : #endif
    3114           0 :                 ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
    3115           0 :                 ss << "    }\n";
    3116             :             }
    3117           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    3118             :             {
    3119             :                 const formula::SingleVectorRefToken* pSVR =
    3120           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    3121             : #ifdef  ISNAN
    3122           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    3123           0 :                 ss << "    {\n";
    3124             : #endif
    3125           0 :                 ss << "        arg = ";
    3126           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3127             : #ifdef ISNAN
    3128           0 :                 ss << "        if (!isNan(arg))\n";
    3129           0 :                 ss << "        {\n";
    3130             : #endif
    3131           0 :                 ss << "            vSum += (arg - fMean) * (arg - fMean);\n";
    3132             : #ifdef ISNAN
    3133           0 :                 ss << "        }\n";
    3134           0 :                 ss << "    }\n";
    3135             : #endif
    3136             :             }
    3137             :             else
    3138             :             {
    3139           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    3140           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    3141             :             }
    3142             :         }
    3143             :         else
    3144             :         {
    3145           0 :             ss << "    arg = ";
    3146           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3147           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    3148             :         }
    3149             :     }
    3150           0 :     ss << "    if (fCount == 0.0)\n";
    3151           0 :     ss << "        return DBL_MAX;\n";
    3152           0 :     ss << "    else\n";
    3153           0 :     ss << "        return sqrt(vSum * pow(fCount,-1.0));\n";
    3154           0 :     ss << "}\n";
    3155           0 : }
    3156             : 
    3157           0 : void OpSlope::GenSlidingWindowFunction(std::stringstream &ss,
    3158             :             const std::string &sSymName, SubArguments &vSubArguments)
    3159             : {
    3160           0 :     ss << "\ndouble " << sSymName;
    3161           0 :     ss << "_" << BinFuncName() << "(";
    3162           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3163             :     {
    3164           0 :         if (i)
    3165           0 :             ss << ",";
    3166           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3167             :     }
    3168           0 :     ss << "){\n";
    3169           0 :     ss << "    int gid0 = get_global_id(0);\n";
    3170           0 :     ss << "    double fSumX = 0.0;\n";
    3171           0 :     ss << "    double fSumY = 0.0;\n";
    3172           0 :     ss << "    double fMeanX = 0.0;\n";
    3173           0 :     ss << "    double fMeanY = 0.0;\n";
    3174           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    3175           0 :     ss << "    double fSumSqrDeltaX = 0.0;\n";
    3176           0 :     ss << "    double fCount = 0.0;\n";
    3177           0 :     ss << "    double argX = 0.0;\n";
    3178           0 :     ss << "    double argY = 0.0;\n";
    3179           0 :     if(vSubArguments.size() != 2)
    3180             :     {
    3181           0 :         ss << "    return NAN;\n";
    3182           0 :         ss << "}\n";
    3183           0 :         return ;
    3184             :     }
    3185           0 :     FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
    3186           0 :     FormulaToken *pCur1 = vSubArguments[0]->GetFormulaToken();
    3187             :     assert(pCur);
    3188             :     assert(pCur1);
    3189           0 :     if (pCur->GetType() == formula::svDoubleVectorRef&&
    3190           0 :         pCur1->GetType() == formula::svDoubleVectorRef)
    3191             :     {
    3192             :         const formula::DoubleVectorRefToken* pDVR =
    3193           0 :             static_cast<const formula::DoubleVectorRefToken *>(pCur);
    3194             :         const formula::DoubleVectorRefToken* pDVR1 =
    3195           0 :             static_cast<const formula::DoubleVectorRefToken *>(pCur1);
    3196             : 
    3197           0 :         size_t nCurWindowSize = pDVR->GetRefRowSize();
    3198           0 :         size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
    3199           0 :         size_t arrayLength = pDVR->GetArrayLength()<
    3200           0 :                pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
    3201           0 :                     pDVR1->GetArrayLength();
    3202           0 :         if(nCurWindowSize != nCurWindowSize1)
    3203             :         {
    3204           0 :             ss << "    return NAN;\n";
    3205           0 :             ss << "}\n";
    3206           0 :             return ;
    3207             :         }
    3208           0 :         ss << "    for (int i = ";
    3209           0 :         if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3210           0 :             &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    3211             :         {
    3212             : #ifdef  ISNAN
    3213           0 :             ss << "gid0; i < " << arrayLength;
    3214           0 :             ss << " && i < " << nCurWindowSize  << "; i++)\n";
    3215           0 :             ss << "    {\n";
    3216             : #else
    3217             :             ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    3218             :             ss << "    {\n";
    3219             : #endif
    3220             :         }
    3221           0 :         else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3222           0 :             &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3223             :         {
    3224             : #ifdef  ISNAN
    3225           0 :             ss << "0; i < " << arrayLength ;
    3226           0 :             ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    3227           0 :             ss << "    {\n";
    3228             : #else
    3229             :             ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    3230             :             ss << "    {\n";
    3231             : #endif
    3232             :         }
    3233           0 :         else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3234           0 :             &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3235             :         {
    3236             : #ifdef  ISNAN
    3237           0 :             ss << "0; i + gid0 < " << arrayLength;
    3238           0 :             ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    3239           0 :             ss << "    {\n";
    3240             : #else
    3241             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3242             :             ss << "    {\n";
    3243             : #endif
    3244             :         }
    3245           0 :         else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3246           0 :             &&(pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    3247             :         {
    3248             : #ifdef  ISNAN
    3249           0 :             ss << "0; i < " << arrayLength << "; i++)\n";
    3250           0 :             ss << "    {\n";
    3251             : #else
    3252             :             ss << "0; i < " << arrayLength << "; i++)\n";
    3253             :             ss << "    {\n";
    3254             : #endif
    3255             :         }
    3256             :         else
    3257             :         {
    3258             : #ifdef  ISNAN
    3259           0 :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3260           0 :             ss << "    {\n";
    3261             : #else
    3262             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3263             :             ss << "    {\n";
    3264             : #endif
    3265           0 :             ss << "        break;\n";
    3266           0 :             ss << "    }";
    3267           0 :             ss << "    return NAN;\n";
    3268           0 :             ss << "}\n";
    3269           0 :             return ;
    3270             :         }
    3271             : 
    3272           0 :         ss << "        argX = ";
    3273           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    3274           0 :         ss << "        argY = ";
    3275           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    3276             : #ifdef  ISNAN
    3277           0 :         ss << "        if (isNan(argX) || isNan(argY))\n";
    3278           0 :         ss << "            continue;\n";
    3279             : #endif
    3280           0 :         ss << "        fSumX += argX;\n";
    3281           0 :         ss << "        fSumY += argY;\n";
    3282           0 :         ss << "        fCount += 1.0;\n";
    3283           0 :         ss << "    }\n";
    3284             : 
    3285           0 :         ss << "    if (fCount < 1.0)\n";
    3286           0 :         ss << "        return NAN;\n";
    3287           0 :         ss << "    else\n";
    3288           0 :         ss << "    {\n";
    3289           0 :         ss << "        fMeanX = fSumX * pow(fCount,-1.0);\n";
    3290           0 :         ss << "        fMeanY = fSumY * pow(fCount,-1.0);\n";
    3291             : 
    3292           0 :         ss << "        for (int i = ";
    3293           0 :         if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3294           0 :             &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    3295             :         {
    3296             : #ifdef  ISNAN
    3297           0 :             ss << "gid0; i < " << arrayLength;
    3298           0 :             ss << " && i < " << nCurWindowSize  << "; i++)\n";
    3299           0 :             ss << "        {\n";
    3300             : #else
    3301             :             ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    3302             :             ss << "        {\n";
    3303             : #endif
    3304             :         }
    3305           0 :         else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3306           0 :             &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3307             :         {
    3308             : #ifdef  ISNAN
    3309           0 :             ss << "0; i < " << arrayLength ;
    3310           0 :             ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    3311           0 :             ss << "        {\n";
    3312             : #else
    3313             :             ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    3314             :             ss << "        {\n";
    3315             : #endif
    3316             :         }
    3317           0 :         else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3318           0 :             &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3319             :         {
    3320             : #ifdef  ISNAN
    3321           0 :             ss << "0; i + gid0 < " << arrayLength;
    3322           0 :             ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    3323           0 :             ss << "        {\n";
    3324             : #else
    3325             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3326             :             ss << "        {\n";
    3327             : #endif
    3328             :         }
    3329             :         else
    3330             :         {
    3331             : #ifdef  ISNAN
    3332           0 :             ss << "0; i < " << arrayLength << "; i++)\n";
    3333           0 :             ss << "        {\n";
    3334             : #else
    3335             :             ss << "0; i < " << arrayLength << "; i++)\n";
    3336             :             ss << "        {\n";
    3337             : #endif
    3338             :         }
    3339             : 
    3340           0 :         ss << "            argX = ";
    3341           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    3342           0 :         ss << "            argY = ";
    3343           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    3344             : #ifdef  ISNAN
    3345           0 :         ss << "            if (isNan(argX) || isNan(argY))\n";
    3346           0 :         ss << "                 continue;\n";
    3347             : #endif
    3348           0 :         ss << "            fSumDeltaXDeltaY += (argX-fMeanX)*(argY-fMeanY);\n";
    3349           0 :         ss << "            fSumSqrDeltaX += (argX-fMeanX) * (argX-fMeanX);\n";
    3350           0 :         ss << "        }\n";
    3351           0 :         ss << "        if(fSumSqrDeltaX == 0.0)\n";
    3352           0 :         ss << "            return NAN;\n";
    3353           0 :         ss << "        else\n";
    3354           0 :         ss << "        {\n";
    3355           0 :         ss << "            return fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0);\n";
    3356           0 :         ss << "        }\n";
    3357           0 :         ss << "    }\n";
    3358           0 :         ss << "}\n";
    3359             :     }
    3360             :     else
    3361             :     {
    3362           0 :         ss << "    return NAN;\n";
    3363           0 :         ss << "}\n";
    3364             :     }
    3365             : }
    3366           0 : void OpSTEYX::GenSlidingWindowFunction(std::stringstream &ss,
    3367             :             const std::string &sSymName, SubArguments &vSubArguments)
    3368             : {
    3369           0 :     ss << "\ndouble " << sSymName;
    3370           0 :     ss << "_" << BinFuncName() << "(";
    3371           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3372             :     {
    3373           0 :     if (i)
    3374           0 :             ss << ",";
    3375           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3376             :     }
    3377           0 :     ss << "){\n";
    3378           0 :     ss << "    int gid0 = get_global_id(0);\n";
    3379           0 :     ss << "    double fSumX = 0.0;\n";
    3380           0 :     ss << "    double fSumY = 0.0;\n";
    3381           0 :     ss << "    double fMeanX = 0.0;\n";
    3382           0 :     ss << "    double fMeanY = 0.0;\n";
    3383           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    3384           0 :     ss << "    double fSumSqrDeltaX = 0.0;\n";
    3385           0 :     ss << "    double fSumSqrDeltaY = 0.0;\n";
    3386           0 :     ss << "    double fCount = 0.0;\n";
    3387           0 :     ss << "    double argX = 0.0;\n";
    3388           0 :     ss << "    double argY = 0.0;\n";
    3389           0 :     FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
    3390           0 :     FormulaToken *pCur1 = vSubArguments[0]->GetFormulaToken();
    3391             :     assert(pCur);
    3392             :     assert(pCur1);
    3393           0 :     if (pCur->GetType() == formula::svDoubleVectorRef&&
    3394           0 :         pCur1->GetType() == formula::svDoubleVectorRef)
    3395             :     {
    3396             :         const formula::DoubleVectorRefToken* pDVR =
    3397           0 :             static_cast<const formula::DoubleVectorRefToken *>(pCur);
    3398             :         const formula::DoubleVectorRefToken* pDVR1 =
    3399           0 :             static_cast<const formula::DoubleVectorRefToken *>(pCur1);
    3400           0 :         size_t nCurWindowSize = pDVR->GetRefRowSize();
    3401           0 :         size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
    3402           0 :         size_t arrayLength = pDVR->GetArrayLength()<
    3403           0 :                pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
    3404           0 :                     pDVR1->GetArrayLength();
    3405           0 :         if(nCurWindowSize != nCurWindowSize1)
    3406             :         {
    3407           0 :             ss << "    return DBL_MAX;\n";
    3408           0 :             ss << "}\n";
    3409           0 :             return ;
    3410             :         }
    3411           0 :         ss << "    for (int i = ";
    3412           0 :         if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3413           0 :             &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    3414             :         {
    3415             : #ifdef  ISNAN
    3416           0 :             ss << "gid0; i < " << arrayLength;
    3417           0 :             ss << " && i < " << nCurWindowSize  << "; i++)\n";
    3418           0 :             ss << "    {\n";
    3419             : #else
    3420             :             ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    3421             :             ss << "    {\n";
    3422             : #endif
    3423             :         }
    3424           0 :         else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3425           0 :             &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3426             :         {
    3427             : #ifdef  ISNAN
    3428           0 :             ss << "0; i < " << arrayLength;
    3429           0 :             ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    3430           0 :             ss << "    {\n";
    3431             : #else
    3432             :             ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    3433             :             ss << "    {\n";
    3434             : #endif
    3435             :         }
    3436           0 :         else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3437           0 :             &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3438             :         {
    3439             : #ifdef  ISNAN
    3440           0 :             ss << "0; i + gid0 < " << arrayLength;
    3441           0 :             ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    3442           0 :             ss << "    {\n";
    3443             : #else
    3444             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3445             :             ss << "    {\n";
    3446             : #endif
    3447             :         }
    3448           0 :         else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3449           0 :             &&(pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    3450             :         {
    3451             : #ifdef  ISNAN
    3452           0 :             ss << "0; i < " << arrayLength << "; i++)\n";
    3453           0 :             ss << "    {\n";
    3454             : #else
    3455             :             ss << "0; i < " << arrayLength << "; i++)\n";
    3456             :             ss << "    {\n";
    3457             : #endif
    3458             :         }
    3459             :         else
    3460             :         {
    3461             : #ifdef  ISNAN
    3462           0 :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3463           0 :             ss << "    {\n";
    3464             : #else
    3465             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3466             :             ss << "    {\n";
    3467             : #endif
    3468           0 :             ss << "        break;\n";
    3469           0 :             ss << "    }";
    3470           0 :             ss << "    return DBL_MAX;\n";
    3471           0 :             ss << "}\n";
    3472           0 :             return ;
    3473             :         }
    3474             : 
    3475           0 :         ss << "        argX = ";
    3476           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    3477           0 :         ss << "        argY = ";
    3478           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    3479             : #ifdef  ISNAN
    3480           0 :         ss << "        if (isNan(argX) || isNan(argY))\n";
    3481           0 :         ss << "            continue;\n";
    3482             : #endif
    3483           0 :         ss << "        fSumX += argX;\n";
    3484           0 :         ss << "        fSumY += argY;\n";
    3485           0 :         ss << "        fCount += 1.0;\n";
    3486           0 :         ss << "    }\n";
    3487             : 
    3488           0 :         ss << "    if (fCount < 3.0)\n";
    3489           0 :         ss << "        return DBL_MAX;\n";
    3490           0 :         ss << "    else\n";
    3491           0 :         ss << "    {\n";
    3492           0 :         ss << "        fMeanX = fSumX * pow(fCount,-1.0);\n";
    3493           0 :         ss << "        fMeanY = fSumY * pow(fCount,-1.0);\n";
    3494             : 
    3495           0 :         ss << "        for (int i = ";
    3496           0 :         if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3497           0 :             &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    3498             :         {
    3499             : #ifdef  ISNAN
    3500           0 :             ss << "gid0; i < " << arrayLength;
    3501           0 :             ss << " && i < " << nCurWindowSize  << "; i++)\n";
    3502           0 :             ss << "        {\n";
    3503             : #else
    3504             :             ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    3505             :             ss << "        {\n";
    3506             : #endif
    3507             :         }
    3508           0 :         else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3509           0 :             &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3510             :         {
    3511             : #ifdef  ISNAN
    3512           0 :             ss << "0; i < " << arrayLength ;
    3513           0 :             ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    3514           0 :             ss << "        {\n";
    3515             : #else
    3516             :             ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    3517             :             ss << "        {\n";
    3518             : #endif
    3519             :         }
    3520           0 :         else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())&&
    3521           0 :             (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3522             :         {
    3523             : #ifdef  ISNAN
    3524           0 :             ss << "0; i + gid0 < " << arrayLength;
    3525           0 :             ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    3526           0 :             ss << "        {\n";
    3527             : #else
    3528             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3529             :             ss << "        {\n";
    3530             : #endif
    3531             :         }
    3532             :         else
    3533             :         {
    3534             : #ifdef  ISNAN
    3535           0 :             ss << "0; i < " << arrayLength << "; i++)\n";
    3536           0 :             ss << "        {\n";
    3537             : #else
    3538             :             ss << "0; i < " << arrayLength << "; i++)\n";
    3539             :             ss << "        {\n";
    3540             : #endif
    3541             :         }
    3542             : 
    3543           0 :         ss << "            argX = ";
    3544           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    3545           0 :         ss << "            argY = ";
    3546           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    3547             : #ifdef  ISNAN
    3548           0 :         ss << "            if (isNan(argX)||isNan(argY))\n";
    3549           0 :         ss << "                continue;\n";
    3550             : #endif
    3551           0 :         ss << "            fSumDeltaXDeltaY +=(argX-fMeanX)*(argY-fMeanY);\n";
    3552           0 :         ss << "            fSumSqrDeltaX += (argX-fMeanX)*(argX-fMeanX);\n";
    3553           0 :         ss << "            fSumSqrDeltaY += (argY-fMeanY)*(argY-fMeanY);\n";
    3554           0 :         ss << "        }\n";
    3555           0 :         ss << "        if(fSumSqrDeltaX == 0.0)\n";
    3556           0 :         ss << "            return DBL_MAX;\n";
    3557           0 :         ss << "        else\n";
    3558           0 :         ss << "        {\n";
    3559           0 :         ss << "            return sqrt((fSumSqrDeltaY - fSumDeltaXDeltaY * \n";
    3560           0 :         ss << "                   fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0))\n";
    3561           0 :         ss << "                   *pow(fCount - 2.0,-1.0));\n";
    3562           0 :         ss << "        }\n";
    3563           0 :         ss << "    }\n";
    3564           0 :         ss << "}\n";
    3565             :     }
    3566             :     else
    3567             :     {
    3568           0 :         ss << "    return DBL_MAX;\n";
    3569           0 :         ss << "}\n";
    3570             :     }
    3571             : }
    3572           0 : void OpFisher::GenSlidingWindowFunction(
    3573             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    3574             : vSubArguments)
    3575             : {
    3576           0 :     ss << "\ndouble " << sSymName;
    3577           0 :     ss << "_"<< BinFuncName() <<"(";
    3578           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3579             :     {
    3580           0 :         if (i)
    3581           0 :             ss << ",";
    3582           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3583             :     }
    3584           0 :     ss << ") {\n";
    3585           0 :     ss <<"    int gid0=get_global_id(0);\n";
    3586           0 :     ss <<"    double arg0;\n";
    3587           0 :     if(vSubArguments.size() != 1)
    3588             :     {
    3589           0 :         ss << "    return DBL_MAX;\n";
    3590           0 :         return ;
    3591             :     }
    3592           0 :     FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
    3593             :     assert(pCur);
    3594           0 :     if (pCur->GetType() == formula::svDoubleVectorRef)
    3595             :     {
    3596           0 :         ss << "    return DBL_MAX;\n";
    3597           0 :         return ;
    3598             :     }
    3599           0 :     else if (pCur->GetType() == formula::svSingleVectorRef)
    3600             :     {
    3601             :         const formula::SingleVectorRefToken* pSVR =
    3602           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    3603           0 :         ss << "    arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    3604           0 :         ss << ";\n";
    3605             : #ifdef ISNAN
    3606           0 :         ss<< "    if(isNan(arg0)||(gid0>=";
    3607           0 :         ss<<pSVR->GetArrayLength();
    3608           0 :         ss<<"))\n";
    3609           0 :         ss<<"        arg0 = 0;\n";
    3610             : #endif
    3611             :     }
    3612           0 :     else if (pCur->GetType() == formula::svDouble)
    3613             :     {
    3614           0 :         ss << "    arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    3615           0 :         ss << ";\n";
    3616             : #ifdef ISNAN
    3617           0 :         ss << "    if(isNan(arg0))\n";
    3618           0 :         ss << "        return DBL_MAX;\n";
    3619             : #endif
    3620             :     }
    3621           0 :     ss << "    if (fabs(arg0) >= 1.0)\n";
    3622           0 :     ss << "        return DBL_MAX;\n";
    3623           0 :     ss << "    double tmp=0.5*log((1+arg0)*pow((1-arg0),-1));\n";
    3624           0 :     ss << "    return tmp;\n";
    3625           0 :     ss << "}\n";
    3626             : }
    3627             : 
    3628           0 : void OpFisherInv::GenSlidingWindowFunction(
    3629             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    3630             : {
    3631           0 :     FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
    3632             :     const formula::SingleVectorRefToken*tmpCurDVR = static_cast<const
    3633           0 :           formula::SingleVectorRefToken *>(tmpCur);
    3634           0 :     ss << "\ndouble " << sSymName;
    3635           0 :     ss << "_"<< BinFuncName() <<"(";
    3636           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3637             :     {
    3638           0 :         if (i)
    3639           0 :             ss << ",";
    3640           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3641             :     }
    3642           0 :     ss << ") {\n\t";
    3643           0 :     ss <<"int gid0=get_global_id(0);\n\t";
    3644           0 :     ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    3645           0 :     ss << ";\n\t";
    3646             : #ifdef ISNAN
    3647           0 :     ss<< "if(isNan(arg0)||(gid0>=";
    3648           0 :     ss<<tmpCurDVR->GetArrayLength();
    3649           0 :     ss<<"))\n\t\t";
    3650           0 :     ss<<"arg0 = 0;\n\t";
    3651             : #endif
    3652           0 :     ss << "double tmp=tanh(arg0);\n\t";
    3653           0 :     ss << "return tmp;\n";
    3654           0 :     ss << "}\n";
    3655           0 : }
    3656             : 
    3657           0 : void OpGamma::GenSlidingWindowFunction(
    3658             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    3659             : {
    3660           0 :     ss << "\ndouble " << sSymName;
    3661           0 :     ss << "_"<< BinFuncName() <<"(";
    3662           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3663             :     {
    3664           0 :         if (i)
    3665           0 :             ss << ",";
    3666           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3667             :     }
    3668           0 :     ss << ") {\n\t";
    3669           0 :     ss <<"int gid0=get_global_id(0);\n\t";
    3670           0 :     ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    3671           0 :     ss << ";\n\t";
    3672           0 :     ss << "double tmp=tgamma(arg0);\n\t";
    3673           0 :     ss << "return tmp;\n";
    3674           0 :     ss << "}\n";
    3675           0 : }
    3676             : 
    3677           0 : void OpCorrel::GenSlidingWindowFunction(
    3678             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    3679             : {
    3680           0 :     if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
    3681           0 :         ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
    3682           0 :         ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
    3683             :         ///only support DoubleVector in OpCorrelfor GPU calculating.
    3684           0 :         throw Unhandled();
    3685             :     const formula::DoubleVectorRefToken* pCurDVRX =
    3686             :         static_cast<const formula::DoubleVectorRefToken *>(
    3687           0 :         vSubArguments[0]->GetFormulaToken());
    3688             :     const formula::DoubleVectorRefToken* pCurDVRY =
    3689             :         static_cast<const formula::DoubleVectorRefToken *>(
    3690           0 :         vSubArguments[1]->GetFormulaToken());
    3691           0 :     if(  pCurDVRX->GetRefRowSize() != pCurDVRY->GetRefRowSize() )
    3692           0 :          throw Unhandled();
    3693             : 
    3694           0 :     ss << "\ndouble " << sSymName;
    3695           0 :     ss << "_"<< BinFuncName() <<"(";
    3696           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3697             :     {
    3698           0 :         if (i)
    3699           0 :             ss << ",";
    3700           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3701             :     }
    3702           0 :     ss << ") {\n\t";
    3703           0 :     ss << "double vSum = 0.0;\n\t";
    3704           0 :     ss << "double vXSum = 0.0;\n\t";
    3705           0 :     ss << "double vYSum = 0.0;\n\t";
    3706           0 :     ss << "double vXMean = 0.0;\n\t";
    3707           0 :     ss << "double vYMean = 0.0;\n\t";
    3708             : 
    3709           0 :     ss << "int gid0 = get_global_id(0);\n\t";
    3710           0 :     ss << "double arg0 = 0.0;\n\t";
    3711           0 :     ss << "double arg1 = 0.0;\n\t";
    3712           0 :     ss << "int cnt = 0;\n\t";
    3713             : 
    3714           0 :     size_t nCurWindowSizeX = pCurDVRY->GetRefRowSize();
    3715             : 
    3716           0 :         ss << "for (int i = ";
    3717           0 :         if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    3718           0 :             ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
    3719           0 :             ss << "arg0 = " << vSubArguments[0]
    3720           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
    3721           0 :             ss << "arg1 = " << vSubArguments[1]
    3722           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
    3723             : #ifdef ISNAN
    3724           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3725           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3726           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
    3727           0 :             ss << "arg0 = 0.0;\n\t\t\t";
    3728           0 :             ss << "arg1 = 0.0;\n\t\t\t";
    3729           0 :             ss << "--cnt;\n\t\t";
    3730           0 :             ss << "}\n\t\t";
    3731             : #endif
    3732           0 :             ss << "++cnt;\n\t\t";
    3733           0 :             ss << "vXSum += arg0;\n\t\t";
    3734           0 :             ss << "vYSum += arg1;\n\t";
    3735           0 :             ss << "}\n\t";
    3736           0 :         } else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
    3737           0 :             ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n\t\t";
    3738           0 :             ss << "arg0 = " << vSubArguments[0]
    3739           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
    3740           0 :             ss << "arg1 = " << vSubArguments[1]
    3741           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
    3742             : #ifdef ISNAN
    3743           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3744           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3745           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
    3746           0 :             ss << "arg0 = 0.0;\n\t\t\t";
    3747           0 :             ss << "arg1 = 0.0;\n\t\t\t";
    3748           0 :             ss << "--cnt;\n\t\t";
    3749           0 :             ss << "}\n\t\t";
    3750             : #endif
    3751           0 :             ss << "++cnt;\n\t\t";
    3752           0 :             ss << "vXSum += arg0;\n\t\t";
    3753           0 :             ss << "vYSum += arg1;\n\t";
    3754           0 :             ss << "}\n\t";
    3755             :         }
    3756           0 :         else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    3757           0 :             ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
    3758           0 :             ss << "arg0 = " << vSubArguments[0]
    3759           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
    3760           0 :             ss << "arg1 = " << vSubArguments[1]
    3761           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
    3762             : #ifdef ISNAN
    3763           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3764           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3765           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
    3766           0 :             ss << "arg0 = 0.0;\n\t\t\t";
    3767           0 :             ss << "arg1 = 0.0;\n\t\t\t";
    3768           0 :             ss << "--cnt;\n\t\t";
    3769           0 :             ss << "}\n\t\t";
    3770             : #endif
    3771           0 :             ss << "++cnt;\n\t\t";
    3772           0 :             ss << "vXSum += arg0;\n\t\t";
    3773           0 :             ss << "vYSum += arg1;\n\t";
    3774           0 :             ss << "}\n\t";
    3775             :         } else {
    3776           0 :             ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
    3777           0 :             ss << "arg0 = " << vSubArguments[0]
    3778           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
    3779           0 :             ss << "arg1 = " << vSubArguments[1]
    3780           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t";
    3781             : #ifdef ISNAN
    3782           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i + gid0 >= ";
    3783           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >=";
    3784           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
    3785           0 :             ss << "arg0 = 0.0;\n\t\t\t";
    3786           0 :             ss << "arg1 = 0.0;\n\t\t\t";
    3787           0 :             ss << "--cnt;\n\t\t";
    3788           0 :             ss << "}\n\t\t";
    3789             : #endif
    3790           0 :             ss << "++cnt;\n\t\t";
    3791           0 :             ss << "vXSum += arg0;\n\t\t";
    3792           0 :             ss << "vYSum += arg1;\n\t";
    3793           0 :             ss << "}\n\t";
    3794             :         }
    3795             : 
    3796           0 :         ss << "if(cnt < 1) {\n\t\t";
    3797           0 :         ss << "return DBL_MIN;\n\t";
    3798           0 :         ss << "}\n\t";
    3799           0 :         ss << "else {\n\t\t";
    3800           0 :         ss << "vXMean = vXSum/cnt;\n\t\t";
    3801           0 :         ss << "vYMean = vYSum/cnt;\n\t\t";
    3802           0 :         ss << "vXSum = 0.0;\n\t\t";
    3803           0 :         ss << "vYSum = 0.0;\n\t\t";
    3804             : 
    3805           0 :         ss << "for (int i = ";
    3806           0 :         if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    3807           0 :             ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
    3808           0 :             ss << "arg0 = " << vSubArguments[0]
    3809           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
    3810           0 :             ss << "arg1 = " << vSubArguments[1]
    3811           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
    3812             : #ifdef ISNAN
    3813           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3814           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3815           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
    3816           0 :             ss << "arg0 = vXMean;\n\t\t\t\t";
    3817           0 :             ss << "arg1 = vYMean;\n\t\t\t";
    3818           0 :             ss << "}\n\t\t\t";
    3819             : #endif
    3820           0 :             ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
    3821           0 :             ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
    3822           0 :             ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
    3823           0 :             ss << "}\n\t\t";
    3824           0 :         } else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
    3825           0 :             ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n\t\t\t";
    3826           0 :             ss << "arg0 = " << vSubArguments[0]
    3827           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
    3828           0 :             ss << "arg1 = " << vSubArguments[1]
    3829           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
    3830             : #ifdef ISNAN
    3831           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3832           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3833           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
    3834           0 :             ss << "arg0 = vXMean;\n\t\t\t\t";
    3835           0 :             ss << "arg1 = vYMean;\n\t\t\t";
    3836           0 :             ss << "}\n\t\t\t";
    3837             : #endif
    3838           0 :             ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
    3839           0 :             ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
    3840           0 :             ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
    3841           0 :             ss << "}\n\t\t";
    3842           0 :         } else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    3843           0 :             ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
    3844           0 :             ss << "arg0 = " << vSubArguments[0]
    3845           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
    3846           0 :             ss << "arg1 = " << vSubArguments[1]
    3847           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
    3848             : #ifdef ISNAN
    3849           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3850           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3851           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
    3852           0 :             ss << "arg0 = vXMean;\n\t\t\t\t";
    3853           0 :             ss << "arg1 = vYMean;\n\t\t\t";
    3854           0 :             ss << "}\n\t\t\t";
    3855             : #endif
    3856           0 :             ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
    3857           0 :             ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
    3858           0 :             ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
    3859           0 :             ss << "}\n\t\t";
    3860             :         } else {
    3861           0 :             ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
    3862           0 :             ss << "arg0 = " << vSubArguments[0]
    3863           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
    3864           0 :             ss << "arg1 = " << vSubArguments[1]
    3865           0 :                 ->GenSlidingWindowDeclRef(true) << ";\n\t\t\t";
    3866             : #ifdef ISNAN
    3867           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i + gid0 >= ";
    3868           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >=";
    3869           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
    3870           0 :             ss << "arg0 = vXMean;\n\t\t\t\t";
    3871           0 :             ss << "arg1 = vYMean;\n\t\t\t";
    3872           0 :             ss << "}\n\t\t\t";
    3873             : #endif
    3874           0 :             ss << "vXSum += ((arg0 - vXMean)*(arg0 - vXMean));\n\t\t\t";
    3875           0 :             ss << "vYSum += ((arg1 - vYMean)*(arg1 - vYMean));\n\t\t\t";
    3876           0 :             ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
    3877           0 :             ss << "}\n\t\t";
    3878             :         }
    3879             : 
    3880           0 :         ss << "if(vXSum == 0.0 || vYSum == 0.0) {\n\t\t\t";
    3881           0 :         ss << "return NAN;\n\t\t";
    3882           0 :         ss << "}\n\t\t";
    3883           0 :         ss << "else {\n\t\t\t";
    3884           0 :         ss << "return vSum/pow(vXSum*vYSum, 0.5);\n\t\t";
    3885           0 :         ss << "}\n\t";
    3886           0 :         ss << "}\n";
    3887           0 :         ss << "}";
    3888           0 : }
    3889             : 
    3890           0 : void OpNegbinomdist::GenSlidingWindowFunction(
    3891             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    3892             : {
    3893           0 :     ss << "\ndouble " << sSymName;
    3894           0 :     ss << "_"<< BinFuncName() <<"(";
    3895           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3896             :     {
    3897           0 :         if (i)
    3898           0 :             ss << ",";
    3899           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3900             :     }
    3901           0 :     ss << ")\n";
    3902           0 :     ss << "{\n\t";
    3903           0 :     ss << "double f,s,p,tmp0,tmp1,tmp2;\n";
    3904           0 :     ss << " int gid0=get_global_id(0);\n";
    3905           0 :     size_t i = vSubArguments.size();
    3906           0 :     ss <<"\n";
    3907           0 :     for (i = 0; i < vSubArguments.size(); i++)
    3908             :     {
    3909           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    3910             :         assert(pCur);
    3911           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    3912             :         {
    3913             : #ifdef  ISNAN
    3914             :             const formula::SingleVectorRefToken* pSVR =
    3915           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    3916           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    3917             : #endif
    3918             :         }
    3919           0 :         else if (pCur->GetType() == formula::svDouble)
    3920             :         {
    3921             : #ifdef  ISNAN
    3922           0 :             ss << "{\n";
    3923             : #endif
    3924             :         }
    3925             :         else
    3926             :         {
    3927             : #ifdef  ISNAN
    3928             : #endif
    3929             :         }
    3930             : #ifdef  ISNAN
    3931           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    3932             :         {
    3933           0 :             ss << "    if (isNan(";
    3934           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    3935           0 :             ss << "))\n";
    3936           0 :             ss << "        tmp"<<i<<"= 0;\n";
    3937           0 :             ss << "    else\n";
    3938           0 :             ss << "        tmp"<<i<<"=\n";
    3939           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    3940           0 :             ss << ";\n}\n";
    3941             :         }
    3942             :         else
    3943             :         {
    3944           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    3945           0 :             ss <<";\n";
    3946             :         }
    3947             : #endif
    3948             :     }
    3949           0 :     ss << " p = tmp2;\n";
    3950           0 :     ss << " s = tmp1;\n";
    3951           0 :     ss << " f = tmp0;\n";
    3952           0 :     ss << " double q = 1.0 - p;\n\t";
    3953           0 :     ss << " double fFactor = pow(p,s);\n\t";
    3954           0 :     ss << " for(int i=0; i<f; i++)\n\t";
    3955           0 :     ss << " {\n\t";
    3956           0 :     ss << "  fFactor *= ((double)i+s)*pow(((double)i+1.0),-1.0)/pow(q,-1);\n";
    3957           0 :     ss << " }\n\t";
    3958           0 :     ss << " double temp=fFactor;\n\t";
    3959           0 :     ss << " return temp;\n";
    3960           0 :     ss << "}\n";
    3961           0 : }
    3962             : 
    3963           0 : void OpPearson::GenSlidingWindowFunction(
    3964             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    3965             : {
    3966           0 :     if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
    3967           0 :         ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
    3968           0 :         ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
    3969             :         ///only support DoubleVector in OpPearson for GPU calculating.
    3970           0 :         throw Unhandled();
    3971             :     const formula::DoubleVectorRefToken* pDVR =
    3972             :         static_cast<const formula::DoubleVectorRefToken *>(
    3973           0 :         vSubArguments[0]->GetFormulaToken());
    3974             :     const formula::DoubleVectorRefToken* pCurDVRY =
    3975             :         static_cast<const formula::DoubleVectorRefToken *>(
    3976           0 :         vSubArguments[1]->GetFormulaToken());
    3977           0 :     if(  pDVR->GetRefRowSize() != pCurDVRY->GetRefRowSize() )
    3978           0 :          throw Unhandled();
    3979             : 
    3980           0 :     size_t nCurWindowSize = pDVR->GetRefRowSize();
    3981             : 
    3982           0 :     ss << "\ndouble " << sSymName;
    3983           0 :     ss << "_"<< BinFuncName() <<"(";
    3984           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3985             :     {
    3986           0 :         if (i)
    3987           0 :             ss << ",";
    3988           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3989             :     }
    3990           0 :     ss << ")\n";
    3991           0 :     ss << "{\n";
    3992           0 :     ss << "    int gid0=get_global_id(0);\n";
    3993           0 :     ss << "    double fCount = 0.0;\n";
    3994           0 :     ss << "    double fSumX = 0.0;\n";
    3995           0 :     ss << "    double fSumY = 0.0;\n";
    3996           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    3997           0 :     ss << "    double fInx;\n";
    3998           0 :     ss << "    double fIny;\n";
    3999           0 :     ss << "for (int i = ";
    4000           0 :     if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    4001           0 :         ss << "gid0; i < " << pDVR->GetArrayLength();
    4002           0 :         ss << " && i < " << nCurWindowSize  << "; i++){\n";
    4003           0 :     } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    4004           0 :         ss << "0; i < " << pDVR->GetArrayLength();
    4005           0 :         ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    4006           0 :     } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    4007           0 :         ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    4008           0 :         ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    4009             :     }
    4010             :     else {
    4011           0 :         ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    4012             :     }
    4013           0 :     ss << "          fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef(true);
    4014           0 :     ss << ";\n";
    4015           0 :     ss << "          fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef(true);
    4016           0 :     ss << "  ;\n";
    4017           0 :     ss << " if(isNan(fInx)||isNan(fIny)){fInx=0.0;fIny=0.0;fCount = fCount-1;}\n";
    4018           0 :     ss << "       fSumX += fInx;\n";
    4019           0 :     ss << "       fSumY += fIny;\n";
    4020           0 :     ss << "       fCount = fCount + 1;\n";
    4021           0 :     ss << "     }\n";
    4022           0 :     ss << "       double fMeanX = fSumX / fCount;\n";
    4023           0 :     ss << "       double fMeanY = fSumY / fCount;\n";
    4024           0 :     ss << "       fSumX = 0.0;\n";
    4025           0 :     ss << "       fSumY = 0.0;\n";
    4026           0 :     ss << "for (int i = ";
    4027           0 :     if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    4028           0 :         ss << "gid0; i < " << pDVR->GetArrayLength();
    4029           0 :         ss << " && i < " << nCurWindowSize  << "; i++){\n";
    4030           0 :     } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    4031           0 :         ss << "0; i < " << pDVR->GetArrayLength();
    4032           0 :         ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    4033           0 :     } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    4034           0 :          ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    4035           0 :          ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    4036             :     }
    4037             :     else {
    4038           0 :          ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    4039             :     }
    4040           0 :     ss << "           fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef(true);
    4041           0 :     ss << " ;\n";
    4042           0 :     ss << "           fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef(true);
    4043           0 :     ss << " ;\n";
    4044           0 :     ss << " if(isNan(fInx)||isNan(fIny)){fInx=0.0;fIny=0.0;}\n";
    4045           0 :     ss << "           fSumDeltaXDeltaY += (fInx - fMeanX) * (fIny - fMeanY);\n";
    4046           0 :     ss << "           fSumX += pow(fInx - fMeanX,2);\n";
    4047           0 :     ss << "           fSumY += pow(fIny - fMeanY,2);\n";
    4048           0 :     ss << "       }\n";
    4049           0 :     ss << "      double tmp = ( fSumDeltaXDeltaY / ";
    4050           0 :     ss << "sqrt( fSumX * fSumY));\n\t";
    4051           0 :     ss << "      return tmp;\n";
    4052           0 :     ss << "}\n";
    4053           0 : }
    4054             : 
    4055           0 : void OpGammaLn::GenSlidingWindowFunction(
    4056             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    4057             : {
    4058           0 :     FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
    4059             :     const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
    4060           0 : formula::SingleVectorRefToken *>(tmpCur);
    4061           0 :     ss << "\ndouble " << sSymName;
    4062           0 :     ss << "_"<< BinFuncName() <<"(";
    4063           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4064             :     {
    4065           0 :         if (i)
    4066           0 :             ss << ",";
    4067           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4068             :     }
    4069           0 :     ss << ") {\n\t";
    4070           0 :     ss <<"int gid0=get_global_id(0);\n\t";
    4071           0 :     ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    4072           0 :     ss << ";\n\t";
    4073             : #ifdef ISNAN
    4074           0 :     ss<< "if(isNan(arg0)||(gid0>=";
    4075           0 :     ss<<tmpCurDVR->GetArrayLength();
    4076           0 :     ss<<"))\n\t\t";
    4077           0 :     ss<<"arg0 = 0;\n\t";
    4078             : #endif
    4079           0 :     ss << "double tmp=lgamma(arg0);\n\t";
    4080           0 :     ss << "return tmp;\n";
    4081           0 :     ss << "}\n";
    4082           0 : }
    4083           0 : void OpGauss::BinInlineFun(std::set<std::string>& decls,
    4084             :     std::set<std::string>& funs)
    4085             : {
    4086           0 :     decls.insert(taylorDecl);decls.insert(phiDecl);
    4087           0 :     decls.insert(gaussDecl);
    4088           0 :     funs.insert(taylor);funs.insert(phi);
    4089           0 :     funs.insert(gauss);
    4090           0 : }
    4091             : 
    4092           0 : void OpGauss::GenSlidingWindowFunction(
    4093             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    4094             : vSubArguments)
    4095             : {
    4096           0 :     ss << "\ndouble " << sSymName;
    4097           0 :     ss << "_"<< BinFuncName() <<"(";
    4098           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4099             :     {
    4100           0 :         if (i)
    4101           0 :             ss << ",";
    4102           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4103             :     }
    4104           0 :     ss << ") {\n";
    4105           0 :     ss <<"    int gid0=get_global_id(0);\n";
    4106           0 :     ss <<"    double arg0;\n";
    4107           0 :     if(vSubArguments.size() != 1)
    4108             :     {
    4109           0 :         ss << "    return DBL_MAX;\n";
    4110           0 :         return ;
    4111             :     }
    4112           0 :     FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
    4113             :     assert(pCur);
    4114           0 :     if (pCur->GetType() == formula::svDoubleVectorRef)
    4115             :     {
    4116           0 :         ss << "    return DBL_MAX;\n";
    4117           0 :         return ;
    4118             :     }
    4119           0 :     else if (pCur->GetType() == formula::svSingleVectorRef)
    4120             :     {
    4121             :         const formula::SingleVectorRefToken* pSVR =
    4122           0 :             static_cast< const formula::SingleVectorRefToken* >(pCur);
    4123           0 :         ss << "    arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    4124           0 :         ss << ";\n";
    4125             : #ifdef ISNAN
    4126           0 :         ss<< "    if(isNan(arg0)||(gid0>=";
    4127           0 :         ss<<pSVR->GetArrayLength();
    4128           0 :         ss<<"))\n";
    4129           0 :         ss<<"        arg0 = 0;\n";
    4130             : #endif
    4131             :     }
    4132           0 :     else if (pCur->GetType() == formula::svDouble)
    4133             :     {
    4134           0 :         ss << "    arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    4135           0 :         ss << ";\n";
    4136             : #ifdef ISNAN
    4137           0 :         ss << "    if(isNan(arg0))\n";
    4138           0 :         ss << "        return DBL_MAX;\n";
    4139             : #endif
    4140             :     }
    4141           0 :     ss << "    double tmp=gauss(arg0);\n";
    4142           0 :     ss << "    return tmp;\n";
    4143           0 :     ss << "}\n";
    4144             : }
    4145             : 
    4146           0 : void OpGeoMean::GenSlidingWindowFunction(
    4147             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    4148             : {
    4149           0 :     ss << "__kernel void ";
    4150           0 :     ss << "GeoMean_reduction(  ";
    4151           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4152             :     {
    4153           0 :         if (i)
    4154           0 :             ss << ",";
    4155           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4156             :     }
    4157           0 :     ss << ", __global double *result)\n";
    4158           0 :     ss << "{\n";
    4159           0 :     ss << "    double tmp =0;\n";
    4160           0 :     ss << "    int count = 0;\n";
    4161           0 :     ss << "    int i ;\n";
    4162           0 :     GenTmpVariables(ss,vSubArguments);
    4163           0 :     ss << "    double current_sum = 0.0;\n";
    4164           0 :     ss << "    int windowSize;\n";
    4165           0 :     ss << "    int arrayLength;\n";
    4166           0 :     ss << "    int current_count = 0;\n";
    4167           0 :     ss << "    int writePos = get_group_id(1);\n";
    4168           0 :     ss << "    int lidx = get_local_id(0);\n";
    4169           0 :     ss << "    __local double shm_buf[256];\n";
    4170           0 :     ss << "    __local int count_buf[256];\n";
    4171           0 :     ss << "    int loop;\n";
    4172           0 :     ss << "    int offset;\n";
    4173           0 :     ss << "    barrier(CLK_LOCAL_MEM_FENCE);\n";
    4174             : 
    4175           0 :     for(unsigned i=0;i<vSubArguments.size();i++)
    4176             :     {
    4177             :         assert(vSubArguments[i]->GetFormulaToken());
    4178             : 
    4179           0 :         if(vSubArguments[i]->GetFormulaToken()->GetType() ==
    4180             :         formula::svDoubleVectorRef)
    4181             :         {
    4182           0 :             FormulaToken *tmpCur = vSubArguments[i]->GetFormulaToken();
    4183             :                 const formula::DoubleVectorRefToken*pCurDVR= static_cast<const
    4184           0 :                      formula::DoubleVectorRefToken *>(tmpCur);
    4185           0 :                 size_t nCurWindowSize = pCurDVR->GetArrayLength() <
    4186           0 :                 pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
    4187           0 :                 pCurDVR->GetRefRowSize() ;
    4188             : 
    4189           0 :             if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
    4190           0 :                 ss << "    offset = 0;\n";
    4191           0 :             else if (!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
    4192           0 :                 ss << "    offset = get_group_id(1);\n";
    4193             :             else
    4194           0 :                 throw Unhandled();
    4195           0 :             ss << "    windowSize = ";
    4196           0 :             ss << nCurWindowSize;
    4197           0 :             ss << ";\n";
    4198           0 :             ss << "    arrayLength = ";
    4199           0 :             ss << pCurDVR->GetArrayLength();
    4200           0 :             ss << ";\n";
    4201           0 :             ss << "    loop = arrayLength/512 + 1;\n";
    4202           0 :             ss << "    for (int l=0; l<loop; l++){\n";
    4203           0 :             ss << "        tmp = 0.0;\n";
    4204           0 :             ss << "        count = 0;\n";
    4205           0 :             ss << "        int loopOffset = l*512;\n";
    4206           0 :             ss << "        int p1 = loopOffset + lidx + offset, p2 = p1 + 256;\n";
    4207           0 :             ss << "        if (p2 < min(offset + windowSize, arrayLength)) {\n";
    4208           0 :             ss << "            tmp0 = 0.0;\n";
    4209           0 :             std::string p1 = "p1";
    4210           0 :             std::string p2 = "p2";
    4211             : 
    4212           0 :             ss << "        tmp0 =";
    4213           0 :             vSubArguments[i]->GenDeclRef(ss);
    4214           0 :             ss << "["<<p1.c_str()<<"];\n";
    4215           0 :             ss << "        if(!isNan(tmp0))\n";
    4216           0 :             ss << "       {\n";
    4217           0 :             ss << "           tmp += log(tmp0);\n";
    4218           0 :             ss << "           count++;\n";
    4219           0 :             ss << "       }\n";
    4220             : 
    4221           0 :             ss << "        tmp0 =";
    4222           0 :             vSubArguments[i]->GenDeclRef(ss);
    4223           0 :             ss << "["<<p2.c_str()<<"];\n";
    4224           0 :             ss << "        if(!isNan(tmp0))\n";
    4225           0 :             ss << "       {\n";
    4226           0 :             ss << "           tmp += log(tmp0);\n";
    4227           0 :             ss << "           count++;\n";
    4228           0 :             ss << "       }\n";
    4229             : 
    4230           0 :             ss << "        }\n";
    4231           0 :             ss << "        else if (p1 < min(arrayLength, offset + windowSize)) {\n";
    4232             : 
    4233           0 :             ss << "        tmp0 =";
    4234           0 :             vSubArguments[i]->GenDeclRef(ss);
    4235           0 :             ss << "["<<p1.c_str()<<"];\n";
    4236           0 :             ss << "        if(!isNan(tmp0))\n";
    4237           0 :             ss << "        {\n";
    4238           0 :             ss << "            tmp += log(tmp0);\n";
    4239           0 :             ss << "            count++;\n";
    4240           0 :             ss << "        }\n";
    4241             : 
    4242           0 :             ss << "        }\n";
    4243           0 :             ss << "        shm_buf[lidx] = tmp;\n";
    4244           0 :             ss << "        count_buf[lidx] = count;\n";
    4245           0 :             ss << "        barrier(CLK_LOCAL_MEM_FENCE);\n";
    4246             : 
    4247           0 :             ss << "        for (int i = 128; i >0; i/=2) {\n";
    4248           0 :             ss << "            if (lidx < i)\n";
    4249           0 :             ss << "            {\n";
    4250           0 :             ss << "                shm_buf[lidx] += shm_buf[lidx + i];\n";
    4251           0 :             ss << "                count_buf[lidx] += count_buf[lidx + i];\n";
    4252           0 :             ss << "            }\n";
    4253           0 :             ss << "            barrier(CLK_LOCAL_MEM_FENCE);\n";
    4254           0 :             ss << "        }\n";
    4255           0 :             ss << "        if (lidx == 0)\n";
    4256           0 :             ss << "        {\n";
    4257           0 :             ss << "            current_sum += shm_buf[0];\n";
    4258           0 :             ss << "            current_count += count_buf[0];\n";
    4259           0 :             ss << "        }\n";
    4260             :              //  ss << "if(writePos == 14 && lidx ==0)\n";
    4261             :             //ss <<"printf(\"\\n********************sum is  is %f, count is%d\",current_sum,current_count);\n";
    4262           0 :             ss << "        barrier(CLK_LOCAL_MEM_FENCE);\n";
    4263           0 :             ss << "    }\n";
    4264             :         }else
    4265             :         {
    4266           0 :             ss << "    if (lidx == 0)\n";
    4267           0 :             ss << "    {\n";
    4268           0 :             ss << "        tmp0 =";
    4269           0 :             if(vSubArguments[i]->GetFormulaToken()->GetType() ==
    4270             :      formula::svSingleVectorRef)
    4271             :             {
    4272           0 :                 vSubArguments[i]->GenDeclRef(ss);
    4273           0 :                 ss << "[writePos];\n";
    4274             :             }
    4275             :             else
    4276             :             {
    4277           0 :                 vSubArguments[i]->GenDeclRef(ss);
    4278           0 :                 ss <<";\n";
    4279             :                 //ss <<"printf(\"\\n********************tmp0 is %f\",tmp0);\n";
    4280             :             }
    4281           0 :             ss << "        if(!isNan(tmp0))\n";
    4282           0 :             ss << "       {\n";
    4283           0 :             ss << "           current_sum += log(tmp0);\n";
    4284           0 :             ss << "           current_count++;\n";
    4285           0 :             ss << "       }\n";
    4286           0 :             ss << "    }\n";
    4287             :         }
    4288             :     }
    4289             : 
    4290           0 :     ss << "    if (lidx == 0)\n";
    4291           0 :     ss << "        result[writePos] = exp(current_sum/current_count);\n";
    4292           0 :     ss << "}\n";
    4293             : 
    4294           0 :     ss << "\ndouble " << sSymName;
    4295           0 :     ss << "_"<< BinFuncName() <<"(";
    4296           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4297             :     {
    4298           0 :         if (i)
    4299           0 :             ss << ",";
    4300           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4301             :     }
    4302           0 :     ss << ")\n    {\n";
    4303           0 :     ss <<"    int gid0=get_global_id(0);\n";
    4304           0 :     ss << "    double tmp =0;\n";
    4305           0 :     ss << "    tmp =";
    4306           0 :     vSubArguments[0]->GenDeclRef(ss);
    4307           0 :     ss << "[gid0];\n";
    4308           0 :     ss << "    return tmp;\n";
    4309           0 :     ss << "}";
    4310           0 : }
    4311             : 
    4312           0 : void OpHarMean::GenSlidingWindowFunction(
    4313             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    4314             : vSubArguments)
    4315             : {
    4316             : 
    4317           0 :     ss << "\ndouble " << sSymName;
    4318           0 :     ss << "_"<< BinFuncName() <<"( ";
    4319           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4320             :     {
    4321           0 :         if (i)
    4322           0 :             ss << ",";
    4323           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4324             :     }
    4325           0 :     ss << ")\n";
    4326           0 :     ss <<"{\n";
    4327           0 :     ss << "    int gid0 = get_global_id(0);\n";
    4328           0 :     ss << "    double nVal=0.0;\n";
    4329           0 :     ss << "    double tmp = 0;\n";
    4330           0 :     ss << "    int length;\n";
    4331           0 :     ss << "    int totallength=0;\n";
    4332           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4333             :     {
    4334           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4335             :         assert(pCur);
    4336           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    4337             :         {
    4338             :             const formula::DoubleVectorRefToken* pDVR =
    4339           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    4340           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    4341           0 :             ss << "    length="<<nCurWindowSize;
    4342           0 :             ss << ";\n";
    4343           0 :             ss << "    for (int i = ";
    4344           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    4345           0 :             ss << "    {\n";
    4346           0 :             ss << "        double arg"<<i<<" = ";
    4347           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4348           0 :             ss << ";\n";
    4349             : #ifdef ISNAN
    4350           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
    4351           0 :             ss << pDVR->GetArrayLength();
    4352           0 :             ss << "))\n";
    4353           0 :             ss << "        {\n";
    4354           0 :             ss << "            length--;\n";
    4355           0 :             ss << "            continue;\n";
    4356           0 :             ss << "        }\n";
    4357             : #endif
    4358           0 :             ss << "        nVal += (1.0 *pow(";
    4359           0 :             ss << " arg"<<i<<",-1));\n";
    4360           0 :             ss << "    }\n";
    4361           0 :             ss << "    totallength +=length;\n";
    4362             :         }
    4363           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    4364             :         {
    4365           0 :             ss << "    tmp = ";
    4366           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4367           0 :             ss << ";\n";
    4368             : #ifdef ISNAN
    4369           0 :             ss << "    if(!isNan(tmp))\n";
    4370           0 :             ss << "    {\n";
    4371           0 :             ss << "        nVal += (1.0 * pow( tmp,-1));\n";
    4372           0 :             ss << "        totallength +=1;\n";
    4373           0 :             ss << "    }\n";
    4374             : #endif
    4375             :         }
    4376           0 :         else if (pCur->GetType() == formula::svDouble)
    4377             :         {
    4378           0 :            ss << "    tmp = ";
    4379           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4380           0 :            ss << ";\n";
    4381           0 :            ss << "    nVal += (1.0 *pow( tmp,-1));\n";
    4382           0 :            ss << "    totallength +=1;\n";
    4383             :         }
    4384             :         else
    4385             :         {
    4386           0 :             ss << "    return DBL_MIN;\n";
    4387             :         }
    4388             :     }
    4389           0 :     ss << "    tmp = totallength*pow(nVal,-1);\n";
    4390           0 :     ss << "    return tmp;\n";
    4391           0 :     ss << "}";
    4392           0 : }
    4393             : 
    4394           0 : void OpConfidence::BinInlineFun(std::set<std::string>& decls,
    4395             :     std::set<std::string>& funs)
    4396             : {
    4397           0 :     decls.insert(gaussinvDecl);
    4398           0 :     funs.insert(gaussinv);
    4399           0 : }
    4400             : 
    4401           0 : void OpConfidence::GenSlidingWindowFunction(std::stringstream& ss,
    4402             :     const std::string &sSymName, SubArguments& vSubArguments)
    4403             : {
    4404           0 :     ss << "\ndouble " << sSymName;
    4405           0 :     ss << "_"<< BinFuncName() <<"(";
    4406           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4407             :     {
    4408           0 :         if (i)
    4409           0 :             ss << ",";
    4410           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4411             :     }
    4412           0 :     ss << ") {\n";
    4413           0 :     ss << "    double tmp = " << GetBottom() <<";\n";
    4414           0 :     ss << "    int gid0 = get_global_id(0);\n";
    4415           0 :     ss << "    double alpha = " << GetBottom() <<";\n";
    4416           0 :     ss << "    double sigma = " << GetBottom() <<";\n";
    4417           0 :     ss << "    double size = " << GetBottom() <<";\n";
    4418           0 :     ss << "    double tmp0,tmp1,tmp2;\n";
    4419           0 :     size_t i = vSubArguments.size();
    4420           0 :     ss <<"\n";
    4421           0 :     for (i = 0; i < vSubArguments.size(); i++)
    4422             :     {
    4423           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4424             :         assert(pCur);
    4425           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    4426             :         {
    4427             : #ifdef  ISNAN
    4428             :             const formula::SingleVectorRefToken* pSVR =
    4429           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    4430           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4431             : #endif
    4432             :         }
    4433           0 :         else if (pCur->GetType() == formula::svDouble)
    4434             :         {
    4435             : #ifdef  ISNAN
    4436           0 :             ss << "{\n";
    4437             : #endif
    4438             :         }
    4439             :         else
    4440             :         {
    4441             : #ifdef  ISNAN
    4442             : #endif
    4443             :         }
    4444             : #ifdef  ISNAN
    4445           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    4446             :         {
    4447           0 :             ss << "    if (isNan(";
    4448           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4449           0 :             ss << "))\n";
    4450           0 :             ss << "        tmp"<<i<<"= 0;\n";
    4451           0 :             ss << "    else\n";
    4452           0 :             ss << "        tmp"<<i<<"=\n";
    4453           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4454           0 :             ss << ";\n}\n";
    4455             :         }
    4456             :         else
    4457             :         {
    4458           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4459           0 :             ss <<";\n";
    4460             :         }
    4461             : #endif
    4462             :     }
    4463           0 :     ss << "    alpha = tmp0;\n";
    4464           0 :     ss << "    sigma = tmp1;\n";
    4465           0 :     ss << "    size = tmp2;\n";
    4466           0 :     ss << "    double rn = floor(size);\n";
    4467           0 :     ss << "    if(sigma <= 0.0 || alpha <= 0.0 || alpha >= 1.0";
    4468           0 :     ss << "|| rn < 1.0)\n";
    4469           0 :     ss << "        tmp = -DBL_MAX;\n";
    4470           0 :     ss << "    else\n";
    4471           0 :     ss << "        tmp = gaussinv(1.0 - alpha * pow(2.0,-1.0)) * sigma ";
    4472           0 :     ss << "* pow(sqrt( rn ),-1);\n";
    4473           0 :     ss << "    return tmp;\n";
    4474           0 :     ss << "}";
    4475           0 : }
    4476             : 
    4477           0 : void OpCritBinom::BinInlineFun(std::set<std::string>& decls,
    4478             :     std::set<std::string>& funs)
    4479             : {
    4480           0 :     decls.insert(MinDecl);
    4481           0 :     funs.insert("");
    4482           0 : }
    4483             : 
    4484           0 : void OpCritBinom::GenSlidingWindowFunction(std::stringstream& ss,
    4485             :     const std::string &sSymName, SubArguments& vSubArguments)
    4486             : {
    4487           0 :     ss << "\ndouble " << sSymName;
    4488           0 :     ss << "_"<< BinFuncName() <<"(";
    4489           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4490             :     {
    4491           0 :         if (i)
    4492           0 :             ss << ",";
    4493           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4494             :     }
    4495           0 :     ss << ") {\n";
    4496           0 :     ss << "    double tmp = " << GetBottom() <<";\n";
    4497           0 :     ss << "    int gid0 = get_global_id(0);\n";
    4498           0 :     ss << "    double n = " << GetBottom() <<";\n";
    4499           0 :     ss << "    double p = " << GetBottom() <<";\n";
    4500           0 :     ss << "    double alpha = " << GetBottom() <<";\n";
    4501           0 :     ss << "    double tmp0 = 0.0,tmp1 = 0.0,tmp2 = 0.0;\n";
    4502           0 :     size_t i = vSubArguments.size();
    4503           0 :     ss <<"\n";
    4504           0 :     for (i = 0; i < vSubArguments.size(); i++)
    4505             :     {
    4506           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4507             :         assert(pCur);
    4508           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    4509             :         {
    4510             : #ifdef  ISNAN
    4511             :             const formula::SingleVectorRefToken* pSVR =
    4512           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    4513           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4514             : #endif
    4515             :         }
    4516           0 :         else if (pCur->GetType() == formula::svDouble)
    4517             :         {
    4518             : #ifdef  ISNAN
    4519           0 :             ss << "{\n";
    4520             : #endif
    4521             :         }
    4522             :         else
    4523             :         {
    4524             : #ifdef  ISNAN
    4525             : #endif
    4526             :         }
    4527             : #ifdef  ISNAN
    4528           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    4529             :         {
    4530           0 :             ss << "    if (isNan(";
    4531           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4532           0 :             ss << "))\n";
    4533           0 :             ss << "        tmp"<<i<<"= 0;\n";
    4534           0 :             ss << "    else\n";
    4535           0 :             ss << "        tmp"<<i<<"=\n";
    4536           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4537           0 :             ss << ";\n}\n";
    4538             :         }
    4539             :         else
    4540             :         {
    4541           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4542           0 :             ss <<";\n";
    4543             :         }
    4544             : #endif
    4545             :     }
    4546           0 :     ss << "    n = tmp0;\n";
    4547           0 :     ss << "    p = tmp1;\n";
    4548           0 :     ss << "    alpha = tmp2;\n";
    4549           0 :     ss << "    double rn = floor(n);\n";
    4550           0 :     ss << "    if (rn < 0.0 || alpha <= 0.0 || alpha >= 1.0 || p < 0.0";
    4551           0 :     ss << " || p > 1.0)\n";
    4552           0 :     ss << "        tmp = -DBL_MIN;\n";
    4553           0 :     ss << "    else\n";
    4554           0 :     ss << "    {\n";
    4555           0 :     ss << "        double rq = (0.5 - p) + 0.5;\n";
    4556           0 :     ss << "        double fFactor = pow(rq, rn);\n";
    4557           0 :     ss << "        if (fFactor <= Min)\n";
    4558           0 :     ss << "        {\n";
    4559           0 :     ss << "            fFactor = pow(p, rn);\n";
    4560           0 :     ss << "            if (fFactor <= Min)\n";
    4561           0 :     ss << "                tmp = -DBL_MAX;\n";
    4562           0 :     ss << "            else\n";
    4563           0 :     ss << "            {\n";
    4564           0 :     ss << "                double fSum = 1.0 - fFactor;\n";
    4565           0 :     ss << "                uint max =(uint)(rn), i;\n";
    4566           0 :     ss << "                for (i = 0; i < max && fSum >= alpha; i++)\n";
    4567           0 :     ss << "                {\n";
    4568           0 :     ss << " fFactor *= (rn - i) * pow((double)(i + 1),-1.0) *";
    4569           0 :     ss << " rq * pow(p, -1.0);\n";
    4570           0 :     ss << "                    fSum -= fFactor;\n";
    4571           0 :     ss << "                }\n";
    4572           0 :     ss << "                tmp = (rn - i);\n";
    4573           0 :     ss << "            }\n";
    4574           0 :     ss << "        }\n";
    4575           0 :     ss << "        else\n";
    4576           0 :     ss << "        {\n";
    4577           0 :     ss << "            double fSum = fFactor;\n";
    4578           0 :     ss << "            uint max = (uint)(rn), i;\n";
    4579           0 :     ss << "            for (i = 0; i < max && fSum < alpha; i++)\n";
    4580           0 :     ss << "            {\n";
    4581           0 :     ss << " fFactor *= (rn - i) * pow((double)(i + 1), -1.0) *";
    4582           0 :     ss << " p * pow(rq, -1.0);\n";
    4583           0 :     ss << "                fSum += fFactor;\n";
    4584           0 :     ss << "            }\n";
    4585           0 :     ss << "            tmp = (i);\n";
    4586           0 :     ss << "        }\n";
    4587           0 :     ss << "    }\n";
    4588           0 :     ss << "    return tmp;\n";
    4589           0 :     ss << "}";
    4590           0 : }
    4591             : 
    4592           0 : void OpRsq::GenSlidingWindowFunction(
    4593             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    4594             : {
    4595           0 :     if( vSubArguments.size() !=2 ||vSubArguments[0]->GetFormulaToken()
    4596           0 :         ->GetType() != formula::svDoubleVectorRef||vSubArguments[1]
    4597           0 :         ->GetFormulaToken()->GetType() != formula::svDoubleVectorRef )
    4598             :         ///only support DoubleVector in OpRsq for GPU calculating.
    4599           0 :         throw Unhandled();
    4600             :     const formula::DoubleVectorRefToken* pCurDVR1 =
    4601             :         static_cast<const formula::DoubleVectorRefToken *>(
    4602           0 :         vSubArguments[0]->GetFormulaToken());
    4603             :     const formula::DoubleVectorRefToken* pCurDVR2 =
    4604             :         static_cast<const formula::DoubleVectorRefToken *>(
    4605           0 :         vSubArguments[1]->GetFormulaToken());
    4606           0 :     if(  pCurDVR1->GetRefRowSize() != pCurDVR2->GetRefRowSize() )
    4607           0 :          throw Unhandled();
    4608             : 
    4609           0 :     size_t nCurWindowSize = pCurDVR1->GetRefRowSize();
    4610             : 
    4611           0 :     ss << "\ndouble " << sSymName;
    4612           0 :     ss << "_"<< BinFuncName() <<"(";
    4613           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4614             :     {
    4615           0 :         if (i)
    4616           0 :             ss << ",";
    4617           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4618             :     }
    4619           0 :     ss << ")\n";
    4620           0 :     ss << "{\n";
    4621           0 :     ss << "    int gid0=get_global_id(0);\n";
    4622           0 :     ss << "    double fCount = 0.0;\n";
    4623           0 :     ss << "    double fSumX = 0.0;\n";
    4624           0 :     ss << "    double fSumY = 0.0;\n";
    4625           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    4626           0 :     ss << "    double fInx;\n";
    4627           0 :     ss << "    double fIny;\n";
    4628           0 :     ss << "    double tmp0,tmp1;\n";
    4629           0 :     vSubArguments.size();
    4630           0 :     ss <<"\n";
    4631             : 
    4632           0 :     ss << "   for(int i=0; i<"<<nCurWindowSize<<"; i++)\n";
    4633           0 :     ss << "   {\n";
    4634           0 :     ss << "     if(isNan(";
    4635           0 :     ss << vSubArguments[0]->GenSlidingWindowDeclRef(true);
    4636           0 :     ss << "))\n";
    4637           0 :     ss << "         fInx = 0;\n";
    4638           0 :     ss << "     else\n";
    4639           0 :     ss << "        fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
    4640           0 :     ss << ";\n";
    4641           0 :     ss << "      if(isNan(";
    4642           0 :     ss << vSubArguments[1]->GenSlidingWindowDeclRef(true);
    4643           0 :     ss << "))\n";
    4644           0 :     ss << "          fIny = 0;\n";
    4645           0 :     ss << "      else\n";
    4646           0 :     ss << "        fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
    4647           0 :     ss << " ;\n";
    4648           0 :     ss << "      fSumX += fInx;\n";
    4649           0 :     ss << "      fSumY += fIny;\n";
    4650           0 :     ss << "      fCount = fCount + 1;\n";
    4651           0 :     ss << "    }\n";
    4652           0 :     ss << "    double fMeanX = fSumX / fCount;\n";
    4653           0 :     ss << "    double fMeanY = fSumY / fCount;\n";
    4654           0 :     ss << "    fSumX = 0.0;\n";
    4655           0 :     ss << "    fSumY = 0.0;\n";
    4656           0 :     ss << "    for(int i=0; i<"<<nCurWindowSize<<"; i++)\n";
    4657           0 :     ss << "    {\n";
    4658           0 :     ss << "     if(isNan(";
    4659           0 :     ss << vSubArguments[0]->GenSlidingWindowDeclRef(true);
    4660           0 :     ss << "))\n";
    4661           0 :     ss << "         fInx = 0;\n";
    4662           0 :     ss << "     else\n";
    4663           0 :     ss << "        fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
    4664           0 :     ss << ";\n";
    4665           0 :     ss << "      if(isNan(";
    4666           0 :     ss << vSubArguments[1]->GenSlidingWindowDeclRef();
    4667           0 :     ss << "))\n";
    4668           0 :     ss << "          fIny = 0;\n";
    4669           0 :     ss << "      else\n";
    4670           0 :     ss << "        fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
    4671           0 :     ss << " ;\n";
    4672           0 :     ss << "        fSumDeltaXDeltaY += (fInx - fMeanX) * (fIny - fMeanY);\n";
    4673           0 :     ss << "        fSumX    += pow(fInx - fMeanX,2);\n";
    4674           0 :     ss << "        fSumY    += pow(fIny - fMeanY,2);\n";
    4675           0 :     ss << "    }\n";
    4676           0 :     ss << "    double tmp = pow( fSumDeltaXDeltaY,2) / (fSumX * fSumY);\n";
    4677           0 :     ss << "    return tmp ;\n";
    4678           0 :     ss << "}\n";
    4679           0 : }
    4680             : 
    4681           0 : void OpChiInv::BinInlineFun(std::set<std::string>& decls,
    4682             :     std::set<std::string>& funs)
    4683             : {
    4684           0 :     decls.insert(fMachEpsDecl);
    4685           0 :     funs.insert("");
    4686           0 :     decls.insert(fBigInvDecl);
    4687           0 :     funs.insert("");
    4688           0 :     decls.insert(fHalfMachEpsDecl);
    4689           0 :     funs.insert("");
    4690           0 :     decls.insert(lcl_IterateInverseChiInvDecl);
    4691           0 :     funs.insert(lcl_IterateInverseChiInv);
    4692           0 :     decls.insert(GetChiDistDecl);
    4693           0 :     funs.insert(GetChiDist);
    4694           0 :     decls.insert(lcl_HasChangeOfSignDecl);
    4695           0 :     funs.insert(lcl_HasChangeOfSign);
    4696           0 :     decls.insert(GetUpRegIGammaDecl);
    4697           0 :     funs.insert(GetUpRegIGamma);
    4698           0 :     decls.insert(GetGammaContFractionDecl);
    4699           0 :     funs.insert(GetGammaContFraction);
    4700           0 :     decls.insert(GetGammaSeriesDecl);
    4701           0 :     funs.insert(GetGammaSeries);
    4702           0 : }
    4703           0 : void OpChiInv::GenSlidingWindowFunction(
    4704             :     std::stringstream &ss,const std::string &sSymName,
    4705             :     SubArguments &vSubArguments)
    4706             : {
    4707           0 :     ss << "\ndouble " << sSymName;
    4708           0 :     ss << "_"<< BinFuncName() <<"(";
    4709           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4710             :     {
    4711           0 :         if (i)
    4712           0 :             ss << ",";
    4713           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4714             :     }
    4715           0 :     ss << ")\n";
    4716           0 :     ss << "{\n";
    4717           0 :     ss << "    double tmp0,tmp1,tmp;\n";
    4718           0 :     ss << "    int gid0=get_global_id(0);\n";
    4719           0 :     size_t i = vSubArguments.size();
    4720           0 :     ss <<"\n    ";
    4721           0 :     for (i = 0; i < vSubArguments.size(); i++)
    4722             :     {
    4723           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4724             :         assert(pCur);
    4725           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    4726             :         {
    4727             :             const formula::DoubleVectorRefToken* pDVR =
    4728           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    4729           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    4730           0 :             ss << "for (int i = ";
    4731           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    4732             : #ifdef  ISNAN
    4733           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    4734           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    4735             : #else
    4736             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    4737             : #endif
    4738           0 :                 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    4739             : #ifdef  ISNAN
    4740           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    4741           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    4742             : #else
    4743             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    4744             : #endif
    4745           0 :                 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    4746             : #ifdef  ISNAN
    4747           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    4748           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    4749             : #else
    4750             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    4751             : #endif
    4752             :                 }
    4753             :                 else {
    4754             : #ifdef  ISNAN
    4755           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    4756             : #else
    4757             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    4758             : #endif
    4759             :                 }
    4760             :             }
    4761           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    4762             :             {
    4763             : #ifdef  ISNAN
    4764             :                 const formula::SingleVectorRefToken* pSVR =
    4765           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    4766           0 :                 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4767             : #else
    4768             : #endif
    4769             :             }
    4770           0 :             else if (pCur->GetType() == formula::svDouble)
    4771             :             {
    4772             : #ifdef  ISNAN
    4773           0 :                 ss << "{\n";
    4774             : #endif
    4775             :             }
    4776             :             else
    4777             :             {
    4778             : #ifdef  ISNAN
    4779             : #endif
    4780             :             }
    4781             : #ifdef  ISNAN
    4782           0 :             if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    4783             :             {
    4784           0 :                 ss << "if (isNan(";
    4785           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4786           0 :                 ss << "))\n";
    4787           0 :                 ss << "    tmp"<<i<<"= 0;\n";
    4788           0 :                 ss << "else\n";
    4789           0 :                 ss <<"tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4790           0 :                 ss << ";\n}\n";
    4791             :             }
    4792             :             else
    4793             :             {
    4794           0 :                ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4795           0 :                ss << ";\n";
    4796             :             }
    4797             : #else
    4798             :     ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4799             :     ss << ";\n";
    4800             : #endif
    4801             :             }
    4802           0 :     ss << "    tmp1 = floor(tmp1);";
    4803           0 :     ss << "    if (tmp1 < 1.0 || tmp0 <= 0.0 || tmp0 > 1.0 )\n";
    4804           0 :     ss << "    {\n";
    4805           0 :     ss << "        return DBL_MIN;\n";
    4806           0 :     ss << "    }\n";
    4807           0 :     ss << "    bool bConvError;\n";
    4808           0 :     ss << "    double fVal = lcl_IterateInverseChiInv";
    4809           0 :     ss << "(tmp0, tmp1, tmp1*0.5, tmp1, &bConvError);\n";
    4810           0 :     ss << "    if(bConvError)\n";
    4811           0 :     ss << "        return DBL_MIN;\n";
    4812           0 :     ss << "    return fVal;\n";
    4813           0 :     ss << "}\n";
    4814           0 : }
    4815           0 : void OpNormdist::GenSlidingWindowFunction(
    4816             :     std::stringstream &ss, const std::string &sSymName,
    4817             :     SubArguments &vSubArguments)
    4818             : {
    4819           0 :     ss << "\ndouble " << sSymName;
    4820           0 :     ss << "_"<< BinFuncName() <<"(";
    4821           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4822             :     {
    4823           0 :         if (i)
    4824           0 :             ss << ",";
    4825           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4826             :     }
    4827           0 :     ss << ")\n";
    4828           0 :     ss << "{\n";
    4829           0 :     ss << "    double x,mue,sigma,c;\n";
    4830           0 :     ss << "    int gid0=get_global_id(0);\n";
    4831           0 :     ss << "    double tmp0,tmp1,tmp2,tmp3;\n";
    4832           0 :     size_t i = vSubArguments.size();
    4833           0 :     ss <<"\n    ";
    4834           0 :     for (i = 0; i < vSubArguments.size(); i++)
    4835             :     {
    4836           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4837             :         assert(pCur);
    4838           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    4839             :         {
    4840             : #ifdef  ISNAN
    4841             :             const formula::SingleVectorRefToken* pSVR =
    4842           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    4843           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4844             : #endif
    4845             :         }
    4846           0 :         else if (pCur->GetType() == formula::svDouble)
    4847             :         {
    4848             : #ifdef  ISNAN
    4849           0 :             ss << "{\n";
    4850             : #endif
    4851             :         }
    4852             : #ifdef  ISNAN
    4853           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    4854             :         {
    4855           0 :             ss << "    if (isNan(";
    4856           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4857           0 :             ss << "))\n";
    4858           0 :             ss << "        tmp"<<i<<"= 0;\n";
    4859           0 :             ss << "    else\n";
    4860           0 :             ss << "        tmp"<<i<<"=\n";
    4861           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4862           0 :             ss << ";\n}\n";
    4863             :         }
    4864             :         else
    4865             :         {
    4866           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4867           0 :             ss <<";\n";
    4868             :         }
    4869             : #endif
    4870             :     }
    4871           0 :     ss << "x = tmp0;\n";
    4872           0 :     ss << "mue = tmp1;\n";
    4873           0 :     ss << "sigma = tmp2;\n";
    4874           0 :     ss << "c = tmp3;\n";
    4875           0 :     ss << "double mid,tmp;\n";
    4876           0 :     ss << "mid = (x - mue)/sigma;\n";
    4877           0 :     ss << "if(c)\n";
    4878           0 :     ss << "    tmp = 0.5 *erfc(-mid * 0.7071067811865475);\n";
    4879           0 :     ss << "else \n";
    4880           0 :     ss <<"     tmp=(0.39894228040143268*exp(-pow(mid,2)/2.0))/sigma;\n";
    4881           0 :     ss << "return tmp;\n";
    4882           0 :     ss << "}\n";
    4883           0 : }
    4884           0 : void OpNormsdist::BinInlineFun(std::set<std::string>& decls,
    4885             :     std::set<std::string>& funs)
    4886             : {
    4887           0 :     decls.insert(lcl_Erfc0600Decl);
    4888           0 :     funs.insert(lcl_Erfc0600);
    4889           0 :     decls.insert(lcl_Erfc2654Decl);
    4890           0 :     funs.insert(lcl_Erfc2654);
    4891           0 :     decls.insert(lcl_Erf0065Decl);
    4892           0 :     funs.insert(lcl_Erf0065);
    4893           0 :     decls.insert(rtl_math_erf_rdDecl);
    4894           0 :     funs.insert(rtl_math_erf_rd);
    4895           0 :     decls.insert(rtl_math_erfc_rdDecl);
    4896           0 :     funs.insert(rtl_math_erfc_rd);
    4897           0 : }
    4898             : 
    4899           0 : void OpNormsdist::GenSlidingWindowFunction(
    4900             :     std::stringstream &ss,const std::string &sSymName,
    4901             :     SubArguments &vSubArguments)
    4902             : {
    4903           0 :     ss << "\ndouble " << sSymName;
    4904           0 :     ss << "_"<< BinFuncName() <<"(";
    4905           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4906             :     {
    4907           0 :         if (i)
    4908           0 :             ss << ",";
    4909           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4910             :     }
    4911           0 :     ss << ")\n";
    4912           0 :     ss << "{\n";
    4913           0 :     ss << "    double x = 0,tmp0 = 0;\n";
    4914           0 :     ss << "    int gid0=get_global_id(0);\n";
    4915           0 :     size_t i = vSubArguments.size();
    4916           0 :     ss <<"\n    ";
    4917           0 :     for (i = 0; i < vSubArguments.size(); i++)
    4918             :     {
    4919           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4920             :         assert(pCur);
    4921           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    4922             :         {
    4923             : #ifdef  ISNAN
    4924             :             const formula::SingleVectorRefToken* pSVR =
    4925           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    4926           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4927             : #endif
    4928             :         }
    4929           0 :         else if (pCur->GetType() == formula::svDouble)
    4930             :         {
    4931             : #ifdef  ISNAN
    4932           0 :             ss << "{\n";
    4933             : #endif
    4934             :         }
    4935             :         else
    4936             :         {
    4937             : #ifdef  ISNAN
    4938             : #endif
    4939             :         }
    4940             : #ifdef  ISNAN
    4941           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    4942             :         {
    4943           0 :             ss << "    if (isNan(";
    4944           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4945           0 :             ss << "))\n";
    4946           0 :             ss << "        tmp"<<i<<"= 0;\n";
    4947           0 :             ss << "    else\n";
    4948           0 :             ss << "        tmp"<<i<<"=\n";
    4949           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4950           0 :             ss << ";\n}\n";
    4951             :         }
    4952             :         else
    4953             :         {
    4954           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4955           0 :             ss <<";\n";
    4956             :         }
    4957             : #endif
    4958             :     }
    4959           0 :     ss << "    x = tmp0;\n";
    4960           0 :     ss << "    double tmp = 0.5 * rtl_math_erfc_rd((-1)*x * 0.7071067811865475);\n";
    4961           0 :     ss << "    return tmp;\n";
    4962           0 :     ss << "}\n";
    4963           0 : }
    4964             : 
    4965           0 : void OpVariationen::GenSlidingWindowFunction(
    4966             :     std::stringstream &ss,const std::string &sSymName,
    4967             :     SubArguments &vSubArguments)
    4968             : {
    4969           0 :     ss << "\ndouble " << sSymName;
    4970           0 :     ss << "_"<< BinFuncName() <<"(";
    4971           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4972             :     {
    4973           0 :        if (i)
    4974           0 :          ss << ",";
    4975           0 :        vSubArguments[i]->GenSlidingWindowDecl(ss);
    4976             :      }
    4977           0 :     ss << ") {\n";
    4978           0 :     ss <<"    int gid0=get_global_id(0);\n";
    4979           0 :     ss <<"    double inA;\n";
    4980           0 :     ss <<"    double inB;\n";
    4981           0 :     ss <<"    double tmp0,tmp1;\n";
    4982           0 :     ss <<"    double tmp = 1 ;\n";
    4983           0 :     size_t i = vSubArguments.size();
    4984           0 :     ss <<"\n";
    4985           0 :     for (i = 0; i < vSubArguments.size(); i++)
    4986             :     {
    4987           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4988             :         assert(pCur);
    4989           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    4990             :         {
    4991             : #ifdef  ISNAN
    4992             :             const formula::SingleVectorRefToken* pSVR =
    4993           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    4994           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4995             : 
    4996             : #endif
    4997             :         }
    4998           0 :         else if (pCur->GetType() == formula::svDouble)
    4999             :         {
    5000             : #ifdef  ISNAN
    5001           0 :             ss << "{\n";
    5002             : #endif
    5003             :         }
    5004             :         else
    5005             :         {
    5006             : #ifdef  ISNAN
    5007             : #endif
    5008             :         }
    5009             : #ifdef  ISNAN
    5010           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    5011             :         {
    5012           0 :             ss << "    if (isNan(";
    5013           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5014           0 :             ss << "))\n";
    5015           0 :             ss << "        tmp"<<i<<"= 0;\n";
    5016           0 :             ss << "    else\n";
    5017           0 :             ss << "        tmp"<<i<<"=\n";
    5018           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5019           0 :             ss << ";\n}\n";
    5020             :         }
    5021             :         else
    5022             :         {
    5023           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    5024           0 :             ss <<";\n";
    5025             :         }
    5026             : #endif
    5027             :     }
    5028           0 :     ss << "      inA = tmp0;\n";
    5029           0 :     ss << "      inB = tmp1;\n";
    5030           0 :     ss << "      for( int i =0; i<inB; i++)\n";
    5031           0 :     ss << "      {\n";
    5032           0 :     ss << "        tmp *= inA ;\n";
    5033           0 :     ss << "        inA = inA - 1.0;\n";
    5034           0 :     ss << "      }\n";
    5035           0 :     ss << "      return tmp;\n";
    5036           0 :     ss << "}\n";
    5037           0 : }
    5038           0 : void OpVariationen2::GenSlidingWindowFunction(
    5039             :     std::stringstream &ss,const std::string &sSymName,
    5040             :     SubArguments &vSubArguments)
    5041             : {
    5042           0 :     ss << "\ndouble " << sSymName;
    5043           0 :     ss << "_"<< BinFuncName() <<"(";
    5044           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5045             :     {
    5046           0 :        if (i)
    5047           0 :          ss << ",";
    5048           0 :        vSubArguments[i]->GenSlidingWindowDecl(ss);
    5049             :     }
    5050           0 :     ss << ") {\n";
    5051           0 :     ss <<"    int gid0=get_global_id(0);\n";
    5052           0 :     ss <<"    double inA;\n";
    5053           0 :     ss <<"    double inB;\n";
    5054           0 :     ss <<"    double tmp = 1.0;\n";
    5055             : #ifdef ISNAN
    5056           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    5057             :     const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
    5058           0 :     formula::SingleVectorRefToken *>(tmpCur0);
    5059           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    5060             :     const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
    5061           0 :     formula::SingleVectorRefToken *>(tmpCur1);
    5062           0 :     ss << "int buffer_fIna_len = ";
    5063           0 :     ss << tmpCurDVR0->GetArrayLength();
    5064           0 :     ss << ";\n";
    5065           0 :     ss << "    int buffer_fInb_len = ";
    5066           0 :     ss << tmpCurDVR1->GetArrayLength();
    5067           0 :     ss << ";\n";
    5068             : #endif
    5069             : #ifdef ISNAN
    5070           0 :     ss << "    if((gid0)>=buffer_fIna_len || isNan(";
    5071           0 :     ss << vSubArguments[0]->GenSlidingWindowDeclRef();
    5072           0 :     ss << "))\n";
    5073           0 :     ss << "    inA = 0;\nelse \n";
    5074             : #endif
    5075           0 :     ss << "        inA = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
    5076           0 :     ss << ";\n";
    5077             : #ifdef ISNAN
    5078           0 :     ss << "if((gid0)>=buffer_fInb_len || isNan(";
    5079           0 :     ss << vSubArguments[1]->GenSlidingWindowDeclRef();
    5080           0 :     ss << "))\n";
    5081           0 :     ss << "inB = 0;\nelse \n";
    5082             : #endif
    5083           0 :     ss << "    inB = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
    5084           0 :     ss << ";\n";
    5085           0 :     ss << " for(int i=0; i<inB; i++)\n";
    5086           0 :     ss << " {\n";
    5087           0 :     ss << "     tmp *= inA;\n";
    5088           0 :     ss << " }\n";
    5089           0 :     ss << "    return tmp;\n";
    5090           0 :     ss << "}\n";
    5091           0 : }
    5092             : 
    5093           0 : void OpPhi::GenSlidingWindowFunction(
    5094             :     std::stringstream &ss,const std::string &sSymName,
    5095             :     SubArguments &vSubArguments)
    5096             : {
    5097           0 :     ss << "\ndouble " << sSymName;
    5098           0 :     ss << "_"<< BinFuncName() <<"(";
    5099           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5100             :     {
    5101           0 :         if (i)
    5102           0 :             ss << ",";
    5103           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5104             :     }
    5105           0 :     ss << ")\n";
    5106           0 :     ss << "{\n";
    5107           0 :     ss << "    double x,tmp0;\n";
    5108           0 :     ss << "    int gid0=get_global_id(0);\n";
    5109           0 :     size_t i = vSubArguments.size();
    5110           0 :     ss <<"\n";
    5111           0 :     for (i = 0; i < vSubArguments.size(); i++)
    5112             :     {
    5113           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5114             :         assert(pCur);
    5115           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    5116             :         {
    5117             : #ifdef  ISNAN
    5118             :             const formula::SingleVectorRefToken* pSVR =
    5119           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    5120           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    5121             : #endif
    5122             :         }
    5123           0 :         else if (pCur->GetType() == formula::svDouble)
    5124             :         {
    5125             : #ifdef  ISNAN
    5126           0 :             ss << "{\n";
    5127             : #endif
    5128             :         }
    5129             :         else
    5130             :         {
    5131             : #ifdef  ISNAN
    5132             : #endif
    5133             :         }
    5134             : #ifdef  ISNAN
    5135           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    5136             :         {
    5137           0 :             ss << "    if (isNan(";
    5138           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5139           0 :             ss << "))\n";
    5140           0 :             ss << "        tmp"<<i<<"= 0;\n";
    5141           0 :             ss << "    else\n";
    5142           0 :             ss << "        tmp"<<i<<"=\n";
    5143           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5144           0 :             ss << ";\n}\n";
    5145             :         }
    5146             :         else
    5147             :         {
    5148           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    5149           0 :             ss <<";\n";
    5150             :         }
    5151             : #endif
    5152             :     }
    5153           0 :     ss << "    x = tmp0;\n";
    5154           0 :     ss << "    double tmp = 0.39894228040143268 * exp((-1)*pow(x,2) / 2.0);\n";
    5155           0 :     ss << "     return tmp;\n";
    5156           0 :     ss << "}\n";
    5157           0 : }
    5158             : 
    5159           0 : void OpNorminv::GenSlidingWindowFunction(
    5160             :     std::stringstream &ss,const std::string &sSymName,
    5161             :     SubArguments &vSubArguments)
    5162             : {
    5163           0 :     ss << "\ndouble " << sSymName;
    5164           0 :     ss << "_"<< BinFuncName() <<"(";
    5165           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5166             :     {
    5167           0 :         if (i)
    5168           0 :             ss << ",";
    5169           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5170             :     }
    5171           0 :     ss << ")\n";
    5172           0 :     ss << "{\n";
    5173           0 :     ss <<"    double q,t,z;\n";
    5174           0 :     ss <<"    double x,mue,sigma;\n";
    5175           0 :     ss <<"    double tmp0,tmp1,tmp2;\n";
    5176           0 :     ss <<"    int gid0=get_global_id(0);\n";
    5177           0 :     size_t i = vSubArguments.size();
    5178           0 :     ss <<"\n";
    5179           0 :     for (i = 0; i < vSubArguments.size(); i++)
    5180             :     {
    5181           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5182             :         assert(pCur);
    5183           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    5184             :         {
    5185             : #ifdef  ISNAN
    5186             :             const formula::SingleVectorRefToken* pSVR =
    5187           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    5188           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    5189             : #endif
    5190             :         }
    5191           0 :         else if (pCur->GetType() == formula::svDouble)
    5192             :         {
    5193             : #ifdef  ISNAN
    5194           0 :             ss << "{\n";
    5195             : #endif
    5196             :         }
    5197             :         else
    5198             :         {
    5199             : #ifdef  ISNAN
    5200             : #endif
    5201             :         }
    5202             : #ifdef  ISNAN
    5203           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    5204             :         {
    5205           0 :             ss << "    if (isNan(";
    5206           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5207           0 :             ss << "))\n";
    5208           0 :             ss << "        tmp"<<i<<"= 0;\n";
    5209           0 :             ss << "    else\n";
    5210           0 :             ss << "        tmp"<<i<<"=\n";
    5211           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5212           0 :             ss << ";\n}\n";
    5213             :         }
    5214             :         else
    5215             :         {
    5216           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    5217           0 :             ss <<";\n";
    5218             :         }
    5219             : #endif
    5220             :     }
    5221           0 :     ss <<"    x = tmp0;\n";
    5222           0 :     ss <<"    mue = tmp1;\n";
    5223           0 :     ss <<"    sigma = tmp2;\n";
    5224           0 :     ss <<"    q = x -0.5;\n";
    5225           0 :     ss <<"    if(fabs(q)<=.425)\n";
    5226           0 :     ss <<"   {\n";
    5227           0 :     ss <<"        t=0.180625-pow(q,2);\n";
    5228             :     ss <<"        z=\n"
    5229             :     "q*\n"
    5230             :     "(\n"
    5231             :             "(\n"
    5232             :                 "(\n"
    5233             :                     "(\n"
    5234             :                         "(\n"
    5235             :                             "(\n"
    5236             :                                 "(\n"
    5237           0 :                                     "t*2509.0809287301226727";
    5238             :     ss <<"+33430.575583588128105\n"
    5239             :                                 ")\n"
    5240             :                                 "*t+67265.770927008700853\n"
    5241             :                             ")\n"
    5242             :                             "*t+45921.953931549871457\n"
    5243             :                         ")\n"
    5244             :                         "*t+13731.693765509461125\n"
    5245             :                     ")\n"
    5246             :                     "*t+1971.5909503065514427\n"
    5247             :                 ")\n"
    5248             :                 "*t+133.14166789178437745\n"
    5249             :             ")\n"
    5250             :             "*t+3.387132872796366608\n"
    5251             :         ")\n"
    5252             :         "/\n"
    5253             :         "(\n"
    5254             :             "(\n"
    5255             :                 "(\n"
    5256             :                     "(\n"
    5257             :                         "(\n"
    5258             :                             "(\n"
    5259             :                                 "(\n"
    5260           0 :                                     "t*5226.495278852854561";
    5261             :     ss <<"+28729.085735721942674\n"
    5262             :                                 ")\n"
    5263             :                                 "*t+39307.89580009271061\n"
    5264             :                             ")\n"
    5265             :                             "*t+21213.794301586595867\n"
    5266             :                         ")\n"
    5267             :                         "*t+5394.1960214247511077\n"
    5268             :                     ")\n"
    5269             :                     "*t+687.1870074920579083\n"
    5270             :                 ")\n"
    5271             :                 "*t+42.313330701600911252\n"
    5272             :             ")\n"
    5273             :             "*t+1.0\n"
    5274           0 :     ");\n";
    5275           0 :     ss <<"}\nelse\n{\n";
    5276           0 :     ss <<" if(q>0)\nt=1-x;\n";
    5277           0 :     ss <<"else\nt=x;\n";
    5278           0 :     ss <<"t=sqrt(-log(t));\n";
    5279           0 :     ss <<"if(t<=5.0)\n{\n";
    5280           0 :     ss <<"t+=-1.6;\n";
    5281             :     ss <<"z=\n"
    5282             :             "(\n"
    5283             :                 "(\n"
    5284             :                     "(\n"
    5285             :                         "(\n"
    5286             :                             "(\n"
    5287             :                                "(\n"
    5288             :                                    "(\n"
    5289           0 :                                         "t*7.7454501427834140764e-4";
    5290             :     ss <<"+0.0227238449892691845833\n"
    5291             :                                     ")\n"
    5292             :                                     "*t+0.24178072517745061177\n"
    5293             :                                 ")\n"
    5294             :                                 "*t+1.27045825245236838258\n"
    5295             :                             ")\n"
    5296             :                             "*t+3.64784832476320460504\n"
    5297             :                         ")\n"
    5298             :                         "*t+5.7694972214606914055\n"
    5299             :                     ")\n"
    5300             :                     "*t+4.6303378461565452959\n"
    5301             :                 ")\n"
    5302             :                 "*t+1.42343711074968357734\n"
    5303             :             ")\n"
    5304             :             "/\n"
    5305             :             "(\n"
    5306             :                 "(\n"
    5307             :                     "(\n"
    5308             :                         "(\n"
    5309             :                             "(\n"
    5310             :                                 "(\n"
    5311             :                                     "(\n"
    5312           0 :                                         "t*1.05075007164441684324e-9";
    5313             :     ss <<"+5.475938084995344946e-4\n"
    5314             :                                     ")\n"
    5315             :                                     "*t+0.0151986665636164571966\n"
    5316             :                                 ")\n"
    5317             :                                 "*t+0.14810397642748007459\n"
    5318             :                             ")\n"
    5319             :                             "*t+0.68976733498510000455\n"
    5320             :                         ")\n"
    5321             :                         "*t+1.6763848301838038494\n"
    5322             :                     ")\n"
    5323             :                     "*t+2.05319162663775882187\n"
    5324             :                 ")\n"
    5325             :                 "*t+1.0\n"
    5326           0 :             ");\n}\n";
    5327           0 :     ss <<"else\n{\n";
    5328           0 :     ss <<"t+=-5.0;\n";
    5329             :     ss <<"z=\n"
    5330             :             "(\n"
    5331             :                 "(\n"
    5332             :                     "(\n"
    5333             :                         "(\n"
    5334             :                             "(\n"
    5335             :                                 "(\n"
    5336             :                                     "(\n"
    5337           0 :                                         "t*2.01033439929228813265e-7";
    5338             :     ss<<"+2.71155556874348757815e-5\n"
    5339             :                                     ")\n"
    5340             :                                     "*t+0.0012426609473880784386\n"
    5341             :                                 ")\n"
    5342             :                                 "*t+0.026532189526576123093\n"
    5343             :                             ")\n"
    5344             :                             "*t+0.29656057182850489123\n"
    5345             :                         ")\n"
    5346             :                         "*t+1.7848265399172913358\n"
    5347             :                     ")\n"
    5348             :                     "*t+5.4637849111641143699\n"
    5349             :                 ")\n"
    5350             :                 "*t+6.6579046435011037772\n"
    5351             :             ")\n"
    5352             :             "/\n"
    5353             :             "(\n"
    5354             :                 "(\n"
    5355             :                     "(\n"
    5356             :                         "(\n"
    5357             :                             "(\n"
    5358             :                                 "(\n"
    5359             :                                     "(\n"
    5360             :                                         "t*2.04426310338993978564e-15"
    5361             :           "+1.4215117583164458887e-7\n"
    5362             :                                     ")\n"
    5363             :                                     "*t+1.8463183175100546818e-5\n"
    5364             :                                 ")\n"
    5365             :                                 "*t+7.868691311456132591e-4\n"
    5366             :                             ")\n"
    5367             :                             "*t+0.0148753612908506148525\n"
    5368             :                         ")\n"
    5369             :                         "*t+0.13692988092273580531\n"
    5370             :                     ")\n"
    5371             :                     "*t+0.59983220655588793769\n"
    5372             :                 ")\n"
    5373             :                 "*t+1.0\n"
    5374           0 :             ");\n";
    5375           0 :     ss<<"}\n";
    5376           0 :     ss << "z = q < 0.0 ? (-1)*z : z;\n";
    5377           0 :     ss<<"}\n";
    5378           0 :     ss<<"double tmp =  z*sigma + mue;\n";
    5379           0 :     ss<<"return tmp;\n";
    5380           0 :     ss<<"}\n";
    5381           0 : }
    5382           0 : void OpNormsinv:: GenSlidingWindowFunction
    5383             :     (std::stringstream &ss,const std::string &sSymName,
    5384             :     SubArguments &vSubArguments)
    5385             : {
    5386           0 :     ss << "\ndouble " << sSymName;
    5387           0 :     ss << "_"<< BinFuncName() <<"(";
    5388           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5389             :     {
    5390           0 :         if (i)
    5391           0 :             ss << ",";
    5392           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5393             :     }
    5394           0 :     ss << ")\n";
    5395           0 :     ss << "{\n";
    5396           0 :     ss << "    double q,t,z,x,tmp0;\n";
    5397           0 :     ss << "    int gid0=get_global_id(0);\n";
    5398           0 :     size_t i = vSubArguments.size();
    5399           0 :     ss <<"\n";
    5400           0 :     for (i = 0; i < vSubArguments.size(); i++)
    5401             :     {
    5402           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5403             :         assert(pCur);
    5404           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    5405             :         {
    5406             : #ifdef  ISNAN
    5407             :             const formula::SingleVectorRefToken* pSVR =
    5408           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    5409           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    5410             : #endif
    5411             :         }
    5412           0 :         else if (pCur->GetType() == formula::svDouble)
    5413             :         {
    5414             : #ifdef  ISNAN
    5415           0 :             ss << "{\n";
    5416             : #endif
    5417             :         }
    5418             :         else
    5419             :         {
    5420             : #ifdef  ISNAN
    5421             : #endif
    5422             :         }
    5423             : #ifdef  ISNAN
    5424           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    5425             :         {
    5426           0 :             ss << "    if (isNan(";
    5427           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5428           0 :             ss << "))\n";
    5429           0 :             ss << "        tmp"<<i<<"= 0;\n";
    5430           0 :             ss << "    else\n";
    5431           0 :             ss << "        tmp"<<i<<"=\n";
    5432           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5433           0 :             ss << ";\n}\n";
    5434             :         }
    5435             :         else
    5436             :         {
    5437           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    5438           0 :             ss <<";\n";
    5439             :         }
    5440             : #endif
    5441             :     }
    5442           0 :     ss <<"    x = tmp0;\n";
    5443           0 :     ss <<"    q = x -0.5;\n";
    5444           0 :     ss <<"    if(fabs(q)<=.425)\n";
    5445           0 :     ss <<"    {\n";
    5446           0 :     ss <<"        t=0.180625-pow(q,2);\n";
    5447             :     ss <<"        z=\n"
    5448             :          "q*\n"
    5449             :          "(\n"
    5450             :             "(\n"
    5451             :                 "(\n"
    5452             :                     "(\n"
    5453             :                         "(\n"
    5454             :                             "(\n"
    5455             :                                 "(\n"
    5456           0 :                                     "t*2509.0809287301226727";
    5457             :     ss <<"+33430.575583588128105\n"
    5458             :                                 ")\n"
    5459             :                                 "*t+67265.770927008700853\n"
    5460             :                             ")\n"
    5461             :                             "*t+45921.953931549871457\n"
    5462             :                         ")\n"
    5463             :                         "*t+13731.693765509461125\n"
    5464             :                     ")\n"
    5465             :                     "*t+1971.5909503065514427\n"
    5466             :                 ")\n"
    5467             :                 "*t+133.14166789178437745\n"
    5468             :             ")\n"
    5469             :             "*t+3.387132872796366608\n"
    5470             :         ")\n"
    5471             :         "/\n"
    5472             :         "(\n"
    5473             :             "(\n"
    5474             :                 "(\n"
    5475             :                     "(\n"
    5476             :                         "(\n"
    5477             :                             "(\n"
    5478             :                                 "(\n"
    5479           0 :                                     "t*5226.495278852854561";
    5480             :     ss <<"+28729.085735721942674\n"
    5481             :                                 ")\n"
    5482             :                                 "*t+39307.89580009271061\n"
    5483             :                             ")\n"
    5484             :                             "*t+21213.794301586595867\n"
    5485             :                         ")\n"
    5486             :                         "*t+5394.1960214247511077\n"
    5487             :                     ")\n"
    5488             :                     "*t+687.1870074920579083\n"
    5489             :                 ")\n"
    5490             :                 "*t+42.313330701600911252\n"
    5491             :             ")\n"
    5492             :             "*t+1.0\n"
    5493           0 :          ");\n";
    5494           0 :     ss <<"}\nelse\n{\n";
    5495           0 :     ss <<" if(q>0)\nt=1-x;\n";
    5496           0 :     ss <<"else\nt=x;\n";
    5497           0 :     ss <<"t=sqrt(-log(t));\n";
    5498           0 :     ss <<"if(t<=5.0)\n{\n";
    5499           0 :     ss <<"t+=-1.6;\n";
    5500             :     ss <<"z=\n"
    5501             :             "(\n"
    5502             :                 "(\n"
    5503             :                     "(\n"
    5504             :                         "(\n"
    5505             :                             "(\n"
    5506             :                                "(\n"
    5507             :                                    "(\n"
    5508           0 :                                         "t*7.7454501427834140764e-4";
    5509             :     ss <<"+0.0227238449892691845833\n"
    5510             :                                     ")\n"
    5511             :                                     "*t+0.24178072517745061177\n"
    5512             :                                 ")\n"
    5513             :                                 "*t+1.27045825245236838258\n"
    5514             :                             ")\n"
    5515             :                             "*t+3.64784832476320460504\n"
    5516             :                         ")\n"
    5517             :                         "*t+5.7694972214606914055\n"
    5518             :                     ")\n"
    5519             :                     "*t+4.6303378461565452959\n"
    5520             :                 ")\n"
    5521             :                 "*t+1.42343711074968357734\n"
    5522             :             ")\n"
    5523             :             "/\n"
    5524             :             "(\n"
    5525             :                 "(\n"
    5526             :                     "(\n"
    5527             :                         "(\n"
    5528             :                             "(\n"
    5529             :                                 "(\n"
    5530             :                                     "(\n"
    5531           0 :                                         "t*1.05075007164441684324e-9";
    5532             :     ss <<"+5.475938084995344946e-4\n"
    5533             :                                     ")\n"
    5534             :                                     "*t+0.0151986665636164571966\n"
    5535             :                                 ")\n"
    5536             :                                 "*t+0.14810397642748007459\n"
    5537             :                             ")\n"
    5538             :                             "*t+0.68976733498510000455\n"
    5539             :                         ")\n"
    5540             :                         "*t+1.6763848301838038494\n"
    5541             :                     ")\n"
    5542             :                     "*t+2.05319162663775882187\n"
    5543             :                 ")\n"
    5544             :                 "*t+1.0\n"
    5545           0 :             ");\n}\n";
    5546           0 :     ss <<"else\n{\n";
    5547           0 :     ss <<"t+=-5.0;\n";
    5548             :     ss <<"z=\n"
    5549             :             "(\n"
    5550             :                 "(\n"
    5551             :                     "(\n"
    5552             :                         "(\n"
    5553             :                             "(\n"
    5554             :                                 "(\n"
    5555             :                                     "(\n"
    5556           0 :                                         "t*2.01033439929228813265e-7";
    5557             :     ss <<"+2.71155556874348757815e-5\n"
    5558             :                                     ")\n"
    5559             :                                     "*t+0.0012426609473880784386\n"
    5560             :                                 ")\n"
    5561             :                                 "*t+0.026532189526576123093\n"
    5562             :                             ")\n"
    5563             :                             "*t+0.29656057182850489123\n"
    5564             :                         ")\n"
    5565             :                         "*t+1.7848265399172913358\n"
    5566             :                     ")\n"
    5567             :                     "*t+5.4637849111641143699\n"
    5568             :                 ")\n"
    5569             :                 "*t+6.6579046435011037772\n"
    5570             :             ")\n"
    5571             :             "/\n"
    5572             :             "(\n"
    5573             :                 "(\n"
    5574             :                     "(\n"
    5575             :                         "(\n"
    5576             :                             "(\n"
    5577             :                                 "(\n"
    5578             :                                     "(\n"
    5579             :                                         "t*2.04426310338993978564e-15"
    5580             :           "+1.4215117583164458887e-7\n"
    5581             :                                     ")\n"
    5582             :                                     "*t+1.8463183175100546818e-5\n"
    5583             :                                 ")\n"
    5584             :                                 "*t+7.868691311456132591e-4\n"
    5585             :                             ")\n"
    5586             :                             "*t+0.0148753612908506148525\n"
    5587             :                         ")\n"
    5588             :                         "*t+0.13692988092273580531\n"
    5589             :                     ")\n"
    5590             :                     "*t+0.59983220655588793769\n"
    5591             :                 ")\n"
    5592             :                 "*t+1.0\n"
    5593           0 :             ");\n";
    5594           0 :     ss <<"}\n";
    5595           0 :     ss << "z = q < 0.0 ? (-1)*z : z;\n";
    5596           0 :     ss <<"}\n";
    5597           0 :     ss <<"double tmp = z;\n";
    5598           0 :     ss <<"return tmp;\n";
    5599           0 :     ss <<"}\n";
    5600           0 : }
    5601           0 : void OpMedian::GenSlidingWindowFunction(
    5602             :     std::stringstream &ss, const std::string &sSymName,
    5603             :     SubArguments &vSubArguments)
    5604             : {
    5605           0 :     ss << "\ndouble " << sSymName;
    5606           0 :     ss << "_"<< BinFuncName() <<"(";
    5607           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5608             :     {
    5609           0 :         if (i)
    5610           0 :             ss << ",";
    5611           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5612             :     }
    5613           0 :     ss << ") {\n";
    5614           0 :     ss << "    int gid0 = get_global_id(0);\n";
    5615           0 :     ss << "    double tmp = 0;\n";
    5616           0 :     ss << "    int i;\n";
    5617           0 :     ss << "    unsigned int startFlag = 0;\n";
    5618           0 :     ss << "    unsigned int endFlag = 0;\n";
    5619           0 :     ss << "    double dataIna;\n";
    5620           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5621             :     {
    5622           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5623             :         assert(pCur);
    5624           0 :         if (const formula::DoubleVectorRefToken* pCurDVR =
    5625           0 :             dynamic_cast<const formula::DoubleVectorRefToken *>(pCur))
    5626             :         {
    5627           0 :             size_t nCurWindowSize = pCurDVR->GetRefRowSize();
    5628           0 :             ss << "startFlag = ";
    5629           0 :             if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
    5630             :             {
    5631           0 :                 ss << "gid0; endFlag = "<< nCurWindowSize <<"-gid0;\n";
    5632             :             }
    5633           0 :             ss << "gid0; endFlag = gid0+"<< nCurWindowSize <<";\n";
    5634             :         }
    5635             :         else
    5636             :         {
    5637           0 :             ss<<"startFlag=gid0;endFlag=gid0;\n";
    5638             :         }
    5639             :     }
    5640             : #ifdef ISNAN
    5641           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    5642             :     const formula::DoubleVectorRefToken*tmpCurDVR0= static_cast<const
    5643           0 :     formula::DoubleVectorRefToken *>(tmpCur0);
    5644           0 :     ss << "int buffer_fIna_len = ";
    5645           0 :     ss << tmpCurDVR0->GetArrayLength();
    5646           0 :     ss << ";\n";
    5647             : #endif
    5648             : #ifdef ISNAN
    5649           0 :     ss<<"if((i+gid0)>=buffer_fIna_len || isNan(";
    5650           0 :     ss << vSubArguments[0]->GenSlidingWindowDeclRef();
    5651           0 :     ss<<"))\n";
    5652           0 :     ss<<"    dataIna = 0;\n";
    5653             : #endif
    5654           0 :     ss << "    int nSize =endFlag- startFlag ;\n";
    5655           0 :     ss << "    if (nSize & 1)\n";
    5656           0 :     ss << "    {\n";
    5657           0 :     ss << "        tmp = "<<vSubArguments[0]->GetName();
    5658           0 :     ss << "        [startFlag+nSize/2];\n";
    5659           0 :     ss << "    }\n";
    5660           0 :     ss << "    else\n";
    5661           0 :     ss << "    {\n";
    5662           0 :     ss << "        tmp =("<<vSubArguments[0]->GetName();
    5663           0 :     ss << "        [startFlag+nSize/2]+";
    5664           0 :     ss <<          vSubArguments[0]->GetName();
    5665           0 :     ss << "        [startFlag+nSize/2-1])/2;\n";
    5666           0 :     ss << "    }\n";
    5667           0 :     ss <<"     return tmp;\n";
    5668           0 :     ss << "}\n";
    5669           0 : }
    5670           0 : void OpKurt:: GenSlidingWindowFunction(std::stringstream &ss,
    5671             :             const std::string &sSymName, SubArguments &vSubArguments)
    5672             : {
    5673           0 :     ss << "\ndouble " << sSymName;
    5674           0 :     ss << "_"<< BinFuncName() <<"( ";
    5675           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5676             :     {
    5677           0 :         if (i)
    5678           0 :             ss << ",";
    5679           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5680             :     }
    5681           0 :     ss << ")\n";
    5682           0 :     ss <<"{\n";
    5683           0 :     ss << "    int gid0 = get_global_id(0);\n";
    5684           0 :     ss << "    double fSum = 0.0;\n";
    5685           0 :     ss << "    double vSum = 0.0;\n";
    5686           0 :     ss << "    double length;\n";
    5687           0 :     ss << "    double totallength=0;\n";
    5688           0 :     ss << "    double tmp = 0;\n";
    5689           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5690             :     {
    5691           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5692             :         assert(pCur);
    5693           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    5694             :         {
    5695             :             const formula::DoubleVectorRefToken* pDVR =
    5696           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    5697           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    5698           0 :             ss << "    length="<<nCurWindowSize;
    5699           0 :             ss << ";\n";
    5700           0 :             ss << "    for (int i = ";
    5701           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    5702           0 :             ss << "    {\n";
    5703           0 :             ss << "        double arg"<<i<<" = ";
    5704           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef(true);
    5705           0 :             ss << ";\n";
    5706             : #ifdef ISNAN
    5707           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
    5708           0 :             ss << pDVR->GetArrayLength();
    5709           0 :             ss << "))\n";
    5710           0 :             ss << "        {\n";
    5711           0 :             ss << "            length-=1.0;\n";
    5712           0 :             ss << "            continue;\n";
    5713           0 :             ss << "        }\n";
    5714             : #endif
    5715           0 :             ss << "        fSum +=  arg"<<i<<";\n";
    5716           0 :             ss << "    }\n";
    5717           0 :             ss << "    totallength +=length;\n";
    5718             :         }
    5719           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    5720             :         {
    5721           0 :             ss << "    tmp = ";
    5722           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5723           0 :             ss << ";\n";
    5724             : #ifdef ISNAN
    5725           0 :             ss << "    if(!isNan(tmp))\n";
    5726           0 :             ss << "    {\n";
    5727           0 :             ss << "        fSum += tmp;\n";
    5728           0 :             ss << "        totallength +=1;\n";
    5729           0 :             ss << "    }\n";
    5730             : #endif
    5731             :         }
    5732           0 :         else if (pCur->GetType() == formula::svDouble)
    5733             :         {
    5734           0 :            ss << "    tmp = ";
    5735           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5736           0 :            ss << ";\n";
    5737           0 :            ss << "    fSum += tmp;\n";
    5738           0 :            ss << "    totallength +=1;\n";
    5739             :         }
    5740             :         else
    5741             :         {
    5742           0 :             ss << "    return DBL_MIN;\n";
    5743             :         }
    5744             :     }
    5745           0 :     ss << "    double fMean = fSum * pow(totallength,-1);\n";
    5746           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5747             :     {
    5748           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5749             :         assert(pCur);
    5750           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    5751             :         {
    5752             :             const formula::DoubleVectorRefToken* pDVR =
    5753           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    5754           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    5755           0 :             ss << "    for (int i = ";
    5756           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    5757           0 :             ss << "    {\n";
    5758           0 :             ss << "        double arg"<<i<<" = ";
    5759           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef(true);
    5760           0 :             ss << ";\n";
    5761             : #ifdef ISNAN
    5762           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
    5763           0 :             ss << pDVR->GetArrayLength();
    5764           0 :             ss << "))\n";
    5765           0 :             ss << "        {\n";
    5766           0 :             ss << "            continue;\n";
    5767           0 :             ss << "        }\n";
    5768             : #endif
    5769           0 :             ss << "        vSum +=  (arg"<<i<<"-fMean)*(arg"<<i<<"-fMean);\n";
    5770           0 :             ss << "    }\n";
    5771             :         }
    5772           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    5773             :         {
    5774           0 :             ss << "    tmp = ";
    5775           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5776           0 :             ss << ";\n";
    5777             : #ifdef ISNAN
    5778           0 :             ss << "    if(!isNan(tmp))\n";
    5779           0 :             ss << "    {\n";
    5780           0 :             ss << "        vSum += (tmp-fMean)*(tmp-fMean);\n";
    5781           0 :             ss << "    }\n";
    5782             : #endif
    5783             :         }
    5784           0 :         else if (pCur->GetType() == formula::svDouble)
    5785             :         {
    5786           0 :            ss << "    tmp = ";
    5787           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5788           0 :            ss << ";\n";
    5789           0 :            ss << "    vSum += (tmp-fMean)*(tmp-fMean);\n";
    5790             :         }
    5791             :     }
    5792           0 :     ss << "    double fStdDev = sqrt(vSum / (totallength - 1.0));\n";
    5793           0 :     ss << "    double dx = 0.0;\n";
    5794           0 :     ss << "    double xpower4 = 0.0;\n";
    5795           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5796             :     {
    5797           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5798             :         assert(pCur);
    5799           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    5800             :         {
    5801             :             const formula::DoubleVectorRefToken* pDVR =
    5802           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    5803           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    5804           0 :             ss << "    for (int i = ";
    5805           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    5806           0 :             ss << "    {\n";
    5807           0 :             ss << "        double arg"<<i<<" = ";
    5808           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef(true);
    5809           0 :             ss << ";\n";
    5810             : #ifdef ISNAN
    5811           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
    5812           0 :             ss << pDVR->GetArrayLength();
    5813           0 :             ss << "))\n";
    5814           0 :             ss << "        {\n";
    5815           0 :             ss << "            continue;\n";
    5816           0 :             ss << "        }\n";
    5817             : #endif
    5818           0 :             ss<< "        dx = (arg"<<i<<" -fMean) / fStdDev;\n";
    5819           0 :             ss<< "        xpower4 = xpower4 + (dx * dx * dx * dx);\n";
    5820           0 :             ss << "    }\n";
    5821             :         }
    5822           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    5823             :         {
    5824           0 :             ss << "    tmp = ";
    5825           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5826           0 :             ss << ";\n";
    5827             : #ifdef ISNAN
    5828           0 :             ss << "    if(!isNan(tmp))\n";
    5829           0 :             ss << "    {\n";
    5830           0 :             ss<< "        dx = (tmp -fMean) / fStdDev;\n";
    5831           0 :             ss<< "        xpower4 = xpower4 + (dx * dx * dx * dx);\n";
    5832           0 :             ss << "    }\n";
    5833             : #endif
    5834             :         }
    5835           0 :         else if (pCur->GetType() == formula::svDouble)
    5836             :         {
    5837           0 :            ss << "    tmp = ";
    5838           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5839           0 :            ss << ";\n";
    5840           0 :            ss<< "        dx = (tmp -fMean) / fStdDev;\n";
    5841           0 :            ss<< "        xpower4 = xpower4 + (dx * dx * dx * dx);\n";
    5842             :         }
    5843             :     }
    5844           0 :     ss<< "    double k_d = (totallength - 2.0) * (totallength - 3.0);\n";
    5845           0 :     ss<< "    double k_l = totallength * (totallength + 1.0) /";
    5846           0 :     ss<< "((totallength - 1.0) * k_d);\n";
    5847           0 :     ss<< "    double k_t = 3.0 * (totallength - 1.0) * ";
    5848           0 :     ss<< "(totallength - 1.0) / k_d;\n";
    5849           0 :     ss<< "    tmp = xpower4 * k_l - k_t;\n";
    5850           0 :     ss<< "    return tmp;\n";
    5851           0 :     ss << "}";
    5852           0 : }
    5853             : 
    5854           0 : void OpIntercept::GenSlidingWindowFunction(std::stringstream &ss,
    5855             :             const std::string &sSymName, SubArguments &vSubArguments)
    5856             : {
    5857           0 :     ss << "\ndouble " << sSymName;
    5858           0 :     ss << "_" << BinFuncName() << "(";
    5859           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5860             :     {
    5861           0 :         if (i)
    5862           0 :             ss << ",";
    5863           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5864             :     }
    5865           0 :     ss << "){\n";
    5866           0 :     ss << "    int gid0 = get_global_id(0);\n";
    5867           0 :     ss << "    double fSumX = 0.0;\n";
    5868           0 :     ss << "    double fSumY = 0.0;\n";
    5869           0 :     ss << "    double fMeanX = 0.0;\n";
    5870           0 :     ss << "    double fMeanY = 0.0;\n";
    5871           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    5872           0 :     ss << "    double fSumSqrDeltaX = 0.0;\n";
    5873           0 :     ss << "    double fCount = 0.0;\n";
    5874           0 :     ss << "    double argX = 0.0;\n";
    5875           0 :     ss << "    double argY = 0.0;\n";
    5876           0 :     if(vSubArguments.size() != 2)
    5877             :     {
    5878           0 :         ss << "    return NAN;\n";
    5879           0 :         ss << "}\n";
    5880           0 :         return ;
    5881             :     }
    5882           0 :     FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
    5883           0 :     FormulaToken *pCur1 = vSubArguments[0]->GetFormulaToken();
    5884             :     assert(pCur);
    5885             :     assert(pCur1);
    5886           0 :     if (pCur->GetType() == formula::svDoubleVectorRef&&
    5887           0 :         pCur1->GetType() == formula::svDoubleVectorRef)
    5888             :     {
    5889             :         const formula::DoubleVectorRefToken* pDVR =
    5890           0 :             static_cast<const formula::DoubleVectorRefToken *>(pCur);
    5891             :         const formula::DoubleVectorRefToken* pDVR1 =
    5892           0 :             static_cast<const formula::DoubleVectorRefToken *>(pCur1);
    5893             : 
    5894           0 :         size_t nCurWindowSize = pDVR->GetRefRowSize();
    5895           0 :         size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
    5896           0 :         size_t arrayLength = pDVR->GetArrayLength()<
    5897           0 :                pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
    5898           0 :                     pDVR1->GetArrayLength();
    5899           0 :         if(nCurWindowSize != nCurWindowSize1)
    5900             :         {
    5901           0 :             ss << "    return NAN;\n";
    5902           0 :             ss << "}\n";
    5903           0 :             return ;
    5904             :         }
    5905           0 :         ss << "    for (int i = ";
    5906           0 :         if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    5907           0 :             &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    5908             :         {
    5909             : #ifdef  ISNAN
    5910           0 :             ss << "gid0; i < " << arrayLength;
    5911           0 :             ss << " && i < " << nCurWindowSize  << "; i++)\n";
    5912           0 :             ss << "    {\n";
    5913             : #else
    5914             :             ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    5915             :             ss << "    {\n";
    5916             : #endif
    5917             :         }
    5918           0 :         else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    5919           0 :             &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    5920             :         {
    5921             : #ifdef  ISNAN
    5922           0 :             ss << "0; i < " << arrayLength ;
    5923           0 :             ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    5924           0 :             ss << "    {\n";
    5925             : #else
    5926             :             ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    5927             :             ss << "    {\n";
    5928             : #endif
    5929             :         }
    5930           0 :         else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    5931           0 :             &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    5932             :         {
    5933             : #ifdef  ISNAN
    5934           0 :             ss << "0; i + gid0 < " << arrayLength;
    5935           0 :             ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    5936           0 :             ss << "    {\n";
    5937             : #else
    5938             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    5939             :             ss << "    {\n";
    5940             : #endif
    5941             :         }
    5942           0 :         else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
    5943           0 :             &&(pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    5944             :         {
    5945             : #ifdef  ISNAN
    5946           0 :             ss << "0; i < " << arrayLength << "; i++)\n";
    5947           0 :             ss << "    {\n";
    5948             : #else
    5949             :             ss << "0; i < " << arrayLength << "; i++)\n";
    5950             :             ss << "    {\n";
    5951             : #endif
    5952             :         }
    5953             :         else
    5954             :         {
    5955             : #ifdef  ISNAN
    5956           0 :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    5957           0 :             ss << "    {\n";
    5958             : #else
    5959             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    5960             :             ss << "    {\n";
    5961             : #endif
    5962           0 :             ss << "        break;\n";
    5963           0 :             ss << "    }";
    5964           0 :             ss << "    return NAN;\n";
    5965           0 :             ss << "}\n";
    5966           0 :             return ;
    5967             :         }
    5968             : 
    5969           0 :         ss << "        argX = ";
    5970           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    5971           0 :         ss << "        argY = ";
    5972           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    5973             : #ifdef  ISNAN
    5974           0 :         ss << "        if (isNan(argX) || isNan(argY))\n";
    5975           0 :         ss << "            continue;\n";
    5976             : #endif
    5977           0 :         ss << "        fSumX += argX;\n";
    5978           0 :         ss << "        fSumY += argY;\n";
    5979           0 :         ss << "        fCount += 1.0;\n";
    5980           0 :         ss << "    }\n";
    5981             : 
    5982           0 :         ss << "    if (fCount < 1.0)\n";
    5983           0 :         ss << "        return NAN;\n";
    5984           0 :         ss << "    else\n";
    5985           0 :         ss << "    {\n";
    5986           0 :         ss << "        fMeanX = fSumX * pow(fCount,-1.0);\n";
    5987           0 :         ss << "        fMeanY = fSumY * pow(fCount,-1.0);\n";
    5988             : 
    5989           0 :         ss << "        for (int i = ";
    5990           0 :         if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    5991           0 :             &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    5992             :         {
    5993             : #ifdef  ISNAN
    5994           0 :             ss << "gid0; i < " << arrayLength;
    5995           0 :             ss << " && i < " << nCurWindowSize  << "; i++)\n";
    5996           0 :             ss << "        {\n";
    5997             : #else
    5998             :             ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    5999             :             ss << "        {\n";
    6000             : #endif
    6001             :         }
    6002           0 :         else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    6003           0 :             &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    6004             :         {
    6005             : #ifdef  ISNAN
    6006           0 :             ss << "0; i < " << arrayLength ;
    6007           0 :             ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    6008           0 :             ss << "        {\n";
    6009             : #else
    6010             :             ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    6011             :             ss << "        {\n";
    6012             : #endif
    6013             :         }
    6014           0 :         else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    6015           0 :             &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    6016             :         {
    6017             : #ifdef  ISNAN
    6018           0 :             ss << "0; i + gid0 < " << arrayLength;
    6019           0 :             ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    6020           0 :             ss << "        {\n";
    6021             : #else
    6022             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    6023             :             ss << "        {\n";
    6024             : #endif
    6025             :         }
    6026             :         else
    6027             :         {
    6028             : #ifdef  ISNAN
    6029           0 :             ss << "0; i < " << arrayLength << "; i++)\n";
    6030           0 :             ss << "        {\n";
    6031             : #else
    6032             :             ss << "0; i < " << arrayLength << "; i++)\n";
    6033             :             ss << "        {\n";
    6034             : #endif
    6035             :         }
    6036             : 
    6037           0 :         ss << "            argX = ";
    6038           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    6039           0 :         ss << "            argY = ";
    6040           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    6041             : #ifdef  ISNAN
    6042           0 :         ss << "            if (isNan(argX) || isNan(argY))\n";
    6043           0 :         ss << "                 continue;\n";
    6044             : #endif
    6045           0 :         ss << "            fSumDeltaXDeltaY += (argX-fMeanX)*(argY-fMeanY);\n";
    6046           0 :         ss << "            fSumSqrDeltaX += (argX-fMeanX) * (argX-fMeanX);\n";
    6047           0 :         ss << "        }\n";
    6048           0 :         ss << "        if(fSumSqrDeltaX == 0.0)\n";
    6049           0 :         ss << "            return NAN;\n";
    6050           0 :         ss << "        else\n";
    6051           0 :         ss << "        {\n";
    6052           0 :         ss << "            return fMeanY -";
    6053           0 :         ss << " (fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0))*fMeanX;\n";
    6054           0 :         ss << "        }\n";
    6055           0 :         ss << "    }\n";
    6056           0 :         ss << "}\n";
    6057             :     }
    6058             :     else
    6059             :     {
    6060           0 :         ss << "    return NAN;\n";
    6061           0 :         ss << "}\n";
    6062             :     }
    6063             : }
    6064           0 : void OpLogInv:: GenSlidingWindowFunction(std::stringstream &ss,
    6065             :             const std::string &sSymName, SubArguments &vSubArguments)
    6066             : {
    6067           0 :     ss << "\ndouble " << sSymName;
    6068           0 :     ss << "_"<< BinFuncName() <<"(";
    6069           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6070             :     {
    6071           0 :         if (i)
    6072           0 :             ss << ",";
    6073           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6074             :     }
    6075           0 :     ss << ") {\n";
    6076           0 :     ss << "    int gid0=get_global_id(0);\n";
    6077           0 :     ss << "    double tmp;\n";
    6078           0 :     ss << "    double arg0,arg1,arg2,arg3;\n";
    6079           0 :     size_t i = vSubArguments.size();
    6080           0 :     for (i = 0; i < vSubArguments.size(); i++)
    6081             :     {
    6082           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    6083             :         assert(pCur);
    6084           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    6085             :         {
    6086             :             const formula::DoubleVectorRefToken* pDVR =
    6087           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    6088           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    6089           0 :             ss << "for (int i = ";
    6090           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    6091             : #ifdef  ISNAN
    6092           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    6093           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    6094             : #else
    6095             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    6096             : #endif
    6097           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    6098             : #ifdef  ISNAN
    6099           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    6100           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    6101             : #else
    6102             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    6103             : #endif
    6104           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    6105             : #ifdef  ISNAN
    6106           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    6107           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    6108             : #else
    6109             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6110             : #endif
    6111             :             }
    6112             :             else {
    6113             : #ifdef  ISNAN
    6114           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    6115             : #else
    6116             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6117             : #endif
    6118             :             }
    6119             :         }
    6120           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    6121             :         {
    6122             : #ifdef  ISNAN
    6123             :             const formula::SingleVectorRefToken* pSVR =
    6124           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    6125           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    6126           0 :             ss << "    {\n";
    6127           0 :             ss << "        if (isNan(";
    6128           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6129           0 :             ss << "))\n";
    6130           0 :             ss << "            arg"<<i<<"= 0;\n";
    6131           0 :             ss << "        else\n";
    6132           0 :             ss << "            arg"<<i<<"=";
    6133           0 :             ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6134           0 :             ss << ";\n";
    6135           0 :             ss << "    }\n";
    6136           0 :             ss << "    else\n";
    6137           0 :             ss << "        arg"<<i<<"= 0;\n";
    6138             : #endif
    6139             :         }
    6140           0 :         else if (pCur->GetType() == formula::svDouble)
    6141             :         {
    6142             : #ifdef  ISNAN
    6143           0 :             ss << "    if (isNan(";
    6144           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6145           0 :             ss << "))\n";
    6146           0 :             ss << "        arg"<<i<<"= 0;\n";
    6147           0 :             ss << "    else\n";
    6148           0 :             ss << "        arg"<<i<<"=";
    6149           0 :             ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6150           0 :             ss << ";\n";
    6151             : #endif
    6152             :         }
    6153             :     }
    6154           0 :     ss<< "    double q,t,z;\n";
    6155           0 :     ss<< "    q = arg0 -0.5;\n";
    6156           0 :     ss<< "    if(fabs(q)<=.425)\n";
    6157           0 :     ss<< "    {\n";
    6158           0 :     ss<< "        t=0.180625-pow(q, 2);\n";
    6159             :     ss<< "        z=\n"
    6160             :     "        q*\n"
    6161             :     "        (\n"
    6162             :     "            (\n"
    6163             :     "                (\n"
    6164             :     "                    (\n"
    6165             :     "                        (\n"
    6166             :     "                            (\n"
    6167             :     "                                (\n"
    6168           0 :     "                                    t*2509.0809287301226727";
    6169             :     ss<<"+33430.575583588128105\n"
    6170             :     "                                )\n"
    6171             :     "                                *t+67265.770927008700853\n"
    6172             :     "                            )\n"
    6173             :     "                            *t+45921.953931549871457\n"
    6174             :     "                        )\n"
    6175             :     "                        *t+13731.693765509461125\n"
    6176             :     "                    )\n"
    6177             :     "                    *t+1971.5909503065514427\n"
    6178             :     "                )\n"
    6179             :     "                *t+133.14166789178437745\n"
    6180             :     "            )\n"
    6181             :     "            *t+3.387132872796366608\n"
    6182             :     "        )\n"
    6183             :     "        /\n"
    6184             :     "        (\n"
    6185             :     "            (\n"
    6186             :     "                (\n"
    6187             :     "                    (\n"
    6188             :     "                        (\n"
    6189             :     "                            (\n"
    6190             :     "                                (\n"
    6191           0 :     "                                    t*5226.495278852854561";
    6192             :     ss<<"+28729.085735721942674\n"
    6193             :     "                                )\n"
    6194             :     "                                *t+39307.89580009271061\n"
    6195             :     "                            )\n"
    6196             :     "                            *t+21213.794301586595867\n"
    6197             :     "                        )\n"
    6198             :     "                        *t+5394.1960214247511077\n"
    6199             :     "                    )\n"
    6200             :     "                    *t+687.1870074920579083\n"
    6201             :     "                )\n"
    6202             :     "                *t+42.313330701600911252\n"
    6203             :     "            )\n"
    6204             :     "            *t+1.0\n"
    6205           0 :     "        );\n";
    6206           0 :     ss<<"    }\n";
    6207           0 :     ss<<"    else\n";
    6208           0 :     ss<<"    {\n";
    6209           0 :     ss<<"        t = q > 0 ? 1 - arg0 : arg0;\n";
    6210           0 :     ss<<"        t=sqrt(-log(t));\n";
    6211           0 :     ss<<"        if(t<=5.0)\n";
    6212           0 :     ss<<"        {\n";
    6213           0 :     ss<<"            t+=-1.6;\n";
    6214             :     ss<<"            z=\n"
    6215             :     "            (\n"
    6216             :     "                (\n"
    6217             :     "                    (\n"
    6218             :     "                        (\n"
    6219             :     "                            (\n"
    6220             :     "                                (\n"
    6221             :     "                                    (\n"
    6222           0 :     "                                        t*7.7454501427834140764e-4";
    6223             :     ss<<"+0.0227238449892691845833\n"
    6224             :     "                                    )\n"
    6225             :     "                                    *t+0.24178072517745061177\n"
    6226             :     "                                )\n"
    6227             :     "                                *t+1.27045825245236838258\n"
    6228             :     "                            )\n"
    6229             :     "                            *t+3.64784832476320460504\n"
    6230             :     "                        )\n"
    6231             :     "                        *t+5.7694972214606914055\n"
    6232             :     "                    )\n"
    6233             :     "                    *t+4.6303378461565452959\n"
    6234             :     "                )\n"
    6235             :     "                *t+1.42343711074968357734\n"
    6236             :     "            )\n"
    6237             :     "            /\n"
    6238             :     "            (\n"
    6239             :     "                (\n"
    6240             :     "                    (\n"
    6241             :     "                        (\n"
    6242             :     "                            (\n"
    6243             :     "                                (\n"
    6244             :     "                                    (\n"
    6245           0 :     "                                        t*1.05075007164441684324e-9";
    6246             :     ss<<"+5.475938084995344946e-4\n"
    6247             :     "                                    )\n"
    6248             :     "                                    *t+0.0151986665636164571966\n"
    6249             :     "                                )\n"
    6250             :     "                                *t+0.14810397642748007459\n"
    6251             :     "                            )\n"
    6252             :     "                            *t+0.68976733498510000455\n"
    6253             :     "                        )\n"
    6254             :     "                        *t+1.6763848301838038494\n"
    6255             :     "                    )\n"
    6256             :     "                    *t+2.05319162663775882187\n"
    6257             :     "                )\n"
    6258             :     "                *t+1.0\n"
    6259           0 :     "            );\n";
    6260           0 :     ss<<"        }\n";
    6261           0 :     ss<<"        else\n";
    6262           0 :     ss<<"        {\n";
    6263           0 :     ss<<"            t+=-5.0;\n";
    6264             :     ss<<"            z=\n"
    6265             :     "            (\n"
    6266             :     "                (\n"
    6267             :     "                    (\n"
    6268             :     "                        (\n"
    6269             :     "                            (\n"
    6270             :     "                                (\n"
    6271             :     "                                    (\n"
    6272           0 :     "                                        t*2.01033439929228813265e-7";
    6273             :     ss<<"+2.71155556874348757815e-5\n"
    6274             :     "                                    )\n"
    6275             :     "                                    *t+0.0012426609473880784386\n"
    6276             :     "                                )\n"
    6277             :     "                                *t+0.026532189526576123093\n"
    6278             :     "                            )\n"
    6279             :     "                            *t+0.29656057182850489123\n"
    6280             :     "                        )\n"
    6281             :     "                        *t+1.7848265399172913358\n"
    6282             :     "                    )\n"
    6283             :     "                    *t+5.4637849111641143699\n"
    6284             :     "                )\n"
    6285             :     "                *t+6.6579046435011037772\n"
    6286             :     "            )\n"
    6287             :     "            /\n"
    6288             :     "            (\n"
    6289             :     "                (\n"
    6290             :     "                    (\n"
    6291             :     "                        (\n"
    6292             :     "                            (\n"
    6293             :     "                                (\n"
    6294             :     "                                    (\n"
    6295             :     "                                        t*2.04426310338993978564e-15"
    6296             :     " +1.4215117583164458887e-7\n"
    6297             :     "                                    )\n"
    6298             :     "                                    *t+1.8463183175100546818e-5\n"
    6299             :     "                                )\n"
    6300             :     "                                *t+7.868691311456132591e-4\n"
    6301             :     "                            )\n"
    6302             :     "                            *t+0.0148753612908506148525\n"
    6303             :     "                        )\n"
    6304             :     "                        *t+0.13692988092273580531\n"
    6305             :     "                    )\n"
    6306             :     "                    *t+0.59983220655588793769\n"
    6307             :     "                )\n"
    6308             :     "                *t+1.0\n"
    6309           0 :     "            );\n";
    6310           0 :     ss << "        }\n";
    6311           0 :     ss << "        z = q < 0.0 ? (-1)*z : z;\n";
    6312           0 :     ss << "    }\n";
    6313           0 :     ss << "    tmp = exp(arg1+arg2*z);\n";
    6314           0 :     ss << "    return tmp;\n";
    6315           0 :     ss << "}\n";
    6316           0 : }
    6317             : 
    6318           0 : void OpForecast::GenSlidingWindowFunction(std::stringstream &ss,
    6319             :             const std::string &sSymName, SubArguments &vSubArguments)
    6320             : {
    6321           0 :     FormulaToken *pCur0 = vSubArguments[0]->GetFormulaToken();
    6322             :     assert(pCur0);
    6323             :     const formula::SingleVectorRefToken*pCurDVR0= static_cast<const
    6324           0 :           formula::SingleVectorRefToken *>(pCur0);
    6325           0 :     FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
    6326             :     assert(pCur1);
    6327             :     const formula::DoubleVectorRefToken* pCurDVR1 =
    6328           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur1);
    6329           0 :     size_t nCurWindowSize = pCurDVR1->GetRefRowSize();
    6330           0 :     FormulaToken *pCur2 = vSubArguments[2]->GetFormulaToken();
    6331             :     assert(pCur2);
    6332             :     const formula::DoubleVectorRefToken* pCurDVR2 =
    6333           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur2);
    6334           0 :     size_t nCurWindowSize1 = pCurDVR2->GetRefRowSize();
    6335           0 :     ss << "\ndouble " << sSymName;
    6336           0 :     ss << "_"<< BinFuncName() <<"( ";
    6337           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6338             :     {
    6339           0 :         if (i)
    6340           0 :             ss << ",";
    6341           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6342             :     }
    6343           0 :     ss << ") {\n";
    6344           0 :     ss << "    int gid0 = get_global_id(0);\n";
    6345           0 :     ss << "    double fSumX = 0.0;\n";
    6346           0 :     ss << "    double fSumY = 0.0;\n";
    6347           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    6348           0 :     ss << "    double fSumSqrDeltaX = 0.0;\n";
    6349           0 :     if(pCur0->GetType()== formula::svDouble ||
    6350           0 :         pCur0->GetType() == formula::svSingleVectorRef)
    6351             :     {
    6352           0 :         ss << "    double arg0 = ";
    6353           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef();
    6354           0 :         ss << ";\n";
    6355             :     }
    6356             :     else
    6357           0 :         ss << "return HUGE_VAL";
    6358           0 :     if(pCur1->GetType() != formula::svDoubleVectorRef ||
    6359           0 :         pCur2->GetType() != formula::svDoubleVectorRef)
    6360           0 :         ss << "return HUGE_VAL";
    6361             :     else
    6362             :     {
    6363             : #ifdef ISNAN
    6364           0 :         ss<< "    if(isNan(arg0)||(gid0>=";
    6365           0 :         ss<<pCurDVR0->GetArrayLength();
    6366           0 :         ss<<"))\n";
    6367           0 :         ss<<"        arg0 = 0;\n";
    6368             : #endif
    6369           0 :         ss << "    int length="<<nCurWindowSize;
    6370           0 :         ss << ";\n";
    6371           0 :         ss << "    int length1= "<<nCurWindowSize1;
    6372           0 :         ss << ";\n";
    6373           0 :         ss << "    if(length!=length1)\n";
    6374           0 :         ss << "        return 0;\n";
    6375           0 :         ss << "    double tmp = 0;\n";
    6376           0 :         ss << "    for (int i = 0; i <" << nCurWindowSize << "; i++)\n";
    6377           0 :         ss << "    {\n";
    6378           0 :         ss << "        double arg1 = ";
    6379           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef(true);
    6380           0 :         ss << ";\n";
    6381           0 :         ss << "        double arg2 = ";
    6382           0 :         ss << vSubArguments[2]->GenSlidingWindowDeclRef(true);
    6383           0 :         ss << ";\n";
    6384             : #ifdef ISNAN
    6385           0 :         ss << "        if(isNan(arg1)||((gid0+i)>=";
    6386           0 :         ss << pCurDVR1->GetArrayLength();
    6387           0 :         ss << "))\n";
    6388           0 :         ss << "        {\n";
    6389           0 :         ss << "            length--;\n";
    6390           0 :         ss << "            continue;\n";
    6391           0 :         ss << "        }\n";
    6392             : #endif
    6393             : #ifdef ISNAN
    6394           0 :         ss << "        if(isNan(arg2)||((gid0+i)>=";
    6395           0 :         ss << pCurDVR2->GetArrayLength();
    6396           0 :         ss << "))\n";
    6397           0 :         ss << "        {\n";
    6398           0 :         ss << "            length--;\n";
    6399           0 :         ss << "            continue;\n";
    6400           0 :         ss << "        }\n";
    6401             : #endif
    6402           0 :         ss << "        fSumY+=arg1;\n";
    6403           0 :         ss << "        fSumX+=arg2;\n";
    6404           0 :         ss << "    }\n";
    6405           0 :         ss << "    double fMeanX = fSumX / length;\n";
    6406           0 :         ss << "    double fMeanY = fSumY / length;\n";
    6407           0 :         ss << "    for (int i = 0; i <" << nCurWindowSize << "; i++)\n";
    6408           0 :         ss << "    {\n";
    6409           0 :         ss << "        double arg1 = ";
    6410           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef(true);
    6411           0 :         ss << ";\n";
    6412           0 :         ss << "        double arg2 = ";
    6413           0 :         ss << vSubArguments[2]->GenSlidingWindowDeclRef(true);
    6414           0 :         ss << ";\n";
    6415             : #ifdef ISNAN
    6416           0 :         ss << "        if(isNan(arg1)||((gid0+i)>=";
    6417           0 :         ss <<pCurDVR1->GetArrayLength();
    6418           0 :         ss <<"))\n";
    6419           0 :         ss <<"        {\n";
    6420           0 :         ss <<"            continue;\n";
    6421           0 :         ss <<"        }\n";
    6422             : #endif
    6423             : #ifdef ISNAN
    6424           0 :         ss << "        if(isNan(arg2)||((gid0+i)>=";
    6425           0 :         ss <<pCurDVR2->GetArrayLength();
    6426           0 :         ss <<"))\n";
    6427           0 :         ss <<"        {\n";
    6428           0 :         ss <<"            continue;\n";
    6429           0 :         ss <<"        }\n";
    6430             : #endif
    6431           0 :         ss <<"        fSumDeltaXDeltaY+=(arg2 - fMeanX) * (arg1 - fMeanY);\n";
    6432           0 :         ss <<"        fSumSqrDeltaX+=pow(arg2 - fMeanX, 2);\n";
    6433           0 :         ss <<"    }\n";
    6434           0 :         ss <<"    tmp =fMeanY + fSumDeltaXDeltaY / fSumSqrDeltaX *";
    6435           0 :         ss <<" (arg0 - fMeanX);\n";
    6436           0 :         ss <<"    return tmp;\n";
    6437           0 :         ss << "}";
    6438             :     }
    6439           0 : }
    6440           0 : void OpLogNormDist::GenSlidingWindowFunction(std::stringstream &ss,
    6441             :             const std::string &sSymName, SubArguments &vSubArguments)
    6442             : {
    6443           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    6444             :     const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
    6445           0 :         formula::SingleVectorRefToken *>(tmpCur0);
    6446           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    6447             :     const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
    6448           0 :         formula::SingleVectorRefToken *>(tmpCur1);
    6449           0 :     FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
    6450             :     const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
    6451           0 :         formula::SingleVectorRefToken *>(tmpCur2);
    6452           0 :     FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
    6453             :     const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
    6454           0 :         formula::SingleVectorRefToken *>(tmpCur3);
    6455           0 :     ss << "\ndouble " << sSymName;
    6456           0 :     ss << "_"<< BinFuncName() <<"(";
    6457           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6458             :     {
    6459           0 :         if (i)
    6460           0 :             ss << ",";
    6461           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6462             :     }
    6463           0 :     ss << ") {\n";
    6464           0 :     ss << "    int gid0=get_global_id(0);\n";
    6465           0 :     ss << "    double arg0,arg1,arg2,arg3;\n";
    6466           0 :     size_t i = vSubArguments.size();
    6467           0 :     for (i = 0; i < vSubArguments.size(); i++)
    6468             :     {
    6469           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    6470             :         assert(pCur);
    6471           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    6472             :         {
    6473             :             const formula::DoubleVectorRefToken* pDVR =
    6474           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    6475           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    6476           0 :             ss << "for (int i = ";
    6477           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    6478             : #ifdef  ISNAN
    6479           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    6480           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    6481             : #else
    6482             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    6483             : #endif
    6484           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    6485             : #ifdef  ISNAN
    6486           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    6487           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    6488             : #else
    6489             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    6490             : #endif
    6491           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    6492             : #ifdef  ISNAN
    6493           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    6494           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    6495             : #else
    6496             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6497             : #endif
    6498             :             }
    6499             :             else {
    6500             : #ifdef  ISNAN
    6501           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    6502             : #else
    6503             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6504             : #endif
    6505             :             }
    6506             :         }
    6507           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    6508             :         {
    6509             : #ifdef  ISNAN
    6510             :             const formula::SingleVectorRefToken* pSVR =
    6511           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    6512           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    6513           0 :             ss << "    {\n";
    6514           0 :             ss << "        if (isNan(";
    6515           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6516           0 :             ss << "))\n";
    6517           0 :             ss << "            arg"<<i<<"= 0;\n";
    6518           0 :             ss << "        else\n";
    6519           0 :             ss << "            arg"<<i<<"=";
    6520           0 :             ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6521           0 :             ss << ";\n";
    6522           0 :             ss << "    }\n";
    6523           0 :             ss << "    else\n";
    6524           0 :             ss << "        arg"<<i<<"= 0;\n";
    6525             : #endif
    6526             :         }
    6527           0 :         else if (pCur->GetType() == formula::svDouble)
    6528             :         {
    6529             : #ifdef  ISNAN
    6530           0 :             ss << "    if (isNan(";
    6531           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6532           0 :             ss << "))\n";
    6533           0 :             ss << "        arg"<<i<<"= 0;\n";
    6534           0 :             ss << "    else\n";
    6535           0 :             ss << "        arg"<<i<<"=";
    6536           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    6537           0 :             ss << ";\n";
    6538             : #endif
    6539             :         }
    6540             :     }
    6541           0 :     ss << "    double tmp;\n";
    6542             : #ifdef ISNAN
    6543           0 :     ss << "    if(isNan(arg0)||(gid0>=";
    6544           0 :     ss << tmpCurDVR0->GetArrayLength();
    6545           0 :     ss << "))\n";
    6546           0 :     ss << "        arg0 = 0;\n";
    6547             : #endif
    6548             : #ifdef ISNAN
    6549           0 :     ss << "    if(isNan(arg1)||(gid0>=";
    6550           0 :     ss << tmpCurDVR1->GetArrayLength();
    6551           0 :     ss << "))\n";
    6552           0 :     ss << "        arg1 = 0;\n";
    6553             : #endif
    6554             : #ifdef ISNAN
    6555           0 :     ss << "    if(isNan(arg2)||(gid0>=";
    6556           0 :     ss << tmpCurDVR2->GetArrayLength();
    6557           0 :     ss << "))\n";
    6558           0 :     ss << "        arg2 = 0;\n";
    6559             : #endif
    6560             : #ifdef ISNAN
    6561           0 :     ss << "    if(isNan(arg3)||(gid0>=";
    6562           0 :     ss << tmpCurDVR3->GetArrayLength();
    6563           0 :     ss << "))\n";
    6564           0 :     ss << "        arg3 = 0;\n";
    6565             : #endif
    6566           0 :     ss << "    double temp = (log(arg0)-arg1)/arg2;\n";
    6567           0 :     ss << "    if(arg3)\n";
    6568           0 :     ss << "    {\n";
    6569           0 :     ss << "        if(arg0<=0)\n";
    6570           0 :     ss << "            tmp = 0.0;\n";
    6571           0 :     ss << "        else\n";
    6572           0 :     ss << "            tmp = 0.5 * erfc(-temp * 0.7071067811865475);\n";
    6573           0 :     ss << "    }\n";
    6574           0 :     ss << "    else\n";
    6575           0 :     ss << "        tmp = (0.39894228040143268 * exp((-1)*pow(temp, 2)";
    6576           0 :     ss << " / 2.0))/(arg2*arg0);\n";
    6577           0 :     ss << "    return tmp;\n";
    6578           0 :     ss << "}\n";
    6579           0 : }
    6580             : 
    6581           0 : void OpGammaDist::BinInlineFun(std::set<std::string>& decls,
    6582             :     std::set<std::string>& funs)
    6583             : {
    6584           0 :     decls.insert(fBigInvDecl);decls.insert(fLogDblMaxDecl);
    6585           0 :     decls.insert(fHalfMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
    6586           0 :     decls.insert(GetGammaSeriesDecl);decls.insert(GetGammaContFractionDecl);
    6587           0 :     decls.insert(GetLowRegIGammaDecl);decls.insert(GetGammaDistDecl);
    6588           0 :     decls.insert(GetGammaDistPDFDecl);
    6589           0 :     funs.insert(GetGammaSeries);funs.insert(GetGammaContFraction);
    6590           0 :     funs.insert(GetLowRegIGamma);funs.insert(GetGammaDist);
    6591           0 :     funs.insert(GetGammaDistPDF);
    6592           0 : }
    6593             : 
    6594           0 : void OpGammaDist::GenSlidingWindowFunction(std::stringstream &ss,
    6595             :             const std::string &sSymName, SubArguments &vSubArguments)
    6596             : {
    6597           0 :     ss << "\ndouble " << sSymName;
    6598           0 :     ss << "_"<< BinFuncName() <<"(";
    6599           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6600             :     {
    6601           0 :         if (i)
    6602           0 :             ss << ",";
    6603           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6604             :     }
    6605           0 :     ss << ") {\n";
    6606           0 :     ss << "    int gid0=get_global_id(0);\n";
    6607           0 :     ss << "    double tmp;\n";
    6608           0 :     ss << "    double arg0,arg1,arg2,arg3;\n";
    6609           0 :     size_t i = vSubArguments.size();
    6610             : 
    6611           0 :     for (i = 0; i < vSubArguments.size(); i++)
    6612             :     {
    6613           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    6614             :         assert(pCur);
    6615           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    6616             :         {
    6617             :             const formula::DoubleVectorRefToken* pDVR =
    6618           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    6619           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    6620           0 :             ss << "for (int i = ";
    6621           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    6622             : #ifdef  ISNAN
    6623           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    6624           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    6625             : #else
    6626             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    6627             : #endif
    6628           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    6629             : #ifdef  ISNAN
    6630           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    6631           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    6632             : #else
    6633             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    6634             : #endif
    6635           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    6636             : #ifdef  ISNAN
    6637           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    6638           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    6639             : #else
    6640             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6641             : #endif
    6642             :             }
    6643             :             else {
    6644             : #ifdef  ISNAN
    6645           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    6646             : #else
    6647             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6648             : #endif
    6649             :             }
    6650             :         }
    6651           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    6652             :         {
    6653             : #ifdef  ISNAN
    6654             :             const formula::SingleVectorRefToken* pSVR =
    6655           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    6656           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    6657           0 :             ss << "    {\n";
    6658           0 :             ss << "        if (isNan(";
    6659           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6660           0 :             ss << "))\n";
    6661           0 :             ss << "            arg"<<i<<"= 0;\n";
    6662           0 :             ss << "        else\n";
    6663           0 :             ss << "            arg"<<i<<"=";
    6664           0 :             ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6665           0 :             ss << ";\n";
    6666           0 :             ss << "    }\n";
    6667           0 :             ss << "    else\n";
    6668           0 :             ss << "        arg"<<i<<"= 0;\n";
    6669             : #endif
    6670             :         }
    6671           0 :         else if (pCur->GetType() == formula::svDouble)
    6672             :         {
    6673             : #ifdef  ISNAN
    6674           0 :             ss << "    if (isNan(";
    6675           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6676           0 :             ss << "))\n";
    6677           0 :             ss << "        arg"<<i<<"= 0;\n";
    6678           0 :             ss << "    else\n";
    6679           0 :             ss << "        arg"<<i<<"=";
    6680           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    6681           0 :             ss << ";\n";
    6682             : #endif
    6683             :         }
    6684             :     }
    6685           0 :     ss << "    if (arg3)\n";
    6686           0 :     ss << "        tmp=GetGammaDist( arg0, arg1, arg2);\n";
    6687           0 :     ss << "    else\n";
    6688           0 :     ss << "        tmp=GetGammaDistPDF( arg0, arg1, arg2);\n";
    6689           0 :     ss << "    return tmp;\n";
    6690           0 :     ss << "}\n";
    6691           0 : }
    6692           0 : void OpChiDist::BinInlineFun(std::set<std::string>& decls,
    6693             :     std::set<std::string>& funs)
    6694             : {
    6695           0 :     decls.insert(fBigInvDecl);
    6696           0 :     funs.insert("");
    6697           0 :     decls.insert(fHalfMachEpsDecl);
    6698           0 :     funs.insert("");
    6699           0 :     decls.insert(GetUpRegIGammaDecl);
    6700           0 :     funs.insert(GetUpRegIGamma);
    6701           0 :     decls.insert(GetGammaSeriesDecl);
    6702           0 :     funs.insert(GetGammaSeries);
    6703           0 :     decls.insert(GetGammaContFractionDecl);
    6704           0 :     funs.insert(GetGammaContFraction);
    6705           0 :     decls.insert(GetChiDistDecl);
    6706           0 :     funs.insert(GetChiDist);
    6707           0 : }
    6708           0 : void OpChiDist::GenSlidingWindowFunction(
    6709             :     std::stringstream &ss,const std::string &sSymName,
    6710             :     SubArguments &vSubArguments)
    6711             : {
    6712           0 :     ss << "\ndouble " << sSymName;
    6713           0 :     ss << "_"<< BinFuncName() <<"(";
    6714           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6715             :     {
    6716           0 :         if (i)
    6717           0 :             ss << ",";
    6718           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6719             :     }
    6720           0 :     ss << ")\n";
    6721           0 :     ss << "{\n";
    6722           0 :     ss << "    double fx,fDF,tmp=0,tmp0=0,tmp1=0;\n";
    6723           0 :     ss << "    int gid0=get_global_id(0);\n";
    6724             : 
    6725           0 :     size_t i = vSubArguments.size();
    6726           0 :     ss <<"\n";
    6727           0 :     for (i = 0; i < vSubArguments.size(); i++)
    6728             :     {
    6729           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    6730             :         assert(pCur);
    6731           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    6732             :         {
    6733             :             const formula::DoubleVectorRefToken* pDVR =
    6734           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    6735           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    6736           0 :             ss << "for (int i = ";
    6737           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    6738             :             {
    6739             : #ifdef  ISNAN
    6740           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    6741           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    6742             : #else
    6743             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    6744             : #endif
    6745             :             }
    6746           0 :             else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    6747             :             {
    6748             : #ifdef  ISNAN
    6749           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    6750           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    6751             : #else
    6752             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    6753             : #endif
    6754             :             }
    6755           0 :             else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    6756             :             {
    6757             : #ifdef  ISNAN
    6758           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    6759           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    6760             : #else
    6761             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6762             : #endif
    6763             :             }
    6764             :             else
    6765             :             {
    6766             : #ifdef  ISNAN
    6767           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    6768             : #else
    6769             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6770             : #endif
    6771             :             }
    6772             :         }
    6773           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    6774             :         {
    6775             : #ifdef  ISNAN
    6776             :             const formula::SingleVectorRefToken* pSVR =
    6777           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    6778           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    6779             : #else
    6780             : #endif
    6781             :         }
    6782           0 :         else if (pCur->GetType() == formula::svDouble)
    6783             :         {
    6784             : #ifdef  ISNAN
    6785           0 :             ss << "{\n";
    6786             : #endif
    6787             :         }
    6788             :         else
    6789             :         {
    6790             : #ifdef  ISNAN
    6791             : #endif
    6792             :         }
    6793             : #ifdef  ISNAN
    6794           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    6795             :         {
    6796           0 :             ss << "    if (isNan(";
    6797           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6798           0 :             ss << "))\n";
    6799           0 :             ss << "        tmp"<<i<<"= 0;\n";
    6800           0 :             ss << "    else\n";
    6801           0 :             ss << "        tmp"<<i<<"=\n";
    6802           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6803           0 :             ss << ";\n}\n";
    6804             :         }
    6805             : #endif
    6806             :     }
    6807           0 :     ss << "    fx = tmp0;\n";
    6808           0 :     ss << "    fDF = floor(tmp1);\n";
    6809           0 :     ss << "    if(fDF < 1.0)\n";
    6810           0 :     ss << "    {\n";
    6811           0 :     ss << "        return DBL_MIN;\n";
    6812           0 :     ss << "    }\n";
    6813           0 :     ss << "    tmp = GetChiDist( fx, fDF);\n";
    6814           0 :     ss << "    return tmp;\n";
    6815           0 :     ss << "}\n";
    6816           0 : }
    6817           0 : void OpBinomdist::BinInlineFun(std::set<std::string>& decls,
    6818             :     std::set<std::string>& funs)
    6819             : {
    6820           0 :     decls.insert(fMachEpsDecl);
    6821           0 :     funs.insert("");
    6822           0 :     decls.insert(MinDecl);
    6823           0 :     funs.insert("");
    6824           0 :     decls.insert(fMaxGammaArgumentDecl);
    6825           0 :     funs.insert("");
    6826           0 :     decls.insert(GetBinomDistPMFDecl);
    6827           0 :     funs.insert(GetBinomDistPMF);
    6828           0 :     decls.insert(GetBetaDistDecl);
    6829           0 :     funs.insert(GetBetaDist);
    6830           0 :     decls.insert(lcl_GetBinomDistRangeDecl);
    6831           0 :     funs.insert(lcl_GetBinomDistRange);
    6832           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);
    6833           0 :     funs.insert(lcl_GetBetaHelperContFrac);
    6834           0 :     decls.insert(GetBetaDistPDFDecl);
    6835           0 :     funs.insert(GetBetaDistPDF);
    6836           0 :     decls.insert(GetLogBetaDecl);
    6837           0 :     funs.insert(GetLogBeta);
    6838           0 :     decls.insert(GetBetaDecl);
    6839           0 :     funs.insert(GetBeta);
    6840           0 :     decls.insert(lcl_getLanczosSumDecl);
    6841           0 :     funs.insert(lcl_getLanczosSum);
    6842           0 : }
    6843           0 : void OpBinomdist::GenSlidingWindowFunction(
    6844             :     std::stringstream &ss,const std::string &sSymName,
    6845             :     SubArguments &vSubArguments)
    6846             : {
    6847           0 :     ss << "\ndouble " << sSymName;
    6848           0 :     ss << "_"<< BinFuncName() <<"(";
    6849           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6850             :     {
    6851           0 :         if (i)
    6852           0 :             ss << ",";
    6853           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6854             :     }
    6855           0 :     ss << ")\n";
    6856           0 :     ss << "{\n";
    6857           0 :     ss << "    double tmp0,tmp1,tmp2,tmp3;\n";
    6858           0 :     ss << "    int gid0=get_global_id(0);\n";
    6859           0 :     size_t i = vSubArguments.size();
    6860             : 
    6861           0 :     ss <<"\n    ";
    6862             :     //while (i-- > 1)
    6863           0 :     for (i = 0; i < vSubArguments.size(); i++)
    6864             :     {
    6865           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    6866             :         assert(pCur);
    6867           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    6868             :         {
    6869             :             const formula::DoubleVectorRefToken* pDVR =
    6870           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    6871           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    6872           0 :             ss << "for (int i = ";
    6873           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    6874             :             {
    6875             : #ifdef  ISNAN
    6876           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    6877           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    6878             : #else
    6879             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    6880             : #endif
    6881             :             }
    6882           0 :             else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    6883             :             {
    6884             : #ifdef  ISNAN
    6885           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    6886           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    6887             : #else
    6888             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    6889             : #endif
    6890             :             }
    6891           0 :             else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    6892             :             {
    6893             : #ifdef  ISNAN
    6894           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    6895           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    6896             : #else
    6897             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6898             : #endif
    6899             :             }
    6900             :             else
    6901             :             {
    6902             : #ifdef  ISNAN
    6903           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    6904             : #else
    6905             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6906             : #endif
    6907             :             }
    6908             :         }
    6909           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    6910             :         {
    6911             : #ifdef  ISNAN
    6912             :             const formula::SingleVectorRefToken* pSVR =
    6913           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    6914           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    6915             : #else
    6916             : #endif
    6917             :         }
    6918           0 :         else if (pCur->GetType() == formula::svDouble)
    6919             :         {
    6920             : #ifdef  ISNAN
    6921           0 :             ss << "{\n";
    6922             : #endif
    6923             :         }
    6924             :         else
    6925             :         {
    6926             : #ifdef  ISNAN
    6927             : #endif
    6928             :         }
    6929             : #ifdef  ISNAN
    6930           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    6931             :         {
    6932           0 :             ss << "    if (isNan(";
    6933           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6934           0 :             ss << "))\n";
    6935           0 :             ss << "        tmp"<<i<<"= 0;\n";
    6936           0 :             ss << "    else\n";
    6937           0 :             ss << "        tmp"<<i<<"=\n";
    6938           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6939           0 :             ss << ";\n}\n";
    6940             :         }
    6941             :         else
    6942             :         {
    6943           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6944           0 :             ss <<";\n";
    6945             :         }
    6946             : #else
    6947             :     ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6948             :     ss <<";\n    ";
    6949             : #endif
    6950             :     }
    6951           0 :     ss << "    tmp0 = floor(tmp0);\n";
    6952           0 :     ss << "    tmp1 = floor(tmp1);\n";
    6953           0 :     ss << "    double rq = (0.5 - tmp2) + 0.5;\n";
    6954           0 :     ss << "    if (tmp1 < 0.0 || tmp0 < 0.0 || tmp0 > tmp1 ||";
    6955           0 :     ss << "tmp2 < 0.0 || tmp2 > 1.0)\n";
    6956           0 :     ss << "    {\n";
    6957           0 :     ss << "        return DBL_MIN;\n";
    6958           0 :     ss << "    }\n";
    6959           0 :     ss << "    if(tmp2 == 0.0)\n";
    6960           0 :     ss << "        return ( (tmp0 == 0.0 || tmp3) ? 1.0 : 0.0 );\n";
    6961           0 :     ss << "    if(tmp2 == 1.0)\n";
    6962           0 :     ss << "        return ( (tmp0 == tmp1) ? 1.0 : 0.0);\n";
    6963           0 :     ss << "    if(!tmp3)\n";
    6964           0 :     ss << "        return ( GetBinomDistPMF(tmp0, tmp1, tmp2));\n";
    6965           0 :     ss << "    else \n";
    6966           0 :     ss << "    {\n";
    6967           0 :     ss << "        if(tmp0 == tmp1)\n";
    6968           0 :     ss << "            return 1.0;\n";
    6969           0 :     ss << "        else\n";
    6970           0 :     ss << "        {\n";
    6971           0 :     ss << "            double fFactor = pow(rq,tmp1);\n";
    6972           0 :     ss << "            if(tmp0 == 0.0)\n";
    6973           0 :     ss << "            return (fFactor);\n";
    6974           0 :     ss << "            else if(fFactor <= Min)\n";
    6975           0 :     ss << "            {\n";
    6976           0 :     ss << "                fFactor = pow(tmp2,tmp1);\n";
    6977           0 :     ss << "                if(fFactor <= Min)\n";
    6978           0 :     ss << "                    return GetBetaDist";
    6979           0 :     ss << "(rq, tmp1 - tmp0, tmp0 + 1.0);\n";
    6980           0 :     ss << "                else\n";
    6981           0 :     ss << "                {\n";
    6982           0 :     ss << "                    if(fFactor > fMachEps)\n";
    6983           0 :     ss << "                    {\n";
    6984           0 :     ss << "                        double fSum = 1.0 - fFactor;\n";
    6985           0 :     ss << "                        unsigned int max = ";
    6986           0 :     ss << "(unsigned int)((tmp1 - tmp0)-1);\n";
    6987           0 :     ss << "                        for (uint i = 0; i < max && fFactor > 0.0;";
    6988           0 :     ss << " i++)\n";
    6989           0 :     ss << "                        {\n";
    6990           0 :     ss << "                           fFactor *= (tmp1 - i)*pow((i + 1),-1.0)*";
    6991           0 :     ss << "rq*pow(tmp2,-1.0);\n";
    6992           0 :     ss << "                            fSum -= fFactor;\n";
    6993           0 :     ss << "                        }\n";
    6994           0 :     ss << "                         return ( (fSum < 0.0) ? 0.0 : fSum );\n";
    6995           0 :     ss << "                    }\n";
    6996           0 :     ss << "                    else \n";
    6997           0 :     ss << "                        return (lcl_GetBinomDistRange";
    6998           0 :     ss << "(tmp1, tmp1 -  tmp0, tmp1, fFactor, rq, tmp2));\n";
    6999           0 :     ss << "                }\n";
    7000           0 :     ss << "            }\n";
    7001           0 :     ss << "           else\n";
    7002           0 :     ss << "           {\n";
    7003           0 :     ss << "               double rtmp = ( lcl_GetBinomDistRange";
    7004           0 :     ss << "(tmp1, 0.0, tmp0, fFactor, tmp2, rq));\n";
    7005           0 :     ss << "               return rtmp;\n";
    7006           0 :     ss << "           }\n";
    7007           0 :     ss << "       }\n";
    7008           0 :     ss << "   }\n";
    7009           0 :     ss << "}\n";
    7010           0 : }
    7011             : 
    7012           0 : void OpChiSqDist::BinInlineFun(std::set<std::string>& decls,
    7013             :     std::set<std::string>& funs)
    7014             : {
    7015           0 :     decls.insert(fMaxGammaArgumentDecl);decls.insert(GetChiSqDistCDFDecl);
    7016           0 :     decls.insert(GetChiSqDistPDFDecl);decls.insert(GetLowRegIGammaDecl);
    7017           0 :     decls.insert(GetGammaContFractionDecl);decls.insert(GetGammaSeriesDecl);
    7018           0 :     decls.insert(fHalfMachEpsDecl);decls.insert(F_PIDecl);
    7019           0 :     decls.insert(fBigInvDecl);
    7020             : 
    7021           0 :     funs.insert(GetGammaContFraction);funs.insert(GetChiSqDistCDF);
    7022           0 :     funs.insert(GetChiSqDistPDF);funs.insert(GetLowRegIGamma);
    7023           0 :     funs.insert(GetGammaSeries);
    7024           0 : }
    7025             : 
    7026           0 : void OpChiSqDist::GenSlidingWindowFunction(
    7027             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    7028             : vSubArguments)
    7029             : {
    7030           0 :     ss << "\ndouble " << sSymName;
    7031           0 :     ss << "_"<< BinFuncName() <<"(";
    7032           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7033             :     {
    7034           0 :         if (i)
    7035           0 :             ss << ",";
    7036           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7037             :     }
    7038           0 :     ss << ") {\n";
    7039           0 :     ss << "    int gid0 = get_global_id(0);\n";
    7040           0 :     ss << "    int singleIndex = gid0;\n";
    7041           0 :     ss << "    double result = 0;\n";
    7042           0 :     if(vSubArguments.size()<2)
    7043             :     {
    7044           0 :         ss << "    result = -DBL_MAX;\n";
    7045           0 :         ss << "    return result;\n";
    7046             :     }else
    7047             :     {
    7048           0 :         GenTmpVariables(ss,vSubArguments);
    7049           0 :         CheckAllSubArgumentIsNan(ss,vSubArguments);
    7050           0 :         if(vSubArguments.size() == 2)
    7051             :         {
    7052           0 :             ss << "    int tmp2  = 1;\n";
    7053             :         }
    7054             :     }
    7055           0 :     size_t i = vSubArguments.size();
    7056           0 :     ss <<"\n";
    7057           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7058             :     {
    7059           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7060             :         assert(pCur);
    7061           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    7062             :         {
    7063             : #ifdef  ISNAN
    7064             :             const formula::SingleVectorRefToken* pSVR =
    7065           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7066           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    7067             : #endif
    7068             :         }
    7069           0 :         else if (pCur->GetType() == formula::svDouble)
    7070             :         {
    7071             : #ifdef  ISNAN
    7072           0 :             ss << "{\n";
    7073             : #endif
    7074             :         }
    7075             : 
    7076             : #ifdef  ISNAN
    7077           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    7078             :         {
    7079           0 :             ss << "    if (isNan(";
    7080           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7081           0 :             ss << "))\n";
    7082           0 :             ss << "        tmp"<<i<<"= 0;\n";
    7083           0 :             ss << "    else\n";
    7084           0 :             ss << "        tmp"<<i<<"=\n";
    7085           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7086           0 :             ss << ";\n}\n";
    7087             :         }
    7088             :         else
    7089             :         {
    7090           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    7091           0 :             ss <<";\n";
    7092             :         }
    7093             : #endif
    7094             :     }
    7095           0 :         ss << "    tmp1 = floor(tmp1);\n";
    7096           0 :         ss << "    if(tmp1 < 1.0)\n";
    7097           0 :         ss << "        result = -DBL_MAX;\n";
    7098           0 :         ss << "    else\n";
    7099           0 :         ss << "    {\n";
    7100           0 :         ss << "        if(tmp2)\n";
    7101           0 :         ss << "            result =GetChiSqDistCDF(tmp0,tmp1);\n";
    7102           0 :         ss << "        else\n";
    7103           0 :         ss << "            result =GetChiSqDistPDF(tmp0,tmp1);\n";
    7104           0 :         ss << "    }\n";
    7105           0 :         ss << "    return result;\n";
    7106           0 :         ss << "}";
    7107           0 :     }
    7108             : 
    7109           0 :  void OpChiSqInv::BinInlineFun(std::set<std::string>& decls,
    7110             :     std::set<std::string>& funs)
    7111             : {
    7112           0 :     decls.insert(fMaxGammaArgumentDecl);decls.insert(GetChiSqDistCDFDecl);
    7113           0 :     decls.insert(GetLowRegIGammaDecl);decls.insert(lcl_IterateInverseChiSQInvDecl);
    7114           0 :     decls.insert(GetGammaContFractionDecl);decls.insert(GetGammaSeriesDecl);
    7115           0 :     decls.insert(fHalfMachEpsDecl);decls.insert(F_PIDecl);
    7116           0 :     decls.insert(fBigInvDecl);decls.insert(lcl_HasChangeOfSignDecl);
    7117           0 :     decls.insert(fMachEpsDecl);
    7118             : 
    7119           0 :     funs.insert(GetGammaContFraction);funs.insert(GetChiSqDistCDF);
    7120           0 :     funs.insert(GetLowRegIGamma);funs.insert(lcl_HasChangeOfSign);
    7121           0 :     funs.insert(GetGammaSeries);funs.insert(lcl_IterateInverseChiSQInv);
    7122           0 : }
    7123             : 
    7124           0 : void OpChiSqInv::GenSlidingWindowFunction(
    7125             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    7126             : vSubArguments)
    7127             : {
    7128           0 :     ss << "\ndouble " << sSymName;
    7129           0 :     ss << "_"<< BinFuncName() <<"(";
    7130           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7131             :     {
    7132           0 :         if (i)
    7133           0 :             ss << ",";
    7134           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7135             :     }
    7136           0 :     ss << ") {\n";
    7137           0 :     ss << "    int gid0 = get_global_id(0);\n";
    7138           0 :     ss << "    int singleIndex = gid0;\n";
    7139           0 :     ss << "    double result = 0;\n";
    7140           0 :     if(vSubArguments.size()!=2)
    7141             :     {
    7142           0 :         ss << "    result = -DBL_MAX;\n";
    7143           0 :         ss << "    return result;\n";
    7144             :     }
    7145             :     else
    7146             :     {
    7147           0 :         GenTmpVariables(ss,vSubArguments);
    7148           0 :         CheckAllSubArgumentIsNan(ss,vSubArguments);
    7149           0 :         size_t i = vSubArguments.size();
    7150           0 :     ss <<"\n";
    7151           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7152             :     {
    7153           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7154             :         assert(pCur);
    7155           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    7156             :         {
    7157             : #ifdef  ISNAN
    7158             :             const formula::SingleVectorRefToken* pSVR =
    7159           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7160           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    7161             : #endif
    7162             :         }
    7163           0 :         else if (pCur->GetType() == formula::svDouble)
    7164             :         {
    7165             : #ifdef  ISNAN
    7166           0 :             ss << "{\n";
    7167             : #endif
    7168             :         }
    7169             : 
    7170             : #ifdef  ISNAN
    7171           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    7172             :         {
    7173           0 :             ss << "    if (isNan(";
    7174           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7175           0 :             ss << "))\n";
    7176           0 :             ss << "        tmp"<<i<<"= 0;\n";
    7177           0 :             ss << "    else\n";
    7178           0 :             ss << "        tmp"<<i<<"=\n";
    7179           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7180           0 :             ss << ";\n}\n";
    7181             :         }
    7182             :         else
    7183             :         {
    7184           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    7185           0 :             ss <<";\n";
    7186             :         }
    7187             : #endif
    7188             :     }
    7189           0 :         ss << "    tmp1 = floor(tmp1);\n";
    7190           0 :         ss << "    bool bConvError;\n";
    7191           0 :         ss << "    if(tmp1 < 1.0 || tmp0 < 0 || tmp0>=1.0)\n";
    7192           0 :         ss << "        result = -DBL_MAX;\n";
    7193           0 :         ss << "    else\n";
    7194           0 :         ss << "    {\n";
    7195           0 :         ss << "        result =lcl_IterateInverseChiSQInv( tmp0, tmp1,";
    7196           0 :         ss << "tmp1*0.5, tmp1, &bConvError );\n";
    7197           0 :         ss << "    }\n";
    7198           0 :         ss << "    if(bConvError)\n";
    7199           0 :         ss << "        result = -DBL_MAX;\n";
    7200           0 :         ss << "    return result;\n";
    7201           0 :         ss << "}";
    7202             :     }
    7203             : 
    7204           0 : }
    7205           0 : void OpGammaInv::BinInlineFun(std::set<std::string>& decls,
    7206             :     std::set<std::string>& funs)
    7207             : {
    7208           0 :     decls.insert(fBigInvDecl);decls.insert(fHalfMachEpsDecl);
    7209           0 :     decls.insert(GetGammaSeriesDecl);decls.insert(GetGammaContFractionDecl);
    7210           0 :     decls.insert(GetGammaInvValueDecl);
    7211           0 :     funs.insert(GetGammaSeries);funs.insert(GetGammaContFraction);
    7212           0 :     funs.insert(GetGammaInvValue);
    7213           0 : }
    7214             : 
    7215           0 : void OpGammaInv::GenSlidingWindowFunction(std::stringstream &ss,
    7216             :             const std::string &sSymName, SubArguments &vSubArguments)
    7217             : {
    7218           0 :     ss << "\ndouble " << sSymName;
    7219           0 :     ss << "_"<< BinFuncName() <<"(";
    7220           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7221             :     {
    7222           0 :         if (i)
    7223           0 :             ss << ",";
    7224           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7225             :     }
    7226           0 :     ss << ") {\n";
    7227           0 :     ss << "    int gid0=get_global_id(0);\n";
    7228           0 :     ss << "    double tmp;\n";
    7229           0 :     ss << "    double arg0,arg1,arg2;\n";
    7230           0 :     size_t i = vSubArguments.size();
    7231             : 
    7232           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7233             :     {
    7234           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7235             :         assert(pCur);
    7236           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    7237             :         {
    7238             :             const formula::DoubleVectorRefToken* pDVR =
    7239           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    7240           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    7241           0 :             ss << "for (int i = ";
    7242           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    7243             : #ifdef  ISNAN
    7244           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    7245           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    7246             : #else
    7247             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    7248             : #endif
    7249           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    7250             : #ifdef  ISNAN
    7251           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    7252           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    7253             : #else
    7254             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    7255             : #endif
    7256           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    7257             : #ifdef  ISNAN
    7258           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    7259           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    7260             : #else
    7261             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7262             : #endif
    7263             :             }
    7264             :             else {
    7265             : #ifdef  ISNAN
    7266           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    7267             : #else
    7268             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7269             : #endif
    7270             :             }
    7271             :         }
    7272           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    7273             :         {
    7274             : #ifdef  ISNAN
    7275             :             const formula::SingleVectorRefToken* pSVR =
    7276           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7277           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    7278           0 :             ss << "    {\n";
    7279           0 :             ss << "        if (isNan(";
    7280           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7281           0 :             ss << "))\n";
    7282           0 :             ss << "            arg"<<i<<"= 0;\n";
    7283           0 :             ss << "        else\n";
    7284           0 :             ss << "            arg"<<i<<"=";
    7285           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    7286           0 :             ss << ";\n";
    7287           0 :             ss << "    }\n";
    7288           0 :             ss << "    else\n";
    7289           0 :             ss << "        arg"<<i<<"= 0;\n";
    7290             : #endif
    7291             :         }
    7292           0 :         else if (pCur->GetType() == formula::svDouble)
    7293             :         {
    7294             : #ifdef  ISNAN
    7295           0 :             ss << "    if (isNan(";
    7296           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7297           0 :             ss << "))\n";
    7298           0 :             ss << "        arg"<<i<<"= 0;\n";
    7299           0 :             ss << "    else\n";
    7300           0 :             ss << "        arg"<<i<<"=";
    7301           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    7302           0 :             ss << ";\n";
    7303             : #endif
    7304             :         }
    7305             :     }
    7306             :     ss << "    if (arg0 == 0.0)\n"
    7307             :     "    {\n"
    7308             :     "        tmp=0.0;\n"
    7309             :     "        return tmp;\n"
    7310             :     "    }\n"
    7311             :     "    else\n"
    7312             :     "    {\n"
    7313             :     "        bool bConvError;\n"
    7314             :     "        double fStart = arg1 * arg2;\n"
    7315             :     "        double fAx=fStart*0.5;\n"
    7316             :     "        double fBx=fStart;\n"
    7317             :     "        bConvError = false;\n"
    7318             :     "        double fYEps = 1.0E-307;\n"
    7319             :     "        double fXEps = 2.22045e-016;\n"
    7320             :     "        double fAy = arg0-GetGammaInvValue(arg1,arg2,fAx);\n"
    7321             :     "        double fBy = arg0-GetGammaInvValue(arg1,arg2,fBx);\n"
    7322             :     "        double fTemp;\n"
    7323             :     "        unsigned short nCount;\n"
    7324             :     "        for (nCount = 0; nCount < 1000 && !((fAy < 0.0 && fBy > 0.0)"
    7325             :     " || (fAy > 0.0 && fBy < 0.0)); nCount++)\n"
    7326             :     "        {\n"
    7327             :     "            if (fabs(fAy) <= fabs(fBy))\n"
    7328             :     "            {\n"
    7329             :     "                fTemp = fAx;\n"
    7330             :     "                fAx += 2.0 * (fAx - fBx);\n"
    7331             :     "                if (fAx < 0.0)\n"
    7332             :     "                    fAx = 0.0;\n"
    7333             :     "                fBx = fTemp;\n"
    7334             :     "                fBy = fAy;\n"
    7335             :     "                fAy = arg0-GetGammaInvValue(arg1,arg2,fAx);\n"
    7336             :     "            }\n"
    7337             :     "            else\n"
    7338             :     "            {\n"
    7339             :     "                fTemp = fBx;\n"
    7340             :     "                fBx += 2.0 * (fBx - fAx);\n"
    7341             :     "                fAx = fTemp;\n"
    7342             :     "                fAy = fBy;\n"
    7343             :     "                fBy = arg0-GetGammaInvValue(arg1,arg2,fBx);\n"
    7344             :     "            }\n"
    7345             :     "        }\n"
    7346             :     "        if (fAy == 0.0)\n"
    7347             :     "        {\n"
    7348             :     "            tmp = fAx;\n"
    7349             :     "            return tmp;\n"
    7350             :     "        }\n"
    7351             :     "        if (fBy == 0.0)\n"
    7352             :     "        {\n"
    7353             :     "            tmp = fBx;\n"
    7354             :     "            return tmp;\n"
    7355             :     "        }\n"
    7356             :     "        if (!((fAy < 0.0 && fBy > 0.0) || (fAy > 0.0 && fBy < 0.0)))\n"
    7357             :     "        {\n"
    7358             :     "            bConvError = true;\n"
    7359             :     "            tmp = 0.0;\n"
    7360             :     "            return tmp;\n"
    7361             :     "        }\n"
    7362             :     "        double fPx = fAx;\n"
    7363             :     "        double fPy = fAy;\n"
    7364             :     "        double fQx = fBx;\n"
    7365             :     "        double fQy = fBy;\n"
    7366             :     "        double fRx = fAx;\n"
    7367             :     "        double fRy = fAy;\n"
    7368             :     "        double fSx = 0.5 * (fAx + fBx);\n"
    7369             :     "        bool bHasToInterpolate = true;\n"
    7370             :     "        nCount = 0;\n"
    7371             :     "        while ( nCount < 500 && fabs(fRy) > fYEps &&"
    7372             :     "(fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
    7373             :     "        {\n"
    7374             :     "            if (bHasToInterpolate)\n"
    7375             :     "            {\n"
    7376             :     "                if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
    7377             :     "                {\n"
    7378             :     "                    fSx = fPx * fRy * fQy *pow( (fRy-fPy),-1) *pow"
    7379             :     "( (fQy-fPy),-1)"
    7380             :     "+ fRx * fQy * fPy *pow( (fQy-fRy),-1) *pow( (fPy-fRy),-1)"
    7381             :     "+ fQx * fPy * fRy *pow( (fPy-fQy),-1) *pow( (fRy-fQy),-1);\n"
    7382             :     "                    bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
    7383             :     "                }\n"
    7384             :     "                else\n"
    7385             :     "                    bHasToInterpolate = false;\n"
    7386             :     "            }\n"
    7387             :     "            if(!bHasToInterpolate)\n"
    7388             :     "            {\n"
    7389             :     "                fSx = 0.5 * (fAx + fBx);\n"
    7390             :     "                fPx = fAx; fPy = fAy;\n"
    7391             :     "                fQx = fBx; fQy = fBy;\n"
    7392             :     "                bHasToInterpolate = true;\n"
    7393             :     "            }\n"
    7394             :     "            fPx = fQx; fQx = fRx; fRx = fSx;\n"
    7395             :     "            fPy = fQy; fQy = fRy;\n"
    7396             :     "            fRy =  arg0-GetGammaInvValue(arg1,arg2,fSx);\n"
    7397             :     "            if ((fAy < 0.0 && fRy > 0.0) || (fAy > 0.0 && fRy < 0.0))\n"
    7398             :     "            {\n"
    7399             :     "                fBx = fRx;\n"
    7400             :     "                fBy = fRy;\n"
    7401             :     "            }\n"
    7402             :     "            else\n"
    7403             :     "            {\n"
    7404             :     "                fAx = fRx;\n"
    7405             :     "                fAy = fRy;\n"
    7406             :     "            }\n"
    7407             :     "            bHasToInterpolate = bHasToInterpolate && (fabs(fRy)"
    7408             :     " * 2.0 <= fabs(fQy));\n"
    7409             :     "            ++nCount;\n"
    7410             :     "        }\n"
    7411             :     "        tmp = fRx;\n"
    7412             :     "        return tmp;\n"
    7413             :     "    }\n"
    7414           0 :     "}\n";
    7415           0 : }
    7416           0 : void OpFInv::BinInlineFun(std::set<std::string>& decls,
    7417             :     std::set<std::string>& funs)
    7418             : {
    7419           0 :     decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
    7420           0 :     decls.insert(lcl_getLanczosSumDecl);decls.insert(GetBetaDecl);
    7421           0 :     decls.insert(GetLogBetaDecl);decls.insert(GetBetaDistPDFDecl);
    7422           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetFInvValueDecl);
    7423           0 :     funs.insert(lcl_getLanczosSum);funs.insert(GetBeta);
    7424           0 :     funs.insert(GetLogBeta);funs.insert(GetBetaDistPDF);
    7425           0 :     funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetFInvValue);
    7426           0 : }
    7427             : 
    7428           0 : void OpFInv::GenSlidingWindowFunction(std::stringstream &ss,
    7429             :             const std::string &sSymName, SubArguments &vSubArguments)
    7430             : {
    7431           0 :     ss << "\ndouble " << sSymName;
    7432           0 :     ss << "_"<< BinFuncName() <<"(";
    7433           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7434             :     {
    7435           0 :         if (i)
    7436           0 :             ss << ",";
    7437           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7438             :     }
    7439           0 :     ss << ") {\n";
    7440           0 :     ss << "    int gid0=get_global_id(0);\n";
    7441           0 :     ss << "    double tmp;\n";
    7442           0 :     ss << "    double arg0,arg1,arg2;\n";
    7443           0 :     size_t i = vSubArguments.size();
    7444             : 
    7445           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7446             :     {
    7447           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7448             :         assert(pCur);
    7449           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    7450             :         {
    7451             :             const formula::DoubleVectorRefToken* pDVR =
    7452           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    7453           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    7454           0 :             ss << "for (int i = ";
    7455           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    7456             : #ifdef  ISNAN
    7457           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    7458           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    7459             : #else
    7460             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    7461             : #endif
    7462           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    7463             : #ifdef  ISNAN
    7464           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    7465           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    7466             : #else
    7467             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    7468             : #endif
    7469           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    7470             : #ifdef  ISNAN
    7471           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    7472           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    7473             : #else
    7474             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7475             : #endif
    7476             :             }
    7477             :             else {
    7478             : #ifdef  ISNAN
    7479           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    7480             : #else
    7481             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7482             : #endif
    7483             :             }
    7484             :         }
    7485           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    7486             :         {
    7487             : #ifdef  ISNAN
    7488             :             const formula::SingleVectorRefToken* pSVR =
    7489           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7490           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    7491           0 :             ss << "    {\n";
    7492           0 :             ss << "        if (isNan(";
    7493           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7494           0 :             ss << "))\n";
    7495           0 :             ss << "            arg"<<i<<"= 0;\n";
    7496           0 :             ss << "        else\n";
    7497           0 :             ss << "            arg"<<i<<"="<<vSubArguments[i]->
    7498           0 :                                               GenSlidingWindowDeclRef();
    7499           0 :             ss << ";\n";
    7500           0 :             ss << "    }\n";
    7501           0 :             ss << "    else\n";
    7502           0 :             ss << "        arg"<<i<<"= 0;\n";
    7503             : #endif
    7504             :         }
    7505           0 :         else if (pCur->GetType() == formula::svDouble)
    7506             :         {
    7507             : #ifdef  ISNAN
    7508           0 :             ss << "    if (isNan(";
    7509           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7510           0 :             ss << "))\n";
    7511           0 :             ss << "        arg"<<i<<"= 0;\n";
    7512           0 :             ss << "    else\n";
    7513           0 :             ss << "        arg"<<i<<"="<<vSubArguments[i]->
    7514           0 :                                              GenSlidingWindowDeclRef();
    7515           0 :             ss << ";\n";
    7516             : #endif
    7517             :         }
    7518             :     }
    7519             :     ss << "    double fF2=floor(arg2);\n"
    7520             :     "    double fF1=floor(arg1);\n"
    7521             :     "    bool bConvError;\n"
    7522             :     "    double fAx=fF1*0.5;\n"
    7523             :     "    double fBx=fF1;\n"
    7524             :     "    bConvError = false;\n"
    7525             :     "    const double fYEps = 1.0E-307;\n"
    7526             :     "    const double fXEps = 2.22045e-016;\n"
    7527             :     "    double fAy = arg0-GetFInvValue(fF1,fF2,fAx);\n"
    7528             :     "    double fBy = arg0-GetFInvValue(fF1,fF2,fBx);\n"
    7529             :     "    double fTemp;\n"
    7530             :     "    unsigned short nCount;\n"
    7531             :     "    for (nCount = 0; nCount < 1000 && !((fAy < 0.0 && fBy > 0.0)"
    7532             :     " || (fAy > 0.0 && fBy < 0.0)); nCount++)\n"
    7533             :     "    {\n"
    7534             :     "        if (fabs(fAy) <= fabs(fBy))\n"
    7535             :     "        {\n"
    7536             :     "            fTemp = fAx;\n"
    7537             :     "            fAx += 2.0 * (fAx - fBx);\n"
    7538             :     "            if (fAx < 0.0)\n"
    7539             :     "                fAx = 0.0;\n"
    7540             :     "            fBx = fTemp;\n"
    7541             :     "            fBy = fAy;\n"
    7542             :     "            fAy = arg0-GetFInvValue(fF1,fF2,fAx);\n"
    7543             :     "        }\n"
    7544             :     "        else\n"
    7545             :     "        {\n"
    7546             :     "            fTemp = fBx;\n"
    7547             :     "            fBx += 2.0 * (fBx - fAx);\n"
    7548             :     "            fAx = fTemp;\n"
    7549             :     "            fAy = fBy;\n"
    7550             :     "            fBy = arg0-GetFInvValue(fF1,fF2,fBx);\n"
    7551             :     "        }\n"
    7552             :     "    }\n"
    7553             :     "    if (fAy == 0.0)\n"
    7554             :     "    {\n"
    7555             :     "        tmp = fAx;\n"
    7556             :     "        return tmp;\n"
    7557             :     "    }\n"
    7558             :     "    if (fBy == 0.0)\n"
    7559             :     "    {\n"
    7560             :     "        tmp = fBx;\n"
    7561             :     "        return tmp;\n"
    7562             :     "    }\n"
    7563             :     "    if (!((fAy < 0.0 && fBy > 0.0) || (fAy > 0.0 && fBy < 0.0)))\n"
    7564             :     "    {\n"
    7565             :     "        bConvError = true;\n"
    7566             :     "        tmp = 0.0;\n"
    7567             :     "        return tmp;\n"
    7568             :     "    }\n"
    7569             :     "    double fPx = fAx;\n"
    7570             :     "    double fPy = fAy;\n"
    7571             :     "    double fQx = fBx;\n"
    7572             :     "    double fQy = fBy;\n"
    7573             :     "    double fRx = fAx;\n"
    7574             :     "    double fRy = fAy;\n"
    7575             :     "    double fSx = 0.5 * (fAx + fBx);\n"
    7576             :     "    bool bHasToInterpolate = true;\n"
    7577             :     "    nCount = 0;\n"
    7578             :     "    while ( nCount < 500 && fabs(fRy) > fYEps &&"
    7579             :     "(fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
    7580             :     "    {\n"
    7581             :     "        if (bHasToInterpolate)\n"
    7582             :     "        {\n"
    7583             :     "            if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
    7584             :     "            {\n"
    7585             :     "                fSx = fPx * fRy * fQy *pow( (fRy-fPy),-1)"
    7586             :     " *pow( (fQy-fPy),-1)+fRx * fQy * fPy*pow( (fQy-fRy),-1) *"
    7587             :     "pow( (fPy-fRy),-1)+ fQx * fPy * fRy *pow( (fPy-fQy),-1)"
    7588             :     " *pow((fRy-fQy),-1);\n"
    7589             :     "                bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
    7590             :     "            }\n"
    7591             :     "            else\n"
    7592             :     "                bHasToInterpolate = false;\n"
    7593             :     "        }\n"
    7594             :     "        if(!bHasToInterpolate)\n"
    7595             :     "        {\n"
    7596             :     "            fSx = 0.5 * (fAx + fBx);\n"
    7597             :     "            fPx = fAx; fPy = fAy;\n"
    7598             :     "            fQx = fBx; fQy = fBy;\n"
    7599             :     "            bHasToInterpolate = true;\n"
    7600             :     "        }\n"
    7601             :     "        fPx = fQx; fQx = fRx; fRx = fSx;\n"
    7602             :     "        fPy = fQy; fQy = fRy;\n"
    7603             :     "        fRy = arg0-GetFInvValue(fF1,fF2,fSx);\n"
    7604             :     "        if ((fAy < 0.0 && fRy > 0.0) || (fAy > 0.0 && fRy < 0.0))\n"
    7605             :     "        {\n"
    7606             :     "            fBx = fRx; fBy = fRy;\n"
    7607             :     "        }\n"
    7608             :     "        else\n"
    7609             :     "        {\n"
    7610             :     "            fAx = fRx; fAy = fRy;\n"
    7611             :     "        }\n"
    7612             :     "        bHasToInterpolate = bHasToInterpolate && (fabs(fRy)"
    7613             :     " * 2.0 <= fabs(fQy));\n"
    7614             :     "        ++nCount;\n"
    7615             :     "    }\n"
    7616             :     "    tmp = fRx;\n"
    7617             :     "    return tmp;"
    7618           0 :     "}";
    7619           0 : }
    7620           0 : void OpFTest::BinInlineFun(std::set<std::string>& decls,
    7621             :     std::set<std::string>& funs)
    7622             : {
    7623           0 :     decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
    7624           0 :     decls.insert(lcl_getLanczosSumDecl);decls.insert(GetBetaDecl);
    7625           0 :     decls.insert(GetLogBetaDecl);decls.insert(GetBetaDistPDFDecl);
    7626           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetBetaDistDecl);
    7627           0 :     decls.insert(GetFDistDecl);
    7628           0 :     funs.insert(lcl_getLanczosSum);funs.insert(GetBeta);
    7629           0 :     funs.insert(GetLogBeta);funs.insert(GetBetaDistPDF);
    7630           0 :     funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetBetaDist);
    7631           0 :     funs.insert(GetFDist);
    7632           0 : }
    7633           0 : void OpFTest::GenSlidingWindowFunction(std::stringstream &ss,
    7634             :             const std::string &sSymName, SubArguments &vSubArguments)
    7635             : {
    7636           0 :     FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
    7637             :     assert(pCur);
    7638             :     const formula::DoubleVectorRefToken* pCurDVR =
    7639           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur);
    7640           0 :     size_t nCurWindowSize = pCurDVR->GetRefRowSize();
    7641           0 :     FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
    7642             :     assert(pCur1);
    7643             :     const formula::DoubleVectorRefToken* pCurDVR1 =
    7644           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur1);
    7645           0 :     size_t nCurWindowSize1 = pCurDVR1->GetRefRowSize();
    7646           0 :     ss << "\ndouble " << sSymName;
    7647           0 :     ss << "_"<< BinFuncName() <<"( ";
    7648           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7649             :     {
    7650           0 :         if (i)
    7651           0 :             ss << ",";
    7652           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7653             :     }
    7654           0 :     ss << ") {\n";
    7655           0 :     ss << "    int gid0 = get_global_id(0);\n";
    7656           0 :     ss << "    double fSum1 = 0.0;\n";
    7657           0 :     ss << "    double fSumSqr1 = 0.0;\n";
    7658           0 :     ss << "    double fSum2 = 0.0;\n";
    7659           0 :     ss << "    double fSumSqr2 = 0.0;\n";
    7660           0 :     ss << "    int length0="<<nCurWindowSize;
    7661           0 :     ss << ";\n";
    7662           0 :     ss << "    int length1= "<<nCurWindowSize1;
    7663           0 :     ss << ";\n";
    7664           0 :     ss << "    double tmp = 0;\n";
    7665           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7666             :     {
    7667           0 :         FormulaToken *pCurSub = vSubArguments[i]->GetFormulaToken();
    7668             :         assert(pCurSub);
    7669           0 :         if (pCurSub->GetType() == formula::svDoubleVectorRef)
    7670             :         {
    7671             :             const formula::DoubleVectorRefToken* pDVR =
    7672           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCurSub);
    7673           0 :             ss << "    for (int i = ";
    7674             : #ifdef  ISNAN
    7675           0 :             ss << "0; i < "<< pDVR->GetRefRowSize() << "; i++){\n";
    7676           0 :             ss << "        double arg"<<i<<" = ";
    7677           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef(true);
    7678           0 :             ss << ";\n";
    7679             : #ifdef ISNAN
    7680           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
    7681           0 :             ss << pDVR->GetArrayLength();
    7682           0 :             ss << "))\n";
    7683           0 :             ss << "        {\n";
    7684           0 :             ss << "            length"<<i<<"--;\n";
    7685           0 :             ss << "            continue;\n";
    7686           0 :             ss << "        }\n";
    7687             : #endif
    7688           0 :             ss << "        fSum"<<i+1<<" += arg"<<i<<";\n";
    7689           0 :             ss << "        fSumSqr"<<i+1<<" += arg"<<i;
    7690           0 :             ss << " * arg"<<i<<";\n";
    7691           0 :             ss << "    }\n";
    7692             : #endif
    7693             :         }
    7694           0 :         else if (pCurSub->GetType() == formula::svSingleVectorRef)
    7695             :         {
    7696             : #ifdef  ISNAN
    7697           0 :             ss << "return HUGE_VAL";
    7698             : #endif
    7699             :         }
    7700           0 :         else if (pCurSub->GetType() == formula::svDouble)
    7701             :         {
    7702             : #ifdef  ISNAN
    7703           0 :             ss << "return HUGE_VAL";
    7704             : #endif
    7705             :         }
    7706             :     }
    7707             :     ss << "    double fS1 = (fSumSqr1-fSum1*fSum1/length0)/(length0-1.0);\n"
    7708             :         "    double fS2 = (fSumSqr2-fSum2*fSum2/length1)/(length1-1.0);\n"
    7709             :         "    double fF, fF1, fF2;\n"
    7710             :         "    if (fS1 > fS2)\n"
    7711             :         "    {\n"
    7712             :         "        fF = fS1/fS2;\n"
    7713             :         "        fF1 = length0-1.0;\n"
    7714             :         "        fF2 = length1-1.0;\n"
    7715             :         "    }\n"
    7716             :         "    else\n"
    7717             :         "    {\n"
    7718             :         "        fF = fS2/fS1;\n"
    7719             :         "        fF1 = length1-1.0;\n"
    7720             :         "        fF2 = length0-1.0;\n"
    7721             :         "    }\n"
    7722           0 :         "    tmp = 2.0*GetFDist(fF, fF1, fF2);\n";
    7723           0 :     ss << "    return tmp;\n";
    7724           0 :     ss << "}";
    7725           0 : }
    7726           0 : void OpB::BinInlineFun(std::set<std::string>& decls,
    7727             :     std::set<std::string>& funs)
    7728             : {
    7729             :     //decls.insert(fBigInvDecl);decls.insert(fLogDblMaxDecl);
    7730           0 :     decls.insert(GetBinomDistPMFDecl);decls.insert(MinDecl);
    7731           0 :     decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
    7732           0 :     decls.insert(GetBetaDistDecl);decls.insert(GetBetaDistPDFDecl);
    7733           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetLogBetaDecl);
    7734           0 :     decls.insert(lcl_getLanczosSumDecl); decls.insert(GetBetaDecl);
    7735           0 :     funs.insert(GetBinomDistPMF);funs.insert(lcl_GetBinomDistRange);
    7736           0 :     funs.insert(GetBetaDist);funs.insert(GetBetaDistPDF);
    7737           0 :     funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetLogBeta);
    7738           0 :     funs.insert(lcl_getLanczosSum);funs.insert(GetBeta);
    7739           0 : }
    7740             : 
    7741           0 : void OpB::GenSlidingWindowFunction(std::stringstream &ss,
    7742             :             const std::string &sSymName, SubArguments &vSubArguments)
    7743             : {
    7744           0 :     ss << "\ndouble " << sSymName;
    7745           0 :     ss << "_"<< BinFuncName() <<"(";
    7746           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7747             :     {
    7748           0 :         if (i)
    7749           0 :             ss << ",";
    7750           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7751             :     }
    7752           0 :     ss << ") {\n";
    7753           0 :     ss << "    int gid0=get_global_id(0);\n";
    7754           0 :     ss << "    double min = 2.22507e-308;\n";
    7755           0 :     ss << "    double tmp;\n";
    7756           0 :     ss << "    double arg0,arg1,arg2,arg3;\n";
    7757           0 :     size_t i = vSubArguments.size();
    7758           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7759             :     {
    7760           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7761             :         assert(pCur);
    7762           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    7763             :         {
    7764             :             const formula::DoubleVectorRefToken* pDVR =
    7765           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    7766           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    7767           0 :             ss << "for (int i = ";
    7768           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    7769             : #ifdef  ISNAN
    7770           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    7771           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    7772             : #else
    7773             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    7774             : #endif
    7775           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    7776             : #ifdef  ISNAN
    7777           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    7778           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    7779             : #else
    7780             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    7781             : #endif
    7782           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    7783             : #ifdef  ISNAN
    7784           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    7785           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    7786             : #else
    7787             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7788             : #endif
    7789             :             }
    7790             :             else {
    7791             : #ifdef  ISNAN
    7792           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    7793             : #else
    7794             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7795             : #endif
    7796             :             }
    7797             :         }
    7798           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    7799             :         {
    7800             : #ifdef  ISNAN
    7801             :             const formula::SingleVectorRefToken* pSVR =
    7802           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7803           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    7804           0 :             ss << "    {\n";
    7805           0 :             ss << "        if (isNan(";
    7806           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7807           0 :             ss << "))\n";
    7808           0 :             ss << "            arg"<<i<<"= 0;\n";
    7809           0 :             ss << "        else\n";
    7810           0 :             ss << "            arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    7811           0 :             ss << ";\n";
    7812           0 :             ss << "    }\n";
    7813           0 :             ss << "    else\n";
    7814           0 :             ss << "        arg"<<i<<"= 0;\n";
    7815             : #endif
    7816             :         }
    7817           0 :         else if (pCur->GetType() == formula::svDouble)
    7818             :         {
    7819             : #ifdef  ISNAN
    7820           0 :             ss << "    if (isNan(";
    7821           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7822           0 :             ss << "))\n";
    7823           0 :             ss << "        arg"<<i<<"= 0;\n";
    7824           0 :             ss << "    else\n";
    7825           0 :             ss << "        arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    7826           0 :             ss << ";\n";
    7827             : #endif
    7828             :         }
    7829             :     }
    7830             :     ss << "    double rxs = floor(arg2);\n"
    7831             :     "    double rxe = floor(arg3);\n"
    7832             :     "    double rn = floor(arg0);\n"
    7833             :     "    double rq = (0.5 - arg1) + 0.5;\n"
    7834             :     "    bool bIsValidX = (0.0 <= rxs && rxs <= rxe && rxe <= rn);\n"
    7835             :     "    if (bIsValidX && 0.0 < arg1 && arg1 < 1.0)\n"
    7836             :     "    {\n"
    7837             :     "        if (rxs == rxe)\n"
    7838             :     "            tmp = GetBinomDistPMF(rxs, rn, arg1);\n"
    7839             :     "        else\n"
    7840             :     "        {\n"
    7841             :     "            double fFactor = pow(rq, rn);\n"
    7842             :     "            if (fFactor > min)\n"
    7843             :     "                tmp ="
    7844             :     " lcl_GetBinomDistRange(rn, rxs, rxe, fFactor, arg1, rq);\n"
    7845             :     "            else\n"
    7846             :     "            {\n"
    7847             :     "                fFactor = pow(arg1, rn);\n"
    7848             :     "                if (fFactor > min)\n"
    7849             :     "                {\n"
    7850             :     "                    tmp ="
    7851             :     "lcl_GetBinomDistRange(rn, rn - rxe, rn - rxs, fFactor, rq, arg1);\n"
    7852             :     "            }\n"
    7853             :     "                else\n"
    7854             :     "                    tmp ="
    7855             :     "GetBetaDist(rq, rn - rxe, rxe + 1.0)"
    7856             :     "- GetBetaDist(rq, rn - rxs + 1, rxs);\n"
    7857             :     "            }\n"
    7858             :     "        }\n"
    7859             :     "    }\n"
    7860             :     "    else\n"
    7861             :     "    {\n"
    7862             :     "        if (bIsValidX)\n"
    7863             :     "        {\n"
    7864             :     "            if (arg1 == 0.0)\n"
    7865             :     "                tmp = (rxs == 0.0 ? 1.0 : 0.0);\n"
    7866             :     "            else if (arg1 == 1.0)\n"
    7867             :     "                tmp = (rxe == rn ? 1.0 : 0.0);\n"
    7868             :     "            else\n"
    7869             :     "            {\n"
    7870             :     "                tmp = DBL_MIN;\n"
    7871             :     "            }\n"
    7872             :     "        }\n"
    7873             :     "        else\n"
    7874             :     "        {\n"
    7875             :     "            tmp = DBL_MIN;\n"
    7876             :     "        }\n"
    7877             :     "    }\n"
    7878             :     "    return tmp;"
    7879           0 :     "}\n";
    7880           0 : }
    7881           0 : void OpBetaDist::BinInlineFun(std::set<std::string>& decls,
    7882             :     std::set<std::string>& funs)
    7883             : {
    7884           0 :     decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
    7885           0 :     decls.insert(GetBetaDistDecl);decls.insert(GetBetaDistPDFDecl);
    7886           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetLogBetaDecl);
    7887           0 :     decls.insert(GetBetaDecl);decls.insert(lcl_getLanczosSumDecl);
    7888           0 :     funs.insert(GetBetaDist);funs.insert(GetBetaDistPDF);
    7889           0 :     funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetLogBeta);
    7890           0 :     funs.insert(GetBeta);funs.insert(lcl_getLanczosSum);
    7891           0 : }
    7892           0 : void OpPoisson::BinInlineFun(std::set<std::string>& decls,
    7893             :     std::set<std::string>& funs)
    7894             : {
    7895           0 :     decls.insert(fHalfMachEpsDecl);
    7896           0 :     funs.insert("");
    7897           0 :     decls.insert(fMaxGammaArgumentDecl);
    7898           0 :     funs.insert("");
    7899           0 :     decls.insert(fBigInvDecl);
    7900           0 :     funs.insert("");
    7901           0 :     decls.insert(GetLogGammaDecl);
    7902           0 :     funs.insert(GetLogGamma);
    7903           0 :     decls.insert(lcl_GetLogGammaHelperDecl);
    7904           0 :     funs.insert(lcl_GetLogGammaHelper);
    7905           0 :     decls.insert(lcl_GetGammaHelperDecl);
    7906           0 :     funs.insert(lcl_GetGammaHelper);
    7907           0 :     decls.insert(lcl_getLanczosSumDecl);
    7908           0 :     funs.insert(lcl_getLanczosSum);
    7909           0 :     decls.insert(GetUpRegIGammaDecl);
    7910           0 :     funs.insert(GetUpRegIGamma);
    7911           0 :     decls.insert(GetGammaContFractionDecl);
    7912           0 :     funs.insert(GetGammaContFraction);
    7913           0 :     decls.insert(GetGammaSeriesDecl);
    7914           0 :     funs.insert(GetGammaSeries);
    7915           0 : }
    7916           0 : void OpPoisson::GenSlidingWindowFunction(
    7917             :     std::stringstream &ss,const std::string &sSymName,
    7918             :     SubArguments &vSubArguments)
    7919             : {
    7920           0 :     ss << "\ndouble " << sSymName;
    7921           0 :     ss << "_"<< BinFuncName() <<"(";
    7922           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7923             :     {
    7924           0 :         if (i)
    7925           0 :             ss << ",";
    7926           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7927             :     }
    7928           0 :     ss << ")\n";
    7929           0 :     ss << "{\n";
    7930           0 :     ss << "    double x,lambda,tmp,tmp0,tmp1,tmp2;\n";
    7931           0 :     ss << "    int bCumulative;\n";
    7932           0 :     ss << "    int gid0=get_global_id(0);\n";
    7933           0 :     size_t i = vSubArguments.size();
    7934           0 :     ss <<"\n    ";
    7935           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7936             :     {
    7937           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7938             :         assert(pCur);
    7939           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    7940             :         {
    7941             : #ifdef  ISNAN
    7942             :             const formula::SingleVectorRefToken* pSVR =
    7943           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7944           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    7945             : #endif
    7946             :         }
    7947           0 :         else if (pCur->GetType() == formula::svDouble)
    7948             :         {
    7949             : #ifdef  ISNAN
    7950           0 :             ss << "{\n";
    7951             : #endif
    7952             :         }
    7953             :         else
    7954             :         {
    7955             : #ifdef  ISNAN
    7956             : #endif
    7957             :         }
    7958             : #ifdef  ISNAN
    7959           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    7960             :         {
    7961           0 :             ss << "    if (isNan(";
    7962           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7963           0 :             ss << "))\n";
    7964           0 :             ss << "        tmp"<<i<<"= 0;\n";
    7965           0 :             ss << "    else\n";
    7966           0 :             ss << "        tmp"<<i<<"=\n";
    7967           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7968           0 :             ss << ";\n}\n";
    7969             :         }
    7970             :         else
    7971             :         {
    7972           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    7973           0 :             ss <<";\n";
    7974             :         }
    7975             : #endif
    7976             :     }
    7977           0 :     ss << "    x = floor(tmp0);\n";
    7978           0 :     ss << "    lambda = tmp1;\n";
    7979           0 :     ss << "    bCumulative = tmp2;\n ";
    7980           0 :     ss << "    if (!bCumulative)\n";
    7981           0 :     ss << "    {\n";
    7982           0 :     ss << "        if(lambda == 0.0)\n";
    7983           0 :     ss << "        {\n";
    7984           0 :     ss << "            return 0;\n";
    7985           0 :     ss << "        }\n";
    7986           0 :     ss << "        else\n";
    7987           0 :     ss << "        {\n";
    7988           0 :     ss << "            if (lambda >712)\n";
    7989           0 :     ss << "            {\n";
    7990           0 :     ss << "            tmp = (exp(x*log(lambda)-lambda-GetLogGamma(x+1.0)));\n";
    7991           0 :     ss << "            return tmp;\n";
    7992           0 :     ss << "            }\n";
    7993           0 :     ss << "            else\n";
    7994           0 :     ss << "            {\n";
    7995           0 :     ss << "                double fPoissonVar = 1.0;\n";
    7996           0 :     ss << "                for ( int f = 0; f < x; ++f )\n";
    7997           0 :     ss << "          fPoissonVar *= lambda * pow(( (double)f + 1.0 ),-1);\n";
    7998           0 :     ss << "                tmp = ( fPoissonVar * exp( -lambda ) );\n";
    7999           0 :     ss << "                return tmp;\n";
    8000           0 :     ss << "            }\n";
    8001           0 :     ss << "        }\n";
    8002           0 :     ss << "     } \n";
    8003           0 :     ss << "     else\n";
    8004           0 :     ss << "     {\n";
    8005           0 :     ss << "         if (lambda == 0.0)\n";
    8006           0 :     ss << "         {\n";
    8007           0 :     ss << "             return 1;\n";
    8008           0 :     ss << "         }\n";
    8009           0 :     ss << "         else\n";
    8010           0 :     ss << "         {\n";
    8011           0 :     ss << "             if (lambda > 712 )\n";
    8012           0 :     ss << "             {\n";
    8013           0 :     ss << "                 tmp = (GetUpRegIGamma(x+1.0,lambda));\n";
    8014           0 :     ss << "                 return tmp;\n";
    8015           0 :     ss << "             }\n";
    8016           0 :     ss << "             else\n";
    8017           0 :     ss << "             {\n";
    8018           0 :     ss << "                 if (x >= 936.0)\n";
    8019           0 :     ss << "                 {\n";
    8020           0 :     ss << "                     return 1;\n";
    8021           0 :     ss << "                 }\n";
    8022           0 :     ss << "                 else\n";
    8023           0 :     ss << "                 {\n";
    8024           0 :     ss << "                     double fSummand = exp(-lambda);\n";
    8025           0 :     ss << "                     double fSum = fSummand;\n";
    8026           0 :     ss << "                     int nEnd = (int) (x + 0.5);\n";
    8027           0 :     ss << "                     for (int i = 1; i <= nEnd; i++)\n";
    8028           0 :     ss << "                     {\n";
    8029           0 :     ss << "                fSummand = (fSummand*lambda)*pow((double)i,-1);\n";
    8030           0 :     ss << "                         fSum += fSummand;\n";
    8031           0 :     ss << "                     }\n";
    8032           0 :     ss << "                     tmp = fSum;\n";
    8033           0 :     ss << "                     return tmp;\n";
    8034           0 :     ss << "                 }\n";
    8035           0 :     ss << "             }\n";
    8036           0 :     ss << "         }\n";
    8037           0 :     ss << "     }\n";
    8038           0 :     ss << "}\n";
    8039           0 : }
    8040           0 : void OpCovar::GenSlidingWindowFunction(std::stringstream& ss,
    8041             :     const std::string &sSymName, SubArguments& vSubArguments)
    8042             : {
    8043           0 :         ss << "\ndouble " << sSymName;
    8044           0 :         ss << "_"<< BinFuncName() <<"(";
    8045           0 :         for (unsigned i = 0; i < vSubArguments.size(); i++)
    8046             :         {
    8047           0 :             if (i)
    8048           0 :                 ss << ",";
    8049           0 :             vSubArguments[i]->GenSlidingWindowDecl(ss);
    8050             :         }
    8051           0 :         ss << ") {\n";
    8052           0 :         ss << "    int gid0 = get_global_id(0);\n";
    8053           0 :         ss << "    double vSum = 0.0;\n";
    8054           0 :         ss << "    double vSum0 = 0.0;\n";
    8055           0 :         ss << "    double vSum1 = 0.0;\n";
    8056           0 :         ss << "    double vMean0 = 0.0;\n";
    8057           0 :         ss << "    double vMean1 = 0.0;\n";
    8058           0 :         ss << "    double arg0 = 0.0;\n";
    8059           0 :         ss << "    double arg1 = 0.0;\n";
    8060           0 :         FormulaToken* pCurX = vSubArguments[0]->GetFormulaToken();
    8061           0 :         FormulaToken* pCurY = vSubArguments[1]->GetFormulaToken();
    8062           0 :         if ((pCurX->GetType() == formula::svDoubleVectorRef)&&
    8063           0 :             (pCurY->GetType() == formula::svDoubleVectorRef)){
    8064           0 :         ss << "    int cnt = 0;\n";
    8065             :         const formula::DoubleVectorRefToken* pCurDVRX =
    8066           0 :             static_cast<const formula::DoubleVectorRefToken* >(pCurX);
    8067             :         const formula::DoubleVectorRefToken* pCurDVRY =
    8068           0 :             static_cast<const formula::DoubleVectorRefToken* >(pCurY);
    8069           0 :         size_t nCurWindowSizeX = pCurDVRX->GetRefRowSize();
    8070           0 :         size_t nCurWindowSizeY = pCurDVRY->GetRefRowSize();
    8071           0 :         if(nCurWindowSizeX == nCurWindowSizeY)
    8072             :         {
    8073           0 :             ss << "    for( ";
    8074           0 :             if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    8075           0 :                 ss << "int i = gid0; i < " << nCurWindowSizeX;
    8076           0 :                 ss << " && i < " << pCurDVRX->GetArrayLength() << " && i < ";
    8077           0 :                 ss << pCurDVRY->GetArrayLength() << "; i++){\n";
    8078             : #ifdef  ISNAN
    8079           0 :                 ss << "        if(isNan(";
    8080           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") ||";
    8081           0 :                 ss << " isNan("<< vSubArguments[1]->GenSlidingWindowDeclRef();
    8082           0 :                 ss << ")) {\n";
    8083           0 :                 ss << "            arg0 = 0.0;\n";
    8084           0 :                 ss << "            arg1 = 0.0;\n";
    8085           0 :                 ss << "            --cnt;\n";
    8086           0 :                 ss << "        }\n";
    8087           0 :                 ss << "else{\n";
    8088           0 :                 ss << "        arg0 = ";
    8089           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8090           0 :                 ss << "        arg1 = ";
    8091           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8092           0 :                 ss << "}\n";
    8093             : #else
    8094             :                 ss << "        arg0 = ";
    8095             :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8096             :                 ss << "        arg1 = ";
    8097             :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8098             : #endif
    8099           0 :                 ss << "        ++cnt;\n";
    8100           0 :                 ss << "        vSum0 += arg0;\n";
    8101           0 :                 ss << "        vSum1 += arg1;\n";
    8102           0 :                 ss << "    }\n";
    8103             :             }
    8104           0 :             else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
    8105           0 :                 ss << "int i = 0; i < gid0 + " << nCurWindowSizeX << " && ";
    8106           0 :                 ss << " i < " << pCurDVRX->GetArrayLength() << " && ";
    8107           0 :                 ss << " i < " << pCurDVRY->GetArrayLength() << "; i++) {\n";
    8108             : #ifdef ISNAN
    8109           0 :                 ss << "        if(isNan(";
    8110           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") ||";
    8111           0 :                 ss << " isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
    8112           0 :                 ss << ")) {\n";
    8113           0 :                 ss << "            arg0 = 0.0;\n";
    8114           0 :                 ss << "            arg1 = 0.0;\n";
    8115           0 :                 ss << "            --cnt;\n";
    8116           0 :                 ss << "        }\n";
    8117           0 :                 ss << "else{\n";
    8118           0 :                 ss << "        arg0 = ";
    8119           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8120           0 :                 ss << "        arg1 = ";
    8121           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";}\n";
    8122             : #else
    8123             :                 ss << "        arg0 = ";
    8124             :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef()<< ";\n";
    8125             :                 ss << "        arg1 = ";
    8126             :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8127             : #endif
    8128           0 :                 ss << "        ++cnt;\n";
    8129           0 :                 ss << "        vSum0 += arg0;\n";
    8130           0 :                 ss << "        vSum1 += arg1;\n";
    8131           0 :                 ss << "    }\n";
    8132             :             }
    8133           0 :             else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    8134           0 :                 ss << "int i = 0; i < " << nCurWindowSizeX << " && i < ";
    8135           0 :                 ss << pCurDVRX->GetArrayLength() << " && i < ";
    8136           0 :                 ss << pCurDVRY->GetArrayLength() << "; i++) {\n";
    8137             : #ifdef ISNAN
    8138           0 :                 ss << "        if(isNan(";
    8139           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") ||";
    8140           0 :                 ss << " isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
    8141           0 :                 ss << ")) {\n";
    8142           0 :                 ss << "            arg0 = 0.0;\n";
    8143           0 :                 ss << "            arg1 = 0.0;\n";
    8144           0 :                 ss << "            --cnt;\n";
    8145           0 :                 ss << "        }\n";
    8146           0 :                 ss << "else{\n";
    8147           0 :                 ss << "        arg0 = ";
    8148           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8149           0 :                 ss << "        arg1 = ";
    8150           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";}\n";
    8151             : #else
    8152             :                 ss << "        arg0 = ";
    8153             :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8154             :                 ss << "        arg1 = ";
    8155             :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8156             : #endif
    8157           0 :                 ss << "        ++cnt;\n";
    8158           0 :                 ss << "        vSum0 += arg0;\n";
    8159           0 :                 ss << "        vSum1 += arg1;\n";
    8160           0 :                 ss << "    }\n";
    8161             :             }
    8162             :             else {
    8163           0 :                 ss << "int i = 0; i < " << nCurWindowSizeX << " && ";
    8164           0 :                 ss << " i + gid0 < " << pCurDVRX->GetArrayLength();
    8165           0 :                 ss << " && i + gid0 < " << pCurDVRX->GetArrayLength();
    8166           0 :                 ss << "; i++) {\n";
    8167             : #ifdef ISNAN
    8168           0 :                 ss << "if ((isNan(";
    8169           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ")) || ";
    8170           0 :                 ss << "(isNan("<< vSubArguments[1]->GenSlidingWindowDeclRef();
    8171           0 :                 ss << ")))  {\n";
    8172           0 :                 ss << "            arg0 = 0.0;\n";
    8173           0 :                 ss << "            arg1 = 0.0;\n";
    8174           0 :                 ss << "            --cnt;\n";
    8175           0 :                 ss << "        }\n";
    8176           0 :                 ss << "        else {\n";
    8177           0 :                 ss << "            arg0 = ";
    8178           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8179           0 :                 ss << "            arg1 = ";
    8180           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8181           0 :                 ss << "        }\n";
    8182             : #else
    8183             :                 ss << "        arg0 = ";
    8184             :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8185             :                 ss << "        arg1 = ";
    8186             :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8187             : #endif
    8188           0 :                 ss << "        ++cnt;\n";
    8189           0 :                 ss << "        vSum0 += arg0;\n";
    8190           0 :                 ss << "        vSum1 += arg1;\n";
    8191           0 :                 ss << "    }\n";
    8192             :             }
    8193           0 :             ss << "    if(cnt < 1) {\n";
    8194           0 :             ss << "        return -DBL_MAX;\n";
    8195           0 :             ss << "    }\n";
    8196           0 :             ss << "    else {\n";
    8197           0 :             ss << "        vMean0 = vSum0 / cnt;\n";
    8198           0 :             ss << "        vMean1 = vSum1 / cnt;\n";
    8199           0 :             ss << "    for(";
    8200           0 :             if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    8201           0 :                 ss << "int i = gid0; i < " << nCurWindowSizeX;
    8202           0 :                 ss << " && i < " << pCurDVRX->GetArrayLength() << " && i < ";
    8203           0 :                 ss << pCurDVRY->GetArrayLength() << "; i++){\n";
    8204             : #ifdef ISNAN
    8205           0 :                 ss << "        if(isNan(";
    8206           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") ||";
    8207           0 :                 ss << " isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
    8208           0 :                 ss << ")){\n";
    8209           0 :                 ss << "            arg0 = vMean0;\n";
    8210           0 :                 ss << "            arg1 = vMean1;\n";
    8211           0 :                 ss << "        }\n";
    8212           0 :                 ss << "        else{\n";
    8213           0 :                 ss << "            arg0 = ";
    8214           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8215           0 :                 ss << "            arg1 = ";
    8216           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";}\n";
    8217             : #else
    8218             :                 ss << "        arg0 = ";
    8219             :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8220             :                 ss << "        arg1 = ";
    8221             :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8222             : #endif
    8223           0 :                 ss << "        vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
    8224           0 :                 ss << "    }\n";
    8225             :             }
    8226           0 :             else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
    8227           0 :                 ss << "int i = 0; i < gid0 + " << nCurWindowSizeX << " && ";
    8228           0 :                 ss << " i < " << pCurDVRX->GetArrayLength() << " && ";
    8229           0 :                 ss << " i < " << pCurDVRY->GetArrayLength() << "; i++) {\n";
    8230             : #ifdef ISNAN
    8231           0 :                 ss << "        if(isNan(";
    8232           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") || ";
    8233           0 :                 ss << "isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
    8234           0 :                 ss << ")) {\n";
    8235           0 :                 ss << "            arg0 = vMean0;\n";
    8236           0 :                 ss << "            arg1 = vMean1;\n";
    8237           0 :                 ss << "        }\n";
    8238           0 :                 ss << "else{\n";
    8239           0 :                 ss << "        arg0 = ";
    8240           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8241           0 :                 ss << "        arg1 = ";
    8242           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";}\n";
    8243             : #else
    8244             :                 ss << "        arg0 = ";
    8245             :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8246             :                 ss << "        arg1 = ";
    8247             :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8248             : #endif
    8249           0 :                 ss << "        vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
    8250           0 :                 ss << "    }\n";
    8251             :             }
    8252           0 :             else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    8253           0 :                 ss << "int i = 0; i < " << nCurWindowSizeX << " && i < ";
    8254           0 :                 ss << pCurDVRX->GetArrayLength() << " && i < ";
    8255           0 :                 ss << pCurDVRY->GetArrayLength() << "; i++) {\n";
    8256             : #ifdef ISNAN
    8257           0 :                 ss << "        if(isNan(";
    8258           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ") || ";
    8259           0 :                 ss << "isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
    8260           0 :                 ss << ")) {\n";
    8261           0 :                 ss << "            arg0 = vMean0;\n";
    8262           0 :                 ss << "            arg1 = vMean1;\n";
    8263           0 :                 ss << "        }\n";
    8264           0 :                 ss << "else{\n";
    8265           0 :                 ss << "        arg0 = ";
    8266           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8267           0 :                 ss << "        arg1 = ";
    8268           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";}\n";
    8269             : #else
    8270             :                 ss << "        arg0 = ";
    8271             :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8272             :                 ss << "        arg1 = ";
    8273             :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8274             : #endif
    8275           0 :                 ss << "        vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
    8276           0 :                 ss << "    }\n";
    8277             :             }
    8278             :             else {
    8279           0 :                 ss << "int i = 0; i < " << nCurWindowSizeX << " && ";
    8280           0 :                 ss << " i + gid0 < " << pCurDVRX->GetArrayLength();
    8281           0 :                 ss << " && i + gid0 < " << pCurDVRX->GetArrayLength();
    8282           0 :                 ss << "; i++) {\n";
    8283             : #ifdef ISNAN
    8284           0 :                 ss << "if((isNan(";
    8285           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ")) || ";
    8286           0 :                 ss << "(isNan(" << vSubArguments[1]->GenSlidingWindowDeclRef();
    8287           0 :                 ss << ")))  {\n";
    8288           0 :                 ss << "            arg0 = vMean0;\n";
    8289           0 :                 ss << "            arg1 = vMean1;\n";
    8290           0 :                 ss << "        }\n";
    8291           0 :                 ss << "        else{\n";
    8292           0 :                 ss << "            arg0 = ";
    8293           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8294           0 :                 ss << "            arg1 = ";
    8295           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8296           0 :                 ss << "        }\n";
    8297             : #else
    8298             :                 ss << "        arg0 = ";
    8299             :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef()<< ";\n";
    8300             :                 ss << "        arg1 = ";
    8301             :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8302             : #endif
    8303           0 :                 ss << "        vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
    8304           0 :                 ss << "    }\n";
    8305             :             }
    8306           0 :             ss << "    return vSum / cnt;\n";
    8307           0 :             ss << "    }\n";
    8308           0 :             ss << "}";
    8309             :         }
    8310             :         }
    8311             :         else {
    8312           0 :         ss << "    int cnt0 = 0,cnt1 = 0;\n";
    8313           0 :         for (unsigned i = 0; i < vSubArguments.size(); i++)
    8314             :         {
    8315           0 :             FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
    8316           0 :             if (pCur->GetType() == formula::svSingleVectorRef){
    8317             : #ifdef  ISNAN
    8318             :                 const formula::SingleVectorRefToken* pTVR =
    8319           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    8320           0 :                 ss << "    if(isNan(";
    8321           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8322           0 :                 ss << ") || gid0 >= " << pTVR->GetArrayLength() << ")\n";
    8323           0 :                 ss << "        arg" << i << " = 0;\n    else\n";
    8324             : #endif
    8325           0 :                 ss << "        arg" << i << " = ";
    8326           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8327           0 :                 ss << "    cnt" << i << "++;\n";
    8328           0 :                 ss << "    vSum" << i << " += arg" << i << ";\n";
    8329             :             }
    8330           0 :             else if (pCur->GetType() == formula::svDouble){
    8331             : #ifdef  ISNAN
    8332           0 :                 ss << "    if(isNan ( ";
    8333           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << "))\n";
    8334           0 :                 ss << "        arg" << i << " = 0;\n    else\n";
    8335             : #endif
    8336           0 :                 ss << "        arg" << i << " = ";
    8337           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8338           0 :                 ss << "    cnt" << i << "++;\n";
    8339           0 :                 ss << "    vSum" << i << " += arg" << i << ";\n";
    8340             :             }
    8341             :             else {
    8342           0 :                 ss << "    arg" << i << " = ";
    8343           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8344           0 :                 ss << "    cnt" << i << "++;\n";
    8345           0 :                 ss << "    vSum" << i << " += arg" << i << ";\n";
    8346             :             }
    8347             :         }
    8348           0 :         ss << "        vMean0 = vSum0 / cnt0;\n";
    8349           0 :         ss << "        vMean1 = vSum0 / cnt1;\n";
    8350           0 :         for(unsigned i = 0; i < vSubArguments.size(); i++ ) {
    8351           0 :             FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
    8352           0 :             if (pCur->GetType() == formula::svSingleVectorRef) {
    8353             : #ifdef  ISNAN
    8354             :                 const formula::SingleVectorRefToken* pTVR =
    8355           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    8356           0 :                 ss << "    if(isNan(";
    8357           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8358           0 :                 ss << ") || gid0 >= " << pTVR->GetArrayLength() << ")\n";
    8359           0 :                 ss << "        arg" << i << " = vMean" << i << ";\n";
    8360           0 :                 ss << "    else\n";
    8361             : #endif
    8362           0 :                 ss << "        arg" << i << " = ";
    8363           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8364             :             }
    8365           0 :             else if (pCur->GetType() == formula::svDouble) {
    8366           0 :                 ss << "    arg" << i << " = ";
    8367           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8368             : #ifdef  ISNAN
    8369           0 :                 ss << "    if(isNan(arg" << i << "))\n";
    8370           0 :                 ss << "        arg" << i << " = vMean" << i << ";\n";
    8371             : #endif
    8372             :             }
    8373             :             else {
    8374           0 :                 ss << "    arg" << i << " = ";
    8375           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8376             :             }
    8377             :         }
    8378           0 :         ss << "        vSum += (arg0 - vMean0) * ( arg1 - vMean1 );\n";
    8379           0 :         ss << "    return vSum / cnt0;\n";
    8380           0 :         ss << "}";
    8381             :         }
    8382           0 : }
    8383           0 : void OpBetaDist::GenSlidingWindowFunction(std::stringstream &ss,
    8384             :             const std::string &sSymName, SubArguments &vSubArguments)
    8385             : {
    8386           0 :     ss << "\ndouble " << sSymName;
    8387           0 :     ss << "_"<< BinFuncName() <<"(";
    8388           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    8389             :     {
    8390           0 :         if (i)
    8391           0 :             ss << ",";
    8392           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    8393             :     }
    8394           0 :     ss << ") {\n";
    8395           0 :     ss << "    int gid0=get_global_id(0);\n";
    8396           0 :     ss << "    double tmp;\n";
    8397           0 :     ss << "    double arg0,arg1,arg2,arg3,arg4,arg5;\n";
    8398           0 :     size_t i = vSubArguments.size();
    8399             : 
    8400           0 :     for (i = 0; i < vSubArguments.size(); i++)
    8401             :     {
    8402           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    8403             :         assert(pCur);
    8404           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    8405             :         {
    8406             :             const formula::DoubleVectorRefToken* pDVR =
    8407           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    8408           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    8409           0 :             ss << "for (int i = ";
    8410           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    8411             : #ifdef  ISNAN
    8412           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    8413           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    8414             : #else
    8415             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    8416             : #endif
    8417           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    8418             : #ifdef  ISNAN
    8419           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    8420           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    8421             : #else
    8422             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    8423             : #endif
    8424           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    8425             : #ifdef  ISNAN
    8426           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    8427           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    8428             : #else
    8429             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    8430             : #endif
    8431             :             }
    8432             :             else {
    8433             : #ifdef  ISNAN
    8434           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    8435             : #else
    8436             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    8437             : #endif
    8438             :             }
    8439             : 
    8440             :         }
    8441           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    8442             :         {
    8443             : #ifdef  ISNAN
    8444             :             const formula::SingleVectorRefToken* pSVR =
    8445           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    8446           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    8447           0 :             ss << "    {\n";
    8448           0 :             ss << "        if (isNan(";
    8449           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8450           0 :             ss << "))\n";
    8451           0 :             ss << "            arg"<<i<<"= 0;\n";
    8452           0 :             ss << "        else\n";
    8453           0 :             ss << "            arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    8454           0 :             ss << ";\n";
    8455           0 :             ss << "    }\n";
    8456           0 :             ss << "    else\n";
    8457           0 :             ss << "        arg"<<i<<"= 0;\n";
    8458             : #endif
    8459             :         }
    8460           0 :         else if (pCur->GetType() == formula::svDouble)
    8461             :         {
    8462             : #ifdef  ISNAN
    8463           0 :             ss << "    if (isNan(";
    8464           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8465           0 :             ss << "))\n";
    8466           0 :             ss << "        arg"<<i<<"= 0;\n";
    8467           0 :             ss << "    else\n";
    8468           0 :             ss << "        arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    8469           0 :             ss << ";\n";
    8470             : #endif
    8471             :         }
    8472             :     }
    8473             :     ss << "    double fScale = arg4 - arg3;\n"
    8474             :     "    if (fScale <= 0.0 || arg1 <= 0.0 || arg2 <= 0.0)\n"
    8475             :     "    {\n"
    8476             :     "        tmp = DBL_MIN;\n"
    8477             :     "        return tmp;\n"
    8478             :     "    }\n"
    8479             :     "    if (arg5)\n"
    8480             :     "    {\n"
    8481             :     "        if (arg0< arg3)\n"
    8482             :     "        {\n"
    8483             :     "            tmp = 0.0;\n"
    8484             :     "            return tmp;\n"
    8485             :     "        }\n"
    8486             :     "        if (arg0 > arg4)\n"
    8487             :     "        {\n"
    8488             :     "            tmp = 1.0;\n"
    8489             :     "            return tmp;\n"
    8490             :     "        }\n"
    8491             :     "        arg0 = (arg0-arg3)*pow(fScale,-1);\n"
    8492             :     "        tmp =  GetBetaDist(arg0, arg1, arg2);\n"
    8493             :     "    }\n"
    8494             :     "    else\n"
    8495             :     "    {\n"
    8496             :     "        if (arg0 < arg3 || arg0 > arg4 )\n"
    8497             :     "        {\n"
    8498             :     "            tmp = 0.0;\n"
    8499             :     "            return tmp;\n"
    8500             :     "        }\n"
    8501             :     "        arg0 = (arg0 - arg3)*pow(fScale,-1);\n"
    8502             :     "        tmp = GetBetaDistPDF(arg0, arg1, arg2)*pow(fScale,-1);\n"
    8503           0 :     "    }\n";
    8504           0 :     ss << "    return tmp;\n";
    8505           0 :     ss << "}\n";
    8506           0 : }
    8507           0 : void OpBetainv::BinInlineFun(std::set<std::string>& decls,
    8508             :     std::set<std::string>& funs)
    8509             : {
    8510           0 :     decls.insert(fMachEpsDecl);
    8511           0 :     funs.insert("");
    8512           0 :     decls.insert(fMaxGammaArgumentDecl);
    8513           0 :     funs.insert("");
    8514           0 :     decls.insert(lcl_IterateInverseBetaInvDecl);
    8515           0 :     funs.insert(lcl_IterateInverseBetaInv);
    8516           0 :     decls.insert(GetBetaDistDecl);
    8517           0 :     funs.insert(GetBetaDist);
    8518           0 :     decls.insert(lcl_HasChangeOfSignDecl);
    8519           0 :     funs.insert(lcl_HasChangeOfSign);
    8520           0 :     decls.insert(lcl_HasChangeOfSignDecl);
    8521           0 :     funs.insert(lcl_HasChangeOfSign);
    8522           0 :     decls.insert(lcl_HasChangeOfSignDecl);
    8523           0 :     funs.insert(lcl_HasChangeOfSign);
    8524           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);
    8525           0 :     funs.insert(lcl_GetBetaHelperContFrac);
    8526           0 :     decls.insert(GetBetaDistPDFDecl);
    8527           0 :     funs.insert(GetBetaDistPDF);
    8528           0 :     decls.insert(GetLogBetaDecl);
    8529           0 :     funs.insert(GetLogBeta);
    8530           0 :     decls.insert(GetBetaDecl);
    8531           0 :     funs.insert(GetBeta);
    8532           0 :     decls.insert(lcl_getLanczosSumDecl);
    8533           0 :     funs.insert(lcl_getLanczosSum);
    8534           0 : }
    8535           0 : void OpBetainv::GenSlidingWindowFunction(
    8536             :     std::stringstream &ss,const std::string &sSymName,
    8537             :     SubArguments &vSubArguments)
    8538             : {
    8539           0 :     ss << "\ndouble " << sSymName;
    8540           0 :     ss << "_"<< BinFuncName() <<"(";
    8541           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    8542             :     {
    8543           0 :         if (i)
    8544           0 :             ss << ",";
    8545           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    8546             :     }
    8547           0 :     ss << ")\n";
    8548           0 :     ss << "{\n";
    8549           0 :     ss << "    double tmp0,tmp1,tmp2,tmp3,tmp4;\n";
    8550           0 :     ss << "    int gid0=get_global_id(0);\n";
    8551           0 :     size_t i = vSubArguments.size();
    8552           0 :     ss <<"\n    ";
    8553             :     //while (i-- > 1)
    8554           0 :     for (i = 0; i < vSubArguments.size(); i++)
    8555             :     {
    8556           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    8557             :         assert(pCur);
    8558           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    8559             :         {
    8560             :             const formula::DoubleVectorRefToken* pDVR =
    8561           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    8562           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    8563           0 :             ss << "for (int i = ";
    8564           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    8565             : #ifdef  ISNAN
    8566           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    8567           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    8568             : #else
    8569             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    8570             : #endif
    8571           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    8572             : #ifdef  ISNAN
    8573           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    8574           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    8575             : #else
    8576             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    8577             : #endif
    8578           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    8579             : #ifdef  ISNAN
    8580           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    8581           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    8582             : #else
    8583             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    8584             : #endif
    8585             :             }
    8586             :             else {
    8587             : #ifdef  ISNAN
    8588           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    8589             : #else
    8590             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    8591             : #endif
    8592             :             }
    8593             :         }
    8594           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    8595             :         {
    8596             : #ifdef  ISNAN
    8597             :             const formula::SingleVectorRefToken* pSVR =
    8598           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    8599           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    8600             : #else
    8601             : #endif
    8602             :         }
    8603           0 :         else if (pCur->GetType() == formula::svDouble)
    8604             :         {
    8605             : #ifdef  ISNAN
    8606           0 :             ss << "{\n";
    8607             : #endif
    8608             :         }
    8609             :         else
    8610             :         {
    8611             : #ifdef  ISNAN
    8612             : #endif
    8613             : 
    8614             :         }
    8615             : #ifdef  ISNAN
    8616           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    8617             :         {
    8618           0 :             ss << "    if (isNan(";
    8619           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8620           0 :             ss << "))\n";
    8621           0 :             ss << "        tmp"<<i<<"= 0;\n";
    8622           0 :             ss << "    else\n";
    8623           0 :             ss << "        tmp"<<i<<"=\n";
    8624           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8625           0 :             ss << ";\n}\n";
    8626             :         }
    8627             :         else
    8628             :         {
    8629           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    8630           0 :             ss <<";\n";
    8631             :         }
    8632             : #else
    8633             :     ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    8634             :     ss <<";\n";
    8635             : #endif
    8636             :     }
    8637           0 :     ss << "    if (tmp0 < 0.0 || tmp0 >= 1.0 ||";
    8638           0 :     ss << "tmp3 == tmp4 || tmp1 <= 0.0 || tmp2 <= 0.0)\n";
    8639           0 :     ss << "    {\n";
    8640           0 :     ss << "        return DBL_MIN;\n";
    8641           0 :     ss << "    }\n";
    8642           0 :     ss << "    if (tmp0 == 0.0)\n";
    8643           0 :     ss << "        return 0.0;\n";
    8644           0 :     ss << "    else\n";
    8645           0 :     ss << "    {";
    8646           0 :     ss << "        bool bConvError;";
    8647           0 :     ss << "        double fVal = lcl_IterateInverseBetaInv";
    8648           0 :     ss << "(tmp0, tmp1, tmp2, 0.0, 1.0, &bConvError);\n";
    8649           0 :     ss << "        if(bConvError)\n";
    8650           0 :     ss << "            return DBL_MIN;\n";
    8651           0 :     ss << "        else\n";
    8652           0 :     ss << "            return (tmp3 + fVal*(tmp4 - tmp3));\n";
    8653           0 :     ss << "    }";
    8654           0 :     ss << "}\n";
    8655           0 : }
    8656           0 : void OpDevSq::GenSlidingWindowFunction(std::stringstream& ss,
    8657             :     const std::string &sSymName, SubArguments& vSubArguments)
    8658             : {
    8659           0 :         ss << "\ndouble " << sSymName;
    8660           0 :         ss << "_"<< BinFuncName() <<"(";
    8661           0 :         for (unsigned i = 0; i < vSubArguments.size(); i++)
    8662             :         {
    8663           0 :             if (i)
    8664           0 :                 ss << ",";
    8665           0 :             vSubArguments[i]->GenSlidingWindowDecl(ss);
    8666             :             }
    8667           0 :         ss << ") {\n";
    8668           0 :         ss << "    int gid0 = get_global_id(0);\n";
    8669           0 :         ss << "    double vSum = 0.0;\n";
    8670           0 :         ss << "    double vMean = 0.0;\n";
    8671           0 :         ss << "    int cnt = 0;\n";
    8672           0 :         for(unsigned i = 0; i < vSubArguments.size(); i++ )
    8673             :         {
    8674           0 :         ss << "    double arg" << i << " = 0.0;\n";
    8675           0 :         FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
    8676           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    8677             :         {
    8678             :         const formula::DoubleVectorRefToken* pCurDVR =
    8679           0 :             static_cast<const formula::DoubleVectorRefToken* >(pCur);
    8680           0 :         size_t nCurWindowSize = pCurDVR->GetRefRowSize();
    8681           0 :         ss << "    for(int i = ";
    8682           0 :         if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
    8683           0 :             ss << "gid0; i < " << nCurWindowSize << "; i++) {\n";
    8684           0 :             ss << "        arg" << i << " = ";
    8685           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8686             : #ifdef ISNAN
    8687           0 :             ss << "        if(isNan(arg" << i << ") || (i >= ";
    8688           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8689           0 :             ss << "            arg" << i << " = 0.0;\n";
    8690           0 :             ss << "            --cnt;\n";
    8691           0 :             ss << "        }\n";
    8692             : #endif
    8693           0 :             ss << "        ++cnt;\n";
    8694           0 :             ss << "        vSum += arg" << i << ";\n";
    8695           0 :             ss << "    }\n";
    8696           0 :         } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
    8697           0 :             ss << "0; i < gid0 + " << nCurWindowSize << "; i++) {\n";
    8698           0 :             ss << "        arg" << i << " = ";
    8699           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8700             : #ifdef ISNAN
    8701           0 :             ss << "        if(isNan(arg" << i << ") || (i >= ";
    8702           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8703           0 :             ss << "            arg" << i << " = 0.0;\n";
    8704           0 :             ss << "            --cnt;\n";
    8705           0 :             ss << "        }\n";
    8706             : #endif
    8707           0 :             ss << "        ++cnt;\n";
    8708           0 :             ss << "        vSum += arg" << i << ";\n";
    8709           0 :             ss << "    }\n";
    8710           0 :         } else if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
    8711           0 :             ss << "0; i < " << nCurWindowSize << "; i++) {\n";
    8712           0 :             ss << "        arg" << i << " = ";
    8713           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8714             : #ifdef ISNAN
    8715           0 :             ss << "        if(isNan(arg" << i << ") || (i >= ";
    8716           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8717           0 :             ss << "            arg" << i << " = 0.0;\n";
    8718           0 :             ss << "            --cnt;\n";
    8719           0 :             ss << "        }\n";
    8720             : #endif
    8721           0 :             ss << "        ++cnt;\n";
    8722           0 :             ss << "        vSum += arg" << i << ";\n";
    8723           0 :             ss << "    }\n";
    8724             :         } else {
    8725           0 :             ss << "0; i < " << nCurWindowSize << "; i++) {\n";
    8726           0 :             ss << "        arg" << i << " = ";
    8727           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8728             : #ifdef ISNAN
    8729           0 :             ss << "        if(isNan(arg" << i << ") || (i + gid0 >= ";
    8730           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8731           0 :             ss << "            arg" << i << " = 0.0;\n";
    8732           0 :             ss << "            --cnt;\n";
    8733           0 :             ss << "        }\n";
    8734             : #endif
    8735           0 :             ss << "        ++cnt;\n";
    8736           0 :             ss << "        vSum += arg" << i << ";\n";
    8737           0 :             ss << "    }\n";
    8738             :         }
    8739             : }
    8740           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    8741             :         {
    8742             : #ifdef  ISNAN
    8743             :             const formula::SingleVectorRefToken* pTVR =
    8744           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    8745           0 :             ss << "    if(isNan(";
    8746           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8747           0 :             ss << ") || gid0 >= " << pTVR->GetArrayLength() << ")\n";
    8748           0 :             ss << "        arg" << i << " = 0;\n    else\n";
    8749             : #endif
    8750           0 :             ss << "        arg" << i << " = ";
    8751           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8752           0 :             ss << "    cnt++;\n";
    8753           0 :             ss << "    vSum += arg" << i << ";\n";
    8754             :         }
    8755           0 :         else if (pCur->GetType() == formula::svDouble)
    8756             :         {
    8757             : #ifdef  ISNAN
    8758           0 :             ss << "    if(isNan ( ";
    8759           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << "))\n";
    8760           0 :             ss << "        arg" << i << " = 0;\n    else\n";
    8761             : #endif
    8762           0 :             ss << "        arg" << i << " = ";
    8763           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8764           0 :             ss << "    cnt++;\n";
    8765           0 :             ss << "    vSum += arg" << i << ";\n";
    8766             :         }
    8767             :         else
    8768             :         {
    8769           0 :             ss << "    arg" << i << " = ";
    8770           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8771           0 :             ss << "    cnt++;\n";
    8772           0 :             ss << "    vSum += arg" << i << ";\n";
    8773             :         }
    8774             :             }
    8775           0 :         ss << "    vMean = vSum / cnt;\n";
    8776           0 :         ss << "    vSum = 0.0;\n";
    8777           0 :         for(unsigned k = 0; k < vSubArguments.size(); k++ )
    8778             :         {
    8779           0 :         FormulaToken* pCur = vSubArguments[k]->GetFormulaToken();
    8780           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    8781             :         {
    8782             :         const formula::DoubleVectorRefToken* pCurDVR =
    8783           0 :             static_cast<const formula::DoubleVectorRefToken* >(pCur);
    8784           0 :         size_t nCurWindowSize = pCurDVR->GetRefRowSize();
    8785           0 :         ss << "    for(int i = ";
    8786           0 :         if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
    8787           0 :             ss << "gid0; i < " << nCurWindowSize << "; i++) {\n";
    8788           0 :             ss << "        arg" << k << " = ";
    8789           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8790             : #ifdef ISNAN
    8791           0 :             ss << "        if(isNan( arg" << k << " ) || (i >= ";
    8792           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8793           0 :             ss << "            arg" << k << " = vXMean;\n";
    8794           0 :             ss << "        }\n";
    8795             : #endif
    8796           0 :             ss << "        vSum += pow( arg" << k << " - vMean, 2 );\n";
    8797           0 :             ss << "    }\n";
    8798           0 :         } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
    8799           0 :             ss << "0; i < gid0 + " << nCurWindowSize << "; i++) {\n";
    8800           0 :             ss << "        arg" << k << " = ";
    8801           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8802             : #ifdef ISNAN
    8803           0 :             ss << "        if(isNan( arg" << k << ") || (i >= ";
    8804           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8805           0 :             ss << "            arg" << k << " = vMean;\n";
    8806           0 :             ss << "        }\n";
    8807             : #endif
    8808           0 :             ss << "        vSum += pow( arg" << k << " - vMean, 2 );\n";
    8809           0 :             ss << "    }\n";
    8810           0 :         } else if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
    8811           0 :             ss << "0; i < " << nCurWindowSize << "; i++) {\n";
    8812           0 :             ss << "        arg" << k << " = ";
    8813           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8814             : #ifdef ISNAN
    8815           0 :             ss << "        if(isNan(arg" << k << ") || (i >= ";
    8816           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8817           0 :             ss << "            arg" << k << " = vMean;\n";
    8818           0 :             ss << "        }\n";
    8819             : #endif
    8820           0 :             ss << "        vSum += pow( arg" << k << " - vMean, 2 );\n";
    8821           0 :             ss << "    }\n";
    8822             :         } else {
    8823           0 :             ss << "0; i < " << nCurWindowSize << "; i++) {\n";
    8824           0 :             ss << "        arg" << k << " = ";
    8825           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8826             : #ifdef ISNAN
    8827           0 :             ss << "        if(isNan(arg" << k << ") || (i + gid0 >= ";
    8828           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8829           0 :             ss << "            arg" << k << " = vMean;\n";
    8830           0 :             ss << "        }\n";
    8831             : #endif
    8832           0 :             ss << "        vSum += pow( arg" << k << " - vMean, 2 );\n";
    8833           0 :             ss << "    }\n";
    8834             :         }
    8835             :             }
    8836           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    8837             :         {
    8838             : #ifdef  ISNAN
    8839             :             const formula::SingleVectorRefToken* pTVR =
    8840           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    8841           0 :             ss << "    if(isNan(";
    8842           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef();
    8843           0 :             ss << ") || gid0 >= " << pTVR->GetArrayLength() << ")\n";
    8844           0 :             ss << "        arg" << k << " = vMean;\n    else\n";
    8845             : #endif
    8846           0 :             ss << "        arg" << k << " = ";
    8847           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef()<<";\n";
    8848           0 :             ss << "    vSum += pow( arg" << k << " - vMean, 2 );\n";
    8849             :         }
    8850           0 :         else if (pCur->GetType() == formula::svDouble)
    8851             :         {
    8852           0 :             ss << "    arg" << k << " = ";
    8853           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8854             : #ifdef  ISNAN
    8855           0 :             ss << "    if(isNan(arg" << k << "))\n";
    8856           0 :             ss << "        arg" << k << " = vMean;\n";
    8857             : #endif
    8858           0 :             ss << "    vSum += pow( arg" << k << " - vMean, 2 );\n";
    8859             :         }
    8860             :         else
    8861             :         {
    8862           0 :             ss << "    arg" << k << " = ";
    8863           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8864           0 :             ss << "    vSum += pow( arg" << k << " - vMean, 2 );\n";
    8865             :         }
    8866             :             }
    8867           0 :         ss << "    return vSum;\n";
    8868           0 :         ss << "}";
    8869           0 : }
    8870           0 : void OpHypGeomDist::GenSlidingWindowFunction(std::stringstream &ss,
    8871             :             const std::string &sSymName, SubArguments &vSubArguments)
    8872             : {
    8873           0 :     ss << "\ndouble " << sSymName;
    8874           0 :     ss << "_"<< BinFuncName() <<"(";
    8875           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    8876             :     {
    8877           0 :         if (i)
    8878           0 :             ss << ",";
    8879           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    8880             :     }
    8881           0 :     ss << ") {\n";
    8882           0 :     ss << "    int gid0=get_global_id(0);\n";
    8883           0 :     ss << "    double arg0,arg1,arg2,arg3;\n";
    8884           0 :     size_t i = vSubArguments.size();
    8885             : 
    8886           0 :     for (i = 0; i < vSubArguments.size(); i++)
    8887             :     {
    8888           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    8889             :         assert(pCur);
    8890           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    8891             :         {
    8892             :             const formula::DoubleVectorRefToken* pDVR =
    8893           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    8894           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    8895           0 :             ss << "for (int i = ";
    8896           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    8897             : #ifdef  ISNAN
    8898           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    8899           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    8900             : #endif
    8901           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    8902             : #ifdef  ISNAN
    8903           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    8904           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    8905             : #endif
    8906           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    8907             : #ifdef  ISNAN
    8908           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    8909           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    8910             : #endif
    8911             :             }
    8912             :             else {
    8913             : #ifdef  ISNAN
    8914           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    8915             : #endif
    8916             :             }
    8917             :         }
    8918           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    8919             :         {
    8920             : #ifdef  ISNAN
    8921             :             const formula::SingleVectorRefToken* pSVR =
    8922           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    8923           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    8924           0 :             ss << "    {\n";
    8925           0 :             ss << "        if (isNan(";
    8926           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8927           0 :             ss << "))\n";
    8928           0 :             ss << "            arg"<<i<<"= 0;\n";
    8929           0 :             ss << "        else\n";
    8930           0 :             ss << "            arg"<<i<<"=";
    8931           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    8932           0 :             ss << ";\n";
    8933           0 :             ss << "    }\n";
    8934           0 :             ss << "    else\n";
    8935           0 :             ss << "        arg"<<i<<"= 0;\n";
    8936             : #endif
    8937             :         }
    8938           0 :         else if (pCur->GetType() == formula::svDouble)
    8939             :         {
    8940             : #ifdef  ISNAN
    8941           0 :             ss << "    if (isNan(";
    8942           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8943           0 :             ss << "))\n";
    8944           0 :             ss << "        arg"<<i<<"= 0;\n";
    8945           0 :             ss << "    else\n";
    8946           0 :             ss << "        arg"<<i<<"=";
    8947           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    8948           0 :             ss << ";\n";
    8949             : #endif
    8950             :         }
    8951             :     }
    8952             :     ss << "    double N1=floor(arg3);\n"
    8953             :     "    double M1=floor(arg2);\n"
    8954             :     "    double n1=floor(arg1);\n"
    8955             :     "    double x1=floor(arg0);\n"
    8956             :     "    double num[9];\n"
    8957             :     "    double PI = 3.1415926535897932384626433832795;\n"
    8958             :     "    double tmp;\n"
    8959             :     "    if( (x1 < 0.0) || (n1 < x1) || (M1 < x1) || (N1 < n1) ||"
    8960             :     "(N1 < M1) || (x1 < n1 - N1 + M1) )\n"
    8961             :     "    {\n"
    8962             :     "        tmp = DBL_MIN;\n"
    8963             :     "        return tmp;\n"
    8964             :     "    }\n"
    8965             :     "    num[0]=M1;\n"
    8966             :     "    num[1]=x1;\n"
    8967             :     "    num[2]=M1-x1;\n"
    8968             :     "    num[3]=N1-M1;\n"
    8969             :     "    num[4]=n1-x1;\n"
    8970             :     "    num[5]=N1-M1-n1+x1;\n"
    8971             :     "    num[6]=N1;\n"
    8972             :     "    num[7]=n1;\n"
    8973             :     "    num[8]=N1-n1;\n"
    8974             :     "    for(int i=0;i<9;i++)\n"
    8975             :     "    {\n"
    8976             :     "        if(num[i]<171)\n"
    8977             :     "        {\n"
    8978             :     "            if(num[i]==0)\n"
    8979             :     "                num[i]=0;\n"
    8980             :     "            else\n"
    8981             :     "                num[i]=log(tgamma(num[i])*num[i]);\n"
    8982             :     "        }\n"
    8983             :     "        else\n"
    8984             :     "            num[i]=0.5*log(2.0*PI)+(num[i]+0.5)*log(num[i])-num[i]+"
    8985             :     "(1.0*pow(12.0*num[i],-1)-1.0*pow(360*pow(num[i],3),-1));\n"
    8986           0 :     "    }\n";
    8987           0 :     ss << "    tmp=pow(M_E,(num[0]+num[3]+num[7]+num[8]";
    8988           0 :     ss << "-num[1]-num[2]-num[4]-num[5]-num[6]));\n";
    8989           0 :     ss << "    return tmp;\n";
    8990           0 :     ss << "}\n";
    8991           0 : }
    8992             : 
    8993             : enum MixDoubleString
    8994             : {
    8995             :     svDoubleVectorRefDoubleString,
    8996             :     svDoubleVectorRefDouble,
    8997             :     svDoubleVectorRefString,
    8998             :     svDoubleVectorRefNULL,
    8999             :     svSingleVectorRefDoubleString,
    9000             :     svSingleVectorRefDouble,
    9001             :     svSingleVectorRefString,
    9002             :     svSingleVectorRefNULL,
    9003             :     svDoubleDouble
    9004             : };
    9005             : 
    9006           0 : void OpMinA::GenSlidingWindowFunction(
    9007             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    9008             : {
    9009           0 :     int isMixed = 0;
    9010           0 :     ss << "\ndouble " << sSymName;
    9011           0 :     ss << "_"<< BinFuncName() <<"(";
    9012           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    9013             :     {
    9014           0 :         if (i)
    9015           0 :             ss << ",";
    9016           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    9017             :     }
    9018           0 :     ss << ")\n";
    9019           0 :     ss << "{\n";
    9020           0 :     ss << "    int gid0=get_global_id(0);\n";
    9021           0 :     ss << "    double tmp0 = 1.79769e+308;\n";
    9022           0 :     size_t i = vSubArguments.size();
    9023           0 :     ss <<"\n";
    9024           0 :     for (i = 0; i < vSubArguments.size(); i++)
    9025             :     {
    9026           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    9027             :         assert(pCur);
    9028           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    9029             :         {
    9030             :             const formula::DoubleVectorRefToken* pDVR =
    9031           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9032           0 :             if(pDVR->GetArrays()[0].mpNumericArray
    9033           0 :                 && pDVR->GetArrays()[0].mpStringArray)
    9034           0 :                 isMixed = svDoubleVectorRefDoubleString;
    9035           0 :             else if(pDVR->GetArrays()[0].mpNumericArray)
    9036           0 :                 isMixed = svDoubleVectorRefDouble;
    9037           0 :             else if(pDVR->GetArrays()[0].mpStringArray)
    9038           0 :                 isMixed = svDoubleVectorRefString;
    9039             :             else
    9040           0 :                 isMixed = svDoubleVectorRefNULL;
    9041           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    9042           0 :             ss << "    for (int i = ";
    9043           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    9044             : #ifdef  ISNAN
    9045           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    9046           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    9047             : #else
    9048             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    9049             : #endif
    9050           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    9051             : #ifdef  ISNAN
    9052           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    9053           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    9054             : #else
    9055             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    9056             : #endif
    9057           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    9058             : #ifdef  ISNAN
    9059           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    9060           0 :                 ss << " && i < "<< nCurWindowSize << "; i++){\n";
    9061             : #else
    9062             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9063             : #endif
    9064             :             }
    9065             :             else {
    9066             : #ifdef  ISNAN
    9067           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    9068             : #else
    9069             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9070             : #endif
    9071             :             }
    9072             :         }
    9073           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    9074             :         {
    9075             : #ifdef  ISNAN
    9076             :             const formula::SingleVectorRefToken* pSVR =
    9077           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    9078             : 
    9079           0 :             if(pSVR->GetArray().mpNumericArray
    9080           0 :                 && pSVR->GetArray().mpStringArray)
    9081           0 :                 isMixed = svSingleVectorRefDoubleString;
    9082           0 :             else if(pSVR->GetArray().mpNumericArray)
    9083           0 :                 isMixed = svSingleVectorRefDouble;
    9084           0 :             else if(pSVR->GetArray().mpStringArray)
    9085           0 :                 isMixed = svSingleVectorRefString;
    9086             :             else
    9087           0 :                 isMixed = svSingleVectorRefNULL;
    9088           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    9089             : #else
    9090             : #endif
    9091             :         }
    9092           0 :         else if (pCur->GetType() == formula::svDouble)
    9093             :         {
    9094             : #ifdef  ISNAN
    9095           0 :             ss << "    {\n";
    9096           0 :             isMixed = svDoubleDouble;
    9097             : #endif
    9098             :         }
    9099             :         else
    9100             :         {
    9101             : #ifdef  ISNAN
    9102             : #endif
    9103             :         }
    9104             : 
    9105             : #ifdef  ISNAN
    9106           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9107             :         {
    9108           0 :             if(isMixed == svDoubleVectorRefDoubleString
    9109           0 :                 || isMixed == svSingleVectorRefDoubleString)
    9110             :             {
    9111           0 :                 ss << "        if (!isNan(";
    9112           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9113           0 :                 ss << "))\n";
    9114           0 :                 ss << "            tmp0 = tmp0 > ";
    9115           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9116           0 :                 ss << " ? ";
    9117           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9118           0 :                 ss << " : tmp0;\n";
    9119           0 :                 ss << "        else if(isNan(";
    9120           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9121           0 :                 ss << ") && ";
    9122           0 :                 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9123           0 :                 ss << " != 0)\n";
    9124           0 :                 ss << "            tmp0 = tmp0 > 0.0 ? 0.0 : tmp0;\n";
    9125           0 :                 ss << "    }\n";
    9126             :             }
    9127           0 :             else if(isMixed == svDoubleVectorRefDouble
    9128           0 :                 || isMixed == svSingleVectorRefDouble)
    9129             :             {
    9130           0 :                 ss << "        if (!isNan(";
    9131           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9132           0 :                 ss << "))\n";
    9133           0 :                 ss << "            tmp0 = tmp0 > ";
    9134           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9135           0 :                 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9136           0 :                 ss << " : tmp0;";
    9137           0 :                 ss <<"\n    }\n";
    9138             :             }
    9139           0 :             else if(isMixed == svDoubleVectorRefString)
    9140             :             {
    9141           0 :                 ss << "        if(";
    9142           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9143           0 :                 ss << " == 0)\n            continue;\n";
    9144           0 :                 ss << "        tmp0 = tmp0 > 0.0 ? 0.0 : tmp0;\n";
    9145           0 :                 ss << "    }\n";
    9146             :             }
    9147           0 :             else if(isMixed == svSingleVectorRefString)
    9148             :             {
    9149           0 :                 ss << "        if(";
    9150           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9151           0 :                 ss << " != 0)\n";
    9152           0 :                 ss << "            tmp0 = tmp0 > 0.0 ? 0.0 : tmp0;\n";
    9153           0 :                 ss << "    }\n";
    9154             :             }
    9155           0 :             else if(isMixed == svDoubleDouble)
    9156             :             {
    9157           0 :                 ss << "        tmp0 = tmp0 > ";
    9158           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9159           0 :                 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9160           0 :                 ss << " : tmp0;\n    }\n";
    9161             :             }
    9162             :             else
    9163             :             {
    9164           0 :                 ss << "    }\n";
    9165             :             }
    9166             :         }
    9167             :         else
    9168             :         {
    9169           0 :             ss << "        tmp0 = tmp0 > ";
    9170           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9171           0 :             ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9172           0 :             ss << " : tmp0;";
    9173           0 :             ss <<"\n    }\n";
    9174             :         }
    9175             : #else
    9176             :         ss << "        tmp0 = tmp0 > ";
    9177             :         ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9178             :         ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9179             :         ss << " : tmp0;";
    9180             :         ss <<"\n    }\n";
    9181             : #endif
    9182             :     }
    9183           0 :     ss << "    return tmp0 == 1.79769e+308 ? 0.0 : tmp0;\n";
    9184           0 :     ss << "}\n";
    9185           0 : }
    9186           0 : void OpCountA::GenSlidingWindowFunction(
    9187             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    9188             : vSubArguments)
    9189             : {
    9190           0 :     int isMixed = 0;
    9191           0 :     ss << "\ndouble " << sSymName;
    9192           0 :     ss << "_"<< BinFuncName() <<"(";
    9193           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    9194             :     {
    9195           0 :         if (i)
    9196           0 :             ss << ",";
    9197           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    9198             :     }
    9199           0 :     ss << ")\n";
    9200           0 :     ss << "{\n";
    9201           0 :     ss << "    int gid0=get_global_id(0);\n";
    9202           0 :     ss << "    double nCount = 0.0;\n";
    9203           0 :     size_t i = vSubArguments.size();
    9204             : 
    9205           0 :     ss <<"\n";
    9206           0 :     for (i = 0; i < vSubArguments.size(); i++)
    9207             :     {
    9208           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    9209             :         assert(pCur);
    9210           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    9211             :         {
    9212             :             const formula::DoubleVectorRefToken* pDVR =
    9213           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9214           0 :             if(pDVR->GetArrays()[0].mpNumericArray
    9215           0 :                 && pDVR->GetArrays()[0].mpStringArray)
    9216           0 :                 isMixed = svDoubleVectorRefDoubleString;
    9217           0 :             else if(pDVR->GetArrays()[0].mpNumericArray)
    9218           0 :                 isMixed = svDoubleVectorRefDouble;
    9219           0 :             else if(pDVR->GetArrays()[0].mpStringArray)
    9220           0 :                 isMixed = svDoubleVectorRefString;
    9221             :             else
    9222           0 :                 isMixed = svDoubleVectorRefNULL;
    9223           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    9224           0 :             ss << "    for (int i = ";
    9225           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    9226             : #ifdef  ISNAN
    9227           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    9228           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    9229             : #else
    9230             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    9231             : #endif
    9232           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    9233             : #ifdef  ISNAN
    9234           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    9235           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    9236             : #else
    9237             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    9238             : #endif
    9239           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    9240             : #ifdef  ISNAN
    9241           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    9242           0 :                 ss << " && i < "<< nCurWindowSize << "; i++){\n";
    9243             : #else
    9244             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9245             : #endif
    9246             :             }
    9247             :             else {
    9248             : #ifdef  ISNAN
    9249           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    9250             : #else
    9251             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9252             : #endif
    9253             :             }
    9254             :         }
    9255           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    9256             :         {
    9257             : #ifdef  ISNAN
    9258             :             const formula::SingleVectorRefToken* pSVR =
    9259           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    9260             : 
    9261           0 :             if(pSVR->GetArray().mpNumericArray
    9262           0 :                 && pSVR->GetArray().mpStringArray)
    9263           0 :                 isMixed = svSingleVectorRefDoubleString;
    9264           0 :             else if(pSVR->GetArray().mpNumericArray)
    9265           0 :                 isMixed = svSingleVectorRefDouble;
    9266           0 :             else if(pSVR->GetArray().mpStringArray)
    9267           0 :                 isMixed = svSingleVectorRefString;
    9268             :             else
    9269           0 :                 isMixed = svSingleVectorRefNULL;
    9270           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    9271             : #else
    9272             : #endif
    9273             :         }
    9274           0 :         else if (pCur->GetType() == formula::svDouble)
    9275             :         {
    9276             : #ifdef  ISNAN
    9277           0 :             ss << "    {\n";
    9278           0 :             isMixed = svDoubleDouble;
    9279             : #endif
    9280             :         }
    9281             :         else
    9282             :         {
    9283             : #ifdef  ISNAN
    9284             : #endif
    9285             :         }
    9286             : 
    9287             : #ifdef  ISNAN
    9288           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9289             :         {
    9290           0 :             if(isMixed == svDoubleVectorRefDoubleString
    9291           0 :                 || isMixed == svSingleVectorRefDoubleString)
    9292             :             {
    9293           0 :                 ss << "        if (!isNan(";
    9294           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9295           0 :                 ss << ")){\n";
    9296           0 :                 ss << "            nCount+=1.0;\n";
    9297           0 :                 ss << "    }\n";
    9298           0 :                 ss << "        else if(isNan(";
    9299           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9300           0 :                 ss << ") && ";
    9301           0 :                 ss<< vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9302           0 :                 ss << " != 0)\n";
    9303           0 :                 ss << "            nCount+=1.0;\n";
    9304           0 :                 ss << "    }\n";
    9305             :             }
    9306           0 :             else if(isMixed == svDoubleVectorRefDouble
    9307           0 :                 || isMixed == svSingleVectorRefDouble)
    9308             :             {
    9309           0 :                 ss << "        if (!isNan(";
    9310           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9311           0 :                 ss << ")){\n";
    9312           0 :                 ss << "            nCount+=1.0;\n";
    9313           0 :                 ss <<"}\n    }\n";
    9314             :             }
    9315           0 :             else if(isMixed == svDoubleVectorRefString)
    9316             :             {
    9317           0 :                 ss << "        if (!isNan(";
    9318           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9319           0 :                 ss << "))\n";
    9320           0 :                 ss << "            nCount+=1.0;\n";
    9321           0 :                 ss <<"\n    }\n";
    9322             :             }
    9323           0 :             else if(isMixed == svSingleVectorRefString)
    9324             :             {
    9325           0 :                 ss << "        if(";
    9326           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9327           0 :                 ss << " != 0)\n";
    9328           0 :                 ss << "            nCount+=1.0;\n";
    9329           0 :                 ss << "    }\n";
    9330             :             }
    9331           0 :             else if(isMixed == svDoubleDouble)
    9332             :             {
    9333           0 :                 ss << "            nCount+=1.0;\n";
    9334           0 :                 ss << "    }\n";
    9335             :             }
    9336             :             else
    9337             :             {
    9338           0 :                 ss << "    }\n";
    9339             :             }
    9340             :         }
    9341             :         else
    9342             :         {
    9343           0 :                 ss << "            nCount+=1.0;\n";
    9344           0 :                 ss << "    }\n";
    9345             :         }
    9346             : #else
    9347             :         ss << "            nCount+=1.0;\n";
    9348             :         ss << "    }\n";
    9349             : #endif
    9350             :     }
    9351           0 :     ss << "    return nCount;\n";
    9352           0 :     ss << "}\n";
    9353           0 : }
    9354           0 : void OpMaxA::GenSlidingWindowFunction(
    9355             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    9356             : vSubArguments)
    9357             : {
    9358           0 :     int isMixed = 0;
    9359           0 :     ss << "\ndouble " << sSymName;
    9360           0 :     ss << "_"<< BinFuncName() <<"(";
    9361           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    9362             :     {
    9363           0 :         if (i)
    9364           0 :             ss << ",";
    9365           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    9366             :     }
    9367           0 :     ss << ")\n";
    9368           0 :     ss << "{\n";
    9369           0 :     ss << "    int gid0=get_global_id(0);\n";
    9370           0 :     ss << "    double tmp0 = 2.22507e-308;\n";
    9371           0 :     size_t i = vSubArguments.size();
    9372             : 
    9373           0 :     ss <<"\n";
    9374           0 :     for (i = 0; i < vSubArguments.size(); i++)
    9375             :     {
    9376           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    9377             :         assert(pCur);
    9378           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    9379             :         {
    9380             :             const formula::DoubleVectorRefToken* pDVR =
    9381           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9382           0 :             if(pDVR->GetArrays()[0].mpNumericArray
    9383           0 :                 && pDVR->GetArrays()[0].mpStringArray)
    9384           0 :                 isMixed = svDoubleVectorRefDoubleString;
    9385           0 :             else if(pDVR->GetArrays()[0].mpNumericArray)
    9386           0 :                 isMixed = svDoubleVectorRefDouble;
    9387           0 :             else if(pDVR->GetArrays()[0].mpStringArray)
    9388           0 :                 isMixed = svDoubleVectorRefString;
    9389             :             else
    9390           0 :                 isMixed = svDoubleVectorRefNULL;
    9391           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    9392           0 :             ss << "    for (int i = ";
    9393           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    9394             : #ifdef  ISNAN
    9395           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    9396           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    9397             : #else
    9398             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    9399             : #endif
    9400           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    9401             : #ifdef  ISNAN
    9402           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    9403           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    9404             : #else
    9405             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    9406             : #endif
    9407           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    9408             : #ifdef  ISNAN
    9409           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    9410           0 :                 ss << " && i < "<< nCurWindowSize << "; i++){\n";
    9411             : #else
    9412             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9413             : #endif
    9414             :             }
    9415             :             else {
    9416             : #ifdef  ISNAN
    9417           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    9418             : #else
    9419             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9420             : #endif
    9421             :             }
    9422             :         }
    9423           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    9424             :         {
    9425             : #ifdef  ISNAN
    9426             :             const formula::SingleVectorRefToken* pSVR =
    9427           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    9428             : 
    9429           0 :             if(pSVR->GetArray().mpNumericArray
    9430           0 :                 && pSVR->GetArray().mpStringArray)
    9431           0 :                 isMixed = svSingleVectorRefDoubleString;
    9432           0 :             else if(pSVR->GetArray().mpNumericArray)
    9433           0 :                 isMixed = svSingleVectorRefDouble;
    9434           0 :             else if(pSVR->GetArray().mpStringArray)
    9435           0 :                 isMixed = svSingleVectorRefString;
    9436             :             else
    9437           0 :                 isMixed = svSingleVectorRefNULL;
    9438           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    9439             : #else
    9440             : #endif
    9441             :         }
    9442           0 :         else if (pCur->GetType() == formula::svDouble)
    9443             :         {
    9444             : #ifdef  ISNAN
    9445           0 :             ss << "    {\n";
    9446           0 :             isMixed = svDoubleDouble;
    9447             : #endif
    9448             :         }
    9449             :         else
    9450             :         {
    9451             : #ifdef  ISNAN
    9452             : #endif
    9453             :         }
    9454             : 
    9455             : #ifdef  ISNAN
    9456           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9457             :         {
    9458           0 :             if(isMixed == svDoubleVectorRefDoubleString
    9459           0 :                 || isMixed == svSingleVectorRefDoubleString)
    9460             :             {
    9461           0 :                 ss << "        if (!isNan(";
    9462           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9463           0 :                 ss << "))\n";
    9464           0 :                 ss << "            tmp0 = tmp0 < ";
    9465           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9466           0 :                 ss << " ? ";
    9467           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9468           0 :                 ss << " : tmp0;\n";
    9469           0 :                 ss << "        else if(isNan(";
    9470           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9471           0 :                 ss << ") && ";
    9472           0 :                 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9473           0 :                 ss << " != 0)\n";
    9474           0 :                 ss << "            tmp0 = tmp0 < 0.0 ? 0.0 : tmp0;\n";
    9475           0 :                 ss << "    }\n";
    9476             :             }
    9477           0 :             else if(isMixed == svDoubleVectorRefDouble
    9478           0 :                 || isMixed == svSingleVectorRefDouble)
    9479             :             {
    9480           0 :                 ss << "        if (!isNan(";
    9481           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9482           0 :                 ss << "))\n";
    9483           0 :                 ss << "            tmp0 = tmp0 < ";
    9484           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9485           0 :                 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9486           0 :                 ss << " : tmp0;";
    9487           0 :                 ss <<"\n    }\n";
    9488             :             }
    9489           0 :             else if(isMixed == svDoubleVectorRefString)
    9490             :             {
    9491           0 :                 ss << "        if(";
    9492           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9493           0 :                 ss << " == 0)\n            continue;\n";
    9494           0 :                 ss << "        tmp0 = tmp0 < 0.0 ? 0.0 : tmp0;\n";
    9495           0 :                 ss << "    }\n";
    9496             :             }
    9497           0 :             else if(isMixed == svSingleVectorRefString)
    9498             :             {
    9499           0 :                 ss << "        if(";
    9500           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9501           0 :                 ss << " != 0)\n";
    9502           0 :                 ss << "            tmp0 = tmp0 < 0.0 ? 0.0 : tmp0;\n";
    9503           0 :                 ss << "    }\n";
    9504             :             }
    9505           0 :             else if(isMixed == svDoubleDouble)
    9506             :             {
    9507           0 :                 ss << "        tmp0 = tmp0 < ";
    9508           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9509           0 :                 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9510           0 :                 ss << " : tmp0;\n    }\n";
    9511             :             }
    9512             :             else
    9513             :             {
    9514           0 :                 ss << "    }\n";
    9515             :             }
    9516             :         }
    9517             :         else
    9518             :         {
    9519           0 :             ss << "        tmp0 = tmp0 < ";
    9520           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9521           0 :             ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9522           0 :             ss << " : tmp0;";
    9523           0 :             ss <<"\n    }\n";
    9524             :         }
    9525             : #else
    9526             :         ss << "        tmp0 = tmp0 < ";
    9527             :         ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9528             :         ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9529             :         ss << " : tmp0;";
    9530             :         ss <<"\n    }\n";
    9531             : #endif
    9532             :     }
    9533           0 :     ss << "    return tmp0 == 2.22507e-308 ? 0.0 : tmp0;\n";
    9534           0 :     ss << "}\n";
    9535           0 : }
    9536           0 : void OpAverageA::GenSlidingWindowFunction(
    9537             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    9538             : vSubArguments)
    9539             : {
    9540           0 :     int isMixed = 0;
    9541           0 :     ss << "\ndouble " << sSymName;
    9542           0 :     ss << "_"<< BinFuncName() <<"(";
    9543           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    9544             :     {
    9545           0 :         if (i)
    9546           0 :             ss << ",";
    9547           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    9548             :     }
    9549           0 :     ss << ")\n";
    9550           0 :     ss << "{\n";
    9551           0 :     ss << "    int gid0=get_global_id(0);\n";
    9552           0 :     ss << "    double tmp0 = 0.0;\n";
    9553           0 :     ss << "    double nCount = 0.0;\n";
    9554           0 :     size_t i = vSubArguments.size();
    9555           0 :     ss <<"\n";
    9556           0 :     for (i = 0; i < vSubArguments.size(); i++)
    9557             :     {
    9558           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    9559             :         assert(pCur);
    9560           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    9561             :         {
    9562             :             const formula::DoubleVectorRefToken* pDVR =
    9563           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9564           0 :             if(pDVR->GetArrays()[0].mpNumericArray
    9565           0 :                 && pDVR->GetArrays()[0].mpStringArray)
    9566           0 :                 isMixed = svDoubleVectorRefDoubleString;
    9567           0 :             else if(pDVR->GetArrays()[0].mpNumericArray)
    9568           0 :                 isMixed = svDoubleVectorRefDouble;
    9569           0 :             else if(pDVR->GetArrays()[0].mpStringArray)
    9570           0 :                 isMixed = svDoubleVectorRefString;
    9571             :             else
    9572           0 :                 isMixed = svDoubleVectorRefNULL;
    9573           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    9574           0 :             ss << "    for (int i = ";
    9575           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    9576             : #ifdef  ISNAN
    9577           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    9578           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    9579             : #else
    9580             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    9581             : #endif
    9582           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    9583             : #ifdef  ISNAN
    9584           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    9585           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    9586             : #else
    9587             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    9588             : #endif
    9589           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    9590             : #ifdef  ISNAN
    9591           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    9592           0 :                 ss << " && i < "<< nCurWindowSize << "; i++){\n";
    9593             : #else
    9594             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9595             : #endif
    9596             :             }
    9597             :             else {
    9598             : #ifdef  ISNAN
    9599           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    9600             : #else
    9601             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9602             : #endif
    9603             :             }
    9604             :         }
    9605           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    9606             :         {
    9607             : #ifdef  ISNAN
    9608             :             const formula::SingleVectorRefToken* pSVR =
    9609           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    9610             : 
    9611           0 :             if(pSVR->GetArray().mpNumericArray
    9612           0 :                 && pSVR->GetArray().mpStringArray)
    9613           0 :                 isMixed = svSingleVectorRefDoubleString;
    9614           0 :             else if(pSVR->GetArray().mpNumericArray)
    9615           0 :                 isMixed = svSingleVectorRefDouble;
    9616           0 :             else if(pSVR->GetArray().mpStringArray)
    9617           0 :                 isMixed = svSingleVectorRefString;
    9618             :             else
    9619           0 :                 isMixed = svSingleVectorRefNULL;
    9620           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    9621             : #else
    9622             : #endif
    9623             :         }
    9624           0 :         else if (pCur->GetType() == formula::svDouble)
    9625             :         {
    9626             : #ifdef  ISNAN
    9627           0 :             ss << "    {\n";
    9628           0 :             isMixed = svDoubleDouble;
    9629             : #endif
    9630             :         }
    9631             :         else
    9632             :         {
    9633             : #ifdef  ISNAN
    9634             : #endif
    9635             :         }
    9636             : 
    9637             : #ifdef  ISNAN
    9638           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9639             :         {
    9640           0 :             if(isMixed == svDoubleVectorRefDoubleString
    9641           0 :                 || isMixed == svSingleVectorRefDoubleString)
    9642             :             {
    9643           0 :                 ss << "        if (!isNan(";
    9644           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9645           0 :                 ss << ")){\n";
    9646           0 :                 ss << "            tmp0 +=";
    9647           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9648           0 :                 ss << ";\n";
    9649           0 :                 ss << "            nCount+=1.0;\n";
    9650           0 :                 ss << "    }\n";
    9651           0 :                 ss << "        else if(isNan(";
    9652           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9653           0 :                 ss << ") && ";
    9654           0 :                 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9655           0 :                 ss << " != 0)\n";
    9656           0 :                 ss << "            nCount+=1.0;\n";
    9657           0 :                 ss << "    }\n";
    9658             :             }
    9659           0 :             else if(isMixed == svDoubleVectorRefDouble
    9660           0 :                 || isMixed == svSingleVectorRefDouble)
    9661             :             {
    9662           0 :                 ss << "        if (!isNan(";
    9663           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9664           0 :                 ss << ")){\n";
    9665           0 :                 ss << "            tmp0 +=";
    9666           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9667           0 :                 ss << ";\n";
    9668           0 :                 ss << "            nCount+=1.0;\n";
    9669           0 :                 ss <<"}\n    }\n";
    9670             :             }
    9671           0 :             else if(isMixed == svDoubleVectorRefString)
    9672             :             {
    9673           0 :                 ss << "        if (!isNan(";
    9674           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9675           0 :                 ss << "))\n";
    9676           0 :                 ss << "            nCount+=1.0;\n";
    9677           0 :                 ss <<"\n    }\n";
    9678             :             }
    9679           0 :             else if(isMixed == svSingleVectorRefString)
    9680             :             {
    9681           0 :                 ss << "        if(";
    9682           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9683           0 :                 ss << " != 0)\n";
    9684           0 :                 ss << "            nCount+=1.0;\n";
    9685           0 :                 ss << "    }\n";
    9686             :             }
    9687           0 :             else if(isMixed == svDoubleDouble)
    9688             :             {
    9689           0 :                 ss << "            tmp0 +=";
    9690           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9691           0 :                 ss << ";\n";
    9692           0 :                 ss << "            nCount+=1.0;\n";
    9693           0 :                 ss << "    }\n";
    9694             :             }
    9695             :             else
    9696             :             {
    9697           0 :                 ss << "    }\n";
    9698             :             }
    9699             :         }
    9700             :         else
    9701             :         {
    9702           0 :                 ss << "            tmp0 +=";
    9703           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9704           0 :                 ss << ";\n";
    9705           0 :                 ss << "            nCount+=1.0;\n";
    9706           0 :                 ss << "    }\n";
    9707             :         }
    9708             : #else
    9709             :         ss << "            tmp0 +=";
    9710             :         ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9711             :         ss << ";\n";
    9712             :         ss << "            nCount+=1.0;\n";
    9713             :         ss << "    }\n";
    9714             : #endif
    9715             :     }
    9716           0 :     ss << "    return tmp0*pow(nCount,-1);\n";
    9717           0 :     ss << "}\n";
    9718           0 : }
    9719           0 : void OpVarA::GenSlidingWindowFunction(std::stringstream &ss,
    9720             :             const std::string &sSymName, SubArguments &vSubArguments)
    9721             : {
    9722           0 :     int isMixedDV = 0;
    9723           0 :     int isMixedSV = 0;
    9724           0 :     ss << "\ndouble " << sSymName;
    9725           0 :     ss << "_" << BinFuncName() << "(";
    9726           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    9727             :     {
    9728           0 :         if (i)
    9729           0 :             ss << ",";
    9730           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    9731             :     }
    9732           0 :     ss << "){\n";
    9733           0 :     ss << "    int gid0 = get_global_id(0);\n";
    9734           0 :     ss << "    double fSum = 0.0;\n";
    9735           0 :     ss << "    double fMean = 0.0;\n";
    9736           0 :     ss << "    double vSum = 0.0;\n";
    9737           0 :     ss << "    double fCount = 0.0;\n";
    9738           0 :     ss << "    double arg = 0.0;\n";
    9739           0 :     unsigned i = vSubArguments.size();
    9740           0 :     while (i--)
    9741             :     {
    9742           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    9743             :         assert(pCur);
    9744             : 
    9745           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9746             :         {
    9747           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    9748             :             {
    9749             :                 const formula::DoubleVectorRefToken* pDVR =
    9750           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9751             :                 assert(pDVR);
    9752           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
    9753           0 :                     && pDVR->GetArrays()[0].mpStringArray)
    9754           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
    9755           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
    9756           0 :                     isMixedDV = svDoubleVectorRefDouble;
    9757           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
    9758           0 :                     isMixedDV = svDoubleVectorRefString;
    9759             :                 else
    9760           0 :                     isMixedDV = svDoubleVectorRefNULL;
    9761             : 
    9762           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    9763           0 :                 ss << "    for (int i = ";
    9764           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    9765             :                 {
    9766             : #ifdef  ISNAN
    9767           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    9768           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    9769           0 :                     ss << "    {\n";
    9770             : #else
    9771             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    9772             :                     ss << "    {\n";
    9773             : #endif
    9774             :                 }
    9775           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    9776             :                 {
    9777             : #ifdef  ISNAN
    9778           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    9779           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    9780           0 :                     ss << "    {\n";
    9781             : #else
    9782             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    9783             :                     ss << "    {\n";
    9784             : #endif
    9785             :                 }
    9786           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    9787             :                 {
    9788             : #ifdef  ISNAN
    9789           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    9790           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    9791           0 :                     ss << "    {\n";
    9792             : #else
    9793             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    9794             :                     ss << "    {\n";
    9795             : #endif
    9796             :                 }
    9797             :                 else
    9798             :                 {
    9799             : #ifdef  ISNAN
    9800           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    9801           0 :                     ss << "    {\n";
    9802             : #else
    9803             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    9804             :                     ss << "    {\n";
    9805             : #endif
    9806             :                 }
    9807             : 
    9808           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
    9809             :                 {
    9810           0 :                     ss << "        arg = ";
    9811           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9812           0 :                     ss << ";\n";
    9813             : #ifdef  ISNAN
    9814           0 :                     ss << "        if (isNan(arg) && ";
    9815           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9816           0 :                     ss << " == 0)\n";
    9817           0 :                     ss << "            continue;\n";
    9818           0 :                     ss << "        if(isNan(arg) && ";
    9819           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9820           0 :                     ss << " != 0)\n";
    9821           0 :                     ss << "        {\n";
    9822           0 :                     ss << "            fCount = fCount + 1.0;\n";
    9823           0 :                     ss << "            continue;\n";
    9824           0 :                     ss << "        }\n";
    9825             : #endif
    9826           0 :                     ss << "        fSum += arg;\n";
    9827           0 :                     ss << "        fCount = fCount + 1.0;\n";
    9828           0 :                     ss << "    }\n";
    9829             :                 }
    9830           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
    9831             :                 {
    9832           0 :                     ss << "        arg = ";
    9833           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9834           0 :                     ss << ";\n";
    9835             : #ifdef  ISNAN
    9836           0 :                     ss << "        if (isNan(arg))\n";
    9837           0 :                     ss << "            continue;\n";
    9838             : #endif
    9839           0 :                     ss << "        fSum += arg;\n";
    9840           0 :                     ss << "        fCount = fCount + 1.0;\n";
    9841           0 :                     ss << "    }\n";
    9842             :                 }
    9843           0 :                 else if(isMixedDV == svDoubleVectorRefString)
    9844             :                 {
    9845           0 :                     ss << "        if (";
    9846           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9847           0 :                     ss << " == 0)\n";
    9848           0 :                     ss << "            continue;\n";
    9849           0 :                     ss << "        fCount = fCount + 1.0;\n";
    9850           0 :                     ss << "    }\n";
    9851             :                 }
    9852             :                 else
    9853             :                 {
    9854           0 :                     ss << "        continue;\n";
    9855           0 :                     ss << "    }\n";
    9856             :                 }
    9857             : 
    9858             :             }
    9859           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    9860             :             {
    9861             :                 const formula::SingleVectorRefToken* pSVR =
    9862           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    9863             :                 assert(pSVR);
    9864           0 :                 if(pSVR->GetArray().mpNumericArray
    9865           0 :                     && pSVR->GetArray().mpStringArray)
    9866           0 :                     isMixedSV = svSingleVectorRefDoubleString;
    9867           0 :                 else if(pSVR->GetArray().mpNumericArray)
    9868           0 :                     isMixedSV = svSingleVectorRefDouble;
    9869           0 :                 else if(pSVR->GetArray().mpStringArray)
    9870           0 :                     isMixedSV = svSingleVectorRefString;
    9871             :                 else
    9872           0 :                     isMixedSV = svSingleVectorRefNULL;
    9873             : 
    9874           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
    9875             :                 {
    9876             : #ifdef  ISNAN
    9877           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    9878           0 :                     ss << "    {\n";
    9879             : #endif
    9880           0 :                     ss << "        arg = ";
    9881           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9882           0 :                     ss << ";\n";
    9883             : #ifdef  ISNAN
    9884           0 :                     ss << "        if (!isNan(arg))\n";
    9885           0 :                     ss << "        {\n";
    9886             : #endif
    9887           0 :                     ss << "            fSum += arg;\n";
    9888           0 :                     ss << "            fCount = fCount + 1.0;\n";
    9889             : #ifdef ISNAN
    9890           0 :                     ss << "        }\n";
    9891           0 :                     ss << "        if (isNan(arg) && ";
    9892           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9893           0 :                     ss << " != 0)\n";
    9894           0 :                     ss << "            fCount = fCount + 1.0;\n";
    9895           0 :                     ss << "    }\n";
    9896             : #endif
    9897             :                 }
    9898           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
    9899             :                 {
    9900             : #ifdef  ISNAN
    9901           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    9902           0 :                     ss << "    {\n";
    9903             : #endif
    9904           0 :                     ss << "        arg = ";
    9905           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    9906             : #ifdef ISNAN
    9907           0 :                     ss << "        if (!isNan(arg))\n";
    9908           0 :                     ss << "        {\n";
    9909             : #endif
    9910           0 :                     ss << "            fSum += arg;\n";
    9911           0 :                     ss << "            fCount += 1.0;\n";
    9912             : #ifdef ISNAN
    9913           0 :                     ss << "        }\n";
    9914           0 :                     ss << "    }\n";
    9915             : #endif
    9916             :                 }
    9917           0 :                 else if(isMixedSV == svSingleVectorRefString)
    9918             :                 {
    9919             : 
    9920           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    9921           0 :                     ss << "    {\n";
    9922           0 :                     ss << "        if (";
    9923           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9924           0 :                     ss << " != 0)\n";
    9925           0 :                     ss << "            fCount = fCount + 1.0;\n";
    9926           0 :                     ss << "    }\n";
    9927             :                 }
    9928             :                 else
    9929             :                 {
    9930           0 :                     ss << "    arg =0.0;\n";
    9931             :                 }
    9932             :             }
    9933             :             else
    9934             :             {
    9935           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    9936           0 :                 ss << "    fSum += arg;\n";
    9937           0 :                 ss << "    fCount = fCount + 1.0;\n";
    9938             :             }
    9939             :         }
    9940             :         else
    9941             :         {
    9942           0 :             ss << "    arg = ";
    9943           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    9944           0 :             ss << "    fSum += arg;\n";
    9945           0 :             ss << "    fCount = fCount + 1.0;\n";
    9946             :         }
    9947           0 :         if (i == 0)
    9948             :         {
    9949           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
    9950             :         }
    9951             :     }
    9952           0 :     i = vSubArguments.size();
    9953           0 :     while (i--)
    9954             :     {
    9955           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    9956             :         assert(pCur);
    9957             : 
    9958           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9959             :         {
    9960           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    9961             :             {
    9962             :                 const formula::DoubleVectorRefToken* pDVR =
    9963           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9964           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
    9965           0 :                     && pDVR->GetArrays()[0].mpStringArray)
    9966           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
    9967           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
    9968           0 :                     isMixedDV = svDoubleVectorRefDouble;
    9969           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
    9970           0 :                     isMixedDV = svDoubleVectorRefString;
    9971             :                 else
    9972           0 :                     isMixedDV = svDoubleVectorRefNULL;
    9973             : 
    9974           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    9975           0 :                 ss << "    for (int i = ";
    9976           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    9977             :                 {
    9978             : #ifdef  ISNAN
    9979           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    9980           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    9981           0 :                     ss << "    {\n";
    9982             : #else
    9983             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    9984             :                     ss << "    {\n";
    9985             : #endif
    9986             :                 }
    9987           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    9988             :                 {
    9989             : #ifdef  ISNAN
    9990           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    9991           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    9992           0 :                     ss << "    {\n";
    9993             : #else
    9994             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    9995             :                     ss << "    {\n";
    9996             : #endif
    9997             :                 }
    9998           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    9999             :                 {
   10000             : #ifdef  ISNAN
   10001           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   10002           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   10003           0 :                     ss << "    {\n";
   10004             : #else
   10005             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   10006             :                     ss << "    {\n";
   10007             : #endif
   10008             :                 }
   10009             :                 else
   10010             :                 {
   10011             : #ifdef  ISNAN
   10012           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10013           0 :                     ss << "    {\n";
   10014             : #else
   10015             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10016             :                     ss << "    {\n";
   10017             : #endif
   10018             :                 }
   10019           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   10020             :                 {
   10021           0 :                     ss << "        arg = ";
   10022           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10023           0 :                     ss << ";\n";
   10024             : #ifdef  ISNAN
   10025           0 :                     ss << "        if (isNan(arg) && ";
   10026           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10027           0 :                     ss << " == 0)\n";
   10028           0 :                     ss << "            continue;\n";
   10029           0 :                     ss << "        if(isNan(arg) && ";
   10030           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10031           0 :                     ss << " != 0)\n";
   10032           0 :                     ss << "            arg = 0.0;\n";
   10033             : #endif
   10034           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10035           0 :                     ss << "    }\n";
   10036             : 
   10037             :                 }
   10038           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   10039             :                 {
   10040           0 :                     ss << "        arg = ";
   10041           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10042           0 :                     ss << ";\n";
   10043             : #ifdef  ISNAN
   10044           0 :                     ss << "        if (isNan(arg))\n";
   10045           0 :                     ss << "            continue;\n";
   10046             : #endif
   10047           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10048           0 :                     ss << "    }\n";
   10049             : 
   10050             :                 }
   10051           0 :                 else if(isMixedDV == svDoubleVectorRefString)
   10052             :                 {
   10053           0 :                     ss << "        if (";
   10054           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10055           0 :                     ss << " == 0)\n";
   10056           0 :                     ss << "            continue;\n";
   10057           0 :                     ss << "        arg = 0.0;\n";
   10058           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10059           0 :                     ss << "    }\n";
   10060             :                 }
   10061             :                 else
   10062             :                 {
   10063           0 :                     ss << "        continue;\n";
   10064           0 :                     ss << "    }\n";
   10065             :                 }
   10066             :             }
   10067           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   10068             :             {
   10069             :                 const formula::SingleVectorRefToken* pSVR =
   10070           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   10071           0 :                 if(pSVR->GetArray().mpNumericArray
   10072           0 :                     && pSVR->GetArray().mpStringArray)
   10073           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   10074           0 :                 else if(pSVR->GetArray().mpNumericArray)
   10075           0 :                     isMixedSV = svSingleVectorRefDouble;
   10076           0 :                 else if(pSVR->GetArray().mpStringArray)
   10077           0 :                     isMixedSV = svSingleVectorRefString;
   10078             :                 else
   10079           0 :                     isMixedSV = svSingleVectorRefNULL;
   10080             : 
   10081           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   10082             :                 {
   10083             : #ifdef  ISNAN
   10084           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10085           0 :                     ss << "    {\n";
   10086             : #endif
   10087           0 :                     ss << "        arg = ";
   10088           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10089           0 :                     ss << ";\n";
   10090             : #ifdef  ISNAN
   10091           0 :                     ss << "        if (!isNan(arg))\n";
   10092             : #endif
   10093           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10094             : #ifdef ISNAN
   10095           0 :                     ss << "        if (isNan(arg) && ";
   10096           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10097           0 :                     ss << " != 0)\n";
   10098           0 :                     ss << "        {\n";
   10099           0 :                     ss << "            arg = 0.0;\n";
   10100           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10101           0 :                     ss << "        }\n";
   10102           0 :                     ss << "    }\n";
   10103             : #endif
   10104             :                 }
   10105           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   10106             :                 {
   10107             : #ifdef  ISNAN
   10108           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10109           0 :                    ss << "    {\n";
   10110             : #endif
   10111           0 :                     ss << "        arg = ";
   10112           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10113             : #ifdef  ISNAN
   10114           0 :                     ss << "        if (!isNan(arg))\n";
   10115           0 :                     ss << "        {\n";
   10116             : #endif
   10117           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10118             : #ifdef ISNAN
   10119           0 :                     ss << "        }\n";
   10120           0 :                    ss << "    }\n";
   10121             : #endif
   10122             :                 }
   10123           0 :                 else if(isMixedSV == svSingleVectorRefString)
   10124             :                 {
   10125           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10126           0 :                     ss << "    {\n";
   10127           0 :                     ss << "        if (";
   10128           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10129           0 :                     ss << " != 0)\n";
   10130           0 :                     ss << "        {\n";
   10131           0 :                     ss << "            arg = 0.0;\n";
   10132           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10133           0 :                     ss << "        }\n";
   10134           0 :                     ss << "    }\n";
   10135             :                 }
   10136             :                 else
   10137             :                 {
   10138           0 :                     ss << "    arg = 0.0;\n";
   10139             :                 }
   10140             :             }
   10141             :             else
   10142             :             {
   10143           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   10144           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   10145             :             }
   10146             :         }
   10147             :         else
   10148             :         {
   10149           0 :             ss << "    arg = ";
   10150           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10151           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   10152             :         }
   10153             :     }
   10154           0 :     ss << "    if (fCount <= 1.0)\n";
   10155           0 :     ss << "        return DBL_MAX;\n";
   10156           0 :     ss << "    else\n";
   10157           0 :     ss << "        return vSum * pow(fCount - 1.0,-1.0);\n";
   10158           0 :     ss << "}\n";
   10159           0 : }
   10160             : 
   10161           0 : void OpVarPA::GenSlidingWindowFunction(std::stringstream &ss,
   10162             :             const std::string &sSymName, SubArguments &vSubArguments)
   10163             : {
   10164           0 :     int isMixedDV = 0;
   10165           0 :     int isMixedSV = 0;
   10166           0 :     ss << "\ndouble " << sSymName;
   10167           0 :     ss << "_" << BinFuncName() << "(";
   10168           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   10169             :     {
   10170           0 :         if (i)
   10171           0 :             ss << ",";
   10172           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
   10173             :     }
   10174           0 :     ss << "){\n";
   10175           0 :     ss << "    int gid0 = get_global_id(0);\n";
   10176           0 :     ss << "    double fSum = 0.0;\n";
   10177           0 :     ss << "    double fMean = 0.0;\n";
   10178           0 :     ss << "    double vSum = 0.0;\n";
   10179           0 :     ss << "    double fCount = 0.0;\n";
   10180           0 :     ss << "    double arg = 0.0;\n";
   10181           0 :     unsigned i = vSubArguments.size();
   10182           0 :     while (i--)
   10183             :     {
   10184           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   10185             :         assert(pCur);
   10186             : 
   10187           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
   10188             :         {
   10189           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   10190             :             {
   10191             :                 const formula::DoubleVectorRefToken* pDVR =
   10192           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   10193           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   10194           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   10195           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   10196           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   10197           0 :                     isMixedDV = svDoubleVectorRefDouble;
   10198           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   10199           0 :                     isMixedDV = svDoubleVectorRefString;
   10200             :                 else
   10201           0 :                     isMixedDV = svDoubleVectorRefNULL;
   10202             : 
   10203           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   10204           0 :                 ss << "    for (int i = ";
   10205           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   10206             :                 {
   10207             : #ifdef  ISNAN
   10208           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   10209           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   10210           0 :                     ss << "    {\n";
   10211             : #else
   10212             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   10213             :                     ss << "    {\n";
   10214             : #endif
   10215             :                 }
   10216           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10217             :                 {
   10218             : #ifdef  ISNAN
   10219           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   10220           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   10221           0 :                     ss << "    {\n";
   10222             : #else
   10223             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   10224             :                     ss << "    {\n";
   10225             : #endif
   10226             :                 }
   10227           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10228             :                 {
   10229             : #ifdef  ISNAN
   10230           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   10231           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   10232           0 :                     ss << "    {\n";
   10233             : #else
   10234             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   10235             :                     ss << "    {\n";
   10236             : #endif
   10237             :                 }
   10238             :                 else
   10239             :                 {
   10240             : #ifdef  ISNAN
   10241           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10242           0 :                     ss << "    {\n";
   10243             : #else
   10244             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10245             :                     ss << "    {\n";
   10246             : #endif
   10247             :                 }
   10248             : 
   10249           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   10250             :                 {
   10251           0 :                     ss << "        arg = ";
   10252           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10253           0 :                     ss << ";\n";
   10254             : #ifdef  ISNAN
   10255           0 :                     ss << "        if (isNan(arg) && ";
   10256           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10257           0 :                     ss << " == 0)\n";
   10258           0 :                     ss << "            continue;\n";
   10259           0 :                     ss << "        if(isNan(arg) && ";
   10260           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10261           0 :                     ss << " != 0)\n";
   10262           0 :                     ss << "        {\n";
   10263           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10264           0 :                     ss << "            continue;\n";
   10265           0 :                     ss << "        }\n";
   10266             : #endif
   10267           0 :                     ss << "        fSum += arg;\n";
   10268           0 :                     ss << "        fCount = fCount + 1.0;\n";
   10269           0 :                     ss << "    }\n";
   10270             :                 }
   10271           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   10272             :                 {
   10273           0 :                     ss << "        arg = ";
   10274           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10275           0 :                     ss << ";\n";
   10276             : #ifdef  ISNAN
   10277           0 :                     ss << "        if (isNan(arg))\n";
   10278           0 :                     ss << "            continue;\n";
   10279             : #endif
   10280           0 :                     ss << "        fSum += arg;\n";
   10281           0 :                     ss << "        fCount = fCount + 1.0;\n";
   10282           0 :                     ss << "    }\n";
   10283             :                 }
   10284           0 :                 else if(isMixedDV == svDoubleVectorRefString)
   10285             :                 {
   10286           0 :                     ss << "        if (";
   10287           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10288           0 :                     ss << " == 0)\n";
   10289           0 :                     ss << "            continue;\n";
   10290           0 :                     ss << "        fCount = fCount + 1.0;\n";
   10291           0 :                     ss << "    }\n";
   10292             :                 }
   10293             :                 else
   10294             :                 {
   10295           0 :                     ss << "        continue;\n";
   10296           0 :                     ss << "    }\n";
   10297             :                 }
   10298             : 
   10299             :             }
   10300           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   10301             :             {
   10302             :                 const formula::SingleVectorRefToken* pSVR =
   10303           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   10304           0 :                 if(pSVR->GetArray().mpNumericArray
   10305           0 :                     && pSVR->GetArray().mpStringArray)
   10306           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   10307           0 :                 else if(pSVR->GetArray().mpNumericArray)
   10308           0 :                     isMixedSV = svSingleVectorRefDouble;
   10309           0 :                 else if(pSVR->GetArray().mpStringArray)
   10310           0 :                     isMixedSV = svSingleVectorRefString;
   10311             :                 else
   10312           0 :                     isMixedSV = svSingleVectorRefNULL;
   10313             : 
   10314           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   10315             :                 {
   10316             : #ifdef  ISNAN
   10317           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10318           0 :                     ss << "    {\n";
   10319             : #endif
   10320           0 :                     ss << "        arg = ";
   10321           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10322           0 :                     ss << ";\n";
   10323             : #ifdef  ISNAN
   10324           0 :                     ss << "        if (!isNan(arg))\n";
   10325           0 :                     ss << "        {\n";
   10326             : #endif
   10327           0 :                     ss << "            fSum += arg;\n";
   10328           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10329             : #ifdef ISNAN
   10330           0 :                     ss << "        }\n";
   10331           0 :                     ss << "        if (isNan(arg) && ";
   10332           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10333           0 :                     ss << " != 0)\n";
   10334           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10335           0 :                     ss << "    }\n";
   10336             : #endif
   10337             :                 }
   10338           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   10339             :                 {
   10340             : #ifdef  ISNAN
   10341           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10342           0 :                     ss << "    {\n";
   10343             : #endif
   10344           0 :                     ss << "        arg = ";
   10345           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10346             : #ifdef ISNAN
   10347           0 :                     ss << "        if (!isNan(arg))\n";
   10348           0 :                     ss << "        {\n";
   10349             : #endif
   10350           0 :                     ss << "            fSum += arg;\n";
   10351           0 :                     ss << "            fCount += 1.0;\n";
   10352             : #ifdef ISNAN
   10353           0 :                     ss << "        }\n";
   10354           0 :                     ss << "    }\n";
   10355             : #endif
   10356             :                 }
   10357           0 :                 else if(isMixedSV == svSingleVectorRefString)
   10358             :                 {
   10359             : 
   10360           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10361           0 :                     ss << "    {\n";
   10362           0 :                     ss << "        if (";
   10363           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10364           0 :                     ss << " != 0)\n";
   10365           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10366           0 :                     ss << "    }\n";
   10367             :                 }
   10368             :                 else
   10369             :                 {
   10370           0 :                     ss << "    arg =0.0;\n";
   10371             :                 }
   10372             :             }
   10373             :             else
   10374             :             {
   10375           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   10376           0 :                 ss << "    fSum += arg;\n";
   10377           0 :                 ss << "    fCount = fCount + 1.0;\n";
   10378             :             }
   10379             :         }
   10380             :         else
   10381             :         {
   10382           0 :             ss << "    arg = ";
   10383           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10384           0 :             ss << "    fSum += arg;\n";
   10385           0 :             ss << "    fCount = fCount + 1.0;\n";
   10386             :         }
   10387           0 :         if (i == 0)
   10388             :         {
   10389           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
   10390             :         }
   10391             :     }
   10392           0 :     i = vSubArguments.size();
   10393           0 :     while (i--)
   10394             :     {
   10395           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   10396             :         assert(pCur);
   10397             : 
   10398           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
   10399             :         {
   10400           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   10401             :             {
   10402             :                 const formula::DoubleVectorRefToken* pDVR =
   10403           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   10404           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   10405           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   10406           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   10407           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   10408           0 :                     isMixedDV = svDoubleVectorRefDouble;
   10409           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   10410           0 :                     isMixedDV = svDoubleVectorRefString;
   10411             :                 else
   10412           0 :                     isMixedDV = svDoubleVectorRefNULL;
   10413             : 
   10414           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   10415           0 :                 ss << "    for (int i = ";
   10416           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   10417             :                 {
   10418             : #ifdef  ISNAN
   10419           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   10420           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   10421           0 :                     ss << "    {\n";
   10422             : #else
   10423             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   10424             :                     ss << "    {\n";
   10425             : #endif
   10426             :                 }
   10427           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10428             :                 {
   10429             : #ifdef  ISNAN
   10430           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   10431           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   10432           0 :                     ss << "    {\n";
   10433             : #else
   10434             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   10435             :                     ss << "    {\n";
   10436             : #endif
   10437             :                 }
   10438           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10439             :                 {
   10440             : #ifdef  ISNAN
   10441           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   10442           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   10443           0 :                     ss << "    {\n";
   10444             : #else
   10445             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   10446             :                     ss << "    {\n";
   10447             : #endif
   10448             :                 }
   10449             :                 else
   10450             :                 {
   10451             : #ifdef  ISNAN
   10452           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10453           0 :                     ss << "    {\n";
   10454             : #else
   10455             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10456             :                     ss << "    {\n";
   10457             : #endif
   10458             :                 }
   10459           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   10460             :                 {
   10461           0 :                     ss << "        arg = ";
   10462           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10463           0 :                     ss << ";\n";
   10464             : #ifdef  ISNAN
   10465           0 :                     ss << "        if (isNan(arg) && ";
   10466           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10467           0 :                     ss << " == 0)\n";
   10468           0 :                     ss << "            continue;\n";
   10469           0 :                     ss << "        if(isNan(arg) && ";
   10470           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10471           0 :                     ss << " != 0)\n";
   10472           0 :                     ss << "            arg = 0.0;\n";
   10473             : #endif
   10474           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10475           0 :                     ss << "    }\n";
   10476             : 
   10477             :                 }
   10478           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   10479             :                 {
   10480           0 :                     ss << "        arg = ";
   10481           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10482           0 :                     ss << ";\n";
   10483             : #ifdef  ISNAN
   10484           0 :                     ss << "        if (isNan(arg))\n";
   10485           0 :                     ss << "            continue;\n";
   10486             : #endif
   10487           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10488           0 :                     ss << "    }\n";
   10489             : 
   10490             :                 }
   10491           0 :                 else if(isMixedDV == svDoubleVectorRefString)
   10492             :                 {
   10493           0 :                     ss << "        if (";
   10494           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10495           0 :                     ss << " == 0)\n";
   10496           0 :                     ss << "            continue;\n";
   10497           0 :                     ss << "        arg = 0.0;\n";
   10498           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10499           0 :                     ss << "    }\n";
   10500             :                 }
   10501             :                 else
   10502             :                 {
   10503           0 :                     ss << "        continue;\n";
   10504           0 :                     ss << "    }\n";
   10505             :                 }
   10506             :             }
   10507           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   10508             :             {
   10509             :                 const formula::SingleVectorRefToken* pSVR =
   10510           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   10511           0 :                 if(pSVR->GetArray().mpNumericArray
   10512           0 :                     && pSVR->GetArray().mpStringArray)
   10513           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   10514           0 :                 else if(pSVR->GetArray().mpNumericArray)
   10515           0 :                     isMixedSV = svSingleVectorRefDouble;
   10516           0 :                 else if(pSVR->GetArray().mpStringArray)
   10517           0 :                     isMixedSV = svSingleVectorRefString;
   10518             :                 else
   10519           0 :                     isMixedSV = svSingleVectorRefNULL;
   10520             : 
   10521           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   10522             :                 {
   10523             : #ifdef  ISNAN
   10524           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10525           0 :                     ss << "    {\n";
   10526             : #endif
   10527           0 :                     ss << "        arg = ";
   10528           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10529           0 :                     ss << ";\n";
   10530             : #ifdef  ISNAN
   10531           0 :                     ss << "        if (!isNan(arg))\n";
   10532             : #endif
   10533           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10534             : #ifdef ISNAN
   10535           0 :                     ss << "        if (isNan(arg) && ";
   10536           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10537           0 :                     ss << " != 0)\n";
   10538           0 :                     ss << "        {\n";
   10539           0 :                     ss << "            arg = 0.0;\n";
   10540           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10541           0 :                     ss << "        }\n";
   10542           0 :                     ss << "    }\n";
   10543             : #endif
   10544             :                 }
   10545           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   10546             :                 {
   10547             : #ifdef  ISNAN
   10548           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10549           0 :                    ss << "    {\n";
   10550             : #endif
   10551           0 :                     ss << "        arg = ";
   10552           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10553             : #ifdef  ISNAN
   10554           0 :                     ss << "        if (!isNan(arg))\n";
   10555           0 :                     ss << "        {\n";
   10556             : #endif
   10557           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10558             : #ifdef ISNAN
   10559           0 :                     ss << "        }\n";
   10560           0 :                    ss << "    }\n";
   10561             : #endif
   10562             :                 }
   10563           0 :                 else if(isMixedSV == svSingleVectorRefString)
   10564             :                 {
   10565           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10566           0 :                     ss << "    {\n";
   10567           0 :                     ss << "        if (";
   10568           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10569           0 :                     ss << " != 0)\n";
   10570           0 :                     ss << "        {\n";
   10571           0 :                     ss << "            arg = 0.0;\n";
   10572           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10573           0 :                     ss << "        }\n";
   10574           0 :                     ss << "    }\n";
   10575             :                 }
   10576             :                 else
   10577             :                 {
   10578           0 :                     ss << "    arg = 0.0;\n";
   10579             :                 }
   10580             :             }
   10581             :             else
   10582             :             {
   10583           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   10584           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   10585             :             }
   10586             :         }
   10587             :         else
   10588             :         {
   10589           0 :             ss << "    arg = ";
   10590           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10591           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   10592             :         }
   10593             :     }
   10594           0 :     ss << "    if (fCount == 0.0)\n";
   10595           0 :     ss << "        return DBL_MAX;\n";
   10596           0 :     ss << "    else\n";
   10597           0 :     ss << "        return vSum * pow(fCount,-1.0);\n";
   10598           0 :     ss << "}\n";
   10599           0 : }
   10600           0 : void OpStDevA::GenSlidingWindowFunction(std::stringstream &ss,
   10601             :             const std::string &sSymName, SubArguments &vSubArguments)
   10602             : {
   10603           0 :     int isMixedDV = 0;
   10604           0 :     int isMixedSV = 0;
   10605           0 :     ss << "\ndouble " << sSymName;
   10606           0 :     ss << "_" << BinFuncName() << "(";
   10607           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   10608             :     {
   10609           0 :         if (i)
   10610           0 :             ss << ",";
   10611           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
   10612             :     }
   10613           0 :     ss << "){\n";
   10614           0 :     ss << "    int gid0 = get_global_id(0);\n";
   10615           0 :     ss << "    double fSum = 0.0;\n";
   10616           0 :     ss << "    double fMean = 0.0;\n";
   10617           0 :     ss << "    double vSum = 0.0;\n";
   10618           0 :     ss << "    double fCount = 0.0;\n";
   10619           0 :     ss << "    double arg = 0.0;\n";
   10620           0 :     unsigned i = vSubArguments.size();
   10621           0 :     while (i--)
   10622             :     {
   10623           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   10624             :         assert(pCur);
   10625             : 
   10626           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
   10627             :         {
   10628           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   10629             :             {
   10630             :                 const formula::DoubleVectorRefToken* pDVR =
   10631           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   10632           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   10633           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   10634           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   10635           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   10636           0 :                     isMixedDV = svDoubleVectorRefDouble;
   10637           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   10638           0 :                     isMixedDV = svDoubleVectorRefString;
   10639             :                 else
   10640           0 :                     isMixedDV = svDoubleVectorRefNULL;
   10641             : 
   10642           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   10643           0 :                 ss << "    for (int i = ";
   10644           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   10645             :                 {
   10646             : #ifdef  ISNAN
   10647           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   10648           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   10649           0 :                     ss << "    {\n";
   10650             : #else
   10651             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   10652             :                     ss << "    {\n";
   10653             : #endif
   10654             :                 }
   10655           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10656             :                 {
   10657             : #ifdef  ISNAN
   10658           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   10659           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   10660           0 :                     ss << "    {\n";
   10661             : #else
   10662             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   10663             :                     ss << "    {\n";
   10664             : #endif
   10665             :                 }
   10666           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10667             :                 {
   10668             : #ifdef  ISNAN
   10669           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   10670           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   10671           0 :                     ss << "    {\n";
   10672             : #else
   10673             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   10674             :                     ss << "    {\n";
   10675             : #endif
   10676             :                 }
   10677             :                 else
   10678             :                 {
   10679             : #ifdef  ISNAN
   10680           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10681           0 :                     ss << "    {\n";
   10682             : #else
   10683             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10684             :                     ss << "    {\n";
   10685             : #endif
   10686             :                 }
   10687             : 
   10688           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   10689             :                 {
   10690           0 :                     ss << "        arg = ";
   10691           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10692           0 :                     ss << ";\n";
   10693             : #ifdef  ISNAN
   10694           0 :                     ss << "        if (isNan(arg) && ";
   10695           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10696           0 :                     ss << " == 0)\n";
   10697           0 :                     ss << "            continue;\n";
   10698           0 :                     ss << "        if(isNan(arg) && ";
   10699           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10700           0 :                     ss << " != 0)\n";
   10701           0 :                     ss << "        {\n";
   10702           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10703           0 :                     ss << "            continue;\n";
   10704           0 :                     ss << "        }\n";
   10705             : #endif
   10706           0 :                     ss << "        fSum += arg;\n";
   10707           0 :                     ss << "        fCount = fCount + 1.0;\n";
   10708           0 :                     ss << "    }\n";
   10709             :                 }
   10710           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   10711             :                 {
   10712           0 :                     ss << "        arg = ";
   10713           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10714           0 :                     ss << ";\n";
   10715             : #ifdef  ISNAN
   10716           0 :                     ss << "        if (isNan(arg))\n";
   10717           0 :                     ss << "            continue;\n";
   10718             : #endif
   10719           0 :                     ss << "        fSum += arg;\n";
   10720           0 :                     ss << "        fCount = fCount + 1.0;\n";
   10721           0 :                     ss << "    }\n";
   10722             :                 }
   10723           0 :                 else if(isMixedDV == svDoubleVectorRefString)
   10724             :                 {
   10725           0 :                     ss << "        if (";
   10726           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10727           0 :                     ss << " == 0)\n";
   10728           0 :                     ss << "            continue;\n";
   10729           0 :                     ss << "        fCount = fCount + 1.0;\n";
   10730           0 :                     ss << "    }\n";
   10731             :                 }
   10732             :                 else
   10733             :                 {
   10734           0 :                     ss << "        continue;\n";
   10735           0 :                     ss << "    }\n";
   10736             :                 }
   10737             : 
   10738             :             }
   10739           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   10740             :             {
   10741             :                 const formula::SingleVectorRefToken* pSVR =
   10742           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   10743           0 :                 if(pSVR->GetArray().mpNumericArray
   10744           0 :                     && pSVR->GetArray().mpStringArray)
   10745           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   10746           0 :                 else if(pSVR->GetArray().mpNumericArray)
   10747           0 :                     isMixedSV = svSingleVectorRefDouble;
   10748           0 :                 else if(pSVR->GetArray().mpStringArray)
   10749           0 :                     isMixedSV = svSingleVectorRefString;
   10750             :                 else
   10751           0 :                     isMixedSV = svSingleVectorRefNULL;
   10752             : 
   10753           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   10754             :                 {
   10755             : #ifdef  ISNAN
   10756           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10757           0 :                     ss << "    {\n";
   10758             : #endif
   10759           0 :                     ss << "        arg = ";
   10760           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10761           0 :                     ss << ";\n";
   10762             : #ifdef  ISNAN
   10763           0 :                     ss << "        if (!isNan(arg))\n";
   10764           0 :                     ss << "        {\n";
   10765             : #endif
   10766           0 :                     ss << "            fSum += arg;\n";
   10767           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10768             : #ifdef ISNAN
   10769           0 :                     ss << "        }\n";
   10770           0 :                     ss << "        if (isNan(arg) && ";
   10771           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10772           0 :                     ss << " != 0)\n";
   10773           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10774           0 :                     ss << "    }\n";
   10775             : #endif
   10776             :                 }
   10777           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   10778             :                 {
   10779             : #ifdef  ISNAN
   10780           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10781           0 :                     ss << "    {\n";
   10782             : #endif
   10783           0 :                     ss << "        arg = ";
   10784           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10785             : #ifdef ISNAN
   10786           0 :                     ss << "        if (!isNan(arg))\n";
   10787           0 :                     ss << "        {\n";
   10788             : #endif
   10789           0 :                     ss << "            fSum += arg;\n";
   10790           0 :                     ss << "            fCount += 1.0;\n";
   10791             : #ifdef ISNAN
   10792           0 :                     ss << "        }\n";
   10793           0 :                     ss << "    }\n";
   10794             : #endif
   10795             :                 }
   10796           0 :                 else if(isMixedSV == svSingleVectorRefString)
   10797             :                 {
   10798             : 
   10799           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10800           0 :                     ss << "    {\n";
   10801           0 :                     ss << "        if (";
   10802           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10803           0 :                     ss << " != 0)\n";
   10804           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10805           0 :                     ss << "    }\n";
   10806             :                 }
   10807             :                 else
   10808             :                 {
   10809           0 :                     ss << "    arg =0.0;\n";
   10810             :                 }
   10811             :             }
   10812             :             else
   10813             :             {
   10814           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   10815           0 :                 ss << "    fSum += arg;\n";
   10816           0 :                 ss << "    fCount = fCount + 1.0;\n";
   10817             :             }
   10818             :         }
   10819             :         else
   10820             :         {
   10821           0 :             ss << "    arg = ";
   10822           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10823           0 :             ss << "    fSum += arg;\n";
   10824           0 :             ss << "    fCount = fCount + 1.0;\n";
   10825             :         }
   10826           0 :         if (i == 0)
   10827             :         {
   10828           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
   10829             :         }
   10830             :     }
   10831           0 :     i = vSubArguments.size();
   10832           0 :     while (i--)
   10833             :     {
   10834           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   10835             :         assert(pCur);
   10836             : 
   10837           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
   10838             :         {
   10839           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   10840             :             {
   10841             :                 const formula::DoubleVectorRefToken* pDVR =
   10842           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   10843           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   10844           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   10845           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   10846           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   10847           0 :                     isMixedDV = svDoubleVectorRefDouble;
   10848           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   10849           0 :                     isMixedDV = svDoubleVectorRefString;
   10850             :                 else
   10851           0 :                     isMixedDV = svDoubleVectorRefNULL;
   10852             : 
   10853           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   10854           0 :                 ss << "    for (int i = ";
   10855           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   10856             :                 {
   10857             : #ifdef  ISNAN
   10858           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   10859           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   10860           0 :                     ss << "    {\n";
   10861             : #else
   10862             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   10863             :                     ss << "    {\n";
   10864             : #endif
   10865             :                 }
   10866           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10867             :                 {
   10868             : #ifdef  ISNAN
   10869           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   10870           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   10871           0 :                     ss << "    {\n";
   10872             : #else
   10873             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   10874             :                     ss << "    {\n";
   10875             : #endif
   10876             :                 }
   10877           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10878             :                 {
   10879             : #ifdef  ISNAN
   10880           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   10881           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   10882           0 :                     ss << "    {\n";
   10883             : #else
   10884             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   10885             :                     ss << "    {\n";
   10886             : #endif
   10887             :                 }
   10888             :                 else
   10889             :                 {
   10890             : #ifdef  ISNAN
   10891           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10892           0 :                     ss << "    {\n";
   10893             : #else
   10894             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10895             :                     ss << "    {\n";
   10896             : #endif
   10897             :                 }
   10898           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   10899             :                 {
   10900           0 :                     ss << "        arg = ";
   10901           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10902           0 :                     ss << ";\n";
   10903             : #ifdef  ISNAN
   10904           0 :                     ss << "        if (isNan(arg) && ";
   10905           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10906           0 :                     ss << " == 0)\n";
   10907           0 :                     ss << "            continue;\n";
   10908           0 :                     ss << "        if(isNan(arg) && ";
   10909           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10910           0 :                     ss << " != 0)\n";
   10911           0 :                     ss << "            arg = 0.0;\n";
   10912             : #endif
   10913           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10914           0 :                     ss << "    }\n";
   10915             : 
   10916             :                 }
   10917           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   10918             :                 {
   10919           0 :                     ss << "        arg = ";
   10920           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10921           0 :                     ss << ";\n";
   10922             : #ifdef  ISNAN
   10923           0 :                     ss << "        if (isNan(arg))\n";
   10924           0 :                     ss << "            continue;\n";
   10925             : #endif
   10926           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10927           0 :                     ss << "    }\n";
   10928             : 
   10929             :                 }
   10930           0 :                 else if(isMixedDV == svDoubleVectorRefString)
   10931             :                 {
   10932           0 :                     ss << "        if (";
   10933           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10934           0 :                     ss << " == 0)\n";
   10935           0 :                     ss << "            continue;\n";
   10936           0 :                     ss << "        arg = 0.0;\n";
   10937           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10938           0 :                     ss << "    }\n";
   10939             :                 }
   10940             :                 else
   10941             :                 {
   10942           0 :                     ss << "        continue;\n";
   10943           0 :                     ss << "    }\n";
   10944             :                 }
   10945             :             }
   10946           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   10947             :             {
   10948             :                 const formula::SingleVectorRefToken* pSVR =
   10949           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   10950           0 :                 if(pSVR->GetArray().mpNumericArray
   10951           0 :                     && pSVR->GetArray().mpStringArray)
   10952           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   10953           0 :                 else if(pSVR->GetArray().mpNumericArray)
   10954           0 :                     isMixedSV = svSingleVectorRefDouble;
   10955           0 :                 else if(pSVR->GetArray().mpStringArray)
   10956           0 :                     isMixedSV = svSingleVectorRefString;
   10957             :                 else
   10958           0 :                     isMixedSV = svSingleVectorRefNULL;
   10959             : 
   10960           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   10961             :                 {
   10962             : #ifdef  ISNAN
   10963           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10964           0 :                     ss << "    {\n";
   10965             : #endif
   10966           0 :                     ss << "        arg = ";
   10967           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10968           0 :                     ss << ";\n";
   10969             : #ifdef  ISNAN
   10970           0 :                     ss << "        if (!isNan(arg))\n";
   10971             : #endif
   10972           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10973             : #ifdef ISNAN
   10974           0 :                     ss << "        if (isNan(arg) && ";
   10975           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10976           0 :                     ss << " != 0)\n";
   10977           0 :                     ss << "        {\n";
   10978           0 :                     ss << "            arg = 0.0;\n";
   10979           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10980           0 :                     ss << "        }\n";
   10981           0 :                     ss << "    }\n";
   10982             : #endif
   10983             :                 }
   10984           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   10985             :                 {
   10986             : #ifdef  ISNAN
   10987           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10988           0 :                    ss << "    {\n";
   10989             : #endif
   10990           0 :                     ss << "        arg = ";
   10991           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10992             : #ifdef  ISNAN
   10993           0 :                     ss << "        if (!isNan(arg))\n";
   10994           0 :                     ss << "        {\n";
   10995             : #endif
   10996           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10997             : #ifdef ISNAN
   10998           0 :                     ss << "        }\n";
   10999           0 :                    ss << "    }\n";
   11000             : #endif
   11001             :                 }
   11002           0 :                 else if(isMixedSV == svSingleVectorRefString)
   11003             :                 {
   11004           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11005           0 :                     ss << "    {\n";
   11006           0 :                     ss << "        if (";
   11007           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11008           0 :                     ss << " != 0)\n";
   11009           0 :                     ss << "        {\n";
   11010           0 :                     ss << "            arg = 0.0;\n";
   11011           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   11012           0 :                     ss << "        }\n";
   11013           0 :                     ss << "    }\n";
   11014             :                 }
   11015             :                 else
   11016             :                 {
   11017           0 :                     ss << "    arg = 0.0;\n";
   11018             :                 }
   11019             :             }
   11020             :             else
   11021             :             {
   11022           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   11023           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   11024             :             }
   11025             :         }
   11026             :         else
   11027             :         {
   11028           0 :             ss << "    arg = ";
   11029           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   11030           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   11031             :         }
   11032             :     }
   11033           0 :     ss << "    if (fCount <= 1.0)\n";
   11034           0 :     ss << "        return DBL_MAX;\n";
   11035           0 :     ss << "    else\n";
   11036           0 :     ss << "        return sqrt(vSum * pow(fCount - 1.0,-1.0));\n";
   11037           0 :     ss << "}\n";
   11038           0 : }
   11039             : 
   11040           0 : void OpStDevPA::GenSlidingWindowFunction(std::stringstream &ss,
   11041             :             const std::string &sSymName, SubArguments &vSubArguments)
   11042             : {
   11043           0 :     int isMixedDV = 0;
   11044           0 :     int isMixedSV = 0;
   11045           0 :     ss << "\ndouble " << sSymName;
   11046           0 :     ss << "_" << BinFuncName() << "(";
   11047           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   11048             :     {
   11049           0 :         if (i)
   11050           0 :             ss << ",";
   11051           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
   11052             :     }
   11053           0 :     ss << "){\n";
   11054           0 :     ss << "    int gid0 = get_global_id(0);\n";
   11055           0 :     ss << "    double fSum = 0.0;\n";
   11056           0 :     ss << "    double fMean = 0.0;\n";
   11057           0 :     ss << "    double vSum = 0.0;\n";
   11058           0 :     ss << "    double fCount = 0.0;\n";
   11059           0 :     ss << "    double arg = 0.0;\n";
   11060           0 :     unsigned i = vSubArguments.size();
   11061           0 :     while (i--)
   11062             :     {
   11063           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   11064             :         assert(pCur);
   11065             : 
   11066           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
   11067             :         {
   11068           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   11069             :             {
   11070             :                 const formula::DoubleVectorRefToken* pDVR =
   11071           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   11072           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   11073           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   11074           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   11075           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   11076           0 :                     isMixedDV = svDoubleVectorRefDouble;
   11077           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   11078           0 :                     isMixedDV = svDoubleVectorRefString;
   11079             :                 else
   11080           0 :                     isMixedDV = svDoubleVectorRefNULL;
   11081             : 
   11082           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   11083           0 :                 ss << "    for (int i = ";
   11084           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   11085             :                 {
   11086             : #ifdef  ISNAN
   11087           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   11088           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   11089           0 :                     ss << "    {\n";
   11090             : #else
   11091             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   11092             :                     ss << "    {\n";
   11093             : #endif
   11094             :                 }
   11095           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   11096             :                 {
   11097             : #ifdef  ISNAN
   11098           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   11099           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   11100           0 :                     ss << "    {\n";
   11101             : #else
   11102             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   11103             :                     ss << "    {\n";
   11104             : #endif
   11105             :                 }
   11106           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   11107             :                 {
   11108             : #ifdef  ISNAN
   11109           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   11110           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   11111           0 :                     ss << "    {\n";
   11112             : #else
   11113             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   11114             :                     ss << "    {\n";
   11115             : #endif
   11116             :                 }
   11117             :                 else
   11118             :                 {
   11119             : #ifdef  ISNAN
   11120           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   11121           0 :                     ss << "    {\n";
   11122             : #else
   11123             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   11124             :                     ss << "    {\n";
   11125             : #endif
   11126             :                 }
   11127             : 
   11128           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   11129             :                 {
   11130           0 :                     ss << "        arg = ";
   11131           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   11132           0 :                     ss << ";\n";
   11133             : #ifdef  ISNAN
   11134           0 :                     ss << "        if (isNan(arg) && ";
   11135           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11136           0 :                     ss << " == 0)\n";
   11137           0 :                     ss << "            continue;\n";
   11138           0 :                     ss << "        if(isNan(arg) && ";
   11139           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11140           0 :                     ss << " != 0)\n";
   11141           0 :                     ss << "        {\n";
   11142           0 :                     ss << "            fCount = fCount + 1.0;\n";
   11143           0 :                     ss << "            continue;\n";
   11144           0 :                     ss << "        }\n";
   11145             : #endif
   11146           0 :                     ss << "        fSum += arg;\n";
   11147           0 :                     ss << "        fCount = fCount + 1.0;\n";
   11148           0 :                     ss << "    }\n";
   11149             :                 }
   11150           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   11151             :                 {
   11152           0 :                     ss << "        arg = ";
   11153           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11154           0 :                     ss << ";\n";
   11155             : #ifdef  ISNAN
   11156           0 :                     ss << "        if (isNan(arg))\n";
   11157           0 :                     ss << "            continue;\n";
   11158             : #endif
   11159           0 :                     ss << "        fSum += arg;\n";
   11160           0 :                     ss << "        fCount = fCount + 1.0;\n";
   11161           0 :                     ss << "    }\n";
   11162             :                 }
   11163           0 :                 else if(isMixedDV == svDoubleVectorRefString)
   11164             :                 {
   11165           0 :                     ss << "        if (";
   11166           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11167           0 :                     ss << " == 0)\n";
   11168           0 :                     ss << "            continue;\n";
   11169           0 :                     ss << "        fCount = fCount + 1.0;\n";
   11170           0 :                     ss << "    }\n";
   11171             :                 }
   11172             :                 else
   11173             :                 {
   11174           0 :                     ss << "        continue;\n";
   11175           0 :                     ss << "    }\n";
   11176             :                 }
   11177             : 
   11178             :             }
   11179           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   11180             :             {
   11181             :                 const formula::SingleVectorRefToken* pSVR =
   11182           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   11183           0 :                 if(pSVR->GetArray().mpNumericArray
   11184           0 :                     && pSVR->GetArray().mpStringArray)
   11185           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   11186           0 :                 else if(pSVR->GetArray().mpNumericArray)
   11187           0 :                     isMixedSV = svSingleVectorRefDouble;
   11188           0 :                 else if(pSVR->GetArray().mpStringArray)
   11189           0 :                     isMixedSV = svSingleVectorRefString;
   11190             :                 else
   11191           0 :                     isMixedSV = svSingleVectorRefNULL;
   11192             : 
   11193           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   11194             :                 {
   11195             : #ifdef  ISNAN
   11196           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11197           0 :                     ss << "    {\n";
   11198             : #endif
   11199           0 :                     ss << "        arg = ";
   11200           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   11201           0 :                     ss << ";\n";
   11202             : #ifdef  ISNAN
   11203           0 :                     ss << "        if (!isNan(arg))\n";
   11204           0 :                     ss << "        {\n";
   11205             : #endif
   11206           0 :                     ss << "            fSum += arg;\n";
   11207           0 :                     ss << "            fCount = fCount + 1.0;\n";
   11208             : #ifdef ISNAN
   11209           0 :                     ss << "        }\n";
   11210           0 :                     ss << "        if (isNan(arg) && ";
   11211           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11212           0 :                     ss << " != 0)\n";
   11213           0 :                     ss << "            fCount = fCount + 1.0;\n";
   11214           0 :                     ss << "    }\n";
   11215             : #endif
   11216             :                 }
   11217           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   11218             :                 {
   11219             : #ifdef  ISNAN
   11220           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11221           0 :                     ss << "    {\n";
   11222             : #endif
   11223           0 :                     ss << "        arg = ";
   11224           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   11225             : #ifdef ISNAN
   11226           0 :                     ss << "        if (!isNan(arg))\n";
   11227           0 :                     ss << "        {\n";
   11228             : #endif
   11229           0 :                     ss << "            fSum += arg;\n";
   11230           0 :                     ss << "            fCount += 1.0;\n";
   11231             : #ifdef ISNAN
   11232           0 :                     ss << "        }\n";
   11233           0 :                     ss << "    }\n";
   11234             : #endif
   11235             :                 }
   11236           0 :                 else if(isMixedSV == svSingleVectorRefString)
   11237             :                 {
   11238             : 
   11239           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11240           0 :                     ss << "    {\n";
   11241           0 :                     ss << "        if (";
   11242           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11243           0 :                     ss << " != 0)\n";
   11244           0 :                     ss << "            fCount = fCount + 1.0;\n";
   11245           0 :                     ss << "    }\n";
   11246             :                 }
   11247             :                 else
   11248             :                 {
   11249           0 :                     ss << "    arg =0.0;\n";
   11250             :                 }
   11251             :             }
   11252             :             else
   11253             :             {
   11254           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   11255           0 :                 ss << "    fSum += arg;\n";
   11256           0 :                 ss << "    fCount = fCount + 1.0;\n";
   11257             :             }
   11258             :         }
   11259             :         else
   11260             :         {
   11261           0 :             ss << "    arg = ";
   11262           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   11263           0 :             ss << "    fSum += arg;\n";
   11264           0 :             ss << "    fCount = fCount + 1.0;\n";
   11265             :         }
   11266           0 :         if (i == 0)
   11267             :         {
   11268           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
   11269             :         }
   11270             :     }
   11271           0 :     i = vSubArguments.size();
   11272           0 :     while (i--)
   11273             :     {
   11274           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   11275             :         assert(pCur);
   11276             : 
   11277           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
   11278             :         {
   11279           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   11280             :             {
   11281             :                 const formula::DoubleVectorRefToken* pDVR =
   11282           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   11283           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   11284           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   11285           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   11286           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   11287           0 :                     isMixedDV = svDoubleVectorRefDouble;
   11288           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   11289           0 :                     isMixedDV = svDoubleVectorRefString;
   11290             :                 else
   11291           0 :                     isMixedDV = svDoubleVectorRefNULL;
   11292             : 
   11293           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   11294           0 :                 ss << "    for (int i = ";
   11295           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   11296             :                 {
   11297             : #ifdef  ISNAN
   11298           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   11299           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   11300           0 :                     ss << "    {\n";
   11301             : #else
   11302             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   11303             :                     ss << "    {\n";
   11304             : #endif
   11305             :                 }
   11306           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   11307             :                 {
   11308             : #ifdef  ISNAN
   11309           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   11310           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   11311           0 :                     ss << "    {\n";
   11312             : #else
   11313             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   11314             :                     ss << "    {\n";
   11315             : #endif
   11316             :                 }
   11317           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   11318             :                 {
   11319             : #ifdef  ISNAN
   11320           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   11321           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   11322           0 :                     ss << "    {\n";
   11323             : #else
   11324             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   11325             :                     ss << "    {\n";
   11326             : #endif
   11327             :                 }
   11328             :                 else
   11329             :                 {
   11330             : #ifdef  ISNAN
   11331           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   11332           0 :                     ss << "    {\n";
   11333             : #else
   11334             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   11335             :                     ss << "    {\n";
   11336             : #endif
   11337             :                 }
   11338           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   11339             :                 {
   11340           0 :                     ss << "        arg = ";
   11341           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   11342           0 :                     ss << ";\n";
   11343             : #ifdef  ISNAN
   11344           0 :                     ss << "        if (isNan(arg) && ";
   11345           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11346           0 :                     ss << " == 0)\n";
   11347           0 :                     ss << "            continue;\n";
   11348           0 :                     ss << "        if(isNan(arg) && ";
   11349           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11350           0 :                     ss << " != 0)\n";
   11351           0 :                     ss << "            arg = 0.0;\n";
   11352             : #endif
   11353           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   11354           0 :                     ss << "    }\n";
   11355             : 
   11356             :                 }
   11357           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   11358             :                 {
   11359           0 :                     ss << "        arg = ";
   11360           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11361           0 :                     ss << ";\n";
   11362             : #ifdef  ISNAN
   11363           0 :                     ss << "        if (isNan(arg))\n";
   11364           0 :                     ss << "            continue;\n";
   11365             : #endif
   11366           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   11367           0 :                     ss << "    }\n";
   11368             : 
   11369             :                 }
   11370           0 :                 else if(isMixedDV == svDoubleVectorRefString)
   11371             :                 {
   11372           0 :                     ss << "        if (";
   11373           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11374           0 :                     ss << " == 0)\n";
   11375           0 :                     ss << "            continue;\n";
   11376           0 :                     ss << "        arg = 0.0;\n";
   11377           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   11378           0 :                     ss << "    }\n";
   11379             :                 }
   11380             :                 else
   11381             :                 {
   11382           0 :                     ss << "        continue;\n";
   11383           0 :                     ss << "    }\n";
   11384             :                 }
   11385             :             }
   11386           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   11387             :             {
   11388             :                 const formula::SingleVectorRefToken* pSVR =
   11389           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   11390           0 :                 if(pSVR->GetArray().mpNumericArray
   11391           0 :                     && pSVR->GetArray().mpStringArray)
   11392           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   11393           0 :                 else if(pSVR->GetArray().mpNumericArray)
   11394           0 :                     isMixedSV = svSingleVectorRefDouble;
   11395           0 :                 else if(pSVR->GetArray().mpStringArray)
   11396           0 :                     isMixedSV = svSingleVectorRefString;
   11397             :                 else
   11398           0 :                     isMixedSV = svSingleVectorRefNULL;
   11399             : 
   11400           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   11401             :                 {
   11402             : #ifdef  ISNAN
   11403           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11404           0 :                     ss << "    {\n";
   11405             : #endif
   11406           0 :                     ss << "        arg = ";
   11407           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   11408           0 :                     ss << ";\n";
   11409             : #ifdef  ISNAN
   11410           0 :                     ss << "        if (!isNan(arg))\n";
   11411             : #endif
   11412           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   11413             : #ifdef ISNAN
   11414           0 :                     ss << "        if (isNan(arg) && ";
   11415           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11416           0 :                     ss << " != 0)\n";
   11417           0 :                     ss << "        {\n";
   11418           0 :                     ss << "            arg = 0.0;\n";
   11419           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   11420           0 :                     ss << "        }\n";
   11421           0 :                     ss << "    }\n";
   11422             : #endif
   11423             :                 }
   11424           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   11425             :                 {
   11426             : #ifdef  ISNAN
   11427           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11428           0 :                    ss << "    {\n";
   11429             : #endif
   11430           0 :                     ss << "        arg = ";
   11431           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   11432             : #ifdef  ISNAN
   11433           0 :                     ss << "        if (!isNan(arg))\n";
   11434           0 :                     ss << "        {\n";
   11435             : #endif
   11436           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   11437             : #ifdef ISNAN
   11438           0 :                     ss << "        }\n";
   11439           0 :                    ss << "    }\n";
   11440             : #endif
   11441             :                 }
   11442           0 :                 else if(isMixedSV == svSingleVectorRefString)
   11443             :                 {
   11444           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11445           0 :                     ss << "    {\n";
   11446           0 :                     ss << "        if (";
   11447           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11448           0 :                     ss << " != 0)\n";
   11449           0 :                     ss << "        {\n";
   11450           0 :                     ss << "            arg = 0.0;\n";
   11451           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   11452           0 :                     ss << "        }\n";
   11453           0 :                     ss << "    }\n";
   11454             :                 }
   11455             :                 else
   11456             :                 {
   11457           0 :                     ss << "    arg = 0.0;\n";
   11458             :                 }
   11459             :             }
   11460             :             else
   11461             :             {
   11462           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   11463           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   11464             :             }
   11465             :         }
   11466             :         else
   11467             :         {
   11468           0 :             ss << "    arg = ";
   11469           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   11470           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   11471             :         }
   11472             :     }
   11473           0 :     ss << "    if (fCount == 1.0)\n";
   11474           0 :     ss << "        return DBL_MAX;\n";
   11475           0 :     ss << "    else\n";
   11476           0 :     ss << "        return sqrt(vSum * pow(fCount,-1.0));\n";
   11477           0 :     ss << "}\n";
   11478           0 : }
   11479             : 
   11480           0 : void OpAveDev:: GenSlidingWindowFunction(std::stringstream &ss,
   11481             :             const std::string &sSymName, SubArguments &vSubArguments)
   11482             : {
   11483           0 :     ss << "\ndouble " << sSymName;
   11484           0 :     ss << "_"<< BinFuncName() <<"( ";
   11485           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   11486             :     {
   11487           0 :         if (i)
   11488           0 :             ss << ",";
   11489           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
   11490             :     }
   11491           0 :     ss << ")\n";
   11492           0 :     ss <<"{\n";
   11493           0 :     ss << "    int gid0 = get_global_id(0);\n";
   11494           0 :     ss << "    double sum=0.0;\n";
   11495           0 :     ss << "    double length;\n";
   11496           0 :     ss << "    double totallength=0;\n";
   11497           0 :     ss << "    double tmp = 0;\n";
   11498           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   11499             :     {
   11500           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   11501             :         assert(pCur);
   11502           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
   11503             :         {
   11504             :             const formula::DoubleVectorRefToken* pDVR =
   11505           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
   11506           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
   11507           0 :             ss << "    length="<<nCurWindowSize;
   11508           0 :             ss << ";\n";
   11509           0 :             ss << "    for (int i = ";
   11510           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
   11511           0 :             ss << "    {\n";
   11512           0 :             ss << "        double arg"<<i<<" = ";
   11513           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11514           0 :             ss << ";\n";
   11515             : #ifdef ISNAN
   11516           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
   11517           0 :             ss << pDVR->GetArrayLength();
   11518           0 :             ss << "))\n";
   11519           0 :             ss << "        {\n";
   11520           0 :             ss << "            length-=1.0;\n";
   11521           0 :             ss << "            continue;\n";
   11522           0 :             ss << "        }\n";
   11523             : #endif
   11524           0 :             ss << "        sum +=  arg"<<i<<";\n";
   11525           0 :             ss << "    }\n";
   11526           0 :             ss << "    totallength +=length;\n";
   11527             :         }
   11528           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
   11529             :         {
   11530           0 :             ss << "    tmp = ";
   11531           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11532           0 :             ss << ";\n";
   11533             : #ifdef ISNAN
   11534           0 :             ss << "    if(!isNan(tmp))\n";
   11535           0 :             ss << "    {\n";
   11536           0 :             ss << "        sum += tmp;\n";
   11537           0 :             ss << "        totallength +=1;\n";
   11538           0 :             ss << "    }\n";
   11539             : #endif
   11540             :         }
   11541           0 :         else if (pCur->GetType() == formula::svDouble)
   11542             :         {
   11543           0 :            ss << "    tmp = ";
   11544           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11545           0 :            ss << ";\n";
   11546           0 :            ss << "    sum += tmp;\n";
   11547           0 :            ss << "    totallength +=1;\n";
   11548             :         }
   11549             :     }
   11550           0 :     ss << "    double mean = sum * pow(totallength,-1);\n";
   11551           0 :     ss << "    sum = 0.0;\n";
   11552           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   11553             :     {
   11554           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   11555             :         assert(pCur);
   11556           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
   11557             :         {
   11558             :             const formula::DoubleVectorRefToken* pDVR =
   11559           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
   11560           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
   11561           0 :             ss << "    for (int i = ";
   11562           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
   11563           0 :             ss << "    {\n";
   11564           0 :             ss << "        double arg"<<i<<" = ";
   11565           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11566           0 :             ss << ";\n";
   11567             : #ifdef ISNAN
   11568           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
   11569           0 :             ss << pDVR->GetArrayLength();
   11570           0 :             ss << "))\n";
   11571           0 :             ss << "        {\n";
   11572           0 :             ss << "            continue;\n";
   11573           0 :             ss << "        }\n";
   11574             : #endif
   11575           0 :             ss << "        sum +=  fabs(arg"<<i<<"-mean);\n";
   11576           0 :             ss << "    }\n";
   11577             :         }
   11578           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
   11579             :         {
   11580           0 :             ss << "    tmp = ";
   11581           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11582           0 :             ss << ";\n";
   11583             : #ifdef ISNAN
   11584           0 :             ss << "    if(!isNan(tmp))\n";
   11585           0 :             ss << "    {\n";
   11586           0 :             ss << "        sum += fabs(tmp-mean);\n";
   11587           0 :             ss << "    }\n";
   11588             : #endif
   11589             :         }
   11590           0 :         else if (pCur->GetType() == formula::svDouble)
   11591             :         {
   11592           0 :            ss << "    tmp = ";
   11593           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11594           0 :            ss << ";\n";
   11595           0 :            ss << "    sum += fabs(tmp-mean);\n";
   11596             :         }
   11597             :     }
   11598           0 :     ss << "    tmp=sum*pow(totallength,-1);\n";
   11599           0 :     ss << "    return tmp;\n";
   11600           0 :     ss << "}";
   11601           0 : }
   11602         228 : }}
   11603             : 
   11604             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10