File: stat.c

package info (click to toggle)
deal 3.1.9-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,552 kB
  • sloc: ansic: 5,224; cpp: 4,186; tcl: 3,125; makefile: 200; sh: 10
file content (442 lines) | stat: -rw-r--r-- 10,676 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * Copyright (C) 1996-2001, Thomas Andrews
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "math.h"
#include "deal.h"
#include "stat.h"
#include "keywords.h"
#include "tcl.h"

double sdev_data(double weight,double sum, double squares) {
  return sqrt((squares-(sum*sum)/weight)/(weight-1));
}

double rms_data(double weight,double sum,double squares) {
  return sqrt((squares-(sum*sum)/weight)/weight);
}

double sdev(sd)
     SDev *sd;
{
  double xx=sd->sumsquared,xy=sd->sum;
  double weight=sd->weight;
  return sdev_data(weight,xy,xx);
}

double sdevAverage(sd)
     SDev *sd;
{
  return sd->sum/sd->weight;
}

void sdevAddData(sd,weight,data) 
     SDev *sd;
     double weight;
     double data;
{
  if (sd->count==0) { 
	sd->max=data; sd->min=data;
  } else {
        if ((sd->max) < data) { (sd->max)=data; }
        if ((sd->min) > data) { (sd->min)=data; }
  }
 
  sd->count++;
  sd->weight += weight;
  sd->sum += (weight*data); 
  sd->sumsquared += (weight*data*data);
}

void sdevFree(ClientData sdev) {
  Tcl_Free((char *)sdev);
}
	
void sdevReset(SDev *sdev) {
  sdev->sum=0.0;
  sdev->sumsquared=0.0;
  sdev->weight=0.0;
  sdev->count=0;
}

SDev *sdevNew() {
  SDev *sdev=(SDev *)Tcl_Alloc(sizeof(SDev));
  sdevReset(sdev);
  return sdev;
}

void corrAddData(corr,weight,x,y)
     Correlation *corr;
	 double weight;
     double x,y;
{
  corr->count++;
  corr->weight += weight;
  corr->sumx += (weight*x);
  corr->sumxx += (weight*x*x);
  corr->sumy += (weight*y);
  corr->sumyy += (weight*y*y);
  corr->sumxy += (weight*x*y);
}

static double deviationsq(double weight,
			  double sumu,
			  double sumv,
			  double sumuv)
{
  return (sumuv*weight-sumu*sumv);
}

double corrResult(Correlation *corr)
{
  double x,y,xy;
  if (corr->count==0) { return -2; }
  x=deviationsq(corr->weight,corr->sumx,corr->sumx,corr->sumxx);
  y=deviationsq(corr->weight,corr->sumy,corr->sumy,corr->sumyy);
  xy=deviationsq(corr->weight,corr->sumx,corr->sumy,corr->sumxy);
  return (xy/sqrt(x*y));
}

Correlation *correlationNew() {
  Correlation *corr=(Correlation *)Tcl_Alloc(sizeof(Correlation));
  corrReset(corr);
  return corr;
}

void corrReset(Correlation *corr) {
  corr->count=0;
  corr->weight=0.0;
  corr->sumx=0.0; 
  corr->sumxx=0.0;
  corr->sumy=0.0;
  corr->sumyy=0.0;
  corr->sumxy=0.0;
}

void correlationFree(ClientData corr) {
  Tcl_Free((char *)corr);
}

int count(sd)
SDev *sd;
{
	return sd->count;
}


