This is a question regarding the use of “=” signs in Julia.
I can do the following for-loop in MATLAB
for ii = 1 : dt
ηp1 = shallowwave1D(ηn,ηm1,r);
ηm1 = ηn; ηn = ηp1;
end
Can the same be done in Julia? Or will there be some issues with copies where whatever I do to ηn changes ηm1 (see the second statement in the second line of the for-loop)?
Just asking, because this has tripped me up multiple times in Python where I needed to do copying to resolve the issue.
Do I need to do
for ii = 1 : dt
ηp1 = shallowwave1D(ηn,ηm1,r);
ηm1 = deepcopy(ηn); ηn = deepcopy(ηp1);
end
ηn = ηp1 does not mutateηn, but re-binds it to another array. So, ηm1 is not affected.
It would only be affected if you modified ηn in-place, e.g. with copyto!(ηn, ηp1).
In addition to @Vasily_Pisarev’s answer (with which I fully agree), I would like to add that I think a common idiom to perform what (I think) you want to do would be something like:
# Initialize input arrays
ηm1 = something
ηn = something
# Preallocate an array for the result
ηp1 = similar(ηn)
# No need to have a named iteration variable if you don't use it:
# naming the variable "_" makes it clear that it is not used
for _ = 1 : dt
# shallowwave1D is rewritten in such a way that it mutates ηp1
# instead of creating a new array (hence the "!" suffix)
shallowwave1D!(ηp1,ηn,ηm1,r)
# simply rotate the variables, leaving the now-unused ηm1 array
# free to be modified as ηp1 in the next iteration
(ηm1, ηn, ηp1) = (ηn, ηp1, ηm1)
end
As already explained, this way your underlying arrays are only re-bound to other variables, but remain distinct (i.e. at any point in time, there are 3 arrays, independent from one another in the sense that modifying a coefficient in one of the array will not affect other arrays)
Mmmmmm interesting. Are there performance benefits to this, actually? Because I’m very new to the entire concept of mutable things, coming from MATLAB where all I did all day was reassign values to the variables haha.
Yes, often by orders of magnitude. The fact that Matlab insists on silently copying things all over the place is one of many design choices that make that language slow.