Jonathan Denton
Jonathan Denton

Reputation: 1

Julia Code Changes Variable Value When Value is only Read, Never Assigned

First, is anyone able to recreate the exact output I get? Second, Can anyone tell me why the value for u changes in this while loop? To the best of my understanding it should be constant yet it continues to decrease... Or if no one else can recreate this issue I'll look into hardware errors. I'm guessing it's a novice user error in Julia and I tried both global and non-global u declarations... Thanks!

Windows 7 Pro, HP Z820, 64-Bit, running Julia 0.4.5

global u=[2.681726086033021];
eo1  = u;
tem5 = [9999.9];
ktr = [1];
tsize=[1];
axnl=[0.1641272464920273];
aynl=[-.08693550068203583];

sineo1 = sin(eo1); #initialize
coseo1 = cos(eo1);

while (maximum(abs(tem5)) >= 1.0e-12 )== 1 && maximum(ktr) <= 10
tem5orig = abs(tem5) .>= 1.0e-12; #we care about updating records *originally* meeting conditions
sineo1[tem5orig] = sin(eo1[tem5orig]);
coseo1[tem5orig] = cos(eo1[tem5orig]);
tem5temp1 = ones(tsize) - coseo1 .* axnl - sineo1 .* aynl;
tem5[tem5orig]  = tem5temp1[tem5orig];
tem5[tem5orig]   = (u[tem5orig] - aynl[tem5orig] .*coseo1[tem5orig] + axnl[tem5orig] .*sineo1[tem5orig] - eo1[tem5orig]) ./tem5[tem5orig];
tem5temp2 = tem5;
if (maximum(abs(tem5temp2)) > 0.95) == 1
    if maximum((abs(tem5temp2) .> 0.95) + (tem5temp2 .> 0) .> 1) == 1
        tem5temp2[((abs(tem5temp2) .> 0.95) + (tem5temp2 .> 0)) .> 1] = 0.95;
    end
    if (maximum((abs(tem5temp2) .> 0.95) + (tem5temp2 .<= 0)) .> 1) == 1
        tem5temp2[((abs(tem5temp2) .> 0.95) + (tem5temp2 .<= 0)) .> 1] = -0.95;
    end
end
tem5[tem5orig] = tem5temp2[tem5orig];
eo1[tem5orig] = eo1[tem5orig] + tem5[tem5orig];
ktrtemp = ktr + 1;
ktr[tem5orig] = ktrtemp[tem5orig];
println(ktr,",",tem5,",",eo1,",",sineo1,",",coseo1,",",u,",",tem5orig)
end


Output:
    [2],[-0.0042672957728621345],[2.6774587902601588],[0.4438285405397952],-0.8961117266291717],[2.6774587902601588],Bool[true]
    [3],[-0.003598989243971466],[2.6738598010161874],[0.4476484617059426],[-0.8942096257211187],[2.6738598010161874],Bool[true]
    [4],[-0.003035319765326468],[2.670824481250861],[0.4508638064529911],[-0.8925927559815393],[2.670824481250861],Bool[true]
    [5],[-0.0025599151808959153],[2.6682645660699653],[0.4535710297874299],[-0.8912201304602417],[2.6682645660699653],Bool[true]
    [6],[-0.0021589606544931365],[2.6661056054154724],[0.4558509890748779],[-0.8900561082086093],[2.6661056054154724],Bool[true]
    [7],[-0.0018208009676989512],[2.6642848044477736],[0.4577715213143546],[-0.8890698702990342],[2.6642848044477736],Bool[true]
    [8],[-0.0015356039646266324],[2.662749200483147],[0.45938958087161175],[-0.8882348861571498],[2.662749200483147],Bool[true]
    [9],[-0.001295076024952105],[2.661454124458195],[0.46075301570977506],[-0.8875283987086542],[2.661454124458195],Bool[true]
    [10],[-0.0010922216912360376],[2.660361902766959],[0.4619020457466296],[-0.8869309444004525],[2.660361902766959],Bool[true]
    [11],[-0.0009211406996768078],[2.659440762067282],[0.4628704952575407],[-0.8864259160358744],[2.659440762067282],Bool[true]

Upvotes: 0

Views: 73

Answers (1)

StefanKarpinski
StefanKarpinski

Reputation: 33249

eo1 and u refer to the same array object. When you modify the contents of one, the other one also changes – because they are the same object. If you initialize eo1 = copy(u) instead, u won't change when you modify eo1.

Upvotes: 5

Related Questions