This simple code gives signal segv
I cannot figure out what's wrong :-(
f1(x,y,z) = x*y-z^2-1;
f2(x,y,z) = x*y*z+y^2-x^2-2;
f3(x,y,z) = exp(x)+z-exp(y)-3;
function Fun(x)
return [ f1(x[1],x[2],x[3]); f2(x[1],x[2],x[3]); f3(x[1],x[2],x[3]) ];
end;
function Jac(x)
return [ x[2] x[1] -2*x[3] ; x[2]*x[3]-2x[1] x[1]*x[3]+2x[2] x[1]*x[2] ; exp(x[1]) -exp(x[2]) 1 ];
end
x = ones(3); # [1.0, 1.0, 1.0];
for it=1:5
h = - \(Jac(x),Fun(x))
x = x+h
end
The propblem is probably with for loop. One can try and do several steps without loop.
My julia is Version 0.6.0 (2017-06-19 13:05 UTC)
I tried both on Mac and Win. The same problem.
Manifests as a stack overflow error on my machine:
julia> for it=1:5
h = - (Jac(x),Fun(x))
x = x+h
end
ERROR: StackOverflowError:
Stacktrace:
[1] _methods_by_ftype(::Any, ::Int64, ::UInt64, ::Array{UInt64,1}, ::Array{UInt64,1}) at ./reflection.jl:521
[2] abstract_call_gf_by_type(::Any, ::Any, ::Core.Inference.InferenceState) at ./inference.jl:1311
[3] abstract_call(::Any, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Core.Inference.InferenceState) at ./inference.jl:1897
[4] abstract_eval_call(::Expr, ::Array{Any,1}, ::Core.Inference.InferenceState) at ./inference.jl:1927
[5] abstract_eval(::Any, ::Array{Any,1}, ::Core.Inference.InferenceState) at ./inference.jl:1950
[6] typeinf_work(::Core.Inference.InferenceState) at ./inference.jl:2722
[7] typeinf(::Core.Inference.InferenceState) at ./inference.jl:2787
[8] typeinf_edge(::Method, ::Any, ::SimpleVector, ::Core.Inference.InferenceState) at ./inference.jl:2535
[9] abstract_call_gf_by_type(::Any, ::Any, ::Core.Inference.InferenceState) at ./inference.jl:1420
[10] abstract_call(::Any, ::Array{Any,1}, ::Array{Any,1}, ::Array{Any,1}, ::Core.Inference.InferenceState) at ./inference.jl:1897
[11] abstract_eval_call(::Expr, ::Array{Any,1}, ::Core.Inference.InferenceState) at ./inference.jl:1927
[12] abstract_eval(::Any, ::Array{Any,1}, ::Core.Inference.InferenceState) at ./inference.jl:1950
[13] (::Core.Inference.##189#190{Array{Any,1},Core.Inference.InferenceState})(::Expr) at ./<missing>:0
[14] next(::Core.Inference.Generator{Array{Any,1},Core.Inference.##189#190{Array{Any,1},Core.Inference.InferenceState}}, ::Int64) at ./generator.jl:45
[15] copy!(::Array{Any,1}, ::Core.Inference.Generator{Array{Any,1},Core.Inference.##189#190{Array{Any,1},Core.Inference.InferenceState}}) at ./abstractarray.jl:573
[16] _collect(::Type{Any}, ::Core.Inference.Generator{Array{Any,1},Core.Inference.##189#190{Array{Any,1},Core.Inference.InferenceState}}, ::Core.Inference.HasShape) at ./array.jl:396
[17] collect(::Type{Any}, ::Core.Inference.Generator{Array{Any,1},Core.Inference.##189#190{Array{Any,1},Core.Inference.InferenceState}}) at ./array.jl:393
[18] abstract_eval_call(::Expr, ::Array{Any,1}, ::Core.Inference.InferenceState) at ./inference.jl:1901
[19] abstract_eval(::Any, ::Array{Any,1}, ::Core.Inference.InferenceState) at ./inference.jl:1950
[20] abstract_interpret(::Any, ::Array{Any,1}, ::Core.Inference.InferenceState) at ./inference.jl:2076
[21] typeinf_work(::Core.Inference.InferenceState) at ./inference.jl:2669
[22] typeinf(::Core.Inference.InferenceState) at ./inference.jl:2787
[23] typeinf_ext(::Core.MethodInstance, ::UInt64) at ./inference.jl:2628
Same thing happens on current master.
I know. I check 0.7 as well. I didn鈥檛 sleep this night because of this.
Somewhat different stacktrace (d3afdfd): https://gist.github.com/fredrikekre/e4f089ca902b97932e1c6dcffc76f21a , in particular note the
ERROR: UndefVarError: #s1 not defined
Stacktrace:
[1] anonymous at ./<missing>:?
at the end. Note also that for me it does not crash the julia session.
I was preparing my lecture for my students about Newton method for solving system of nonlinear equations. Please tell me what should say to my students.
Wrap in a function:
f1(x,y,z) = x*y-z^2-1
f2(x,y,z) = x*y*z+y^2-x^2-2
f3(x,y,z) = exp(x)+z-exp(y)-3
function Fun(x)
return [ f1(x[1],x[2],x[3]); f2(x[1],x[2],x[3]); f3(x[1],x[2],x[3]) ]
end
function Jac(x)
return [ x[2] x[1] -2*x[3] ; x[2]*x[3]-2x[1] x[1]*x[3]+2x[2] x[1]*x[2] ; exp(x[1]) -exp(x[2]) 1 ]
end
function newton(x)
for it = 1:5
h = - (Jac(x)\Fun(x))
x += h
end
return x
end
Works on both 0.6 and master:
julia> newton(ones(3))
3-element Array{Float64,1}:
1.77767
1.42396
1.23747
Thank you very much guys.
Also, you should use 0.6 for teaching. 0.7 is changing/breaking stuff daily.
I think he is on 0.6
My julia is Version 0.6.0 (2017-06-19 13:05 UTC)
...
I check 0.7 as well.
Sorry for being confused.
Most helpful comment
I was preparing my lecture for my students about Newton method for solving system of nonlinear equations. Please tell me what should say to my students.