Skip to content

Commit

Permalink
More unsafe ops for QQ/ZZMPolyRingElem (#1904)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin authored Oct 18, 2024
1 parent 50132ae commit 6848ca3
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 154 deletions.
95 changes: 20 additions & 75 deletions src/flint/fmpq_mpoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,71 +336,23 @@ end
#
###############################################################################

for (jT, cN, cT) in ((QQFieldElem, :fmpq, Ref{QQFieldElem}), (ZZRingElem, :fmpz, Ref{ZZRingElem}),
(Int, :si, Int), (UInt, :ui, UInt))
for jT in (QQFieldElem, ZZRingElem, Integer, Rational)
@eval begin
function +(a::QQMPolyRingElem, b::($jT))
z = parent(a)()
return add!(z, a, b)
end
+(a::QQMPolyRingElem, b::($jT)) = add!(parent(a)(), a, b)
+(a::($jT), b::QQMPolyRingElem) = add!(parent(b)(), a, b)

+(a::($jT), b::QQMPolyRingElem) = b + a

function -(a::QQMPolyRingElem, b::($jT))
z = parent(a)()
return sub!(z, a, b)
end
-(a::QQMPolyRingElem, b::($jT)) = sub!(parent(a)(), a, b)
-(a::($jT), b::QQMPolyRingElem) = sub!(parent(b)(), a, b)

-(a::($jT), b::QQMPolyRingElem) = neg!(b - a)
*(a::QQMPolyRingElem, b::($jT)) = mul!(parent(a)(), a, b)
*(a::($jT), b::QQMPolyRingElem) = mul!(parent(b)(), a, b)

function *(a::QQMPolyRingElem, b::($jT))
z = parent(a)()
return mul!(z, a, b)
end

*(a::($jT), b::QQMPolyRingElem) = b * a

function divexact(a::QQMPolyRingElem, b::($jT); check::Bool=true)
z = parent(a)()
return divexact!(z, a, b)
end
divexact(a::QQMPolyRingElem, b::($jT); check::Bool=true) = divexact!(parent(a)(), a, b)

//(a::QQMPolyRingElem, b::($jT)) = a//parent(a)(b)
end
end

+(a::QQMPolyRingElem, b::Integer) = a + flintify(b)

+(a::Integer, b::QQMPolyRingElem) = b + a

-(a::QQMPolyRingElem, b::Integer) = a - flintify(b)

-(a::Integer, b::QQMPolyRingElem) = neg!(b - a)

+(a::QQMPolyRingElem, b::Rational{<:Integer}) = a + QQFieldElem(b)

+(a::Rational{<:Integer}, b::QQMPolyRingElem) = b + a

-(a::QQMPolyRingElem, b::Rational{<:Integer}) = a - QQFieldElem(b)

-(a::Rational{<:Integer}, b::QQMPolyRingElem) = neg!(b - a)

*(a::QQMPolyRingElem, b::Integer) = a * flintify(b)

*(a::Integer, b::QQMPolyRingElem) = b * a

*(a::QQMPolyRingElem, b::Rational{<:Integer}) = a * QQFieldElem(b)

*(a::Rational{<:Integer}, b::QQMPolyRingElem) = b * a

divexact(a::QQMPolyRingElem, b::Integer; check::Bool=true) = divexact(a, flintify(b); check=check)

divexact(a::QQMPolyRingElem, b::Rational{<:Integer}; check::Bool=true) = divexact(a, QQFieldElem(b); check=check)

//(a::QQMPolyRingElem, b::Integer) = //(a, flintify(b))

//(a::QQMPolyRingElem, b::Rational{<:Integer}) = //(a, QQFieldElem(b))

###############################################################################
#
# Powering
Expand Down Expand Up @@ -849,26 +801,20 @@ for (jT, cN, cT) in ((QQFieldElem, :fmpq, Ref{QQFieldElem}), (ZZRingElem, :fmpz,
return a
end

add!(a::QQMPolyRingElem, b::($jT), c::QQMPolyRingElem) = add!(a, c, b)

function sub!(a::QQMPolyRingElem, b::QQMPolyRingElem, c::($jT))
ccall(($(string(:fmpq_mpoly_sub_, cN)), libflint), Nothing,
(Ref{QQMPolyRingElem}, Ref{QQMPolyRingElem}, ($cT), Ref{QQMPolyRing}),
a, b, c, parent(a))
return a
end

sub!(a::QQMPolyRingElem, b::($jT), c::QQMPolyRingElem) = neg!(sub!(a, c, b))

function mul!(a::QQMPolyRingElem, b::QQMPolyRingElem, c::($jT))
ccall(($(string(:fmpq_mpoly_scalar_mul_, cN)), libflint), Nothing,
(Ref{QQMPolyRingElem}, Ref{QQMPolyRingElem}, ($cT), Ref{QQMPolyRing}),
a, b, c, parent(a))
return a
end

mul!(a::QQMPolyRingElem, b::($jT), c::QQMPolyRingElem) = mul!(a, c, b)

function divexact!(a::QQMPolyRingElem, b::QQMPolyRingElem, c::($jT))
ccall(($(string(:fmpq_mpoly_scalar_div_, cN)), libflint), Nothing,
(Ref{QQMPolyRingElem}, Ref{QQMPolyRingElem}, ($cT), Ref{QQMPolyRing}),
Expand All @@ -878,6 +824,16 @@ for (jT, cN, cT) in ((QQFieldElem, :fmpq, Ref{QQFieldElem}), (ZZRingElem, :fmpz,
end
end

add!(a::QQMPolyRingElem, b::QQMPolyRingElem, c::RationalUnion) = add!(a, b, flintify(c))
add!(a::QQMPolyRingElem, b::RationalUnion, c::QQMPolyRingElem) = add!(a, c, b)

sub!(a::QQMPolyRingElem, b::QQMPolyRingElem, c::RationalUnion) = sub!(a, b, flintify(c))
sub!(a::QQMPolyRingElem, b::RationalUnion, c::QQMPolyRingElem) = neg!(sub!(a, c, b))

mul!(a::QQMPolyRingElem, b::QQMPolyRingElem, c::RationalUnion) = mul!(a, b, flintify(c))
mul!(a::QQMPolyRingElem, b::RationalUnion, c::QQMPolyRingElem) = mul!(a, c, b)

divexact!(a::QQMPolyRingElem, b::QQMPolyRingElem, c::RationalUnion) = divexact!(a, b, flintify(c))

# Set the n-th coefficient of a to c. If zero coefficients are inserted, they
# must be removed with combine_like_terms!
Expand Down Expand Up @@ -1047,19 +1003,8 @@ function setcoeff!(a::QQMPolyRingElem, exps::Vector{Int}, b::QQFieldElem)
end

# Set the coefficient of the term with the given exponent vector to the
# given integer. Removal of a zero term is performed.
setcoeff!(a::QQMPolyRingElem, exps::Vector{Int}, b::Rational{<:Integer}) =
setcoeff!(a, exps, QQFieldElem(b))

# Set the coefficient of the term with the given exponent vector to the
# given ZZRingElem. Removal of a zero term is performed.
setcoeff!(a::QQMPolyRingElem, exps::Vector{Int}, b::ZZRingElem) =
setcoeff!(a, exps, QQFieldElem(b))

# Set the coefficient of the term with the given exponent vector to the
# given integer. Removal of a zero term is performed.
setcoeff!(a::QQMPolyRingElem, exps::Vector{Int}, b::Integer) =
setcoeff!(a, exps, QQFieldElem(b))
# given value. Removal of a zero term is performed.
setcoeff!(a::QQMPolyRingElem, exps::Vector{Int}, b::RationalUnion) = setcoeff!(a, exps, QQ(b))

# Sort the terms according to the ordering. This is only needed if unsafe
# functions such as those above have been called and terms have been inserted
Expand Down
164 changes: 85 additions & 79 deletions src/flint/fmpz_mpoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,28 +286,19 @@ end
function +(a::ZZMPolyRingElem, b::ZZMPolyRingElem)
check_parent(a, b)
z = parent(a)()
ccall((:fmpz_mpoly_add, libflint), Nothing,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}),
z, a, b, a.parent)
return z
return add!(z, a, b)
end

function -(a::ZZMPolyRingElem, b::ZZMPolyRingElem)
check_parent(a, b)
z = parent(a)()
ccall((:fmpz_mpoly_sub, libflint), Nothing,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}),
z, a, b, a.parent)
return z
return sub!(z, a, b)
end

function *(a::ZZMPolyRingElem, b::ZZMPolyRingElem)
check_parent(a, b)
z = parent(a)()
ccall((:fmpz_mpoly_mul, libflint), Nothing,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}),
z, a, b, a.parent)
return z
return mul!(z, a, b)
end

