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  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "analysisdefs.hxx"
21 #include "analysis.hxx"
22 #include "bessel.hxx"
23 #include <comphelper/random.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <cppuhelper/weak.hxx>
26 #include <o3tl/any.hxx>
27 #include <rtl/math.hxx>
28 #include <sal/macros.h>
29 #include <unotools/resmgr.hxx>
30 #include <i18nlangtag/languagetag.hxx>
31 #include <algorithm>
32 #include <cmath>
33 #include <float.h>
34 
35 #define ADDIN_SERVICE               "com.sun.star.sheet.AddIn"
36 #define MY_SERVICE                  "com.sun.star.sheet.addin.Analysis"
37 #define MY_IMPLNAME                 "com.sun.star.sheet.addin.AnalysisImpl"
38 
39 using namespace                 ::com::sun::star;
40 using namespace sca::analysis;
41 using namespace std;
42 
GetFuncDescrStr(const char ** pResId,sal_uInt16 nStrIndex)43 OUString AnalysisAddIn::GetFuncDescrStr(const char** pResId, sal_uInt16 nStrIndex)
44 {
45     return AnalysisResId(pResId[nStrIndex - 1]);
46 }
47 
InitData()48 void AnalysisAddIn::InitData()
49 {
50     aResLocale = Translate::Create("sca", LanguageTag(aFuncLoc));
51 
52     pFD.reset(new FuncDataList);
53     InitFuncDataList(*pFD);
54 
55     pDefLocales.reset();
56 }
57 
AnalysisAddIn(const uno::Reference<uno::XComponentContext> & xContext)58 AnalysisAddIn::AnalysisAddIn( const uno::Reference< uno::XComponentContext >& xContext ) :
59     AnalysisAddIn_Base(m_aMutex),
60     aAnyConv( xContext )
61 {
62 }
63 
~AnalysisAddIn()64 AnalysisAddIn::~AnalysisAddIn()
65 {
66 }
67 
getDateMode(const uno::Reference<beans::XPropertySet> & xPropSet,const uno::Any & rAny)68 sal_Int32 AnalysisAddIn::getDateMode(
69         const uno::Reference< beans::XPropertySet >& xPropSet,
70         const uno::Any& rAny )
71 {
72     sal_Int32 nMode = aAnyConv.getInt32( xPropSet, rAny, 0 );
73     if( (nMode < 0) || (nMode > 4) )
74         throw lang::IllegalArgumentException();
75     return nMode;
76 }
77 
78 #define MAXFACTDOUBLE   300
79 
FactDouble(sal_Int32 nNum)80 double AnalysisAddIn::FactDouble( sal_Int32 nNum )
81 {
82     if( nNum < 0 || nNum > MAXFACTDOUBLE )
83         throw lang::IllegalArgumentException();
84 
85     if( !pFactDoubles )
86     {
87         pFactDoubles.reset( new double[ MAXFACTDOUBLE + 1 ] );
88 
89         pFactDoubles[ 0 ] = 1.0;    // by default
90 
91         double      fOdd = 1.0;
92         double      fEven = 2.0;
93 
94         pFactDoubles[ 1 ] = fOdd;
95         pFactDoubles[ 2 ] = fEven;
96 
97         bool    bOdd = true;
98 
99         for( sal_uInt16 nCnt = 3 ; nCnt <= MAXFACTDOUBLE ; nCnt++ )
100         {
101             if( bOdd )
102             {
103                 fOdd *= nCnt;
104                 pFactDoubles[ nCnt ] = fOdd;
105             }
106             else
107             {
108                 fEven *= nCnt;
109                 pFactDoubles[ nCnt ] = fEven;
110             }
111 
112             bOdd = !bOdd;
113 
114         }
115     }
116 
117     return pFactDoubles[ nNum ];
118 }
119 
120 // XServiceName
getServiceName()121 OUString SAL_CALL AnalysisAddIn::getServiceName()
122 {
123     // name of specific AddIn service
124     return MY_SERVICE;
125 }
126 
127 // XServiceInfo
getImplementationName()128 OUString SAL_CALL AnalysisAddIn::getImplementationName()
129 {
130     return MY_IMPLNAME;
131 }
132 
supportsService(const OUString & aName)133 sal_Bool SAL_CALL AnalysisAddIn::supportsService( const OUString& aName )
134 {
135     return cppu::supportsService(this, aName);
136 }
137 
getSupportedServiceNames()138 uno::Sequence< OUString > SAL_CALL AnalysisAddIn::getSupportedServiceNames()
139 {
140     return { ADDIN_SERVICE, MY_SERVICE };
141 }
142 
143 // XLocalizable
setLocale(const lang::Locale & eLocale)144 void SAL_CALL AnalysisAddIn::setLocale( const lang::Locale& eLocale )
145 {
146     aFuncLoc = eLocale;
147 
148     InitData();     // change of locale invalidates resources!
149 }
150 
getLocale()151 lang::Locale SAL_CALL AnalysisAddIn::getLocale()
152 {
153     return aFuncLoc;
154 }
155 
156 // XAddIn
getProgrammaticFuntionName(const OUString &)157 OUString SAL_CALL AnalysisAddIn::getProgrammaticFuntionName( const OUString& )
158 {
159     //  not used by calc
160     //  (but should be implemented for other uses of the AddIn service)
161 
162     return OUString();
163 }
164 
getDisplayFunctionName(const OUString & aProgrammaticName)165 OUString SAL_CALL AnalysisAddIn::getDisplayFunctionName( const OUString& aProgrammaticName )
166 {
167     OUString          aRet;
168 
169     auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aProgrammaticName ) );
170     if( it != pFD->end() )
171     {
172         aRet = AnalysisResId(it->GetUINameID());
173         if( it->IsDouble() )
174         {
175             const OUString& rSuffix = it->GetSuffix();
176             if (!rSuffix.isEmpty())
177                 aRet += rSuffix;
178             else
179                 aRet += "_ADD";
180         }
181     }
182     else
183     {
184         aRet = "UNKNOWNFUNC_" + aProgrammaticName;
185     }
186 
187     return aRet;
188 }
189 
getFunctionDescription(const OUString & aProgrammaticName)190 OUString SAL_CALL AnalysisAddIn::getFunctionDescription( const OUString& aProgrammaticName )
191 {
192     OUString          aRet;
193 
194     auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aProgrammaticName ) );
195     if( it != pFD->end() )
196         aRet = GetFuncDescrStr( it->GetDescrID(), 1 );
197 
198     return aRet;
199 }
200 
getDisplayArgumentName(const OUString & aName,sal_Int32 nArg)201 OUString SAL_CALL AnalysisAddIn::getDisplayArgumentName( const OUString& aName, sal_Int32 nArg )
202 {
203     OUString          aRet;
204 
205     auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aName ) );
206     if( it != pFD->end() && nArg <= 0xFFFF )
207     {
208         sal_uInt16  nStr = it->GetStrIndex( sal_uInt16( nArg ) );
209         if( nStr )
210             aRet = GetFuncDescrStr( it->GetDescrID(), nStr );
211         else
212             aRet = "internal";
213     }
214 
215     return aRet;
216 }
217 
getArgumentDescription(const OUString & aName,sal_Int32 nArg)218 OUString SAL_CALL AnalysisAddIn::getArgumentDescription( const OUString& aName, sal_Int32 nArg )
219 {
220     OUString          aRet;
221 
222     auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aName ) );
223     if( it != pFD->end() && nArg <= 0xFFFF )
224     {
225         sal_uInt16  nStr = it->GetStrIndex( sal_uInt16( nArg ) );
226         if( nStr )
227             aRet = GetFuncDescrStr( it->GetDescrID(), nStr + 1 );
228         else
229             aRet = "for internal use only";
230     }
231 
232     return aRet;
233 }
234 
235 constexpr OUStringLiteral pDefCatName = u"Add-In";
236 
getProgrammaticCategoryName(const OUString & aName)237 OUString SAL_CALL AnalysisAddIn::getProgrammaticCategoryName( const OUString& aName )
238 {
239     //  return non-translated strings
240     //  return OUString( "Add-In" );
241     auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aName ) );
242     OUString              aRet;
243     if( it != pFD->end() )
244     {
245         switch( it->GetCategory() )
246         {
247             case FDCategory::DateTime:    aRet = "Date&Time";         break;
248             case FDCategory::Finance:     aRet = "Financial";         break;
249             case FDCategory::Inf:         aRet = "Information";       break;
250             case FDCategory::Math:        aRet = "Mathematical";      break;
251             case FDCategory::Tech:        aRet = "Technical";         break;
252         }
253     }
254     else
255         aRet = pDefCatName;
256 
257     return aRet;
258 }
259 
getDisplayCategoryName(const OUString & aProgrammaticFunctionName)260 OUString SAL_CALL AnalysisAddIn::getDisplayCategoryName( const OUString& aProgrammaticFunctionName )
261 {
262     //  return translated strings, not used for predefined categories
263     auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aProgrammaticFunctionName ) );
264     OUString              aRet;
265     if( it != pFD->end() )
266     {
267         switch( it->GetCategory() )
268         {
269             case FDCategory::DateTime:    aRet = "Date&Time";         break;
270             case FDCategory::Finance:     aRet = "Financial";         break;
271             case FDCategory::Inf:         aRet = "Information";       break;
272             case FDCategory::Math:        aRet = "Mathematical";      break;
273             case FDCategory::Tech:        aRet = "Technical";         break;
274         }
275     }
276     else
277         aRet = pDefCatName;
278 
279     return aRet;
280 }
281 
282 static const char*          pLang[] = { "de", "en" };
283 static const char*          pCoun[] = { "DE", "US" };
284 const sal_uInt32     nNumOfLoc = SAL_N_ELEMENTS(pLang);
285 
InitDefLocales()286 void AnalysisAddIn::InitDefLocales()
287 {
288     pDefLocales.reset( new lang::Locale[ nNumOfLoc ] );
289 
290     for( sal_uInt32 n = 0 ; n < nNumOfLoc ; n++ )
291     {
292         pDefLocales[ n ].Language = OUString::createFromAscii( pLang[ n ] );
293         pDefLocales[ n ].Country = OUString::createFromAscii( pCoun[ n ] );
294     }
295 }
296 
GetLocale(sal_uInt32 nInd)297 inline const lang::Locale& AnalysisAddIn::GetLocale( sal_uInt32 nInd )
298 {
299     if( !pDefLocales )
300         InitDefLocales();
301 
302     if( nInd < sizeof( pLang ) )
303         return pDefLocales[ nInd ];
304     else
305         return aFuncLoc;
306 }
307 
getCompatibilityNames(const OUString & aProgrammaticName)308 uno::Sequence< sheet::LocalizedName > SAL_CALL AnalysisAddIn::getCompatibilityNames( const OUString& aProgrammaticName )
309 {
310     auto it = std::find_if(pFD->begin(), pFD->end(), FindFuncData( aProgrammaticName ) );
311     if( it == pFD->end() )
312         return uno::Sequence< sheet::LocalizedName >( 0 );
313 
314     const std::vector<OUString>& r = it->GetCompNameList();
315     sal_uInt32                   nCount = r.size();
316 
317     uno::Sequence< sheet::LocalizedName >                aRet( nCount );
318 
319     sheet::LocalizedName*  pArray = aRet.getArray();
320 
321     for( sal_uInt32 n = 0 ; n < nCount ; n++ )
322     {
323         pArray[ n ] = sheet::LocalizedName( GetLocale( n ), r[n] );
324     }
325 
326     return aRet;
327 }
328 
329 // XAnalysis
330 /** Workday */
getWorkday(const uno::Reference<beans::XPropertySet> & xOptions,sal_Int32 nDate,sal_Int32 nDays,const uno::Any & aHDay)331 sal_Int32 SAL_CALL AnalysisAddIn::getWorkday( const uno::Reference< beans::XPropertySet >& xOptions,
332     sal_Int32 nDate, sal_Int32 nDays, const uno::Any& aHDay )
333 {
334     if( !nDays )
335         return nDate;
336 
337     sal_Int32                   nNullDate = GetNullDate( xOptions );
338 
339     SortedIndividualInt32List   aSrtLst;
340 
341     aSrtLst.InsertHolidayList( aAnyConv, xOptions, aHDay, nNullDate );
342 
343     sal_Int32                   nActDate = nDate + nNullDate;
344 
345     if( nDays > 0 )
346     {
347         if( GetDayOfWeek( nActDate ) == 5 )
348             // when starting on Saturday, assuming we're starting on Sunday to get the jump over the weekend
349             nActDate++;
350 
351         while( nDays )
352         {
353             nActDate++;
354 
355             if( GetDayOfWeek( nActDate ) < 5 )
356             {
357                 if( !aSrtLst.Find( nActDate ) )
358                     nDays--;
359             }
360             else
361                 nActDate++;     // jump over weekend
362         }
363     }
364     else
365     {
366         if( GetDayOfWeek( nActDate ) == 6 )
367             // when starting on Sunday, assuming we're starting on Saturday to get the jump over the weekend
368             nActDate--;
369 
370         while( nDays )
371         {
372             nActDate--;
373 
374             if( GetDayOfWeek( nActDate ) < 5 )
375             {
376                 if( !aSrtLst.Find( nActDate ) )
377                     nDays++;
378             }
379             else
380                 nActDate--;     // jump over weekend
381         }
382     }
383 
384     return nActDate - nNullDate;
385 }
386 
387 /** Yearfrac */
getYearfrac(const uno::Reference<beans::XPropertySet> & xOpt,sal_Int32 nStartDate,sal_Int32 nEndDate,const uno::Any & rMode)388 double SAL_CALL AnalysisAddIn::getYearfrac( const uno::Reference< beans::XPropertySet >& xOpt,
389     sal_Int32 nStartDate, sal_Int32 nEndDate, const uno::Any& rMode )
390 {
391     double fRet = GetYearFrac( xOpt, nStartDate, nEndDate, getDateMode( xOpt, rMode ) );
392     RETURN_FINITE( fRet );
393 }
394 
getEdate(const uno::Reference<beans::XPropertySet> & xOpt,sal_Int32 nStartDate,sal_Int32 nMonths)395 sal_Int32 SAL_CALL AnalysisAddIn::getEdate( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nMonths )
396 {
397     sal_Int32 nNullDate = GetNullDate( xOpt );
398     ScaDate aDate( nNullDate, nStartDate, 5 );
399     aDate.addMonths( nMonths );
400     return aDate.getDate( nNullDate );
401 }
402 
getWeeknum(const uno::Reference<beans::XPropertySet> & xOpt,sal_Int32 nDate,sal_Int32 nMode)403 sal_Int32 SAL_CALL AnalysisAddIn::getWeeknum( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nDate, sal_Int32 nMode )
404 {
405     nDate += GetNullDate( xOpt );
406 
407     sal_uInt16  nDay, nMonth, nYear;
408     DaysToDate( nDate, nDay, nMonth, nYear );
409 
410     sal_Int32   nFirstInYear = DateToDays( 1, 1, nYear );
411     sal_uInt16  nFirstDayInYear = GetDayOfWeek( nFirstInYear );
412 
413     return ( nDate - nFirstInYear + ( ( nMode == 1 )? ( nFirstDayInYear + 1 ) % 7 : nFirstDayInYear ) ) / 7 + 1;
414 }
415 
getEomonth(const uno::Reference<beans::XPropertySet> & xOpt,sal_Int32 nDate,sal_Int32 nMonths)416 sal_Int32 SAL_CALL AnalysisAddIn::getEomonth( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nDate, sal_Int32 nMonths )
417 {
418     sal_Int32   nNullDate = GetNullDate( xOpt );
419     nDate += nNullDate;
420     sal_uInt16  nDay, nMonth, nYear;
421     DaysToDate( nDate, nDay, nMonth, nYear );
422 
423     sal_Int32   nNewMonth = nMonth + nMonths;
424 
425     if( nNewMonth > 12 )
426     {
427         nYear = sal::static_int_cast<sal_uInt16>( nYear + ( nNewMonth / 12 ) );
428         nNewMonth %= 12;
429     }
430     else if( nNewMonth < 1 )
431     {
432         nNewMonth = -nNewMonth;
433         nYear = sal::static_int_cast<sal_uInt16>( nYear - ( nNewMonth / 12 ) );
434         nYear--;
435         nNewMonth %= 12;
436         nNewMonth = 12 - nNewMonth;
437     }
438 
439     return DateToDays( DaysInMonth( sal_uInt16( nNewMonth ), nYear ), sal_uInt16( nNewMonth ), nYear ) - nNullDate;
440 }
441 
getNetworkdays(const uno::Reference<beans::XPropertySet> & xOpt,sal_Int32 nStartDate,sal_Int32 nEndDate,const uno::Any & aHDay)442 sal_Int32 SAL_CALL AnalysisAddIn::getNetworkdays( const uno::Reference< beans::XPropertySet >& xOpt,
443         sal_Int32 nStartDate, sal_Int32 nEndDate, const uno::Any& aHDay )
444 {
445     sal_Int32                   nNullDate = GetNullDate( xOpt );
446 
447     SortedIndividualInt32List   aSrtLst;
448 
449     aSrtLst.InsertHolidayList( aAnyConv, xOpt, aHDay, nNullDate );
450 
451     sal_Int32                   nActDate = nStartDate + nNullDate;
452     sal_Int32                   nStopDate = nEndDate + nNullDate;
453     sal_Int32                   nCnt = 0;
454 
455     if( nActDate <= nStopDate )
456     {
457         while( nActDate <= nStopDate )
458         {
459             if( GetDayOfWeek( nActDate ) < 5 && !aSrtLst.Find( nActDate ) )
460                 nCnt++;
461 
462             nActDate++;
463         }
464     }
465     else
466     {
467         while( nActDate >= nStopDate )
468         {
469             if( GetDayOfWeek( nActDate ) < 5 && !aSrtLst.Find( nActDate ) )
470                 nCnt--;
471 
472             nActDate--;
473         }
474     }
475 
476     return nCnt;
477 }
478 
getIseven(sal_Int32 nVal)479 sal_Int32 SAL_CALL AnalysisAddIn::getIseven( sal_Int32 nVal )
480 {
481     return ( nVal & 0x00000001 )? 0 : 1;
482 }
483 
getIsodd(sal_Int32 nVal)484 sal_Int32 SAL_CALL AnalysisAddIn::getIsodd( sal_Int32 nVal )
485 {
486     return ( nVal & 0x00000001 )? 1 : 0;
487 }
488 
489 double SAL_CALL
getMultinomial(const uno::Reference<beans::XPropertySet> & xOpt,const uno::Sequence<uno::Sequence<sal_Int32>> & aVLst,const uno::Sequence<uno::Any> & aOptVLst)490 AnalysisAddIn::getMultinomial( const uno::Reference< beans::XPropertySet >& xOpt, const uno::Sequence< uno::Sequence< sal_Int32 > >& aVLst,
491                                const uno::Sequence< uno::Any >& aOptVLst )
492 {
493     ScaDoubleListGE0 aValList;
494 
495     aValList.Append( aVLst );
496     aValList.Append( aAnyConv, xOpt, aOptVLst );
497 
498     if( aValList.Count() == 0 )
499         return 0.0;
500 
501     double nZ = 0;
502     double fRet = 1.0;
503 
504     for( sal_uInt32 i = 0; i < aValList.Count(); ++i )
505     {
506         const double d = aValList.Get(i);
507         double n = (d >= 0.0) ? rtl::math::approxFloor( d ) : rtl::math::approxCeil( d );
508         if ( n < 0.0 )
509             throw lang::IllegalArgumentException();
510 
511         if( n > 0.0 )
512         {
513             nZ += n;
514             fRet *= BinomialCoefficient(nZ, n);
515         }
516     }
517     RETURN_FINITE( fRet );
518 }
519 
getSeriessum(double fX,double fN,double fM,const uno::Sequence<uno::Sequence<double>> & aCoeffList)520 double SAL_CALL AnalysisAddIn::getSeriessum( double fX, double fN, double fM, const uno::Sequence< uno::Sequence< double > >& aCoeffList )
521 {
522     double                          fRet = 0.0;
523 
524     // #i32269# 0^0 is undefined, Excel returns #NUM! error
525     if( fX == 0.0 && fN == 0 )
526         throw uno::RuntimeException("undefined expression: 0^0");
527 
528     if( fX != 0.0 )
529     {
530         for( const uno::Sequence< double >& rList : aCoeffList )
531         {
532             for( const double fCoef : rList )
533             {
534                 fRet += fCoef * pow( fX, fN );
535 
536                 fN += fM;
537             }
538         }
539     }
540 
541     RETURN_FINITE( fRet );
542 }
543 
getQuotient(double fNum,double fDenom)544 double SAL_CALL AnalysisAddIn::getQuotient( double fNum, double fDenom )
545 {
546     double fRet;
547     if( (fNum < 0) != (fDenom < 0) )
548         fRet = ::rtl::math::approxCeil( fNum / fDenom );
549     else
550         fRet = ::rtl::math::approxFloor( fNum / fDenom );
551     RETURN_FINITE( fRet );
552 }
553 
getMround(double fNum,double fMult)554 double SAL_CALL AnalysisAddIn::getMround( double fNum, double fMult )
555 {
556     if( fMult == 0.0 )
557         return fMult;
558 
559     double fRet = fMult * ::rtl::math::round( fNum / fMult );
560     RETURN_FINITE( fRet );
561 }
562 
getSqrtpi(double fNum)563 double SAL_CALL AnalysisAddIn::getSqrtpi( double fNum )
564 {
565     double fRet = sqrt( fNum * PI );
566     RETURN_FINITE( fRet );
567 }
568 
getRandbetween(double fMin,double fMax)569 double SAL_CALL AnalysisAddIn::getRandbetween( double fMin, double fMax )
570 {
571     fMin = ::rtl::math::round( fMin, 0, rtl_math_RoundingMode_Up );
572     fMax = ::rtl::math::round( fMax, 0, rtl_math_RoundingMode_Up );
573     if( fMin > fMax )
574         throw lang::IllegalArgumentException();
575 
576     double fRet = floor(comphelper::rng::uniform_real_distribution(fMin, nextafter(fMax+1, -DBL_MAX)));
577     RETURN_FINITE( fRet );
578 }
579 
getGcd(const uno::Reference<beans::XPropertySet> & xOpt,const uno::Sequence<uno::Sequence<double>> & aVLst,const uno::Sequence<uno::Any> & aOptVLst)580 double SAL_CALL AnalysisAddIn::getGcd( const uno::Reference< beans::XPropertySet >& xOpt, const uno::Sequence< uno::Sequence< double > >& aVLst, const uno::Sequence< uno::Any >& aOptVLst )
581 {
582     ScaDoubleListGT0 aValList;
583 
584     aValList.Append( aVLst );
585     aValList.Append( aAnyConv, xOpt, aOptVLst );
586 
587     if( aValList.Count() == 0 )
588         return 0.0;
589 
590     double          f = aValList.Get(0);
591     for( sal_uInt32 i = 1; i < aValList.Count(); ++i )
592     {
593         f = GetGcd( aValList.Get(i), f );
594     }
595 
596     RETURN_FINITE( f );
597 }
598 
getLcm(const uno::Reference<beans::XPropertySet> & xOpt,const uno::Sequence<uno::Sequence<double>> & aVLst,const uno::Sequence<uno::Any> & aOptVLst)599 double SAL_CALL AnalysisAddIn::getLcm( const uno::Reference< beans::XPropertySet >& xOpt, const uno::Sequence< uno::Sequence< double > >& aVLst, const uno::Sequence< uno::Any >& aOptVLst )
600 {
601     ScaDoubleListGE0 aValList;
602 
603     aValList.Append( aVLst );
604     aValList.Append( aAnyConv, xOpt, aOptVLst );
605 
606     if( aValList.Count() == 0 )
607         return 0.0;
608 
609     double f = rtl::math::approxFloor( aValList.Get(0) );
610     if( f < 0.0 )
611         throw lang::IllegalArgumentException();
612 
613     if( f == 0.0 )
614         return f;
615 
616     for( sal_uInt32 i = 1; i < aValList.Count(); ++i )
617     {
618         double fTmp = rtl::math::approxFloor( aValList.Get(i) );
619         if( fTmp < 0.0 )
620             throw lang::IllegalArgumentException();
621 
622         f = fTmp * f / GetGcd( fTmp, f );
623         if( f == 0.0 )
624             return f;
625     }
626 
627     RETURN_FINITE( f );
628 }
629 
getBesseli(double fNum,sal_Int32 nOrder)630 double SAL_CALL AnalysisAddIn::getBesseli( double fNum, sal_Int32 nOrder )
631 {
632     double fRet = sca::analysis::BesselI( fNum, nOrder );
633     RETURN_FINITE( fRet );
634 }
635 
getBesselj(double fNum,sal_Int32 nOrder)636 double SAL_CALL AnalysisAddIn::getBesselj( double fNum, sal_Int32 nOrder )
637 {
638     double fRet = sca::analysis::BesselJ( fNum, nOrder );
639     RETURN_FINITE( fRet );
640 }
641 
getBesselk(double fNum,sal_Int32 nOrder)642 double SAL_CALL AnalysisAddIn::getBesselk( double fNum, sal_Int32 nOrder )
643 {
644     if( nOrder < 0 || fNum <= 0.0 )
645         throw lang::IllegalArgumentException();
646 
647     double fRet = sca::analysis::BesselK( fNum, nOrder );
648     RETURN_FINITE( fRet );
649 }
650 
getBessely(double fNum,sal_Int32 nOrder)651 double SAL_CALL AnalysisAddIn::getBessely( double fNum, sal_Int32 nOrder )
652 {
653     if( nOrder < 0 || fNum <= 0.0 )
654         throw lang::IllegalArgumentException();
655 
656     double fRet = sca::analysis::BesselY( fNum, nOrder );
657     RETURN_FINITE( fRet );
658 }
659 
660 const double    SCA_MAX2        = 511.0;            // min. val for binary numbers (9 bits + sign)
661 const double    SCA_MIN2        = -SCA_MAX2-1.0;    // min. val for binary numbers (9 bits + sign)
662 const double    SCA_MAX8        = 536870911.0;      // max. val for octal numbers (29 bits + sign)
663 const double    SCA_MIN8        = -SCA_MAX8-1.0;    // min. val for octal numbers (29 bits + sign)
664 const double    SCA_MAX16       = 549755813887.0;   // max. val for hexadecimal numbers (39 bits + sign)
665 const double    SCA_MIN16       = -SCA_MAX16-1.0;   // min. val for hexadecimal numbers (39 bits + sign)
666 const sal_Int32 SCA_MAXPLACES   = 10;               // max. number of places
667 
getBin2Oct(const uno::Reference<beans::XPropertySet> & xOpt,const OUString & aNum,const uno::Any & rPlaces)668 OUString SAL_CALL AnalysisAddIn::getBin2Oct( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
669 {
670     double fVal = ConvertToDec( aNum, 2, SCA_MAXPLACES );
671     sal_Int32 nPlaces = 0;
672     bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
673     return ConvertFromDec( fVal, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
674 }
675 
getBin2Dec(const OUString & aNum)676 double SAL_CALL AnalysisAddIn::getBin2Dec( const OUString& aNum )
677 {
678     double fRet = ConvertToDec( aNum, 2, SCA_MAXPLACES );
679     RETURN_FINITE( fRet );
680 }
681 
getBin2Hex(const uno::Reference<beans::XPropertySet> & xOpt,const OUString & aNum,const uno::Any & rPlaces)682 OUString SAL_CALL AnalysisAddIn::getBin2Hex( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
683 {
684     double fVal = ConvertToDec( aNum, 2, SCA_MAXPLACES );
685     sal_Int32 nPlaces = 0;
686     bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
687     return ConvertFromDec( fVal, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
688 }
689 
getOct2Bin(const uno::Reference<beans::XPropertySet> & xOpt,const OUString & aNum,const uno::Any & rPlaces)690 OUString SAL_CALL AnalysisAddIn::getOct2Bin( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
691 {
692     double fVal = ConvertToDec( aNum, 8, SCA_MAXPLACES );
693     sal_Int32 nPlaces = 0;
694     bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
695     return ConvertFromDec( fVal, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
696 }
697 
getOct2Dec(const OUString & aNum)698 double SAL_CALL AnalysisAddIn::getOct2Dec( const OUString& aNum )
699 {
700     double fRet = ConvertToDec( aNum, 8, SCA_MAXPLACES );
701     RETURN_FINITE( fRet );
702 }
703 
getOct2Hex(const uno::Reference<beans::XPropertySet> & xOpt,const OUString & aNum,const uno::Any & rPlaces)704 OUString SAL_CALL AnalysisAddIn::getOct2Hex( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
705 {
706     double fVal = ConvertToDec( aNum, 8, SCA_MAXPLACES );
707     sal_Int32 nPlaces = 0;
708     bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
709     return ConvertFromDec( fVal, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
710 }
711 
getDec2Bin(const uno::Reference<beans::XPropertySet> & xOpt,sal_Int32 nNum,const uno::Any & rPlaces)712 OUString SAL_CALL AnalysisAddIn::getDec2Bin( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nNum, const uno::Any& rPlaces )
713 {
714     sal_Int32 nPlaces = 0;
715     bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
716     return ConvertFromDec( nNum, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
717 }
718 
getDec2Oct(const uno::Reference<beans::XPropertySet> & xOpt,sal_Int32 nNum,const uno::Any & rPlaces)719 OUString SAL_CALL AnalysisAddIn::getDec2Oct( const uno::Reference< beans::XPropertySet >& xOpt, sal_Int32 nNum, const uno::Any& rPlaces )
720 {
721     sal_Int32 nPlaces = 0;
722     bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
723     return ConvertFromDec( nNum, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
724 }
725 
getDec2Hex(const uno::Reference<beans::XPropertySet> & xOpt,double fNum,const uno::Any & rPlaces)726 OUString SAL_CALL AnalysisAddIn::getDec2Hex( const uno::Reference< beans::XPropertySet >& xOpt, double fNum, const uno::Any& rPlaces )
727 {
728     sal_Int32 nPlaces = 0;
729     bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
730     return ConvertFromDec( fNum, SCA_MIN16, SCA_MAX16, 16, nPlaces, SCA_MAXPLACES, bUsePlaces );
731 }
732 
getHex2Bin(const uno::Reference<beans::XPropertySet> & xOpt,const OUString & aNum,const uno::Any & rPlaces)733 OUString SAL_CALL AnalysisAddIn::getHex2Bin( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
734 {
735     double fVal = ConvertToDec( aNum, 16, SCA_MAXPLACES );
736     sal_Int32 nPlaces = 0;
737     bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
738     return ConvertFromDec( fVal, SCA_MIN2, SCA_MAX2, 2, nPlaces, SCA_MAXPLACES, bUsePlaces );
739 }
740 
getHex2Dec(const OUString & aNum)741 double SAL_CALL AnalysisAddIn::getHex2Dec( const OUString& aNum )
742 {
743     double fRet = ConvertToDec( aNum, 16, SCA_MAXPLACES );
744     RETURN_FINITE( fRet );
745 }
746 
getHex2Oct(const uno::Reference<beans::XPropertySet> & xOpt,const OUString & aNum,const uno::Any & rPlaces)747 OUString SAL_CALL AnalysisAddIn::getHex2Oct( const uno::Reference< beans::XPropertySet >& xOpt, const OUString& aNum, const uno::Any& rPlaces )
748 {
749     double fVal = ConvertToDec( aNum, 16, SCA_MAXPLACES );
750     sal_Int32 nPlaces = 0;
751     bool bUsePlaces = aAnyConv.getInt32( nPlaces, xOpt, rPlaces );
752     return ConvertFromDec( fVal, SCA_MIN8, SCA_MAX8, 8, nPlaces, SCA_MAXPLACES, bUsePlaces );
753 }
754 
getDelta(const uno::Reference<beans::XPropertySet> & xOpt,double fNum1,const uno::Any & rNum2)755 sal_Int32 SAL_CALL AnalysisAddIn::getDelta( const uno::Reference< beans::XPropertySet >& xOpt, double fNum1, const uno::Any& rNum2 )
756 {
757     return sal_Int32(fNum1 == aAnyConv.getDouble( xOpt, rNum2, 0.0 ));
758 }
759 
getErf(const uno::Reference<beans::XPropertySet> & xOpt,double fLL,const uno::Any & rUL)760 double SAL_CALL AnalysisAddIn::getErf( const uno::Reference< beans::XPropertySet >& xOpt, double fLL, const uno::Any& rUL )
761 {
762     double fUL, fRet;
763     bool bContainsValue = aAnyConv.getDouble( fUL, xOpt, rUL );
764 
765     fRet = bContainsValue ? (Erf( fUL ) - Erf( fLL )) : Erf( fLL );
766     RETURN_FINITE( fRet );
767 }
768 
getErfc(double f)769 double SAL_CALL AnalysisAddIn::getErfc( double f )
770 {
771     double fRet = Erfc( f );
772     RETURN_FINITE( fRet );
773 }
774 
getGestep(const uno::Reference<beans::XPropertySet> & xOpt,double fNum,const uno::Any & rStep)775 sal_Int32 SAL_CALL AnalysisAddIn::getGestep( const uno::Reference< beans::XPropertySet >& xOpt, double fNum, const uno::Any& rStep )
776 {
777     return sal_Int32(fNum >= aAnyConv.getDouble( xOpt, rStep, 0.0 ));
778 }
779 
getFactdouble(sal_Int32 nNum)780 double SAL_CALL AnalysisAddIn::getFactdouble( sal_Int32 nNum )
781 {
782     double fRet = FactDouble( nNum );
783     RETURN_FINITE( fRet );
784 }
785 
getImabs(const OUString & aNum)786 double SAL_CALL AnalysisAddIn::getImabs( const OUString& aNum )
787 {
788     double fRet = Complex( aNum ).Abs();
789     RETURN_FINITE( fRet );
790 }
791 
getImaginary(const OUString & aNum)792 double SAL_CALL AnalysisAddIn::getImaginary( const OUString& aNum )
793 {
794     double fRet = Complex( aNum ).Imag();
795     RETURN_FINITE( fRet );
796 }
797 
getImpower(const OUString & aNum,double f)798 OUString SAL_CALL AnalysisAddIn::getImpower( const OUString& aNum, double f )
799 {
800     Complex     z( aNum );
801 
802     z.Power( f );
803 
804     return z.GetString();
805 }
806 
getImargument(const OUString & aNum)807 double SAL_CALL AnalysisAddIn::getImargument( const OUString& aNum )
808 {
809     double fRet = Complex( aNum ).Arg();
810     RETURN_FINITE( fRet );
811 }
812 
getImcos(const OUString & aNum)813 OUString SAL_CALL AnalysisAddIn::getImcos( const OUString& aNum )
814 {
815     Complex     z( aNum );
816 
817     z.Cos();
818 
819     return z.GetString();
820 }
821 
getImdiv(const OUString & aDivid,const OUString & aDivis)822 OUString SAL_CALL AnalysisAddIn::getImdiv( const OUString& aDivid, const OUString& aDivis )
823 {
824     Complex     z( aDivid );
825 
826     z.Div( Complex( aDivis ) );
827 
828     return z.GetString();
829 }
830 
getImexp(const OUString & aNum)831 OUString SAL_CALL AnalysisAddIn::getImexp( const OUString& aNum )
832 {
833     Complex     z( aNum );
834 
835     z.Exp();
836 
837     return z.GetString();
838 }
839 
getImconjugate(const OUString & aNum)840 OUString SAL_CALL AnalysisAddIn::getImconjugate( const OUString& aNum )
841 {
842     Complex     z( aNum );
843 
844     z.Conjugate();
845 
846     return z.GetString();
847 }
848 
getImln(const OUString & aNum)849 OUString SAL_CALL AnalysisAddIn::getImln( const OUString& aNum )
850 {
851     Complex     z( aNum );
852 
853     z.Ln();
854 
855     return z.GetString();
856 }
857 
getImlog10(const OUString & aNum)858 OUString SAL_CALL AnalysisAddIn::getImlog10( const OUString& aNum )
859 {
860     Complex     z( aNum );
861 
862     z.Log10();
863 
864     return z.GetString();
865 }
866 
getImlog2(const OUString & aNum)867 OUString SAL_CALL AnalysisAddIn::getImlog2( const OUString& aNum )
868 {
869     Complex     z( aNum );
870 
871     z.Log2();
872 
873     return z.GetString();
874 }
875 
getImproduct(const uno::Reference<beans::XPropertySet> &,const uno::Sequence<uno::Sequence<OUString>> & aNum1,const uno::Sequence<uno::Any> & aNL)876 OUString SAL_CALL AnalysisAddIn::getImproduct( const uno::Reference< beans::XPropertySet >&, const uno::Sequence< uno::Sequence< OUString > >& aNum1, const uno::Sequence< uno::Any >& aNL )
877 {
878     ComplexList     z_list;
879 
880     z_list.Append( aNum1 );
881     z_list.Append( aNL );
882 
883     if( z_list.empty() )
884         return Complex( 0 ).GetString();
885 
886     Complex         z = z_list.Get(0);
887     for( sal_uInt32 i = 1; i < z_list.Count(); ++i )
888         z.Mult( z_list.Get(i) );
889 
890     return z.GetString();
891 }
892 
getImreal(const OUString & aNum)893 double SAL_CALL AnalysisAddIn::getImreal( const OUString& aNum )
894 {
895     double fRet = Complex( aNum ).Real();
896     RETURN_FINITE( fRet );
897 }
898 
getImsin(const OUString & aNum)899 OUString SAL_CALL AnalysisAddIn::getImsin( const OUString& aNum )
900 {
901     Complex     z( aNum );
902 
903     z.Sin();
904 
905     return z.GetString();
906 }
907 
getImsub(const OUString & aNum1,const OUString & aNum2)908 OUString SAL_CALL AnalysisAddIn::getImsub( const OUString& aNum1, const OUString& aNum2 )
909 {
910     Complex     z( aNum1 );
911 
912     z.Sub( Complex( aNum2 ) );
913 
914     return z.GetString();
915 }
916 
getImsum(const uno::Reference<beans::XPropertySet> &,const uno::Sequence<uno::Sequence<OUString>> & aNum1,const uno::Sequence<uno::Any> & aFollowingPars)917 OUString SAL_CALL AnalysisAddIn::getImsum( const uno::Reference< beans::XPropertySet >&, const uno::Sequence< uno::Sequence< OUString > >& aNum1, const uno::Sequence< uno::Any >& aFollowingPars )
918 {
919     ComplexList     z_list;
920 
921     z_list.Append( aNum1 );
922     z_list.Append( aFollowingPars );
923 
924     if( z_list.empty() )
925         return Complex( 0 ).GetString();
926 
927     Complex         z( z_list.Get(0) );
928     for( sal_uInt32 i = 1; i < z_list.Count(); ++i )
929         z.Add( z_list.Get(i) );
930 
931     return z.GetString();
932 }
933 
getImsqrt(const OUString & aNum)934 OUString SAL_CALL AnalysisAddIn::getImsqrt( const OUString& aNum )
935 {
936     Complex     z( aNum );
937 
938     z.Sqrt();
939 
940     return z.GetString();
941 }
942 
getImtan(const OUString & aNum)943 OUString SAL_CALL AnalysisAddIn::getImtan( const OUString& aNum )
944 {
945     Complex     z( aNum );
946 
947     z.Tan();
948 
949     return z.GetString();
950 }
951 
getImsec(const OUString & aNum)952 OUString SAL_CALL AnalysisAddIn::getImsec( const OUString& aNum )
953 {
954     Complex     z( aNum );
955 
956     z.Sec();
957 
958     return z.GetString();
959 }
960 
getImcsc(const OUString & aNum)961 OUString SAL_CALL AnalysisAddIn::getImcsc( const OUString& aNum )
962 {
963     Complex     z( aNum );
964 
965     z.Csc();
966 
967     return z.GetString();
968 }
969 
getImcot(const OUString & aNum)970 OUString SAL_CALL AnalysisAddIn::getImcot( const OUString& aNum )
971 {
972     Complex     z( aNum );
973 
974     z.Cot();
975 
976     return z.GetString();
977 }
978 
getImsinh(const OUString & aNum)979 OUString SAL_CALL AnalysisAddIn::getImsinh( const OUString& aNum )
980 {
981     Complex     z( aNum );
982 
983     z.Sinh();
984 
985     return z.GetString();
986 }
987 
getImcosh(const OUString & aNum)988 OUString SAL_CALL AnalysisAddIn::getImcosh( const OUString& aNum )
989 {
990     Complex     z( aNum );
991 
992     z.Cosh();
993 
994     return z.GetString();
995 }
996 
getImsech(const OUString & aNum)997 OUString SAL_CALL AnalysisAddIn::getImsech( const OUString& aNum )
998 {
999     Complex     z( aNum );
1000 
1001     z.Sech();
1002 
1003     return z.GetString();
1004 }
1005 
getImcsch(const OUString & aNum)1006 OUString SAL_CALL AnalysisAddIn::getImcsch( const OUString& aNum )
1007 {
1008     Complex     z( aNum );
1009 
1010     z.Csch();
1011 
1012     return z.GetString();
1013 }
1014 
getComplex(double fR,double fI,const uno::Any & rSuff)1015 OUString SAL_CALL AnalysisAddIn::getComplex( double fR, double fI, const uno::Any& rSuff )
1016 {
1017     bool    bi;
1018 
1019     switch( rSuff.getValueTypeClass() )
1020     {
1021         case uno::TypeClass_VOID:
1022             bi = true;
1023             break;
1024         case uno::TypeClass_STRING:
1025             {
1026             auto   pSuff = o3tl::forceAccess<OUString>(rSuff);
1027             bi = *pSuff == "i" || pSuff->isEmpty();
1028             if( !bi && *pSuff != "j" )
1029                 throw lang::IllegalArgumentException();
1030             }
1031             break;
1032         default:
1033             throw lang::IllegalArgumentException();
1034     }
1035 
1036     return Complex( fR, fI, bi ? 'i' : 'j' ).GetString();
1037 }
1038 
getConvert(double f,const OUString & aFU,const OUString & aTU)1039 double SAL_CALL AnalysisAddIn::getConvert( double f, const OUString& aFU, const OUString& aTU )
1040 {
1041     if( !pCDL )
1042         pCDL.reset(new ConvertDataList());
1043 
1044     double fRet = pCDL->Convert( f, aFU, aTU );
1045     RETURN_FINITE( fRet );
1046 }
1047 
AnalysisResId(const char * pResId)1048 OUString AnalysisAddIn::AnalysisResId(const char* pResId)
1049 {
1050     return Translate::get(pResId, aResLocale);
1051 }
1052 
1053 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
scaddins_AnalysisAddIn_get_implementation(css::uno::XComponentContext * context,css::uno::Sequence<css::uno::Any> const &)1054 scaddins_AnalysisAddIn_get_implementation(
1055     css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
1056 {
1057     return cppu::acquire(new AnalysisAddIn(context));
1058 }
1059 
1060 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
1061