LCOV - code coverage report
Current view: top level - sc/source/core/opencl - op_statistical.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 6626 0.0 %
Date: 2014-04-14 Functions: 0 91 0.0 %
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(" << pCur3->GetDouble() << ");\n";
     688             :             }
     689             :             else
     690             :             {
     691           0 :                 ss << "    return DBL_MAX;\n";
     692           0 :                 ss << "}\n";
     693           0 :                 return ;
     694             :             }
     695             :         }
     696             :         else
     697             :         {
     698           0 :             ss << "    type=floor(";
     699           0 :             ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ");\n";
     700             :         }
     701           0 :         ss << "    if(!(type == 1.0||type == 2.0||type == 3.0))\n";
     702           0 :         ss << "        return DBL_MAX;\n";
     703             : 
     704           0 :         if(pCur->GetType() == formula::svDoubleVectorRef &&
     705           0 :                pCur1->GetType() == formula::svDoubleVectorRef)
     706             :         {
     707             :             const formula::DoubleVectorRefToken* pDVR =
     708           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
     709             :             const formula::DoubleVectorRefToken* pDVR1 =
     710           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur1);
     711             : 
     712           0 :             size_t nCurWindowSize  = pDVR->GetRefRowSize();
     713           0 :             size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
     714             : 
     715           0 :             if(nCurWindowSize == nCurWindowSize1)
     716             :             {
     717           0 :                 ss << "    if(type == 1.0)\n";
     718           0 :                 ss << "    {\n";
     719           0 :                 ss << "        for (int i = ";
     720           0 :                 if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed()) &&
     721           0 :                          (!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
     722             :                 {
     723             : #ifdef  ISNAN
     724           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
     725           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
     726           0 :                     ss << "        {\n";
     727             : #else
     728             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
     729             :                     ss << "        {\n";
     730             : #endif
     731             :                 }
     732           0 :                 else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed()) &&
     733           0 :                              (pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
     734             :                 {
     735             : #ifdef  ISNAN
     736           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
     737           0 :                     ss << " && i < gid0+"<< nCurWindowSize << "; i++)\n";
     738           0 :                     ss << "        {\n";
     739             : #else
     740             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
     741             :                     ss << "        {\n";
     742             : #endif
     743             :                 }
     744           0 :                 else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed()) &&
     745           0 :                              (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
     746             :                 {
     747             : #ifdef  ISNAN
     748           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
     749           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
     750           0 :                     ss << "        {\n";
     751             : #else
     752             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
     753             :                     ss << "        {\n";
     754             : #endif
     755             :                 }
     756           0 :                 else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed()) &&
     757           0 :                              (pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
     758             :                 {
     759             : #ifdef  ISNAN
     760           0 :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
     761           0 :                     ss << "        {\n";
     762             : #else
     763             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
     764             :                     ss << "        {\n";
     765             : #endif
     766             :                 }
     767             :                 else
     768             :                 {
     769             : #ifdef  ISNAN
     770           0 :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
     771           0 :                     ss << "        {\n";
     772             : #else
     773             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
     774             :                     ss << "        {\n";
     775             : #endif
     776           0 :                     ss << "            break;\n";
     777           0 :                     ss << "        }";
     778           0 :                     ss << "        return DBL_MAX;\n";
     779           0 :                     ss << "    }\n";
     780           0 :                     ss << "}\n";
     781           0 :                     return ;
     782             :                 }
     783             : 
     784           0 :                 ss << "            arg1 = ";
     785           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
     786           0 :                 ss << "            arg2 = ";
     787           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
     788             : #ifdef  ISNAN
     789           0 :                 ss << "            if (isNan(arg1)||isNan(arg2))\n";
     790           0 :                 ss << "                continue;\n";
     791             : #endif
     792           0 :                 ss << "            fSum1 += arg1;\n";
     793           0 :                 ss << "            fSum2 += arg2;\n";
     794           0 :                 ss << "            fSumSqr1 += (arg1 - arg2)*(arg1 - arg2);\n";
     795           0 :                 ss << "            fCount1 += 1;\n";
     796           0 :                 ss << "        }\n";
     797           0 :                 ss << "        if(fCount1 < 1.0)\n";
     798           0 :                 ss << "            return DBL_MAX;\n";
     799           0 :                 ss << "        fT = sqrt(fCount1-1.0) * fabs(fSum1 - fSum2)\n";
     800           0 :                 ss << "            /sqrt(fCount1 * fSumSqr1 - (fSum1-fSum2)\n";
     801           0 :                 ss << "             *(fSum1-fSum2));\n";
     802           0 :                 ss << "        fF = fCount1 - 1.0;\n";
     803             :             }
     804             :             else
     805             :             {
     806           0 :                 ss << "    return DBL_MAX;\n";
     807           0 :                 ss << "}\n";
     808           0 :                 return ;
     809             :             }
     810             :         }
     811             :         else
     812             :         {
     813           0 :             ss << "    return DBL_MAX;\n";
     814           0 :             ss << "}\n";
     815           0 :             return ;
     816             :         }
     817           0 :         ss << "    }\n";
     818           0 :         ss << "    if(type == 2.0 || type == 3.0)\n";
     819           0 :         ss << "    {\n";
     820             : 
     821           0 :         if(pCur->GetType() == formula::svDoubleVectorRef &&
     822           0 :                pCur1->GetType() == formula::svDoubleVectorRef)
     823             :         {
     824             :             const formula::DoubleVectorRefToken* pDVR =
     825           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
     826             :             const formula::DoubleVectorRefToken* pDVR1 =
     827           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur1);
     828             : 
     829           0 :             size_t nCurWindowSize  = pDVR->GetRefRowSize();
     830           0 :             size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
     831           0 :             ss << "        for (int i = ";
     832           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
     833             :             {
     834             : #ifdef  ISNAN
     835           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
     836           0 :                 ss << " && i < " << nCurWindowSize  << "; i++)\n";
     837           0 :                 ss << "        {\n";
     838             : #else
     839             :                 ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
     840             :                 ss << "        {\n";
     841             : #endif
     842             :             }
     843           0 :             else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
     844             :             {
     845             : #ifdef  ISNAN
     846           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
     847           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++)\n";
     848           0 :                 ss << "        {\n";
     849             : #else
     850             :                 ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
     851             :                 ss << "        {\n";
     852             : #endif
     853             :             }
     854           0 :             else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
     855             :             {
     856             : #ifdef  ISNAN
     857           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
     858           0 :                 ss << " &&  i < " << nCurWindowSize << "; i++)\n";
     859           0 :                 ss << "        {\n";
     860             : #else
     861             :                 ss << "0; i < " << nCurWindowSize << "; i++)\n";
     862             :                 ss << "        {\n";
     863             : #endif
     864             :             }
     865             :             else
     866             :             {
     867             : #ifdef  ISNAN
     868           0 :                 ss << "0; i < " << nCurWindowSize << "; i++)\n";
     869           0 :                 ss << "        {\n";
     870             : #else
     871             :                 ss << "0; i < " << nCurWindowSize << "; i++)\n";
     872             :                 ss << "        {\n";
     873             : #endif
     874             :             }
     875             : 
     876           0 :             ss << "            arg1 = ";
     877           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
     878             : #ifdef  ISNAN
     879           0 :             ss << "            if (isNan(arg1))\n";
     880           0 :             ss << "                continue;\n";
     881             : #endif
     882           0 :             ss << "            fSum1 += arg1;\n";
     883           0 :             ss << "            fSumSqr1 += arg1 * arg1;\n";
     884           0 :             ss << "            fCount1 += 1;\n";
     885           0 :             ss << "        }\n";
     886             : 
     887           0 :             ss << "        for (int i = ";
     888           0 :             if (!pDVR1->IsStartFixed() && pDVR1->IsEndFixed())
     889             :             {
     890             : #ifdef  ISNAN
     891           0 :                 ss << "gid0; i < " << pDVR1->GetArrayLength();
     892           0 :                 ss << " && i < " << nCurWindowSize1  << "; i++)\n";
     893           0 :                 ss << "        {\n";
     894             : #else
     895             :                 ss << "gid0; i < " << nCurWindowSize1 << "; i++)\n";
     896             :                 ss << "        {\n";
     897             : #endif
     898             :             }
     899           0 :             else if (pDVR1->IsStartFixed() && !pDVR1->IsEndFixed())
     900             :             {
     901             : #ifdef  ISNAN
     902           0 :                 ss << "0; i < " << pDVR1->GetArrayLength();
     903           0 :                 ss << " && i < gid0+"<< nCurWindowSize1 << "; i++)\n";
     904           0 :                 ss << "        {\n";
     905             : #else
     906             :                 ss << "0; i < gid0+" << nCurWindowSize1 << "; i++)\n";
     907             :                 ss << "        {\n";
     908             : #endif
     909             :             }
     910           0 :             else if (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed())
     911             :             {
     912             : #ifdef  ISNAN
     913           0 :                 ss << "0; i + gid0 < " << pDVR1->GetArrayLength();
     914           0 :                 ss << " &&  i < " << nCurWindowSize1 << "; i++)\n";
     915           0 :                 ss << "        {\n";
     916             : #else
     917             :                 ss << "0; i < " << nCurWindowSize1 << "; i++)\n";
     918             :                 ss << "        {\n";
     919             : #endif
     920             :             }
     921             :             else
     922             :             {
     923             : #ifdef  ISNAN
     924           0 :                 ss << "0; i < " << nCurWindowSize1 << "; i++)\n";
     925           0 :                 ss << "        {\n";
     926             : #else
     927             :                 ss << "0; i < " << nCurWindowSize1 << "; i++)\n";
     928             :                 ss << "        {\n";
     929             : #endif
     930             :             }
     931           0 :             ss << "            arg2 = ";
     932           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
     933             : #ifdef  ISNAN
     934           0 :             ss << "            if (isNan(arg2))\n";
     935           0 :             ss << "                continue;\n";
     936             : #endif
     937           0 :             ss << "            fSum2 += arg2;\n";
     938           0 :             ss << "            fSumSqr2 += arg2 * arg2;\n";
     939           0 :             ss << "            fCount2 += 1;\n";
     940           0 :             ss << "        }\n";
     941             :         }
     942             :         else
     943             :         {
     944           0 :             ss << "        return DBL_MAX;\n";
     945           0 :             ss << "    }\n";
     946           0 :             ss << "}\n";
     947           0 :             return ;
     948             :         }
     949           0 :         ss << "        if (fCount1 < 2.0 || fCount2 < 2.0)\n";
     950           0 :         ss << "            return DBL_MAX;\n";
     951           0 :         ss << "    }\n";
     952           0 :         ss << "    if(type == 3.0)\n";
     953           0 :         ss << "    {\n";
     954           0 :         ss << "        double fS1 = (fSumSqr1-fSum1*fSum1/fCount1)\n";
     955           0 :         ss << "            /(fCount1-1.0)/fCount1;\n";
     956           0 :         ss << "        double fS2 = (fSumSqr2-fSum2*fSum2/fCount2)\n";
     957           0 :         ss << "            /(fCount2-1.0)/fCount2;\n";
     958           0 :         ss << "        if (fS1 + fS2 == 0.0)\n";
     959           0 :         ss << "            return DBL_MAX;\n";
     960           0 :         ss << "        fT = fabs(fSum1/fCount1 - fSum2/fCount2)\n";
     961           0 :         ss << "             /sqrt(fS1+fS2);\n";
     962           0 :         ss << "        double c = fS1/(fS1+fS2);\n";
     963           0 :         ss << "        fF = 1.0/(c*c/(fCount1-1.0)+(1.0-c)*(1.0-c)\n";
     964           0 :         ss << "             /(fCount2-1.0));\n";
     965           0 :         ss << "    }\n";
     966           0 :         ss << "    if(type == 2.0)\n";
     967           0 :         ss << "    {\n";
     968           0 :         ss << "        double fS1 = (fSumSqr1 - fSum1*fSum1/fCount1)\n";
     969           0 :         ss << "             /(fCount1 - 1.0);\n";
     970           0 :         ss << "        double fS2 = (fSumSqr2 - fSum2*fSum2/fCount2)\n";
     971           0 :         ss << "             /(fCount2 - 1.0);\n";
     972           0 :         ss << "        fT = fabs( fSum1/fCount1 - fSum2/fCount2 )\n";
     973           0 :         ss << "            /sqrt( (fCount1-1.0)*fS1 + (fCount2-1.0)*fS2 )\n";
     974           0 :         ss << "            *sqrt( fCount1*fCount2*(fCount1+fCount2-2)\n";
     975           0 :         ss << "            /(fCount1+fCount2) );\n";
     976           0 :         ss << "        fF = fCount1 + fCount2 - 2;\n";
     977           0 :         ss << "    }\n";
     978             : 
     979           0 :         ss << "    double tdist=GetTDist(fT, fF);\n";
     980           0 :         ss << "    if (mode==1)\n";
     981           0 :         ss << "        return tdist;\n";
     982           0 :         ss << "    else\n";
     983           0 :         ss << "        return 2.0*tdist;\n";
     984           0 :         ss << "}\n";
     985             :     }
     986             : }
     987           0 : void OpVarP::GenSlidingWindowFunction(std::stringstream &ss,
     988             :             const std::string &sSymName, SubArguments &vSubArguments)
     989             : {
     990           0 :     ss << "\ndouble " << sSymName;
     991           0 :     ss << "_" << BinFuncName() << "(";
     992           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
     993             :     {
     994           0 :         if (i)
     995           0 :             ss << ",";
     996           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
     997             :     }
     998           0 :     ss << "){\n";
     999           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1000           0 :     ss << "    double fSum = 0.0;\n";
    1001           0 :     ss << "    double fMean = 0.0;\n";
    1002           0 :     ss << "    double vSum = 0.0;\n";
    1003           0 :     ss << "    double fCount = 0.0;\n";
    1004           0 :     ss << "    double arg = 0.0;\n";
    1005           0 :     unsigned i = vSubArguments.size();
    1006           0 :     while (i--)
    1007             :     {
    1008           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    1009             :         assert(pCur);
    1010           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    1011             :         {
    1012           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    1013             :             {
    1014             :                 const formula::DoubleVectorRefToken* pDVR =
    1015           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    1016           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    1017           0 :                 ss << "    for (int i = ";
    1018           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    1019             :                 {
    1020             : #ifdef  ISNAN
    1021           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    1022           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    1023           0 :                     ss << "    {\n";
    1024             : #else
    1025             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    1026             :                     ss << "    {\n";
    1027             : #endif
    1028             :                 }
    1029           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1030             :                 {
    1031             : #ifdef  ISNAN
    1032           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    1033           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    1034           0 :                     ss << "    {\n";
    1035             : #else
    1036             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    1037             :                     ss << "    {\n";
    1038             : #endif
    1039             :                 }
    1040           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1041             :                 {
    1042             : #ifdef  ISNAN
    1043           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    1044           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    1045           0 :                     ss << "    {\n";
    1046             : #else
    1047             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    1048             :                     ss << "    {\n";
    1049             : #endif
    1050             :                 }
    1051             :                 else
    1052             :                 {
    1053             : #ifdef  ISNAN
    1054           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1055           0 :                     ss << "    {\n";
    1056             : #else
    1057             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1058             :                     ss << "    {\n";
    1059             : #endif
    1060             :                 }
    1061           0 :                 ss << "        arg = ";
    1062           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1063           0 :                 ss << ";\n";
    1064             : #ifdef  ISNAN
    1065           0 :                 ss << "        if (isNan(arg))\n";
    1066           0 :                 ss << "            continue;\n";
    1067             : #endif
    1068           0 :                 ss << "        fSum += arg;\n";
    1069           0 :                 ss << "        fCount += 1.0;\n";
    1070           0 :                 ss << "    }\n";
    1071             :             }
    1072           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    1073             :             {
    1074             :                 const formula::SingleVectorRefToken* pSVR =
    1075           0 :                     static_cast< const formula::SingleVectorRefToken*>(pCur);
    1076             : #ifdef  ISNAN
    1077           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    1078           0 :                 ss << "    {\n";
    1079             : #endif
    1080           0 :                 ss << "        arg = ";
    1081           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1082             : #ifdef  ISNAN
    1083           0 :                 ss << "        if (!isNan(arg))\n";
    1084           0 :                 ss << "        {\n";
    1085             : #endif
    1086           0 :                 ss << "            fSum += arg;\n";
    1087           0 :                 ss << "            fCount += 1.0;\n";
    1088             : #ifdef ISNAN
    1089           0 :                 ss << "        }\n";
    1090           0 :                 ss << "    }\n";
    1091             : #endif
    1092             :             }
    1093             :             else
    1094             :             {
    1095           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    1096           0 :                 ss << "    fSum += arg;\n";
    1097           0 :                 ss << "    fCount += 1.0;\n";
    1098             :             }
    1099             :         }
    1100             :         else
    1101             :         {
    1102           0 :             ss << "    arg = ";
    1103           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1104           0 :             ss << "    fSum += arg;\n";
    1105           0 :             ss << "    fCount += 1.0;\n";
    1106             :         }
    1107           0 :         if (i == 0)
    1108             :         {
    1109           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
    1110             :         }
    1111             :     }
    1112           0 :     i = vSubArguments.size();
    1113           0 :     while (i--)
    1114             :     {
    1115           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    1116             :         assert(pCur);
    1117           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    1118             :         {
    1119           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    1120             :             {
    1121             :                 const formula::DoubleVectorRefToken* pDVR =
    1122           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    1123           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    1124           0 :                 ss << "    for (int i = ";
    1125           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    1126             :                 {
    1127             : #ifdef  ISNAN
    1128           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    1129           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    1130           0 :                     ss << "    {\n";
    1131             : #else
    1132             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    1133             :                     ss << "    {\n";
    1134             : #endif
    1135             :                 }
    1136           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1137             :                 {
    1138             : #ifdef  ISNAN
    1139           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    1140           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    1141           0 :                     ss << "    {\n";
    1142             : #else
    1143             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    1144             :                     ss << "    {\n";
    1145             : #endif
    1146             :                 }
    1147           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1148             :                 {
    1149             : #ifdef  ISNAN
    1150           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    1151           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    1152           0 :                     ss << "    {\n";
    1153             : #else
    1154             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    1155             :                     ss << "    {\n";
    1156             : #endif
    1157             :                 }
    1158             :                 else
    1159             :                 {
    1160             : #ifdef  ISNAN
    1161           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1162           0 :                     ss << "    {\n";
    1163             : #else
    1164             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1165             :                     ss << "    {\n";
    1166             : #endif
    1167             :                 }
    1168             : 
    1169           0 :                 ss << "        arg = ";
    1170           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1171           0 :                 ss << ";\n";
    1172             : #ifdef  ISNAN
    1173           0 :                 ss << "        if (isNan(arg))\n";
    1174           0 :                 ss << "            continue;\n";
    1175             : #endif
    1176           0 :                 ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
    1177           0 :                 ss << "    }\n";
    1178             :             }
    1179           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    1180             :             {
    1181             :                 const formula::SingleVectorRefToken* pSVR =
    1182           0 :                     static_cast< const formula::SingleVectorRefToken*>(pCur);
    1183             : #ifdef  ISNAN
    1184           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    1185           0 :                 ss << "    {\n";
    1186             : #endif
    1187           0 :                 ss << "        arg = ";
    1188           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1189             : #ifdef  ISNAN
    1190           0 :                 ss << "        if (!isNan(arg))\n";
    1191           0 :                 ss << "        {\n";
    1192             : #endif
    1193           0 :                 ss << "            vSum += (arg - fMean) * (arg - fMean);\n";
    1194             : #ifdef ISNAN
    1195           0 :                 ss << "        }\n";
    1196           0 :                 ss << "    }\n";
    1197             : #endif
    1198             :             }
    1199             :             else
    1200             :             {
    1201           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    1202           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    1203             :             }
    1204             :         }
    1205             :         else
    1206             :         {
    1207           0 :             ss << "    arg = ";
    1208           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1209           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    1210             :         }
    1211             :     }
    1212           0 :     ss << "    if (fCount == 0.0)\n";
    1213           0 :     ss << "        return DBL_MAX;\n";
    1214           0 :     ss << "    else\n";
    1215           0 :     ss << "        return vSum * pow(fCount,-1.0);\n";
    1216           0 :     ss << "}\n";
    1217           0 : }
    1218             : 
    1219           0 : void OpTDist::BinInlineFun(std::set<std::string>& decls,
    1220             :     std::set<std::string>& funs)
    1221             : {
    1222           0 :     decls.insert(fMachEpsDecl);
    1223           0 :     funs.insert("");
    1224           0 :     decls.insert(fMaxGammaArgumentDecl);
    1225           0 :     funs.insert("");
    1226           0 :     decls.insert(lcl_getLanczosSumDecl);
    1227           0 :     funs.insert(lcl_getLanczosSum);
    1228           0 :     decls.insert(GetBetaDecl);
    1229           0 :     funs.insert(GetBeta);
    1230           0 :     decls.insert(GetLogBetaDecl);
    1231           0 :     funs.insert(GetLogBeta);
    1232           0 :     decls.insert(GetBetaDistPDFDecl);
    1233           0 :     funs.insert(GetBetaDistPDF);
    1234           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);
    1235           0 :     funs.insert(lcl_GetBetaHelperContFrac);
    1236           0 :     decls.insert(GetBetaDistDecl);
    1237           0 :     funs.insert(GetBetaDist);
    1238           0 :     decls.insert(GetTDistDecl);
    1239           0 :     funs.insert(GetTDist);
    1240           0 : }
    1241           0 : void OpTDist::GenSlidingWindowFunction(std::stringstream &ss,
    1242             :             const std::string &sSymName, SubArguments &vSubArguments)
    1243             : {
    1244           0 :     ss << "\ndouble " << sSymName;
    1245           0 :     ss << "_" << BinFuncName() << "(";
    1246           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1247             :     {
    1248           0 :         if (i)
    1249           0 :             ss << ",";
    1250           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1251             :     }
    1252           0 :     ss << ") {\n";
    1253           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1254           0 :     ss << "    double x = 0.0;\n";
    1255           0 :     ss << "    double fDF = 0.0;\n";
    1256           0 :     ss << "    double fFlag = 0.0;\n";
    1257           0 :     if(vSubArguments.size() != 3)
    1258             :     {
    1259           0 :         ss << "    return DBL_MAX;\n" << "}\n";
    1260           0 :         return ;
    1261             :     }
    1262           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    1263             :     assert(tmpCur0);
    1264           0 :     if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
    1265             :     {
    1266           0 :         if(tmpCur0->GetType() == formula::svSingleVectorRef)
    1267             :         {
    1268             :             const formula::SingleVectorRefToken*tmpCurDVR0 =
    1269           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
    1270             : #ifdef ISNAN
    1271           0 :             ss << "    if(gid0 < ";
    1272           0 :             ss << tmpCurDVR0->GetArrayLength() << ")\n";
    1273           0 :             ss << "    {\n";
    1274             : #endif
    1275           0 :             ss << "        x = ";
    1276           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1277             : #ifdef ISNAN
    1278           0 :             ss << "        if(isNan(x))\n";
    1279           0 :             ss << "            x = 0.0;\n";
    1280           0 :             ss << "    }\n";
    1281             : #endif
    1282             :         }
    1283           0 :         else if(tmpCur0->GetType() == formula::svDouble)
    1284             :         {
    1285           0 :             ss << "    x = " << tmpCur0->GetDouble() << ";\n";
    1286             :         }
    1287             :         else
    1288             :         {
    1289           0 :             ss << "    return DBL_MAX;\n" << "}\n";
    1290           0 :             return ;
    1291             :         }
    1292             :     }
    1293             :     else
    1294             :     {
    1295           0 :         ss << "    x = ";
    1296           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1297             :     }
    1298           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    1299             :     assert(tmpCur1);
    1300           0 :     if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
    1301             :     {
    1302           0 :         if(tmpCur1->GetType() == formula::svSingleVectorRef)
    1303             :         {
    1304             :             const formula::SingleVectorRefToken*tmpCurDVR1 =
    1305           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
    1306             : #ifdef ISNAN
    1307           0 :             ss << "    if(gid0 < ";
    1308           0 :             ss << tmpCurDVR1->GetArrayLength() << ")\n";
    1309           0 :             ss << "    {\n";
    1310             : #endif
    1311           0 :             ss << "        fDF = ";
    1312           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    1313             : #ifdef ISNAN
    1314           0 :             ss << "        if(isNan(fDF))\n";
    1315           0 :             ss << "            fDF = 0.0;\n";
    1316           0 :             ss << "        else\n";
    1317             : #endif
    1318           0 :             ss << "            fDF = floor(fDF);\n";
    1319             : #ifdef ISNAN
    1320           0 :             ss << "    }\n";
    1321             : #endif
    1322             :         }
    1323           0 :         else if(tmpCur1->GetType() == formula::svDouble)
    1324             :         {
    1325           0 :             ss << "    fDF = floor(convert_double(";
    1326           0 :             ss << tmpCur1->GetDouble() << "));\n";
    1327             :         }
    1328             :         else
    1329             :         {
    1330           0 :             ss << "    return DBL_MAX;\n" << "}\n";
    1331           0 :             return ;
    1332             :         }
    1333             :     }
    1334             :     else
    1335             :     {
    1336           0 :         ss << "    fDF = floor(";
    1337           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
    1338             :     }
    1339             : 
    1340           0 :     FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
    1341             :     assert(tmpCur2);
    1342           0 :     if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
    1343             :     {
    1344           0 :         if(tmpCur2->GetType() == formula::svSingleVectorRef)
    1345             :         {
    1346             :             const formula::SingleVectorRefToken*tmpCurDVR2 =
    1347           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
    1348             : #ifdef ISNAN
    1349           0 :             ss << "    if(gid0 < ";
    1350           0 :             ss << tmpCurDVR2->GetArrayLength() << ")\n";
    1351           0 :             ss << "    {\n";
    1352             : #endif
    1353           0 :             ss << "        fFlag = ";
    1354           0 :             ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
    1355             : #ifdef ISNAN
    1356           0 :             ss << "        if(isNan(fFlag))\n";
    1357           0 :             ss << "            fFlag = 0.0;\n";
    1358           0 :             ss << "        else\n";
    1359             : #endif
    1360           0 :             ss << "            fFlag = floor(fFlag);\n";
    1361             : #ifdef ISNAN
    1362           0 :             ss << "    }\n";
    1363             : #endif
    1364             : 
    1365             :         }
    1366           0 :         else if(tmpCur2->GetType() == formula::svDouble)
    1367             :         {
    1368           0 :             ss << "    fFlag = floor(convert_double(";
    1369           0 :             ss << tmpCur2->GetDouble() << "));\n";
    1370             :         }
    1371             :         else
    1372             :         {
    1373           0 :             ss << "    return DBL_MAX;\n" << "}\n";
    1374           0 :             return ;
    1375             :         }
    1376             :     }
    1377             :     else
    1378             :     {
    1379           0 :         ss << "    fFlag = floor(";
    1380           0 :         ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ");\n";
    1381             :     }
    1382           0 :     ss << "    if(fDF < 1.0 || x < 0.0 || (fFlag != 1.0 && fFlag != 2.0))\n";
    1383           0 :     ss << "        return DBL_MAX;\n";
    1384           0 :     ss << "    double R = GetTDist(x, fDF);\n";
    1385           0 :     ss << "    if (fFlag == 1.0)\n";
    1386           0 :     ss << "        return R;\n";
    1387           0 :     ss << "    else\n";
    1388           0 :     ss << "        return 2.0 * R;\n";
    1389           0 :     ss << "}\n";
    1390             : }
    1391           0 : void OpExponDist::GenSlidingWindowFunction(std::stringstream &ss,
    1392             :         const std::string &sSymName, SubArguments &vSubArguments)
    1393             : {
    1394           0 :     ss << "\ndouble " << sSymName;
    1395           0 :     ss << "_"<< BinFuncName() <<"(";
    1396           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1397             :     {
    1398           0 :         if (i)
    1399           0 :             ss << ",";
    1400           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1401             :     }
    1402           0 :     ss << ") {\n";
    1403           0 :     ss << "    double tmp = 0,tmp0=0,tmp1=0,tmp2=0;\n";
    1404           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1405           0 :     ss << "    double rx,rlambda,rkum;\n";
    1406           0 :     size_t i = vSubArguments.size();
    1407           0 :     ss <<"\n";
    1408           0 :     for (i = 0; i < vSubArguments.size(); i++)
    1409             :     {
    1410           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    1411             :         assert(pCur);
    1412           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    1413             :         {
    1414             : #ifdef  ISNAN
    1415             :             const formula::SingleVectorRefToken* pSVR =
    1416           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    1417           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    1418             : #endif
    1419             :         }
    1420           0 :         else if (pCur->GetType() == formula::svDouble)
    1421             :         {
    1422             : #ifdef  ISNAN
    1423           0 :             ss << "{\n";
    1424             : #endif
    1425             :         }
    1426             :         else
    1427             :         {
    1428             : #ifdef  ISNAN
    1429             : #endif
    1430             :         }
    1431             : #ifdef  ISNAN
    1432           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    1433             :         {
    1434           0 :             ss << "    if (isNan(";
    1435           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1436           0 :             ss << "))\n";
    1437           0 :             ss << "        tmp"<<i<<"= 0;\n";
    1438           0 :             ss << "    else\n";
    1439           0 :             ss << "        tmp"<<i<<"=\n";
    1440           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1441           0 :             ss << ";\n}\n";
    1442             :         }
    1443             :         else
    1444             :         {
    1445           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    1446           0 :             ss <<";\n";
    1447             :         }
    1448             : #endif
    1449             :     }
    1450           0 :     ss << "   rx = tmp0;\n";
    1451           0 :     ss << "   rlambda = tmp1;\n";
    1452           0 :     ss << "   rkum = tmp2;\n";
    1453           0 :     ss <<"    if(rlambda <= 0.0)\n";
    1454           0 :     ss <<"    {\n";
    1455           0 :     ss <<"        tmp = -DBL_MAX;\n";
    1456           0 :     ss <<"    }\n";
    1457           0 :     ss <<"    else if(rkum == 0)\n";
    1458           0 :     ss <<"    {\n";
    1459           0 :     ss <<"        if(rx >= 0)\n";
    1460           0 :     ss <<"            tmp = rlambda*exp(-rlambda*rx);\n";
    1461           0 :     ss <<"        else\n";
    1462           0 :     ss <<"            tmp = 0.0;\n";
    1463           0 :     ss <<"    }\n";
    1464           0 :     ss <<"    else\n";
    1465           0 :     ss <<"    {\n";
    1466           0 :     ss <<"        if(rx > 0)\n";
    1467           0 :     ss <<"            tmp = 1.0 - exp(-rlambda*rx);\n";
    1468           0 :     ss <<"        else\n";
    1469           0 :     ss <<"            tmp = 0.0;\n";
    1470           0 :     ss <<"    }\n";
    1471           0 :     ss <<"    return tmp;\n";
    1472           0 :     ss <<"}";
    1473           0 : }
    1474           0 : void OpFdist::BinInlineFun(std::set<std::string>& decls,
    1475             :     std::set<std::string>& funs)
    1476             : {
    1477           0 :     decls.insert(GetFDistDecl);decls.insert(GetBetaDistDecl);
    1478           0 :     decls.insert(GetBetaDecl);decls.insert(fMaxGammaArgumentDecl);
    1479           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);
    1480           0 :     decls.insert(GetBetaDistPDFDecl);
    1481           0 :     decls.insert(GetLogBetaDecl);decls.insert(lcl_getLanczosSumDecl);
    1482           0 :     decls.insert(fMachEpsDecl);
    1483           0 :     funs.insert(GetFDist);funs.insert(GetBetaDist);
    1484           0 :     funs.insert(GetBeta);
    1485           0 :     funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetBetaDistPDF);
    1486           0 :     funs.insert(GetLogBeta);
    1487           0 :     funs.insert(lcl_getLanczosSum);
    1488           0 : }
    1489           0 : void OpFdist::GenSlidingWindowFunction(std::stringstream &ss,
    1490             :         const std::string &sSymName, SubArguments &vSubArguments)
    1491             : {
    1492           0 :     ss << "\ndouble " << sSymName;
    1493           0 :     ss << "_"<< BinFuncName() <<"(";
    1494           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1495             :     {
    1496           0 :         if (i)
    1497           0 :             ss << ",";
    1498           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1499             :     }
    1500           0 :     ss << ") {\n";
    1501           0 :     ss << "    double tmp = 0,tmp0=0,tmp1=0,tmp2=0;\n";
    1502           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1503           0 :     ss << "    double rF1,rF2,rX;\n";
    1504           0 :     size_t i = vSubArguments.size();
    1505           0 :     ss <<"\n";
    1506           0 :     for (i = 0; i < vSubArguments.size(); i++)
    1507             :     {
    1508           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    1509             :         assert(pCur);
    1510           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    1511             :         {
    1512             : #ifdef  ISNAN
    1513             :             const formula::SingleVectorRefToken* pSVR =
    1514           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    1515           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    1516             : #endif
    1517             :         }
    1518           0 :         else if (pCur->GetType() == formula::svDouble)
    1519             :         {
    1520             : #ifdef  ISNAN
    1521           0 :             ss << "{\n";
    1522             : #endif
    1523             :         }
    1524             :         else
    1525             :         {
    1526             : #ifdef  ISNAN
    1527             : #endif
    1528             :         }
    1529             : #ifdef  ISNAN
    1530           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    1531             :         {
    1532           0 :             ss << "    if (isNan(";
    1533           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1534           0 :             ss << "))\n";
    1535           0 :             ss << "        tmp"<<i<<"= 0;\n";
    1536           0 :             ss << "    else\n";
    1537           0 :             ss << "        tmp"<<i<<"=\n";
    1538           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    1539           0 :             ss << ";\n}\n";
    1540             :         }
    1541             :         else
    1542             :         {
    1543           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    1544           0 :             ss <<";\n";
    1545             :         }
    1546             : #endif
    1547             :     }
    1548           0 :     ss << " rX  = tmp0;\n";
    1549           0 :     ss << " rF1 = floor(tmp1);\n";
    1550           0 :     ss << " rF2 = floor(tmp2);\n";
    1551           0 :     ss <<"    if (rX < 0.0 || rF1 < 1.0 || rF2 < 1.0 || rF1 >= 1.0E10 ||";
    1552           0 :     ss <<"rF2 >= 1.0E10)\n";
    1553           0 :     ss <<"    {\n";
    1554           0 :     ss <<"        tmp = -DBL_MAX;\n";
    1555           0 :     ss <<"    }\n";
    1556           0 :     ss <<"    tmp = GetFDist(rX, rF1, rF2);\n";
    1557           0 :     ss <<"    return tmp;\n";
    1558           0 :     ss <<"}";
    1559           0 : }
    1560             : 
    1561           0 : void OpStandard::GenSlidingWindowFunction(std::stringstream &ss,
    1562             :             const std::string &sSymName, SubArguments &vSubArguments)
    1563             : {
    1564           0 :     ss << "\ndouble " << sSymName;
    1565           0 :     ss << "_" << BinFuncName() << "(";
    1566           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1567             :     {
    1568           0 :         if (i)
    1569           0 :             ss << ",";
    1570           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1571             :     }
    1572           0 :     ss << ") {\n";
    1573           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1574           0 :     ss << "    double x = 0.0;\n";
    1575           0 :     ss << "    double mu = 0.0;\n";
    1576           0 :     ss << "    double sigma = 0.0;\n";
    1577           0 :     if(vSubArguments.size() != 3)
    1578             :     {
    1579           0 :         ss << "    return DBL_MAX;\n" << "}\n";
    1580           0 :         return ;
    1581             :     }
    1582           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    1583             :     assert(tmpCur0);
    1584           0 :     if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
    1585             :     {
    1586           0 :         if(tmpCur0->GetType() == formula::svSingleVectorRef)
    1587             :         {
    1588             :             const formula::SingleVectorRefToken*tmpCurSVR0 =
    1589           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
    1590             : #ifdef  ISNAN
    1591           0 :             ss << "    if (gid0 < " << tmpCurSVR0->GetArrayLength() << ")\n";
    1592           0 :             ss << "    {\n";
    1593             : #endif
    1594           0 :             ss << "        x = ";
    1595           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1596             : #ifdef  ISNAN
    1597           0 :             ss << "        if (isNan(x))\n";
    1598           0 :             ss << "            x = 0.0;\n";
    1599           0 :             ss << "    }\n";
    1600             : #endif
    1601             :         }
    1602           0 :         else if(tmpCur0->GetType() == formula::svDouble)
    1603             :         {
    1604           0 :             ss << "    x = " << tmpCur0->GetDouble() << ";\n";
    1605             :         }
    1606             :         else
    1607             :         {
    1608           0 :             ss << "    return DBL_MAX;\n" << "}\n";
    1609           0 :             return ;
    1610             :         }
    1611             :     }
    1612             :     else
    1613             :     {
    1614           0 :         ss << "    x = ";
    1615           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1616             :     }
    1617             : 
    1618           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    1619             :     assert(tmpCur1);
    1620           0 :     if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
    1621             :     {
    1622           0 :         if(tmpCur1->GetType() == formula::svSingleVectorRef)
    1623             :         {
    1624             :             const formula::SingleVectorRefToken*tmpCurSVR1 =
    1625           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
    1626             : #ifdef  ISNAN
    1627           0 :             ss << "    if (gid0 < " << tmpCurSVR1->GetArrayLength() << ")\n";
    1628           0 :             ss << "    {\n";
    1629             : #endif
    1630           0 :             ss << "        mu = ";
    1631           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    1632             : #ifdef  ISNAN
    1633           0 :             ss << "        if (isNan(mu))\n";
    1634           0 :             ss << "            mu = 0.0;\n";
    1635           0 :             ss << "    }\n";
    1636             : #endif
    1637             :         }
    1638           0 :         else if(tmpCur1->GetType() == formula::svDouble)
    1639             :         {
    1640           0 :             ss << "    mu = " << tmpCur1->GetDouble() << ";\n";
    1641             :         }
    1642             :         else
    1643             :         {
    1644           0 :             ss << "    return DBL_MAX;\n" << "}\n";
    1645           0 :             return ;
    1646             :         }
    1647             :     }
    1648             :     else
    1649             :     {
    1650           0 :         ss << "    mu = ";
    1651           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    1652             :     }
    1653             : 
    1654           0 :     FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
    1655             :     assert(tmpCur2);
    1656           0 :     if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
    1657             :     {
    1658           0 :         if(tmpCur2->GetType() == formula::svSingleVectorRef)
    1659             :         {
    1660             :             const formula::SingleVectorRefToken*tmpCurSVR2 =
    1661           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
    1662             : #ifdef  ISNAN
    1663           0 :             ss << "    if (gid0 < " << tmpCurSVR2->GetArrayLength() << ")\n";
    1664           0 :             ss << "    {\n";
    1665             : #endif
    1666           0 :             ss << "        sigma = ";
    1667           0 :             ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
    1668             : #ifdef  ISNAN
    1669           0 :             ss << "        if (isNan(sigma))\n";
    1670           0 :             ss << "            sigma = 0.0;\n";
    1671           0 :             ss << "    }\n";
    1672             : #endif
    1673             :         }
    1674           0 :         else if(tmpCur2->GetType() == formula::svDouble)
    1675             :         {
    1676           0 :             ss << "    sigma = " << tmpCur2->GetDouble() << ";\n";
    1677             :         }
    1678             :         else
    1679             :         {
    1680           0 :             ss << "    return DBL_MAX;\n";
    1681           0 :             ss << "}\n";
    1682           0 :             return ;
    1683             :         }
    1684             :     }
    1685             :     else
    1686             :     {
    1687           0 :         ss << "    sigma = ";
    1688           0 :         ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
    1689             :     }
    1690             : 
    1691           0 :     ss << "    if(sigma <= 0.0)\n";
    1692           0 :     ss << "        return DBL_MAX;\n";
    1693           0 :     ss << "    else\n";
    1694           0 :     ss << "        return (x - mu)*pow(sigma,-1.0);\n";
    1695           0 :     ss << "}";
    1696             : }
    1697             : 
    1698           0 : void OpWeibull::GenSlidingWindowFunction(std::stringstream &ss,
    1699             :             const std::string &sSymName, SubArguments &vSubArguments)
    1700             : {
    1701           0 :     ss << "\ndouble " << sSymName;
    1702           0 :     ss << "_" << BinFuncName() << "(";
    1703           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1704             :     {
    1705           0 :         if (i)
    1706           0 :             ss << ",";
    1707           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1708             :     }
    1709           0 :     ss << ") {\n";
    1710           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1711           0 :     ss << "    double x = 0.0;\n";
    1712           0 :     ss << "    double alpha = 0.0;\n";
    1713           0 :     ss << "    double beta = 0.0;\n";
    1714           0 :     ss << "    double kum = 0.0;\n";
    1715           0 :     if(vSubArguments.size() != 4)
    1716             :     {
    1717           0 :         ss << "    return DBL_MAX;\n" << "}\n";
    1718           0 :         return ;
    1719             :     }
    1720           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    1721             :     assert(tmpCur0);
    1722           0 :     if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
    1723             :     {
    1724           0 :         if(tmpCur0->GetType() == formula::svSingleVectorRef)
    1725             :         {
    1726             :             const formula::SingleVectorRefToken*tmpCurSVR0 =
    1727           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
    1728             : #ifdef  ISNAN
    1729           0 :             ss << "    if (gid0 < " << tmpCurSVR0->GetArrayLength() << ")\n";
    1730           0 :             ss << "    {\n";
    1731             : #endif
    1732           0 :             ss << "        x = ";
    1733           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1734             : #ifdef  ISNAN
    1735           0 :             ss << "        if (isNan(x))\n";
    1736           0 :             ss << "            x = 0.0;\n";
    1737           0 :             ss << "    }\n";
    1738             : #endif
    1739             :         }
    1740           0 :         else if(tmpCur0->GetType() == formula::svDouble)
    1741             :         {
    1742           0 :             ss << "    x = " << tmpCur0->GetDouble() << ";\n";
    1743             :         }
    1744             :         else
    1745             :         {
    1746           0 :             ss << "    return DBL_MAX;\n" << "}\n";
    1747           0 :             return ;
    1748             :         }
    1749             :     }
    1750             :     else
    1751             :     {
    1752           0 :         ss << "    x = ";
    1753           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    1754             :     }
    1755             : 
    1756           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    1757             :     assert(tmpCur1);
    1758           0 :     if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
    1759             :     {
    1760           0 :         if(tmpCur1->GetType() == formula::svSingleVectorRef)
    1761             :         {
    1762             :             const formula::SingleVectorRefToken*tmpCurSVR1 =
    1763           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
    1764             : #ifdef  ISNAN
    1765           0 :             ss << "    if (gid0 < " << tmpCurSVR1->GetArrayLength() << ")\n";
    1766           0 :             ss << "    {\n";
    1767             : #endif
    1768           0 :             ss << "        alpha = ";
    1769           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    1770             : #ifdef  ISNAN
    1771           0 :             ss << "        if (isNan(alpha))\n";
    1772           0 :             ss << "            alpha = 0.0;\n";
    1773           0 :             ss << "    }\n";
    1774             : #endif
    1775             :         }
    1776           0 :         else if(tmpCur1->GetType() == formula::svDouble)
    1777             :         {
    1778           0 :             ss << "    alpha = " << tmpCur1->GetDouble() << ";\n";
    1779             :         }
    1780             :         else
    1781             :         {
    1782           0 :             ss << "    return DBL_MAX;\n" << "}\n";
    1783           0 :             return ;
    1784             :         }
    1785             :     }
    1786             :     else
    1787             :     {
    1788           0 :         ss << "    alpha = ";
    1789           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    1790             :     }
    1791             : 
    1792           0 :     FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
    1793             :     assert(tmpCur2);
    1794           0 :     if(ocPush == vSubArguments[2]->GetFormulaToken()->GetOpCode())
    1795             :     {
    1796           0 :         if(tmpCur2->GetType() == formula::svSingleVectorRef)
    1797             :         {
    1798             :             const formula::SingleVectorRefToken*tmpCurSVR2 =
    1799           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur2);
    1800             : #ifdef  ISNAN
    1801           0 :             ss << "    if (gid0 < " << tmpCurSVR2->GetArrayLength() << ")\n";
    1802           0 :             ss << "    {\n";
    1803             : #endif
    1804           0 :             ss << "        beta = ";
    1805           0 :             ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
    1806             : #ifdef  ISNAN
    1807           0 :             ss << "        if (isNan(beta))\n";
    1808           0 :             ss << "            beta = 0.0;\n";
    1809           0 :             ss << "    }\n";
    1810             : #endif
    1811             :         }
    1812           0 :         else if(tmpCur2->GetType() == formula::svDouble)
    1813             :         {
    1814           0 :             ss << "    beta = " << tmpCur2->GetDouble() << ";\n";
    1815             :         }
    1816             :         else
    1817             :         {
    1818           0 :             ss << "    return DBL_MAX;\n" << "}\n";
    1819           0 :             return ;
    1820             :         }
    1821             :     }
    1822             :     else
    1823             :     {
    1824           0 :         ss << "    beta = ";
    1825           0 :         ss << vSubArguments[2]->GenSlidingWindowDeclRef() << ";\n";
    1826             :     }
    1827             : 
    1828           0 :     FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
    1829             :     assert(tmpCur3);
    1830           0 :     if(ocPush == vSubArguments[3]->GetFormulaToken()->GetOpCode())
    1831             :     {
    1832           0 :         if(tmpCur3->GetType() == formula::svSingleVectorRef)
    1833             :         {
    1834             :             const formula::SingleVectorRefToken*tmpCurSVR3 =
    1835           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur3);
    1836             : #ifdef  ISNAN
    1837           0 :             ss << "    if (gid0 < " << tmpCurSVR3->GetArrayLength() << ")\n";
    1838           0 :             ss << "    {\n";
    1839             : #endif
    1840           0 :             ss << "        kum = ";
    1841           0 :             ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ";\n";
    1842             : #ifdef  ISNAN
    1843           0 :             ss << "        if (isNan(kum))\n";
    1844           0 :             ss << "            kum = 0.0;\n";
    1845           0 :             ss << "    }\n";
    1846             : #endif
    1847             :         }
    1848           0 :         else if(tmpCur3->GetType() == formula::svDouble)
    1849             :         {
    1850           0 :             ss << "    kum = " << tmpCur3->GetDouble() << ";\n";
    1851             :         }
    1852             :         else
    1853             :         {
    1854           0 :             ss << "    return DBL_MAX;\n" << "}\n";
    1855           0 :             return ;
    1856             :         }
    1857             :     }
    1858             :     else
    1859             :     {
    1860           0 :         ss << "    kum = ";
    1861           0 :         ss << vSubArguments[3]->GenSlidingWindowDeclRef() << ";\n";
    1862             :     }
    1863             : 
    1864           0 :     ss << "    if(alpha <= 0.0 || beta <=0.0 || kum < 0.0)\n";
    1865           0 :     ss << "        return DBL_MAX;\n";
    1866           0 :     ss << "    else if(kum == 0.0)\n";
    1867           0 :     ss << "    {\n";
    1868           0 :     ss << "        return alpha*pow(pow(beta,alpha),-1.0)*pow(x,alpha-1.0)";
    1869           0 :     ss << "*exp(-pow(x*pow(beta,-1.0),alpha));\n";
    1870           0 :     ss << "    }\n";
    1871           0 :     ss << "    else\n";
    1872           0 :     ss << "        return 1.0-exp(-pow(x*pow(beta,-1.0),alpha));\n";
    1873           0 :     ss << "}\n";
    1874             : }
    1875             : 
    1876           0 : void OpSkew::GenSlidingWindowFunction(std::stringstream &ss,
    1877             :             const std::string &sSymName, SubArguments &vSubArguments)
    1878             : {
    1879           0 :     ss << "\ndouble " << sSymName;
    1880           0 :     ss << "_" << BinFuncName() << "(";
    1881           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    1882             :     {
    1883           0 :         if (i)
    1884           0 :             ss << ",";
    1885           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    1886             :     }
    1887           0 :     ss << "){\n";
    1888           0 :     ss << "    int gid0 = get_global_id(0);\n";
    1889           0 :     ss << "    double fSum = 0.0;\n";
    1890           0 :     ss << "    double fMean = 0.0;\n";
    1891           0 :     ss << "    double vSum = 0.0;\n";
    1892           0 :     ss << "    double fCount = 0.0;\n";
    1893           0 :     ss << "    double arg = 0.0;\n";
    1894           0 :     unsigned i = vSubArguments.size();
    1895           0 :     while (i--)
    1896             :     {
    1897           0 :         FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
    1898             :         assert(pCur);
    1899           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    1900             :         {
    1901           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    1902             :             {
    1903             :                 const formula::DoubleVectorRefToken* pDVR =
    1904           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    1905           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    1906           0 :                 ss << "    for (int i = ";
    1907           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    1908             :                 {
    1909             : #ifdef  ISNAN
    1910           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    1911           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    1912           0 :                     ss << "    {\n";
    1913             : #else
    1914             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    1915             :                     ss << "    {\n";
    1916             : #endif
    1917             :                 }
    1918           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1919             :                 {
    1920             : #ifdef  ISNAN
    1921           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    1922           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    1923           0 :                     ss << "    {\n";
    1924             : #else
    1925             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    1926             :                     ss << "    {\n";
    1927             : #endif
    1928             :                 }
    1929           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    1930             :                 {
    1931             : #ifdef  ISNAN
    1932           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    1933           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    1934           0 :                     ss << "    {\n";
    1935             : #else
    1936             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    1937             :                     ss << "    {\n";
    1938             : #endif
    1939             :                 }
    1940             :                 else
    1941             :                 {
    1942             : #ifdef  ISNAN
    1943           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1944           0 :                     ss << "    {\n";
    1945             : #else
    1946             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    1947             :                     ss << "    {\n";
    1948             : #endif
    1949             :                 }
    1950             : 
    1951           0 :                 ss << "        arg = ";
    1952           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1953             : #ifdef  ISNAN
    1954           0 :                 ss << "        if (isNan(arg))\n";
    1955           0 :                 ss << "            continue;\n";
    1956             : #endif
    1957           0 :                 ss << "        fSum += arg;\n";
    1958           0 :                 ss << "        fCount += 1.0;\n";
    1959           0 :                 ss << "    }\n";
    1960             :             }
    1961           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    1962             :             {
    1963             :                 const formula::SingleVectorRefToken* pSVR =
    1964           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    1965             : #ifdef  ISNAN
    1966           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    1967           0 :                 ss << "    {\n";
    1968             : #endif
    1969           0 :                 ss << "        arg = ";
    1970           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1971             : #ifdef  ISNAN
    1972           0 :                 ss << "        if (!isNan(arg))\n";
    1973           0 :                 ss << "        {\n";
    1974             : #endif
    1975           0 :                 ss << "            fSum += arg;\n";
    1976           0 :                 ss << "            fCount += 1.0;\n";
    1977             : #ifdef ISNAN
    1978           0 :                 ss << "        }\n";
    1979           0 :                 ss << "    }\n";
    1980             : #endif
    1981             :             }
    1982             :             else
    1983             :             {
    1984           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    1985           0 :                 ss << "    fSum += arg;\n";
    1986           0 :                 ss << "    fCount += 1.0;\n";
    1987             :             }
    1988             :         }
    1989             :         else
    1990             :         {
    1991           0 :             ss << "    arg = ";
    1992           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    1993           0 :             ss << "    fSum += arg;\n";
    1994           0 :             ss << "    fCount += 1.0;\n";
    1995             :         }
    1996             : 
    1997           0 :         if(i == 0)
    1998             :         {
    1999           0 :             ss << "    if(fCount <= 2.0)\n";
    2000           0 :             ss << "        return DBL_MAX;\n";
    2001           0 :             ss << "    else\n";
    2002           0 :             ss << "        fMean = fSum * pow(fCount,-1.0);\n";
    2003             :         }
    2004             :     }
    2005           0 :     i = vSubArguments.size();
    2006           0 :     while (i--)
    2007             :     {
    2008           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2009             :         assert(pCur);
    2010           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2011             :         {
    2012           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2013             :             {
    2014             :                 const formula::DoubleVectorRefToken* pDVR =
    2015           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2016           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2017           0 :                 ss << "    for (int i = ";
    2018           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2019             :                 {
    2020             : #ifdef  ISNAN
    2021           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2022           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2023           0 :                     ss << "    {\n";
    2024             : #else
    2025             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2026             :                     ss << "    {\n";
    2027             : #endif
    2028             :                 }
    2029           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2030             :                 {
    2031             : #ifdef  ISNAN
    2032           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2033           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2034           0 :                     ss << "    {\n";
    2035             : #else
    2036             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2037             :                     ss << "    {\n";
    2038             : #endif
    2039             :                 }
    2040           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2041             :                 {
    2042             : #ifdef  ISNAN
    2043           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2044           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2045           0 :                     ss << "    {\n";
    2046             : #else
    2047             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2048             :                     ss << "    {\n";
    2049             : #endif
    2050             :                 }
    2051             :                 else
    2052             :                 {
    2053             : #ifdef  ISNAN
    2054           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2055           0 :                     ss << "    {\n";
    2056             : #else
    2057             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2058             :                     ss << "    {\n";
    2059             : #endif
    2060             :                 }
    2061             : 
    2062           0 :                 ss << "        arg = ";
    2063           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2064             : #ifdef  ISNAN
    2065           0 :                 ss << "        if (isNan(arg))\n";
    2066           0 :                 ss << "            continue;\n";
    2067             : #endif
    2068           0 :                 ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
    2069           0 :                 ss << "    }\n";
    2070             :             }
    2071           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2072             :             {
    2073             :                 const formula::SingleVectorRefToken* pSVR =
    2074           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2075             : #ifdef  ISNAN
    2076           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2077           0 :                 ss << "    {\n";
    2078             : #endif
    2079           0 :                 ss << "        arg = ";
    2080           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2081             : #ifdef  ISNAN
    2082           0 :                 ss << "        if (!isNan(arg))\n";
    2083           0 :                 ss << "        {\n";
    2084             : #endif
    2085           0 :                 ss << "            vSum += (arg - fMean) * (arg - fMean);\n";
    2086             : #ifdef ISNAN
    2087           0 :                 ss << "        }\n";
    2088           0 :                 ss << "    }\n";
    2089             : #endif
    2090             :             }
    2091             :             else
    2092             :             {
    2093           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2094           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2095             :             }
    2096             :         }
    2097             :         else
    2098             :         {
    2099           0 :             ss << "    arg = ";
    2100           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2101           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2102             :         }
    2103             :     }
    2104           0 :     ss << "    double fStdDev = sqrt(vSum * pow(fCount - 1.0,-1.0));\n";
    2105           0 :     ss << "    double dx = 0.0;\n";
    2106           0 :     ss << "    double xcube = 0.0;\n";
    2107           0 :     ss << "    if(fStdDev == 0.0)\n";
    2108           0 :     ss << "        return DBL_MAX;\n";
    2109           0 :     i = vSubArguments.size();
    2110           0 :     while (i--)
    2111             :     {
    2112           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2113             :         assert(pCur);
    2114           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2115             :         {
    2116           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2117             :             {
    2118             :                 const formula::DoubleVectorRefToken* pDVR =
    2119           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2120           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2121           0 :                 ss << "    for (int i = ";
    2122           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2123             :                 {
    2124             : #ifdef  ISNAN
    2125           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2126           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2127           0 :                     ss << "    {\n";
    2128             : #else
    2129             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2130             :                     ss << "    {\n";
    2131             : #endif
    2132             :                 }
    2133           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2134             :                 {
    2135             : #ifdef  ISNAN
    2136           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2137           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2138           0 :                     ss << "    {\n";
    2139             : #else
    2140             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2141             :                     ss << "    {\n";
    2142             : #endif
    2143             :                 }
    2144           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2145             :                 {
    2146             : #ifdef  ISNAN
    2147           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2148           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2149           0 :                     ss << "    {\n";
    2150             : #else
    2151             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2152             :                     ss << "    {\n";
    2153             : #endif
    2154             :                 }
    2155             :                 else
    2156             :                 {
    2157             : #ifdef  ISNAN
    2158           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2159           0 :                     ss << "    {\n";
    2160             : #else
    2161             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2162             :                     ss << "    {\n";
    2163             : #endif
    2164             :                 }
    2165             : 
    2166           0 :                 ss << "        arg = ";
    2167           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2168             : #ifdef  ISNAN
    2169           0 :                 ss << "        if (isNan(arg))\n";
    2170           0 :                 ss << "            continue;\n";
    2171             : #endif
    2172           0 :                 ss << "        dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2173           0 :                 ss << "        xcube = xcube + dx * dx * dx;\n";
    2174           0 :                 ss << "    }\n";
    2175             :             }
    2176           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2177             :             {
    2178             :                 const formula::SingleVectorRefToken* pSVR =
    2179           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2180             : #ifdef  ISNAN
    2181           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2182           0 :                 ss << "    {\n";
    2183             : #endif
    2184           0 :                 ss << "        arg = ";
    2185           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2186             : #ifdef  ISNAN
    2187           0 :                 ss << "        if (!isNan(arg))\n";
    2188           0 :                 ss << "        {\n";
    2189             : #endif
    2190           0 :                 ss << "            dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2191           0 :                 ss << "            xcube = xcube + dx * dx * dx;\n";
    2192             : #ifdef ISNAN
    2193           0 :                 ss << "        }\n";
    2194           0 :                 ss << "    }\n";
    2195             : #endif
    2196             :             }
    2197             :             else
    2198             :             {
    2199           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2200           0 :                 ss << "    dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2201           0 :                 ss << "    xcube = xcube + dx * dx * dx;\n";
    2202             :             }
    2203             :         }
    2204             :         else
    2205             :         {
    2206           0 :             ss << "    arg = ";
    2207           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2208           0 :             ss << "    dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2209           0 :             ss << "    xcube = xcube + dx * dx * dx;\n";
    2210             :         }
    2211             :     }
    2212           0 :     ss << "    return ((xcube * fCount) * pow(fCount - 1.0,-1.0))";
    2213           0 :     ss << " * pow(fCount - 2.0,-1.0);\n";
    2214           0 :     ss << "}\n";
    2215           0 : }
    2216             : 
    2217           0 : void OpSkewp::GenSlidingWindowFunction(std::stringstream &ss,
    2218             :             const std::string &sSymName, SubArguments &vSubArguments)
    2219             : {
    2220           0 :     ss << "\ndouble " << sSymName;
    2221           0 :     ss << "_" << BinFuncName() << "(";
    2222           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    2223             :     {
    2224           0 :         if (i)
    2225           0 :             ss << ",";
    2226           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    2227             :     }
    2228           0 :     ss << "){\n";
    2229           0 :     ss << "    int gid0 = get_global_id(0);\n";
    2230           0 :     ss << "    double fSum = 0.0;\n";
    2231           0 :     ss << "    double fMean = 0.0;\n";
    2232           0 :     ss << "    double vSum = 0.0;\n";
    2233           0 :     ss << "    double fCount = 0.0;\n";
    2234           0 :     ss << "    double arg = 0.0;\n";
    2235           0 :     unsigned i = vSubArguments.size();
    2236           0 :     while (i--)
    2237             :     {
    2238           0 :         FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
    2239             :         assert(pCur);
    2240           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2241             :         {
    2242           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2243             :             {
    2244             :                 const formula::DoubleVectorRefToken* pDVR =
    2245           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2246           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2247           0 :                 ss << "    for (int i = ";
    2248           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2249             :                 {
    2250             : #ifdef  ISNAN
    2251           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2252           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2253           0 :                     ss << "    {\n";
    2254             : #else
    2255             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2256             :                     ss << "    {\n";
    2257             : #endif
    2258             :                 }
    2259           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2260             :                 {
    2261             : #ifdef  ISNAN
    2262           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2263           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2264           0 :                     ss << "    {\n";
    2265             : #else
    2266             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2267             :                     ss << "    {\n";
    2268             : #endif
    2269             :                 }
    2270           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2271             :                 {
    2272             : #ifdef  ISNAN
    2273           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2274           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2275           0 :                     ss << "    {\n";
    2276             : #else
    2277             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2278             :                     ss << "    {\n";
    2279             : #endif
    2280             :                 }
    2281             :                 else
    2282             :                 {
    2283             : #ifdef  ISNAN
    2284           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2285           0 :                     ss << "    {\n";
    2286             : #else
    2287             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2288             :                     ss << "    {\n";
    2289             : #endif
    2290             :                 }
    2291             : 
    2292           0 :                 ss << "        arg = ";
    2293           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2294             : #ifdef  ISNAN
    2295           0 :                 ss << "        if (isNan(arg))\n";
    2296           0 :                 ss << "            continue;\n";
    2297             : #endif
    2298           0 :                 ss << "        fSum += arg;\n";
    2299           0 :                 ss << "        fCount += 1.0;\n";
    2300           0 :                 ss << "    }\n";
    2301             :             }
    2302           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2303             :             {
    2304             :                 const formula::SingleVectorRefToken* pSVR =
    2305           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2306             : #ifdef  ISNAN
    2307           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2308           0 :                 ss << "    {\n";
    2309             : #endif
    2310           0 :                 ss << "        arg = ";
    2311           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2312             : #ifdef  ISNAN
    2313           0 :                 ss << "        if (!isNan(arg))\n";
    2314           0 :                 ss << "        {\n";
    2315             : #endif
    2316           0 :                 ss << "            fSum += arg;\n";
    2317           0 :                 ss << "            fCount += 1.0;\n";
    2318             : #ifdef ISNAN
    2319           0 :                 ss << "        }\n";
    2320           0 :                 ss << "    }\n";
    2321             : #endif
    2322             :             }
    2323             :             else
    2324             :             {
    2325           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2326           0 :                 ss << "    fSum += arg;\n";
    2327           0 :                 ss << "    fCount += 1.0;\n";
    2328             :             }
    2329             :         }
    2330             :         else
    2331             :         {
    2332           0 :             ss << "    arg = ";
    2333           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2334           0 :             ss << "    fSum += arg;\n";
    2335           0 :             ss << "    fCount += 1.0;\n";
    2336             :         }
    2337             : 
    2338           0 :         if(i == 0)
    2339             :         {
    2340           0 :             ss << "    if(fCount <= 2.0)\n";
    2341           0 :             ss << "        return DBL_MAX;\n";
    2342           0 :             ss << "    else\n";
    2343           0 :             ss << "        fMean = fSum * pow(fCount,-1.0);\n";
    2344             :         }
    2345             :     }
    2346           0 :     i = vSubArguments.size();
    2347           0 :     while (i--)
    2348             :     {
    2349           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2350             :         assert(pCur);
    2351           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2352             :         {
    2353           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2354             :             {
    2355             :                 const formula::DoubleVectorRefToken* pDVR =
    2356           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2357           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2358           0 :                 ss << "    for (int i = ";
    2359           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2360             :                 {
    2361             : #ifdef  ISNAN
    2362           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2363           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2364           0 :                     ss << "    {\n";
    2365             : #else
    2366             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2367             :                     ss << "    {\n";
    2368             : #endif
    2369             :                 }
    2370           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2371             :                 {
    2372             : #ifdef  ISNAN
    2373           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2374           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2375           0 :                     ss << "    {\n";
    2376             : #else
    2377             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2378             :                     ss << "    {\n";
    2379             : #endif
    2380             :                 }
    2381           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2382             :                 {
    2383             : #ifdef  ISNAN
    2384           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2385           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2386           0 :                     ss << "    {\n";
    2387             : #else
    2388             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2389             :                     ss << "    {\n";
    2390             : #endif
    2391             :                 }
    2392             :                 else
    2393             :                 {
    2394             : #ifdef  ISNAN
    2395           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2396           0 :                     ss << "    {\n";
    2397             : #else
    2398             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2399             :                     ss << "    {\n";
    2400             : #endif
    2401             :                 }
    2402             : 
    2403           0 :                 ss << "        arg = ";
    2404           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2405             : #ifdef  ISNAN
    2406           0 :                 ss << "        if (isNan(arg))\n";
    2407           0 :                 ss << "            continue;\n";
    2408             : #endif
    2409           0 :                 ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
    2410           0 :                 ss << "    }\n";
    2411             :             }
    2412           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2413             :             {
    2414             :                 const formula::SingleVectorRefToken* pSVR =
    2415           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2416             : #ifdef  ISNAN
    2417           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2418           0 :                 ss << "    {\n";
    2419             : #endif
    2420           0 :                 ss << "        arg = ";
    2421           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2422             : #ifdef  ISNAN
    2423           0 :                 ss << "        if (!isNan(arg))\n";
    2424           0 :                 ss << "        {\n";
    2425             : #endif
    2426           0 :                 ss << "            vSum += (arg - fMean) * (arg - fMean);\n";
    2427             : #ifdef ISNAN
    2428           0 :                 ss << "        }\n";
    2429           0 :                 ss << "    }\n";
    2430             : #endif
    2431             :             }
    2432             :             else
    2433             :             {
    2434           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2435           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2436             :             }
    2437             :         }
    2438             :         else
    2439             :         {
    2440           0 :             ss << "    arg = ";
    2441           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2442           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2443             :         }
    2444             :     }
    2445           0 :     ss << "    double fStdDev = sqrt(vSum * pow(fCount,-1.0));\n";
    2446           0 :     ss << "    double dx = 0.0;\n";
    2447           0 :     ss << "    double xcube = 0.0;\n";
    2448           0 :     ss << "    if(fStdDev == 0.0)\n";
    2449           0 :     ss << "        return DBL_MAX;\n";
    2450           0 :     i = vSubArguments.size();
    2451           0 :     while (i--)
    2452             :     {
    2453           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2454             :         assert(pCur);
    2455           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2456             :         {
    2457           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2458             :             {
    2459             :                 const formula::DoubleVectorRefToken* pDVR =
    2460           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2461           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2462           0 :                 ss << "    for (int i = ";
    2463           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2464             :                 {
    2465             : #ifdef  ISNAN
    2466           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2467           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2468           0 :                     ss << "    {\n";
    2469             : #else
    2470             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2471             :                     ss << "    {\n";
    2472             : #endif
    2473             :                 }
    2474           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2475             :                 {
    2476             : #ifdef  ISNAN
    2477           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2478           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2479           0 :                     ss << "    {\n";
    2480             : #else
    2481             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2482             :                     ss << "    {\n";
    2483             : #endif
    2484             :                 }
    2485           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2486             :                 {
    2487             : #ifdef  ISNAN
    2488           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2489           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2490           0 :                     ss << "    {\n";
    2491             : #else
    2492             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2493             :                     ss << "    {\n";
    2494             : #endif
    2495             :                 }
    2496             :                 else
    2497             :                 {
    2498             : #ifdef  ISNAN
    2499           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2500           0 :                     ss << "    {\n";
    2501             : #else
    2502             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2503             :                     ss << "    {\n";
    2504             : #endif
    2505             :                 }
    2506             : 
    2507           0 :                 ss << "        arg = ";
    2508           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2509             : #ifdef  ISNAN
    2510           0 :                 ss << "        if (isNan(arg))\n";
    2511           0 :                 ss << "            continue;\n";
    2512             : #endif
    2513           0 :                 ss << "        dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2514           0 :                 ss << "        xcube = xcube + dx * dx * dx;\n";
    2515           0 :                 ss << "    }\n";
    2516             :             }
    2517           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2518             :             {
    2519             :                 const formula::SingleVectorRefToken* pSVR =
    2520           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2521             : #ifdef  ISNAN
    2522           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2523           0 :                 ss << "    {\n";
    2524             : #endif
    2525           0 :                 ss << "        arg = ";
    2526           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2527             : #ifdef  ISNAN
    2528           0 :                 ss << "        if (!isNan(arg))\n";
    2529           0 :                 ss << "        {\n";
    2530             : #endif
    2531           0 :                 ss << "            dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2532           0 :                 ss << "            xcube = xcube + dx * dx * dx;\n";
    2533             : #ifdef ISNAN
    2534           0 :                 ss << "        }\n";
    2535           0 :                 ss << "    }\n";
    2536             : #endif
    2537             :             }
    2538             :             else
    2539             :             {
    2540           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2541           0 :                 ss << "    dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2542           0 :                 ss << "    xcube = xcube + dx * dx * dx;\n";
    2543             :             }
    2544             :         }
    2545             :         else
    2546             :         {
    2547           0 :             ss << "    arg = ";
    2548           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2549           0 :             ss << "    dx = (arg - fMean) * pow(fStdDev,-1.0);\n";
    2550           0 :             ss << "    xcube = xcube + dx * dx * dx;\n";
    2551             :         }
    2552             :     }
    2553           0 :     ss << "    return xcube * pow(fCount,-1.0);\n";
    2554           0 :     ss << "}\n";
    2555           0 : }
    2556             : 
    2557           0 : void OpTInv::BinInlineFun(std::set<std::string>& decls,
    2558             :     std::set<std::string>& funs)
    2559             : {
    2560           0 :     decls.insert(fMachEpsDecl);
    2561           0 :     funs.insert("");
    2562           0 :     decls.insert(fMaxGammaArgumentDecl);
    2563           0 :     funs.insert("");
    2564           0 :     decls.insert(lcl_getLanczosSumDecl);
    2565           0 :     funs.insert(lcl_getLanczosSum);
    2566           0 :     decls.insert(GetBetaDecl);
    2567           0 :     funs.insert(GetBeta);
    2568           0 :     decls.insert(GetLogBetaDecl);
    2569           0 :     funs.insert(GetLogBeta);
    2570           0 :     decls.insert(GetBetaDistPDFDecl);
    2571           0 :     funs.insert(GetBetaDistPDF);
    2572           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);
    2573           0 :     funs.insert(lcl_GetBetaHelperContFrac);
    2574           0 :     decls.insert(GetBetaDistDecl);
    2575           0 :     funs.insert(GetBetaDist);
    2576           0 :     decls.insert(GetTDistDecl);
    2577           0 :     funs.insert(GetTDist);
    2578           0 :     decls.insert(GetValueDecl);
    2579           0 :     funs.insert(GetValue);
    2580           0 :     decls.insert(lcl_HasChangeOfSignDecl);
    2581           0 :     funs.insert(lcl_HasChangeOfSign);
    2582           0 :     decls.insert(lcl_IterateInverseDecl);
    2583           0 :     funs.insert(lcl_IterateInverse);
    2584           0 : }
    2585             : 
    2586           0 : void OpTInv::GenSlidingWindowFunction(std::stringstream &ss,
    2587             :             const std::string &sSymName, SubArguments &vSubArguments)
    2588             : {
    2589           0 :     ss << "\ndouble " << sSymName;
    2590           0 :     ss << "_" << BinFuncName() << "(";
    2591           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    2592             :     {
    2593           0 :         if (i)
    2594           0 :             ss << ",";
    2595           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    2596             :     }
    2597           0 :     ss << ") {\n";
    2598           0 :     ss << "    int gid0 = get_global_id(0);\n";
    2599           0 :     ss << "    double x = 0.0;\n";
    2600           0 :     ss << "    double fDF = 0.0;\n";
    2601           0 :     if(vSubArguments.size() != 2)
    2602             :     {
    2603           0 :         ss << "    return DBL_MAX;\n" << "}\n";
    2604           0 :         return ;
    2605             :     }
    2606           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    2607             :     assert(tmpCur0);
    2608           0 :     if(ocPush == vSubArguments[0]->GetFormulaToken()->GetOpCode())
    2609             :     {
    2610           0 :         if(tmpCur0->GetType() == formula::svSingleVectorRef)
    2611             :         {
    2612             :             const formula::SingleVectorRefToken*tmpCurDVR0 =
    2613           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur0);
    2614             : #ifdef ISNAN
    2615           0 :             ss << "    if(gid0 < ";
    2616           0 :             ss << tmpCurDVR0->GetArrayLength() << ")\n";
    2617           0 :             ss << "    {\n";
    2618             : #endif
    2619           0 :             ss << "        x = ";
    2620           0 :             ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    2621             : #ifdef ISNAN
    2622           0 :             ss << "        if(isNan(x))\n";
    2623           0 :             ss << "            x = 0.0;\n";
    2624           0 :             ss << "    }\n";
    2625             : #endif
    2626             :         }
    2627           0 :         else if(tmpCur0->GetType() == formula::svDouble)
    2628             :         {
    2629           0 :             ss << "    x = " << tmpCur0->GetDouble() << ";\n";
    2630             :         }
    2631             :         else
    2632             :         {
    2633           0 :             ss << "    return DBL_MAX;\n" << "}\n";
    2634           0 :             return ;
    2635             :         }
    2636             :     }
    2637             :     else
    2638             :     {
    2639           0 :         ss << "    x = ";
    2640           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    2641             :     }
    2642           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    2643             :     assert(tmpCur1);
    2644           0 :     if(ocPush == vSubArguments[1]->GetFormulaToken()->GetOpCode())
    2645             :     {
    2646           0 :         if(tmpCur1->GetType() == formula::svSingleVectorRef)
    2647             :         {
    2648             :             const formula::SingleVectorRefToken*tmpCurDVR1 =
    2649           0 :                 static_cast<const formula::SingleVectorRefToken *>(tmpCur1);
    2650             : #ifdef ISNAN
    2651           0 :             ss << "    if(gid0 < ";
    2652           0 :             ss << tmpCurDVR1->GetArrayLength() << ")\n";
    2653           0 :             ss << "    {\n";
    2654             : #endif
    2655           0 :             ss << "        fDF = ";
    2656           0 :             ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    2657             : #ifdef ISNAN
    2658           0 :             ss << "        if(isNan(fDF))\n";
    2659           0 :             ss << "            fDF = 0.0;\n";
    2660           0 :             ss << "        else\n";
    2661             : #endif
    2662           0 :             ss << "            fDF = floor(fDF);\n";
    2663             : #ifdef ISNAN
    2664           0 :             ss << "    }\n";
    2665             : #endif
    2666             :         }
    2667           0 :         else if(tmpCur1->GetType() == formula::svDouble)
    2668             :         {
    2669           0 :             ss << "    fDF = floor(convert_double(";
    2670           0 :             ss << tmpCur1->GetDouble() << "));\n";
    2671             :         }
    2672             :         else
    2673             :         {
    2674           0 :             ss << "    return DBL_MAX;\n" << "}\n";
    2675           0 :             return ;
    2676             :         }
    2677             :     }
    2678             :     else
    2679             :     {
    2680           0 :         ss << "    fDF = floor(";
    2681           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ");\n";
    2682             :     }
    2683           0 :     ss << "    if (x > 1.0||fDF < 1.0 || fDF > 1.0E10 || x <= 0.0)\n";
    2684           0 :     ss << "        return DBL_MAX;\n";
    2685           0 :     ss << "    bool bConvError;\n";
    2686           0 :     ss << "    double fVal = lcl_IterateInverse(\n";
    2687           0 :     ss << "        fDF*0.5, fDF, &bConvError,x,fDF );\n";
    2688           0 :     ss << "    if (bConvError)\n";
    2689           0 :     ss << "        return DBL_MAX;\n";
    2690           0 :     ss << "    return fVal;\n";
    2691           0 :     ss << "}\n";
    2692             : }
    2693             : 
    2694           0 : void OpStDev::GenSlidingWindowFunction(std::stringstream &ss,
    2695             :             const std::string &sSymName, SubArguments &vSubArguments)
    2696             : {
    2697           0 :     ss << "\ndouble " << sSymName;
    2698           0 :     ss << "_" << BinFuncName() << "(";
    2699           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    2700             :     {
    2701           0 :         if (i)
    2702           0 :             ss << ",";
    2703           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    2704             :     }
    2705           0 :     ss << "){\n";
    2706           0 :     ss << "    int gid0 = get_global_id(0);\n";
    2707           0 :     ss << "    double fSum = 0.0;\n";
    2708           0 :     ss << "    double vSum = 0.0;\n";
    2709           0 :     ss << "    double fMean = 0.0;\n";
    2710           0 :     ss << "    double fCount = 0.0;\n";
    2711           0 :     ss << "    double arg = 0.0;\n";
    2712           0 :     unsigned i = vSubArguments.size();
    2713           0 :     while (i--)
    2714             :     {
    2715           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2716             :         assert(pCur);
    2717           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2718             :         {
    2719           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2720             :             {
    2721             :                 const formula::DoubleVectorRefToken* pDVR =
    2722           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2723           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2724           0 :                 ss << "    for (int i = ";
    2725           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2726             :                 {
    2727             : #ifdef  ISNAN
    2728           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2729           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2730           0 :                     ss << "    {\n";
    2731             : #else
    2732             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2733             :                     ss << "    {\n";
    2734             : #endif
    2735             :                 }
    2736           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2737             :                 {
    2738             : #ifdef  ISNAN
    2739           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2740           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2741           0 :                     ss << "    {\n";
    2742             : #else
    2743             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2744             :                     ss << "    {\n";
    2745             : #endif
    2746             :                 }
    2747           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2748             :                 {
    2749             : #ifdef  ISNAN
    2750           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2751           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2752           0 :                     ss << "    {\n";
    2753             : #else
    2754             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2755             :                     ss << "    {\n";
    2756             : #endif
    2757             :                 }
    2758             :                 else
    2759             :                 {
    2760             : #ifdef  ISNAN
    2761           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2762           0 :                     ss << "    {\n";
    2763             : #else
    2764             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2765             :                     ss << "    {\n";
    2766             : #endif
    2767             :                 }
    2768             : 
    2769           0 :                 ss << "        arg = ";
    2770           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2771             : #ifdef  ISNAN
    2772           0 :                 ss << "        if (isNan(arg))\n";
    2773           0 :                 ss << "            continue;\n";
    2774             : #endif
    2775           0 :                 ss << "        fSum += arg;\n";
    2776           0 :                 ss << "        fCount += 1.0;\n";
    2777           0 :                 ss << "    }\n";
    2778             :             }
    2779           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2780             :             {
    2781             :                 const formula::SingleVectorRefToken* pSVR =
    2782           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2783             : #ifdef  ISNAN
    2784           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2785           0 :                 ss << "    {\n";
    2786             : #endif
    2787           0 :                 ss << "        arg = ";
    2788           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2789             : #ifdef  ISNAN
    2790           0 :                 ss << "        if (!isNan(arg))\n";
    2791           0 :                 ss << "        {\n";
    2792             : #endif
    2793           0 :                 ss << "            fSum += arg;\n";
    2794           0 :                 ss << "            fCount += 1.0;\n";
    2795             : #ifdef ISNAN
    2796           0 :                 ss << "        }\n";
    2797           0 :                 ss << "    }\n";
    2798             : #endif
    2799             :             }
    2800             :             else
    2801             :             {
    2802           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2803           0 :                 ss << "    fSum += arg;\n";
    2804           0 :                 ss << "    fCount += 1.0;\n";
    2805             :             }
    2806             :         }
    2807             :         else
    2808             :         {
    2809           0 :             ss << "    arg = ";
    2810           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2811           0 :             ss << "    fSum += arg;\n";
    2812           0 :             ss << "    fCount += 1.0;\n";
    2813             :         }
    2814           0 :         if (i == 0)
    2815             :         {
    2816           0 :             ss << "    fMean = fSum * pow(fCount,-1.0)" << ";\n";
    2817             :         }
    2818             :     }
    2819           0 :     i = vSubArguments.size();
    2820           0 :     while (i--)
    2821             :     {
    2822           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2823             :         assert(pCur);
    2824           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2825             :         {
    2826           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2827             :             {
    2828             :                 const formula::DoubleVectorRefToken* pDVR =
    2829           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2830           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2831           0 :                 ss << "    for (int i = ";
    2832           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2833             :                 {
    2834             : #ifdef  ISNAN
    2835           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2836           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2837           0 :                     ss << "    {\n";
    2838             : #else
    2839             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2840             :                     ss << "    {\n";
    2841             : #endif
    2842             :                 }
    2843           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2844             :                 {
    2845             : #ifdef  ISNAN
    2846           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2847           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2848           0 :                     ss << "    {\n";
    2849             : #else
    2850             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2851             :                     ss << "    {\n";
    2852             : #endif
    2853             :                 }
    2854           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2855             :                 {
    2856             : #ifdef  ISNAN
    2857           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2858           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2859           0 :                     ss << "    {\n";
    2860             : #else
    2861             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2862             :                     ss << "    {\n";
    2863             : #endif
    2864             :                 }
    2865             :                 else
    2866             :                 {
    2867             : #ifdef  ISNAN
    2868           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2869           0 :                     ss << "    {\n";
    2870             : #else
    2871             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2872             :                     ss << "    {\n";
    2873             : #endif
    2874             :                 }
    2875           0 :                 ss << "        arg = ";
    2876           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2877             : #ifdef  ISNAN
    2878           0 :                 ss << "        if (isNan(arg))\n";
    2879           0 :                 ss << "            continue;\n";
    2880             : #endif
    2881           0 :                 ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
    2882           0 :                 ss << "    }\n";
    2883             :             }
    2884           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    2885             :             {
    2886             :                 const formula::SingleVectorRefToken* pSVR =
    2887           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    2888             : #ifdef  ISNAN
    2889           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    2890           0 :                 ss << "    {\n";
    2891             : #endif
    2892           0 :                 ss << "        arg = ";
    2893           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2894             : #ifdef  ISNAN
    2895           0 :                 ss << "        if (!isNan(arg))\n";
    2896           0 :                 ss << "        {\n";
    2897             : #endif
    2898           0 :                 ss << "            vSum += (arg - fMean) * (arg - fMean);\n";
    2899             : #ifdef ISNAN
    2900           0 :                 ss << "        }\n";
    2901           0 :                 ss << "    }\n";
    2902             : #endif
    2903             :             }
    2904             :             else
    2905             :             {
    2906           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    2907           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2908             :             }
    2909             :         }
    2910             :         else
    2911             :         {
    2912           0 :             ss << "    arg = ";
    2913           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    2914           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    2915             :         }
    2916             :     }
    2917           0 :     ss << "    if (fCount <= 1.0)\n";
    2918           0 :     ss << "        return DBL_MAX;\n";
    2919           0 :     ss << "    else\n";
    2920           0 :     ss << "        return sqrt(vSum * pow(fCount - 1.0,-1.0));\n";
    2921           0 :     ss << "}\n";
    2922           0 : }
    2923             : 
    2924           0 : void OpStDevP::GenSlidingWindowFunction(std::stringstream &ss,
    2925             :             const std::string &sSymName, SubArguments &vSubArguments)
    2926             : {
    2927           0 :     ss << "\ndouble " << sSymName;
    2928           0 :     ss << "_" << BinFuncName() << "(";
    2929           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    2930             :     {
    2931           0 :         if (i)
    2932           0 :             ss << ",";
    2933           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    2934             :     }
    2935           0 :     ss << "){\n";
    2936           0 :     ss << "    int gid0 = get_global_id(0);\n";
    2937           0 :     ss << "    double fSum = 0.0;\n";
    2938           0 :     ss << "    double fMean = 0.0;\n";
    2939           0 :     ss << "    double vSum = 0.0;\n";
    2940           0 :     ss << "    double fCount = 0.0;\n";
    2941           0 :     ss << "    double arg = 0.0;\n";
    2942           0 :     unsigned i = vSubArguments.size();
    2943           0 :     while (i--)
    2944             :     {
    2945           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    2946             :         assert(pCur);
    2947           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    2948             :         {
    2949           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    2950             :             {
    2951             :                 const formula::DoubleVectorRefToken* pDVR =
    2952           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    2953           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    2954           0 :                 ss << "    for (int i = ";
    2955           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    2956             :                 {
    2957             : #ifdef  ISNAN
    2958           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    2959           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    2960           0 :                     ss << "    {\n";
    2961             : #else
    2962             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    2963             :                     ss << "    {\n";
    2964             : #endif
    2965             :                 }
    2966           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2967             :                 {
    2968             : #ifdef  ISNAN
    2969           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    2970           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    2971           0 :                     ss << "    {\n";
    2972             : #else
    2973             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    2974             :                     ss << "    {\n";
    2975             : #endif
    2976             :                 }
    2977           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    2978             :                 {
    2979             : #ifdef  ISNAN
    2980           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    2981           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    2982           0 :                     ss << "    {\n";
    2983             : #else
    2984             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    2985             :                     ss << "    {\n";
    2986             : #endif
    2987             :                 }
    2988             :                 else
    2989             :                 {
    2990             : #ifdef  ISNAN
    2991           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2992           0 :                     ss << "    {\n";
    2993             : #else
    2994             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    2995             :                     ss << "    {\n";
    2996             : #endif
    2997             :                 }
    2998             : 
    2999           0 :                 ss << "        arg = ";
    3000           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3001             : #ifdef  ISNAN
    3002           0 :                 ss << "        if (isNan(arg))\n";
    3003           0 :                 ss << "            continue;\n";
    3004             : #endif
    3005           0 :                 ss << "        fSum += arg;\n";
    3006           0 :                 ss << "        fCount += 1.0;\n";
    3007           0 :                 ss << "    }\n";
    3008             :             }
    3009           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    3010             :             {
    3011             :                 const formula::SingleVectorRefToken* pSVR =
    3012           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    3013             : #ifdef  ISNAN
    3014           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    3015           0 :                 ss << "    {\n";
    3016             : #endif
    3017           0 :                 ss << "        arg = ";
    3018           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3019             : #ifdef ISNAN
    3020           0 :                 ss << "        if (!isNan(arg))\n";
    3021           0 :                 ss << "        {\n";
    3022             : #endif
    3023           0 :                 ss << "            fSum += arg;\n";
    3024           0 :                 ss << "            fCount += 1.0;\n";
    3025             : #ifdef ISNAN
    3026           0 :                 ss << "        }\n";
    3027           0 :                 ss << "    }\n";
    3028             : #endif
    3029             :             }
    3030             :             else
    3031             :             {
    3032           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    3033           0 :                 ss << "    fSum += arg;\n";
    3034           0 :                 ss << "    fCount += 1.0;\n";
    3035             :             }
    3036             :         }
    3037             :         else
    3038             :         {
    3039           0 :             ss << "    arg = ";
    3040           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3041           0 :             ss << "    fSum += arg;\n";
    3042           0 :             ss << "    fCount += 1.0;\n";
    3043             : 
    3044             :         }
    3045           0 :         if (i == 0)
    3046             :         {
    3047           0 :             ss << "    fMean = fSum * pow(fCount,-1.0)" << ";\n";
    3048             :         }
    3049             :     }
    3050           0 :     i = vSubArguments.size();
    3051           0 :     while (i--)
    3052             :     {
    3053           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    3054             :         assert(pCur);
    3055           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    3056             :         {
    3057           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    3058             :             {
    3059             :                 const formula::DoubleVectorRefToken* pDVR =
    3060           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    3061           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    3062           0 :                 ss << "    for (int i = ";
    3063           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3064             :                 {
    3065             : #ifdef  ISNAN
    3066           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    3067           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    3068           0 :                     ss << "    {\n";
    3069             : #else
    3070             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    3071             :                     ss << "    {\n";
    3072             : #endif
    3073             :                 }
    3074           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3075             :                 {
    3076             : #ifdef  ISNAN
    3077           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    3078           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    3079           0 :                     ss << "    {\n";
    3080             : #else
    3081             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    3082             :                     ss << "    {\n";
    3083             : #endif
    3084             :                 }
    3085           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3086             :                 {
    3087             : #ifdef  ISNAN
    3088           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    3089           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    3090           0 :                     ss << "    {\n";
    3091             : #else
    3092             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3093             :                     ss << "    {\n";
    3094             : #endif
    3095             :                 }
    3096             :                 else
    3097             :                 {
    3098             : #ifdef  ISNAN
    3099           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    3100           0 :                     ss << "    {\n";
    3101             : #else
    3102             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    3103             :                     ss << "    {\n";
    3104             : #endif
    3105             :                 }
    3106             : 
    3107           0 :                 ss << "        arg = ";
    3108           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3109             : #ifdef  ISNAN
    3110           0 :                 ss << "        if (isNan(arg))\n";
    3111           0 :                 ss << "            continue;\n";
    3112             : #endif
    3113           0 :                 ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
    3114           0 :                 ss << "    }\n";
    3115             :             }
    3116           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    3117             :             {
    3118             :                 const formula::SingleVectorRefToken* pSVR =
    3119           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    3120             : #ifdef  ISNAN
    3121           0 :                 ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    3122           0 :                 ss << "    {\n";
    3123             : #endif
    3124           0 :                 ss << "        arg = ";
    3125           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3126             : #ifdef ISNAN
    3127           0 :                 ss << "        if (!isNan(arg))\n";
    3128           0 :                 ss << "        {\n";
    3129             : #endif
    3130           0 :                 ss << "            vSum += (arg - fMean) * (arg - fMean);\n";
    3131             : #ifdef ISNAN
    3132           0 :                 ss << "        }\n";
    3133           0 :                 ss << "    }\n";
    3134             : #endif
    3135             :             }
    3136             :             else
    3137             :             {
    3138           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    3139           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    3140             :             }
    3141             :         }
    3142             :         else
    3143             :         {
    3144           0 :             ss << "    arg = ";
    3145           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    3146           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
    3147             :         }
    3148             :     }
    3149           0 :     ss << "    if (fCount == 0.0)\n";
    3150           0 :     ss << "        return DBL_MAX;\n";
    3151           0 :     ss << "    else\n";
    3152           0 :     ss << "        return sqrt(vSum * pow(fCount,-1.0));\n";
    3153           0 :     ss << "}\n";
    3154           0 : }
    3155             : 
    3156           0 : void OpSlope::GenSlidingWindowFunction(std::stringstream &ss,
    3157             :             const std::string &sSymName, SubArguments &vSubArguments)
    3158             : {
    3159           0 :     ss << "\ndouble " << sSymName;
    3160           0 :     ss << "_" << BinFuncName() << "(";
    3161           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3162             :     {
    3163           0 :         if (i)
    3164           0 :             ss << ",";
    3165           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3166             :     }
    3167           0 :     ss << "){\n";
    3168           0 :     ss << "    int gid0 = get_global_id(0);\n";
    3169           0 :     ss << "    double fSumX = 0.0;\n";
    3170           0 :     ss << "    double fSumY = 0.0;\n";
    3171           0 :     ss << "    double fMeanX = 0.0;\n";
    3172           0 :     ss << "    double fMeanY = 0.0;\n";
    3173           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    3174           0 :     ss << "    double fSumSqrDeltaX = 0.0;\n";
    3175           0 :     ss << "    double fCount = 0.0;\n";
    3176           0 :     ss << "    double argX = 0.0;\n";
    3177           0 :     ss << "    double argY = 0.0;\n";
    3178           0 :     if(vSubArguments.size() != 2)
    3179             :     {
    3180           0 :         ss << "    return DBL_MAX;\n";
    3181           0 :         ss << "}\n";
    3182           0 :         return ;
    3183             :     }
    3184           0 :     FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
    3185           0 :     FormulaToken *pCur1 = vSubArguments[0]->GetFormulaToken();
    3186             :     assert(pCur);
    3187             :     assert(pCur1);
    3188           0 :     if (pCur->GetType() == formula::svDoubleVectorRef&&
    3189           0 :         pCur1->GetType() == formula::svDoubleVectorRef)
    3190             :     {
    3191             :         const formula::DoubleVectorRefToken* pDVR =
    3192           0 :             static_cast<const formula::DoubleVectorRefToken *>(pCur);
    3193             :         const formula::DoubleVectorRefToken* pDVR1 =
    3194           0 :             static_cast<const formula::DoubleVectorRefToken *>(pCur1);
    3195             : 
    3196           0 :         size_t nCurWindowSize = pDVR->GetRefRowSize();
    3197           0 :         size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
    3198           0 :         size_t arrayLength = pDVR->GetArrayLength()<
    3199           0 :                pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
    3200           0 :                     pDVR1->GetArrayLength();
    3201           0 :         if(nCurWindowSize != nCurWindowSize1)
    3202             :         {
    3203           0 :             ss << "    return DBL_MAX;\n";
    3204           0 :             ss << "}\n";
    3205           0 :             return ;
    3206             :         }
    3207           0 :         ss << "    for (int i = ";
    3208           0 :         if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3209           0 :             &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    3210             :         {
    3211             : #ifdef  ISNAN
    3212           0 :             ss << "gid0; i < " << arrayLength;
    3213           0 :             ss << " && i < " << nCurWindowSize  << "; i++)\n";
    3214           0 :             ss << "    {\n";
    3215             : #else
    3216             :             ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    3217             :             ss << "    {\n";
    3218             : #endif
    3219             :         }
    3220           0 :         else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3221           0 :             &&(pDVR->IsStartFixed() && !pDVR->IsEndFixed()))
    3222             :         {
    3223             : #ifdef  ISNAN
    3224           0 :             ss << "0; i < " << arrayLength ;
    3225           0 :             ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    3226           0 :             ss << "    {\n";
    3227             : #else
    3228             :             ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    3229             :             ss << "    {\n";
    3230             : #endif
    3231             :         }
    3232           0 :         else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3233           0 :             &&(!pDVR->IsStartFixed() && !pDVR->IsEndFixed()))
    3234             :         {
    3235             : #ifdef  ISNAN
    3236           0 :             ss << "0; i + gid0 < " << arrayLength;
    3237           0 :             ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    3238           0 :             ss << "    {\n";
    3239             : #else
    3240             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3241             :             ss << "    {\n";
    3242             : #endif
    3243             :         }
    3244           0 :         else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3245           0 :             &&(pDVR->IsStartFixed() && pDVR->IsEndFixed()))
    3246             :         {
    3247             : #ifdef  ISNAN
    3248           0 :             ss << "0; i < " << arrayLength << "; i++)\n";
    3249           0 :             ss << "    {\n";
    3250             : #else
    3251             :             ss << "0; i < " << arrayLength << "; i++)\n";
    3252             :             ss << "    {\n";
    3253             : #endif
    3254             :         }
    3255             :         else
    3256             :         {
    3257             : #ifdef  ISNAN
    3258           0 :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3259           0 :             ss << "    {\n";
    3260             : #else
    3261             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3262             :             ss << "    {\n";
    3263             : #endif
    3264           0 :             ss << "        break;\n";
    3265           0 :             ss << "    }";
    3266           0 :             ss << "    return DBL_MAX;\n";
    3267           0 :             ss << "}\n";
    3268           0 :             return ;
    3269             :         }
    3270             : 
    3271           0 :         ss << "        argX = ";
    3272           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    3273           0 :         ss << "        argY = ";
    3274           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    3275             : #ifdef  ISNAN
    3276           0 :         ss << "        if (isNan(argX) || isNan(argY))\n";
    3277           0 :         ss << "            continue;\n";
    3278             : #endif
    3279           0 :         ss << "        fSumX += argX;\n";
    3280           0 :         ss << "        fSumY += argY;\n";
    3281           0 :         ss << "        fCount += 1.0;\n";
    3282           0 :         ss << "    }\n";
    3283             : 
    3284           0 :         ss << "    if (fCount < 1.0)\n";
    3285           0 :         ss << "        return DBL_MAX;\n";
    3286           0 :         ss << "    else\n";
    3287           0 :         ss << "    {\n";
    3288           0 :         ss << "        fMeanX = fSumX * pow(fCount,-1.0);\n";
    3289           0 :         ss << "        fMeanY = fSumY * pow(fCount,-1.0);\n";
    3290             : 
    3291           0 :         ss << "        for (int i = ";
    3292           0 :         if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3293           0 :             &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    3294             :         {
    3295             : #ifdef  ISNAN
    3296           0 :             ss << "gid0; i < " << arrayLength;
    3297           0 :             ss << " && i < " << nCurWindowSize  << "; i++)\n";
    3298           0 :             ss << "        {\n";
    3299             : #else
    3300             :             ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    3301             :             ss << "        {\n";
    3302             : #endif
    3303             :         }
    3304           0 :         else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3305           0 :             &&(pDVR->IsStartFixed() && !pDVR->IsEndFixed()))
    3306             :         {
    3307             : #ifdef  ISNAN
    3308           0 :             ss << "0; i < " << arrayLength ;
    3309           0 :             ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    3310           0 :             ss << "        {\n";
    3311             : #else
    3312             :             ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    3313             :             ss << "        {\n";
    3314             : #endif
    3315             :         }
    3316           0 :         else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3317           0 :             &&(!pDVR->IsStartFixed() && !pDVR->IsEndFixed()))
    3318             :         {
    3319             : #ifdef  ISNAN
    3320           0 :             ss << "0; i + gid0 < " << arrayLength;
    3321           0 :             ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    3322           0 :             ss << "        {\n";
    3323             : #else
    3324             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3325             :             ss << "        {\n";
    3326             : #endif
    3327             :         }
    3328             :         else
    3329             :         {
    3330             : #ifdef  ISNAN
    3331           0 :             ss << "0; i < " << arrayLength << "; i++)\n";
    3332           0 :             ss << "        {\n";
    3333             : #else
    3334             :             ss << "0; i < " << arrayLength << "; i++)\n";
    3335             :             ss << "        {\n";
    3336             : #endif
    3337             :         }
    3338             : 
    3339           0 :         ss << "            argX = ";
    3340           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    3341           0 :         ss << "            argY = ";
    3342           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    3343             : #ifdef  ISNAN
    3344           0 :         ss << "            if (isNan(argX) || isNan(argY))\n";
    3345           0 :         ss << "                 continue;\n";
    3346             : #endif
    3347           0 :         ss << "            fSumDeltaXDeltaY += (argX-fMeanX)*(argY-fMeanY);\n";
    3348           0 :         ss << "            fSumSqrDeltaX += (argX-fMeanX) * (argX-fMeanX);\n";
    3349           0 :         ss << "        }\n";
    3350           0 :         ss << "        if(fSumSqrDeltaX == 0.0)\n";
    3351           0 :         ss << "            return DBL_MAX;\n";
    3352           0 :         ss << "        else\n";
    3353           0 :         ss << "        {\n";
    3354           0 :         ss << "            return fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0);\n";
    3355           0 :         ss << "        }\n";
    3356           0 :         ss << "    }\n";
    3357           0 :         ss << "}\n";
    3358             :     }
    3359             :     else
    3360             :     {
    3361           0 :         ss << "    return DBL_MAX;\n";
    3362           0 :         ss << "}\n";
    3363             :     }
    3364             : }
    3365           0 : void OpSTEYX::GenSlidingWindowFunction(std::stringstream &ss,
    3366             :             const std::string &sSymName, SubArguments &vSubArguments)
    3367             : {
    3368           0 :     ss << "\ndouble " << sSymName;
    3369           0 :     ss << "_" << BinFuncName() << "(";
    3370           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3371             :     {
    3372           0 :     if (i)
    3373           0 :             ss << ",";
    3374           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3375             :     }
    3376           0 :     ss << "){\n";
    3377           0 :     ss << "    int gid0 = get_global_id(0);\n";
    3378           0 :     ss << "    double fSumX = 0.0;\n";
    3379           0 :     ss << "    double fSumY = 0.0;\n";
    3380           0 :     ss << "    double fMeanX = 0.0;\n";
    3381           0 :     ss << "    double fMeanY = 0.0;\n";
    3382           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    3383           0 :     ss << "    double fSumSqrDeltaX = 0.0;\n";
    3384           0 :     ss << "    double fSumSqrDeltaY = 0.0;\n";
    3385           0 :     ss << "    double fCount = 0.0;\n";
    3386           0 :     ss << "    double argX = 0.0;\n";
    3387           0 :     ss << "    double argY = 0.0;\n";
    3388           0 :     FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
    3389           0 :     FormulaToken *pCur1 = vSubArguments[0]->GetFormulaToken();
    3390             :     assert(pCur);
    3391             :     assert(pCur1);
    3392           0 :     if (pCur->GetType() == formula::svDoubleVectorRef&&
    3393           0 :         pCur1->GetType() == formula::svDoubleVectorRef)
    3394             :     {
    3395             :         const formula::DoubleVectorRefToken* pDVR =
    3396           0 :             static_cast<const formula::DoubleVectorRefToken *>(pCur);
    3397             :         const formula::DoubleVectorRefToken* pDVR1 =
    3398           0 :             static_cast<const formula::DoubleVectorRefToken *>(pCur1);
    3399           0 :         size_t nCurWindowSize = pDVR->GetRefRowSize();
    3400           0 :         size_t nCurWindowSize1 = pDVR1->GetRefRowSize();
    3401           0 :         size_t arrayLength = pDVR->GetArrayLength()<
    3402           0 :                pDVR1->GetArrayLength() ? pDVR->GetArrayLength():
    3403           0 :                     pDVR1->GetArrayLength();
    3404           0 :         if(nCurWindowSize != nCurWindowSize1)
    3405             :         {
    3406           0 :             ss << "    return DBL_MAX;\n";
    3407           0 :             ss << "}\n";
    3408           0 :             return ;
    3409             :         }
    3410           0 :         ss << "    for (int i = ";
    3411           0 :         if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3412           0 :             &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    3413             :         {
    3414             : #ifdef  ISNAN
    3415           0 :             ss << "gid0; i < " << arrayLength;
    3416           0 :             ss << " && i < " << nCurWindowSize  << "; i++)\n";
    3417           0 :             ss << "    {\n";
    3418             : #else
    3419             :             ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    3420             :             ss << "    {\n";
    3421             : #endif
    3422             :         }
    3423           0 :         else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3424           0 :             &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3425             :         {
    3426             : #ifdef  ISNAN
    3427           0 :             ss << "0; i < " << arrayLength;
    3428           0 :             ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    3429           0 :             ss << "    {\n";
    3430             : #else
    3431             :             ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    3432             :             ss << "    {\n";
    3433             : #endif
    3434             :         }
    3435           0 :         else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3436           0 :             &&(!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3437             :         {
    3438             : #ifdef  ISNAN
    3439           0 :             ss << "0; i + gid0 < " << arrayLength;
    3440           0 :             ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    3441           0 :             ss << "    {\n";
    3442             : #else
    3443             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3444             :             ss << "    {\n";
    3445             : #endif
    3446             :         }
    3447           0 :         else if ((pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3448           0 :             &&(pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    3449             :         {
    3450             : #ifdef  ISNAN
    3451           0 :             ss << "0; i < " << arrayLength << "; i++)\n";
    3452           0 :             ss << "    {\n";
    3453             : #else
    3454             :             ss << "0; i < " << arrayLength << "; i++)\n";
    3455             :             ss << "    {\n";
    3456             : #endif
    3457             :         }
    3458             :         else
    3459             :         {
    3460             : #ifdef  ISNAN
    3461           0 :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3462           0 :             ss << "    {\n";
    3463             : #else
    3464             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3465             :             ss << "    {\n";
    3466             : #endif
    3467           0 :             ss << "        break;\n";
    3468           0 :             ss << "    }";
    3469           0 :             ss << "    return DBL_MAX;\n";
    3470           0 :             ss << "}\n";
    3471           0 :             return ;
    3472             :         }
    3473             : 
    3474           0 :         ss << "        argX = ";
    3475           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    3476           0 :         ss << "        argY = ";
    3477           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    3478             : #ifdef  ISNAN
    3479           0 :         ss << "        if (isNan(argX) || isNan(argY))\n";
    3480           0 :         ss << "            continue;\n";
    3481             : #endif
    3482           0 :         ss << "        fSumX += argX;\n";
    3483           0 :         ss << "        fSumY += argY;\n";
    3484           0 :         ss << "        fCount += 1.0;\n";
    3485           0 :         ss << "    }\n";
    3486             : 
    3487           0 :         ss << "    if (fCount < 3.0)\n";
    3488           0 :         ss << "        return DBL_MAX;\n";
    3489           0 :         ss << "    else\n";
    3490           0 :         ss << "    {\n";
    3491           0 :         ss << "        fMeanX = fSumX * pow(fCount,-1.0);\n";
    3492           0 :         ss << "        fMeanY = fSumY * pow(fCount,-1.0);\n";
    3493             : 
    3494           0 :         ss << "        for (int i = ";
    3495           0 :         if ((!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    3496           0 :             &&(!pDVR1->IsStartFixed() && pDVR1->IsEndFixed()))
    3497             :         {
    3498             : #ifdef  ISNAN
    3499           0 :             ss << "gid0; i < " << arrayLength;
    3500           0 :             ss << " && i < " << nCurWindowSize  << "; i++)\n";
    3501           0 :             ss << "        {\n";
    3502             : #else
    3503             :             ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    3504             :             ss << "        {\n";
    3505             : #endif
    3506             :         }
    3507           0 :         else if ((pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    3508           0 :             &&(pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3509             :         {
    3510             : #ifdef  ISNAN
    3511           0 :             ss << "0; i < " << arrayLength ;
    3512           0 :             ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    3513           0 :             ss << "        {\n";
    3514             : #else
    3515             :             ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    3516             :             ss << "        {\n";
    3517             : #endif
    3518             :         }
    3519           0 :         else if ((!pDVR->IsStartFixed() && !pDVR->IsEndFixed())&&
    3520           0 :             (!pDVR1->IsStartFixed() && !pDVR1->IsEndFixed()))
    3521             :         {
    3522             : #ifdef  ISNAN
    3523           0 :             ss << "0; i + gid0 < " << arrayLength;
    3524           0 :             ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    3525           0 :             ss << "        {\n";
    3526             : #else
    3527             :             ss << "0; i < " << nCurWindowSize << "; i++)\n";
    3528             :             ss << "        {\n";
    3529             : #endif
    3530             :         }
    3531             :         else
    3532             :         {
    3533             : #ifdef  ISNAN
    3534           0 :             ss << "0; i < " << arrayLength << "; i++)\n";
    3535           0 :             ss << "        {\n";
    3536             : #else
    3537             :             ss << "0; i < " << arrayLength << "; i++)\n";
    3538             :             ss << "        {\n";
    3539             : #endif
    3540             :         }
    3541             : 
    3542           0 :         ss << "            argX = ";
    3543           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    3544           0 :         ss << "            argY = ";
    3545           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    3546             : #ifdef  ISNAN
    3547           0 :         ss << "            if (isNan(argX)||isNan(argY))\n";
    3548           0 :         ss << "                continue;\n";
    3549             : #endif
    3550           0 :         ss << "            fSumDeltaXDeltaY +=(argX-fMeanX)*(argY-fMeanY);\n";
    3551           0 :         ss << "            fSumSqrDeltaX += (argX-fMeanX)*(argX-fMeanX);\n";
    3552           0 :         ss << "            fSumSqrDeltaY += (argY-fMeanY)*(argY-fMeanY);\n";
    3553           0 :         ss << "        }\n";
    3554           0 :         ss << "        if(fSumSqrDeltaX == 0.0)\n";
    3555           0 :         ss << "            return DBL_MAX;\n";
    3556           0 :         ss << "        else\n";
    3557           0 :         ss << "        {\n";
    3558           0 :         ss << "            return sqrt((fSumSqrDeltaY - fSumDeltaXDeltaY * \n";
    3559           0 :         ss << "                   fSumDeltaXDeltaY*pow(fSumSqrDeltaX,-1.0))\n";
    3560           0 :         ss << "                   *pow(fCount - 2.0,-1.0));\n";
    3561           0 :         ss << "        }\n";
    3562           0 :         ss << "    }\n";
    3563           0 :         ss << "}\n";
    3564             :     }
    3565             :     else
    3566             :     {
    3567           0 :         ss << "    return DBL_MAX;\n";
    3568           0 :         ss << "}\n";
    3569             :     }
    3570             : }
    3571           0 : void OpFisher::GenSlidingWindowFunction(
    3572             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    3573             : vSubArguments)
    3574             : {
    3575           0 :     ss << "\ndouble " << sSymName;
    3576           0 :     ss << "_"<< BinFuncName() <<"(";
    3577           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3578             :     {
    3579           0 :         if (i)
    3580           0 :             ss << ",";
    3581           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3582             :     }
    3583           0 :     ss << ") {\n";
    3584           0 :     ss <<"    int gid0=get_global_id(0);\n";
    3585           0 :     ss <<"    double arg0;\n";
    3586           0 :     if(vSubArguments.size() != 1)
    3587             :     {
    3588           0 :         ss << "    return DBL_MAX;\n";
    3589           0 :         return ;
    3590             :     }
    3591           0 :     FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
    3592             :     assert(pCur);
    3593           0 :     if (pCur->GetType() == formula::svDoubleVectorRef)
    3594             :     {
    3595           0 :         ss << "    return DBL_MAX;\n";
    3596           0 :         return ;
    3597             :     }
    3598           0 :     else if (pCur->GetType() == formula::svSingleVectorRef)
    3599             :     {
    3600             :         const formula::SingleVectorRefToken* pSVR =
    3601           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    3602           0 :         ss << "    arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    3603           0 :         ss << ";\n";
    3604             : #ifdef ISNAN
    3605           0 :         ss<< "    if(isNan(arg0)||(gid0>=";
    3606           0 :         ss<<pSVR->GetArrayLength();
    3607           0 :         ss<<"))\n";
    3608           0 :         ss<<"        arg0 = 0;\n";
    3609             : #endif
    3610             :     }
    3611           0 :     else if (pCur->GetType() == formula::svDouble)
    3612             :     {
    3613           0 :         ss << "    arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    3614           0 :         ss << ";\n";
    3615             : #ifdef ISNAN
    3616           0 :         ss << "    if(isNan(arg0))\n";
    3617           0 :         ss << "        return DBL_MAX;\n";
    3618             : #endif
    3619             :     }
    3620           0 :     ss << "    if (fabs(arg0) >= 1.0)\n";
    3621           0 :     ss << "        return DBL_MAX;\n";
    3622           0 :     ss << "    double tmp=0.5*log((1+arg0)*pow((1-arg0),-1));\n";
    3623           0 :     ss << "    return tmp;\n";
    3624           0 :     ss << "}\n";
    3625             : }
    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 :     ss << "\ndouble " << sSymName;
    3681           0 :     ss << "_"<< BinFuncName() <<"(";
    3682           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3683             :     {
    3684           0 :         if (i)
    3685           0 :             ss << ",";
    3686           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3687             :     }
    3688           0 :     ss << ") {\n\t";
    3689           0 :     ss << "double vSum = 0.0;\n\t";
    3690           0 :     ss << "double vXSum = 0.0;\n\t";
    3691           0 :     ss << "double vYSum = 0.0;\n\t";
    3692           0 :     ss << "double vXMean = 0.0;\n\t";
    3693           0 :     ss << "double vYMean = 0.0;\n\t";
    3694             : 
    3695           0 :     ss << "int gid0 = get_global_id(0);\n\t";
    3696           0 :     ss << "double arg0 = 0.0;\n\t";
    3697           0 :     ss << "double arg1 = 0.0;\n\t";
    3698           0 :     ss << "int cnt = 0;\n\t";
    3699             : 
    3700           0 :     FormulaToken *pCurX = vSubArguments[0]->GetFormulaToken();
    3701           0 :     FormulaToken *pCurY = vSubArguments[1]->GetFormulaToken();
    3702             :     const formula::DoubleVectorRefToken* pCurDVRX =
    3703           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCurX);
    3704             :     const formula::DoubleVectorRefToken* pCurDVRY =
    3705           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCurY);
    3706           0 :     size_t nCurWindowSizeX = pCurDVRX->GetRefRowSize();
    3707           0 :     size_t nCurWindowSizeY = pCurDVRY->GetRefRowSize();
    3708           0 :     if(nCurWindowSizeX == nCurWindowSizeY)
    3709             :     {
    3710           0 :         ss << "for (int i = ";
    3711           0 :         if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    3712           0 :             ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
    3713           0 :             ss << "arg0 = " << vSubArguments[0]
    3714           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t";
    3715           0 :             ss << "arg1 = " << vSubArguments[1]
    3716           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t";
    3717             : #ifdef ISNAN
    3718           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3719           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3720           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
    3721           0 :             ss << "arg0 = 0.0;\n\t\t\t";
    3722           0 :             ss << "arg1 = 0.0;\n\t\t\t";
    3723           0 :             ss << "--cnt;\n\t\t";
    3724           0 :             ss << "}\n\t\t";
    3725             : #endif
    3726           0 :             ss << "++cnt;\n\t\t";
    3727           0 :             ss << "vXSum += arg0;\n\t\t";
    3728           0 :             ss << "vYSum += arg1;\n\t";
    3729           0 :             ss << "}\n\t";
    3730           0 :         } else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
    3731           0 :             ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n\t\t";
    3732           0 :             ss << "arg0 = " << vSubArguments[0]
    3733           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t";
    3734           0 :             ss << "arg1 = " << vSubArguments[1]
    3735           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t";
    3736             : #ifdef ISNAN
    3737           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3738           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3739           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
    3740           0 :             ss << "arg0 = 0.0;\n\t\t\t";
    3741           0 :             ss << "arg1 = 0.0;\n\t\t\t";
    3742           0 :             ss << "--cnt;\n\t\t";
    3743           0 :             ss << "}\n\t\t";
    3744             : #endif
    3745           0 :             ss << "++cnt;\n\t\t";
    3746           0 :             ss << "vXSum += arg0;\n\t\t";
    3747           0 :             ss << "vYSum += arg1;\n\t";
    3748           0 :             ss << "}\n\t";
    3749             :         }
    3750           0 :         else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    3751           0 :             ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
    3752           0 :             ss << "arg0 = " << vSubArguments[0]
    3753           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t";
    3754           0 :             ss << "arg1 = " << vSubArguments[1]
    3755           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t";
    3756             : #ifdef ISNAN
    3757           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3758           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3759           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
    3760           0 :             ss << "arg0 = 0.0;\n\t\t\t";
    3761           0 :             ss << "arg1 = 0.0;\n\t\t\t";
    3762           0 :             ss << "--cnt;\n\t\t";
    3763           0 :             ss << "}\n\t\t";
    3764             : #endif
    3765           0 :             ss << "++cnt;\n\t\t";
    3766           0 :             ss << "vXSum += arg0;\n\t\t";
    3767           0 :             ss << "vYSum += arg1;\n\t";
    3768           0 :             ss << "}\n\t";
    3769             :         } else {
    3770           0 :             ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t";
    3771           0 :             ss << "arg0 = " << vSubArguments[0]
    3772           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t";
    3773           0 :             ss << "arg1 = " << vSubArguments[1]
    3774           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t";
    3775             : #ifdef ISNAN
    3776           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i + gid0 >= ";
    3777           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >=";
    3778           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t";
    3779           0 :             ss << "arg0 = 0.0;\n\t\t\t";
    3780           0 :             ss << "arg1 = 0.0;\n\t\t\t";
    3781           0 :             ss << "--cnt;\n\t\t";
    3782           0 :             ss << "}\n\t\t";
    3783             : #endif
    3784           0 :             ss << "++cnt;\n\t\t";
    3785           0 :             ss << "vXSum += arg0;\n\t\t";
    3786           0 :             ss << "vYSum += arg1;\n\t";
    3787           0 :             ss << "}\n\t";
    3788             :         }
    3789             : 
    3790           0 :         ss << "if(cnt < 1) {\n\t\t";
    3791           0 :         ss << "return DBL_MIN;\n\t";
    3792           0 :         ss << "}\n\t";
    3793           0 :         ss << "else {\n\t\t";
    3794           0 :         ss << "vXMean = vXSum/cnt;\n\t\t";
    3795           0 :         ss << "vYMean = vYSum/cnt;\n\t\t";
    3796           0 :         ss << "vXSum = 0.0;\n\t\t";
    3797           0 :         ss << "vYSum = 0.0;\n\t\t";
    3798             : 
    3799           0 :         ss << "for (int i = ";
    3800           0 :         if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    3801           0 :             ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
    3802           0 :             ss << "arg0 = " << vSubArguments[0]
    3803           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t\t";
    3804           0 :             ss << "arg1 = " << vSubArguments[1]
    3805           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t\t";
    3806             : #ifdef ISNAN
    3807           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3808           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3809           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
    3810           0 :             ss << "arg0 = vXMean;\n\t\t\t\t";
    3811           0 :             ss << "arg1 = vYMean;\n\t\t\t";
    3812           0 :             ss << "}\n\t\t\t";
    3813             : #endif
    3814           0 :             ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
    3815           0 :             ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
    3816           0 :             ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
    3817           0 :             ss << "}\n\t\t";
    3818           0 :         } else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
    3819           0 :             ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n\t\t\t";
    3820           0 :             ss << "arg0 = " << vSubArguments[0]
    3821           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t\t";
    3822           0 :             ss << "arg1 = " << vSubArguments[1]
    3823           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t\t";
    3824             : #ifdef ISNAN
    3825           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3826           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3827           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
    3828           0 :             ss << "arg0 = vXMean;\n\t\t\t\t";
    3829           0 :             ss << "arg1 = vYMean;\n\t\t\t";
    3830           0 :             ss << "}\n\t\t\t";
    3831             : #endif
    3832           0 :             ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
    3833           0 :             ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
    3834           0 :             ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
    3835           0 :             ss << "}\n\t\t";
    3836           0 :         } else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    3837           0 :             ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
    3838           0 :             ss << "arg0 = " << vSubArguments[0]
    3839           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t\t";
    3840           0 :             ss << "arg1 = " << vSubArguments[1]
    3841           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t\t";
    3842             : #ifdef ISNAN
    3843           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i >= ";
    3844           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    3845           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
    3846           0 :             ss << "arg0 = vXMean;\n\t\t\t\t";
    3847           0 :             ss << "arg1 = vYMean;\n\t\t\t";
    3848           0 :             ss << "}\n\t\t\t";
    3849             : #endif
    3850           0 :             ss << "vXSum += pow(arg0 - vXMean, 2);\n\t\t\t";
    3851           0 :             ss << "vYSum += pow(arg1 - vYMean, 2);\n\t\t\t";
    3852           0 :             ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
    3853           0 :             ss << "}\n\t\t";
    3854             :         } else {
    3855           0 :             ss << "0; i < " << nCurWindowSizeX << "; i++) {\n\t\t\t";
    3856           0 :             ss << "arg0 = " << vSubArguments[0]
    3857           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t\t";
    3858           0 :             ss << "arg1 = " << vSubArguments[1]
    3859           0 :                 ->GenSlidingWindowDeclRef() << ";\n\t\t\t";
    3860             : #ifdef ISNAN
    3861           0 :             ss << "if(isNan(arg0) || isNan(arg1) || (i + gid0 >= ";
    3862           0 :             ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >=";
    3863           0 :             ss << pCurDVRY->GetArrayLength() << ")) {\n\t\t\t\t";
    3864           0 :             ss << "arg0 = vXMean;\n\t\t\t\t";
    3865           0 :             ss << "arg1 = vYMean;\n\t\t\t";
    3866           0 :             ss << "}\n\t\t\t";
    3867             : #endif
    3868           0 :             ss << "vXSum += ((arg0 - vXMean)*(arg0 - vXMean));\n\t\t\t";
    3869           0 :             ss << "vYSum += ((arg1 - vYMean)*(arg1 - vYMean));\n\t\t\t";
    3870           0 :             ss << "vSum += (arg0 - vXMean)*(arg1 - vYMean);\n\t\t";
    3871           0 :             ss << "}\n\t\t";
    3872             :         }
    3873             : 
    3874           0 :         ss << "if(vXSum == 0.0 || vYSum == 0.0) {\n\t\t\t";
    3875           0 :         ss << "return DBL_MIN;\n\t\t";
    3876           0 :         ss << "}\n\t\t";
    3877           0 :         ss << "else {\n\t\t\t";
    3878           0 :         ss << "return vSum/pow(vXSum*vYSum, 0.5);\n\t\t";
    3879           0 :         ss << "}\n\t";
    3880           0 :         ss << "}\n";
    3881           0 :         ss << "}";
    3882             :     }
    3883           0 : }
    3884             : 
    3885           0 : void OpNegbinomdist::GenSlidingWindowFunction(
    3886             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    3887             : {
    3888           0 :     ss << "\ndouble " << sSymName;
    3889           0 :     ss << "_"<< BinFuncName() <<"(";
    3890           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3891             :     {
    3892           0 :         if (i)
    3893           0 :             ss << ",";
    3894           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3895             :     }
    3896           0 :     ss << ")\n";
    3897           0 :     ss << "{\n\t";
    3898           0 :     ss << "double f,s,p,tmp0,tmp1,tmp2;\n";
    3899           0 :     ss << " int gid0=get_global_id(0);\n";
    3900           0 :     size_t i = vSubArguments.size();
    3901           0 :     ss <<"\n";
    3902           0 :     for (i = 0; i < vSubArguments.size(); i++)
    3903             :     {
    3904           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    3905             :         assert(pCur);
    3906           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    3907             :         {
    3908             : #ifdef  ISNAN
    3909             :             const formula::SingleVectorRefToken* pSVR =
    3910           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    3911           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    3912             : #endif
    3913             :         }
    3914           0 :         else if (pCur->GetType() == formula::svDouble)
    3915             :         {
    3916             : #ifdef  ISNAN
    3917           0 :             ss << "{\n";
    3918             : #endif
    3919             :         }
    3920             :         else
    3921             :         {
    3922             : #ifdef  ISNAN
    3923             : #endif
    3924             :         }
    3925             : #ifdef  ISNAN
    3926           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    3927             :         {
    3928           0 :             ss << "    if (isNan(";
    3929           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    3930           0 :             ss << "))\n";
    3931           0 :             ss << "        tmp"<<i<<"= 0;\n";
    3932           0 :             ss << "    else\n";
    3933           0 :             ss << "        tmp"<<i<<"=\n";
    3934           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    3935           0 :             ss << ";\n}\n";
    3936             :         }
    3937             :         else
    3938             :         {
    3939           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    3940           0 :             ss <<";\n";
    3941             :         }
    3942             : #endif
    3943             :     }
    3944           0 :     ss << " p = tmp2;\n";
    3945           0 :     ss << " s = tmp1;\n";
    3946           0 :     ss << " f = tmp0;\n";
    3947           0 :     ss << " double q = 1.0 - p;\n\t";
    3948           0 :     ss << " double fFactor = pow(p,s);\n\t";
    3949           0 :     ss << " for(int i=0; i<f; i++)\n\t";
    3950           0 :     ss << " {\n\t";
    3951           0 :     ss << "  fFactor *= ((double)i+s)*pow(((double)i+1.0),-1.0)/pow(q,-1);\n";
    3952           0 :     ss << " }\n\t";
    3953           0 :     ss << " double temp=fFactor;\n\t";
    3954           0 :     ss << " return temp;\n";
    3955           0 :     ss << "}\n";
    3956           0 : }
    3957             : 
    3958           0 : void OpPearson::GenSlidingWindowFunction(
    3959             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    3960             : {
    3961           0 :     FormulaToken* pCur = vSubArguments[1]->GetFormulaToken();
    3962             :     assert(pCur);
    3963             :     const formula::DoubleVectorRefToken* pCurDVR =
    3964           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur);
    3965           0 :     size_t CurWindowSize = pCurDVR->GetRefRowSize();
    3966             : 
    3967           0 :     ss << "\ndouble " << sSymName;
    3968           0 :     ss << "_"<< BinFuncName() <<"(";
    3969           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    3970             :     {
    3971           0 :         if (i)
    3972           0 :             ss << ",";
    3973           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    3974             :     }
    3975           0 :     ss << ")\n";
    3976           0 :     ss << "{\n";
    3977           0 :     ss << "    int gid0=get_global_id(0);\n";
    3978           0 :     ss << "    double fCount = 0.0;\n";
    3979           0 :     ss << "    double fSumX = 0.0;\n";
    3980           0 :     ss << "    double fSumY = 0.0;\n";
    3981           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    3982           0 :     ss << "    double fInx;\n";
    3983           0 :     ss << "    double fIny;\n";
    3984           0 :     size_t i = vSubArguments.size();
    3985           0 :     ss <<"\n";
    3986           0 :     for (i = 0; i < vSubArguments.size(); i++)
    3987             :     {
    3988           0 :         pCur = vSubArguments[i]->GetFormulaToken();
    3989             :         assert(pCur);
    3990           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    3991             :         {
    3992             :             const formula::DoubleVectorRefToken* pDVR =
    3993           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    3994           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    3995           0 :             ss << "for (int i = ";
    3996           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    3997             : #ifdef  ISNAN
    3998           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    3999           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    4000             : #else
    4001             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    4002             : #endif
    4003           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    4004             : #ifdef  ISNAN
    4005           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    4006           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    4007             : #else
    4008             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    4009             : #endif
    4010           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    4011             : #ifdef  ISNAN
    4012           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    4013           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    4014             : #else
    4015             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    4016             : #endif
    4017             :             }
    4018             :             else {
    4019             : #ifdef  ISNAN
    4020           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    4021             : #else
    4022             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    4023             : #endif
    4024             :             }
    4025             :         }
    4026           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    4027             :         {
    4028             : #ifdef  ISNAN
    4029             :             const formula::SingleVectorRefToken* pSVR =
    4030           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    4031           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4032             : #endif
    4033             :         }
    4034           0 :         else if (pCur->GetType() == formula::svDouble)
    4035             :         {
    4036             : #ifdef  ISNAN
    4037           0 :             ss << "{\n";
    4038             : #endif
    4039             :         }
    4040           0 :     ss << "\n}\n";
    4041             :     }
    4042             : #ifdef ISNAN
    4043           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    4044             :     const formula::DoubleVectorRefToken*tmpCurDVR0= static_cast<const
    4045           0 :     formula::DoubleVectorRefToken *>(tmpCur0);
    4046             : 
    4047           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    4048             :     const formula::DoubleVectorRefToken*tmpCurDVR1= static_cast<const
    4049           0 :     formula::DoubleVectorRefToken *>(tmpCur1);
    4050             : 
    4051           0 :     ss << "    int buffer_fInx_len = ";
    4052           0 :     ss << tmpCurDVR0->GetArrayLength();
    4053           0 :     ss << ";\n";
    4054             : 
    4055           0 :     ss << "    int buffer_fIny_len = ";
    4056           0 :     ss << tmpCurDVR1->GetArrayLength();
    4057           0 :     ss << ";\n";
    4058             : #endif
    4059           0 :     ss << "    for(int i=0; i<"<<CurWindowSize<<"; i++)\n";
    4060           0 :     ss << "    {\n";
    4061             : #ifdef ISNAN
    4062           0 :     ss <<"        if((gid0+i)>=buffer_fInx_len || isNan(";
    4063           0 :     ss << vSubArguments[0]->GenSlidingWindowDeclRef();
    4064           0 :     ss <<"))\n";
    4065           0 :     ss <<"            fInx = 0;\n";
    4066           0 :     ss <<"        else\n";
    4067             : #endif
    4068           0 :     ss << "          fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
    4069           0 :     ss << ";\n";
    4070             : #ifdef ISNAN
    4071           0 :     ss <<"        if((gid0+i)>=buffer_fIny_len || isNan(";
    4072           0 :     ss << vSubArguments[1]->GenSlidingWindowDeclRef();
    4073           0 :     ss <<"))\n";
    4074           0 :     ss <<"            fIny = 0;\n";
    4075           0 :     ss <<"        else\n";
    4076             : #endif
    4077           0 :     ss << "          fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
    4078           0 :     ss << "  ;\n";
    4079           0 :     ss << "       fSumX += fInx;\n";
    4080           0 :     ss << "       fSumY += fIny;\n";
    4081           0 :     ss << "       fCount = fCount + 1;\n";
    4082           0 :     ss << "     }\n";
    4083           0 :     ss << "       double fMeanX = fSumX / fCount;\n";
    4084           0 :     ss << "       double fMeanY = fSumY / fCount;\n";
    4085           0 :     ss << "       fSumX = 0.0;\n";
    4086           0 :     ss << "       fSumY = 0.0;\n";
    4087           0 :     ss << "       for(int i=0; i<"<<CurWindowSize<<"; i++)\n";
    4088           0 :     ss << "       {\n";
    4089           0 :     ss << "           fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
    4090           0 :     ss << " ;\n";
    4091           0 :     ss << "           fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
    4092           0 :     ss << " ;\n";
    4093           0 :     ss << "           fSumDeltaXDeltaY += (fInx - fMeanX) * (fIny - fMeanY);\n";
    4094           0 :     ss << "           fSumX += pow(fInx - fMeanX,2);\n";
    4095           0 :     ss << "           fSumY += pow(fIny - fMeanY,2);\n";
    4096           0 :     ss << "       }\n";
    4097           0 :     ss << "      double tmp = ( fSumDeltaXDeltaY / ";
    4098           0 :     ss << "sqrt( fSumX * fSumY));\n\t";
    4099           0 :     ss << "      return tmp;\n";
    4100           0 :     ss << "}\n";
    4101           0 : }
    4102             : 
    4103           0 : void OpGammaLn::GenSlidingWindowFunction(
    4104             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    4105             : {
    4106           0 :     FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
    4107             :     const formula::SingleVectorRefToken*tmpCurDVR= static_cast<const
    4108           0 : formula::SingleVectorRefToken *>(tmpCur);
    4109           0 :     ss << "\ndouble " << sSymName;
    4110           0 :     ss << "_"<< BinFuncName() <<"(";
    4111           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4112             :     {
    4113           0 :         if (i)
    4114           0 :             ss << ",";
    4115           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4116             :     }
    4117           0 :     ss << ") {\n\t";
    4118           0 :     ss <<"int gid0=get_global_id(0);\n\t";
    4119           0 :     ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    4120           0 :     ss << ";\n\t";
    4121             : #ifdef ISNAN
    4122           0 :     ss<< "if(isNan(arg0)||(gid0>=";
    4123           0 :     ss<<tmpCurDVR->GetArrayLength();
    4124           0 :     ss<<"))\n\t\t";
    4125           0 :     ss<<"arg0 = 0;\n\t";
    4126             : #endif
    4127           0 :     ss << "double tmp=lgamma(arg0);\n\t";
    4128           0 :     ss << "return tmp;\n";
    4129           0 :     ss << "}\n";
    4130           0 : }
    4131           0 : void OpGauss::BinInlineFun(std::set<std::string>& decls,
    4132             :     std::set<std::string>& funs)
    4133             : {
    4134           0 :     decls.insert(taylorDecl);decls.insert(phiDecl);
    4135           0 :     decls.insert(gaussDecl);
    4136           0 :     funs.insert(taylor);funs.insert(phi);
    4137           0 :     funs.insert(gauss);
    4138           0 : }
    4139             : 
    4140           0 : void OpGauss::GenSlidingWindowFunction(
    4141             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    4142             : vSubArguments)
    4143             : {
    4144           0 :     ss << "\ndouble " << sSymName;
    4145           0 :     ss << "_"<< BinFuncName() <<"(";
    4146           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4147             :     {
    4148           0 :         if (i)
    4149           0 :             ss << ",";
    4150           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4151             :     }
    4152           0 :     ss << ") {\n";
    4153           0 :     ss <<"    int gid0=get_global_id(0);\n";
    4154           0 :     ss <<"    double arg0;\n";
    4155           0 :     if(vSubArguments.size() != 1)
    4156             :     {
    4157           0 :         ss << "    return DBL_MAX;\n";
    4158           0 :         return ;
    4159             :     }
    4160           0 :     FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
    4161             :     assert(pCur);
    4162           0 :     if (pCur->GetType() == formula::svDoubleVectorRef)
    4163             :     {
    4164           0 :         ss << "    return DBL_MAX;\n";
    4165           0 :         return ;
    4166             :     }
    4167           0 :     else if (pCur->GetType() == formula::svSingleVectorRef)
    4168             :     {
    4169             :         const formula::SingleVectorRefToken* pSVR =
    4170           0 :             static_cast< const formula::SingleVectorRefToken* >(pCur);
    4171           0 :         ss << "    arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    4172           0 :         ss << ";\n";
    4173             : #ifdef ISNAN
    4174           0 :         ss<< "    if(isNan(arg0)||(gid0>=";
    4175           0 :         ss<<pSVR->GetArrayLength();
    4176           0 :         ss<<"))\n";
    4177           0 :         ss<<"        arg0 = 0;\n";
    4178             : #endif
    4179             :     }
    4180           0 :     else if (pCur->GetType() == formula::svDouble)
    4181             :     {
    4182           0 :         ss << "    arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
    4183           0 :         ss << ";\n";
    4184             : #ifdef ISNAN
    4185           0 :         ss << "    if(isNan(arg0))\n";
    4186           0 :         ss << "        return DBL_MAX;\n";
    4187             : #endif
    4188             :     }
    4189           0 :     ss << "    double tmp=gauss(arg0);\n";
    4190           0 :     ss << "    return tmp;\n";
    4191           0 :     ss << "}\n";
    4192             : }
    4193             : 
    4194           0 : void OpGeoMean::GenSlidingWindowFunction(
    4195             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    4196             : {
    4197           0 :     ss << "__kernel void ";
    4198           0 :     ss << "GeoMean_reduction(  ";
    4199           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4200             :     {
    4201           0 :         if (i)
    4202           0 :             ss << ",";
    4203           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4204             :     }
    4205           0 :     ss << ", __global double *result)\n";
    4206           0 :     ss << "{\n";
    4207           0 :     ss << "    double tmp =0;\n";
    4208           0 :     ss << "    int count = 0;\n";
    4209           0 :     ss << "    int i ;\n";
    4210           0 :     GenTmpVariables(ss,vSubArguments);
    4211           0 :     ss << "    double current_sum = 0.0;\n";
    4212           0 :     ss << "    int windowSize;\n";
    4213           0 :     ss << "    int arrayLength;\n";
    4214           0 :     ss << "    int current_count = 0;\n";
    4215           0 :     ss << "    int writePos = get_group_id(1);\n";
    4216           0 :     ss << "    int lidx = get_local_id(0);\n";
    4217           0 :     ss << "    __local double shm_buf[256];\n";
    4218           0 :     ss << "    __local int count_buf[256];\n";
    4219           0 :     ss << "    int loop;\n";
    4220           0 :     ss << "    int offset;\n";
    4221           0 :     ss << "    barrier(CLK_LOCAL_MEM_FENCE);\n";
    4222             : 
    4223           0 :     for(unsigned i=0;i<vSubArguments.size();i++)
    4224             :     {
    4225             :         assert(vSubArguments[i]->GetFormulaToken());
    4226             : 
    4227           0 :         if(vSubArguments[i]->GetFormulaToken()->GetType() ==
    4228             :         formula::svDoubleVectorRef)
    4229             :         {
    4230           0 :             FormulaToken *tmpCur = vSubArguments[i]->GetFormulaToken();
    4231             :                 const formula::DoubleVectorRefToken*pCurDVR= static_cast<const
    4232           0 :                      formula::DoubleVectorRefToken *>(tmpCur);
    4233           0 :                 size_t nCurWindowSize = pCurDVR->GetArrayLength() <
    4234           0 :                 pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
    4235           0 :                 pCurDVR->GetRefRowSize() ;
    4236             : 
    4237           0 :             if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
    4238           0 :                 ss << "    offset = 0;\n";
    4239           0 :             else if (!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
    4240           0 :                 ss << "    offset = get_group_id(1);\n";
    4241             :             else
    4242           0 :                 throw Unhandled();
    4243           0 :             ss << "    windowSize = ";
    4244           0 :             ss << nCurWindowSize;
    4245           0 :             ss << ";\n";
    4246           0 :             ss << "    arrayLength = ";
    4247           0 :             ss << pCurDVR->GetArrayLength();
    4248           0 :             ss << ";\n";
    4249           0 :             ss << "    loop = arrayLength/512 + 1;\n";
    4250           0 :             ss << "    for (int l=0; l<loop; l++){\n";
    4251           0 :             ss << "        tmp = 0.0;\n";
    4252           0 :             ss << "        count = 0;\n";
    4253           0 :             ss << "        int loopOffset = l*512;\n";
    4254           0 :             ss << "        int p1 = loopOffset + lidx + offset, p2 = p1 + 256;\n";
    4255           0 :             ss << "        if (p2 < min(offset + windowSize, arrayLength)) {\n";
    4256           0 :             ss << "            tmp0 = 0.0;\n";
    4257           0 :             std::string p1 = "p1";
    4258           0 :             std::string p2 = "p2";
    4259             : 
    4260           0 :             ss << "        tmp0 =";
    4261           0 :             vSubArguments[i]->GenDeclRef(ss);
    4262           0 :             ss << "["<<p1.c_str()<<"];\n";
    4263           0 :             ss << "        if(!isNan(tmp0))\n";
    4264           0 :             ss << "       {\n";
    4265           0 :             ss << "           tmp += log(tmp0);\n";
    4266           0 :             ss << "           count++;\n";
    4267           0 :             ss << "       }\n";
    4268             : 
    4269           0 :             ss << "        tmp0 =";
    4270           0 :             vSubArguments[i]->GenDeclRef(ss);
    4271           0 :             ss << "["<<p2.c_str()<<"];\n";
    4272           0 :             ss << "        if(!isNan(tmp0))\n";
    4273           0 :             ss << "       {\n";
    4274           0 :             ss << "           tmp += log(tmp0);\n";
    4275           0 :             ss << "           count++;\n";
    4276           0 :             ss << "       }\n";
    4277             : 
    4278           0 :             ss << "        }\n";
    4279           0 :             ss << "        else if (p1 < min(arrayLength, offset + windowSize)) {\n";
    4280             : 
    4281           0 :             ss << "        tmp0 =";
    4282           0 :             vSubArguments[i]->GenDeclRef(ss);
    4283           0 :             ss << "["<<p1.c_str()<<"];\n";
    4284           0 :             ss << "        if(!isNan(tmp0))\n";
    4285           0 :             ss << "        {\n";
    4286           0 :             ss << "            tmp += log(tmp0);\n";
    4287           0 :             ss << "            count++;\n";
    4288           0 :             ss << "        }\n";
    4289             : 
    4290           0 :             ss << "        }\n";
    4291           0 :             ss << "        shm_buf[lidx] = tmp;\n";
    4292           0 :             ss << "        count_buf[lidx] = count;\n";
    4293           0 :             ss << "        barrier(CLK_LOCAL_MEM_FENCE);\n";
    4294             : 
    4295           0 :             ss << "        for (int i = 128; i >0; i/=2) {\n";
    4296           0 :             ss << "            if (lidx < i)\n";
    4297           0 :             ss << "            {\n";
    4298           0 :             ss << "                shm_buf[lidx] += shm_buf[lidx + i];\n";
    4299           0 :             ss << "                count_buf[lidx] += count_buf[lidx + i];\n";
    4300           0 :             ss << "            }\n";
    4301           0 :             ss << "            barrier(CLK_LOCAL_MEM_FENCE);\n";
    4302           0 :             ss << "        }\n";
    4303           0 :             ss << "        if (lidx == 0)\n";
    4304           0 :             ss << "        {\n";
    4305           0 :             ss << "            current_sum += shm_buf[0];\n";
    4306           0 :             ss << "            current_count += count_buf[0];\n";
    4307           0 :             ss << "        }\n";
    4308             :              //  ss << "if(writePos == 14 && lidx ==0)\n";
    4309             :             //ss <<"printf(\"\\n********************sum is  is %f, count is%d\",current_sum,current_count);\n";
    4310           0 :             ss << "        barrier(CLK_LOCAL_MEM_FENCE);\n";
    4311           0 :             ss << "    }\n";
    4312             :         }else
    4313             :         {
    4314           0 :             ss << "    if (lidx == 0)\n";
    4315           0 :             ss << "    {\n";
    4316           0 :             ss << "        tmp0 =";
    4317           0 :             if(vSubArguments[i]->GetFormulaToken()->GetType() ==
    4318             :      formula::svSingleVectorRef)
    4319             :             {
    4320           0 :                 vSubArguments[i]->GenDeclRef(ss);
    4321           0 :                 ss << "[writePos];\n";
    4322             :             }
    4323             :             else
    4324             :             {
    4325           0 :                 vSubArguments[i]->GenDeclRef(ss);
    4326           0 :                 ss <<";\n";
    4327             :                 //ss <<"printf(\"\\n********************tmp0 is %f\",tmp0);\n";
    4328             :             }
    4329           0 :             ss << "        if(!isNan(tmp0))\n";
    4330           0 :             ss << "       {\n";
    4331           0 :             ss << "           current_sum += log(tmp0);\n";
    4332           0 :             ss << "           current_count++;\n";
    4333           0 :             ss << "       }\n";
    4334           0 :             ss << "    }\n";
    4335             :         }
    4336             :     }
    4337             : 
    4338           0 :     ss << "    if (lidx == 0)\n";
    4339           0 :     ss << "        result[writePos] = exp(current_sum/current_count);\n";
    4340           0 :     ss << "}\n";
    4341             : 
    4342           0 :     ss << "\ndouble " << sSymName;
    4343           0 :     ss << "_"<< BinFuncName() <<"(";
    4344           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4345             :     {
    4346           0 :         if (i)
    4347           0 :             ss << ",";
    4348           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4349             :     }
    4350           0 :     ss << ")\n    {\n";
    4351           0 :     ss <<"    int gid0=get_global_id(0);\n";
    4352           0 :     ss << "    double tmp =0;\n";
    4353           0 :     ss << "    tmp =";
    4354           0 :     vSubArguments[0]->GenDeclRef(ss);
    4355           0 :     ss << "[gid0];\n";
    4356           0 :     ss << "    return tmp;\n";
    4357           0 :     ss << "}";
    4358           0 : }
    4359             : 
    4360           0 : void OpHarMean::GenSlidingWindowFunction(
    4361             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    4362             : vSubArguments)
    4363             : {
    4364             : 
    4365           0 :     ss << "\ndouble " << sSymName;
    4366           0 :     ss << "_"<< BinFuncName() <<"( ";
    4367           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4368             :     {
    4369           0 :         if (i)
    4370           0 :             ss << ",";
    4371           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4372             :     }
    4373           0 :     ss << ")\n";
    4374           0 :     ss <<"{\n";
    4375           0 :     ss << "    int gid0 = get_global_id(0);\n";
    4376           0 :     ss << "    double nVal=0.0;\n";
    4377           0 :     ss << "    double tmp = 0;\n";
    4378           0 :     ss << "    int length;\n";
    4379           0 :     ss << "    int totallength=0;\n";
    4380           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4381             :     {
    4382           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4383             :         assert(pCur);
    4384           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    4385             :         {
    4386             :             const formula::DoubleVectorRefToken* pDVR =
    4387           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    4388           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    4389           0 :             ss << "    length="<<nCurWindowSize;
    4390           0 :             ss << ";\n";
    4391           0 :             ss << "    for (int i = ";
    4392           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    4393           0 :             ss << "    {\n";
    4394           0 :             ss << "        double arg"<<i<<" = ";
    4395           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4396           0 :             ss << ";\n";
    4397             : #ifdef ISNAN
    4398           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
    4399           0 :             ss << pDVR->GetArrayLength();
    4400           0 :             ss << "))\n";
    4401           0 :             ss << "        {\n";
    4402           0 :             ss << "            length--;\n";
    4403           0 :             ss << "            continue;\n";
    4404           0 :             ss << "        }\n";
    4405             : #endif
    4406           0 :             ss << "        nVal += (1.0 *pow(";
    4407           0 :             ss << " arg"<<i<<",-1));\n";
    4408           0 :             ss << "    }\n";
    4409           0 :             ss << "    totallength +=length;\n";
    4410             :         }
    4411           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    4412             :         {
    4413           0 :             ss << "    tmp = ";
    4414           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4415           0 :             ss << ";\n";
    4416             : #ifdef ISNAN
    4417           0 :             ss << "    if(!isNan(tmp))\n";
    4418           0 :             ss << "    {\n";
    4419           0 :             ss << "        nVal += (1.0 * pow( tmp,-1));\n";
    4420           0 :             ss << "        totallength +=1;\n";
    4421           0 :             ss << "    }\n";
    4422             : #endif
    4423             :         }
    4424           0 :         else if (pCur->GetType() == formula::svDouble)
    4425             :         {
    4426           0 :            ss << "    tmp = ";
    4427           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4428           0 :            ss << ";\n";
    4429           0 :            ss << "    nVal += (1.0 *pow( tmp,-1));\n";
    4430           0 :            ss << "    totallength +=1;\n";
    4431             :         }
    4432             :         else
    4433             :         {
    4434           0 :             ss << "    return DBL_MIN;\n";
    4435             :         }
    4436             :     }
    4437           0 :     ss << "    tmp = totallength*pow(nVal,-1);\n";
    4438           0 :     ss << "    return tmp;\n";
    4439           0 :     ss << "}";
    4440           0 : }
    4441             : 
    4442           0 : void OpConfidence::BinInlineFun(std::set<std::string>& decls,
    4443             :     std::set<std::string>& funs)
    4444             : {
    4445           0 :     decls.insert(gaussinvDecl);
    4446           0 :     funs.insert(gaussinv);
    4447           0 : }
    4448             : 
    4449           0 : void OpConfidence::GenSlidingWindowFunction(std::stringstream& ss,
    4450             :     const std::string &sSymName, SubArguments& vSubArguments)
    4451             : {
    4452           0 :     ss << "\ndouble " << sSymName;
    4453           0 :     ss << "_"<< BinFuncName() <<"(";
    4454           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4455             :     {
    4456           0 :         if (i)
    4457           0 :             ss << ",";
    4458           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4459             :     }
    4460           0 :     ss << ") {\n";
    4461           0 :     ss << "    double tmp = " << GetBottom() <<";\n";
    4462           0 :     ss << "    int gid0 = get_global_id(0);\n";
    4463           0 :     ss << "    double alpha = " << GetBottom() <<";\n";
    4464           0 :     ss << "    double sigma = " << GetBottom() <<";\n";
    4465           0 :     ss << "    double size = " << GetBottom() <<";\n";
    4466           0 :     ss << "    double tmp0,tmp1,tmp2;\n";
    4467           0 :     size_t i = vSubArguments.size();
    4468           0 :     ss <<"\n";
    4469           0 :     for (i = 0; i < vSubArguments.size(); i++)
    4470             :     {
    4471           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4472             :         assert(pCur);
    4473           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    4474             :         {
    4475             : #ifdef  ISNAN
    4476             :             const formula::SingleVectorRefToken* pSVR =
    4477           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    4478           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4479             : #endif
    4480             :         }
    4481           0 :         else if (pCur->GetType() == formula::svDouble)
    4482             :         {
    4483             : #ifdef  ISNAN
    4484           0 :             ss << "{\n";
    4485             : #endif
    4486             :         }
    4487             :         else
    4488             :         {
    4489             : #ifdef  ISNAN
    4490             : #endif
    4491             :         }
    4492             : #ifdef  ISNAN
    4493           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    4494             :         {
    4495           0 :             ss << "    if (isNan(";
    4496           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4497           0 :             ss << "))\n";
    4498           0 :             ss << "        tmp"<<i<<"= 0;\n";
    4499           0 :             ss << "    else\n";
    4500           0 :             ss << "        tmp"<<i<<"=\n";
    4501           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4502           0 :             ss << ";\n}\n";
    4503             :         }
    4504             :         else
    4505             :         {
    4506           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4507           0 :             ss <<";\n";
    4508             :         }
    4509             : #endif
    4510             :     }
    4511           0 :     ss << "    alpha = tmp0;\n";
    4512           0 :     ss << "    sigma = tmp1;\n";
    4513           0 :     ss << "    size = tmp2;\n";
    4514           0 :     ss << "    double rn = floor(size);\n";
    4515           0 :     ss << "    if(sigma <= 0.0 || alpha <= 0.0 || alpha >= 1.0";
    4516           0 :     ss << "|| rn < 1.0)\n";
    4517           0 :     ss << "        tmp = -DBL_MAX;\n";
    4518           0 :     ss << "    else\n";
    4519           0 :     ss << "        tmp = gaussinv(1.0 - alpha * pow(2.0,-1.0)) * sigma ";
    4520           0 :     ss << "* pow(sqrt( rn ),-1);\n";
    4521           0 :     ss << "    return tmp;\n";
    4522           0 :     ss << "}";
    4523           0 : }
    4524             : 
    4525             : 
    4526           0 : void OpCritBinom::BinInlineFun(std::set<std::string>& decls,
    4527             :     std::set<std::string>& funs)
    4528             : {
    4529           0 :     decls.insert(MinDecl);
    4530           0 :     funs.insert("");
    4531           0 : }
    4532             : 
    4533           0 : void OpCritBinom::GenSlidingWindowFunction(std::stringstream& ss,
    4534             :     const std::string &sSymName, SubArguments& vSubArguments)
    4535             : {
    4536           0 :     ss << "\ndouble " << sSymName;
    4537           0 :     ss << "_"<< BinFuncName() <<"(";
    4538           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4539             :     {
    4540           0 :         if (i)
    4541           0 :             ss << ",";
    4542           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4543             :     }
    4544           0 :     ss << ") {\n";
    4545           0 :     ss << "    double tmp = " << GetBottom() <<";\n";
    4546           0 :     ss << "    int gid0 = get_global_id(0);\n";
    4547           0 :     ss << "    double n = " << GetBottom() <<";\n";
    4548           0 :     ss << "    double p = " << GetBottom() <<";\n";
    4549           0 :     ss << "    double alpha = " << GetBottom() <<";\n";
    4550           0 :     ss << "    double tmp0 = 0.0,tmp1 = 0.0,tmp2 = 0.0;\n";
    4551           0 :     size_t i = vSubArguments.size();
    4552           0 :     ss <<"\n";
    4553           0 :     for (i = 0; i < vSubArguments.size(); i++)
    4554             :     {
    4555           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4556             :         assert(pCur);
    4557           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    4558             :         {
    4559             : #ifdef  ISNAN
    4560             :             const formula::SingleVectorRefToken* pSVR =
    4561           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    4562           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4563             : #endif
    4564             :         }
    4565           0 :         else if (pCur->GetType() == formula::svDouble)
    4566             :         {
    4567             : #ifdef  ISNAN
    4568           0 :             ss << "{\n";
    4569             : #endif
    4570             :         }
    4571             :         else
    4572             :         {
    4573             : #ifdef  ISNAN
    4574             : #endif
    4575             :         }
    4576             : #ifdef  ISNAN
    4577           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    4578             :         {
    4579           0 :             ss << "    if (isNan(";
    4580           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4581           0 :             ss << "))\n";
    4582           0 :             ss << "        tmp"<<i<<"= 0;\n";
    4583           0 :             ss << "    else\n";
    4584           0 :             ss << "        tmp"<<i<<"=\n";
    4585           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4586           0 :             ss << ";\n}\n";
    4587             :         }
    4588             :         else
    4589             :         {
    4590           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4591           0 :             ss <<";\n";
    4592             :         }
    4593             : #endif
    4594             :     }
    4595           0 :     ss << "    n = tmp0;\n";
    4596           0 :     ss << "    p = tmp1;\n";
    4597           0 :     ss << "    alpha = tmp2;\n";
    4598           0 :     ss << "    double rn = floor(n);\n";
    4599           0 :     ss << "    if (rn < 0.0 || alpha <= 0.0 || alpha >= 1.0 || p < 0.0";
    4600           0 :     ss << " || p > 1.0)\n";
    4601           0 :     ss << "        tmp = -DBL_MIN;\n";
    4602           0 :     ss << "    else\n";
    4603           0 :     ss << "    {\n";
    4604           0 :     ss << "        double rq = (0.5 - p) + 0.5;\n";
    4605           0 :     ss << "        double fFactor = pow(rq, rn);\n";
    4606           0 :     ss << "        if (fFactor <= Min)\n";
    4607           0 :     ss << "        {\n";
    4608           0 :     ss << "            fFactor = pow(p, rn);\n";
    4609           0 :     ss << "            if (fFactor <= Min)\n";
    4610           0 :     ss << "                tmp = -DBL_MAX;\n";
    4611           0 :     ss << "            else\n";
    4612           0 :     ss << "            {\n";
    4613           0 :     ss << "                double fSum = 1.0 - fFactor;\n";
    4614           0 :     ss << "                uint max =(uint)(rn), i;\n";
    4615           0 :     ss << "                for (i = 0; i < max && fSum >= alpha; i++)\n";
    4616           0 :     ss << "                {\n";
    4617           0 :     ss << " fFactor *= (rn - i) * pow((double)(i + 1),-1.0) *";
    4618           0 :     ss << " rq * pow(p, -1.0);\n";
    4619           0 :     ss << "                    fSum -= fFactor;\n";
    4620           0 :     ss << "                }\n";
    4621           0 :     ss << "                tmp = (rn - i);\n";
    4622           0 :     ss << "            }\n";
    4623           0 :     ss << "        }\n";
    4624           0 :     ss << "        else\n";
    4625           0 :     ss << "        {\n";
    4626           0 :     ss << "            double fSum = fFactor;\n";
    4627           0 :     ss << "            uint max = (uint)(rn), i;\n";
    4628           0 :     ss << "            for (i = 0; i < max && fSum < alpha; i++)\n";
    4629           0 :     ss << "            {\n";
    4630           0 :     ss << " fFactor *= (rn - i) * pow((double)(i + 1), -1.0) *";
    4631           0 :     ss << " p * pow(rq, -1.0);\n";
    4632           0 :     ss << "                fSum += fFactor;\n";
    4633           0 :     ss << "            }\n";
    4634           0 :     ss << "            tmp = (i);\n";
    4635           0 :     ss << "        }\n";
    4636           0 :     ss << "    }\n";
    4637           0 :     ss << "    return tmp;\n";
    4638           0 :     ss << "}";
    4639           0 : }
    4640             : 
    4641             : 
    4642           0 : void OpRsq::GenSlidingWindowFunction(
    4643             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    4644             : {
    4645           0 :     FormulaToken* pCur = vSubArguments[1]->GetFormulaToken();
    4646             :     assert(pCur);
    4647             :     const formula::DoubleVectorRefToken* pCurDVR =
    4648           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur);
    4649           0 :     size_t nCurWindowSize = pCurDVR->GetRefRowSize();
    4650             : 
    4651           0 :     ss << "\ndouble " << sSymName;
    4652           0 :     ss << "_"<< BinFuncName() <<"(";
    4653           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4654             :     {
    4655           0 :         if (i)
    4656           0 :             ss << ",";
    4657           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4658             :     }
    4659           0 :     ss << ")\n";
    4660           0 :     ss << "{\n";
    4661           0 :     ss << "    int gid0=get_global_id(0);\n";
    4662           0 :     ss << "    double fCount = 0.0;\n";
    4663           0 :     ss << "    double fSumX = 0.0;\n";
    4664           0 :     ss << "    double fSumY = 0.0;\n";
    4665           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    4666           0 :     ss << "    double fInx;\n";
    4667           0 :     ss << "    double fIny;\n";
    4668           0 :     ss << "    double tmp0,tmp1;\n";
    4669           0 :     size_t i = vSubArguments.size();
    4670           0 :     ss <<"\n";
    4671           0 :     for (i = 0; i < vSubArguments.size(); i++)
    4672             :     {
    4673           0 :         pCur = vSubArguments[i]->GetFormulaToken();
    4674             :         assert(pCur);
    4675           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    4676             :         {
    4677             :             const formula::DoubleVectorRefToken* pDVR =
    4678           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    4679           0 :             nCurWindowSize = pDVR->GetRefRowSize();
    4680           0 :             ss << "for (int i = ";
    4681           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    4682             : #ifdef  ISNAN
    4683           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    4684           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    4685             : #else
    4686             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    4687             : #endif
    4688           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    4689             : #ifdef  ISNAN
    4690           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    4691           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    4692             : #else
    4693             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    4694             : #endif
    4695           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    4696             : #ifdef  ISNAN
    4697           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    4698           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    4699             : #else
    4700             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    4701             : #endif
    4702             :             }
    4703             :             else {
    4704             : #ifdef  ISNAN
    4705           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    4706             : #else
    4707             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    4708             : #endif
    4709             :             }
    4710             :         }
    4711           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    4712             :         {
    4713             : #ifdef  ISNAN
    4714             :             const formula::SingleVectorRefToken* pSVR =
    4715           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    4716           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4717             : #endif
    4718             :         }
    4719           0 :         else if (pCur->GetType() == formula::svDouble)
    4720             :         {
    4721             : #ifdef  ISNAN
    4722           0 :             ss << "{\n";
    4723             : #endif
    4724             :         }
    4725           0 :     ss << "\n}\n";
    4726             :     }
    4727             : #ifdef ISNAN
    4728           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    4729             :     const formula::DoubleVectorRefToken*tmpCurDVR0= static_cast<const
    4730           0 :     formula::DoubleVectorRefToken *>(tmpCur0);
    4731           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    4732             :     const formula::DoubleVectorRefToken*tmpCurDVR1= static_cast<const
    4733           0 :     formula::DoubleVectorRefToken *>(tmpCur1);
    4734           0 :     ss << "    int buffer_fInx_len = ";
    4735           0 :     ss << tmpCurDVR0->GetArrayLength();
    4736           0 :     ss << ";\n";
    4737           0 :     ss << "    int buffer_fIny_len = ";
    4738           0 :     ss << tmpCurDVR1->GetArrayLength();
    4739           0 :     ss << ";\n";
    4740             : #endif
    4741           0 :     ss << "   for(int i=0; i<"<<nCurWindowSize<<"; i++)\n";
    4742           0 :     ss << "   {\n";
    4743             : #ifdef ISNAN
    4744           0 :     ss << "     if((gid0+i)>=buffer_fInx_len || isNan(";
    4745           0 :     ss << vSubArguments[0]->GenSlidingWindowDeclRef();
    4746           0 :     ss << "))\n";
    4747           0 :     ss << "         fInx = 0;\n";
    4748           0 :     ss << "     else\n";
    4749             : #endif
    4750           0 :     ss << "        fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
    4751           0 :     ss << ";\n";
    4752             : #ifdef ISNAN
    4753           0 :     ss << "      if((gid0+i)>=buffer_fIny_len || isNan(";
    4754           0 :     ss << vSubArguments[1]->GenSlidingWindowDeclRef();
    4755           0 :     ss << "))\n";
    4756           0 :     ss << "          fIny = 0;\n";
    4757           0 :     ss << "      else\n";
    4758             : #endif
    4759           0 :     ss << "        fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
    4760           0 :     ss << " ;\n";
    4761           0 :     ss << "      fSumX += fInx;\n";
    4762           0 :     ss << "      fSumY += fIny;\n";
    4763           0 :     ss << "      fCount = fCount + 1;\n";
    4764           0 :     ss << "    }\n";
    4765           0 :     ss << "    double fMeanX = fSumX / fCount;\n";
    4766           0 :     ss << "    double fMeanY = fSumY / fCount;\n";
    4767           0 :     ss << "    fSumX = 0.0;\n";
    4768           0 :     ss << "    fSumY = 0.0;\n";
    4769           0 :     ss << "    for(int i=0; i<"<<nCurWindowSize<<"; i++)\n";
    4770           0 :     ss << "    {\n";
    4771           0 :     ss << "        fInx = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
    4772           0 :     ss << " ;\n";
    4773           0 :     ss << "        fIny = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
    4774           0 :     ss << " ;\n";
    4775           0 :     ss << "        fSumDeltaXDeltaY += (fInx - fMeanX) * (fIny - fMeanY);\n";
    4776           0 :     ss << "        fSumX    += pow(fInx - fMeanX,2);\n";
    4777           0 :     ss << "        fSumY    += pow(fIny - fMeanY,2);\n";
    4778           0 :     ss << "    }\n";
    4779           0 :     ss << "    double tmp = pow( fSumDeltaXDeltaY,2) / (fSumX * fSumY);\n";
    4780           0 :     ss << "    return tmp ;\n";
    4781           0 :     ss << "}\n";
    4782           0 : }
    4783             : 
    4784           0 : void OpChiInv::BinInlineFun(std::set<std::string>& decls,
    4785             :     std::set<std::string>& funs)
    4786             : {
    4787           0 :     decls.insert(fMachEpsDecl);
    4788           0 :     funs.insert("");
    4789           0 :     decls.insert(fBigInvDecl);
    4790           0 :     funs.insert("");
    4791           0 :     decls.insert(fHalfMachEpsDecl);
    4792           0 :     funs.insert("");
    4793           0 :     decls.insert(lcl_IterateInverseChiInvDecl);
    4794           0 :     funs.insert(lcl_IterateInverseChiInv);
    4795           0 :     decls.insert(GetChiDistDecl);
    4796           0 :     funs.insert(GetChiDist);
    4797           0 :     decls.insert(lcl_HasChangeOfSignDecl);
    4798           0 :     funs.insert(lcl_HasChangeOfSign);
    4799           0 :     decls.insert(GetUpRegIGammaDecl);
    4800           0 :     funs.insert(GetUpRegIGamma);
    4801           0 :     decls.insert(GetGammaContFractionDecl);
    4802           0 :     funs.insert(GetGammaContFraction);
    4803           0 :     decls.insert(GetGammaSeriesDecl);
    4804           0 :     funs.insert(GetGammaSeries);
    4805           0 : }
    4806           0 : void OpChiInv::GenSlidingWindowFunction(
    4807             :     std::stringstream &ss,const std::string &sSymName,
    4808             :     SubArguments &vSubArguments)
    4809             : {
    4810           0 :     ss << "\ndouble " << sSymName;
    4811           0 :     ss << "_"<< BinFuncName() <<"(";
    4812           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4813             :     {
    4814           0 :         if (i)
    4815           0 :             ss << ",";
    4816           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4817             :     }
    4818           0 :     ss << ")\n";
    4819           0 :     ss << "{\n";
    4820           0 :     ss << "    double tmp0,tmp1,tmp;\n";
    4821           0 :     ss << "    int gid0=get_global_id(0);\n";
    4822           0 :     size_t i = vSubArguments.size();
    4823           0 :     size_t nItems = 0;
    4824           0 :     ss <<"\n    ";
    4825           0 :     for (i = 0; i < vSubArguments.size(); i++)
    4826             :     {
    4827           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4828             :         assert(pCur);
    4829           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    4830             :         {
    4831             :             const formula::DoubleVectorRefToken* pDVR =
    4832           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    4833           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    4834           0 :             ss << "for (int i = ";
    4835           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    4836             : #ifdef  ISNAN
    4837           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    4838           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    4839             : #else
    4840             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    4841             : #endif
    4842           0 :                 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    4843             : #ifdef  ISNAN
    4844           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    4845           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    4846             : #else
    4847             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    4848             : #endif
    4849           0 :                 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    4850             : #ifdef  ISNAN
    4851           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    4852           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    4853             : #else
    4854             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    4855             : #endif
    4856             :                 }
    4857             :                 else {
    4858             : #ifdef  ISNAN
    4859           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    4860             : #else
    4861             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    4862             : #endif
    4863             :                 }
    4864           0 :                 nItems += nCurWindowSize;
    4865             :             }
    4866           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    4867             :             {
    4868             : #ifdef  ISNAN
    4869             :                 const formula::SingleVectorRefToken* pSVR =
    4870           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    4871           0 :                 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4872             : #else
    4873             :                 nItems += 1;
    4874             : #endif
    4875             :             }
    4876           0 :             else if (pCur->GetType() == formula::svDouble)
    4877             :             {
    4878             : #ifdef  ISNAN
    4879           0 :                 ss << "{\n";
    4880             : #endif
    4881           0 :                 nItems += 1;
    4882             :             }
    4883             :             else
    4884             :             {
    4885             : #ifdef  ISNAN
    4886             : #endif
    4887           0 :                 nItems += 1;
    4888             :             }
    4889             : #ifdef  ISNAN
    4890           0 :             if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    4891             :             {
    4892           0 :                 ss << "if (isNan(";
    4893           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4894           0 :                 ss << "))\n";
    4895           0 :                 ss << "    tmp"<<i<<"= 0;\n";
    4896           0 :                 ss << "else\n";
    4897           0 :                 ss <<"tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4898           0 :                 ss << ";\n}\n";
    4899             :             }
    4900             :             else
    4901             :             {
    4902           0 :                ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4903           0 :                ss << ";\n";
    4904             :             }
    4905             : #else
    4906             :     ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4907             :     ss << ";\n";
    4908             : #endif
    4909             :             }
    4910           0 :     ss << "    tmp1 = floor(tmp1);";
    4911           0 :     ss << "    if (tmp1 < 1.0 || tmp0 <= 0.0 || tmp0 > 1.0 )\n";
    4912           0 :     ss << "    {\n";
    4913           0 :     ss << "        return DBL_MIN;\n";
    4914           0 :     ss << "    }\n";
    4915           0 :     ss << "    bool bConvError;\n";
    4916           0 :     ss << "    double fVal = lcl_IterateInverseChiInv";
    4917           0 :     ss << "(tmp0, tmp1, tmp1*0.5, tmp1, &bConvError);\n";
    4918           0 :     ss << "    if(bConvError)\n";
    4919           0 :     ss << "        return DBL_MIN;\n";
    4920           0 :     ss << "    return fVal;\n";
    4921           0 :     ss << "}\n";
    4922           0 : }
    4923           0 : void OpNormdist::GenSlidingWindowFunction(
    4924             :     std::stringstream &ss, const std::string &sSymName,
    4925             :     SubArguments &vSubArguments)
    4926             : {
    4927           0 :     ss << "\ndouble " << sSymName;
    4928           0 :     ss << "_"<< BinFuncName() <<"(";
    4929           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    4930             :     {
    4931           0 :         if (i)
    4932           0 :             ss << ",";
    4933           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    4934             :     }
    4935           0 :     ss << ")\n";
    4936           0 :     ss << "{\n";
    4937           0 :     ss << "    double x,mue,sigma,c;\n";
    4938           0 :     ss << "    int gid0=get_global_id(0);\n";
    4939           0 :     ss << "    double tmp0,tmp1,tmp2,tmp3;\n";
    4940           0 :     size_t i = vSubArguments.size();
    4941           0 :     ss <<"\n    ";
    4942           0 :     for (i = 0; i < vSubArguments.size(); i++)
    4943             :     {
    4944           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    4945             :         assert(pCur);
    4946           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    4947             :         {
    4948             : #ifdef  ISNAN
    4949             :             const formula::SingleVectorRefToken* pSVR =
    4950           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    4951           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    4952             : #endif
    4953             :         }
    4954           0 :         else if (pCur->GetType() == formula::svDouble)
    4955             :         {
    4956             : #ifdef  ISNAN
    4957           0 :             ss << "{\n";
    4958             : #endif
    4959             :         }
    4960             : #ifdef  ISNAN
    4961           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    4962             :         {
    4963           0 :             ss << "    if (isNan(";
    4964           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4965           0 :             ss << "))\n";
    4966           0 :             ss << "        tmp"<<i<<"= 0;\n";
    4967           0 :             ss << "    else\n";
    4968           0 :             ss << "        tmp"<<i<<"=\n";
    4969           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    4970           0 :             ss << ";\n}\n";
    4971             :         }
    4972             :         else
    4973             :         {
    4974           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    4975           0 :             ss <<";\n";
    4976             :         }
    4977             : #endif
    4978             :     }
    4979           0 :     ss << "x = tmp0;\n";
    4980           0 :     ss << "mue = tmp1;\n";
    4981           0 :     ss << "sigma = tmp2;\n";
    4982           0 :     ss << "c = tmp3;\n";
    4983           0 :     ss << "double mid,tmp;\n";
    4984           0 :     ss << "mid = (x - mue)/sigma;\n";
    4985           0 :     ss << "if(c)\n";
    4986           0 :     ss << "    tmp = 0.5 *erfc(-mid * 0.7071067811865475);\n";
    4987           0 :     ss << "else \n";
    4988           0 :     ss <<"     tmp=(0.39894228040143268*exp(-pow(mid,2)/2.0))/sigma;\n";
    4989           0 :     ss << "return tmp;\n";
    4990           0 :     ss << "}\n";
    4991           0 : }
    4992           0 : void OpNormsdist::BinInlineFun(std::set<std::string>& decls,
    4993             :     std::set<std::string>& funs)
    4994             : {
    4995           0 :     decls.insert(lcl_Erfc0600Decl);
    4996           0 :     funs.insert(lcl_Erfc0600);
    4997           0 :     decls.insert(lcl_Erfc2654Decl);
    4998           0 :     funs.insert(lcl_Erfc2654);
    4999           0 :     decls.insert(lcl_Erf0065Decl);
    5000           0 :     funs.insert(lcl_Erf0065);
    5001           0 :     decls.insert(rtl_math_erf_rdDecl);
    5002           0 :     funs.insert(rtl_math_erf_rd);
    5003           0 :     decls.insert(rtl_math_erfc_rdDecl);
    5004           0 :     funs.insert(rtl_math_erfc_rd);
    5005           0 : }
    5006             : 
    5007           0 : void OpNormsdist::GenSlidingWindowFunction(
    5008             :     std::stringstream &ss,const std::string &sSymName,
    5009             :     SubArguments &vSubArguments)
    5010             : {
    5011           0 :     ss << "\ndouble " << sSymName;
    5012           0 :     ss << "_"<< BinFuncName() <<"(";
    5013           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5014             :     {
    5015           0 :         if (i)
    5016           0 :             ss << ",";
    5017           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5018             :     }
    5019           0 :     ss << ")\n";
    5020           0 :     ss << "{\n";
    5021           0 :     ss << "    double x = 0,tmp0 = 0;\n";
    5022           0 :     ss << "    int gid0=get_global_id(0);\n";
    5023           0 :     size_t i = vSubArguments.size();
    5024           0 :     ss <<"\n    ";
    5025           0 :     for (i = 0; i < vSubArguments.size(); i++)
    5026             :     {
    5027           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5028             :         assert(pCur);
    5029           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    5030             :         {
    5031             : #ifdef  ISNAN
    5032             :             const formula::SingleVectorRefToken* pSVR =
    5033           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    5034           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    5035             : #endif
    5036             :         }
    5037           0 :         else if (pCur->GetType() == formula::svDouble)
    5038             :         {
    5039             : #ifdef  ISNAN
    5040           0 :             ss << "{\n";
    5041             : #endif
    5042             :         }
    5043             :         else
    5044             :         {
    5045             : #ifdef  ISNAN
    5046             : #endif
    5047             :         }
    5048             : #ifdef  ISNAN
    5049           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    5050             :         {
    5051           0 :             ss << "    if (isNan(";
    5052           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5053           0 :             ss << "))\n";
    5054           0 :             ss << "        tmp"<<i<<"= 0;\n";
    5055           0 :             ss << "    else\n";
    5056           0 :             ss << "        tmp"<<i<<"=\n";
    5057           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5058           0 :             ss << ";\n}\n";
    5059             :         }
    5060             :         else
    5061             :         {
    5062           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    5063           0 :             ss <<";\n";
    5064             :         }
    5065             : #endif
    5066             :     }
    5067           0 :     ss << "    x = tmp0;\n";
    5068           0 :     ss << "    double tmp = 0.5 * rtl_math_erfc_rd((-1)*x * 0.7071067811865475);\n";
    5069           0 :     ss << "    return tmp;\n";
    5070           0 :     ss << "}\n";
    5071           0 : }
    5072             : 
    5073           0 : void OpVariationen::GenSlidingWindowFunction(
    5074             :     std::stringstream &ss,const std::string &sSymName,
    5075             :     SubArguments &vSubArguments)
    5076             : {
    5077           0 :     ss << "\ndouble " << sSymName;
    5078           0 :     ss << "_"<< BinFuncName() <<"(";
    5079           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5080             :     {
    5081           0 :        if (i)
    5082           0 :          ss << ",";
    5083           0 :        vSubArguments[i]->GenSlidingWindowDecl(ss);
    5084             :      }
    5085           0 :     ss << ") {\n";
    5086           0 :     ss <<"    int gid0=get_global_id(0);\n";
    5087           0 :     ss <<"    double inA;\n";
    5088           0 :     ss <<"    double inB;\n";
    5089           0 :     ss <<"    double tmp0,tmp1;\n";
    5090           0 :     ss <<"    double tmp = 1 ;\n";
    5091           0 :     size_t i = vSubArguments.size();
    5092           0 :     ss <<"\n";
    5093           0 :     for (i = 0; i < vSubArguments.size(); i++)
    5094             :     {
    5095           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5096             :         assert(pCur);
    5097           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    5098             :         {
    5099             : #ifdef  ISNAN
    5100             :             const formula::SingleVectorRefToken* pSVR =
    5101           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    5102           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    5103             : 
    5104             : #endif
    5105             :         }
    5106           0 :         else if (pCur->GetType() == formula::svDouble)
    5107             :         {
    5108             : #ifdef  ISNAN
    5109           0 :             ss << "{\n";
    5110             : #endif
    5111             :         }
    5112             :         else
    5113             :         {
    5114             : #ifdef  ISNAN
    5115             : #endif
    5116             :         }
    5117             : #ifdef  ISNAN
    5118           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    5119             :         {
    5120           0 :             ss << "    if (isNan(";
    5121           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5122           0 :             ss << "))\n";
    5123           0 :             ss << "        tmp"<<i<<"= 0;\n";
    5124           0 :             ss << "    else\n";
    5125           0 :             ss << "        tmp"<<i<<"=\n";
    5126           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5127           0 :             ss << ";\n}\n";
    5128             :         }
    5129             :         else
    5130             :         {
    5131           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    5132           0 :             ss <<";\n";
    5133             :         }
    5134             : #endif
    5135             :     }
    5136           0 :     ss << "      inA = tmp0;\n";
    5137           0 :     ss << "      inB = tmp1;\n";
    5138           0 :     ss << "      for( int i =0; i<inB; i++)\n";
    5139           0 :     ss << "      {\n";
    5140           0 :     ss << "        tmp *= inA ;\n";
    5141           0 :     ss << "        inA = inA - 1.0;\n";
    5142           0 :     ss << "      }\n";
    5143           0 :     ss << "      return tmp;\n";
    5144           0 :     ss << "}\n";
    5145           0 : }
    5146           0 : void OpVariationen2::GenSlidingWindowFunction(
    5147             :     std::stringstream &ss,const std::string &sSymName,
    5148             :     SubArguments &vSubArguments)
    5149             : {
    5150           0 :     ss << "\ndouble " << sSymName;
    5151           0 :     ss << "_"<< BinFuncName() <<"(";
    5152           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5153             :     {
    5154           0 :        if (i)
    5155           0 :          ss << ",";
    5156           0 :        vSubArguments[i]->GenSlidingWindowDecl(ss);
    5157             :     }
    5158           0 :     ss << ") {\n";
    5159           0 :     ss <<"    int gid0=get_global_id(0);\n";
    5160           0 :     ss <<"    double inA;\n";
    5161           0 :     ss <<"    double inB;\n";
    5162           0 :     ss <<"    double tmp = 1.0;\n";
    5163             : #ifdef ISNAN
    5164           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    5165             :     const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
    5166           0 :     formula::SingleVectorRefToken *>(tmpCur0);
    5167           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    5168             :     const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
    5169           0 :     formula::SingleVectorRefToken *>(tmpCur1);
    5170           0 :     ss << "int buffer_fIna_len = ";
    5171           0 :     ss << tmpCurDVR0->GetArrayLength();
    5172           0 :     ss << ";\n";
    5173           0 :     ss << "    int buffer_fInb_len = ";
    5174           0 :     ss << tmpCurDVR1->GetArrayLength();
    5175           0 :     ss << ";\n";
    5176             : #endif
    5177             : #ifdef ISNAN
    5178           0 :     ss << "    if((gid0)>=buffer_fIna_len || isNan(";
    5179           0 :     ss << vSubArguments[0]->GenSlidingWindowDeclRef();
    5180           0 :     ss << "))\n";
    5181           0 :     ss << "    inA = 0;\nelse \n";
    5182             : #endif
    5183           0 :     ss << "        inA = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
    5184           0 :     ss << ";\n";
    5185             : #ifdef ISNAN
    5186           0 :     ss << "if((gid0)>=buffer_fInb_len || isNan(";
    5187           0 :     ss << vSubArguments[1]->GenSlidingWindowDeclRef();
    5188           0 :     ss << "))\n";
    5189           0 :     ss << "inB = 0;\nelse \n";
    5190             : #endif
    5191           0 :     ss << "    inB = "<<vSubArguments[1]->GenSlidingWindowDeclRef();
    5192           0 :     ss << ";\n";
    5193           0 :     ss << " for(int i=0; i<inB; i++)\n";
    5194           0 :     ss << " {\n";
    5195           0 :     ss << "     tmp *= inA;\n";
    5196           0 :     ss << " }\n";
    5197           0 :     ss << "    return tmp;\n";
    5198           0 :     ss << "}\n";
    5199           0 : }
    5200             : 
    5201             : 
    5202           0 : void OpPhi::GenSlidingWindowFunction(
    5203             :     std::stringstream &ss,const std::string &sSymName,
    5204             :     SubArguments &vSubArguments)
    5205             : {
    5206           0 :     ss << "\ndouble " << sSymName;
    5207           0 :     ss << "_"<< BinFuncName() <<"(";
    5208           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5209             :     {
    5210           0 :         if (i)
    5211           0 :             ss << ",";
    5212           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5213             :     }
    5214           0 :     ss << ")\n";
    5215           0 :     ss << "{\n";
    5216           0 :     ss << "    double x,tmp0;\n";
    5217           0 :     ss << "    int gid0=get_global_id(0);\n";
    5218           0 :     size_t i = vSubArguments.size();
    5219           0 :     ss <<"\n";
    5220           0 :     for (i = 0; i < vSubArguments.size(); i++)
    5221             :     {
    5222           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5223             :         assert(pCur);
    5224           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    5225             :         {
    5226             : #ifdef  ISNAN
    5227             :             const formula::SingleVectorRefToken* pSVR =
    5228           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    5229           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    5230             : #endif
    5231             :         }
    5232           0 :         else if (pCur->GetType() == formula::svDouble)
    5233             :         {
    5234             : #ifdef  ISNAN
    5235           0 :             ss << "{\n";
    5236             : #endif
    5237             :         }
    5238             :         else
    5239             :         {
    5240             : #ifdef  ISNAN
    5241             : #endif
    5242             :         }
    5243             : #ifdef  ISNAN
    5244           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    5245             :         {
    5246           0 :             ss << "    if (isNan(";
    5247           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5248           0 :             ss << "))\n";
    5249           0 :             ss << "        tmp"<<i<<"= 0;\n";
    5250           0 :             ss << "    else\n";
    5251           0 :             ss << "        tmp"<<i<<"=\n";
    5252           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5253           0 :             ss << ";\n}\n";
    5254             :         }
    5255             :         else
    5256             :         {
    5257           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    5258           0 :             ss <<";\n";
    5259             :         }
    5260             : #endif
    5261             :     }
    5262           0 :     ss << "    x = tmp0;\n";
    5263           0 :     ss << "    double tmp = 0.39894228040143268 * exp((-1)*pow(x,2) / 2.0);\n";
    5264           0 :     ss << "     return tmp;\n";
    5265           0 :     ss << "}\n";
    5266           0 : }
    5267             : 
    5268           0 : void OpNorminv::GenSlidingWindowFunction(
    5269             :     std::stringstream &ss,const std::string &sSymName,
    5270             :     SubArguments &vSubArguments)
    5271             : {
    5272           0 :     ss << "\ndouble " << sSymName;
    5273           0 :     ss << "_"<< BinFuncName() <<"(";
    5274           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5275             :     {
    5276           0 :         if (i)
    5277           0 :             ss << ",";
    5278           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5279             :     }
    5280           0 :     ss << ")\n";
    5281           0 :     ss << "{\n";
    5282           0 :     ss <<"    double q,t,z;\n";
    5283           0 :     ss <<"    double x,mue,sigma;\n";
    5284           0 :     ss <<"    double tmp0,tmp1,tmp2;\n";
    5285           0 :     ss <<"    int gid0=get_global_id(0);\n";
    5286           0 :     size_t i = vSubArguments.size();
    5287           0 :     ss <<"\n";
    5288           0 :     for (i = 0; i < vSubArguments.size(); i++)
    5289             :     {
    5290           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5291             :         assert(pCur);
    5292           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    5293             :         {
    5294             : #ifdef  ISNAN
    5295             :             const formula::SingleVectorRefToken* pSVR =
    5296           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    5297           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    5298             : #endif
    5299             :         }
    5300           0 :         else if (pCur->GetType() == formula::svDouble)
    5301             :         {
    5302             : #ifdef  ISNAN
    5303           0 :             ss << "{\n";
    5304             : #endif
    5305             :         }
    5306             :         else
    5307             :         {
    5308             : #ifdef  ISNAN
    5309             : #endif
    5310             :         }
    5311             : #ifdef  ISNAN
    5312           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    5313             :         {
    5314           0 :             ss << "    if (isNan(";
    5315           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5316           0 :             ss << "))\n";
    5317           0 :             ss << "        tmp"<<i<<"= 0;\n";
    5318           0 :             ss << "    else\n";
    5319           0 :             ss << "        tmp"<<i<<"=\n";
    5320           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5321           0 :             ss << ";\n}\n";
    5322             :         }
    5323             :         else
    5324             :         {
    5325           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    5326           0 :             ss <<";\n";
    5327             :         }
    5328             : #endif
    5329             :     }
    5330           0 :     ss <<"    x = tmp0;\n";
    5331           0 :     ss <<"    mue = tmp1;\n";
    5332           0 :     ss <<"    sigma = tmp2;\n";
    5333           0 :     ss <<"    q = x -0.5;\n";
    5334           0 :     ss <<"    if(fabs(q)<=.425)\n";
    5335           0 :     ss <<"   {\n";
    5336           0 :     ss <<"        t=0.180625-pow(q,2);\n";
    5337             :     ss <<"        z=\n"
    5338             :     "q*\n"
    5339             :     "(\n"
    5340             :             "(\n"
    5341             :                 "(\n"
    5342             :                     "(\n"
    5343             :                         "(\n"
    5344             :                             "(\n"
    5345             :                                 "(\n"
    5346           0 :                                     "t*2509.0809287301226727";
    5347             :     ss <<"+33430.575583588128105\n"
    5348             :                                 ")\n"
    5349             :                                 "*t+67265.770927008700853\n"
    5350             :                             ")\n"
    5351             :                             "*t+45921.953931549871457\n"
    5352             :                         ")\n"
    5353             :                         "*t+13731.693765509461125\n"
    5354             :                     ")\n"
    5355             :                     "*t+1971.5909503065514427\n"
    5356             :                 ")\n"
    5357             :                 "*t+133.14166789178437745\n"
    5358             :             ")\n"
    5359             :             "*t+3.387132872796366608\n"
    5360             :         ")\n"
    5361             :         "/\n"
    5362             :         "(\n"
    5363             :             "(\n"
    5364             :                 "(\n"
    5365             :                     "(\n"
    5366             :                         "(\n"
    5367             :                             "(\n"
    5368             :                                 "(\n"
    5369           0 :                                     "t*5226.495278852854561";
    5370             :     ss <<"+28729.085735721942674\n"
    5371             :                                 ")\n"
    5372             :                                 "*t+39307.89580009271061\n"
    5373             :                             ")\n"
    5374             :                             "*t+21213.794301586595867\n"
    5375             :                         ")\n"
    5376             :                         "*t+5394.1960214247511077\n"
    5377             :                     ")\n"
    5378             :                     "*t+687.1870074920579083\n"
    5379             :                 ")\n"
    5380             :                 "*t+42.313330701600911252\n"
    5381             :             ")\n"
    5382             :             "*t+1.0\n"
    5383           0 :     ");\n";
    5384           0 :     ss <<"}\nelse\n{\n";
    5385           0 :     ss <<" if(q>0)\nt=1-x;\n";
    5386           0 :     ss <<"else\nt=x;\n";
    5387           0 :     ss <<"t=sqrt(-log(t));\n";
    5388           0 :     ss <<"if(t<=5.0)\n{\n";
    5389           0 :     ss <<"t+=-1.6;\n";
    5390             :     ss <<"z=\n"
    5391             :             "(\n"
    5392             :                 "(\n"
    5393             :                     "(\n"
    5394             :                         "(\n"
    5395             :                             "(\n"
    5396             :                                "(\n"
    5397             :                                    "(\n"
    5398           0 :                                         "t*7.7454501427834140764e-4";
    5399             :     ss <<"+0.0227238449892691845833\n"
    5400             :                                     ")\n"
    5401             :                                     "*t+0.24178072517745061177\n"
    5402             :                                 ")\n"
    5403             :                                 "*t+1.27045825245236838258\n"
    5404             :                             ")\n"
    5405             :                             "*t+3.64784832476320460504\n"
    5406             :                         ")\n"
    5407             :                         "*t+5.7694972214606914055\n"
    5408             :                     ")\n"
    5409             :                     "*t+4.6303378461565452959\n"
    5410             :                 ")\n"
    5411             :                 "*t+1.42343711074968357734\n"
    5412             :             ")\n"
    5413             :             "/\n"
    5414             :             "(\n"
    5415             :                 "(\n"
    5416             :                     "(\n"
    5417             :                         "(\n"
    5418             :                             "(\n"
    5419             :                                 "(\n"
    5420             :                                     "(\n"
    5421           0 :                                         "t*1.05075007164441684324e-9";
    5422             :     ss <<"+5.475938084995344946e-4\n"
    5423             :                                     ")\n"
    5424             :                                     "*t+0.0151986665636164571966\n"
    5425             :                                 ")\n"
    5426             :                                 "*t+0.14810397642748007459\n"
    5427             :                             ")\n"
    5428             :                             "*t+0.68976733498510000455\n"
    5429             :                         ")\n"
    5430             :                         "*t+1.6763848301838038494\n"
    5431             :                     ")\n"
    5432             :                     "*t+2.05319162663775882187\n"
    5433             :                 ")\n"
    5434             :                 "*t+1.0\n"
    5435           0 :             ");\n}\n";
    5436           0 :     ss <<"else\n{\n";
    5437           0 :     ss <<"t+=-5.0;\n";
    5438             :     ss <<"z=\n"
    5439             :             "(\n"
    5440             :                 "(\n"
    5441             :                     "(\n"
    5442             :                         "(\n"
    5443             :                             "(\n"
    5444             :                                 "(\n"
    5445             :                                     "(\n"
    5446           0 :                                         "t*2.01033439929228813265e-7";
    5447             :     ss<<"+2.71155556874348757815e-5\n"
    5448             :                                     ")\n"
    5449             :                                     "*t+0.0012426609473880784386\n"
    5450             :                                 ")\n"
    5451             :                                 "*t+0.026532189526576123093\n"
    5452             :                             ")\n"
    5453             :                             "*t+0.29656057182850489123\n"
    5454             :                         ")\n"
    5455             :                         "*t+1.7848265399172913358\n"
    5456             :                     ")\n"
    5457             :                     "*t+5.4637849111641143699\n"
    5458             :                 ")\n"
    5459             :                 "*t+6.6579046435011037772\n"
    5460             :             ")\n"
    5461             :             "/\n"
    5462             :             "(\n"
    5463             :                 "(\n"
    5464             :                     "(\n"
    5465             :                         "(\n"
    5466             :                             "(\n"
    5467             :                                 "(\n"
    5468             :                                     "(\n"
    5469             :                                         "t*2.04426310338993978564e-15"
    5470             :           "+1.4215117583164458887e-7\n"
    5471             :                                     ")\n"
    5472             :                                     "*t+1.8463183175100546818e-5\n"
    5473             :                                 ")\n"
    5474             :                                 "*t+7.868691311456132591e-4\n"
    5475             :                             ")\n"
    5476             :                             "*t+0.0148753612908506148525\n"
    5477             :                         ")\n"
    5478             :                         "*t+0.13692988092273580531\n"
    5479             :                     ")\n"
    5480             :                     "*t+0.59983220655588793769\n"
    5481             :                 ")\n"
    5482             :                 "*t+1.0\n"
    5483           0 :             ");\n";
    5484           0 :     ss<<"}\n";
    5485           0 :     ss << "z = q < 0.0 ? (-1)*z : z;\n";
    5486           0 :     ss<<"}\n";
    5487           0 :     ss<<"double tmp =  z*sigma + mue;\n";
    5488           0 :     ss<<"return tmp;\n";
    5489           0 :     ss<<"}\n";
    5490           0 : }
    5491           0 : void OpNormsinv:: GenSlidingWindowFunction
    5492             :     (std::stringstream &ss,const std::string &sSymName,
    5493             :     SubArguments &vSubArguments)
    5494             : {
    5495           0 :     ss << "\ndouble " << sSymName;
    5496           0 :     ss << "_"<< BinFuncName() <<"(";
    5497           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5498             :     {
    5499           0 :         if (i)
    5500           0 :             ss << ",";
    5501           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5502             :     }
    5503           0 :     ss << ")\n";
    5504           0 :     ss << "{\n";
    5505           0 :     ss << "    double q,t,z,x,tmp0;\n";
    5506           0 :     ss << "    int gid0=get_global_id(0);\n";
    5507           0 :     size_t i = vSubArguments.size();
    5508           0 :     ss <<"\n";
    5509           0 :     for (i = 0; i < vSubArguments.size(); i++)
    5510             :     {
    5511           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5512             :         assert(pCur);
    5513           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    5514             :         {
    5515             : #ifdef  ISNAN
    5516             :             const formula::SingleVectorRefToken* pSVR =
    5517           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    5518           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    5519             : #endif
    5520             :         }
    5521           0 :         else if (pCur->GetType() == formula::svDouble)
    5522             :         {
    5523             : #ifdef  ISNAN
    5524           0 :             ss << "{\n";
    5525             : #endif
    5526             :         }
    5527             :         else
    5528             :         {
    5529             : #ifdef  ISNAN
    5530             : #endif
    5531             :         }
    5532             : #ifdef  ISNAN
    5533           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    5534             :         {
    5535           0 :             ss << "    if (isNan(";
    5536           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5537           0 :             ss << "))\n";
    5538           0 :             ss << "        tmp"<<i<<"= 0;\n";
    5539           0 :             ss << "    else\n";
    5540           0 :             ss << "        tmp"<<i<<"=\n";
    5541           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5542           0 :             ss << ";\n}\n";
    5543             :         }
    5544             :         else
    5545             :         {
    5546           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    5547           0 :             ss <<";\n";
    5548             :         }
    5549             : #endif
    5550             :     }
    5551           0 :     ss <<"    x = tmp0;\n";
    5552           0 :     ss <<"    q = x -0.5;\n";
    5553           0 :     ss <<"    if(fabs(q)<=.425)\n";
    5554           0 :     ss <<"    {\n";
    5555           0 :     ss <<"        t=0.180625-pow(q,2);\n";
    5556             :     ss <<"        z=\n"
    5557             :          "q*\n"
    5558             :          "(\n"
    5559             :             "(\n"
    5560             :                 "(\n"
    5561             :                     "(\n"
    5562             :                         "(\n"
    5563             :                             "(\n"
    5564             :                                 "(\n"
    5565           0 :                                     "t*2509.0809287301226727";
    5566             :     ss <<"+33430.575583588128105\n"
    5567             :                                 ")\n"
    5568             :                                 "*t+67265.770927008700853\n"
    5569             :                             ")\n"
    5570             :                             "*t+45921.953931549871457\n"
    5571             :                         ")\n"
    5572             :                         "*t+13731.693765509461125\n"
    5573             :                     ")\n"
    5574             :                     "*t+1971.5909503065514427\n"
    5575             :                 ")\n"
    5576             :                 "*t+133.14166789178437745\n"
    5577             :             ")\n"
    5578             :             "*t+3.387132872796366608\n"
    5579             :         ")\n"
    5580             :         "/\n"
    5581             :         "(\n"
    5582             :             "(\n"
    5583             :                 "(\n"
    5584             :                     "(\n"
    5585             :                         "(\n"
    5586             :                             "(\n"
    5587             :                                 "(\n"
    5588           0 :                                     "t*5226.495278852854561";
    5589             :     ss <<"+28729.085735721942674\n"
    5590             :                                 ")\n"
    5591             :                                 "*t+39307.89580009271061\n"
    5592             :                             ")\n"
    5593             :                             "*t+21213.794301586595867\n"
    5594             :                         ")\n"
    5595             :                         "*t+5394.1960214247511077\n"
    5596             :                     ")\n"
    5597             :                     "*t+687.1870074920579083\n"
    5598             :                 ")\n"
    5599             :                 "*t+42.313330701600911252\n"
    5600             :             ")\n"
    5601             :             "*t+1.0\n"
    5602           0 :          ");\n";
    5603           0 :     ss <<"}\nelse\n{\n";
    5604           0 :     ss <<" if(q>0)\nt=1-x;\n";
    5605           0 :     ss <<"else\nt=x;\n";
    5606           0 :     ss <<"t=sqrt(-log(t));\n";
    5607           0 :     ss <<"if(t<=5.0)\n{\n";
    5608           0 :     ss <<"t+=-1.6;\n";
    5609             :     ss <<"z=\n"
    5610             :             "(\n"
    5611             :                 "(\n"
    5612             :                     "(\n"
    5613             :                         "(\n"
    5614             :                             "(\n"
    5615             :                                "(\n"
    5616             :                                    "(\n"
    5617           0 :                                         "t*7.7454501427834140764e-4";
    5618             :     ss <<"+0.0227238449892691845833\n"
    5619             :                                     ")\n"
    5620             :                                     "*t+0.24178072517745061177\n"
    5621             :                                 ")\n"
    5622             :                                 "*t+1.27045825245236838258\n"
    5623             :                             ")\n"
    5624             :                             "*t+3.64784832476320460504\n"
    5625             :                         ")\n"
    5626             :                         "*t+5.7694972214606914055\n"
    5627             :                     ")\n"
    5628             :                     "*t+4.6303378461565452959\n"
    5629             :                 ")\n"
    5630             :                 "*t+1.42343711074968357734\n"
    5631             :             ")\n"
    5632             :             "/\n"
    5633             :             "(\n"
    5634             :                 "(\n"
    5635             :                     "(\n"
    5636             :                         "(\n"
    5637             :                             "(\n"
    5638             :                                 "(\n"
    5639             :                                     "(\n"
    5640           0 :                                         "t*1.05075007164441684324e-9";
    5641             :     ss <<"+5.475938084995344946e-4\n"
    5642             :                                     ")\n"
    5643             :                                     "*t+0.0151986665636164571966\n"
    5644             :                                 ")\n"
    5645             :                                 "*t+0.14810397642748007459\n"
    5646             :                             ")\n"
    5647             :                             "*t+0.68976733498510000455\n"
    5648             :                         ")\n"
    5649             :                         "*t+1.6763848301838038494\n"
    5650             :                     ")\n"
    5651             :                     "*t+2.05319162663775882187\n"
    5652             :                 ")\n"
    5653             :                 "*t+1.0\n"
    5654           0 :             ");\n}\n";
    5655           0 :     ss <<"else\n{\n";
    5656           0 :     ss <<"t+=-5.0;\n";
    5657             :     ss <<"z=\n"
    5658             :             "(\n"
    5659             :                 "(\n"
    5660             :                     "(\n"
    5661             :                         "(\n"
    5662             :                             "(\n"
    5663             :                                 "(\n"
    5664             :                                     "(\n"
    5665           0 :                                         "t*2.01033439929228813265e-7";
    5666             :     ss <<"+2.71155556874348757815e-5\n"
    5667             :                                     ")\n"
    5668             :                                     "*t+0.0012426609473880784386\n"
    5669             :                                 ")\n"
    5670             :                                 "*t+0.026532189526576123093\n"
    5671             :                             ")\n"
    5672             :                             "*t+0.29656057182850489123\n"
    5673             :                         ")\n"
    5674             :                         "*t+1.7848265399172913358\n"
    5675             :                     ")\n"
    5676             :                     "*t+5.4637849111641143699\n"
    5677             :                 ")\n"
    5678             :                 "*t+6.6579046435011037772\n"
    5679             :             ")\n"
    5680             :             "/\n"
    5681             :             "(\n"
    5682             :                 "(\n"
    5683             :                     "(\n"
    5684             :                         "(\n"
    5685             :                             "(\n"
    5686             :                                 "(\n"
    5687             :                                     "(\n"
    5688             :                                         "t*2.04426310338993978564e-15"
    5689             :           "+1.4215117583164458887e-7\n"
    5690             :                                     ")\n"
    5691             :                                     "*t+1.8463183175100546818e-5\n"
    5692             :                                 ")\n"
    5693             :                                 "*t+7.868691311456132591e-4\n"
    5694             :                             ")\n"
    5695             :                             "*t+0.0148753612908506148525\n"
    5696             :                         ")\n"
    5697             :                         "*t+0.13692988092273580531\n"
    5698             :                     ")\n"
    5699             :                     "*t+0.59983220655588793769\n"
    5700             :                 ")\n"
    5701             :                 "*t+1.0\n"
    5702           0 :             ");\n";
    5703           0 :     ss <<"}\n";
    5704           0 :     ss << "z = q < 0.0 ? (-1)*z : z;\n";
    5705           0 :     ss <<"}\n";
    5706           0 :     ss <<"double tmp = z;\n";
    5707           0 :     ss <<"return tmp;\n";
    5708           0 :     ss <<"}\n";
    5709           0 : }
    5710           0 : void OpMedian::GenSlidingWindowFunction(
    5711             :     std::stringstream &ss, const std::string &sSymName,
    5712             :     SubArguments &vSubArguments)
    5713             : {
    5714           0 :     ss << "\ndouble " << sSymName;
    5715           0 :     ss << "_"<< BinFuncName() <<"(";
    5716           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5717             :     {
    5718           0 :         if (i)
    5719           0 :             ss << ",";
    5720           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5721             :     }
    5722           0 :     ss << ") {\n";
    5723           0 :     ss << "    int gid0 = get_global_id(0);\n";
    5724           0 :     ss << "    double tmp = 0;\n";
    5725           0 :     ss << "    int i;\n";
    5726           0 :     ss << "    unsigned int startFlag = 0;\n";
    5727           0 :     ss << "    unsigned int endFlag = 0;\n";
    5728           0 :     ss << "    double dataIna;\n";
    5729           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5730             :     {
    5731           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5732             :         assert(pCur);
    5733           0 :         if (const formula::DoubleVectorRefToken* pCurDVR =
    5734           0 :             dynamic_cast<const formula::DoubleVectorRefToken *>(pCur))
    5735             :         {
    5736           0 :             size_t nCurWindowSize = pCurDVR->GetRefRowSize();
    5737           0 :             ss << "startFlag = ";
    5738           0 :             if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
    5739             :             {
    5740           0 :                 ss << "gid0; endFlag = "<< nCurWindowSize <<"-gid0;\n";
    5741             :             }
    5742           0 :             else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
    5743             :             {
    5744           0 :                 ss << "gid0; endFlag = gid0+"<< nCurWindowSize <<";\n";
    5745             :             }
    5746             :             else
    5747             :             {
    5748           0 :                 ss << "gid0; endFlag = gid0+"<< nCurWindowSize <<";\n";
    5749             :             }
    5750             :         }
    5751             :         else
    5752             :         {
    5753           0 :             ss<<"startFlag=gid0;endFlag=gid0;\n";
    5754             :         }
    5755             :     }
    5756             : #ifdef ISNAN
    5757           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    5758             :     const formula::DoubleVectorRefToken*tmpCurDVR0= static_cast<const
    5759           0 :     formula::DoubleVectorRefToken *>(tmpCur0);
    5760           0 :     ss << "int buffer_fIna_len = ";
    5761           0 :     ss << tmpCurDVR0->GetArrayLength();
    5762           0 :     ss << ";\n";
    5763             : #endif
    5764             : #ifdef ISNAN
    5765           0 :     ss<<"if((i+gid0)>=buffer_fIna_len || isNan(";
    5766           0 :     ss << vSubArguments[0]->GenSlidingWindowDeclRef();
    5767           0 :     ss<<"))\n";
    5768           0 :     ss<<"    dataIna = 0;\n";
    5769             : #endif
    5770           0 :     ss << "    int nSize =endFlag- startFlag ;\n";
    5771           0 :     ss << "    if (nSize & 1)\n";
    5772           0 :     ss << "    {\n";
    5773           0 :     ss << "        tmp = "<<vSubArguments[0]->GetNameAsString();
    5774           0 :     ss << "        [startFlag+nSize/2];\n";
    5775           0 :     ss << "    }\n";
    5776           0 :     ss << "    else\n";
    5777           0 :     ss << "    {\n";
    5778           0 :     ss << "        tmp =("<<vSubArguments[0]->GetNameAsString();
    5779           0 :     ss << "        [startFlag+nSize/2]+";
    5780           0 :     ss <<          vSubArguments[0]->GetNameAsString();
    5781           0 :     ss << "        [startFlag+nSize/2-1])/2;\n";
    5782           0 :     ss << "    }\n";
    5783           0 :     ss <<"     return tmp;\n";
    5784           0 :     ss << "}\n";
    5785           0 : }
    5786           0 : void OpKurt:: GenSlidingWindowFunction(std::stringstream &ss,
    5787             :             const std::string &sSymName, SubArguments &vSubArguments)
    5788             : {
    5789           0 :     ss << "\ndouble " << sSymName;
    5790           0 :     ss << "_"<< BinFuncName() <<"( ";
    5791           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5792             :     {
    5793           0 :         if (i)
    5794           0 :             ss << ",";
    5795           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5796             :     }
    5797           0 :     ss << ")\n";
    5798           0 :     ss <<"{\n";
    5799           0 :     ss << "    int gid0 = get_global_id(0);\n";
    5800           0 :     ss << "    double fSum = 0.0;\n";
    5801           0 :     ss << "    double vSum = 0.0;\n";
    5802           0 :     ss << "    double length;\n";
    5803           0 :     ss << "    double totallength=0;\n";
    5804           0 :     ss << "    double tmp = 0;\n";
    5805           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5806             :     {
    5807           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5808             :         assert(pCur);
    5809           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    5810             :         {
    5811             :             const formula::DoubleVectorRefToken* pDVR =
    5812           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    5813           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    5814           0 :             ss << "    length="<<nCurWindowSize;
    5815           0 :             ss << ";\n";
    5816           0 :             ss << "    for (int i = ";
    5817           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    5818           0 :             ss << "    {\n";
    5819           0 :             ss << "        double arg"<<i<<" = ";
    5820           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5821           0 :             ss << ";\n";
    5822             : #ifdef ISNAN
    5823           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
    5824           0 :             ss << pDVR->GetArrayLength();
    5825           0 :             ss << "))\n";
    5826           0 :             ss << "        {\n";
    5827           0 :             ss << "            length-=1.0;\n";
    5828           0 :             ss << "            continue;\n";
    5829           0 :             ss << "        }\n";
    5830             : #endif
    5831           0 :             ss << "        fSum +=  arg"<<i<<";\n";
    5832           0 :             ss << "    }\n";
    5833           0 :             ss << "    totallength +=length;\n";
    5834             :         }
    5835           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    5836             :         {
    5837           0 :             ss << "    tmp = ";
    5838           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5839           0 :             ss << ";\n";
    5840             : #ifdef ISNAN
    5841           0 :             ss << "    if(!isNan(tmp))\n";
    5842           0 :             ss << "    {\n";
    5843           0 :             ss << "        fSum += tmp;\n";
    5844           0 :             ss << "        totallength +=1;\n";
    5845           0 :             ss << "    }\n";
    5846             : #endif
    5847             :         }
    5848           0 :         else if (pCur->GetType() == formula::svDouble)
    5849             :         {
    5850           0 :            ss << "    tmp = ";
    5851           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5852           0 :            ss << ";\n";
    5853           0 :            ss << "    fSum += tmp;\n";
    5854           0 :            ss << "    totallength +=1;\n";
    5855             :         }
    5856             :         else
    5857             :         {
    5858           0 :             ss << "    return DBL_MIN;\n";
    5859             :         }
    5860             :     }
    5861           0 :     ss << "    double fMean = fSum * pow(totallength,-1);\n";
    5862           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5863             :     {
    5864           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5865             :         assert(pCur);
    5866           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    5867             :         {
    5868             :             const formula::DoubleVectorRefToken* pDVR =
    5869           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    5870           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    5871           0 :             ss << "    for (int i = ";
    5872           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    5873           0 :             ss << "    {\n";
    5874           0 :             ss << "        double arg"<<i<<" = ";
    5875           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5876           0 :             ss << ";\n";
    5877             : #ifdef ISNAN
    5878           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
    5879           0 :             ss << pDVR->GetArrayLength();
    5880           0 :             ss << "))\n";
    5881           0 :             ss << "        {\n";
    5882           0 :             ss << "            continue;\n";
    5883           0 :             ss << "        }\n";
    5884             : #endif
    5885           0 :             ss << "        vSum +=  (arg"<<i<<"-fMean)*(arg"<<i<<"-fMean);\n";
    5886           0 :             ss << "    }\n";
    5887             :         }
    5888           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    5889             :         {
    5890           0 :             ss << "    tmp = ";
    5891           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5892           0 :             ss << ";\n";
    5893             : #ifdef ISNAN
    5894           0 :             ss << "    if(!isNan(tmp))\n";
    5895           0 :             ss << "    {\n";
    5896           0 :             ss << "        vSum += (tmp-fMean)*(tmp-fMean);\n";
    5897           0 :             ss << "    }\n";
    5898             : #endif
    5899             :         }
    5900           0 :         else if (pCur->GetType() == formula::svDouble)
    5901             :         {
    5902           0 :            ss << "    tmp = ";
    5903           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5904           0 :            ss << ";\n";
    5905           0 :            ss << "    vSum += (tmp-fMean)*(tmp-fMean);\n";
    5906             :         }
    5907             :     }
    5908           0 :     ss << "    double fStdDev = sqrt(vSum / (totallength - 1.0));\n";
    5909           0 :     ss << "    double dx = 0.0;\n";
    5910           0 :     ss << "    double xpower4 = 0.0;\n";
    5911           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5912             :     {
    5913           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    5914             :         assert(pCur);
    5915           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    5916             :         {
    5917             :             const formula::DoubleVectorRefToken* pDVR =
    5918           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    5919           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    5920           0 :             ss << "    for (int i = ";
    5921           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    5922           0 :             ss << "    {\n";
    5923           0 :             ss << "        double arg"<<i<<" = ";
    5924           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5925           0 :             ss << ";\n";
    5926             : #ifdef ISNAN
    5927           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
    5928           0 :             ss << pDVR->GetArrayLength();
    5929           0 :             ss << "))\n";
    5930           0 :             ss << "        {\n";
    5931           0 :             ss << "            continue;\n";
    5932           0 :             ss << "        }\n";
    5933             : #endif
    5934           0 :             ss<< "        dx = (arg"<<i<<" -fMean) / fStdDev;\n";
    5935           0 :             ss<< "        xpower4 = xpower4 + (dx * dx * dx * dx);\n";
    5936           0 :             ss << "    }\n";
    5937             :         }
    5938           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    5939             :         {
    5940           0 :             ss << "    tmp = ";
    5941           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5942           0 :             ss << ";\n";
    5943             : #ifdef ISNAN
    5944           0 :             ss << "    if(!isNan(tmp))\n";
    5945           0 :             ss << "    {\n";
    5946           0 :             ss<< "        dx = (tmp -fMean) / fStdDev;\n";
    5947           0 :             ss<< "        xpower4 = xpower4 + (dx * dx * dx * dx);\n";
    5948           0 :             ss << "    }\n";
    5949             : #endif
    5950             :         }
    5951           0 :         else if (pCur->GetType() == formula::svDouble)
    5952             :         {
    5953           0 :            ss << "    tmp = ";
    5954           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    5955           0 :            ss << ";\n";
    5956           0 :            ss<< "        dx = (tmp -fMean) / fStdDev;\n";
    5957           0 :            ss<< "        xpower4 = xpower4 + (dx * dx * dx * dx);\n";
    5958             :         }
    5959             :     }
    5960           0 :     ss<< "    double k_d = (totallength - 2.0) * (totallength - 3.0);\n";
    5961           0 :     ss<< "    double k_l = totallength * (totallength + 1.0) /";
    5962           0 :     ss<< "((totallength - 1.0) * k_d);\n";
    5963           0 :     ss<< "    double k_t = 3.0 * (totallength - 1.0) * ";
    5964           0 :     ss<< "(totallength - 1.0) / k_d;\n";
    5965           0 :     ss<< "    tmp = xpower4 * k_l - k_t;\n";
    5966           0 :     ss<< "    return tmp;\n";
    5967           0 :     ss << "}";
    5968           0 : }
    5969             : 
    5970           0 : void OpIntercept::GenSlidingWindowFunction(std::stringstream &ss,
    5971             :             const std::string &sSymName, SubArguments &vSubArguments)
    5972             : {
    5973           0 :     FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
    5974             :     assert(pCur);
    5975             :     const formula::DoubleVectorRefToken* pCurDVR =
    5976           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur);
    5977           0 :     size_t nCurWindowSize = pCurDVR->GetRefRowSize();
    5978           0 :     FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
    5979             :     assert(pCur1);
    5980             :     const formula::DoubleVectorRefToken* pCurDVR1 =
    5981           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur1);
    5982           0 :     size_t nCurWindowSize1 = pCurDVR1->GetRefRowSize();
    5983           0 :     ss << "\ndouble " << sSymName;
    5984           0 :     ss << "_"<< BinFuncName() <<"( ";
    5985           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    5986             :     {
    5987           0 :         if (i)
    5988           0 :             ss << ",";
    5989           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    5990             :     }
    5991           0 :     ss << ") {\n";
    5992           0 :     ss << "    int gid0 = get_global_id(0);\n";
    5993           0 :     ss << "    double fSumX = 0.0;\n";
    5994           0 :     ss << "    double fSumY = 0.0;\n";
    5995           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    5996           0 :     ss << "    double fSumSqrDeltaX = 0.0;\n";
    5997           0 :     ss << "    int length="<<nCurWindowSize;
    5998           0 :     ss << ";\n";
    5999           0 :     ss << "    int length1= "<<nCurWindowSize1;
    6000           0 :     ss << ";\n";
    6001           0 :     ss << "    if(length!=length1)\n";
    6002           0 :     ss << "        return 0;\n";
    6003           0 :     ss << "    double tmp = 0;\n";
    6004           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6005             :     {
    6006           0 :         pCur = vSubArguments[i]->GetFormulaToken();
    6007             :         assert(pCur);
    6008           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    6009             :         {
    6010             : 
    6011             :         }
    6012           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    6013             :         {
    6014             : #ifdef  ISNAN
    6015           0 :             ss << "return HUGE_VAL";
    6016             : #endif
    6017             :         }
    6018           0 :         else if (pCur->GetType() == formula::svDouble)
    6019             :         {
    6020             : #ifdef  ISNAN
    6021           0 :             ss << "return HUGE_VAL";
    6022             : #endif
    6023             :         }
    6024             :     }
    6025           0 :     ss << "    for (int i = 0; i <" << nCurWindowSize << "; i++)\n";
    6026           0 :     ss << "    {\n";
    6027           0 :     ss << "        double arg0 = ";
    6028           0 :     ss << vSubArguments[0]->GenSlidingWindowDeclRef();
    6029           0 :     ss << ";\n";
    6030           0 :     ss << "        double arg1 = ";
    6031           0 :     ss << vSubArguments[1]->GenSlidingWindowDeclRef();
    6032           0 :     ss << ";\n";
    6033             : #ifdef ISNAN
    6034           0 :     ss << "        if(isNan(arg0)||((gid0+i)>=";
    6035           0 :     ss << pCurDVR->GetArrayLength();
    6036           0 :     ss << "))\n";
    6037           0 :     ss << "        {\n";
    6038           0 :     ss << "            length--;\n";
    6039           0 :     ss << "            continue;\n";
    6040           0 :     ss << "        }\n";
    6041             : #endif
    6042             : #ifdef ISNAN
    6043           0 :     ss << "        if(isNan(arg1)||((gid0+i)>=";
    6044           0 :     ss << pCurDVR1->GetArrayLength();
    6045           0 :     ss << "))\n";
    6046           0 :     ss << "        {\n";
    6047           0 :     ss << "            length--;\n";
    6048           0 :     ss << "            continue;\n";
    6049           0 :     ss << "        }\n";
    6050             : #endif
    6051           0 :     ss << "        fSumY+=arg0;\n";
    6052           0 :     ss << "        fSumX+=arg1;\n";
    6053           0 :     ss << "    }\n";
    6054           0 :     ss <<"    double fMeanX = fSumX / length;\n";
    6055           0 :     ss <<"    double fMeanY = fSumY / length;\n";
    6056           0 :     ss << "    for (int i = 0; i <" << nCurWindowSize << "; i++)\n";
    6057           0 :     ss << "    {\n";
    6058           0 :     ss << "        double arg0 = ";
    6059           0 :     ss << vSubArguments[0]->GenSlidingWindowDeclRef();
    6060           0 :     ss << ";\n";
    6061           0 :     ss << "        double arg1 = ";
    6062           0 :     ss << vSubArguments[1]->GenSlidingWindowDeclRef();
    6063           0 :     ss << ";\n";
    6064             : #ifdef ISNAN
    6065           0 :     ss << "        if(isNan(arg0)||((gid0+i)>=";
    6066           0 :     ss << pCurDVR->GetArrayLength();
    6067           0 :     ss << "))\n";
    6068           0 :     ss << "        {\n";
    6069           0 :     ss << "            continue;\n";
    6070           0 :     ss << "        }\n";
    6071             : #endif
    6072             : #ifdef ISNAN
    6073           0 :     ss << "        if(isNan(arg1)||((gid0+i)>=";
    6074           0 :     ss <<pCurDVR1->GetArrayLength();
    6075           0 :     ss <<"))\n";
    6076           0 :     ss << "        {\n";
    6077           0 :     ss << "            continue;\n";
    6078           0 :     ss << "        }\n";
    6079             : #endif
    6080           0 :     ss << "        fSumDeltaXDeltaY+=(arg1 - fMeanX) * (arg0 - fMeanY);";
    6081           0 :     ss << ";\n";
    6082           0 :     ss << "        fSumSqrDeltaX    += pow(arg1 - fMeanX, 2);\n";
    6083           0 :     ss << "    }\n";
    6084           0 :     ss << "    tmp = fMeanY - fSumDeltaXDeltaY / fSumSqrDeltaX";
    6085           0 :     ss << "* fMeanX;\n";
    6086           0 :     ss << "    return tmp;\n";
    6087           0 :         ss << "}";
    6088           0 : }
    6089           0 : void OpLogInv:: GenSlidingWindowFunction(std::stringstream &ss,
    6090             :             const std::string &sSymName, SubArguments &vSubArguments)
    6091             : {
    6092           0 :     ss << "\ndouble " << sSymName;
    6093           0 :     ss << "_"<< BinFuncName() <<"(";
    6094           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6095             :     {
    6096           0 :         if (i)
    6097           0 :             ss << ",";
    6098           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6099             :     }
    6100           0 :     ss << ") {\n";
    6101           0 :     ss << "    int gid0=get_global_id(0);\n";
    6102           0 :     ss << "    double tmp;\n";
    6103           0 :     ss << "    double arg0,arg1,arg2,arg3;\n";
    6104           0 :     size_t i = vSubArguments.size();
    6105           0 :     size_t nItems = 0;
    6106           0 :     for (i = 0; i < vSubArguments.size(); i++)
    6107             :     {
    6108           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    6109             :         assert(pCur);
    6110           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    6111             :         {
    6112             :             const formula::DoubleVectorRefToken* pDVR =
    6113           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    6114           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    6115           0 :             ss << "for (int i = ";
    6116           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    6117             : #ifdef  ISNAN
    6118           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    6119           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    6120             : #else
    6121             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    6122             : #endif
    6123           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    6124             : #ifdef  ISNAN
    6125           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    6126           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    6127             : #else
    6128             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    6129             : #endif
    6130           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    6131             : #ifdef  ISNAN
    6132           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    6133           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    6134             : #else
    6135             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6136             : #endif
    6137             :             }
    6138             :             else {
    6139             : #ifdef  ISNAN
    6140           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    6141             : #else
    6142             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6143             : #endif
    6144             :             }
    6145           0 :             nItems += nCurWindowSize;
    6146             :         }
    6147           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    6148             :         {
    6149             : #ifdef  ISNAN
    6150             :             const formula::SingleVectorRefToken* pSVR =
    6151           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    6152           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    6153           0 :             ss << "    {\n";
    6154           0 :             ss << "        if (isNan(";
    6155           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6156           0 :             ss << "))\n";
    6157           0 :             ss << "            arg"<<i<<"= 0;\n";
    6158           0 :             ss << "        else\n";
    6159           0 :             ss << "            arg"<<i<<"=";
    6160           0 :             ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6161           0 :             ss << ";\n";
    6162           0 :             ss << "    }\n";
    6163           0 :             ss << "    else\n";
    6164           0 :             ss << "        arg"<<i<<"= 0;\n";
    6165             : #endif
    6166             :         }
    6167           0 :         else if (pCur->GetType() == formula::svDouble)
    6168             :         {
    6169             : #ifdef  ISNAN
    6170           0 :             ss << "    if (isNan(";
    6171           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6172           0 :             ss << "))\n";
    6173           0 :             ss << "        arg"<<i<<"= 0;\n";
    6174           0 :             ss << "    else\n";
    6175           0 :             ss << "        arg"<<i<<"=";
    6176           0 :             ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6177           0 :             ss << ";\n";
    6178             : #endif
    6179             :         }
    6180             :     }
    6181           0 :     ss<< "    double q,t,z;\n";
    6182           0 :     ss<< "    q = arg0 -0.5;\n";
    6183           0 :     ss<< "    if(fabs(q)<=.425)\n";
    6184           0 :     ss<< "    {\n";
    6185           0 :     ss<< "        t=0.180625-pow(q, 2);\n";
    6186             :     ss<< "        z=\n"
    6187             :     "        q*\n"
    6188             :     "        (\n"
    6189             :     "            (\n"
    6190             :     "                (\n"
    6191             :     "                    (\n"
    6192             :     "                        (\n"
    6193             :     "                            (\n"
    6194             :     "                                (\n"
    6195           0 :     "                                    t*2509.0809287301226727";
    6196             :     ss<<"+33430.575583588128105\n"
    6197             :     "                                )\n"
    6198             :     "                                *t+67265.770927008700853\n"
    6199             :     "                            )\n"
    6200             :     "                            *t+45921.953931549871457\n"
    6201             :     "                        )\n"
    6202             :     "                        *t+13731.693765509461125\n"
    6203             :     "                    )\n"
    6204             :     "                    *t+1971.5909503065514427\n"
    6205             :     "                )\n"
    6206             :     "                *t+133.14166789178437745\n"
    6207             :     "            )\n"
    6208             :     "            *t+3.387132872796366608\n"
    6209             :     "        )\n"
    6210             :     "        /\n"
    6211             :     "        (\n"
    6212             :     "            (\n"
    6213             :     "                (\n"
    6214             :     "                    (\n"
    6215             :     "                        (\n"
    6216             :     "                            (\n"
    6217             :     "                                (\n"
    6218           0 :     "                                    t*5226.495278852854561";
    6219             :     ss<<"+28729.085735721942674\n"
    6220             :     "                                )\n"
    6221             :     "                                *t+39307.89580009271061\n"
    6222             :     "                            )\n"
    6223             :     "                            *t+21213.794301586595867\n"
    6224             :     "                        )\n"
    6225             :     "                        *t+5394.1960214247511077\n"
    6226             :     "                    )\n"
    6227             :     "                    *t+687.1870074920579083\n"
    6228             :     "                )\n"
    6229             :     "                *t+42.313330701600911252\n"
    6230             :     "            )\n"
    6231             :     "            *t+1.0\n"
    6232           0 :     "        );\n";
    6233           0 :     ss<<"    }\n";
    6234           0 :     ss<<"    else\n";
    6235           0 :     ss<<"    {\n";
    6236           0 :     ss<<"        t = q > 0 ? 1 - arg0 : arg0;\n";
    6237           0 :     ss<<"        t=sqrt(-log(t));\n";
    6238           0 :     ss<<"        if(t<=5.0)\n";
    6239           0 :     ss<<"        {\n";
    6240           0 :     ss<<"            t+=-1.6;\n";
    6241             :     ss<<"            z=\n"
    6242             :     "            (\n"
    6243             :     "                (\n"
    6244             :     "                    (\n"
    6245             :     "                        (\n"
    6246             :     "                            (\n"
    6247             :     "                                (\n"
    6248             :     "                                    (\n"
    6249           0 :     "                                        t*7.7454501427834140764e-4";
    6250             :     ss<<"+0.0227238449892691845833\n"
    6251             :     "                                    )\n"
    6252             :     "                                    *t+0.24178072517745061177\n"
    6253             :     "                                )\n"
    6254             :     "                                *t+1.27045825245236838258\n"
    6255             :     "                            )\n"
    6256             :     "                            *t+3.64784832476320460504\n"
    6257             :     "                        )\n"
    6258             :     "                        *t+5.7694972214606914055\n"
    6259             :     "                    )\n"
    6260             :     "                    *t+4.6303378461565452959\n"
    6261             :     "                )\n"
    6262             :     "                *t+1.42343711074968357734\n"
    6263             :     "            )\n"
    6264             :     "            /\n"
    6265             :     "            (\n"
    6266             :     "                (\n"
    6267             :     "                    (\n"
    6268             :     "                        (\n"
    6269             :     "                            (\n"
    6270             :     "                                (\n"
    6271             :     "                                    (\n"
    6272           0 :     "                                        t*1.05075007164441684324e-9";
    6273             :     ss<<"+5.475938084995344946e-4\n"
    6274             :     "                                    )\n"
    6275             :     "                                    *t+0.0151986665636164571966\n"
    6276             :     "                                )\n"
    6277             :     "                                *t+0.14810397642748007459\n"
    6278             :     "                            )\n"
    6279             :     "                            *t+0.68976733498510000455\n"
    6280             :     "                        )\n"
    6281             :     "                        *t+1.6763848301838038494\n"
    6282             :     "                    )\n"
    6283             :     "                    *t+2.05319162663775882187\n"
    6284             :     "                )\n"
    6285             :     "                *t+1.0\n"
    6286           0 :     "            );\n";
    6287           0 :     ss<<"        }\n";
    6288           0 :     ss<<"        else\n";
    6289           0 :     ss<<"        {\n";
    6290           0 :     ss<<"            t+=-5.0;\n";
    6291             :     ss<<"            z=\n"
    6292             :     "            (\n"
    6293             :     "                (\n"
    6294             :     "                    (\n"
    6295             :     "                        (\n"
    6296             :     "                            (\n"
    6297             :     "                                (\n"
    6298             :     "                                    (\n"
    6299           0 :     "                                        t*2.01033439929228813265e-7";
    6300             :     ss<<"+2.71155556874348757815e-5\n"
    6301             :     "                                    )\n"
    6302             :     "                                    *t+0.0012426609473880784386\n"
    6303             :     "                                )\n"
    6304             :     "                                *t+0.026532189526576123093\n"
    6305             :     "                            )\n"
    6306             :     "                            *t+0.29656057182850489123\n"
    6307             :     "                        )\n"
    6308             :     "                        *t+1.7848265399172913358\n"
    6309             :     "                    )\n"
    6310             :     "                    *t+5.4637849111641143699\n"
    6311             :     "                )\n"
    6312             :     "                *t+6.6579046435011037772\n"
    6313             :     "            )\n"
    6314             :     "            /\n"
    6315             :     "            (\n"
    6316             :     "                (\n"
    6317             :     "                    (\n"
    6318             :     "                        (\n"
    6319             :     "                            (\n"
    6320             :     "                                (\n"
    6321             :     "                                    (\n"
    6322             :     "                                        t*2.04426310338993978564e-15"
    6323             :     " +1.4215117583164458887e-7\n"
    6324             :     "                                    )\n"
    6325             :     "                                    *t+1.8463183175100546818e-5\n"
    6326             :     "                                )\n"
    6327             :     "                                *t+7.868691311456132591e-4\n"
    6328             :     "                            )\n"
    6329             :     "                            *t+0.0148753612908506148525\n"
    6330             :     "                        )\n"
    6331             :     "                        *t+0.13692988092273580531\n"
    6332             :     "                    )\n"
    6333             :     "                    *t+0.59983220655588793769\n"
    6334             :     "                )\n"
    6335             :     "                *t+1.0\n"
    6336           0 :     "            );\n";
    6337           0 :     ss << "        }\n";
    6338           0 :     ss << "        z = q < 0.0 ? (-1)*z : z;\n";
    6339           0 :     ss << "    }\n";
    6340           0 :     ss << "    tmp = exp(arg1+arg2*z);\n";
    6341           0 :     ss << "    return tmp;\n";
    6342           0 :     ss << "}\n";
    6343           0 : }
    6344             : 
    6345           0 : void OpForecast::GenSlidingWindowFunction(std::stringstream &ss,
    6346             :             const std::string &sSymName, SubArguments &vSubArguments)
    6347             : {
    6348           0 :     FormulaToken *pCur0 = vSubArguments[0]->GetFormulaToken();
    6349             :     assert(pCur0);
    6350             :     const formula::SingleVectorRefToken*pCurDVR0= static_cast<const
    6351           0 :           formula::SingleVectorRefToken *>(pCur0);
    6352           0 :     FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
    6353             :     assert(pCur1);
    6354             :     const formula::DoubleVectorRefToken* pCurDVR1 =
    6355           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur1);
    6356           0 :     size_t nCurWindowSize = pCurDVR1->GetRefRowSize();
    6357           0 :     FormulaToken *pCur2 = vSubArguments[2]->GetFormulaToken();
    6358             :     assert(pCur2);
    6359             :     const formula::DoubleVectorRefToken* pCurDVR2 =
    6360           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur2);
    6361           0 :     size_t nCurWindowSize1 = pCurDVR2->GetRefRowSize();
    6362           0 :     ss << "\ndouble " << sSymName;
    6363           0 :     ss << "_"<< BinFuncName() <<"( ";
    6364           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6365             :     {
    6366           0 :         if (i)
    6367           0 :             ss << ",";
    6368           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6369             :     }
    6370           0 :     ss << ") {\n";
    6371           0 :     ss << "    int gid0 = get_global_id(0);\n";
    6372           0 :     ss << "    double fSumX = 0.0;\n";
    6373           0 :     ss << "    double fSumY = 0.0;\n";
    6374           0 :     ss << "    double fSumDeltaXDeltaY = 0.0;\n";
    6375           0 :     ss << "    double fSumSqrDeltaX = 0.0;\n";
    6376           0 :     if(pCur0->GetType()== formula::svDouble ||
    6377           0 :         pCur0->GetType() == formula::svSingleVectorRef)
    6378             :     {
    6379           0 :         ss << "    double arg0 = ";
    6380           0 :         ss << vSubArguments[0]->GenSlidingWindowDeclRef();
    6381           0 :         ss << ";\n";
    6382             :     }
    6383             :     else
    6384           0 :         ss << "return HUGE_VAL";
    6385           0 :     if(pCur1->GetType() != formula::svDoubleVectorRef ||
    6386           0 :         pCur2->GetType() != formula::svDoubleVectorRef)
    6387           0 :         ss << "return HUGE_VAL";
    6388             :     else
    6389             :     {
    6390             : #ifdef ISNAN
    6391           0 :         ss<< "    if(isNan(arg0)||(gid0>=";
    6392           0 :         ss<<pCurDVR0->GetArrayLength();
    6393           0 :         ss<<"))\n";
    6394           0 :         ss<<"        arg0 = 0;\n";
    6395             : #endif
    6396           0 :         ss << "    int length="<<nCurWindowSize;
    6397           0 :         ss << ";\n";
    6398           0 :         ss << "    int length1= "<<nCurWindowSize1;
    6399           0 :         ss << ";\n";
    6400           0 :         ss << "    if(length!=length1)\n";
    6401           0 :         ss << "        return 0;\n";
    6402           0 :         ss << "    double tmp = 0;\n";
    6403           0 :         ss << "    for (int i = 0; i <" << nCurWindowSize << "; i++)\n";
    6404           0 :         ss << "    {\n";
    6405           0 :         ss << "        double arg1 = ";
    6406           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef();
    6407           0 :         ss << ";\n";
    6408           0 :         ss << "        double arg2 = ";
    6409           0 :         ss << vSubArguments[2]->GenSlidingWindowDeclRef();
    6410           0 :         ss << ";\n";
    6411             : #ifdef ISNAN
    6412           0 :         ss << "        if(isNan(arg1)||((gid0+i)>=";
    6413           0 :         ss << pCurDVR1->GetArrayLength();
    6414           0 :         ss << "))\n";
    6415           0 :         ss << "        {\n";
    6416           0 :         ss << "            length--;\n";
    6417           0 :         ss << "            continue;\n";
    6418           0 :         ss << "        }\n";
    6419             : #endif
    6420             : #ifdef ISNAN
    6421           0 :         ss << "        if(isNan(arg2)||((gid0+i)>=";
    6422           0 :         ss << pCurDVR2->GetArrayLength();
    6423           0 :         ss << "))\n";
    6424           0 :         ss << "        {\n";
    6425           0 :         ss << "            length--;\n";
    6426           0 :         ss << "            continue;\n";
    6427           0 :         ss << "        }\n";
    6428             : #endif
    6429           0 :         ss << "        fSumY+=arg1;\n";
    6430           0 :         ss << "        fSumX+=arg2;\n";
    6431           0 :         ss << "    }\n";
    6432           0 :         ss << "    double fMeanX = fSumX / length;\n";
    6433           0 :         ss << "    double fMeanY = fSumY / length;\n";
    6434           0 :         ss << "    for (int i = 0; i <" << nCurWindowSize << "; i++)\n";
    6435           0 :         ss << "    {\n";
    6436           0 :         ss << "        double arg1 = ";
    6437           0 :         ss << vSubArguments[1]->GenSlidingWindowDeclRef();
    6438           0 :         ss << ";\n";
    6439           0 :         ss << "        double arg2 = ";
    6440           0 :         ss << vSubArguments[2]->GenSlidingWindowDeclRef();
    6441           0 :         ss << ";\n";
    6442             : #ifdef ISNAN
    6443           0 :         ss << "        if(isNan(arg1)||((gid0+i)>=";
    6444           0 :         ss <<pCurDVR1->GetArrayLength();
    6445           0 :         ss <<"))\n";
    6446           0 :         ss <<"        {\n";
    6447           0 :         ss <<"            continue;\n";
    6448           0 :         ss <<"        }\n";
    6449             : #endif
    6450             : #ifdef ISNAN
    6451           0 :         ss << "        if(isNan(arg2)||((gid0+i)>=";
    6452           0 :         ss <<pCurDVR2->GetArrayLength();
    6453           0 :         ss <<"))\n";
    6454           0 :         ss <<"        {\n";
    6455           0 :         ss <<"            continue;\n";
    6456           0 :         ss <<"        }\n";
    6457             : #endif
    6458           0 :         ss <<"        fSumDeltaXDeltaY+=(arg2 - fMeanX) * (arg1 - fMeanY);\n";
    6459           0 :         ss <<"        fSumSqrDeltaX+=pow(arg2 - fMeanX, 2);\n";
    6460           0 :         ss <<"    }\n";
    6461           0 :         ss <<"    tmp =fMeanY + fSumDeltaXDeltaY / fSumSqrDeltaX *";
    6462           0 :         ss <<" (arg0 - fMeanX);\n";
    6463           0 :         ss <<"    return tmp;\n";
    6464           0 :         ss << "}";
    6465             :     }
    6466           0 : }
    6467           0 : void OpLogNormDist::GenSlidingWindowFunction(std::stringstream &ss,
    6468             :             const std::string &sSymName, SubArguments &vSubArguments)
    6469             : {
    6470           0 :     FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
    6471             :     const formula::SingleVectorRefToken*tmpCurDVR0= static_cast<const
    6472           0 :         formula::SingleVectorRefToken *>(tmpCur0);
    6473           0 :     FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
    6474             :     const formula::SingleVectorRefToken*tmpCurDVR1= static_cast<const
    6475           0 :         formula::SingleVectorRefToken *>(tmpCur1);
    6476           0 :     FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
    6477             :     const formula::SingleVectorRefToken*tmpCurDVR2= static_cast<const
    6478           0 :         formula::SingleVectorRefToken *>(tmpCur2);
    6479           0 :     FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
    6480             :     const formula::SingleVectorRefToken*tmpCurDVR3= static_cast<const
    6481           0 :         formula::SingleVectorRefToken *>(tmpCur3);
    6482           0 :     ss << "\ndouble " << sSymName;
    6483           0 :     ss << "_"<< BinFuncName() <<"(";
    6484           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6485             :     {
    6486           0 :         if (i)
    6487           0 :             ss << ",";
    6488           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6489             :     }
    6490           0 :     ss << ") {\n";
    6491           0 :     ss << "    int gid0=get_global_id(0);\n";
    6492           0 :     ss << "    double arg0,arg1,arg2,arg3;\n";
    6493           0 :     size_t i = vSubArguments.size();
    6494           0 :     size_t nItems = 0;
    6495           0 :     for (i = 0; i < vSubArguments.size(); i++)
    6496             :     {
    6497           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    6498             :         assert(pCur);
    6499           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    6500             :         {
    6501             :             const formula::DoubleVectorRefToken* pDVR =
    6502           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    6503           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    6504           0 :             ss << "for (int i = ";
    6505           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    6506             : #ifdef  ISNAN
    6507           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    6508           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    6509             : #else
    6510             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    6511             : #endif
    6512           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    6513             : #ifdef  ISNAN
    6514           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    6515           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    6516             : #else
    6517             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    6518             : #endif
    6519           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    6520             : #ifdef  ISNAN
    6521           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    6522           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    6523             : #else
    6524             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6525             : #endif
    6526             :             }
    6527             :             else {
    6528             : #ifdef  ISNAN
    6529           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    6530             : #else
    6531             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6532             : #endif
    6533             :             }
    6534           0 :             nItems += nCurWindowSize;
    6535             :         }
    6536           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    6537             :         {
    6538             : #ifdef  ISNAN
    6539             :             const formula::SingleVectorRefToken* pSVR =
    6540           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    6541           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    6542           0 :             ss << "    {\n";
    6543           0 :             ss << "        if (isNan(";
    6544           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6545           0 :             ss << "))\n";
    6546           0 :             ss << "            arg"<<i<<"= 0;\n";
    6547           0 :             ss << "        else\n";
    6548           0 :             ss << "            arg"<<i<<"=";
    6549           0 :             ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6550           0 :             ss << ";\n";
    6551           0 :             ss << "    }\n";
    6552           0 :             ss << "    else\n";
    6553           0 :             ss << "        arg"<<i<<"= 0;\n";
    6554             : #endif
    6555             :         }
    6556           0 :         else if (pCur->GetType() == formula::svDouble)
    6557             :         {
    6558             : #ifdef  ISNAN
    6559           0 :             ss << "    if (isNan(";
    6560           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6561           0 :             ss << "))\n";
    6562           0 :             ss << "        arg"<<i<<"= 0;\n";
    6563           0 :             ss << "    else\n";
    6564           0 :             ss << "        arg"<<i<<"=";
    6565           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    6566           0 :             ss << ";\n";
    6567             : #endif
    6568             :         }
    6569             :     }
    6570           0 :     ss << "    double tmp;\n";
    6571             : #ifdef ISNAN
    6572           0 :     ss << "    if(isNan(arg0)||(gid0>=";
    6573           0 :     ss << tmpCurDVR0->GetArrayLength();
    6574           0 :     ss << "))\n";
    6575           0 :     ss << "        arg0 = 0;\n";
    6576             : #endif
    6577             : #ifdef ISNAN
    6578           0 :     ss << "    if(isNan(arg1)||(gid0>=";
    6579           0 :     ss << tmpCurDVR1->GetArrayLength();
    6580           0 :     ss << "))\n";
    6581           0 :     ss << "        arg1 = 0;\n";
    6582             : #endif
    6583             : #ifdef ISNAN
    6584           0 :     ss << "    if(isNan(arg2)||(gid0>=";
    6585           0 :     ss << tmpCurDVR2->GetArrayLength();
    6586           0 :     ss << "))\n";
    6587           0 :     ss << "        arg2 = 0;\n";
    6588             : #endif
    6589             : #ifdef ISNAN
    6590           0 :     ss << "    if(isNan(arg3)||(gid0>=";
    6591           0 :     ss << tmpCurDVR3->GetArrayLength();
    6592           0 :     ss << "))\n";
    6593           0 :     ss << "        arg3 = 0;\n";
    6594             : #endif
    6595           0 :     ss << "    double temp = (log(arg0)-arg1)/arg2;\n";
    6596           0 :     ss << "    if(arg3)\n";
    6597           0 :     ss << "    {\n";
    6598           0 :     ss << "        if(arg0<=0)\n";
    6599           0 :     ss << "            tmp = 0.0;\n";
    6600           0 :     ss << "        else\n";
    6601           0 :     ss << "            tmp = 0.5 * erfc(-temp * 0.7071067811865475);\n";
    6602           0 :     ss << "    }\n";
    6603           0 :     ss << "    else\n";
    6604           0 :     ss << "        tmp = (0.39894228040143268 * exp((-1)*pow(temp, 2)";
    6605           0 :     ss << " / 2.0))/(arg2*arg0);\n";
    6606           0 :     ss << "    return tmp;\n";
    6607           0 :     ss << "}\n";
    6608           0 : }
    6609             : 
    6610           0 : void OpGammaDist::BinInlineFun(std::set<std::string>& decls,
    6611             :     std::set<std::string>& funs)
    6612             : {
    6613           0 :     decls.insert(fBigInvDecl);decls.insert(fLogDblMaxDecl);
    6614           0 :     decls.insert(fHalfMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
    6615           0 :     decls.insert(GetGammaSeriesDecl);decls.insert(GetGammaContFractionDecl);
    6616           0 :     decls.insert(GetLowRegIGammaDecl);decls.insert(GetGammaDistDecl);
    6617           0 :     decls.insert(GetGammaDistPDFDecl);
    6618           0 :     funs.insert(GetGammaSeries);funs.insert(GetGammaContFraction);
    6619           0 :     funs.insert(GetLowRegIGamma);funs.insert(GetGammaDist);
    6620           0 :     funs.insert(GetGammaDistPDF);
    6621           0 : }
    6622             : 
    6623           0 : void OpGammaDist::GenSlidingWindowFunction(std::stringstream &ss,
    6624             :             const std::string &sSymName, SubArguments &vSubArguments)
    6625             : {
    6626           0 :     ss << "\ndouble " << sSymName;
    6627           0 :     ss << "_"<< BinFuncName() <<"(";
    6628           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6629             :     {
    6630           0 :         if (i)
    6631           0 :             ss << ",";
    6632           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6633             :     }
    6634           0 :     ss << ") {\n";
    6635           0 :     ss << "    int gid0=get_global_id(0);\n";
    6636           0 :     ss << "    double tmp;\n";
    6637           0 :     ss << "    double arg0,arg1,arg2,arg3;\n";
    6638           0 :     size_t i = vSubArguments.size();
    6639           0 :     size_t nItems = 0;
    6640           0 :     for (i = 0; i < vSubArguments.size(); i++)
    6641             :     {
    6642           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    6643             :         assert(pCur);
    6644           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    6645             :         {
    6646             :             const formula::DoubleVectorRefToken* pDVR =
    6647           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    6648           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    6649           0 :             ss << "for (int i = ";
    6650           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    6651             : #ifdef  ISNAN
    6652           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    6653           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    6654             : #else
    6655             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    6656             : #endif
    6657           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    6658             : #ifdef  ISNAN
    6659           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    6660           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    6661             : #else
    6662             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    6663             : #endif
    6664           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    6665             : #ifdef  ISNAN
    6666           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    6667           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    6668             : #else
    6669             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6670             : #endif
    6671             :             }
    6672             :             else {
    6673             : #ifdef  ISNAN
    6674           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    6675             : #else
    6676             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6677             : #endif
    6678             :             }
    6679           0 :             nItems += nCurWindowSize;
    6680             :         }
    6681           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    6682             :         {
    6683             : #ifdef  ISNAN
    6684             :             const formula::SingleVectorRefToken* pSVR =
    6685           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    6686           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    6687           0 :             ss << "    {\n";
    6688           0 :             ss << "        if (isNan(";
    6689           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6690           0 :             ss << "))\n";
    6691           0 :             ss << "            arg"<<i<<"= 0;\n";
    6692           0 :             ss << "        else\n";
    6693           0 :             ss << "            arg"<<i<<"=";
    6694           0 :             ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6695           0 :             ss << ";\n";
    6696           0 :             ss << "    }\n";
    6697           0 :             ss << "    else\n";
    6698           0 :             ss << "        arg"<<i<<"= 0;\n";
    6699             : #endif
    6700             :         }
    6701           0 :         else if (pCur->GetType() == formula::svDouble)
    6702             :         {
    6703             : #ifdef  ISNAN
    6704           0 :             ss << "    if (isNan(";
    6705           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6706           0 :             ss << "))\n";
    6707           0 :             ss << "        arg"<<i<<"= 0;\n";
    6708           0 :             ss << "    else\n";
    6709           0 :             ss << "        arg"<<i<<"=";
    6710           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    6711           0 :             ss << ";\n";
    6712             : #endif
    6713             :         }
    6714             :     }
    6715           0 :     ss << "    if (arg3)\n";
    6716           0 :     ss << "        tmp=GetGammaDist( arg0, arg1, arg2);\n";
    6717           0 :     ss << "    else\n";
    6718           0 :     ss << "        tmp=GetGammaDistPDF( arg0, arg1, arg2);\n";
    6719           0 :     ss << "    return tmp;\n";
    6720           0 :     ss << "}\n";
    6721           0 : }
    6722           0 : void OpChiDist::BinInlineFun(std::set<std::string>& decls,
    6723             :     std::set<std::string>& funs)
    6724             : {
    6725           0 :     decls.insert(fBigInvDecl);
    6726           0 :     funs.insert("");
    6727           0 :     decls.insert(fHalfMachEpsDecl);
    6728           0 :     funs.insert("");
    6729           0 :     decls.insert(GetUpRegIGammaDecl);
    6730           0 :     funs.insert(GetUpRegIGamma);
    6731           0 :     decls.insert(GetGammaSeriesDecl);
    6732           0 :     funs.insert(GetGammaSeries);
    6733           0 :     decls.insert(GetGammaContFractionDecl);
    6734           0 :     funs.insert(GetGammaContFraction);
    6735           0 :     decls.insert(GetChiDistDecl);
    6736           0 :     funs.insert(GetChiDist);
    6737           0 : }
    6738           0 : void OpChiDist::GenSlidingWindowFunction(
    6739             :     std::stringstream &ss,const std::string &sSymName,
    6740             :     SubArguments &vSubArguments)
    6741             : {
    6742           0 :     ss << "\ndouble " << sSymName;
    6743           0 :     ss << "_"<< BinFuncName() <<"(";
    6744           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6745             :     {
    6746           0 :         if (i)
    6747           0 :             ss << ",";
    6748           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6749             :     }
    6750           0 :     ss << ")\n";
    6751           0 :     ss << "{\n";
    6752           0 :     ss << "    double fx,fDF,tmp=0,tmp0=0,tmp1=0;\n";
    6753           0 :     ss << "    int gid0=get_global_id(0);\n";
    6754             : 
    6755           0 :     size_t i = vSubArguments.size();
    6756           0 :     size_t nItems = 0;
    6757           0 :     ss <<"\n";
    6758           0 :     for (i = 0; i < vSubArguments.size(); i++)
    6759             :     {
    6760           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    6761             :         assert(pCur);
    6762           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    6763             :         {
    6764             :             const formula::DoubleVectorRefToken* pDVR =
    6765           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    6766           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    6767           0 :             ss << "for (int i = ";
    6768           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    6769             :             {
    6770             : #ifdef  ISNAN
    6771           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    6772           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    6773             : #else
    6774             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    6775             : #endif
    6776             :             }
    6777           0 :             else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    6778             :             {
    6779             : #ifdef  ISNAN
    6780           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    6781           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    6782             : #else
    6783             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    6784             : #endif
    6785             :             }
    6786           0 :             else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    6787             :             {
    6788             : #ifdef  ISNAN
    6789           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    6790           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    6791             : #else
    6792             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6793             : #endif
    6794             :             }
    6795             :             else
    6796             :             {
    6797             : #ifdef  ISNAN
    6798           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    6799             : #else
    6800             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6801             : #endif
    6802             :             }
    6803           0 :             nItems += nCurWindowSize;
    6804             :         }
    6805           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    6806             :         {
    6807             : #ifdef  ISNAN
    6808             :             const formula::SingleVectorRefToken* pSVR =
    6809           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    6810           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    6811             : #else
    6812             :             nItems += 1;
    6813             : #endif
    6814             :         }
    6815           0 :         else if (pCur->GetType() == formula::svDouble)
    6816             :         {
    6817             : #ifdef  ISNAN
    6818           0 :             ss << "{\n";
    6819             : #endif
    6820           0 :             nItems += 1;
    6821             :         }
    6822             :         else
    6823             :         {
    6824             : #ifdef  ISNAN
    6825             : #endif
    6826           0 :             nItems += 1;
    6827             :         }
    6828             : #ifdef  ISNAN
    6829           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    6830             :         {
    6831           0 :             ss << "    if (isNan(";
    6832           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6833           0 :             ss << "))\n";
    6834           0 :             ss << "        tmp"<<i<<"= 0;\n";
    6835           0 :             ss << "    else\n";
    6836           0 :             ss << "        tmp"<<i<<"=\n";
    6837           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6838           0 :             ss << ";\n}\n";
    6839             :         }
    6840             : #endif
    6841             :     }
    6842           0 :     ss << "    fx = tmp0;\n";
    6843           0 :     ss << "    fDF = floor(tmp1);\n";
    6844           0 :     ss << "    if(fDF < 1.0)\n";
    6845           0 :     ss << "    {\n";
    6846           0 :     ss << "        return DBL_MIN;\n";
    6847           0 :     ss << "    }\n";
    6848           0 :     ss << "    tmp = GetChiDist( fx, fDF);\n";
    6849           0 :     ss << "    return tmp;\n";
    6850           0 :     ss << "}\n";
    6851           0 : }
    6852           0 : void OpBinomdist::BinInlineFun(std::set<std::string>& decls,
    6853             :     std::set<std::string>& funs)
    6854             : {
    6855           0 :     decls.insert(fMachEpsDecl);
    6856           0 :     funs.insert("");
    6857           0 :     decls.insert(MinDecl);
    6858           0 :     funs.insert("");
    6859           0 :     decls.insert(fMaxGammaArgumentDecl);
    6860           0 :     funs.insert("");
    6861           0 :     decls.insert(GetBinomDistPMFDecl);
    6862           0 :     funs.insert(GetBinomDistPMF);
    6863           0 :     decls.insert(GetBetaDistDecl);
    6864           0 :     funs.insert(GetBetaDist);
    6865           0 :     decls.insert(lcl_GetBinomDistRangeDecl);
    6866           0 :     funs.insert(lcl_GetBinomDistRange);
    6867           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);
    6868           0 :     funs.insert(lcl_GetBetaHelperContFrac);
    6869           0 :     decls.insert(GetBetaDistPDFDecl);
    6870           0 :     funs.insert(GetBetaDistPDF);
    6871           0 :     decls.insert(GetLogBetaDecl);
    6872           0 :     funs.insert(GetLogBeta);
    6873           0 :     decls.insert(GetBetaDecl);
    6874           0 :     funs.insert(GetBeta);
    6875           0 :     decls.insert(lcl_getLanczosSumDecl);
    6876           0 :     funs.insert(lcl_getLanczosSum);
    6877           0 : }
    6878           0 : void OpBinomdist::GenSlidingWindowFunction(
    6879             :     std::stringstream &ss,const std::string &sSymName,
    6880             :     SubArguments &vSubArguments)
    6881             : {
    6882           0 :     ss << "\ndouble " << sSymName;
    6883           0 :     ss << "_"<< BinFuncName() <<"(";
    6884           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    6885             :     {
    6886           0 :         if (i)
    6887           0 :             ss << ",";
    6888           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    6889             :     }
    6890           0 :     ss << ")\n";
    6891           0 :     ss << "{\n";
    6892           0 :     ss << "    double tmp0,tmp1,tmp2,tmp3;\n";
    6893           0 :     ss << "    int gid0=get_global_id(0);\n";
    6894           0 :     size_t i = vSubArguments.size();
    6895           0 :     size_t nItems = 0;
    6896           0 :     ss <<"\n    ";
    6897             :     //while (i-- > 1)
    6898           0 :     for (i = 0; i < vSubArguments.size(); i++)
    6899             :     {
    6900           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    6901             :         assert(pCur);
    6902           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    6903             :         {
    6904             :             const formula::DoubleVectorRefToken* pDVR =
    6905           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    6906           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    6907           0 :             ss << "for (int i = ";
    6908           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    6909             :             {
    6910             : #ifdef  ISNAN
    6911           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    6912           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    6913             : #else
    6914             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    6915             : #endif
    6916             :             }
    6917           0 :             else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    6918             :             {
    6919             : #ifdef  ISNAN
    6920           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    6921           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    6922             : #else
    6923             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    6924             : #endif
    6925             :             }
    6926           0 :             else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    6927             :             {
    6928             : #ifdef  ISNAN
    6929           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    6930           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    6931             : #else
    6932             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6933             : #endif
    6934             :             }
    6935             :             else
    6936             :             {
    6937             : #ifdef  ISNAN
    6938           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    6939             : #else
    6940             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    6941             : #endif
    6942             :             }
    6943           0 :             nItems += nCurWindowSize;
    6944             :         }
    6945           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    6946             :         {
    6947             : #ifdef  ISNAN
    6948             :             const formula::SingleVectorRefToken* pSVR =
    6949           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    6950           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    6951             : #else
    6952             :             nItems += 1;
    6953             : #endif
    6954             :         }
    6955           0 :         else if (pCur->GetType() == formula::svDouble)
    6956             :         {
    6957             : #ifdef  ISNAN
    6958           0 :             ss << "{\n";
    6959             : #endif
    6960           0 :             nItems += 1;
    6961             :         }
    6962             :         else
    6963             :         {
    6964             : #ifdef  ISNAN
    6965             : #endif
    6966           0 :             nItems += 1;
    6967             :         }
    6968             : #ifdef  ISNAN
    6969           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    6970             :         {
    6971           0 :             ss << "    if (isNan(";
    6972           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6973           0 :             ss << "))\n";
    6974           0 :             ss << "        tmp"<<i<<"= 0;\n";
    6975           0 :             ss << "    else\n";
    6976           0 :             ss << "        tmp"<<i<<"=\n";
    6977           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    6978           0 :             ss << ";\n}\n";
    6979             :         }
    6980             :         else
    6981             :         {
    6982           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6983           0 :             ss <<";\n";
    6984             :         }
    6985             : #else
    6986             :     ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    6987             :     ss <<";\n    ";
    6988             : #endif
    6989             :     }
    6990           0 :     ss << "    tmp0 = floor(tmp0);\n";
    6991           0 :     ss << "    tmp1 = floor(tmp1);\n";
    6992           0 :     ss << "    double rq = (0.5 - tmp2) + 0.5;\n";
    6993           0 :     ss << "    if (tmp1 < 0.0 || tmp0 < 0.0 || tmp0 > tmp1 ||";
    6994           0 :     ss << "tmp2 < 0.0 || tmp2 > 1.0)\n";
    6995           0 :     ss << "    {\n";
    6996           0 :     ss << "        return DBL_MIN;\n";
    6997           0 :     ss << "    }\n";
    6998           0 :     ss << "    if(tmp2 == 0.0)\n";
    6999           0 :     ss << "        return ( (tmp0 == 0.0 || tmp3) ? 1.0 : 0.0 );\n";
    7000           0 :     ss << "    if(tmp2 == 1.0)\n";
    7001           0 :     ss << "        return ( (tmp0 == tmp1) ? 1.0 : 0.0);\n";
    7002           0 :     ss << "    if(!tmp3)\n";
    7003           0 :     ss << "        return ( GetBinomDistPMF(tmp0, tmp1, tmp2));\n";
    7004           0 :     ss << "    else \n";
    7005           0 :     ss << "    {\n";
    7006           0 :     ss << "        if(tmp0 == tmp1)\n";
    7007           0 :     ss << "            return 1.0;\n";
    7008           0 :     ss << "        else\n";
    7009           0 :     ss << "        {\n";
    7010           0 :     ss << "            double fFactor = pow(rq,tmp1);\n";
    7011           0 :     ss << "            if(tmp0 == 0.0)\n";
    7012           0 :     ss << "            return (fFactor);\n";
    7013           0 :     ss << "            else if(fFactor <= Min)\n";
    7014           0 :     ss << "            {\n";
    7015           0 :     ss << "                fFactor = pow(tmp2,tmp1);\n";
    7016           0 :     ss << "                if(fFactor <= Min)\n";
    7017           0 :     ss << "                    return GetBetaDist";
    7018           0 :     ss << "(rq, tmp1 - tmp0, tmp0 + 1.0);\n";
    7019           0 :     ss << "                else\n";
    7020           0 :     ss << "                {\n";
    7021           0 :     ss << "                    if(fFactor > fMachEps)\n";
    7022           0 :     ss << "                    {\n";
    7023           0 :     ss << "                        double fSum = 1.0 - fFactor;\n";
    7024           0 :     ss << "                        unsigned int max = ";
    7025           0 :     ss << "(unsigned int)((tmp1 - tmp0)-1);\n";
    7026           0 :     ss << "                        for (uint i = 0; i < max && fFactor > 0.0;";
    7027           0 :     ss << " i++)\n";
    7028           0 :     ss << "                        {\n";
    7029           0 :     ss << "                           fFactor *= (tmp1 - i)*pow((i + 1),-1.0)*";
    7030           0 :     ss << "rq*pow(tmp2,-1.0);\n";
    7031           0 :     ss << "                            fSum -= fFactor;\n";
    7032           0 :     ss << "                        }\n";
    7033           0 :     ss << "                         return ( (fSum < 0.0) ? 0.0 : fSum );\n";
    7034           0 :     ss << "                    }\n";
    7035           0 :     ss << "                    else \n";
    7036           0 :     ss << "                        return (lcl_GetBinomDistRange";
    7037           0 :     ss << "(tmp1, tmp1 -  tmp0, tmp1, fFactor, rq, tmp2));\n";
    7038           0 :     ss << "                }\n";
    7039           0 :     ss << "            }\n";
    7040           0 :     ss << "           else\n";
    7041           0 :     ss << "           {\n";
    7042           0 :     ss << "               double rtmp = ( lcl_GetBinomDistRange";
    7043           0 :     ss << "(tmp1, 0.0, tmp0, fFactor, tmp2, rq));\n";
    7044           0 :     ss << "               return rtmp;\n";
    7045           0 :     ss << "           }\n";
    7046           0 :     ss << "       }\n";
    7047           0 :     ss << "   }\n";
    7048           0 :     ss << "}\n";
    7049           0 : }
    7050             : 
    7051             : 
    7052           0 : void OpChiSqDist::BinInlineFun(std::set<std::string>& decls,
    7053             :     std::set<std::string>& funs)
    7054             : {
    7055           0 :     decls.insert(fMaxGammaArgumentDecl);decls.insert(GetChiSqDistCDFDecl);
    7056           0 :     decls.insert(GetChiSqDistPDFDecl);decls.insert(GetLowRegIGammaDecl);
    7057           0 :     decls.insert(GetGammaContFractionDecl);decls.insert(GetGammaSeriesDecl);
    7058           0 :     decls.insert(fHalfMachEpsDecl);decls.insert(F_PIDecl);
    7059           0 :     decls.insert(fBigInvDecl);
    7060             : 
    7061           0 :     funs.insert(GetGammaContFraction);funs.insert(GetChiSqDistCDF);
    7062           0 :     funs.insert(GetChiSqDistPDF);funs.insert(GetLowRegIGamma);
    7063           0 :     funs.insert(GetGammaSeries);
    7064           0 : }
    7065             : 
    7066           0 : void OpChiSqDist::GenSlidingWindowFunction(
    7067             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    7068             : vSubArguments)
    7069             : {
    7070           0 :     ss << "\ndouble " << sSymName;
    7071           0 :     ss << "_"<< BinFuncName() <<"(";
    7072           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7073             :     {
    7074           0 :         if (i)
    7075           0 :             ss << ",";
    7076           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7077             :     }
    7078           0 :     ss << ") {\n";
    7079           0 :     ss << "    int gid0 = get_global_id(0);\n";
    7080           0 :     ss << "    int singleIndex = gid0;\n";
    7081           0 :     ss << "    double result = 0;\n";
    7082           0 :     if(vSubArguments.size()<2)
    7083             :     {
    7084           0 :         ss << "    result = -DBL_MAX;\n";
    7085           0 :         ss << "    return result;\n";
    7086             :     }else
    7087             :     {
    7088           0 :         GenTmpVariables(ss,vSubArguments);
    7089           0 :         CheckAllSubArgumentIsNan(ss,vSubArguments);
    7090           0 :         if(vSubArguments.size() == 2)
    7091             :         {
    7092           0 :             ss << "    int tmp2  = 1;\n";
    7093             :         }
    7094             :     }
    7095           0 :     size_t i = vSubArguments.size();
    7096           0 :     ss <<"\n";
    7097           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7098             :     {
    7099           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7100             :         assert(pCur);
    7101           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    7102             :         {
    7103             : #ifdef  ISNAN
    7104             :             const formula::SingleVectorRefToken* pSVR =
    7105           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7106           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    7107             : #endif
    7108             :         }
    7109           0 :         else if (pCur->GetType() == formula::svDouble)
    7110             :         {
    7111             : #ifdef  ISNAN
    7112           0 :             ss << "{\n";
    7113             : #endif
    7114             :         }
    7115             : 
    7116             : #ifdef  ISNAN
    7117           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    7118             :         {
    7119           0 :             ss << "    if (isNan(";
    7120           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7121           0 :             ss << "))\n";
    7122           0 :             ss << "        tmp"<<i<<"= 0;\n";
    7123           0 :             ss << "    else\n";
    7124           0 :             ss << "        tmp"<<i<<"=\n";
    7125           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7126           0 :             ss << ";\n}\n";
    7127             :         }
    7128             :         else
    7129             :         {
    7130           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    7131           0 :             ss <<";\n";
    7132             :         }
    7133             : #endif
    7134             :     }
    7135           0 :         ss << "    tmp1 = floor(tmp1);\n";
    7136           0 :         ss << "    if(tmp1 < 1.0)\n";
    7137           0 :         ss << "        result = -DBL_MAX;\n";
    7138           0 :         ss << "    else\n";
    7139           0 :         ss << "    {\n";
    7140           0 :         ss << "        if(tmp2)\n";
    7141           0 :         ss << "            result =GetChiSqDistCDF(tmp0,tmp1);\n";
    7142           0 :         ss << "        else\n";
    7143           0 :         ss << "            result =GetChiSqDistPDF(tmp0,tmp1);\n";
    7144           0 :         ss << "    }\n";
    7145           0 :         ss << "    return result;\n";
    7146           0 :         ss << "}";
    7147           0 :     }
    7148             : 
    7149           0 :  void OpChiSqInv::BinInlineFun(std::set<std::string>& decls,
    7150             :     std::set<std::string>& funs)
    7151             : {
    7152           0 :     decls.insert(fMaxGammaArgumentDecl);decls.insert(GetChiSqDistCDFDecl);
    7153           0 :     decls.insert(GetLowRegIGammaDecl);decls.insert(lcl_IterateInverseChiSQInvDecl);
    7154           0 :     decls.insert(GetGammaContFractionDecl);decls.insert(GetGammaSeriesDecl);
    7155           0 :     decls.insert(fHalfMachEpsDecl);decls.insert(F_PIDecl);
    7156           0 :     decls.insert(fBigInvDecl);decls.insert(lcl_HasChangeOfSignDecl);
    7157           0 :     decls.insert(fMachEpsDecl);
    7158             : 
    7159           0 :     funs.insert(GetGammaContFraction);funs.insert(GetChiSqDistCDF);
    7160           0 :     funs.insert(GetLowRegIGamma);funs.insert(lcl_HasChangeOfSign);
    7161           0 :     funs.insert(GetGammaSeries);funs.insert(lcl_IterateInverseChiSQInv);
    7162           0 : }
    7163             : 
    7164           0 : void OpChiSqInv::GenSlidingWindowFunction(
    7165             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    7166             : vSubArguments)
    7167             : {
    7168           0 :     ss << "\ndouble " << sSymName;
    7169           0 :     ss << "_"<< BinFuncName() <<"(";
    7170           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7171             :     {
    7172           0 :         if (i)
    7173           0 :             ss << ",";
    7174           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7175             :     }
    7176           0 :     ss << ") {\n";
    7177           0 :     ss << "    int gid0 = get_global_id(0);\n";
    7178           0 :     ss << "    int singleIndex = gid0;\n";
    7179           0 :     ss << "    double result = 0;\n";
    7180           0 :     if(vSubArguments.size()!=2)
    7181             :     {
    7182           0 :         ss << "    result = -DBL_MAX;\n";
    7183           0 :         ss << "    return result;\n";
    7184             :     }
    7185             :     else
    7186             :     {
    7187           0 :         GenTmpVariables(ss,vSubArguments);
    7188           0 :         CheckAllSubArgumentIsNan(ss,vSubArguments);
    7189           0 :         size_t i = vSubArguments.size();
    7190           0 :     ss <<"\n";
    7191           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7192             :     {
    7193           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7194             :         assert(pCur);
    7195           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    7196             :         {
    7197             : #ifdef  ISNAN
    7198             :             const formula::SingleVectorRefToken* pSVR =
    7199           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7200           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    7201             : #endif
    7202             :         }
    7203           0 :         else if (pCur->GetType() == formula::svDouble)
    7204             :         {
    7205             : #ifdef  ISNAN
    7206           0 :             ss << "{\n";
    7207             : #endif
    7208             :         }
    7209             : 
    7210             : #ifdef  ISNAN
    7211           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    7212             :         {
    7213           0 :             ss << "    if (isNan(";
    7214           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7215           0 :             ss << "))\n";
    7216           0 :             ss << "        tmp"<<i<<"= 0;\n";
    7217           0 :             ss << "    else\n";
    7218           0 :             ss << "        tmp"<<i<<"=\n";
    7219           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7220           0 :             ss << ";\n}\n";
    7221             :         }
    7222             :         else
    7223             :         {
    7224           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    7225           0 :             ss <<";\n";
    7226             :         }
    7227             : #endif
    7228             :     }
    7229           0 :         ss << "    tmp1 = floor(tmp1);\n";
    7230           0 :         ss << "    bool bConvError;\n";
    7231           0 :         ss << "    if(tmp1 < 1.0 || tmp0 < 0 || tmp0>=1.0)\n";
    7232           0 :         ss << "        result = -DBL_MAX;\n";
    7233           0 :         ss << "    else\n";
    7234           0 :         ss << "    {\n";
    7235           0 :         ss << "        result =lcl_IterateInverseChiSQInv( tmp0, tmp1,";
    7236           0 :         ss << "tmp1*0.5, tmp1, &bConvError );\n";
    7237           0 :         ss << "    }\n";
    7238           0 :         ss << "    if(bConvError)\n";
    7239           0 :         ss << "        result = -DBL_MAX;\n";
    7240           0 :         ss << "    return result;\n";
    7241           0 :         ss << "}";
    7242             :     }
    7243             : 
    7244           0 : }
    7245           0 : void OpGammaInv::BinInlineFun(std::set<std::string>& decls,
    7246             :     std::set<std::string>& funs)
    7247             : {
    7248           0 :     decls.insert(fBigInvDecl);decls.insert(fHalfMachEpsDecl);
    7249           0 :     decls.insert(GetGammaSeriesDecl);decls.insert(GetGammaContFractionDecl);
    7250           0 :     decls.insert(GetGammaInvValueDecl);
    7251           0 :     funs.insert(GetGammaSeries);funs.insert(GetGammaContFraction);
    7252           0 :     funs.insert(GetGammaInvValue);
    7253           0 : }
    7254             : 
    7255           0 : void OpGammaInv::GenSlidingWindowFunction(std::stringstream &ss,
    7256             :             const std::string &sSymName, SubArguments &vSubArguments)
    7257             : {
    7258           0 :     ss << "\ndouble " << sSymName;
    7259           0 :     ss << "_"<< BinFuncName() <<"(";
    7260           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7261             :     {
    7262           0 :         if (i)
    7263           0 :             ss << ",";
    7264           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7265             :     }
    7266           0 :     ss << ") {\n";
    7267           0 :     ss << "    int gid0=get_global_id(0);\n";
    7268           0 :     ss << "    double tmp;\n";
    7269           0 :     ss << "    double arg0,arg1,arg2;\n";
    7270           0 :     size_t i = vSubArguments.size();
    7271           0 :     size_t nItems = 0;
    7272           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7273             :     {
    7274           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7275             :         assert(pCur);
    7276           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    7277             :         {
    7278             :             const formula::DoubleVectorRefToken* pDVR =
    7279           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    7280           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    7281           0 :             ss << "for (int i = ";
    7282           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    7283             : #ifdef  ISNAN
    7284           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    7285           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    7286             : #else
    7287             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    7288             : #endif
    7289           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    7290             : #ifdef  ISNAN
    7291           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    7292           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    7293             : #else
    7294             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    7295             : #endif
    7296           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    7297             : #ifdef  ISNAN
    7298           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    7299           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    7300             : #else
    7301             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7302             : #endif
    7303             :             }
    7304             :             else {
    7305             : #ifdef  ISNAN
    7306           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    7307             : #else
    7308             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7309             : #endif
    7310             :             }
    7311           0 :             nItems += nCurWindowSize;
    7312             :         }
    7313           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    7314             :         {
    7315             : #ifdef  ISNAN
    7316             :             const formula::SingleVectorRefToken* pSVR =
    7317           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7318           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    7319           0 :             ss << "    {\n";
    7320           0 :             ss << "        if (isNan(";
    7321           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7322           0 :             ss << "))\n";
    7323           0 :             ss << "            arg"<<i<<"= 0;\n";
    7324           0 :             ss << "        else\n";
    7325           0 :             ss << "            arg"<<i<<"=";
    7326           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    7327           0 :             ss << ";\n";
    7328           0 :             ss << "    }\n";
    7329           0 :             ss << "    else\n";
    7330           0 :             ss << "        arg"<<i<<"= 0;\n";
    7331             : #endif
    7332             :         }
    7333           0 :         else if (pCur->GetType() == formula::svDouble)
    7334             :         {
    7335             : #ifdef  ISNAN
    7336           0 :             ss << "    if (isNan(";
    7337           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7338           0 :             ss << "))\n";
    7339           0 :             ss << "        arg"<<i<<"= 0;\n";
    7340           0 :             ss << "    else\n";
    7341           0 :             ss << "        arg"<<i<<"=";
    7342           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    7343           0 :             ss << ";\n";
    7344             : #endif
    7345             :         }
    7346             :     }
    7347             :     ss << "    if (arg0 == 0.0)\n"
    7348             :     "    {\n"
    7349             :     "        tmp=0.0;\n"
    7350             :     "        return tmp;\n"
    7351             :     "    }\n"
    7352             :     "    else\n"
    7353             :     "    {\n"
    7354             :     "        bool bConvError;\n"
    7355             :     "        double fStart = arg1 * arg2;\n"
    7356             :     "        double fAx=fStart*0.5;\n"
    7357             :     "        double fBx=fStart;\n"
    7358             :     "        bConvError = false;\n"
    7359             :     "        double fYEps = 1.0E-307;\n"
    7360             :     "        double fXEps = 2.22045e-016;\n"
    7361             :     "        double fAy = arg0-GetGammaInvValue(arg1,arg2,fAx);\n"
    7362             :     "        double fBy = arg0-GetGammaInvValue(arg1,arg2,fBx);\n"
    7363             :     "        double fTemp;\n"
    7364             :     "        unsigned short nCount;\n"
    7365             :     "        for (nCount = 0; nCount < 1000 && !((fAy < 0.0 && fBy > 0.0)"
    7366             :     " || (fAy > 0.0 && fBy < 0.0)); nCount++)\n"
    7367             :     "        {\n"
    7368             :     "            if (fabs(fAy) <= fabs(fBy))\n"
    7369             :     "            {\n"
    7370             :     "                fTemp = fAx;\n"
    7371             :     "                fAx += 2.0 * (fAx - fBx);\n"
    7372             :     "                if (fAx < 0.0)\n"
    7373             :     "                    fAx = 0.0;\n"
    7374             :     "                fBx = fTemp;\n"
    7375             :     "                fBy = fAy;\n"
    7376             :     "                fAy = arg0-GetGammaInvValue(arg1,arg2,fAx);\n"
    7377             :     "            }\n"
    7378             :     "            else\n"
    7379             :     "            {\n"
    7380             :     "                fTemp = fBx;\n"
    7381             :     "                fBx += 2.0 * (fBx - fAx);\n"
    7382             :     "                fAx = fTemp;\n"
    7383             :     "                fAy = fBy;\n"
    7384             :     "                fBy = arg0-GetGammaInvValue(arg1,arg2,fBx);\n"
    7385             :     "            }\n"
    7386             :     "        }\n"
    7387             :     "        if (fAy == 0.0)\n"
    7388             :     "        {\n"
    7389             :     "            tmp = fAx;\n"
    7390             :     "            return tmp;\n"
    7391             :     "        }\n"
    7392             :     "        if (fBy == 0.0)\n"
    7393             :     "        {\n"
    7394             :     "            tmp = fBx;\n"
    7395             :     "            return tmp;\n"
    7396             :     "        }\n"
    7397             :     "        if (!((fAy < 0.0 && fBy > 0.0) || (fAy > 0.0 && fBy < 0.0)))\n"
    7398             :     "        {\n"
    7399             :     "            bConvError = true;\n"
    7400             :     "            tmp = 0.0;\n"
    7401             :     "            return tmp;\n"
    7402             :     "        }\n"
    7403             :     "        double fPx = fAx;\n"
    7404             :     "        double fPy = fAy;\n"
    7405             :     "        double fQx = fBx;\n"
    7406             :     "        double fQy = fBy;\n"
    7407             :     "        double fRx = fAx;\n"
    7408             :     "        double fRy = fAy;\n"
    7409             :     "        double fSx = 0.5 * (fAx + fBx);\n"
    7410             :     "        bool bHasToInterpolate = true;\n"
    7411             :     "        nCount = 0;\n"
    7412             :     "        while ( nCount < 500 && fabs(fRy) > fYEps &&"
    7413             :     "(fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
    7414             :     "        {\n"
    7415             :     "            if (bHasToInterpolate)\n"
    7416             :     "            {\n"
    7417             :     "                if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
    7418             :     "                {\n"
    7419             :     "                    fSx = fPx * fRy * fQy *pow( (fRy-fPy),-1) *pow"
    7420             :     "( (fQy-fPy),-1)"
    7421             :     "+ fRx * fQy * fPy *pow( (fQy-fRy),-1) *pow( (fPy-fRy),-1)"
    7422             :     "+ fQx * fPy * fRy *pow( (fPy-fQy),-1) *pow( (fRy-fQy),-1);\n"
    7423             :     "                    bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
    7424             :     "                }\n"
    7425             :     "                else\n"
    7426             :     "                    bHasToInterpolate = false;\n"
    7427             :     "            }\n"
    7428             :     "            if(!bHasToInterpolate)\n"
    7429             :     "            {\n"
    7430             :     "                fSx = 0.5 * (fAx + fBx);\n"
    7431             :     "                fPx = fAx; fPy = fAy;\n"
    7432             :     "                fQx = fBx; fQy = fBy;\n"
    7433             :     "                bHasToInterpolate = true;\n"
    7434             :     "            }\n"
    7435             :     "            fPx = fQx; fQx = fRx; fRx = fSx;\n"
    7436             :     "            fPy = fQy; fQy = fRy;\n"
    7437             :     "            fRy =  arg0-GetGammaInvValue(arg1,arg2,fSx);\n"
    7438             :     "            if ((fAy < 0.0 && fRy > 0.0) || (fAy > 0.0 && fRy < 0.0))\n"
    7439             :     "            {\n"
    7440             :     "                fBx = fRx;\n"
    7441             :     "                fBy = fRy;\n"
    7442             :     "            }\n"
    7443             :     "            else\n"
    7444             :     "            {\n"
    7445             :     "                fAx = fRx;\n"
    7446             :     "                fAy = fRy;\n"
    7447             :     "            }\n"
    7448             :     "            bHasToInterpolate = bHasToInterpolate && (fabs(fRy)"
    7449             :     " * 2.0 <= fabs(fQy));\n"
    7450             :     "            ++nCount;\n"
    7451             :     "        }\n"
    7452             :     "        tmp = fRx;\n"
    7453             :     "        return tmp;\n"
    7454             :     "    }\n"
    7455           0 :     "}\n";
    7456           0 : }
    7457           0 : void OpFInv::BinInlineFun(std::set<std::string>& decls,
    7458             :     std::set<std::string>& funs)
    7459             : {
    7460           0 :     decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
    7461           0 :     decls.insert(lcl_getLanczosSumDecl);decls.insert(GetBetaDecl);
    7462           0 :     decls.insert(GetLogBetaDecl);decls.insert(GetBetaDistPDFDecl);
    7463           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetFInvValueDecl);
    7464           0 :     funs.insert(lcl_getLanczosSum);funs.insert(GetBeta);
    7465           0 :     funs.insert(GetLogBeta);funs.insert(GetBetaDistPDF);
    7466           0 :     funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetFInvValue);
    7467           0 : }
    7468             : 
    7469           0 : void OpFInv::GenSlidingWindowFunction(std::stringstream &ss,
    7470             :             const std::string &sSymName, SubArguments &vSubArguments)
    7471             : {
    7472           0 :     ss << "\ndouble " << sSymName;
    7473           0 :     ss << "_"<< BinFuncName() <<"(";
    7474           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7475             :     {
    7476           0 :         if (i)
    7477           0 :             ss << ",";
    7478           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7479             :     }
    7480           0 :     ss << ") {\n";
    7481           0 :     ss << "    int gid0=get_global_id(0);\n";
    7482           0 :     ss << "    double tmp;\n";
    7483           0 :     ss << "    double arg0,arg1,arg2;\n";
    7484           0 :     size_t i = vSubArguments.size();
    7485           0 :     size_t nItems = 0;
    7486           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7487             :     {
    7488           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7489             :         assert(pCur);
    7490           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    7491             :         {
    7492             :             const formula::DoubleVectorRefToken* pDVR =
    7493           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    7494           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    7495           0 :             ss << "for (int i = ";
    7496           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    7497             : #ifdef  ISNAN
    7498           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    7499           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    7500             : #else
    7501             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    7502             : #endif
    7503           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    7504             : #ifdef  ISNAN
    7505           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    7506           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    7507             : #else
    7508             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    7509             : #endif
    7510           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    7511             : #ifdef  ISNAN
    7512           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    7513           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    7514             : #else
    7515             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7516             : #endif
    7517             :             }
    7518             :             else {
    7519             : #ifdef  ISNAN
    7520           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    7521             : #else
    7522             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7523             : #endif
    7524             :             }
    7525           0 :             nItems += nCurWindowSize;
    7526             :         }
    7527           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    7528             :         {
    7529             : #ifdef  ISNAN
    7530             :             const formula::SingleVectorRefToken* pSVR =
    7531           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7532           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    7533           0 :             ss << "    {\n";
    7534           0 :             ss << "        if (isNan(";
    7535           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7536           0 :             ss << "))\n";
    7537           0 :             ss << "            arg"<<i<<"= 0;\n";
    7538           0 :             ss << "        else\n";
    7539           0 :             ss << "            arg"<<i<<"="<<vSubArguments[i]->
    7540           0 :                                               GenSlidingWindowDeclRef();
    7541           0 :             ss << ";\n";
    7542           0 :             ss << "    }\n";
    7543           0 :             ss << "    else\n";
    7544           0 :             ss << "        arg"<<i<<"= 0;\n";
    7545             : #endif
    7546             :         }
    7547           0 :         else if (pCur->GetType() == formula::svDouble)
    7548             :         {
    7549             : #ifdef  ISNAN
    7550           0 :             ss << "    if (isNan(";
    7551           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7552           0 :             ss << "))\n";
    7553           0 :             ss << "        arg"<<i<<"= 0;\n";
    7554           0 :             ss << "    else\n";
    7555           0 :             ss << "        arg"<<i<<"="<<vSubArguments[i]->
    7556           0 :                                              GenSlidingWindowDeclRef();
    7557           0 :             ss << ";\n";
    7558             : #endif
    7559             :         }
    7560             :     }
    7561             :     ss << "    double fF2=floor(arg2);\n"
    7562             :     "    double fF1=floor(arg1);\n"
    7563             :     "    bool bConvError;\n"
    7564             :     "    double fAx=fF1*0.5;\n"
    7565             :     "    double fBx=fF1;\n"
    7566             :     "    bConvError = false;\n"
    7567             :     "    const double fYEps = 1.0E-307;\n"
    7568             :     "    const double fXEps = 2.22045e-016;\n"
    7569             :     "    double fAy = arg0-GetFInvValue(fF1,fF2,fAx);\n"
    7570             :     "    double fBy = arg0-GetFInvValue(fF1,fF2,fBx);\n"
    7571             :     "    double fTemp;\n"
    7572             :     "    unsigned short nCount;\n"
    7573             :     "    for (nCount = 0; nCount < 1000 && !((fAy < 0.0 && fBy > 0.0)"
    7574             :     " || (fAy > 0.0 && fBy < 0.0)); nCount++)\n"
    7575             :     "    {\n"
    7576             :     "        if (fabs(fAy) <= fabs(fBy))\n"
    7577             :     "        {\n"
    7578             :     "            fTemp = fAx;\n"
    7579             :     "            fAx += 2.0 * (fAx - fBx);\n"
    7580             :     "            if (fAx < 0.0)\n"
    7581             :     "                fAx = 0.0;\n"
    7582             :     "            fBx = fTemp;\n"
    7583             :     "            fBy = fAy;\n"
    7584             :     "            fAy = arg0-GetFInvValue(fF1,fF2,fAx);\n"
    7585             :     "        }\n"
    7586             :     "        else\n"
    7587             :     "        {\n"
    7588             :     "            fTemp = fBx;\n"
    7589             :     "            fBx += 2.0 * (fBx - fAx);\n"
    7590             :     "            fAx = fTemp;\n"
    7591             :     "            fAy = fBy;\n"
    7592             :     "            fBy = arg0-GetFInvValue(fF1,fF2,fBx);\n"
    7593             :     "        }\n"
    7594             :     "    }\n"
    7595             :     "    if (fAy == 0.0)\n"
    7596             :     "    {\n"
    7597             :     "        tmp = fAx;\n"
    7598             :     "        return tmp;\n"
    7599             :     "    }\n"
    7600             :     "    if (fBy == 0.0)\n"
    7601             :     "    {\n"
    7602             :     "        tmp = fBx;\n"
    7603             :     "        return tmp;\n"
    7604             :     "    }\n"
    7605             :     "    if (!((fAy < 0.0 && fBy > 0.0) || (fAy > 0.0 && fBy < 0.0)))\n"
    7606             :     "    {\n"
    7607             :     "        bConvError = true;\n"
    7608             :     "        tmp = 0.0;\n"
    7609             :     "        return tmp;\n"
    7610             :     "    }\n"
    7611             :     "    double fPx = fAx;\n"
    7612             :     "    double fPy = fAy;\n"
    7613             :     "    double fQx = fBx;\n"
    7614             :     "    double fQy = fBy;\n"
    7615             :     "    double fRx = fAx;\n"
    7616             :     "    double fRy = fAy;\n"
    7617             :     "    double fSx = 0.5 * (fAx + fBx);\n"
    7618             :     "    bool bHasToInterpolate = true;\n"
    7619             :     "    nCount = 0;\n"
    7620             :     "    while ( nCount < 500 && fabs(fRy) > fYEps &&"
    7621             :     "(fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
    7622             :     "    {\n"
    7623             :     "        if (bHasToInterpolate)\n"
    7624             :     "        {\n"
    7625             :     "            if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
    7626             :     "            {\n"
    7627             :     "                fSx = fPx * fRy * fQy *pow( (fRy-fPy),-1)"
    7628             :     " *pow( (fQy-fPy),-1)+fRx * fQy * fPy*pow( (fQy-fRy),-1) *"
    7629             :     "pow( (fPy-fRy),-1)+ fQx * fPy * fRy *pow( (fPy-fQy),-1)"
    7630             :     " *pow((fRy-fQy),-1);\n"
    7631             :     "                bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
    7632             :     "            }\n"
    7633             :     "            else\n"
    7634             :     "                bHasToInterpolate = false;\n"
    7635             :     "        }\n"
    7636             :     "        if(!bHasToInterpolate)\n"
    7637             :     "        {\n"
    7638             :     "            fSx = 0.5 * (fAx + fBx);\n"
    7639             :     "            fPx = fAx; fPy = fAy;\n"
    7640             :     "            fQx = fBx; fQy = fBy;\n"
    7641             :     "            bHasToInterpolate = true;\n"
    7642             :     "        }\n"
    7643             :     "        fPx = fQx; fQx = fRx; fRx = fSx;\n"
    7644             :     "        fPy = fQy; fQy = fRy;\n"
    7645             :     "        fRy = arg0-GetFInvValue(fF1,fF2,fSx);\n"
    7646             :     "        if ((fAy < 0.0 && fRy > 0.0) || (fAy > 0.0 && fRy < 0.0))\n"
    7647             :     "        {\n"
    7648             :     "            fBx = fRx; fBy = fRy;\n"
    7649             :     "        }\n"
    7650             :     "        else\n"
    7651             :     "        {\n"
    7652             :     "            fAx = fRx; fAy = fRy;\n"
    7653             :     "        }\n"
    7654             :     "        bHasToInterpolate = bHasToInterpolate && (fabs(fRy)"
    7655             :     " * 2.0 <= fabs(fQy));\n"
    7656             :     "        ++nCount;\n"
    7657             :     "    }\n"
    7658             :     "    tmp = fRx;\n"
    7659             :     "    return tmp;"
    7660           0 :     "}";
    7661           0 : }
    7662           0 : void OpFTest::BinInlineFun(std::set<std::string>& decls,
    7663             :     std::set<std::string>& funs)
    7664             : {
    7665           0 :     decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
    7666           0 :     decls.insert(lcl_getLanczosSumDecl);decls.insert(GetBetaDecl);
    7667           0 :     decls.insert(GetLogBetaDecl);decls.insert(GetBetaDistPDFDecl);
    7668           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetBetaDistDecl);
    7669           0 :     decls.insert(GetFDistDecl);
    7670           0 :     funs.insert(lcl_getLanczosSum);funs.insert(GetBeta);
    7671           0 :     funs.insert(GetLogBeta);funs.insert(GetBetaDistPDF);
    7672           0 :     funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetBetaDist);
    7673           0 :     funs.insert(GetFDist);
    7674           0 : }
    7675           0 : void OpFTest::GenSlidingWindowFunction(std::stringstream &ss,
    7676             :             const std::string &sSymName, SubArguments &vSubArguments)
    7677             : {
    7678           0 :     FormulaToken *pCur = vSubArguments[0]->GetFormulaToken();
    7679             :     assert(pCur);
    7680             :     const formula::DoubleVectorRefToken* pCurDVR =
    7681           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur);
    7682           0 :     size_t nCurWindowSize = pCurDVR->GetRefRowSize();
    7683           0 :     FormulaToken *pCur1 = vSubArguments[1]->GetFormulaToken();
    7684             :     assert(pCur1);
    7685             :     const formula::DoubleVectorRefToken* pCurDVR1 =
    7686           0 :         static_cast<const formula::DoubleVectorRefToken *>(pCur1);
    7687           0 :     size_t nCurWindowSize1 = pCurDVR1->GetRefRowSize();
    7688           0 :     ss << "\ndouble " << sSymName;
    7689           0 :     ss << "_"<< BinFuncName() <<"( ";
    7690           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7691             :     {
    7692           0 :         if (i)
    7693           0 :             ss << ",";
    7694           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7695             :     }
    7696           0 :     ss << ") {\n";
    7697           0 :     ss << "    int gid0 = get_global_id(0);\n";
    7698           0 :     ss << "    double fSum1 = 0.0;\n";
    7699           0 :     ss << "    double fSumSqr1 = 0.0;\n";
    7700           0 :     ss << "    double fSum2 = 0.0;\n";
    7701           0 :     ss << "    double fSumSqr2 = 0.0;\n";
    7702           0 :     ss << "    int length0="<<nCurWindowSize;
    7703           0 :     ss << ";\n";
    7704           0 :     ss << "    int length1= "<<nCurWindowSize1;
    7705           0 :     ss << ";\n";
    7706           0 :     ss << "    double tmp = 0;\n";
    7707           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7708             :     {
    7709           0 :         FormulaToken *pCurSub = vSubArguments[i]->GetFormulaToken();
    7710             :         assert(pCurSub);
    7711           0 :         if (pCurSub->GetType() == formula::svDoubleVectorRef)
    7712             :         {
    7713             :             const formula::DoubleVectorRefToken* pDVR =
    7714           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCurSub);
    7715           0 :             ss << "    for (int i = ";
    7716             : #ifdef  ISNAN
    7717           0 :             ss << "0; i < "<< pDVR->GetRefRowSize() << "; i++){\n";
    7718           0 :             ss << "        double arg"<<i<<" = ";
    7719           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7720           0 :             ss << ";\n";
    7721             : #ifdef ISNAN
    7722           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
    7723           0 :             ss << pDVR->GetArrayLength();
    7724           0 :             ss << "))\n";
    7725           0 :             ss << "        {\n";
    7726           0 :             ss << "            length"<<i<<"--;\n";
    7727           0 :             ss << "            continue;\n";
    7728           0 :             ss << "        }\n";
    7729             : #endif
    7730           0 :             ss << "        fSum"<<i+1<<" += arg"<<i<<";\n";
    7731           0 :             ss << "        fSumSqr"<<i+1<<" += arg"<<i;
    7732           0 :             ss << " * arg"<<i<<";\n";
    7733           0 :             ss << "    }\n";
    7734             : #endif
    7735             :         }
    7736           0 :         else if (pCurSub->GetType() == formula::svSingleVectorRef)
    7737             :         {
    7738             : #ifdef  ISNAN
    7739           0 :             ss << "return HUGE_VAL";
    7740             : #endif
    7741             :         }
    7742           0 :         else if (pCurSub->GetType() == formula::svDouble)
    7743             :         {
    7744             : #ifdef  ISNAN
    7745           0 :             ss << "return HUGE_VAL";
    7746             : #endif
    7747             :         }
    7748             :     }
    7749             :     ss << "    double fS1 = (fSumSqr1-fSum1*fSum1/length0)/(length0-1.0);\n"
    7750             :         "    double fS2 = (fSumSqr2-fSum2*fSum2/length1)/(length1-1.0);\n"
    7751             :         "    double fF, fF1, fF2;\n"
    7752             :         "    if (fS1 > fS2)\n"
    7753             :         "    {\n"
    7754             :         "        fF = fS1/fS2;\n"
    7755             :         "        fF1 = length0-1.0;\n"
    7756             :         "        fF2 = length1-1.0;\n"
    7757             :         "    }\n"
    7758             :         "    else\n"
    7759             :         "    {\n"
    7760             :         "        fF = fS2/fS1;\n"
    7761             :         "        fF1 = length1-1.0;\n"
    7762             :         "        fF2 = length0-1.0;\n"
    7763             :         "    }\n"
    7764           0 :         "    tmp = 2.0*GetFDist(fF, fF1, fF2);\n";
    7765           0 :     ss << "    return tmp;\n";
    7766           0 :     ss << "}";
    7767           0 : }
    7768           0 : void OpB::BinInlineFun(std::set<std::string>& decls,
    7769             :     std::set<std::string>& funs)
    7770             : {
    7771             :     //decls.insert(fBigInvDecl);decls.insert(fLogDblMaxDecl);
    7772           0 :     decls.insert(GetBinomDistPMFDecl);decls.insert(MinDecl);
    7773           0 :     decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
    7774           0 :     decls.insert(GetBetaDistDecl);decls.insert(GetBetaDistPDFDecl);
    7775           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetLogBetaDecl);
    7776           0 :     decls.insert(lcl_getLanczosSumDecl); decls.insert(GetBetaDecl);
    7777           0 :     funs.insert(GetBinomDistPMF);funs.insert(lcl_GetBinomDistRange);
    7778           0 :     funs.insert(GetBetaDist);funs.insert(GetBetaDistPDF);
    7779           0 :     funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetLogBeta);
    7780           0 :     funs.insert(lcl_getLanczosSum);funs.insert(GetBeta);
    7781           0 : }
    7782             : 
    7783           0 : void OpB::GenSlidingWindowFunction(std::stringstream &ss,
    7784             :             const std::string &sSymName, SubArguments &vSubArguments)
    7785             : {
    7786           0 :     ss << "\ndouble " << sSymName;
    7787           0 :     ss << "_"<< BinFuncName() <<"(";
    7788           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7789             :     {
    7790           0 :         if (i)
    7791           0 :             ss << ",";
    7792           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7793             :     }
    7794           0 :     ss << ") {\n";
    7795           0 :     ss << "    int gid0=get_global_id(0);\n";
    7796           0 :     ss << "    double min = 2.22507e-308;\n";
    7797           0 :     ss << "    double tmp;\n";
    7798           0 :     ss << "    double arg0,arg1,arg2,arg3;\n";
    7799           0 :     size_t i = vSubArguments.size();
    7800           0 :     size_t nItems = 0;
    7801           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7802             :     {
    7803           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7804             :         assert(pCur);
    7805           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    7806             :         {
    7807             :             const formula::DoubleVectorRefToken* pDVR =
    7808           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    7809           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    7810           0 :             ss << "for (int i = ";
    7811           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    7812             : #ifdef  ISNAN
    7813           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    7814           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    7815             : #else
    7816             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    7817             : #endif
    7818           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    7819             : #ifdef  ISNAN
    7820           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    7821           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    7822             : #else
    7823             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    7824             : #endif
    7825           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    7826             : #ifdef  ISNAN
    7827           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    7828           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    7829             : #else
    7830             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7831             : #endif
    7832             :             }
    7833             :             else {
    7834             : #ifdef  ISNAN
    7835           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    7836             : #else
    7837             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    7838             : #endif
    7839             :             }
    7840           0 :             nItems += nCurWindowSize;
    7841             :         }
    7842           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    7843             :         {
    7844             : #ifdef  ISNAN
    7845             :             const formula::SingleVectorRefToken* pSVR =
    7846           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7847           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    7848           0 :             ss << "    {\n";
    7849           0 :             ss << "        if (isNan(";
    7850           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7851           0 :             ss << "))\n";
    7852           0 :             ss << "            arg"<<i<<"= 0;\n";
    7853           0 :             ss << "        else\n";
    7854           0 :             ss << "            arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    7855           0 :             ss << ";\n";
    7856           0 :             ss << "    }\n";
    7857           0 :             ss << "    else\n";
    7858           0 :             ss << "        arg"<<i<<"= 0;\n";
    7859             : #endif
    7860             :         }
    7861           0 :         else if (pCur->GetType() == formula::svDouble)
    7862             :         {
    7863             : #ifdef  ISNAN
    7864           0 :             ss << "    if (isNan(";
    7865           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    7866           0 :             ss << "))\n";
    7867           0 :             ss << "        arg"<<i<<"= 0;\n";
    7868           0 :             ss << "    else\n";
    7869           0 :             ss << "        arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    7870           0 :             ss << ";\n";
    7871             : #endif
    7872             :         }
    7873             :     }
    7874             :     ss << "    double rxs = floor(arg2);\n"
    7875             :     "    double rxe = floor(arg3);\n"
    7876             :     "    double rn = floor(arg0);\n"
    7877             :     "    double rq = (0.5 - arg1) + 0.5;\n"
    7878             :     "    bool bIsValidX = (0.0 <= rxs && rxs <= rxe && rxe <= rn);\n"
    7879             :     "    if (bIsValidX && 0.0 < arg1 && arg1 < 1.0)\n"
    7880             :     "    {\n"
    7881             :     "        if (rxs == rxe)\n"
    7882             :     "            tmp = GetBinomDistPMF(rxs, rn, arg1);\n"
    7883             :     "        else\n"
    7884             :     "        {\n"
    7885             :     "            double fFactor = pow(rq, rn);\n"
    7886             :     "            if (fFactor > min)\n"
    7887             :     "                tmp ="
    7888             :     " lcl_GetBinomDistRange(rn, rxs, rxe, fFactor, arg1, rq);\n"
    7889             :     "            else\n"
    7890             :     "            {\n"
    7891             :     "                fFactor = pow(arg1, rn);\n"
    7892             :     "                if (fFactor > min)\n"
    7893             :     "                {\n"
    7894             :     "                    tmp ="
    7895             :     "lcl_GetBinomDistRange(rn, rn - rxe, rn - rxs, fFactor, rq, arg1);\n"
    7896             :     "            }\n"
    7897             :     "                else\n"
    7898             :     "                    tmp ="
    7899             :     "GetBetaDist(rq, rn - rxe, rxe + 1.0)"
    7900             :     "- GetBetaDist(rq, rn - rxs + 1, rxs);\n"
    7901             :     "            }\n"
    7902             :     "        }\n"
    7903             :     "    }\n"
    7904             :     "    else\n"
    7905             :     "    {\n"
    7906             :     "        if (bIsValidX)\n"
    7907             :     "        {\n"
    7908             :     "            if (arg1 == 0.0)\n"
    7909             :     "                tmp = (rxs == 0.0 ? 1.0 : 0.0);\n"
    7910             :     "            else if (arg1 == 1.0)\n"
    7911             :     "                tmp = (rxe == rn ? 1.0 : 0.0);\n"
    7912             :     "            else\n"
    7913             :     "            {\n"
    7914             :     "                tmp = DBL_MIN;\n"
    7915             :     "            }\n"
    7916             :     "        }\n"
    7917             :     "        else\n"
    7918             :     "        {\n"
    7919             :     "            tmp = DBL_MIN;\n"
    7920             :     "        }\n"
    7921             :     "    }\n"
    7922             :     "    return tmp;"
    7923           0 :     "}\n";
    7924           0 : }
    7925           0 : void OpBetaDist::BinInlineFun(std::set<std::string>& decls,
    7926             :     std::set<std::string>& funs)
    7927             : {
    7928           0 :     decls.insert(fMachEpsDecl);decls.insert(fMaxGammaArgumentDecl);
    7929           0 :     decls.insert(GetBetaDistDecl);decls.insert(GetBetaDistPDFDecl);
    7930           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);decls.insert(GetLogBetaDecl);
    7931           0 :     decls.insert(GetBetaDecl);decls.insert(lcl_getLanczosSumDecl);
    7932           0 :     funs.insert(GetBetaDist);funs.insert(GetBetaDistPDF);
    7933           0 :     funs.insert(lcl_GetBetaHelperContFrac);funs.insert(GetLogBeta);
    7934           0 :     funs.insert(GetBeta);funs.insert(lcl_getLanczosSum);
    7935           0 : }
    7936           0 : void OpPoisson::BinInlineFun(std::set<std::string>& decls,
    7937             :     std::set<std::string>& funs)
    7938             : {
    7939           0 :     decls.insert(fHalfMachEpsDecl);
    7940           0 :     funs.insert("");
    7941           0 :     decls.insert(fMaxGammaArgumentDecl);
    7942           0 :     funs.insert("");
    7943           0 :     decls.insert(fBigInvDecl);
    7944           0 :     funs.insert("");
    7945           0 :     decls.insert(GetLogGammaDecl);
    7946           0 :     funs.insert(GetLogGamma);
    7947           0 :     decls.insert(lcl_GetLogGammaHelperDecl);
    7948           0 :     funs.insert(lcl_GetLogGammaHelper);
    7949           0 :     decls.insert(lcl_GetGammaHelperDecl);
    7950           0 :     funs.insert(lcl_GetGammaHelper);
    7951           0 :     decls.insert(lcl_getLanczosSumDecl);
    7952           0 :     funs.insert(lcl_getLanczosSum);
    7953           0 :     decls.insert(GetUpRegIGammaDecl);
    7954           0 :     funs.insert(GetUpRegIGamma);
    7955           0 :     decls.insert(GetGammaContFractionDecl);
    7956           0 :     funs.insert(GetGammaContFraction);
    7957           0 :     decls.insert(GetGammaSeriesDecl);
    7958           0 :     funs.insert(GetGammaSeries);
    7959           0 : }
    7960           0 : void OpPoisson::GenSlidingWindowFunction(
    7961             :     std::stringstream &ss,const std::string &sSymName,
    7962             :     SubArguments &vSubArguments)
    7963             : {
    7964           0 :     ss << "\ndouble " << sSymName;
    7965           0 :     ss << "_"<< BinFuncName() <<"(";
    7966           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    7967             :     {
    7968           0 :         if (i)
    7969           0 :             ss << ",";
    7970           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    7971             :     }
    7972           0 :     ss << ")\n";
    7973           0 :     ss << "{\n";
    7974           0 :     ss << "    double x,lambda,tmp,tmp0,tmp1,tmp2;\n";
    7975           0 :     ss << "    int bCumulative;\n";
    7976           0 :     ss << "    int gid0=get_global_id(0);\n";
    7977           0 :     size_t i = vSubArguments.size();
    7978           0 :     ss <<"\n    ";
    7979           0 :     for (i = 0; i < vSubArguments.size(); i++)
    7980             :     {
    7981           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    7982             :         assert(pCur);
    7983           0 :         if (pCur->GetType() == formula::svSingleVectorRef)
    7984             :         {
    7985             : #ifdef  ISNAN
    7986             :             const formula::SingleVectorRefToken* pSVR =
    7987           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    7988           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    7989             : #endif
    7990             :         }
    7991           0 :         else if (pCur->GetType() == formula::svDouble)
    7992             :         {
    7993             : #ifdef  ISNAN
    7994           0 :             ss << "{\n";
    7995             : #endif
    7996             :         }
    7997             :         else
    7998             :         {
    7999             : #ifdef  ISNAN
    8000             : #endif
    8001             :         }
    8002             : #ifdef  ISNAN
    8003           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    8004             :         {
    8005           0 :             ss << "    if (isNan(";
    8006           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8007           0 :             ss << "))\n";
    8008           0 :             ss << "        tmp"<<i<<"= 0;\n";
    8009           0 :             ss << "    else\n";
    8010           0 :             ss << "        tmp"<<i<<"=\n";
    8011           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8012           0 :             ss << ";\n}\n";
    8013             :         }
    8014             :         else
    8015             :         {
    8016           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    8017           0 :             ss <<";\n";
    8018             :         }
    8019             : #endif
    8020             :     }
    8021           0 :     ss << "    x = floor(tmp0);\n";
    8022           0 :     ss << "    lambda = tmp1;\n";
    8023           0 :     ss << "    bCumulative = tmp2;\n ";
    8024           0 :     ss << "    if (!bCumulative)\n";
    8025           0 :     ss << "    {\n";
    8026           0 :     ss << "        if(lambda == 0.0)\n";
    8027           0 :     ss << "        {\n";
    8028           0 :     ss << "            return 0;\n";
    8029           0 :     ss << "        }\n";
    8030           0 :     ss << "        else\n";
    8031           0 :     ss << "        {\n";
    8032           0 :     ss << "            if (lambda >712)\n";
    8033           0 :     ss << "            {\n";
    8034           0 :     ss << "            tmp = (exp(x*log(lambda)-lambda-GetLogGamma(x+1.0)));\n";
    8035           0 :     ss << "            return tmp;\n";
    8036           0 :     ss << "            }\n";
    8037           0 :     ss << "            else\n";
    8038           0 :     ss << "            {\n";
    8039           0 :     ss << "                double fPoissonVar = 1.0;\n";
    8040           0 :     ss << "                for ( int f = 0; f < x; ++f )\n";
    8041           0 :     ss << "          fPoissonVar *= lambda * pow(( (double)f + 1.0 ),-1);\n";
    8042           0 :     ss << "                tmp = ( fPoissonVar * exp( -lambda ) );\n";
    8043           0 :     ss << "                return tmp;\n";
    8044           0 :     ss << "            }\n";
    8045           0 :     ss << "        }\n";
    8046           0 :     ss << "     } \n";
    8047           0 :     ss << "     else\n";
    8048           0 :     ss << "     {\n";
    8049           0 :     ss << "         if (lambda == 0.0)\n";
    8050           0 :     ss << "         {\n";
    8051           0 :     ss << "             return 1;\n";
    8052           0 :     ss << "         }\n";
    8053           0 :     ss << "         else\n";
    8054           0 :     ss << "         {\n";
    8055           0 :     ss << "             if (lambda > 712 )\n";
    8056           0 :     ss << "             {\n";
    8057           0 :     ss << "                 tmp = (GetUpRegIGamma(x+1.0,lambda));\n";
    8058           0 :     ss << "                 return tmp;\n";
    8059           0 :     ss << "             }\n";
    8060           0 :     ss << "             else\n";
    8061           0 :     ss << "             {\n";
    8062           0 :     ss << "                 if (x >= 936.0)\n";
    8063           0 :     ss << "                 {\n";
    8064           0 :     ss << "                     return 1;\n";
    8065           0 :     ss << "                 }\n";
    8066           0 :     ss << "                 else\n";
    8067           0 :     ss << "                 {\n";
    8068           0 :     ss << "                     double fSummand = exp(-lambda);\n";
    8069           0 :     ss << "                     double fSum = fSummand;\n";
    8070           0 :     ss << "                     int nEnd = (int) (x + 0.5);\n";
    8071           0 :     ss << "                     for (int i = 1; i <= nEnd; i++)\n";
    8072           0 :     ss << "                     {\n";
    8073           0 :     ss << "                fSummand = (fSummand*lambda)*pow((double)i,-1);\n";
    8074           0 :     ss << "                         fSum += fSummand;\n";
    8075           0 :     ss << "                     }\n";
    8076           0 :     ss << "                     tmp = fSum;\n";
    8077           0 :     ss << "                     return tmp;\n";
    8078           0 :     ss << "                 }\n";
    8079           0 :     ss << "             }\n";
    8080           0 :     ss << "         }\n";
    8081           0 :     ss << "     }\n";
    8082           0 :     ss << "}\n";
    8083           0 : }
    8084           0 : void OpCovar::GenSlidingWindowFunction(std::stringstream& ss,
    8085             :     const std::string &sSymName, SubArguments& vSubArguments)
    8086             : {
    8087           0 :         ss << "\ndouble " << sSymName;
    8088           0 :         ss << "_"<< BinFuncName() <<"(";
    8089           0 :         for (unsigned i = 0; i < vSubArguments.size(); i++)
    8090             :         {
    8091           0 :             if (i)
    8092           0 :                 ss << ",";
    8093           0 :             vSubArguments[i]->GenSlidingWindowDecl(ss);
    8094             :         }
    8095           0 :         ss << ") {\n";
    8096           0 :         ss << "    int gid0 = get_global_id(0);\n";
    8097           0 :         ss << "    double vSum = 0.0;\n";
    8098           0 :         ss << "    double vSum0 = 0.0;\n";
    8099           0 :         ss << "    double vSum1 = 0.0;\n";
    8100           0 :         ss << "    double vMean0 = 0.0;\n";
    8101           0 :         ss << "    double vMean1 = 0.0;\n";
    8102           0 :         ss << "    double arg0 = 0.0;\n";
    8103           0 :         ss << "    double arg1 = 0.0;\n";
    8104           0 :         FormulaToken* pCurX = vSubArguments[0]->GetFormulaToken();
    8105           0 :         FormulaToken* pCurY = vSubArguments[1]->GetFormulaToken();
    8106           0 :         if ((pCurX->GetType() == formula::svDoubleVectorRef)&&
    8107           0 :             (pCurY->GetType() == formula::svDoubleVectorRef)){
    8108           0 :         ss << "    int cnt = 0;\n";
    8109             :         const formula::DoubleVectorRefToken* pCurDVRX =
    8110           0 :             static_cast<const formula::DoubleVectorRefToken* >(pCurX);
    8111             :         const formula::DoubleVectorRefToken* pCurDVRY =
    8112           0 :             static_cast<const formula::DoubleVectorRefToken* >(pCurY);
    8113           0 :         size_t nCurWindowSizeX = pCurDVRX->GetRefRowSize();
    8114           0 :         size_t nCurWindowSizeY = pCurDVRY->GetRefRowSize();
    8115           0 :         if(nCurWindowSizeX == nCurWindowSizeY)
    8116             :         {
    8117           0 :             ss << "    for(int i = ";
    8118           0 :             if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    8119           0 :                 ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n";
    8120           0 :                 ss << "        arg0 = ";
    8121           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef()<< ";\n";
    8122           0 :                 ss << "        arg1 = ";
    8123           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8124             : #ifdef ISNAN
    8125           0 :                 ss << "        if(isNan(arg0) || isNan(arg1) || (i >= ";
    8126           0 :                 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    8127           0 :                 ss << pCurDVRY->GetArrayLength() << ")) {\n";
    8128           0 :                 ss << "            arg0 = 0.0;\n";
    8129           0 :                 ss << "            arg1 = 0.0;\n";
    8130           0 :                 ss << "            --cnt;\n";
    8131           0 :                 ss << "        }\n";
    8132             : #endif
    8133           0 :                 ss << "        ++cnt;\n";
    8134           0 :                 ss << "        vSum0 += arg0;\n";
    8135           0 :                 ss << "        vSum1 += arg1;\n";
    8136           0 :                 ss << "    }\n";
    8137             :             }
    8138           0 :             else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
    8139           0 :                 ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n";
    8140           0 :                 ss << "        arg0 = ";
    8141           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8142           0 :                 ss << "        arg1 = ";
    8143           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8144             : #ifdef ISNAN
    8145           0 :                 ss << "        if(isNan(arg0) || isNan(arg1) || (i >= ";
    8146           0 :                 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    8147           0 :                 ss << pCurDVRY->GetArrayLength() << ")) {\n";
    8148           0 :                 ss << "            arg0 = 0.0;\n";
    8149           0 :                 ss << "            arg1 = 0.0;\n";
    8150           0 :                 ss << "            --cnt;\n";
    8151           0 :                 ss << "        }\n";
    8152             : #endif
    8153           0 :                 ss << "        ++cnt;\n";
    8154           0 :                 ss << "        vSum0 += arg0;\n";
    8155           0 :                 ss << "        vSum1 += arg1;\n";
    8156           0 :                 ss << "    }\n";
    8157             :             }
    8158           0 :             else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    8159           0 :                 ss << "0; i < " << nCurWindowSizeX << "; i++) {\n";
    8160           0 :                 ss << "        arg0 = ";
    8161           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8162           0 :                 ss << "        arg1 = ";
    8163           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8164             : #ifdef ISNAN
    8165           0 :                 ss << "        if(isNan(arg0) || isNan(arg1) || (i >= ";
    8166           0 :                 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    8167           0 :                 ss << pCurDVRY->GetArrayLength() << ")) {\n";
    8168           0 :                 ss << "            arg0 = 0.0;\n";
    8169           0 :                 ss << "            arg1 = 0.0;\n";
    8170           0 :                 ss << "            --cnt;\n";
    8171           0 :                 ss << "        }\n";
    8172             : #endif
    8173           0 :                 ss << "        ++cnt;\n";
    8174           0 :                 ss << "        vSum0 += arg0;\n";
    8175           0 :                 ss << "        vSum1 += arg1;\n";
    8176           0 :                 ss << "    }\n";
    8177             :             }
    8178             :             else {
    8179           0 :                 ss << "0; i < " << nCurWindowSizeX << "; i++) {\n";
    8180           0 :                 ss << "        arg0 = ";
    8181           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8182           0 :                 ss << "        arg1 = ";
    8183           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8184             : #ifdef ISNAN
    8185           0 :                 ss << "        if(isNan(arg0) || isNan(arg1) || (i + gid0 >= ";
    8186           0 :                 ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >=";
    8187           0 :                 ss << pCurDVRY->GetArrayLength() << ")) {\n";
    8188           0 :                 ss << "            arg0 = 0.0;\n";
    8189           0 :                 ss << "            arg1 = 0.0;\n";
    8190           0 :                 ss << "            --cnt;\n";
    8191           0 :                 ss << "        }\n";
    8192             : #endif
    8193           0 :                 ss << "        ++cnt;\n";
    8194           0 :                 ss << "        vSum0 += arg0;\n";
    8195           0 :                 ss << "        vSum1 += arg1;\n";
    8196           0 :                 ss << "    }\n";
    8197             :             }
    8198           0 :             ss << "    if(cnt < 1) {\n";
    8199           0 :             ss << "        return -DBL_MAX;\n";
    8200           0 :             ss << "    }\n";
    8201           0 :             ss << "    else {\n";
    8202           0 :             ss << "        vMean0 = vSum0 / cnt;\n";
    8203           0 :             ss << "        vMean1 = vSum1 / cnt;\n";
    8204           0 :             ss << "    for(int i = ";
    8205           0 :             if (!pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    8206           0 :                 ss << "gid0; i < " << nCurWindowSizeX << "; i++) {\n";
    8207           0 :                 ss << "arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef()
    8208           0 :                     << ";\n";
    8209           0 :                 ss << "arg1 = " << vSubArguments[1]->GenSlidingWindowDeclRef()
    8210           0 :                     << ";\n";
    8211             : #ifdef ISNAN
    8212           0 :                 ss << "        if(isNan(arg0) || isNan(arg1) || (i >= ";
    8213           0 :                 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    8214           0 :                 ss << pCurDVRY->GetArrayLength() << ")) {\n";
    8215           0 :                 ss << "            arg0 = vMean0;\n";
    8216           0 :                 ss << "            arg1 = vMean1;\n";
    8217           0 :                 ss << "        }\n";
    8218             : #endif
    8219           0 :                 ss << "        vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
    8220           0 :                 ss << "    }\n";
    8221           0 :             } else if (pCurDVRX->IsStartFixed() && !pCurDVRX->IsEndFixed()) {
    8222           0 :                 ss << "0; i < gid0 + " << nCurWindowSizeX << "; i++) {\n";
    8223           0 :                 ss << "        arg0 = ";
    8224           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8225           0 :                 ss << "        arg1 = ";
    8226           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8227             : #ifdef ISNAN
    8228           0 :                 ss << "        if(isNan(arg0) || isNan(arg1) || (i >= ";
    8229           0 :                 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    8230           0 :                 ss << pCurDVRY->GetArrayLength() << ")) {\n";
    8231           0 :                 ss << "            arg0 = vMean0;\n";
    8232           0 :                 ss << "            arg1 = vMean1;\n";
    8233           0 :                 ss << "        }\n";
    8234             : #endif
    8235           0 :                 ss << "        vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
    8236           0 :                 ss << "    }\n";
    8237           0 :             } else if (pCurDVRX->IsStartFixed() && pCurDVRX->IsEndFixed()) {
    8238           0 :                 ss << "0; i < " << nCurWindowSizeX << "; i++) {\n";
    8239           0 :                 ss << "        arg0 = ";
    8240           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8241           0 :                 ss << "        arg1 = ";
    8242           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8243             : #ifdef ISNAN
    8244           0 :                 ss << "        if(isNan(arg0) || isNan(arg1) || (i >= ";
    8245           0 :                 ss << pCurDVRX->GetArrayLength() << ") || (i >=";
    8246           0 :                 ss << pCurDVRY->GetArrayLength() << ")) {\n";
    8247           0 :                 ss << "            arg0 = vMean0;\n";
    8248           0 :                 ss << "            arg1 = vMean1;\n";
    8249           0 :                 ss << "        }\n";
    8250             : #endif
    8251           0 :                 ss << "        vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
    8252           0 :                 ss << "    }\n";
    8253             :             } else {
    8254           0 :                 ss << "0; i < " << nCurWindowSizeX << "; i++) {\n";
    8255           0 :                 ss << "        arg0 = ";
    8256           0 :                 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
    8257           0 :                 ss << "        arg1 = ";
    8258           0 :                 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
    8259             : #ifdef ISNAN
    8260           0 :                 ss << "        if(isNan(arg0) || isNan(arg1) || (i + gid0 >= ";
    8261           0 :                 ss << pCurDVRX->GetArrayLength() << ") || (i + gid0 >= ";
    8262           0 :                 ss << pCurDVRY->GetArrayLength() << ")) {\n";
    8263           0 :                 ss << "            arg0 = vMean0;\n";
    8264           0 :                 ss << "            arg1 = vMean1;\n";
    8265           0 :                 ss << "        }\n";
    8266             : #endif
    8267           0 :                 ss << "        vSum += (arg0 - vMean0) * (arg1 - vMean1);\n";
    8268           0 :                 ss << "    }\n";
    8269             :             }
    8270           0 :             ss << "    return vSum / cnt;\n";
    8271           0 :             ss << "    }\n";
    8272           0 :             ss << "}";
    8273             :         }
    8274             :         }
    8275             :         else {
    8276           0 :         ss << "    int cnt0 = 0,cnt1 = 0;\n";
    8277           0 :         for (unsigned i = 0; i < vSubArguments.size(); i++)
    8278             :         {
    8279           0 :             FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
    8280           0 :             if (pCur->GetType() == formula::svSingleVectorRef){
    8281             : #ifdef  ISNAN
    8282             :                 const formula::SingleVectorRefToken* pTVR =
    8283           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    8284           0 :                 ss << "    if(isNan(";
    8285           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8286           0 :                 ss << ") || gid0 >= " << pTVR->GetArrayLength() << ")\n";
    8287           0 :                 ss << "        arg" << i << " = 0;\n    else\n";
    8288             : #endif
    8289           0 :                 ss << "        arg" << i << " = ";
    8290           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8291           0 :                 ss << "    cnt" << i << "++;\n";
    8292           0 :                 ss << "    vSum" << i << " += arg" << i << ";\n";
    8293             :             }
    8294           0 :             else if (pCur->GetType() == formula::svDouble){
    8295             : #ifdef  ISNAN
    8296           0 :                 ss << "    if(isNan ( ";
    8297           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << "))\n";
    8298           0 :                 ss << "        arg" << i << " = 0;\n    else\n";
    8299             : #endif
    8300           0 :                 ss << "        arg" << i << " = ";
    8301           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8302           0 :                 ss << "    cnt" << i << "++;\n";
    8303           0 :                 ss << "    vSum" << i << " += arg" << i << ";\n";
    8304             :             }
    8305             :             else {
    8306           0 :                 ss << "    arg" << i << " = ";
    8307           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8308           0 :                 ss << "    cnt" << i << "++;\n";
    8309           0 :                 ss << "    vSum" << i << " += arg" << i << ";\n";
    8310             :             }
    8311             :         }
    8312           0 :         ss << "        vMean0 = vSum0 / cnt0;\n";
    8313           0 :         ss << "        vMean1 = vSum0 / cnt1;\n";
    8314           0 :         for(unsigned i = 0; i < vSubArguments.size(); i++ ) {
    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 << " = vMean" << i << ";\n";
    8324           0 :                 ss << "    else\n";
    8325             : #endif
    8326           0 :                 ss << "        arg" << i << " = ";
    8327           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8328             :             }
    8329           0 :             else if (pCur->GetType() == formula::svDouble) {
    8330           0 :                 ss << "    arg" << i << " = ";
    8331           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8332             : #ifdef  ISNAN
    8333           0 :                 ss << "    if(isNan(arg" << i << "))\n";
    8334           0 :                 ss << "        arg" << i << " = vMean" << i << ";\n";
    8335             : #endif
    8336             :             }
    8337             :             else {
    8338           0 :                 ss << "    arg" << i << " = ";
    8339           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8340             :             }
    8341             :         }
    8342           0 :         ss << "        vSum += (arg0 - vMean0) * ( arg1 - vMean1 );\n";
    8343           0 :         ss << "    return vSum / cnt0;\n";
    8344           0 :         ss << "}";
    8345             :         }
    8346           0 : }
    8347           0 : void OpBetaDist::GenSlidingWindowFunction(std::stringstream &ss,
    8348             :             const std::string &sSymName, SubArguments &vSubArguments)
    8349             : {
    8350           0 :     ss << "\ndouble " << sSymName;
    8351           0 :     ss << "_"<< BinFuncName() <<"(";
    8352           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    8353             :     {
    8354           0 :         if (i)
    8355           0 :             ss << ",";
    8356           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    8357             :     }
    8358           0 :     ss << ") {\n";
    8359           0 :     ss << "    int gid0=get_global_id(0);\n";
    8360           0 :     ss << "    double tmp;\n";
    8361           0 :     ss << "    double arg0,arg1,arg2,arg3,arg4,arg5;\n";
    8362           0 :     size_t i = vSubArguments.size();
    8363           0 :     size_t nItems = 0;
    8364           0 :     for (i = 0; i < vSubArguments.size(); i++)
    8365             :     {
    8366           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    8367             :         assert(pCur);
    8368           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    8369             :         {
    8370             :             const formula::DoubleVectorRefToken* pDVR =
    8371           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    8372           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    8373           0 :             ss << "for (int i = ";
    8374           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    8375             : #ifdef  ISNAN
    8376           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    8377           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    8378             : #else
    8379             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    8380             : #endif
    8381           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    8382             : #ifdef  ISNAN
    8383           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    8384           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    8385             : #else
    8386             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n ";
    8387             : #endif
    8388           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    8389             : #ifdef  ISNAN
    8390           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    8391           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    8392             : #else
    8393             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    8394             : #endif
    8395             :             }
    8396             :             else {
    8397             : #ifdef  ISNAN
    8398           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    8399             : #else
    8400             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    8401             : #endif
    8402             :             }
    8403           0 :             nItems += nCurWindowSize;
    8404             :         }
    8405           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    8406             :         {
    8407             : #ifdef  ISNAN
    8408             :             const formula::SingleVectorRefToken* pSVR =
    8409           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    8410           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    8411           0 :             ss << "    {\n";
    8412           0 :             ss << "        if (isNan(";
    8413           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8414           0 :             ss << "))\n";
    8415           0 :             ss << "            arg"<<i<<"= 0;\n";
    8416           0 :             ss << "        else\n";
    8417           0 :             ss << "            arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    8418           0 :             ss << ";\n";
    8419           0 :             ss << "    }\n";
    8420           0 :             ss << "    else\n";
    8421           0 :             ss << "        arg"<<i<<"= 0;\n";
    8422             : #endif
    8423             :         }
    8424           0 :         else if (pCur->GetType() == formula::svDouble)
    8425             :         {
    8426             : #ifdef  ISNAN
    8427           0 :             ss << "    if (isNan(";
    8428           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8429           0 :             ss << "))\n";
    8430           0 :             ss << "        arg"<<i<<"= 0;\n";
    8431           0 :             ss << "    else\n";
    8432           0 :             ss << "        arg"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    8433           0 :             ss << ";\n";
    8434             : #endif
    8435             :         }
    8436             :     }
    8437             :     ss << "    double fScale = arg4 - arg3;\n"
    8438             :     "    if (fScale <= 0.0 || arg1 <= 0.0 || arg2 <= 0.0)\n"
    8439             :     "    {\n"
    8440             :     "        tmp = DBL_MIN;\n"
    8441             :     "        return tmp;\n"
    8442             :     "    }\n"
    8443             :     "    if (arg5)\n"
    8444             :     "    {\n"
    8445             :     "        if (arg0< arg3)\n"
    8446             :     "        {\n"
    8447             :     "            tmp = 0.0;\n"
    8448             :     "            return tmp;\n"
    8449             :     "        }\n"
    8450             :     "        if (arg0 > arg4)\n"
    8451             :     "        {\n"
    8452             :     "            tmp = 1.0;\n"
    8453             :     "            return tmp;\n"
    8454             :     "        }\n"
    8455             :     "        arg0 = (arg0-arg3)*pow(fScale,-1);\n"
    8456             :     "        tmp =  GetBetaDist(arg0, arg1, arg2);\n"
    8457             :     "    }\n"
    8458             :     "    else\n"
    8459             :     "    {\n"
    8460             :     "        if (arg0 < arg3 || arg0 > arg4 )\n"
    8461             :     "        {\n"
    8462             :     "            tmp = 0.0;\n"
    8463             :     "            return tmp;\n"
    8464             :     "        }\n"
    8465             :     "        arg0 = (arg0 - arg3)*pow(fScale,-1);\n"
    8466             :     "        tmp = GetBetaDistPDF(arg0, arg1, arg2)*pow(fScale,-1);\n"
    8467           0 :     "    }\n";
    8468           0 :     ss << "    return tmp;\n";
    8469           0 :     ss << "}\n";
    8470           0 : }
    8471           0 : void OpBetainv::BinInlineFun(std::set<std::string>& decls,
    8472             :     std::set<std::string>& funs)
    8473             : {
    8474           0 :     decls.insert(fMachEpsDecl);
    8475           0 :     funs.insert("");
    8476           0 :     decls.insert(fMaxGammaArgumentDecl);
    8477           0 :     funs.insert("");
    8478           0 :     decls.insert(lcl_IterateInverseBetaInvDecl);
    8479           0 :     funs.insert(lcl_IterateInverseBetaInv);
    8480           0 :     decls.insert(GetBetaDistDecl);
    8481           0 :     funs.insert(GetBetaDist);
    8482           0 :     decls.insert(lcl_HasChangeOfSignDecl);
    8483           0 :     funs.insert(lcl_HasChangeOfSign);
    8484           0 :     decls.insert(lcl_HasChangeOfSignDecl);
    8485           0 :     funs.insert(lcl_HasChangeOfSign);
    8486           0 :     decls.insert(lcl_HasChangeOfSignDecl);
    8487           0 :     funs.insert(lcl_HasChangeOfSign);
    8488           0 :     decls.insert(lcl_GetBetaHelperContFracDecl);
    8489           0 :     funs.insert(lcl_GetBetaHelperContFrac);
    8490           0 :     decls.insert(GetBetaDistPDFDecl);
    8491           0 :     funs.insert(GetBetaDistPDF);
    8492           0 :     decls.insert(GetLogBetaDecl);
    8493           0 :     funs.insert(GetLogBeta);
    8494           0 :     decls.insert(GetBetaDecl);
    8495           0 :     funs.insert(GetBeta);
    8496           0 :     decls.insert(lcl_getLanczosSumDecl);
    8497           0 :     funs.insert(lcl_getLanczosSum);
    8498           0 : }
    8499           0 : void OpBetainv::GenSlidingWindowFunction(
    8500             :     std::stringstream &ss,const std::string &sSymName,
    8501             :     SubArguments &vSubArguments)
    8502             : {
    8503           0 :     ss << "\ndouble " << sSymName;
    8504           0 :     ss << "_"<< BinFuncName() <<"(";
    8505           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    8506             :     {
    8507           0 :         if (i)
    8508           0 :             ss << ",";
    8509           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    8510             :     }
    8511           0 :     ss << ")\n";
    8512           0 :     ss << "{\n";
    8513           0 :     ss << "    double tmp0,tmp1,tmp2,tmp3,tmp4;\n";
    8514           0 :     ss << "    int gid0=get_global_id(0);\n";
    8515           0 :     size_t i = vSubArguments.size();
    8516           0 :     size_t nItems = 0;
    8517           0 :     ss <<"\n    ";
    8518             :     //while (i-- > 1)
    8519           0 :     for (i = 0; i < vSubArguments.size(); i++)
    8520             :     {
    8521           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    8522             :         assert(pCur);
    8523           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    8524             :         {
    8525             :             const formula::DoubleVectorRefToken* pDVR =
    8526           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    8527           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    8528           0 :             ss << "for (int i = ";
    8529           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    8530             : #ifdef  ISNAN
    8531           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    8532           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    8533             : #else
    8534             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    8535             : #endif
    8536           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    8537             : #ifdef  ISNAN
    8538           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    8539           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    8540             : #else
    8541             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    8542             : #endif
    8543           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    8544             : #ifdef  ISNAN
    8545           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    8546           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n";
    8547             : #else
    8548             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    8549             : #endif
    8550             :             }
    8551             :             else {
    8552             : #ifdef  ISNAN
    8553           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    8554             : #else
    8555             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    8556             : #endif
    8557             :             }
    8558           0 :             nItems += nCurWindowSize;
    8559             :         }
    8560           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    8561             :         {
    8562             : #ifdef  ISNAN
    8563             :             const formula::SingleVectorRefToken* pSVR =
    8564           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    8565           0 :             ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    8566             : #else
    8567             :             nItems += 1;
    8568             : #endif
    8569             :         }
    8570           0 :         else if (pCur->GetType() == formula::svDouble)
    8571             :         {
    8572             : #ifdef  ISNAN
    8573           0 :             ss << "{\n";
    8574             : #endif
    8575           0 :             nItems += 1;
    8576             :         }
    8577             :         else
    8578             :         {
    8579             : #ifdef  ISNAN
    8580             : #endif
    8581           0 :             nItems += 1;
    8582             :         }
    8583             : #ifdef  ISNAN
    8584           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    8585             :         {
    8586           0 :             ss << "    if (isNan(";
    8587           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8588           0 :             ss << "))\n";
    8589           0 :             ss << "        tmp"<<i<<"= 0;\n";
    8590           0 :             ss << "    else\n";
    8591           0 :             ss << "        tmp"<<i<<"=\n";
    8592           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8593           0 :             ss << ";\n}\n";
    8594             :         }
    8595             :         else
    8596             :         {
    8597           0 :             ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    8598           0 :             ss <<";\n";
    8599             :         }
    8600             : #else
    8601             :     ss << "tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
    8602             :     ss <<";\n";
    8603             : #endif
    8604             :     }
    8605           0 :     ss << "    if (tmp0 < 0.0 || tmp0 >= 1.0 ||";
    8606           0 :     ss << "tmp3 == tmp4 || tmp1 <= 0.0 || tmp2 <= 0.0)\n";
    8607           0 :     ss << "    {\n";
    8608           0 :     ss << "        return DBL_MIN;\n";
    8609           0 :     ss << "    }\n";
    8610           0 :     ss << "    if (tmp0 == 0.0)\n";
    8611           0 :     ss << "        return 0.0;\n";
    8612           0 :     ss << "    else\n";
    8613           0 :     ss << "    {";
    8614           0 :     ss << "        bool bConvError;";
    8615           0 :     ss << "        double fVal = lcl_IterateInverseBetaInv";
    8616           0 :     ss << "(tmp0, tmp1, tmp2, 0.0, 1.0, &bConvError);\n";
    8617           0 :     ss << "        if(bConvError)\n";
    8618           0 :     ss << "            return DBL_MIN;\n";
    8619           0 :     ss << "        else\n";
    8620           0 :     ss << "            return (tmp3 + fVal*(tmp4 - tmp3));\n";
    8621           0 :     ss << "    }";
    8622           0 :     ss << "}\n";
    8623           0 : }
    8624           0 : void OpDevSq::GenSlidingWindowFunction(std::stringstream& ss,
    8625             :     const std::string &sSymName, SubArguments& vSubArguments)
    8626             : {
    8627           0 :         ss << "\ndouble " << sSymName;
    8628           0 :         ss << "_"<< BinFuncName() <<"(";
    8629           0 :         for (unsigned i = 0; i < vSubArguments.size(); i++)
    8630             :         {
    8631           0 :             if (i)
    8632           0 :                 ss << ",";
    8633           0 :             vSubArguments[i]->GenSlidingWindowDecl(ss);
    8634             :             }
    8635           0 :         ss << ") {\n";
    8636           0 :         ss << "    int gid0 = get_global_id(0);\n";
    8637           0 :         ss << "    double vSum = 0.0;\n";
    8638           0 :         ss << "    double vMean = 0.0;\n";
    8639           0 :         ss << "    int cnt = 0;\n";
    8640           0 :         for(unsigned i = 0; i < vSubArguments.size(); i++ )
    8641             :         {
    8642           0 :         ss << "    double arg" << i << " = 0.0;\n";
    8643           0 :         FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
    8644           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    8645             :         {
    8646             :         const formula::DoubleVectorRefToken* pCurDVR =
    8647           0 :             static_cast<const formula::DoubleVectorRefToken* >(pCur);
    8648           0 :         size_t nCurWindowSize = pCurDVR->GetRefRowSize();
    8649           0 :         ss << "    for(int i = ";
    8650           0 :         if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
    8651           0 :             ss << "gid0; i < " << nCurWindowSize << "; i++) {\n";
    8652           0 :             ss << "        arg" << i << " = ";
    8653           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8654             : #ifdef ISNAN
    8655           0 :             ss << "        if(isNan(arg" << i << ") || (i >= ";
    8656           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8657           0 :             ss << "            arg" << i << " = 0.0;\n";
    8658           0 :             ss << "            --cnt;\n";
    8659           0 :             ss << "        }\n";
    8660             : #endif
    8661           0 :             ss << "        ++cnt;\n";
    8662           0 :             ss << "        vSum += arg" << i << ";\n";
    8663           0 :             ss << "    }\n";
    8664           0 :         } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
    8665           0 :             ss << "0; i < gid0 + " << nCurWindowSize << "; i++) {\n";
    8666           0 :             ss << "        arg" << i << " = ";
    8667           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8668             : #ifdef ISNAN
    8669           0 :             ss << "        if(isNan(arg" << i << ") || (i >= ";
    8670           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8671           0 :             ss << "            arg" << i << " = 0.0;\n";
    8672           0 :             ss << "            --cnt;\n";
    8673           0 :             ss << "        }\n";
    8674             : #endif
    8675           0 :             ss << "        ++cnt;\n";
    8676           0 :             ss << "        vSum += arg" << i << ";\n";
    8677           0 :             ss << "    }\n";
    8678           0 :         } else if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
    8679           0 :             ss << "0; i < " << nCurWindowSize << "; i++) {\n";
    8680           0 :             ss << "        arg" << i << " = ";
    8681           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8682             : #ifdef ISNAN
    8683           0 :             ss << "        if(isNan(arg" << i << ") || (i >= ";
    8684           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8685           0 :             ss << "            arg" << i << " = 0.0;\n";
    8686           0 :             ss << "            --cnt;\n";
    8687           0 :             ss << "        }\n";
    8688             : #endif
    8689           0 :             ss << "        ++cnt;\n";
    8690           0 :             ss << "        vSum += arg" << i << ";\n";
    8691           0 :             ss << "    }\n";
    8692             :         } else {
    8693           0 :             ss << "0; i < " << nCurWindowSize << "; i++) {\n";
    8694           0 :             ss << "        arg" << i << " = ";
    8695           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8696             : #ifdef ISNAN
    8697           0 :             ss << "        if(isNan(arg" << i << ") || (i + gid0 >= ";
    8698           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8699           0 :             ss << "            arg" << i << " = 0.0;\n";
    8700           0 :             ss << "            --cnt;\n";
    8701           0 :             ss << "        }\n";
    8702             : #endif
    8703           0 :             ss << "        ++cnt;\n";
    8704           0 :             ss << "        vSum += arg" << i << ";\n";
    8705           0 :             ss << "    }\n";
    8706             :         }
    8707             : }
    8708           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    8709             :         {
    8710             : #ifdef  ISNAN
    8711             :             const formula::SingleVectorRefToken* pTVR =
    8712           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    8713           0 :             ss << "    if(isNan(";
    8714           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8715           0 :             ss << ") || gid0 >= " << pTVR->GetArrayLength() << ")\n";
    8716           0 :             ss << "        arg" << i << " = 0;\n    else\n";
    8717             : #endif
    8718           0 :             ss << "        arg" << i << " = ";
    8719           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8720           0 :             ss << "    cnt++;\n";
    8721           0 :             ss << "    vSum += arg" << i << ";\n";
    8722             :         }
    8723           0 :         else if (pCur->GetType() == formula::svDouble)
    8724             :         {
    8725             : #ifdef  ISNAN
    8726           0 :             ss << "    if(isNan ( ";
    8727           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << "))\n";
    8728           0 :             ss << "        arg" << i << " = 0;\n    else\n";
    8729             : #endif
    8730           0 :             ss << "        arg" << i << " = ";
    8731           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8732           0 :             ss << "    cnt++;\n";
    8733           0 :             ss << "    vSum += arg" << i << ";\n";
    8734             :         }
    8735             :         else
    8736             :         {
    8737           0 :             ss << "    arg" << i << " = ";
    8738           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    8739           0 :             ss << "    cnt++;\n";
    8740           0 :             ss << "    vSum += arg" << i << ";\n";
    8741             :         }
    8742             :             }
    8743           0 :         ss << "    vMean = vSum / cnt;\n";
    8744           0 :         ss << "    vSum = 0.0;\n";
    8745           0 :         for(unsigned k = 0; k < vSubArguments.size(); k++ )
    8746             :         {
    8747           0 :         FormulaToken* pCur = vSubArguments[k]->GetFormulaToken();
    8748           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    8749             :         {
    8750             :         const formula::DoubleVectorRefToken* pCurDVR =
    8751           0 :             static_cast<const formula::DoubleVectorRefToken* >(pCur);
    8752           0 :         size_t nCurWindowSize = pCurDVR->GetRefRowSize();
    8753           0 :         ss << "    for(int i = ";
    8754           0 :         if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
    8755           0 :             ss << "gid0; i < " << nCurWindowSize << "; i++) {\n";
    8756           0 :             ss << "        arg" << k << " = ";
    8757           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8758             : #ifdef ISNAN
    8759           0 :             ss << "        if(isNan( arg" << k << " ) || (i >= ";
    8760           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8761           0 :             ss << "            arg" << k << " = vXMean;\n";
    8762           0 :             ss << "        }\n";
    8763             : #endif
    8764           0 :             ss << "        vSum += pow( arg" << k << " - vMean, 2 );\n";
    8765           0 :             ss << "    }\n";
    8766           0 :         } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
    8767           0 :             ss << "0; i < gid0 + " << nCurWindowSize << "; i++) {\n";
    8768           0 :             ss << "        arg" << k << " = ";
    8769           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8770             : #ifdef ISNAN
    8771           0 :             ss << "        if(isNan( arg" << k << ") || (i >= ";
    8772           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8773           0 :             ss << "            arg" << k << " = vMean;\n";
    8774           0 :             ss << "        }\n";
    8775             : #endif
    8776           0 :             ss << "        vSum += pow( arg" << k << " - vMean, 2 );\n";
    8777           0 :             ss << "    }\n";
    8778           0 :         } else if (pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
    8779           0 :             ss << "0; i < " << nCurWindowSize << "; i++) {\n";
    8780           0 :             ss << "        arg" << k << " = ";
    8781           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8782             : #ifdef ISNAN
    8783           0 :             ss << "        if(isNan(arg" << k << ") || (i >= ";
    8784           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8785           0 :             ss << "            arg" << k << " = vMean;\n";
    8786           0 :             ss << "        }\n";
    8787             : #endif
    8788           0 :             ss << "        vSum += pow( arg" << k << " - vMean, 2 );\n";
    8789           0 :             ss << "    }\n";
    8790             :         } else {
    8791           0 :             ss << "0; i < " << nCurWindowSize << "; i++) {\n";
    8792           0 :             ss << "        arg" << k << " = ";
    8793           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8794             : #ifdef ISNAN
    8795           0 :             ss << "        if(isNan(arg" << k << ") || (i + gid0 >= ";
    8796           0 :             ss << pCurDVR->GetArrayLength() << ")) {\n";
    8797           0 :             ss << "            arg" << k << " = vMean;\n";
    8798           0 :             ss << "        }\n";
    8799             : #endif
    8800           0 :             ss << "        vSum += pow( arg" << k << " - vMean, 2 );\n";
    8801           0 :             ss << "    }\n";
    8802             :         }
    8803             :             }
    8804           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    8805             :         {
    8806             : #ifdef  ISNAN
    8807             :             const formula::SingleVectorRefToken* pTVR =
    8808           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    8809           0 :             ss << "    if(isNan(";
    8810           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef();
    8811           0 :             ss << ") || gid0 >= " << pTVR->GetArrayLength() << ")\n";
    8812           0 :             ss << "        arg" << k << " = vMean;\n    else\n";
    8813             : #endif
    8814           0 :             ss << "        arg" << k << " = ";
    8815           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef()<<";\n";
    8816           0 :             ss << "    vSum += pow( arg" << k << " - vMean, 2 );\n";
    8817             :         }
    8818           0 :         else if (pCur->GetType() == formula::svDouble)
    8819             :         {
    8820           0 :             ss << "    arg" << k << " = ";
    8821           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8822             : #ifdef  ISNAN
    8823           0 :             ss << "    if(isNan(arg" << k << "))\n";
    8824           0 :             ss << "        arg" << k << " = vMean;\n";
    8825             : #endif
    8826           0 :             ss << "    vSum += pow( arg" << k << " - vMean, 2 );\n";
    8827             :         }
    8828             :         else
    8829             :         {
    8830           0 :             ss << "    arg" << k << " = ";
    8831           0 :             ss << vSubArguments[k]->GenSlidingWindowDeclRef() << ";\n";
    8832           0 :             ss << "    vSum += pow( arg" << k << " - vMean, 2 );\n";
    8833             :         }
    8834             :             }
    8835           0 :         ss << "    return vSum;\n";
    8836           0 :         ss << "}";
    8837           0 : }
    8838           0 : void OpHypGeomDist::GenSlidingWindowFunction(std::stringstream &ss,
    8839             :             const std::string &sSymName, SubArguments &vSubArguments)
    8840             : {
    8841           0 :     ss << "\ndouble " << sSymName;
    8842           0 :     ss << "_"<< BinFuncName() <<"(";
    8843           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    8844             :     {
    8845           0 :         if (i)
    8846           0 :             ss << ",";
    8847           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    8848             :     }
    8849           0 :     ss << ") {\n";
    8850           0 :     ss << "    int gid0=get_global_id(0);\n";
    8851           0 :     ss << "    double arg0,arg1,arg2,arg3;\n";
    8852           0 :     size_t i = vSubArguments.size();
    8853           0 :     size_t nItems = 0;
    8854           0 :     for (i = 0; i < vSubArguments.size(); i++)
    8855             :     {
    8856           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    8857             :         assert(pCur);
    8858           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    8859             :         {
    8860             :             const formula::DoubleVectorRefToken* pDVR =
    8861           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    8862           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    8863           0 :             ss << "for (int i = ";
    8864           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    8865             : #ifdef  ISNAN
    8866           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    8867           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    8868             : #endif
    8869           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    8870             : #ifdef  ISNAN
    8871           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    8872           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    8873             : #endif
    8874           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    8875             : #ifdef  ISNAN
    8876           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    8877           0 :                 ss << " &&  i < "<< nCurWindowSize << "; i++){\n ";
    8878             : #endif
    8879             :             }
    8880             :             else {
    8881             : #ifdef  ISNAN
    8882           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    8883             : #endif
    8884             :             }
    8885           0 :             nItems += nCurWindowSize;
    8886             :         }
    8887           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    8888             :         {
    8889             : #ifdef  ISNAN
    8890             :             const formula::SingleVectorRefToken* pSVR =
    8891           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    8892           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    8893           0 :             ss << "    {\n";
    8894           0 :             ss << "        if (isNan(";
    8895           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8896           0 :             ss << "))\n";
    8897           0 :             ss << "            arg"<<i<<"= 0;\n";
    8898           0 :             ss << "        else\n";
    8899           0 :             ss << "            arg"<<i<<"=";
    8900           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    8901           0 :             ss << ";\n";
    8902           0 :             ss << "    }\n";
    8903           0 :             ss << "    else\n";
    8904           0 :             ss << "        arg"<<i<<"= 0;\n";
    8905             : #endif
    8906             :         }
    8907           0 :         else if (pCur->GetType() == formula::svDouble)
    8908             :         {
    8909             : #ifdef  ISNAN
    8910           0 :             ss << "    if (isNan(";
    8911           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    8912           0 :             ss << "))\n";
    8913           0 :             ss << "        arg"<<i<<"= 0;\n";
    8914           0 :             ss << "    else\n";
    8915           0 :             ss << "        arg"<<i<<"=";
    8916           0 :             ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
    8917           0 :             ss << ";\n";
    8918             : #endif
    8919             :         }
    8920             :     }
    8921             :     ss << "    double N1=floor(arg3);\n"
    8922             :     "    double M1=floor(arg2);\n"
    8923             :     "    double n1=floor(arg1);\n"
    8924             :     "    double x1=floor(arg0);\n"
    8925             :     "    double num[9];\n"
    8926             :     "    double PI = 3.1415926535897932384626433832795;\n"
    8927             :     "    double tmp;\n"
    8928             :     "    if( (x1 < 0.0) || (n1 < x1) || (M1 < x1) || (N1 < n1) ||"
    8929             :     "(N1 < M1) || (x1 < n1 - N1 + M1) )\n"
    8930             :     "    {\n"
    8931             :     "        tmp = DBL_MIN;\n"
    8932             :     "        return tmp;\n"
    8933             :     "    }\n"
    8934             :     "    num[0]=M1;\n"
    8935             :     "    num[1]=x1;\n"
    8936             :     "    num[2]=M1-x1;\n"
    8937             :     "    num[3]=N1-M1;\n"
    8938             :     "    num[4]=n1-x1;\n"
    8939             :     "    num[5]=N1-M1-n1+x1;\n"
    8940             :     "    num[6]=N1;\n"
    8941             :     "    num[7]=n1;\n"
    8942             :     "    num[8]=N1-n1;\n"
    8943             :     "    for(int i=0;i<9;i++)\n"
    8944             :     "    {\n"
    8945             :     "        if(num[i]<171)\n"
    8946             :     "        {\n"
    8947             :     "            if(num[i]==0)\n"
    8948             :     "                num[i]=0;\n"
    8949             :     "            else\n"
    8950             :     "                num[i]=log(tgamma(num[i])*num[i]);\n"
    8951             :     "        }\n"
    8952             :     "        else\n"
    8953             :     "            num[i]=0.5*log(2.0*PI)+(num[i]+0.5)*log(num[i])-num[i]+"
    8954             :     "(1.0*pow(12.0*num[i],-1)-1.0*pow(360*pow(num[i],3),-1));\n"
    8955           0 :     "    }\n";
    8956           0 :     ss << "    tmp=pow(M_E,(num[0]+num[3]+num[7]+num[8]";
    8957           0 :     ss << "-num[1]-num[2]-num[4]-num[5]-num[6]));\n";
    8958           0 :     ss << "    return tmp;\n";
    8959           0 :     ss << "}\n";
    8960           0 : }
    8961             : 
    8962             : enum MixDoubleString
    8963             : {
    8964             :     svDoubleVectorRefDoubleString,
    8965             :     svDoubleVectorRefDouble,
    8966             :     svDoubleVectorRefString,
    8967             :     svDoubleVectorRefNULL,
    8968             :     svSingleVectorRefDoubleString,
    8969             :     svSingleVectorRefDouble,
    8970             :     svSingleVectorRefString,
    8971             :     svSingleVectorRefNULL,
    8972             :     svDoubleDouble
    8973             : };
    8974             : 
    8975           0 : void OpMinA::GenSlidingWindowFunction(
    8976             :     std::stringstream &ss, const std::string &sSymName, SubArguments &vSubArguments)
    8977             : {
    8978           0 :     int isMixed = 0;
    8979           0 :     ss << "\ndouble " << sSymName;
    8980           0 :     ss << "_"<< BinFuncName() <<"(";
    8981           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    8982             :     {
    8983           0 :         if (i)
    8984           0 :             ss << ",";
    8985           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    8986             :     }
    8987           0 :     ss << ")\n";
    8988           0 :     ss << "{\n";
    8989           0 :     ss << "    int gid0=get_global_id(0);\n";
    8990           0 :     ss << "    double tmp0 = 1.79769e+308;\n";
    8991           0 :     size_t i = vSubArguments.size();
    8992           0 :     size_t nItems = 0;
    8993           0 :     ss <<"\n";
    8994           0 :     for (i = 0; i < vSubArguments.size(); i++)
    8995             :     {
    8996           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    8997             :         assert(pCur);
    8998           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    8999             :         {
    9000             :             const formula::DoubleVectorRefToken* pDVR =
    9001           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9002           0 :             if(pDVR->GetArrays()[0].mpNumericArray
    9003           0 :                 && pDVR->GetArrays()[0].mpStringArray)
    9004           0 :                 isMixed = svDoubleVectorRefDoubleString;
    9005           0 :             else if(pDVR->GetArrays()[0].mpNumericArray)
    9006           0 :                 isMixed = svDoubleVectorRefDouble;
    9007           0 :             else if(pDVR->GetArrays()[0].mpStringArray)
    9008           0 :                 isMixed = svDoubleVectorRefString;
    9009             :             else
    9010           0 :                 isMixed = svDoubleVectorRefNULL;
    9011           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    9012           0 :             ss << "    for (int i = ";
    9013           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    9014             : #ifdef  ISNAN
    9015           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    9016           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    9017             : #else
    9018             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    9019             : #endif
    9020           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    9021             : #ifdef  ISNAN
    9022           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    9023           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    9024             : #else
    9025             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    9026             : #endif
    9027           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    9028             : #ifdef  ISNAN
    9029           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    9030           0 :                 ss << " && i < "<< nCurWindowSize << "; i++){\n";
    9031             : #else
    9032             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9033             : #endif
    9034             :             }
    9035             :             else {
    9036             : #ifdef  ISNAN
    9037           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    9038             : #else
    9039             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9040             : #endif
    9041             :             }
    9042           0 :             nItems += nCurWindowSize;
    9043             :         }
    9044           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    9045             :         {
    9046             : #ifdef  ISNAN
    9047             :             const formula::SingleVectorRefToken* pSVR =
    9048           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    9049             : 
    9050           0 :             if(pSVR->GetArray().mpNumericArray
    9051           0 :                 && pSVR->GetArray().mpStringArray)
    9052           0 :                 isMixed = svSingleVectorRefDoubleString;
    9053           0 :             else if(pSVR->GetArray().mpNumericArray)
    9054           0 :                 isMixed = svSingleVectorRefDouble;
    9055           0 :             else if(pSVR->GetArray().mpStringArray)
    9056           0 :                 isMixed = svSingleVectorRefString;
    9057             :             else
    9058           0 :                 isMixed = svSingleVectorRefNULL;
    9059           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    9060             : #else
    9061             :             nItems += 1;
    9062             : #endif
    9063             :         }
    9064           0 :         else if (pCur->GetType() == formula::svDouble)
    9065             :         {
    9066             : #ifdef  ISNAN
    9067           0 :             ss << "    {\n";
    9068           0 :             isMixed = svDoubleDouble;
    9069             : #endif
    9070           0 :             nItems += 1;
    9071             :         }
    9072             :         else
    9073             :         {
    9074             : #ifdef  ISNAN
    9075             : #endif
    9076           0 :             nItems += 1;
    9077             :         }
    9078             : 
    9079             : #ifdef  ISNAN
    9080           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9081             :         {
    9082           0 :             if(isMixed == svDoubleVectorRefDoubleString
    9083           0 :                 || isMixed == svSingleVectorRefDoubleString)
    9084             :             {
    9085           0 :                 ss << "        if (!isNan(";
    9086           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9087           0 :                 ss << "))\n";
    9088           0 :                 ss << "            tmp0 = tmp0 > ";
    9089           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9090           0 :                 ss << " ? ";
    9091           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9092           0 :                 ss << " : tmp0;\n";
    9093           0 :                 ss << "        else if(isNan(";
    9094           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9095           0 :                 ss << ") && ";
    9096           0 :                 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9097           0 :                 ss << " != 0)\n";
    9098           0 :                 ss << "            tmp0 = tmp0 > 0.0 ? 0.0 : tmp0;\n";
    9099           0 :                 ss << "    }\n";
    9100             :             }
    9101           0 :             else if(isMixed == svDoubleVectorRefDouble
    9102           0 :                 || isMixed == svSingleVectorRefDouble)
    9103             :             {
    9104           0 :                 ss << "        if (!isNan(";
    9105           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9106           0 :                 ss << "))\n";
    9107           0 :                 ss << "            tmp0 = tmp0 > ";
    9108           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9109           0 :                 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9110           0 :                 ss << " : tmp0;";
    9111           0 :                 ss <<"\n    }\n";
    9112             :             }
    9113           0 :             else if(isMixed == svDoubleVectorRefString)
    9114             :             {
    9115           0 :                 ss << "        if(";
    9116           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9117           0 :                 ss << " == 0)\n            continue;\n";
    9118           0 :                 ss << "        tmp0 = tmp0 > 0.0 ? 0.0 : tmp0;\n";
    9119           0 :                 ss << "    }\n";
    9120             :             }
    9121           0 :             else if(isMixed == svSingleVectorRefString)
    9122             :             {
    9123           0 :                 ss << "        if(";
    9124           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9125           0 :                 ss << " != 0)\n";
    9126           0 :                 ss << "            tmp0 = tmp0 > 0.0 ? 0.0 : tmp0;\n";
    9127           0 :                 ss << "    }\n";
    9128             :             }
    9129           0 :             else if(isMixed == svDoubleDouble)
    9130             :             {
    9131           0 :                 ss << "        tmp0 = tmp0 > ";
    9132           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9133           0 :                 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9134           0 :                 ss << " : tmp0;\n    }\n";
    9135             :             }
    9136             :             else
    9137             :             {
    9138           0 :                 ss << "    }\n";
    9139             :             }
    9140             :         }
    9141             :         else
    9142             :         {
    9143           0 :             ss << "        tmp0 = tmp0 > ";
    9144           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9145           0 :             ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9146           0 :             ss << " : tmp0;";
    9147           0 :             ss <<"\n    }\n";
    9148             :         }
    9149             : #else
    9150             :         ss << "        tmp0 = tmp0 > ";
    9151             :         ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9152             :         ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9153             :         ss << " : tmp0;";
    9154             :         ss <<"\n    }\n";
    9155             : #endif
    9156             :     }
    9157           0 :     ss << "    return tmp0 == 1.79769e+308 ? 0.0 : tmp0;\n";
    9158           0 :     ss << "}\n";
    9159           0 : }
    9160           0 : void OpCountA::GenSlidingWindowFunction(
    9161             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    9162             : vSubArguments)
    9163             : {
    9164           0 :     int isMixed = 0;
    9165           0 :     ss << "\ndouble " << sSymName;
    9166           0 :     ss << "_"<< BinFuncName() <<"(";
    9167           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    9168             :     {
    9169           0 :         if (i)
    9170           0 :             ss << ",";
    9171           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    9172             :     }
    9173           0 :     ss << ")\n";
    9174           0 :     ss << "{\n";
    9175           0 :     ss << "    int gid0=get_global_id(0);\n";
    9176           0 :     ss << "    double nCount = 0.0;\n";
    9177           0 :     size_t i = vSubArguments.size();
    9178           0 :     size_t nItems = 0;
    9179           0 :     ss <<"\n";
    9180           0 :     for (i = 0; i < vSubArguments.size(); i++)
    9181             :     {
    9182           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    9183             :         assert(pCur);
    9184           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    9185             :         {
    9186             :             const formula::DoubleVectorRefToken* pDVR =
    9187           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9188           0 :             if(pDVR->GetArrays()[0].mpNumericArray
    9189           0 :                 && pDVR->GetArrays()[0].mpStringArray)
    9190           0 :                 isMixed = svDoubleVectorRefDoubleString;
    9191           0 :             else if(pDVR->GetArrays()[0].mpNumericArray)
    9192           0 :                 isMixed = svDoubleVectorRefDouble;
    9193           0 :             else if(pDVR->GetArrays()[0].mpStringArray)
    9194           0 :                 isMixed = svDoubleVectorRefString;
    9195             :             else
    9196           0 :                 isMixed = svDoubleVectorRefNULL;
    9197           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    9198           0 :             ss << "    for (int i = ";
    9199           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    9200             : #ifdef  ISNAN
    9201           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    9202           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    9203             : #else
    9204             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    9205             : #endif
    9206           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    9207             : #ifdef  ISNAN
    9208           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    9209           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    9210             : #else
    9211             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    9212             : #endif
    9213           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    9214             : #ifdef  ISNAN
    9215           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    9216           0 :                 ss << " && i < "<< nCurWindowSize << "; i++){\n";
    9217             : #else
    9218             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9219             : #endif
    9220             :             }
    9221             :             else {
    9222             : #ifdef  ISNAN
    9223           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    9224             : #else
    9225             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9226             : #endif
    9227             :             }
    9228           0 :             nItems += nCurWindowSize;
    9229             :         }
    9230           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    9231             :         {
    9232             : #ifdef  ISNAN
    9233             :             const formula::SingleVectorRefToken* pSVR =
    9234           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    9235             : 
    9236           0 :             if(pSVR->GetArray().mpNumericArray
    9237           0 :                 && pSVR->GetArray().mpStringArray)
    9238           0 :                 isMixed = svSingleVectorRefDoubleString;
    9239           0 :             else if(pSVR->GetArray().mpNumericArray)
    9240           0 :                 isMixed = svSingleVectorRefDouble;
    9241           0 :             else if(pSVR->GetArray().mpStringArray)
    9242           0 :                 isMixed = svSingleVectorRefString;
    9243             :             else
    9244           0 :                 isMixed = svSingleVectorRefNULL;
    9245           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    9246             : #else
    9247             :             nItems += 1;
    9248             : #endif
    9249             :         }
    9250           0 :         else if (pCur->GetType() == formula::svDouble)
    9251             :         {
    9252             : #ifdef  ISNAN
    9253           0 :             ss << "    {\n";
    9254           0 :             isMixed = svDoubleDouble;
    9255             : #endif
    9256           0 :             nItems += 1;
    9257             :         }
    9258             :         else
    9259             :         {
    9260             : #ifdef  ISNAN
    9261             : #endif
    9262           0 :             nItems += 1;
    9263             :         }
    9264             : 
    9265             : #ifdef  ISNAN
    9266           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9267             :         {
    9268           0 :             if(isMixed == svDoubleVectorRefDoubleString
    9269           0 :                 || isMixed == svSingleVectorRefDoubleString)
    9270             :             {
    9271           0 :                 ss << "        if (!isNan(";
    9272           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9273           0 :                 ss << ")){\n";
    9274           0 :                 ss << "            nCount+=1.0;\n";
    9275           0 :                 ss << "    }\n";
    9276           0 :                 ss << "        else if(isNan(";
    9277           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9278           0 :                 ss << ") && ";
    9279           0 :                 ss<< vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9280           0 :                 ss << " != 0)\n";
    9281           0 :                 ss << "            nCount+=1.0;\n";
    9282           0 :                 ss << "    }\n";
    9283             :             }
    9284           0 :             else if(isMixed == svDoubleVectorRefDouble
    9285           0 :                 || isMixed == svSingleVectorRefDouble)
    9286             :             {
    9287           0 :                 ss << "        if (!isNan(";
    9288           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9289           0 :                 ss << ")){\n";
    9290           0 :                 ss << "            nCount+=1.0;\n";
    9291           0 :                 ss <<"}\n    }\n";
    9292             :             }
    9293           0 :             else if(isMixed == svDoubleVectorRefString)
    9294             :             {
    9295           0 :                 ss << "        if (!isNan(";
    9296           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9297           0 :                 ss << "))\n";
    9298           0 :                 ss << "            nCount+=1.0;\n";
    9299           0 :                 ss <<"\n    }\n";
    9300             :             }
    9301           0 :             else if(isMixed == svSingleVectorRefString)
    9302             :             {
    9303           0 :                 ss << "        if(";
    9304           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9305           0 :                 ss << " != 0)\n";
    9306           0 :                 ss << "            nCount+=1.0;\n";
    9307           0 :                 ss << "    }\n";
    9308             :             }
    9309           0 :             else if(isMixed == svDoubleDouble)
    9310             :             {
    9311           0 :                 ss << "            nCount+=1.0;\n";
    9312           0 :                 ss << "    }\n";
    9313             :             }
    9314             :             else
    9315             :             {
    9316           0 :                 ss << "    }\n";
    9317             :             }
    9318             :         }
    9319             :         else
    9320             :         {
    9321           0 :                 ss << "            nCount+=1.0;\n";
    9322           0 :                 ss << "    }\n";
    9323             :         }
    9324             : #else
    9325             :         ss << "            nCount+=1.0;\n";
    9326             :         ss << "    }\n";
    9327             : #endif
    9328             :     }
    9329           0 :     ss << "    return nCount;\n";
    9330           0 :     ss << "}\n";
    9331           0 : }
    9332           0 : void OpMaxA::GenSlidingWindowFunction(
    9333             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    9334             : vSubArguments)
    9335             : {
    9336           0 :     int isMixed = 0;
    9337           0 :     ss << "\ndouble " << sSymName;
    9338           0 :     ss << "_"<< BinFuncName() <<"(";
    9339           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    9340             :     {
    9341           0 :         if (i)
    9342           0 :             ss << ",";
    9343           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    9344             :     }
    9345           0 :     ss << ")\n";
    9346           0 :     ss << "{\n";
    9347           0 :     ss << "    int gid0=get_global_id(0);\n";
    9348           0 :     ss << "    double tmp0 = 2.22507e-308;\n";
    9349           0 :     size_t i = vSubArguments.size();
    9350           0 :     size_t nItems = 0;
    9351           0 :     ss <<"\n";
    9352           0 :     for (i = 0; i < vSubArguments.size(); i++)
    9353             :     {
    9354           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    9355             :         assert(pCur);
    9356           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    9357             :         {
    9358             :             const formula::DoubleVectorRefToken* pDVR =
    9359           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9360           0 :             if(pDVR->GetArrays()[0].mpNumericArray
    9361           0 :                 && pDVR->GetArrays()[0].mpStringArray)
    9362           0 :                 isMixed = svDoubleVectorRefDoubleString;
    9363           0 :             else if(pDVR->GetArrays()[0].mpNumericArray)
    9364           0 :                 isMixed = svDoubleVectorRefDouble;
    9365           0 :             else if(pDVR->GetArrays()[0].mpStringArray)
    9366           0 :                 isMixed = svDoubleVectorRefString;
    9367             :             else
    9368           0 :                 isMixed = svDoubleVectorRefNULL;
    9369           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    9370           0 :             ss << "    for (int i = ";
    9371           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    9372             : #ifdef  ISNAN
    9373           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    9374           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    9375             : #else
    9376             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    9377             : #endif
    9378           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    9379             : #ifdef  ISNAN
    9380           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    9381           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    9382             : #else
    9383             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    9384             : #endif
    9385           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    9386             : #ifdef  ISNAN
    9387           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    9388           0 :                 ss << " && i < "<< nCurWindowSize << "; i++){\n";
    9389             : #else
    9390             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9391             : #endif
    9392             :             }
    9393             :             else {
    9394             : #ifdef  ISNAN
    9395           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    9396             : #else
    9397             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9398             : #endif
    9399             :             }
    9400           0 :             nItems += nCurWindowSize;
    9401             :         }
    9402           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    9403             :         {
    9404             : #ifdef  ISNAN
    9405             :             const formula::SingleVectorRefToken* pSVR =
    9406           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    9407             : 
    9408           0 :             if(pSVR->GetArray().mpNumericArray
    9409           0 :                 && pSVR->GetArray().mpStringArray)
    9410           0 :                 isMixed = svSingleVectorRefDoubleString;
    9411           0 :             else if(pSVR->GetArray().mpNumericArray)
    9412           0 :                 isMixed = svSingleVectorRefDouble;
    9413           0 :             else if(pSVR->GetArray().mpStringArray)
    9414           0 :                 isMixed = svSingleVectorRefString;
    9415             :             else
    9416           0 :                 isMixed = svSingleVectorRefNULL;
    9417           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    9418             : #else
    9419             :             nItems += 1;
    9420             : #endif
    9421             :         }
    9422           0 :         else if (pCur->GetType() == formula::svDouble)
    9423             :         {
    9424             : #ifdef  ISNAN
    9425           0 :             ss << "    {\n";
    9426           0 :             isMixed = svDoubleDouble;
    9427             : #endif
    9428           0 :             nItems += 1;
    9429             :         }
    9430             :         else
    9431             :         {
    9432             : #ifdef  ISNAN
    9433             : #endif
    9434           0 :             nItems += 1;
    9435             :         }
    9436             : 
    9437             : #ifdef  ISNAN
    9438           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9439             :         {
    9440           0 :             if(isMixed == svDoubleVectorRefDoubleString
    9441           0 :                 || isMixed == svSingleVectorRefDoubleString)
    9442             :             {
    9443           0 :                 ss << "        if (!isNan(";
    9444           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9445           0 :                 ss << "))\n";
    9446           0 :                 ss << "            tmp0 = tmp0 < ";
    9447           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9448           0 :                 ss << " ? ";
    9449           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9450           0 :                 ss << " : tmp0;\n";
    9451           0 :                 ss << "        else if(isNan(";
    9452           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9453           0 :                 ss << ") && ";
    9454           0 :                 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9455           0 :                 ss << " != 0)\n";
    9456           0 :                 ss << "            tmp0 = tmp0 < 0.0 ? 0.0 : tmp0;\n";
    9457           0 :                 ss << "    }\n";
    9458             :             }
    9459           0 :             else if(isMixed == svDoubleVectorRefDouble
    9460           0 :                 || isMixed == svSingleVectorRefDouble)
    9461             :             {
    9462           0 :                 ss << "        if (!isNan(";
    9463           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9464           0 :                 ss << "))\n";
    9465           0 :                 ss << "            tmp0 = tmp0 < ";
    9466           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9467           0 :                 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9468           0 :                 ss << " : tmp0;";
    9469           0 :                 ss <<"\n    }\n";
    9470             :             }
    9471           0 :             else if(isMixed == svDoubleVectorRefString)
    9472             :             {
    9473           0 :                 ss << "        if(";
    9474           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9475           0 :                 ss << " == 0)\n            continue;\n";
    9476           0 :                 ss << "        tmp0 = tmp0 < 0.0 ? 0.0 : tmp0;\n";
    9477           0 :                 ss << "    }\n";
    9478             :             }
    9479           0 :             else if(isMixed == svSingleVectorRefString)
    9480             :             {
    9481           0 :                 ss << "        if(";
    9482           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9483           0 :                 ss << " != 0)\n";
    9484           0 :                 ss << "            tmp0 = tmp0 < 0.0 ? 0.0 : tmp0;\n";
    9485           0 :                 ss << "    }\n";
    9486             :             }
    9487           0 :             else if(isMixed == svDoubleDouble)
    9488             :             {
    9489           0 :                 ss << "        tmp0 = tmp0 < ";
    9490           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9491           0 :                 ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9492           0 :                 ss << " : tmp0;\n    }\n";
    9493             :             }
    9494             :             else
    9495             :             {
    9496           0 :                 ss << "    }\n";
    9497             :             }
    9498             :         }
    9499             :         else
    9500             :         {
    9501           0 :             ss << "        tmp0 = tmp0 < ";
    9502           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9503           0 :             ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9504           0 :             ss << " : tmp0;";
    9505           0 :             ss <<"\n    }\n";
    9506             :         }
    9507             : #else
    9508             :         ss << "        tmp0 = tmp0 < ";
    9509             :         ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9510             :         ss << " ? " << vSubArguments[i]->GenSlidingWindowDeclRef();
    9511             :         ss << " : tmp0;";
    9512             :         ss <<"\n    }\n";
    9513             : #endif
    9514             :     }
    9515           0 :     ss << "    return tmp0 == 2.22507e-308 ? 0.0 : tmp0;\n";
    9516           0 :     ss << "}\n";
    9517           0 : }
    9518           0 : void OpAverageA::GenSlidingWindowFunction(
    9519             :     std::stringstream &ss, const std::string &sSymName, SubArguments &
    9520             : vSubArguments)
    9521             : {
    9522           0 :     int isMixed = 0;
    9523           0 :     ss << "\ndouble " << sSymName;
    9524           0 :     ss << "_"<< BinFuncName() <<"(";
    9525           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    9526             :     {
    9527           0 :         if (i)
    9528           0 :             ss << ",";
    9529           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    9530             :     }
    9531           0 :     ss << ")\n";
    9532           0 :     ss << "{\n";
    9533           0 :     ss << "    int gid0=get_global_id(0);\n";
    9534           0 :     ss << "    double tmp0 = 0.0;\n";
    9535           0 :     ss << "    double nCount = 0.0;\n";
    9536           0 :     size_t i = vSubArguments.size();
    9537           0 :     size_t nItems = 0;
    9538           0 :     ss <<"\n";
    9539           0 :     for (i = 0; i < vSubArguments.size(); i++)
    9540             :     {
    9541           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    9542             :         assert(pCur);
    9543           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
    9544             :         {
    9545             :             const formula::DoubleVectorRefToken* pDVR =
    9546           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9547           0 :             if(pDVR->GetArrays()[0].mpNumericArray
    9548           0 :                 && pDVR->GetArrays()[0].mpStringArray)
    9549           0 :                 isMixed = svDoubleVectorRefDoubleString;
    9550           0 :             else if(pDVR->GetArrays()[0].mpNumericArray)
    9551           0 :                 isMixed = svDoubleVectorRefDouble;
    9552           0 :             else if(pDVR->GetArrays()[0].mpStringArray)
    9553           0 :                 isMixed = svDoubleVectorRefString;
    9554             :             else
    9555           0 :                 isMixed = svDoubleVectorRefNULL;
    9556           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
    9557           0 :             ss << "    for (int i = ";
    9558           0 :             if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
    9559             : #ifdef  ISNAN
    9560           0 :                 ss << "gid0; i < " << pDVR->GetArrayLength();
    9561           0 :                 ss << " && i < " << nCurWindowSize  << "; i++){\n";
    9562             : #else
    9563             :                 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
    9564             : #endif
    9565           0 :             } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
    9566             : #ifdef  ISNAN
    9567           0 :                 ss << "0; i < " << pDVR->GetArrayLength();
    9568           0 :                 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
    9569             : #else
    9570             :                 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
    9571             : #endif
    9572           0 :             } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
    9573             : #ifdef  ISNAN
    9574           0 :                 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    9575           0 :                 ss << " && i < "<< nCurWindowSize << "; i++){\n";
    9576             : #else
    9577             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9578             : #endif
    9579             :             }
    9580             :             else {
    9581             : #ifdef  ISNAN
    9582           0 :                 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
    9583             : #else
    9584             :                 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
    9585             : #endif
    9586             :             }
    9587           0 :             nItems += nCurWindowSize;
    9588             :         }
    9589           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
    9590             :         {
    9591             : #ifdef  ISNAN
    9592             :             const formula::SingleVectorRefToken* pSVR =
    9593           0 :                 static_cast< const formula::SingleVectorRefToken* >(pCur);
    9594             : 
    9595           0 :             if(pSVR->GetArray().mpNumericArray
    9596           0 :                 && pSVR->GetArray().mpStringArray)
    9597           0 :                 isMixed = svSingleVectorRefDoubleString;
    9598           0 :             else if(pSVR->GetArray().mpNumericArray)
    9599           0 :                 isMixed = svSingleVectorRefDouble;
    9600           0 :             else if(pSVR->GetArray().mpStringArray)
    9601           0 :                 isMixed = svSingleVectorRefString;
    9602             :             else
    9603           0 :                 isMixed = svSingleVectorRefNULL;
    9604           0 :             ss << "    if (gid0 < " << pSVR->GetArrayLength() << "){\n";
    9605             : #else
    9606             :             nItems += 1;
    9607             : #endif
    9608             :         }
    9609           0 :         else if (pCur->GetType() == formula::svDouble)
    9610             :         {
    9611             : #ifdef  ISNAN
    9612           0 :             ss << "    {\n";
    9613           0 :             isMixed = svDoubleDouble;
    9614             : #endif
    9615           0 :             nItems += 1;
    9616             :         }
    9617             :         else
    9618             :         {
    9619             : #ifdef  ISNAN
    9620             : #endif
    9621           0 :             nItems += 1;
    9622             :         }
    9623             : 
    9624             : #ifdef  ISNAN
    9625           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9626             :         {
    9627           0 :             if(isMixed == svDoubleVectorRefDoubleString
    9628           0 :                 || isMixed == svSingleVectorRefDoubleString)
    9629             :             {
    9630           0 :                 ss << "        if (!isNan(";
    9631           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9632           0 :                 ss << ")){\n";
    9633           0 :                 ss << "            tmp0 +=";
    9634           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9635           0 :                 ss << ";\n";
    9636           0 :                 ss << "            nCount+=1.0;\n";
    9637           0 :                 ss << "    }\n";
    9638           0 :                 ss << "        else if(isNan(";
    9639           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9640           0 :                 ss << ") && ";
    9641           0 :                 ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9642           0 :                 ss << " != 0)\n";
    9643           0 :                 ss << "            nCount+=1.0;\n";
    9644           0 :                 ss << "    }\n";
    9645             :             }
    9646           0 :             else if(isMixed == svDoubleVectorRefDouble
    9647           0 :                 || isMixed == svSingleVectorRefDouble)
    9648             :             {
    9649           0 :                 ss << "        if (!isNan(";
    9650           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9651           0 :                 ss << ")){\n";
    9652           0 :                 ss << "            tmp0 +=";
    9653           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9654           0 :                 ss << ";\n";
    9655           0 :                 ss << "            nCount+=1.0;\n";
    9656           0 :                 ss <<"}\n    }\n";
    9657             :             }
    9658           0 :             else if(isMixed == svDoubleVectorRefString)
    9659             :             {
    9660           0 :                 ss << "        if (!isNan(";
    9661           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9662           0 :                 ss << "))\n";
    9663           0 :                 ss << "            nCount+=1.0;\n";
    9664           0 :                 ss <<"\n    }\n";
    9665             :             }
    9666           0 :             else if(isMixed == svSingleVectorRefString)
    9667             :             {
    9668           0 :                 ss << "        if(";
    9669           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9670           0 :                 ss << " != 0)\n";
    9671           0 :                 ss << "            nCount+=1.0;\n";
    9672           0 :                 ss << "    }\n";
    9673             :             }
    9674           0 :             else if(isMixed == svDoubleDouble)
    9675             :             {
    9676           0 :                 ss << "            tmp0 +=";
    9677           0 :                 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9678           0 :                 ss << ";\n";
    9679           0 :                 ss << "            nCount+=1.0;\n";
    9680           0 :                 ss << "    }\n";
    9681             :             }
    9682             :             else
    9683             :             {
    9684           0 :                 ss << "    }\n";
    9685             :             }
    9686             :         }
    9687             :         else
    9688             :         {
    9689           0 :                 ss << "            tmp0 +=";
    9690           0 :                 ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9691           0 :                 ss << ";\n";
    9692           0 :                 ss << "            nCount+=1.0;\n";
    9693           0 :                 ss << "    }\n";
    9694             :         }
    9695             : #else
    9696             :         ss << "            tmp0 +=";
    9697             :         ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9698             :         ss << ";\n";
    9699             :         ss << "            nCount+=1.0;\n";
    9700             :         ss << "    }\n";
    9701             : #endif
    9702             :     }
    9703           0 :     ss << "    return tmp0*pow(nCount,-1);\n";
    9704           0 :     ss << "}\n";
    9705           0 : }
    9706           0 : void OpVarA::GenSlidingWindowFunction(std::stringstream &ss,
    9707             :             const std::string &sSymName, SubArguments &vSubArguments)
    9708             : {
    9709           0 :     int isMixedDV = 0;
    9710           0 :     int isMixedSV = 0;
    9711           0 :     ss << "\ndouble " << sSymName;
    9712           0 :     ss << "_" << BinFuncName() << "(";
    9713           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
    9714             :     {
    9715           0 :         if (i)
    9716           0 :             ss << ",";
    9717           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
    9718             :     }
    9719           0 :     ss << "){\n";
    9720           0 :     ss << "    int gid0 = get_global_id(0);\n";
    9721           0 :     ss << "    double fSum = 0.0;\n";
    9722           0 :     ss << "    double fMean = 0.0;\n";
    9723           0 :     ss << "    double vSum = 0.0;\n";
    9724           0 :     ss << "    double fCount = 0.0;\n";
    9725           0 :     ss << "    double arg = 0.0;\n";
    9726           0 :     unsigned i = vSubArguments.size();
    9727           0 :     while (i--)
    9728             :     {
    9729           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    9730             :         assert(pCur);
    9731             : 
    9732           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9733             :         {
    9734           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    9735             :             {
    9736             :                 const formula::DoubleVectorRefToken* pDVR =
    9737           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9738             :                 assert(pDVR);
    9739           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
    9740           0 :                     && pDVR->GetArrays()[0].mpStringArray)
    9741           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
    9742           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
    9743           0 :                     isMixedDV = svDoubleVectorRefDouble;
    9744           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
    9745           0 :                     isMixedDV = svDoubleVectorRefString;
    9746             :                 else
    9747           0 :                     isMixedDV = svDoubleVectorRefNULL;
    9748             : 
    9749           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    9750           0 :                 ss << "    for (int i = ";
    9751           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    9752             :                 {
    9753             : #ifdef  ISNAN
    9754           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    9755           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    9756           0 :                     ss << "    {\n";
    9757             : #else
    9758             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    9759             :                     ss << "    {\n";
    9760             : #endif
    9761             :                 }
    9762           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    9763             :                 {
    9764             : #ifdef  ISNAN
    9765           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    9766           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    9767           0 :                     ss << "    {\n";
    9768             : #else
    9769             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    9770             :                     ss << "    {\n";
    9771             : #endif
    9772             :                 }
    9773           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    9774             :                 {
    9775             : #ifdef  ISNAN
    9776           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    9777           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    9778           0 :                     ss << "    {\n";
    9779             : #else
    9780             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    9781             :                     ss << "    {\n";
    9782             : #endif
    9783             :                 }
    9784             :                 else
    9785             :                 {
    9786             : #ifdef  ISNAN
    9787           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    9788           0 :                     ss << "    {\n";
    9789             : #else
    9790             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
    9791             :                     ss << "    {\n";
    9792             : #endif
    9793             :                 }
    9794             : 
    9795           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
    9796             :                 {
    9797           0 :                     ss << "        arg = ";
    9798           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9799           0 :                     ss << ";\n";
    9800             : #ifdef  ISNAN
    9801           0 :                     ss << "        if (isNan(arg) && ";
    9802           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9803           0 :                     ss << " == 0)\n";
    9804           0 :                     ss << "            continue;\n";
    9805           0 :                     ss << "        if(isNan(arg) && ";
    9806           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9807           0 :                     ss << " != 0)\n";
    9808           0 :                     ss << "        {\n";
    9809           0 :                     ss << "            fCount = fCount + 1.0;\n";
    9810           0 :                     ss << "            continue;\n";
    9811           0 :                     ss << "        }\n";
    9812             : #endif
    9813           0 :                     ss << "        fSum += arg;\n";
    9814           0 :                     ss << "        fCount = fCount + 1.0;\n";
    9815           0 :                     ss << "    }\n";
    9816             :                 }
    9817           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
    9818             :                 {
    9819           0 :                     ss << "        arg = ";
    9820           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9821           0 :                     ss << ";\n";
    9822             : #ifdef  ISNAN
    9823           0 :                     ss << "        if (isNan(arg))\n";
    9824           0 :                     ss << "            continue;\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 == svDoubleVectorRefString)
    9831             :                 {
    9832           0 :                     ss << "        if (";
    9833           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9834           0 :                     ss << " == 0)\n";
    9835           0 :                     ss << "            continue;\n";
    9836           0 :                     ss << "        fCount = fCount + 1.0;\n";
    9837           0 :                     ss << "    }\n";
    9838             :                 }
    9839             :                 else
    9840             :                 {
    9841           0 :                     ss << "        continue;\n";
    9842           0 :                     ss << "    }\n";
    9843             :                 }
    9844             : 
    9845             :             }
    9846           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
    9847             :             {
    9848             :                 const formula::SingleVectorRefToken* pSVR =
    9849           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
    9850             :                 assert(pSVR);
    9851           0 :                 if(pSVR->GetArray().mpNumericArray
    9852           0 :                     && pSVR->GetArray().mpStringArray)
    9853           0 :                     isMixedSV = svSingleVectorRefDoubleString;
    9854           0 :                 else if(pSVR->GetArray().mpNumericArray)
    9855           0 :                     isMixedSV = svSingleVectorRefDouble;
    9856           0 :                 else if(pSVR->GetArray().mpStringArray)
    9857           0 :                     isMixedSV = svSingleVectorRefString;
    9858             :                 else
    9859           0 :                     isMixedSV = svSingleVectorRefNULL;
    9860             : 
    9861           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
    9862             :                 {
    9863             : #ifdef  ISNAN
    9864           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    9865           0 :                     ss << "    {\n";
    9866             : #endif
    9867           0 :                     ss << "        arg = ";
    9868           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
    9869           0 :                     ss << ";\n";
    9870             : #ifdef  ISNAN
    9871           0 :                     ss << "        if (!isNan(arg))\n";
    9872           0 :                     ss << "        {\n";
    9873             : #endif
    9874           0 :                     ss << "            fSum += arg;\n";
    9875           0 :                     ss << "            fCount = fCount + 1.0;\n";
    9876             : #ifdef ISNAN
    9877           0 :                     ss << "        }\n";
    9878           0 :                     ss << "        if (isNan(arg) && ";
    9879           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
    9880           0 :                     ss << " != 0)\n";
    9881           0 :                     ss << "            fCount = fCount + 1.0;\n";
    9882           0 :                     ss << "    }\n";
    9883             : #endif
    9884             :                 }
    9885           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
    9886             :                 {
    9887             : #ifdef  ISNAN
    9888           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    9889           0 :                     ss << "    {\n";
    9890             : #endif
    9891           0 :                     ss << "        arg = ";
    9892           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    9893             : #ifdef ISNAN
    9894           0 :                     ss << "        if (!isNan(arg))\n";
    9895           0 :                     ss << "        {\n";
    9896             : #endif
    9897           0 :                     ss << "            fSum += arg;\n";
    9898           0 :                     ss << "            fCount += 1.0;\n";
    9899             : #ifdef ISNAN
    9900           0 :                     ss << "        }\n";
    9901           0 :                     ss << "    }\n";
    9902             : #endif
    9903             :                 }
    9904           0 :                 else if(isMixedSV == svSingleVectorRefString)
    9905             :                 {
    9906             : 
    9907           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
    9908           0 :                     ss << "    {\n";
    9909           0 :                     ss << "        if (";
    9910           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
    9911           0 :                     ss << " != 0)\n";
    9912           0 :                     ss << "            fCount = fCount + 1.0;\n";
    9913           0 :                     ss << "    }\n";
    9914             :                 }
    9915             :                 else
    9916             :                 {
    9917           0 :                     ss << "    arg =0.0;\n";
    9918             :                 }
    9919             :             }
    9920             :             else
    9921             :             {
    9922           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
    9923           0 :                 ss << "    fSum += arg;\n";
    9924           0 :                 ss << "    fCount = fCount + 1.0;\n";
    9925             :             }
    9926             :         }
    9927             :         else
    9928             :         {
    9929           0 :             ss << "    arg = ";
    9930           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
    9931           0 :             ss << "    fSum += arg;\n";
    9932           0 :             ss << "    fCount = fCount + 1.0;\n";
    9933             :         }
    9934           0 :         if (i == 0)
    9935             :         {
    9936           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
    9937             :         }
    9938             :     }
    9939           0 :     i = vSubArguments.size();
    9940           0 :     while (i--)
    9941             :     {
    9942           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
    9943             :         assert(pCur);
    9944             : 
    9945           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
    9946             :         {
    9947           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
    9948             :             {
    9949             :                 const formula::DoubleVectorRefToken* pDVR =
    9950           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
    9951           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
    9952           0 :                     && pDVR->GetArrays()[0].mpStringArray)
    9953           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
    9954           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
    9955           0 :                     isMixedDV = svDoubleVectorRefDouble;
    9956           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
    9957           0 :                     isMixedDV = svDoubleVectorRefString;
    9958             :                 else
    9959           0 :                     isMixedDV = svDoubleVectorRefNULL;
    9960             : 
    9961           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
    9962           0 :                 ss << "    for (int i = ";
    9963           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
    9964             :                 {
    9965             : #ifdef  ISNAN
    9966           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
    9967           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
    9968           0 :                     ss << "    {\n";
    9969             : #else
    9970             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
    9971             :                     ss << "    {\n";
    9972             : #endif
    9973             :                 }
    9974           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    9975             :                 {
    9976             : #ifdef  ISNAN
    9977           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
    9978           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
    9979           0 :                     ss << "    {\n";
    9980             : #else
    9981             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
    9982             :                     ss << "    {\n";
    9983             : #endif
    9984             :                 }
    9985           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
    9986             :                 {
    9987             : #ifdef  ISNAN
    9988           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
    9989           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
    9990           0 :                     ss << "    {\n";
    9991             : #else
    9992             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
    9993             :                     ss << "    {\n";
    9994             : #endif
    9995             :                 }
    9996             :                 else
    9997             :                 {
    9998             : #ifdef  ISNAN
    9999           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10000           0 :                     ss << "    {\n";
   10001             : #else
   10002             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10003             :                     ss << "    {\n";
   10004             : #endif
   10005             :                 }
   10006           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   10007             :                 {
   10008           0 :                     ss << "        arg = ";
   10009           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10010           0 :                     ss << ";\n";
   10011             : #ifdef  ISNAN
   10012           0 :                     ss << "        if (isNan(arg) && ";
   10013           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10014           0 :                     ss << " == 0)\n";
   10015           0 :                     ss << "            continue;\n";
   10016           0 :                     ss << "        if(isNan(arg) && ";
   10017           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10018           0 :                     ss << " != 0)\n";
   10019           0 :                     ss << "            arg = 0.0;\n";
   10020             : #endif
   10021           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10022           0 :                     ss << "    }\n";
   10023             : 
   10024             :                 }
   10025           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   10026             :                 {
   10027           0 :                     ss << "        arg = ";
   10028           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10029           0 :                     ss << ";\n";
   10030             : #ifdef  ISNAN
   10031           0 :                     ss << "        if (isNan(arg))\n";
   10032           0 :                     ss << "            continue;\n";
   10033             : #endif
   10034           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10035           0 :                     ss << "    }\n";
   10036             : 
   10037             :                 }
   10038           0 :                 else if(isMixedDV == svDoubleVectorRefString)
   10039             :                 {
   10040           0 :                     ss << "        if (";
   10041           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10042           0 :                     ss << " == 0)\n";
   10043           0 :                     ss << "            continue;\n";
   10044           0 :                     ss << "        arg = 0.0;\n";
   10045           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10046           0 :                     ss << "    }\n";
   10047             :                 }
   10048             :                 else
   10049             :                 {
   10050           0 :                     ss << "        continue;\n";
   10051           0 :                     ss << "    }\n";
   10052             :                 }
   10053             :             }
   10054           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   10055             :             {
   10056             :                 const formula::SingleVectorRefToken* pSVR =
   10057           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   10058           0 :                 if(pSVR->GetArray().mpNumericArray
   10059           0 :                     && pSVR->GetArray().mpStringArray)
   10060           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   10061           0 :                 else if(pSVR->GetArray().mpNumericArray)
   10062           0 :                     isMixedSV = svSingleVectorRefDouble;
   10063           0 :                 else if(pSVR->GetArray().mpStringArray)
   10064           0 :                     isMixedSV = svSingleVectorRefString;
   10065             :                 else
   10066           0 :                     isMixedSV = svSingleVectorRefNULL;
   10067             : 
   10068           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   10069             :                 {
   10070             : #ifdef  ISNAN
   10071           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10072           0 :                     ss << "    {\n";
   10073             : #endif
   10074           0 :                     ss << "        arg = ";
   10075           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10076           0 :                     ss << ";\n";
   10077             : #ifdef  ISNAN
   10078           0 :                     ss << "        if (!isNan(arg))\n";
   10079             : #endif
   10080           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10081             : #ifdef ISNAN
   10082           0 :                     ss << "        if (isNan(arg) && ";
   10083           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10084           0 :                     ss << " != 0)\n";
   10085           0 :                     ss << "        {\n";
   10086           0 :                     ss << "            arg = 0.0;\n";
   10087           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10088           0 :                     ss << "        }\n";
   10089           0 :                     ss << "    }\n";
   10090             : #endif
   10091             :                 }
   10092           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   10093             :                 {
   10094             : #ifdef  ISNAN
   10095           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10096           0 :                    ss << "    {\n";
   10097             : #endif
   10098           0 :                     ss << "        arg = ";
   10099           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10100             : #ifdef  ISNAN
   10101           0 :                     ss << "        if (!isNan(arg))\n";
   10102           0 :                     ss << "        {\n";
   10103             : #endif
   10104           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10105             : #ifdef ISNAN
   10106           0 :                     ss << "        }\n";
   10107           0 :                    ss << "    }\n";
   10108             : #endif
   10109             :                 }
   10110           0 :                 else if(isMixedSV == svSingleVectorRefString)
   10111             :                 {
   10112           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10113           0 :                     ss << "    {\n";
   10114           0 :                     ss << "        if (";
   10115           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10116           0 :                     ss << " != 0)\n";
   10117           0 :                     ss << "        {\n";
   10118           0 :                     ss << "            arg = 0.0;\n";
   10119           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10120           0 :                     ss << "        }\n";
   10121           0 :                     ss << "    }\n";
   10122             :                 }
   10123             :                 else
   10124             :                 {
   10125           0 :                     ss << "    arg = 0.0;\n";
   10126             :                 }
   10127             :             }
   10128             :             else
   10129             :             {
   10130           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   10131           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   10132             :             }
   10133             :         }
   10134             :         else
   10135             :         {
   10136           0 :             ss << "    arg = ";
   10137           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10138           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   10139             :         }
   10140             :     }
   10141           0 :     ss << "    if (fCount <= 1.0)\n";
   10142           0 :     ss << "        return DBL_MAX;\n";
   10143           0 :     ss << "    else\n";
   10144           0 :     ss << "        return vSum * pow(fCount - 1.0,-1.0);\n";
   10145           0 :     ss << "}\n";
   10146           0 : }
   10147             : 
   10148           0 : void OpVarPA::GenSlidingWindowFunction(std::stringstream &ss,
   10149             :             const std::string &sSymName, SubArguments &vSubArguments)
   10150             : {
   10151           0 :     int isMixedDV = 0;
   10152           0 :     int isMixedSV = 0;
   10153           0 :     ss << "\ndouble " << sSymName;
   10154           0 :     ss << "_" << BinFuncName() << "(";
   10155           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   10156             :     {
   10157           0 :         if (i)
   10158           0 :             ss << ",";
   10159           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
   10160             :     }
   10161           0 :     ss << "){\n";
   10162           0 :     ss << "    int gid0 = get_global_id(0);\n";
   10163           0 :     ss << "    double fSum = 0.0;\n";
   10164           0 :     ss << "    double fMean = 0.0;\n";
   10165           0 :     ss << "    double vSum = 0.0;\n";
   10166           0 :     ss << "    double fCount = 0.0;\n";
   10167           0 :     ss << "    double arg = 0.0;\n";
   10168           0 :     unsigned i = vSubArguments.size();
   10169           0 :     while (i--)
   10170             :     {
   10171           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   10172             :         assert(pCur);
   10173             : 
   10174           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
   10175             :         {
   10176           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   10177             :             {
   10178             :                 const formula::DoubleVectorRefToken* pDVR =
   10179           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   10180           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   10181           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   10182           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   10183           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   10184           0 :                     isMixedDV = svDoubleVectorRefDouble;
   10185           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   10186           0 :                     isMixedDV = svDoubleVectorRefString;
   10187             :                 else
   10188           0 :                     isMixedDV = svDoubleVectorRefNULL;
   10189             : 
   10190           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   10191           0 :                 ss << "    for (int i = ";
   10192           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   10193             :                 {
   10194             : #ifdef  ISNAN
   10195           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   10196           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   10197           0 :                     ss << "    {\n";
   10198             : #else
   10199             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   10200             :                     ss << "    {\n";
   10201             : #endif
   10202             :                 }
   10203           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10204             :                 {
   10205             : #ifdef  ISNAN
   10206           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   10207           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   10208           0 :                     ss << "    {\n";
   10209             : #else
   10210             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   10211             :                     ss << "    {\n";
   10212             : #endif
   10213             :                 }
   10214           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10215             :                 {
   10216             : #ifdef  ISNAN
   10217           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   10218           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   10219           0 :                     ss << "    {\n";
   10220             : #else
   10221             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   10222             :                     ss << "    {\n";
   10223             : #endif
   10224             :                 }
   10225             :                 else
   10226             :                 {
   10227             : #ifdef  ISNAN
   10228           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10229           0 :                     ss << "    {\n";
   10230             : #else
   10231             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10232             :                     ss << "    {\n";
   10233             : #endif
   10234             :                 }
   10235             : 
   10236           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   10237             :                 {
   10238           0 :                     ss << "        arg = ";
   10239           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10240           0 :                     ss << ";\n";
   10241             : #ifdef  ISNAN
   10242           0 :                     ss << "        if (isNan(arg) && ";
   10243           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10244           0 :                     ss << " == 0)\n";
   10245           0 :                     ss << "            continue;\n";
   10246           0 :                     ss << "        if(isNan(arg) && ";
   10247           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10248           0 :                     ss << " != 0)\n";
   10249           0 :                     ss << "        {\n";
   10250           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10251           0 :                     ss << "            continue;\n";
   10252           0 :                     ss << "        }\n";
   10253             : #endif
   10254           0 :                     ss << "        fSum += arg;\n";
   10255           0 :                     ss << "        fCount = fCount + 1.0;\n";
   10256           0 :                     ss << "    }\n";
   10257             :                 }
   10258           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   10259             :                 {
   10260           0 :                     ss << "        arg = ";
   10261           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10262           0 :                     ss << ";\n";
   10263             : #ifdef  ISNAN
   10264           0 :                     ss << "        if (isNan(arg))\n";
   10265           0 :                     ss << "            continue;\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 == svDoubleVectorRefString)
   10272             :                 {
   10273           0 :                     ss << "        if (";
   10274           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10275           0 :                     ss << " == 0)\n";
   10276           0 :                     ss << "            continue;\n";
   10277           0 :                     ss << "        fCount = fCount + 1.0;\n";
   10278           0 :                     ss << "    }\n";
   10279             :                 }
   10280             :                 else
   10281             :                 {
   10282           0 :                     ss << "        continue;\n";
   10283           0 :                     ss << "    }\n";
   10284             :                 }
   10285             : 
   10286             :             }
   10287           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   10288             :             {
   10289             :                 const formula::SingleVectorRefToken* pSVR =
   10290           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   10291           0 :                 if(pSVR->GetArray().mpNumericArray
   10292           0 :                     && pSVR->GetArray().mpStringArray)
   10293           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   10294           0 :                 else if(pSVR->GetArray().mpNumericArray)
   10295           0 :                     isMixedSV = svSingleVectorRefDouble;
   10296           0 :                 else if(pSVR->GetArray().mpStringArray)
   10297           0 :                     isMixedSV = svSingleVectorRefString;
   10298             :                 else
   10299           0 :                     isMixedSV = svSingleVectorRefNULL;
   10300             : 
   10301           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   10302             :                 {
   10303             : #ifdef  ISNAN
   10304           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10305           0 :                     ss << "    {\n";
   10306             : #endif
   10307           0 :                     ss << "        arg = ";
   10308           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10309           0 :                     ss << ";\n";
   10310             : #ifdef  ISNAN
   10311           0 :                     ss << "        if (!isNan(arg))\n";
   10312           0 :                     ss << "        {\n";
   10313             : #endif
   10314           0 :                     ss << "            fSum += arg;\n";
   10315           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10316             : #ifdef ISNAN
   10317           0 :                     ss << "        }\n";
   10318           0 :                     ss << "        if (isNan(arg) && ";
   10319           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10320           0 :                     ss << " != 0)\n";
   10321           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10322           0 :                     ss << "    }\n";
   10323             : #endif
   10324             :                 }
   10325           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   10326             :                 {
   10327             : #ifdef  ISNAN
   10328           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10329           0 :                     ss << "    {\n";
   10330             : #endif
   10331           0 :                     ss << "        arg = ";
   10332           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10333             : #ifdef ISNAN
   10334           0 :                     ss << "        if (!isNan(arg))\n";
   10335           0 :                     ss << "        {\n";
   10336             : #endif
   10337           0 :                     ss << "            fSum += arg;\n";
   10338           0 :                     ss << "            fCount += 1.0;\n";
   10339             : #ifdef ISNAN
   10340           0 :                     ss << "        }\n";
   10341           0 :                     ss << "    }\n";
   10342             : #endif
   10343             :                 }
   10344           0 :                 else if(isMixedSV == svSingleVectorRefString)
   10345             :                 {
   10346             : 
   10347           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10348           0 :                     ss << "    {\n";
   10349           0 :                     ss << "        if (";
   10350           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10351           0 :                     ss << " != 0)\n";
   10352           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10353           0 :                     ss << "    }\n";
   10354             :                 }
   10355             :                 else
   10356             :                 {
   10357           0 :                     ss << "    arg =0.0;\n";
   10358             :                 }
   10359             :             }
   10360             :             else
   10361             :             {
   10362           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   10363           0 :                 ss << "    fSum += arg;\n";
   10364           0 :                 ss << "    fCount = fCount + 1.0;\n";
   10365             :             }
   10366             :         }
   10367             :         else
   10368             :         {
   10369           0 :             ss << "    arg = ";
   10370           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10371           0 :             ss << "    fSum += arg;\n";
   10372           0 :             ss << "    fCount = fCount + 1.0;\n";
   10373             :         }
   10374           0 :         if (i == 0)
   10375             :         {
   10376           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
   10377             :         }
   10378             :     }
   10379           0 :     i = vSubArguments.size();
   10380           0 :     while (i--)
   10381             :     {
   10382           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   10383             :         assert(pCur);
   10384             : 
   10385           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
   10386             :         {
   10387           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   10388             :             {
   10389             :                 const formula::DoubleVectorRefToken* pDVR =
   10390           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   10391           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   10392           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   10393           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   10394           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   10395           0 :                     isMixedDV = svDoubleVectorRefDouble;
   10396           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   10397           0 :                     isMixedDV = svDoubleVectorRefString;
   10398             :                 else
   10399           0 :                     isMixedDV = svDoubleVectorRefNULL;
   10400             : 
   10401           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   10402           0 :                 ss << "    for (int i = ";
   10403           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   10404             :                 {
   10405             : #ifdef  ISNAN
   10406           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   10407           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   10408           0 :                     ss << "    {\n";
   10409             : #else
   10410             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   10411             :                     ss << "    {\n";
   10412             : #endif
   10413             :                 }
   10414           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10415             :                 {
   10416             : #ifdef  ISNAN
   10417           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   10418           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   10419           0 :                     ss << "    {\n";
   10420             : #else
   10421             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   10422             :                     ss << "    {\n";
   10423             : #endif
   10424             :                 }
   10425           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10426             :                 {
   10427             : #ifdef  ISNAN
   10428           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   10429           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   10430           0 :                     ss << "    {\n";
   10431             : #else
   10432             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   10433             :                     ss << "    {\n";
   10434             : #endif
   10435             :                 }
   10436             :                 else
   10437             :                 {
   10438             : #ifdef  ISNAN
   10439           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10440           0 :                     ss << "    {\n";
   10441             : #else
   10442             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10443             :                     ss << "    {\n";
   10444             : #endif
   10445             :                 }
   10446           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   10447             :                 {
   10448           0 :                     ss << "        arg = ";
   10449           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10450           0 :                     ss << ";\n";
   10451             : #ifdef  ISNAN
   10452           0 :                     ss << "        if (isNan(arg) && ";
   10453           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10454           0 :                     ss << " == 0)\n";
   10455           0 :                     ss << "            continue;\n";
   10456           0 :                     ss << "        if(isNan(arg) && ";
   10457           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10458           0 :                     ss << " != 0)\n";
   10459           0 :                     ss << "            arg = 0.0;\n";
   10460             : #endif
   10461           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10462           0 :                     ss << "    }\n";
   10463             : 
   10464             :                 }
   10465           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   10466             :                 {
   10467           0 :                     ss << "        arg = ";
   10468           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10469           0 :                     ss << ";\n";
   10470             : #ifdef  ISNAN
   10471           0 :                     ss << "        if (isNan(arg))\n";
   10472           0 :                     ss << "            continue;\n";
   10473             : #endif
   10474           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10475           0 :                     ss << "    }\n";
   10476             : 
   10477             :                 }
   10478           0 :                 else if(isMixedDV == svDoubleVectorRefString)
   10479             :                 {
   10480           0 :                     ss << "        if (";
   10481           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10482           0 :                     ss << " == 0)\n";
   10483           0 :                     ss << "            continue;\n";
   10484           0 :                     ss << "        arg = 0.0;\n";
   10485           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10486           0 :                     ss << "    }\n";
   10487             :                 }
   10488             :                 else
   10489             :                 {
   10490           0 :                     ss << "        continue;\n";
   10491           0 :                     ss << "    }\n";
   10492             :                 }
   10493             :             }
   10494           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   10495             :             {
   10496             :                 const formula::SingleVectorRefToken* pSVR =
   10497           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   10498           0 :                 if(pSVR->GetArray().mpNumericArray
   10499           0 :                     && pSVR->GetArray().mpStringArray)
   10500           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   10501           0 :                 else if(pSVR->GetArray().mpNumericArray)
   10502           0 :                     isMixedSV = svSingleVectorRefDouble;
   10503           0 :                 else if(pSVR->GetArray().mpStringArray)
   10504           0 :                     isMixedSV = svSingleVectorRefString;
   10505             :                 else
   10506           0 :                     isMixedSV = svSingleVectorRefNULL;
   10507             : 
   10508           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   10509             :                 {
   10510             : #ifdef  ISNAN
   10511           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10512           0 :                     ss << "    {\n";
   10513             : #endif
   10514           0 :                     ss << "        arg = ";
   10515           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10516           0 :                     ss << ";\n";
   10517             : #ifdef  ISNAN
   10518           0 :                     ss << "        if (!isNan(arg))\n";
   10519             : #endif
   10520           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10521             : #ifdef ISNAN
   10522           0 :                     ss << "        if (isNan(arg) && ";
   10523           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10524           0 :                     ss << " != 0)\n";
   10525           0 :                     ss << "        {\n";
   10526           0 :                     ss << "            arg = 0.0;\n";
   10527           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10528           0 :                     ss << "        }\n";
   10529           0 :                     ss << "    }\n";
   10530             : #endif
   10531             :                 }
   10532           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   10533             :                 {
   10534             : #ifdef  ISNAN
   10535           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10536           0 :                    ss << "    {\n";
   10537             : #endif
   10538           0 :                     ss << "        arg = ";
   10539           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10540             : #ifdef  ISNAN
   10541           0 :                     ss << "        if (!isNan(arg))\n";
   10542           0 :                     ss << "        {\n";
   10543             : #endif
   10544           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10545             : #ifdef ISNAN
   10546           0 :                     ss << "        }\n";
   10547           0 :                    ss << "    }\n";
   10548             : #endif
   10549             :                 }
   10550           0 :                 else if(isMixedSV == svSingleVectorRefString)
   10551             :                 {
   10552           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10553           0 :                     ss << "    {\n";
   10554           0 :                     ss << "        if (";
   10555           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10556           0 :                     ss << " != 0)\n";
   10557           0 :                     ss << "        {\n";
   10558           0 :                     ss << "            arg = 0.0;\n";
   10559           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10560           0 :                     ss << "        }\n";
   10561           0 :                     ss << "    }\n";
   10562             :                 }
   10563             :                 else
   10564             :                 {
   10565           0 :                     ss << "    arg = 0.0;\n";
   10566             :                 }
   10567             :             }
   10568             :             else
   10569             :             {
   10570           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   10571           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   10572             :             }
   10573             :         }
   10574             :         else
   10575             :         {
   10576           0 :             ss << "    arg = ";
   10577           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10578           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   10579             :         }
   10580             :     }
   10581           0 :     ss << "    if (fCount == 0.0)\n";
   10582           0 :     ss << "        return DBL_MAX;\n";
   10583           0 :     ss << "    else\n";
   10584           0 :     ss << "        return vSum * pow(fCount,-1.0);\n";
   10585           0 :     ss << "}\n";
   10586           0 : }
   10587           0 : void OpStDevA::GenSlidingWindowFunction(std::stringstream &ss,
   10588             :             const std::string &sSymName, SubArguments &vSubArguments)
   10589             : {
   10590           0 :     int isMixedDV = 0;
   10591           0 :     int isMixedSV = 0;
   10592           0 :     ss << "\ndouble " << sSymName;
   10593           0 :     ss << "_" << BinFuncName() << "(";
   10594           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   10595             :     {
   10596           0 :         if (i)
   10597           0 :             ss << ",";
   10598           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
   10599             :     }
   10600           0 :     ss << "){\n";
   10601           0 :     ss << "    int gid0 = get_global_id(0);\n";
   10602           0 :     ss << "    double fSum = 0.0;\n";
   10603           0 :     ss << "    double fMean = 0.0;\n";
   10604           0 :     ss << "    double vSum = 0.0;\n";
   10605           0 :     ss << "    double fCount = 0.0;\n";
   10606           0 :     ss << "    double arg = 0.0;\n";
   10607           0 :     unsigned i = vSubArguments.size();
   10608           0 :     while (i--)
   10609             :     {
   10610           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   10611             :         assert(pCur);
   10612             : 
   10613           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
   10614             :         {
   10615           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   10616             :             {
   10617             :                 const formula::DoubleVectorRefToken* pDVR =
   10618           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   10619           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   10620           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   10621           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   10622           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   10623           0 :                     isMixedDV = svDoubleVectorRefDouble;
   10624           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   10625           0 :                     isMixedDV = svDoubleVectorRefString;
   10626             :                 else
   10627           0 :                     isMixedDV = svDoubleVectorRefNULL;
   10628             : 
   10629           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   10630           0 :                 ss << "    for (int i = ";
   10631           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   10632             :                 {
   10633             : #ifdef  ISNAN
   10634           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   10635           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   10636           0 :                     ss << "    {\n";
   10637             : #else
   10638             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   10639             :                     ss << "    {\n";
   10640             : #endif
   10641             :                 }
   10642           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10643             :                 {
   10644             : #ifdef  ISNAN
   10645           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   10646           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   10647           0 :                     ss << "    {\n";
   10648             : #else
   10649             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   10650             :                     ss << "    {\n";
   10651             : #endif
   10652             :                 }
   10653           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10654             :                 {
   10655             : #ifdef  ISNAN
   10656           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   10657           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   10658           0 :                     ss << "    {\n";
   10659             : #else
   10660             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   10661             :                     ss << "    {\n";
   10662             : #endif
   10663             :                 }
   10664             :                 else
   10665             :                 {
   10666             : #ifdef  ISNAN
   10667           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10668           0 :                     ss << "    {\n";
   10669             : #else
   10670             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10671             :                     ss << "    {\n";
   10672             : #endif
   10673             :                 }
   10674             : 
   10675           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   10676             :                 {
   10677           0 :                     ss << "        arg = ";
   10678           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10679           0 :                     ss << ";\n";
   10680             : #ifdef  ISNAN
   10681           0 :                     ss << "        if (isNan(arg) && ";
   10682           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10683           0 :                     ss << " == 0)\n";
   10684           0 :                     ss << "            continue;\n";
   10685           0 :                     ss << "        if(isNan(arg) && ";
   10686           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10687           0 :                     ss << " != 0)\n";
   10688           0 :                     ss << "        {\n";
   10689           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10690           0 :                     ss << "            continue;\n";
   10691           0 :                     ss << "        }\n";
   10692             : #endif
   10693           0 :                     ss << "        fSum += arg;\n";
   10694           0 :                     ss << "        fCount = fCount + 1.0;\n";
   10695           0 :                     ss << "    }\n";
   10696             :                 }
   10697           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   10698             :                 {
   10699           0 :                     ss << "        arg = ";
   10700           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10701           0 :                     ss << ";\n";
   10702             : #ifdef  ISNAN
   10703           0 :                     ss << "        if (isNan(arg))\n";
   10704           0 :                     ss << "            continue;\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 == svDoubleVectorRefString)
   10711             :                 {
   10712           0 :                     ss << "        if (";
   10713           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10714           0 :                     ss << " == 0)\n";
   10715           0 :                     ss << "            continue;\n";
   10716           0 :                     ss << "        fCount = fCount + 1.0;\n";
   10717           0 :                     ss << "    }\n";
   10718             :                 }
   10719             :                 else
   10720             :                 {
   10721           0 :                     ss << "        continue;\n";
   10722           0 :                     ss << "    }\n";
   10723             :                 }
   10724             : 
   10725             :             }
   10726           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   10727             :             {
   10728             :                 const formula::SingleVectorRefToken* pSVR =
   10729           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   10730           0 :                 if(pSVR->GetArray().mpNumericArray
   10731           0 :                     && pSVR->GetArray().mpStringArray)
   10732           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   10733           0 :                 else if(pSVR->GetArray().mpNumericArray)
   10734           0 :                     isMixedSV = svSingleVectorRefDouble;
   10735           0 :                 else if(pSVR->GetArray().mpStringArray)
   10736           0 :                     isMixedSV = svSingleVectorRefString;
   10737             :                 else
   10738           0 :                     isMixedSV = svSingleVectorRefNULL;
   10739             : 
   10740           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   10741             :                 {
   10742             : #ifdef  ISNAN
   10743           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10744           0 :                     ss << "    {\n";
   10745             : #endif
   10746           0 :                     ss << "        arg = ";
   10747           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10748           0 :                     ss << ";\n";
   10749             : #ifdef  ISNAN
   10750           0 :                     ss << "        if (!isNan(arg))\n";
   10751           0 :                     ss << "        {\n";
   10752             : #endif
   10753           0 :                     ss << "            fSum += arg;\n";
   10754           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10755             : #ifdef ISNAN
   10756           0 :                     ss << "        }\n";
   10757           0 :                     ss << "        if (isNan(arg) && ";
   10758           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10759           0 :                     ss << " != 0)\n";
   10760           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10761           0 :                     ss << "    }\n";
   10762             : #endif
   10763             :                 }
   10764           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   10765             :                 {
   10766             : #ifdef  ISNAN
   10767           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10768           0 :                     ss << "    {\n";
   10769             : #endif
   10770           0 :                     ss << "        arg = ";
   10771           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10772             : #ifdef ISNAN
   10773           0 :                     ss << "        if (!isNan(arg))\n";
   10774           0 :                     ss << "        {\n";
   10775             : #endif
   10776           0 :                     ss << "            fSum += arg;\n";
   10777           0 :                     ss << "            fCount += 1.0;\n";
   10778             : #ifdef ISNAN
   10779           0 :                     ss << "        }\n";
   10780           0 :                     ss << "    }\n";
   10781             : #endif
   10782             :                 }
   10783           0 :                 else if(isMixedSV == svSingleVectorRefString)
   10784             :                 {
   10785             : 
   10786           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10787           0 :                     ss << "    {\n";
   10788           0 :                     ss << "        if (";
   10789           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10790           0 :                     ss << " != 0)\n";
   10791           0 :                     ss << "            fCount = fCount + 1.0;\n";
   10792           0 :                     ss << "    }\n";
   10793             :                 }
   10794             :                 else
   10795             :                 {
   10796           0 :                     ss << "    arg =0.0;\n";
   10797             :                 }
   10798             :             }
   10799             :             else
   10800             :             {
   10801           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   10802           0 :                 ss << "    fSum += arg;\n";
   10803           0 :                 ss << "    fCount = fCount + 1.0;\n";
   10804             :             }
   10805             :         }
   10806             :         else
   10807             :         {
   10808           0 :             ss << "    arg = ";
   10809           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10810           0 :             ss << "    fSum += arg;\n";
   10811           0 :             ss << "    fCount = fCount + 1.0;\n";
   10812             :         }
   10813           0 :         if (i == 0)
   10814             :         {
   10815           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
   10816             :         }
   10817             :     }
   10818           0 :     i = vSubArguments.size();
   10819           0 :     while (i--)
   10820             :     {
   10821           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   10822             :         assert(pCur);
   10823             : 
   10824           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
   10825             :         {
   10826           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   10827             :             {
   10828             :                 const formula::DoubleVectorRefToken* pDVR =
   10829           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   10830           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   10831           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   10832           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   10833           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   10834           0 :                     isMixedDV = svDoubleVectorRefDouble;
   10835           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   10836           0 :                     isMixedDV = svDoubleVectorRefString;
   10837             :                 else
   10838           0 :                     isMixedDV = svDoubleVectorRefNULL;
   10839             : 
   10840           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   10841           0 :                 ss << "    for (int i = ";
   10842           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   10843             :                 {
   10844             : #ifdef  ISNAN
   10845           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   10846           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   10847           0 :                     ss << "    {\n";
   10848             : #else
   10849             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   10850             :                     ss << "    {\n";
   10851             : #endif
   10852             :                 }
   10853           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10854             :                 {
   10855             : #ifdef  ISNAN
   10856           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   10857           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   10858           0 :                     ss << "    {\n";
   10859             : #else
   10860             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   10861             :                     ss << "    {\n";
   10862             : #endif
   10863             :                 }
   10864           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   10865             :                 {
   10866             : #ifdef  ISNAN
   10867           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   10868           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   10869           0 :                     ss << "    {\n";
   10870             : #else
   10871             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   10872             :                     ss << "    {\n";
   10873             : #endif
   10874             :                 }
   10875             :                 else
   10876             :                 {
   10877             : #ifdef  ISNAN
   10878           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10879           0 :                     ss << "    {\n";
   10880             : #else
   10881             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   10882             :                     ss << "    {\n";
   10883             : #endif
   10884             :                 }
   10885           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   10886             :                 {
   10887           0 :                     ss << "        arg = ";
   10888           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10889           0 :                     ss << ";\n";
   10890             : #ifdef  ISNAN
   10891           0 :                     ss << "        if (isNan(arg) && ";
   10892           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10893           0 :                     ss << " == 0)\n";
   10894           0 :                     ss << "            continue;\n";
   10895           0 :                     ss << "        if(isNan(arg) && ";
   10896           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10897           0 :                     ss << " != 0)\n";
   10898           0 :                     ss << "            arg = 0.0;\n";
   10899             : #endif
   10900           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10901           0 :                     ss << "    }\n";
   10902             : 
   10903             :                 }
   10904           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   10905             :                 {
   10906           0 :                     ss << "        arg = ";
   10907           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10908           0 :                     ss << ";\n";
   10909             : #ifdef  ISNAN
   10910           0 :                     ss << "        if (isNan(arg))\n";
   10911           0 :                     ss << "            continue;\n";
   10912             : #endif
   10913           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10914           0 :                     ss << "    }\n";
   10915             : 
   10916             :                 }
   10917           0 :                 else if(isMixedDV == svDoubleVectorRefString)
   10918             :                 {
   10919           0 :                     ss << "        if (";
   10920           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10921           0 :                     ss << " == 0)\n";
   10922           0 :                     ss << "            continue;\n";
   10923           0 :                     ss << "        arg = 0.0;\n";
   10924           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   10925           0 :                     ss << "    }\n";
   10926             :                 }
   10927             :                 else
   10928             :                 {
   10929           0 :                     ss << "        continue;\n";
   10930           0 :                     ss << "    }\n";
   10931             :                 }
   10932             :             }
   10933           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   10934             :             {
   10935             :                 const formula::SingleVectorRefToken* pSVR =
   10936           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   10937           0 :                 if(pSVR->GetArray().mpNumericArray
   10938           0 :                     && pSVR->GetArray().mpStringArray)
   10939           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   10940           0 :                 else if(pSVR->GetArray().mpNumericArray)
   10941           0 :                     isMixedSV = svSingleVectorRefDouble;
   10942           0 :                 else if(pSVR->GetArray().mpStringArray)
   10943           0 :                     isMixedSV = svSingleVectorRefString;
   10944             :                 else
   10945           0 :                     isMixedSV = svSingleVectorRefNULL;
   10946             : 
   10947           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   10948             :                 {
   10949             : #ifdef  ISNAN
   10950           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10951           0 :                     ss << "    {\n";
   10952             : #endif
   10953           0 :                     ss << "        arg = ";
   10954           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   10955           0 :                     ss << ";\n";
   10956             : #ifdef  ISNAN
   10957           0 :                     ss << "        if (!isNan(arg))\n";
   10958             : #endif
   10959           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10960             : #ifdef ISNAN
   10961           0 :                     ss << "        if (isNan(arg) && ";
   10962           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   10963           0 :                     ss << " != 0)\n";
   10964           0 :                     ss << "        {\n";
   10965           0 :                     ss << "            arg = 0.0;\n";
   10966           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10967           0 :                     ss << "        }\n";
   10968           0 :                     ss << "    }\n";
   10969             : #endif
   10970             :                 }
   10971           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   10972             :                 {
   10973             : #ifdef  ISNAN
   10974           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10975           0 :                    ss << "    {\n";
   10976             : #endif
   10977           0 :                     ss << "        arg = ";
   10978           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   10979             : #ifdef  ISNAN
   10980           0 :                     ss << "        if (!isNan(arg))\n";
   10981           0 :                     ss << "        {\n";
   10982             : #endif
   10983           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10984             : #ifdef ISNAN
   10985           0 :                     ss << "        }\n";
   10986           0 :                    ss << "    }\n";
   10987             : #endif
   10988             :                 }
   10989           0 :                 else if(isMixedSV == svSingleVectorRefString)
   10990             :                 {
   10991           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   10992           0 :                     ss << "    {\n";
   10993           0 :                     ss << "        if (";
   10994           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   10995           0 :                     ss << " != 0)\n";
   10996           0 :                     ss << "        {\n";
   10997           0 :                     ss << "            arg = 0.0;\n";
   10998           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   10999           0 :                     ss << "        }\n";
   11000           0 :                     ss << "    }\n";
   11001             :                 }
   11002             :                 else
   11003             :                 {
   11004           0 :                     ss << "    arg = 0.0;\n";
   11005             :                 }
   11006             :             }
   11007             :             else
   11008             :             {
   11009           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   11010           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   11011             :             }
   11012             :         }
   11013             :         else
   11014             :         {
   11015           0 :             ss << "    arg = ";
   11016           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   11017           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   11018             :         }
   11019             :     }
   11020           0 :     ss << "    if (fCount <= 1.0)\n";
   11021           0 :     ss << "        return DBL_MAX;\n";
   11022           0 :     ss << "    else\n";
   11023           0 :     ss << "        return sqrt(vSum * pow(fCount - 1.0,-1.0));\n";
   11024           0 :     ss << "}\n";
   11025           0 : }
   11026             : 
   11027           0 : void OpStDevPA::GenSlidingWindowFunction(std::stringstream &ss,
   11028             :             const std::string &sSymName, SubArguments &vSubArguments)
   11029             : {
   11030           0 :     int isMixedDV = 0;
   11031           0 :     int isMixedSV = 0;
   11032           0 :     ss << "\ndouble " << sSymName;
   11033           0 :     ss << "_" << BinFuncName() << "(";
   11034           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   11035             :     {
   11036           0 :         if (i)
   11037           0 :             ss << ",";
   11038           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
   11039             :     }
   11040           0 :     ss << "){\n";
   11041           0 :     ss << "    int gid0 = get_global_id(0);\n";
   11042           0 :     ss << "    double fSum = 0.0;\n";
   11043           0 :     ss << "    double fMean = 0.0;\n";
   11044           0 :     ss << "    double vSum = 0.0;\n";
   11045           0 :     ss << "    double fCount = 0.0;\n";
   11046           0 :     ss << "    double arg = 0.0;\n";
   11047           0 :     unsigned i = vSubArguments.size();
   11048           0 :     while (i--)
   11049             :     {
   11050           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   11051             :         assert(pCur);
   11052             : 
   11053           0 :         if(ocPush == vSubArguments[i]->GetFormulaToken()->GetOpCode())
   11054             :         {
   11055           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   11056             :             {
   11057             :                 const formula::DoubleVectorRefToken* pDVR =
   11058           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   11059           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   11060           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   11061           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   11062           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   11063           0 :                     isMixedDV = svDoubleVectorRefDouble;
   11064           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   11065           0 :                     isMixedDV = svDoubleVectorRefString;
   11066             :                 else
   11067           0 :                     isMixedDV = svDoubleVectorRefNULL;
   11068             : 
   11069           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   11070           0 :                 ss << "    for (int i = ";
   11071           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   11072             :                 {
   11073             : #ifdef  ISNAN
   11074           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   11075           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   11076           0 :                     ss << "    {\n";
   11077             : #else
   11078             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   11079             :                     ss << "    {\n";
   11080             : #endif
   11081             :                 }
   11082           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   11083             :                 {
   11084             : #ifdef  ISNAN
   11085           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   11086           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   11087           0 :                     ss << "    {\n";
   11088             : #else
   11089             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   11090             :                     ss << "    {\n";
   11091             : #endif
   11092             :                 }
   11093           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   11094             :                 {
   11095             : #ifdef  ISNAN
   11096           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   11097           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   11098           0 :                     ss << "    {\n";
   11099             : #else
   11100             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   11101             :                     ss << "    {\n";
   11102             : #endif
   11103             :                 }
   11104             :                 else
   11105             :                 {
   11106             : #ifdef  ISNAN
   11107           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   11108           0 :                     ss << "    {\n";
   11109             : #else
   11110             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   11111             :                     ss << "    {\n";
   11112             : #endif
   11113             :                 }
   11114             : 
   11115           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   11116             :                 {
   11117           0 :                     ss << "        arg = ";
   11118           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   11119           0 :                     ss << ";\n";
   11120             : #ifdef  ISNAN
   11121           0 :                     ss << "        if (isNan(arg) && ";
   11122           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11123           0 :                     ss << " == 0)\n";
   11124           0 :                     ss << "            continue;\n";
   11125           0 :                     ss << "        if(isNan(arg) && ";
   11126           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11127           0 :                     ss << " != 0)\n";
   11128           0 :                     ss << "        {\n";
   11129           0 :                     ss << "            fCount = fCount + 1.0;\n";
   11130           0 :                     ss << "            continue;\n";
   11131           0 :                     ss << "        }\n";
   11132             : #endif
   11133           0 :                     ss << "        fSum += arg;\n";
   11134           0 :                     ss << "        fCount = fCount + 1.0;\n";
   11135           0 :                     ss << "    }\n";
   11136             :                 }
   11137           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   11138             :                 {
   11139           0 :                     ss << "        arg = ";
   11140           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11141           0 :                     ss << ";\n";
   11142             : #ifdef  ISNAN
   11143           0 :                     ss << "        if (isNan(arg))\n";
   11144           0 :                     ss << "            continue;\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 == svDoubleVectorRefString)
   11151             :                 {
   11152           0 :                     ss << "        if (";
   11153           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11154           0 :                     ss << " == 0)\n";
   11155           0 :                     ss << "            continue;\n";
   11156           0 :                     ss << "        fCount = fCount + 1.0;\n";
   11157           0 :                     ss << "    }\n";
   11158             :                 }
   11159             :                 else
   11160             :                 {
   11161           0 :                     ss << "        continue;\n";
   11162           0 :                     ss << "    }\n";
   11163             :                 }
   11164             : 
   11165             :             }
   11166           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   11167             :             {
   11168             :                 const formula::SingleVectorRefToken* pSVR =
   11169           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   11170           0 :                 if(pSVR->GetArray().mpNumericArray
   11171           0 :                     && pSVR->GetArray().mpStringArray)
   11172           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   11173           0 :                 else if(pSVR->GetArray().mpNumericArray)
   11174           0 :                     isMixedSV = svSingleVectorRefDouble;
   11175           0 :                 else if(pSVR->GetArray().mpStringArray)
   11176           0 :                     isMixedSV = svSingleVectorRefString;
   11177             :                 else
   11178           0 :                     isMixedSV = svSingleVectorRefNULL;
   11179             : 
   11180           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   11181             :                 {
   11182             : #ifdef  ISNAN
   11183           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11184           0 :                     ss << "    {\n";
   11185             : #endif
   11186           0 :                     ss << "        arg = ";
   11187           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   11188           0 :                     ss << ";\n";
   11189             : #ifdef  ISNAN
   11190           0 :                     ss << "        if (!isNan(arg))\n";
   11191           0 :                     ss << "        {\n";
   11192             : #endif
   11193           0 :                     ss << "            fSum += arg;\n";
   11194           0 :                     ss << "            fCount = fCount + 1.0;\n";
   11195             : #ifdef ISNAN
   11196           0 :                     ss << "        }\n";
   11197           0 :                     ss << "        if (isNan(arg) && ";
   11198           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11199           0 :                     ss << " != 0)\n";
   11200           0 :                     ss << "            fCount = fCount + 1.0;\n";
   11201           0 :                     ss << "    }\n";
   11202             : #endif
   11203             :                 }
   11204           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   11205             :                 {
   11206             : #ifdef  ISNAN
   11207           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11208           0 :                     ss << "    {\n";
   11209             : #endif
   11210           0 :                     ss << "        arg = ";
   11211           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   11212             : #ifdef ISNAN
   11213           0 :                     ss << "        if (!isNan(arg))\n";
   11214           0 :                     ss << "        {\n";
   11215             : #endif
   11216           0 :                     ss << "            fSum += arg;\n";
   11217           0 :                     ss << "            fCount += 1.0;\n";
   11218             : #ifdef ISNAN
   11219           0 :                     ss << "        }\n";
   11220           0 :                     ss << "    }\n";
   11221             : #endif
   11222             :                 }
   11223           0 :                 else if(isMixedSV == svSingleVectorRefString)
   11224             :                 {
   11225             : 
   11226           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11227           0 :                     ss << "    {\n";
   11228           0 :                     ss << "        if (";
   11229           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11230           0 :                     ss << " != 0)\n";
   11231           0 :                     ss << "            fCount = fCount + 1.0;\n";
   11232           0 :                     ss << "    }\n";
   11233             :                 }
   11234             :                 else
   11235             :                 {
   11236           0 :                     ss << "    arg =0.0;\n";
   11237             :                 }
   11238             :             }
   11239             :             else
   11240             :             {
   11241           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   11242           0 :                 ss << "    fSum += arg;\n";
   11243           0 :                 ss << "    fCount = fCount + 1.0;\n";
   11244             :             }
   11245             :         }
   11246             :         else
   11247             :         {
   11248           0 :             ss << "    arg = ";
   11249           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   11250           0 :             ss << "    fSum += arg;\n";
   11251           0 :             ss << "    fCount = fCount + 1.0;\n";
   11252             :         }
   11253           0 :         if (i == 0)
   11254             :         {
   11255           0 :             ss << "    fMean = fSum * pow(fCount,-1.0);\n";
   11256             :         }
   11257             :     }
   11258           0 :     i = vSubArguments.size();
   11259           0 :     while (i--)
   11260             :     {
   11261           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   11262             :         assert(pCur);
   11263             : 
   11264           0 :         if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
   11265             :         {
   11266           0 :             if (pCur->GetType() == formula::svDoubleVectorRef)
   11267             :             {
   11268             :                 const formula::DoubleVectorRefToken* pDVR =
   11269           0 :                     static_cast<const formula::DoubleVectorRefToken *>(pCur);
   11270           0 :                 if(pDVR->GetArrays()[0].mpNumericArray
   11271           0 :                     && pDVR->GetArrays()[0].mpStringArray)
   11272           0 :                     isMixedDV = svDoubleVectorRefDoubleString;
   11273           0 :                 else if(pDVR->GetArrays()[0].mpNumericArray)
   11274           0 :                     isMixedDV = svDoubleVectorRefDouble;
   11275           0 :                 else if(pDVR->GetArrays()[0].mpStringArray)
   11276           0 :                     isMixedDV = svDoubleVectorRefString;
   11277             :                 else
   11278           0 :                     isMixedDV = svDoubleVectorRefNULL;
   11279             : 
   11280           0 :                 size_t nCurWindowSize = pDVR->GetRefRowSize();
   11281           0 :                 ss << "    for (int i = ";
   11282           0 :                 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed())
   11283             :                 {
   11284             : #ifdef  ISNAN
   11285           0 :                     ss << "gid0; i < " << pDVR->GetArrayLength();
   11286           0 :                     ss << " && i < " << nCurWindowSize  << "; i++)\n";
   11287           0 :                     ss << "    {\n";
   11288             : #else
   11289             :                     ss << "gid0; i < " << nCurWindowSize << "; i++)\n";
   11290             :                     ss << "    {\n";
   11291             : #endif
   11292             :                 }
   11293           0 :                 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   11294             :                 {
   11295             : #ifdef  ISNAN
   11296           0 :                     ss << "0; i < " << pDVR->GetArrayLength();
   11297           0 :                     ss << " && i < gid0+" << nCurWindowSize << "; i++)\n";
   11298           0 :                     ss << "    {\n";
   11299             : #else
   11300             :                     ss << "0; i < gid0+" << nCurWindowSize << "; i++)\n";
   11301             :                     ss << "    {\n";
   11302             : #endif
   11303             :                 }
   11304           0 :                 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed())
   11305             :                 {
   11306             : #ifdef  ISNAN
   11307           0 :                     ss << "0; i + gid0 < " << pDVR->GetArrayLength();
   11308           0 :                     ss << " &&  i < " << nCurWindowSize << "; i++)\n";
   11309           0 :                     ss << "    {\n";
   11310             : #else
   11311             :                     ss << "0; i < " << nCurWindowSize << "; i++)\n";
   11312             :                     ss << "    {\n";
   11313             : #endif
   11314             :                 }
   11315             :                 else
   11316             :                 {
   11317             : #ifdef  ISNAN
   11318           0 :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   11319           0 :                     ss << "    {\n";
   11320             : #else
   11321             :                     ss << "0; i < " << pDVR->GetArrayLength() << "; i++)\n";
   11322             :                     ss << "    {\n";
   11323             : #endif
   11324             :                 }
   11325           0 :                 if(isMixedDV == svDoubleVectorRefDoubleString)
   11326             :                 {
   11327           0 :                     ss << "        arg = ";
   11328           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   11329           0 :                     ss << ";\n";
   11330             : #ifdef  ISNAN
   11331           0 :                     ss << "        if (isNan(arg) && ";
   11332           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11333           0 :                     ss << " == 0)\n";
   11334           0 :                     ss << "            continue;\n";
   11335           0 :                     ss << "        if(isNan(arg) && ";
   11336           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11337           0 :                     ss << " != 0)\n";
   11338           0 :                     ss << "            arg = 0.0;\n";
   11339             : #endif
   11340           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   11341           0 :                     ss << "    }\n";
   11342             : 
   11343             :                 }
   11344           0 :                 else if(isMixedDV == svDoubleVectorRefDouble)
   11345             :                 {
   11346           0 :                     ss << "        arg = ";
   11347           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11348           0 :                     ss << ";\n";
   11349             : #ifdef  ISNAN
   11350           0 :                     ss << "        if (isNan(arg))\n";
   11351           0 :                     ss << "            continue;\n";
   11352             : #endif
   11353           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   11354           0 :                     ss << "    }\n";
   11355             : 
   11356             :                 }
   11357           0 :                 else if(isMixedDV == svDoubleVectorRefString)
   11358             :                 {
   11359           0 :                     ss << "        if (";
   11360           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11361           0 :                     ss << " == 0)\n";
   11362           0 :                     ss << "            continue;\n";
   11363           0 :                     ss << "        arg = 0.0;\n";
   11364           0 :                     ss << "        vSum += (arg - fMean) * (arg - fMean);\n";
   11365           0 :                     ss << "    }\n";
   11366             :                 }
   11367             :                 else
   11368             :                 {
   11369           0 :                     ss << "        continue;\n";
   11370           0 :                     ss << "    }\n";
   11371             :                 }
   11372             :             }
   11373           0 :             else if (pCur->GetType() == formula::svSingleVectorRef)
   11374             :             {
   11375             :                 const formula::SingleVectorRefToken* pSVR =
   11376           0 :                     static_cast< const formula::SingleVectorRefToken* >(pCur);
   11377           0 :                 if(pSVR->GetArray().mpNumericArray
   11378           0 :                     && pSVR->GetArray().mpStringArray)
   11379           0 :                     isMixedSV = svSingleVectorRefDoubleString;
   11380           0 :                 else if(pSVR->GetArray().mpNumericArray)
   11381           0 :                     isMixedSV = svSingleVectorRefDouble;
   11382           0 :                 else if(pSVR->GetArray().mpStringArray)
   11383           0 :                     isMixedSV = svSingleVectorRefString;
   11384             :                 else
   11385           0 :                     isMixedSV = svSingleVectorRefNULL;
   11386             : 
   11387           0 :                 if(isMixedSV == svSingleVectorRefDoubleString)
   11388             :                 {
   11389             : #ifdef  ISNAN
   11390           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11391           0 :                     ss << "    {\n";
   11392             : #endif
   11393           0 :                     ss << "        arg = ";
   11394           0 :                     ss << vSubArguments[i]->GenDoubleSlidingWindowDeclRef();
   11395           0 :                     ss << ";\n";
   11396             : #ifdef  ISNAN
   11397           0 :                     ss << "        if (!isNan(arg))\n";
   11398             : #endif
   11399           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   11400             : #ifdef ISNAN
   11401           0 :                     ss << "        if (isNan(arg) && ";
   11402           0 :                     ss << vSubArguments[i]->GenStringSlidingWindowDeclRef();
   11403           0 :                     ss << " != 0)\n";
   11404           0 :                     ss << "        {\n";
   11405           0 :                     ss << "            arg = 0.0;\n";
   11406           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   11407           0 :                     ss << "        }\n";
   11408           0 :                     ss << "    }\n";
   11409             : #endif
   11410             :                 }
   11411           0 :                 else if(isMixedSV == svSingleVectorRefDouble)
   11412             :                 {
   11413             : #ifdef  ISNAN
   11414           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11415           0 :                    ss << "    {\n";
   11416             : #endif
   11417           0 :                     ss << "        arg = ";
   11418           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   11419             : #ifdef  ISNAN
   11420           0 :                     ss << "        if (!isNan(arg))\n";
   11421           0 :                     ss << "        {\n";
   11422             : #endif
   11423           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   11424             : #ifdef ISNAN
   11425           0 :                     ss << "        }\n";
   11426           0 :                    ss << "    }\n";
   11427             : #endif
   11428             :                 }
   11429           0 :                 else if(isMixedSV == svSingleVectorRefString)
   11430             :                 {
   11431           0 :                     ss << "    if (gid0 < " << pSVR->GetArrayLength() << ")\n";
   11432           0 :                     ss << "    {\n";
   11433           0 :                     ss << "        if (";
   11434           0 :                     ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11435           0 :                     ss << " != 0)\n";
   11436           0 :                     ss << "        {\n";
   11437           0 :                     ss << "            arg = 0.0;\n";
   11438           0 :                     ss << "            vSum += (arg - fMean)*(arg - fMean);\n";
   11439           0 :                     ss << "        }\n";
   11440           0 :                     ss << "    }\n";
   11441             :                 }
   11442             :                 else
   11443             :                 {
   11444           0 :                     ss << "    arg = 0.0;\n";
   11445             :                 }
   11446             :             }
   11447             :             else
   11448             :             {
   11449           0 :                 ss << "    arg = " << pCur->GetDouble() << ";\n";
   11450           0 :                 ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   11451             :             }
   11452             :         }
   11453             :         else
   11454             :         {
   11455           0 :             ss << "    arg = ";
   11456           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
   11457           0 :             ss << "    vSum += (arg - fMean) * (arg - fMean);\n";
   11458             :         }
   11459             :     }
   11460           0 :     ss << "    if (fCount == 1.0)\n";
   11461           0 :     ss << "        return DBL_MAX;\n";
   11462           0 :     ss << "    else\n";
   11463           0 :     ss << "        return sqrt(vSum * pow(fCount,-1.0));\n";
   11464           0 :     ss << "}\n";
   11465           0 : }
   11466             : 
   11467           0 : void OpAveDev:: GenSlidingWindowFunction(std::stringstream &ss,
   11468             :             const std::string &sSymName, SubArguments &vSubArguments)
   11469             : {
   11470           0 :     ss << "\ndouble " << sSymName;
   11471           0 :     ss << "_"<< BinFuncName() <<"( ";
   11472           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   11473             :     {
   11474           0 :         if (i)
   11475           0 :             ss << ",";
   11476           0 :         vSubArguments[i]->GenSlidingWindowDecl(ss);
   11477             :     }
   11478           0 :     ss << ")\n";
   11479           0 :     ss <<"{\n";
   11480           0 :     ss << "    int gid0 = get_global_id(0);\n";
   11481           0 :     ss << "    double sum=0.0;\n";
   11482           0 :     ss << "    double length;\n";
   11483           0 :     ss << "    double totallength=0;\n";
   11484           0 :     ss << "    double tmp = 0;\n";
   11485           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   11486             :     {
   11487           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   11488             :         assert(pCur);
   11489           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
   11490             :         {
   11491             :             const formula::DoubleVectorRefToken* pDVR =
   11492           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
   11493           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
   11494           0 :             ss << "    length="<<nCurWindowSize;
   11495           0 :             ss << ";\n";
   11496           0 :             ss << "    for (int i = ";
   11497           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
   11498           0 :             ss << "    {\n";
   11499           0 :             ss << "        double arg"<<i<<" = ";
   11500           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11501           0 :             ss << ";\n";
   11502             : #ifdef ISNAN
   11503           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
   11504           0 :             ss << pDVR->GetArrayLength();
   11505           0 :             ss << "))\n";
   11506           0 :             ss << "        {\n";
   11507           0 :             ss << "            length-=1.0;\n";
   11508           0 :             ss << "            continue;\n";
   11509           0 :             ss << "        }\n";
   11510             : #endif
   11511           0 :             ss << "        sum +=  arg"<<i<<";\n";
   11512           0 :             ss << "    }\n";
   11513           0 :             ss << "    totallength +=length;\n";
   11514             :         }
   11515           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
   11516             :         {
   11517           0 :             ss << "    tmp = ";
   11518           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11519           0 :             ss << ";\n";
   11520             : #ifdef ISNAN
   11521           0 :             ss << "    if(!isNan(tmp))\n";
   11522           0 :             ss << "    {\n";
   11523           0 :             ss << "        sum += tmp;\n";
   11524           0 :             ss << "        totallength +=1;\n";
   11525           0 :             ss << "    }\n";
   11526             : #endif
   11527             :         }
   11528           0 :         else if (pCur->GetType() == formula::svDouble)
   11529             :         {
   11530           0 :            ss << "    tmp = ";
   11531           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11532           0 :            ss << ";\n";
   11533           0 :            ss << "    sum += tmp;\n";
   11534           0 :            ss << "    totallength +=1;\n";
   11535             :         }
   11536             :     }
   11537           0 :     ss << "    double mean = sum * pow(totallength,-1);\n";
   11538           0 :     ss << "    sum = 0.0;\n";
   11539           0 :     for (unsigned i = 0; i < vSubArguments.size(); i++)
   11540             :     {
   11541           0 :         FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
   11542             :         assert(pCur);
   11543           0 :         if (pCur->GetType() == formula::svDoubleVectorRef)
   11544             :         {
   11545             :             const formula::DoubleVectorRefToken* pDVR =
   11546           0 :                 static_cast<const formula::DoubleVectorRefToken *>(pCur);
   11547           0 :             size_t nCurWindowSize = pDVR->GetRefRowSize();
   11548           0 :             ss << "    for (int i = ";
   11549           0 :             ss << "0; i < "<< nCurWindowSize << "; i++)\n";
   11550           0 :             ss << "    {\n";
   11551           0 :             ss << "        double arg"<<i<<" = ";
   11552           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11553           0 :             ss << ";\n";
   11554             : #ifdef ISNAN
   11555           0 :             ss << "        if(isNan(arg"<<i<<")||((gid0+i)>=";
   11556           0 :             ss << pDVR->GetArrayLength();
   11557           0 :             ss << "))\n";
   11558           0 :             ss << "        {\n";
   11559           0 :             ss << "            continue;\n";
   11560           0 :             ss << "        }\n";
   11561             : #endif
   11562           0 :             ss << "        sum +=  fabs(arg"<<i<<"-mean);\n";
   11563           0 :             ss << "    }\n";
   11564             :         }
   11565           0 :         else if (pCur->GetType() == formula::svSingleVectorRef)
   11566             :         {
   11567           0 :             ss << "    tmp = ";
   11568           0 :             ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11569           0 :             ss << ";\n";
   11570             : #ifdef ISNAN
   11571           0 :             ss << "    if(!isNan(tmp))\n";
   11572           0 :             ss << "    {\n";
   11573           0 :             ss << "        sum += fabs(tmp-mean);\n";
   11574           0 :             ss << "    }\n";
   11575             : #endif
   11576             :         }
   11577           0 :         else if (pCur->GetType() == formula::svDouble)
   11578             :         {
   11579           0 :            ss << "    tmp = ";
   11580           0 :            ss << vSubArguments[i]->GenSlidingWindowDeclRef();
   11581           0 :            ss << ";\n";
   11582           0 :            ss << "    sum += fabs(tmp-mean);\n";
   11583             :         }
   11584             :     }
   11585           0 :     ss << "    tmp=sum*pow(totallength,-1);\n";
   11586           0 :     ss << "    return tmp;\n";
   11587           0 :     ss << "}";
   11588           0 : }
   11589           0 : }}
   11590             : 
   11591             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10