This kind of routine is what I'm talking about...
# copy assignment for vectors
function copyassignment!(a::Vector,b::Vector)
@assert length(a) == length(b)
for n=1:length(a)
a[n]=b[n];
end
end
My questions:
1) Is there a standard function that does this?
2) Is there a better way to do this so it'll handle any type of
multi-dimensional array of integers and floats without performance penalty?
On Thursday, 2 October 2014 18:09:16 UTC-4, Roy Wang wrote:
>
> 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
>