@@ -15,6 +15,30 @@ functions {
15
15
array[] vector rtn_vec_array(array[] vector x) { return x; }
16
16
array[] row_vector rtn_rowvec_array(array[] row_vector x) { return x; }
17
17
array[] matrix rtn_matrix_array(array[] matrix x) { return x; }
18
+
19
+ tuple(int, int) rtn_tuple_int(tuple(int, int) x) { return x; }
20
+ tuple(real, real) rtn_tuple_real(tuple(real, real) x) { return x; }
21
+ tuple(vector, vector) rtn_tuple_vec(tuple(vector, vector) x) { return x; }
22
+ tuple(row_vector, row_vector) rtn_tuple_rowvec(tuple(row_vector, row_vector) x) { return x; }
23
+ tuple(matrix, matrix) rtn_tuple_matrix(tuple(matrix, matrix) x) { return x; }
24
+
25
+ tuple(array[] int, array[] int) rtn_tuple_int_array(tuple(array[] int, array[] int) x) { return x; }
26
+ tuple(array[] real, array[] real) rtn_tuple_real_array(tuple(array[] real, array[] real) x) { return x; }
27
+ tuple(array[] vector, array[] vector) rtn_tuple_vec_array(tuple(array[] vector, array[] vector) x) { return x; }
28
+ tuple(array[] row_vector, array[] row_vector) rtn_tuple_rowvec_array(tuple(array[] row_vector, array[] row_vector) x) { return x; }
29
+ tuple(array[] matrix, array[] matrix) rtn_tuple_matrix_array(tuple(array[] matrix, array[] matrix) x) { return x; }
30
+
31
+ tuple(int, tuple(int, int)) rtn_nest_tuple_int(tuple(int, tuple(int, int)) x) { return x; }
32
+ tuple(int, tuple(real, real)) rtn_nest_tuple_real(tuple(int, tuple(real, real)) x) { return x; }
33
+ tuple(int, tuple(vector, vector)) rtn_nest_tuple_vec(tuple(int, tuple(vector, vector)) x) { return x; }
34
+ tuple(int, tuple(row_vector, row_vector)) rtn_nest_tuple_rowvec(tuple(int, tuple(row_vector, row_vector)) x) { return x; }
35
+ tuple(int, tuple(matrix, matrix)) rtn_nest_tuple_matrix(tuple(int, tuple(matrix, matrix)) x) { return x; }
36
+
37
+ tuple(int, tuple(array[] int, array[] int)) rtn_nest_tuple_int_array(tuple(int, tuple(array[] int, array[] int)) x) { return x; }
38
+ tuple(int, tuple(array[] real, array[] real)) rtn_nest_tuple_real_array(tuple(int, tuple(array[] real, array[] real)) x) { return x; }
39
+ tuple(int, tuple(array[] vector, array[] vector)) rtn_nest_tuple_vec_array(tuple(int, tuple(array[] vector, array[] vector)) x) { return x; }
40
+ tuple(int, tuple(array[] row_vector, array[] row_vector)) rtn_nest_tuple_rowvec_array(tuple(int, tuple(array[] row_vector, array[] row_vector)) x) { return x; }
41
+ tuple(int, tuple(array[] matrix, array[] matrix)) rtn_nest_tuple_matrix_array(tuple(int, tuple(array[] matrix, array[] matrix)) x) { return x; }
18
42
}"
19
43
stan_prog <- paste(function_decl ,
20
44
paste(readLines(testing_stan_file(" bernoulli" )),
@@ -35,9 +59,13 @@ test_that("Functions can be exposed in model object", {
35
59
test_that(" Functions handle types correctly" , {
36
60
skip_if(os_is_wsl())
37
61
62
+ # ## Scalar
63
+
38
64
expect_equal(mod $ functions $ rtn_int(10 ), 10 )
39
65
expect_equal(mod $ functions $ rtn_real(1.67 ), 1.67 )
40
66
67
+ # ## Container
68
+
41
69
vec <- c(1.2 ,234 ,0.3 ,- 0.4 )
42
70
rowvec <- t(vec )
43
71
matrix <- matrix (c(2.11 , - 6.35 , 4.87 , - 0.9871 ), nrow = 2 , ncol = 2 )
@@ -48,13 +76,75 @@ test_that("Functions handle types correctly", {
48
76
expect_equal(mod $ functions $ rtn_int_array(1 : 5 ), 1 : 5 )
49
77
expect_equal(mod $ functions $ rtn_real_array(vec ), vec )
50
78
79
+ # ## Array of Container
80
+
51
81
vec_array <- list (vec , vec * 2 , vec + 0.1 )
52
82
rowvec_array <- list (rowvec , rowvec * 2 , rowvec + 0.1 )
53
83
matrix_array <- list (matrix , matrix * 2 , matrix + 0.1 )
54
84
55
85
expect_equal(mod $ functions $ rtn_vec_array(vec_array ), vec_array )
56
86
expect_equal(mod $ functions $ rtn_rowvec_array(rowvec_array ), rowvec_array )
57
87
expect_equal(mod $ functions $ rtn_matrix_array(matrix_array ), matrix_array )
88
+
89
+ # ## Tuple of Scalar
90
+
91
+ tuple_int <- list (10 , 35 )
92
+ tuple_dbl <- list (31.87 , - 19.09 )
93
+ expect_equal(mod $ functions $ rtn_tuple_int(tuple_int ), tuple_int )
94
+ expect_equal(mod $ functions $ rtn_tuple_real(tuple_dbl ), tuple_dbl )
95
+
96
+ # ## Tuple of Container
97
+
98
+ tuple_vec <- list (vec , vec * 12 )
99
+ tuple_rowvec <- list (rowvec , rowvec * 0.5 )
100
+ tuple_matrix <- list (matrix , matrix * 0.23 )
101
+ tuple_int_array <- list (1 : 10 , - 3 : 2 )
102
+
103
+ expect_equal(mod $ functions $ rtn_tuple_vec(tuple_vec ), tuple_vec )
104
+ expect_equal(mod $ functions $ rtn_tuple_rowvec(tuple_rowvec ), tuple_rowvec )
105
+ expect_equal(mod $ functions $ rtn_tuple_matrix(tuple_matrix ), tuple_matrix )
106
+ expect_equal(mod $ functions $ rtn_tuple_int_array(tuple_int_array ), tuple_int_array )
107
+ expect_equal(mod $ functions $ rtn_tuple_real_array(tuple_vec ), tuple_vec )
108
+
109
+ # ## Tuple of Container Arrays
110
+
111
+ tuple_vec_array <- list (vec_array , vec_array )
112
+ tuple_rowvec_array <- list (rowvec_array , rowvec_array )
113
+ tuple_matrix_array <- list (matrix_array , matrix_array )
114
+
115
+ expect_equal(mod $ functions $ rtn_tuple_vec_array(tuple_vec_array ), tuple_vec_array )
116
+ expect_equal(mod $ functions $ rtn_tuple_rowvec_array(tuple_rowvec_array ), tuple_rowvec_array )
117
+ expect_equal(mod $ functions $ rtn_tuple_matrix_array(tuple_matrix_array ), tuple_matrix_array )
118
+
119
+ # ## Nested Tuple of Scalar
120
+
121
+ nest_tuple_int <- list (10 , tuple_int )
122
+ nest_tuple_dbl <- list (31 , tuple_dbl )
123
+ expect_equal(mod $ functions $ rtn_nest_tuple_int(nest_tuple_int ), nest_tuple_int )
124
+ expect_equal(mod $ functions $ rtn_nest_tuple_real(nest_tuple_dbl ), nest_tuple_dbl )
125
+
126
+ # ## Nested Tuple of Container
127
+
128
+ nest_tuple_vec <- list (12 , tuple_vec )
129
+ nest_tuple_rowvec <- list (2 , tuple_rowvec )
130
+ nest_tuple_matrix <- list (- 23 , tuple_matrix )
131
+ nest_tuple_int_array <- list (21 , tuple_int_array )
132
+
133
+ expect_equal(mod $ functions $ rtn_nest_tuple_vec(nest_tuple_vec ), nest_tuple_vec )
134
+ expect_equal(mod $ functions $ rtn_nest_tuple_rowvec(nest_tuple_rowvec ), nest_tuple_rowvec )
135
+ expect_equal(mod $ functions $ rtn_nest_tuple_matrix(nest_tuple_matrix ), nest_tuple_matrix )
136
+ expect_equal(mod $ functions $ rtn_nest_tuple_int_array(nest_tuple_int_array ), nest_tuple_int_array )
137
+ expect_equal(mod $ functions $ rtn_nest_tuple_real_array(nest_tuple_vec ), nest_tuple_vec )
138
+
139
+ # ## Nested Tuple of Container Arrays
140
+
141
+ nest_tuple_vec_array <- list (- 21 , tuple_vec_array )
142
+ nest_tuple_rowvec_array <- list (1000 , tuple_rowvec_array )
143
+ nest_tuple_matrix_array <- list (0 , tuple_matrix_array )
144
+
145
+ expect_equal(mod $ functions $ rtn_nest_tuple_vec_array(nest_tuple_vec_array ), nest_tuple_vec_array )
146
+ expect_equal(mod $ functions $ rtn_nest_tuple_rowvec_array(nest_tuple_rowvec_array ), nest_tuple_rowvec_array )
147
+ expect_equal(mod $ functions $ rtn_nest_tuple_matrix_array(nest_tuple_matrix_array ), nest_tuple_matrix_array )
58
148
})
59
149
60
150
test_that(" Functions can be exposed in fit object" , {
0 commit comments