/
real-valued_basic_functions.Rmd
1656 lines (1246 loc) · 56.7 KB
/
real-valued_basic_functions.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Real-Valued Basic Functions
This chapter describes built-in functions that take zero or more real
or integer arguments and return real values.
```{r results='asis', echo=FALSE}
if (knitr::is_html_output()) {
cat(' * <a href="fun-vectorization.html">Vectorization of Real-Valued Functions</a>\n')
cat(' * <a href="built-in-constants.html">Mathematical Constants</a>\n')
cat(' * <a href="special-values.html">Special Values</a>\n')
cat(' * <a href="get-log-prob.html">Log Probability Function</a>\n')
cat(' * <a href="logical-functions.html">Logical Functions</a>\n')
cat(' * <a href="real-valued-arithmetic-operators.html">Real-Valued Arithmetic Operators</a>\n')
cat(' * <a href="step-functions.html">Step-like Functions</a>\n')
cat(' * <a href="power-and-logarithm-functions.html">Power and Logarithm Functions</a>\n')
cat(' * <a href="trigonometric-functions.html">Trigonometric Functions</a>\n')
cat(' * <a href="hyperbolic-trigonometric-functions.html">Hyperbolic Trigonometric Functions</a>\n')
cat(' * <a href="link-functions.html">Link Functions</a>\n')
cat(' * <a href="phi-function.html">Probability-Related Functions</a>\n')
cat(' * <a href="betafun.html">Combinatorial Functions</a>\n')
cat(' * <a href="composed-functions.html">Composed Functions</a>\n')
cat(' * <a href="special-functions.html">Special Functions</a>\n')
}
```
## Vectorization of real-valued functions {#fun-vectorization}
Although listed in this chapter, many of Stan's built-in functions are
vectorized so that they may be applied to any argument type. The
vectorized form of these functions is not any faster than writing an
explicit loop that iterates over the elements applying the
function---it's just easier to read and write and less error prone.
### Unary function vectorization
Many of Stan's unary functions can be applied to any argument type.
For example, the exponential function, `exp`, can be applied to `real`
arguments or arrays of `real` arguments. Other than for integer
arguments, the result type is the same as the argument type, including
dimensionality and size. Integer arguments are first promoted to real
values, but the result will still have the same dimensionality and
size as the argument.
#### Real and real array arguments
When applied to a simple real value, the result is a real value. When
applied to arrays, vectorized functions like `exp()` are defined
elementwise. For example,
```stan
// declare some variables for arguments
real x0;
array[5] real x1;
array[4, 7] real x2;
// ...
// declare some variables for results
real y0;
array[5] real y1;
array[4, 7] real y2;
// ...
// calculate and assign results
y0 = exp(x0);
y1 = exp(x1);
y2 = exp(x2);
```
When `exp` is applied to an array, it applies elementwise. For
example, the statement above,
```stan
y2 = exp(x2);
```
produces the same result for `y2` as the explicit loop
```stan
for (i in 1:4) {
for (j in 1:7) {
y2[i, j] = exp(x2[i, j]);
}
}
```
#### Vector and matrix arguments
Vectorized functions also apply elementwise to vectors and matrices.
For example,
```stan
vector[5] xv;
row_vector[7] xrv;
matrix[10, 20] xm;
vector[5] yv;
row_vector[7] yrv;
matrix[10, 20] ym;
yv = exp(xv);
yrv = exp(xrv);
ym = exp(xm);
```
Arrays of vectors and matrices work the same way. For example,
```stan
array[12] matrix[17, 93] u;
array[12] matrix[17, 93] z;
z = exp(u);
```
After this has been executed, `z[i, j, k]` will be equal to `exp(u[i,
j, k])`.
#### Integer and integer array arguments
Integer arguments are promoted to real values in vectorized unary
functions. Thus if `n` is of type `int`, `exp(n)` is of type `real`.
Arrays work the same way, so that if `n2` is a one dimensional array
of integers, then `exp(n2)` will be a one-dimensional array of reals
with the same number of elements as `n2`. For example,
```stan
array[23] int n1;
array[23] real z1;
z1 = exp(n1);
```
It would be illegal to try to assign `exp(n1)` to an array of
integers; the return type is a real array.
### Binary function vectorization
Like the unary functions, many of Stan's binary functions have been
vectorized, and can be applied elementwise to combinations of both
scalars or container types.
#### Scalar and scalar array arguments
When applied to two scalar values, the result is a scalar value. When
applied to two arrays, or combination of a scalar value and an array,
vectorized functions like `pow()` are defined elementwise. For example,
```stan
// declare some variables for arguments
real x00;
real x01;
array[5] real x10;
array[5]real x11;
array[4, 7] real x20;
array[4, 7] real x21;
// ...
// declare some variables for results
real y0;
array[5] real y1;
array[4, 7] real y2;
// ...
// calculate and assign results
y0 = pow(x00, x01);
y1 = pow(x10, x11);
y2 = pow(x20, x21);
```
When `pow` is applied to two arrays, it applies elementwise. For
example, the statement above,
```stan
y2 = pow(x20, x21);
```
produces the same result for `y2` as the explicit loop
```stan
for (i in 1:4) {
for (j in 1:7) {
y2[i, j] = pow(x20[i, j], x21[i, j]);
}
}
```
Alternatively, if a combination of an array and a scalar are
provided, the scalar value is broadcast to be applied to each
value of the array. For example, the following statement:
```stan
y2 = pow(x20, x00);
```
produces the same result for `y2` as the explicit loop:
```stan
for (i in 1:4) {
for (j in 1:7) {
y2[i, j] = pow(x20[i, j], x00);
}
}
```
#### Vector and matrix arguments
Vectorized binary functions also apply elementwise to vectors and matrices,
and to combinations of these with scalar values.
For example,
```stan
real x00;
vector[5] xv00;
vector[5] xv01;
row_vector[7] xrv;
matrix[10, 20] xm;
vector[5] yv;
row_vector[7] yrv;
matrix[10, 20] ym;
yv = pow(xv00, xv01);
yrv = pow(xrv, x00);
ym = pow(x00, xm);
```
Arrays of vectors and matrices work the same way. For example,
```stan
array[12] matrix[17, 93] u;
array[12] matrix[17, 93] z;
z = pow(u, x00);
```
After this has been executed, `z[i, j, k]` will be equal to `pow(u[i,
j, k], x00)`.
#### Input & return types
Vectorised binary functions require that both inputs, unless one is a real,
be containers of the same type and size. For example, the following statements
are legal:
```stan
vector[5] xv;
row_vector[7] xrv;
matrix[10, 20] xm;
vector[5] yv = pow(xv, xv)
row_vector[7] yrv = pow(xrv, xrv)
matrix[10, 20] = pow(xm, xm)
```
But the following statements are not:
```stan
vector[5] xv;
vector[7] xv2;
row_vector[5] xrv;
// Cannot mix different types
vector[5] yv = pow(xv, xrv)
// Cannot mix different sizes of the same type
vector[5] yv = pow(xv, xv2)
```
While the vectorized binary functions generally require the same input types,
the only exception to this is for binary functions that require one input to be
an integer and the other to be a real (e.g., `bessel_first_kind`). For these
functions, one argument can be a container of any type while the other can be
an integer array, as long as the dimensions of both are the same. For example,
the following statements are legal:
```stan
vector[5] xv;
matrix[5, 5] xm;
array[5] int xi;
array[5, 5] int xii;
vector[5] yv = bessel_first_kind(xi, xv);
matrix[5, 5] ym = bessel_first_kind(xii, xm);
```
Whereas these are not:
```stan
vector[5] xv;
matrix[5, 5] xm;
array[7] int xi;
// Dimensions of containers do not match
vector[5] yv = bessel_first_kind(xi, xv);
// Function requires first argument be an integer type
matrix[5, 5] ym = bessel_first_kind(xm, xm);
```
## Mathematical constants {#built-in-constants}
Constants are represented as functions with no arguments and must be
called as such. For instance, the mathematical constant $\pi$ must be
written in a Stan program as `pi()`.
<!-- real; pi; (); -->
\index{{\tt \bfseries pi }!{\tt (): real}|hyperpage}
`real` **`pi`**`()`<br>\newline
$\pi$, the ratio of a circle's circumference to its diameter
`r since("2.0")`
<!-- real; e; (); -->
\index{{\tt \bfseries e }!{\tt (): real}|hyperpage}
`real` **`e`**`()`<br>\newline
$e$, the base of the natural logarithm
`r since("2.0")`
<!-- real; sqrt2; (); -->
\index{{\tt \bfseries sqrt2 }!{\tt (): real}|hyperpage}
`real` **`sqrt2`**`()`<br>\newline
The square root of 2
`r since("2.0")`
<!-- real; log2; (); -->
\index{{\tt \bfseries log2 }!{\tt (): real}|hyperpage}
`real` **`log2`**`()`<br>\newline
The natural logarithm of 2
`r since("2.0")`
<!-- real; log10; (); -->
\index{{\tt \bfseries log10 }!{\tt (): real}|hyperpage}
`real` **`log10`**`()`<br>\newline
The natural logarithm of 10
`r since("2.0")`
## Special values
<!-- real; not_a_number; (); -->
\index{{\tt \bfseries not\_a\_number }!{\tt (): real}|hyperpage}
`real` **`not_a_number`**`()`<br>\newline
Not-a-number, a special non-finite real value returned to signal an
error
`r since("2.0")`
<!-- real; positive_infinity; (); -->
\index{{\tt \bfseries positive\_infinity }!{\tt (): real}|hyperpage}
`real` **`positive_infinity`**`()`<br>\newline
Positive infinity, a special non-finite real value larger than all
finite numbers
`r since("2.0")`
<!-- real; negative_infinity; (); -->
\index{{\tt \bfseries negative\_infinity }!{\tt (): real}|hyperpage}
`real` **`negative_infinity`**`()`<br>\newline
Negative infinity, a special non-finite real value smaller than all
finite numbers
`r since("2.0")`
<!-- real; machine_precision; (); -->
\index{{\tt \bfseries machine\_precision }!{\tt (): real}|hyperpage}
`real` **`machine_precision`**`()`<br>\newline
The smallest number $x$ such that $(x + 1) \neq 1$ in floating-point
arithmetic on the current hardware platform
`r since("2.0")`
## Log probability function {#get-log-prob}
The basic purpose of a Stan program is to compute a log probability
function and its derivatives. The log probability function in a Stan
model outputs the log density on the unconstrained scale. A log
probability accumulator starts at zero and is then incremented in
various ways by a Stan program. The variables are first transformed
from unconstrained to constrained, and the log Jacobian determinant
added to the log probability accumulator. Then the model block is
executed on the constrained parameters, with each sampling statement
(`~`) and log probability increment statement (`increment_log_prob`)
adding to the accumulator. At the end of the model block execution,
the value of the log probability accumulator is the log probability
value returned by the Stan program.
Stan provides a special built-in function `target()` that takes no
arguments and returns the current value of the log probability
accumulator.[^fn_lp] This function is primarily useful for debugging
purposes, where for instance, it may be used with a print statement to
display the log probability accumulator at various stages of execution
to see where it becomes ill defined.
[^fn_lp]: This function used to be called `get_lp()`, but that name
has been deprecated; using it will print a warning. The function
`get_lp()` will be removed in Stan 2.32.0.
<!-- real; target; (); -->
\index{{\tt \bfseries target }!{\tt (): real}|hyperpage}
`real` **`target`**`()`<br>\newline
Return the current value of the log probability accumulator.
`r since("2.10")`
<!-- real; get_lp; (); -->
\index{{\tt \bfseries get\_lp }!{\tt (): real}|hyperpage}
`real` **`get_lp`**`()`<br>\newline
Return the current value of the log probability accumulator;
**deprecated;** - use `target()` instead.
`r since("2.5, scheduled for removal in 2.32.0")`
Both `target` and the deprecated `get_lp` act like other functions
ending in `_lp`, meaning that they may only may only be used in the
model block.
## Logical functions
Like C++, BUGS, and R, Stan uses 0 to encode false, and 1 to encode
true. Stan supports the usual boolean comparison operations and
boolean operators. These all have the same syntax and precedence as
in C++; for the full list of operators and precedences, see the
reference manual.
### Comparison operators
All comparison operators return boolean values, either 0 or 1. Each
operator has two signatures, one for integer comparisons and one for
floating-point comparisons. Comparing an integer and real value is
carried out by first promoting the integer value.
<!-- int; operator<; (int x, int y); -->
\index{{\tt \bfseries operator\_logical\_less\_than }!{\tt (int x, int y): int}|hyperpage}
`int` **`operator<`**`(int x, int y)`<br>\newline
<!-- int; operator<; (real x, real y); -->
\index{{\tt \bfseries operator\_logical\_less\_than }!{\tt (real x, real y): int}|hyperpage}
`int` **`operator<`**`(real x, real y)`<br>\newline
Return 1 if x is less than y and 0 otherwise. \[ \text{operator<}(x,y)
= \begin{cases} 1 & \text{if $x < y$} \\ 0 & \text{otherwise}
\end{cases} \]
`r since("2.0")`
<!-- int; operator<=; (int x, int y); -->
\index{{\tt \bfseries operator\_logical\_less\_than\_equal }!{\tt (int x, int y): int}|hyperpage}
`int` **`operator<=`**`(int x, int y)`<br>\newline
<!-- int; operator<=; (real x, real y); -->
\index{{\tt \bfseries operator\_logical\_less\_than\_equal }!{\tt (real x, real y): int}|hyperpage}
`int` **`operator<=`**`(real x, real y)`<br>\newline
Return 1 if x is less than or equal y and 0 otherwise. \[
\text{operator<=}(x,y) = \begin{cases} 1 & \text{if $x \leq y$} \\ 0 &
\text{otherwise} \end{cases} \]
`r since("2.0")`
<!-- int; operator>; (int x, int y); -->
\index{{\tt \bfseries operator\_logical\_greater\_than }!{\tt (int x, int y): int}|hyperpage}
`int` **`operator>`**`(int x, int y)`<br>\newline
<!-- int; operator>; (real x, real y); -->
\index{{\tt \bfseries operator\_logical\_greater\_than }!{\tt (real x, real y): int}|hyperpage}
`int` **`operator>`**`(real x, real y)`<br>\newline
Return 1 if x is greater than y and 0 otherwise. \[ \text{operator>} =
\begin{cases} 1 & \text{if $x > y$} \\ 0 & \text{otherwise}
\end{cases} \]
`r since("2.0")`
<!-- int; operator>=; (int x, int y); -->
\index{{\tt \bfseries operator\_logical\_greater\_than\_equal }!{\tt (int x, int y): int}|hyperpage}
`int` **`operator>=`**`(int x, int y)`<br>\newline
<!-- int; operator>=; (real x, real y); -->
\index{{\tt \bfseries operator\_logical\_greater\_than\_equal }!{\tt (real x, real y): int}|hyperpage}
`int` **`operator>=`**`(real x, real y)`<br>\newline
Return 1 if x is greater than or equal to y and 0 otherwise. \[
\text{operator>=} = \begin{cases} 1 & \text{if $x \geq y$} \\ 0 &
\text{otherwise} \end{cases} \]
`r since("2.0")`
<!-- int; operator==; (int x, int y); -->
\index{{\tt \bfseries operator\_logical\_equal }!{\tt (int x, int y): int}|hyperpage}
`int` **`operator==`**`(int x, int y)`<br>\newline
<!-- int; operator==; (real x, real y); -->
\index{{\tt \bfseries operator\_logical\_equal }!{\tt (real x, real y): int}|hyperpage}
`int` **`operator==`**`(real x, real y)`<br>\newline
Return 1 if x is equal to y and 0 otherwise. \[ \text{operator==}(x,y)
= \begin{cases} 1 & \text{if $x = y$} \\ 0 & \text{otherwise}
\end{cases} \]
`r since("2.0")`
<!-- int; operator!=; (int x, int y); -->
\index{{\tt \bfseries operator\_logical\_not\_equal }!{\tt (int x, int y): int}|hyperpage}
`int` **`operator!=`**`(int x, int y)`<br>\newline
<!-- int; operator!=; (real x, real y); -->
\index{{\tt \bfseries operator\_logical\_not\_equal }!{\tt (real x, real y): int}|hyperpage}
`int` **`operator!=`**`(real x, real y)`<br>\newline
Return 1 if x is not equal to y and 0 otherwise. \[
\text{operator!=}(x,y) = \begin{cases} 1 & \text{if $x \neq y$} \\ 0 &
\text{otherwise} \end{cases} \]
`r since("2.0")`
### Boolean operators
Boolean operators return either 0 for false or 1 for true. Inputs may
be any real or integer values, with non-zero values being treated as
true and zero values treated as false. These operators have the usual
precedences, with negation (not) binding the most tightly, conjunction
the next and disjunction the weakest; all of the operators bind more
tightly than the comparisons. Thus an expression such as `!a && b` is
interpreted as `(!a) && b`, and `a < b || c >= d && e != f` as `(a <
b) || (((c >= d) && (e != f)))`.
<!-- int; operator!; (int x); -->
\index{{\tt \bfseries operator\_negation }!{\tt (int x): int}|hyperpage}
`int` **`operator!`**`(int x)`<br>\newline
<!-- int; operator!; (real x); -->
\index{{\tt \bfseries operator\_negation }!{\tt (real x): int}|hyperpage}
`int` **`operator!`**`(real x)`<br>\newline
Return 1 if x is zero and 0 otherwise. \[ \text{operator!}(x) =
\begin{cases} 0 & \text{if $x \neq 0$} \\ 1 & \text{if $x = 0$}
\end{cases} \]
`r since("2.0")`
<!-- int; operator&&; (int x, int y); -->
\index{{\tt \bfseries operator\_logical\_and }!{\tt (int x, int y): int}|hyperpage}
`int` **`operator&&`**`(int x, int y)`<br>\newline
<!-- int; operator&&; (real x, real y); -->
\index{{\tt \bfseries operator\_logical\_and }!{\tt (real x, real y): int}|hyperpage}
`int` **`operator&&`**`(real x, real y)`<br>\newline
Return 1 if x is unequal to 0 and y is unequal to 0. \[
\mathrm{operator\&\&}(x,y) = \begin{cases} 1 & \text{if $x \neq 0$}
\text{ and } y \neq 0\\ 0 & \text{otherwise} \end{cases} \]
`r since("2.0")`
<!-- int; operator||; (int x, int y); -->
\index{{\tt \bfseries operator\_logical\_or }!{\tt (int x, int y): int}|hyperpage}
`int` **`operator||`**`(int x, int y)`<br>\newline
<!-- int; operator||; (real x, real y); -->
\index{{\tt \bfseries operator\_logical\_or }!{\tt (real x, real y): int}|hyperpage}
`int` **`operator||`**`(real x, real y)`<br>\newline
Return 1 if x is unequal to 0 or y is unequal to 0. \[
\text{operator||}(x,y) = \begin{cases} 1 & \text{if $x \neq 0$}
\textrm{ or } y \neq 0\\ 0 & \text{otherwise} \end{cases} \]
`r since("2.0")`
#### Boolean operator short circuiting
Like in C++, the boolean operators `&&` and `||` and are implemented
to short circuit directly to a return value after evaluating the first
argument if it is sufficient to resolve the result. In evaluating `a
|| b`, if `a` evaluates to a value other than zero, the expression
returns the value 1 without evaluating the expression `b`. Similarly,
evaluating `a && b` first evaluates `a`, and if the result is zero,
returns 0 without evaluating `b`.
### Logical functions
The logical functions introduce conditional behavior functionally and
are primarily provided for compatibility with BUGS and JAGS.
<!-- real; step; (real x); -->
\index{{\tt \bfseries step }!{\tt (real x): real}|hyperpage}
`real` **`step`**`(real x)`<br>\newline
Return 1 if x is positive and 0 otherwise. \[ \text{step}(x) =
\begin{cases} 0 & \text{if } x < 0 \\ 1 & \text{otherwise} \end{cases}
\] _**Warning:**_ `int_step(0)` and `int_step(NaN)` return 0 whereas
`step(0)` and `step(NaN)` return 1.
The step function is often used in BUGS to perform conditional
operations. For instance, `step(a-b)` evaluates to 1 if `a` is
greater than `b` and evaluates to 0 otherwise. `step` is a step-like
functions; see the warning in section [step functions](#step-functions) applied to
expressions dependent on parameters.
`r since("2.0")`
<!-- int; is_inf; (real x); -->
\index{{\tt \bfseries is\_inf }!{\tt (real x): int}|hyperpage}
`int` **`is_inf`**`(real x)`<br>\newline
Return 1 if x is infinite (positive or negative) and 0 otherwise.
`r since("2.5")`
<!-- int; is_nan; (real x); -->
\index{{\tt \bfseries is\_nan }!{\tt (real x): int}|hyperpage}
`int` **`is_nan`**`(real x)`<br>\newline
Return 1 if x is NaN and 0 otherwise.
`r since("2.5")`
Care must be taken because both of these indicator functions are
step-like and thus can cause discontinuities in gradients when applied
to parameters; see section [step-like functions](#step-functions) for details.
## Real-valued arithmetic operators {#real-valued-arithmetic-operators}
The arithmetic operators are presented using C++ notation. For
instance `operator+(x,y)` refers to the binary addition operator and
`operator-(x)` to the unary negation operator. In Stan programs,
these are written using the usual infix and prefix notations as `x +
y` and `-x`, respectively.
### Binary infix operators
<!-- real; operator+; (real x, real y); -->
\index{{\tt \bfseries operator\_add }!{\tt (real x, real y): real}|hyperpage}
`real` **`operator+`**`(real x, real y)`<br>\newline
Return the sum of x and y. \[ (x + y) = \text{operator+}(x,y) = x+y \]
`r since("2.0")`
<!-- real; operator-; (real x, real y); -->
\index{{\tt \bfseries operator\_subtract }!{\tt (real x, real y): real}|hyperpage}
`real` **`operator-`**`(real x, real y)`<br>\newline
Return the difference between x and y. \[ (x - y) =
\text{operator-}(x,y) = x - y \]
`r since("2.0")`
<!-- real; operator*; (real x, real y); -->
\index{{\tt \bfseries operator\_multiply }!{\tt (real x, real y): real}|hyperpage}
`real` **`operator*`**`(real x, real y)`<br>\newline
Return the product of x and y. \[ (x * y) = \text{operator*}(x,y) = xy
\]
`r since("2.0")`
<!-- real; operator/; (real x, real y); -->
\index{{\tt \bfseries operator\_divide }!{\tt (real x, real y): real}|hyperpage}
`real` **`operator/`**`(real x, real y)`<br>\newline
Return the quotient of x and y. \[ (x / y) = \text{operator/}(x,y) =
\frac{x}{y} \]
`r since("2.0")`
<!-- real; operator^; (real x, real y); -->
\index{{\tt \bfseries operator\_pow }!{\tt (real x, real y): real}|hyperpage}
`real` **`operator^`**`(real x, real y)`<br>\newline
Return x raised to the power of y. \[ (x^\mathrm{\wedge}y) =
\text{operator}^\mathrm{\wedge}(x,y) = x^y \]
`r since("2.5")`
### Unary prefix operators
<!-- real; operator-; (real x); -->
\index{{\tt \bfseries operator\_subtract }!{\tt (real x): real}|hyperpage}
`real` **`operator-`**`(real x)`<br>\newline
Return the negation of the subtrahend x. \[ \text{operator-}(x) = (-x)
\]
`r since("2.0")`
<!-- real; operator+; (real x); -->
\index{{\tt \bfseries operator\_add }!{\tt (real x): real}|hyperpage}
`real` **`operator+`**`(real x)`<br>\newline
Return the value of x. \[ \text{operator+}(x) = x \]
`r since("2.0")`
## Step-like functions {#step-functions}
_**Warning:**_ *These functions can seriously hinder sampling and
optimization efficiency for gradient-based methods (e.g., NUTS, HMC,
BFGS) if applied to parameters (including transformed parameters and
local variables in the transformed parameters or model block). The
problem is that they break gradients due to discontinuities coupled
with zero gradients elsewhere. They do not hinder sampling when
used in the data, transformed data, or generated quantities blocks.*
### Absolute value functions
<!-- R; fabs; (T x); -->
\index{{\tt \bfseries fabs }!{\tt (T x): R}|hyperpage}
`R` **`fabs`**`(T x)`<br>\newline
absolute value of x
`r since("2.0, vectorized in 2.13")`
<!-- real; fdim; (real x, real y); -->
\index{{\tt \bfseries fdim }!{\tt (real x, real y): real}|hyperpage}
`real` **`fdim`**`(real x, real y)`<br>\newline
Return the positive difference between x and y, which is x - y if x is
greater than y and 0 otherwise; see warning above.
\[ \text{fdim}(x,y) = \begin{cases} x-y &
\text{if } x \geq y \\ 0 & \text{otherwise} \end{cases} \]
`r since("2.0")`
<!-- R; fdim; (T1 x, T2 y); -->
\index{{\tt \bfseries fdim }!{\tt (T1 x, T2 y): R}|hyperpage}
`R` **`fdim`**`(T1 x, T2 y)`<br>\newline
Vectorized implementation of the `fdim` function
`r since("2.25")`
### Bounds functions
<!-- real; fmin; (real x, real y); -->
\index{{\tt \bfseries fmin }!{\tt (real x, real y): real}|hyperpage}
`real` **`fmin`**`(real x, real y)`<br>\newline
Return the minimum of x and y; see warning above.
\[ \text{fmin}(x,y) = \begin{cases} x &
\text{if } x \leq y \\ y & \text{otherwise} \end{cases} \]
`r since("2.0")`
<!-- R; fmin; (T1 x, T2 y); -->
\index{{\tt \bfseries fmin }!{\tt (T1 x, T2 y): R}|hyperpage}
`R` **`fmin`**`(T1 x, T2 y)`<br>\newline
Vectorized implementation of the `fmin` function
`r since("2.25")`
<!-- real; fmax; (real x, real y); -->
\index{{\tt \bfseries fmax }!{\tt (real x, real y): real}|hyperpage}
`real` **`fmax`**`(real x, real y)`<br>\newline
Return the maximum of x and y; see warning above.
\[ \text{fmax}(x,y) = \begin{cases} x &
\text{if } x \geq y \\ y & \text{otherwise} \end{cases} \]
`r since("2.0")`
<!-- R; fmax; (T1 x, T2 y); -->
\index{{\tt \bfseries fmax }!{\tt (T1 x, T2 y): R}|hyperpage}
`R` **`fmax`**`(T1 x, T2 y)`<br>\newline
Vectorized implementation of the `fmax` function
`r since("2.25")`
### Arithmetic functions
<!-- real; fmod; (real x, real y); -->
\index{{\tt \bfseries fmod }!{\tt (real x, real y): real}|hyperpage}
`real` **`fmod`**`(real x, real y)`<br>\newline
Return the real value remainder after dividing x by y; see warning above.
\[ \text{fmod}(x,y) = x - \left\lfloor \frac{x}{y} \right\rfloor \, y \]
The operator $\lfloor u \rfloor$ is the floor operation; see below.
`r since("2.0")`
<!-- R; fmod; (T1 x, T2 y); -->
\index{{\tt \bfseries fmod }!{\tt (T1 x, T2 y): R}|hyperpage}
`R` **`fmod`**`(T1 x, T2 y)`<br>\newline
Vectorized implementation of the `fmod` function
`r since("2.25")`
### Rounding functions
_**Warning:**_ Rounding functions convert real values to integers.
Because the output is an integer, any gradient information resulting
from functions applied to the integer is not passed to the real value
it was derived from. With MCMC sampling using HMC or NUTS, the MCMC
acceptance procedure will correct for any error due to poor gradient
calculations, but the result is likely to be reduced acceptance
probabilities and less efficient sampling.
The rounding functions cannot be used as indices to arrays because
they return real values. Stan may introduce integer-valued versions
of these in the future, but as of now, there is no good workaround.
<!-- R; floor; (T x); -->
\index{{\tt \bfseries floor }!{\tt (T x): R}|hyperpage}
`R` **`floor`**`(T x)`<br>\newline
floor of x, which is the largest integer less than or equal to x,
converted to a real value; see warning at start of section
[step-like functions](#step-functions)
`r since("2.0, vectorized in 2.13")`
<!-- R; ceil; (T x); -->
\index{{\tt \bfseries ceil }!{\tt (T x): R}|hyperpage}
`R` **`ceil`**`(T x)`<br>\newline
ceiling of x, which is the smallest integer greater than or equal to
x, converted to a real value; see warning at start of section
[step-like functions](#step-functions)
`r since("2.0, vectorized in 2.13")`
<!-- R; round; (T x); -->
\index{{\tt \bfseries round }!{\tt (T x): R}|hyperpage}
`R` **`round`**`(T x)`<br>\newline
nearest integer to x, converted to a real value; see warning at start
of section [step-like functions](#step-functions)
`r since("2.0, vectorized in 2.13")`
<!-- R; trunc; (T x); -->
\index{{\tt \bfseries trunc }!{\tt (T x): R}|hyperpage}
`R` **`trunc`**`(T x)`<br>\newline
integer nearest to but no larger in magnitude than x, converted to a
double value; see warning at start of section [step-like functions](#step-functions)
`r since("2.0, vectorized in 2.13")`
## Power and logarithm functions
<!-- R; sqrt; (T x); -->
\index{{\tt \bfseries sqrt }!{\tt (T x): R}|hyperpage}
`R` **`sqrt`**`(T x)`<br>\newline
square root of x
`r since("2.0, vectorized in 2.13")`
<!-- R; cbrt; (T x); -->
\index{{\tt \bfseries cbrt }!{\tt (T x): R}|hyperpage}
`R` **`cbrt`**`(T x)`<br>\newline
cube root of x
`r since("2.0, vectorized in 2.13")`
<!-- R; square; (T x); -->
\index{{\tt \bfseries square }!{\tt (T x): R}|hyperpage}
`R` **`square`**`(T x)`<br>\newline
square of x
`r since("2.0, vectorized in 2.13")`
<!-- R; exp; (T x); -->
\index{{\tt \bfseries exp }!{\tt (T x): R}|hyperpage}
`R` **`exp`**`(T x)`<br>\newline
natural exponential of x
`r since("2.0, vectorized in 2.13")`
<!-- R; exp2; (T x); -->
\index{{\tt \bfseries exp2 }!{\tt (T x): R}|hyperpage}
`R` **`exp2`**`(T x)`<br>\newline
base-2 exponential of x
`r since("2.0, vectorized in 2.13")`
<!-- R; log; (T x); -->
\index{{\tt \bfseries log }!{\tt (T x): R}|hyperpage}
`R` **`log`**`(T x)`<br>\newline
natural logarithm of x
`r since("2.0, vectorized in 2.13")`
<!-- R; log2; (T x); -->
\index{{\tt \bfseries log2 }!{\tt (T x): R}|hyperpage}
`R` **`log2`**`(T x)`<br>\newline
base-2 logarithm of x
`r since("2.0, vectorized in 2.13")`
<!-- R; log10; (T x); -->
\index{{\tt \bfseries log10 }!{\tt (T x): R}|hyperpage}
`R` **`log10`**`(T x)`<br>\newline
base-10 logarithm of x
`r since("2.0, vectorized in 2.13")`
<!-- real; pow; (real x, real y); -->
\index{{\tt \bfseries pow }!{\tt (real x, real y): real}|hyperpage}
`real` **`pow`**`(real x, real y)`<br>\newline
Return x raised to the power of y. \[ \text{pow}(x,y) = x^y \]
`r since("2.0")`
<!-- R; pow; (T1 x, T2 y); -->
\index{{\tt \bfseries pow }!{\tt (T1 x, T2 y): R}|hyperpage}
`R` **`pow`**`(T1 x, T2 y)`<br>\newline
Vectorized implementation of the `pow` function
`r since("2.25")`
<!-- R; inv; (T x); -->
\index{{\tt \bfseries inv }!{\tt (T x): R}|hyperpage}
`R` **`inv`**`(T x)`<br>\newline
inverse of x
`r since("2.0, vectorized in 2.13")`
<!-- R; inv_sqrt; (T x); -->
\index{{\tt \bfseries inv\_sqrt }!{\tt (T x): R}|hyperpage}
`R` **`inv_sqrt`**`(T x)`<br>\newline
inverse of the square root of x
`r since("2.0, vectorized in 2.13")`
<!-- R; inv_square; (T x); -->
\index{{\tt \bfseries inv\_square }!{\tt (T x): R}|hyperpage}
`R` **`inv_square`**`(T x)`<br>\newline
inverse of the square of x
`r since("2.0, vectorized in 2.13")`
## Trigonometric functions
<!-- real; hypot; (real x, real y); -->
\index{{\tt \bfseries hypot }!{\tt (real x, real y): real}|hyperpage}
`real` **`hypot`**`(real x, real y)`<br>\newline
Return the length of the hypotenuse of a right triangle with sides of
length x and y. \[ \text{hypot}(x,y) = \begin{cases} \sqrt{x^2+y^2} &
\text{if } x,y\geq 0 \\ \textrm{NaN} & \text{otherwise} \end{cases} \]
`r since("2.0")`
<!-- R; hypot; (T1 x, T2 y); -->
\index{{\tt \bfseries hypot }!{\tt (T1 x, T2 y): R}|hyperpage}
`R` **`hypot`**`(T1 x, T2 y)`<br>\newline
Vectorized implementation of the `hypot` function
`r since("2.25")`
<!-- R; cos; (T x); -->
\index{{\tt \bfseries cos }!{\tt (T x): R}|hyperpage}
`R` **`cos`**`(T x)`<br>\newline
cosine of the angle x (in radians)
`r since("2.0, vectorized in 2.13")`
<!-- R; sin; (T x); -->
\index{{\tt \bfseries sin }!{\tt (T x): R}|hyperpage}
`R` **`sin`**`(T x)`<br>\newline
sine of the angle x (in radians)
`r since("2.0, vectorized in 2.13")`
<!-- R; tan; (T x); -->
\index{{\tt \bfseries tan }!{\tt (T x): R}|hyperpage}
`R` **`tan`**`(T x)`<br>\newline
tangent of the angle x (in radians)
`r since("2.0, vectorized in 2.13")`
<!-- R; acos; (T x); -->
\index{{\tt \bfseries acos }!{\tt (T x): R}|hyperpage}
`R` **`acos`**`(T x)`<br>\newline
principal arc (inverse) cosine (in radians) of x
`r since("2.0, vectorized in 2.13")`
<!-- R; asin; (T x); -->
\index{{\tt \bfseries asin }!{\tt (T x): R}|hyperpage}
`R` **`asin`**`(T x)`<br>\newline
principal arc (inverse) sine (in radians) of x
`r since("2.0")`
<!-- R; atan; (T x); -->
\index{{\tt \bfseries atan }!{\tt (T x): R}|hyperpage}
`R` **`atan`**`(T x)`<br>\newline
principal arc (inverse) tangent (in radians) of x, with values from
$-\pi/2$ to $\pi/2$
`r since("2.0, vectorized in 2.13")`
<!-- real; atan2; (real y, real x); -->
\index{{\tt \bfseries atan2 }!{\tt (real y, real x): real}|hyperpage}
`real` **`atan2`**`(real y, real x)`<br>\newline
Return the principal arc (inverse) tangent (in radians) of y divided
by x, \[ \text{atan2}(y, x) = \arctan\left(\frac{y}{x}\right) \]
`r since("2.0, vectorized in 2.13")`
## Hyperbolic trigonometric functions
<!-- R; cosh; (T x); -->
\index{{\tt \bfseries cosh }!{\tt (T x): R}|hyperpage}
`R` **`cosh`**`(T x)`<br>\newline
hyperbolic cosine of x (in radians)
`r since("2.0, vectorized in 2.13")`
<!-- R; sinh; (T x); -->
\index{{\tt \bfseries sinh }!{\tt (T x): R}|hyperpage}
`R` **`sinh`**`(T x)`<br>\newline
hyperbolic sine of x (in radians)
`r since("2.0, vectorized in 2.13")`
<!-- R; tanh; (T x); -->
\index{{\tt \bfseries tanh }!{\tt (T x): R}|hyperpage}
`R` **`tanh`**`(T x)`<br>\newline
hyperbolic tangent of x (in radians)
`r since("2.0, vectorized in 2.13")`
<!-- R; acosh; (T x); -->
\index{{\tt \bfseries acosh }!{\tt (T x): R}|hyperpage}