3 // PSPP - a program for statistical analysis.
4 // Copyright (C) 2005, 2006, 2009, 2010, 2011, 2012, 2015, 2016 Free Software Foundation, Inc.
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.
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.
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/>.
19 operator NEG (x) = -x;
21 operator ADD (a, b) = a + b;
22 operator SUB (a, b) = a - b;
24 absorb_miss operator MUL (a, b)
25 = (a == 0. || b == 0. ? 0.
26 : a == SYSMIS || b == SYSMIS ? SYSMIS
29 absorb_miss operator DIV (a, b)
31 : a == SYSMIS || b == SYSMIS ? SYSMIS
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
40 absorb_miss boolean operator AND (boolean a, boolean b)
43 : b == SYSMIS ? SYSMIS
46 absorb_miss boolean operator OR (boolean a, boolean b)
49 : b == SYSMIS ? SYSMIS
52 boolean operator NOT (boolean a)
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;
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;
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) = round_zero (x, 1, 0);
94 function TRUNC (x, mult != 0) = round_zero (x, mult, 0);
95 function TRUNC (x, mult != 0, fuzzbits >= 0) = round_zero (x, mult, fuzzbits);
97 absorb_miss function MOD (n, d)
100 return n != SYSMIS ? fmod (n, d) : SYSMIS;
102 return n != 0. ? SYSMIS : 0.;
105 // N-ary numeric functions.
106 absorb_miss boolean function ANY (x, a[n])
108 double retval = SYSMIS;
111 for (size_t i = 0; i < n; i++)
114 else if (a[i] != SYSMIS)
120 boolean function ANY (string x, string a[n])
124 for (i = 0; i < n; i++)
125 if (!compare_string_3way (&x, &a[i]))
130 function CFVAR.2 (a[n])
132 double mean, variance;
134 moments_of_doubles (a, n, NULL, &mean, &variance, NULL, NULL);
136 if (mean == SYSMIS || mean == 0 || variance == SYSMIS)
139 return sqrt (variance) / mean;
142 function MAX.1 (a[n])
148 for (i = 0; i < n; i++)
149 if (a[i] != SYSMIS && a[i] > max)
154 string function MAX (string a[n])
156 struct substring *max;
160 for (i = 1; i < n; i++)
161 if (compare_string_3way (&a[i], max) > 0)
166 function MEAN.1 (a[n])
169 moments_of_doubles (a, n, NULL, &mean, NULL, NULL, NULL);
173 function MEDIAN.1 (a[n])
175 return median (a, n);
178 function MIN.1 (a[n])
184 for (i = 0; i < n; i++)
185 if (a[i] != SYSMIS && a[i] < min)
190 string function MIN (string a[n])
192 struct substring *min;
196 for (i = 1; i < n; i++)
197 if (compare_string_3way (&a[i], min) < 0)
202 absorb_miss function NMISS (a[n])
204 size_t n_missings = 0;
205 for (size_t i = 0; i < n; i++)
206 n_missings += a[i] == SYSMIS;
210 absorb_miss function NVALID (a[n])
213 for (size_t i = 0; i < n; i++)
214 n_valids += a[i] != SYSMIS;
218 absorb_miss boolean function RANGE (x != SYSMIS, a[n*2])
222 for (size_t i = 0; i < n; i++)
225 double y = a[2 * i + 1];
226 if (w != SYSMIS && y != SYSMIS)
228 if (w <= x && x <= y)
236 return found ? true : valid ? false : SYSMIS;
239 boolean function RANGE (string x, string a[n*2])
242 for (size_t i = 0; i < n; i++)
244 struct substring *w = &a[2 * i];
245 struct substring *y = &a[2 * i + 1];
246 if (compare_string_3way (w, &x) <= 0 && compare_string_3way (&x, y) <= 0)
248 else if (compare_string_3way (w, y) > 0)
257 moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
258 return sqrt (variance);
261 function SUM.1 (a[n])
267 for (i = 0; i < n; i++)
273 function VARIANCE.2 (a[n])
276 moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
280 // Time construction & extraction functions.
281 function TIME.HMS (h, m, s)
285 if ((h > 0. || m > 0. || s > 0.) && (h < 0. || m < 0. || s < 0.))
287 msg_at (SW, expr_location (e, n),
288 _("TIME.HMS cannot accept a mix of positive and negative "
290 double args[] = { h, m, s };
291 for (size_t i = 0; i < 3; i++)
293 msg_at (SN, expr_location (e, n->args[i]),
294 _("This argument has positive value %g."), args[i]);
295 else if (args[i] < 0)
296 msg_at (SN, expr_location (e, n->args[i]),
297 _("This argument has negative value %g."), args[i]);
301 return H_S * h + MIN_S * m + s;
303 function TIME.DAYS (days) = days * DAY_S;
304 function CTIME.DAYS (time) = time / DAY_S;
305 function CTIME.HOURS (time) = time / H_S;
306 function CTIME.MINUTES (time) = time / MIN_S;
307 function CTIME.SECONDS (time) = time;
309 // Date construction functions.
310 function DATE.DMY (integer d, integer m, integer y)
313 = expr_ymd_to_date (y, m, d, e, n, 3, 2, 1);
315 function DATE.MDY (integer m, integer d, integer y)
318 = expr_ymd_to_date (y, m, d, e, n, 3, 1, 2);
320 function DATE.MOYR (integer m, integer y)
323 = expr_ymd_to_date (y, m, 1, e, n, 2, 1, 0);
325 function DATE.QYR (integer q, integer y)
331 msg_at (SW, expr_location (e, n->args[0]),
332 _("Argument 1 to DATE.QYR must be 1, 2, 3, or 4 (not %d)."), q);
335 return expr_ymd_to_date (y, q * 3 - 2, 1, e, n, 2, 0, 0);
338 function DATE.WKYR (integer w, integer y)
344 msg_at (SE, expr_location (e, n->args[0]),
345 _("The week argument to DATE.WKYR is outside the acceptable "
346 "range of 1 to 53. The result will be system-missing."));
351 double yr_1_1 = expr_ymd_to_ofs (y, 1, 1, e, n, 2, 0, 0);
352 if (yr_1_1 != SYSMIS)
353 return DAY_S * (yr_1_1 + WEEK_DAY * (w - 1));
359 function DATE.YRDAY (integer y, integer yd)
363 if (yd < 1 || yd > 366)
365 msg_at (SE, expr_location (e, n->args[1]),
366 _("The value %d as day argument to DATE.YRDAY is outside the "
367 "acceptable range of 1 to 366. "
368 "The result will be system-missing."), yd);
373 double yr_1_1 = expr_ymd_to_ofs (y, 1, 1, e, n, 1, 0, 0);
374 if (yr_1_1 != SYSMIS)
375 return DAY_S * (yr_1_1 + yd - 1.);
381 function YRMODA (integer y, integer m, integer d)
385 if (y >= 0 && y <= 99)
389 msg_at (SE, expr_location (e, n->args[0]),
390 _("The year argument to YRMODA is greater than 47516. "
391 "The result will be system-missing."));
395 return expr_ymd_to_ofs (y, m, d, e, n, 1, 2, 3);
398 // Date extraction functions.
399 function XDATE.TDAY (date) = floor (date / DAY_S);
400 function XDATE.HOUR (date) = fmod (floor (date / H_S), DAY_H);
401 function XDATE.MINUTE (date) = fmod (floor (date / H_MIN), H_MIN);
402 function XDATE.SECOND (date) = fmod (date, MIN_S);
403 function XDATE.DATE (date) = floor (date / DAY_S) * DAY_S;
404 function XDATE.TIME (date) = fmod (date, DAY_S);
406 function XDATE.JDAY (date >= DAY_S) = calendar_offset_to_yday (date / DAY_S);
407 function XDATE.MDAY (date >= DAY_S) = calendar_offset_to_mday (date / DAY_S);
408 function XDATE.MONTH (date >= DAY_S)
409 = calendar_offset_to_month (date / DAY_S);
410 function XDATE.QUARTER (date >= DAY_S)
411 = (calendar_offset_to_month (date / DAY_S) - 1) / 3 + 1;
412 function XDATE.WEEK (date >= DAY_S)
413 = (calendar_offset_to_yday (date / DAY_S) - 1) / 7 + 1;
414 function XDATE.WKDAY (date >= DAY_S) = calendar_offset_to_wday (date / DAY_S);
415 function XDATE.YEAR (date >= DAY_S) = calendar_offset_to_year (date / DAY_S);
417 // Date arithmetic functions.
418 no_abbrev function DATEDIFF (date2 >= DAY_S, date1 >= DAY_S, string unit)
421 = expr_date_difference (date1, date2, unit, e, n);
423 no_abbrev function DATESUM (date, quantity, string unit)
426 = expr_date_sum_closest (date, quantity, unit, e, n);
427 no_abbrev function DATESUM (date, quantity, string unit, string method)
430 = expr_date_sum (date, quantity, unit, method, e, n);
434 string function CONCAT (string a[n])
437 struct substring dst;
440 dst = alloc_string (e, MAX_STRING);
442 for (i = 0; i < n; i++)
444 struct substring *src = &a[i];
447 copy_len = src->length;
448 if (dst.length + copy_len > MAX_STRING)
449 copy_len = MAX_STRING - dst.length;
450 memcpy (&dst.string[dst.length], src->string, copy_len);
451 dst.length += copy_len;
457 function INDEX (string haystack, string needle)
459 if (haystack.length >= needle.length)
461 size_t limit = haystack.length - needle.length + 1;
462 for (size_t i = 1; i <= limit; i++)
463 if (!memcmp (&haystack.string[i - 1], needle.string, needle.length))
469 function INDEX (string haystack, string needles, integer needle_len)
473 if (needle_len <= 0 || needles.length % needle_len != 0)
475 msg_at (SE, expr_location (e, n),
476 _("INDEX needle length argument must evenly divide the "
477 "length of the needles argument."));
478 msg_at (SN, expr_location (e, n->args[1]),
479 _("The needles argument has length %zu."), needles.length);
480 msg_at (SN, expr_location (e, n->args[2]),
481 _("The needle length argument has value %d."), needle_len);
485 if (haystack.length >= needle_len)
487 size_t limit = haystack.length - needle_len + 1;
488 for (size_t i = 1; i <= limit; i++)
489 for (size_t j = 0; j < needles.length; j += needle_len)
490 if (!memcmp (&haystack.string[i - 1], &needles.string[j], needle_len))
497 function RINDEX (string haystack, string needle)
499 if (haystack.length >= needle.length)
501 size_t limit = haystack.length - needle.length + 1;
502 for (size_t i = limit; i >= 1; i--)
503 if (!memcmp (&haystack.string[i - 1], needle.string, needle.length))
510 function RINDEX (string haystack, string needles, integer needle_len)
514 if (needle_len <= 0 || needles.length % needle_len != 0)
516 msg_at (SE, expr_location (e, n),
517 _("RINDEX needle length argument must evenly divide the "
518 "length of the needles argument."));
519 msg_at (SN, expr_location (e, n->args[1]),
520 _("The needles argument has length %zu."), needles.length);
521 msg_at (SN, expr_location (e, n->args[2]),
522 _("The needle length argument has value %d."), needle_len);
526 if (haystack.length >= needle_len)
528 size_t limit = haystack.length - needle_len + 1;
529 for (size_t i = limit; i >= 1; i--)
530 for (size_t j = 0; j < needles.length; j += needle_len)
531 if (!memcmp (&haystack.string[i - 1], &needles.string[j], needle_len))
538 function LENGTH (string s)
543 string function LOWER (string s)
547 for (i = 0; i < s.length; i++)
548 s.string[i] = tolower ((unsigned char) s.string[i]);
552 function MBLEN.BYTE (string s, idx)
554 if (idx < 0 || idx >= s.length || (int) idx != idx)
560 string function UPCASE (string s)
564 for (i = 0; i < s.length; i++)
565 s.string[i] = toupper ((unsigned char) s.string[i]);
569 absorb_miss string function LPAD (string s, integer n)
573 if (n < 0 || n > MAX_STRING)
577 msg_at (SE, expr_location (e, node),
578 _("The length argument to LPAD must be between 0 and %d."),
580 msg_at (SN, expr_location (e, node->args[1]),
581 _("The length argument is %d."), n);
586 else if (s.length >= n)
590 struct substring t = alloc_string (e, n);
591 size_t pad = n - s.length;
592 memset (t.string, ' ', pad);
593 memcpy (&t.string[pad], s.string, s.length);
598 absorb_miss string function LPAD (string s, integer n, string c)
602 if (n < 0 || n > MAX_STRING)
606 msg_at (SE, expr_location (e, node),
607 _("The length argument to LPAD must be between 0 and %d."),
609 msg_at (SN, expr_location (e, node->args[1]),
610 _("The length argument is %d."), n);
615 else if (s.length >= n)
617 else if (c.length == 0)
619 msg_at (SE, expr_location (e, node),
620 _("The padding argument to LPAD must not be an empty string."));
625 size_t n_pad = (n - s.length) / c.length;
629 struct substring t = alloc_string (e, n);
631 for (size_t i = 0; i < n_pad; i++)
633 memcpy (t.string + t.length, c.string, c.length);
634 t.length += c.length;
636 memcpy (t.string + t.length, s.string, s.length);
637 t.length += s.length;
642 string function REPLACE (string haystack, string needle, string replacement)
644 = replace_string (e, haystack, needle, replacement, INT_MAX);
646 absorb_miss string function REPLACE (string haystack, string needle,
647 string replacement, integer n)
649 = replace_string (e, haystack, needle, replacement, n);
651 absorb_miss string function RPAD (string s, integer n)
655 if (n < 0 || n > MAX_STRING)
659 msg_at (SE, expr_location (e, node),
660 _("The length argument to RPAD must be between 0 and %d."),
662 msg_at (SN, expr_location (e, node->args[1]),
663 _("The length argument is %d."), n);
668 else if (s.length >= n)
672 struct substring t = alloc_string (e, n);
673 size_t pad = n - s.length;
674 memcpy (t.string, s.string, s.length);
675 memset (t.string + s.length, ' ', pad);
680 absorb_miss string function RPAD (string s, integer n, string c)
684 if (n < 0 || n > MAX_STRING)
688 msg_at (SE, expr_location (e, node),
689 _("The length argument to RPAD must be between 0 and %d."),
691 msg_at (SN, expr_location (e, node->args[1]),
692 _("The length argument is %d."), n);
697 else if (s.length >= n)
699 else if (c.length == 0)
701 msg_at (SE, expr_location (e, node),
702 _("The padding argument to RPAD must not be an empty string."));
707 size_t n_pad = (n - s.length) / c.length;
711 struct substring t = alloc_string (e, n);
712 memcpy (t.string, s.string, s.length);
714 for (size_t i = 0; i < n_pad; i++)
716 memcpy (t.string + t.length, c.string, c.length);
717 t.length += c.length;
723 string function LTRIM (string s)
725 while (s.length > 0 && s.string[0] == ' ')
733 string function LTRIM (string s, string c)
736 while (s.length >= c.length && !memcmp (s.string, c.string, c.length))
738 s.length -= c.length;
739 s.string += c.length;
744 string function RTRIM (string s)
746 while (s.length > 0 && s.string[s.length - 1] == ' ')
751 string function RTRIM (string s, string c)
754 while (s.length >= c.length
755 && !memcmp (&s.string[s.length - c.length], c.string, c.length))
756 s.length -= c.length;
760 function NUMBER (string s, ni_format f)
768 char *error = data_in (s, C_ENCODING, f.type, settings_get_fmt_settings (),
771 data_in_imply_decimals (s, C_ENCODING, f.type, f.d,
772 settings_get_fmt_settings (), &out);
775 msg_at (SE, expr_location (e, n->args[0]),
776 _("Cannot parse \"%.*s\" as format %s: %s"),
777 (int) s.length, s.string, fmt_name (f.type), error);
783 absorb_miss string function STRING (x, no_format f)
787 struct substring dst;
792 assert (!fmt_is_string (f.type));
793 s = data_out (&v, C_ENCODING, f, settings_get_fmt_settings ());
794 dst = alloc_string (e, strlen (s));
795 strcpy (dst.string, s);
800 absorb_miss string function STRUNC (string s, integer n)
803 return n == INT_MIN ? s : empty_string;
807 while (s.length > 0 && s.string[s.length - 1] == ' ')
812 absorb_miss string function SUBSTR (string s, integer ofs)
814 return (ofs >= 1 && ofs <= s.length
815 ? ss_substr (s, ofs - 1, SIZE_MAX)
819 absorb_miss string function SUBSTR (string s, integer ofs, integer len)
821 return (ofs >= 1 && len >= 1
822 ? ss_substr (s, ofs - 1, len)
826 absorb_miss no_opt no_abbrev string function VALUELABEL (var v)
830 const char *label = var_lookup_value_label (v, case_data (c, v));
832 return copy_string (e, label, strlen (label));
838 operator SQUARE (x) = x * x;
840 absorb_miss boolean operator OPERAND_TO_BOOLEAN (x, expr_node parent)
844 if (x == 0. || x == 1. || x == SYSMIS)
847 switch (parent->n_args)
850 msg_at (SE, expr_location (e, parent),
851 /* TRANSLATORS: There are exactly two operands. */
852 _("The operands of %s must have value 0 or 1."),
853 operations[parent->type].name);
857 msg_at (SE, expr_location (e, parent),
858 _("The operand of %s must have value 0 or 1."),
859 operations[parent->type].name);
866 msg_at (SN, expr_location (e, n),
867 _("This operand with unexpected value %g will be treated as 0."), x);
871 absorb_miss boolean operator EXPR_TO_BOOLEAN (x)
875 if (x == 0. || x == 1. || x == SYSMIS)
878 msg_at (SE, expr_location (e, n),
879 _("This expression, which must be 0 or 1, evaluated to %g. "
880 "It will be treated as 0."), x);
884 operator NUM_TO_INTEGER (x)
888 if (x == floor (x) && x > INT_MIN && x <= INT_MAX)
891 msg_at (SE, expr_location (e, n),
892 _("Treating unexpected non-integer value %g as missing."), x);
896 operator BOOLEAN_TO_NUM (boolean x) = x;
898 // Beta distribution.
899 function PDF.BETA (x >= 0 && x <= 1, a > 0, b > 0)
900 = gsl_ran_beta_pdf (x, a, b);
901 function CDF.BETA (x >= 0 && x <= 1, a > 0, b > 0) = gsl_cdf_beta_P (x, a, b);
902 function IDF.BETA (P >= 0 && P <= 1, a > 0, b > 0)
903 = gsl_cdf_beta_Pinv (P, a, b);
904 no_opt function RV.BETA (a > 0, b > 0) = gsl_ran_beta (get_rng (), a, b);
905 function NCDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
906 = ncdf_beta (x, a, b, lambda);
907 function NPDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
908 = npdf_beta (x, a, b, lambda);
910 // Bivariate normal distribution.
911 function CDF.BVNOR (x0, x1, r >= -1 && r <= 1) = cdf_bvnor (x0, x1, r);
912 function PDF.BVNOR (x0, x1, r >= -1 && r <= 1)
913 = gsl_ran_bivariate_gaussian_pdf (x0, x1, 1, 1, r);
915 // Cauchy distribution.
916 function CDF.CAUCHY (x, a, b > 0) = gsl_cdf_cauchy_P ((x - a) / b, 1);
917 function IDF.CAUCHY (P > 0 && P < 1, a, b > 0)
918 = a + b * gsl_cdf_cauchy_Pinv (P, 1);
919 function PDF.CAUCHY (x, a, b > 0) = gsl_ran_cauchy_pdf ((x - a) / b, 1) / b;
920 no_opt function RV.CAUCHY (a, b > 0) = a + b * gsl_ran_cauchy (get_rng (), 1);
922 // Chi-square distribution.
923 function CDF.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_P (x, df);
924 function IDF.CHISQ (P >= 0 && P < 1, df > 0) = gsl_cdf_chisq_Pinv (P, df);
925 function PDF.CHISQ (x >= 0, df > 0) = gsl_ran_chisq_pdf (x, df);
926 no_opt function RV.CHISQ (df > 0) = gsl_ran_chisq (get_rng (), df);
927 function NCDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
928 function NPDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
929 function SIG.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_Q (x, df);
931 // Exponential distribution.
932 function CDF.EXP (x >= 0, a > 0) = gsl_cdf_exponential_P (x, 1. / a);
933 function IDF.EXP (P >= 0 && P < 1, a > 0)
934 = gsl_cdf_exponential_Pinv (P, 1. / a);
935 function PDF.EXP (x >= 0, a > 0) = gsl_ran_exponential_pdf (x, 1. / a);
936 no_opt function RV.EXP (a > 0) = gsl_ran_exponential (get_rng (), 1. / a);
938 // Exponential power distribution.
939 extension function PDF.XPOWER (x, a > 0, b >= 0)
940 = gsl_ran_exppow_pdf (x, a, b);
941 no_opt extension function RV.XPOWER (a > 0, b >= 0)
942 = gsl_ran_exppow (get_rng (), a, b);
945 function CDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_P (x, df1, df2);
946 function IDF.F (P >= 0 && P < 1, df1 > 0, df2 > 0) = idf_fdist (P, df1, df2);
947 function PDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_ran_fdist_pdf (x, df1, df2);
948 no_opt function RV.F (df1 > 0, df2 > 0) = gsl_ran_fdist (get_rng (), df1, df2);
949 function NCDF.F (x >= 0, df1 > 0, df2 > 0, lambda >= 0) = unimplemented;
950 function NPDF.F (x >= 0, df1 > 0, df2 > 0, lmabda >= 0) = unimplemented;
951 function SIG.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_Q (x, df1, df2);
953 // Gamma distribution.
954 function CDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_cdf_gamma_P (x, a, 1. / b);
955 function IDF.GAMMA (P >= 0 && P <= 1, a > 0, b > 0)
956 = gsl_cdf_gamma_Pinv (P, a, 1. / b);
957 function PDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_ran_gamma_pdf (x, a, 1. / b);
958 no_opt function RV.GAMMA (a > 0, b > 0)
959 = gsl_ran_gamma (get_rng (), a, 1. / b);
961 // Half-normal distribution.
962 function CDF.HALFNRM (x, a, b > 0) = unimplemented;
963 function IDF.HALFNRM (P > 0 && P < 1, a, b > 0) = unimplemented;
964 function PDF.HALFNRM (x, a, b > 0) = unimplemented;
965 no_opt function RV.HALFNRM (a, b > 0) = unimplemented;
967 // Inverse Gaussian distribution.
968 function CDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
969 function IDF.IGAUSS (P >= 0 && P < 1, a > 0, b > 0) = unimplemented;
970 function PDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
971 no_opt function RV.IGAUSS (a > 0, b > 0) = unimplemented;
973 // Landau distribution.
974 extension function PDF.LANDAU (x) = gsl_ran_landau_pdf (x);
975 no_opt extension function RV.LANDAU () = gsl_ran_landau (get_rng ());
977 // Laplace distribution.
978 function CDF.LAPLACE (x, a, b > 0) = gsl_cdf_laplace_P ((x - a) / b, 1);
979 function IDF.LAPLACE (P > 0 && P < 1, a, b > 0)
980 = a + b * gsl_cdf_laplace_Pinv (P, 1);
981 function PDF.LAPLACE (x, a, b > 0) = gsl_ran_laplace_pdf ((x - a) / b, 1) / b;
982 no_opt function RV.LAPLACE (a, b > 0)
983 = a + b * gsl_ran_laplace (get_rng (), 1);
985 // Levy alpha-stable distribution.
986 no_opt extension function RV.LEVY (c, alpha > 0 && alpha <= 2)
987 = gsl_ran_levy (get_rng (), c, alpha);
989 // Levy skew alpha-stable distribution.
990 no_opt extension function RV.LVSKEW (c, alpha > 0 && alpha <= 2,
991 beta >= -1 && beta <= 1)
992 = gsl_ran_levy_skew (get_rng (), c, alpha, beta);
994 // Logistic distribution.
995 function CDF.LOGISTIC (x, a, b > 0) = gsl_cdf_logistic_P ((x - a) / b, 1);
996 function IDF.LOGISTIC (P > 0 && P < 1, a, b > 0)
997 = a + b * gsl_cdf_logistic_Pinv (P, 1);
998 function PDF.LOGISTIC (x, a, b > 0)
999 = gsl_ran_logistic_pdf ((x - a) / b, 1) / b;
1000 no_opt function RV.LOGISTIC (a, b > 0)
1001 = a + b * gsl_ran_logistic (get_rng (), 1);
1003 // Lognormal distribution.
1004 function CDF.LNORMAL (x >= 0, m > 0, s > 0)
1005 = gsl_cdf_lognormal_P (x, log (m), s);
1006 function IDF.LNORMAL (P >= 0 && P < 1, m > 0, s > 0)
1007 = gsl_cdf_lognormal_Pinv (P, log (m), s);
1008 function PDF.LNORMAL (x >= 0, m > 0, s > 0)
1009 = gsl_ran_lognormal_pdf (x, log (m), s);
1010 no_opt function RV.LNORMAL (m > 0, s > 0)
1011 = gsl_ran_lognormal (get_rng (), log (m), s);
1013 // Normal distribution.
1014 function CDF.NORMAL (x, u, s > 0) = gsl_cdf_gaussian_P (x - u, s);
1015 function IDF.NORMAL (P > 0 && P < 1, u, s > 0)
1016 = u + gsl_cdf_gaussian_Pinv (P, s);
1017 function PDF.NORMAL (x, u, s > 0) = gsl_ran_gaussian_pdf ((x - u) / s, 1) / s;
1018 no_opt function RV.NORMAL (u, s > 0) = u + gsl_ran_gaussian (get_rng (), s);
1019 function CDFNORM (x) = gsl_cdf_ugaussian_P (x);
1020 function PROBIT (P > 0 && P < 1) = gsl_cdf_ugaussian_Pinv (P);
1021 no_opt function NORMAL (s > 0) = gsl_ran_gaussian (get_rng (), s);
1023 // Normal tail distribution.
1024 function PDF.NTAIL (x, a > 0, sigma > 0)
1025 = gsl_ran_gaussian_tail_pdf (x, a, sigma);
1026 no_opt function RV.NTAIL (a > 0, sigma > 0)
1027 = gsl_ran_gaussian_tail (get_rng (), a, sigma);
1029 // Pareto distribution.
1030 function CDF.PARETO (x >= a, a > 0, b > 0) = gsl_cdf_pareto_P (x, b, a);
1031 function IDF.PARETO (P >= 0 && P < 1, a > 0, b > 0)
1032 = gsl_cdf_pareto_Pinv (P, b, a);
1033 function PDF.PARETO (x >= a, a > 0, b > 0) = gsl_ran_pareto_pdf (x, b, a);
1034 no_opt function RV.PARETO (a > 0, b > 0) = gsl_ran_pareto (get_rng (), b, a);
1036 // Rayleigh distribution.
1037 extension function CDF.RAYLEIGH (x, sigma > 0) = gsl_cdf_rayleigh_P (x, sigma);
1038 extension function IDF.RAYLEIGH (P >= 0 && P <= 1, sigma > 0)
1039 = gsl_cdf_rayleigh_Pinv (P, sigma);
1040 extension function PDF.RAYLEIGH (x, sigma > 0)
1041 = gsl_ran_rayleigh_pdf (x, sigma);
1042 no_opt extension function RV.RAYLEIGH (sigma > 0)
1043 = gsl_ran_rayleigh (get_rng (), sigma);
1045 // Rayleigh tail distribution.
1046 extension function PDF.RTAIL (x, a, sigma)
1047 = gsl_ran_rayleigh_tail_pdf (x, a, sigma);
1048 no_opt extension function RV.RTAIL (a, sigma)
1049 = gsl_ran_rayleigh_tail (get_rng (), a, sigma);
1051 // Studentized maximum modulus distribution.
1052 function CDF.SMOD (x > 0, a >= 1, b >= 1) = unimplemented;
1053 function IDF.SMOD (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
1055 // Studentized range distribution.
1056 function CDF.SRANGE (x > 0, a >= 1, b >= 1) = unimplemented;
1057 function IDF.SRANGE (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
1059 // Student t distribution.
1060 function CDF.T (x, df > 0) = gsl_cdf_tdist_P (x, df);
1061 function IDF.T (P > 0 && P < 1, df > 0) = gsl_cdf_tdist_Pinv (P, df);
1062 function PDF.T (x, df > 0) = gsl_ran_tdist_pdf (x, df);
1063 no_opt function RV.T (df > 0) = gsl_ran_tdist (get_rng (), df);
1064 function NCDF.T (x, df > 0, nc) = unimplemented;
1065 function NPDF.T (x, df > 0, nc) = unimplemented;
1067 // Type-1 Gumbel distribution.
1068 extension function CDF.T1G (x, a, b) = gsl_cdf_gumbel1_P (x, a, b);
1069 extension function IDF.T1G (P >= 0 && P <= 1, a, b)
1070 = gsl_cdf_gumbel1_Pinv (P, a, b);
1071 extension function PDF.T1G (x, a, b) = gsl_ran_gumbel1_pdf (x, a, b);
1072 no_opt extension function RV.T1G (a, b) = gsl_ran_gumbel1 (get_rng (), a, b);
1074 // Type-2 Gumbel distribution.
1075 extension function CDF.T2G (x, a, b) = gsl_cdf_gumbel2_P (x, a, b);
1076 extension function IDF.T2G (P >= 0 && P <= 1, a, b)
1077 = gsl_cdf_gumbel2_Pinv (P, a, b);
1078 extension function PDF.T2G (x, a, b) = gsl_ran_gumbel2_pdf (x, a, b);
1079 no_opt extension function RV.T2G (a, b) = gsl_ran_gumbel2 (get_rng (), a, b);
1081 // Uniform distribution.
1082 function CDF.UNIFORM (x <= b, a <= x, b) = gsl_cdf_flat_P (x, a, b);
1083 function IDF.UNIFORM (P >= 0 && P <= 1, a <= b, b)
1084 = gsl_cdf_flat_Pinv (P, a, b);
1085 function PDF.UNIFORM (x <= b, a <= x, b) = gsl_ran_flat_pdf (x, a, b);
1086 no_opt function RV.UNIFORM (a <= b, b) = gsl_ran_flat (get_rng (), a, b);
1087 no_opt function UNIFORM (b >= 0) = gsl_ran_flat (get_rng (), 0, b);
1089 // Weibull distribution.
1090 function CDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_cdf_weibull_P (x, a, b);
1091 function IDF.WEIBULL (P >= 0 && P < 1, a > 0, b > 0)
1092 = gsl_cdf_weibull_Pinv (P, a, b);
1093 function PDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_ran_weibull_pdf (x, a, b);
1094 no_opt function RV.WEIBULL (a > 0, b > 0) = gsl_ran_weibull (get_rng (), a, b);
1096 // Bernoulli distribution.
1097 function CDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1)
1099 function PDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1)
1100 = gsl_ran_bernoulli_pdf (k, p);
1101 no_opt function RV.BERNOULLI (p >= 0 && p <= 1)
1102 = gsl_ran_bernoulli (get_rng (), p);
1104 // Binomial distribution.
1105 function CDF.BINOM (k, n > 0 && n == floor (n), p >= 0 && p <= 1)
1106 = gsl_cdf_binomial_P (k, p, n);
1107 function PDF.BINOM (k >= 0 && k == floor (k) && k <= n,
1108 n > 0 && n == floor (n),
1110 = gsl_ran_binomial_pdf (k, p, n);
1111 no_opt function RV.BINOM (p > 0 && p == floor (p), n >= 0 && n <= 1)
1112 = gsl_ran_binomial (get_rng (), p, n);
1114 // Geometric distribution.
1115 function CDF.GEOM (k >= 1 && k == floor (k), p >= 0 && p <= 1)
1116 = gsl_cdf_geometric_P (k, p);
1117 function PDF.GEOM (k >= 1 && k == floor (k),
1119 = gsl_ran_geometric_pdf (k, p);
1120 no_opt function RV.GEOM (p >= 0 && p <= 1) = gsl_ran_geometric (get_rng (), p);
1122 // Hypergeometric distribution.
1123 function CDF.HYPER (k >= 0 && k == floor (k) && k <= c,
1124 a > 0 && a == floor (a),
1125 b > 0 && b == floor (b) && b <= a,
1126 c > 0 && c == floor (c) && c <= a)
1127 = gsl_cdf_hypergeometric_P (k, c, a - c, b);
1128 function PDF.HYPER (k >= 0 && k == floor (k) && k <= c,
1129 a > 0 && a == floor (a),
1130 b > 0 && b == floor (b) && b <= a,
1131 c > 0 && c == floor (c) && c <= a)
1132 = gsl_ran_hypergeometric_pdf (k, c, a - c, b);
1133 no_opt function RV.HYPER (a > 0 && a == floor (a),
1134 b > 0 && b == floor (b) && b <= a,
1135 c > 0 && c == floor (c) && c <= a)
1136 = gsl_ran_hypergeometric (get_rng (), c, a - c, b);
1138 // Logarithmic distribution.
1139 extension function PDF.LOG (k >= 1, p > 0 && p <= 1)
1140 = gsl_ran_logarithmic_pdf (k, p);
1141 no_opt extension function RV.LOG (p > 0 && p <= 1)
1142 = gsl_ran_logarithmic (get_rng (), p);
1144 // Negative binomial distribution.
1145 function CDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
1146 = gsl_cdf_negative_binomial_P (k, p, n);
1147 function PDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
1148 = gsl_ran_negative_binomial_pdf (k, p, n);
1149 no_opt function RV.NEGBIN (n == floor (n), p > 0 && p <= 1)
1150 = gsl_ran_negative_binomial (get_rng (), p, n);
1152 // Poisson distribution.
1153 function CDF.POISSON (k >= 0 && k == floor (k), mu > 0)
1154 = gsl_cdf_poisson_P (k, mu);
1155 function PDF.POISSON (k >= 0 && k == floor (k), mu > 0)
1156 = gsl_ran_poisson_pdf (k, mu);
1157 no_opt function RV.POISSON (mu > 0) = gsl_ran_poisson (get_rng (), mu);
1160 absorb_miss boolean function MISSING (x) = x == SYSMIS || !isfinite (x);
1161 absorb_miss boolean function SYSMIS (x) = x == SYSMIS || !isfinite (x);
1162 no_opt boolean function SYSMIS (num_var v)
1165 return case_num (c, v) == SYSMIS;
1167 no_opt function VALUE (num_var v)
1170 return case_num (c, v);
1172 no_opt function VALUE (num_vec_elem v)
1177 // A numeric vector element used in a "normal" context, in which a user-missing
1178 // value becomes system-missing.
1179 absorb_miss no_opt operator VEC_ELEM_NUM (idx)
1185 const struct variable *var = expr_index_vector (e, n, v, idx);
1188 double d = case_num (c, var);
1189 if (var_is_num_missing (var, d) != MV_USER)
1195 // A numeric vector element used as the argument to the VALUE() function, in
1196 // which a user-missing value retains its value.
1198 // All numeric vector elements are initially parsed this way. In most contexts
1199 // they then get coerced into numbers.
1200 absorb_miss no_opt num_vec_elem operator VEC_ELEM_NUM_RAW (idx)
1206 const struct variable *var = expr_index_vector (e, n, v, idx);
1207 return var ? case_num (c, var) : SYSMIS;
1210 absorb_miss no_opt string operator VEC_ELEM_STR (idx)
1216 const struct variable *var = expr_index_vector (e, n, v, idx);
1218 ? copy_string (e, CHAR_CAST_BUG (char *, case_str (c, var)),
1219 var_get_width (var))
1225 no_opt operator NUM_VAR ()
1229 double d = case_num (c, v);
1230 return var_is_num_missing (v, d) ? SYSMIS : d;
1233 no_opt string operator STR_VAR ()
1238 struct substring s = alloc_string (e, var_get_width (v));
1239 memcpy (s.string, case_str (c, v), var_get_width (v));
1243 no_opt perm_only function LAG (num_var v, pos_int n_before)
1246 const struct ccase *c = lagged_case (ds, n_before);
1249 double x = case_num (c, v);
1250 return var_is_num_missing (v, x) ? SYSMIS : x;
1256 no_opt perm_only function LAG (num_var v)
1259 const struct ccase *c = lagged_case (ds, 1);
1262 double x = case_num (c, v);
1263 return var_is_num_missing (v, x) ? SYSMIS : x;
1269 no_opt perm_only string function LAG (str_var v, pos_int n_before)
1273 const struct ccase *c = lagged_case (ds, n_before);
1275 return copy_string (e, CHAR_CAST_BUG (char *, case_str (c, v)),
1278 return empty_string;
1281 no_opt perm_only string function LAG (str_var v)
1285 const struct ccase *c = lagged_case (ds, 1);
1287 return copy_string (e, CHAR_CAST_BUG (char *, case_str (c, v)),
1290 return empty_string;
1293 no_opt operator NUM_SYS ()
1297 return case_num (c, v) == SYSMIS;
1300 no_opt operator NUM_VAL ()
1304 return case_num (c, v);
1307 no_opt operator CASENUM ()