expressions: Implement the REPLACE string function.
[pspp.git] / src / language / expressions / operations.def
blob203ccbb42a2b9825135d624c6700bc67b73e0cee
1 // -*- c -*-
2 //
3 // PSPP - a program for statistical analysis.
4 // Copyright (C) 2005, 2006, 2009, 2010, 2011, 2012, 2015, 2016 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/>.
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
27 : a * b);
29 absorb_miss operator DIV (a, b)
30 = (a == 0. ? 0.
31 : a == SYSMIS || b == SYSMIS ? SYSMIS
32 : a / b);
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));
40 absorb_miss boolean operator AND (boolean a, boolean b)
41 = (a == 0. ? 0.
42 : b == 0. ? 0.
43 : b == SYSMIS ? SYSMIS
44 : a);
46 absorb_miss boolean operator OR (boolean a, boolean b)
47 = (a == 1. ? 1.
48 : b == 1. ? 1.
49 : b == SYSMIS ? SYSMIS
50 : a);
52 boolean operator NOT (boolean a)
53 = (a == 0. ? 1.
54 : a == 1. ? 0.
55 : SYSMIS);
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;
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);
95 absorb_miss function MOD (n, d)
97 if (d != SYSMIS)
98 return n != SYSMIS ? fmod (n, d) : SYSMIS;
99 else
100 return n != 0. ? SYSMIS : 0.;
103 // N-ary numeric functions.
104 absorb_miss boolean function ANY (x != SYSMIS, a[n])
106 int sysmis = 0;
107 size_t i;
109 for (i = 0; i < n; i++)
110 if (a[i] == x)
111 return 1.;
112 else if (a[i] == SYSMIS)
113 sysmis = 1;
115 return sysmis ? SYSMIS : 0.;
118 boolean function ANY (string x, string a[n])
120 size_t i;
122 for (i = 0; i < n; i++)
123 if (!compare_string_3way (&x, &a[i]))
124 return 1.;
125 return 0.;
128 function CFVAR.2 (a[n])
130 double mean, variance;
132 moments_of_doubles (a, n, NULL, &mean, &variance, NULL, NULL);
134 if (mean == SYSMIS || mean == 0 || variance == SYSMIS)
135 return SYSMIS;
136 else
137 return sqrt (variance) / mean;
140 function MAX.1 (a[n])
142 double max;
143 size_t i;
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;
152 string function MAX (string a[n])
154 struct substring *max;
155 size_t i;
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;
164 function MEAN.1 (a[n])
166 double mean;
167 moments_of_doubles (a, n, NULL, &mean, NULL, NULL, NULL);
168 return mean;
171 function MIN.1 (a[n])
173 double min;
174 size_t i;
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;
183 string function MIN (string a[n])
185 struct substring *min;
186 size_t i;
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;
195 absorb_miss function NMISS (a[n])
197 size_t i;
198 size_t missing_cnt = 0;
200 for (i = 0; i < n; i++)
201 missing_cnt += a[i] == SYSMIS;
202 return missing_cnt;
205 absorb_miss function NVALID (a[n])
207 size_t i;
208 size_t valid_cnt = 0;
210 for (i = 0; i < n; i++)
211 valid_cnt += a[i] != SYSMIS;
212 return valid_cnt;
215 absorb_miss boolean function RANGE (x != SYSMIS, a[n*2])
217 size_t i;
218 int sysmis = 0;
220 for (i = 0; i < n; i++)
222 double w = a[2 * i];
223 double y = a[2 * i + 1];
224 if (w != SYSMIS && y != SYSMIS)
226 if (w <= x && x <= y)
227 return 1.0;
229 else
230 sysmis = 1;
232 return sysmis ? SYSMIS : 0.;
235 boolean function RANGE (string x, string a[n*2])
237 int i;
239 for (i = 0; i < n; i++)
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.;
246 return 0.;
249 function SD.2 (a[n])
251 double variance;
252 moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
253 return sqrt (variance);
256 function SUM.1 (a[n])
258 double sum;
259 size_t i;
261 sum = 0.;
262 for (i = 0; i < n; i++)
263 if (a[i] != SYSMIS)
264 sum += a[i];
265 return sum;
268 function VARIANCE.2 (a[n])
270 double variance;
271 moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
272 return variance;
275 // Time construction & extraction functions.
276 function TIME.HMS (h, m, s)
278 if ((h > 0. || m > 0. || s > 0.) && (h < 0. || m < 0. || s < 0.))
280 msg (SW, _("TIME.HMS cannot mix positive and negative arguments."));
281 return SYSMIS;
283 else
284 return H_S * h + MIN_S * m + s;
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;
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)
298 if (q < 1.0 || q > 4.0 || q != (int) q)
300 msg (SW, _("The first argument to DATE.QYR must be 1, 2, 3, or 4."));
301 return SYSMIS;
303 return expr_ymd_to_date (y, q * 3 - 2, 1);
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);
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);
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);
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);
337 // String functions.
338 string function CONCAT (string a[n])
339 expression e;
341 struct substring dst;
342 size_t i;
344 dst = alloc_string (e, MAX_STRING);
345 dst.length = 0;
346 for (i = 0; i < n; i++)
348 struct substring *src = &a[i];
349 size_t copy_len;
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;
358 return dst;
361 function INDEX (string haystack, string needle)
363 if (needle.length == 0)
364 return SYSMIS;
365 else
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;
376 function INDEX (string haystack, string needles, needle_len_d)
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
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
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;
402 function RINDEX (string haystack, string needle)
404 if (needle.length == 0)
405 return SYSMIS;
406 else
408 int limit = haystack.length - needle.length + 1;
409 int i;
410 for (i = limit; i >= 1; i--)
411 if (!memcmp (&haystack.string[i - 1], needle.string, needle.length))
412 return i;
413 return 0;
417 function RINDEX (string haystack, string needles, needle_len_d)
419 if (needle_len_d <= 0 || needle_len_d >= INT_MAX
420 || (int) needle_len_d != needle_len_d
421 || needles.length == 0)
422 return SYSMIS;
423 else
425 int needle_len = needle_len_d;
426 if (needle_len < 0 || needle_len > needles.length
427 || needles.length % needle_len != 0)
428 return SYSMIS;
429 else
431 int limit = haystack.length - needle_len + 1;
432 int i, j;
433 for (i = limit; i >= 1; i--)
434 for (j = 0; j < needles.length; j += needle_len)
435 if (!memcmp (&haystack.string[i - 1],
436 &needles.string[j], needle_len))
437 return i;
438 return 0;
443 function LENGTH (string s)
445 return s.length;
448 string function LOWER (string s)
450 int i;
452 for (i = 0; i < s.length; i++)
453 s.string[i] = tolower ((unsigned char) s.string[i]);
454 return s;
457 function MBLEN.BYTE (string s, idx)
459 if (idx < 0 || idx >= s.length || (int) idx != idx)
460 return SYSMIS;
461 else
462 return 1;
465 string function UPCASE (string s)
467 int i;
469 for (i = 0; i < s.length; i++)
470 s.string[i] = toupper ((unsigned char) s.string[i]);
471 return s;
474 absorb_miss string function LPAD (string s, n)
475 expression e;
477 if (n < 0 || n > MAX_STRING || (int) n != n)
478 return empty_string;
479 else if (s.length >= n)
480 return s;
481 else
483 struct substring t = alloc_string (e, n);
484 memset (t.string, ' ', n - s.length);
485 memcpy (&t.string[(int) n - s.length], s.string, s.length);
486 return t;
490 absorb_miss string function LPAD (string s, n, string c)
491 expression e;
493 if (n < 0 || n > MAX_STRING || (int) n != n || c.length != 1)
494 return empty_string;
495 else if (s.length >= n)
496 return s;
497 else
499 struct substring t = alloc_string (e, n);
500 memset (t.string, c.string[0], n - s.length);
501 memcpy (&t.string[(int) n - s.length], s.string, s.length);
502 return t;
506 string function REPLACE (string haystack, string needle, string replacement)
507 expression e;
508 = replace_string (e, haystack, needle, replacement, DBL_MAX);
510 absorb_miss string function REPLACE (string haystack, string needle,
511 string replacement, n)
512 expression e;
513 = replace_string (e, haystack, needle, replacement, n);
515 absorb_miss string function RPAD (string s, n)
516 expression e;
518 if (n < 0 || n > MAX_STRING || (int) n != n)
519 return empty_string;
520 else if (s.length >= n)
521 return s;
522 else
524 struct substring t = alloc_string (e, n);
525 memcpy (t.string, s.string, s.length);
526 memset (&t.string[s.length], ' ', n - s.length);
527 return t;
531 absorb_miss string function RPAD (string s, n, string c)
532 expression e;
534 if (n < 0 || n > MAX_STRING || (int) n != n || c.length != 1)
535 return empty_string;
536 else if (s.length >= n)
537 return s;
538 else
540 struct substring t = alloc_string (e, n);
541 memcpy (t.string, s.string, s.length);
542 memset (&t.string[s.length], c.string[0], n - s.length);
543 return t;
547 string function LTRIM (string s)
549 while (s.length > 0 && s.string[0] == ' ')
551 s.length--;
552 s.string++;
554 return s;
557 string function LTRIM (string s, string c)
559 if (c.length == 1)
561 while (s.length > 0 && s.string[0] == c.string[0])
563 s.length--;
564 s.string++;
566 return s;
568 else
569 return empty_string;
572 string function RTRIM (string s)
574 while (s.length > 0 && s.string[s.length - 1] == ' ')
575 s.length--;
576 return s;
579 string function RTRIM (string s, string c)
581 if (c.length == 1)
583 while (s.length > 0 && s.string[s.length - 1] == c.string[0])
584 s.length--;
585 return s;
587 else
588 return empty_string;
591 function NUMBER (string s, ni_format f)
593 union value out;
594 char *error;
596 if (s.length > f->w)
597 s.length = f->w;
598 error = data_in (s, C_ENCODING, f->type, &out, 0, NULL);
599 if (error == NULL)
600 data_in_imply_decimals (s, C_ENCODING, f->type, f->d, &out);
601 else
603 msg (SE, "Cannot parse `%.*s' as format %s: %s",
604 (int) s.length, s.string, fmt_name (f->type), error);
605 free (error);
607 return out.f;
610 absorb_miss string function STRING (x, no_format f)
611 expression e;
613 union value v;
614 struct substring dst;
615 char *s;
617 v.f = x;
619 assert (!fmt_is_string (f->type));
620 s = data_out (&v, C_ENCODING, f);
621 dst = alloc_string (e, strlen (s));
622 strcpy (dst.string, s);
623 free (s);
624 return dst;
627 absorb_miss string function SUBSTR (string s, ofs)
628 expression e;
630 if (ofs >= 1 && ofs <= s.length && (int) ofs == ofs)
631 return copy_string (e, &s.string[(int) ofs - 1], s.length - ofs + 1);
632 else
633 return empty_string;
636 absorb_miss string function SUBSTR (string s, ofs, cnt)
637 expression e;
639 if (ofs >= 1 && ofs <= s.length && (int) ofs == ofs
640 && cnt >= 1 && cnt <= INT_MAX && (int) cnt == cnt)
642 int cnt_max = s.length - (int) ofs + 1;
643 return copy_string (e, &s.string[(int) ofs - 1],
644 cnt <= cnt_max ? cnt : cnt_max);
646 else
647 return empty_string;
650 absorb_miss no_opt no_abbrev string function VALUELABEL (var v)
651 expression e;
652 case c;
654 const char *label = var_lookup_value_label (v, case_data (c, v));
655 if (label != NULL)
656 return copy_string (e, label, strlen (label));
657 else
658 return empty_string;
661 // Artificial.
662 operator SQUARE (x) = x * x;
663 boolean operator NUM_TO_BOOLEAN (x, string op_name)
665 if (x == 0. || x == 1. || x == SYSMIS)
666 return x;
668 if (!ss_is_empty (op_name))
669 msg (SE, _("An operand of the %.*s operator was found to have a value "
670 "other than 0 (false), 1 (true), or the system-missing "
671 "value. The result was forced to 0."),
672 (int) op_name.length, op_name.string);
673 else
674 msg (SE, _("A logical expression was found to have a value other than 0 "
675 "(false), 1 (true), or the system-missing value. The result "
676 "was forced to 0."));
677 return 0.;
680 operator BOOLEAN_TO_NUM (boolean x) = x;
682 // Beta distribution.
683 function PDF.BETA (x >= 0 && x <= 1, a > 0, b > 0)
684 = gsl_ran_beta_pdf (x, a, b);
685 function CDF.BETA (x >= 0 && x <= 1, a > 0, b > 0) = gsl_cdf_beta_P (x, a, b);
686 function IDF.BETA (P >= 0 && P <= 1, a > 0, b > 0)
687 = gsl_cdf_beta_Pinv (P, a, b);
688 no_opt function RV.BETA (a > 0, b > 0) = gsl_ran_beta (get_rng (), a, b);
689 function NCDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
690 = ncdf_beta (x, a, b, lambda);
691 function NPDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
692 = npdf_beta (x, a, b, lambda);
694 // Bivariate normal distribution.
695 function CDF.BVNOR (x0, x1, r >= -1 && r <= 1) = cdf_bvnor (x0, x1, r);
696 function PDF.BVNOR (x0, x1, r >= -1 && r <= 1)
697 = gsl_ran_bivariate_gaussian_pdf (x0, x1, 1, 1, r);
699 // Cauchy distribution.
700 function CDF.CAUCHY (x, a, b > 0) = gsl_cdf_cauchy_P ((x - a) / b, 1);
701 function IDF.CAUCHY (P > 0 && P < 1, a, b > 0)
702 = a + b * gsl_cdf_cauchy_Pinv (P, 1);
703 function PDF.CAUCHY (x, a, b > 0) = gsl_ran_cauchy_pdf ((x - a) / b, 1) / b;
704 no_opt function RV.CAUCHY (a, b > 0) = a + b * gsl_ran_cauchy (get_rng (), 1);
706 // Chi-square distribution.
707 function CDF.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_P (x, df);
708 function IDF.CHISQ (P >= 0 && P < 1, df > 0) = gsl_cdf_chisq_Pinv (P, df);
709 function PDF.CHISQ (x >= 0, df > 0) = gsl_ran_chisq_pdf (x, df);
710 no_opt function RV.CHISQ (df > 0) = gsl_ran_chisq (get_rng (), df);
711 function NCDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
712 function NPDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
713 function SIG.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_Q (x, df);
715 // Exponential distribution.
716 function CDF.EXP (x >= 0, a > 0) = gsl_cdf_exponential_P (x, 1. / a);
717 function IDF.EXP (P >= 0 && P < 1, a > 0)
718 = gsl_cdf_exponential_Pinv (P, 1. / a);
719 function PDF.EXP (x >= 0, a > 0) = gsl_ran_exponential_pdf (x, 1. / a);
720 no_opt function RV.EXP (a > 0) = gsl_ran_exponential (get_rng (), 1. / a);
722 // Exponential power distribution.
723 extension function PDF.XPOWER (x, a > 0, b >= 0)
724 = gsl_ran_exppow_pdf (x, a, b);
725 no_opt extension function RV.XPOWER (a > 0, b >= 0)
726 = gsl_ran_exppow (get_rng (), a, b);
728 // F distribution.
729 function CDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_P (x, df1, df2);
730 function IDF.F (P >= 0 && P < 1, df1 > 0, df2 > 0) = idf_fdist (P, df1, df2);
731 function PDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_ran_fdist_pdf (x, df1, df2);
732 no_opt function RV.F (df1 > 0, df2 > 0) = gsl_ran_fdist (get_rng (), df1, df2);
733 function NCDF.F (x >= 0, df1 > 0, df2 > 0, lambda >= 0) = unimplemented;
734 function NPDF.F (x >= 0, df1 > 0, df2 > 0, lmabda >= 0) = unimplemented;
735 function SIG.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_Q (x, df1, df2);
737 // Gamma distribution.
738 function CDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_cdf_gamma_P (x, a, 1. / b);
739 function IDF.GAMMA (P >= 0 && P <= 1, a > 0, b > 0)
740 = gsl_cdf_gamma_Pinv (P, a, 1. / b);
741 function PDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_ran_gamma_pdf (x, a, 1. / b);
742 no_opt function RV.GAMMA (a > 0, b > 0)
743 = gsl_ran_gamma (get_rng (), a, 1. / b);
745 // Half-normal distribution.
746 function CDF.HALFNRM (x, a, b > 0) = unimplemented;
747 function IDF.HALFNRM (P > 0 && P < 1, a, b > 0) = unimplemented;
748 function PDF.HALFNRM (x, a, b > 0) = unimplemented;
749 no_opt function RV.HALFNRM (a, b > 0) = unimplemented;
751 // Inverse Gaussian distribution.
752 function CDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
753 function IDF.IGAUSS (P >= 0 && P < 1, a > 0, b > 0) = unimplemented;
754 function PDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
755 no_opt function RV.IGAUSS (a > 0, b > 0) = unimplemented;
757 // Landau distribution.
758 extension function PDF.LANDAU (x) = gsl_ran_landau_pdf (x);
759 no_opt extension function RV.LANDAU () = gsl_ran_landau (get_rng ());
761 // Laplace distribution.
762 function CDF.LAPLACE (x, a, b > 0) = gsl_cdf_laplace_P ((x - a) / b, 1);
763 function IDF.LAPLACE (P > 0 && P < 1, a, b > 0)
764 = a + b * gsl_cdf_laplace_Pinv (P, 1);
765 function PDF.LAPLACE (x, a, b > 0) = gsl_ran_laplace_pdf ((x - a) / b, 1) / b;
766 no_opt function RV.LAPLACE (a, b > 0)
767 = a + b * gsl_ran_laplace (get_rng (), 1);
769 // Levy alpha-stable distribution.
770 no_opt extension function RV.LEVY (c, alpha > 0 && alpha <= 2)
771 = gsl_ran_levy (get_rng (), c, alpha);
773 // Levy skew alpha-stable distribution.
774 no_opt extension function RV.LVSKEW (c, alpha > 0 && alpha <= 2,
775 beta >= -1 && beta <= 1)
776 = gsl_ran_levy_skew (get_rng (), c, alpha, beta);
778 // Logistic distribution.
779 function CDF.LOGISTIC (x, a, b > 0) = gsl_cdf_logistic_P ((x - a) / b, 1);
780 function IDF.LOGISTIC (P > 0 && P < 1, a, b > 0)
781 = a + b * gsl_cdf_logistic_Pinv (P, 1);
782 function PDF.LOGISTIC (x, a, b > 0)
783 = gsl_ran_logistic_pdf ((x - a) / b, 1) / b;
784 no_opt function RV.LOGISTIC (a, b > 0)
785 = a + b * gsl_ran_logistic (get_rng (), 1);
787 // Lognormal distribution.
788 function CDF.LNORMAL (x >= 0, m > 0, s > 0)
789 = gsl_cdf_lognormal_P (x, log (m), s);
790 function IDF.LNORMAL (P >= 0 && P < 1, m > 0, s > 0)
791 = gsl_cdf_lognormal_Pinv (P, log (m), s);
792 function PDF.LNORMAL (x >= 0, m > 0, s > 0)
793 = gsl_ran_lognormal_pdf (x, log (m), s);
794 no_opt function RV.LNORMAL (m > 0, s > 0)
795 = gsl_ran_lognormal (get_rng (), log (m), s);
797 // Normal distribution.
798 function CDF.NORMAL (x, u, s > 0) = gsl_cdf_gaussian_P (x - u, s);
799 function IDF.NORMAL (P > 0 && P < 1, u, s > 0)
800 = u + gsl_cdf_gaussian_Pinv (P, s);
801 function PDF.NORMAL (x, u, s > 0) = gsl_ran_gaussian_pdf ((x - u) / s, 1) / s;
802 no_opt function RV.NORMAL (u, s > 0) = u + gsl_ran_gaussian (get_rng (), s);
803 function CDFNORM (x) = gsl_cdf_ugaussian_P (x);
804 function PROBIT (P > 0 && P < 1) = gsl_cdf_ugaussian_Pinv (P);
805 no_opt function NORMAL (s > 0) = gsl_ran_gaussian (get_rng (), s);
807 // Normal tail distribution.
808 function PDF.NTAIL (x, a > 0, sigma > 0)
809 = gsl_ran_gaussian_tail_pdf (x, a, sigma);
810 no_opt function RV.NTAIL (a > 0, sigma > 0)
811 = gsl_ran_gaussian_tail (get_rng (), a, sigma);
813 // Pareto distribution.
814 function CDF.PARETO (x >= a, a > 0, b > 0) = gsl_cdf_pareto_P (x, b, a);
815 function IDF.PARETO (P >= 0 && P < 1, a > 0, b > 0)
816 = gsl_cdf_pareto_Pinv (P, b, a);
817 function PDF.PARETO (x >= a, a > 0, b > 0) = gsl_ran_pareto_pdf (x, b, a);
818 no_opt function RV.PARETO (a > 0, b > 0) = gsl_ran_pareto (get_rng (), b, a);
820 // Rayleigh distribution.
821 extension function CDF.RAYLEIGH (x, sigma > 0) = gsl_cdf_rayleigh_P (x, sigma);
822 extension function IDF.RAYLEIGH (P >= 0 && P <= 1, sigma > 0)
823 = gsl_cdf_rayleigh_Pinv (P, sigma);
824 extension function PDF.RAYLEIGH (x, sigma > 0)
825 = gsl_ran_rayleigh_pdf (x, sigma);
826 no_opt extension function RV.RAYLEIGH (sigma > 0)
827 = gsl_ran_rayleigh (get_rng (), sigma);
829 // Rayleigh tail distribution.
830 extension function PDF.RTAIL (x, a, sigma)
831 = gsl_ran_rayleigh_tail_pdf (x, a, sigma);
832 no_opt extension function RV.RTAIL (a, sigma)
833 = gsl_ran_rayleigh_tail (get_rng (), a, sigma);
835 // Studentized maximum modulus distribution.
836 function CDF.SMOD (x > 0, a >= 1, b >= 1) = unimplemented;
837 function IDF.SMOD (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
839 // Studentized range distribution.
840 function CDF.SRANGE (x > 0, a >= 1, b >= 1) = unimplemented;
841 function IDF.SRANGE (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
843 // Student t distribution.
844 function CDF.T (x, df > 0) = gsl_cdf_tdist_P (x, df);
845 function IDF.T (P > 0 && P < 1, df > 0) = gsl_cdf_tdist_Pinv (P, df);
846 function PDF.T (x, df > 0) = gsl_ran_tdist_pdf (x, df);
847 no_opt function RV.T (df > 0) = gsl_ran_tdist (get_rng (), df);
848 function NCDF.T (x, df > 0, nc) = unimplemented;
849 function NPDF.T (x, df > 0, nc) = unimplemented;
851 // Type-1 Gumbel distribution.
852 extension function CDF.T1G (x, a, b) = gsl_cdf_gumbel1_P (x, a, b);
853 extension function IDF.T1G (P >= 0 && P <= 1, a, b)
854 = gsl_cdf_gumbel1_P (P, a, b);
855 extension function PDF.T1G (x, a, b) = gsl_ran_gumbel1_pdf (x, a, b);
856 no_opt extension function RV.T1G (a, b) = gsl_ran_gumbel1 (get_rng (), a, b);
858 // Type-2 Gumbel distribution.
859 extension function CDF.T2G (x, a, b) = gsl_cdf_gumbel2_P (x, a, b);
860 extension function IDF.T2G (P >= 0 && P <= 1, a, b)
861 = gsl_cdf_gumbel2_P (P, a, b);
862 extension function PDF.T2G (x, a, b) = gsl_ran_gumbel2_pdf (x, a, b);
863 no_opt extension function RV.T2G (a, b) = gsl_ran_gumbel2 (get_rng (), a, b);
865 // Uniform distribution.
866 function CDF.UNIFORM (x <= b, a <= x, b) = gsl_cdf_flat_P (x, a, b);
867 function IDF.UNIFORM (P >= 0 && P <= 1, a <= b, b)
868 = gsl_cdf_flat_Pinv (P, a, b);
869 function PDF.UNIFORM (x <= b, a <= x, b) = gsl_ran_flat_pdf (x, a, b);
870 no_opt function RV.UNIFORM (a <= b, b) = gsl_ran_flat (get_rng (), a, b);
871 no_opt function UNIFORM (b >= 0) = gsl_ran_flat (get_rng (), 0, b);
873 // Weibull distribution.
874 function CDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_cdf_weibull_P (x, a, b);
875 function IDF.WEIBULL (P >= 0 && P < 1, a > 0, b > 0)
876 = gsl_cdf_weibull_Pinv (P, a, b);
877 function PDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_ran_weibull_pdf (x, a, b);
878 no_opt function RV.WEIBULL (a > 0, b > 0) = gsl_ran_weibull (get_rng (), a, b);
880 // Bernoulli distribution.
881 function CDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1)
882 = k ? 1 : 1 - p;
883 function PDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1)
884 = gsl_ran_bernoulli_pdf (k, p);
885 no_opt function RV.BERNOULLI (p >= 0 && p <= 1)
886 = gsl_ran_bernoulli (get_rng (), p);
888 // Binomial distribution.
889 function CDF.BINOM (k, n > 0 && n == floor (n), p >= 0 && p <= 1)
890 = gsl_cdf_binomial_P (k, p, n);
891 function PDF.BINOM (k >= 0 && k == floor (k) && k <= n,
892 n > 0 && n == floor (n),
893 p >= 0 && p <= 1)
894 = gsl_ran_binomial_pdf (k, p, n);
895 no_opt function RV.BINOM (p > 0 && p == floor (p), n >= 0 && n <= 1)
896 = gsl_ran_binomial (get_rng (), p, n);
898 // Geometric distribution.
899 function CDF.GEOM (k >= 1 && k == floor (k), p >= 0 && p <= 1)
900 = gsl_cdf_geometric_P (k, p);
901 function PDF.GEOM (k >= 1 && k == floor (k),
902 p >= 0 && p <= 1)
903 = gsl_ran_geometric_pdf (k, p);
904 no_opt function RV.GEOM (p >= 0 && p <= 1) = gsl_ran_geometric (get_rng (), p);
906 // Hypergeometric distribution.
907 function CDF.HYPER (k >= 0 && k == floor (k) && k <= c,
908 a > 0 && a == floor (a),
909 b > 0 && b == floor (b) && b <= a,
910 c > 0 && c == floor (c) && c <= a)
911 = gsl_cdf_hypergeometric_P (k, c, a - c, b);
912 function PDF.HYPER (k >= 0 && k == floor (k) && k <= c,
913 a > 0 && a == floor (a),
914 b > 0 && b == floor (b) && b <= a,
915 c > 0 && c == floor (c) && c <= a)
916 = gsl_ran_hypergeometric_pdf (k, c, a - c, b);
917 no_opt function RV.HYPER (a > 0 && a == floor (a),
918 b > 0 && b == floor (b) && b <= a,
919 c > 0 && c == floor (c) && c <= a)
920 = gsl_ran_hypergeometric (get_rng (), c, a - c, b);
922 // Logarithmic distribution.
923 extension function PDF.LOG (k >= 1, p > 0 && p <= 1)
924 = gsl_ran_logarithmic_pdf (k, p);
925 no_opt extension function RV.LOG (p > 0 && p <= 1)
926 = gsl_ran_logarithmic (get_rng (), p);
928 // Negative binomial distribution.
929 function CDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
930 = gsl_cdf_negative_binomial_P (k, p, n);
931 function PDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
932 = gsl_ran_negative_binomial_pdf (k, p, n);
933 no_opt function RV.NEGBIN (n == floor (n), p > 0 && p <= 1)
934 = gsl_ran_negative_binomial (get_rng (), p, n);
936 // Poisson distribution.
937 function CDF.POISSON (k >= 0 && k == floor (k), mu > 0)
938 = gsl_cdf_poisson_P (k, mu);
939 function PDF.POISSON (k >= 0 && k == floor (k), mu > 0)
940 = gsl_ran_poisson_pdf (k, mu);
941 no_opt function RV.POISSON (mu > 0) = gsl_ran_poisson (get_rng (), mu);
943 // Weirdness.
944 absorb_miss boolean function MISSING (x) = x == SYSMIS || !finite (x);
945 absorb_miss boolean function SYSMIS (x) = x == SYSMIS || !finite (x);
946 no_opt boolean function SYSMIS (num_var v)
947 case c;
949 return case_num (c, v) == SYSMIS;
951 no_opt boolean function VALUE (num_var v)
952 case c;
954 return case_num (c, v);
957 no_opt operator VEC_ELEM_NUM (idx)
958 vector v;
959 case c;
961 if (idx >= 1 && idx <= vector_get_var_cnt (v))
963 const struct variable *var = vector_get_var (v, (size_t) idx - 1);
964 double value = case_num (c, var);
965 return !var_is_num_missing (var, value, MV_USER) ? value : SYSMIS;
967 else
969 if (idx == SYSMIS)
970 msg (SE, _("SYSMIS is not a valid index value for vector "
971 "%s. The result will be set to SYSMIS."),
972 vector_get_name (v));
973 else
974 msg (SE, _("%g is not a valid index value for vector %s. "
975 "The result will be set to SYSMIS."),
976 idx, vector_get_name (v));
977 return SYSMIS;
981 absorb_miss no_opt string operator VEC_ELEM_STR (idx)
982 expression e;
983 vector v;
984 case c;
986 if (idx >= 1 && idx <= vector_get_var_cnt (v))
988 struct variable *var = vector_get_var (v, (size_t) idx - 1);
989 return copy_string (e, CHAR_CAST_BUG (char *, case_str (c, var)),
990 var_get_width (var));
992 else
994 if (idx == SYSMIS)
995 msg (SE, _("SYSMIS is not a valid index value for vector "
996 "%s. The result will be set to the empty string."),
997 vector_get_name (v));
998 else
999 msg (SE, _("%g is not a valid index value for vector %s. "
1000 "The result will be set to the empty string."),
1001 idx, vector_get_name (v));
1002 return empty_string;
1006 // Terminals.
1008 no_opt operator NUM_VAR ()
1009 case c;
1010 num_var v;
1012 double d = case_num (c, v);
1013 return !var_is_num_missing (v, d, MV_USER) ? d : SYSMIS;
1016 no_opt string operator STR_VAR ()
1017 case c;
1018 expression e;
1019 str_var v;
1021 struct substring s = alloc_string (e, var_get_width (v));
1022 memcpy (s.string, case_str (c, v), var_get_width (v));
1023 return s;
1026 no_opt perm_only function LAG (num_var v, pos_int n_before)
1027 dataset ds;
1029 const struct ccase *c = lagged_case (ds, n_before);
1030 if (c != NULL)
1032 double x = case_num (c, v);
1033 return !var_is_num_missing (v, x, MV_USER) ? x : SYSMIS;
1035 else
1036 return SYSMIS;
1039 no_opt perm_only function LAG (num_var v)
1040 dataset ds;
1042 const struct ccase *c = lagged_case (ds, 1);
1043 if (c != NULL)
1045 double x = case_num (c, v);
1046 return !var_is_num_missing (v, x, MV_USER) ? x : SYSMIS;
1048 else
1049 return SYSMIS;
1052 no_opt perm_only string function LAG (str_var v, pos_int n_before)
1053 expression e;
1054 dataset ds;
1056 const struct ccase *c = lagged_case (ds, n_before);
1057 if (c != NULL)
1058 return copy_string (e, CHAR_CAST_BUG (char *, case_str (c, v)),
1059 var_get_width (v));
1060 else
1061 return empty_string;
1064 no_opt perm_only string function LAG (str_var v)
1065 expression e;
1066 dataset ds;
1068 const struct ccase *c = lagged_case (ds, 1);
1069 if (c != NULL)
1070 return copy_string (e, CHAR_CAST_BUG (char *, case_str (c, v)),
1071 var_get_width (v));
1072 else
1073 return empty_string;
1076 no_opt operator NUM_SYS ()
1077 case c;
1078 num_var v;
1080 return case_num (c, v) == SYSMIS;
1083 no_opt operator NUM_VAL ()
1084 case c;
1085 num_var v;
1087 return case_num (c, v);
1090 no_opt operator CASENUM ()
1091 case_idx idx;
1093 return idx;