Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "array[] int, int" signature to infix operators + and - #3104

Open
paul-buerkner opened this issue Sep 18, 2024 · 2 comments
Open

Add "array[] int, int" signature to infix operators + and - #3104

paul-buerkner opened this issue Sep 18, 2024 · 2 comments

Comments

@paul-buerkner
Copy link

Description

When I try to add an array of integers and a single integer, I get the error:

Ill-typed arguments supplied to infix operator +. Available signatures: 
...
Instead supplied arguments of incompatible type: array[] int, int.

Is there a specific reason we don't have an array[] int, int signature for + and -?

Example

The following Stan code snippet will results in an error, but would be a well defined (vectorized) opteration I believe.

data {
  int N;
  array[N] int J;
}
transformed data {
  array[N] int K = J + N;
}
@syclik
Copy link
Member

syclik commented Nov 9, 2024

Is there a specific reason we don't have an array[] int, int signature for + and -?

Strictly speaking, a Stan array is a container and we've gone with the definition that a Stan array doesn't have mathematical operations. (Adding two arrays doesn't work, adding an array and anything else doesn't work, etc.)

That said, this might not be the right interpretation of the language now.

@SteveBronder / @WardBrian: is this allowed in the language now? Any reason we wouldn't want to allow it?

Would we get into any trouble if we did allow it in the C++?

@WardBrian
Copy link
Member

There’s really no reason we couldn’t do it, but it would be almost exactly the same as this user defined function (as in, not any faster, just syntax for basically the same code under the hood)

array[] int add(array[] x, array[] y){
  int N = size(x);
  if(N != size(y))
    reject(“sizes mismatch”)
  array[N] result;
  for (i in 1:N)
    result[i] = x[i] + y[i];

  return result;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants