Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MOI.Utilities.scalar_type for (Matrix|Sparse)VectorAffineFunction #264

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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

Check warning on line 278 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L277-L278

Added lines #L277 - L278 were not covered by tests
end

odow marked this conversation as resolved.
Show resolved Hide resolved
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
Loading