1
votes

I have the following code:

function generateinputdistributions! (jointA, jointB)
    rand! (jointA)
    rand! (jointB)
    println (sum (jointB))
    jointA *= (1.0/sum (jointA))
    jointB *= (1.0/sum (jointB))
    println (sum(jointB))
end

And I have a few lines of code calling it in another file:

generateinputdistributions! (jointA, jointB)
println (sum (jointB))

where jointA and jointB have been preallocated. I expected that the third print statement should give an answer of 1 (the second one does). However, it does not, and instead gives the value of the first print statement. Thus, it looks like the jointA in jointA *= (1.0/sum (jointA)) is a local object that is destroyed. Can someone please explain what exactly is going on?

What I want to do is modify jointA and jointB in place (for performance reasons). rand! seems to do its job correctly. I don't understand this behavior of *=.

1

1 Answers

1
votes

jointA *= (1.0/sum (jointA)) will result in the jointA variable containing a new object leaving the old jointA untouched. the reason is that multiplication does not modify it's operands in place. *= is the same as saying = *(...) After that line jointA contains a different object than before the line. The new object is scoped to the function since it is allocated there and never returned so julia is free to GC it after the function exits.