# Why is there type instability in this function?

**URL:** <https://discourse.julialang.org/t/why-is-there-type-instability-in-this-function/2508>\
**Category:** General Usage\
**Created:** [March 6, 2017, 11:48pm UTC](https://discourse.julialang.org/t/why-is-there-type-instability-in-this-function/2508 "2017-03-06T23:48:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_McBane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sean_mcbane/32/4580_2.png) [@Sean\_McBane](https://discourse.julialang.org/u/Sean_McBane)\
**Post date:** [March 6, 2017, 11:48pm UTC](https://discourse.julialang.org/t/why-is-there-type-instability-in-this-function/2508/1 "2017-03-06T23:48:15Z")

</div>

I have the following toy function implementing a backward difference formula (homework):

```
function newton_iteration{T}(x0::T, F, J; tol=2*eps(), testfun=norm)::T
    x = x0 - J(x0) \ F(x0)
    while testfun(x) > tol
        x -= J(x) \ F(x)
    end
    x
end

function backdiff2(x0, t0, tf, n::Integer, F, J)
    h = (tf - t0) / n
    solution_vector = Vector{typeof(x0)}(n+1)
    solution_vector[1] = x0

    # First step uses Euler.
    solution_vector[2] = x0 .+ h.*F(x0,t0)

 	# The rest of the steps use Newton's method to solve the
    # implicit formula.
    for i = 3:n+1
	    t = t0 + (i-1)*h
	
	    # f defines the mapping whose root is the next value of x.
	    f = x -> x .- (4/3).*solution_vector[i-1] .+
		    (1/3).*solution_vector[i-2] .- (2*h/3).*F(x, t)
		
	    # j defines the Jacobian of the above:
	    j = x -> eye(length(x)) .+ (2*h/3).*J(x,t)
	    solution_vector[i] = newton_iteration(solution_vector[i-1], f, j)
    end
    solution_vector
end

```

When inspected with `@code_warntype` it seems that the loop index `i` is being boxed, and wherever the uncertainty about `i` is coming from, it propagates to `t` and indexing operations using `i` (if I’m reading the output right). No annotation I tried got rid of the instability. What is the cause?

Sample call that generates the above described output:

```
using FixedSizeArrays
F(z, t) = Vec(z[2], -z[1])
J(z, t) = Mat{2,2,Float64}((0, 1), (-1, 0))
@code_warntype backdiff2(Vec(1.0, 0.0), 0., 9*pi/4, 100, F, J)

```

Hopefully no typos in that… I’ll check again when I’m not on the bus.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 6, 2017, 11:56pm UTC](https://discourse.julialang.org/t/why-is-there-type-instability-in-this-function/2508/2 "2017-03-06T23:56:34Z")

</div>

Please provide the calling code and the `@code_warntype` expression you are running.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 7, 2017, 12:11am UTC](https://discourse.julialang.org/t/why-is-there-type-instability-in-this-function/2508/3 "2017-03-07T00:11:16Z")

</div>

`i` should be type stable since it’s only an integer, and nothing changes that. It may be boxing in the `@code_warntype` but not actually have an instability. Could you check a little lower and see if there’s an instability?

Also, could you post a full runnable example? Complete with the functions you’re calling this on?

Also,

> [@Sean\_McBane](#):
>
> j = x → eye(length(x)) .+ (2\*h/3).\*J(x,t)

You can make this a little better using the UniformScaling operator I:

```julia
j = x -> I .+ (2*h/3).*J(x,t)

```

this will get rid of a `length(x) x length(x)` allocation each iteration, which can be quite large!

There’s usually a transformation that people do here to avoid the NxN operation of multiplying `J` by a constant too, if you’re looking for speed. Just divide the whole algorithm by the constant, and change the other coefficients.

---

<div class="post-metadata">

**Author:** ![Sean\_McBane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sean_mcbane/32/4580_2.png) [@Sean\_McBane](https://discourse.julialang.org/u/Sean_McBane)\
**Post date:** [March 7, 2017, 12:16am UTC](https://discourse.julialang.org/t/why-is-there-type-instability-in-this-function/2508/4 "2017-03-07T00:16:29Z")

</div>

I added an example to my post. I think it’s identical to the code I observed the behavior in.

---

<div class="post-metadata">

**Author:** ![Sean\_McBane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sean_mcbane/32/4580_2.png) [@Sean\_McBane](https://discourse.julialang.org/u/Sean_McBane)\
**Post date:** [March 7, 2017, 12:18am UTC](https://discourse.julialang.org/t/why-is-there-type-instability-in-this-function/2508/5 "2017-03-07T00:18:06Z")

</div>

I added the minimal usage example I was using before.

Thanks for the performance tip - I’m not really worried about performance since it’s just a numerical verification of an order of accuracy proof. I’m always filing things away for later, though.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 7, 2017, 12:47am UTC](https://discourse.julialang.org/t/why-is-there-type-instability-in-this-function/2508/6 "2017-03-07T00:47:36Z")

</div>

Very likely caused by  
[https://github.com/JuliaLang/julia/issues/15276](https://github.com/JuliaLang/julia/issues/15276)
