Zach
Zach

Reputation: 620

Julia - Stackoverflow error when using norm

I am trying to run a 5 years old Julia code that computes a linear regression. It contains the following constraint:

# y::Vector{Float64} of size 122
# X::Matrix{Float64} of size 122x122
# beta::Vector{VariableRef} of size 122
# t::VariableRef
@constraint(m, norm(y - X * beta) <= t)

This code should have been correct at the time since it was used as an example in a practical work. However, when I run it now the norm function returns the following error:

ERROR: StackOverflowError:
Stacktrace:
     [1] norm2(x::AffExpr)
       @ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:558
     [2] norm(itr::AffExpr, p::Int64)
       @ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:627
     [3] norm(itr::AffExpr)
       @ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:625
     [4] generic_normInf(x::AffExpr)
       @ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:466
     [5] normInf(x::AffExpr)
       @ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:556
     [6] generic_norm2(x::AffExpr)
       @ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:497
--- the last 6 lines are repeated 13329 more times ---
 [79981] norm2(x::AffExpr)
       @ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:558
 [79982] norm(itr::AffExpr, p::Int64)
       @ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:627
 [79983] norm(itr::AffExpr)
       @ LinearAlgebra /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/generic.jl:625

I assume that the error is due to a change in the norm function in the last 5 years but I do not know how to solve this... Do you have any idea how to make it work?

EDIT: this seems to occur when the input of the norm is not of type Number (source). This does not solve my problem though since I am still looking for a replacement for the norm function in the context of a @constraint.

Upvotes: 0

Views: 58

Answers (1)

Zach
Zach

Reputation: 620

It can be done by adding this function:

function normP(v::Vector{AffExpr}; p::Int=2)
    return sum(v[i]^p for i in 1:length(v))
end 

Upvotes: 0

Related Questions