00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00035 #include "aggregatefunctstddevsampexpression.h"
00036
00037 AggregateFunctStdDevSampExpression::AggregateFunctStdDevSampExpression(VarExpression * _var):
00038 AggregateFunctExpression(et_aggregatefunctstddevsamp, _var) {
00039 }
00040
00041 AggregateFunctStdDevSampExpression::AggregateFunctStdDevSampExpression(VarStepExpression * _varstep):
00042 AggregateFunctExpression(et_aggregatefunctstddevsamp, _varstep) {
00043 }
00044
00045 AggregateFunctStdDevSampExpression::~AggregateFunctStdDevSampExpression() {
00046 }
00047
00048 void AggregateFunctStdDevSampExpression::eval(OutputStream & eos,
00049 Environment * env,
00050 unsigned modus) {
00051
00052 switch (modus) {
00053
00054 case EVAL_QUERY:
00055 {
00056 bool empty_sequence = true;
00057 long double stddevsamp_val =
00058 calculateValue(eos, env, modus, empty_sequence);
00059 if (!empty_sequence) {
00060 const char *stddevsamp_str =
00061 MiscFunctions::getStringFromNumerical(stddevsamp_val);
00062 eos << stddevsamp_str;
00063 delete[]stddevsamp_str;
00064 } else {
00065 eos << OUTPUT_STDDEVSAMP_ON_EMPTY_SEQUENCE;
00066 }
00067 break;
00068 }
00069
00070 case EVAL_QUERY_SILENT:
00071 {
00072 bool empty_sequence = true;
00073 long double stddevsamp_val =
00074 calculateValue(eos, env, modus, empty_sequence);
00075 if (!empty_sequence) {
00076 cur_val.setNumVal(stddevsamp_val);
00077 } else {
00078 const char *stddevsamp_empty_val =
00079 new char[strlen(OUTPUT_STDDEVSAMP_ON_EMPTY_SEQUENCE) + 1];
00080 strcpy((char *) stddevsamp_empty_val,
00081 OUTPUT_STDDEVSAMP_ON_EMPTY_SEQUENCE);
00082 cur_val.setStrVal(stddevsamp_empty_val);
00083 }
00084 break;
00085 }
00086
00087 case EVAL_SIGNOFF:
00088 forexp->eval(eos, env, EVAL_SIGNOFF);
00089 break;
00090
00091 default:
00092 throw
00093 RuntimeException
00094 ("AggregateFunctStdDevSampExpression: Illegal Evaluation Mode",
00095 eid_runtime_illegalmode);
00096 break;
00097 }
00098 }
00099
00100 long double AggregateFunctStdDevSampExpression::
00101 calculateValue(OutputStream & eos, Environment * env, unsigned modus,
00102 bool & empty_sequence) {
00103
00104 var ? bit->init(env->getNodeBinding(var->getId())) :
00105 bit->init(env->getNodeBinding(varstep->getId()));
00106
00107 long double stddevsamp_ret = 0;
00108 long double result = 0;
00109 vector < long double >*pcds = new vector < long double >;
00110 const char *pcd = NULL;
00111
00112 while (BufferNode * cur =
00113 bit->getNext(READ_UP_TO_CLOSE_CONTEXT, LOCK_CONTEXT_ALWAYS_CLEAR)) {
00114
00115 empty_sequence = false;
00116
00117 pcd = cur->getPCDataRepresentation();
00118 long double tmp = MiscFunctions::getNumericFromString(pcd);
00119
00120 result = MiscFunctions::getSummationFrom(result, tmp);
00121 pcds->push_back(tmp);
00122
00123 free((char *) pcd);
00124
00125 forexp->evalSignOffForBinding(eos, env, cur);
00126 }
00127 bit->clear();
00128
00129 if (!empty_sequence) {
00130 long double avg_val =
00131 MiscFunctions::getDivisionFrom(result, pcds->size());
00132 result = 0;
00133 for (unsigned i = 0; i < pcds->size(); i++) {
00134 result +=
00135 MiscFunctions::getPowerFrom(MiscFunctions::
00136 getSubtractionFrom((*pcds)[i],
00137 avg_val), 2);
00138 }
00139 result =
00140 MiscFunctions::getRadicalFrom(MiscFunctions::
00141 getDivisionFrom(result,
00142 pcds->size() - 1), 2);
00143 stddevsamp_ret = MiscFunctions::getRoundFrom(result);
00144 }
00145 delete pcds;
00146
00147 return stddevsamp_ret;
00148 }