###############################################################################
Expand All @@ -316,69 +307,30 @@ end
#
###############################################################################

for (jT, cN, cT) in ((ZZRingElem, :fmpz, Ref{ZZRingElem}), (Int, :si, Int), (UInt, :ui, UInt))
for jT in (ZZRingElem, Integer)
@eval begin
function +(a::ZZMPolyRingElem, b::($jT))
z = parent(a)()
ccall(($(string(:fmpz_mpoly_add_, cN)), libflint), Nothing,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, ($cT), Ref{ZZMPolyRing}),
z, a, b, parent(a))
return z
end

+(a::($jT), b::ZZMPolyRingElem) = b + a

function -(a::ZZMPolyRingElem, b::($jT))
z = parent(a)()
ccall(($(string(:fmpz_mpoly_sub_, cN)), libflint), Nothing,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, ($cT), Ref{ZZMPolyRing}),
z, a, b, parent(a))
return z
end

-(a::($jT), b::ZZMPolyRingElem) = - (b - a)
+(a::ZZMPolyRingElem, b::($jT)) = add!(parent(a)(), a, b)
+(a::($jT), b::ZZMPolyRingElem) = add!(parent(b)(), a, b)

function *(a::ZZMPolyRingElem, b::($jT))
z = parent(a)()
ccall(($(string(:fmpz_mpoly_scalar_mul_, cN)), libflint), Nothing,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, ($cT), Ref{ZZMPolyRing}),
z, a, b, parent(a))
return z
end
-(a::ZZMPolyRingElem, b::($jT)) = sub!(parent(a)(), a, b)
-(a::($jT), b::ZZMPolyRingElem) = sub!(parent(b)(), a, b)

