KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
9.1KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. * Boston, MA 02110-1301, USA.
  15. */
  16. #ifndef KSPREAD_VALUECALC
  17. #define KSPREAD_VALUECALC
  18. #include <map>
  19. #include <tqvaluelist.h>
  20. #include <tqvaluevector.h>
  21. #include "kspread_value.h"
  22. namespace KSpread
  23. {
  24. class Doc;
  25. class ValueCalc;
  26. class ValueConverter;
  27. // Condition structures
  28. enum Comp { isEqual, isLess, isGreater, lessEqual, greaterEqual, notEqual };
  29. enum Type { numeric, string };
  30. struct Condition
  31. {
  32. Comp comp;
  33. int index;
  34. double value;
  35. TQString stringValue;
  36. Type type;
  37. };
  38. typedef TQValueList<Condition> ConditionList;
  39. typedef void (*arrayWalkFunc) (ValueCalc *, Value &result,
  40. Value val, Value param);
  41. /**
  42. The ValueCalc class is used to perform all sorts of calculations.
  43. No other means of calculation should be performed, to achieve
  44. transparency, and to ease addition of new datatypes.
  45. Currently, most functions simply convert data to double and work with that.
  46. The idea is such that after we add support for bigger precision, we only need
  47. to adjust this class and the parsing/formatting/converting classes. All
  48. function implementations will remain exactly the same.
  49. Of course, for some functions, it might be impossible to apply them
  50. on all datatypes, but since all of them can be applied on both
  51. doubles and GnuMP-based numbers, that is not of much concern ;)
  52. */
  53. class ValueCalc {
  54. public:
  55. ValueCalc (ValueConverter* c);
  56. ValueConverter *conv () { return converter; };
  57. // some functions need access to the document
  58. void setDoc (Doc *d) { _doc = d; };
  59. Doc *doc () { return _doc; };
  60. /** basic arithmetic operations */
  61. Value add (const Value &a, const Value &b);
  62. Value sub (const Value &a, const Value &b);
  63. Value mul (const Value &a, const Value &b);
  64. Value div (const Value &a, const Value &b);
  65. Value mod (const Value &a, const Value &b);
  66. Value pow (const Value &a, const Value &b);
  67. Value sqr (const Value &a);
  68. Value sqrt (const Value &a);
  69. Value add (const Value &a, double b);
  70. Value sub (const Value &a, double b);
  71. Value mul (const Value &a, double b);
  72. Value div (const Value &a, double b);
  73. Value pow (const Value &a, double b);
  74. Value abs (const Value &a);
  75. /** comparison and related */
  76. bool isZero (const Value &a);
  77. bool isEven (const Value &a);
  78. /** numerical comparison */
  79. bool equal (const Value &a, const Value &b);
  80. /** numerical comparison with a little epsilon tolerance */
  81. bool approxEqual (const Value &a, const Value &b);
  82. /** numerical comparison */
  83. bool greater (const Value &a, const Value &b);
  84. /** numerical comparison - greater or equal */
  85. bool gequal (const Value &a, const Value &b);
  86. /** numerical comparison */
  87. bool lower (const Value &a, const Value &b);
  88. /** string comparison */
  89. bool strEqual (const Value &a, const Value &b);
  90. int sign (const Value &a);
  91. /** rounding */
  92. Value roundDown (const Value &a, const Value &digits);
  93. Value roundUp (const Value &a, const Value &digits);
  94. Value round (const Value &a, const Value &digits);
  95. Value roundDown (const Value &a, int digits = 0);
  96. Value roundUp (const Value &a, int digits = 0);
  97. Value round (const Value &a, int digits = 0);
  98. /** logarithms and exponentials */
  99. Value log (const Value &number, const Value &base);
  100. Value log (const Value &number, double base = 10);
  101. Value ln (const Value &number);
  102. Value exp (const Value &number);
  103. /** constants */
  104. Value pi ();
  105. Value eps ();
  106. /** random number from <0.0, range) */
  107. Value random (double range = 1.0);
  108. Value random (Value range);
  109. /** some computational functions */
  110. Value fact (const Value &which);
  111. Value fact (const Value &which, const Value &end);
  112. Value fact (int which, int end = 0);
  113. /** double factorial (every other number multiplied) */
  114. Value factDouble (int which);
  115. Value factDouble (Value which);
  116. /** combinations */
  117. Value combin (int n, int k);
  118. Value combin (Value n, Value k);
  119. /** greatest common divisor */
  120. Value gcd (const Value &a, const Value &b);
  121. /** lowest common multiplicator */
  122. Value lcm (const Value &a, const Value &b);
  123. /** base conversion 10 -> base */
  124. Value base (const Value &val, int base = 16, int prec = 0);
  125. /** base conversion base -> 10 */
  126. Value fromBase (const Value &val, int base = 16);
  127. /** goniometric functions */
  128. Value sin (const Value &number);
  129. Value cos (const Value &number);
  130. Value tg (const Value &number);
  131. Value cotg (const Value &number);
  132. Value asin (const Value &number);
  133. Value acos (const Value &number);
  134. Value atg (const Value &number);
  135. Value atan2 (const Value &y, const Value &x);
  136. /** hyperbolic functions */
  137. Value sinh (const Value &number);
  138. Value cosh (const Value &number);
  139. Value tgh (const Value &number);
  140. Value asinh (const Value &number);
  141. Value acosh (const Value &number);
  142. Value atgh (const Value &number);
  143. /** some statistical stuff
  144. TODO: we may want to move these over to a separate class or something,
  145. as the functions are mostly big */
  146. Value phi (Value x);
  147. Value gauss (Value xx);
  148. Value gaussinv (Value xx);
  149. Value GetGamma (Value _x);
  150. Value GetLogGamma (Value _x);
  151. Value GetGammaDist (Value _x, Value _alpha,
  152. Value _beta);
  153. Value GetBeta (Value _x, Value _alpha,
  154. Value _beta);
  155. /** bessel functions - may also end up being separated from here */
  156. Value besseli (Value v, Value x);
  157. Value besselj (Value v, Value x);
  158. Value besselk (Value v, Value x);
  159. Value besseln (Value v, Value x);
  160. /** error functions (see: man erf) */
  161. Value erf (Value x);
  162. Value erfc (Value x);
  163. /** array/range walking */
  164. void arrayWalk (const Value &range, Value &res,
  165. arrayWalkFunc func, Value param);
  166. /** Walk the array in function-like style.
  167. This method is here to avoid duplication in function handlers. */
  168. void arrayWalk (TQValueVector<Value> &range, Value &res,
  169. arrayWalkFunc func, Value param);
  170. void twoArrayWalk (const Value &a1, const Value &a2,
  171. Value &res, arrayWalkFunc func);
  172. void twoArrayWalk (TQValueVector<Value> &a1,
  173. TQValueVector<Value> &a2, Value &res, arrayWalkFunc func);
  174. arrayWalkFunc awFunc (const TQString &name);
  175. void registerAwFunc (const TQString &name, arrayWalkFunc func);
  176. /** basic range functions */
  177. // if full is true, A-version is used (means string/bool values included)
  178. Value sum (const Value &range, bool full = true);
  179. Value sumsq (const Value &range, bool full = true);
  180. Value sumIf (const Value &range,
  181. const Value &checkRange, const Condition &cond);
  182. int count (const Value &range, bool full = true);
  183. int countIf (const Value &range, const Condition &cond);
  184. Value avg (const Value &range, bool full = true);
  185. Value max (const Value &range, bool full = true);
  186. Value min (const Value &range, bool full = true);
  187. Value product (const Value &range, Value init,
  188. bool full = true);
  189. Value stddev (const Value &range, bool full = true);
  190. Value stddev (const Value &range, Value avg,
  191. bool full = true);
  192. Value stddevP (const Value &range, bool full = true);
  193. Value stddevP (const Value &range, Value avg,
  194. bool full = true);
  195. /** range functions using value lists */
  196. Value sum (TQValueVector<Value> range, bool full = true);
  197. int count (TQValueVector<Value> range, bool full = true);
  198. Value avg (TQValueVector<Value> range, bool full = true);
  199. Value max (TQValueVector<Value> range, bool full = true);
  200. Value min (TQValueVector<Value> range, bool full = true);
  201. Value product (TQValueVector<Value> range, Value init,
  202. bool full = true);
  203. Value stddev (TQValueVector<Value> range, bool full = true);
  204. Value stddev (TQValueVector<Value> range, Value avg,
  205. bool full = true);
  206. Value stddevP (TQValueVector<Value> range, bool full = true);
  207. Value stddevP (TQValueVector<Value> range, Value avg,
  208. bool full = true);
  209. /**
  210. This method parses the condition in string text to the condition cond.
  211. It sets the condition's type and value.
  212. */
  213. void getCond (Condition &cond, Value val);
  214. /**
  215. Returns true if value d matches the condition cond, built with getCond().
  216. Otherwise, it returns false.
  217. */
  218. bool matches (const Condition &cond, Value d);
  219. protected:
  220. ValueConverter* converter;
  221. /** return result formatting, based on these two values */
  222. Value::Format format (Value::Format a, Value::Format b);
  223. Doc *_doc;
  224. /** registered array-walk functions */
  225. std::map<TQString, arrayWalkFunc> awFuncs;
  226. };
  227. } //namespace KSpread
  228. #endif // KSPREAD_VALUECALC