Skip to content

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

Merged
odow merged 6 commits into
masterfrom
od/base-functions
Jun 22, 2023
Merged

odow merged 6 commits into
masterfrom
od/base-functions

Conversation

@odow

@odow odow commented Jun 21, 2023

Copy link
Copy Markdown
Member

@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

odow commented Jun 21, 2023

Copy link
Copy Markdown
Member Author

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

blegat commented Jun 21, 2023

Copy link
Copy Markdown
Member

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

odow commented Jun 21, 2023

Copy link
Copy Markdown
Member Author

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 force-pushed the od/base-functions branch from 7bccf3f to 91e868d Compare June 21, 2023 23:46
@odow odow changed the title WIP: [Utilities] tidy Base overloads in functions.jl [Utilities] tidy Base overloads in functions.jl Jun 22, 2023
@odow

odow commented Jun 22, 2023

Copy link
Copy Markdown
Member Author

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
@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.

2 participants