Absolute value error?

Hello! I am using value function iteration to maximize a Bellman equation. When I check the difference between two vectors then take the absolute value, I get an error from the “di” line:

    subt = vnew - v
    println(subt)
    di = abs.(subt)
    v = vnew

For context, “subt” looks like:
[41474.08152719693 46131.513156937144; 42846.34653896693 47535.076846955286; 45098.84448852737

MethodError: no method matching isless(::Float64, ::Matrix{Float64})
Closest candidates are:
isless(::T, ::T) where T<:Union{Float16, Float32, Float64} at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/float.jl:460
isless(::AbstractFloat, ::AbstractFloat) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/operators.jl:184
isless(::Real, ::AbstractFloat) at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/operators.jl:185

But

It’s probably not hard to track this down. Maybe someone else can see the issue already; I don’t. Some things that might help

  • the expression for subt that you gave is incomplete. What is the full expression?
  • You wrote “But”… Is something missing?

Perhaps some context is missing from your post. I have no problems (v 1.8.1, Intel Mac)

julia> v=rand(3); vnew=rand(3); subt=vnew-v;

julia> println(subt)
[2.32118e-01, -6.61210e-02, 2.67815e-01]

julia> di=abs.(subt);

julia> v = vnew;

julia> di
3-element Vector{Float64}:
 2.32118e-01
 6.61210e-02
 2.67815e-01

You don’t happen to have a variable called abs or otherwise accidentally redefined the abs function?

What’s the full stacktrace?

1 Like

abs.(subt) is the elementwise absolute value, which returns a vector. It sounds like you want the norm of the difference, i.e. a single number norm(vnew - v). (The norm function is provided by the LinearAlgebra standard library.)

(Your MethodError: no method matching isless sounds like it is coming from a later line in your code, where you are comparing di to a scalar, e.g. you have di ≤ sometolerance, which fails because you can’t compare a vector to a scalar.)

3 Likes