-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
gcd
, lcm
, gcdx
for Rational
#33910
gcd
, lcm
, gcdx
for Rational
#33910
Conversation
@ararslan , @StefanKarpinski the missing documentation has been added. |
bump |
Thanks, @KlausC! Will be in 1.4. |
I would have preferred a slightly different implementation. For example, instead of gcd(a::Union{Integer,Rational}, b::Union{Integer,Rational}) = gcd(promote(a,b)...) I would have preferred gcd(a::Real, b::Real) = gcd(promote(a,b)...)
gcd(a::T, b::T) where T<:Real = throw(MethodError(gcd, (a, b))) This would make it easier to extend Base.gcd(x::HalfInteger) = x
Base.lcm(x::HalfInteger) = x
Base.gcd(a::T, b::T) where T<:HalfInteger = ACTUAL CODE
Base.lcm(a::T, b::T) where T<:HalfInteger = ACTUAL CODE
Base.gcdx(x::T, y::T) where T<:HalfInteger = ACTUAL CODE
const IntOrRatOrHalf = Union{Integer, Rational, HalfInteger}
Base.lcm(a::IntOrRatOrHalf, b::IntOrRatOrHalf) = lcm(promote(a,b)...)
Base.gcd(a::IntOrRatOrHalf, b::IntOrRatOrHalf) = gcd(promote(a,b)...)
Base.lcm(a::IntOrRatOrHalf, b::IntOrRatOrHalf...) = lcm(a, lcm(b...))
Base.gcd(a::IntOrRatOrHalf, b::IntOrRatOrHalf...) = gcd(a, gcd(b...))
Base.gcdx(a::IntOrRatOrHalf, b::IntOrRatOrHalf) = gcdx(promote(a,b)...)
Base.lcm(abc::AbstractArray{<:IntOrRatOrHalf}) = reduce(lcm, abc; init=one(eltype(abc)))
function Base.gcd(abc::AbstractArray{<:IntOrRatOrHalf})
a = zero(eltype(abc))
for b in abc
a = gcd(a,b)
if a == 1
return a
end
end
return a
end where everything below and including the In my opinion, this is important for the interoperability between packages. If there are two packages which define types numeric types What do you think about this? Should I open an issue or submit a PR? |
I'm a bit unclear on what the issue there is: shouldn't integers and half-integers promote to half-integers? That seems like it should "just work". Similarly, half-integers and rationals should promote to rationals, which should also "just work". Is the issue that you want to intercept mixed type? |
No, the issue is that I have to implement const IntOrRatOrHalf = Union{Integer, Rational, HalfInteger}
Base.gcd(a::IntOrRatOrHalf, b::IntOrRatOrHalf) = gcd(promote(a,b)...) and a bunch of other methods, since there is no Promoting gcd(a::Real, b::Real) = gcd(promote(a,b)...) There is only gcd(a::Union{Integer,Rational}, b::Union{Integer,Rational}) = gcd(promote(a,b)...) |
It seems like |
Except that we'd need to have |
Are there plans to add an Edit: you were a couple of seconds quicker |
By the way, there are at least two other operations that also define their fallback methods for Lines 232 to 233 in 074974a
|
Should I just make a PR? |
Sure, go for it. |
Tackles issue #27039.