📄 vartab.c
字号:
/*--------------------------------------------------------------------*/void vt_copy (VARTAB *dst, const VARTAB *src){ /* --- copy a frequency table */ int i; /* loop variable */ VARDATA *dd; const VARDATA *sd; /* to traverse the table elements */ int *di; const int *si; /* to traverse the dest. vectors */ assert(dst && src /* check the function arguments */ && (dst->size >= src->cnt)); dst->cnt = src->cnt; /* copy the current size */ sd = src->data +dst->cnt +1; /* traverse and copy */ dd = dst->data +dst->cnt +1; /* the column statistics */ for (i = dst->cnt +1; --i >= 0; ) *--dd = *--sd; si = src->dsts +dst->cnt; /* traverse and copy the */ di = dst->dsts +dst->cnt; /* destination indicators */ for (i = dst->cnt +1; --i >= 0; ) *--di = *--si; dst->known = src->known; /* copy number of cases */ dst->frq = src->frq; /* with a known value, */ dst->sum = src->sum; /* the total number of cases, */ dst->ssv = src->ssv; /* and the statistics */ dst->mean = src->mean; dst->sse = src->sse;} /* vt_copy() *//*--------------------------------------------------------------------*/void vt_add (VARTAB *vtab, int x, double y, double frq){ /* --- add to a variance table */ VARDATA *p; /* table column referred to */ assert(vtab /* check the function arguments */ && (x >= -1) && (x < vtab->cnt)); p = vtab->data +x +1; /* get the column's variation data */ p->frq += frq; /* adapt the column's statistics */ p->sum += frq *= y; p->ssv += frq *= y;} /* vt_add() *//*--------------------------------------------------------------------*/void vt_calc (VARTAB *vtab){ /* --- calculate estimates */ int i; /* loop variable */ VARDATA *p; /* to traverse the variation data */ assert(vtab); /* check the function argument */ vtab->frq = vtab->sum = vtab->ssv = 0; for (p = vtab->data +(i = vtab->cnt +1); --i >= 0; ) { vtab->frq += (--p)->frq; /* traverse the table columns */ vtab->sum += p->sum; /* and sum the statistics */ vtab->ssv += p->ssv; /* of the columns, then */ } /* compute the aggregates */ vtab->known = vtab->frq - p->frq; vtab->mean = (vtab->frq > 0) ? vtab->sum /vtab->frq : 0; vtab->sse = vtab->ssv -vtab->mean *vtab->sum; for (p = vtab->data +(i = vtab->cnt +1); --i >= 0; ) { --p; /* traverse the table columns again */ p->mean = (p->frq > 0) ? p->sum /p->frq : vtab->mean; p->sse = p->ssv -p->mean *p->sum; } /* compute the column aggregates */} /* vt_calc() *//*--------------------------------------------------------------------*/void vt_move (VARTAB *vtab, int xsrc, int xdst, double y, double frq){ /* --- move value/frequency */ VARDATA *src, *dst; /* source and destination column */ assert(vtab /* check the function arguments */ && (xsrc >= 0) && (xsrc < vtab->cnt) && (xdst >= 0) && (xdst < vtab->cnt)); src = vtab->data +xsrc +1; /* check the indices and */ dst = vtab->data +xdst +1; /* get the column data */ src->frq -= frq; dst->frq += frq; /* move the value */ src->sum -= frq *= y; dst->sum += frq; /* and its frequency */ src->ssv -= frq *= y; dst->ssv += frq; src->mean = (src->frq > 0) ? src->sum /src->frq : vtab->mean; src->sse = src->ssv -src->mean *src->sum; dst->mean = (dst->frq > 0) ? dst->sum /dst->frq : vtab->mean; dst->sse = dst->ssv -dst->mean *dst->sum;} /* vt_move() */ /* recompute column aggregates *//*--------------------------------------------------------------------*/void vt_comb (VARTAB *vtab, int xsrc, int xdst){ /* --- combine two columns */ VARDATA *src, *dst; /* source and destination column */ assert(vtab /* check the function arguments */ && (xsrc >= 0) && (xsrc < vtab->cnt) && (xdst >= 0) && (xdst < vtab->cnt) && (vtab->dsts[xsrc] < 0)); vtab->dsts[xsrc] = xdst; /* note reference to destination */ src = vtab->data +xsrc +1; /* get the source and */ dst = vtab->data +xdst +1; /* the destination column */ dst->frq += src->frq; /* update the statistics */ dst->sum += src->sum; /* of the destination column */ dst->ssv += src->ssv; /* and recompute the aggregates */ dst->mean = (dst->frq > 0) ? dst->sum /dst->frq : vtab->mean; dst->sse = dst->ssv -dst->mean *dst->sum;} /* vt_comb() *//*--------------------------------------------------------------------*/void vt_uncomb (VARTAB *vtab, int xsrc){ /* --- uncombine a column */ int xdst; /* index of destination column */ VARDATA *src, *dst; /* source and destination column */ assert(vtab /* check the function arguments */ && (xsrc >= 0) && (xsrc < vtab->cnt)); xdst = vtab->dsts[xsrc]; /* get the first destination column */ if (xdst < 0) return; /* if column is not combined, abort */ vtab->dsts[xsrc] = -1; /* clear the destination reference */ src = vtab->data +xsrc +1; /* get the source */ do { /* value/frequency remove loop */ dst = vtab->data +xdst +1; /* get the destination column */ dst->frq -= src->frq; /* and update the statistics */ dst->sum -= src->sum; /* of the destination column, */ dst->ssv -= src->ssv; /* then recompute the aggregates */ dst->mean = (dst->frq > 0) ? dst->sum /dst->frq : vtab->mean; dst->sse = dst->ssv -dst->mean *dst->sum; xdst = vtab->dsts[xdst]; /* get the next destination column, */ } while (xdst >= 0); /* while there is another */} /* vt_uncomb() *//*--------------------------------------------------------------------*/int vt_dest (VARTAB *vtab, int x){ /* --- get destination of a column */ int t; /* temporary buffer */ assert(vtab /* check the function arguments */ && (x >= 0) && (x < vtab->cnt)); while ((t = vtab->dsts[x]) >= 0) x = t; return x; /* follow references and return id */} /* vt_dest() *//*--------------------------------------------------------------------*/void vt_alldst (VARTAB *vtab, int *dsts){ /* --- get all column destinations */ int i; /* loop variable */ int x, t; /* temporary buffers */ assert(vtab && dsts); /* check the function arguments */ for (i = 0; i < vtab->cnt; i++) { x = i; /* default: index of source column */ while ((t = vtab->dsts[x]) >= 0) x = t; *dsts++ = x; /* follow references and */ } /* set id of destination column */} /* vt_alldst() *//*--------------------------------------------------------------------*/#ifdef VT_EVALdouble vt_eval (VARTAB *vtab, int measure, double *params){ /* --- evaluate variance table */ int m = measure & ~VEF_WGTD; /* flagless measure */ assert(vtab); /* check the function arguments */ if ((m <= VEM_NONE) || (m >= VEM_UNKNOWN)) return 0; /* check the measure code */ return _evalfn[m](vtab, measure, params);} /* vt_eval() */ /* return the evaluation result *//*--------------------------------------------------------------------*/const char* vt_mname (int measure){ /* --- get name of evaluation measure */ static char buf[64]; /* buffer for measure name */ int m = measure & ~VEF_WGTD; /* index in names vector */ if ((m < VEM_NONE) || (m > VEM_UNKNOWN)) m = VEM_UNKNOWN; /* check measure code */ strcpy(buf, (measure & VEF_WGTD) ? "weighted " : ""); strcat(buf, mnames[m]); /* build measure name */ return buf; /* and return it */} /* vt_mname() */#endif/*--------------------------------------------------------------------*/#ifndef NDEBUGvoid vt_show (VARTAB *vtab){ /* --- show variance table */ int i; /* loop variable */ VARDATA *p; /* to traverse the variance data */ assert(vtab); /* check the function arguments */ p = vtab->data; /* traverse the table columns */ for (i = -1; i < vtab->cnt; p++, i++) { if (i >= 0) printf("%2d: ", i); else printf("uv: "); /* print column indicator */ printf("%5.1f %5.1f %7.1f ", p->frq, p->sum, p->ssv); printf("%5.1f %7.1f\n", p->mean, p->sse); } /* print column statistics */ printf("tt: %5.1f %5.1f %7.1f ", vtab->frq, vtab->sum, vtab->ssv); printf("%5.1f %7.1f\n", vtab->mean, vtab->sse);} /* vt_show() */ /* print total statistics */#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -