Array of reals vs Matrix

97 views
Skip to first unread message

Juan

unread,
Nov 7, 2016, 8:03:17 AM11/7/16
to Stan users mailing list
Hello.

Is it better to use a bidimensional array of reals or a matrix?
(Speed, memory, safety or any other interesting related issue).

And another question, why is not possible to assign an array to a matrix or viceversa?

Daniel Lee

unread,
Nov 7, 2016, 9:08:37 AM11/7/16
to stan-users mailing list
Hi Juan,

On Mon, Nov 7, 2016 at 8:03 AM, Juan <juan...@gmail.com> wrote:
Hello.

Is it better to use a bidimensional array of reals or a matrix?
(Speed, memory, safety or any other interesting related issue).

Speed: it'll depend on your case. Often an array of vectors could be the fastest thing to do. The deep reason is that it has to do with memory locality.
Memory: matrix. There's just slightly less overhead using the matrix variable type over arrays of arrays. But it shouldn't be much.
Saftey: both are equally safe

The real key to the difference is the usability. When you need to do linear algebra, use matrix, vector, row_vector. These have shapes and sizes and operations are well defined. Treat arrays as collections. What does it mean to multiply a bidirectional array with another? That doesn't really have meaning. (And if you can come up with some sort of "natural" thing, I'm sure someone thinks something else is "natural" for that same operation.)
 

And another question, why is not possible to assign an array to a matrix or viceversa?

That's easy: an array is not a matrix and a matrix is not an array.

There should be enough conversion functions in the library for this to be easy. See to_array(), etc. If there's something obvious that's missing, please let us know.

 

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Lawrence

unread,
Nov 7, 2016, 11:19:29 AM11/7/16
to stan-...@googlegroups.com
On Mon, Nov 7, 2016 at 9:03 AM, Juan <juan...@gmail.com> wrote:
Is it better to use a bidimensional array of reals or a matrix?
(Speed, memory, safety or any other interesting related issue).

See section 4.3 from the manual, which speaks to exactly this question
 

Bob Carpenter

unread,
Nov 7, 2016, 11:26:16 AM11/7/16
to stan-...@googlegroups.com
There's a chapter in the manual explaining all the
differences.

- Bob
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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.

Bob Carpenter

unread,
Nov 7, 2016, 11:26:32 AM11/7/16
to stan-...@googlegroups.com
Sorry---didn't see Mike's post! What he said.

- Bob

Juan

unread,
Nov 7, 2016, 3:20:16 PM11/7/16
to Stan users mailing list


On Monday, November 7, 2016 at 3:08:37 PM UTC+1, Daniel Lee wrote:
Hi Juan,

On Mon, Nov 7, 2016 at 8:03 AM, Juan <juan...@gmail.com> wrote:
Hello.

Is it better to use a bidimensional array of reals or a matrix?
(Speed, memory, safety or any other interesting related issue).

Speed: it'll depend on your case. Often an array of vectors could be the fastest thing to do. The deep reason is that it has to do with memory locality.
Memory: matrix. There's just slightly less overhead using the matrix variable type over arrays of arrays. But it shouldn't be much.
Saftey: both are equally safe

The real key to the difference is the usability. When you need to do linear algebra, use matrix, vector, row_vector. These have shapes and sizes and operations are well defined. Treat arrays as collections. What does it mean to multiply a bidirectional array with another? That doesn't really have meaning. (And if you can come up with some sort of "natural" thing, I'm sure someone thinks something else is "natural" for that same operation.)
 

And another question, why is not possible to assign an array to a matrix or viceversa?

That's easy: an array is not a matrix and a matrix is not an array.

There should be enough conversion functions in the library for this to be easy. See to_array(), etc. If there's something obvious that's missing, please let us know. 


OK, Thanks. 
In the manual says generic things but none of the things mentioned by Daniel Lee.

Bob Carpenter

unread,
Nov 7, 2016, 10:29:59 PM11/7/16
to stan-...@googlegroups.com
A lot of it's in there, but can be translated better
for users rather than referencing what the generated
C++ looks like.

The safety's sort of assumed for everything, but
I should mention that with the indexing somewhere. If
you index out of range, it throws an exception in the
C++ and reports back through Stan.

- 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.
Reply all
Reply to author
Forward
0 new messages