Skip to content

Allow dot(::Vector{VariableIndex}, ::Real) #1380

@dourouc05

Description

@dourouc05

To write bridges, I found myself using dot (from the standard package LinearAlgebra). However, MOI is currently missing the operation dot between individual elements for this to work out of the box (according to the docs: https://github.com/JuliaLang/julia/blob/v1.6.0/stdlib/LinearAlgebra/src/generic.jl#L846):

  MethodError: no method matching iterate(::MathOptInterface.VariableIndex)
  Closest candidates are:
    iterate(::Union{LinRange, StepRangeLen}) at range.jl:664
    iterate(::Union{LinRange, StepRangeLen}, ::Int64) at range.jl:664
    iterate(::T) where T<:Union{Base.KeySet{var"#s79", var"#s78"} where {var"#s79", var"#s78"<:Dict}, Base.ValueIterator{var"#s77"} where var"#s77"<:Dict} at dict.jl:693
    ...
  Stacktrace:
    [1] dot(x::MathOptInterface.VariableIndex, y::Int64)
      @ LinearAlgebra C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\LinearAlgebra\src\generic.jl:876
    [2] dot(x::Vector{MathOptInterface.VariableIndex}, y::Vector{Int64})
      @ LinearAlgebra C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\LinearAlgebra\src\generic.jl:914
    [3] macro expansion
      @ .\show.jl:955 [inlined]

I wrote the following code to make it work properly:

function LinearAlgebra.dot(variable::MOI.VariableIndex, coefficient::T) where {T}
    return MOI.ScalarAffineFunction(
        [MOI.ScalarAffineTerm(coefficient, variable)], 
        zero(T)
    )
end
function LinearAlgebra.dot(coefficient::T, variable::MOI.VariableIndex) where {T}
    return MOI.ScalarAffineFunction(
        [MOI.ScalarAffineTerm(coefficient, variable)], 
        zero(T)
    )
end

I guess this should go into Utilities/functions.jl, but shall I make a PR against MOI 0.9 or 0.10?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions