22#include "valueconverter.h"
39 res = c-> add (res, val);
45 res = c-> add (res, val);
51 res = c-> add (res, c->sqr (val));
57 res = c-> add (res, c->sqr (val));
63 res = c-> add (res, 1);
69 res = c-> add (res, 1);
78 if (c-> greater (val, res)) res = val;
99 if (c-> lower (val, res)) res = val;
109 if (c-> lower (val, res))
117 res = c->mul (res, val);
123 res = c->mul (res, val);
131 res = c-> add (res, c->sqr (c->sub (val, avg)));
139 res = c-> add (res, c->sqr (c->sub (val, avg)));
143bool isDate ( Value val) {
144 Value::Format fmt = val. format();
145 if ((fmt == Value::fmt_Date) || (fmt == Value::fmt_DateTime))
160 registerAwFunc ( "sum", awSum);
161 registerAwFunc ( "suma", awSumA);
162 registerAwFunc ( "sumsq", awSumSq);
163 registerAwFunc ( "sumsqa", awSumSqA);
164 registerAwFunc ( "count", awCount);
165 registerAwFunc ( "counta", awCountA);
166 registerAwFunc ( "max", awMax);
167 registerAwFunc ( "maxa", awMaxA);
168 registerAwFunc ( "min", awMin);
169 registerAwFunc ( "mina", awMinA);
170 registerAwFunc ( "prod", awProd);
171 registerAwFunc ( "proda", awProdA);
172 registerAwFunc ( "devsq", awDevSq);
173 registerAwFunc ( "devsqa", awDevSq);
185 aa = converter->asInteger (a).asInteger();
186 bb = converter->asInteger (b).asInteger();
187 res = Value (aa + bb);
192 aa = converter->asFloat (a).asFloat();
193 bb = converter->asFloat (b).asFloat();
194 res = Value (aa + bb);
200 if (isDate(a) && isDate(b))
211 aa = converter->asFloat (a). asFloat();
212 bb = converter->asFloat (b). asFloat();
218 if (isDate(a) && isDate(b))
229 aa = converter->asFloat (a). asFloat();
230 bb = converter->asFloat (b). asFloat();
236 if (isDate(a) && isDate(b))
247 aa = converter->asFloat (a).asFloat();
248 bb = converter->asFloat (b).asFloat();
253 res = Value (aa / bb);
258 if (isDate(a) && isDate(b))
269 aa = converter->asFloat (a).asFloat();
270 bb = converter->asFloat (b).asFloat();
276 double m = fmod (aa, bb);
281 if ((bb > 0) && (aa < 0))
282 while (m < 0) m += bb;
286 while (m < bb) m -= bb;
287 while (m > 0) m += bb;
295 if (isDate(a) && isDate(b))
306 aa = converter->asFloat (a).asFloat();
307 bb = converter->asFloat (b).asFloat();
308 Value res = Value (::pow (aa, bb));
313 if (isDate(a) || isDate(b))
328 Value res = Value (::sqrt (converter->asFloat(a).asFloat()));
341 Value res = Value (converter->asFloat(a).asFloat() + b);
349Value ValueCalc::sub ( const Value &a, double b)
352 Value res = Value (converter->asFloat(a).asFloat() - b);
360Value ValueCalc::mul ( const Value &a, double b)
363 Value res = Value (converter->asFloat(a).asFloat() * b);
371Value ValueCalc::div ( const Value &a, double b)
378 res = Value (converter->asFloat(a).asFloat() / b);
386Value ValueCalc::pow ( const Value &a, double b)
389 Value res = Value (::pow (converter->asFloat(a).asFloat(), b));
400 return Value (fabs (converter->asFloat (a).asFloat()));
406 return (converter->asFloat (a).asFloat() == 0.0);
409bool ValueCalc::isEven ( const Value &a)
412 return ((converter->asInteger (a). asInteger() % 2) == 0);
417 return (converter->asFloat (a).asFloat() == converter->asFloat (b).asFloat());
428 double aa = converter->asFloat (a).asFloat();
429 double bb = converter->asFloat (b).asFloat();
433 return (x < 0.0 ? -x : x) < ((aa < 0.0 ? -aa : aa) * DBL_EPSILON);
438 TQString aa = converter->asString(a).asString();
439 TQString bb = converter->asString(b).asString();
449 TQString aa = converter->asString(a).asString();
450 TQString bb = converter->asString(b).asString();
460 TQString aa = converter->asString(a).asString();
461 TQString bb = converter->asString(b).asString();
504 double aa = converter->asFloat (a).asFloat();
505 double bb = converter->asFloat (b).asFloat();
520 const Value &digits) {
525 const Value &digits) {
526 return roundUp (a, converter->asInteger (digits). asInteger());
530 const Value &digits) {
531 return round (a, converter->asInteger (digits). asInteger());
539 for ( int i = 0; i < digits; ++i)
542 for ( int i = 0; i < digits; ++i)
545 val = Value (floor (converter->asFloat (val).asFloat()));
548 for ( int i = 0; i < digits; ++i)
551 for ( int i = 0; i < digits; ++i)
556Value ValueCalc::roundUp ( const Value &a, int digits)
561 for ( int i = 0; i < digits; ++i)
564 for ( int i = 0; i < digits; ++i)
567 val = Value (ceil (converter->asFloat (val).asFloat()));
570 for ( int i = 0; i < digits; ++i)
573 for ( int i = 0; i < digits; ++i)
578Value ValueCalc::round ( const Value &a, int digits)
583 for ( int i = 0; i < digits; ++i)
586 for ( int i = 0; i < digits; ++i)
589 val = Value ( int(converter->asFloat (val).asFloat()+0.5));
592 for ( int i = 0; i < digits; ++i)
595 for ( int i = 0; i < digits; ++i)
600int ValueCalc::sign ( const Value &a)
602 double val = converter-> asFloat (a).asFloat ();
603 if (val == 0) return 0;
604 if (val > 0) return 1;
612 double logbase = converter-> asFloat ( base).asFloat();
618 logbase = log10 (logbase);
619 Value res = Value (log10 (converter->asFloat (number).asFloat()) / logbase);
644 double num = converter->asFloat (number). asFloat();
655 return Value (::exp (converter->asFloat (number).asFloat()));
666Value ValueCalc::eps ()
670 return Value (DBL_EPSILON);
675 return Value (range * ( double) rand() / (RAND_MAX + 1.0));
687 return fact (converter->asInteger (which).asInteger());
696 converter->asInteger (end). asInteger ());
708 return (mul ( fact (which-1, end), which));
715 if ((which == 0) || (which == 1))
730 double result = ::exp(lgamma (n + 1) - lgamma (k + 1) - lgamma (n-k+1));
731 return Value (floor(result + 0.5));
739 int nn = converter->asInteger (n). asInteger();
740 int kk = converter->asInteger (k). asInteger();
747 Value aa = round (a);
748 Value bb = round (b);
752 if (aa.isZero()) return bb;
753 if (bb.isZero()) return aa;
757 return gcd (bb, mod (aa, bb));
759 return gcd (aa, mod (bb, aa));
764 Value aa = round (a);
765 Value bb = round (b);
769 if (aa.isZero()) return bb;
770 if (bb.isZero()) return aa;
776 return div (mul (aa, bb), g);
781 if ( base == 10) return round (val, prec);
782 if (prec < 0) prec = 2;
786 double value = converter->asFloat (val).asFloat();
787 TQString result = TQString::number (( int)value, base);
791 result += "."; value = value - (int)value;
794 for( int i = 0; i < prec; i++ )
796 ix = (int) value * base;
797 result += "0123456789abcdefghijklmnopqrstuvwxyz"[ix];
798 value = base * (value - (double)ix/ base);
802 return Value (result.upper());
807 TQString str = converter-> asString (val).asString();
809 double num = str.toLong (&ok, base);
848 Value res = Value (div (1, ::tan (converter->asFloat (number).asFloat())));
859 Value res = Value (::asin (converter->asFloat (number).asFloat()));
872 Value res = Value (::acos (converter->asFloat (number).asFloat()));
885 Value res = Value (::atan (converter->asFloat (number).asFloat()));
897 double yy = converter-> asFloat (y).asFloat();
898 double xx = converter->asFloat (x).asFloat();
899 return Value (::atan2 (yy, xx));
935 Value res = Value (::asinh (converter->asFloat (number).asFloat()));
948 Value res = Value (::acosh (converter->asFloat (number).asFloat()));
961 Value res = Value (::atanh (converter->asFloat (number).asFloat()));
973 Value constant (0.39894228040143268);
976 Value x2neg = mul (sqr (x), -1);
977 return mul (constant, exp (div (x2neg, 2.0)));
980static double taylor_helper ( double* pPolynom, uint nMax, double x)
982 double nVal = pPolynom[nMax];
983 for ( int i = nMax-1; i >= 0; i--) {
984 nVal = pPolynom[i] + (nVal * x);
992 double x = converter->asFloat (xx).asFloat();
995 { 0.39894228040143268, -0.06649038006690545, 0.00997355701003582,
996 -0.00118732821548045, 0.00011543468761616, -0.00000944465625950,
997 0.00000066596935163, -0.00000004122667415, 0.00000000227352982,
998 0.00000000011301172, 0.00000000000511243, -0.00000000000021218 };
1000 { 0.47724986805182079, 0.05399096651318805, -0.05399096651318805,
1001 0.02699548325659403, -0.00449924720943234, -0.00224962360471617,
1002 0.00134977416282970, -0.00011783742691370, -0.00011515930357476,
1003 0.00003704737285544, 0.00000282690796889, -0.00000354513195524,
1004 0.00000037669563126, 0.00000019202407921, -0.00000005226908590,
1005 -0.00000000491799345, 0.00000000366377919, -0.00000000015981997,
1006 -0.00000000017381238, 0.00000000002624031, 0.00000000000560919,
1007 -0.00000000000172127, -0.00000000000008634, 0.00000000000007894 };
1009 { 0.49996832875816688, 0.00013383022576489, -0.00026766045152977,
1010 0.00033457556441221, -0.00028996548915725, 0.00018178605666397,
1011 -0.00008252863922168, 0.00002551802519049, -0.00000391665839292,
1012 -0.00000074018205222, 0.00000064422023359, -0.00000017370155340,
1013 0.00000000909595465, 0.00000000944943118, -0.00000000329957075,
1014 0.00000000029492075, 0.00000000011874477, -0.00000000004420396,
1015 0.00000000000361422, 0.00000000000143638, -0.00000000000045848 };
1016 double asympt[] = { -1.0, 1.0, -3.0, 15.0, -105.0 };
1018 double xAbs = fabs(x);
1019 uint xShort = static_cast<uint >(floor(xAbs));
1022 nVal = taylor_helper(t0, 11, (xAbs * xAbs)) * xAbs;
1023 else if ((xShort >= 1) && (xShort <= 2))
1024 nVal = taylor_helper(t2, 23, (xAbs - 2.0));
1025 else if ((xShort >= 3) && (xShort <= 4))
1026 nVal = taylor_helper(t4, 20, (xAbs - 4.0));
1029 double phiAbs = converter->asFloat ( phi (xAbs)).asFloat();
1030 nVal = 0.5 + phiAbs * taylor_helper(asympt, 4, 1.0 / (xAbs * xAbs)) / xAbs;
1034 return Value (-nVal);
1036 return Value (nVal);
1042 double x = converter->asFloat (xx).asFloat();
1061 t*2509.0809287301226727+33430.575583588128105
1063 *t+67265.770927008700853
1065 *t+45921.953931549871457
1067 *t+13731.693765509461125
1069 *t+1971.5909503065514427
1071 *t+133.14166789178437745
1073 *t+3.387132872796366608
1083 t*5226.495278852854561+28729.085735721942674
1085 *t+39307.89580009271061
1087 *t+21213.794301586595867
1089 *t+5394.1960214247511077
1091 *t+687.1870074920579083
1093 *t+42.313330701600911252
1104 t=::sqrt(- ::log(t));
1118 t*7.7454501427834140764e-4+0.0227238449892691845833
1120 *t+0.24178072517745061177
1122 *t+1.27045825245236838258
1124 *t+3.64784832476320460504
1126 *t+5.7694972214606914055
1128 *t+4.6303378461565452959
1130 *t+1.42343711074968357734
1140 t*1.05075007164441684324e-9+5.475938084995344946e-4
1142 *t+0.0151986665636164571966
1144 *t+0.14810397642748007459
1146 *t+0.68976733498510000455
1148 *t+1.6763848301838038494
1150 *t+2.05319162663775882187
1168 t*2.01033439929228813265e-7+2.71155556874348757815e-5
1170 *t+0.0012426609473880784386
1172 *t+0.026532189526576123093
1174 *t+0.29656057182850489123
1176 *t+1.7848265399172913358
1178 *t+5.4637849111641143699
1180 *t+6.6579046435011037772
1190 t*2.04426310338993978564e-15+1.4215117583164458887e-7
1192 *t+1.8463183175100546818e-5
1194 *t+7.868691311456132591e-4
1196 *t+0.0148753612908506148525
1198 *t+0.13692988092273580531
1200 *t+0.59983220655588793769
1214static double GammaHelp( double& x, bool& bReflect)
1216 double c[6] = {76.18009173, -86.50532033, 24.01409822,
1217 -1.231739516, 0.120858003E-2, -0.536382E-5};
1231 for (uint i = 0; i < 6; i++)
1242 double x = converter->asFloat (_x).asFloat();
1245 double G = GammaHelp(x, bReflect);
1246 G = ::pow(x+5.5,x+0.5)*G/::exp(x+5.5);
1248 G = M_PI*x/(G* ::sin(M_PI*x));
1254 double x = converter->asFloat (_x).asFloat();
1257 double G = GammaHelp(x, bReflect);
1267 double x = converter->asFloat (_x).asFloat();
1268 double alpha = converter->asFloat (_alpha).asFloat();
1269 double beta = converter->asFloat (_beta).asFloat();
1275 double gamma = alpha;
1277 double c = 0.918938533204672741;
1279 0.833333333333333333E-1,
1280 -0.277777777777777778E-2,
1281 0.793650793650793651E-3,
1282 -0.595238095238095238E-3,
1283 0.841750841750841751E-3,
1284 -0.191752691752691753E-2,
1285 0.641025641025641025E-2,
1286 -0.295506535947712418E-1,
1287 0.179644372368830573,
1288 -0.139243221690590111E1
1292 double dgamma = gamma;
1297 while ( z < 10.0 ) {
1306 double a = ( z - 0.5 ) * ::log(z) - z + c;
1307 double b = d[0]/z + d[1]/z3 + d[2]/z5 + d[3]/(z2*z5) + d[4]/(z4*z5) +
1308 d[5]/(z*z5*z5) + d[6]/(z3*z5*z5) + d[7]/(z5*z5*z5) + d[8]/(z2*z5*z5*z5);
1311 double sum = 1.0 / dgamma;
1312 double term = 1.0 / dgamma;
1313 double cut1 = dx - dgamma;
1314 double cut2 = dx * 10000000000.0;
1316 for ( int i=1; i<=maxit; i++ ) {
1318 term = dx * term / ( dgamma + ai );
1320 double cutoff = cut1 + ( cut2 * term / sum );
1321 if ( ai > cutoff ) {
1324 return Value (::exp( dgamma * ::log(dx) - dx - a - b ) * t * den);
1334 if ( equal (_beta, 1.0))
1335 return pow (_x, _alpha);
1336 else if ( equal (_alpha, 1.0))
1338 return sub (1.0, pow (sub (1.0, _x), _beta));
1340 double x = converter->asFloat (_x).asFloat();
1341 double alpha = converter->asFloat (_alpha).asFloat();
1342 double beta = converter->asFloat (_beta).asFloat();
1344 double fEps = 1.0E-8;
1348 if (x < (alpha+1.0)/(alpha+beta+1.0)) {
1362 double a1, b1, a2, b2, fnorm, rm, apl2m, d2m, d2m1, cfnew;
1364 b2 = 1.0 - (fA+fB)*x/(fA+1.0);
1376 for (uint j = 1; j <= 100; j++) {
1378 apl2m = fA + 2.0*rm;
1379 d2m = rm*(fB-rm)*x/((apl2m-1.0)*apl2m);
1380 d2m1 = -(fA+rm)*(fA+fB+rm)*x/(apl2m*(apl2m+1.0));
1381 a1 = (a2+d2m*a1)*fnorm;
1382 b1 = (b2+d2m*b1)*fnorm;
1383 a2 = a1 + d2m1*a2*fnorm;
1384 b2 = b1 + d2m1*b2*fnorm;
1388 if (fabs(cf-cfnew)/cf < fEps)
1397 b1 = ::exp(GetLogGamma(fA).asFloat()+GetLogGamma(fB).asFloat()-
1398 GetLogGamma(fA+fB).asFloat());
1400 cf *= ::pow(x, fA)*::pow(1.0-x,fB)/(fA*b1);
1403 return Value (1.0-cf);
1423static double ccmath_gaml( double x)
1425 for(g=1.; x<30. ;g*=x,x+=1.); h=x*x;
1426 g=(x-.5)*log(x)-x+.918938533204672-log(g);
1427 g+=(1.-(1./6.-(1./3.-1./(4.*h))/(7.*h))/(5.*h))/(12.*x);
1431static double ccmath_psi( int m)
1432{ double s= -.577215664901533; int k;
1433 for(k=1; k<m ;++k) s+=1./k;
1437static double ccmath_ibes( double v, double x)
1438{ double y,s,t,tp; int p,m;
1439 y=x-9.; if(y>0.) y*=y; tp=v*v*.2+25.;
1440 if(y<tp){ x/=2.; m=(int)x;
1441 if(x>0.) s=t=exp(v*log(x)-ccmath_gaml(v+1.));
1442 else{ if(v>0.) return 0.; else if(v==0.) return 1.;}
1443 for(p=1,x*=x;;++p){ t*=x/(p*(v+=1.)); s+=t;
1444 if(p>m && t<1.e-13*s) break;
1447 else{ double u,a0=1.57079632679490;
1448 s=t=1./sqrt(x*a0); x*=2.; u=0.;
1449 for(p=1,y=.5; (tp=fabs(t))>1.e-14 ;++p,y+=1.){
1450 t*=(v+y)*(v-y)/(p*x); if(y>v && fabs(t)>=tp) break;
1451 if(!(p&1)) s+=t; else u-=t;
1453 x/=2.; s=cosh(x)*s+sinh(x)*u;
1458static double ccmath_kbes( double v, double x)
1459{ double y,s,t,tp,f,a0=1.57079632679490;
1461 if(x==0.) return HUGE_VAL;
1462 y=x-10.5; if(y>0.) y*=y; tp=25.+.185*v*v;
1463 if(y<tp && modf(v+.5,&t)!=0.){ y=1.5+.5*v;
1464 if(x<y){ x/=2.; m=(int)x; tp=t=exp(v*log(x)-ccmath_gaml(v+1.));
1465 if(modf(v,&y)==0.){ k=(int)y; tp*=v;
1466 f=2.*log(x)-ccmath_psi(1)-ccmath_psi(k+1);
1467 t/=2.; if(!(k&1)) t= -t; s=f*t;
1468 for(p=1,x*=x;;++p){ f-=1./p+1./(v+=1.);
1469 t*=x/(p*v); s+=(y=t*f);
1470 if(p>m && fabs(y)<1.e-14) break; }
1471 if(k>0){ x= -x; s+=(t=1./(tp*2.));
1472 for(p=1,--k; k>0 ;++p,--k) s+=(t*=x/(p*k)); }
1474 else{ f=1./(t*v*2.); t*=a0/sin(2.*a0*v); s=f-t;
1475 for(p=1,x*=x,tp=v;;++p){
1476 t*=x/(p*(v+=1.)); f*= -x/(p*(tp-=1.));
1477 s+=(y=f-t); if(p>m && fabs(y)<1.e-14) break; }
1480 else{ double tq,h,w,z,r;
1481 t=12./pow(x,.333); k=(int)(t*t); y=2.*(x+k);
1482 m=(int)v; v-=m; tp=v*v-.25; v+=1.; tq=v*v-.25;
1483 for(s=h=1.,r=f=z=w=0.; k>0 ;--k,y-=2.){
1484 t=(y*h-(k+1)*z)/(k-1-tp/k); z=h; f+=(h=t);
1485 t=(y*s-(k+1)*w)/(k-1-tq/k); w=s; r+=(s=t); }
1486 t=sqrt(a0/x)*exp(-x); s*=t/r; h*=t/f; x/=2.; if(m==0) s=h;
1487 for(k=1; k<m ;++k){ t=v*s/x+h; h=s; s=t; v+=1.;}
1490 else{ s=t=sqrt(a0/x); x*=2.;
1491 for(p=1,y=.5; (tp=fabs(t))>1.e-14 ;++p,y+=1.){
1492 t*=(v+y)*(v-y)/(p*x); if(y>v && fabs(t)>=tp) break; s+=t; }
1498static double ccmath_jbes( double v, double x)
1499{ double y,s,t,tp; int p,m;
1500 y=x-8.5; if(y>0.) y*=y; tp=v*v/4.+13.69;
1501 if(y<tp){ x/=2.; m=(int)x;
1502 if(x>0.) s=t=exp(v*log(x)-ccmath_gaml(v+1.));
1503 else{ if(v>0.) return 0.; else if(v==0.) return 1.;}
1504 for(p=1,x*= -x;;++p){ t*=x/(p*(v+=1.)); s+=t;
1505 if(p>m && fabs(t)<1.e-13) break;
1508 else{ double u,a0=1.57079632679490;
1509 s=t=1./sqrt(x*a0); x*=2.; u=0.;
1510 for(p=1,y=.5; (tp=fabs(t))>1.e-14 ;++p,y+=1.){
1511 t*=(v+y)*(v-y)/(p*x); if(y>v && fabs(t)>=tp) break;
1512 if(!(p&1)){ t= -t; s+=t;} else u-=t;
1514 y=x/2.-(v+.5)*a0; s=cos(y)*s+sin(y)*u;
1519static double ccmath_nbes( double v, double x)
1520{ double y,s,t,tp,u,f,a0=3.14159265358979;
1522 y=x-8.5; if(y>0.) y*=y; tp=v*v/4.+13.69;
1523 if(y<tp){ if(x==0.) return HUGE_VAL;
1524 x/=2.; m=(int)x; u=t=exp(v*log(x)-ccmath_gaml(v+1.));
1525 if(modf(v,&y)==0.){ k=(int)y; u*=v;
1526 f=2.*log(x)-ccmath_psi(1)-ccmath_psi(k+1);
1527 t/=a0; x*= -x; s=f*t;
1528 for(p=1;;++p){ f-=1./p+1./(v+=1.);
1529 t*=x/(p*v); s+=(y=t*f); if(p>m && fabs(y)<1.e-13) break; }
1530 if(k>0){ x= -x; s-=(t=1./(u*a0));
1531 for(p=1,--k; k>0 ;++p,--k) s-=(t*=x/(p*k)); }
1533 else{ f=1./(t*v*a0); t/=tan(a0*v); s=t-f;
1534 for(p=1,x*=x,u=v;;++p){
1535 t*= -x/(p*(v+=1.)); f*=x/(p*(u-=1.));
1536 s+=(y=t-f); if(p>m && fabs(y)<1.e-13) break; }
1539 else{ x*=2.; s=t=2./sqrt(x*a0); u=0.;
1540 for(p=1,y=.5; (tp=fabs(t))>1.e-14 ;++p,y+=1.){
1541 t*=(v+y)*(v-y)/(p*x); if(y>v && fabs(t)>tp) break;
1542 if(!(p&1)){ t= -t; s+=t;} else u+=t;
1544 y=(x-(v+.5)*a0)/2.; s=sin(y)*s+cos(y)*u;
1555 double vv = converter-> asFloat (v).asFloat ();
1556 double xx = converter-> asFloat (x).asFloat ();
1557 return Value (ccmath_ibes (vv, xx));
1562 double vv = converter->asFloat (v). asFloat ();
1563 double xx = converter->asFloat (x). asFloat ();
1564 return Value (ccmath_jbes (vv, xx));
1569 double vv = converter->asFloat (v). asFloat ();
1570 double xx = converter->asFloat (x). asFloat ();
1571 return Value (ccmath_kbes (vv, xx));
1576 double vv = converter->asFloat (v).asFloat ();
1577 double xx = converter->asFloat (x).asFloat ();
1578 return Value (ccmath_nbes (vv, xx));
1585 return Value ( ::erf (converter->asFloat (x).asFloat()));
1590 return Value (::erfc (converter->asFloat (x). asFloat()));
1601 func ( this, res, range, param);
1605 int rows = range. rows ();
1607 for ( int r = 0; r < rows; r++)
1608 for ( int c = 0; c < cols; c++)
1614 func ( this, res, v, param);
1615 if (res. format() == Value::fmt_None)
1625 for ( unsigned int i = 0; i < range.count(); ++i)
1629void ValueCalc::twoArrayWalk ( const Value &a1, const Value &a2,
1630 Value &res, arrayWalkFunc func)
1635 func ( this, res, a1, a2);
1639 int rows = a1. rows ();
1641 int rows2 = a2. rows ();
1643 if ((rows != rows2) || (cols != cols2)) {
1647 for ( int r = 0; r < rows; r++)
1648 for ( int c = 0; c < cols; c++)
1653 twoArrayWalk (v1, v2, res, func);
1655 func ( this, res, v1, v2);
1656 if (res. format() == Value::fmt_None)
1662void ValueCalc::twoArrayWalk (TQValueVector<Value> &a1,
1663 TQValueVector<Value> &a2, Value &res, arrayWalkFunc func)
1666 if (a1.count() != a2.count()) {
1670 for ( unsigned int i = 0; i < a1.count(); ++i)
1671 twoArrayWalk (a1[i], a2[i], res, func);
1674arrayWalkFunc ValueCalc::awFunc ( const TQString &name)
1681void ValueCalc::registerAwFunc ( const TQString &name, arrayWalkFunc func)
1691 arrayWalk (range, res, awFunc (full ? "suma" : "sum"), 0);
1698 arrayWalk (range, res, awFunc (full ? "suma" : "sum"), 0);
1703Value ValueCalc::sumsq ( const Value &range, bool full)
1706 arrayWalk (range, res, awFunc (full ? "sumsqa" : "sumsq"), 0);
1711 const Value &checkRange, const Condition &cond)
1723 unsigned int rows = range. rows ();
1724 unsigned int cols = range. columns ();
1725 for ( unsigned int r = 0; r < rows; r++)
1726 for ( unsigned int c = 0; c < cols; c++)
1728 Value v = range. element (c, r);
1730 if ((c < checkRange. columns()) && (r < checkRange. rows()))
1731 newcheck = checkRange. element (c, r);
1734 res = add (res, sumIf (v, newcheck, cond));
1743int ValueCalc::count ( const Value &range, bool full)
1746 arrayWalk (range, res, awFunc (full ? "counta" : "count"), 0);
1747 return converter->asInteger (res).asInteger ();
1750int ValueCalc::count (TQValueVector<Value> range, bool full)
1753 arrayWalk (range, res, awFunc (full ? "counta" : "count"), 0);
1754 return converter->asInteger (res).asInteger ();
1757int ValueCalc::countIf ( const Value &range, const Condition &cond)
1762 return range. isEmpty() ? 0 : 1;
1769 int rows = range. rows ();
1770 for ( int r = 0; r < rows; r++)
1771 for ( int c = 0; c < cols; c++)
1773 Value v = range. element (c, r);
1776 res += countIf (v, cond);
1785Value ValueCalc::avg ( const Value &range, bool full)
1787 int cnt = count (range, full);
1789 return div ( sum (range, full), cnt);
1793Value ValueCalc::avg (TQValueVector<Value> range, bool full)
1795 int cnt = count (range, full);
1797 return div ( sum (range, full), cnt);
1801Value ValueCalc::max ( const Value &range, bool full)
1804 arrayWalk (range, res, awFunc (full ? "maxa" : "max"), 0);
1808Value ValueCalc::max (TQValueVector<Value> range, bool full)
1811 arrayWalk (range, res, awFunc (full ? "maxa" : "max"), 0);
1815Value ValueCalc::min ( const Value &range, bool full)
1818 arrayWalk (range, res, awFunc (full ? "mina" : "min"), 0);
1822Value ValueCalc::min (TQValueVector<Value> range, bool full)
1825 arrayWalk (range, res, awFunc (full ? "mina" : "min"), 0);
1835 if (count (range, full) == 0)
1839 arrayWalk (range, res, awFunc (full ? "proda" : "prod"), 0);
1843Value ValueCalc::product (TQValueVector<Value> range,
1844 Value init, bool full)
1849 if (count (range, full) == 0)
1853 arrayWalk (range, res, awFunc (full ? "proda" : "prod"), 0);
1857Value ValueCalc::stddev ( const Value &range, bool full)
1859 return stddev (range, avg (range, full), full);
1866 int cnt = count (range, full);
1867 arrayWalk (range, res, awFunc (full ? "devsqa" : "devsq"), avg);
1868 return sqrt (div (res, cnt-1));
1871Value ValueCalc::stddev (TQValueVector<Value> range, bool full)
1873 return stddev (range, avg (range, full), full);
1876Value ValueCalc::stddev (TQValueVector<Value> range,
1877 Value avg, bool full)
1880 int cnt = count (range, full);
1881 arrayWalk (range, res, awFunc (full ? "devsqa" : "devsq"), avg);
1882 return sqrt (div (res, cnt-1));
1885Value ValueCalc::stddevP ( const Value &range, bool full)
1887 return stddevP (range, avg (range, full), full);
1894 int cnt = count (range, full);
1895 arrayWalk (range, res, awFunc (full ? "devsqa" : "devsq"), avg);
1896 return sqrt (div (res, cnt));
1899Value ValueCalc::stddevP (TQValueVector<Value> range, bool full)
1901 return stddevP (range, avg (range, full), full);
1904Value ValueCalc::stddevP (TQValueVector<Value> range,
1905 Value avg, bool full)
1908 int cnt = count (range, full);
1909 arrayWalk (range, res, awFunc (full ? "devsqa" : "devsq"), avg);
1910 return sqrt (div (res, cnt));
1916 if ((a == Value::fmt_None) || (a == Value::fmt_Boolean))
1929 cond.comp = isEqual;
1930 cond.type = numeric;
1931 cond.value = converter->asFloat (val).asFloat();
1934 TQString text = converter->asString (val).asString();
1935 cond.comp = isEqual;
1936 text = text.stripWhiteSpace();
1938 if ( text.startsWith( "<=" ) )
1940 cond.comp = lessEqual;
1941 text = text.remove( 0, 2 );
1943 else if ( text.startsWith( ">=" ) )
1945 cond.comp = greaterEqual;
1946 text = text.remove( 0, 2 );
1948 else if ( text.startsWith( "!=" ) || text.startsWith( "<>" ) )
1950 cond.comp = notEqual;
1951 text = text.remove( 0, 2 );
1953 else if ( text.startsWith( "==" ) )
1955 cond.comp = isEqual;
1956 text = text.remove( 0, 2 );
1958 else if ( text.startsWith( "<" ) )
1961 text = text.remove( 0, 1 );
1963 else if ( text.startsWith( ">" ) )
1965 cond.comp = isGreater;
1966 text = text.remove( 0, 1 );
1968 else if ( text.startsWith( "=" ) )
1970 cond.comp = isEqual;
1971 text = text.remove( 0, 1 );
1974 text = text.stripWhiteSpace();
1977 double d = text.toDouble( &ok );
1980 cond.type = numeric;
1986 cond.stringValue = text;
1994 if (cond.type == numeric) {
1995 double d = converter->asFloat (val).asFloat();
1996 switch ( cond.comp )
2003 if (d < cond.value) return true;
2007 if (d > cond.value) return true;
2011 if (d <= cond.value) return true;
2015 if (d >= cond.value) return true;
2019 if (d != cond.value) return true;
2023 TQString d = converter->asString (val).asString();
2024 switch ( cond.comp )
2027 if (d == cond.stringValue) return true;
2031 if (d < cond.stringValue) return true;
2035 if (d > cond.stringValue) return true;
2039 if (d <= cond.stringValue) return true;
2043 if (d >= cond.stringValue) return true;
2047 if (d != cond.stringValue) return true;
The ValueCalc class is used to perform all sorts of calculations.
Value gcd(const Value &a, const Value &b) greatest common divisor
Value random(double range=1.0) random number from <0.0, range)
bool strLower(const Value &a, const Value &b, bool CS=true) string comparison
Value sum(const Value &range, bool full=true) basic range functions
bool gequal(const Value &a, const Value &b) numerical comparison - greater or equal
bool strGreater(const Value &a, const Value &b, bool CS=true) string comparison
bool naturalGequal(const Value &a, const Value &b, bool CS=true) string or numerical comparison - greater or equal
std::map< TQString, arrayWalkFunc > awFuncs registered array-walk functions
bool approxEqual(const Value &a, const Value &b) numerical comparison with a little epsilon tolerance
Value phi(Value x) some statistical stuff TODO: we may want to move these over to a separate class or something,...
Value factDouble(int which) double factorial (every other number multiplied)
bool matches(const Condition &cond, Value d) Returns true if value d matches the condition cond, built with getCond().
bool greater(const Value &a, const Value &b) numerical comparison
Value besseli(Value v, Value x) bessel functions - may also end up being separated from here
bool isZero(const Value &a) comparison and related
Value roundDown(const Value &a, const Value &digits) rounding
Value erf(Value x) error functions (see: man erf)
Value fact(const Value &which) some computational functions
Value sin(const Value &number) goniometric functions
Value combin(int n, int k) combinations
bool equal(const Value &a, const Value &b) numerical comparison
void arrayWalk(const Value &range, Value &res, arrayWalkFunc func, Value param) array/range walking
Value::Format format(Value::Format a, Value::Format b) return result formatting, based on these two values
Value log(const Value &number, const Value &base) logarithms and exponentials
void getCond(Condition &cond, Value val) This method parses the condition in string text to the condition cond.
bool naturalLower(const Value &a, const Value &b, bool CS=true) string or numerical comparison
Value fromBase(const Value &val, int base=16) base conversion base -> 10
bool strEqual(const Value &a, const Value &b, bool CS=true) string comparison
Value lcm(const Value &a, const Value &b) lowest common multiplicator
bool naturalEqual(const Value &a, const Value &b, bool CS=true) string or numerical comparison
bool lower(const Value &a, const Value &b) numerical comparison
bool naturalGreater(const Value &a, const Value &b, bool CS=true) string or numerical comparison
bool strGequal(const Value &a, const Value &b, bool CS=true) string comparison - greater or equal
Value add(const Value &a, const Value &b) basic arithmetic operations
Value sinh(const Value &number) hyperbolic functions
Value base(const Value &val, int base=16, int prec=0) base conversion 10 -> base
The ValueConverter class can convert Value between various different formats.
Value asNumeric(const Value &value) const convert to a numeric value - not necessarily float
Provides a wrapper for cell value.
unsigned columns() const If this value is an array, return the number of columns.
Value element(unsigned column, unsigned row) const Return an element in the array value.
static const Value & errorDIV0() Returns constant reference to DIV/0!
void setFormat(Format fmt) Sets format information for this value.
bool greater(const Value &v) const Returns true if this value is greater than v.
Format format() const Returns the format of the value (i.e.
unsigned rows() const If this value is an array, return the number of rows.
bool isBoolean() const Returns true if the type of this value is Boolean.
bool isEmpty() const Returns true if empty.
bool isNumber() const Returns true if the type of this value is either integer or floating-point.
bool equal(const Value &v) const Returns true if this value is equal to v.
bool isArray() const Returns true if the type of this value is array.
bool allowComparison(const Value &v) const Returns true if it is OK to compare this value with v.
double asFloat() const Returns the floating-point value of this value.
long asInteger() const Returns the integer value of this value.
TQString asString() const Returns the string value of this value.
static const Value & errorVALUE() Returns constant reference to VALUE!
bool isError() const Returns true if this value holds error information.
bool isString() const Returns true if the type of this value is string.
bool isInteger() const Returns true if the type of this value is integer.
static const Value & errorNA() Returns constant reference to N/A error.
|