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

Ambiguities whac-a-mole? #161

Open
aplavin opened this issue Jan 7, 2025 · 1 comment
Open

Ambiguities whac-a-mole? #161

aplavin opened this issue Jan 7, 2025 · 1 comment

Comments

@aplavin
Copy link
Contributor

aplavin commented Jan 7, 2025

The nature of this package makes ambiguities easy to create – it wants to overload "all" arithmetic operations basically. Some examples below.

Of course, one could directly follow the recommendation from error messages and define those specific methods. But it seems like an endless game of whacking more and more ambiguities.
I wonder if you had some thoughts on more principled approaches to avoid them? If that's possible at all...

julia> (1±2)^(2//1)
ERROR: MethodError: ^(::Particles{Float64, 2000}, ::Rational{Int64}) is ambiguous.

Candidates:
  ^(p::Particles{T, N}, a::Real...) where {T, N}
    @ MonteCarloMeasurements ~/.julia/dev/MonteCarloMeasurements/src/register_primitive.jl:44
  ^(x::Number, y::Rational)
    @ Base rational.jl:537

Possible fix, define
  ^(::Particles{T, N}, ::Rational) where {T, N}
julia> div((5±1), (3±1), RoundNearest)
ERROR: MethodError: div(::Particles{Float64, 2000}, ::Particles{Float64, 2000}, ::RoundingMode{:Nearest}) is ambiguous.

Candidates:
  div(p1::Particles{T, N}, p2::Particles{T, N}, args...) where {T, N}
    @ MonteCarloMeasurements ~/.julia/dev/MonteCarloMeasurements/src/particles.jl:354
  div(x::Real, y::Real, r::RoundingMode)
    @ Base div.jl:341

Possible fix, define
  div(::Particles{T, N}, ::Particles{T, N}, ::RoundingMode) where {T, N}
julia> norm((5±1))
5.0 ± 1.0 Particles{Float64, 2000}

julia> norm((5±1), 2)
ERROR: MethodError: norm(::Particles{Float64, 2000}, ::Int64) is ambiguous.

Candidates:
  norm(x::AbstractParticles, args...)
    @ MonteCarloMeasurements ~/.julia/dev/MonteCarloMeasurements/src/particles.jl:585
  norm(x::Number, p::Real)
    @ LinearAlgebra ~/.julia/juliaup/julia-1.11.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/LinearAlgebra/src/generic.jl:644
  norm(itr, p::Real)
    @ LinearAlgebra ~/.julia/juliaup/julia-1.11.2+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/LinearAlgebra/src/generic.jl:601

Possible fix, define
  norm(::AbstractParticles, ::Real)
@baggepinnen
Copy link
Owner

This is an unfortunate consequence of the approach taken in this package together with julia's lack of proper interfaces. It's hard to know what methods to implement to follow an interface, e.g., what does it mean to be a Real?

But it seems like an endless game of whacking more and more ambiguities.

It is if you want to cover everything, but it is not an endless game trying to get one particular calculation working. Getting several particular calculations working has been more or less how this package has been developed. The interaction between two different kinds of "special real", like particles and either of {Quantity, Dual, BigFloat} are prime examples of where lots of extra methods are required. Still, I think it's quite amazing how far you get by this kind of dispatch-based implementation, most workflows do not encounter anything other than Float64, and ones that do are likely sufficiently advanced for it to be acceptable to define a new method here and there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants