Possibly unexpected behavior of dmatrix

4 views
Skip to first unread message

Cole Monnahan - NOAA Federal

unread,
May 13, 2020, 2:52:43 PM5/13/20
to devel...@admb-project.org
Hi all,

I stumbled across something recently that I wanted to mention. It relates to the copy behavior of dmatrix objects.

If you declare a matrix explicitly and then set it equal, it will copy the values of the original object (deep copy). Like this:

dmatrix mat1(1,2,1,2);
mat1=mat0;  

Instead if you declare and assign in one step, it creates a shallow copy so that if the original object changes so does the new one.

dmatrix mat2=mat0; 

I naively assumed these would be the same and was perplexed by the behavior until I had some help to track it down (HT Matt Supernaw). The attached model demonstrates the behavior. 

I wonder if it is worth putting this in the documentation somewhere? I doubt many users would expect this. But at the same time it's not a common thing to do when building a model, so maybe it is not that important. I didn't check with other data types either.

I just wanted to flag this for consideration and get thoughts.

Thanks,
Cole
dmatrix_demo.tpl

Johnoel

unread,
May 13, 2020, 2:56:10 PM5/13/20
to Cole Monnahan - NOAA Federal, devel...@admb-project.org
That is expected in ADMB.  If the type was not allocated, it will just point to the other instance. 

--
To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@admb-project.org.

Richard Methot - NOAA Federal

unread,
May 13, 2020, 3:24:16 PM5/13/20
to Johnoel, Cole Monnahan - NOAA Federal, devel...@admb-project.org
I've used this shallow copy to more easily refer to a subset of a high dimension array.
ALK is a 4D array, so this creates a matrix, ALK_w,  that is a shallow copy of just the last 2 dimensions.

    dvar_matrix ALK_w=ALK(ALK_idx,g);        //  shallow copy

Richard D. Methot Jr. Ph.D.
NOAA Fisheries Senior Scientist for Stock Assessments
Mobile: 301-787-0241

Reply all
Reply to author
Forward
0 new messages