00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef KSPREAD_VALUECALC
00021 #define KSPREAD_VALUECALC
00022
00023 #include <map>
00024
00025 #include <qvaluelist.h>
00026 #include <qvaluevector.h>
00027
00028 #include "kspread_value.h"
00029
00030 namespace KSpread
00031 {
00032 class Doc;
00033 class ValueCalc;
00034 class ValueConverter;
00035
00036
00037 enum Comp { isEqual, isLess, isGreater, lessEqual, greaterEqual, notEqual };
00038 enum Type { numeric, string };
00039
00040 struct Condition
00041 {
00042 Comp comp;
00043 int index;
00044 double value;
00045 QString stringValue;
00046 Type type;
00047 };
00048
00049 typedef QValueList<Condition> ConditionList;
00050
00051 typedef void (*arrayWalkFunc) (ValueCalc *, Value &result,
00052 Value val, Value param);
00053
00069 class ValueCalc {
00070 public:
00071 ValueCalc (ValueConverter* c);
00072
00073 ValueConverter *conv () { return converter; };
00074
00075
00076 void setDoc (Doc *d) { _doc = d; };
00077 Doc *doc () { return _doc; };
00078
00080 Value add (const Value &a, const Value &b);
00081 Value sub (const Value &a, const Value &b);
00082 Value mul (const Value &a, const Value &b);
00083 Value div (const Value &a, const Value &b);
00084 Value mod (const Value &a, const Value &b);
00085 Value pow (const Value &a, const Value &b);
00086 Value sqr (const Value &a);
00087 Value sqrt (const Value &a);
00088 Value add (const Value &a, double b);
00089 Value sub (const Value &a, double b);
00090 Value mul (const Value &a, double b);
00091 Value div (const Value &a, double b);
00092 Value pow (const Value &a, double b);
00093 Value abs (const Value &a);
00094
00096 bool isZero (const Value &a);
00097 bool isEven (const Value &a);
00099 bool equal (const Value &a, const Value &b);
00101 bool approxEqual (const Value &a, const Value &b);
00103 bool greater (const Value &a, const Value &b);
00105 bool gequal (const Value &a, const Value &b);
00107 bool lower (const Value &a, const Value &b);
00109 bool strEqual (const Value &a, const Value &b);
00110 int sign (const Value &a);
00111
00113 Value roundDown (const Value &a, const Value &digits);
00114 Value roundUp (const Value &a, const Value &digits);
00115 Value round (const Value &a, const Value &digits);
00116 Value roundDown (const Value &a, int digits = 0);
00117 Value roundUp (const Value &a, int digits = 0);
00118 Value round (const Value &a, int digits = 0);
00119
00121 Value log (const Value &number, const Value &base);
00122 Value log (const Value &number, double base = 10);
00123 Value ln (const Value &number);
00124 Value exp (const Value &number);
00125
00127 Value pi ();
00128 Value eps ();
00129
00131 Value random (double range = 1.0);
00132 Value random (Value range);
00133
00135 Value fact (const Value &which);
00136 Value fact (const Value &which, const Value &end);
00137 Value fact (int which, int end = 0);
00139 Value factDouble (int which);
00140 Value factDouble (Value which);
00141
00143 Value combin (int n, int k);
00144 Value combin (Value n, Value k);
00145
00147 Value gcd (const Value &a, const Value &b);
00149 Value lcm (const Value &a, const Value &b);
00150
00152 Value base (const Value &val, int base = 16, int prec = 0);
00154 Value fromBase (const Value &val, int base = 16);
00155
00157 Value sin (const Value &number);
00158 Value cos (const Value &number);
00159 Value tg (const Value &number);
00160 Value cotg (const Value &number);
00161 Value asin (const Value &number);
00162 Value acos (const Value &number);
00163 Value atg (const Value &number);
00164 Value atan2 (const Value &y, const Value &x);
00165
00167 Value sinh (const Value &number);
00168 Value cosh (const Value &number);
00169 Value tgh (const Value &number);
00170 Value asinh (const Value &number);
00171 Value acosh (const Value &number);
00172 Value atgh (const Value &number);
00173
00177 Value phi (Value x);
00178 Value gauss (Value xx);
00179 Value gaussinv (Value xx);
00180 Value GetGamma (Value _x);
00181 Value GetLogGamma (Value _x);
00182 Value GetGammaDist (Value _x, Value _alpha,
00183 Value _beta);
00184 Value GetBeta (Value _x, Value _alpha,
00185 Value _beta);
00186
00188 Value besseli (Value v, Value x);
00189 Value besselj (Value v, Value x);
00190 Value besselk (Value v, Value x);
00191 Value besseln (Value v, Value x);
00192
00194 Value erf (Value x);
00195 Value erfc (Value x);
00196
00198 void arrayWalk (const Value &range, Value &res,
00199 arrayWalkFunc func, Value param);
00202 void arrayWalk (QValueVector<Value> &range, Value &res,
00203 arrayWalkFunc func, Value param);
00204 void twoArrayWalk (const Value &a1, const Value &a2,
00205 Value &res, arrayWalkFunc func);
00206 void twoArrayWalk (QValueVector<Value> &a1,
00207 QValueVector<Value> &a2, Value &res, arrayWalkFunc func);
00208 arrayWalkFunc awFunc (const QString &name);
00209 void registerAwFunc (const QString &name, arrayWalkFunc func);
00210
00212
00213 Value sum (const Value &range, bool full = true);
00214 Value sumsq (const Value &range, bool full = true);
00215 Value sumIf (const Value &range,
00216 const Value &checkRange, const Condition &cond);
00217 int count (const Value &range, bool full = true);
00218 int countIf (const Value &range, const Condition &cond);
00219 Value avg (const Value &range, bool full = true);
00220 Value max (const Value &range, bool full = true);
00221 Value min (const Value &range, bool full = true);
00222 Value product (const Value &range, Value init,
00223 bool full = true);
00224 Value stddev (const Value &range, bool full = true);
00225 Value stddev (const Value &range, Value avg,
00226 bool full = true);
00227 Value stddevP (const Value &range, bool full = true);
00228 Value stddevP (const Value &range, Value avg,
00229 bool full = true);
00230
00232 Value sum (QValueVector<Value> range, bool full = true);
00233 int count (QValueVector<Value> range, bool full = true);
00234 Value avg (QValueVector<Value> range, bool full = true);
00235 Value max (QValueVector<Value> range, bool full = true);
00236 Value min (QValueVector<Value> range, bool full = true);
00237 Value product (QValueVector<Value> range, Value init,
00238 bool full = true);
00239 Value stddev (QValueVector<Value> range, bool full = true);
00240 Value stddev (QValueVector<Value> range, Value avg,
00241 bool full = true);
00242 Value stddevP (QValueVector<Value> range, bool full = true);
00243 Value stddevP (QValueVector<Value> range, Value avg,
00244 bool full = true);
00245
00250 void getCond (Condition &cond, Value val);
00251
00256 bool matches (const Condition &cond, Value d);
00257
00258 protected:
00259 ValueConverter* converter;
00261 Value::Format format (Value::Format a, Value::Format b);
00262
00263 Doc *_doc;
00264
00266 std::map<QString, arrayWalkFunc> awFuncs;
00267 };
00268
00269 }
00270
00271
00272 #endif // KSPREAD_VALUECALC
00273