*(a::($jT), b::ZZMPolyRingElem) = b * a
*(a::ZZMPolyRingElem, b::($jT)) = mul!(parent(a)(), a, b)
*(a::($jT), b::ZZMPolyRingElem) = mul!(parent(b)(), a, b)

function divexact(a::ZZMPolyRingElem, b::($jT); check::Bool=true)
z = parent(a)()
if check
divides = Bool(ccall(($(string(:fmpz_mpoly_scalar_divides_, cN)), libflint), Cint,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, ($cT), Ref{ZZMPolyRing}),
z, a, b, parent(a)))
divides || error("Division is not exact in divexact")
d, z = divides!(z, a, b)
d || error("Division is not exact in divexact")
else
ccall(($(string(:fmpz_mpoly_scalar_divexact_, cN)), libflint), Nothing,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, ($cT), Ref{ZZMPolyRing}),
z, a, b, parent(a))
z = divexact!(z, a, b)
end
return z
end
end
end

+(a::ZZMPolyRingElem, b::Integer) = a + flintify(b)

+(a::Integer, b::ZZMPolyRingElem) = b + a

-(a::ZZMPolyRingElem, b::Integer) = a - flintify(b)

-(a::Integer, b::ZZMPolyRingElem) = -(b - a)

*(a::ZZMPolyRingElem, b::Integer) = a * flintify(b)

*(a::Integer, b::ZZMPolyRingElem) = b * a

divexact(a::ZZMPolyRingElem, b::Integer; check::Bool=true) = divexact(a, ZZRingElem(b); check=check)

###############################################################################
#
# Powering
Expand Down Expand Up @@ -572,19 +524,12 @@ end
#
###############################################################################

function divides(a::ZZMPolyRingElem, b::ZZMPolyRingElem)
function divides(a::ZZMPolyRingElem, b::Union{IntegerUnion, ZZMPolyRingElem})
check_parent(a, b)
if iszero(a)
return true, zero(parent(a))
end
if iszero(b)
return false, zero(parent(a))
end
z = parent(a)()
d = ccall((:fmpz_mpoly_divides, libflint), Cint,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}),
z, a, b, a.parent)
return isone(d), z
z = zero(parent(a))
iszero(a) && return true, z
iszero(b) && return false, z
return divides!(z, a, b)
end

###############################################################################
Expand Down Expand Up @@ -783,20 +728,81 @@ function neg!(z::ZZMPolyRingElem, a::ZZMPolyRingElem)
return z
end

function add!(a::ZZMPolyRingElem, b::ZZMPolyRingElem, c::ZZMPolyRingElem)
function add!(z::ZZMPolyRingElem, a::ZZMPolyRingElem, b::ZZMPolyRingElem)
ccall((:fmpz_mpoly_add, libflint), Nothing,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem},
Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}), a, b, c, a.parent)
return a
Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}), z, a, b, a.parent)
return z
end