int tcl_sdev_command ( TCLOBJ_PARAMS ) TCLOBJ_DECL
{
  SDev *sd=(SDev *)cd;
  static int
    addCommandID=-1,
	addwCommandID=-1,
    averageCommandID=-1,
    deviationCommandID=-1,
    countCommandID=-1,
	weightCommandID=-1,
    rmsCommandID=-1,
    minCommandID=-1,
    maxCommandID=-1,
    initialize=1;

  int cmd;

  if (initialize) {
      initialize=0;
      addCommandID=Keyword_addKey("add");
	  addwCommandID=Keyword_addKey("addw");
	  weightCommandID=Keyword_addKey("weight");
      averageCommandID=Keyword_addKey("average");
      deviationCommandID=Keyword_addKey("sdev");
      countCommandID=Keyword_addKey("count");
      rmsCommandID=Keyword_addKey("rms");
      minCommandID=Keyword_addKey("min");
      maxCommandID=Keyword_addKey("max");
  }

  cmd=Keyword_getIdFromObj(interp,objv[1]);

  if (cmd==addCommandID && objc>=3) {
    double data;
    int i;
    for (i=2; i<objc; i++) {
      if (TCL_OK!=Tcl_GetDoubleFromObj(interp,objv[i],&data)) {
	    return TCL_ERROR;
      }
      sdevAddData(sd,1.0,data);
    }
    return TCL_OK;
  }

  if (cmd==addwCommandID && objc>=4) {
	  double weight,data;
	  int i;

	  if (TCL_OK!=Tcl_GetDoubleFromObj(interp,objv[2],&weight)) {
		  return TCL_ERROR;
      }

      if (weight<0.0) { return TCL_ERROR; }

      for (i=3; i<objc; i++) {
        if (TCL_OK!=Tcl_GetDoubleFromObj(interp,objv[i],&data)) {
	      return TCL_ERROR;
		}
        sdevAddData(sd,weight,data);
	  }
	  return TCL_OK;
  }

  if (cmd==countCommandID && objc == 2) {
	Tcl_Obj *obj=Tcl_NewIntObj(count(sd));
	Tcl_SetObjResult(interp,obj);
	return TCL_OK;
  }

  if (cmd==maxCommandID && objc == 2) {
	  Tcl_Obj *obj=Tcl_NewDoubleObj(sd->max);
	  Tcl_SetObjResult(interp,obj);
	  return TCL_OK;
  }
  if (cmd==minCommandID && objc == 2) {
	  Tcl_Obj *obj=Tcl_NewDoubleObj(sd->min);
	  Tcl_SetObjResult(interp,obj);
	  return TCL_OK;
  }
  if (cmd==weightCommandID && objc == 2) {
	  Tcl_Obj *obj=Tcl_NewDoubleObj(sd->weight);
	  Tcl_SetObjResult(interp,obj);
	  return TCL_OK;
  }

  if (sd->count==0 || sd->weight==0.0) {
    Tcl_AddErrorInfo(interp,"Can not do statistical computation without data");
    return TCL_ERROR;
  }

  if (cmd==rmsCommandID && objc==2) {
    Tcl_Obj *obj=Tcl_NewDoubleObj(rms_data(sd->weight,sd->sum,sd->sumsquared));
    Tcl_SetObjResult(interp,obj);
    return TCL_OK;
  }

  if (cmd==averageCommandID && objc==2) {
    double result=sdevAverage(sd);
    Tcl_Obj *obj=Tcl_NewDoubleObj(result);
    Tcl_SetObjResult(interp,obj);
    return TCL_OK;
  }
  
  if (sd->count==1) {
    Tcl_AddErrorInfo(interp,"Cannot compute deviation on one point of data");
    return TCL_ERROR;
  }

  if (cmd==deviationCommandID && objc==2) {
    double result=sdev(sd);
    Tcl_Obj *obj=Tcl_NewDoubleObj(result);
    Tcl_SetObjResult(interp,obj);
    return TCL_OK;
  }

  return TCL_ERROR;
}

