File: stats-module.inc

package info (click to toggle)
slang2 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,588 kB
  • ctags: 10,558
  • sloc: ansic: 95,506; sh: 3,277; makefile: 945; pascal: 143
file content (227 lines) | stat: -rw-r--r-- 4,127 bytes parent folder | download
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
/*-*-C-*-*/

#ifdef MEAN_FUNCTION
static int MEAN_FUNCTION (VOID_STAR xp, unsigned int inc, unsigned int num, VOID_STAR yp)
{
   GENERIC_TYPE *x, *xmax;
   double c, m, err;

   x = (GENERIC_TYPE *) xp;
   xmax = x + num;

   num = num/inc;
   if (num == 0)
     return 0;

   c = (double) *x;
   if (num == 1)
     {
	*(MEAN_RESULT_TYPE *)yp = (MEAN_RESULT_TYPE) c;
	return 0;
     }

   err = 0.0;
   m = c;

   while (x < xmax)
     {
	double v = *x;
	double dm = (v - c)/num;
	double m1 = m + dm;
	err += dm - (m1 - m);
	m = m1;
	x += inc;
     }
   *(MEAN_RESULT_TYPE *)yp = (MEAN_RESULT_TYPE) (m + err);
   return 0;
}
# undef MEAN_FUNCTION
# undef MEAN_RESULT_TYPE
#endif

#ifdef STDDEV_FUNCTION
static int
STDDEV_FUNCTION (VOID_STAR xp, unsigned int inc, unsigned int num, VOID_STAR s)
{
   unsigned int i, n;
   double mean_i, variance_i;
   GENERIC_TYPE *x = (GENERIC_TYPE *) xp;
   double err;

   err = mean_i = variance_i = 0.0;
   n = 1;
   for (i = 0; i < num; i += inc)
     {
	double diff, x_i;
	double dv, v1;

	x_i = x[i];
	diff = x_i - mean_i;
	mean_i += diff / n;
	dv = diff * (x_i - mean_i);
	v1 = variance_i + dv;
	err += dv - (v1 - variance_i);
	variance_i = v1;
	n++;
     }
   variance_i += err;
   n--;
   if (n > 1)
     variance_i = sqrt (variance_i / (n - 1));
   else
     variance_i = 0.0;

   *(STDDEV_RESULT_TYPE *)s = (STDDEV_RESULT_TYPE) variance_i;

   return 0;
}
#undef STDDEV_RESULT_TYPE
#undef STDDEV_FUNCTION
#endif

/*
 * The following code is public domain.
 * Algorithm by Torben Mogensen, implementation by N. Devillard.
 * This code in public domain.
 */
#ifdef NC_MEDIAN_FUNCTION
static int NC_MEDIAN_FUNCTION (VOID_STAR ap, unsigned int inc, unsigned int num, VOID_STAR y)
{
   unsigned int i, n, m;
   GENERIC_TYPE *a;
   GENERIC_TYPE min, max, guess, maxltguess, mingtguess;
   unsigned int less, greater, equal;

   n = num/inc;
   if (n == 0)
     {
	SLang_set_error (SL_INVALID_PARM);
	return -1;
     }
   a = (GENERIC_TYPE *)ap;
   m = (n + 1)/2;

   min = max = a[0];
   for (i = 0; i < num; i += inc)
     {
	GENERIC_TYPE ai = a[i];
        if (ai < min) min = ai;
        if (ai > max) max = ai;
    }

   while (1)
     {
	less = 0; greater = 0; equal = 0;
	guess = min/2 + max/2;
        maxltguess = min;
        mingtguess = max;

        for (i=0; i < num; i += inc)
	  {
	     GENERIC_TYPE ai = a[i];
	     if (ai<guess)
	       {
		  less++;
		  if (ai>maxltguess) maxltguess = ai;
		  continue;
	       }
	     if (ai > guess)
	       {
		  greater++;
		  if (a[i] < mingtguess) mingtguess = ai;
		  continue;
	       }
	     equal++;
	  }
        if ((less <= m) && (greater <= m))
	  break;

        if (less > greater)
	  max = maxltguess;
        else
	  min = mingtguess;
     }
   if (less >= m)
     guess = maxltguess;
   else if (less + equal < m)
     guess = mingtguess;

   *(GENERIC_TYPE *)y = guess;
   return 0;
}
#undef NC_MEDIAN_FUNCTION
#endif

#ifdef MEDIAN_FUNCTION
static int
MEDIAN_FUNCTION (VOID_STAR ap, unsigned int inc, unsigned int num, VOID_STAR y)
{
   GENERIC_TYPE *aa = (GENERIC_TYPE *) ap;
   GENERIC_TYPE *a;
   unsigned int i, j, k, l, m, n;

   n = num/inc;
   if (n < 3)
     {
	if (n == 0)
	  {
	     SLang_set_error (SL_INVALID_PARM);
	     return -1;
	  }
	if ((n == 1)
	    || (*aa < *(aa + inc)))
	  {
	     *(GENERIC_TYPE *)y = *aa;
	     return 0;
	  }
	*(GENERIC_TYPE *)y = *(aa + inc);
	return 0;
     }

   if (NULL == (a = (GENERIC_TYPE *) SLmalloc (sizeof (GENERIC_TYPE)*n)))
     return -1;

   for (i = 0; i < n; i++)
     {
	a[i] = *aa;
	aa += inc;
     }

   if (n & 1)
     k = n/2;
   else
     k = n/2 - 1;

   l = 0;
   m = n - 1;

   while (l < m)
     {
        GENERIC_TYPE x = a[k];
        i = l;
        j = m ;
        do
	  {
	     while (a[i] < x) i++;
	     while (x < a[j]) j--;
	     if (i <= j)
	       {
		  GENERIC_TYPE tmp = a[i];
		  a[i] = a[j];
		  a[j] = tmp;
		  i++;
		  j--;
	       }
	  }
	while (i<=j);

        if (j < k) l=i;
        if (k < i) m=j;
     }
   *(GENERIC_TYPE *) y = a[k];
   SLfree ((char *) a);
   return 0;
}
#undef MEDIAN_FUNCTION
#endif
#undef GENERIC_TYPE