Problem with the type of an argument in a function

Dear all,

I am trying to build a function, my attempt is in the code below

vector neighbors_stan( int Minc[M, N], int M, int N) {
    //int n = dims(Minc)[1]; // Number of nodes
    int max_neighbors = (M * (M - 1)) / 2; // Maximum number of possible neighbors
    vector[max_neighbors] neighbor_indices;
    int index = 1; // Index to keep track of position in the neighbor_indices vector
    
    // Iterate over the upper triangular part of the matrix
    for (i in 2:M) {
      for (j in 1:(i - 1)) {
        if (Minc[i, j] == 1) {
          neighbor_indices[index] = j; // Store the neighbor
          index += 1;
        }
      }
    }

I am getting the following error:

 3:    vector neighbors_stan(int Minc[M,N], int M, int N) {
                                         ^
     4:      int max_neighbors = (M * (M - 1)) / 2; // Maximum number of possible neighbors
     5:      vector[max_neighbors] neighbors; // Initialize a vector to store neighbors
   -------------------------------------------------

"," or ")" expected after function argument declaration.

As this function, I have another ones depending on the matrix Minc. I tried to use the matrix type instead of of the int array and it worked, the issue is that at some point, I need to get an index in function of this matrix, to declare another vector, something like this.,

int  N_edges;  // total numbere of edges
int nei[N_edges]; //vector stacking up the direected neighbors of each node

N_edges  = sum(adjmatinf,N,N); 

if I use the matrix type, the result of the sum above for N_edges will be real, that cannot be use as an index. Since Stan does not allow coversion from real to integer, Do you have any suggestion for me to solve this issue?

Thanks in advance,

Alejandro.

Function arguments don’t take the array size, just empty brackets. The correct declaration is either

vector neighbors_stan( int[ , ] Minc, int M, int N) {

or

vector neighbors_stan( array[ , ] int Minc, int M, int N) {

depending on which version of Stan you are using.

1 Like