int tcl_correlate_command (TCLOBJ_PARAMS) TCLOBJ_DECL
{
  static int addCommandID=-1,
	  addwCommandID=-1,
    correlateCommandID=-1,
    averageCommandID=-1,
	weightCommandID=-1,
    sdevCommandID=-1,
    rmsCommandID=-1,
    xID=-1,
    yID=-1,
    countCommandID=-1,
    initialize=1;
  int command;
  Correlation *corr=(Correlation *)cd;

  if (initialize) {
    initialize=0;
    addCommandID=Keyword_addKey("add");
    addwCommandID=Keyword_addKey("addw");
    correlateCommandID=Keyword_addKey("correlate");
    averageCommandID=Keyword_addKey("average");
    sdevCommandID=Keyword_addKey("sdev");
    countCommandID=Keyword_addKey("count");
    weightCommandID=Keyword_addKey("weight");
    rmsCommandID=Keyword_addKey("rms");
    correlateCommandID=Keyword_addKey("correlate");
    xID=Keyword_addKey("x");
    yID=Keyword_addKey("y");
  }

  if (objc==1) { goto corrusage; }

  command=Keyword_getIdFromObj(interp,objv[1]);

  if (KEYWORD_INVALID_ID==command) {
    goto corrusage;
  }

  if (command==addCommandID && objc>=3) {
    double value1;
    double value2;
    int i;

    if (objc%2==1) {
      Tcl_AddErrorInfo(interp,"Data must be added in pairs - got an odd number of datapoints");
      return TCL_ERROR;
    }

    for (i=2; i<objc; i+=2) {
      int result=Tcl_GetDoubleFromObj(interp,objv[i],&value1);
      if (result!=TCL_OK) { goto corrusage; }
      result=Tcl_GetDoubleFromObj(interp,objv[i+1],&value2);
      if (result!=TCL_OK) { goto corrusage; }
      corrAddData(corr,1.0,value1,value2);
    }
    return TCL_OK;
  }

  if (command==addwCommandID && objc>=4) {
	double weight;
    double value1;
    double value2;
    int i;

	if (TCL_OK!=Tcl_GetDoubleFromObj(interp,objv[2],&weight)) {
	  return TCL_ERROR;
    }

    if (weight<0.0) { return TCL_ERROR; }


    if (objc%2==0) {
      Tcl_AddErrorInfo(interp,"Data must be added in pairs - got an odd number of datapoints");
      return TCL_ERROR;
    }

    for (i=3; i<objc; i+=2) {
      int result=Tcl_GetDoubleFromObj(interp,objv[i],&value1);
      if (result!=TCL_OK) { goto corrusage; }
      result=Tcl_GetDoubleFromObj(interp,objv[i+1],&value2);
      if (result!=TCL_OK) { goto corrusage; }
      corrAddData(corr,weight,value1,value2);
    }
    return TCL_OK;
  }


  if (command==countCommandID && objc==2) {
    Tcl_Obj *obj=Tcl_NewIntObj(corr->count);
    Tcl_SetObjResult(interp,obj);
    return TCL_OK;
  }

  if (command==weightCommandID && objc==2) {
    Tcl_Obj *obj=Tcl_NewDoubleObj(corr->weight);
    Tcl_SetObjResult(interp,obj);
    return TCL_OK;
  }

  if (corr->count==0) {
    Tcl_AddErrorInfo(interp,"Cannot compute values without data");
    return TCL_ERROR;
  }


  if (command==averageCommandID && objc==3) {
    double value;
    int var=Keyword_getIdFromObj(interp,objv[2]);
    if (var==xID) {
      value=corr->sumx/corr->count;
    } else if (var==yID) {
      value=corr->sumy/corr->count;
    } else {
      goto corrusage;
    }
    Tcl_SetObjResult(interp,Tcl_NewDoubleObj(value));
    return TCL_OK;
  }

  if (command==rmsCommandID && objc==3) {
    double value;
    int var=Keyword_getIdFromObj(interp,objv[2]);
    if (var==xID) {
      value=rms_data(corr->count,corr->sumx,corr->sumxx);
    } else if (var==yID) {
      value=rms_data(corr->count,corr->sumy,corr->sumyy);
    } else {
      goto corrusage;
    }
    Tcl_SetObjResult(interp,Tcl_NewDoubleObj(value));
    return TCL_OK;
  }

  if (corr->count<=1) {
    Tcl_AddErrorInfo(interp,"Cannot compute correlation or standard deviation\n");
    Tcl_AddErrorInfo(interp,"Without at least two points of data");
    return TCL_ERROR;
  }

  if (command==correlateCommandID && objc==2) {
    double value=corrResult(corr);
    Tcl_Obj *obj=Tcl_NewDoubleObj(value);
    Tcl_SetObjResult(interp,obj);
    return TCL_OK;
  }

  if (command==sdevCommandID && objc==3) {
    double value;
    int var=Keyword_getIdFromObj(interp,objv[2]);
    if (var==xID) {
      value=sdev_data(corr->count,corr->sumx,corr->sumxx);
    } else if (var==yID) {
      value=sdev_data(corr->count,corr->sumy,corr->sumyy);
    } else {
      goto corrusage;
    }
    Tcl_SetObjResult(interp,Tcl_NewDoubleObj(value));
    return TCL_OK;
  }

 corrusage:
  return TCL_ERROR;
}

int tcl_sdev_define ( TCL_PARAMS ) TCL_DECL
{
  CONST84 char *name=argv[1];
  SDev *sd;
  argc--; argv++;
  sd=sdevNew();
  Tcl_CreateObjCommand(interp,name,tcl_sdev_command,(ClientData)sd,sdevFree);
  return TCL_OK;
}

int tcl_correlation_define ( TCLOBJ_PARAMS ) TCLOBJ_DECL
{
  char *name=Tcl_GetString(objv[1]);
  Correlation *corr;
  corr=correlationNew();
  Tcl_CreateObjCommand(interp,name,tcl_correlate_command,(ClientData)corr,correlationFree);
  return TCL_OK;
}