I often need a "copy assignment" type of operation to an existing 
destination array of the exact same element type and size. Let's only talk 
about arrays of concrete types, like a multi-dimensional array of floats. 
This is useful when I write optimization solvers, and I need to store 
vectors or matrices from the previous step. I usually pre-allocate a pair 
of arrays of the same type and size, *x* and *x_next*, then do:

*x_next = x;*
 
at the end of each iteration of my solver.

At first, I thought using *copy()* (shallow copy) on them is fine to make 
sure they are separate entities, since floating point numbers and integers 
are concrete types in Julia. While I verified this is true (at least on 
arrays of Float64s), I looked at (around line 202 at the time of this 
post), and *copy()* seems to call *copy!( similar(a), a)*. To my 
understanding, this allocates a new destination array, fills it with the 
corresponding values from the source array, then assigns the pointer of 
this new destination array to *x_next*, and the garbage collector removes 
the old array that *x_next* was pointing to. This is a lot of work when I 
just want to traverse through *x_next*, and assign it the corresponding 
values from* x*. Please correct me if my understanding is wrong!

This is a really common operation. I'd appreciate it if someone can advise 
me whether there is already an existing method for doing this (or a better 
solution) before I write my own.

Cheers,

Roy

Reply via email to