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

[Utilities] tidy Base overloads in functions.jl #2213

Merged
merged 6 commits into from Jun 22, 2023
Merged

Conversation

odow
Copy link
Member

@odow odow commented Jun 21, 2023

@blegat there's some inconsistent behavior with how we deal with mixed coefficient types. What would you like to happen?

Here's the latest release version (NOT this PR):

(jmp) pkg> st -m MathOptInterface
      Status `/private/tmp/jmp/Manifest.toml`
  [b8f27783] MathOptInterface v1.17.1

julia> x = MOI.VariableIndex(1)
MOI.VariableIndex(1)

julia> 2 * x
(0) + (2) MOI.VariableIndex(1)

julia> (2 * x) + 3
(3) + (2) MOI.VariableIndex(1)

julia> (2 * x) + 3.0
ERROR: MethodError: no method matching +(::MathOptInterface.ScalarAffineFunction{Int64}, ::Float64)
[...]

julia> (2 * x) * 3.0
0.0 + 6.0 MOI.VariableIndex(1)

julia> (2 * x) - 3.0
ERROR: MethodError: no method matching -(::MathOptInterface.ScalarAffineFunction{Int64}, ::Float64)
[...]

julia> (2 * x) / 3.0
ERROR: MethodError: no method matching /(::MathOptInterface.ScalarAffineFunction{Int64}, ::Float64)
[...]

@odow odow changed the title [Utilities] tidy Base overloads in functions.jl WIP: [Utilities] tidy Base overloads in functions.jl Jun 21, 2023
@odow
Copy link
Member Author

odow commented Jun 21, 2023

Part of the complexity here because of

# Number-like but not subtype of `Number`
struct NonNumber
value::Int
end
Base.:*(a::NonNumber, b::NonNumber) = NonNumber(a.value * b.value)
Base.:+(a::NonNumber, b::NonNumber) = NonNumber(a.value + b.value)
Base.zero(::Type{NonNumber}) = NonNumber(0)
function Base.isapprox(a::NonNumber, b::NonNumber; kws...)
return isapprox(a.value, b.value; kws...)
end
function test_NonNumber()
two = NonNumber(2)
three = NonNumber(3)
six = NonNumber(6)
three_x = MOI.Utilities.operate(*, NonNumber, three, x)
six_x = MOI.Utilities.operate(*, NonNumber, six, x)
@test six_x two * three_x
@test six_x three_x * two
return
end

Do we really want to define * between things that are not numbers? What does that even mean? Do we have any use-cases of a function with non-number coefficients? How do we know an arithmetic exists?

@blegat
Copy link
Member

blegat commented Jun 21, 2023

I don't know of any use-case. We had so many issues with upstream expecting things to be number, e.g., in jump-dev/MutableArithmetics.jl#47 that I wanted to stay away from it when possible. However for MOI coefficients it's probably fine to assumes the coefficients are numbers

@odow
Copy link
Member Author

odow commented Jun 21, 2023

Do we want + and - to also work for non-numbers? I'll leave * as-is for now, but add a comment about removing it in MOI 2.0.

@odow odow changed the title WIP: [Utilities] tidy Base overloads in functions.jl [Utilities] tidy Base overloads in functions.jl Jun 22, 2023
@odow
Copy link
Member Author

odow commented Jun 22, 2023

Left as-is for now. There hasn't been a need to add more methods for Base.:+ and Base.:-, so let's hold off until there is a reason.

@odow odow merged commit 7b88c78 into master Jun 22, 2023
12 checks passed
@odow odow deleted the od/base-functions branch June 22, 2023 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants