"real a[2]" vs "vector[2] a" ?

1,500 views
Skip to first unread message

Mike Lawrence

unread,
Jun 26, 2013, 7:56:23 AM6/26/13
to stan-...@googlegroups.com
Hopefully a quickie: what's the difference between defining a variable as
    real a[2];
versus
    vector[2] a;
?

Both seem to yield an object that can contain indexable list of values, but I'd like to make sure I'm using the right version for my purposes if there is a difference.

Mike

--
Mike Lawrence
Graduate Student
Department of Psychology & Neuroscience
Dalhousie University

~ Certainty is (possibly) folly ~

Dieter Menne

unread,
Jun 26, 2013, 8:53:30 AM6/26/13
to stan-...@googlegroups.com

Hopefully a quickie: what's the difference between defining a variable as
    real a[2];
versus
    vector[2] a;
?


Ben Goodrich in
https://groups.google.com/forum/?fromgroups#!searchin/stan-users/menne/stan-users/2Kwi0uXxJGE/8jcScpmdJBwJ

"For the most part, a vector is preferable to a one-dimensional array of reals in the sense that operations like beta * x are defined. However, I think there are some operations that work elementwise on an array but don't work for a vector. The array is necessary for integer data and sometimes a multidimensional array is useful. "

I read this as "try vector first to check until function overloading fails, then resort to real[]." Yet, most examples use real[], so I am not really sure.

Dieter

Bob Carpenter

unread,
Jun 26, 2013, 12:28:18 PM6/26/13
to stan-...@googlegroups.com
They have slightly different ops defined on them, but
we'll be unifying those in the future.

In general, you should prefer vector unless there's an
operation that only works on arrays. The reason is that
the vector/matrix/linear-algebra ops are only defined on
vectors and all the multivariate distributions are defined
in terms of vector/matrix parameters.

Also note that arrays of row vectors behave very similarly
to matrices:

matrix[M,N] x;

vs.

row_vector[N] x[M];

The latter array-based version has faster row access if you
use x[m] (which is row m of the matrix x).

- Bob
> --
> You received this message because you are subscribed to the Google Groups "stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Bob Carpenter

unread,
Jun 26, 2013, 12:30:38 PM6/26/13
to stan-...@googlegroups.com
Most of the examples used real[] because we wanted
the models to look familiar to BUGS users. We'll probably
change them to use vectors in the future.

- Bob
Reply all
Reply to author
Forward
0 new messages