60ed2eac2022b863af4ef05dbf34f22452687aa5
[pspp] / src / language / expressions / operations.def
1 // -*- c -*-
2 //
3 // PSPP - a program for statistical analysis.
4 // Copyright (C) 2005, 2006, 2009, 2010, 2011, 2012, 2015 Free Software Foundation, Inc.
5 // 
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 // 
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 // 
16 // You should have received a copy of the GNU General Public License
17 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 operator NEG (x) = -x;
20
21 operator ADD (a, b) = a + b;
22 operator SUB (a, b) = a - b;
23
24 absorb_miss operator MUL (a, b)
25 = (a == 0. || b == 0. ? 0.
26    : a == SYSMIS || b == SYSMIS ? SYSMIS
27    : a * b);
28
29 absorb_miss operator DIV (a, b)
30 = (a == 0. ? 0.
31    : a == SYSMIS || b == SYSMIS ? SYSMIS
32    : a / b);
33
34 absorb_miss operator POW (a, b)
35 = (a == SYSMIS ? (b == 0. ? 1. : a)
36    : b == SYSMIS ? (a == 0. ? 0. : SYSMIS)
37    : a == 0. && b <= 0. ? SYSMIS
38    : pow (a, b));
39
40 absorb_miss boolean operator AND (boolean a, boolean b)
41 = (a == 0. ? 0.
42    : b == 0. ? 0.
43    : b == SYSMIS ? SYSMIS
44    : a);
45
46 absorb_miss boolean operator OR (boolean a, boolean b)
47 = (a == 1. ? 1.
48    : b == 1. ? 1.
49    : b == SYSMIS ? SYSMIS
50    : a);
51
52 boolean operator NOT (boolean a)
53 = (a == 0. ? 1.
54    : a == 1. ? 0.
55    : SYSMIS);
56
57 // Numeric relational operators.
58 boolean operator EQ (a, b) = a == b;
59 boolean operator GE (a, b) = a >= b;
60 boolean operator GT (a, b) = a > b;
61 boolean operator LE (a, b) = a <= b;
62 boolean operator LT (a, b) = a < b;
63 boolean operator NE (a, b) = a != b;
64
65 // String relational operators.
66 boolean operator EQ_STRING (string a, string b) = compare_string_3way (&a, &b) == 0;
67 boolean operator GE_STRING (string a, string b) = compare_string_3way (&a, &b) >= 0;
68 boolean operator GT_STRING (string a, string b) = compare_string_3way (&a, &b) > 0;
69 boolean operator LE_STRING (string a, string b) = compare_string_3way (&a, &b) <= 0;
70 boolean operator LT_STRING (string a, string b) = compare_string_3way (&a, &b) < 0;
71 boolean operator NE_STRING (string a, string b) = compare_string_3way (&a, &b) != 0;
72
73 // Unary functions.
74 function ABS (x) = fabs (x);
75 extension function ACOS (x >= -1 && x <= 1) = acos (x);
76 function ASIN (x >= -1 && x <= 1) = asin (x);
77 function ATAN (x) = atan (x);
78 extension function ARCOS (x >= -1 && x <= 1) = acos (x);
79 function ARSIN (x >= -1 && x <= 1) = asin (x);
80 function ARTAN (x) = atan (x);
81 function COS (x) = cos (x);
82 function EXP (x) = check_errno (exp (x));
83 function LG10(x) = check_errno (log10 (x));
84 function LN (x) = check_errno (log (x));
85 function LNGAMMA (x >= 0) = gsl_sf_lngamma (x);
86 function MOD10 (x) = fmod (x, 10);
87 function RND (x) = round_nearest (x, 1, 0);
88 function RND (x, mult != 0) = round_nearest (x, mult, 0);
89 function RND (x, mult != 0, fuzzbits >= 0) = round_nearest (x, mult, fuzzbits);
90 function SIN (x) = sin (x);
91 function SQRT (x >= 0) = sqrt (x);
92 function TAN (x) = check_errno (tan (x));
93 function TRUNC (x) = x >= 0. ? floor (x) : -floor (-x);
94
95 absorb_miss function MOD (n, d)
96 {
97   if (d != SYSMIS)
98     return n != SYSMIS ? fmod (n, d) : SYSMIS;
99   else
100     return n != 0. ? SYSMIS : 0.;
101 }
102
103 // N-ary numeric functions.
104 absorb_miss boolean function ANY (x != SYSMIS, a[n])
105 {
106   int sysmis = 0;
107   size_t i;
108
109   for (i = 0; i < n; i++)
110     if (a[i] == x)
111       return 1.;
112     else if (a[i] == SYSMIS)
113       sysmis = 1;
114
115   return sysmis ? SYSMIS : 0.;
116 }
117
118 boolean function ANY (string x, string a[n])
119 {
120   size_t i;
121
122   for (i = 0; i < n; i++)
123     if (!compare_string_3way (&x, &a[i]))
124       return 1.;
125   return 0.;
126 }
127
128 function CFVAR.2 (a[n])
129 {
130   double mean, variance;
131
132   moments_of_doubles (a, n, NULL, &mean, &variance, NULL, NULL);
133
134   if (mean == SYSMIS || mean == 0 || variance == SYSMIS)
135     return SYSMIS;
136   else
137     return sqrt (variance) / mean;
138 }
139
140 function MAX.1 (a[n])
141 {
142   double max;
143   size_t i;
144
145   max = -DBL_MAX;
146   for (i = 0; i < n; i++)
147     if (a[i] != SYSMIS && a[i] > max)
148       max = a[i];
149   return max;
150 }
151
152 string function MAX (string a[n])
153 {
154   struct substring *max;
155   size_t i;
156
157   max = &a[0];
158   for (i = 1; i < n; i++)
159     if (compare_string_3way (&a[i], max) > 0)
160       max = &a[i];
161   return *max;
162 }
163
164 function MEAN.1 (a[n])
165 {
166   double mean;
167   moments_of_doubles (a, n, NULL, &mean, NULL, NULL, NULL);
168   return mean;
169 }
170
171 function MIN.1 (a[n])
172 {
173   double min;
174   size_t i;
175
176   min = DBL_MAX;
177   for (i = 0; i < n; i++)
178     if (a[i] != SYSMIS && a[i] < min)
179       min = a[i];
180   return min;
181 }
182
183 string function MIN (string a[n])
184 {
185   struct substring *min;
186   size_t i;
187
188   min = &a[0];
189   for (i = 1; i < n; i++)
190     if (compare_string_3way (&a[i], min) < 0)
191       min = &a[i];
192   return *min;
193 }
194
195 absorb_miss function NMISS (a[n])
196 {
197   size_t i;
198   size_t missing_cnt = 0;
199
200   for (i = 0; i < n; i++)
201     missing_cnt += a[i] == SYSMIS;
202   return missing_cnt;
203 }
204
205 absorb_miss function NVALID (a[n])
206 {
207   size_t i;
208   size_t valid_cnt = 0;
209
210   for (i = 0; i < n; i++)
211     valid_cnt += a[i] != SYSMIS;
212   return valid_cnt;
213 }
214
215 absorb_miss boolean function RANGE (x != SYSMIS, a[n*2])
216 {
217   size_t i;
218   int sysmis = 0;
219
220   for (i = 0; i < n; i++)
221     {
222       double w = a[2 * i];
223       double y = a[2 * i + 1];
224       if (w != SYSMIS && y != SYSMIS)
225         {
226           if (w <= x && x <= y)
227             return 1.0;
228         }
229       else
230         sysmis = 1;
231     }
232   return sysmis ? SYSMIS : 0.;
233 }
234
235 boolean function RANGE (string x, string a[n*2])
236 {
237   int i;
238
239   for (i = 0; i < n; i++)
240     {
241       struct substring *w = &a[2 * i];
242       struct substring *y = &a[2 * i + 1];
243       if (compare_string_3way (w, &x) <= 0 && compare_string_3way (&x, y) <= 0)
244         return 1.;
245     }
246   return 0.;
247 }
248
249 function SD.2 (a[n])
250 {
251   double variance;
252   moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
253   return sqrt (variance);
254 }
255
256 function SUM.1 (a[n])
257 {
258   double sum;
259   size_t i;
260
261   sum = 0.;
262   for (i = 0; i < n; i++)
263     if (a[i] != SYSMIS)
264       sum += a[i];
265   return sum;
266 }
267
268 function VARIANCE.2 (a[n])
269 {
270   double variance;
271   moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
272   return variance;
273 }
274
275 // Time construction & extraction functions.
276 function TIME.HMS (h, m, s)
277 {
278   if ((h > 0. || m > 0. || s > 0.) && (h < 0. || m < 0. || s < 0.))
279     {
280       msg (SW, _("TIME.HMS cannot mix positive and negative arguments."));
281       return SYSMIS;
282     }
283   else
284     return H_S * h + MIN_S * m + s;
285 }
286 function TIME.DAYS (days) = days * DAY_S;
287 function CTIME.DAYS (time) = time / DAY_S;
288 function CTIME.HOURS (time) = time / H_S;
289 function CTIME.MINUTES (time) = time / MIN_S;
290 function CTIME.SECONDS (time) = time;
291
292 // Date construction functions.
293 function DATE.DMY (d, m, y) = expr_ymd_to_date (y, m, d);
294 function DATE.MDY (m, d, y) = expr_ymd_to_date (y, m, d);
295 function DATE.MOYR (m, y) = expr_ymd_to_date (y, m, 1);
296 function DATE.QYR (q, y)
297 {
298   if (q < 1.0 || q > 4.0 || q != (int) q)
299     {
300       msg (SW, _("The first argument to DATE.QYR must be 1, 2, 3, or 4."));
301       return SYSMIS;
302     }
303    return expr_ymd_to_date (y, q * 3 - 2, 1);
304 }
305 function DATE.WKYR (w, y) = expr_wkyr_to_date (w, y);
306 function DATE.YRDAY (y, yday) = expr_yrday_to_date (y, yday);
307 function YRMODA (y, m, d) = expr_yrmoda (y, m, d);
308
309 // Date extraction functions.
310 function XDATE.TDAY (date) = floor (date / DAY_S);
311 function XDATE.HOUR (date) = fmod (floor (date / H_S), DAY_H);
312 function XDATE.MINUTE (date) = fmod (floor (date / H_MIN), H_MIN);
313 function XDATE.SECOND (date) = fmod (date, MIN_S);
314 function XDATE.DATE (date) = floor (date / DAY_S) * DAY_S;
315 function XDATE.TIME (date) = fmod (date, DAY_S);
316
317 function XDATE.JDAY (date >= DAY_S) = calendar_offset_to_yday (date / DAY_S);
318 function XDATE.MDAY (date >= DAY_S) = calendar_offset_to_mday (date / DAY_S);
319 function XDATE.MONTH (date >= DAY_S)
320      = calendar_offset_to_month (date / DAY_S);
321 function XDATE.QUARTER (date >= DAY_S)
322     = (calendar_offset_to_month (date / DAY_S) - 1) / 3 + 1;
323 function XDATE.WEEK (date >= DAY_S)
324     = (calendar_offset_to_yday (date / DAY_S) - 1) / 7 + 1;
325 function XDATE.WKDAY (date >= DAY_S) = calendar_offset_to_wday (date / DAY_S);
326 function XDATE.YEAR (date >= DAY_S) = calendar_offset_to_year (date / DAY_S);
327
328 // Date arithmetic functions.
329 no_abbrev function DATEDIFF (date2 >= DAY_S, date1 >= DAY_S, string unit)
330      = expr_date_difference (date1, date2, unit);
331 no_abbrev function DATESUM (date, quantity, string unit)
332      = expr_date_sum (date, quantity, unit, ss_cstr ("closest"));
333 no_abbrev function DATESUM (date, quantity, string unit, string method)
334      = expr_date_sum (date, quantity, unit, method);
335
336
337 // String functions.
338 string function CONCAT (string a[n])
339      expression e;
340 {
341   struct substring dst;
342   size_t i;
343
344   dst = alloc_string (e, MAX_STRING);
345   dst.length = 0;
346   for (i = 0; i < n; i++)
347     {
348       struct substring *src = &a[i];
349       size_t copy_len;
350
351       copy_len = src->length;
352       if (dst.length + copy_len > MAX_STRING)
353         copy_len = MAX_STRING - dst.length;
354       memcpy (&dst.string[dst.length], src->string, copy_len);
355       dst.length += copy_len;
356     }
357
358   return dst;
359 }
360
361 function INDEX (string haystack, string needle)
362 {
363   if (needle.length == 0)
364     return SYSMIS;
365   else
366     {
367       int limit = haystack.length - needle.length + 1;
368       int i;
369       for (i = 1; i <= limit; i++)
370         if (!memcmp (&haystack.string[i - 1], needle.string, needle.length))
371           return i;
372       return 0;
373     }
374 }
375
376 function INDEX (string haystack, string needles, needle_len_d)
377 {
378   if (needle_len_d <= INT_MIN || needle_len_d >= INT_MAX
379       || (int) needle_len_d != needle_len_d
380       || needles.length == 0)
381     return SYSMIS;
382   else
383     {
384       int needle_len = needle_len_d;
385       if (needle_len < 0 || needle_len > needles.length
386           || needles.length % needle_len != 0)
387         return SYSMIS;
388       else
389         {
390           int limit = haystack.length - needle_len + 1;
391           int i, j;
392           for (i = 1; i <= limit; i++)
393             for (j = 0; j < needles.length; j += needle_len)
394               if (!memcmp (&haystack.string[i - 1], &needles.string[j],
395                            needle_len))
396                 return i;
397           return 0;
398         }
399     }
400 }
401
402
403 function RINDEX (string haystack, string needle)
404 {
405   if (needle.length == 0)
406     return SYSMIS;
407   else
408     {
409       int limit = haystack.length - needle.length + 1;
410       int i;
411       for (i = limit; i >= 1; i--)
412         if (!memcmp (&haystack.string[i - 1], needle.string, needle.length))
413           return i;
414       return 0;
415     }
416 }
417
418 function RINDEX (string haystack, string needles, needle_len_d)
419 {
420   if (needle_len_d <= INT_MIN || needle_len_d >= INT_MAX
421       || (int) needle_len_d != needle_len_d
422       || needles.length == 0)
423     return SYSMIS;
424   else
425     {
426       int needle_len = needle_len_d;
427       if (needle_len < 0 || needle_len > needles.length
428           || needles.length % needle_len != 0)
429         return SYSMIS;
430       else
431         {
432           int limit = haystack.length - needle_len + 1;
433           int i, j;
434           for (i = limit; i >= 1; i--)
435             for (j = 0; j < needles.length; j += needle_len)
436               if (!memcmp (&haystack.string[i - 1],
437                            &needles.string[j], needle_len))
438                 return i;
439           return 0;
440         }
441     }
442 }
443
444 function LENGTH (string s)
445 {
446   return s.length;
447 }
448
449 string function LOWER (string s)
450 {
451   int i;
452
453   for (i = 0; i < s.length; i++)
454     s.string[i] = tolower ((unsigned char) s.string[i]);
455   return s;
456 }
457
458 function MBLEN.BYTE (string s, idx)
459 {
460   if (idx < 0 || idx >= s.length || (int) idx != idx)
461     return SYSMIS;
462   else
463     return 1;
464 }
465
466 string function UPCASE (string s)
467 {
468   int i;
469
470   for (i = 0; i < s.length; i++)
471     s.string[i] = toupper ((unsigned char) s.string[i]);
472   return s;
473 }
474
475 absorb_miss string function LPAD (string s, n)
476      expression e;
477 {
478   if (n < 0 || n > MAX_STRING || (int) n != n)
479     return empty_string;
480   else if (s.length >= n)
481     return s;
482   else
483     {
484       struct substring t = alloc_string (e, n);
485       memset (t.string, ' ', n - s.length);
486       memcpy (&t.string[(int) n - s.length], s.string, s.length);
487       return t;
488     }
489 }
490
491 absorb_miss string function LPAD (string s, n, string c)
492      expression e;
493 {
494   if (n < 0 || n > MAX_STRING || (int) n != n || c.length != 1)
495     return empty_string;
496   else if (s.length >= n)
497     return s;
498   else
499     {
500       struct substring t = alloc_string (e, n);
501       memset (t.string, c.string[0], n - s.length);
502       memcpy (&t.string[(int) n - s.length], s.string, s.length);
503       return t;
504     }
505 }
506
507 absorb_miss string function RPAD (string s, n)
508      expression e;
509 {
510   if (n < 0 || n > MAX_STRING || (int) n != n)
511     return empty_string;
512   else if (s.length >= n)
513     return s;
514   else
515     {
516       struct substring t = alloc_string (e, n);
517       memcpy (t.string, s.string, s.length);
518       memset (&t.string[s.length], ' ', n - s.length);
519       return t;
520     }
521 }
522
523 absorb_miss string function RPAD (string s, n, string c)
524      expression e;
525 {
526   if (n < 0 || n > MAX_STRING || (int) n != n || c.length != 1)
527     return empty_string;
528   else if (s.length >= n)
529     return s;
530   else
531     {
532       struct substring t = alloc_string (e, n);
533       memcpy (t.string, s.string, s.length);
534       memset (&t.string[s.length], c.string[0], n - s.length);
535       return t;
536     }
537 }
538
539 string function LTRIM (string s)
540 {
541   while (s.length > 0 && s.string[0] == ' ') 
542     {
543       s.length--;
544       s.string++;
545     }
546   return s;
547 }
548
549 string function LTRIM (string s, string c)
550 {
551   if (c.length == 1)
552     {
553       while (s.length > 0 && s.string[0] == c.string[0]) 
554         {
555           s.length--;
556           s.string++;
557         }
558       return s;
559     }
560   else
561     return empty_string;
562 }
563
564 string function RTRIM (string s)
565 {
566   while (s.length > 0 && s.string[s.length - 1] == ' ')
567     s.length--;
568   return s;
569 }
570
571 string function RTRIM (string s, string c)
572 {
573   if (c.length == 1)
574     {
575       while (s.length > 0 && s.string[s.length - 1] == c.string[0])
576         s.length--;
577       return s;
578     }
579   else
580     return empty_string;
581 }
582
583 function NUMBER (string s, ni_format f)
584 {
585   union value out;
586   char *error;
587
588   if (s.length > f->w)
589     s.length = f->w;
590   error = data_in (s, C_ENCODING, f->type, &out, 0, NULL);
591   if (error == NULL)
592     data_in_imply_decimals (s, C_ENCODING, f->type, f->d, &out);
593   else
594     {
595       msg (SE, "Cannot parse `%.*s' as format %s: %s",
596            (int) s.length, s.string, fmt_name (f->type), error);
597       free (error);
598     }
599   return out.f;
600 }
601
602 absorb_miss string function STRING (x, no_format f)
603      expression e;
604 {
605   union value v;
606   struct substring dst;
607   char *s;
608
609   v.f = x;
610
611   assert (!fmt_is_string (f->type));
612   s = data_out (&v, C_ENCODING, f);
613   dst = alloc_string (e, strlen (s));
614   strcpy (dst.string, s);
615   free (s);
616   return dst;
617 }
618
619 absorb_miss string function SUBSTR (string s, ofs)
620      expression e;
621 {
622   if (ofs >= 1 && ofs <= s.length && (int) ofs == ofs)
623     return copy_string (e, &s.string[(int) ofs - 1], s.length - ofs + 1);
624   else
625     return empty_string;
626 }
627
628 absorb_miss string function SUBSTR (string s, ofs, cnt)
629      expression e;
630 {
631   if (ofs >= 1 && ofs <= s.length && (int) ofs == ofs
632       && cnt >= 1 && cnt <= INT_MAX && (int) cnt == cnt)
633     {
634       int cnt_max = s.length - (int) ofs + 1;
635       return copy_string (e, &s.string[(int) ofs - 1],
636                           cnt <= cnt_max ? cnt : cnt_max);
637     }
638   else
639     return empty_string;
640 }
641
642 absorb_miss no_opt no_abbrev string function VALUELABEL (var v)
643      expression e;
644      case c;
645 {
646   const char *label = var_lookup_value_label (v, case_data (c, v));
647   if (label != NULL)
648     return copy_string (e, label, strlen (label));
649   else
650     return empty_string;
651 }
652
653 // Artificial.
654 operator SQUARE (x) = x * x;
655 boolean operator NUM_TO_BOOLEAN (x, string op_name)
656 {
657   if (x == 0. || x == 1. || x == SYSMIS)
658     return x;
659
660   if (!ss_is_empty (op_name))
661     msg (SE, _("An operand of the %.*s operator was found to have a value "
662                "other than 0 (false), 1 (true), or the system-missing "
663                "value.  The result was forced to 0."),
664          (int) op_name.length, op_name.string);
665   else
666     msg (SE, _("A logical expression was found to have a value other than 0 "
667                "(false), 1 (true), or the system-missing value.  The result "
668                "was forced to 0."));
669   return 0.;
670 }
671
672 operator BOOLEAN_TO_NUM (boolean x) = x;
673
674 // Beta distribution.
675 function PDF.BETA (x >= 0 && x <= 1, a > 0, b > 0)
676      = gsl_ran_beta_pdf (x, a, b);
677 function CDF.BETA (x >= 0 && x <= 1, a > 0, b > 0) = gsl_cdf_beta_P (x, a, b);
678 function IDF.BETA (P >= 0 && P <= 1, a > 0, b > 0)
679      = gsl_cdf_beta_Pinv (P, a, b);
680 no_opt function RV.BETA (a > 0, b > 0) = gsl_ran_beta (get_rng (), a, b);
681 function NCDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
682      = ncdf_beta (x, a, b, lambda);
683 function NPDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
684      = npdf_beta (x, a, b, lambda);
685
686 // Bivariate normal distribution.
687 function CDF.BVNOR (x0, x1, r >= -1 && r <= 1) = cdf_bvnor (x0, x1, r);
688 function PDF.BVNOR (x0, x1, r >= -1 && r <= 1)
689      = gsl_ran_bivariate_gaussian_pdf (x0, x1, 1, 1, r);
690
691 // Cauchy distribution.
692 function CDF.CAUCHY (x, a, b > 0) = gsl_cdf_cauchy_P ((x - a) / b, 1);
693 function IDF.CAUCHY (P > 0 && P < 1, a, b > 0)
694      = a + b * gsl_cdf_cauchy_Pinv (P, 1);
695 function PDF.CAUCHY (x, a, b > 0) = gsl_ran_cauchy_pdf ((x - a) / b, 1) / b;
696 no_opt function RV.CAUCHY (a, b > 0) = a + b * gsl_ran_cauchy (get_rng (), 1);
697
698 // Chi-square distribution.
699 function CDF.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_P (x, df);
700 function IDF.CHISQ (P >= 0 && P < 1, df > 0) = gsl_cdf_chisq_Pinv (P, df);
701 function PDF.CHISQ (x >= 0, df > 0) = gsl_ran_chisq_pdf (x, df);
702 no_opt function RV.CHISQ (df > 0) = gsl_ran_chisq (get_rng (), df);
703 function NCDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
704 function NPDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
705 function SIG.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_Q (x, df);
706
707 // Exponential distribution.
708 function CDF.EXP (x >= 0, a > 0) = gsl_cdf_exponential_P (x, 1. / a);
709 function IDF.EXP (P >= 0 && P < 1, a > 0)
710      = gsl_cdf_exponential_Pinv (P, 1. / a);
711 function PDF.EXP (x >= 0, a > 0) = gsl_ran_exponential_pdf (x, 1. / a);
712 no_opt function RV.EXP (a > 0) = gsl_ran_exponential (get_rng (), 1. / a);
713
714 // Exponential power distribution.
715 extension function PDF.XPOWER (x, a > 0, b >= 0)
716      = gsl_ran_exppow_pdf (x, a, b);
717 no_opt extension function RV.XPOWER (a > 0, b >= 0)
718      = gsl_ran_exppow (get_rng (), a, b);
719
720 // F distribution.
721 function CDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_P (x, df1, df2);
722 function IDF.F (P >= 0 && P < 1, df1 > 0, df2 > 0) = idf_fdist (P, df1, df2);
723 function PDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_ran_fdist_pdf (x, df1, df2);
724 no_opt function RV.F (df1 > 0, df2 > 0) = gsl_ran_fdist (get_rng (), df1, df2);
725 function NCDF.F (x >= 0, df1 > 0, df2 > 0, lambda >= 0) = unimplemented;
726 function NPDF.F (x >= 0, df1 > 0, df2 > 0, lmabda >= 0) = unimplemented;
727 function SIG.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_Q (x, df1, df2);
728
729 // Gamma distribution.
730 function CDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_cdf_gamma_P (x, a, 1. / b);
731 function IDF.GAMMA (P >= 0 && P <= 1, a > 0, b > 0)
732      = gsl_cdf_gamma_Pinv (P, a, 1. / b);
733 function PDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_ran_gamma_pdf (x, a, 1. / b);
734 no_opt function RV.GAMMA (a > 0, b > 0) 
735      = gsl_ran_gamma (get_rng (), a, 1. / b);
736
737 // Half-normal distribution.
738 function CDF.HALFNRM (x, a, b > 0) = unimplemented;
739 function IDF.HALFNRM (P > 0 && P < 1, a, b > 0) = unimplemented;
740 function PDF.HALFNRM (x, a, b > 0) = unimplemented;
741 no_opt function RV.HALFNRM (a, b > 0) = unimplemented;
742
743 // Inverse Gaussian distribution.
744 function CDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
745 function IDF.IGAUSS (P >= 0 && P < 1, a > 0, b > 0) = unimplemented;
746 function PDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
747 no_opt function RV.IGAUSS (a > 0, b > 0) = unimplemented;
748
749 // Landau distribution.
750 extension function PDF.LANDAU (x) = gsl_ran_landau_pdf (x);
751 no_opt extension function RV.LANDAU () = gsl_ran_landau (get_rng ());
752
753 // Laplace distribution.
754 function CDF.LAPLACE (x, a, b > 0) = gsl_cdf_laplace_P ((x - a) / b, 1);
755 function IDF.LAPLACE (P > 0 && P < 1, a, b > 0)
756      = a + b * gsl_cdf_laplace_Pinv (P, 1);
757 function PDF.LAPLACE (x, a, b > 0) = gsl_ran_laplace_pdf ((x - a) / b, 1) / b;
758 no_opt function RV.LAPLACE (a, b > 0) 
759      = a + b * gsl_ran_laplace (get_rng (), 1);
760
761 // Levy alpha-stable distribution.
762 no_opt extension function RV.LEVY (c, alpha > 0 && alpha <= 2) 
763      = gsl_ran_levy (get_rng (), c, alpha);
764
765 // Levy skew alpha-stable distribution.
766 no_opt extension function RV.LVSKEW (c, alpha > 0 && alpha <= 2,
767                                      beta >= -1 && beta <= 1) 
768      = gsl_ran_levy_skew (get_rng (), c, alpha, beta);
769
770 // Logistic distribution.
771 function CDF.LOGISTIC (x, a, b > 0) = gsl_cdf_logistic_P ((x - a) / b, 1);
772 function IDF.LOGISTIC (P > 0 && P < 1, a, b > 0)
773      = a + b * gsl_cdf_logistic_Pinv (P, 1);
774 function PDF.LOGISTIC (x, a, b > 0)
775      = gsl_ran_logistic_pdf ((x - a) / b, 1) / b;
776 no_opt function RV.LOGISTIC (a, b > 0) 
777      = a + b * gsl_ran_logistic (get_rng (), 1);
778
779 // Lognormal distribution.
780 function CDF.LNORMAL (x >= 0, m > 0, s > 0)
781      = gsl_cdf_lognormal_P (x, log (m), s);
782 function IDF.LNORMAL (P >= 0 && P < 1, m > 0, s > 0)
783      = gsl_cdf_lognormal_Pinv (P, log (m), s);
784 function PDF.LNORMAL (x >= 0, m > 0, s > 0)
785      = gsl_ran_lognormal_pdf (x, log (m), s);
786 no_opt function RV.LNORMAL (m > 0, s > 0) 
787      = gsl_ran_lognormal (get_rng (), log (m), s);
788
789 // Normal distribution.
790 function CDF.NORMAL (x, u, s > 0) = gsl_cdf_gaussian_P (x - u, s);
791 function IDF.NORMAL (P > 0 && P < 1, u, s > 0)
792      = u + gsl_cdf_gaussian_Pinv (P, s);
793 function PDF.NORMAL (x, u, s > 0) = gsl_ran_gaussian_pdf ((x - u) / s, 1) / s;
794 no_opt function RV.NORMAL (u, s > 0) = u + gsl_ran_gaussian (get_rng (), s);
795 function CDFNORM (x) = gsl_cdf_ugaussian_P (x);
796 function PROBIT (P > 0 && P < 1) = gsl_cdf_ugaussian_Pinv (P);
797 no_opt function NORMAL (s > 0) = gsl_ran_gaussian (get_rng (), s);
798
799 // Normal tail distribution.
800 function PDF.NTAIL (x, a > 0, sigma > 0)
801      = gsl_ran_gaussian_tail_pdf (x, a, sigma);
802 no_opt function RV.NTAIL (a > 0, sigma > 0) 
803      = gsl_ran_gaussian_tail (get_rng (), a, sigma);
804
805 // Pareto distribution.
806 function CDF.PARETO (x >= a, a > 0, b > 0) = gsl_cdf_pareto_P (x, b, a);
807 function IDF.PARETO (P >= 0 && P < 1, a > 0, b > 0)
808      = gsl_cdf_pareto_Pinv (P, b, a);
809 function PDF.PARETO (x >= a, a > 0, b > 0) = gsl_ran_pareto_pdf (x, b, a);
810 no_opt function RV.PARETO (a > 0, b > 0) = gsl_ran_pareto (get_rng (), b, a);
811
812 // Rayleigh distribution.
813 extension function CDF.RAYLEIGH (x, sigma > 0) = gsl_cdf_rayleigh_P (x, sigma);
814 extension function IDF.RAYLEIGH (P >= 0 && P <= 1, sigma > 0)
815      = gsl_cdf_rayleigh_Pinv (P, sigma);
816 extension function PDF.RAYLEIGH (x, sigma > 0)
817      = gsl_ran_rayleigh_pdf (x, sigma);
818 no_opt extension function RV.RAYLEIGH (sigma > 0) 
819      = gsl_ran_rayleigh (get_rng (), sigma);
820
821 // Rayleigh tail distribution.
822 extension function PDF.RTAIL (x, a, sigma)
823      = gsl_ran_rayleigh_tail_pdf (x, a, sigma);
824 no_opt extension function RV.RTAIL (a, sigma) 
825      = gsl_ran_rayleigh_tail (get_rng (), a, sigma);
826
827 // Studentized maximum modulus distribution.
828 function CDF.SMOD (x > 0, a >= 1, b >= 1) = unimplemented;
829 function IDF.SMOD (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
830
831 // Studentized range distribution.
832 function CDF.SRANGE (x > 0, a >= 1, b >= 1) = unimplemented;
833 function IDF.SRANGE (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
834
835 // Student t distribution.
836 function CDF.T (x, df > 0) = gsl_cdf_tdist_P (x, df);
837 function IDF.T (P > 0 && P < 1, df > 0) = gsl_cdf_tdist_Pinv (P, df);
838 function PDF.T (x, df > 0) = gsl_ran_tdist_pdf (x, df);
839 no_opt function RV.T (df > 0) = gsl_ran_tdist (get_rng (), df);
840 function NCDF.T (x, df > 0, nc) = unimplemented;
841 function NPDF.T (x, df > 0, nc) = unimplemented;
842
843 // Type-1 Gumbel distribution.
844 extension function CDF.T1G (x, a, b) = gsl_cdf_gumbel1_P (x, a, b);
845 extension function IDF.T1G (P >= 0 && P <= 1, a, b)
846      = gsl_cdf_gumbel1_P (P, a, b);
847 extension function PDF.T1G (x, a, b) = gsl_ran_gumbel1_pdf (x, a, b);
848 no_opt extension function RV.T1G (a, b) = gsl_ran_gumbel1 (get_rng (), a, b);
849
850 // Type-2 Gumbel distribution.
851 extension function CDF.T2G (x, a, b) = gsl_cdf_gumbel2_P (x, a, b);
852 extension function IDF.T2G (P >= 0 && P <= 1, a, b)
853      = gsl_cdf_gumbel2_P (P, a, b);
854 extension function PDF.T2G (x, a, b) = gsl_ran_gumbel2_pdf (x, a, b);
855 no_opt extension function RV.T2G (a, b) = gsl_ran_gumbel2 (get_rng (), a, b);
856
857 // Uniform distribution.
858 function CDF.UNIFORM (x <= b, a <= x, b) = gsl_cdf_flat_P (x, a, b);
859 function IDF.UNIFORM (P >= 0 && P <= 1, a <= b, b)
860      = gsl_cdf_flat_Pinv (P, a, b);
861 function PDF.UNIFORM (x <= b, a <= x, b) = gsl_ran_flat_pdf (x, a, b);
862 no_opt function RV.UNIFORM (a <= b, b) = gsl_ran_flat (get_rng (), a, b);
863 no_opt function UNIFORM (b >= 0) = gsl_ran_flat (get_rng (), 0, b);
864
865 // Weibull distribution.
866 function CDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_cdf_weibull_P (x, a, b);
867 function IDF.WEIBULL (P >= 0 && P < 1, a > 0, b > 0)
868      = gsl_cdf_weibull_Pinv (P, a, b);
869 function PDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_ran_weibull_pdf (x, a, b);
870 no_opt function RV.WEIBULL (a > 0, b > 0) = gsl_ran_weibull (get_rng (), a, b);
871
872 // Bernoulli distribution.
873 function CDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1) 
874      = k ? 1 : 1 - p;
875 function PDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1)
876      = gsl_ran_bernoulli_pdf (k, p);
877 no_opt function RV.BERNOULLI (p >= 0 && p <= 1) 
878      = gsl_ran_bernoulli (get_rng (), p);
879
880 // Binomial distribution.
881 function CDF.BINOM (k, n > 0 && n == floor (n), p >= 0 && p <= 1)
882      = gsl_cdf_binomial_P (k, p, n);
883 function PDF.BINOM (k >= 0 && k == floor (k) && k <= n,
884                     n > 0 && n == floor (n),
885                     p >= 0 && p <= 1)
886      = gsl_ran_binomial_pdf (k, p, n);
887 no_opt function RV.BINOM (p > 0 && p == floor (p), n >= 0 && n <= 1) 
888      = gsl_ran_binomial (get_rng (), p, n);
889
890 // Geometric distribution.
891 function CDF.GEOM (k >= 1 && k == floor (k), p >= 0 && p <= 1)
892      = gsl_cdf_geometric_P (k, p);
893 function PDF.GEOM (k >= 1 && k == floor (k),
894                    p >= 0 && p <= 1)
895      = gsl_ran_geometric_pdf (k, p);
896 no_opt function RV.GEOM (p >= 0 && p <= 1) = gsl_ran_geometric (get_rng (), p);
897
898 // Hypergeometric distribution.
899 function CDF.HYPER (k >= 0 && k == floor (k) && k <= c,
900                     a > 0 && a == floor (a),
901                     b > 0 && b == floor (b) && b <= a,
902                     c > 0 && c == floor (c) && c <= a)
903      = gsl_cdf_hypergeometric_P (k, c, a - c, b);
904 function PDF.HYPER (k >= 0 && k == floor (k) && k <= c,
905                     a > 0 && a == floor (a),
906                     b > 0 && b == floor (b) && b <= a,
907                     c > 0 && c == floor (c) && c <= a)
908      = gsl_ran_hypergeometric_pdf (k, c, a - c, b);
909 no_opt function RV.HYPER (a > 0 && a == floor (a),
910                           b > 0 && b == floor (b) && b <= a,
911                           c > 0 && c == floor (c) && c <= a)
912      = gsl_ran_hypergeometric (get_rng (), c, a - c, b);
913
914 // Logarithmic distribution.
915 extension function PDF.LOG (k >= 1, p > 0 && p <= 1)
916      = gsl_ran_logarithmic_pdf (k, p);
917 no_opt extension function RV.LOG (p > 0 && p <= 1) 
918      = gsl_ran_logarithmic (get_rng (), p);
919
920 // Negative binomial distribution.
921 function CDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
922      = gsl_cdf_negative_binomial_P (k, p, n);
923 function PDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
924      = gsl_ran_negative_binomial_pdf (k, p, n);
925 no_opt function RV.NEGBIN (n == floor (n), p > 0 && p <= 1) 
926      = gsl_ran_negative_binomial (get_rng (), p, n);
927
928 // Poisson distribution.
929 function CDF.POISSON (k >= 0 && k == floor (k), mu > 0)
930      = gsl_cdf_poisson_P (k, mu);
931 function PDF.POISSON (k >= 0 && k == floor (k), mu > 0)
932      = gsl_ran_poisson_pdf (k, mu);
933 no_opt function RV.POISSON (mu > 0) = gsl_ran_poisson (get_rng (), mu);
934
935 // Weirdness.
936 absorb_miss boolean function MISSING (x) = x == SYSMIS || !finite (x);
937 absorb_miss boolean function SYSMIS (x) = x == SYSMIS || !finite (x);
938 no_opt boolean function SYSMIS (num_var v)
939      case c;
940 {
941   return case_num (c, v) == SYSMIS;
942 }
943 no_opt boolean function VALUE (num_var v)
944      case c;
945 {
946   return case_num (c, v);
947 }
948
949 no_opt operator VEC_ELEM_NUM (idx)
950      vector v;
951      case c;
952 {
953   if (idx >= 1 && idx <= vector_get_var_cnt (v)) 
954     {
955       const struct variable *var = vector_get_var (v, (size_t) idx - 1);
956       double value = case_num (c, var);
957       return !var_is_num_missing (var, value, MV_USER) ? value : SYSMIS; 
958     }
959   else
960     {
961       if (idx == SYSMIS)
962         msg (SE, _("SYSMIS is not a valid index value for vector "
963                    "%s.  The result will be set to SYSMIS."),
964              vector_get_name (v));
965       else
966         msg (SE, _("%g is not a valid index value for vector %s.  "
967                    "The result will be set to SYSMIS."),
968              idx, vector_get_name (v));
969       return SYSMIS;
970     }
971 }
972
973 absorb_miss no_opt string operator VEC_ELEM_STR (idx)
974      expression e;
975      vector v;
976      case c;
977 {
978   if (idx >= 1 && idx <= vector_get_var_cnt (v))
979     {
980       struct variable *var = vector_get_var (v, (size_t) idx - 1);
981       return copy_string (e, CHAR_CAST_BUG (char *, case_str (c, var)),
982                           var_get_width (var));
983     }
984   else
985     {
986       if (idx == SYSMIS)
987         msg (SE, _("SYSMIS is not a valid index value for vector "
988                    "%s.  The result will be set to the empty string."),
989              vector_get_name (v));
990       else
991         msg (SE, _("%g is not a valid index value for vector %s.  "
992                    "The result will be set to the empty string."),
993              idx, vector_get_name (v));
994       return empty_string;
995     }
996 }
997
998 // Terminals.
999
1000 no_opt operator NUM_VAR ()
1001      case c;
1002      num_var v;
1003 {
1004   double d = case_num (c, v);
1005   return !var_is_num_missing (v, d, MV_USER) ? d : SYSMIS;
1006 }
1007
1008 no_opt string operator STR_VAR ()
1009      case c;
1010      expression e;
1011      str_var v;
1012 {
1013   struct substring s = alloc_string (e, var_get_width (v));
1014   memcpy (s.string, case_str (c, v), var_get_width (v));
1015   return s;
1016 }
1017
1018 no_opt perm_only function LAG (num_var v, pos_int n_before)
1019     dataset ds;
1020 {
1021   const struct ccase *c = lagged_case (ds, n_before);
1022   if (c != NULL)
1023     {
1024       double x = case_num (c, v);
1025       return !var_is_num_missing (v, x, MV_USER) ? x : SYSMIS;
1026     }
1027   else
1028     return SYSMIS;
1029 }
1030
1031 no_opt perm_only function LAG (num_var v)
1032     dataset ds;
1033 {
1034   const struct ccase *c = lagged_case (ds, 1);
1035   if (c != NULL)
1036     {
1037       double x = case_num (c, v);
1038       return !var_is_num_missing (v, x, MV_USER) ? x : SYSMIS;
1039     }
1040   else
1041     return SYSMIS;
1042 }
1043
1044 no_opt perm_only string function LAG (str_var v, pos_int n_before)
1045      expression e;
1046      dataset ds;
1047 {
1048   const struct ccase *c = lagged_case (ds, n_before);
1049   if (c != NULL)
1050     return copy_string (e, CHAR_CAST_BUG (char *, case_str (c, v)),
1051                         var_get_width (v));
1052   else
1053     return empty_string;
1054 }
1055
1056 no_opt perm_only string function LAG (str_var v)
1057      expression e;
1058      dataset ds;
1059 {
1060   const struct ccase *c = lagged_case (ds, 1);
1061   if (c != NULL)
1062     return copy_string (e, CHAR_CAST_BUG (char *, case_str (c, v)),
1063                         var_get_width (v));
1064   else
1065     return empty_string;
1066 }
1067
1068 no_opt operator NUM_SYS ()
1069      case c;
1070      num_var v;
1071 {
1072   return case_num (c, v) == SYSMIS;
1073 }
1074
1075 no_opt operator NUM_VAL ()
1076      case c;
1077      num_var v;
1078 {
1079   return case_num (c, v);
1080 }
1081
1082 no_opt operator CASENUM ()
1083      case_idx idx;
1084 {
1085   return idx;
1086 }