From 81b8471be245af2c8a8cee36078b5205e98d8414 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 17 Oct 2024 15:04:09 +0200 Subject: [PATCH] Unsafe ops for QQ/ZZPolyRingElem, QQ/ZZMatrix Move flintify calls down into unsafe ops --- src/flint/FlintTypes.jl | 72 +++------- src/flint/fmpq_mat.jl | 206 +++++++++++--------------- src/flint/fmpq_poly.jl | 312 ++++++++++++++++++++-------------------- src/flint/fmpz_mat.jl | 141 ++++++++---------- src/flint/fmpz_poly.jl | 219 +++++++++++++++------------- 5 files changed, 440 insertions(+), 510 deletions(-) diff --git a/src/flint/FlintTypes.jl b/src/flint/FlintTypes.jl index 206a09de1..e74d379a8 100644 --- a/src/flint/FlintTypes.jl +++ b/src/flint/FlintTypes.jl @@ -253,37 +253,20 @@ mutable struct ZZPolyRingElem <: PolyRingElem{ZZRingElem} return z end - function ZZPolyRingElem(a::Vector{ZZRingElem}) + function ZZPolyRingElem(a::Vector{<:Union{Integer,ZZRingElem}}) z = new() ccall((:fmpz_poly_init2, libflint), Nothing, (Ref{ZZPolyRingElem}, Int), z, length(a)) for i = 1:length(a) - ccall((:fmpz_poly_set_coeff_fmpz, libflint), Nothing, - (Ref{ZZPolyRingElem}, Int, Ref{ZZRingElem}), z, i - 1, a[i]) + setcoeff!(z, i - 1, a[i]) end finalizer(_fmpz_poly_clear_fn, z) return z end - function ZZPolyRingElem(a::Int) - z = ZZPolyRingElem() - ccall((:fmpz_poly_set_si, libflint), Nothing, (Ref{ZZPolyRingElem}, Int), z, a) - return z - end - - function ZZPolyRingElem(a::ZZRingElem) - z = ZZPolyRingElem() - ccall((:fmpz_poly_set_fmpz, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZRingElem}), z, a) - return z - end - - function ZZPolyRingElem(a::ZZPolyRingElem) - z = ZZPolyRingElem() - ccall((:fmpz_poly_set, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}), z, a) - return z - end + ZZPolyRingElem(a::Integer) = set!(ZZPolyRingElem(), a) + ZZPolyRingElem(a::ZZRingElem) = set!(ZZPolyRingElem(), a) + ZZPolyRingElem(a::ZZPolyRingElem) = set!(ZZPolyRingElem(), a) end function _fmpz_poly_clear_fn(a::ZZPolyRingElem) @@ -346,51 +329,25 @@ mutable struct QQPolyRingElem <: PolyRingElem{QQFieldElem} return z end - function QQPolyRingElem(a::Vector{QQFieldElem}) + function QQPolyRingElem(a::Vector{<:Union{Integer,Rational,ZZRingElem,QQFieldElem}}) z = new() ccall((:fmpq_poly_init2, libflint), Nothing, (Ref{QQPolyRingElem}, Int), z, length(a)) for i = 1:length(a) - ccall((:fmpq_poly_set_coeff_fmpq, libflint), Nothing, - (Ref{QQPolyRingElem}, Int, Ref{QQFieldElem}), z, i - 1, a[i]) + setcoeff!(z, i - 1, a[i]) end finalizer(_fmpq_poly_clear_fn, z) return z end - function QQPolyRingElem(a::Int) - z = QQPolyRingElem() - ccall((:fmpq_poly_set_si, libflint), Nothing, (Ref{QQPolyRingElem}, Int), z, a) - return z - end + QQPolyRingElem(a::Integer) = set!(QQPolyRingElem(), a) + QQPolyRingElem(a::Rational) = set!(QQPolyRingElem(), a) - function QQPolyRingElem(a::ZZRingElem) - z = QQPolyRingElem() - ccall((:fmpq_poly_set_fmpz, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{ZZRingElem}), z, a) - return z - end - - function QQPolyRingElem(a::QQFieldElem) - z = QQPolyRingElem() - ccall((:fmpq_poly_set_fmpq, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQFieldElem}), z, a) - return z - end - - function QQPolyRingElem(a::ZZPolyRingElem) - z = QQPolyRingElem() - ccall((:fmpq_poly_set_fmpz_poly, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{ZZPolyRingElem}), z, a) - return z - end + QQPolyRingElem(a::ZZRingElem) = set!(QQPolyRingElem(), a) + QQPolyRingElem(a::QQFieldElem) = set!(QQPolyRingElem(), a) - function QQPolyRingElem(a::QQPolyRingElem) - z = QQPolyRingElem() - ccall((:fmpq_poly_set, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}), z, a) - return z - end + QQPolyRingElem(a::ZZPolyRingElem) = set!(QQPolyRingElem(), a) + QQPolyRingElem(a::QQPolyRingElem) = set!(QQPolyRingElem(), a) end function _fmpq_poly_clear_fn(a::QQPolyRingElem) @@ -6302,6 +6259,9 @@ const FlintPuiseuxSeriesFieldElemOrPtr{T <: RingElem} = Union{FlintPuiseuxSeries const PadicFieldElemOrPtr = Union{PadicFieldElem, Ref{PadicFieldElem}, Ptr{PadicFieldElem}} const QadicFieldElemOrPtr = Union{QadicFieldElem, Ref{QadicFieldElem}, Ptr{QadicFieldElem}} +const IntegerUnionOrPtr = Union{Integer, ZZRingElemOrPtr} +const RationalUnionOrPtr = Union{Integer, ZZRingElemOrPtr, Rational, QQFieldElemOrPtr} + ############################################################################### # # Docstrings for the systematically added types in this file diff --git a/src/flint/fmpq_mat.jl b/src/flint/fmpq_mat.jl index 0527380eb..6acb2fec7 100644 --- a/src/flint/fmpq_mat.jl +++ b/src/flint/fmpq_mat.jl @@ -213,7 +213,7 @@ end transpose!(A::Union{ZZMatrix,QQMatrix}) = is_square(A) ? transpose!(A, A) : transpose(A) -function transpose!(A::QQMatrix, B::QQMatrix) +function transpose!(A::QQMatrixOrPtr, B::QQMatrixOrPtr) ccall((:fmpq_mat_transpose, libflint), Nothing, (Ref{QQMatrix}, Ref{QQMatrix}), A, B) return A @@ -225,7 +225,7 @@ end # ############################################################################### -function swap_rows!(x::QQMatrix, i::Int, j::Int) +function swap_rows!(x::QQMatrixOrPtr, i::Int, j::Int) ccall((:fmpq_mat_swap_rows, libflint), Nothing, (Ref{QQMatrix}, Ptr{Nothing}, Int, Int), x, C_NULL, i - 1, j - 1) return x @@ -237,7 +237,7 @@ function swap_rows(x::QQMatrix, i::Int, j::Int) return swap_rows!(y, i, j) end -function swap_cols!(x::QQMatrix, i::Int, j::Int) +function swap_cols!(x::QQMatrixOrPtr, i::Int, j::Int) ccall((:fmpq_mat_swap_cols, libflint), Nothing, (Ref{QQMatrix}, Ptr{Nothing}, Int, Int), x, C_NULL, i - 1, j - 1) return x @@ -249,7 +249,7 @@ function swap_cols(x::QQMatrix, i::Int, j::Int) return swap_cols!(y, i, j) end -function reverse_rows!(x::QQMatrix) +function reverse_rows!(x::QQMatrixOrPtr) ccall((:fmpq_mat_invert_rows, libflint), Nothing, (Ref{QQMatrix}, Ptr{Nothing}), x, C_NULL) return x @@ -257,7 +257,7 @@ end reverse_rows(x::QQMatrix) = reverse_rows!(deepcopy(x)) -function reverse_cols!(x::QQMatrix) +function reverse_cols!(x::QQMatrixOrPtr) ccall((:fmpq_mat_invert_cols, libflint), Nothing, (Ref{QQMatrix}, Ptr{Nothing}), x, C_NULL) return x @@ -274,26 +274,19 @@ reverse_cols(x::QQMatrix) = reverse_cols!(deepcopy(x)) function +(x::QQMatrix, y::QQMatrix) check_parent(x, y) z = similar(x) - add!(z, x, y) - return z + return add!(z, x, y) end function -(x::QQMatrix, y::QQMatrix) check_parent(x, y) z = similar(x) - ccall((:fmpq_mat_sub, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQMatrix}), - z, x, y) - return z + return sub!(z, x, y) end function *(x::QQMatrix, y::QQMatrix) ncols(x) != nrows(y) && error("Incompatible matrix dimensions") z = similar(x, nrows(x), ncols(y)) - ccall((:fmpq_mat_mul, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQMatrix}), - z, x, y) - return z + return mul!(z, x, y) end ############################################################################### @@ -302,90 +295,39 @@ end # ############################################################################### -function *(x::ZZRingElem, y::QQMatrix) - z = similar(y) - ccall((:fmpq_mat_scalar_mul_fmpz, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}, Ref{ZZRingElem}), z, y, x) - return z -end - -function *(x::QQFieldElem, y::QQMatrix) - z = similar(y) - ccall((:fmpq_mat_scalar_mul_fmpz, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQFieldElem}), z, y, numerator(x)) - ccall((:fmpq_mat_scalar_div_fmpz, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQFieldElem}), z, z, denominator(x)) - return z -end - -*(x::QQMatrix, y::QQFieldElem) = y*x - -*(x::QQMatrix, y::ZZRingElem) = y*x - -*(x::Integer, y::QQMatrix) = ZZRingElem(x)*y - -*(x::QQMatrix, y::Integer) = ZZRingElem(y)*x - -*(x::Rational, y::QQMatrix) = QQFieldElem(x)*y - -*(x::QQMatrix, y::Rational) = QQFieldElem(y)*x - -for T in [Integer, ZZRingElem, QQFieldElem] +for T in [Integer, Rational, ZZRingElem, QQFieldElem] @eval begin - function +(x::QQMatrix, y::$T) - z = deepcopy(x) - for i = 1:min(nrows(x), ncols(x)) - z[i, i] += y + *(mat::QQMatrix, scalar::$T) = mul!(similar(mat), mat, scalar) + *(scalar::$T, mat::QQMatrix) = mul!(similar(mat), mat, scalar) + + function +(mat::QQMatrix, scalar::$T) + z = deepcopy(mat) + for i = 1:min(nrows(mat), ncols(mat)) + add!(mat_entry_ptr(z, i, i), scalar) end return z end - +(x::$T, y::QQMatrix) = y + x + +(scalar::$T, mat::QQMatrix) = mat + scalar - function -(x::QQMatrix, y::$T) - z = deepcopy(x) - for i = 1:min(nrows(x), ncols(x)) - z[i, i] -= y + function -(mat::QQMatrix, scalar::$T) + z = deepcopy(mat) + for i = 1:min(nrows(mat), ncols(mat)) + sub!(mat_entry_ptr(z, i, i), scalar) end return z end - function -(x::$T, y::QQMatrix) - z = -y - for i = 1:min(nrows(y), ncols(y)) - z[i, i] += x + function -(scalar::$T, mat::QQMatrix) + z = -mat + for i = 1:min(nrows(mat), ncols(mat)) + add!(mat_entry_ptr(z, i, i), scalar) end return z end end end -function +(x::QQMatrix, y::Rational) - z = deepcopy(x) - for i = 1:min(nrows(x), ncols(x)) - z[i, i] += y - end - return z -end - -+(x::Rational, y::QQMatrix) = y + x - -function -(x::QQMatrix, y::Rational) - z = deepcopy(x) - for i = 1:min(nrows(x), ncols(x)) - z[i, i] -= y - end - return z -end - -function -(x::Rational, y::QQMatrix) - z = -y - for i = 1:min(nrows(y), ncols(y)) - z[i, i] += x - end - return z -end - ############################################################################### # # Comparisons @@ -424,9 +366,9 @@ end ==(x::Integer, y::QQMatrix) = y == x -==(x::QQMatrix, y::Rational{T}) where T <: Union{Int, BigInt} = x == QQFieldElem(y) +==(x::QQMatrix, y::Rational) = x == QQFieldElem(y) -==(x::Rational{T}, y::QQMatrix) where T <: Union{Int, BigInt} = y == x +==(x::Rational, y::QQMatrix) = y == x ############################################################################### # @@ -462,23 +404,19 @@ end function divexact(x::QQMatrix, y::QQFieldElem; check::Bool=true) z = similar(x) - ccall((:fmpq_mat_scalar_div_fmpz, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}, Ref{ZZRingElem}), z, x, numerator(y)) - ccall((:fmpq_mat_scalar_mul_fmpz, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}, Ref{ZZRingElem}), z, z, denominator(y)) + divexact!(z, x, y) return z end function divexact(x::QQMatrix, y::ZZRingElem; check::Bool=true) z = similar(x) - ccall((:fmpq_mat_scalar_div_fmpz, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}, Ref{ZZRingElem}), z, x, y) + divexact!(z, x, y) return z end divexact(x::QQMatrix, y::Integer; check::Bool=true) = divexact(x, ZZRingElem(y); check=check) -divexact(x::QQMatrix, y::Rational{T}; check::Bool=true) where T <: Union{Int, BigInt} = divexact(x, QQFieldElem(y); check=check) +divexact(x::QQMatrix, y::Rational; check::Bool=true) = divexact(x, QQFieldElem(y); check=check) ############################################################################### # @@ -723,88 +661,112 @@ end # ############################################################################### -function zero!(z::QQMatrix) +function zero!(z::QQMatrixOrPtr) ccall((:fmpq_mat_zero, libflint), Nothing, (Ref{QQMatrix},), z) return z end -function one!(z::QQMatrix) +function one!(z::QQMatrixOrPtr) ccall((:fmpq_mat_one, libflint), Nothing, (Ref{QQMatrix},), z) return z end -function neg!(z::QQMatrix, a::QQMatrix) +function neg!(z::QQMatrixOrPtr, a::QQMatrixOrPtr) ccall((:fmpq_mat_neg, libflint), Nothing, (Ref{QQMatrix}, Ref{QQMatrix}), z, a) return z end -function mul!(z::QQMatrix, x::QQMatrix, y::QQMatrix) +function add!(z::QQMatrixOrPtr, x::QQMatrixOrPtr, y::QQMatrixOrPtr) + ccall((:fmpq_mat_add, libflint), Nothing, + (Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQMatrix}), z, x, y) + return z +end + +function sub!(z::QQMatrixOrPtr, x::QQMatrixOrPtr, y::QQMatrixOrPtr) + ccall((:fmpq_mat_sub, libflint), Nothing, + (Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQMatrix}), z, x, y) + return z +end + +# +# matrix x matrix +# +function mul!(z::QQMatrixOrPtr, x::QQMatrixOrPtr, y::QQMatrixOrPtr) ccall((:fmpq_mat_mul, libflint), Nothing, (Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQMatrix}), z, x, y) return z end -function mul!(z::Vector{QQFieldElem}, a::Vector{QQFieldElem}, b::QQMatrix) +# +# matrix x vector, vector x matrix +# +function mul!(z::Vector{QQFieldElem}, a::Vector{QQFieldElem}, b::QQMatrixOrPtr) ccall((:fmpq_mat_fmpq_vec_mul_ptr, libflint), Nothing, (Ptr{Ref{QQFieldElem}}, Ptr{Ref{QQFieldElem}}, Int, Ref{QQMatrix}), z, a, length(a), b) return z end -function mul!(z::Vector{QQFieldElem}, a::QQMatrix, b::Vector{QQFieldElem}) +function mul!(z::Vector{QQFieldElem}, a::QQMatrixOrPtr, b::Vector{QQFieldElem}) ccall((:fmpq_mat_mul_fmpq_vec_ptr, libflint), Nothing, (Ptr{Ref{QQFieldElem}}, Ref{QQMatrix}, Ptr{Ref{QQFieldElem}}, Int), z, a, b, length(b)) return z end -function mul!(z::Vector{QQFieldElem}, a::Vector{ZZRingElem}, b::QQMatrix) +function mul!(z::Vector{QQFieldElem}, a::Vector{ZZRingElem}, b::QQMatrixOrPtr) ccall((:fmpq_mat_fmpz_vec_mul_ptr, libflint), Nothing, (Ptr{Ref{QQFieldElem}}, Ptr{Ref{ZZRingElem}}, Int, Ref{QQMatrix}), z, a, length(a), b) return z end -function mul!(z::Vector{QQFieldElem}, a::QQMatrix, b::Vector{ZZRingElem}) +function mul!(z::Vector{QQFieldElem}, a::QQMatrixOrPtr, b::Vector{ZZRingElem}) ccall((:fmpq_mat_mul_fmpz_vec_ptr, libflint), Nothing, (Ptr{Ref{QQFieldElem}}, Ref{QQMatrix}, Ptr{Ref{ZZRingElem}}, Int), z, a, b, length(b)) return z end -function add!(z::QQMatrix, x::QQMatrix, y::QQMatrix) - ccall((:fmpq_mat_add, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQMatrix}), z, x, y) - return z -end - -function mul!(z::QQMatrix, y::QQMatrix, x::QQFieldElem) +# +# matrix x scalar, scalar x matrix +# +function mul!(z::QQMatrixOrPtr, a::QQMatrixOrPtr, b::QQFieldElemOrPtr) ccall((:fmpq_mat_scalar_mul_fmpq, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQFieldElem}), z, y, x) + (Ref{QQMatrix}, Ref{QQMatrix}, Ref{QQFieldElem}), z, a, b) return z end -mul!(z::QQMatrix, y::QQFieldElem, x::QQMatrix) = mul!(z, x, y) - -mul!(x::QQMatrix, y::QQFieldElem) = mul!(x, x, y) - -function mul!(z::QQMatrix, y::QQMatrix, x::ZZRingElem) +function mul!(z::QQMatrixOrPtr, a::QQMatrixOrPtr, b::ZZRingElemOrPtr) ccall((:fmpq_mat_scalar_mul_fmpz, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}, Ref{ZZRingElem}), z, y, x) + (Ref{QQMatrix}, Ref{QQMatrix}, Ref{ZZRingElem}), z, a, b) return z end -# delegate everything integral to mul!(::QQMatrix, ::QQMatrix, ::ZZRingElem) -mul!(z::QQMatrix, y::IntegerUnion, x::QQMatrix) = mul!(z, x, y) +mul!(z::QQMatrixOrPtr, a::QQMatrixOrPtr, b::Integer) = mul!(z, a, ZZ(b)) +mul!(z::QQMatrixOrPtr, a::QQMatrixOrPtr, b::Rational) = mul!(z, a, QQ(b)) + +mul!(z::QQMatrixOrPtr, a::RationalUnionOrPtr, b::QQMatrixOrPtr) = mul!(z, b, a) -mul!(x::QQMatrix, y::IntegerUnion) = mul!(x, x, y) -mul!(z::QQMatrix, y::QQMatrix, x::Integer) = mul!(z, y, ZZ(x)) +function divexact!(z::QQMatrixOrPtr, x::QQMatrixOrPtr, y::QQFieldElemOrPtr) + GC.@preserve y begin + divexact!(z, x, _num_ptr(y)) + mul!(z, z, _den_ptr(y)) + end + return z +end -function Generic.add_one!(a::QQMatrix, i::Int, j::Int) +function divexact!(z::QQMatrixOrPtr, x::QQMatrixOrPtr, y::ZZRingElemOrPtr) + ccall((:fmpq_mat_scalar_div_fmpz, libflint), Nothing, + (Ref{QQMatrix}, Ref{QQMatrix}, Ref{ZZRingElem}), z, x, y) + return z +end + +function Generic.add_one!(a::QQMatrixOrPtr, i::Int, j::Int) @boundscheck _checkbounds(a, i, j) GC.@preserve a begin x = mat_entry_ptr(a, i, j) @@ -909,7 +871,7 @@ promote_rule(::Type{QQMatrix}, ::Type{QQFieldElem}) = QQMatrix promote_rule(::Type{QQMatrix}, ::Type{ZZRingElem}) = QQMatrix -promote_rule(::Type{QQMatrix}, ::Type{Rational{T}}) where T <: Union{Int, BigInt} = QQMatrix +promote_rule(::Type{QQMatrix}, ::Type{<:Rational}) = QQMatrix ############################################################################### # @@ -989,9 +951,7 @@ function identity_matrix(R::QQField, n::Int) if n < 0 error("dimension must not be negative") end - z = QQMatrix(n, n) - ccall((:fmpq_mat_one, libflint), Nothing, (Ref{QQMatrix}, ), z) - return z + return one!(QQMatrix(n, n)) end ################################################################################ diff --git a/src/flint/fmpq_poly.jl b/src/flint/fmpq_poly.jl index 621873fe4..7ffec53ad 100644 --- a/src/flint/fmpq_poly.jl +++ b/src/flint/fmpq_poly.jl @@ -131,28 +131,19 @@ canonical_unit(a::QQPolyRingElem) = canonical_unit(leading_coefficient(a)) function +(x::QQPolyRingElem, y::QQPolyRingElem) check_parent(x, y) z = parent(x)() - ccall((:fmpq_poly_add, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Ref{QQPolyRingElem}), - z, x, y) - return z + return add!(z, x, y) end function -(x::QQPolyRingElem, y::QQPolyRingElem) check_parent(x, y) z = parent(x)() - ccall((:fmpq_poly_sub, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Ref{QQPolyRingElem}), - z, x, y) - return z + return sub!(z, x, y) end function *(x::QQPolyRingElem, y::QQPolyRingElem) check_parent(x, y) z = parent(x)() - ccall((:fmpq_poly_mul, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Ref{QQPolyRingElem}), - z, x, y) - return z + return mul!(z, x, y) end ############################################################################### @@ -161,126 +152,15 @@ end # ############################################################################### -function *(x::Int, y::QQPolyRingElem) - z = parent(y)() - ccall((:fmpq_poly_scalar_mul_si, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Int), z, y, x) - return z -end - -function *(x::ZZRingElem, y::QQPolyRingElem) - z = parent(y)() - ccall((:fmpq_poly_scalar_mul_fmpz, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Ref{ZZRingElem}), z, y, x) - return z -end - -function *(x::QQFieldElem, y::QQPolyRingElem) - z = parent(y)() - ccall((:fmpq_poly_scalar_mul_fmpq, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Ref{QQFieldElem}), z, y, x) - return z -end - -function +(x::QQPolyRingElem, y::Int) - z = parent(x)() - ccall((:fmpq_poly_add_si, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Int), z, x, y) - return z -end - -function +(x::QQPolyRingElem, y::ZZRingElem) - z = parent(x)() - ccall((:fmpq_poly_add_fmpz, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Ref{ZZRingElem}), z, x, y) - return z -end - -function +(x::QQPolyRingElem, y::QQFieldElem) - z = parent(x)() - ccall((:fmpq_poly_add_fmpq, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Ref{QQFieldElem}), z, x, y) - return z -end - -function -(x::QQPolyRingElem, y::Int) - z = parent(x)() - ccall((:fmpq_poly_sub_si, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Int), z, x, y) - return z -end - -function -(x::QQPolyRingElem, y::ZZRingElem) - z = parent(x)() - ccall((:fmpq_poly_sub_fmpz, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Ref{ZZRingElem}), z, x, y) - return z -end - -function -(x::QQPolyRingElem, y::QQFieldElem) - z = parent(x)() - ccall((:fmpq_poly_sub_fmpq, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Ref{QQFieldElem}), z, x, y) - return z -end - -function -(x::Int, y::QQPolyRingElem) - z = parent(y)() - ccall((:fmpq_poly_si_sub, libflint), Nothing, - (Ref{QQPolyRingElem}, Int, Ref{QQPolyRingElem}), z, x, y) - return z -end - -function -(x::ZZRingElem, y::QQPolyRingElem) - z = parent(y)() - ccall((:fmpq_poly_fmpz_sub, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{ZZRingElem}, Ref{QQPolyRingElem}), z, x, y) - return z -end - -function -(x::QQFieldElem, y::QQPolyRingElem) - z = parent(y)() - ccall((:fmpq_poly_fmpq_sub, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQFieldElem}, Ref{QQPolyRingElem}), z, x, y) - return z +for T in [Integer, ZZRingElem, Rational, QQFieldElem] + for (jop, cop) in ((:+,:add!), (:-,:sub!), (:*,:mul!)) + @eval begin + $jop(a::QQPolyRingElem, b::$T) = $cop(similar(a), a, b) + $jop(a::$T, b::QQPolyRingElem) = $cop(similar(b), a, b) + end + end end -+(x::Int, y::QQPolyRingElem) = y + x - -+(x::ZZRingElem, y::QQPolyRingElem) = y + x - -+(x::QQFieldElem, y::QQPolyRingElem) = y + x - -*(x::QQPolyRingElem, y::Int) = y*x - -*(x::QQPolyRingElem, y::ZZRingElem) = y*x - -*(x::QQPolyRingElem, y::QQFieldElem) = y*x - -+(x::Integer, y::QQPolyRingElem) = y + flintify(x) - --(x::Integer, y::QQPolyRingElem) = flintify(x) - y - -*(x::Integer, y::QQPolyRingElem) = flintify(x)*y - -+(x::QQPolyRingElem, y::Integer) = x + flintify(y) - --(x::QQPolyRingElem, y::Integer) = x - flintify(y) - -*(x::QQPolyRingElem, y::Integer) = flintify(y)*x - -+(x::Rational, y::QQPolyRingElem) = QQFieldElem(x) + y - --(x::Rational, y::QQPolyRingElem) = QQFieldElem(x) - y - -*(x::Rational, y::QQPolyRingElem) = QQFieldElem(x) * y - -+(x::QQPolyRingElem, y::Rational) = x + QQFieldElem(y) - --(x::QQPolyRingElem, y::Rational) = x - QQFieldElem(y) - -*(x::QQPolyRingElem, y::Rational) = x * QQFieldElem(y) - ############################################################################### # # Powering @@ -772,54 +652,178 @@ end # ############################################################################### -function zero!(z::QQPolyRingElem) - ccall((:fmpq_poly_zero, libflint), Nothing, - (Ref{QQPolyRingElem},), z) +function zero!(z::QQPolyRingElemOrPtr) + @ccall libflint.fmpq_poly_zero(z::Ref{QQPolyRingElem})::Nothing return z end -function one!(z::QQPolyRingElem) - ccall((:fmpq_poly_one, libflint), Nothing, - (Ref{QQPolyRingElem},), z) +function one!(z::QQPolyRingElemOrPtr) + @ccall libflint.fmpq_poly_one(z::Ref{QQPolyRingElem})::Nothing return z end -function neg!(z::QQPolyRingElem, a::QQPolyRingElem) - ccall((:fmpq_poly_neg, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}), z, a) +function neg!(z::QQPolyRingElemOrPtr, a::QQPolyRingElemOrPtr) + @ccall libflint.fmpq_poly_neg(z::Ref{QQPolyRingElem}, a::Ref{QQPolyRingElem})::Nothing return z end -function fit!(z::QQPolyRingElem, n::Int) - ccall((:fmpq_poly_fit_length, libflint), Nothing, - (Ref{QQPolyRingElem}, Int), z, n) +function fit!(z::QQPolyRingElemOrPtr, n::Int) + @ccall libflint.fmpq_poly_fit_length(z::Ref{QQPolyRingElem}, n::Int)::Nothing return nothing end -function setcoeff!(z::QQPolyRingElem, n::Int, x::ZZRingElem) - ccall((:fmpq_poly_set_coeff_fmpz, libflint), Nothing, - (Ref{QQPolyRingElem}, Int, Ref{ZZRingElem}), z, n, x) +# + +function set!(z::QQPolyRingElemOrPtr, a::QQPolyRingElemOrPtr) + @ccall libflint.fmpq_poly_set(z::Ref{QQPolyRingElem}, a::Ref{QQPolyRingElem})::Nothing return z end -function setcoeff!(z::QQPolyRingElem, n::Int, x::QQFieldElem) - ccall((:fmpq_poly_set_coeff_fmpq, libflint), Nothing, - (Ref{QQPolyRingElem}, Int, Ref{QQFieldElem}), z, n, x) +function set!(z::QQPolyRingElemOrPtr, a::ZZPolyRingElemOrPtr) + @ccall libflint.fmpq_poly_set_fmpz_poly(z::Ref{QQPolyRingElem}, a::Ref{ZZPolyRingElem})::Nothing return z end -function mul!(z::QQPolyRingElem, x::QQPolyRingElem, y::QQPolyRingElem) - ccall((:fmpq_poly_mul, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Ref{QQPolyRingElem}), z, x, y) +function set!(z::QQPolyRingElemOrPtr, a::QQFieldElemOrPtr) + @ccall libflint.fmpq_poly_set_fmpq(z::Ref{QQPolyRingElem}, a::Ref{QQFieldElem})::Nothing return z end -function add!(z::QQPolyRingElem, x::QQPolyRingElem, y::QQPolyRingElem) - ccall((:fmpq_poly_add, libflint), Nothing, - (Ref{QQPolyRingElem}, Ref{QQPolyRingElem}, Ref{QQPolyRingElem}), z, x, y) +function set!(z::QQPolyRingElemOrPtr, a::ZZRingElemOrPtr) + @ccall libflint.fmpq_poly_set_fmpz(z::Ref{QQPolyRingElem}, a::Ref{ZZRingElem})::Nothing + return z +end + +function set!(z::QQPolyRingElemOrPtr, a::Int) + @ccall libflint.fmpq_poly_set_si(z::Ref{QQPolyRingElem}, a::Int)::Nothing return z end +function set!(z::QQPolyRingElemOrPtr, a::UInt) + @ccall libflint.fmpq_poly_set_ui(z::Ref{QQPolyRingElem}, a::UInt)::Nothing + return z +end + +set!(z::QQPolyRingElemOrPtr, a::Union{Integer, Rational}) = set!(z, flintify(a)) + +# + +function setcoeff!(z::QQPolyRingElemOrPtr, n::Int, x::ZZRingElemOrPtr) + @ccall libflint.fmpq_poly_set_coeff_fmpz(z::Ref{QQPolyRingElem}, n::Int, x::Ref{ZZRingElem})::Nothing + return z +end + +function setcoeff!(z::QQPolyRingElemOrPtr, n::Int, x::QQFieldElemOrPtr) + @ccall libflint.fmpq_poly_set_coeff_fmpq(z::Ref{QQPolyRingElem}, n::Int, x::Ref{QQFieldElem})::Nothing + return z +end + +function setcoeff!(z::QQPolyRingElemOrPtr, n::Int, x::UInt) + @ccall libflint.fmpq_poly_set_coeff_ui(z::Ref{QQPolyRingElem}, n::Int, x::UInt)::Nothing + return z +end + +function setcoeff!(z::QQPolyRingElemOrPtr, n::Int, x::Int) + @ccall libflint.fmpq_poly_set_coeff_si(z::Ref{QQPolyRingElem}, n::Int, x::Int)::Nothing + return z +end + +setcoeff!(z::QQPolyRingElemOrPtr, n::Int, x::Union{Integer, Rational}) = setcoeff!(z, n, flintify(x)) + +# + +function add!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::QQPolyRingElemOrPtr) + @ccall libflint.fmpq_poly_add(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Ref{QQPolyRingElem})::Nothing + return z +end + +function add!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::QQFieldElemOrPtr) + @ccall libflint.fmpq_poly_add_fmpq(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Ref{QQFieldElem})::Nothing + return z +end + +function add!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::ZZRingElemOrPtr) + @ccall libflint.fmpq_poly_add_fmpz(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Ref{ZZRingElem})::Nothing + return z +end + +function add!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::Int) + @ccall libflint.fmpq_poly_add_si(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Int)::Nothing + return z +end + +add!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::Union{Integer, Rational}) = add!(z, x, flintify(y)) + +add!(z::QQPolyRingElemOrPtr, x::RationalUnionOrPtr, y::QQPolyRingElemOrPtr) = add!(z, y, x) + +# + +function sub!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::QQPolyRingElemOrPtr) + @ccall libflint.fmpq_poly_sub(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Ref{QQPolyRingElem})::Nothing + return z +end + + +function sub!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::QQFieldElemOrPtr) + @ccall libflint.fmpq_poly_sub_fmpq(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Ref{QQFieldElem})::Nothing + return z +end + +function sub!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::ZZRingElemOrPtr) + @ccall libflint.fmpq_poly_sub_fmpz(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Ref{ZZRingElem})::Nothing + return z +end + +function sub!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::Int) + @ccall libflint.fmpq_poly_sub_si(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Int)::Nothing + return z +end + +function sub!(z::QQPolyRingElemOrPtr, x::QQFieldElemOrPtr, y::QQPolyRingElemOrPtr) + @ccall libflint.fmpq_poly_fmpq_sub(z::Ref{QQPolyRingElem}, x::Ref{QQFieldElem}, y::Ref{QQPolyRingElem})::Nothing + return z +end + +function sub!(z::QQPolyRingElemOrPtr, x::ZZRingElemOrPtr, y::QQPolyRingElemOrPtr) + @ccall libflint.fmpq_poly_fmpz_sub(z::Ref{QQPolyRingElem}, x::Ref{ZZRingElem}, y::Ref{QQPolyRingElem})::Nothing + return z +end + +function sub!(z::QQPolyRingElemOrPtr, x::Int, y::QQPolyRingElemOrPtr) + @ccall libflint.fmpq_poly_si_sub(z::Ref{QQPolyRingElem}, x::Int, y::Ref{QQPolyRingElem})::Nothing + return z +end + +sub!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::Union{Integer, Rational}) = sub!(z, x, flintify(y)) + +sub!(z::QQPolyRingElemOrPtr, x::Union{Integer, Rational}, y::QQPolyRingElemOrPtr) = sub!(z, flintify(x), y) + +# + +function mul!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::QQPolyRingElemOrPtr) + @ccall libflint.fmpq_poly_mul(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Ref{QQPolyRingElem})::Nothing + return z +end + +function mul!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::QQFieldElemOrPtr) + @ccall libflint.fmpq_poly_scalar_mul_fmpq(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Ref{QQFieldElem})::Nothing + return z +end + +function mul!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::ZZRingElemOrPtr) + @ccall libflint.fmpq_poly_scalar_mul_fmpz(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Ref{ZZRingElem})::Nothing + return z +end + +function mul!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::Int) + @ccall libflint.fmpq_poly_scalar_mul_si(z::Ref{QQPolyRingElem}, x::Ref{QQPolyRingElem}, y::Int)::Nothing + return z +end + +mul!(z::QQPolyRingElemOrPtr, x::QQPolyRingElemOrPtr, y::Union{Integer, Rational}) = mul!(z, x, flintify(y)) + +mul!(z::QQPolyRingElemOrPtr, x::RationalUnionOrPtr, y::QQPolyRingElemOrPtr) = mul!(z, y, x) + ############################################################################### # # Promotions diff --git a/src/flint/fmpz_mat.jl b/src/flint/fmpz_mat.jl index 87076cc9a..5a2da69ae 100644 --- a/src/flint/fmpz_mat.jl +++ b/src/flint/fmpz_mat.jl @@ -312,66 +312,37 @@ end # ############################################################################### -function *(x::Int, y::ZZMatrix) - z = similar(y) - ccall((:fmpz_mat_scalar_mul_si, libflint), Nothing, - (Ref{ZZMatrix}, Ref{ZZMatrix}, Int), z, y, x) - return z -end - -function *(x::ZZRingElem, y::ZZMatrix) - z = similar(y) - ccall((:fmpz_mat_scalar_mul_fmpz, libflint), Nothing, - (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZRingElem}), z, y, x) - return z -end - -*(x::ZZMatrix, y::Int) = y*x - -*(x::ZZMatrix, y::ZZRingElem) = y*x - -*(x::Integer, y::ZZMatrix) = flintify(x)*y - -*(x::ZZMatrix, y::Integer) = flintify(y)*x - -function +(x::ZZMatrix, y::Integer) - z = deepcopy(x) - for i = 1:min(nrows(x), ncols(x)) - z[i, i] += y - end - return z -end - -function +(x::ZZMatrix, y::ZZRingElem) - z = deepcopy(x) - for i = 1:min(nrows(x), ncols(x)) - z[i, i] = add!(z[i, i], y) - end - return z -end - -+(x::Integer, y::ZZMatrix) = y + x - -+(x::ZZRingElem, y::ZZMatrix) = y + x - --(x::ZZMatrix, y::Integer) = x + (-y) +for T in [Integer, ZZRingElem] + @eval begin + *(mat::ZZMatrix, scalar::$T) = mul!(similar(mat), mat, scalar) + *(scalar::$T, mat::ZZMatrix) = mul!(similar(mat), mat, scalar) + + function +(mat::ZZMatrix, scalar::$T) + z = deepcopy(mat) + for i = 1:min(nrows(mat), ncols(mat)) + add!(mat_entry_ptr(z, i, i), scalar) + end + return z + end --(x::ZZMatrix, y::ZZRingElem) = x + (-y) + +(scalar::$T, mat::ZZMatrix) = mat + scalar -function -(x::Integer, y::ZZMatrix) - z = -y - for i = 1:min(nrows(y), ncols(y)) - z[i, i] += x - end - return z -end + function -(mat::ZZMatrix, scalar::$T) + z = deepcopy(mat) + for i = 1:min(nrows(mat), ncols(mat)) + sub!(mat_entry_ptr(z, i, i), scalar) + end + return z + end -function -(x::ZZRingElem, y::ZZMatrix) - z = -y - for i = 1:min(nrows(y), ncols(y)) - z[i, i] = add!(z[i, i], x) + function -(scalar::$T, mat::ZZMatrix) + z = -mat + for i = 1:min(nrows(mat), ncols(mat)) + add!(mat_entry_ptr(z, i, i), scalar) + end + return z + end end - return z end ############################################################################### @@ -1810,81 +1781,91 @@ end # ############################################################################### -function zero!(z::ZZMatrix) +function zero!(z::ZZMatrixOrPtr) ccall((:fmpz_mat_zero, libflint), Nothing, (Ref{ZZMatrix},), z) return z end -function one!(z::ZZMatrix) +function one!(z::ZZMatrixOrPtr) ccall((:fmpz_mat_one, libflint), Nothing, (Ref{ZZMatrix},), z) return z end -function neg!(z::ZZMatrix, w::ZZMatrix) +function neg!(z::ZZMatrixOrPtr, w::ZZMatrixOrPtr) ccall((:fmpz_mat_neg, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}), z, w) return z end -function add!(z::ZZMatrix, x::ZZMatrix, y::ZZMatrix) +function add!(z::ZZMatrixOrPtr, x::ZZMatrixOrPtr, y::ZZMatrixOrPtr) ccall((:fmpz_mat_add, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZMatrix}), z, x, y) return z end -function sub!(z::ZZMatrix, x::ZZMatrix, y::ZZMatrix) +function sub!(z::ZZMatrixOrPtr, x::ZZMatrixOrPtr, y::ZZMatrixOrPtr) ccall((:fmpz_mat_sub, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZMatrix}), z, x, y) return z end -function mul!(z::ZZMatrix, x::ZZMatrix, y::ZZMatrix) +function mul!(z::ZZMatrixOrPtr, x::ZZMatrixOrPtr, y::ZZMatrixOrPtr) ccall((:fmpz_mat_mul, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZMatrix}), z, x, y) return z end -function mul!(y::ZZMatrix, x::Int) - mul!(y, y, x) -end - -function mul!(z::ZZMatrix, y::ZZMatrix, x::Int) +function mul!(z::ZZMatrixOrPtr, a::ZZMatrixOrPtr, b::Int) ccall((:fmpz_mat_scalar_mul_si, libflint), Nothing, - (Ref{ZZMatrix}, Ref{ZZMatrix}, Int), z, y, x) + (Ref{ZZMatrix}, Ref{ZZMatrix}, Int), z, a, b) return z end -function mul!(y::ZZMatrix, x::ZZRingElem) - mul!(y, y, x) +function mul!(z::ZZMatrixOrPtr, a::ZZMatrixOrPtr, b::UInt) + ccall((:fmpz_mat_scalar_mul_ui, libflint), Nothing, + (Ref{ZZMatrix}, Ref{ZZMatrix}, UInt), z, a, b) + return z end -function mul!(z::ZZMatrix, y::ZZMatrix, x::ZZRingElem) +function mul!(z::ZZMatrixOrPtr, a::ZZMatrixOrPtr, b::ZZRingElemOrPtr) ccall((:fmpz_mat_scalar_mul_fmpz, libflint), Nothing, - (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZRingElem}), z, y, x) + (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZRingElem}), z, a, b) return z end -function addmul!(z::ZZMatrix, y::ZZMatrix, x::ZZRingElem) +mul!(z::ZZMatrixOrPtr, a::ZZMatrixOrPtr, b::Integer) = mul!(z, a, flintify(b)) +mul!(z::ZZMatrixOrPtr, a::IntegerUnionOrPtr, b::ZZMatrixOrPtr) = mul!(z, b, a) + +function addmul!(z::ZZMatrixOrPtr, a::ZZMatrixOrPtr, b::ZZRingElemOrPtr) ccall((:fmpz_mat_scalar_addmul_fmpz, libflint), Nothing, - (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZRingElem}), z, y, x) + (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZRingElem}), z, a, b) return z end -function addmul!(z::ZZMatrix, y::ZZMatrix, x::Int) +function addmul!(z::ZZMatrixOrPtr, a::ZZMatrixOrPtr, b::Int) ccall((:fmpz_mat_scalar_addmul_si, libflint), Nothing, - (Ref{ZZMatrix}, Ref{ZZMatrix}, Int), z, y, x) + (Ref{ZZMatrix}, Ref{ZZMatrix}, Int), z, a, b) return z end -function mul!(z::Vector{ZZRingElem}, a::ZZMatrix, b::Vector{ZZRingElem}) +function addmul!(z::ZZMatrixOrPtr, a::ZZMatrixOrPtr, b::UInt) + ccall((:fmpz_mat_scalar_addmul_ui, libflint), Nothing, + (Ref{ZZMatrix}, Ref{ZZMatrix}, UInt), z, a, b) + return z +end + +addmul!(z::ZZMatrixOrPtr, a::ZZMatrixOrPtr, b::Integer) = addmul!(z, a, flintify(b)) +addmul!(z::ZZMatrixOrPtr, a::IntegerUnionOrPtr, b::ZZMatrixOrPtr) = addmul!(z, b, a) + +function mul!(z::Vector{ZZRingElem}, a::ZZMatrixOrPtr, b::Vector{ZZRingElem}) ccall((:fmpz_mat_mul_fmpz_vec_ptr, libflint), Nothing, (Ptr{Ref{ZZRingElem}}, Ref{ZZMatrix}, Ptr{Ref{ZZRingElem}}, Int), z, a, b, length(b)) return z end -function mul!(z::Vector{ZZRingElem}, a::Vector{ZZRingElem}, b::ZZMatrix) +function mul!(z::Vector{ZZRingElem}, a::Vector{ZZRingElem}, b::ZZMatrixOrPtr) ccall((:fmpz_mat_fmpz_vec_mul_ptr, libflint), Nothing, (Ptr{Ref{ZZRingElem}}, Ptr{Ref{ZZRingElem}}, Int, Ref{ZZMatrix}), z, a, length(a), b) @@ -1895,9 +1876,7 @@ function Generic.add_one!(a::ZZMatrix, i::Int, j::Int) @boundscheck _checkbounds(a, i, j) GC.@preserve a begin x = mat_entry_ptr(a, i, j) - ccall((:fmpz_add_si, libflint), Nothing, - (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Int), - x, x, 1) + add!(x, 1) end return a end diff --git a/src/flint/fmpz_poly.jl b/src/flint/fmpz_poly.jl index 3934948b7..8dd3700db 100644 --- a/src/flint/fmpz_poly.jl +++ b/src/flint/fmpz_poly.jl @@ -126,28 +126,19 @@ canonical_unit(a::ZZPolyRingElem) = canonical_unit(leading_coefficient(a)) function +(x::ZZPolyRingElem, y::ZZPolyRingElem) check_parent(x, y) z = parent(x)() - ccall((:fmpz_poly_add, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}), - z, x, y) - return z + return add!(z, x, y) end function -(x::ZZPolyRingElem, y::ZZPolyRingElem) check_parent(x, y) z = parent(x)() - ccall((:fmpz_poly_sub, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}), - z, x, y) - return z + return sub!(z, x, y) end function *(x::ZZPolyRingElem, y::ZZPolyRingElem) check_parent(x, y) z = parent(x)() - ccall((:fmpz_poly_mul, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}), - z, x, y) - return z + return mul!(z, x, y) end ############################################################################### @@ -156,82 +147,15 @@ end # ############################################################################### -function *(x::Int, y::ZZPolyRingElem) - z = parent(y)() - ccall((:fmpz_poly_scalar_mul_si, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Int), z, y, x) - return z -end - -function *(x::ZZRingElem, y::ZZPolyRingElem) - z = parent(y)() - ccall((:fmpz_poly_scalar_mul_fmpz, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Ref{ZZRingElem}), z, y, x) - return z -end - -function +(x::ZZPolyRingElem, y::Int) - z = parent(x)() - ccall((:fmpz_poly_add_si, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Int), z, x, y) - return z -end - -function +(x::ZZPolyRingElem, y::ZZRingElem) - z = parent(x)() - ccall((:fmpz_poly_add_fmpz, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Ref{ZZRingElem}), z, x, y) - return z -end - -function -(x::ZZPolyRingElem, y::Int) - z = parent(x)() - ccall((:fmpz_poly_sub_si, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Int), z, x, y) - return z -end - -function -(x::ZZPolyRingElem, y::ZZRingElem) - z = parent(x)() - ccall((:fmpz_poly_sub_fmpz, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Ref{ZZRingElem}), z, x, y) - return z -end - -function -(x::Int, y::ZZPolyRingElem) - z = parent(y)() - ccall((:fmpz_poly_si_sub, libflint), Nothing, - (Ref{ZZPolyRingElem}, Int, Ref{ZZPolyRingElem}), z, x, y) - return z -end - -function -(x::ZZRingElem, y::ZZPolyRingElem) - z = parent(y)() - ccall((:fmpz_poly_fmpz_sub, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZRingElem}, Ref{ZZPolyRingElem}), z, x, y) - return z +for T in [Integer, ZZRingElem] + for (jop, cop) in ((:+,:add!), (:-,:sub!), (:*,:mul!)) + @eval begin + $jop(a::ZZPolyRingElem, b::$T) = $cop(similar(a), a, b) + $jop(a::$T, b::ZZPolyRingElem) = $cop(similar(b), a, b) + end + end end -+(x::Int, y::ZZPolyRingElem) = y + x - -+(x::ZZRingElem, y::ZZPolyRingElem) = y + x - -*(x::ZZPolyRingElem, y::Int) = y*x - -*(x::ZZPolyRingElem, y::ZZRingElem) = y*x - -+(x::Integer, y::ZZPolyRingElem) = y + flintify(x) - --(x::Integer, y::ZZPolyRingElem) = flintify(x) - y - -*(x::Integer, y::ZZPolyRingElem) = flintify(x)*y - -+(x::ZZPolyRingElem, y::Integer) = x + flintify(y) - --(x::ZZPolyRingElem, y::Integer) = x - flintify(y) - -*(x::ZZPolyRingElem, y::Integer) = flintify(y)*x - ############################################################################### # # Powering @@ -873,30 +797,133 @@ function neg!(z::ZZPolyRingElemOrPtr, a::ZZPolyRingElemOrPtr) return z end -function fit!(z::ZZPolyRingElem, n::Int) +function fit!(z::ZZPolyRingElemOrPtr, n::Int) ccall((:fmpz_poly_fit_length, libflint), Nothing, (Ref{ZZPolyRingElem}, Int), z, n) return nothing end -function setcoeff!(z::ZZPolyRingElem, n::Int, x::ZZRingElemOrPtr) - ccall((:fmpz_poly_set_coeff_fmpz, libflint), Nothing, - (Ref{ZZPolyRingElem}, Int, Ref{ZZRingElem}), z, n, x) +# + +function set!(z::ZZPolyRingElemOrPtr, a::ZZPolyRingElemOrPtr) + @ccall libflint.fmpz_poly_set(z::Ref{ZZPolyRingElem}, a::Ref{ZZPolyRingElem})::Nothing return z end -function mul!(z::ZZPolyRingElem, x::ZZPolyRingElem, y::ZZPolyRingElem) - ccall((:fmpz_poly_mul, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}), z, x, y) +function set!(z::ZZPolyRingElemOrPtr, a::ZZRingElemOrPtr) + @ccall libflint.fmpz_poly_set_fmpz(z::Ref{ZZPolyRingElem}, a::Ref{ZZRingElem})::Nothing return z end -function add!(z::ZZPolyRingElem, x::ZZPolyRingElem, y::ZZPolyRingElem) - ccall((:fmpz_poly_add, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}), z, x, y) +function set!(z::ZZPolyRingElemOrPtr, a::Int) + @ccall libflint.fmpz_poly_set_si(z::Ref{ZZPolyRingElem}, a::Int)::Nothing + return z +end + +function set!(z::ZZPolyRingElemOrPtr, a::UInt) + @ccall libflint.fmpz_poly_set_ui(z::Ref{ZZPolyRingElem}, a::UInt)::Nothing + return z +end + +set!(z::ZZPolyRingElemOrPtr, a::Integer) = set!(z, flintify(a)) + +# + +function setcoeff!(z::ZZPolyRingElemOrPtr, n::Int, x::ZZRingElemOrPtr) + @ccall libflint.fmpz_poly_set_coeff_fmpz(z::Ref{ZZPolyRingElem}, n::Int, x::Ref{ZZRingElem})::Nothing + return z +end + +function setcoeff!(z::ZZPolyRingElemOrPtr, n::Int, x::Int) + @ccall libflint.fmpz_poly_set_coeff_si(z::Ref{ZZPolyRingElem}, n::Int, x::Int)::Nothing + return z +end + +function setcoeff!(z::ZZPolyRingElemOrPtr, n::Int, x::UInt) + @ccall libflint.fmpz_poly_set_coeff_ui(z::Ref{ZZPolyRingElem}, n::Int, x::UInt)::Nothing + return z +end + +setcoeff!(z::ZZPolyRingElemOrPtr, n::Int, x::Integer) = setcoeff!(z, n, flintify(x)) + +# + +function add!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::ZZPolyRingElemOrPtr) + @ccall libflint.fmpz_poly_add(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Ref{ZZPolyRingElem})::Nothing + return z +end + +function add!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::ZZRingElemOrPtr) + @ccall libflint.fmpz_poly_add_fmpz(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Ref{ZZRingElem})::Nothing + return z +end + +function add!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::Int) + @ccall libflint.fmpz_poly_add_si(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Int)::Nothing + return z +end + +add!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::Integer) = add!(z, x, flintify(y)) + +add!(z::ZZPolyRingElemOrPtr, x::IntegerUnionOrPtr, y::ZZPolyRingElemOrPtr) = add!(z, y, x) + +# + +function sub!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::ZZPolyRingElemOrPtr) + @ccall libflint.fmpz_poly_sub(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Ref{ZZPolyRingElem})::Nothing + return z +end + +function sub!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::ZZRingElemOrPtr) + @ccall libflint.fmpz_poly_sub_fmpz(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Ref{ZZRingElem})::Nothing + return z +end + +function sub!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::Int) + @ccall libflint.fmpz_poly_sub_si(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Int)::Nothing + return z +end + +function sub!(z::ZZPolyRingElemOrPtr, x::ZZRingElemOrPtr, y::ZZPolyRingElemOrPtr) + @ccall libflint.fmpz_poly_fmpz_sub(z::Ref{ZZPolyRingElem}, x::Ref{ZZRingElem}, y::Ref{ZZPolyRingElem})::Nothing return z end +function sub!(z::ZZPolyRingElemOrPtr, x::Int, y::ZZPolyRingElemOrPtr) + @ccall libflint.fmpz_poly_si_sub(z::Ref{ZZPolyRingElem}, x::Int, y::Ref{ZZPolyRingElem})::Nothing + return z +end + +sub!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::Integer) = sub!(z, x, flintify(y)) + +sub!(z::ZZPolyRingElemOrPtr, x::Integer, y::ZZPolyRingElemOrPtr) = sub!(z, flintify(x), y) + +# + +function mul!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::ZZPolyRingElemOrPtr) + @ccall libflint.fmpz_poly_mul(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Ref{ZZPolyRingElem})::Nothing + return z +end + +function mul!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::ZZRingElemOrPtr) + @ccall libflint.fmpz_poly_scalar_mul_fmpz(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Ref{ZZRingElem})::Nothing + return z +end + +function mul!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::Int) + @ccall libflint.fmpz_poly_scalar_mul_si(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::Int)::Nothing + return z +end + +function mul!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::UInt) + @ccall libflint.fmpz_poly_scalar_mul_ui(z::Ref{ZZPolyRingElem}, x::Ref{ZZPolyRingElem}, y::UInt)::Nothing + return z +end + +mul!(z::ZZPolyRingElemOrPtr, x::ZZPolyRingElemOrPtr, y::Integer) = mul!(z, x, flintify(y)) + +mul!(z::ZZPolyRingElemOrPtr, x::IntegerUnionOrPtr, y::ZZPolyRingElemOrPtr) = mul!(z, y, x) + ############################################################################### # # Promotions