Skip to content

Commit

Permalink
Implement MOI.Utilities.scalar_type for (Matrix|Sparse)VectorAffineFu…
Browse files Browse the repository at this point in the history
…nction (#264)
  • Loading branch information
odow authored Jan 7, 2025
1 parent 07478f8 commit e722173
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ struct MatrixVectorAffineFunction{AT,VT} <: MOI.AbstractVectorFunction
terms::AT
constants::VT
end

MOI.constant(func::MatrixVectorAffineFunction) = func.constants
function Base.convert(
::Type{MOI.VectorAffineFunction{T}},
Expand Down Expand Up @@ -269,14 +270,22 @@ function MOIU.isapprox_zero(
return MOIU.isapprox_zero(standard_form(func), tol)
end

_scalar(::Type{<:MatrixVectorAffineFunction}) = VectorScalarAffineFunction
_scalar(::Type{<:SparseVectorAffineFunction}) = SparseScalarAffineFunction
function MOI.Utilities.scalar_type(::Type{<:MatrixVectorAffineFunction})
return VectorScalarAffineFunction
end

function MOI.Utilities.scalar_type(::Type{<:SparseVectorAffineFunction})
return SparseScalarAffineFunction
end

function Base.getindex(
it::MOI.Utilities.ScalarFunctionIterator{F},
output_index::Integer,
) where {F<:Union{MatrixVectorAffineFunction,SparseVectorAffineFunction}}
return _scalar(F)(it.f.terms[output_index, :], it.f.constants[output_index])
return MOI.Utilities.scalar_type(F)(
it.f.terms[output_index, :],
it.f.constants[output_index],
)
end

function _index_map_to_oneto!(index_map, v::MOI.VariableIndex)
Expand Down

0 comments on commit e722173

Please sign in to comment.