1. /* snfunc.h by K.Tsuru */
  2. /*********************
  3. SN library
  4. Mathematical functions' sorting list.
  5. **********************/
  6. #ifndef SN_FUNCTIONS_H
  7. #define SN_FUNCTIONS_H
  8. // function ID
  9. /*****************************************************************
  10. Comparing two values from the position of one, return how many digits
  11. agree each other.In the case of SDouble the exponent is not considerd
  12. and the head zeros are not counted.
  13. If an error ocuured return a negative value as follows.
  14. not DRADIX : -1
  15. different type : -2
  16. not in standard form : -3
  17. *******************************************************************/
  18. long AgreeUpTo(const SNumber& a, const SNumber& b); // 1000
  19. // SLong class
  20. SLong LFact(ulong n); // factorial n! in SLong arithmetric for reference 2001
  21. SLong FactPF(ulong N); // factorial N! by prime factorization method
  22. // 2009 since ver 2.82
  23. SLong TournamentProduct(const SNBlock <SLong>& a, const uint N);// 2010 since ver 2.182
  24. SLong BothSideProduct(const SNBlock <SLong>& a, const uint N, bool sort = true); // 2011 since ver 2.182
  25. #ifndef STRUCT_PRIMFACTOR
  26. #define STRUCT_PRIMFACTOR
  27. struct primeFactor {
  28. int prime;
  29. int power;
  30. };
  31. #endif // STRUCT_PRIMFACTOR
  32. SLong PrimeFactorProduct(const SNBlock <primeFactor>& pf, int n);// 2012 since ver 2.182
  33. /************* assistant functions for trigonometric ones **************/
  34. /****************************************************************
  35. arcsin by series
  36. Set "fe" a value of "fixedExp".For the calculation of sum
  37. y = a + AsinSeries(x);
  38. in the case of |a|>>|x|, set fe = a.RdxExp() to avoid calculating useless precision.
  39. This is a measure to decrease terms, because convergence of arcsin's series is
  40. very slow.
  41. If fe = INT_MAX > DRADIX_EXP_MAX, set fe = x.RdxExp().
  42. *****************************************************************/
  43. SDouble AsinSeries(const SDouble& x, int fe = INT_MAX); //3204
  44. SDouble AsinBSNW(const SDouble& x);
  45. SDouble CosRN(const SDouble& x); // x/(R^N) |x|< pi/4. The assistant function of CosS_RN(x) 3205
  46. SDouble SinRN(const SDouble& x); // ditto 3206
  47. SDouble TanRN(const SDouble& x); // 3106 Old version's name is "SDouble Tan(const SDouble& x)".
  48. SDouble CosS_RN(const SDouble& x); // Cos(x)
  49. SDouble SinS_RN(const SDouble& x); // Sin(x)
  50. SDouble CosDiv(const SDouble& x); //x=short+long&small |x| < pi/4 3207(ref)
  51. SDouble SinDiv(const SDouble& x); // 3208(ref)
  52. SDouble CosSeries(const SDouble& x, int first = 1); //for first = 1 return cos(x), first=0 cos(x)-1 3209
  53. SDouble SinSeries(const SDouble& x); //3210
  54. /*--------------------------------------------------------------------------
  55. Get a calculation method for sin(x), cos(x) or tan(x) functions.
  56. y = x - n*(pi/2) ( n is an integer, |y| <= pi/4 ).
  57. Return the sign which attatch to calculation result.
  58. If sign = 0, y has an exact value as y = -1.0, 0.0 or 1.0
  59. Give *func a value COS_CALC, SIN_CALC or TAN_CALC, an appropriate function
  60. is set in it.
  61. SDecimal (*pfBSeries)(const SDouble& x), int maxExp, const char* func);
  62. ----------------------------------------------------------------------------*/
  63. enum { COS_CALC = 1, SIN_CALC = 2, TAN_CALC = 3, COT_CALC = 4};
  64. int GetTriCalcMethod(const SDouble& x, SDouble& y, int* func, int* quadrant = NULL);//3201
  65. SDecimal BsinSeries(const SDouble& x);
  66. // exponential and hyperbolic functions
  67. enum { COSH_CALC = 1, SINH_CALC = -1};
  68. // if first != 0 calculate exp(x), else exp(x) - 1 for small x
  69. SDouble ExpSeries(const SDouble& x, int first = 1); // 3306
  70. SDouble CoshSeries(const SDouble& x, int first); // 3307
  71. SDouble CoshSeries(const SDouble& x);//CoshSeries(x, 1) 3307
  72. SDouble SinhSeries(const SDouble& x); // 3308
  73. // artan(x) =0.5*log{(1+x)/(1-x)} |x| << 1.0
  74. SDouble AtanhSeries(const SDouble& x); // 3312
  75. SDouble ExpDS(const SDouble& x); // divide and series method 3313
  76. SDouble ExpL(const SDouble& x); // exp(x) using Log(x) 3314
  77. /******************************************************
  78. ExpBSR(n, d) returns exp(n/d) with SLong n and d using binary splitting method. 3315 ver. 2.18
  79. ******************************************************/
  80. SDouble ExpBSR(const SLong& n, const SLong& d); // 3316 ver. 2.18
  81. /******************************************************
  82. "ExpBSR(n, d, a, c)" returns the value exp(n/d) as a rational number a/c.
  83. ******************************************************/
  84. void ExpBSR(const SLong& n, const SLong& d, SLong& a, SLong& c);// 3317 ver. 2.18
  85. /***************************************************
  86. It evaluates exp(x) using "ExpBSR(...)" above.
  87. ****************************************************/
  88. SDouble ExpBS(const SDouble& x); // 3318 ver. 2.18
  89. SNManager::SNErrorFlag ExpArgCheck(const SDouble& x, int funcID);// 3319 ver. 2.18
  90. /*
  91. It evaluates cos(x) and sin(x) using the binary splitting method. ver. 2.18
  92. */
  93. struct TrigFuncValues {
  94. int quadrant;
  95. SDouble angle, cos, sin, tan;
  96. TrigFuncValues() {}
  97. TrigFuncValues(const SDouble& a, const SDouble& c, const SDouble& s, const SDouble& t)
  98. : angle(a), cos(c), sin(s), tan(t) {}
  99. TrigFuncValues(const TrigFuncValues& a);
  100. TrigFuncValues& operator=(const TrigFuncValues& a);
  101. };
  102. int CosSinBS(const SDouble& x, SDouble& cosVal, SDouble& sinVal); // 3320
  103. TrigFuncValues CosSinBS(const SDouble& x, bool getTan = false); // 3321
  104. SDouble CosBS(const SDouble& x); // 3322 ver. 2.18
  105. SDouble SinBS(const SDouble& x); // 3323 ver. 2.18
  106. SDouble TanBS(const SDouble& x); // 3324 ver. 2.18
  107. /**************************************************************
  108. It evaluates hyperbolic functions using binary splitting method.
  109. ***************************************************************/
  110. void Hyperbolic(const SDouble& x, SDouble& ch, SDouble& sh);
  111. SDouble CoshBS(const SDouble& x); // 3325 ver. 2.18
  112. SDouble SinhBS(const SDouble& x); // 3326 ver. 2.18
  113. SDouble TanhBS(const SDouble& x); // 3327 ver. 2.18
  114. SDouble TanhBSLx(const SDouble& x); //for large x rename "TanBS(x)" since version 2.21
  115. SDouble Log1_X(const SDouble& x); // log(1 - x) |x|<<1.0 3403
  116. SDouble LogE(const SDouble& x); // log(x) using Exp(x) 3404
  117. SDouble LogAGM(const SDouble& x); // log(x) using AGM. 3405
  118. SDouble LogNW(const SDouble& x); //log(x) using Newton's method 3406 ver. 2.18
  119. /*--------------------------------------------------------------------------
  120. Get a calculation method for log(x) functions.
  121. x = X * 10^exp
  122. log(x) = log(X)+exp*log(10), add = exp*log(10)
  123. It returns "true" if x = 1 or 10, set X = 0 or log(10).
  124. ----------------------------------------------------------------------------*/
  125. bool GetLogxCalcMethod(const SDouble& x, SDouble& X, SDouble& add); //3408
  126. // constants
  127. /*************************************************
  128. Entry constants
  129. userV : a value given by user
  130. snV : result, snV != NULL
  131. pfCalcFunc: default function of calculation
  132. maxSize : figure of *snV
  133. Used for Pi(), E(), Log10() functions
  134. **************************************************/
  135. void EntryConst(const SDouble* userV, SDouble* snV,
  136. SDouble (*pfCalcFunc)(), uint* maxSize); // 3500
  137. SDouble SNE(); // The base of natural logarithm using BSE() below. 3501
  138. SDouble SNLog10(); // log(10.0) 3502
  139. SDouble SNPi(); // pi : 3503 Chudnovskys' formura is used since ver 2.30
  140. //inline SDouble SNPi() { return ChudnovskysPi(); } is not used. Because Pi() needs the function pointer of SNPi().
  141. SDouble GaussPi(); // pi(Gauss's formula) 3504 rename since ver 2.18
  142. SDouble MachinPi(); // pi(Machin's formula) 3505 rename since ver 2.18
  143. //RPi() is fast but out of memory occure in the FFT for the calculation 1/sqrt(2.0)
  144. //and reciprocal.
  145. SDouble RamanujanPi(); //pi by Ramanujan's formula 3508 rename since ver 2.30
  146. SDouble RamanujanPi2(); //pi by Ramanujan's formula 3512 since ver 2.30
  147. SDouble StomerPi(); //pi by Stomer's formula 3509 rename since ver 2.18
  148. SDouble KlingenstiernaPi(); //pi by Klingenstierna 3510 rename since ver 2.18
  149. SDouble MPi2(); // pi/2 3511
  150. SDouble MPi4(); // pi/4 3550
  151. SDouble RecLog10(); // 1/log(10.0) 3551
  152. SDouble AGMPi(); //pi by AGM method 3552
  153. //pi by Borweins' method improved by by Takahashi and Kanada.
  154. SDouble BorweinsPi(); // 3553 rename since ver 2.18
  155. //Evaluation of e by binary splitting algorithm
  156. SDouble BSE(); // default method of e 3554 ver. 2.17
  157. SDouble ChudnovskysPi(); // pi by Chudnovskys' formura 3555 ver. 2.17 rename since ver 2.30
  158. /**************************
  159. arctan functions in DRADIX
  160. ***************************/
  161. // arctan2 function(reference)
  162. SDouble Datan2L(ulong num, ulong den); //3601
  163. // arctan(x) = arcsin(x/sqrt(1+x*x))
  164. SDouble AtanA(const SDouble& x); //3602
  165. // arctan(x) = arctan(y) + arctan( (x-y)/(1+x*y) )
  166. SDouble AtanT(const SDouble& x); //3603
  167. /******************************************************************
  168. arctan(x) by series
  169. arctan x = x - x^3/3 + x^5/5 -....
  170. For the meaning of "fe", see "AsinSeries()".
  171. Usually set fe = x.RdxExp().
  172. ******************************************************************/
  173. SDouble AtanSeries(const SDouble& x, int fe); //3604
  174. /***********************************************************************
  175. Set a constant on memory by reading from a text file.
  176. 1. fname = file name
  177. 2. Set pfunc = Log10, E or Pi. Do not check that the given value is correct.
  178. 3. Set "pfCalcFunc" a function pointer for the calculation.If sufficient length
  179. (accuracy)in the file, NULL is ok.If pfCalcFunc == NULL and length is not enough,
  180. a syntax error occures in "EntryConst()".
  181. 4. Set "ef" your effective figures in DRADIX. If ef = 0, set current effective
  182. figures given by "EffFig()".
  183. An example:
  184. SetConstByFile("pi.snc", Pi, NULL, 10000u);
  185. 10000*4 = 40000 digits in decimal are read from "pi.snc" file and set in "Pi()".
  186. return 1 : enough length in file
  187. return 0 : not enough, then needs calculation calling (*pfCalcFunc)()
  188. *************************************************************************/
  189. bool SetConstByFile(const char* fname,
  190. SDouble (*pfunc)(const SDouble* c, SDouble (*pf)()),
  191. SDouble (*pfCalcFunc)(), uint ef); // 3700
  192. // SLong and SInteger class
  193. SInteger BFact(ulong n); // n! in BRADIX 4102
  194. SInteger BdFact(ulong n);// n!(partially multiplication method)BRADIX and FFT 4106
  195. SInteger Ipow(const SInteger& x, ulong n); // n-th power of x(x^n) 4106
  196. SLong combPF(ulong n, ulong k);// combination number(binomial) nCk using "PrimeFactor" 4107 since ver 2.182
  197. SLong permL(const ulong n, const ulong r); // permutation number nPr 4108 since ver 2.182
  198. /********** Using SDecimal arithmetric *****************/
  199. // arctan(num/den); for num < den
  200. SDecimal Batan2(ulong num, ulong den); // 5101
  201. SDecimal BE(); // base of natural logarithms 5102
  202. SDecimal BGaussPi(); // pi by Gauss's fromula 5103 rename since ver 2.18
  203. SDecimal BMachinPi(); // pi by Machin's formula 5104 rename since ver 2.18
  204. // Batan2 convert to DRADIX
  205. SDouble Atan2L(ulong num, ulong den); // 5105
  206. SDecimal BStomerPi(); // pi by Stomer's formula 5106 rename since ver 2.18
  207. SDecimal BKlingenstiernaPi(); // pi by Klingenstierna 5107
  208. /**********************************************************
  209. trigonometric functions in BRAIDX
  210. Not so fast. Nearly same as Cos() and Sin().
  211. Cos(), Sin() are faster for short decimal as x = 0.1;
  212. BcosSeries and BsinSeries are used as assistant functions of SDouble ones.
  213. ************************************************************/
  214. SDecimal Bcos(const SDouble& x); //5201
  215. SDecimal BcosSeries(const SDouble& x); //5202
  216. SDecimal Bsin(const SDouble& x); //5203
  217. SDecimal BsinSeries(const SDouble& x); //5204
  218. // reference function
  219. SDecimal BexpSeries(const SDouble& x); //5211
  220. /************************
  221. inline functions group
  222. **************************/
  223. // abusolute value
  224. inline SLong Labs(const SLong& x){
  225. if(x.Sign() < 0 ) return -x;
  226. return x;
  227. }
  228. inline SDouble Dabs(const SDouble& x){
  229. if(x.SNSign() < 0) return -x;
  230. return x;
  231. }
  232. inline SInteger Iabs(const SInteger& x){
  233. if(x.Sign() < 0 ) return -x;
  234. return x;
  235. }
  236. inline SDecimal Xabs(const SDecimal& x){
  237. if(x.Sign() < 0) return -x;
  238. return x;
  239. }
  240. /// over loaded function absolute value "abs(x)"
  241. inline SDouble abs(const SDouble& x){ return Dabs(x); } // over load
  242. inline SLong abs(const SLong& x){ return Labs(x); } // over load
  243. inline SInteger abs(const SInteger& x) { return Iabs(x); }
  244. inline SDecimal abs(const SDecimal& x) { return Xabs(x); }
  245. #endif // SN_FUNCTIONS_H

snfunc.h : last modifiled at 2017/09/07 16:25:50(12,822 bytes)
created at 2016/04/11 11:18:58
The creation time of this html file is 2017/10/11 16:07:52 (Wed Oct 11 16:07:52 2017).