LibreOffice Module sc (master) 1
op_addin.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#include "op_addin.hxx"
11
13#include <sstream>
14
15using namespace formula;
16
17namespace sc::opencl {
18
20 const std::string &sSymName, SubArguments &vSubArguments)
21{
23 GenerateFunctionDeclaration( sSymName, vSubArguments, ss );
24 ss << "{\n";
25 ss << " int gid0 = get_global_id(0);\n";
26 GenerateArg( "x", 0, vSubArguments, ss );
27 GenerateArg( "N", 1, vSubArguments, ss );
28 ss << " double f_2_DIV_PI = 2.0 / M_PI;\n";
29 ss << " if( N < 0.0 )\n";
30 ss << " return CreateDoubleError(IllegalArgument);\n";
31 ss << " if (x == 0.0)\n";
32 ss << " return (N == 0.0) ? 1.0 : 0.0;\n";
33 ss << " double fSign = ((int)N % 2 == 1 && x < 0.0) ? -1.0 : 1.0;\n";
34 ss << " double fX = fabs(x);\n";
35 ss << " double fMaxIteration = 9000000.0;\n";
36 ss << " double fEstimateIteration = fX * 1.5 + N;\n";
37 ss << " bool bAsymptoticPossible = pow(fX,0.4) > N;\n";
38 ss << " if (fEstimateIteration > fMaxIteration)\n";
39 ss << " {\n";
40 ss << " if (bAsymptoticPossible)\n";
41 ss << " return fSign * sqrt(f_2_DIV_PI/fX)";
42 ss << "* cos(fX-N*M_PI_2-M_PI_4);\n";
43 ss << " else\n";
44 ss << " return CreateDoubleError(NoConvergence);\n";
45 ss << " }\n";
46 ss << " double epsilon = 1.0e-15;\n";
47 ss << " bool bHasfound = false;\n";
48 ss << " double k= 0.0;\n";
49 ss << " double u ;\n";
50 ss << " double m_bar;\n";
51 ss << " double g_bar;\n";
52 ss << " double g_bar_delta_u;\n";
53 ss << " double g = 0.0;\n";
54 ss << " double delta_u = 0.0;\n";
55 ss << " double f_bar = -1.0;\n";
56 ss << " if (N==0)\n";
57 ss << " {\n";
58 ss << " u = 1.0;\n";
59 ss << " g_bar_delta_u = 0.0;\n";
60 ss << " g_bar = - 2.0/fX; \n";
61 ss << " delta_u = g_bar_delta_u / g_bar;\n";
62 ss << " u = u + delta_u ;\n";
63 ss << " g = -1.0 / g_bar; \n";
64 ss << " f_bar = f_bar * g;\n";
65 ss << " k = 2.0;\n";
66 ss << " }\n";
67 ss << " if (N!=0)\n";
68 ss << " {\n";
69 ss << " u=0.0;\n";
70 ss << " for (k =1.0; k<= N-1; k = k + 1.0)\n";
71 ss << " {\n";
72 ss << " m_bar=2.0 * fmod(k-1.0, 2.0) * f_bar;\n";
73 ss << " g_bar_delta_u = - g * delta_u - m_bar * u;\n";
74 ss << " g_bar = m_bar - 2.0*k/fX + g;\n";
75 ss << " delta_u = g_bar_delta_u / g_bar;\n";
76 ss << " u = u + delta_u;\n";
77 ss << " g = -1.0/g_bar;\n";
78 ss << " f_bar=f_bar * g;\n";
79 ss << " }\n";
80 ss << " m_bar=2.0 * fmod(k-1.0, 2.0) * f_bar;\n";
81 ss << " g_bar_delta_u = f_bar - g * delta_u - m_bar * u;\n";
82 ss << " g_bar = m_bar - 2.0*k/fX + g;\n";
83 ss << " delta_u = g_bar_delta_u / g_bar;\n";
84 ss << " u = u + delta_u;\n";
85 ss << " g = -1.0/g_bar;\n";
86 ss << " f_bar = f_bar * g;\n";
87 ss << " k = k + 1.0;\n";
88 ss << " }\n";
89 ss << " do\n";
90 ss << " {\n";
91 ss << " m_bar = 2.0 * fmod(k-1.0, 2.0) * f_bar;\n";
92 ss << " g_bar_delta_u = - g * delta_u - m_bar * u;\n";
93 ss << " g_bar = m_bar - 2.0*k/fX + g;\n";
94 ss << " delta_u = g_bar_delta_u / g_bar;\n";
95 ss << " u = u + delta_u;\n";
96 ss << " g = -pow(g_bar,-1.0);\n";
97 ss << " f_bar = f_bar * g;\n";
98 ss << " bHasfound = (fabs(delta_u)<=fabs(u)*epsilon);\n";
99 ss << " k = k + 1.0;\n";
100 ss << " }\n";
101 ss << " while (!bHasfound && k <= fMaxIteration);\n";
102 ss << " if (bHasfound)\n";
103 ss << " return u * fSign;\n";
104 ss << " else\n";
105 ss << " return CreateDoubleError(NoConvergence);\n";
106 ss << "}";
107}
109 outputstream &ss,const std::string &sSymName,
110 SubArguments &vSubArguments)
111{
112 CHECK_PARAMETER_COUNT( 2, 2 );
113 GenerateFunctionDeclaration( sSymName, vSubArguments, ss );
114 ss << "{\n";
115 ss << " double tmp=0;\n";
116 ss << " int gid0=get_global_id(0);\n";
117 ss <<"\n";
118 GenerateArg( "tmp0", 0, vSubArguments, ss );
119 GenerateArg( "tmp1", 1, vSubArguments, ss );
120 ss << " tmp =tmp0 >= tmp1 ? 1 : 0;\n";
121 ss << " return tmp;\n";
122 ss << "}\n";
123}
124
125}
126
127/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void GenSlidingWindowFunction(outputstream &ss, const std::string &sSymName, SubArguments &vSubArguments) override
Definition: op_addin.cxx:19
virtual void GenSlidingWindowFunction(outputstream &ss, const std::string &sSymName, SubArguments &vSubArguments) override
Definition: op_addin.cxx:108
void GenerateArg(const char *name, int arg, SubArguments &vSubArguments, outputstream &ss, EmptyArgType empty=EmptyIsZero, GenerateArgTypeType generateType=DoNotGenerateArgType)
Definition: opbase.cxx:229
void GenerateFunctionDeclaration(const std::string &sSymName, SubArguments &vSubArguments, outputstream &ss)
Definition: opbase.cxx:566
std::vector< DynamicKernelArgumentRef > SubArguments
Definition: opbase.hxx:347
#define CHECK_PARAMETER_COUNT(min, max)
Definition: opbase.hxx:85