ArgumentError: no outer repetition count may be negative; got (-1,)

I have received the following error, and could not find any explanation for its meaning in Google search results.

ArgumentError: no outer repetition count may be negative; got (-1,)

Stacktrace:
 [1] check
   @ ./abstractarraymath.jl:318 [inlined]
 [2] #repeat#1
   @ ./abstractarraymath.jl:267 [inlined]
 [3] repeat
   @ ./abstractarraymath.jl:224 [inlined]
 [4] buildQP_constrained!(ctrl::MPCController{OSQP.Model}, A::SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}, B::SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}, Q::Diagonal{Float64, Vector{Float64}}, R::Diagonal{Float64, Vector{Float64}}, Qf::Matrix{Float64}; kwargs::Base.Iterators.Pairs{Symbol, Real, Tuple{Symbol, Symbol}, NamedTuple{(:tol, :verbose), Tuple{Float64, Bool}}})
   @ Main ./In[33]:36
 [5] #buildQP!#36
   @ ./In[11]:60 [inlined]
 [6] top-level scope
   @ In[34]:7
 [7] eval
   @ ./boot.jl:360 [inlined]
 [8] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1116

The code causing this problem is as shown:

    ABI = hcat(A, B, -I)

    AB = [sparse(hcat(A, B))]
    
    E = blockdiag(sparse(B), repeat(AB, Nh-3)..., sparse(ABI))  # offblockdiag
    
    # Add the identity matrices
    for i = 1:Nh-1
        E[N_r*(i-1).+(1:N_r), N_b2+N_ab2*(i-1).+(1:N_I)] .= -I(N_I)
    end

According to the stacktrace, the error is occurring on the following line: E = blockdiag(sparse(B), repeat(AB, Nh-3)..., sparse(ABI))

The following information may also be useful:

typeof(A) = SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}
typeof(B) = SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}

I am now quite sure this is a problem with repeat(), although I’m not sure whether the mistake is on my end or the API’s.

The Nh variable may be smaller than or equal to 3? I would guess it has value 2 when the error occurs. (You would be passing -1 to repeat in this case.)

1 Like

Please provide a Minimum Working Example. See point 4 in this post:

1 Like

Yep, this was the problem. Nh was being rewritten from a value of 50 down to 2 later in the code.