Re-using local variables declared in Models section

26 views
Skip to first unread message

terrance savitsky

unread,
Sep 26, 2012, 9:36:15 PM9/26/12
to stan-...@googlegroups.com
A quick question,  I employ a locally-declared (unrestricted) matrix in the Models section that holds an inverse of a covariance matrix.   i declare the local matrix, once, and over-write it repeatedly in a sequential fashion as I successively augment lp__ .   I wanted to confirm that "declare once, use many" is okay for local variables in the Models section.   I encountered an example in response to a post that repeatedly re-declared the local variable before each re-use occasion, prompting this question.

--
Thank you, Terrance Savitsky

Ben Goodrich

unread,
Sep 26, 2012, 9:47:54 PM9/26/12
to stan-...@googlegroups.com
Confirmed. Although this behavior is as would be expected in C++ (and most languages), it contrasts with the behavior in BUGS where reassignment messes with the graphical representation of the model.

Bob Carpenter

unread,
Sep 27, 2012, 12:53:14 AM9/27/12
to stan-...@googlegroups.com
It's not really a redeclaration per se.

It's usually good style to put declarations near
where they're used because it makes the program
easier to understand (less scanning). An exception to
this rule is for compound structures such as arrays
or matrices, but only when they are filled elementwise.
For this case, preallocating outside of a block where
they're repeatedly used can be more efficient.

I added a style guide appendix and I'll put a
note in there about this case with some examples.

- Bob


Reply all
Reply to author
Forward
0 new messages