Skip to content

Commit

Permalink
remove some remaining uses of Stan's old array syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-buerkner committed Nov 13, 2023
1 parent b24dfa7 commit 3625d91
Show file tree
Hide file tree
Showing 21 changed files with 54 additions and 48 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Package: brms
Encoding: UTF-8
Type: Package
Title: Bayesian Regression Models using 'Stan'
Version: 2.20.5
Date: 2023-09-25
Version: 2.20.6
Date: 2023-11-13
Authors@R:
c(person("Paul-Christian", "Bürkner", email = "[email protected]",
role = c("aut", "cre")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* No longer automatically canonicalize the Stan code if cmdstanr is used
as backend. (#1544)

### Bug Fixes

* Remove some remaining uses of Stan's old array syntax.


# brms 2.20.3

Expand Down
2 changes: 1 addition & 1 deletion R/families.R
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ mixture <- function(..., flist = NULL, nmix = 1, order = NULL) {
#'
#' # define the corresponding Stan density function
#' stan_density_vec <- "
#' real beta_binomial2_lpmf(int[] y, vector mu, real phi, int[] N) {
#' real beta_binomial2_lpmf(array[] int y, vector mu, real phi, array[] int N) {
#' return beta_binomial_lpmf(y | N, mu * phi, (1 - mu) * phi);
#' }
#' "
Expand Down
2 changes: 1 addition & 1 deletion R/stan-response.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ stan_response <- function(bterms, data, normalize) {
str_add(out$data) <- glue(
" array[N{resp}, ncat{resp}] int Y{resp}; // response array\n"
)
str_add(out$pll_args) <- glue(", data int[,] Y{resp}")
str_add(out$pll_args) <- glue(", data array[,] int Y{resp}")
}
} else {
if (rtype == "real") {
Expand Down
2 changes: 1 addition & 1 deletion R/stanvars.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ stanvar <- function(x = NULL, name = NULL, scode = NULL,
if (length(x) == 1L) {
pll_type <- paste0(pll_type, "int")
} else {
pll_type <- paste0(pll_type, "int[]")
pll_type <- paste0(pll_type, "array[] int")
}
} else if (is.vector(x)) {
if (length(x) == 1L) {
Expand Down
2 changes: 1 addition & 1 deletion inst/chunks/fun_is_equal.stan
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// are two 1D integer arrays equal?
int is_equal(int[] a, int[] b) {
int is_equal(array[] int a, array[] int b) {
int n_a = size(a);
int n_b = size(b);
if (n_a != n_b) {
Expand Down
2 changes: 1 addition & 1 deletion inst/chunks/fun_multinomial_logit.stan
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
* Returns:
* a scalar to be added to the log posterior
*/
real multinomial_logit2_lpmf(int[] y, vector mu) {
real multinomial_logit2_lpmf(array[] int y, vector mu) {
return multinomial_lpmf(y | softmax(mu));
}
10 changes: 6 additions & 4 deletions inst/chunks/fun_normal_time.stan
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* sum of the log-PDF values of all observations
*/
real normal_time_hom_lpdf(vector y, vector mu, real sigma, matrix chol_cor,
int[] nobs, int[] begin, int[] end) {
array[] int nobs, array[] int begin, array[] int end) {
int I = size(nobs);
vector[I] lp;
matrix[rows(chol_cor), cols(chol_cor)] L = sigma * chol_cor;
Expand All @@ -32,7 +32,7 @@
* sum of the log-PDF values of all observations
*/
real normal_time_het_lpdf(vector y, vector mu, vector sigma, matrix chol_cor,
int[] nobs, int[] begin, int[] end) {
array[] int nobs, array[] int begin, array[] int end) {
int I = size(nobs);
vector[I] lp;
for (i in 1:I) {
Expand All @@ -53,7 +53,8 @@
* sum of the log-PDF values of all observations
*/
real normal_time_hom_flex_lpdf(vector y, vector mu, real sigma, matrix chol_cor,
int[] nobs, int[] begin, int[] end, int[,] Jtime) {
array[] int nobs, array[] int begin, array[] int end,
array[,] int Jtime) {
real lp = 0.0;
int I = size(nobs);
array[I] int has_lp = rep_array(0, I);
Expand Down Expand Up @@ -103,7 +104,8 @@
* sum of the log-PDF values of all observations
*/
real normal_time_het_flex_lpdf(vector y, vector mu, vector sigma, matrix chol_cor,
int[] nobs, int[] begin, int[] end, int[,] Jtime) {
array[] int nobs, array[] int begin, array[] int end,
array[,] int Jtime) {
int I = size(nobs);
vector[I] lp;
array[I] int has_lp = rep_array(0, I);
Expand Down
12 changes: 6 additions & 6 deletions inst/chunks/fun_normal_time_se.stan
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* sum of the log-PDF values of all observations
*/
real normal_time_hom_se_lpdf(vector y, vector mu, real sigma, data vector se2,
matrix chol_cor, int[] nobs, int[] begin, int[] end) {
matrix chol_cor, array[] int nobs, array[] int begin, array[] int end) {
int I = size(nobs);
vector[I] lp;
matrix[rows(chol_cor), cols(chol_cor)] Cov;
Expand All @@ -36,7 +36,7 @@
* sum of the log-PDF values of all observations
*/
real normal_time_het_se_lpdf(vector y, vector mu, vector sigma, data vector se2,
matrix chol_cor, int[] nobs, int[] begin, int[] end) {
matrix chol_cor, array[] int nobs, array[] int begin, array[] int end) {
int I = size(nobs);
vector[I] lp;
for (i in 1:I) {
Expand All @@ -59,8 +59,8 @@
* sum of the log-PDF values of all observations
*/
real normal_time_hom_se_flex_lpdf(vector y, vector mu, real sigma, data vector se2,
matrix chol_cor, int[] nobs, int[] begin,
int[] end, int[,] Jtime) {
matrix chol_cor, array[] int nobs, array[] int begin,
array[] int end, array[,] int Jtime) {
int I = size(nobs);
vector[I] lp;
matrix[rows(chol_cor), cols(chol_cor)] Cov;
Expand All @@ -84,8 +84,8 @@
* sum of the log-PDF values of all observations
*/
real normal_time_het_se_flex_lpdf(vector y, vector mu, vector sigma, data vector se2,
matrix chol_cor, int[] nobs, int[] begin,
int[] end, int[,] Jtime) {
matrix chol_cor, array[] int nobs, array[] int begin,
array[] int end, array[,] int Jtime) {
int I = size(nobs);
vector[I] lp;
matrix[rows(chol_cor), cols(chol_cor)] Cor;
Expand Down
2 changes: 1 addition & 1 deletion inst/chunks/fun_scale_r_cor_by.stan
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Returns:
* matrix of scaled group-level effects
*/
matrix scale_r_cor_by(matrix z, matrix SD, matrix[] L, int[] Jby) {
matrix scale_r_cor_by(matrix z, matrix SD, array[] matrix L, array[] int Jby) {
// r is stored in another dimension order than z
matrix[cols(z), rows(z)] r;
array[size(L)] matrix[rows(L[1]), cols(L[1])] LC;
Expand Down
4 changes: 2 additions & 2 deletions inst/chunks/fun_scale_r_cor_by_cov.stan
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* Returns:
* matrix of scaled group-level effects
*/
matrix scale_r_cor_by_cov(matrix z, matrix SD, matrix[] L,
int[] Jby, matrix Lcov) {
matrix scale_r_cor_by_cov(matrix z, matrix SD, array[] matrix L,
array[] int Jby, matrix Lcov) {
vector[num_elements(z)] z_flat = to_vector(z);
vector[num_elements(z)] r = rep_vector(0, num_elements(z));
array[size(L)] matrix[rows(L[1]), cols(L[1])] LC;
Expand Down
4 changes: 2 additions & 2 deletions inst/chunks/fun_scale_time_err.stan
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* vector of scaled and correlated residuals
*/
vector scale_time_err(vector zerr, real sderr, matrix chol_cor,
int[] nobs, int[] begin, int[] end) {
array[] int nobs, array[] int begin, array[] int end) {
vector[rows(zerr)] err;
for (i in 1:size(nobs)) {
matrix[nobs[i], nobs[i]] L_i;
Expand All @@ -28,7 +28,7 @@
* vector of scaled and correlated residuals
*/
vector scale_time_err_flex(vector zerr, real sderr, matrix chol_cor,
int[] nobs, int[] begin, int[] end, int[,] Jtime) {
array[] int nobs, array[] int begin, array[] int end, array[,] int Jtime) {
vector[rows(zerr)] err;
int I = size(nobs);
array[I] int has_err = rep_array(0, I);
Expand Down
2 changes: 1 addition & 1 deletion inst/chunks/fun_sequence.stan
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Returns:
* an integer sequence from start to end
*/
int[] sequence(int start, int end) {
array[] int sequence(int start, int end) {
array[end - start + 1] int seq;
for (n in 1:num_elements(seq)) {
seq[n] = n + start - 1;
Expand Down
2 changes: 1 addition & 1 deletion inst/chunks/fun_sparse_car_lpdf.stan
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
real sparse_car_lpdf(vector phi, real car, real sdcar, int Nloc,
int Nedges, data vector Nneigh, data vector eigenW,
int[] edges1, int[] edges2) {
array[] int edges1, array[] int edges2) {
real tau; // precision parameter
row_vector[Nloc] phit_D; // phi' * D
row_vector[Nloc] phit_W; // phi' * W
Expand Down
2 changes: 1 addition & 1 deletion inst/chunks/fun_sparse_icar_lpdf.stan
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
real sparse_icar_lpdf(vector phi, real sdcar, int Nloc,
int Nedges, data vector Nneigh, data vector eigenW,
int[] edges1, int[] edges2) {
array[] int edges1, array[] int edges2) {
real tau; // precision parameter
row_vector[Nloc] phit_D; // phi' * D
row_vector[Nloc] phit_W; // phi' * W
Expand Down
4 changes: 2 additions & 2 deletions inst/chunks/fun_stack_vectors.stan
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* grouped data stored linearly in "data" as indexed by begin and end
* is repacked to be stacked into an array of vectors.
*/
vector[] stack_vectors(vector long_data, int n, int[] stack,
int[] begin, int[] end) {
vector[] stack_vectors(vector long_data, int n, array[] int stack,
array[] int begin, array[] int end) {
int S = sum(stack);
int G = size(stack);
array[S] vector[n] stacked;
Expand Down
16 changes: 8 additions & 8 deletions inst/chunks/fun_student_t_time.stan
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* sum of the log-PDF values of all observations
*/
real student_t_time_hom_lpdf(vector y, real nu, vector mu, real sigma,
matrix chol_cor, int[] nobs, int[] begin,
int[] end) {
matrix chol_cor, array[] int nobs, array[] int begin,
array[] int end) {
int I = size(nobs);
vector[I] lp;
for (i in 1:I) {
Expand All @@ -35,8 +35,8 @@
* sum of the log-PDF values of all observations
*/
real student_t_time_het_lpdf(vector y, real nu, vector mu, vector sigma,
matrix chol_cor, int[] nobs, int[] begin,
int[] end) {
matrix chol_cor, array[] int nobs, array[] int begin,
array[] int end) {
int I = size(nobs);
vector[I] lp;
for (i in 1:I) {
Expand All @@ -58,8 +58,8 @@
* sum of the log-PDF values of all observations
*/
real student_t_time_hom_flex_lpdf(vector y, real nu, vector mu, real sigma,
matrix chol_cor, int[] nobs, int[] begin,
int[] end, int[,] Jtime) {
matrix chol_cor, array[] int nobs, array[] int begin,
array[] int end, array[,] int Jtime) {
int I = size(nobs);
vector[I] lp;
matrix[rows(chol_cor), cols(chol_cor)] Cov;
Expand All @@ -83,8 +83,8 @@
* sum of the log-PDF values of all observations
*/
real student_t_time_het_flex_lpdf(vector y, real nu, vector mu, vector sigma,
matrix chol_cor, int[] nobs, int[] begin,
int[] end, int[,] Jtime) {
matrix chol_cor, array[] int nobs, array[] int begin,
array[] int end, array[,] int Jtime) {
int I = size(nobs);
vector[I] lp;
matrix[rows(chol_cor), cols(chol_cor)] Cor;
Expand Down
16 changes: 8 additions & 8 deletions inst/chunks/fun_student_t_time_se.stan
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* sum of the log-PDF values of all observations
*/
real student_t_time_hom_se_lpdf(vector y, real nu, vector mu, real sigma,
data vector se2, matrix chol_cor, int[] nobs,
int[] begin, int[] end) {
data vector se2, matrix chol_cor, array[] int nobs,
array[] int begin, array[] int end) {
int I = size(nobs);
vector[I] lp;
matrix[rows(chol_cor), cols(chol_cor)] Cov;
Expand All @@ -38,8 +38,8 @@
* sum of the log-PDF values of all observations
*/
real student_t_time_het_se_lpdf(vector y, real nu, vector mu, vector sigma,
data vector se2, matrix chol_cor, int[] nobs,
int[] begin, int[] end) {
data vector se2, matrix chol_cor, array[] int nobs,
array[] int begin, array[] int end) {
int I = size(nobs);
vector[I] lp;
for (i in 1:I) {
Expand All @@ -61,8 +61,8 @@
* sum of the log-PDF values of all observations
*/
real student_t_time_hom_se_flex_lpdf(vector y, real nu, vector mu, real sigma,
data vector se2, matrix chol_cor, int[] nobs,
int[] begin, int[] end, int[,] Jtime) {
data vector se2, matrix chol_cor, array[] int nobs,
array[] int begin, array[] int end, array[,] int Jtime) {
int I = size(nobs);
vector[I] lp;
matrix[rows(chol_cor), cols(chol_cor)] Cov;
Expand All @@ -86,8 +86,8 @@
* sum of the log-PDF values of all observations
*/
real student_t_time_het_se_flex_lpdf(vector y, real nu, vector mu, vector sigma,
data vector se2, matrix chol_cor, int[] nobs,
int[] begin, int[] end, int[,] Jtime) {
data vector se2, matrix chol_cor, array[] int nobs,
array[] int begin, array[] int end, array[,] int Jtime) {
int I = size(nobs);
vector[I] lp;
matrix[rows(chol_cor), cols(chol_cor)] Cor;
Expand Down
6 changes: 3 additions & 3 deletions inst/chunks/fun_which_range.stan
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Returns:
* a scalar integer
*/
int size_range(int[] x, int start, int end) {
int size_range(array[] int x, int start, int end) {
int out = 0;
for (i in 1:size(x)) {
out += (x[i] >= start && x[i] <= end);
Expand All @@ -21,7 +21,7 @@
* Returns:
* an integer array
*/
int[] which_range(int[] x, int start, int end) {
array[] int which_range(array[] int x, int start, int end) {
array[size_range(x, start, end)] int out;
int j = 1;
for (i in 1:size(x)) {
Expand All @@ -39,7 +39,7 @@
* Returns:
* an integer array
*/
int[] start_at_one(int[] x, int start) {
array[] int start_at_one(array[] int x, int start) {
array[size(x)] int out;
for (i in 1:size(x)) {
out[i] = x[i] - start + 1;
Expand Down
2 changes: 1 addition & 1 deletion man/custom_family.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/local/tests.models-2.R
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ test_that("Nested non-linear models work correctly", {
test_that("Non-linear non-looped model predictions work correctly in blocked order", {
loss_alt <- transform(loss, row=as.integer(1:nrow(loss)), nr=nrow(loss), test=as.integer(0))
scode_growth <- "
vector growth_test(vector ult, int[] dev, vector theta, vector omega, int[] row, int[] test) {
vector growth_test(vector ult, array[] int dev, vector theta, vector omega, array[] int row, array[] int test) {
int N = rows(ult);
vector[N] mu;
int rows_sorted = 1;
Expand Down

0 comments on commit 3625d91

Please sign in to comment.