function mul!(a::ZZMPolyRingElem, b::ZZMPolyRingElem, c::ZZMPolyRingElem)
function sub!(z::ZZMPolyRingElem, a::ZZMPolyRingElem, b::ZZMPolyRingElem)
ccall((:fmpz_mpoly_sub, libflint), Nothing,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem},
Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}), z, a, b, a.parent)
return z
end

function mul!(z::ZZMPolyRingElem, a::ZZMPolyRingElem, b::ZZMPolyRingElem)
ccall((:fmpz_mpoly_mul, libflint), Nothing,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem},
Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}), a, b, c, a.parent)
return a
Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}), z, a, b, a.parent)
return z
end

function divides!(z::ZZMPolyRingElem, a::ZZMPolyRingElem, b::ZZMPolyRingElem)
d = ccall((:fmpz_mpoly_divides, libflint), Cint,
(Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRingElem}, Ref{ZZMPolyRing}),
z, a, b, parent(a))
return Bool(d), z
end

for (jT, cN, cT) in ((ZZRingElem, :fmpz, Ref{ZZRingElem}), (Int, :si, Int), (UInt, :ui, UInt))
@eval begin
function add!(z::ZZMPolyRingElem, a::ZZMPolyRingElem, b::$jT)
@ccall libflint.$(string(:fmpz_mpoly_add_, cN))(z::Ref{ZZMPolyRingElem},
a::Ref{ZZMPolyRingElem}, b::$cT, parent(a)::Ref{ZZMPolyRing})::Nothing
return z
end

function sub!(z::ZZMPolyRingElem, a::ZZMPolyRingElem, b::($jT))
@ccall libflint.$(string(:fmpz_mpoly_sub_, cN))(z::Ref{ZZMPolyRingElem},
a::Ref{ZZMPolyRingElem}, b::$cT, parent(a)::Ref{ZZMPolyRing})::Nothing
return z
end

function mul!(z::ZZMPolyRingElem, a::ZZMPolyRingElem, b::($jT))
@ccall libflint.$(string(:fmpz_mpoly_scalar_mul_, cN))(z::Ref{ZZMPolyRingElem},
a::Ref{ZZMPolyRingElem}, b::$cT, parent(a)::Ref{ZZMPolyRing})::Nothing
return z
end

function divexact!(z::ZZMPolyRingElem, a::ZZMPolyRingElem, b::($jT))
@ccall libflint.$(string(:fmpz_mpoly_scalar_divexact_, cN))(z::Ref{ZZMPolyRingElem},
a::Ref{ZZMPolyRingElem}, b::$cT, parent(a)::Ref{ZZMPolyRing})::Nothing
return z
end

function divides!(z::ZZMPolyRingElem, a::ZZMPolyRingElem, b::($jT))
d = @ccall libflint.$(string(:fmpz_mpoly_scalar_divides_, cN))(z::Ref{ZZMPolyRingElem},
a::Ref{ZZMPolyRingElem}, b::$cT, parent(a)::Ref{ZZMPolyRing})::Cint
return Bool(d), z
end
end
end

add!(a::ZZMPolyRingElem, b::ZZMPolyRingElem, c::IntegerUnion) = add!(a, b, flintify(c))
add!(a::ZZMPolyRingElem, b::IntegerUnion, c::ZZMPolyRingElem) = add!(a, c, b)

sub!(a::ZZMPolyRingElem, b::ZZMPolyRingElem, c::IntegerUnion) = sub!(a, b, flintify(c))
sub!(a::ZZMPolyRingElem, b::IntegerUnion, c::ZZMPolyRingElem) = neg!(sub!(a, c, b))

mul!(a::ZZMPolyRingElem, b::ZZMPolyRingElem, c::IntegerUnion) = mul!(a, b, flintify(c))
mul!(a::ZZMPolyRingElem, b::IntegerUnion, c::ZZMPolyRingElem) = mul!(a, c, b)

divexact!(a::ZZMPolyRingElem, b::ZZMPolyRingElem, c::RationalUnion) = divexact!(a, b, flintify(c))

divides!(a::ZZMPolyRingElem, b::ZZMPolyRingElem, c::RationalUnion) = divides!(a, b, flintify(c))

# Set the n-th coefficient of a to c. If zero coefficients are inserted, they
# must be removed with combine_like_terms!
function setcoeff!(a::ZZMPolyRingElem, n::Int, c::ZZRingElem)
Expand Down

0 comments on commit 6848ca3

Please sign in to comment.