In your definition I think you meant "type stability," not "type instability."
To be completely explicit, a function is type-stable if you can calculate the
return type(s) of all outputs from the types of the inputs. If you have to
look at the actual values of the inputs, it's not type-stable.
--Tim
On Monday, June 30, 2014 09:17:45 AM Spencer Lyon wrote:
> Does this function suffer from type instability?
>
> function bellman_operator{T <: FloatingPoint}(g::GrowthModel, w::Vector{T},
> compute_policy::Bool=false)
> # === Apply linear interpolation to w === #
> Aw = CoordInterpGrid(g.grid, w, BCnan, InterpLinear)
>
> if compute_policy
> σ = zeros(w)
> end
>
> # === set Tw[i] equal to max_c { u(c) + beta w(f(k_i) - c)} === #
> Tw = zeros(w)
> for (i, k) in enumerate(g.grid)
> objective(c) = - g.u(c) - g.β * Aw[g.f(k) - c]
> res = optimize(objective, 1e-6, g.f(k))
> c_star = res.minimum
> if compute_policy
> σ[i] = c_star
> end
>
> Tw[i] = - objective(c_star)
> end
>
> if compute_policy
> return Tw, σ
> else
> return Tw
> end
> end
>
> As far as I understand (which could very well be the source of my
> uncertainty — can anyone correct this definition?), the definition of type
> instability is that return types depend on the types of input types. In
> this case the return type doesn’t depend on the input type, but it does
> depend on the *value* of the argument compute_policy (return type is either
> Vector{T} or (Vector{T}, Vector{T})). I am a little unsure because given
> the *value* of compute_policy, the return type is unambiguous.
>