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

Correct mutating op fallbacks #1823

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/Fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

Base.divrem(a::T, b::T) where {T <: FieldElem} = divexact(a, b), zero(parent(a))

div(a::T, b::T) where {T <: FieldElem} = divexact(a, b)
Base.div(a::T, b::T) where {T <: FieldElem} = divexact(a, b)

Check warning on line 19 in src/Fields.jl

View check run for this annotation

Codecov / codecov/patch

src/Fields.jl#L19

Added line #L19 was not covered by tests

function gcd(x::T, y::T) where {T <: FieldElem}
check_parent(x, y)
Expand Down
5 changes: 0 additions & 5 deletions src/Poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1479,11 +1479,6 @@ function Base.divrem(f::PolyRingElem{T}, g::PolyRingElem{T}) where T <: RingElem
return q, f
end

function Base.div(f::PolyRingElem{T}, g::PolyRingElem{T}) where T <: RingElement
q, r = divrem(f, g)
return q
end
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant due to

function Base.div(a::T, b::T) where T <: RingElem
return divrem(a, b)[1]
end


##############################################################################
#
# Ad hoc Euclidean division
Expand Down
27 changes: 24 additions & 3 deletions src/fundamental_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,16 @@
inv!(z, a)
inv!(a)

Return `inv(a)`, possibly modifying the object `z` in the process.
Return `AbstractAlgebra.inv(a)`, possibly modifying the object `z` in the process.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be very surprising (esp. for a Julia beginner) that AbstractAlgebra.inv != Base.inv so I think it is good to prefix it here explicitly to raise the awareness. Even more so for when this appears in the Oscar manual.

Should the same be done for div?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see you wrote that as a question. So yeah, I think div! should fallback to AA.div! (not Base.div!) and then its docstring should also be adjusted.

Aliasing is permitted.
The unary version is a shorthand for `inv!(a, a)`.

!!! note
`AbstractAlgebra.inv` and `Base.inv` differ only in their behavior on julia
types like `Integer` and `Rational{Int}`. The former makes it adhere to the
Ring interface.
"""
inv!(z, a) = inv(a)
inv!(z, a) = AbstractAlgebra.inv(a)
inv!(a) = inv!(a, a)

for (name, op) in ((:add!, :+), (:sub!, :-), (:mul!, :*))
Expand Down Expand Up @@ -378,7 +383,7 @@

function divexact end

for name in (:divexact, :div, :rem, :mod, :gcd, :lcm)
for name in (:divexact, :rem, :mod, :gcd, :lcm)
name_bang = Symbol(name, "!")
@eval begin
@doc """
Expand All @@ -394,6 +399,22 @@
end
end

@doc """
div!(z, a, b)
div!(a, b)

Return `div(a, b)`, possibly modifying the object `z` in the process.
Aliasing is permitted.
The two argument version is a shorthand for `div(a, a, b)`.

!!! note
`AbstractAlgebra.div` and `Base.div` differ only in their behavior on julia
types like `Integer` and `Rational{Int}`. The former makes it adhere to the
Ring interface.
"""
div!(z, a, b) = AbstractAlgebra.div(a, b)
div!(a, b) = div!(a, a, b)

Check warning on line 416 in src/fundamental_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/fundamental_interface.jl#L415-L416

Added lines #L415 - L416 were not covered by tests

@doc raw"""
canonical_injection(D, i)

Expand Down
4 changes: 2 additions & 2 deletions src/generic/LaurentMPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function is_gen(a::LaurentMPolyWrap)
return !iszero(_var_index(a))
end

function inv(a::LaurentMPolyWrap)
function Base.inv(a::LaurentMPolyWrap)
(ap, ad) = _normalize(a)
return LaurentMPolyWrap(parent(a), inv(ap), neg!(ad, ad))
end
Expand Down Expand Up @@ -204,7 +204,7 @@ function gcd(a::LaurentMPolyWrap, b::LaurentMPolyWrap)
return LaurentMPolyWrap(parent(a), gcd(ap, bp), zero!(ad))
end

function divrem(a::LaurentMPolyWrap, b::LaurentMPolyWrap)
function Base.divrem(a::LaurentMPolyWrap, b::LaurentMPolyWrap)
check_parent(a, b)
error("divrem not implemented for LaurentMPoly")
end
Expand Down
4 changes: 0 additions & 4 deletions src/generic/Misc/Localization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,6 @@ function Base.divrem(a::LocalizedEuclideanRingElem{T}, b::LocalizedEuclideanRing
end
end

function Base.div(a::LocalizedEuclideanRingElem{T}, b::LocalizedEuclideanRingElem{T}) where {T}
return divrem(a, b)[1]
end

function rem(a::LocalizedEuclideanRingElem{T}, b::LocalizedEuclideanRingElem{T}) where {T}
return divrem(a, b)[2]
end
Expand Down
Loading