Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / opencl / op_financial.cxx
blob2bd250e829bf29bd162e54918f2e0d1497ea5c81
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include "op_financial.hxx"
12 #include "formulagroup.hxx"
13 #include "document.hxx"
14 #include "formulacell.hxx"
15 #include "tokenarray.hxx"
16 #include "compiler.hxx"
17 #include "interpre.hxx"
18 #include "formula/vectortoken.hxx"
19 #include <sstream>
21 using namespace formula;
23 namespace sc { namespace opencl {
24 // Definitions of inline functions
25 #include "opinlinefun_finacial.cxx"
27 void RRI::GenSlidingWindowFunction(
28 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
30 ss << "\ndouble " << sSymName;
31 ss << "_"<< BinFuncName() <<"(";
32 for (unsigned i = 0; i < vSubArguments.size(); i++)
34 if (i)
35 ss << ",";
36 vSubArguments[i]->GenSlidingWindowDecl(ss);
38 ss << ") {\n";
39 ss << " double tmp = " << GetBottom() <<";\n";
40 ss << " int gid0 = get_global_id(0);\n";
41 ss << " double fv;\n";
42 ss << " double pv;\n";
43 ss << " double nper;\n";
44 #ifdef ISNAN
45 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
46 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
47 formula::SingleVectorRefToken *>(tmpCur0);
49 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
50 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
51 formula::SingleVectorRefToken *>(tmpCur1);
53 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
54 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
55 formula::SingleVectorRefToken *>(tmpCur2);
57 ss<< " int buffer_nper_len = ";
58 ss<< tmpCurDVR0->GetArrayLength();
59 ss << ";\n";
61 ss<< " int buffer_pv_len = ";
62 ss<< tmpCurDVR1->GetArrayLength();
63 ss << ";\n";
65 ss<< " int buffer_fv_len = ";
66 ss<< tmpCurDVR2->GetArrayLength();
67 ss << ";\n";
68 #endif
70 #ifdef ISNAN
71 ss<<" if(gid0>=buffer_nper_len || isNan(";
72 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
73 ss<<"))\n";
74 ss<<" nper = 0;\n\telse \n";
75 #endif
76 ss<<" nper = ";
77 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
78 ss<<";\n";
80 #ifdef ISNAN
81 ss<<" if(gid0>=buffer_pv_len || isNan(";
82 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
83 ss<<"))\n";
84 ss<<" pv = 0;\n\telse \n";
85 #endif
86 ss<<" pv = ";
87 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
88 ss<<";\n";
90 #ifdef ISNAN
91 ss<<" if(gid0>=buffer_pv_len || isNan(";
92 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
93 ss<<"))\n";
94 ss<<" fv = 0;\n\telse \n";
95 #endif
96 ss<<" fv = ";
97 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
98 ss<<";\n";
99 ss << " tmp = pow(fv*pow(pv,-1),1.0*pow(nper,-1))-1;\n";
100 ss << " return tmp;\n";
101 ss << "}";
104 void OpNominal::GenSlidingWindowFunction(
105 std::stringstream &ss, const std::string sSymName, SubArguments &
106 vSubArguments)
108 ss << "\ndouble " << sSymName;
109 ss << "_"<< BinFuncName() <<"(";
110 for (unsigned i = 0; i < vSubArguments.size(); i++)
112 if (i)
113 ss << ",";
114 vSubArguments[i]->GenSlidingWindowDecl(ss);
116 ss << ") {\n\t";
117 ss << "double tmp = 0;\n\t";
118 ss << "double temp = 0;\n\t";
119 ss << "int gid0 = get_global_id(0);\n\t";
120 ss << "double tmp0=0,tmp1=0;\n";
121 for (unsigned i = 0; i < vSubArguments.size(); i++)
123 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
124 assert(pCur);
125 if (pCur->GetType() == formula::svSingleVectorRef)
127 #ifdef ISNAN
128 const formula::SingleVectorRefToken* pSVR =
129 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
130 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
131 #else
132 #endif
134 else if (pCur->GetType() == formula::svDouble)
136 #ifdef ISNAN
137 ss << "{\n";
138 #endif
140 else
142 #ifdef ISNAN
143 #endif
145 #ifdef ISNAN
146 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
148 ss <<" temp="<<vSubArguments[i]->GenSlidingWindowDeclRef();
149 ss <<";\n";
150 ss <<" if (isNan(temp))\n";
151 ss <<" tmp"<<i<<"= 0;\n";
152 ss <<" else\n";
153 ss <<" tmp"<<i<<"=temp;\n";
154 ss <<" }\n";
156 else
158 ss <<" tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef(
160 ss <<";\n";
162 #else
163 ss <<" tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
164 ss <<";\n";
165 #endif
167 ss<<"if(tmp1==0)\n\t";
168 ss<<"\treturn 0;\n\t";
169 ss<<"tmp=pow( tmp1,-1);\n\t";
170 ss<<"tmp=( pow( tmp0+ 1.0, tmp ) - 1.0 ) *";
171 ss<<"tmp1;\n\t";
172 ss << "return tmp;\n";
173 ss << "}";
176 void OpDollarde::GenSlidingWindowFunction(
177 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
179 ss << "\ndouble " << sSymName;
180 ss << "_"<< BinFuncName() <<"(";
181 for (unsigned i = 0; i < vSubArguments.size(); i++)
183 if (i)
184 ss << ",";
185 vSubArguments[i]->GenSlidingWindowDecl(ss);
187 ss << ") {\n\t";
188 ss << "double tmp = " << GetBottom() <<";\n\t";
189 ss << "int gid0 = get_global_id(0);\n\t";
190 ss << "double fInt = " << GetBottom() <<";\n\t";
191 ss << "double dollar;\n\t";
192 ss << "double fFrac;\n\t";
193 #ifdef ISNAN
194 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
195 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
196 formula::SingleVectorRefToken *>(tmpCur0);
197 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
198 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
199 formula::SingleVectorRefToken *>(tmpCur1);
200 ss<< "int buffer_dollar_len = ";
201 ss<< tmpCurDVR0->GetArrayLength();
202 ss << ";\n\t";
203 ss<< "int buffer_frac_len = ";
204 ss<< tmpCurDVR1->GetArrayLength();
205 ss << ";\n\t";
206 #endif
207 #ifdef ISNAN
208 ss<<"if((gid0)>=buffer_dollar_len || isNan(";
209 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
210 ss<<"))\n\t\t";
211 ss<<"dollar = 0;\n\telse \n\t\t";
212 #endif
213 ss<<"dollar = ";
214 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
215 ss<<";\n\t";
216 #ifdef ISNAN
217 ss<<"if((gid0)>=buffer_frac_len || isNan(";
218 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
219 ss<<"))\n\t\t";
220 ss<<"fFrac = 0;\n\telse \n\t\t";
221 #endif
222 ss<<"fFrac = ";
223 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
224 ss<<";\n\t";
225 ss << "tmp = modf( dollar , &fInt );\n\t";
226 ss << "tmp /= fFrac;\n\t";
227 ss << "tmp *= pow( 10.0 , ceil( log10(fFrac ) ) );\n\t";
228 ss << "tmp += fInt;\t";
229 ss << "\n\treturn tmp;\n";
230 ss << "}";
233 void OpDollarfr::GenSlidingWindowFunction(std::stringstream &ss,
234 const std::string sSymName, SubArguments &vSubArguments)
236 ss << "\ndouble " << sSymName;
237 ss << "_"<< BinFuncName() <<"(";
238 for (unsigned i = 0; i < vSubArguments.size(); i++)
240 if (i)
241 ss << ",";
242 vSubArguments[i]->GenSlidingWindowDecl(ss);
244 ss << ") {\n\t";
245 ss << "double tmp = " << GetBottom() <<";\n\t";
246 ss << "int gid0 = get_global_id(0);\n\t";
247 ss << "double fInt = " << GetBottom() <<";\n\t";
248 ss << "double dollar;\n\t";
249 ss << "double fFrac;\n\t";
250 #ifdef ISNAN
251 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
252 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
253 formula::SingleVectorRefToken *>(tmpCur0);
254 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
255 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
256 formula::SingleVectorRefToken *>(tmpCur1);
257 ss<< "int buffer_dollar_len = ";
258 ss<< tmpCurDVR0->GetArrayLength();
259 ss << ";\n\t";
260 ss<< "int buffer_frac_len = ";
261 ss<< tmpCurDVR1->GetArrayLength();
262 ss << ";\n\t";
263 #endif
264 #ifdef ISNAN
265 ss<<"if((gid0)>=buffer_dollar_len || isNan(";
266 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
267 ss<<"))\n\t\t";
268 ss<<"dollar = 0;\n\telse \n\t\t";
269 #endif
270 ss<<"dollar = ";
271 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
272 ss<<";\n\t";
273 #ifdef ISNAN
274 ss<<"if((gid0)>=buffer_frac_len || isNan(";
275 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
276 ss<<"))\n\t\t";
277 ss<<"fFrac = 0;\n\telse \n\t\t";
278 #endif
279 ss<<"fFrac = ";
280 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
281 ss<<";\n\t";
282 ss << "tmp = modf( dollar , &fInt );\n\t";
283 ss << "tmp *= fFrac;\n\t";
284 ss << "tmp *= pow( 10.0 , -ceil( log10(fFrac ) ) );\n\t";
285 ss << "tmp += fInt;\t";
286 ss << "\n\treturn tmp;\n";
287 ss << "}";
290 void OpDISC::BinInlineFun(std::set<std::string>& decls,
291 std::set<std::string>& funs)
293 decls.insert(GetYearFrac_newDecl);decls.insert(DaysToDate_newDecl);
294 decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
295 funs.insert(GetYearFrac_new);funs.insert(DaysToDate_new);
296 funs.insert(DaysInMonth);funs.insert(IsLeapYear);
299 void OpDISC::GenSlidingWindowFunction(std::stringstream& ss,
300 const std::string sSymName, SubArguments& vSubArguments)
302 ss << "\ndouble " << sSymName;
303 ss << "_" << BinFuncName() << "(";
304 for (unsigned i = 0; i < vSubArguments.size(); i++)
306 if (i)
307 ss << ",";
308 vSubArguments[i]->GenSlidingWindowDecl(ss);
310 ss << ") {\n\t";
311 ss << " double tmp = " << GetBottom() << ";\n";
312 ss << " int gid0 = get_global_id(0);\n";
313 ss << " double arg0 = " << GetBottom() << ";\n";
314 ss << " double arg1 = " << GetBottom() << ";\n";
315 ss << " double arg2 = " << GetBottom() << ";\n";
316 ss << " double arg3 = " << GetBottom() << ";\n";
317 ss << " double arg4 = " << GetBottom() << ";\n";
318 for (unsigned i = 0; i < vSubArguments.size(); i++)
320 FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
321 assert(pCur);
322 if (pCur->GetType() == formula::svSingleVectorRef)
324 #ifdef ISNAN
325 const formula::SingleVectorRefToken* pSVR =
326 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
327 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
328 #endif
330 else if (pCur->GetType() == formula::svDouble)
332 #ifdef ISNAN
333 ss << " {\n";
334 #endif
336 #ifdef ISNAN
337 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
339 ss << " if (isNan(";
340 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
341 ss << "))\n";
342 ss << " arg" << i << " = 0;\n";
343 ss << " else\n";
344 ss << " arg" << i << " = ";
345 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
346 ss << " }\n";
348 else
350 ss << " arg" << i << " = ";
351 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
353 #else
354 ss << " arg" << i;
355 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
356 #endif
358 ss << " int nNullDate = 693594;\n";
359 ss << " tmp = 1.0 - arg2 / arg3;\n";
360 ss << " tmp /=";
361 ss << " GetYearFrac_new(nNullDate, (int)arg0, (int)arg1, (int)arg4);\n";
362 ss << " return tmp;\n";
363 ss << "}";
367 void OpINTRATE::BinInlineFun(std::set<std::string>& decls,
368 std::set<std::string>& funs)
370 decls.insert(GetYearDiff_newDecl);decls.insert(GetDiffDate_newDecl);
371 decls.insert(DaysToDate_newDecl);decls.insert(GetNullDateDecl);
372 decls.insert(DateToDaysDecl);decls.insert(DaysInMonthDecl);
373 decls.insert(IsLeapYearDecl);
374 funs.insert(GetYearDiff_new);funs.insert(GetDiffDate_new);
375 funs.insert(DaysToDate_new);funs.insert(GetNullDate);
376 funs.insert(DateToDays);funs.insert(DaysInMonth);
377 funs.insert(IsLeapYear);
380 void OpINTRATE::GenSlidingWindowFunction(std::stringstream& ss,
381 const std::string sSymName, SubArguments& vSubArguments)
383 ss << "\ndouble " << sSymName;
384 ss << "_" << BinFuncName() << "(";
385 for (unsigned i = 0; i < vSubArguments.size(); i++)
387 if (i)
388 ss << ",";
389 vSubArguments[i]->GenSlidingWindowDecl(ss);
391 ss << ") {\n";
392 ss << " double tmp = " << GetBottom() << ";\n";
393 ss << " int gid0 = get_global_id(0);\n";
394 ss << " double arg0 = " << GetBottom() << ";\n";
395 ss << " double arg1 = " << GetBottom() << ";\n";
396 ss << " double arg2 = " << GetBottom() << ";\n";
397 ss << " double arg3 = " << GetBottom() << ";\n";
398 ss << " double arg4 = " << GetBottom() << ";\n";
399 for (unsigned i = 0; i < vSubArguments.size(); i++)
401 FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
402 assert(pCur);
403 if (pCur->GetType() == formula::svSingleVectorRef)
405 #ifdef ISNAN
406 const formula::SingleVectorRefToken* pSVR =
407 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
408 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
409 #endif
411 else if (pCur->GetType() == formula::svDouble)
413 #ifdef ISNAN
414 ss << " {\n";
415 #endif
417 #ifdef ISNAN
418 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
420 ss << " if (isNan(";
421 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
422 ss << "))\n";
423 ss << " arg" << i << " = 0;\n";
424 ss << " else\n";
425 ss << " arg" << i << " = ";
426 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
427 ss << " }\n";
429 else
431 ss << " arg" << i << " = ";
432 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
434 #else
435 ss << " arg" << i;
436 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
437 #endif
439 ss << " int nNullDate = GetNullDate();\n";
440 ss << " tmp = arg3 * pow(arg2,-1) - 1.0;\n";
441 ss << " tmp = tmp * pow(GetYearDiff_new(nNullDate, (int)arg0,";
442 ss << " (int)arg1,(int)arg4),-1);\n";
443 ss << " return tmp;\n";
444 ss << "}";
447 void OpFV::BinInlineFun(std::set<std::string>& decls,
448 std::set<std::string>& funs)
450 decls.insert(GetZwDecl);
451 funs.insert(GetZw);
454 void OpFV::GenSlidingWindowFunction(std::stringstream& ss,
455 const std::string sSymName, SubArguments& vSubArguments)
457 ss << "\ndouble " << sSymName;
458 ss << "_"<< BinFuncName() <<"(";
459 for (unsigned i = 0; i < vSubArguments.size(); i++)
461 if (i)
462 ss << ",";
463 vSubArguments[i]->GenSlidingWindowDecl(ss);
465 ss << ") {\n";
466 ss << " double tmp = " << GetBottom() << ";\n";
467 ss << " int gid0 = get_global_id(0);\n";
468 ss << " double arg0 = " << GetBottom() << ";\n";
469 ss << " double arg1 = " << GetBottom() << ";\n";
470 ss << " double arg2 = " << GetBottom() << ";\n";
471 ss << " double arg3 = " << GetBottom() << ";\n";
472 ss << " double arg4 = " << GetBottom() << ";\n";
473 unsigned j = vSubArguments.size();
474 while (j--)
476 FormulaToken* pCur = vSubArguments[j]->GetFormulaToken();
477 assert(pCur);
478 if(pCur->GetType() == formula::svSingleVectorRef)
480 #ifdef ISNAN
481 const formula::SingleVectorRefToken* pSVR =
482 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
483 ss << " if(gid0 >= " << pSVR->GetArrayLength() << " || isNan(";
484 ss << vSubArguments[j]->GenSlidingWindowDeclRef();
485 ss << "))\n";
486 ss << " arg" << j << " = " <<GetBottom() << ";\n";
487 ss << " else\n";
488 #endif
489 ss << " arg" << j << " = ";
490 ss << vSubArguments[j]->GenSlidingWindowDeclRef();
491 ss << ";\n";
494 ss << " tmp = GetZw(arg0, arg1, arg2, arg3, arg4);\n";
495 ss << " return tmp;\n";
496 ss << "}";
499 void OpIPMT::BinInlineFun(std::set<std::string>& decls,
500 std::set<std::string>& funs)
502 decls.insert(GetZwDecl);
503 funs.insert(GetZw);
506 void OpIPMT::GenSlidingWindowFunction(std::stringstream& ss,
507 const std::string sSymName, SubArguments& vSubArguments)
509 ss << "\ndouble " << sSymName;
510 ss << "_"<< BinFuncName() <<"(";
511 for (unsigned i = 0; i < vSubArguments.size(); i++)
513 if (i)
514 ss << ",";
515 vSubArguments[i]->GenSlidingWindowDecl(ss);
517 ss << ") {\n";
518 ss << " double tmp = " << GetBottom() << ";\n";
519 ss << " int gid0 = get_global_id(0);\n";
520 ss << " double arg0 = " << GetBottom() << ";\n";
521 ss << " double arg1 = " << GetBottom() << ";\n";
522 ss << " double arg2 = " << GetBottom() << ";\n";
523 ss << " double arg3 = " << GetBottom() << ";\n";
524 ss << " double arg4 = " << GetBottom() << ";\n";
525 ss << " double arg5 = " << GetBottom() << ";\n";
526 unsigned j = vSubArguments.size();
527 while (j--)
529 FormulaToken* pCur = vSubArguments[j]->GetFormulaToken();
530 assert(pCur);
531 if(pCur->GetType() == formula::svSingleVectorRef)
533 #ifdef ISNAN
534 const formula::SingleVectorRefToken* pSVR =
535 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
536 ss << " if(gid0 >= " << pSVR->GetArrayLength() << " || isNan(";
537 ss << vSubArguments[j]->GenSlidingWindowDeclRef();
538 ss << "))\n";
539 ss << " arg" << j << " = " <<GetBottom() << ";\n";
540 ss << " else\n";
541 #endif
542 ss << " arg" << j << " = ";
543 ss << vSubArguments[j]->GenSlidingWindowDeclRef();
544 ss << ";\n";
547 ss << " double pmt ;\n";
548 ss << " if(arg0 == 0.0)\n";
549 ss << " return 0;\n";
550 ss << " double temp1 = 0;\n";
551 ss << " double abl = pow(1.0 + arg0, arg2);\n";
552 ss << " temp1 -= arg4;\n";
553 ss << " temp1 -= arg3 * abl;\n";
554 ss << " pmt = temp1 / (1.0 + arg0 * arg5) /";
555 ss << " ( (abl - 1.0) / arg0);\n";
556 ss << " double temp = pow( 1 + arg0, arg1 - 2);\n";
557 ss << " if(arg1 == 1.0)\n";
558 ss << " {\n";
559 ss << " if(arg5 > 0.0)\n";
560 ss << " tmp = 0.0;\n";
561 ss << " else\n";
562 ss << " tmp = -arg3;\n";
563 ss << " }\n";
564 ss << " else\n";
565 ss << " {\n";
566 ss << " if(arg5 > 0.0)\n";
567 ss << " tmp = GetZw(arg0, arg1 - 2.0, pmt, arg3, 1.0)";
568 ss << " - pmt;\n";
569 ss << " else\n";
570 ss << " tmp = GetZw(arg0, arg1 - 1.0, pmt, arg3, 0.0);\n";
571 ss << " }\n";
572 ss << " tmp = tmp * arg0;\n";
573 ss << " return tmp;\n";
574 ss << "}";
576 void OpISPMT::GenSlidingWindowFunction(std::stringstream& ss,
577 const std::string sSymName, SubArguments& vSubArguments)
579 ss << "\ndouble " << sSymName;
580 ss << "_" << BinFuncName() <<"(";
581 for (unsigned i = 0; i < vSubArguments.size(); i++)
583 if (i)
584 ss << ",";
585 vSubArguments[i]->GenSlidingWindowDecl(ss);
587 ss << ") {\n";
588 ss << " double tmp = " << GetBottom() << ";\n";
589 ss << " int gid0 = get_global_id(0);\n";
590 ss << " double arg0 = " << GetBottom() << ";\n";
591 ss << " double arg1 = " << GetBottom() << ";\n";
592 ss << " double arg2 = " << GetBottom() << ";\n";
593 ss << " double arg3 = " << GetBottom() << ";\n";
594 for (unsigned i = 0; i < vSubArguments.size(); i++)
596 FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
597 assert(pCur);
598 if (pCur->GetType() == formula::svSingleVectorRef)
600 #ifdef ISNAN
601 const formula::SingleVectorRefToken* pSVR =
602 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
603 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
604 #endif
606 else if (pCur->GetType() == formula::svDouble)
608 #ifdef ISNAN
609 ss << " {\n";
610 #endif
612 #ifdef ISNAN
613 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
615 ss << " if (isNan(";
616 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
617 ss << "))\n";
618 ss << " arg" << i << " = 0;\n";
619 ss << " else\n";
620 ss << " arg" << i << " = ";
621 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
622 ss << " }\n";
624 else
626 ss << " arg" << i << " = ";
627 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
629 #else
630 ss << " arg" << i;
631 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
632 #endif
634 ss << " tmp = arg3 * arg0 * ( arg1 - arg2) * pow(arg2, -1);\n";
635 ss << " return tmp;\n";
636 ss << "}";
639 void OpDuration::GenSlidingWindowFunction(std::stringstream& ss,
640 const std::string sSymName, SubArguments& vSubArguments)
642 ss << "\ndouble " << sSymName;
643 ss << "_" << BinFuncName() <<"(";
644 for (unsigned i = 0; i < vSubArguments.size(); i++)
646 if (i)
647 ss << ",";
648 vSubArguments[i]->GenSlidingWindowDecl(ss);
650 ss << ") {\n";
651 ss << " double tmp = " << GetBottom() << ";\n";
652 ss << " int gid0 = get_global_id(0);\n";
653 ss << " double arg0 = " << GetBottom() << ";\n";
654 ss << " double arg1 = " << GetBottom() << ";\n";
655 ss << " double arg2 = " << GetBottom() << ";\n";
656 for (unsigned i = 0; i < vSubArguments.size(); i++)
658 FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
659 assert(pCur);
660 if (pCur->GetType() == formula::svSingleVectorRef)
662 #ifdef ISNAN
663 const formula::SingleVectorRefToken* pSVR =
664 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
665 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
666 #endif
668 else if (pCur->GetType() == formula::svDouble)
670 #ifdef ISNAN
671 ss << " {\n";
672 #endif
674 #ifdef ISNAN
675 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
677 ss << " if (isNan(";
678 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
679 ss << "))\n";
680 ss << " arg" << i << " = 0;\n";
681 ss << " else\n";
682 ss << " arg" << i << " = ";
683 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
684 ss << " }\n";
686 else
688 ss << " arg" << i << " = ";
689 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
691 #else
692 ss << " arg" << i;
693 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
694 #endif
696 ss << " tmp = log(arg2 * pow( arg1,-1)) / log(arg0 + 1.0);\n";
697 ss << " return tmp;\n";
698 ss << "}";
701 void OpDuration_ADD::BinInlineFun(std::set<std::string>& decls,
702 std::set<std::string>& funs)
704 decls.insert(GetDurationDecl);decls.insert(lcl_GetcoupnumDecl);
705 decls.insert(GetYearFracDecl);decls.insert(DaysToDateDecl);
706 decls.insert(GetNullDateDecl);decls.insert(DateToDaysDecl);
707 decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
708 funs.insert(GetDuration);funs.insert(lcl_Getcoupnum);
709 funs.insert(GetYearFrac);funs.insert(DaysToDate);
710 funs.insert(GetNullDate);funs.insert(DateToDays);
711 funs.insert(DaysInMonth);funs.insert(IsLeapYear);
714 void OpDuration_ADD::GenSlidingWindowFunction(std::stringstream& ss,
715 const std::string sSymName, SubArguments& vSubArguments)
717 ss << "\ndouble " << sSymName;
718 ss << "_"<< BinFuncName() <<"(";
719 for (unsigned i = 0; i < vSubArguments.size(); i++)
721 if (i)
722 ss << ",";
723 vSubArguments[i]->GenSlidingWindowDecl(ss);
725 ss << ") {\n";
726 ss << " double tmp = " << GetBottom() << ";\n";
727 ss << " int gid0 = get_global_id(0);\n";
728 ss << " double arg0 = " << GetBottom() << ";\n";
729 ss << " double arg1 = " << GetBottom() << ";\n";
730 ss << " double arg2 = " << GetBottom() << ";\n";
731 ss << " double arg3 = " << GetBottom() << ";\n";
732 ss << " double arg4 = " << GetBottom() << ";\n";
733 ss << " double arg5 = " << GetBottom() << ";\n";
734 unsigned j = vSubArguments.size();
735 while (j--)
737 FormulaToken* pCur = vSubArguments[j]->GetFormulaToken();
738 assert(pCur);
739 if(pCur->GetType() == formula::svSingleVectorRef)
741 #ifdef ISNAN
742 const formula::SingleVectorRefToken* pSVR =
743 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
744 ss << " if(gid0 >= " << pSVR->GetArrayLength() << " || isNan(";
745 ss << vSubArguments[j]->GenSlidingWindowDeclRef();
746 ss << "))\n";
747 ss << " arg" << j << " = " <<GetBottom() << ";\n";
748 ss << " else\n";
749 #endif
750 ss << " arg" << j << " = ";
751 ss << vSubArguments[j]->GenSlidingWindowDeclRef();
752 ss << ";\n";
755 ss << " int nNullDate = GetNullDate();\n";
756 ss << " tmp = GetDuration( nNullDate, (int)arg0, (int)arg1, arg2,";
757 ss << " arg3, (int)arg4, (int)arg5);\n";
758 ss << " return tmp;\n";
759 ss << "}";
761 void OpMDuration::BinInlineFun(std::set<std::string>& decls,
762 std::set<std::string>& funs)
764 decls.insert(GetDuration_newDecl);decls.insert(lcl_Getcoupnum_newDecl);
765 decls.insert(addMonthsDecl);decls.insert(checklessthanDecl);
766 decls.insert(setDayDecl);decls.insert(ScaDateDecl);
767 decls.insert(GetYearFracDecl);decls.insert(DaysToDateDecl);
768 decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
769 funs.insert(GetDuration_new);funs.insert(lcl_Getcoupnum_new);
770 funs.insert(addMonths);funs.insert(checklessthan);
771 funs.insert(setDay);funs.insert(ScaDate);
772 funs.insert(GetYearFrac);funs.insert(DaysToDate);
773 funs.insert(DaysInMonth);funs.insert(IsLeapYear);
776 void OpMDuration::GenSlidingWindowFunction(std::stringstream& ss,
777 const std::string sSymName, SubArguments& vSubArguments)
779 ss << "\ndouble " << sSymName;
780 ss << "_"<< BinFuncName() <<"(";
781 for (unsigned i = 0; i < vSubArguments.size(); i++)
783 if (i)
784 ss << ",";
785 vSubArguments[i]->GenSlidingWindowDecl(ss);
787 ss << ") {\n";
788 ss << " double tmp = " << GetBottom() << ";\n";
789 ss << " int gid0 = get_global_id(0);\n";
790 ss << " double arg0 = " << GetBottom() << ";\n";
791 ss << " double arg1 = " << GetBottom() << ";\n";
792 ss << " double arg2 = " << GetBottom() << ";\n";
793 ss << " double arg3 = " << GetBottom() << ";\n";
794 ss << " double arg4 = " << GetBottom() << ";\n";
795 ss << " double arg5 = " << GetBottom() << ";\n";
796 for (unsigned i = 0; i < vSubArguments.size(); i++)
798 FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
799 assert(pCur);
800 if (pCur->GetType() == formula::svSingleVectorRef)
802 #ifdef ISNAN
803 const formula::SingleVectorRefToken* pSVR =
804 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
805 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
806 #endif
808 else if (pCur->GetType() == formula::svDouble)
810 #ifdef ISNAN
811 ss << " {\n";
812 #endif
814 #ifdef ISNAN
815 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
817 ss << " if (isNan(";
818 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
819 ss << "))\n";
820 ss << " arg" << i << " = 0;\n";
821 ss << " else\n";
822 ss << " arg" << i << " = ";
823 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
824 ss << " }\n";
826 else
828 ss << " arg" << i << " = ";
829 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
831 #else
832 ss << " arg" << i;
833 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
834 #endif
836 ss << " int nNullDate = 693594;\n";
837 ss << " tmp = GetDuration_new( nNullDate, (int)arg0, (int)arg1, arg2,";
838 ss << " arg3, (int)arg4, (int)arg5);\n";
839 ss << " tmp = tmp * pow(1.0 + arg3 * pow((int)arg4, -1.0), -1);\n";
840 ss << " return tmp;\n";
841 ss << "}";
843 void Fvschedule::GenSlidingWindowFunction(
844 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
846 FormulaToken* pCur = vSubArguments[1]->GetFormulaToken();
847 assert(pCur);
848 const formula::DoubleVectorRefToken* pCurDVR =
849 dynamic_cast<const formula::DoubleVectorRefToken *>(pCur);
850 size_t nCurWindowSize = pCurDVR->GetRefRowSize();
851 ss << "\ndouble " << sSymName;
852 ss << "_"<< BinFuncName() <<"(";
853 for (unsigned i = 0; i < vSubArguments.size(); i++)
855 if (i)
856 ss << ",";
857 vSubArguments[i]->GenSlidingWindowDecl(ss);
859 ss << ") {\n\t";
860 ss << "double tmp = 1.0;\n\t";
861 ss << "int gid0 = get_global_id(0);\n\t";
862 ss << "double arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
863 ss << ";\n\t";
864 #ifdef ISNAN
865 ss << "if (isNan(arg0))\n\t\t";
866 ss << "arg0 = 0;\n\t";
867 #endif
868 ss << "double arg1;\n\t";
869 ss << "int arrayLength = " << pCurDVR->GetArrayLength() << ";\n\t";
870 #ifdef ISNAN
871 ss << "for (int i = 0; i + gid0 < arrayLength &&";
872 ss << " i < " << nCurWindowSize << "; i++){\n\t\t";
873 #else
874 ss << "for (int i = 0; i < " << nCurWindowSize << "; i++){\n\t\t";
875 #endif
876 ss << "arg1 = ";
877 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n\t\t\t";
878 #ifdef ISNAN
879 ss << "if (isNan(arg1))\n\t\t\t\t";
880 ss << "arg1 = 0;\n\t\t\t";
881 #endif
882 ss << "tmp *= arg1 + 1.0;\n\t\t";
883 ss << "}\n\t";
884 ss << "return (double)tmp * arg0";
885 ss << ";\n}";
887 void Cumipmt::BinInlineFun(std::set<std::string>& decls,
888 std::set<std::string>& funs)
890 decls.insert(GetRmz_newDecl); decls.insert(GetZw_newDecl);
891 funs.insert(GetRmz_new);funs.insert(GetZw_new);
893 void Cumipmt::GenSlidingWindowFunction(
894 std::stringstream &ss, const std::string sSymName, SubArguments &
895 vSubArguments)
897 ss << "\ndouble " << sSymName;
898 ss << "_"<< BinFuncName() <<"(";
899 for (unsigned i = 0; i < vSubArguments.size(); i++)
901 if (i)
902 ss << ",";
903 vSubArguments[i]->GenSlidingWindowDecl(ss);
905 ss << ") {\n";
906 ss << " int gid0 = get_global_id(0);\n";
907 ss << " double fRate,fVal;\n";
908 ss << " int nStartPer,nEndPer,nNumPeriods,nPayType;\n";
909 #ifdef ISNAN
910 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
911 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
912 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
913 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
914 FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
915 FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
916 #endif
917 #ifdef ISNAN
918 if(tmpCur0->GetType() == formula::svSingleVectorRef)
920 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
921 formula::SingleVectorRefToken *>(tmpCur0);
922 ss <<" if(gid0 >= "<<tmpCurDVR0->GetArrayLength()<<" || isNan(";
923 ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
924 ss <<"))\n";
925 ss <<" fRate = 0;\n else\n";
927 #endif
928 ss <<" fRate = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
929 ss <<";\n";
930 #ifdef ISNAN
931 if(tmpCur1->GetType() == formula::svSingleVectorRef)
933 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
934 formula::SingleVectorRefToken *>(tmpCur1);
935 ss <<" if(gid0 >= "<<tmpCurDVR1->GetArrayLength()<<" || isNan(";
936 ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
937 ss <<"))\n";
938 ss <<" nNumPeriods = 0;\n else\n";
940 #endif
941 ss <<" nNumPeriods = (int)";
942 ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
943 ss <<";\n";
944 #ifdef ISNAN
945 if(tmpCur2->GetType() == formula::svSingleVectorRef)
947 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
948 formula::SingleVectorRefToken *>(tmpCur2);
949 ss <<" if(gid0 >= "<<tmpCurDVR2->GetArrayLength()<<" || isNan(";
950 ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
951 ss <<"))\n";
952 ss <<" fVal = 0;\n else\n";
954 #endif
955 ss <<" fVal = "<<vSubArguments[2]->GenSlidingWindowDeclRef();
956 ss <<";\n";
957 #ifdef ISNAN
958 if(tmpCur3->GetType() == formula::svSingleVectorRef)
960 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
961 formula::SingleVectorRefToken *>(tmpCur3);
962 ss <<" if(gid0 >= "<<tmpCurDVR3->GetArrayLength()<<" || isNan(";
963 ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
964 ss <<"))\n";
965 ss <<" nStartPer = 0;\n else\n";
967 #endif
968 ss <<" nStartPer = (int)";
969 ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
970 ss <<";\n";
971 #ifdef ISNAN
972 if(tmpCur4->GetType() == formula::svSingleVectorRef)
974 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
975 formula::SingleVectorRefToken *>(tmpCur4);
976 ss <<" if(gid0 >= "<<tmpCurDVR4->GetArrayLength()<<" || isNan(";
977 ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
978 ss <<"))\n";
979 ss <<" nEndPer = 0;\n else\n";
981 #endif
982 ss <<" nEndPer = (int)";
983 ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
984 ss <<";\n";
986 #ifdef ISNAN
987 if(tmpCur5->GetType() == formula::svSingleVectorRef)
989 const formula::SingleVectorRefToken*tmpCurDVR5= dynamic_cast<const
990 formula::SingleVectorRefToken *>(tmpCur5);
991 ss <<" if(gid0 >= "<<tmpCurDVR5->GetArrayLength()<<" || isNan(";
992 ss <<vSubArguments[5]->GenSlidingWindowDeclRef();
993 ss <<"))\n";
994 ss <<" nPayType = 0;\n else\n";
996 #endif
997 ss <<" nPayType = (int)"<<vSubArguments[5]->GenSlidingWindowDeclRef();
998 ss <<";\n";
999 ss <<" double fRmz;\n";
1000 ss <<" fRmz = GetRmz_new( fRate, nNumPeriods, fVal, 0.0, nPayType );\n";
1001 ss <<" double tmp = 0.0;\n";
1002 ss <<" if( nStartPer == 1 )\n";
1003 ss <<" {\n";
1004 ss <<" if( nPayType <= 0 )\n";
1005 ss <<" tmp = -fVal;\n";
1006 ss <<" nStartPer++;\n";
1007 ss <<" }\n";
1008 ss <<" for( ; nStartPer<= nEndPer ; nStartPer++ )\n";
1009 ss <<" {\n";
1010 ss <<" if( nPayType > 0 )\n";
1011 ss <<" tmp += GetZw_new( fRate, nStartPer - 2 , ";
1012 ss <<"fRmz, fVal, 1 ) - fRmz;\n";
1013 ss <<" else\n";
1014 ss <<" tmp += GetZw_new( fRate, nStartPer - 1 , ";
1015 ss <<"fRmz, fVal, 0 );\n";
1016 ss <<" }\n";
1017 ss <<" tmp *= fRate;\n";
1018 ss <<" return tmp;\n";
1019 ss <<"}";
1022 void IRR::GenSlidingWindowFunction(std::stringstream &ss,
1023 const std::string sSymName, SubArguments &vSubArguments)
1025 ss << "\ndouble " << sSymName;
1026 ss << "_" << BinFuncName() << "(";
1027 for (unsigned i = 0; i < vSubArguments.size(); i++)
1029 if (i)
1030 ss << ",";
1031 vSubArguments[i]->GenSlidingWindowDecl(ss);
1033 ss << ") {\n";
1034 ss << " #define Epsilon 1.0E-7\n";
1035 ss << " int gid0 = get_global_id(0);\n";
1036 FormulaToken* pSur = vSubArguments[1]->GetFormulaToken();
1037 assert(pSur);
1038 ss << " double fSchaetzwert = ";
1039 ss << vSubArguments[1]->GenSlidingWindowDeclRef() << ";\n";
1040 ss << " double fEps = 1.0;\n";
1041 ss << " double x = 0.0, xNeu = 0.0, fZaehler = 0.0, fNenner = 0.0;\n";
1042 ss << " double nCount = 0.0;\n";
1043 if (pSur->GetType() == formula::svSingleVectorRef)
1045 #ifdef ISNAN
1046 const formula::SingleVectorRefToken* pSVR =
1047 dynamic_cast< const formula::SingleVectorRefToken* >(pSur);
1048 ss << " if (gid0 >= " << pSVR->GetArrayLength() << ")\n";
1049 ss << " fSchaetzwert = 0.1;\n";
1050 ss << " if (isNan(fSchaetzwert))\n";
1051 ss << " x = 0.1;\n";
1052 ss << " else\n";
1053 #endif
1055 else if (pSur->GetType() == formula::svDouble)
1057 #ifdef ISNAN
1058 ss << " if (isNan(fSchaetzwert))\n";
1059 ss << " x = 0.1;\n";
1060 ss << " else\n";
1061 #endif
1063 ss << " x = fSchaetzwert;\n";
1064 ss << " unsigned short nItCount = 0;\n";
1065 ss << " while (fEps > Epsilon && nItCount < 20){\n";
1066 ss << " nCount = 0.0; fZaehler = 0.0; fNenner = 0.0;\n";
1067 ss << " double arg0, arg1;\n";
1068 ss << " int i = 0;\n";
1069 FormulaToken* pCur = vSubArguments[0]->GetFormulaToken();
1070 assert(pCur);
1071 const formula::DoubleVectorRefToken* pDVR =
1072 dynamic_cast<const formula::DoubleVectorRefToken* >(pCur);
1073 size_t nCurWindowSize = pDVR->GetRefRowSize();
1074 ss << " for ( ";
1075 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
1076 #ifdef ISNAN
1077 ss << "i = gid0; i < " << pDVR->GetArrayLength();
1078 ss << " && i < " << nCurWindowSize << " /2*2; i++){\n";
1079 #else
1080 ss << "i = gid0; i < " << nCurWindowSize << " /2*2; i++)\n";
1081 #endif
1082 ss << " arg0 = ";
1083 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1084 ss << " i++;" << ";\n";
1085 ss << " arg1 = ";
1086 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1087 #ifdef ISNAN
1088 ss << " if (!isNan(arg0)){\n";
1089 #endif
1090 ss << " fZaehler += arg0 / pow(1.0 + x, nCount);\n";
1091 ss << " fNenner+=-1*nCount*arg0/pow(1.0+x,nCount+1.0);\n";
1092 ss << " nCount += 1;\n";
1093 ss << " }\n";
1094 #ifdef ISNAN
1095 ss << " if (!isNan(arg1)){\n";
1096 #endif
1097 ss << " fZaehler += arg1 / pow(1.0 + x, nCount);\n";
1098 ss << " fNenner+=-1*nCount*arg1/pow(1.0+x,nCount+1.0);\n";
1099 ss << " nCount += 1;\n";
1100 ss << " }\n";
1101 #ifdef ISNAN
1102 ss << " }\n";
1103 ss << "if(i < " << pDVR->GetArrayLength();
1104 ss << " && i < " << nCurWindowSize << ") ;{\n";
1105 #else
1106 ss << " i < " << nCurWindowSize << "){\n";
1107 #endif
1109 else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
1110 #ifdef ISNAN
1111 ss << "; i < " << pDVR->GetArrayLength();
1112 ss << " && i < (gid0+" << nCurWindowSize << " )/2*2; i++){\n";
1113 #else
1114 ss << "; i < gid0+" << nCurWindowSize << " /2*2; i++)\n";
1115 #endif
1116 ss << " arg0 = ";
1117 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1118 #ifdef ISNAN
1119 ss << " if (!isNan(arg0)){\n";
1120 #endif
1121 ss << " fZaehler += arg0 / pow(1.0 + x, nCount);\n";
1122 ss << " fNenner+=-1*nCount*arg0/pow(1.0+x,nCount+1.0);\n";
1123 ss << " nCount += 1;\n";
1124 #ifdef ISNAN
1125 ss << " }\n";
1126 #endif
1127 ss << " i++;\n";
1128 ss << " arg1 = ";
1129 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1130 #ifdef ISNAN
1131 ss << " if (!isNan(arg1)){\n";
1132 #endif
1133 ss << " fZaehler += arg1 / pow(1.0 + x, nCount);\n";
1134 ss << " fNenner+=-1*nCount*arg1/pow(1.0+x,nCount+1.0);\n";
1135 ss << " nCount+=1;\n";
1136 ss << " }\n";
1137 #ifdef ISNAN
1138 ss << " }\n";
1139 ss << " if(i < " << pDVR->GetArrayLength();
1140 ss << " && i < gid0+" << nCurWindowSize << "){\n";
1141 #else
1142 ss << " i < " << nCurWindowSize << "){\n";
1143 #endif
1145 else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
1146 #ifdef ISNAN
1147 ss << " ; i + gid0 < " << pDVR->GetArrayLength();
1148 ss << " && i < " << nCurWindowSize << " /2*2; i++){\n";
1149 #else
1150 ss << "; i < " << nCurWindowSize << " /2*2; i++)\n";
1151 #endif
1152 ss << " arg0 = ";
1153 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1154 ss << " i++;" << ";\n";
1155 ss << " arg1 = ";
1156 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1157 #ifdef ISNAN
1158 ss << " if (!isNan(arg0)){\n";
1159 #endif
1160 ss << " fZaehler += arg0 / pow(1.0 + x, nCount);\n";
1161 ss << " fNenner+=-1*nCount*arg0/pow(1.0+x,nCount+1.0);\n";
1162 ss << " nCount += 1;\n";
1163 ss << " }\n";
1164 #ifdef ISNAN
1165 ss << " if (!isNan(arg1)){\n";
1166 #endif
1167 ss << " fZaehler += arg1 / pow(1.0 + x, nCount);\n";
1168 ss << " fNenner+=-1*nCount*arg1/pow(1.0+x,nCount+1.0);\n";
1169 ss << " nCount+=1;\n";
1170 ss << " }\n";
1171 #ifdef ISNAN
1172 ss << " }\n";
1173 ss << " if(i + gid0 < " << pDVR->GetArrayLength() << " &&";
1174 ss << " i < " << nCurWindowSize << "){\n";
1175 #else
1176 ss << " i < " << nCurWindowSize << "){\n";
1177 #endif
1179 } else {
1180 #ifdef ISNAN
1181 ss << "; i < " << nCurWindowSize << " /2*2; i++){\n";
1182 #else
1183 ss << "; i < " << nCurWindowSize << " /2*2; i++)\n";
1184 #endif
1185 ss << " arg0 = ";
1186 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1187 ss << " i++;" << ";\n";
1188 ss << " arg1 = ";
1189 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1190 #ifdef ISNAN
1191 ss << " if (!isNan(arg0)){\n";
1192 #endif
1193 ss << " fZaehler += arg0 / pow(1.0 + x, nCount);\n";
1194 ss << " fNenner+=-1*nCount*arg0/pow(1.0+x,nCount+1.0);\n";
1195 ss << " nCount += 1;\n";
1196 ss << " }\n";
1197 #ifdef ISNAN
1198 ss << " if (!isNan(arg1)){\n";
1199 #endif
1200 ss << " fZaehler += arg1 / pow(1.0 + x, nCount);\n";
1201 ss << " fNenner+=-1*nCount*arg1/pow(1.0+x,nCount+1.0);\n";
1202 ss << " nCount+=1;\n";
1203 ss << " }\n";
1204 #ifdef ISNAN
1205 ss << " }\n";
1206 ss << "if(i<" << nCurWindowSize << "){\n";
1207 #else
1208 ss << " i < " << nCurWindowSize << "){\n";
1209 #endif
1212 ss << " arg0 = ";
1213 ss << vSubArguments[0]->GenSlidingWindowDeclRef() << ";\n";
1214 #ifdef ISNAN
1215 ss << " if (isNan(arg0))\n";
1216 ss << " continue;\n";
1217 #endif
1218 ss << " fZaehler += arg0 / pow(1.0+x, nCount);\n";
1219 ss << " fNenner += -nCount * arg0 / pow(1.0+x,nCount+1.0);\n";
1220 ss << " nCount+=1;\n";
1221 ss << " }\n";
1222 ss << " xNeu = x - fZaehler / fNenner;\n";
1223 ss << " fEps = fabs(xNeu - x);\n";
1224 ss << " x = xNeu;\n";
1225 ss << " nItCount++;\n }\n";
1226 ss << " if (fSchaetzwert == 0.0 && fabs(x) < Epsilon)\n";
1227 ss << " x = 0.0;\n";
1228 ss << " if (fEps < Epsilon)\n";
1229 ss << " return x;\n";
1230 ss << " else\n";
1231 ss << " return (double)523;\n";
1232 ss << "}";
1235 void XNPV::GenSlidingWindowFunction(
1236 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
1238 FormulaToken *pCur = vSubArguments[1]->GetFormulaToken();
1239 assert(pCur);
1240 const formula::DoubleVectorRefToken* pCurDVR =
1241 dynamic_cast<const formula::DoubleVectorRefToken *>(pCur);
1242 size_t nCurWindowSize = pCurDVR->GetRefRowSize();
1243 ss << "\ndouble " << sSymName;
1244 ss << "_"<< BinFuncName() <<"( ";
1245 for (unsigned i = 0; i < vSubArguments.size(); i++)
1247 if (i)
1248 ss << ",";
1249 vSubArguments[i]->GenSlidingWindowDecl(ss);
1252 ss << ") {\n\t";
1253 ss << "double result = 0.0;\n\t";
1254 ss << "int gid0 = get_global_id(0);\n\t";
1255 ss << "int i=0;\n\t";
1256 if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
1257 ss<< "i=gid0;\n\t";
1259 ss << "double date;\n\t";
1260 ss << "double value;\n\t";
1261 ss << "double rate;\n\t";
1262 ss << "double dateNull;\n\t";
1263 #ifdef ISNAN
1264 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1265 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
1266 formula::SingleVectorRefToken *>(tmpCur0);
1268 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1269 const formula::DoubleVectorRefToken*tmpCurDVR1= dynamic_cast<const
1270 formula::DoubleVectorRefToken *>(tmpCur1);
1272 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1273 const formula::DoubleVectorRefToken*tmpCurDVR2= dynamic_cast<const
1274 formula::DoubleVectorRefToken *>(tmpCur2);
1275 ss<< "int buffer_rate_len = ";
1276 ss<< tmpCurDVR0->GetArrayLength();
1277 ss << ";\n\t";
1278 ss<< "int buffer_value_len = ";
1279 ss<< tmpCurDVR1->GetArrayLength();
1280 ss << ";\n\t";
1281 ss<< "int buffer_date_len = ";
1282 ss<< tmpCurDVR2->GetArrayLength();
1283 ss << ";\n\t";
1284 #endif
1285 #ifdef ISNAN
1286 ss<<"if((i+gid0)>=buffer_date_len || isNan(";
1287 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1288 ss<<"))\n\t\t";
1289 ss<<"dateNull = 0;\n\telse \n\t\t";
1290 #endif
1291 ss<<"dateNull = ";
1292 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1293 ss<<";\n\t";
1294 #ifdef ISNAN
1295 ss<<"if((i+gid0)>=buffer_rate_len || isNan(";
1296 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1297 ss<<"))\n\t\t";
1298 ss<<"rate = 0;\n\telse \n\t\t";
1299 #endif
1300 ss<<"rate = ";
1301 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1302 ss<<";\n\t";
1303 ss << "for (int i = ";
1304 if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed())
1306 ss << "gid0; i < "<< nCurWindowSize <<"; i++)\n\t\t";
1308 else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
1310 ss << "0; i < gid0+"<< nCurWindowSize <<"; i++)\n\t\t";
1312 else
1314 ss << "0; i < "<< nCurWindowSize <<"; i++)\n\t\t";
1316 ss << "{\n\t";
1317 ss << "result += ";
1318 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1319 ss << "/(pow((";
1320 ss<<vSubArguments[0]->GenSlidingWindowDeclRef();
1321 ss <<"+1),(";
1322 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1323 ss << "-dateNull)/365));\n\t";
1324 ss <<"}\n\t";
1326 ss << "return result;\n";
1327 ss << "}";
1330 void PriceMat::BinInlineFun(std::set<std::string>& decls,
1331 std::set<std::string>& funs)
1333 decls.insert(GetYearFracDecl);decls.insert(GetNullDateDecl);
1334 decls.insert(DateToDaysDecl);decls.insert(DaysToDateDecl);
1335 decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
1337 funs.insert(GetYearFrac);funs.insert(GetNullDate);
1338 funs.insert(DateToDays);funs.insert(DaysToDate);
1339 funs.insert(DaysInMonth);funs.insert(IsLeapYear);
1341 void PriceMat::GenSlidingWindowFunction(
1342 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
1344 ss << "\ndouble " << sSymName;
1345 ss << "_"<< BinFuncName() <<"(";
1346 for (unsigned i = 0; i < vSubArguments.size(); i++)
1348 if (i)
1349 ss << ",";
1350 vSubArguments[i]->GenSlidingWindowDecl(ss);
1352 ss << ") {\n\t";
1353 ss << "int gid0 = get_global_id(0);\n\t";
1354 ss << "double result=0;\n\t";
1355 ss<< "int nNullDate = GetNullDate( );\n\t";
1356 ss <<"int settle;\n\t";
1357 ss <<"int mat;\n\t";
1358 ss <<"int issue;\n\t";
1359 ss <<"double rate;\n\t";
1360 ss <<"double yield;\n\t";
1361 ss <<"int nBase;\n\t";
1362 #ifdef ISNAN
1363 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1364 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
1365 formula::SingleVectorRefToken *>(tmpCur0);
1366 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1367 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
1368 formula::SingleVectorRefToken *>(tmpCur1);
1369 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1370 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
1371 formula::SingleVectorRefToken *>(tmpCur2);
1372 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
1373 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
1374 formula::SingleVectorRefToken *>(tmpCur3);
1375 FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
1376 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
1377 formula::SingleVectorRefToken *>(tmpCur4);
1378 FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
1379 const formula::SingleVectorRefToken*tmpCurDVR5= dynamic_cast<const
1380 formula::SingleVectorRefToken *>(tmpCur5);
1382 ss<< "int buffer_settle_len = ";
1383 ss<< tmpCurDVR0->GetArrayLength();
1384 ss << ";\n\t";
1385 ss<< "int buffer_mat_len = ";
1386 ss<< tmpCurDVR1->GetArrayLength();
1387 ss << ";\n\t";
1388 ss<< "int buffer_issue_len = ";
1389 ss<< tmpCurDVR2->GetArrayLength();
1390 ss << ";\n\t";
1391 ss<< "int buffer_rate_len = ";
1392 ss<< tmpCurDVR3->GetArrayLength();
1393 ss << ";\n\t";
1394 ss<< "int buffer_yield_len = ";
1395 ss<< tmpCurDVR4->GetArrayLength();
1396 ss << ";\n\t";
1397 ss<< "int buffer_base_len = ";
1398 ss<< tmpCurDVR5->GetArrayLength();
1399 ss << ";\n\t";
1400 #endif
1401 #ifdef ISNAN
1402 ss<<"if(gid0>=buffer_settle_len || isNan(";
1403 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1404 ss<<"))\n\t\t";
1405 ss<<"settle = 0;\n\telse \n\t\t";
1406 #endif
1407 ss<<"settle = ";
1408 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1409 ss<<";\n\t";
1410 #ifdef ISNAN
1411 ss<<"if(gid0>=buffer_mat_len || isNan(";
1412 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1413 ss<<"))\n\t\t";
1414 ss<<"mat = 0;\n\telse \n\t\t";
1415 #endif
1416 ss<<"mat = ";
1417 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1418 ss<<";\n\t";
1419 #ifdef ISNAN
1420 ss<<"if(gid0>=buffer_issue_len || isNan(";
1421 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1422 ss<<"))\n\t\t";
1423 ss<<"issue = 0;\n\telse \n\t\t";
1424 #endif
1425 ss<<"issue = ";
1426 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1427 ss<<";\n\t";
1428 #ifdef ISNAN
1429 ss<<"if(gid0>=buffer_rate_len || isNan(";
1430 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
1431 ss<<"))\n\t\t";
1432 ss<<"rate = 0;\n\telse \n\t\t";
1433 #endif
1434 ss<<"rate = ";
1435 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
1436 ss<<";\n\t";
1437 #ifdef ISNAN
1438 ss<<"if(gid0>=buffer_yield_len || isNan(";
1439 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
1440 ss<<"))\n\t\t";
1441 ss<<"yield = 0;\n\telse \n\t\t";
1442 #endif
1443 ss<<"yield = ";
1444 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
1445 ss<<";\n\t";
1446 #ifdef ISNAN
1447 ss<<"if(gid0>=buffer_base_len || isNan(";
1448 ss << vSubArguments[5]->GenSlidingWindowDeclRef();
1449 ss<<"))\n\t\t";
1450 ss<<"nBase = 0;\n\telse \n\t\t";
1451 #endif
1452 ss<<"nBase = ";
1453 ss << vSubArguments[5]->GenSlidingWindowDeclRef();
1454 ss<<";\n\t";
1455 ss<< "double fIssMat = GetYearFrac( nNullDate, issue, mat, nBase);\n";
1456 ss<<"double fIssSet = GetYearFrac( nNullDate, issue, settle,nBase);\n";
1457 ss<<"double fSetMat = GetYearFrac( nNullDate, settle, mat, nBase);\n";
1458 ss<<"result = 1.0 + fIssMat * rate;\n\t";
1459 ss<<"result /= 1.0 + fSetMat * yield;\n\t";
1460 ss<<"result -= fIssSet * rate;\n\t";
1461 ss<<"result*= 100.0;\n\t";
1462 ss<<"return result;\n\t";
1463 ss<<"}\n";
1466 void OpSYD::GenSlidingWindowFunction(std::stringstream &ss,
1467 const std::string sSymName, SubArguments &vSubArguments)
1469 ss << "\ndouble " << sSymName;
1470 ss << "_"<< BinFuncName() <<"(";
1471 for (unsigned i = 0; i < vSubArguments.size(); i++)
1473 if (i)
1474 ss << ",";
1475 vSubArguments[i]->GenSlidingWindowDecl(ss);
1477 ss << ") {\n";
1478 ss << " int gid0 = get_global_id(0);\n";
1479 ss << " double result=0;\n";
1480 ss << " double cost;\n";
1481 ss << " double salvage;\n";
1482 ss << " double life;\n";
1483 ss << " double period;\n";
1484 #ifdef ISNAN
1485 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1486 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
1487 formula::SingleVectorRefToken *>(tmpCur0);
1489 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1490 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
1491 formula::SingleVectorRefToken *>(tmpCur1);
1493 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1494 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
1495 formula::SingleVectorRefToken *>(tmpCur2);
1497 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
1498 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
1499 formula::SingleVectorRefToken *>(tmpCur3);
1501 ss << " int buffer_cost_len = ";
1502 ss << tmpCurDVR0->GetArrayLength();
1503 ss << ";\n";
1505 ss << " int buffer_salvage_len = ";
1506 ss << tmpCurDVR1->GetArrayLength();
1507 ss << ";\n";
1509 ss << " int buffer_life_len = ";
1510 ss << tmpCurDVR2->GetArrayLength();
1511 ss << ";\n";
1512 ss << " int buffer_period_len = ";
1513 ss << tmpCurDVR3->GetArrayLength();
1514 ss << ";\n";
1515 #endif
1517 #ifdef ISNAN
1518 ss <<" if(gid0>=buffer_cost_len || isNan(";
1519 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1520 ss <<"))\n";
1521 ss <<" cost = 0;\n\telse \n";
1522 #endif
1523 ss <<" cost = ";
1524 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1525 ss <<";\n";
1526 #ifdef ISNAN
1527 ss <<" if(gid0>=buffer_salvage_len || isNan(";
1528 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1529 ss <<"))\n";
1530 ss <<" salvage = 0;\n\telse \n";
1531 #endif
1532 ss <<" salvage = ";
1533 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1534 ss <<";\n";
1535 #ifdef ISNAN
1536 ss <<" if(gid0>=buffer_life_len || isNan(";
1537 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1538 ss <<"))\n";
1539 ss <<" life = 0;\n\telse \n";
1540 #endif
1541 ss <<" life = ";
1542 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1543 ss <<";\n";
1544 #ifdef ISNAN
1545 ss <<" if(gid0>=buffer_period_len || isNan(";
1546 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
1547 ss <<"))\n";
1548 ss <<" period = 0;\n\telse \n";
1549 #endif
1550 ss <<" period = ";
1551 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
1552 ss <<";\n";
1553 ss <<" double tmpvalue = ((life*(life+1))*pow(2.0,-1));\n";
1554 ss <<" result = ((cost-salvage)*(life-period+1)";
1555 ss << "*pow(tmpvalue,-1));\n";
1556 ss <<" return result;\n";
1557 ss <<"}\n";
1560 void MIRR::GenSlidingWindowFunction(
1561 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
1563 FormulaToken* pCur = vSubArguments[0]->GetFormulaToken();
1564 assert(pCur);
1565 const formula::DoubleVectorRefToken* pCurDVR =
1566 dynamic_cast<const formula::DoubleVectorRefToken *>(pCur);
1567 size_t nCurWindowSize = pCurDVR->GetRefRowSize();
1568 FormulaToken* pCur1 = vSubArguments[1]->GetFormulaToken();
1569 assert(pCur1);
1570 const formula::SingleVectorRefToken* pSVR1 =
1571 dynamic_cast< const formula::SingleVectorRefToken* >(pCur1);
1572 assert(pSVR1);
1573 FormulaToken* pCur2 = vSubArguments[2]->GetFormulaToken();
1574 assert(pCur2);
1575 const formula::SingleVectorRefToken* pSVR2 =
1576 dynamic_cast< const formula::SingleVectorRefToken* >(pCur2);
1577 assert(pSVR2);
1579 ss << "\ndouble " << sSymName;
1580 ss << "_"<< BinFuncName() <<"(";
1581 for (unsigned i = 0; i < vSubArguments.size(); i++)
1583 if (i)
1584 ss << ",";
1585 vSubArguments[i]->GenSlidingWindowDecl(ss);
1587 ss << ") {\n\t";
1588 ss << "double tmp = " << GetBottom() <<";\n\t";
1589 ss << "int gid0 = get_global_id(0);\n\t";
1590 ss << "double arg0, arg1, arg2;\n\t";
1591 ss << "arg1 = " << vSubArguments[1]->GenSlidingWindowDeclRef();
1592 ss << ";\n\t";
1593 ss << "arg2 = " << vSubArguments[2]->GenSlidingWindowDeclRef();
1594 ss << ";\n\t";
1595 ss << "int argLen1 = " << pSVR1->GetArrayLength() << ";\n\t";
1596 ss << "int argLen2 = " << pSVR2->GetArrayLength() << ";\n\t";
1597 #ifdef ISNAN
1598 ss << "if (gid0 >= argLen1)\n\t\t";
1599 ss << "arg1 = 0.0;\n\t";
1600 ss << "if (gid0 >= argLen2)\n\t\t";
1601 ss << "arg2 = 0.0;\n\t";
1602 ss << "if (isNan(arg1))\n\t\t";
1603 ss << "arg1 = 0.0;\n\t";
1604 ss << "if (isNan(arg2))\n\t\t";
1605 ss << "arg2 = 0.0;\n\t";
1606 #endif
1607 ss << "double invest = arg1 + 1.0;\n\t";
1608 ss << "double reinvest = arg2 + 1.0;\n\t";
1609 ss << "double NPV_invest = 0.0;\n\t";
1610 ss << "double Pow_invest = 1.0;\n\t";
1611 ss << "double NPV_reinvest = 0.0;\n\t";
1612 ss << "double Pow_reinvest = 1.0;\n\t";
1613 ss << "int nCount = 0;\n\t";
1614 ss << "int arrayLength = " << pCurDVR->GetArrayLength() << ";\n\t";
1615 #ifdef ISNAN
1616 ss << "for (int i = 0; i + gid0 < arrayLength &&";
1617 ss << " i < " << nCurWindowSize << "; i++){\n\t\t";
1618 #else
1619 ss << "for (int i = 0; i < " << nCurWindowSize << "; i++){\n\t\t";
1620 #endif
1621 ss << "arg0 = " << vSubArguments[0]->GenSlidingWindowDeclRef();
1622 ss << ";\n\t\t";
1623 #ifdef ISNAN
1624 ss << "if (isNan(arg0))\n\t\t\t";
1625 ss << "continue;\n\t\t";
1626 #endif
1627 ss << "if (arg0 > 0.0)\n\t\t\t";
1628 ss << "NPV_reinvest += arg0 * Pow_reinvest;\n\t\t";
1629 ss << "else if (arg0 < 0.0)\n\t\t\t";
1630 ss << "NPV_invest += arg0 * Pow_invest;\n\t\t";
1631 ss << "Pow_reinvest /= reinvest;\n\t\t";
1632 ss << "Pow_invest /= invest;\n\t\t";
1633 ss << "nCount++;\n\t";
1634 ss << "}\n\t";
1635 ss << "tmp = ";
1636 ss << "-NPV_reinvest /NPV_invest * pow(reinvest,(double)nCount-1);\n\t";
1637 ss << "tmp = pow(tmp, 1.0 / (nCount - 1)) - 1.0;\n\t";
1638 ss << "return (double)tmp;\n";
1639 ss << "}";
1642 void OpEffective::GenSlidingWindowFunction(std::stringstream& ss,
1643 const std::string sSymName, SubArguments& vSubArguments)
1645 ss << "\ndouble " << sSymName;
1646 ss << "_" << BinFuncName() << "(";
1647 for (unsigned i = 0; i < vSubArguments.size(); i++)
1649 if (i)
1650 ss << ",";
1651 vSubArguments[i]->GenSlidingWindowDecl(ss);
1653 ss << ") {\n";
1654 ss << " double tmp = " << GetBottom() <<";\n";
1655 ss << " int gid0 = get_global_id(0);\n\t";
1656 ss << " double arg0 = " << GetBottom() << ";\n";
1657 ss << " double arg1 = " << GetBottom() << ";\n";
1658 for (unsigned i = 0; i < vSubArguments.size(); i++)
1660 FormulaToken* pCur = vSubArguments[i]->GetFormulaToken();
1661 assert(pCur);
1662 if (pCur->GetType() == formula::svSingleVectorRef)
1664 #ifdef ISNAN
1665 const formula::SingleVectorRefToken* pSVR =
1666 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
1667 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
1668 #endif
1670 else if (pCur->GetType() == formula::svDouble)
1672 #ifdef ISNAN
1673 ss << " {\n";
1674 #endif
1676 #ifdef ISNAN
1677 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
1679 ss << " if (isNan(";
1680 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
1681 ss << "))\n";
1682 ss << " arg" << i << " = 0;\n";
1683 ss << " else\n";
1684 ss << " arg" << i << " = ";
1685 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1686 ss << " }\n";
1688 else
1690 ss << " arg" << i << " = ";
1691 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1693 #else
1694 ss << " arg" << i;
1695 ss << vSubArguments[i]->GenSlidingWindowDeclRef() << ";\n";
1696 #endif
1698 ss << " tmp = pow(1.0 + arg0 * pow(arg1, -1), arg1)-1.0;\n";
1699 ss << " return tmp;\n";
1700 ss << "}";
1703 void OpTbilleq::BinInlineFun(std::set<std::string>& decls,
1704 std::set<std::string>& funs)
1706 decls.insert(GetDiffDate360_Decl);decls.insert(GetDiffDate360Decl);
1707 decls.insert(DateToDaysDecl);decls.insert(DaysToDate_LocalBarrierDecl);
1708 decls.insert(DaysInMonthDecl);decls.insert(GetNullDateDecl);
1709 decls.insert(IsLeapYearDecl);
1710 funs.insert(GetDiffDate360_);funs.insert(GetDiffDate360);
1711 funs.insert(DateToDays);funs.insert(DaysToDate_LocalBarrier);
1712 funs.insert(DaysInMonth);funs.insert(GetNullDate);
1713 funs.insert(IsLeapYear);
1715 void OpTbilleq::GenSlidingWindowFunction(
1716 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
1718 ss << "\ndouble " << sSymName;
1719 ss << "_"<< BinFuncName() <<"(";
1720 for (unsigned i = 0; i < vSubArguments.size(); i++)
1722 if (i)
1723 ss << ",";
1724 vSubArguments[i]->GenSlidingWindowDecl(ss);
1726 ss << ") {\n\t";
1727 ss << " int gid0 = get_global_id(0);\n";
1728 ss << "double tmp = 0;\n\t";
1729 ss << "double tmp000;\n\t";
1730 ss << "double tmp001;\n\t";
1731 ss << "double tmp002;\n\t";
1733 #ifdef ISNAN
1734 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1735 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
1736 formula::SingleVectorRefToken *>(tmpCur0);
1738 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1739 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
1740 formula::SingleVectorRefToken *>(tmpCur1);
1742 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1743 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
1744 formula::SingleVectorRefToken *>(tmpCur2);
1746 ss<< "int buffer_tmp000_len = ";
1747 ss<< tmpCurDVR0->GetArrayLength();
1748 ss << ";\n\t";
1750 ss<< "int buffer_tmp001_len = ";
1751 ss<< tmpCurDVR1->GetArrayLength();
1752 ss << ";\n\t";
1754 ss<< "int buffer_tmp002_len = ";
1755 ss<< tmpCurDVR2->GetArrayLength();
1756 ss << ";\n\t";
1757 #endif
1759 #ifdef ISNAN
1760 ss<<"if(gid0>=buffer_tmp000_len || isNan(";
1761 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1762 ss<<"))\n\t\t";
1763 ss<<"tmp000 = 0;\n\telse \n\t\t";
1764 #endif
1765 ss<<"tmp000 = ";
1766 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
1767 ss<<";\n\t";
1769 #ifdef ISNAN
1770 ss<<"if(gid0>=buffer_tmp001_len || isNan(";
1771 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1772 ss<<"))\n\t\t";
1773 ss<<"tmp001 = 0;\n\telse \n\t\t";
1774 #endif
1775 ss<<"tmp001 = ";
1776 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
1777 ss<<";\n\t";
1779 #ifdef ISNAN
1780 ss<<"if(gid0>=buffer_tmp002_len || isNan(";
1781 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1782 ss<<"))\n\t\t";
1783 ss<<"tmp002 = 0;\n\telse \n\t\t";
1784 #endif
1785 ss<<"tmp002 = ";
1786 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
1787 ss<<";\n\t";
1789 ss<<"tmp001+=1.0;\n";
1790 ss<<"int nDiff =GetDiffDate360(GetNullDate(),tmp000,tmp001,true);\n";
1791 ss<<"tmp =( 365 * tmp002 ) / ( 360 - ( tmp002 * ( nDiff ) ) );\n";
1792 ss << "return tmp;\n";
1793 ss << "}";
1795 void OpCumprinc::BinInlineFun(std::set<std::string>& decls,
1796 std::set<std::string>& funs)
1798 decls.insert(GetRmz_newDecl); decls.insert(GetZw_newDecl);
1799 funs.insert(GetRmz_new);funs.insert(GetZw_new);
1801 void OpCumprinc::GenSlidingWindowFunction(std::stringstream &ss,
1802 const std::string sSymName, SubArguments &vSubArguments)
1804 ss << "\ndouble " << sSymName;
1805 ss << "_"<< BinFuncName() <<"(";
1806 for (unsigned i = 0; i < vSubArguments.size(); i++)
1808 if (i)
1809 ss << ",";
1810 vSubArguments[i]->GenSlidingWindowDecl(ss);
1812 ss << ") {\n";
1813 ss << " double tmp = " << GetBottom() <<";\n";
1814 ss << " int gid0 = get_global_id(0);\n";
1815 ss << " double fRate,fVal;\n";
1816 ss << " int nStartPer,nEndPer,nNumPeriods,nPayType;\n";
1817 #ifdef ISNAN
1818 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
1819 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
1820 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
1821 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
1822 FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
1823 FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
1824 #endif
1825 #ifdef ISNAN
1826 if(tmpCur0->GetType() == formula::svSingleVectorRef)
1828 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
1829 formula::SingleVectorRefToken *>(tmpCur0);
1830 ss <<" if(gid0 >= "<<tmpCurDVR0->GetArrayLength()<<" || isNan(";
1831 ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
1832 ss <<"))\n";
1833 ss <<" fRate = 0;\n else\n";
1835 #endif
1836 ss <<" fRate = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
1837 ss <<";\n";
1838 #ifdef ISNAN
1839 if(tmpCur1->GetType() == formula::svSingleVectorRef)
1841 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
1842 formula::SingleVectorRefToken *>(tmpCur1);
1843 ss <<" if(gid0 >= "<<tmpCurDVR1->GetArrayLength()<<" || isNan(";
1844 ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
1845 ss <<"))\n";
1846 ss <<" nNumPeriods = 0;\n else\n";
1848 #endif
1849 ss <<" nNumPeriods = (int)";
1850 ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
1851 ss <<";\n";
1852 #ifdef ISNAN
1853 if(tmpCur2->GetType() == formula::svSingleVectorRef)
1855 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
1856 formula::SingleVectorRefToken *>(tmpCur2);
1857 ss <<" if(gid0 >= "<<tmpCurDVR2->GetArrayLength()<<" || isNan(";
1858 ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
1859 ss <<"))\n";
1860 ss <<" fVal = 0;\n else\n";
1862 #endif
1863 ss <<" fVal = "<<vSubArguments[2]->GenSlidingWindowDeclRef();
1864 ss <<";\n";
1865 #ifdef ISNAN
1866 if(tmpCur3->GetType() == formula::svSingleVectorRef)
1868 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
1869 formula::SingleVectorRefToken *>(tmpCur3);
1870 ss <<" if(gid0 >= "<<tmpCurDVR3->GetArrayLength()<<" || isNan(";
1871 ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
1872 ss <<"))\n";
1873 ss <<" nStartPer = 0;\n else\n";
1875 #endif
1876 ss <<" nStartPer = (int)";
1877 ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
1878 ss <<";\n";
1879 #ifdef ISNAN
1880 if(tmpCur4->GetType() == formula::svSingleVectorRef)
1882 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
1883 formula::SingleVectorRefToken *>(tmpCur4);
1884 ss <<" if(gid0 >= "<<tmpCurDVR4->GetArrayLength()<<" || isNan(";
1885 ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
1886 ss <<"))\n";
1887 ss <<" nEndPer = 0;\n else\n";
1889 #endif
1890 ss <<" nEndPer = (int)";
1891 ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
1892 ss <<";\n";
1894 #ifdef ISNAN
1895 if(tmpCur5->GetType() == formula::svSingleVectorRef)
1897 const formula::SingleVectorRefToken*tmpCurDVR5= dynamic_cast<const
1898 formula::SingleVectorRefToken *>(tmpCur5);
1899 ss <<" if(gid0 >= "<<tmpCurDVR5->GetArrayLength()<<" || isNan(";
1900 ss <<vSubArguments[5]->GenSlidingWindowDeclRef();
1901 ss <<"))\n";
1902 ss <<" nPayType = 0;\n else\n";
1904 #endif
1905 ss <<" nPayType = (int)";
1906 ss <<vSubArguments[5]->GenSlidingWindowDeclRef();
1907 ss <<";\n";
1908 ss <<" double fRmz;\n";
1909 ss <<" fRmz = GetRmz_new( fRate, nNumPeriods,fVal,0.0,nPayType );\n";
1910 ss <<" if(nStartPer == 1)\n";
1911 ss <<" {\n";
1912 ss <<" if( nPayType <= 0 )\n";
1913 ss <<" tmp = fRmz + fVal * fRate;\n";
1914 ss <<" else\n";
1915 ss <<" tmp = fRmz;\n";
1916 ss <<" nStartPer=nStartPer+1;\n";
1917 ss <<" }\n";
1918 ss <<" for( int i = nStartPer ; i <= nEndPer ; i++ )\n";
1919 ss <<" {\n";
1920 ss <<" if( nPayType > 0 )\n";
1921 ss <<" tmp += fRmz - ( GetZw_new( fRate,i - 2,";
1922 ss <<"fRmz,fVal,1)- fRmz ) * fRate;\n";
1923 ss <<" else\n";
1924 ss <<" tmp += fRmz - GetZw_new( fRate, i - 1,";
1925 ss <<"fRmz,fVal,0 ) * fRate;\n";
1926 ss <<" }\n";
1927 ss <<" return tmp;\n";
1928 ss <<"}";
1930 void OpAccrint::BinInlineFun(std::set<std::string>& decls,
1931 std::set<std::string>& funs)
1933 decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
1934 decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
1935 decls.insert(GetNullDateDecl); decls.insert(GetDiffDateDecl);
1936 funs.insert(IsLeapYear);funs.insert(DaysInMonth);
1937 funs.insert(DaysToDate);funs.insert(DateToDays);
1938 funs.insert(GetNullDate);funs.insert(GetDiffDate);
1940 void OpAccrint::GenSlidingWindowFunction(
1941 std::stringstream &ss, const std::string sSymName,
1942 SubArguments &vSubArguments)
1944 ss << "\ndouble " << sSymName;
1945 ss << "_"<< BinFuncName() <<"(";
1946 for (unsigned i = 0; i < vSubArguments.size(); i++)
1948 if (i)
1949 ss << ",";
1950 vSubArguments[i]->GenSlidingWindowDecl(ss);
1952 ss << ") {\n";
1953 ss << " int gid0 = get_global_id(0);\n";
1954 ss << " double tmp = 0;\n";
1955 ss << " int nStartDate,nEndDate,mode,freq;\n";
1956 ss << " int nDays1stYear=0;\n";
1957 ss << " double fVal,fRate;\n";
1958 #ifdef ISNAN
1959 FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
1960 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
1961 formula::SingleVectorRefToken *>(tmpCur0);
1962 FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
1963 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
1964 formula::SingleVectorRefToken *>(tmpCur2);
1965 FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
1966 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
1967 formula::SingleVectorRefToken *>(tmpCur3);
1968 FormulaToken* tmpCur4 = vSubArguments[4]->GetFormulaToken();
1969 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
1970 formula::SingleVectorRefToken *>(tmpCur4);
1971 FormulaToken* tmpCur5 = vSubArguments[5]->GetFormulaToken();
1972 const formula::SingleVectorRefToken*tmpCurDVR5= dynamic_cast<const
1973 formula::SingleVectorRefToken *>(tmpCur5);
1974 FormulaToken* tmpCur6 = vSubArguments[6]->GetFormulaToken();
1975 const formula::SingleVectorRefToken*tmpCurDVR6= dynamic_cast<const
1976 formula::SingleVectorRefToken *>(tmpCur6);
1977 ss<< " int buffer_nIssue_len = ";
1978 ss<< tmpCurDVR0->GetArrayLength();
1979 ss<< ";\n";
1980 ss<< " int buffer_nSettle_len = ";
1981 ss<< tmpCurDVR2->GetArrayLength();
1982 ss<< ";\n";
1983 ss<< " int buffer_fRate_len = ";
1984 ss<< tmpCurDVR3->GetArrayLength();
1985 ss<< ";\n";
1986 ss<< " int buffer_fVal_len = ";
1987 ss<< tmpCurDVR4->GetArrayLength();
1988 ss<< ";\n";
1989 ss<< " int buffer_nFreq_len = ";
1990 ss<< tmpCurDVR5->GetArrayLength();
1991 ss<< ";\n";
1992 ss<< " int buffer_nMode_len = ";
1993 ss<< tmpCurDVR6->GetArrayLength();
1994 ss << ";\n";
1995 #endif
1996 #ifdef ISNAN
1997 ss<<" if(gid0 >= buffer_nIssue_len || isNan(";
1998 ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
1999 ss <<"))\n";
2000 ss <<" nStartDate = 0;\n else\n";
2001 #endif
2002 ss <<" nStartDate=(int)";
2003 ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
2004 ss <<";\n";
2005 #ifdef ISNAN
2006 ss <<" if(gid0 >= buffer_nSettle_len || isNan(";
2007 ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
2008 ss <<"))\n";
2009 ss <<" nEndDate = 0;\n else\n";
2010 #endif
2011 ss <<" nEndDate=(int)";
2012 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2013 ss << ";\n";
2014 #ifdef ISNAN
2015 ss <<" if(gid0 >= buffer_fRate_len || isNan(";
2016 ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
2017 ss <<"))\n";
2018 ss <<" fRate = 0;\n else\n";
2019 #endif
2020 ss <<" fRate=";
2021 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
2022 ss <<";\n";
2023 #ifdef ISNAN
2024 ss <<" if(gid0 >= buffer_fVal_len || isNan(";
2025 ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
2026 ss <<"))\n";
2027 ss <<" fVal = 0;\n else\n";
2028 #endif
2029 ss <<" fVal=";
2030 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
2031 ss <<";\n";
2032 #ifdef ISNAN
2033 ss <<" if(gid0 >= buffer_nFreq_len || isNan(";
2034 ss <<vSubArguments[5]->GenSlidingWindowDeclRef();
2035 ss <<"))\n";
2036 ss <<" freq = 0;\n else\n";
2037 #endif
2038 ss <<" freq= (int)";
2039 ss << vSubArguments[5]->GenSlidingWindowDeclRef();
2040 ss <<";\n";
2041 #ifdef ISNAN
2042 ss <<" if(gid0 >= buffer_nMode_len || isNan(";
2043 ss <<vSubArguments[6]->GenSlidingWindowDeclRef();
2044 ss <<"))\n";
2045 ss <<" mode = 0;\n else\n";
2046 #endif
2047 ss <<" mode = (int)";
2048 ss << vSubArguments[6]->GenSlidingWindowDeclRef();
2049 ss <<";\n";
2050 ss <<" int nNullDate=GetNullDate();\n";
2051 ss <<" int nTotalDays = GetDiffDate(nNullDate,nStartDate,";
2052 ss <<"nEndDate, mode,&nDays1stYear);\n";
2053 ss <<" tmp = fVal*fRate*convert_double(nTotalDays)";
2054 ss <<"/convert_double(nDays1stYear);\n";
2055 ss <<" return tmp;\n";
2056 ss <<"}";
2059 void OpAccrintm::BinInlineFun(std::set<std::string>& decls,
2060 std::set<std::string>& funs)
2062 decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
2063 decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
2064 decls.insert(GetNullDateDecl); decls.insert(GetDiffDateDecl);
2065 funs.insert(IsLeapYear);funs.insert(DaysInMonth);
2066 funs.insert(DaysToDate);funs.insert(DateToDays);
2067 funs.insert(GetNullDate);funs.insert(GetDiffDate);
2069 void OpAccrintm::GenSlidingWindowFunction(
2070 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
2072 ss << "\ndouble " << sSymName;
2073 ss << "_"<< BinFuncName() <<"(";
2074 for (unsigned i = 0; i < vSubArguments.size(); i++)
2076 if (i)
2077 ss << ",";
2078 vSubArguments[i]->GenSlidingWindowDecl(ss);
2080 ss << ") {\n\t";
2081 ss << "int gid0 = get_global_id(0);\n\t";
2082 ss << "double tmp = " << GetBottom() <<";\n\t";
2083 ss << "int nStartDate,nEndDate,mode;\n\t";
2084 ss << "double fRate,fVal;\n\t";
2085 #ifdef ISNAN
2086 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
2087 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
2088 formula::SingleVectorRefToken *>(tmpCur0);
2089 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
2090 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
2091 formula::SingleVectorRefToken *>(tmpCur1);
2093 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
2094 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
2095 formula::SingleVectorRefToken *>(tmpCur2);
2097 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
2098 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
2099 formula::SingleVectorRefToken *>(tmpCur3);
2101 FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
2102 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
2103 formula::SingleVectorRefToken *>(tmpCur4);
2104 ss<< "int buffer_nIssue_len = ";
2105 ss<< tmpCurDVR0->GetArrayLength();
2106 ss << ";\n\t";
2108 ss<< "int buffer_nSettle_len = ";
2109 ss<< tmpCurDVR1->GetArrayLength();
2110 ss << ";\n\t";
2112 ss<< "int buffer_fRate_len = ";
2113 ss<< tmpCurDVR2->GetArrayLength();
2114 ss << ";\n\t";
2116 ss<< "int buffer_fVal_len = ";
2117 ss<< tmpCurDVR3->GetArrayLength();
2118 ss << ";\n\t";
2120 ss<< "int buffer_nMode_len = ";
2121 ss<< tmpCurDVR4->GetArrayLength();
2122 ss << ";\n\t";
2123 #endif
2124 #ifdef ISNAN
2125 ss <<"if(gid0 >= buffer_nIssue_len || isNan(";
2126 ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
2127 ss <<"))\n\t\t";
2128 ss <<"nStartDate = 0;\n\telse\n\t\t";
2129 #endif
2130 ss << "nStartDate=(int)";
2131 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2132 ss <<";\n\t";
2133 #ifdef ISNAN
2134 ss <<"if(gid0 >= buffer_nSettle_len || isNan(";
2135 ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
2136 ss <<"))\n\t\t";
2137 ss <<"nEndDate = 0;\n\telse\n\t\t";
2138 #endif
2139 ss << "nEndDate=(int)";
2140 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2141 ss << ";\n\t";
2143 #ifdef ISNAN
2144 ss <<"if(gid0 >= buffer_fRate_len || isNan(";
2145 ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
2146 ss <<"))\n\t\t";
2147 ss <<"fRate = 0;\n\telse\n\t\t";
2148 #endif
2149 ss << "fRate=";
2150 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2151 ss <<";\n\t";
2152 #ifdef ISNAN
2153 ss <<"if(gid0 >= buffer_fVal_len || isNan(";
2154 ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
2155 ss <<"))\n\t\t";
2156 ss <<"fVal = 0;\n\telse\n\t\t";
2157 #endif
2158 ss << "fVal=";
2159 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
2160 ss << ";\n\t";
2161 #ifdef ISNAN
2162 ss <<"if(gid0 >= buffer_nMode_len || isNan(";
2163 ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
2164 ss <<"))\n\t\t";
2165 ss <<"mode = 0;\n\telse\n\t\t";
2166 #endif
2167 ss << "mode = (int)";
2168 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
2169 ss << ";\n\t";
2170 ss <<"int nDays1stYear=0;\n\t";
2171 ss <<"int nNullDate=GetNullDate();\n\t";
2172 ss <<"int nTotalDays = GetDiffDate(nNullDate,nStartDate,";
2173 ss <<"nEndDate, mode,&nDays1stYear);\n\t";
2174 ss <<"tmp = fVal*fRate*convert_double(nTotalDays)";
2175 ss <<"/convert_double(nDays1stYear);\n\t";
2176 ss << "return tmp;\n";
2177 ss << "}";
2180 void OpYield::BinInlineFun(std::set<std::string>& decls,
2181 std::set<std::string>& funs)
2183 decls.insert(getYield_Decl);decls.insert(getPrice_Decl);
2184 decls.insert(coupnumDecl);decls.insert(coupdaysncDecl);
2185 decls.insert(coupdaybsDecl);decls.insert(coupdaysDecl);
2186 decls.insert(lcl_GetcoupnumDecl);decls.insert(lcl_GetcoupdaysDecl);
2187 decls.insert(lcl_GetcoupdaybsDecl);decls.insert(getDiffDecl);
2188 decls.insert(getDaysInYearRangeDecl);decls.insert(GetDaysInYearDecl);
2189 decls.insert(GetDaysInYearsDecl);decls.insert(getDaysInMonthRangeDecl);
2190 decls.insert(addMonthsDecl);decls.insert(ScaDateDecl);
2191 decls.insert(GetNullDateDecl);decls.insert(DateToDaysDecl);
2192 decls.insert(DaysToDateDecl);decls.insert(DaysInMonthDecl);
2193 decls.insert(IsLeapYearDecl);
2195 funs.insert(getYield_);funs.insert(getPrice_);
2196 funs.insert(coupnum);funs.insert(coupdaysnc);
2197 funs.insert(coupdaybs);funs.insert(coupdays);
2198 funs.insert(lcl_Getcoupnum);funs.insert(lcl_Getcoupdays);
2199 funs.insert(lcl_Getcoupdaybs);funs.insert(getDiff);
2200 funs.insert(getDaysInYearRange);funs.insert(GetDaysInYear);
2201 funs.insert(GetDaysInYears);funs.insert(getDaysInMonthRange);
2202 funs.insert(addMonths);funs.insert(ScaDate);
2203 funs.insert(GetNullDate);funs.insert(DateToDays);
2204 funs.insert(DaysToDate);funs.insert(DaysInMonth);
2205 funs.insert(IsLeapYear);
2209 void OpYield::GenSlidingWindowFunction(
2210 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
2212 ss << "\ndouble " << sSymName;
2213 ss << "_"<< BinFuncName() <<"(";
2214 for (unsigned i = 0; i < vSubArguments.size(); i++)
2216 if (i)
2217 ss << ",";
2218 vSubArguments[i]->GenSlidingWindowDecl(ss);
2220 ss << ") {\n\t";
2221 ss << "double tmp = " <<"0"<<";\n\t";
2222 ss << "int gid0 = get_global_id(0);\n\t";
2223 ss << "double tmp000;\n\t";
2224 ss << "double tmp001;\n\t";
2225 ss << "double tmp002;\n\t";
2226 ss << "double tmp003;\n\t";
2227 ss << "double tmp004;\n\t";
2228 ss << "double tmp005;\n\t";
2229 ss << "double tmp006;\n\t";
2232 #ifdef ISNAN
2233 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
2234 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
2235 formula::SingleVectorRefToken *>(tmpCur0);
2237 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
2238 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
2239 formula::SingleVectorRefToken *>(tmpCur1);
2241 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
2242 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
2243 formula::SingleVectorRefToken *>(tmpCur2);
2245 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
2246 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
2247 formula::SingleVectorRefToken *>(tmpCur3);
2249 FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
2250 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
2251 formula::SingleVectorRefToken *>(tmpCur4);
2253 FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
2254 const formula::SingleVectorRefToken*tmpCurDVR5= dynamic_cast<const
2255 formula::SingleVectorRefToken *>(tmpCur5);
2257 FormulaToken *tmpCur6 = vSubArguments[6]->GetFormulaToken();
2258 const formula::SingleVectorRefToken*tmpCurDVR6= dynamic_cast<const
2259 formula::SingleVectorRefToken *>(tmpCur6);
2261 ss<< "int buffer_tmp000_len = ";
2262 ss<< tmpCurDVR0->GetArrayLength();
2263 ss << ";\n\t";
2265 ss<< "int buffer_tmp001_len = ";
2266 ss<< tmpCurDVR1->GetArrayLength();
2267 ss << ";\n\t";
2269 ss<< "int buffer_tmp002_len = ";
2270 ss<< tmpCurDVR2->GetArrayLength();
2271 ss << ";\n\t";
2273 ss<< "int buffer_tmp003_len = ";
2274 ss<< tmpCurDVR3->GetArrayLength();
2275 ss << ";\n\t";
2277 ss<< "int buffer_tmp004_len = ";
2278 ss<< tmpCurDVR4->GetArrayLength();
2279 ss << ";\n\t";
2281 ss<< "int buffer_tmp005_len = ";
2282 ss<< tmpCurDVR5->GetArrayLength();
2283 ss << ";\n\t";
2285 ss<< "int buffer_tmp006_len = ";
2286 ss<< tmpCurDVR6->GetArrayLength();
2287 ss << ";\n\t";
2288 #endif
2290 #ifdef ISNAN
2291 ss<<"if(gid0>=buffer_tmp000_len || isNan(";
2292 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2293 ss<<"))\n\t\t";
2294 ss<<"tmp000 = 0;\n\telse \n\t\t";
2295 #endif
2296 ss<<"tmp000 = ";
2297 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2298 ss<<";\n\t";
2300 #ifdef ISNAN
2301 ss<<"if(gid0>=buffer_tmp001_len || isNan(";
2302 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2303 ss<<"))\n\t\t";
2304 ss<<"tmp001 = 0;\n\telse \n\t\t";
2305 #endif
2306 ss<<"tmp001 = ";
2307 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2308 ss<<";\n\t";
2310 #ifdef ISNAN
2311 ss<<"if(gid0>=buffer_tmp002_len || isNan(";
2312 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2313 ss<<"))\n\t\t";
2314 ss<<"tmp002 = 0;\n\telse \n\t\t";
2315 #endif
2316 ss<<"tmp002 = ";
2317 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2318 ss<<";\n\t";
2320 #ifdef ISNAN
2321 ss<<"if(gid0>=buffer_tmp003_len || isNan(";
2322 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
2323 ss<<"))\n\t\t";
2324 ss<<"tmp003 = 0;\n\telse \n\t\t";
2325 #endif
2326 ss<<"tmp003 = ";
2327 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
2328 ss<<";\n\t";
2330 #ifdef ISNAN
2331 ss<<"if(gid0>=buffer_tmp004_len || isNan(";
2332 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
2333 ss<<"))\n\t\t";
2334 ss<<"tmp004 = 0;\n\telse \n\t\t";
2335 #endif
2336 ss<<"tmp004 = ";
2337 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
2338 ss<<";\n\t";
2340 #ifdef ISNAN
2341 ss<<"if(gid0>=buffer_tmp005_len || isNan(";
2342 ss << vSubArguments[5]->GenSlidingWindowDeclRef();
2343 ss<<"))\n\t\t";
2344 ss<<"tmp005 = 0;\n\telse \n\t\t";
2345 #endif
2346 ss<<"tmp005 = ";
2347 ss << vSubArguments[5]->GenSlidingWindowDeclRef();
2348 ss<<";\n\t";
2350 #ifdef ISNAN
2351 ss<<"if(gid0>=buffer_tmp006_len || isNan(";
2352 ss << vSubArguments[6]->GenSlidingWindowDeclRef();
2353 ss<<"))\n\t\t";
2354 ss<<"tmp006 = 0;\n\telse \n\t\t";
2355 #endif
2356 ss<<"tmp006 = ";
2357 ss << vSubArguments[6]->GenSlidingWindowDeclRef();
2358 ss<<";\n\t";
2360 ss << "tmp = getYield_(";
2361 ss << "GetNullDate(),tmp000,tmp001,tmp002,tmp003,tmp004,tmp005,tmp006);\n\t ";
2362 ss << "return tmp;\n";
2363 ss << "}";
2366 void OpSLN::GenSlidingWindowFunction(std::stringstream &ss,
2367 const std::string sSymName, SubArguments &vSubArguments)
2369 ss << "\ndouble " << sSymName;
2370 ss << "_"<< BinFuncName() <<"(";
2371 for (unsigned i = 0; i < vSubArguments.size(); i++)
2373 if (i)
2374 ss << ",";
2375 vSubArguments[i]->GenSlidingWindowDecl(ss);
2377 ss << ") {\n";
2378 ss << " double tmp = " <<"0"<<";\n";
2379 ss << " int gid0 = get_global_id(0);\n";
2380 ss << " double wert;\n";
2381 ss << " double rest;\n";
2382 ss << " double dauer;\n";
2384 #ifdef ISNAN
2385 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
2386 const formula::SingleVectorRefToken*tmpCurDVR0=
2387 dynamic_cast<const formula::SingleVectorRefToken *>(tmpCur0);
2388 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
2389 const formula::SingleVectorRefToken*tmpCurDVR1=
2390 dynamic_cast<const formula::SingleVectorRefToken *>(tmpCur1);
2391 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
2392 const formula::SingleVectorRefToken*tmpCurDVR2=
2393 dynamic_cast<const formula::SingleVectorRefToken *>(tmpCur2);
2394 ss<< " int buffer_wert_len = ";
2395 ss<< tmpCurDVR0->GetArrayLength();
2396 ss << ";\n";
2397 ss<< " int buffer_rest_len = ";
2398 ss<< tmpCurDVR1->GetArrayLength();
2399 ss << ";\n";
2400 ss<< " int buffer_dauer_len = ";
2401 ss<< tmpCurDVR2->GetArrayLength();
2402 ss << ";\n";
2403 #endif
2404 #ifdef ISNAN
2405 ss<<" if(gid0>=buffer_wert_len || isNan(";
2406 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2407 ss<<"))\n";
2408 ss<<" wert = 0;\n\telse \n";
2409 #endif
2410 ss<<" wert = ";
2411 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2412 ss<<";\n";
2413 #ifdef ISNAN
2414 ss<<" if(gid0>=buffer_rest_len || isNan(";
2415 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2416 ss<<"))\n";
2417 ss<<" rest = 0;\n\telse \n";
2418 #endif
2419 ss<<" rest = ";
2420 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2421 ss<<";\n";
2422 #ifdef ISNAN
2423 ss<<" if(gid0>=buffer_dauer_len || isNan(";
2424 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2425 ss<<"))\n";
2426 ss<<" dauer = 0;\n\telse \n";
2427 #endif
2428 ss<<" dauer = ";
2429 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2430 ss<<";\n";
2431 ss << " tmp = (wert-rest)*pow(dauer,-1);\n";
2432 ss << " return tmp;\n";
2433 ss << "}";
2436 void OpYieldmat::BinInlineFun(std::set<std::string>& decls,
2437 std::set<std::string>& funs)
2439 decls.insert(GetYearFrac_newDecl);decls.insert(GetNullDateDecl);
2440 decls.insert(DateToDaysDecl);decls.insert(DaysToDate_newDecl);
2441 decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
2442 decls.insert(GetYieldmatDecl);
2444 funs.insert(GetYearFrac_new);funs.insert(GetNullDate);
2445 funs.insert(DateToDays);funs.insert(DaysToDate_new);
2446 funs.insert(DaysInMonth);funs.insert(IsLeapYear);
2447 funs.insert(GetYieldmat);
2450 void OpYieldmat::GenSlidingWindowFunction(
2451 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
2453 ss << "\ndouble " << sSymName;
2454 ss << "_"<< BinFuncName() <<"(";
2455 for (unsigned i = 0; i < vSubArguments.size(); i++)
2457 if (i)
2458 ss << ",";
2459 vSubArguments[i]->GenSlidingWindowDecl(ss);
2461 ss << ") {\n\t";
2462 ss << "double tmp = " <<"0"<<";\n\t";
2463 ss << "int gid0 = get_global_id(0);\n\t";
2464 ss << "double tmp000;\n\t";
2465 ss << "double tmp001;\n\t";
2466 ss << "double tmp002;\n\t";
2467 ss << "double tmp003;\n\t";
2468 ss << "double tmp004;\n\t";
2469 ss << "double tmp005;\n\t";
2471 #ifdef ISNAN
2472 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
2473 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
2474 formula::SingleVectorRefToken *>(tmpCur0);
2476 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
2477 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
2478 formula::SingleVectorRefToken *>(tmpCur1);
2480 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
2481 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
2482 formula::SingleVectorRefToken *>(tmpCur2);
2484 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
2485 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
2486 formula::SingleVectorRefToken *>(tmpCur3);
2488 FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
2489 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
2490 formula::SingleVectorRefToken *>(tmpCur4);
2492 FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
2493 const formula::SingleVectorRefToken*tmpCurDVR5= dynamic_cast<const
2494 formula::SingleVectorRefToken *>(tmpCur5);
2496 ss<< "int buffer_tmp000_len = ";
2497 ss<< tmpCurDVR0->GetArrayLength();
2498 ss << ";\n\t";
2500 ss<< "int buffer_tmp001_len = ";
2501 ss<< tmpCurDVR1->GetArrayLength();
2502 ss << ";\n\t";
2504 ss<< "int buffer_tmp002_len = ";
2505 ss<< tmpCurDVR2->GetArrayLength();
2506 ss << ";\n\t";
2508 ss<< "int buffer_tmp003_len = ";
2509 ss<< tmpCurDVR3->GetArrayLength();
2510 ss << ";\n\t";
2512 ss<< "int buffer_tmp004_len = ";
2513 ss<< tmpCurDVR4->GetArrayLength();
2514 ss << ";\n\t";
2516 ss<< "int buffer_tmp005_len = ";
2517 ss<< tmpCurDVR5->GetArrayLength();
2518 ss << ";\n\t";
2520 #endif
2522 #ifdef ISNAN
2523 ss<<"if(gid0>=buffer_tmp000_len || isNan(";
2524 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2525 ss<<"))\n\t\t";
2526 ss<<"tmp000 = 0;\n\telse \n\t\t";
2527 #endif
2528 ss<<"tmp000 = ";
2529 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
2530 ss<<";\n\t";
2532 #ifdef ISNAN
2533 ss<<"if(gid0>=buffer_tmp001_len || isNan(";
2534 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2535 ss<<"))\n\t\t";
2536 ss<<"tmp001 = 0;\n\telse \n\t\t";
2537 #endif
2538 ss<<"tmp001 = ";
2539 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
2540 ss<<";\n\t";
2542 #ifdef ISNAN
2543 ss<<"if(gid0>=buffer_tmp002_len || isNan(";
2544 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2545 ss<<"))\n\t\t";
2546 ss<<"tmp002 = 0;\n\telse \n\t\t";
2547 #endif
2548 ss<<"tmp002 = ";
2549 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
2550 ss<<";\n\t";
2552 #ifdef ISNAN
2553 ss<<"if(gid0>=buffer_tmp003_len || isNan(";
2554 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
2555 ss<<"))\n\t\t";
2556 ss<<"tmp003 = 0;\n\telse \n\t\t";
2557 #endif
2558 ss<<"tmp003 = ";
2559 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
2560 ss<<";\n\t";
2562 #ifdef ISNAN
2563 ss<<"if(gid0>=buffer_tmp004_len || isNan(";
2564 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
2565 ss<<"))\n\t\t";
2566 ss<<"tmp004 = 0;\n\telse \n\t\t";
2567 #endif
2568 ss<<"tmp004 = ";
2569 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
2570 ss<<";\n\t";
2572 #ifdef ISNAN
2573 ss<<"if(gid0>=buffer_tmp005_len || isNan(";
2574 ss << vSubArguments[5]->GenSlidingWindowDeclRef();
2575 ss<<"))\n\t\t";
2576 ss<<"tmp005 = 0;\n\telse \n\t\t";
2577 #endif
2578 ss<<"tmp005 = ";
2579 ss << vSubArguments[5]->GenSlidingWindowDeclRef();
2580 ss<<";\n\t";
2582 ss << "tmp = GetYieldmat(";
2583 ss<<"GetNullDate(),tmp000,tmp001,tmp002,tmp003,tmp004,tmp005);\n\t";
2584 ss << "return tmp;\n";
2585 ss << "}";
2588 void OpPMT::GenSlidingWindowFunction(std::stringstream &ss,
2589 const std::string sSymName, SubArguments &vSubArguments)
2591 ss << "\ndouble " << sSymName;
2592 ss << "_"<< BinFuncName() <<"(";
2593 for (unsigned i = 0; i < vSubArguments.size(); i++)
2595 if (i)
2596 ss << ", ";
2597 vSubArguments[i]->GenSlidingWindowDecl(ss);
2599 ss<<") {\n";
2600 ss<<" double tmp = 0;\n";
2601 ss<<" double temp=0.0;\n";
2602 ss<<" int gid0 = get_global_id(0);\n";
2603 ss<<" double tmp0=0,tmp1=0,tmp2=0;\n";
2604 ss<<" double tmp3=0,tmp4=0;\n";
2605 size_t i = vSubArguments.size();
2606 ss <<"\n ";
2607 //while (i-- > 1)
2608 for (i = 0; i < vSubArguments.size(); i++)
2610 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2611 assert(pCur);
2612 if (pCur->GetType() == formula::svSingleVectorRef)
2614 #ifdef ISNAN
2615 const formula::SingleVectorRefToken* pSVR =
2616 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
2617 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2618 #else
2619 #endif
2621 else if (pCur->GetType() == formula::svDouble)
2623 #ifdef ISNAN
2624 ss << "{\n";
2625 #endif
2627 else
2629 #ifdef ISNAN
2630 #endif
2632 #ifdef ISNAN
2633 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
2635 ss <<" temp="<<vSubArguments[i]->GenSlidingWindowDeclRef();
2636 ss <<";\n";
2637 ss <<" if (isNan(temp))\n";
2638 ss <<" tmp"<<i<<"= 0;\n";
2639 ss <<" else\n";
2640 ss <<" tmp"<<i<<"=temp;\n";
2641 ss <<" }\n";
2643 else
2645 ss <<" tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef(
2647 ss <<";\n";
2649 #else
2650 ss <<" tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
2651 ss <<";\n";
2653 #endif
2655 ss<<" if(tmp0==0.0)\n";
2656 ss<<" return -(tmp2+tmp3)/tmp1;\n";
2657 ss<<" tmp-=tmp3;\n";
2658 ss<<" tmp=tmp-tmp2*pow(1.0+tmp0,tmp1);\n";
2659 ss<<" tmp=tmp*pow(( (1.0+tmp0*tmp4)* ";
2660 ss<<"( (pow(1.0+tmp0,tmp1)-1.0)/tmp0)),-1);\n";
2661 ss<<" return tmp;\n";
2662 ss<<"}";
2665 void OpNPV::GenSlidingWindowFunction(std::stringstream &ss,
2666 const std::string sSymName, SubArguments &vSubArguments)
2668 ss << "\ndouble " << sSymName;
2669 ss << "_"<< BinFuncName() <<"(";
2670 for (unsigned i = 0; i < vSubArguments.size(); i++)
2672 if (i)
2673 ss << ", ";
2674 vSubArguments[i]->GenSlidingWindowDecl(ss);
2676 ss << ") {\n";
2677 ss << " double tmp = 0.0;\n";
2678 ss << " int gid0 = get_global_id(0);\n";
2679 ss << " int nCount = 1;\n";
2680 ss << " double arg0=";
2681 ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
2682 ss <<";\n";
2683 //while (i-- > 1)
2684 for (size_t i = 1; i < vSubArguments.size(); i++)
2686 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2687 assert(pCur);
2688 if (pCur->GetType() == formula::svDoubleVectorRef)
2690 const formula::DoubleVectorRefToken* pDVR =
2691 dynamic_cast<const formula::DoubleVectorRefToken *>(pCur);
2692 size_t nCurWindowSize = pDVR->GetRefRowSize();
2693 ss << " for (int i = ";
2694 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
2695 #ifdef ISNAN
2696 ss << "gid0; i < " << pDVR->GetArrayLength();
2697 ss << " && i < " << nCurWindowSize << "; i++){\n";
2698 #else
2699 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
2700 #endif
2701 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
2702 #ifdef ISNAN
2703 ss << "0; i < " << pDVR->GetArrayLength();
2704 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
2705 #else
2706 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
2707 #endif
2708 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
2709 #ifdef ISNAN
2710 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2711 ss << " && i < "<< nCurWindowSize << "; i++){\n";
2712 #else
2713 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
2714 #endif
2716 else {
2717 #ifdef ISNAN
2718 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
2719 #else
2720 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
2721 #endif
2724 else if (pCur->GetType() == formula::svSingleVectorRef)
2726 #ifdef ISNAN
2727 const formula::SingleVectorRefToken* pSVR =
2728 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
2729 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2730 #else
2731 #endif
2733 else if (pCur->GetType() == formula::svDouble)
2735 #ifdef ISNAN
2736 ss << "{\n";
2737 #endif
2739 else
2741 #ifdef ISNAN
2742 ss << "nCount += 1;\n";
2743 #endif
2745 #ifdef ISNAN
2746 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
2748 ss << " double temp=";
2749 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2750 ss << ";\n";
2751 ss << " double temp1=1.0;";
2752 ss << " if (isNan(temp)){\n";
2753 ss << " tmp += 0;}\n";
2754 ss << " else{\n";
2755 ss << " for(int i=1;i<nCount;i+=2)\n";
2756 ss << " temp1*=pow(1.0f+ arg0 ,2);\n";
2757 ss << " if(nCount%2)\n";
2758 ss << " temp1*=1.0f+ arg0;\n";
2759 ss << " tmp +=temp/ temp1;\n";
2760 ss << " nCount += 1;\n";
2761 ss << " }\n";
2762 ss << " }\n";
2764 else
2766 ss << " double temp=";
2767 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2768 ss << ";\n";
2769 ss << " double temp1=1.0;";
2770 ss << " for(int i=1;i<nCount;i+=2)";
2771 ss << " temp1*=pow(1.0f+ arg0 ,2);\n";
2772 ss << " if(nCount%2)";
2773 ss << " temp1*=1.0f+ arg0;\n";
2774 ss << " tmp +=temp/ temp1;\n";
2775 ss << " nCount += 1;\n";
2777 #else
2778 ss << "tmp +="<<vSubArguments[i]->GenSlidingWindowDeclRef();
2779 ss <<" / pow(1.0f+ arg0 ,";
2780 ss <<" (double)nCount );\n";
2781 ss << " nCount += 1;\n";
2782 #endif
2784 ss << " return tmp;\n";
2785 ss << "}";
2788 void OpPrice::BinInlineFun(std::set<std::string>& decls,
2789 std::set<std::string>& funs)
2791 decls.insert(getPrice_new_Decl);
2792 decls.insert(IsLeapYearDecl);decls.insert(DaysInMonthDecl);
2793 decls.insert(DaysToDateDecl);
2794 decls.insert(DateToDaysDecl);
2795 decls.insert(ScaDateDecl);
2796 decls.insert(setDayDecl);decls.insert(checklessthanDecl);
2797 decls.insert(addMonthsDecl);decls.insert(lcl_Getcoupnum_newDecl);
2798 decls.insert(coupnum_newDecl);
2799 decls.insert(DateToDays_newDecl);
2800 decls.insert(getDaysInMonthRangeDecl);
2801 decls.insert(GetDaysInYearsDecl); decls.insert(GetDaysInYearDecl);
2802 decls.insert(getDaysInYearRangeDecl); decls.insert(getDiffDecl);
2803 decls.insert(coupdaybs_newDecl);
2804 decls.insert(lcl_Getcoupdays_newDecl);
2805 decls.insert(lcl_Getcoupdaybs_newDecl);
2806 decls.insert(coupdays_newDecl);
2807 decls.insert(coupdaysnc_newDecl);
2808 funs.insert(IsLeapYear);funs.insert(DaysInMonth_new);
2809 funs.insert(DaysToDate);funs.insert(DateToDays_new);
2810 funs.insert(DateToDays);
2811 funs.insert(ScaDate);
2812 funs.insert(addMonths);funs.insert(getDaysInMonthRange);
2813 funs.insert(GetDaysInYears);funs.insert(GetDaysInYear);
2814 funs.insert(getDaysInYearRange);funs.insert(getDiff);
2815 funs.insert(setDay);funs.insert(checklessthan);
2816 funs.insert(lcl_Getcoupdaybs_new);
2817 funs.insert(coupdaybs_new);
2818 funs.insert(lcl_Getcoupdays_new);
2819 funs.insert(coupdaysnc_new);
2820 funs.insert(coupdays_new);
2821 funs.insert(setDay);funs.insert(checklessthan);
2822 funs.insert(lcl_Getcoupnum_new);
2823 funs.insert(coupnum_new);funs.insert(getPrice_new);
2825 void OpPrice::GenSlidingWindowFunction(std::stringstream &ss,
2826 const std::string sSymName, SubArguments &vSubArguments)
2828 ss << "\ndouble " << sSymName;
2829 ss << "_"<< BinFuncName() <<"(";
2830 for (unsigned i = 0; i < vSubArguments.size(); i++)
2832 if (i)
2833 ss << ", ";
2834 vSubArguments[i]->GenSlidingWindowDecl(ss);
2836 ss<<") {\n";
2837 ss<<" double tmp = 0;\n";
2838 ss<<" int gid0 = get_global_id(0);\n";
2839 ss<<" double tmp0=0;\n";
2840 ss<<" double tmp1=0;\n";
2841 ss<<" double tmp2=0;\n";
2842 ss<<" double tmp3=0;\n";
2843 ss<<" double tmp4=0,tmp5=0;\n";
2844 ss<<" double tmp6=0;\n";
2845 ss<<"\n";
2846 for (size_t i = 0; i < vSubArguments.size(); i++)
2848 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2849 assert(pCur);
2850 if (pCur->GetType() == formula::svSingleVectorRef)
2852 #ifdef ISNAN
2853 const formula::SingleVectorRefToken* pSVR =
2854 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
2855 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2856 #else
2857 #endif
2859 else if (pCur->GetType() == formula::svDouble)
2861 #ifdef ISNAN
2862 ss << "{\n";
2863 #endif
2865 else
2867 #ifdef ISNAN
2868 #endif
2870 #ifdef ISNAN
2871 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
2873 ss << " if (isNan(";
2874 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2875 ss << "))\n";
2876 ss << " tmp"<<i<<"= 0;\n";
2877 ss << " else\n";
2878 ss << " tmp"<<i<<"=";
2879 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2880 ss << ";\n }\n";
2882 else
2884 ss << " tmp"<<i<<"=";
2885 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
2886 ss <<";\n";
2888 #else
2889 ss << " tmp"<<i<<"=";
2890 ss<<vSubArguments[i]->GenSlidingWindowDeclRef();
2891 ss <<";\n";
2892 #endif
2894 ss << " tmp = getPrice_(tmp0,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6);\n";
2895 ss << " return tmp;\n";
2896 ss << "}";
2899 void OpOddlprice::BinInlineFun(std::set<std::string>& decls,
2900 std::set<std::string>& funs)
2902 decls.insert(GetOddlpriceDecl);decls.insert(GetDiffDateDecl);
2903 decls.insert(GetYearDiffDecl);decls.insert(IsLeapYearDecl);
2904 decls.insert(GetNullDateDecl);decls.insert(DateToDaysDecl);
2905 decls.insert(DaysToDateDecl);decls.insert(DaysInMonthDecl);
2906 decls.insert(GetYearFracDecl);
2907 funs.insert(GetOddlprice);funs.insert(GetDiffDate);
2908 funs.insert(GetYearDiff);funs.insert(IsLeapYear);
2909 funs.insert(GetNullDate);funs.insert(DaysInMonth);
2910 funs.insert(DaysToDate);funs.insert(DateToDays);
2911 funs.insert(GetYearFrac);
2913 void OpOddlprice::GenSlidingWindowFunction(std::stringstream &ss,
2914 const std::string sSymName, SubArguments &vSubArguments)
2916 ss << "\ndouble " << sSymName;
2917 ss << "_"<< BinFuncName() <<"(";
2918 for (unsigned i = 0; i < vSubArguments.size(); i++)
2920 if (i)
2921 ss << ", ";
2922 vSubArguments[i]->GenSlidingWindowDecl(ss);
2924 ss <<") {\n";
2925 ss <<" double tmp = 0;\n";
2926 ss <<" int gid0 = get_global_id(0);\n";
2927 ss <<" double tmp0=0;\n";
2928 ss <<" double tmp1=0;\n";
2929 ss <<" double tmp2=0;\n";
2930 ss <<" double tmp3=0;\n";
2931 ss <<" double tmp4=0;\n";
2932 ss <<" double tmp5=0;\n";
2933 ss <<" double tmp6=0;\n";
2934 ss <<" double tmp7=0;\n";
2935 size_t nItems = 0;
2936 ss <<" \n";
2937 for (size_t i = 0; i < vSubArguments.size(); i++)
2939 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
2940 assert(pCur);
2941 if (pCur->GetType() == formula::svDoubleVectorRef)
2943 const formula::DoubleVectorRefToken* pDVR =
2944 dynamic_cast<const formula::DoubleVectorRefToken *>(pCur);
2945 size_t nCurWindowSize = pDVR->GetRefRowSize();
2946 ss << " for (int i = ";
2947 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
2948 #ifdef ISNAN
2949 ss << "gid0; i < " << pDVR->GetArrayLength();
2950 ss << " && i < " << nCurWindowSize << "; i++){\n";
2951 #else
2952 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
2953 #endif
2954 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
2955 #ifdef ISNAN
2956 ss << "0; i < " << pDVR->GetArrayLength();
2957 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
2958 #else
2959 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
2960 #endif
2961 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
2962 #ifdef ISNAN
2963 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
2964 ss << " && i < "<< nCurWindowSize << "; i++){\n";
2965 #else
2966 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
2967 #endif
2969 else {
2970 #ifdef ISNAN
2971 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
2972 #else
2973 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
2974 #endif
2976 nItems += nCurWindowSize;
2978 else if (pCur->GetType() == formula::svSingleVectorRef)
2980 #ifdef ISNAN
2981 const formula::SingleVectorRefToken* pSVR =
2982 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
2983 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
2984 #else
2985 nItems += 1;
2986 #endif
2988 else if (pCur->GetType() == formula::svDouble)
2990 #ifdef ISNAN
2991 ss << "{\n";
2992 #endif
2993 nItems += 1;
2995 else
2997 #ifdef ISNAN
2998 #endif
2999 nItems += 1;
3001 #ifdef ISNAN
3002 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3004 ss << " if (isNan(";
3005 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3006 ss << "))\n";
3007 ss << " tmp"<<i<<"= 0;\n";
3008 ss << " else\n";
3009 ss << " tmp"<<i<<"=";
3010 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
3011 ss << ";\n";
3012 ss <<" }\n";
3014 else
3016 ss << " tmp"<<i<<"=";
3017 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
3018 ss <<";\n";
3020 #else
3021 ss << " tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3022 ss <<";\n";
3023 #endif
3025 ss <<" int nNullDate = GetNullDate();\n";
3026 ss <<" tmp = GetOddlprice(nNullDate,tmp0,tmp1,";
3027 ss <<"tmp2,tmp3,tmp4,tmp5,tmp6,tmp7);\n";
3028 ss <<" return tmp;\n";
3029 ss <<"}";
3031 void OpOddlyield::BinInlineFun(std::set<std::string>& decls,
3032 std::set<std::string>& funs)
3034 decls.insert(GetDiffDateDecl);decls.insert(DaysToDateDecl);
3035 decls.insert(GetYearDiffDecl);decls.insert(IsLeapYearDecl);
3036 decls.insert(GetNullDateDecl);decls.insert(DateToDaysDecl);
3037 decls.insert(DaysInMonthDecl);
3038 decls.insert(GetYearFracDecl);decls.insert(GetOddlyieldDecl);
3039 funs.insert(GetDiffDate);funs.insert(DaysToDate);
3040 funs.insert(GetYearDiff);funs.insert(IsLeapYear);
3041 funs.insert(GetNullDate);funs.insert(DaysInMonth);
3042 funs.insert(DateToDays);
3043 funs.insert(GetYearFrac);funs.insert(GetOddlyield);
3045 void OpOddlyield::GenSlidingWindowFunction(std::stringstream &ss,
3046 const std::string sSymName, SubArguments &vSubArguments)
3048 ss << "\ndouble " << sSymName;
3049 ss << "_"<< BinFuncName() <<"(";
3050 for (unsigned i = 0; i < vSubArguments.size(); i++)
3052 if (i)
3053 ss << ", ";
3054 vSubArguments[i]->GenSlidingWindowDecl(ss);
3056 ss <<") {\n";
3057 ss <<" double tmp = 0;\n";
3058 ss <<" int gid0 = get_global_id(0);\n";
3059 ss <<" double tmp0=0;\n";
3060 ss <<" double tmp1=0;\n";
3061 ss <<" double tmp2=0;\n";
3062 ss <<" double tmp3=0;\n";
3063 ss <<" double tmp4=0;\n";
3064 ss <<" double tmp5=0;\n";
3065 ss <<" double tmp6=0;\n";
3066 ss <<" double tmp7=0;\n";
3067 size_t nItems = 0;
3068 ss <<" \n";
3069 for (size_t i = 0; i < vSubArguments.size(); i++)
3071 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3072 assert(pCur);
3073 if (pCur->GetType() == formula::svDoubleVectorRef)
3075 const formula::DoubleVectorRefToken* pDVR =
3076 dynamic_cast<const formula::DoubleVectorRefToken *>(pCur);
3077 size_t nCurWindowSize = pDVR->GetRefRowSize();
3078 ss << " for (int i = ";
3079 if (!pDVR->IsStartFixed() && pDVR->IsEndFixed()) {
3080 #ifdef ISNAN
3081 ss << "gid0; i < " << pDVR->GetArrayLength();
3082 ss << " && i < " << nCurWindowSize << "; i++){\n";
3083 #else
3084 ss << "gid0; i < "<< nCurWindowSize << "; i++)\n";
3085 #endif
3086 } else if (pDVR->IsStartFixed() && !pDVR->IsEndFixed()) {
3087 #ifdef ISNAN
3088 ss << "0; i < " << pDVR->GetArrayLength();
3089 ss << " && i < gid0+"<< nCurWindowSize << "; i++){\n";
3090 #else
3091 ss << "0; i < gid0+"<< nCurWindowSize << "; i++)\n";
3092 #endif
3093 } else if (!pDVR->IsStartFixed() && !pDVR->IsEndFixed()){
3094 #ifdef ISNAN
3095 ss << "0; i + gid0 < " << pDVR->GetArrayLength();
3096 ss << " && i < "<< nCurWindowSize << "; i++){\n";
3097 #else
3098 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
3099 #endif
3101 else {
3102 #ifdef ISNAN
3103 ss << "0; i < "<< nCurWindowSize << "; i++){\n";
3104 #else
3105 ss << "0; i < "<< nCurWindowSize << "; i++)\n";
3106 #endif
3108 nItems += nCurWindowSize;
3110 else if (pCur->GetType() == formula::svSingleVectorRef)
3112 #ifdef ISNAN
3113 const formula::SingleVectorRefToken* pSVR =
3114 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
3115 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3116 #else
3117 nItems += 1;
3118 #endif
3120 else if (pCur->GetType() == formula::svDouble)
3122 #ifdef ISNAN
3123 ss << "{\n";
3124 #endif
3125 nItems += 1;
3127 else
3129 #ifdef ISNAN
3130 #endif
3131 nItems += 1;
3133 #ifdef ISNAN
3134 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3136 ss << " if (isNan(";
3137 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3138 ss << "))\n";
3139 ss << " tmp"<<i<<"= 0;\n";
3140 ss << " else\n";
3141 ss << " tmp"<<i<<"=";
3142 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
3143 ss << ";\n";
3144 ss <<" }\n";
3146 else
3148 ss << " tmp"<<i<<"=";
3149 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
3150 ss <<";\n";
3152 #else
3153 ss << " tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3154 ss <<";\n";
3155 #endif
3157 ss <<" int nNullDate = GetNullDate();\n";
3158 ss <<" tmp = GetOddlyield(nNullDate,tmp0,tmp1";
3159 ss <<",tmp2,tmp3,tmp4,tmp5,tmp6,tmp7);\n";
3160 ss <<" return tmp;\n";
3161 ss <<"}";
3163 void OpPriceDisc::BinInlineFun(std::set<std::string>& decls,
3164 std::set<std::string>& funs)
3166 decls.insert(GetYearDiffDecl);decls.insert(getDiffDecl);
3167 decls.insert(getDaysInYearRangeDecl);decls.insert(GetDaysInYearDecl);
3168 decls.insert(GetDaysInYearsDecl);decls.insert(getDaysInMonthRangeDecl);
3169 decls.insert(addMonthsDecl);decls.insert(ScaDateDecl);
3170 decls.insert(GetNullDateDecl);decls.insert(DateToDaysDecl);
3171 decls.insert(DaysToDateDecl);decls.insert(DaysInMonthDecl);
3172 decls.insert(IsLeapYearDecl);decls.insert(GetDiffDateDecl);
3173 funs.insert(GetYearDiff);funs.insert(getDiff);
3174 funs.insert(getDaysInYearRange);funs.insert(GetDaysInYear);
3175 funs.insert(GetDaysInYears);funs.insert(getDaysInMonthRange);
3176 funs.insert(addMonths);funs.insert(ScaDate);
3177 funs.insert(GetNullDate);funs.insert(DateToDays);
3178 funs.insert(DaysToDate);funs.insert(DaysInMonth);
3179 funs.insert(IsLeapYear);funs.insert(GetDiffDate);
3181 void OpPriceDisc::GenSlidingWindowFunction(std::stringstream &ss,
3182 const std::string sSymName, SubArguments &vSubArguments)
3184 ss << "\ndouble " << sSymName;
3185 ss << "_"<< BinFuncName() <<"(";
3186 for (unsigned i = 0; i < vSubArguments.size(); i++)
3188 if (i)
3189 ss << ", ";
3190 vSubArguments[i]->GenSlidingWindowDecl(ss);
3192 ss << ") {\n";
3193 ss << " double tmp = 0;\n";
3194 ss << " int gid0 = get_global_id(0);\n";
3195 ss<<" double tmp0=0;\n";
3196 ss<<" double tmp1=0;\n";
3197 ss<<" double tmp2=0;\n";
3198 ss<<" double tmp3=0;\n";
3199 ss<<" double tmp4=0;\n";
3200 ss <<" \n";
3201 for (size_t i = 0; i < vSubArguments.size(); i++)
3203 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3204 assert(pCur);
3205 if (pCur->GetType() == formula::svSingleVectorRef)
3207 #ifdef ISNAN
3208 const formula::SingleVectorRefToken* pSVR =
3209 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
3210 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3211 #else
3212 #endif
3214 else if (pCur->GetType() == formula::svDouble)
3216 #ifdef ISNAN
3217 ss << "{\n";
3218 #endif
3220 else
3222 #ifdef ISNAN
3223 #endif
3225 #ifdef ISNAN
3226 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3228 ss << " if (isNan(";
3229 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3230 ss << "))\n";
3231 ss << " tmp"<<i<<"= 0;\n";
3232 ss << " else\n";
3233 ss << " tmp"<<i<<"=";
3234 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
3235 ss << ";\n";
3236 ss <<" }\n";
3238 else
3240 ss << " tmp"<<i<<"=";
3241 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
3242 ss <<";\n";
3244 #else
3245 ss << " tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3246 ss <<";\n";
3247 #endif
3249 ss <<" int nNullDate = GetNullDate();\n";
3250 ss <<" tmp=tmp3* ( 1.0 -tmp2*GetYearDiff( nNullDate, ";
3251 ss <<"tmp0,tmp1,tmp4));\n";
3252 ss <<" return tmp;\n";
3253 ss <<"}";
3255 void OpNper::GenSlidingWindowFunction(std::stringstream &ss,
3256 const std::string sSymName, SubArguments &vSubArguments)
3258 ss << "\ndouble " << sSymName;
3259 ss << "_"<< BinFuncName() <<"(";
3260 for (unsigned i = 0; i < vSubArguments.size(); i++)
3262 if (i)
3263 ss << ", ";
3264 vSubArguments[i]->GenSlidingWindowDecl(ss);
3266 ss << ") {\n";
3267 ss << " double tmp = 0;\n";
3268 ss << " int gid0 = get_global_id(0);\n";
3269 ss <<" double tmp0=0;\n";
3270 ss <<" double tmp1=0;\n";
3271 ss <<" double tmp2=0;\n";
3272 ss <<" double tmp3=0;\n";
3273 ss <<" double tmp4=0;\n";
3274 size_t nItems = 0;
3275 for (size_t i = 0; i < vSubArguments.size(); i++)
3277 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3278 assert(pCur);
3279 if (pCur->GetType() == formula::svSingleVectorRef)
3281 #ifdef ISNAN
3282 const formula::SingleVectorRefToken* pSVR =
3283 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
3284 ss << " if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3285 #else
3286 nItems += 1;
3287 #endif
3289 else if (pCur->GetType() == formula::svDouble)
3291 #ifdef ISNAN
3292 ss << "{\n";
3293 #endif
3294 nItems += 1;
3296 else
3298 #ifdef ISNAN
3299 #endif
3300 nItems += 1;
3302 #ifdef ISNAN
3303 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3305 ss << " if (isNan(";
3306 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3307 ss << "))\n";
3308 ss << " tmp"<<i<<"= 0;\n";
3309 ss << " else\n";
3310 ss << " tmp"<<i<<"=";
3311 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
3312 ss << ";\n";
3313 ss <<" }\n";
3315 else
3317 ss << " tmp"<<i<<"=";
3318 ss <<vSubArguments[i]->GenSlidingWindowDeclRef();
3319 ss <<";\n";
3321 #else
3322 ss << " tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3323 ss <<";\n";
3324 #endif
3326 ss <<" if (tmp0 == 0.0)\n";
3327 ss <<" tmp=(-1*(tmp2 + tmp3)/tmp1);\n";
3328 ss <<" else if (tmp4 > 0.0)\n";
3329 ss <<" tmp=log(-1*(tmp0*tmp3-tmp1*(1.0+tmp0))*";
3330 ss <<"pow((tmp0*tmp2+tmp1*(1.0+tmp0)),-1))/log(1.0+tmp0);\n";
3331 ss <<" else\n";
3332 ss <<" tmp=log(-1*(tmp0*tmp3-tmp1)*pow(tmp0*tmp2+tmp1,-1))";
3333 ss <<"/log(1.0+tmp0);\n";
3334 ss <<" return tmp;\n";
3335 ss <<"}";
3338 void OpPPMT::BinInlineFun(std::set<std::string>& decls,
3339 std::set<std::string>& funs)
3341 decls.insert(GetZwDecl);
3342 funs.insert(GetZw);
3345 void OpPPMT::GenSlidingWindowFunction(std::stringstream &ss,
3346 const std::string sSymName, SubArguments &vSubArguments)
3348 ss << "\ndouble " << sSymName;
3349 ss << "_"<< BinFuncName() <<"(";
3350 for (unsigned i = 0; i < vSubArguments.size(); i++)
3352 if (i)
3353 ss << ", ";
3354 vSubArguments[i]->GenSlidingWindowDecl(ss);
3356 ss<<") {\n";
3357 ss<<" double tmp = 0;\n";
3358 ss<<" int gid0 = get_global_id(0);\n";
3359 ss<<" double arg=0;\n";
3360 ss<<" double tmp0=0;\n";
3361 ss<<" double tmp1=0;\n";
3362 ss<<" double tmp2=0;\n";
3363 ss<<" double tmp3=0;\n";
3364 ss<<" double tmp4=0,tmp5=0;\n";
3365 ss <<"\n ";
3366 //while (i-- > 1)
3367 for (size_t i = 0; i < vSubArguments.size(); i++)
3369 FormulaToken *pCur = vSubArguments[i]->GetFormulaToken();
3370 assert(pCur);
3371 if (pCur->GetType() == formula::svSingleVectorRef)
3373 #ifdef ISNAN
3374 const formula::SingleVectorRefToken* pSVR =
3375 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
3376 ss << "if (gid0 < " << pSVR->GetArrayLength() << "){\n";
3377 #else
3378 #endif
3380 else if (pCur->GetType() == formula::svDouble)
3382 #ifdef ISNAN
3383 ss << "{\n";
3384 #endif
3386 else
3388 #ifdef ISNAN
3389 #endif
3391 #ifdef ISNAN
3392 if(ocPush==vSubArguments[i]->GetFormulaToken()->GetOpCode())
3394 ss << " arg=";
3395 ss << vSubArguments[i]->GenSlidingWindowDeclRef();
3396 ss << ";\n";
3397 ss << " if (isNan(arg))\n";
3398 ss << " tmp"<<i<<"= 0;\n";
3399 ss << " else\n";
3400 ss << " tmp"<<i<<"=arg;\n";
3401 ss << " }\n";
3403 else
3405 ss<<" tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3406 ss<<";\n";
3408 #else
3409 ss<<"tmp"<<i<<"="<<vSubArguments[i]->GenSlidingWindowDeclRef();
3410 ss<<";\n";
3411 #endif
3413 ss<<" double pmt=0 ;\n";
3414 ss<<" if(tmp0==0.0)\n";
3415 ss<<" return -(tmp3+tmp4)/tmp2;\n";
3416 ss<<" pmt=pmt-tmp4-tmp3*pow(1.0+tmp0,tmp2);\n";
3417 ss<<" pmt=pmt*pow(( (1.0+tmp0*tmp5)* ";
3418 ss<<"( (pow(1.0+tmp0,tmp2)-1.0)/tmp0)),-1);\n";
3419 ss<<" double temp = pow( 1+tmp0,tmp1-2);\n";
3420 ss<<" double re;\n";
3421 ss<<" if(tmp1==1.0){\n";
3422 ss<<" if(tmp5>0.0)\n";
3423 ss<<" re=0.0;\n";
3424 ss<<" else\n";
3425 ss<<" re=-tmp3;\n";
3426 ss<<" }\n";
3427 ss<<" else\n";
3428 ss<<" {\n";
3429 ss<<" if(tmp5>0.0)\n ";
3430 ss<<" re=GetZw(tmp0, tmp1-2.0, pmt, tmp3, 1.0) - pmt;\n";
3431 ss<<" else\n";
3432 ss<<" re=GetZw(tmp0, tmp1-1.0, pmt, tmp3, 0.0);\n";
3433 ss<<" }\n ";
3434 ss<<" re = re * tmp0;\n";
3435 ss<<" tmp = pmt - re;\n";
3436 ss<<" return tmp;\n";
3437 ss<<"}";
3440 void OpCoupdaybs::BinInlineFun(std::set<std::string>& decls,
3441 std::set<std::string>& funs)
3443 decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
3444 decls.insert(DaysToDateDecl); decls.insert(DateToDays_newDecl);
3445 decls.insert(GetNullDate_newDecl); decls.insert(ScaDateDecl);
3446 decls.insert(addMonthsDecl); decls.insert(getDaysInMonthRangeDecl);
3447 decls.insert(GetDaysInYearsDecl);
3448 decls.insert(getDaysInYearRangeDecl); decls.insert(getDiffDecl);
3449 decls.insert(setDayDecl);decls.insert(checklessthanDecl);
3450 decls.insert(lcl_Getcoupdaybs_newDecl);
3451 decls.insert(coupdaybs_newDecl);
3452 funs.insert(IsLeapYear);funs.insert(DaysInMonth);
3453 funs.insert(DaysToDate);funs.insert(DateToDays_new);
3454 funs.insert(GetNullDate_new);funs.insert(ScaDate);
3455 funs.insert(addMonths);funs.insert(getDaysInMonthRange);
3456 funs.insert(GetDaysInYears);
3457 funs.insert(getDaysInYearRange);funs.insert(getDiff);
3458 funs.insert(setDay);funs.insert(checklessthan);
3459 funs.insert(lcl_Getcoupdaybs_new);
3460 funs.insert(coupdaybs_new);
3462 void OpCoupdaybs::GenSlidingWindowFunction(
3463 std::stringstream &ss, const std::string sSymName, SubArguments &
3464 vSubArguments)
3466 ss << "\ndouble " << sSymName;
3467 ss << "_"<< BinFuncName() <<"(";
3468 for (unsigned i = 0; i < vSubArguments.size(); i++)
3470 if (i)
3471 ss << ",";
3472 vSubArguments[i]->GenSlidingWindowDecl(ss);
3474 ss << ") {\n";
3475 ss << " double tmp = 0;\n";
3476 ss << " int gid0 = get_global_id(0);\n";
3477 ss << " int nSettle,nMat,nFreq,nBase;\n";
3478 #ifdef ISNAN
3479 FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3480 FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3481 FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3482 FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3483 #endif
3484 #ifdef ISNAN
3485 if(tmpCur0->GetType() == formula::svSingleVectorRef)
3487 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
3488 formula::SingleVectorRefToken *>(tmpCur0);
3489 ss <<" if(isNan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3490 ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3491 ss <<" nSettle = 0;\n else\n";
3493 #endif
3494 ss <<" nSettle=(int)";
3495 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3496 ss <<";\n";
3497 #ifdef ISNAN
3498 if(tmpCur1->GetType() == formula::svSingleVectorRef)
3500 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
3501 formula::SingleVectorRefToken *>(tmpCur1);
3502 ss <<" if(isNan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3503 ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3504 ss <<" nMat = 0;\n else\n";
3506 #endif
3507 ss <<" nMat=(int)";
3508 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3509 ss <<";\n";
3510 #ifdef ISNAN
3511 if(tmpCur2->GetType() == formula::svSingleVectorRef)
3513 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
3514 formula::SingleVectorRefToken *>(tmpCur2);
3515 ss <<" if(isNan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3516 ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3517 ss <<" nFreq = 0;\n else\n";
3519 #endif
3520 ss << " nFreq=(int)";
3521 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3522 ss <<";\n";
3523 #ifdef ISNAN
3524 if(tmpCur3->GetType() == formula::svSingleVectorRef)
3526 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
3527 formula::SingleVectorRefToken *>(tmpCur3);
3528 ss <<" if(isNan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3529 ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3530 ss <<" nBase = 0;\n else\n";
3532 #endif
3533 ss << " nBase=(int)";
3534 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3535 ss << ";\n";
3536 ss <<" tmp = coupdaybs_new(nSettle,nMat,nFreq,nBase);\n";
3537 ss <<" return tmp;\n";
3538 ss <<"}";
3541 void OpCoupdays::BinInlineFun(std::set<std::string>& decls,
3542 std::set<std::string>& funs)
3544 decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
3545 decls.insert(DaysToDateDecl); decls.insert(DateToDays_newDecl);
3546 decls.insert(GetNullDate_newDecl); decls.insert(ScaDateDecl);
3547 decls.insert(addMonthsDecl); decls.insert(getDaysInMonthRangeDecl);
3548 decls.insert(GetDaysInYearsDecl); decls.insert(GetDaysInYearDecl);
3549 decls.insert(getDaysInYearRangeDecl); decls.insert(getDiffDecl);
3550 decls.insert(setDayDecl);decls.insert(checklessthanDecl);
3551 decls.insert(lcl_Getcoupdays_newDecl);
3552 decls.insert(coupdays_newDecl);
3553 funs.insert(IsLeapYear);funs.insert(DaysInMonth);
3554 funs.insert(DaysToDate);funs.insert(DateToDays_new);
3555 funs.insert(GetNullDate_new);funs.insert(ScaDate);
3556 funs.insert(addMonths);funs.insert(getDaysInMonthRange);
3557 funs.insert(GetDaysInYears);funs.insert(GetDaysInYear);
3558 funs.insert(getDaysInYearRange);funs.insert(getDiff);
3559 funs.insert(lcl_Getcoupdays_new);
3560 funs.insert(setDay);funs.insert(checklessthan);
3561 funs.insert(coupdays_new);
3563 void OpCoupdays::GenSlidingWindowFunction(
3564 std::stringstream &ss, const std::string sSymName, SubArguments &
3565 vSubArguments)
3567 ss << "\ndouble " << sSymName;
3568 ss << "_"<< BinFuncName() <<"(";
3569 for (unsigned i = 0; i < vSubArguments.size(); i++)
3571 if (i)
3572 ss << ",";
3573 vSubArguments[i]->GenSlidingWindowDecl(ss);
3575 ss << ") {\n";
3576 ss << " double tmp = 0;\n";
3577 ss << " int gid0 = get_global_id(0);\n";
3578 ss << " int nSettle,nMat,nFreq,nBase;\n";
3579 #ifdef ISNAN
3580 FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3581 FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3582 FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3583 FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3584 #endif
3585 #ifdef ISNAN
3586 if(tmpCur0->GetType() == formula::svSingleVectorRef)
3588 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
3589 formula::SingleVectorRefToken *>(tmpCur0);
3590 ss <<" if(isNan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3591 ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3592 ss <<" nSettle = 0;\n else\n";
3594 #endif
3595 ss <<" nSettle=(int)";
3596 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3597 ss <<";\n";
3598 #ifdef ISNAN
3599 if(tmpCur1->GetType() == formula::svSingleVectorRef)
3601 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
3602 formula::SingleVectorRefToken *>(tmpCur1);
3603 ss <<" if(isNan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3604 ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3605 ss <<" nMat = 0;\n else\n";
3607 #endif
3608 ss <<" nMat=(int)";
3609 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3610 ss <<";\n";
3611 #ifdef ISNAN
3612 if(tmpCur2->GetType() == formula::svSingleVectorRef)
3614 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
3615 formula::SingleVectorRefToken *>(tmpCur2);
3616 ss <<" if(isNan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3617 ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3618 ss <<" nFreq = 0;\n else\n";
3620 #endif
3621 ss << " nFreq=(int)";
3622 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3623 ss <<";\n";
3624 #ifdef ISNAN
3625 if(tmpCur3->GetType() == formula::svSingleVectorRef)
3627 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
3628 formula::SingleVectorRefToken *>(tmpCur3);
3629 ss <<" if(isNan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3630 ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3631 ss <<" nBase = 0;\n else\n";
3633 #endif
3634 ss << " nBase=(int)";
3635 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3636 ss << ";\n";
3637 ss <<" tmp = coupdays_new(nSettle,nMat,nFreq,nBase);\n";
3638 ss <<" return tmp;\n";
3639 ss << "}";
3641 void OpCouppcd::BinInlineFun(std::set<std::string>& decls,
3642 std::set<std::string>& funs)
3644 decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
3645 decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
3646 decls.insert(GetNullDateDecl);
3647 decls.insert(ScaDateDecl);
3648 decls.insert(addMonthsDecl);
3649 decls.insert(setDayDecl);decls.insert(checklessthanDecl);
3650 decls.insert(lcl_GetCouppcdDecl);
3651 funs.insert(IsLeapYear);funs.insert(DaysInMonth);
3652 funs.insert(DaysToDate);funs.insert(DateToDays);
3653 funs.insert(GetNullDate);
3654 funs.insert(ScaDate);
3655 funs.insert(addMonths);
3656 funs.insert(setDay);funs.insert(checklessthan);
3657 funs.insert(lcl_GetCouppcd);
3659 void OpCouppcd::GenSlidingWindowFunction(
3660 std::stringstream &ss, const std::string sSymName,
3661 SubArguments &vSubArguments)
3663 ss << "\ndouble " << sSymName;
3664 ss << "_"<< BinFuncName() <<"(";
3665 for (unsigned i = 0; i < vSubArguments.size(); i++)
3667 if (i)
3668 ss << ",";
3669 vSubArguments[i]->GenSlidingWindowDecl(ss);
3671 ss << ") {\n";
3672 ss << " double tmp = 0;\n";
3673 ss << " int gid0 = get_global_id(0);\n";
3674 ss << " int nSettle,nMat,nFreq,nBase;\n";
3675 #ifdef ISNAN
3676 FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3677 FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3678 FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3679 FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3680 #endif
3681 #ifdef ISNAN
3682 if(tmpCur0->GetType() == formula::svSingleVectorRef)
3684 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
3685 formula::SingleVectorRefToken *>(tmpCur0);
3686 ss <<" if(isNan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3687 ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3688 ss <<" nSettle = 0;\n else\n";
3690 #endif
3691 ss <<" nSettle=(int)";
3692 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3693 ss <<";\n";
3694 #ifdef ISNAN
3695 if(tmpCur1->GetType() == formula::svSingleVectorRef)
3697 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
3698 formula::SingleVectorRefToken *>(tmpCur1);
3699 ss <<" if(isNan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3700 ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3701 ss <<" nMat = 0;\n else\n";
3703 #endif
3704 ss <<" nMat=(int)";
3705 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3706 ss <<";\n";
3707 #ifdef ISNAN
3708 if(tmpCur2->GetType() == formula::svSingleVectorRef)
3710 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
3711 formula::SingleVectorRefToken *>(tmpCur2);
3712 ss <<" if(isNan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3713 ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3714 ss <<" nFreq = 0;\n else\n";
3716 #endif
3717 ss << " nFreq=(int)";
3718 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3719 ss <<";\n";
3720 #ifdef ISNAN
3721 if(tmpCur3->GetType() == formula::svSingleVectorRef)
3723 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
3724 formula::SingleVectorRefToken *>(tmpCur3);
3725 ss <<" if(isNan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3726 ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3727 ss <<" nBase = 0;\n else\n";
3729 #endif
3730 ss << " nBase=(int)";
3731 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3732 ss << ";\n";
3733 ss <<" int nNullDate=693594;\n";
3734 ss <<" tmp = lcl_GetCouppcd(nNullDate,nSettle,nMat,nFreq,nBase);\n";
3735 ss <<" return tmp;\n";
3736 ss <<"}";
3738 void OpCoupncd::BinInlineFun(std::set<std::string>& decls,
3739 std::set<std::string>& funs)
3741 decls.insert(IsLeapYearDecl); decls.insert(DaysInMonthDecl);
3742 decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
3743 decls.insert(GetNullDateDecl);
3744 decls.insert(ScaDateDecl);
3745 decls.insert(addMonthsDecl);
3746 decls.insert(setDayDecl);decls.insert(checklessthanDecl);
3747 decls.insert(lcl_GetCoupncdDecl);
3748 funs.insert(IsLeapYear);funs.insert(DaysInMonth);
3749 funs.insert(DaysToDate);funs.insert(DateToDays);
3750 funs.insert(GetNullDate);
3751 funs.insert(ScaDate);
3752 funs.insert(addMonths);
3753 funs.insert(setDay);funs.insert(checklessthan);
3754 funs.insert(lcl_GetCoupncd);
3756 void OpCoupncd::GenSlidingWindowFunction(
3757 std::stringstream &ss, const std::string sSymName, SubArguments &
3758 vSubArguments)
3760 ss << "\ndouble " << sSymName;
3761 ss << "_"<< BinFuncName() <<"(";
3762 for (unsigned i = 0; i < vSubArguments.size(); i++)
3764 if (i)
3765 ss << ",";
3766 vSubArguments[i]->GenSlidingWindowDecl(ss);
3768 ss << ") {\n";
3769 ss << " double tmp = 0;\n";
3770 ss << " int gid0 = get_global_id(0);\n";
3771 ss << " int nSettle,nMat,nFreq,nBase;\n";
3772 #ifdef ISNAN
3773 FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3774 FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3775 FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3776 FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3777 #endif
3778 #ifdef ISNAN
3779 if(tmpCur0->GetType() == formula::svSingleVectorRef)
3781 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
3782 formula::SingleVectorRefToken *>(tmpCur0);
3783 ss <<" if(isNan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3784 ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3785 ss <<" nSettle = 0;\n else\n";
3787 #endif
3788 ss <<" nSettle=(int)";
3789 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3790 ss <<";\n";
3791 #ifdef ISNAN
3792 if(tmpCur1->GetType() == formula::svSingleVectorRef)
3794 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
3795 formula::SingleVectorRefToken *>(tmpCur1);
3796 ss <<" if(isNan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3797 ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3798 ss <<" nMat = 0;\n else\n";
3800 #endif
3801 ss <<" nMat=(int)";
3802 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3803 ss <<";\n";
3804 #ifdef ISNAN
3805 if(tmpCur2->GetType() == formula::svSingleVectorRef)
3807 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
3808 formula::SingleVectorRefToken *>(tmpCur2);
3809 ss <<" if(isNan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3810 ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3811 ss <<" nFreq = 0;\n else\n";
3813 #endif
3814 ss << " nFreq=(int)";
3815 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3816 ss <<";\n";
3817 #ifdef ISNAN
3818 if(tmpCur3->GetType() == formula::svSingleVectorRef)
3820 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
3821 formula::SingleVectorRefToken *>(tmpCur3);
3822 ss <<" if(isNan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3823 ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3824 ss <<" nBase = 0;\n else\n";
3826 #endif
3827 ss << " nBase=(int)";
3828 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3829 ss << ";\n";
3830 ss <<" int nNullDate=693594;\n";
3831 ss <<" tmp = lcl_GetCoupncd(nNullDate,nSettle,nMat,nFreq,nBase);\n";
3832 ss <<" return tmp;\n";
3833 ss <<"}";
3836 void OpCoupdaysnc::BinInlineFun(std::set<std::string>& decls,
3837 std::set<std::string>& funs)
3839 decls.insert(IsLeapYearDecl); decls.insert(DaysInMonth_newDecl);
3840 decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
3841 decls.insert(DateToDays_newDecl);
3842 decls.insert(ScaDateDecl);
3843 decls.insert(addMonthsDecl); decls.insert(getDaysInMonthRangeDecl);
3844 decls.insert(GetDaysInYearsDecl); decls.insert(GetDaysInYearDecl);
3845 decls.insert(getDaysInYearRangeDecl); decls.insert(getDiffDecl);
3846 decls.insert(setDayDecl);decls.insert(checklessthanDecl);
3847 decls.insert(coupdaybs_newDecl);
3848 decls.insert(lcl_Getcoupdays_newDecl);
3849 decls.insert(lcl_Getcoupdaybs_newDecl);
3850 decls.insert(coupdays_newDecl);
3851 decls.insert(coupdaysnc_newDecl);
3852 funs.insert(IsLeapYear);funs.insert(DaysInMonth_new);
3853 funs.insert(DaysToDate);funs.insert(DateToDays_new);
3854 funs.insert(DateToDays);
3855 funs.insert(ScaDate);
3856 funs.insert(addMonths);funs.insert(getDaysInMonthRange);
3857 funs.insert(GetDaysInYears);funs.insert(GetDaysInYear);
3858 funs.insert(getDaysInYearRange);funs.insert(getDiff);
3859 funs.insert(setDay);funs.insert(checklessthan);
3860 funs.insert(lcl_Getcoupdaybs_new);
3861 funs.insert(coupdaybs_new);
3862 funs.insert(lcl_Getcoupdays_new);
3863 funs.insert(coupdaysnc_new);
3864 funs.insert(coupdays_new);
3866 void OpCoupdaysnc::GenSlidingWindowFunction(
3867 std::stringstream &ss, const std::string sSymName,
3868 SubArguments &vSubArguments)
3870 ss << "\ndouble " << sSymName;
3871 ss << "_"<< BinFuncName() <<"(";
3872 for (unsigned i = 0; i < vSubArguments.size(); i++)
3874 if (i)
3875 ss << ",";
3876 vSubArguments[i]->GenSlidingWindowDecl(ss);
3878 ss << ") {\n";
3879 ss << " double tmp = 0;\n";
3880 ss << " int gid0 = get_global_id(0);\n";
3881 ss << " int nSettle,nMat,nFreq,nBase;\n";
3882 #ifdef ISNAN
3883 FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3884 FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3885 FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3886 FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3887 #endif
3888 #ifdef ISNAN
3889 if(tmpCur0->GetType() == formula::svSingleVectorRef)
3891 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
3892 formula::SingleVectorRefToken *>(tmpCur0);
3893 ss <<" if(isNan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3894 ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3895 ss <<" nSettle = 0;\n else\n";
3897 #endif
3898 ss <<" nSettle=(int)";
3899 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3900 ss <<";\n";
3901 #ifdef ISNAN
3902 if(tmpCur1->GetType() == formula::svSingleVectorRef)
3904 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
3905 formula::SingleVectorRefToken *>(tmpCur1);
3906 ss <<" if(isNan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
3907 ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
3908 ss <<" nMat = 0;\n else\n";
3910 #endif
3911 ss <<" nMat=(int)";
3912 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
3913 ss <<";\n";
3914 #ifdef ISNAN
3915 if(tmpCur2->GetType() == formula::svSingleVectorRef)
3917 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
3918 formula::SingleVectorRefToken *>(tmpCur2);
3919 ss <<" if(isNan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
3920 ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
3921 ss <<" nFreq = 0;\n else\n";
3923 #endif
3924 ss << " nFreq=(int)";
3925 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
3926 ss <<";\n";
3927 #ifdef ISNAN
3928 if(tmpCur3->GetType() == formula::svSingleVectorRef)
3930 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
3931 formula::SingleVectorRefToken *>(tmpCur3);
3932 ss <<" if(isNan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
3933 ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
3934 ss <<" nBase = 0;\n else\n";
3936 #endif
3937 ss << " nBase=(int)";
3938 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
3939 ss << ";\n";
3940 ss <<" tmp = coupdaysnc_new(nSettle,nMat,nFreq,nBase);\n";
3941 ss <<" return tmp;\n";
3942 ss << "}";
3945 void OpCoupnum::BinInlineFun(std::set<std::string>& decls,
3946 std::set<std::string>& funs)
3948 decls.insert(IsLeapYearDecl);decls.insert(DaysInMonthDecl);
3949 decls.insert(DaysToDateDecl);
3950 decls.insert(DateToDaysDecl);
3951 decls.insert(ScaDateDecl);
3952 decls.insert(setDayDecl);decls.insert(checklessthanDecl);
3953 decls.insert(addMonthsDecl);decls.insert(lcl_Getcoupnum_newDecl);
3954 decls.insert(coupnum_newDecl);
3955 funs.insert(IsLeapYear);funs.insert(DaysInMonth_new);
3956 funs.insert(DaysToDate);
3957 funs.insert(DateToDays);
3958 funs.insert(ScaDate);
3959 funs.insert(setDay);funs.insert(checklessthan);
3960 funs.insert(addMonths);funs.insert(lcl_Getcoupnum_new);
3961 funs.insert(coupnum_new);
3963 void OpCoupnum::GenSlidingWindowFunction(std::stringstream &ss,
3964 const std::string sSymName, SubArguments &vSubArguments)
3966 ss << "\ndouble " << sSymName;
3967 ss << "_"<< BinFuncName() <<"(";
3968 for (unsigned i = 0; i < vSubArguments.size(); i++)
3970 if (i)
3971 ss << ",";
3972 vSubArguments[i]->GenSlidingWindowDecl(ss);
3974 ss << ") {\n";
3975 ss << " double tmp = 0;\n";
3976 ss << " int gid0 = get_global_id(0);\n";
3977 ss << " int nSettle,nMat,nFreq,nBase;\n";
3978 #ifdef ISNAN
3979 FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
3980 FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
3981 FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
3982 FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
3983 #endif
3984 #ifdef ISNAN
3985 if(tmpCur0->GetType() == formula::svSingleVectorRef)
3987 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
3988 formula::SingleVectorRefToken *>(tmpCur0);
3989 ss <<" if(isNan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
3990 ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
3991 ss <<" nSettle = 0;\n else\n";
3993 #endif
3994 ss <<" nSettle=(int)";
3995 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
3996 ss <<";\n";
3997 #ifdef ISNAN
3998 if(tmpCur1->GetType() == formula::svSingleVectorRef)
4000 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
4001 formula::SingleVectorRefToken *>(tmpCur1);
4002 ss <<" if(isNan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
4003 ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
4004 ss <<" nMat = 0;\n else\n";
4006 #endif
4007 ss <<" nMat=(int)";
4008 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4009 ss <<";\n";
4010 #ifdef ISNAN
4011 if(tmpCur2->GetType() == formula::svSingleVectorRef)
4013 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
4014 formula::SingleVectorRefToken *>(tmpCur2);
4015 ss <<" if(isNan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
4016 ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
4017 ss <<" nFreq = 0;\n else\n";
4019 #endif
4020 ss << " nFreq=(int)";
4021 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
4022 ss <<";\n";
4023 #ifdef ISNAN
4024 if(tmpCur3->GetType() == formula::svSingleVectorRef)
4026 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
4027 formula::SingleVectorRefToken *>(tmpCur3);
4028 ss <<" if(isNan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
4029 ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
4030 ss <<" nBase = 0;\n else\n";
4032 #endif
4033 ss << " nBase=(int)";
4034 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
4035 ss << ";\n";
4036 ss <<" tmp = coupnum_new(nSettle,nMat,nFreq,nBase);\n";
4037 ss <<" return tmp;\n";
4038 ss << "}";
4040 void OpAmordegrc::BinInlineFun(std::set<std::string>& decls,
4041 std::set<std::string>& funs)
4043 decls.insert(nKorrValDecl); decls.insert(RoundDecl);
4044 decls.insert(IsLeapYearDecl);decls.insert(DaysInMonthDecl);
4045 decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
4046 decls.insert(GetNullDateDecl); decls.insert(GetYearFracDecl);
4047 funs.insert(Round);
4048 funs.insert(IsLeapYear);funs.insert(DaysInMonth);
4049 funs.insert(DaysToDate);funs.insert(DateToDays);
4050 funs.insert(GetNullDate);funs.insert(GetYearFrac);
4052 void OpAmordegrc::GenSlidingWindowFunction(std::stringstream &ss,
4053 const std::string sSymName, SubArguments &vSubArguments)
4055 ss << "\ndouble " << sSymName;
4056 ss << "_"<< BinFuncName() <<"(";
4057 for (unsigned i = 0; i < vSubArguments.size(); i++)
4059 if (i)
4060 ss << ",";
4061 vSubArguments[i]->GenSlidingWindowDecl(ss);
4063 ss << ") {\n ";
4064 ss << "int gid0 = get_global_id(0);\n";
4065 ss << " double tmp = " << GetBottom() <<";\n";
4066 ss << " double fCost,fRestVal,fPer,fRate;\n";
4067 ss << " int nDate,nFirstPer,nBase;\n";
4068 #ifdef ISNAN
4069 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
4070 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
4071 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
4072 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
4073 FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
4074 FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
4075 FormulaToken *tmpCur6 = vSubArguments[6]->GetFormulaToken();
4076 #endif
4077 #ifdef ISNAN
4078 if(tmpCur0->GetType() == formula::svSingleVectorRef)
4080 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
4081 formula::SingleVectorRefToken *>(tmpCur0);
4082 ss <<" if(isNan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
4083 ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
4084 ss <<" fCost = 0;\n else\n";
4086 #endif
4087 ss << " fCost=";
4088 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4089 ss <<";\n";
4090 #ifdef ISNAN
4091 if(tmpCur1->GetType() == formula::svSingleVectorRef)
4093 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
4094 formula::SingleVectorRefToken *>(tmpCur1);
4095 ss <<" if(isNan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
4096 ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
4097 ss <<" nDate = 0;\n else\n";
4099 #endif
4100 ss << " nDate=(int)";
4101 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4102 ss << ";\n";
4103 #ifdef ISNAN
4104 if(tmpCur2->GetType() == formula::svSingleVectorRef)
4106 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
4107 formula::SingleVectorRefToken *>(tmpCur2);
4108 ss <<" if(isNan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
4109 ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
4110 ss <<" nFirstPer = 0;\n else\n";
4112 #endif
4113 ss << " nFirstPer=(int)";
4114 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
4115 ss <<";\n";
4116 #ifdef ISNAN
4117 if(tmpCur3->GetType() == formula::svSingleVectorRef)
4119 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
4120 formula::SingleVectorRefToken *>(tmpCur3);
4121 ss <<" if(isNan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
4122 ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
4123 ss <<" fRestVal = 0;\n else\n";
4125 #endif
4126 ss << " fRestVal=";
4127 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
4128 ss << ";\n";
4129 #ifdef ISNAN
4130 if(tmpCur4->GetType() == formula::svSingleVectorRef)
4132 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
4133 formula::SingleVectorRefToken *>(tmpCur4);
4134 ss <<" if(isNan(" <<vSubArguments[4]->GenSlidingWindowDeclRef();
4135 ss <<")||(gid0 >="<<tmpCurDVR4->GetArrayLength()<<"))\n";
4136 ss <<" fPer = 0;\n else\n";
4138 #endif
4139 ss << " fPer = ";
4140 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
4141 ss <<";\n";
4142 #ifdef ISNAN
4143 if(tmpCur5->GetType() == formula::svSingleVectorRef)
4145 const formula::SingleVectorRefToken*tmpCurDVR5= dynamic_cast<const
4146 formula::SingleVectorRefToken *>(tmpCur5);
4147 ss <<" if(isNan(" <<vSubArguments[5]->GenSlidingWindowDeclRef();
4148 ss <<")||(gid0 >="<<tmpCurDVR5->GetArrayLength()<<"))\n";
4149 ss <<" fRate = 0;\n else\n";
4151 #endif
4152 ss << " fRate=";
4153 ss << vSubArguments[5]->GenSlidingWindowDeclRef();
4154 ss << ";\n";
4155 #ifdef ISNAN
4156 if(tmpCur6->GetType() == formula::svSingleVectorRef)
4158 const formula::SingleVectorRefToken*tmpCurDVR6= dynamic_cast<const
4159 formula::SingleVectorRefToken *>(tmpCur6);
4160 ss <<" if(isNan(" <<vSubArguments[6]->GenSlidingWindowDeclRef();
4161 ss <<")||(gid0 >="<<tmpCurDVR6->GetArrayLength()<<"))\n";
4162 ss <<" nBase = 0;\n else\n";
4164 #endif
4165 ss << " nBase = (int)";
4166 ss << vSubArguments[6]->GenSlidingWindowDeclRef();
4167 ss << ";\n";
4168 ss <<" uint nPer = convert_int( fPer );\n";
4169 ss <<" double fUsePer = 1.0 *pow( fRate,-1);\n";
4170 ss <<" double fAmorCoeff;\n";
4171 ss <<" if( fUsePer < 3.0 )\n";
4172 ss <<" fAmorCoeff = 1.0;\n";
4173 ss <<" else if( fUsePer < 5.0 )\n";
4174 ss <<" fAmorCoeff = 1.5;\n";
4175 ss <<" else if( fUsePer <= 6.0 )\n";
4176 ss <<" fAmorCoeff = 2.0;\n";
4177 ss <<" else\n";
4178 ss <<" fAmorCoeff = 2.5;\n";
4179 ss <<" fRate *= fAmorCoeff;\n";
4180 ss <<" tmp = Round( GetYearFrac( 693594,";
4181 ss <<"nDate, nFirstPer, nBase ) * fRate * fCost);\n";
4182 ss <<" fCost = fCost-tmp;\n";
4183 ss <<" double fRest = fCost - fRestVal;\n";
4184 ss <<" for( uint n = 0 ; n < nPer ; n++ )\n";
4185 ss <<" {\n";
4186 ss <<" tmp = Round( fRate * fCost);\n";
4187 ss <<" fRest -= tmp;\n";
4188 ss <<" if( fRest < 0.0 )\n";
4189 ss <<" {\n";
4190 ss <<" switch( nPer - n )\n";
4191 ss <<" {\n";
4192 ss <<" case 0:\n";
4193 ss <<" case 1:\n";
4194 ss <<" tmp = Round( fCost * 0.5);\n";
4195 ss <<" default:\n";
4196 ss <<" tmp = 0.0;\n";
4197 ss <<" }\n";
4198 ss <<" }\n";
4199 ss <<" fCost -= tmp;\n";
4200 ss <<" }\n";
4201 ss <<" return tmp;\n";
4202 ss <<"}";
4204 void OpAmorlinc::BinInlineFun(std::set<std::string>& decls,
4205 std::set<std::string>& funs)
4207 decls.insert(nKorrValDecl); decls.insert(RoundDecl);
4208 decls.insert(IsLeapYearDecl);decls.insert(DaysInMonthDecl);
4209 decls.insert(DaysToDateDecl); decls.insert(DateToDaysDecl);
4210 decls.insert(GetYearFracDecl);
4211 funs.insert(Round);
4212 funs.insert(IsLeapYear);funs.insert(DaysInMonth);
4213 funs.insert(DaysToDate);funs.insert(DateToDays);
4214 funs.insert(GetYearFrac);
4216 void OpAmorlinc::GenSlidingWindowFunction(std::stringstream &ss,
4217 const std::string sSymName, SubArguments &vSubArguments)
4219 ss << "\ndouble " << sSymName;
4220 ss << "_"<< BinFuncName() <<"(";
4221 for (unsigned i = 0; i < vSubArguments.size(); i++)
4223 if (i)
4224 ss << ",";
4225 vSubArguments[i]->GenSlidingWindowDecl(ss);
4227 ss << ") {\n";
4228 ss << " int gid0 = get_global_id(0);\n";
4229 ss << " double tmp = 0;\n";
4230 ss << " double fCost,fRestVal,fPer,fRate;\n";
4231 ss << " int nDate,nFirstPer,nBase;\n";
4232 #ifdef ISNAN
4233 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
4234 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
4235 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
4236 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
4237 FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
4238 FormulaToken *tmpCur5 = vSubArguments[5]->GetFormulaToken();
4239 FormulaToken *tmpCur6 = vSubArguments[6]->GetFormulaToken();
4240 #endif
4241 #ifdef ISNAN
4242 if(tmpCur0->GetType() == formula::svSingleVectorRef)
4244 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
4245 formula::SingleVectorRefToken *>(tmpCur0);
4246 ss <<" if(isNan("<<vSubArguments[0]->GenSlidingWindowDeclRef();
4247 ss <<")||(gid0 >="<<tmpCurDVR0->GetArrayLength()<<"))\n";
4248 ss <<" fCost = 0;\n else\n";
4250 #endif
4251 ss << " fCost=";
4252 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4253 ss <<";\n";
4254 #ifdef ISNAN
4255 if(tmpCur1->GetType() == formula::svSingleVectorRef)
4257 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
4258 formula::SingleVectorRefToken *>(tmpCur1);
4259 ss <<" if(isNan("<<vSubArguments[1]->GenSlidingWindowDeclRef();
4260 ss <<")||(gid0 >="<<tmpCurDVR1->GetArrayLength()<<"))\n";
4261 ss <<" nDate = 0;\n else\n";
4263 #endif
4264 ss << " nDate=(int)";
4265 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4266 ss << ";\n";
4267 #ifdef ISNAN
4268 if(tmpCur2->GetType() == formula::svSingleVectorRef)
4270 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
4271 formula::SingleVectorRefToken *>(tmpCur2);
4272 ss <<" if(isNan("<<vSubArguments[2]->GenSlidingWindowDeclRef();
4273 ss <<")||(gid0 >="<<tmpCurDVR2->GetArrayLength()<<"))\n";
4274 ss <<" nFirstPer = 0;\n else\n";
4276 #endif
4277 ss << " nFirstPer=(int)";
4278 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
4279 ss <<";\n";
4280 #ifdef ISNAN
4281 if(tmpCur3->GetType() == formula::svSingleVectorRef)
4283 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
4284 formula::SingleVectorRefToken *>(tmpCur3);
4285 ss <<" if(isNan(" <<vSubArguments[3]->GenSlidingWindowDeclRef();
4286 ss <<")||(gid0 >="<<tmpCurDVR3->GetArrayLength()<<"))\n";
4287 ss <<" fRestVal = 0;\n else\n";
4289 #endif
4290 ss << " fRestVal=";
4291 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
4292 ss << ";\n";
4293 #ifdef ISNAN
4294 if(tmpCur4->GetType() == formula::svSingleVectorRef)
4296 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
4297 formula::SingleVectorRefToken *>(tmpCur4);
4298 ss <<" if(isNan(" <<vSubArguments[4]->GenSlidingWindowDeclRef();
4299 ss <<")||(gid0 >="<<tmpCurDVR4->GetArrayLength()<<"))\n";
4300 ss <<" fPer = 0;\n else\n";
4302 #endif
4303 ss << " fPer = ";
4304 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
4305 ss <<";\n";
4306 #ifdef ISNAN
4307 if(tmpCur5->GetType() == formula::svSingleVectorRef)
4309 const formula::SingleVectorRefToken*tmpCurDVR5= dynamic_cast<const
4310 formula::SingleVectorRefToken *>(tmpCur5);
4311 ss <<" if(isNan(" <<vSubArguments[5]->GenSlidingWindowDeclRef();
4312 ss <<")||(gid0 >="<<tmpCurDVR5->GetArrayLength()<<"))\n";
4313 ss <<" fRate = 0;\n else\n";
4315 #endif
4316 ss << " fRate=";
4317 ss << vSubArguments[5]->GenSlidingWindowDeclRef();
4318 ss << ";\n";
4319 #ifdef ISNAN
4320 if(tmpCur6->GetType() == formula::svSingleVectorRef)
4322 const formula::SingleVectorRefToken*tmpCurDVR6= dynamic_cast<const
4323 formula::SingleVectorRefToken *>(tmpCur6);
4324 ss <<" if(isNan(" <<vSubArguments[6]->GenSlidingWindowDeclRef();
4325 ss <<")||(gid0 >="<<tmpCurDVR6->GetArrayLength()<<"))\n";
4326 ss <<" nBase = 0;\n else\n";
4328 #endif
4329 ss << " nBase = (int)";
4330 ss << vSubArguments[6]->GenSlidingWindowDeclRef();
4331 ss << ";\n";
4332 ss <<" int nPer = convert_int( fPer );\n";
4333 ss <<" double fOneRate = fCost * fRate;\n";
4334 ss <<" double fCostDelta = fCost - fRestVal;\n";
4335 ss <<" double f0Rate = GetYearFrac( 693594,";
4336 ss <<"nDate, nFirstPer, nBase )* fRate * fCost;\n";
4337 ss <<" int nNumOfFullPeriods = (int)";
4338 ss <<"( ( fCost - fRestVal - f0Rate) *pow(fOneRate,-1) );\n";
4339 ss <<" if( nPer == 0 )\n";
4340 ss <<" tmp = f0Rate;\n";
4341 ss <<" else if( nPer <= nNumOfFullPeriods )\n";
4342 ss <<" tmp = fOneRate;\n";
4343 ss <<" else if( nPer == nNumOfFullPeriods + 1 )\n";
4344 ss <<" tmp = fCostDelta - fOneRate * nNumOfFullPeriods - f0Rate;\n";
4345 ss <<" else\n";
4346 ss <<" tmp = 0.0;\n";
4347 ss <<" return tmp;\n";
4348 ss <<"}";
4350 void OpReceived::BinInlineFun(std::set<std::string>& decls,
4351 std::set<std::string>& funs)
4353 decls.insert(GetYearDiffDecl);decls.insert(GetDiffDateDecl);
4354 decls.insert(DaysToDateDecl);decls.insert(DaysInMonthDecl);
4355 decls.insert(GetNullDateDecl);decls.insert(IsLeapYearDecl);
4356 decls.insert(DateToDaysDecl);
4357 funs.insert(GetDiffDate);funs.insert(DaysToDate);
4358 funs.insert(DaysInMonth);funs.insert(GetNullDate);
4359 funs.insert(DateToDays);funs.insert(IsLeapYear);
4360 funs.insert(GetYearDiff);
4363 void OpReceived::GenSlidingWindowFunction(std::stringstream &ss,
4364 const std::string sSymName, SubArguments &vSubArguments)
4366 ss << "\ndouble " << sSymName;
4367 ss << "_"<< BinFuncName() <<"(";
4368 for (unsigned i = 0; i < vSubArguments.size(); i++)
4370 if (i)
4371 ss << ",";
4372 vSubArguments[i]->GenSlidingWindowDecl(ss);
4374 ss << ") {\n";
4375 ss << " double tmp = " << GetBottom() <<";\n";
4376 ss << " int gid0 = get_global_id(0);\n";
4377 ss << " int nSettle, nMat;\n";
4378 ss << " double fInvest,fDisc;\n";
4379 ss << " int rOB;\n";
4380 #ifdef ISNAN
4381 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
4382 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
4383 formula::SingleVectorRefToken *>(tmpCur0);
4384 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
4385 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
4386 formula::SingleVectorRefToken *>(tmpCur1);
4387 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
4388 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
4389 formula::SingleVectorRefToken *>(tmpCur2);
4390 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
4391 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
4392 formula::SingleVectorRefToken *>(tmpCur3);
4393 FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
4394 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
4395 formula::SingleVectorRefToken *>(tmpCur4);
4396 ss<< " int buffer_settle_len = ";
4397 ss<< tmpCurDVR0->GetArrayLength();
4398 ss << ";\n";
4399 ss<< " int buffer_mat_len = ";
4400 ss<< tmpCurDVR1->GetArrayLength();
4401 ss << ";\n";
4402 ss<< " int buffer_invest_len = ";
4403 ss<< tmpCurDVR2->GetArrayLength();
4404 ss << ";\n";
4405 ss<< " int buffer_disc_len = ";
4406 ss<< tmpCurDVR3->GetArrayLength();
4407 ss << ";\n";
4408 ss<< " int buffer_rob_len = ";
4409 ss<< tmpCurDVR4->GetArrayLength();
4410 ss << ";\n";
4411 #endif
4412 #ifdef ISNAN
4413 ss <<" if(gid0 >= buffer_settle_len || isNan(";
4414 ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
4415 ss <<"))\n";
4416 ss <<" nSettle = 0;\n\telse\n";
4417 #endif
4418 ss <<" nSettle = (int)"<<vSubArguments[0]->GenSlidingWindowDeclRef();
4419 ss <<";\n";
4420 #ifdef ISNAN
4421 ss <<" if(gid0 >= buffer_mat_len || isNan(";
4422 ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
4423 ss <<"))\n";
4424 ss <<" nMat = 0;\n\telse\n";
4425 #endif
4426 ss <<" nMat = (int)";
4427 ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
4428 ss <<";\n";
4429 #ifdef ISNAN
4430 ss <<" if(gid0 >= buffer_invest_len || isNan(";
4431 ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
4432 ss <<"))\n";
4433 ss <<" fInvest = 0;\n\telse\n";
4434 #endif
4435 ss <<" fInvest = "<<vSubArguments[2]->GenSlidingWindowDeclRef();
4436 ss <<";\n";
4437 #ifdef ISNAN
4438 ss <<" if(gid0 >= buffer_disc_len || isNan(";
4439 ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
4440 ss <<"))\n";
4441 ss <<" fDisc = 0;\n\telse\n";
4442 #endif
4443 ss <<" fDisc = "<<vSubArguments[3]->GenSlidingWindowDeclRef();
4444 ss <<";\n";
4445 #ifdef ISNAN
4446 ss <<" if(gid0 >= buffer_rob_len || isNan(";
4447 ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
4448 ss <<"))\n";
4449 ss <<" rOB = 0;\n\telse\n";
4450 #endif
4451 ss <<" rOB = (int)"<<vSubArguments[4]->GenSlidingWindowDeclRef();
4452 ss <<";\n";
4453 ss << " double tmpvalue = (1.0-(fDisc";
4454 ss <<" * GetYearDiff( GetNullDate()";
4455 ss <<",nSettle,nMat,rOB)));\n";
4456 ss << " tmp = fInvest*pow(tmpvalue,-1);\n";
4457 ss << " return tmp;\n";
4458 ss << "}";
4461 void OpYielddisc::BinInlineFun(std::set<std::string>& decls,
4462 std::set<std::string>& funs)
4464 decls.insert(GetYearFracDecl);decls.insert(GetNullDateDecl);
4465 decls.insert(DateToDaysDecl);decls.insert(DaysToDateDecl);
4466 decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
4468 funs.insert(GetYearFrac);funs.insert(GetNullDate);
4469 funs.insert(DateToDays);funs.insert(DaysToDate);
4470 funs.insert(DaysInMonth);funs.insert(IsLeapYear);
4472 void OpYielddisc::GenSlidingWindowFunction(
4473 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
4475 ss << "\ndouble " << sSymName;
4476 ss << "_"<< BinFuncName() <<"(";
4477 for (unsigned i = 0; i < vSubArguments.size(); i++)
4479 if (i)
4480 ss << ",";
4481 vSubArguments[i]->GenSlidingWindowDecl(ss);
4483 ss << ") {\n\t";
4484 ss << "double tmp = " <<"0"<<";\n\t";
4485 ss << "int gid0 = get_global_id(0);\n\t";
4486 ss << "double tmp000;\n\t";
4487 ss << "double tmp001;\n\t";
4488 ss << "double tmp002;\n\t";
4489 ss << "double tmp003;\n\t";
4490 ss << "double tmp004;\n\t";
4493 #ifdef ISNAN
4494 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
4495 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
4496 formula::SingleVectorRefToken *>(tmpCur0);
4498 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
4499 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
4500 formula::SingleVectorRefToken *>(tmpCur1);
4502 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
4503 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
4504 formula::SingleVectorRefToken *>(tmpCur2);
4506 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
4507 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
4508 formula::SingleVectorRefToken *>(tmpCur3);
4510 FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
4511 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
4512 formula::SingleVectorRefToken *>(tmpCur4);
4514 ss<< "int buffer_tmp000_len = ";
4515 ss<< tmpCurDVR0->GetArrayLength();
4516 ss << ";\n\t";
4518 ss<< "int buffer_tmp001_len = ";
4519 ss<< tmpCurDVR1->GetArrayLength();
4520 ss << ";\n\t";
4522 ss<< "int buffer_tmp002_len = ";
4523 ss<< tmpCurDVR2->GetArrayLength();
4524 ss << ";\n\t";
4526 ss<< "int buffer_tmp003_len = ";
4527 ss<< tmpCurDVR3->GetArrayLength();
4528 ss << ";\n\t";
4530 ss<< "int buffer_tmp004_len = ";
4531 ss<< tmpCurDVR4->GetArrayLength();
4532 ss << ";\n\t";
4534 #endif
4536 #ifdef ISNAN
4537 ss<<"if(gid0>=buffer_tmp000_len || isNan(";
4538 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4539 ss<<"))\n\t\t";
4540 ss<<"tmp000 = 0;\n\telse \n\t\t";
4541 #endif
4542 ss<<"tmp000 = ";
4543 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4544 ss<<";\n\t";
4546 #ifdef ISNAN
4547 ss<<"if(gid0>=buffer_tmp001_len || isNan(";
4548 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4549 ss<<"))\n\t\t";
4550 ss<<"tmp001 = 0;\n\telse \n\t\t";
4551 #endif
4552 ss<<"tmp001 = ";
4553 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4554 ss<<";\n\t";
4556 #ifdef ISNAN
4557 ss<<"if(gid0>=buffer_tmp002_len || isNan(";
4558 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
4559 ss<<"))\n\t\t";
4560 ss<<"tmp002 = 0;\n\telse \n\t\t";
4561 #endif
4562 ss<<"tmp002 = ";
4563 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
4564 ss<<";\n\t";
4566 #ifdef ISNAN
4567 ss<<"if(gid0>=buffer_tmp003_len || isNan(";
4568 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
4569 ss<<"))\n\t\t";
4570 ss<<"tmp003 = 0;\n\telse \n\t\t";
4571 #endif
4572 ss<<"tmp003 = ";
4573 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
4574 ss<<";\n\t";
4576 #ifdef ISNAN
4577 ss<<"if(gid0>=buffer_tmp004_len || isNan(";
4578 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
4579 ss<<"))\n\t\t";
4580 ss<<"tmp004 = 0;\n\telse \n\t\t";
4581 #endif
4582 ss<<"tmp004 = ";
4583 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
4584 ss<<";\n\t";
4586 ss<< "tmp = (tmp003/tmp002)-1;\n\t";
4587 ss << "tmp /= GetYearFrac( GetNullDate(),tmp000,tmp001,tmp004);\n\t";
4588 ss << "return tmp;\n";
4589 ss << "}";
4592 void OpTbillprice::BinInlineFun(std::set<std::string>& decls,
4593 std::set<std::string>& funs)
4595 decls.insert(GetYearFracDecl);
4596 decls.insert(DateToDaysDecl);decls.insert(DaysToDateDecl);
4597 decls.insert(DaysInMonthDecl);decls.insert(IsLeapYearDecl);
4599 funs.insert(GetYearFrac);
4600 funs.insert(DateToDays);funs.insert(DaysToDate);
4601 funs.insert(DaysInMonth);funs.insert(IsLeapYear);
4604 void OpTbillprice::GenSlidingWindowFunction(
4605 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
4607 ss << "\ndouble " << sSymName;
4608 ss << "_"<< BinFuncName() <<"(";
4609 for (unsigned i = 0; i < vSubArguments.size(); i++)
4611 if (i)
4612 ss << ",";
4613 vSubArguments[i]->GenSlidingWindowDecl(ss);
4615 ss << ") {\n";
4616 ss << " int gid0 = get_global_id(0);\n";
4617 ss << " double tmp = 0;\n";
4619 ss << " int singleIndex = gid0;\n";
4620 ss << " int doubleIndex = gid0;\n";
4621 ss << " int i = gid0;\n";
4622 GenTmpVariables(ss,vSubArguments);
4623 CheckAllSubArgumentIsNan(ss,vSubArguments);
4625 ss << " tmp1+=1.0;\n";
4626 ss << " double fFraction =GetYearFrac(693594,tmp0,tmp1,0);\n";
4627 ss << " tmp = 100.0 * ( 1.0 - tmp2 * fFraction );\n";
4628 ss << " return tmp;\n";
4629 ss << "}\n";
4631 void RATE::BinInlineFun(std::set<std::string>& decls,
4632 std::set<std::string>& funs)
4634 decls.insert(approxEqualDecl);decls.insert(nKorrValDecl);
4635 decls.insert(SCdEpsilonDecl);decls.insert(RoundDecl);
4636 funs.insert(approxEqual);funs.insert(Round);
4639 void RATE::GenSlidingWindowFunction(
4640 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
4642 ss << "\ndouble " << sSymName;
4643 ss << "_"<< BinFuncName() <<"(";
4644 for (unsigned i = 0; i < vSubArguments.size(); i++)
4646 if (i)
4647 ss << ",";
4648 vSubArguments[i]->GenSlidingWindowDecl(ss);
4651 FormulaToken* pCur = vSubArguments[5]->GetFormulaToken();
4652 assert(pCur);
4653 const formula::SingleVectorRefToken* pSVR =
4654 dynamic_cast< const formula::SingleVectorRefToken* >(pCur);
4655 assert(pSVR);
4656 ss << ") {\n";
4657 ss << " double result;\n";
4658 ss << " int gid0 = get_global_id(0);\n";
4659 ss << " bool bValid = true, bFound = false;\n";
4660 ss << " double fX, fXnew, fTerm, fTermDerivation;\n";
4661 ss << " double fGeoSeries, fGeoSeriesDerivation;\n";
4662 ss << " int nIterationsMax = 150;\n";
4663 ss << " int nCount = 0;\n";
4664 ss << " double fEpsilonSmall = 1.0E-14;\n";
4665 ss << " double arg0, arg1, arg2, arg3, arg4, arg5;\n";
4666 ss << " arg0=" << vSubArguments[0]->GenSlidingWindowDeclRef()<<";\n";
4667 ss << " arg1=" << vSubArguments[1]->GenSlidingWindowDeclRef()<<";\n";
4668 ss << " arg2=" << vSubArguments[2]->GenSlidingWindowDeclRef()<<";\n";
4669 ss << " arg3=" << vSubArguments[3]->GenSlidingWindowDeclRef()<<";\n";
4670 ss << " arg4=" << vSubArguments[4]->GenSlidingWindowDeclRef()<<";\n";
4671 ss << " arg5=" << vSubArguments[5]->GenSlidingWindowDeclRef()<<";\n";
4672 ss << " int guessLen = " << pSVR->GetArrayLength() << ";\n";
4673 #ifdef ISNAN
4674 ss << " if (isNan(arg0) || isNan(arg1) || isNan(arg2)){\n";
4675 ss << " result = 523;\n";
4676 ss << " return result;\n";
4677 ss << " }\n";
4678 ss << " if (isNan(arg3))\n";
4679 ss << " arg3 = 0.0;\n";
4680 ss << " if (isNan(arg4))\n";
4681 ss << " arg4 = 0.0;\n";
4682 ss << " if (isNan(arg5))\n";
4683 ss << " arg5 = 0.1;\n";
4684 ss << " if (gid0 >= guessLen)\n";
4685 ss << " arg5 = 0.1;\n";
4686 #endif
4687 ss << " arg3 = arg3 - arg1 * arg4;\n";
4688 ss << " arg2 = arg2 + arg1 * arg4;\n";
4689 ss << " if (arg0 == Round(arg0)){\n";
4690 ss << " fX = arg5;\n";
4691 ss << " double fPowN, fPowNminus1;\n";
4692 ss << " while (!bFound && nCount < nIterationsMax)\n";
4693 ss << " {\n";
4694 ss << " fPowNminus1 = pow( 1.0+fX, arg0-1.0);\n";
4695 ss << " fPowN = fPowNminus1 * (1.0+fX);\n";
4696 ss << " if (approxEqual( fabs(fX), 0.0))\n";
4697 ss << " {\n";
4698 ss << " fGeoSeries = arg0;\n";
4699 ss << " fGeoSeriesDerivation = arg0 * (arg0-1.0)";
4700 ss << "*pow(2.0,-1);\n";
4701 ss << " }\n";
4702 ss << " else\n";
4703 ss << " {";
4704 ss << " fGeoSeries = (fPowN-1.0)*pow(fX,-1);\n";
4705 ss << " fGeoSeriesDerivation =";
4706 ss << " arg0 * fPowNminus1 * pow( fX , -1) - fGeoSeries * pow(fX, -1);\n";
4707 ss << " }\n";
4708 ss << " fTerm = arg3 + arg2 *fPowN+ arg1 * fGeoSeries;\n";
4709 ss << " fTermDerivation = arg2 * arg0 * fPowNminus1 +";
4710 ss << "arg1 * fGeoSeriesDerivation;\n";
4711 ss << " if (fabs(fTerm) < fEpsilonSmall)\n";
4712 ss << " bFound = true;\n";
4713 ss << " else\n";
4714 ss << " {\n";
4715 ss << " if (approxEqual(fabs(fTermDerivation), 0.0))\n";
4716 ss << " fXnew = fX + 1.1 * SCdEpsilon;\n";
4717 ss << " else\n";
4718 ss << " fXnew = fX - fTerm ";
4719 ss << "*pow( fTermDerivation,-1);\n";
4720 ss << " nCount++;\n";
4721 ss << " bFound = (fabs(fXnew - fX) < SCdEpsilon);\n";
4722 ss << " fX = fXnew;\n";
4723 ss << " }\n";
4724 ss << " }\n";
4725 ss << " }\n";
4726 ss << " else\n";
4727 ss << " {";
4728 ss << " fX = (arg5 < -1.0) ? -1.0 : arg5;\n";
4729 ss << " while (bValid && !bFound && nCount < nIterationsMax)\n";
4730 ss << " {\n";
4731 ss << " if (approxEqual(fabs(fX), 0.0)){\n";
4732 ss << " fGeoSeries = arg0;\n";
4733 ss << " fGeoSeriesDerivation = arg0 * ";
4734 ss << "(arg0-1.0)* pow(2.0,-1);\n";
4735 ss << " }else{\n";
4736 ss << " fGeoSeries = (pow( 1.0+fX, arg0) - 1.0)";
4737 ss << " *pow( fX,-1);\n";
4738 ss << " fGeoSeriesDerivation =";
4739 ss << " arg0 * pow(1.0+fX,arg0-1.0) *pow(fX,-1)";
4740 ss << " - fGeoSeries *pow( fX,-1);\n";
4741 ss << " }\n";
4742 ss << " fTerm = arg3 + arg2 *pow(1.0+fX, arg0)";
4743 ss << "+ arg1 * fGeoSeries;\n";
4744 ss << " fTermDerivation =";
4745 ss << "arg2*arg0*pow(1.0+fX,arg0-1.0)";
4746 ss << "+arg1*fGeoSeriesDerivation;\n";
4747 ss << " if (fabs(fTerm) < fEpsilonSmall)\n";
4748 ss << " bFound = true;\n";
4749 ss << " else{\n";
4750 ss << " if (approxEqual(fabs(fTermDerivation), 0.0))\n";
4751 ss << " fXnew = fX + 1.1 * SCdEpsilon;\n";
4752 ss << " else\n";
4753 ss << " fXnew = fX - fTerm ";
4754 ss << "*pow( fTermDerivation,-1);\n";
4755 ss << " nCount++;\n";
4756 ss << " bFound = (fabs(fXnew - fX) < SCdEpsilon);\n";
4757 ss << " fX = fXnew;\n";
4758 ss << " bValid = (fX >= -1.0);\n";
4759 ss << " }\n";
4760 ss << " }\n";
4761 ss << " }\n";
4762 ss << " if (bValid && bFound)\n";
4763 ss << " result = fX;\n";
4764 ss << " else\n";
4765 ss << " result = 523;\n";
4766 ss << " return result;\n";
4767 ss << "}";
4770 void OpTbillyield::BinInlineFun(std::set<std::string>& decls,
4771 std::set<std::string>& funs)
4773 decls.insert(GetDiffDate360Decl);decls.insert(IsLeapYearDecl);
4774 decls.insert(DateToDaysDecl);decls.insert(DaysToDate_LocalBarrierDecl);
4775 decls.insert(DaysInMonthDecl);decls.insert(GetNullDateDecl);
4776 decls.insert(GetDiffDate360_Decl);
4777 funs.insert(GetDiffDate360);funs.insert(DateToDays);
4778 funs.insert(DaysToDate_LocalBarrier);funs.insert(IsLeapYear);
4779 funs.insert(DaysInMonth);funs.insert(GetNullDate);
4780 funs.insert(GetDiffDate360_);
4784 void OpTbillyield::GenSlidingWindowFunction(
4785 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
4787 ss << "\ndouble " << sSymName;
4788 ss << "_"<< BinFuncName() <<"(";
4789 for (unsigned i = 0; i < vSubArguments.size(); i++)
4791 if (i)
4792 ss << ",";
4793 vSubArguments[i]->GenSlidingWindowDecl(ss);
4795 ss << ") {\n\t";
4796 ss << "int gid0 = get_global_id(0);\n\t";
4797 ss << "double tmp = 0;\n\t";
4798 ss << "double tmp000;\n\t";
4799 ss << "double tmp001;\n\t";
4800 ss << "double tmp002;\n\t";
4804 #ifdef ISNAN
4805 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
4806 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
4807 formula::SingleVectorRefToken *>(tmpCur0);
4809 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
4810 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
4811 formula::SingleVectorRefToken *>(tmpCur1);
4813 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
4814 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
4815 formula::SingleVectorRefToken *>(tmpCur2);
4817 ss<< "int buffer_tmp000_len = ";
4818 ss<< tmpCurDVR0->GetArrayLength();
4819 ss << ";\n\t";
4821 ss<< "int buffer_tmp001_len = ";
4822 ss<< tmpCurDVR1->GetArrayLength();
4823 ss << ";\n\t";
4825 ss<< "int buffer_tmp002_len = ";
4826 ss<< tmpCurDVR2->GetArrayLength();
4827 ss << ";\n\t";
4828 #endif
4830 #ifdef ISNAN
4831 ss<<"if(gid0>=buffer_tmp000_len || isNan(";
4832 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4833 ss<<"))\n\t\t";
4834 ss<<"tmp000 = 0;\n\telse \n\t\t";
4835 #endif
4836 ss<<"tmp000 = ";
4837 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
4838 ss<<";\n\t";
4840 #ifdef ISNAN
4841 ss<<"if(gid0>=buffer_tmp001_len || isNan(";
4842 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4843 ss<<"))\n\t\t";
4844 ss<<"tmp001 = 0;\n\telse \n\t\t";
4845 #endif
4846 ss<<"tmp001 = ";
4847 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
4848 ss<<";\n\t";
4850 #ifdef ISNAN
4851 ss<<"if(gid0>=buffer_tmp002_len || isNan(";
4852 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
4853 ss<<"))\n\t\t";
4854 ss<<"tmp002 = 0;\n\telse \n\t\t";
4855 #endif
4856 ss<<"tmp002 = ";
4857 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
4858 ss<<";\n\t";
4859 ss <<" int nDiff=GetDiffDate360(GetNullDate(),tmp000,tmp001,true);\n";
4860 ss <<" nDiff++;\n";
4861 ss <<" tmp=100.0;\n";
4862 ss <<" tmp = tmp *pow( tmp002,-1);\n";
4863 ss <<" tmp = tmp - 1.0;\n";
4864 ss <<" tmp = tmp * pow( nDiff,-1.0 );\n";
4865 ss <<" tmp = tmp * 360.0;\n";
4866 ss <<" return tmp;\n";
4867 ss << "}\n";
4869 void OpDDB::GenSlidingWindowFunction(std::stringstream& ss,
4870 const std::string sSymName, SubArguments& vSubArguments)
4872 ss << "\ndouble " << sSymName;
4873 ss << "_"<< BinFuncName() <<"(";
4874 for (unsigned i = 0; i < vSubArguments.size(); i++)
4876 if (i)
4877 ss << ",";
4878 vSubArguments[i]->GenSlidingWindowDecl(ss);
4880 ss << ") {\n";
4881 ss << " int gid0 = get_global_id(0);\n";
4882 ss << " double tmp = 0;\n";
4883 ss << " double fWert,fRest,fDauer,fPeriode,fFaktor;\n";
4884 ss << " double fZins, fAlterWert, fNeuerWert;\n";
4886 #ifdef ISNAN
4887 FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
4888 FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
4889 FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
4890 FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
4891 FormulaToken* tmpCur4 = vSubArguments[4]->GetFormulaToken();
4892 #endif
4893 #ifdef ISNAN
4894 if(tmpCur0->GetType() == formula::svSingleVectorRef)
4896 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
4897 formula::SingleVectorRefToken *>(tmpCur0);
4898 ss <<" if(gid0 >= "<<tmpCurDVR0->GetArrayLength()<<" || isNan(";
4899 ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
4900 ss <<"))\n";
4901 ss <<" fWert = 0;\n else\n";
4903 #endif
4904 ss <<" fWert = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
4905 ss <<";\n";
4906 #ifdef ISNAN
4907 if(tmpCur1->GetType() == formula::svSingleVectorRef)
4909 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
4910 formula::SingleVectorRefToken *>(tmpCur1);
4911 ss <<" if(gid0 >= "<<tmpCurDVR1->GetArrayLength()<<" || isNan(";
4912 ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
4913 ss <<"))\n";
4914 ss <<" fRest = 0;\n else\n";
4916 #endif
4917 ss <<" fRest = ";
4918 ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
4919 ss <<";\n";
4920 #ifdef ISNAN
4921 if(tmpCur2->GetType() == formula::svSingleVectorRef)
4923 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
4924 formula::SingleVectorRefToken *>(tmpCur2);
4925 ss <<" if(gid0 >= "<<tmpCurDVR2->GetArrayLength()<<" || isNan(";
4926 ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
4927 ss <<"))\n";
4928 ss <<" fDauer = 0;\n else\n";
4930 #endif
4931 ss <<" fDauer = ";
4932 ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
4933 ss <<";\n";
4934 #ifdef ISNAN
4935 if(tmpCur3->GetType() == formula::svSingleVectorRef)
4937 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
4938 formula::SingleVectorRefToken *>(tmpCur3);
4939 ss <<" if(gid0 >= "<<tmpCurDVR3->GetArrayLength()<<" || isNan(";
4940 ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
4941 ss <<"))\n";
4942 ss <<" fPeriode = 0;\n else\n";
4944 #endif
4945 ss <<" fPeriode = "<<vSubArguments[3]->GenSlidingWindowDeclRef();
4946 ss <<";\n";
4947 #ifdef ISNAN
4948 if(tmpCur4->GetType() == formula::svSingleVectorRef)
4950 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
4951 formula::SingleVectorRefToken *>(tmpCur4);
4952 ss <<" if(gid0 >= "<<tmpCurDVR4->GetArrayLength()<<" || isNan(";
4953 ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
4954 ss <<"))\n";
4955 ss <<" fFaktor = 0;\n else\n";
4957 #endif
4958 ss <<" fFaktor = "<<vSubArguments[4]->GenSlidingWindowDeclRef();
4959 ss <<";\n";
4960 ss <<" fZins = fFaktor * pow(fDauer,-1);\n";
4961 ss <<" if (fZins >= 1.0)\n";
4962 ss <<" {\n";
4963 ss <<" fZins = 1.0;\n";
4964 ss <<" if (fPeriode == 1.0)\n";
4965 ss <<" fAlterWert = fWert;\n";
4966 ss <<" else\n";
4967 ss <<" fAlterWert = 0.0;\n";
4968 ss <<" }\n";
4969 ss <<" else\n";
4970 ss <<" fAlterWert = fWert * pow(1.0 - fZins, fPeriode - 1);\n";
4971 ss <<" fNeuerWert = fWert * pow(1.0 - fZins, fPeriode);\n";
4972 ss <<" if (fNeuerWert < fRest)\n";
4973 ss <<" tmp = fAlterWert - fRest;\n";
4974 ss <<" else\n";
4975 ss <<" tmp = fAlterWert - fNeuerWert;\n";
4976 ss <<" if (tmp < 0.0)\n";
4977 ss <<" tmp = 0.0;\n";
4978 ss <<" return tmp;\n";
4979 ss <<"}";
4981 void OpPV::GenSlidingWindowFunction(
4982 std::stringstream &ss, const std::string sSymName, SubArguments &
4983 vSubArguments)
4985 ss << "\ndouble " << sSymName;
4986 ss << "_"<< BinFuncName() <<"(";
4987 for (unsigned i = 0; i < vSubArguments.size(); i++)
4989 if (i)
4990 ss << ",";
4991 vSubArguments[i]->GenSlidingWindowDecl(ss);
4993 ss << ") {\n";
4994 ss << " double result = " <<"0"<<";\n";
4995 ss << " int gid0 = get_global_id(0);\n";
4996 ss << " double zins;\n";
4997 ss << " double zzr;\n";
4998 ss << " double rmz;\n";
4999 ss << " double zw;\n";
5000 ss << " double flag;\n";
5002 #ifdef ISNAN
5003 FormulaToken *tmpCur0 = vSubArguments[0]->GetFormulaToken();
5004 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
5005 formula::SingleVectorRefToken *>(tmpCur0);
5007 FormulaToken *tmpCur1 = vSubArguments[1]->GetFormulaToken();
5008 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
5009 formula::SingleVectorRefToken *>(tmpCur1);
5011 FormulaToken *tmpCur2 = vSubArguments[2]->GetFormulaToken();
5012 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
5013 formula::SingleVectorRefToken *>(tmpCur2);
5015 const formula::SingleVectorRefToken*tmpCurDVR3;
5016 const formula::SingleVectorRefToken*tmpCurDVR4;
5018 if(vSubArguments.size()>3)
5020 FormulaToken *tmpCur3 = vSubArguments[3]->GetFormulaToken();
5021 tmpCurDVR3= dynamic_cast<const formula::SingleVectorRefToken *>(
5022 tmpCur3);
5023 ss<< " int buffer_zw_len = ";
5024 ss<< tmpCurDVR3->GetArrayLength();
5025 ss << ";\n";
5028 if(vSubArguments.size()>4)
5030 FormulaToken *tmpCur4 = vSubArguments[4]->GetFormulaToken();
5031 tmpCurDVR4= dynamic_cast<const formula::SingleVectorRefToken *>(
5032 tmpCur4);
5033 ss<< " int buffer_flag_len = ";
5034 ss<< tmpCurDVR4->GetArrayLength();
5035 ss << ";\n";
5038 ss<< " int buffer_zins_len = ";
5039 ss<< tmpCurDVR0->GetArrayLength();
5040 ss << ";\n";
5042 ss<< " int buffer_zzr_len = ";
5043 ss<< tmpCurDVR1->GetArrayLength();
5044 ss << ";\n";
5046 ss<< " int buffer_rmz_len = ";
5047 ss<< tmpCurDVR2->GetArrayLength();
5048 ss << ";\n";
5050 #endif
5052 #ifdef ISNAN
5053 ss<<" if(gid0>=buffer_zins_len || isNan(";
5054 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
5055 ss<<"))\n";
5056 ss<<" zins = 0;\n else \n";
5057 #endif
5058 ss<<" zins = ";
5059 ss << vSubArguments[0]->GenSlidingWindowDeclRef();
5060 ss<<";\n";
5062 #ifdef ISNAN
5063 ss<<" if(gid0>=buffer_zzr_len || isNan(";
5064 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
5065 ss<<"))\n";
5066 ss<<" zzr = 0;\n else \n";
5067 #endif
5068 ss<<" zzr = ";
5069 ss << vSubArguments[1]->GenSlidingWindowDeclRef();
5070 ss<<";\n";
5072 #ifdef ISNAN
5073 ss<<" if(gid0>=buffer_rmz_len || isNan(";
5074 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
5075 ss<<"))\n";
5076 ss<<" rmz = 0;\n else \n";
5077 #endif
5078 ss<<" rmz = ";
5079 ss << vSubArguments[2]->GenSlidingWindowDeclRef();
5080 ss<<";\n";
5082 if(vSubArguments.size()>3)
5084 #ifdef ISNAN
5085 ss<<" if(gid0>=buffer_zw_len || isNan(";
5086 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
5087 ss<<"))\n";
5088 ss<<" zw = 0;\n else \n";
5089 #endif
5090 ss<<" zw = ";
5091 ss << vSubArguments[3]->GenSlidingWindowDeclRef();
5092 ss<<";\n";
5093 }else
5095 ss<<" zw = 0;\n";
5098 if(vSubArguments.size()>4)
5100 #ifdef ISNAN
5101 ss<<" if(gid0>=buffer_flag_len || isNan(";
5102 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
5103 ss<<"))\n";
5104 ss<<" flag = 0;\n else \n";
5105 #endif
5106 ss<<" flag = ";
5107 ss << vSubArguments[4]->GenSlidingWindowDeclRef();
5108 ss<<";\n";
5109 }else
5111 ss<<" flag = 0;\n";
5113 ss << " if(zins == 0)\n";
5114 ss << " result=zw+rmz*zzr;\n";
5115 ss << " else if(flag > 0)\n";
5116 ss << " result=(zw*pow(1+zins,-zzr))+";
5117 ss << "(rmz*(1-pow(1+zins,-zzr+1))*pow(zins,-1))+rmz;\n";
5118 ss << " else\n";
5119 ss << " result=(zw*pow(1+zins,-zzr))+";
5120 ss << "(rmz*(1-pow(1+zins,-zzr))*pow(zins,-1));\n";
5121 ss << " return -result;\n";
5122 ss << "}";
5124 void OpVDB::BinInlineFun(std::set<std::string>& decls,
5125 std::set<std::string>& funs)
5127 decls.insert(ScGetGDADecl);decls.insert(DblMinDecl);
5128 decls.insert(ScInterVDBDecl);decls.insert(VDBImplementDecl);
5129 funs.insert(ScGetGDA);funs.insert(DblMin);
5130 funs.insert(ScInterVDB);funs.insert(VDBImplement);
5133 void OpVDB::GenSlidingWindowFunction(
5134 std::stringstream &ss, const std::string sSymName, SubArguments &vSubArguments)
5136 ss << "\ndouble " << sSymName;
5137 ss << "_"<< BinFuncName() <<"(";
5138 for (unsigned i = 0; i < vSubArguments.size(); i++)
5140 if (i)
5141 ss << ",";
5142 vSubArguments[i]->GenSlidingWindowDecl(ss);
5144 ss << ") {\n";
5145 ss << " int gid0 = get_global_id(0);\n";
5146 ss << " int singleIndex = gid0;\n";
5147 ss << " double result = 0;\n";
5148 if(vSubArguments.size()<5)
5150 ss << " result = -DBL_MAX;\n";
5151 ss << " return result;\n";
5152 }else
5154 GenTmpVariables(ss,vSubArguments);
5155 CheckAllSubArgumentIsNan(ss,vSubArguments);
5156 if(vSubArguments.size() <= 6)
5158 ss << " int tmp6 = 0;\n";
5160 if(vSubArguments.size() == 5)
5162 ss << " double tmp5= 2.0;\n";
5164 ss << " if(tmp3 < 0 || tmp4<tmp3 || tmp4>tmp2 || tmp0<0 ||tmp1>tmp0";
5165 ss << "|| tmp5 <=0)\n";
5166 ss << " result = -DBL_MAX;\n";
5167 ss << " else\n";
5168 ss << " result =";
5169 ss << "VDBImplement(tmp0,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6);\n";
5170 ss << " return result;\n";
5171 ss << "}";
5176 void OpXirr::GenSlidingWindowFunction(std::stringstream &ss,
5177 const std::string sSymName, SubArguments &vSubArguments)
5179 FormulaToken *tmpCur = vSubArguments[0]->GetFormulaToken();
5180 const formula::DoubleVectorRefToken*pCurDVR= dynamic_cast<const
5181 formula::DoubleVectorRefToken *>(tmpCur);
5182 size_t nCurWindowSize = pCurDVR->GetArrayLength() <
5183 pCurDVR->GetRefRowSize() ? pCurDVR->GetArrayLength():
5184 pCurDVR->GetRefRowSize() ;
5185 ss << "\ndouble " << sSymName;
5186 ss << "_"<< BinFuncName() <<"(";
5187 for (unsigned i = 0; i < vSubArguments.size(); i++)
5189 if (i)
5190 ss << ",";
5191 vSubArguments[i]->GenSlidingWindowDecl(ss);
5193 ss << ") {\n";
5194 ss << " int gid0 = get_global_id(0);\n";
5195 ss << " int doubleIndex = gid0;\n";
5196 ss << " int singleIndex = gid0;\n";
5197 ss << " double result = 0;\n";
5198 ss << " int i=0;\n";
5199 if(vSubArguments.size()<2)
5201 ss << " result = -DBL_MAX;\n";
5202 ss << " return result;\n";
5203 }else
5205 GenTmpVariables(ss,vSubArguments);
5206 if(vSubArguments.size() == 2)
5208 ss << " double tmp2 = 0.1;\n";
5209 }else
5211 CheckSubArgumentIsNan(ss,vSubArguments,2);
5213 ss << " if(tmp2<=-1)\n";
5214 ss << " result = -DBL_MAX;\n";
5215 ss << " else\n";
5216 ss << " {\n";
5217 ss << " double fMaxEps = 1e-10;\n";
5218 ss << " int nMaxIter = 50;\n";
5219 ss << " double fNewRate, fRateEps, fResultValue, fResultValue2;\n";
5220 ss << " int nIter = 0;\n";
5221 ss << " int bContLoop;\n";
5222 ss << " int windowsSize = ";
5223 ss << nCurWindowSize;
5224 ss << ";\n";
5225 CheckSubArgumentIsNan(ss,vSubArguments,0);
5226 CheckSubArgumentIsNan(ss,vSubArguments,1);
5227 ss << " double D_0 = tmp1;\n";
5228 ss << " double V_0 = tmp0;\n";
5229 ss << " double fResultRate = tmp2;\n";
5230 ss << " double r;\n";
5231 ss << " double fResult;\n";
5232 ss << " do\n";
5233 ss << " {\n";
5234 ss << " fResultValue = V_0;\n";
5235 ss << " r = fResultRate + 1;\n";
5236 ss << " for (i = ";
5237 if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
5238 ss << "gid0+1; i < "<< nCurWindowSize <<"; i++)\n";
5239 } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
5240 ss << "1; i < gid0+"<< nCurWindowSize <<"; i++)\n";
5241 } else {
5242 ss << "1; i < "<< nCurWindowSize <<"; i++)\n";
5244 ss << " {\n";
5245 if(!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
5247 ss<< " doubleIndex =i+gid0;\n";
5248 }else
5250 ss<< " doubleIndex =i;\n";
5252 CheckSubArgumentIsNan(ss,vSubArguments,0);
5253 CheckSubArgumentIsNan(ss,vSubArguments,1);
5254 ss << " fResultValue += tmp0/pow(r,(tmp1 - D_0)/365.0);\n";
5255 ss << " }\n";
5256 ss << " fResultValue2 = 0;\n";
5258 ss << " for (i = ";
5259 if (!pCurDVR->IsStartFixed() && pCurDVR->IsEndFixed()) {
5260 ss << "gid0+1; i < "<< nCurWindowSize <<"; i++)\n";
5261 } else if (pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed()) {
5262 ss << "1; i < gid0+"<< nCurWindowSize <<"; i++)\n";
5263 } else {
5264 ss << "1; i < "<< nCurWindowSize <<"; i++)\n";
5266 ss << " {\n";
5267 if(!pCurDVR->IsStartFixed() && !pCurDVR->IsEndFixed())
5269 ss<< " doubleIndex =i+gid0;\n";
5270 }else
5272 ss<< " doubleIndex =i;\n";
5274 CheckSubArgumentIsNan(ss,vSubArguments,0);
5275 CheckSubArgumentIsNan(ss,vSubArguments,1);
5276 ss << " double E_i = (tmp1 - D_0)/365.0;\n";
5277 ss << " fResultValue2 -= E_i * tmp0 / pow(r,E_i + 1.0);\n";
5278 ss << " }\n";
5279 ss << " fNewRate = fResultRate - fResultValue / fResultValue2;\n";
5280 ss << " fRateEps = fabs( fNewRate - fResultRate );\n";
5281 ss << " fResultRate = fNewRate;\n";
5282 ss << " bContLoop = (fRateEps > fMaxEps) && (fabs( fResultValue ) > fMaxEps);\n";
5283 ss << " }\n";
5284 ss << " while( bContLoop && (++nIter < nMaxIter) );\n";
5285 ss << " if( bContLoop )\n";
5286 ss << " result = -DBL_MAX;\n";
5287 ss << " result = fResultRate;\n";
5288 ss << " }\n";
5289 ss << " return result;\n";
5290 ss << "}";
5294 void OpDB::GenSlidingWindowFunction(std::stringstream& ss,
5295 const std::string sSymName, SubArguments& vSubArguments)
5297 ss << "\ndouble " << sSymName;
5298 ss << "_"<< BinFuncName() <<"(";
5299 for (unsigned i = 0; i < vSubArguments.size(); i++)
5301 if (i)
5302 ss << ",";
5303 vSubArguments[i]->GenSlidingWindowDecl(ss);
5305 ss << ") {\n";
5306 ss << " int gid0 = get_global_id(0);\n";
5307 ss << " double nWert,nRest,nDauer,nPeriode;\n";
5308 ss << " int nMonate;\n";
5309 ss << " double tmp = 0;\n";
5310 #ifdef ISNAN
5311 FormulaToken* tmpCur0 = vSubArguments[0]->GetFormulaToken();
5312 const formula::SingleVectorRefToken*tmpCurDVR0= dynamic_cast<const
5313 formula::SingleVectorRefToken *>(tmpCur0);
5314 FormulaToken* tmpCur1 = vSubArguments[1]->GetFormulaToken();
5315 const formula::SingleVectorRefToken*tmpCurDVR1= dynamic_cast<const
5316 formula::SingleVectorRefToken *>(tmpCur1);
5317 FormulaToken* tmpCur2 = vSubArguments[2]->GetFormulaToken();
5318 const formula::SingleVectorRefToken*tmpCurDVR2= dynamic_cast<const
5319 formula::SingleVectorRefToken *>(tmpCur2);
5320 FormulaToken* tmpCur3 = vSubArguments[3]->GetFormulaToken();
5321 const formula::SingleVectorRefToken*tmpCurDVR3= dynamic_cast<const
5322 formula::SingleVectorRefToken *>(tmpCur3);
5323 FormulaToken* tmpCur4 = vSubArguments[4]->GetFormulaToken();
5324 const formula::SingleVectorRefToken*tmpCurDVR4= dynamic_cast<const
5325 formula::SingleVectorRefToken *>(tmpCur4);
5326 ss<< " int buffer_wert_len = ";
5327 ss<< tmpCurDVR0->GetArrayLength();
5328 ss << ";\n";
5329 ss<< " int buffer_rest_len = ";
5330 ss<< tmpCurDVR1->GetArrayLength();
5331 ss << ";\n";
5332 ss<< " int buffer_dauer_len = ";
5333 ss<< tmpCurDVR2->GetArrayLength();
5334 ss << ";\n";
5335 ss<< " int buffer_periode_len = ";
5336 ss<< tmpCurDVR3->GetArrayLength();
5337 ss << ";\n";
5338 ss<< " int buffer_nMonate_len = ";
5339 ss<< tmpCurDVR4->GetArrayLength();
5340 ss << ";\n";
5341 #endif
5342 #ifdef ISNAN
5343 ss <<" if(gid0 >= buffer_wert_len || isNan(";
5344 ss <<vSubArguments[0]->GenSlidingWindowDeclRef();
5345 ss <<"))\n";
5346 ss <<" nWert = 0;\n else\n";
5347 #endif
5348 ss <<" nWert = "<<vSubArguments[0]->GenSlidingWindowDeclRef();
5349 ss <<";\n";
5350 #ifdef ISNAN
5351 ss <<" if(gid0 >= buffer_rest_len || isNan(";
5352 ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
5353 ss <<"))\n";
5354 ss <<" nRest = 0;\n else\n";
5355 #endif
5356 ss <<" nRest = ";
5357 ss <<vSubArguments[1]->GenSlidingWindowDeclRef();
5358 ss <<";\n";
5359 #ifdef ISNAN
5360 ss <<" if(gid0 >= buffer_dauer_len || isNan(";
5361 ss <<vSubArguments[2]->GenSlidingWindowDeclRef();
5362 ss <<"))\n";
5363 ss <<" nDauer = 0;\n else\n";
5364 #endif
5365 ss <<" nDauer = "<<vSubArguments[2]->GenSlidingWindowDeclRef();
5366 ss <<";\n";
5367 #ifdef ISNAN
5368 ss <<" if(gid0 >= buffer_periode_len || isNan(";
5369 ss <<vSubArguments[3]->GenSlidingWindowDeclRef();
5370 ss <<"))\n";
5371 ss <<" nPeriode = 0;\n else\n";
5372 #endif
5373 ss <<" nPeriode = "<<vSubArguments[3]->GenSlidingWindowDeclRef();
5374 ss <<";\n";
5375 #ifdef ISNAN
5376 ss <<" if(gid0 >= buffer_nMonate_len || isNan(";
5377 ss <<vSubArguments[4]->GenSlidingWindowDeclRef();
5378 ss <<"))\n";
5379 ss <<" nMonate = 0;\n else\n";
5380 #endif
5381 ss <<" nMonate = (int)"<<vSubArguments[4]->GenSlidingWindowDeclRef();
5382 ss <<";\n";
5383 ss <<" double nAbRate = 1.0 - pow(nRest / nWert, 1.0 / nDauer);\n";
5384 ss <<" nAbRate = ((int)(nAbRate * 1000.0 + 0.5)) / 1000.0;\n";
5385 ss <<" double nErsteAbRate = nWert * nAbRate * nMonate / 12.0;\n";
5386 ss <<" double nGda2 = 0.0;\n";
5387 ss <<" if ((int)(nPeriode) == 1)\n";
5388 ss <<" nGda2 = nErsteAbRate;\n";
5389 ss <<" else\n";
5390 ss <<" {\n";
5391 ss <<" double nSummAbRate = nErsteAbRate;\n";
5392 ss <<" double nMin = nDauer;\n";
5393 ss <<" if (nMin > nPeriode) nMin = nPeriode;\n";
5394 ss <<" int iMax = (int)nMin;\n";
5395 ss <<" for (int i = 2; i <= iMax; i++)\n";
5396 ss <<" {\n";
5397 ss <<" nGda2 = (nWert - nSummAbRate) * nAbRate;\n";
5398 ss <<" nSummAbRate += nGda2;\n";
5399 ss <<" }\n";
5400 ss <<" if (nPeriode > nDauer)\n";
5401 ss <<" nGda2 = ((nWert - nSummAbRate)";
5402 ss <<"* nAbRate * (12.0 - nMonate)) / 12.0;\n";
5403 ss <<" }\n";
5404 ss <<" tmp = nGda2;\n";
5405 ss <<" return tmp;\n";
5406 ss <<"}";
5410 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */