From 340fc330afcf07fcd0a1d3ea345a1fcabb71fef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Wed, 28 Jun 2023 15:43:48 +0200 Subject: [PATCH 01/11] Remove obvious piracy from `Hecke/src/misc/Integer.jl` now in Nemo --- src/EllCrv/EllCrv.jl | 4 - src/Hecke.jl | 3 +- src/Misc/Integer.jl | 456 ------------------------------------------- 3 files changed, 2 insertions(+), 461 deletions(-) diff --git a/src/EllCrv/EllCrv.jl b/src/EllCrv/EllCrv.jl index 547ea0788f..0c0ca43eb6 100644 --- a/src/EllCrv/EllCrv.jl +++ b/src/EllCrv/EllCrv.jl @@ -1086,10 +1086,6 @@ end # ################################################################################ -function log(a::ZZRingElem, b::ZZRingElem) - log(b)/log(a) -end - function replace_all_squares_modulo(f, g, F) # assumes that f is in Z[x,y^2] and g in Z[x]. Replaces y^2 with g. # the result will be in Z[x] diff --git a/src/Hecke.jl b/src/Hecke.jl index 17e42e0e4f..ac0b09323f 100644 --- a/src/Hecke.jl +++ b/src/Hecke.jl @@ -108,7 +108,8 @@ import Nemo: acb_struct, Ring, Group, Field, zzModRing, zzModRingElem, arf_struc elem_to_mat_row!, elem_from_mat_row, fpFieldElem, fpMatrix, FpFieldElem, Zmodn_poly, Zmodn_mat, fpField, FpField, acb_vec, array, acb_vec_clear, force_coerce, - force_op, fmpz_mod_ctx_struct, divisors, is_zero_entry, IntegerUnion + force_op, fmpz_mod_ctx_struct, divisors, is_zero_entry, IntegerUnion, remove!, + valuation! export show, StepRange, domain, codomain, image, preimage, modord, resultant, next_prime, is_power, number_field, factor, @vtime, RationalUnion diff --git a/src/Misc/Integer.jl b/src/Misc/Integer.jl index 6833049fe0..50388a3044 100644 --- a/src/Misc/Integer.jl +++ b/src/Misc/Integer.jl @@ -4,201 +4,15 @@ # ################################################################################ -function rem(a::ZZRingElem, b::UInt) - return ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), a, b) -end - - -function isless(a::BigFloat, b::Nemo.ZZRingElem) - if _fmpz_is_small(b) - c = ccall((:mpfr_cmp_si, :libmpfr), Int32, (Ref{BigFloat}, Int), a, b.d) - else - c = ccall((:mpfr_cmp_z, :libmpfr), Int32, (Ref{BigFloat}, UInt), a, unsigned(b.d) << 2) - end - return c < 0 -end - -function mulmod(a::UInt, b::UInt, n::UInt, ni::UInt) - ccall((:n_mulmod2_preinv, libflint), UInt, (UInt, UInt, UInt, UInt), a, b, n, ni) -end - - -# TODO (CF): -# should be Bernstein'ed: this is slow for large valuations -# returns the maximal v s.th. z mod p^v == 0 and z div p^v -# also useful if p is not prime.... -# -# TODO: what happens to z = 0??? - -function remove(z::T, p::T) where T <: Integer - z == 0 && return (0, z) - v = 0 - @assert p > 1 - while mod(z, p) == 0 - z = div(z, p) - v += 1 - end - return (v, z) -end - -function remove(z::Rational{T}, p::T) where T <: Integer - z == 0 && return(0, z) - v, d = remove(denominator(z), p) - w, n = remove(numerator(z), p) - return w-v, n//d -end - -function valuation(z::T, p::T) where T <: Integer - z == 0 && return 0 - v = 0 - @assert p > 1 - while mod(z, p) == 0 - z = div(z, p) - v += 1 - end - return v -end - -function valuation(z::Rational{T}, p::T) where T <: Integer - z == 0 && error("Not yet implemented") - v = valuation(denominator(z), p) - w = valuation(numerator(z), p) - return w-v -end - -function remove!(a::ZZRingElem, b::ZZRingElem) - v = ccall((:fmpz_remove, libflint), Clong, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), a, a, b) - return v, a -end - -function remove!(a::QQFieldElem, b::ZZRingElem) - nr = ccall((:fmpq_numerator_ptr, libflint), Ptr{ZZRingElem}, (Ref{QQFieldElem}, ), a) - vn = ccall((:fmpz_remove, libflint), Clong, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), nr, nr, b) - #QQFieldElem's are simplified: either num OR den will be non-trivial - if vn != 0 - return vn, a - end - nr = ccall((:fmpq_denominator_ptr, libflint), Ptr{ZZRingElem}, (Ref{QQFieldElem}, ), a) - vn = ccall((:fmpz_remove, libflint), Clong, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), nr, nr, b) - return -vn, a -end - -function valuation!(a::QQFieldElem, b::ZZRingElem) - nr = ccall((:fmpq_numerator_ptr, libflint), Ptr{ZZRingElem}, (Ref{QQFieldElem}, ), a) - vn = ccall((:fmpz_remove, libflint), Clong, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), nr, nr, b) - #QQFieldElem's are simplified: either num OR den will be non-trivial - if vn != 0 - return vn - end - nr = ccall((:fmpq_denominator_ptr, libflint), Ptr{ZZRingElem}, (Ref{QQFieldElem}, ), a) - vn = ccall((:fmpz_remove, libflint), Clong, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), nr, nr, b) - return -vn -end - -function *(a::ZZRingElem, b::BigFloat) - return BigInt(a)*b -end - -function BigFloat(a::QQFieldElem) - r = BigFloat(0) - ccall((:fmpq_get_mpfr, libflint), Nothing, (Ref{BigFloat}, Ref{QQFieldElem}, Int32), r, a, __get_rounding_mode()) - return r -end - -function isless(a::Float64, b::QQFieldElem) return a Int modord(a::Integer, m::Integer) @@ -227,7 +41,6 @@ function modord(a::Integer, m::Integer) return i end - function neg!(a::ZZRingElem) ccall((:fmpz_neg, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}), a, a) return a @@ -261,10 +74,6 @@ show(io::IO, r::fmpzUnitRange) = print(io, repr(first(r)), ':', repr(last(r))) in(x::IntegerUnion, r::fmpzUnitRange) = first(r) <= x <= last(r) -in(x::IntegerUnion, r::AbstractRange{ZZRingElem}) = - !isempty(r) && first(r) <= x <= last(r) && - mod(convert(ZZRingElem,x),step(r)) == mod(first(r),step(r)) - mod(i::IntegerUnion, r::fmpzUnitRange) = mod(i-first(r), length(r)) + first(r) Base.:(:)(a::ZZRingElem, b::Integer) = (:)(promote(a,b)...) @@ -324,13 +133,7 @@ function rand(rng::AbstractRNG, g::RangeGeneratorfmpz) return rand(rng, g.a) end -function Base.getindex(a::StepRange{ZZRingElem,ZZRingElem}, i::ZZRingElem) - a.start+(i-1)*Base.step(a) -end -function Base.divrem(a::ZZRingElem, b::Int) - return (div(a, b), rem(a, b)) -end ############################################################ # more unsafe function that Bill does not want to have.... @@ -361,97 +164,6 @@ function gcd!(z::ZZRingElem, x::ZZRingElem, y::ZZRingElem) return z end -################################################################################ -# -# power detection -# -################################################################################ -#compare to Oscar/examples/PerfectPowers.jl which is, for large input, -#far superiour over gmp/ fmpz_is_perfect_power - -@doc raw""" - is_power(a::ZZRingElem) -> Int, ZZRingElem - is_power(a::Integer) -> Int, Integer - -Returns $e$, $r$ such that $a = r^e$ with $e$ maximal. Note: $1 = 1^0$. -""" -function is_power(a::ZZRingElem) - if iszero(a) - error("must not be zero") - end - if isone(a) - return 0, a - end - if a < 0 - e, r = is_power(-a) - if isone(e) - return 1, a - end - v, s = iszero(e) ? (0, 0) : remove(e, 2) - return s, -r^(2^v) - end - rt = ZZRingElem() - e = 1 - while true - ex = ccall((:fmpz_is_perfect_power, libflint), Int, (Ref{ZZRingElem}, Ref{ZZRingElem}), rt, a) - if ex == 1 || ex == 0 - return e, a - end - e *= ex - a = rt - end -end - -function is_power(a::Integer) - e, r = is_power(ZZRingElem(a)) - return e, typeof(a)(r) -end - -@doc raw""" - is_power(a::QQFieldElem) -> Int, QQFieldElem - is_power(a::Rational) -> Int, Rational - -Writes $a = r^e$ with $e$ maximal. Note: $1 = 1^0$. -""" -function is_power(a::QQFieldElem) - e, r = is_power(numerator(a)) - if e==1 - return e, a - end - f, s = is_power(denominator(a)) - g = gcd(e, f) - return g, r^div(e, g)//s^div(f, g) -end - -function is_power(a::Rational) - T = typeof(denominator(a)) - e, r = is_power(QQFieldElem(a)) - return e, T(numerator(r))//T(denominator(r)) -end - -@doc raw""" - is_power(a::ZZRingElem, n::Int) -> Bool, ZZRingElem - is_power(a::QQFieldElem, n::Int) -> Bool, QQFieldElem - is_power(a::Integer, n::Int) -> Bool, Integer - -Tests if $a$ is an $n$-th power. Return `true` and the root if successful. -""" -function is_power(a::ZZRingElem, n::Int) - if a < 0 && iseven(n) - return false, a - end - b = iroot(a, n) - return b^n == a, b -end - -function is_power(a::QQFieldElem, n::Int) - fl, nu = is_power(numerator(a), n) - if !fl - return fl, a - end - fl, de = is_power(denominator(a), n) - return fl, QQFieldElem(nu, de) -end ################################################################################ # @@ -521,22 +233,6 @@ function submul!(z::ZZRingElem, x::ZZRingElem, y::ZZRingElem) ccall((:fmpz_submul, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), z, x, y) end -################################################################################ -# -# Number of bits -# -################################################################################ - - -@doc raw""" - nbits(a::Integer) -> Int - - Returns the number of bits necessary to represent $a$. -""" -function nbits(a::Integer) - return ndigits(a, base=2) -end - ################################################################################ # @@ -556,14 +252,6 @@ function mod_sym(a::ZZRingElem, b::ZZRingElem) end end -@doc raw""" - isinteger(a::QQFieldElem) -> Bool - -Returns `true` iff the denominator of $a$ is one. -""" -function isinteger(a::QQFieldElem) - return isone(denominator(a)) -end ################################################################################ # @@ -882,32 +570,6 @@ function _factors_trial_division(n::ZZRingElem, np::Int = 10^5) end -function ceil(::Type{ZZRingElem}, a::BigFloat) - return ZZRingElem(ceil(BigInt, a)) -end - -function ceil(::Type{Int}, a::QQFieldElem) - return Int(ceil(ZZRingElem, a)) -end - -function floor(::Type{ZZRingElem}, a::BigFloat) - return ZZRingElem(floor(BigInt, a)) -end - -function floor(::Type{Int}, a::QQFieldElem) - return Int(floor(ZZRingElem, a)) -end - -function round(::Type{ZZRingElem}, a::BigFloat) - return ZZRingElem(round(BigInt, a)) -end - -function round(::Type{Int}, a::BigFloat) - return Int(round(ZZRingElem, a)) -end - -/(a::BigFloat, b::ZZRingElem) = a/BigInt(b) - function rand!(A::Vector{ZZRingElem}, v::StepRange{ZZRingElem, ZZRingElem}) for i in 1:length(A) A[i] = rand(v) @@ -915,10 +577,6 @@ function rand!(A::Vector{ZZRingElem}, v::StepRange{ZZRingElem, ZZRingElem}) return A end -Base.isless(a::Int, b::ZZRingElem) = a < b - -Base.isless(a::ZZRingElem, b::Int) = a < b - function (::Type{Base.Rational{BigInt}})(x::QQFieldElem) return Rational{BigInt}(BigInt(numerator(x)), BigInt(denominator(x))) end @@ -1330,10 +988,6 @@ function quo(::ZZRing, a::Integer) return R, f end -function (::ZZRing)(x::Rational{Int}) - @assert denominator(x) == 1 - return ZZRingElem(numerator(x)) -end module BitsMod @@ -1482,8 +1136,6 @@ export bits, Limbs ^(a::NfAbsOrdIdl, n::IntegerUnion) = _generic_power(a, n) #^(a::NfRelOrdIdl, n::IntegerUnion) = _generic_power(a, n) -is_negative(n::IntegerUnion) = cmp(n, 0) < 0 -is_positive(n::IntegerUnion) = cmp(n, 0) > 0 function _generic_power(a, n::IntegerUnion) fits(Int, n) && return a^Int(n) @@ -1501,25 +1153,6 @@ function _generic_power(a, n::IntegerUnion) return r end -#square-and-multiply algorithm to compute f^e mod g -function powermod(f::T, e::ZZRingElem, g::T) where {T} - #small exponent -> use powermod - if nbits(e) <= 63 - return powermod(f, Int(e), g) - else - #go through binary representation of exponent and multiply with res - #or (res and f) - res = parent(f)(1) - for b=bits(e) - res = mod(res^2, g) - if b - res = mod(res*f, g) - end - end - return res - end -end - ################################################################################ # @@ -1617,92 +1250,6 @@ function squarefree_up_to(n::Int; coprime_to::Vector{ZZRingElem} = ZZRingElem[], return findall(list) end -################################################################################ -# -# is_squarefree -# -################################################################################ - -#TODO (Hard): Implement this properly. -@doc raw""" - is_squarefree(n::Union{Int, ZZRingElem}) -> Bool - -Returns true if $n$ is squarefree, false otherwise. -""" -function is_squarefree(n::Union{Int,ZZRingElem}) - if iszero(n) - error("Argument must be non-zero") - end - if isone(abs(n)) - return true - end - e, b = is_power(n) - if e > 1 - return false - end - return isone(maximum(values(factor(n).fac))) -end - -################################################################################ -# -# Rounding and friends -# -################################################################################ - -Base.floor(::Type{ZZRingElem}, x::ZZRingElem) = x - -Base.ceil(::Type{ZZRingElem}, x::ZZRingElem) = x - -Base.floor(::Type{ZZRingElem}, x::Int) = ZZRingElem(x) - -Base.ceil(::Type{ZZRingElem}, x::Int) = ZZRingElem(x) - -Base.floor(::Type{ZZRingElem}, x::QQFieldElem) = fdiv(numerator(x), denominator(x)) - -Base.ceil(::Type{ZZRingElem}, x::QQFieldElem) = cdiv(numerator(x), denominator(x)) - -Base.round(x::QQFieldElem, ::RoundingMode{:Up}) = ceil(x) - -Base.round(::Type{ZZRingElem}, x::QQFieldElem, ::RoundingMode{:Up}) = ceil(ZZRingElem, x) - -Base.round(x::QQFieldElem, ::RoundingMode{:Down}) = floor(x) - -Base.round(::Type{ZZRingElem}, x::QQFieldElem, ::RoundingMode{:Down}) = floor(ZZRingElem, x) - -function Base.round(x::QQFieldElem, ::RoundingMode{:Nearest}) - d = denominator(x) - n = numerator(x) - if d == 2 - if mod(n, 4) == 1 - if n > 0 - return div(n, d) - else - return div(n, d) - 1 - end - else - if n > 0 - return div(n, d) + 1 - else - return div(n, d) - end - end - end - - return floor(x + 1//2) -end - -Base.round(x::QQFieldElem, ::RoundingMode{:NearestTiesAway}) = sign(x) * floor(abs(x) + 1//2) - -Base.round(::Type{ZZRingElem}, x::QQFieldElem, ::RoundingMode{:NearestTiesAway}) = sign(x) == 1 ? floor(ZZRingElem, abs(x) + 1//2) : -floor(ZZRingElem, abs(x) + 1//2) - -function Base.round(::Type{ZZRingElem}, a::QQFieldElem) - return round(ZZRingElem, a, RoundNearestTiesAway) -end - -function Base.round(a::QQFieldElem) - return round(ZZRingElem, a) -end - ################################################################################ # # Squarefree part @@ -1746,9 +1293,6 @@ function factor(a::QQFieldElem, ::ZZRing) return fn end -#missing in Nemo... -Hecke.clog(a::Int, b::Int) = clog(ZZRingElem(a), b) - ################################################################################ # # Support From 1c45f4e7fb74d7093b39f6c2d1e7a0eb923713b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 29 Jun 2023 15:44:08 +0200 Subject: [PATCH 02/11] Remove obvious piracy from `Hecke/src/misc/Matrix.jl` now in Nemo --- src/Aliases.jl | 2 - src/Misc/Matrix.jl | 1317 ++------------------------------------------ 2 files changed, 42 insertions(+), 1277 deletions(-) diff --git a/src/Aliases.jl b/src/Aliases.jl index 4957208b08..9ca7f7f948 100644 --- a/src/Aliases.jl +++ b/src/Aliases.jl @@ -57,7 +57,6 @@ @alias isdefining_polynomial_nice is_defining_polynomial_nice @alias isdefinite is_definite @alias isdegenerate is_degenerate -@alias isdiagonal is_diagonal @alias isdiagonalisable is_diagonalisable @alias isdiscriminant is_discriminant @alias isdivisible is_divisible @@ -116,7 +115,6 @@ @alias islocally_isomorphic is_locally_isomorphic @alias islocally_isomorphic_with_isomophism is_locally_isomorphic_with_isomophism @alias islocally_represented_by is_locally_represented_by -@alias islower_triangular is_lower_triangular @alias ismaximal is_maximal @alias ismaximal_integral is_maximal_integral @alias ismaximal_known is_maximal_known diff --git a/src/Misc/Matrix.jl b/src/Misc/Matrix.jl index c831760498..d9cdb0bc97 100644 --- a/src/Misc/Matrix.jl +++ b/src/Misc/Matrix.jl @@ -3,108 +3,6 @@ export is_zero_row, howell_form, kernel_basis, is_diagonal, diagonal, saturate, import Nemo.matrix -import Base.vcat - -import LinearAlgebra -LinearAlgebra.dot(a::RingElem, b::RingElem) = a*b - -################################################################################ -# -# Dense matrix types -# -################################################################################ - -dense_matrix_type(::Type{T}) where {T} = Generic.MatSpaceElem{T} - -################################################################################ -# -# Unsafe functions for generic matrices -# -################################################################################ - -#function zero!(a::MatElem) -# for i in 1:nrows(a) -# for j in 1:ncols(a) -# a[i, j] = zero!(a[i, j]) -# end -# end -# return a -#end - -function mul!(c::MatElem, a::MatElem, b::MatElem) - ncols(a) != nrows(b) && error("Incompatible matrix dimensions") - nrows(c) != nrows(a) && error("Incompatible matrix dimensions") - ncols(c) != ncols(b) && error("Incompatible matrix dimensions") - - if c === a || c === b - d = parent(a)() - return mul!(d, a, b) - end - - t = base_ring(a)() - for i = 1:nrows(a) - for j = 1:ncols(b) - c[i, j] = zero!(c[i, j]) - for k = 1:ncols(a) - c[i, j] = addmul_delayed_reduction!(c[i, j], a[i, k], b[k, j], t) - end - c[i, j] = reduce!(c[i, j]) - end - end - return c -end - -function add!(c::MatElem, a::MatElem, b::MatElem) - parent(a) != parent(b) && error("Parents don't match.") - parent(c) != parent(b) && error("Parents don't match.") - for i = 1:nrows(c) - for j = 1:ncols(c) - c[i, j] = add!(c[i, j], a[i, j], b[i, j]) - end - end - return c -end - -function mul!(a::zzModMatrix, b::zzModMatrix, c::zzModRingElem) - ccall((:nmod_mat_scalar_mul, libflint), Nothing, - (Ref{zzModMatrix}, Ref{zzModMatrix}, UInt), a, b, c.data) - return a -end - -function mul!(c::MatElem, a::MatElem, b::RingElement) - nrows(c) != nrows(a) && error("Incompatible matrix dimensions") - - if c === a || c === b - d = parent(a)() - return mul!(d, a, b) - end - - t = base_ring(a)() - for i = 1:nrows(a) - for j = 1:ncols(a) - c[i, j] = mul!(c[i, j], a[i, j], b) - end - end - return c -end - -################################################################################ -# -# Denominator -# -################################################################################ - -# This function is really slow... -function denominator(M::QQMatrix) - d = one(FlintZZ) - for i in 1:nrows(M) - for j in 1:ncols(M) - d = lcm!(d, d, denominator(M[i, j])) - end - end - return d -end - ################################################################################ # # Saturation @@ -155,151 +53,6 @@ function saturate(A::ZZMatrix) :: ZZMatrix return S end -transpose!(A::Union{ZZMatrix, QQMatrix}) = is_square(A) ? transpose!(A, A) : transpose(A) -transpose!(A::MatrixElem) = transpose(A) - -function transpose!(A::ZZMatrix, B::ZZMatrix) - ccall((:fmpz_mat_transpose, libflint), Nothing, - (Ref{ZZMatrix}, Ref{ZZMatrix}), A, B) - return A -end - -function transpose!(A::QQMatrix, B::QQMatrix) - ccall((:fmpq_mat_transpose, libflint), Nothing, - (Ref{QQMatrix}, Ref{QQMatrix}), A, B) - return A -end - -################################################################################ -# -# Zero matrix constructors -# -################################################################################ - -function zero_matrix(::Type{MatElem}, R::Ring, n::Int) - return zero_matrix(R, n) -end - -function zero_matrix(::Type{MatElem}, R::Ring, n::Int, m::Int) - return zero_matrix(R, n, m) -end - - -function matrix(A::Matrix{ZZRingElem}) - m = matrix(FlintZZ, A) - return m -end - -function matrix(A::Matrix{T}) where T <: RingElem - r, c = size(A) - (r < 0 || c < 0) && error("Array must be non-empty") - m = matrix(parent(A[1, 1]), A) - return m -end - -function matrix(A::Vector{T}) where T <: RingElem - return matrix(reshape(A,length(A),1)) -end - -function scalar_matrix(R::Ring, n::Int, a::RingElement) - b = R(a) - z = zero_matrix(R, n, n) - for i in 1:n - z[i, i] = b - end - return z -end - -function Array(a::ZZMatrix; S::Type{T} = ZZRingElem) where T - A = Array{T}(undef, nrows(a), ncols(a)) - for i = 1:nrows(a) - for j = 1:ncols(a) - A[i,j] = T(a[i,j]) - end - end - return A -end - -function is_zero_row(M::ZZMatrix, i::Int) - GC.@preserve M begin - for j = 1:ncols(M) - m = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), M, i - 1, j - 1) - fl = ccall((:fmpz_is_zero, libflint), Bool, (Ptr{ZZRingElem},), m) - if !fl - return false - end - end - end - return true -end - -function is_positive_entry(M::ZZMatrix, i::Int, j::Int) - GC.@preserve M begin - m = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), M, i - 1, j - 1) - fl = ccall((:fmpz_sgn, libflint), Int, (Ptr{ZZRingElem},), m) - return isone(fl) - end -end - - - -function is_zero_row(M::zzModMatrix, i::Int) - zero = UInt(0) - for j in 1:ncols(M) - t = ccall((:nmod_mat_get_entry, libflint), Base.GMP.Limb, (Ref{zzModMatrix}, Int, Int), M, i - 1, j - 1) - if t != zero - return false - end - end - return true -end - -function is_zero_row(M::MatElem{T}, i::Int) where T - for j in 1:ncols(M) - if !iszero(M[i,j]) - return false - end - end - return true -end - -function is_zero_row(M::Matrix{T}, i::Int) where T <: Integer - for j = 1:Base.size(M, 2) - if M[i,j] != 0 - return false - end - end - return true -end - -function is_zero_row(M::Matrix{ZZRingElem}, i::Int) - for j = 1:Base.size(M, 2) - if M[i,j] != 0 - return false - end - end - return true -end - -function is_zero_row(M::Matrix{T}, i::Int) where T <: RingElem - for j in 1:Base.size(M, 2) - if !iszero(M[i,j]) - return false - end - end - return true -end - -function divexact!(a::ZZMatrix, b::ZZMatrix, d::ZZRingElem) - ccall((:fmpz_mat_scalar_divexact_fmpz, libflint), Nothing, - (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZRingElem}), a, a, d) -end - -function mul!(a::ZZMatrix, b::ZZMatrix, c::ZZRingElem) - ccall((:fmpz_mat_scalar_mul_fmpz, libflint), Nothing, - (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZRingElem}), a, b, c) -end - function _hnf(x::T, shape::Symbol = :upperright) where {T <: MatElem} if shape == :lowerleft h = hnf(reverse_cols(x)) @@ -474,54 +227,6 @@ function is_lll_reduced(x::ZZMatrix, ctx::lll_ctx = lll_ctx(0.99, 0.51)) return Bool(b) end -################################################################################ -# -################################################################################ - -function maximum(f::typeof(abs), a::ZZMatrix) - m = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), a, 0,0) - for i=1:nrows(a) - for j=1:ncols(a) - z = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), a, i-1, j-1) - if ccall((:fmpz_cmpabs, libflint), Cint, (Ptr{ZZRingElem}, Ptr{ZZRingElem}), m, z) < 0 - m = z - end - end - end - r = ZZRingElem() - ccall((:fmpz_abs, libflint), Nothing, (Ref{ZZRingElem}, Ptr{ZZRingElem}), r, m) - return r -end - -function maximum(a::ZZMatrix) - m = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), a, 0,0) - for i=1:nrows(a) - for j=1:ncols(a) - z = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), a, i-1, j-1) - if ccall((:fmpz_cmp, libflint), Cint, (Ptr{ZZRingElem}, Ptr{ZZRingElem}), m, z) < 0 - m = z - end - end - end - r = ZZRingElem() - ccall((:fmpz_set, libflint), Nothing, (Ref{ZZRingElem}, Ptr{ZZRingElem}), r, m) - return r -end - -function minimum(a::ZZMatrix) - m = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), a, 0,0) - for i=1:nrows(a) - for j=1:ncols(a) - z = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), a, i-1, j-1) - if ccall((:fmpz_cmp, libflint), Cint, (Ptr{ZZRingElem}, Ptr{ZZRingElem}), m, z) > 0 - m = z - end - end - end - r = ZZRingElem() - ccall((:fmpz_set, libflint), Nothing, (Ref{ZZRingElem}, Ptr{ZZRingElem}), r, m) - return r -end ################################################################################ # @@ -599,40 +304,6 @@ end # ################################################################################ -@doc raw""" - lift(a::Generic.Mat{Generic.ResidueRingElem{ZZRingElem}}) -> ZZMatrix - -It returns a lift of the matrix to the integers. -""" -function lift(a::Generic.Mat{Generic.ResidueRingElem{ZZRingElem}}) - z = zero_matrix(FlintZZ, nrows(a), ncols(a)) - for i in 1:nrows(a) - for j in 1:ncols(a) - z[i, j] = lift(a[i, j]) - end - end - return z -end - -function lift(a::ZZModMatrix) - z = zero_matrix(FlintZZ, nrows(a), ncols(a)) - GC.@preserve a z begin - for i in 1:nrows(a) - for j in 1:ncols(a) - m = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), z, i - 1, j - 1) - n = ccall((:fmpz_mod_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZModMatrix}, Int, Int), a, i - 1 , j - 1) - ccall((:fmpz_set, libflint), Nothing, (Ptr{ZZRingElem}, Ptr{ZZRingElem}), m, n) - #z[i, j] = lift(a[i, j]) - end - end - end - return z -end - -function lift(x::FpMatrix) - return map_entries(lift , x) -end - function lift_nonsymmetric(a::zzModMatrix) z = zero_matrix(FlintZZ, nrows(a), ncols(a)) ccall((:fmpz_mat_set_nmod_mat_unsigned, Hecke.libflint), Nothing, @@ -647,16 +318,6 @@ function lift_nonsymmetric(a::fpMatrix) return z end -function lift(a::Generic.Mat{Nemo.ZZModRingElem}) - z = zero_matrix(FlintZZ, nrows(a), ncols(a)) - for i in 1:nrows(a) - for j in 1:ncols(a) - z[i, j] = lift(a[i, j]) - end - end - return z -end - function lift_unsigned(a::zzModMatrix) z = zero_matrix(FlintZZ, nrows(a), ncols(a)) ccall((:fmpz_mat_set_nmod_mat_unsigned, libflint), Nothing, @@ -699,729 +360,86 @@ function right_kernel_basis(a::MatElem{T}) where T <: AbstractAlgebra.FieldElem for j in 1:nrows(z) t[j] = R(z[j, i]) end - push!(ar,t) - end - return ar -end - -@doc raw""" - left_kernel_basis(a::MatElem{T}) -> Vector{Vector{T}} - -It returns a basis for the left kernel of the matrix. -""" -left_kernel_basis(a::MatElem{T}) where T <: AbstractAlgebra.FieldElem = right_kernel_basis(transpose(a)) - - - -@doc raw""" - kernel(a::MatElem{T}; side::Symbol = :right) -> Int, MatElem{T} - -It returns a tuple $(n, M)$, where $n$ is the rank of the kernel and $M$ is a basis for it. If side is $:right$ or not -specified, the right kernel is computed. If side is $:left$, the left kernel is computed. -""" -function kernel(A::MatElem; side::Symbol = :right) - if side == :right - return right_kernel(A) - elseif side == :left - return left_kernel(A) - else - error("Unsupported argument: :$side for side: Must be :left or :right") - end -end - -function right_kernel(x::fpMatrix) - z = zero_matrix(base_ring(x), ncols(x), max(nrows(x),ncols(x))) - n = ccall((:nmod_mat_nullspace, libflint), Int, (Ref{fpMatrix}, Ref{fpMatrix}), z, x) - return n, z -end - -function left_kernel(x::fpMatrix) - n, M = right_kernel(transpose(x)) - return n, transpose(M) -end - -@doc raw""" - left_kernel(a::ZZMatrix) -> Int, ZZMatrix - -It returns a tuple $(n, M)$ where $M$ is a matrix whose rows generate -the kernel of $a$ and $n$ is the rank of the kernel. -""" -function left_kernel(x::ZZMatrix) - if nrows(x) == 0 - return 0, zero(x, 0, 0) - end - x1 = transpose(hnf(transpose(x))) - H, U = hnf_with_transform(x1) - i = 1 - for outer i in 1:nrows(H) - if is_zero_row(H, i) - break - end - end - if is_zero_row(H, i) - return nrows(U)-i+1, view(U, i:nrows(U), 1:ncols(U)) - else - return 0, zero_matrix(FlintZZ, 0, ncols(U)) - end -end - -right_kernel(M::MatElem) = nullspace(M) - -function left_kernel(M::MatElem) - rk, M1 = nullspace(transpose(M)) - return rk, transpose(M1) -end - -function right_kernel(x::ZZMatrix) - n, M = left_kernel(transpose(x)) - return n, transpose(M) -end - -function right_kernel(M::zzModMatrix) - R = base_ring(M) - if is_prime(modulus(R)) - k = zero_matrix(R, ncols(M), ncols(M)) - n = ccall((:nmod_mat_nullspace, libflint), Int, (Ref{zzModMatrix}, Ref{zzModMatrix}), k, M) - return n, k - end - - H = hcat(transpose(M), identity_matrix(R, ncols(M))) - if nrows(H) < ncols(H) - H = vcat(H, zero_matrix(R, ncols(H) - nrows(H), ncols(H))) - end - howell_form!(H) - nr = 1 - while nr <= nrows(H) && !is_zero_row(H, nr) - nr += 1 - end - nr -= 1 - h = sub(H, 1:nr, 1:nrows(M)) - for i=1:nrows(h) - if is_zero_row(h, i) - k = sub(H, i:nrows(h), nrows(M)+1:ncols(H)) - return nrows(k), transpose(k) - end - end - return 0, zero_matrix(R,nrows(M),0) -end - -function left_kernel(a::zzModMatrix) - n, M = right_kernel(transpose(a)) - return n, transpose(M) -end - -function right_kernel(M::ZZModMatrix) - R = base_ring(M) - N = hcat(transpose(M), identity_matrix(R, ncols(M))) - if nrows(N) < ncols(N) - N = vcat(N, zero_matrix(R, ncols(N) - nrows(N), ncols(N))) - end - howell_form!(N) - H = N - nr = 1 - while nr <= nrows(H) && !is_zero_row(H, nr) - nr += 1 - end - nr -= 1 - h = sub(H, 1:nr, 1:nrows(M)) - for i=1:nrows(h) - if is_zero_row(h, i) - k = sub(H, i:nrows(h), nrows(M)+1:ncols(H)) - return nrows(k), transpose(k) - end - end - return 0, zero_matrix(R,nrows(M),0) -end - -function left_kernel(a::ZZModMatrix) - n, M = right_kernel(transpose(a)) - return n, transpose(M) -end - -################################################################################ -# -# Kernel over different rings -# -################################################################################ - -@doc raw""" - kernel(a::MatrixElem{T}, R::Ring; side::Symbol = :right) -> n, MatElem{elem_type(R)} - -It returns a tuple $(n, M)$, where $n$ is the rank of the kernel over $R$ and $M$ is a basis for it. If side is $:right$ or not -specified, the right kernel is computed. If side is $:left$, the left kernel is computed. -""" -function kernel(M::MatrixElem, R::Ring; side::Symbol = :right) - MP = change_base_ring(R, M) - return kernel(MP, side = side) -end - -################################################################################ -# -# Diagonal (block) matrix creation -# -################################################################################ - -@doc raw""" - diagonal_matrix(x::T...) where T <: RingElem -> MatElem{T} - diagonal_matrix(x::Vector{T}) where T <: RingElem -> MatElem{T} - diagonal_matrix(Q, x::Vector{T}) where T <: RingElem -> MatElem{T} - -Returns a diagonal matrix whose diagonal entries are the elements of $x$. - -# Examples - -```jldoctest -julia> diagonal_matrix(QQ(1), QQ(2)) -[1 0] -[0 2] - -julia> diagonal_matrix([QQ(3), QQ(4)]) -[3 0] -[0 4] - -julia> diagonal_matrix(QQ, [5, 6]) -[5 0] -[0 6] -``` -""" -function diagonal_matrix(R::Ring, x::Vector{<:RingElement}) - x = R.(x) - M = zero_matrix(R, length(x), length(x)) - for i = 1:length(x) - M[i, i] = x[i] - end - return M -end - -function diagonal_matrix(x::T, xs::T...) where T <: RingElem - return diagonal_matrix(collect((x, xs...))) -end - -diagonal_matrix(x::Vector{<:RingElement}) = diagonal_matrix(parent(x[1]), x) - -@doc raw""" - diagonal_matrix(x::Vector{T}) where T <: MatElem -> MatElem - -Returns a block diagonal matrix whose diagonal blocks are the matrices in $x$. -""" -function diagonal_matrix(x::Vector{T}) where T <: MatElem - return cat(x..., dims = (1, 2))::T -end - -function diagonal_matrix(x::T, xs::T...) where T <: MatElem - return cat(x, xs..., dims = (1, 2))::T -end - -function diagonal_matrix(R::Ring, x::Vector{<:MatElem}) - if length(x) == 0 - return zero_matrix(R, 0, 0) - end - x = [change_base_ring(R, i) for i in x] - return diagonal_matrix(x) -end - -################################################################################ -# -# Copy matrix into another matrix -# -################################################################################ - -# Copy B into A at position (i, j) -function _copy_matrix_into_matrix(A::ZZMatrix, i::Int, j::Int, B::ZZMatrix) - @GC.preserve A B begin - for k in 0:nrows(B) - 1 - for l in 0:ncols(B) - 1 - d = ccall((:fmpz_mat_entry, libflint), - Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), B, k, l) - t = ccall((:fmpz_mat_entry, libflint), - Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), A, i - 1 + k, j - 1 + l) - ccall((:fmpz_set, libflint), Nothing, (Ptr{ZZRingElem}, Ptr{ZZRingElem}), t, d) - end - end - end -end - -function _copy_matrix_into_matrix!(A::QQMatrix, i::Int, j::Int, B::QQMatrix) - @GC.preserve A B begin - for k in 0:nrows(B) - 1 - for l in 0:ncols(B) - 1 - d = ccall((:fmpq_mat_entry, libflint), - Ptr{QQFieldElem}, (Ref{QQMatrix}, Int, Int), B, k, l) - t = ccall((:fmpq_mat_entry, libflint), - Ptr{QQFieldElem}, (Ref{QQMatrix}, Int, Int), A, i - 1 + k, j - 1 + l) - ccall((:fmpq_set, libflint), Nothing, (Ptr{QQFieldElem}, Ptr{QQFieldElem}), t, d) - end - end - end -end - -function _copy_matrix_into_matrix(A::MatElem, r::Vector{Int}, c::Vector{Int}, B::MatElem) - for i = 1:length(r) - for j = 1:length(c) - A[r[i], c[j]] = B[i, j] - end - end - return nothing -end - -@doc raw""" - is_positive_definite(a::ZZMatrix) -> Bool - -Tests if $a$ is positive definite by testing if all principal minors -have positive determinant. -""" -function is_positive_definite(a::ZZMatrix) - for i=1:nrows(a) - if det(sub(a, 1:i, 1:i)) <= 0 - return false - end - end - return true -end - -#Returns a positive integer if A[i, j] > b, negative if A[i, j] < b, 0 otherwise -function compare_index(A::ZZMatrix, i::Int, j::Int, b::ZZRingElem) - a = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), A, i-1, j-1) - return ccall((:fmpz_cmp, libflint), Int32, (Ptr{ZZRingElem}, Ref{ZZRingElem}), a, b) -end - - -#scales the i-th column of a by 2^d[1,i] -function mult_by_2pow_diag!(a::Matrix{BigFloat}, d::ZZMatrix, R = _RealRings[Threads.threadid()]) - s = size(a) - tmp_mpz::BigInt = R.z1 - for i = 1:s[1] - for j = 1:s[2] - e = ccall((:mpfr_get_z_2exp, :libmpfr), Clong, (Ref{BigInt}, Ref{BigFloat}), tmp_mpz, a[i,j]) - ccall((:mpfr_set_z_2exp, :libmpfr), Nothing, (Ref{BigFloat}, Ref{BigInt}, Clong, Int32), a[i,j], tmp_mpz, e+Clong(Int(d[1,j])), __get_rounding_mode()) - end - end -end - -#converts BigFloat -> ZZRingElem via round(a*2^l), in a clever(?) way -function round_scale(a::Matrix{BigFloat}, l::Int) - s = size(a) - b = zero_matrix(FlintZZ, s[1], s[2]) - return round_scale!(b, a, l) -end - -function round_scale!(b::ZZMatrix, a::Matrix{BigFloat}, l::Int, R = _RealRings[Threads.threadid()]) - s = size(a) - - local tmp_mpz::BigInt, tmp_fmpz::ZZRingElem - tmp_mpz = R.z1 - tmp_fmpz = R.zz1 - tmp_mpfr = deepcopy(a[1,1]) #cannot use the R.?? tmp variable as it may/will - #have the wrong precision - - rd = __get_rounding_mode() - for i = 1:s[1] - for j = 1:s[2] - e = a[i,j].exp - a[i,j].exp += l - ccall((:mpfr_round, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Int32), tmp_mpfr, a[i,j], rd) - a[i,j].exp = e - f = ccall((:mpfr_get_z_2exp, :libmpfr), Clong, (Ref{BigInt}, Ref{BigFloat}), - tmp_mpz, tmp_mpfr) - ccall((:fmpz_set_mpz, libflint), Nothing, (Ref{ZZRingElem}, Ref{BigInt}), tmp_fmpz, tmp_mpz) - if f > 0 - ccall((:fmpz_mul_2exp, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, UInt), tmp_fmpz, tmp_fmpz, f) - else - ccall((:fmpz_tdiv_q_2exp, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, UInt), tmp_fmpz, tmp_fmpz, -f); - end - setindex!(b, tmp_fmpz, i, j) - end - end - return b -end - -function round_scale!(b::ZZMatrix, a::arb_mat, l::Int) - s = size(a) - - R = base_ring(a) - r = R() - for i = 1:s[1] - for j = 1:s[2] - v = ccall((:arb_mat_entry_ptr, libarb), Ptr{arb}, - (Ref{arb_mat}, Int, Int), a, i - 1, j - 1) - ccall((:arb_mul_2exp_si, libarb), Nothing, (Ref{arb}, Ptr{arb}, Int), r, v, l) - b[i,j] = round(ZZRingElem, r) - end - end - return b -end - -function round!(b::ZZMatrix, a::arb_mat) - s = size(a) - for i = 1:s[1] - for j = 1:s[2] - b[i, j] = round(ZZRingElem, a[i, j]) - end - end - return b -end - - -function shift!(g::ZZMatrix, l::Int) - for i=1:nrows(g) - for j=1:ncols(g) - z = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), g, i-1, j-1) - if l > 0 - ccall((:fmpz_mul_2exp, libflint), Nothing, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Int), z, z, l) - else - ccall((:fmpz_tdiv_q_2exp, libflint), Nothing, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Int), z, z, -l) - end - end - end - return g -end - -################################################################################ -# -# Reduce the entries of a matrix modulo p -# -################################################################################ - -@doc raw""" - mod!(M::ZZMatrix, p::ZZRingElem) - -Reduces every entry modulo $p$ in-place, i.e. applies the mod function to every entry. -Positive residue system. -""" -function mod!(M::ZZMatrix, p::ZZRingElem) - GC.@preserve M begin - for i=1:nrows(M) - for j=1:ncols(M) - z = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), M, i - 1, j - 1) - ccall((:fmpz_mod, libflint), Nothing, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), z, z, p) - end - end - end - return nothing -end - -@doc raw""" - mod(M::ZZMatrix, p::ZZRingElem) -> ZZMatrix - -Reduces every entry modulo $p$, i.e. applies the mod function to every entry. -""" -function mod(M::ZZMatrix, p::ZZRingElem) - N = deepcopy(M) - mod!(N, p) - return N -end - -@doc raw""" - mod_sym!(M::ZZMatrix, p::ZZRingElem) - -Reduces every entry modulo $p$ in-place, into the symmetric residue system. -""" -function mod_sym!(M::ZZMatrix, B::ZZRingElem) - @assert !iszero(B) - ccall((:fmpz_mat_scalar_smod, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZRingElem}), M, M, B) + push!(ar,t) + end + return ar end -mod_sym!(M::ZZMatrix, B::Integer) = mod_sym!(M, ZZRingElem(B)) @doc raw""" - mod_sym(M::ZZMatrix, p::ZZRingElem) -> ZZMatrix + left_kernel_basis(a::MatElem{T}) -> Vector{Vector{T}} -Reduces every entry modulo $p$ into the symmetric residue system. +It returns a basis for the left kernel of the matrix. """ -function mod_sym(M::ZZMatrix, B::ZZRingElem) - N = zero_matrix(FlintZZ, nrows(M), ncols(M)) - ccall((:fmpz_mat_scalar_smod, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}, Ref{ZZRingElem}), N, M, B) - return N -end -mod_sym(M::ZZMatrix, B::Integer) = mod_sym(M, ZZRingElem(B)) - - -################################################################################ -# -# Special map entries -# -################################################################################ - -function map_entries(R::zzModRing, M::ZZMatrix) - MR = zero_matrix(R, nrows(M), ncols(M)) - ccall((:fmpz_mat_get_nmod_mat, libflint), Cvoid, (Ref{zzModMatrix}, Ref{ZZMatrix}), MR, M) - return MR -end +left_kernel_basis(a::MatElem{T}) where T <: AbstractAlgebra.FieldElem = right_kernel_basis(transpose(a)) ################################################################################ # -# Concatenation of matrices +# Copy matrix into another matrix # ################################################################################ -@doc raw""" - vcat(A::Vector{Generic.Mat}) -> Generic.Mat - vcat(A::Array{ZZMatrix}, 1}) -> ZZMatrix - -Forms a big matrix by vertically concatenating the matrices in $A$. -All component matrices need to have the same number of columns. -""" -function vcat(A::Vector{T}) where {S <: RingElem, T <: MatElem{S}} - if any(x->ncols(x) != ncols(A[1]), A) - error("Matrices must have same number of columns") - end - M = zero_matrix(base_ring(A[1]), sum(nrows, A), ncols(A[1])) - s = 0 - for i=A - for j=1:nrows(i) - for k=1:ncols(i) - M[s+j, k] = i[j,k] - end - end - s += nrows(i) - end - return M -end - -function vcat(A::Vector{ZZMatrix}) - if any(x->ncols(x) != ncols(A[1]), A) - error("Matrices must have same number of columns") - end - M = zero_matrix(base_ring(A[1]), sum(nrows, A), ncols(A[1])) - s = 0 - for i=A - for j=1:nrows(i) - for k=1:ncols(i) - M[s+j, k] = i[j,k] +# Copy B into A at position (i, j) +function _copy_matrix_into_matrix(A::ZZMatrix, i::Int, j::Int, B::ZZMatrix) + @GC.preserve A B begin + for k in 0:nrows(B) - 1 + for l in 0:ncols(B) - 1 + d = ccall((:fmpz_mat_entry, libflint), + Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), B, k, l) + t = ccall((:fmpz_mat_entry, libflint), + Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), A, i - 1 + k, j - 1 + l) + ccall((:fmpz_set, libflint), Nothing, (Ptr{ZZRingElem}, Ptr{ZZRingElem}), t, d) end end - s += nrows(i) end - return M end -function vcat(A::Vector{zzModMatrix}) - if any(x->ncols(x) != ncols(A[1]), A) - error("Matrices must have same number of columns") - end - M = zero_matrix(base_ring(A[1]), sum(nrows, A), ncols(A[1])) - s = 0 - for i=A - for j=1:nrows(i) - for k=1:ncols(i) - M[s+j, k] = i[j,k] +function _copy_matrix_into_matrix!(A::QQMatrix, i::Int, j::Int, B::QQMatrix) + @GC.preserve A B begin + for k in 0:nrows(B) - 1 + for l in 0:ncols(B) - 1 + d = ccall((:fmpq_mat_entry, libflint), + Ptr{QQFieldElem}, (Ref{QQMatrix}, Int, Int), B, k, l) + t = ccall((:fmpq_mat_entry, libflint), + Ptr{QQFieldElem}, (Ref{QQMatrix}, Int, Int), A, i - 1 + k, j - 1 + l) + ccall((:fmpq_set, libflint), Nothing, (Ptr{QQFieldElem}, Ptr{QQFieldElem}), t, d) end end - s += nrows(i) end - return M end -function Base.vcat(A::MatElem...) - r = nrows(A[1]) - c = ncols(A[1]) - R = base_ring(A[1]) - for i=2:length(A) - @assert ncols(A[i]) == c - @assert base_ring(A[i]) == R - r += nrows(A[i]) - end - X = zero_matrix(R, r, c) - o = 1 - for i=1:length(A) - for j=1:nrows(A[i]) - X[o, :] = A[i][j, :] - o += 1 +function _copy_matrix_into_matrix(A::MatElem, r::Vector{Int}, c::Vector{Int}, B::MatElem) + for i = 1:length(r) + for j = 1:length(c) + A[r[i], c[j]] = B[i, j] end end - return X + return nothing end -function Base.hcat(A::Vector{T}) where {S <: RingElem, T <: MatElem{S}} - if any(x->nrows(x) != nrows(A[1]), A) - error("Matrices must have same number of rows") - end - M = zero_matrix(base_ring(A[1]), nrows(A[1]), sum(ncols, A)) - s = 0 - for i = A - for j=1:ncols(i) - for k=1:nrows(i) - M[k, s + j] = i[k,j] - end - end - s += ncols(i) - end - return M -end +@doc raw""" + is_positive_definite(a::ZZMatrix) -> Bool -function Base.hcat(A::MatElem...) - r = nrows(A[1]) - c = ncols(A[1]) - R = base_ring(A[1]) - for i=2:length(A) - @assert nrows(A[i]) == r - @assert base_ring(A[i]) == R - c += ncols(A[i]) - end - X = zero_matrix(R, r, c) - o = 1 - for i=1:length(A) - for j=1:ncols(A[i]) - X[:, o] = A[i][:, j] - o += 1 +Tests if $a$ is positive definite by testing if all principal minors +have positive determinant. +""" +function is_positive_definite(a::ZZMatrix) + for i=1:nrows(a) + if det(sub(a, 1:i, 1:i)) <= 0 + return false end end - return X + return true end -function Base.cat(A::MatElem...;dims) - @assert dims == (1,2) || isa(dims, Int) - - if isa(dims, Int) - if dims == 1 - return hcat(A...) - elseif dims == 2 - return vcat(A...) - else - error("dims must be 1, 2, or (1,2)") - end - end - local X - for i=1:length(A) - if i==1 - X = hcat(A[1], zero_matrix(base_ring(A[1]), nrows(A[1]), sum(Int[ncols(A[j]) for j=2:length(A)]))) - else - X = vcat(X, hcat(zero_matrix(base_ring(A[1]), nrows(A[i]), sum(ncols(A[j]) for j=1:i-1)), A[i], zero_matrix(base_ring(A[1]), nrows(A[i]), sum(Int[ncols(A[j]) for j=i+1:length(A)])))) - end - end - return X -end -#= seems to be in AA now -function Base.hvcat(rows::Tuple{Vararg{Int}}, A::MatElem...) - B = hcat([A[i] for i=1:rows[1]]...) - o = rows[1] - for j=2:length(rows) - C = hcat([A[i+o] for i=1:rows[j]]...) - o += rows[j] - B = vcat(B, C) - end - return B -end -=# ################################################################################ # # Smith normal form with trafo # ################################################################################ -#= -g, e,f = gcdx(a, b) -U = [1 0 ; -divexact(b, g)*f 1]*[1 1; 0 1]; -V = [e -divexact(b, g) ; f divexact(a, g)]; - -then U*[ a 0; 0 b] * V = [g 0 ; 0 l] -=# -@doc raw""" - snf_with_transform(A::ZZMatrix, l::Bool = true, r::Bool = true) -> ZZMatrix, ZZMatrix, ZZMatrix - -Given some integer matrix $A$, compute the Smith normal form (elementary -divisor normal form) of $A$. If `l` and/ or `r` are true, then the corresponding -left and/ or right transformation matrices are computed as well. -""" -function snf_with_transform(A::ZZMatrix, l::Bool = true, r::Bool = true) - if r - R = identity_matrix(FlintZZ, ncols(A)) - end - - if l - L = identity_matrix(FlintZZ, nrows(A)) - end - # TODO: if only one trafo is required, start with the HNF that does not - # compute the trafo - # Rationale: most of the work is on the 1st HNF.. - S = deepcopy(A) - while !is_diagonal(S) - if l - S, T = hnf_with_transform(S) - L = T*L - else - S = hnf!(S) - end - - if is_diagonal(S) - break - end - if r - S, T = hnf_with_transform(transpose(S)) - R = T*R - else - S = hnf!(transpose(S)) - end - S = transpose(S) - end - #this is probably not really optimal... - for i=1:min(nrows(S), ncols(S)) - if S[i,i] == 1 - continue - end - for j=i+1:min(nrows(S), ncols(S)) - if S[j,j] == 0 - continue - end - if S[i,i] != 0 && S[j,j] % S[i,i] == 0 - continue - end - g, e,f = gcdx(S[i,i], S[j,j]) - a = divexact(S[i,i], g) - S[i,i] = g - b = divexact(S[j,j], g) - S[j,j] *= a - if l - # U = [1 0; -b*f 1] * [ 1 1; 0 1] = [1 1; -b*f -b*f+1] - # so row i and j of L will be transformed. We do it naively - # those 2x2 transformations of 2 rows should be a c-primitive - # or at least a Nemo/Hecke primitive - for k=1:ncols(L) - x = -b*f -# L[i,k], L[j,k] = L[i,k]+L[j,k], x*L[i,k]+(x+1)*L[j,k] - L[i,k], L[j,k] = L[i,k]+L[j,k], x*(L[i,k]+L[j,k])+L[j,k] - end - end - if r - # V = [e -b ; f a]; - # so col i and j of R will be transformed. We do it naively - # careful: at this point, R is still transposed - for k=1:nrows(R) - R[i, k], R[j, k] = e*R[i,k]+f*R[j,k], -b*R[i,k]+a*R[j,k] - end - end - end - end - - # It might be the case that S was diagonal with negative diagonal entries. - for i in 1:min(nrows(S), ncols(S)) - if S[i, i] < 0 - if l - multiply_row!(L, ZZRingElem(-1), i) - end - S[i, i] = -S[i, i] - end - end - - if l - if r - return S, L, transpose(R) - else - # last is dummy - return S, L, L - end - elseif r - # second is dummy - return S, R, transpose(R) - else - # last two are dummy - return S, S, S - end -end - - function snf_for_groups(A::ZZMatrix, mod::ZZRingElem) R = identity_matrix(FlintZZ, ncols(A)) S = deepcopy(A) @@ -1519,137 +537,8 @@ function snf_for_groups(A::ZZMatrix, mod::ZZRingElem) return S, R end -################################################################################ -# -# IsUpper\Lower triangular -# -################################################################################ - -function isupper_triangular(M::MatElem) - n = nrows(M) - for i = 2:n - for j = 1:min(i-1, ncols(M)) - if !iszero(M[i, j]) - return false - end - end - end - return true -end - -function isupper_triangular(M::ZZMatrix) - GC.@preserve M begin - for i = 2:nrows(M) - for j = 1:min(i-1, ncols(M)) - t = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), M, i - 1, j - 1) - fl = ccall((:fmpz_is_zero, libflint), Bool, (Ref{ZZRingElem},), t) - if !fl - return false - end - end - end - end - return true -end - -function is_lower_triangular(M::ZZMatrix) - GC.@preserve M begin - for i = 1:nrows(M) - for j = i+1:ncols(M) - t = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), M, i - 1, j - 1) - fl = ccall((:fmpz_is_zero, libflint), Bool, (Ref{ZZRingElem},), t) - if !fl - return false - end - end - end - end - return true -end - -function is_lower_triangular(M::MatElem) - for i = 1:nrows(M) - for j = i+1:ncols(M) - if !iszero(M[i, j]) - return false - end - end - end - return true -end - - -################################################################################ -# -# Is diagonal -# -################################################################################ - -@doc raw""" - is_diagonal(A::Mat) - -Tests if $A$ is diagonal. -""" -function is_diagonal(A::MatElem) - for i = 1:ncols(A) - for j = 1:nrows(A) - if i != j && !iszero(A[j, i]) - return false - end - end - end - return true -end - -function is_diagonal(A::ZZMatrix) - for i = 1:ncols(A) - for j = 1:nrows(A) - if i != j - t = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), A, j - 1, i - 1) - fl = ccall((:fmpz_is_zero, libflint), Bool, (Ref{ZZRingElem},), t) - if !fl - return false - end - end - end - end - return true -end - -################################################################################ -# -# Diagonal -# -################################################################################ - -@doc raw""" - diagonal(A::Mat{T}) -> Vector{T} - -Returns the diagonal of `A` as an array. -""" -diagonal(A::MatrixElem{T}) where {T} = T[A[i, i] for i in 1:nrows(A)] - -################################################################################ -# -# Product of the diagonal entries -# -################################################################################ -function prod_diagonal(A::ZZMatrix) - a = one(ZZRingElem) - GC.@preserve a A begin - for i=1:nrows(A) - b = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), A, i - 1, i - 1) - ccall((:fmpz_mul, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ptr{ZZRingElem}), a, a, b) - end - end - return a -end -function prod_diagonal(A::MatrixElem{T}) where T - @assert nrows(A) == ncols(A) - return prod(T[A[i, i] for i = 1:nrows(A)]) -end ################################################################################ # @@ -1851,47 +740,6 @@ function solve_lt(A::MatElem{T}, b::Vector{T}) where T return x end -@doc raw""" - reduce_mod!(A::MatElem{T}, B::MatElem{T}) where T <: FieldElem - -For a reduced row echelon matrix $B$, reduce $A$ modulo $B$, i.e. all the pivot -columns will be zero afterwards. -""" -function reduce_mod!(A::MatElem{T}, B::MatElem{T}) where T <: FieldElem - if is_rref(B) - scale = false - else - scale = true - end - - for h=1:nrows(A) - j = 1 - for i=1:nrows(B) - while iszero(B[i, j]) - j += 1 - end - if scale - A[h, :] -= A[h, j] * (inv(B[i, j]) * B[i, :]) - else - A[h, :] -= A[h, j] * B[i, :] - end - end - end - return A -end - -@doc raw""" - reduce_mod(A::MatElem{T}, B::MatElem{T}) where T <: FieldElem -> MatElem - -For a reduced row echelon matrix $B$, reduce $A$ modulo $B$, i.e. all the pivot -columns will be zero afterwards. -""" -function reduce_mod(A::MatElem{T}, B::MatElem{T}) where T <: FieldElem - C = deepcopy(A) - reduce_mod!(C, B) - return C -end - """ pivots_of_ref(H::MatrixElem) -> Tuple{Int, BitVector} @@ -2005,6 +853,7 @@ julia> Hecke.non_pivot_cols_of_ref(QQ[0 2 2 2 2; 0 0 3 3 3; 0 0 0 4 4]) """ non_pivot_cols_of_ref(H::MatrixElem) = findall(!, pivots_of_ref(H)[2]) + #@doc raw""" # can_solve_with_solution(A::MatElem{T}, B::MatElem{T}; side = :right) where T <: FieldElem -> Bool, MatElem # @@ -2306,77 +1155,9 @@ Smith normal form of $A$. """ largest_elementary_divisor(A::ZZMatrix) = maximal_elementary_divisor(A) -################################################################################ -# -# Function to convert a matrix to array -# -################################################################################ - -function to_array(M::QQMatrix) - A = Vector{QQFieldElem}(undef, ncols(M)*nrows(M)) - for i = 1:nrows(M) - for j = 1:ncols(M) - A[(i-1)*ncols(M) + j] = M[i, j] - end - end - return A -end - -################################################################################ -# -# Minpoly and Charpoly -# -################################################################################ - -function minpoly(M::MatElem) - k = base_ring(M) - kx, x = polynomial_ring(k, cached = false) - return minpoly(kx, M) -end - -function charpoly(M::MatElem) - k = base_ring(M) - kx, x = polynomial_ring(k, cached = false) - return charpoly(kx, M) -end - -############################################################################### -# -# Sub -# -############################################################################### - -function sub(M::MatElem, rows::Vector{Int}, cols::Vector{Int}) - N = zero_matrix(base_ring(M), length(rows), length(cols)) - for i = 1:length(rows) - for j = 1:length(cols) - N[i, j] = M[rows[i], cols[j]] - end - end - return N -end -function sub(M::Nemo.MatElem{T}, r::UnitRange{<:Integer}, c::UnitRange{<:Integer}) where {T} - z = similar(M, length(r), length(c)) - for i in 1:length(r) - for j in 1:length(c) - z[i, j] = M[r[i], c[j]] - end - end - return z -end -################################################################################ -# -# Map Entries -# -################################################################################ -function map_entries(F::fpField, M::ZZMatrix) - MR = zero_matrix(F, nrows(M), ncols(M)) - ccall((:fmpz_mat_get_nmod_mat, libflint), Cvoid, (Ref{fpMatrix}, Ref{ZZMatrix}), MR, M) - return MR -end ################################################################################ # # Kernel of matrix over Z/nZ @@ -2495,20 +1276,6 @@ function invmod(M::ZZMatrix, d::ZZRingElem) end -function map_entries(R::Nemo.ZZModRing, M::ZZMatrix) - N = zero_matrix(R, nrows(M), ncols(M)) - GC.@preserve M N begin - for i = 1:nrows(M) - for j = 1:ncols(M) - m = ccall((:fmpz_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZMatrix}, Int, Int), M, i - 1, j - 1) - n = ccall((:fmpz_mod_mat_entry, libflint), Ptr{ZZRingElem}, (Ref{ZZModMatrix}, Int, Int), N, i - 1 , j - 1) - ccall((:fmpz_mod, libflint), Nothing, (Ptr{ZZRingElem}, Ptr{ZZRingElem}, Ref{ZZRingElem}), n, m, R.n) - end - end - end - return N -end - ################################################################################ # # multiplicative order ZZRingElem/QQMatrix From 530588805143df2f358c993a5c5bb11b138e23dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 29 Jun 2023 16:20:47 +0200 Subject: [PATCH 03/11] Remove obvious piracy from `Hecke/src/misc/Poly.jl` now in Nemo --- src/Aliases.jl | 1 - src/Misc/Poly.jl | 606 +---------------------------------------------- 2 files changed, 5 insertions(+), 602 deletions(-) diff --git a/src/Aliases.jl b/src/Aliases.jl index 9ca7f7f948..1899e95ea9 100644 --- a/src/Aliases.jl +++ b/src/Aliases.jl @@ -135,7 +135,6 @@ @alias isone_sided is_one_sided @alias ispairwise_coprime is_pairwise_coprime @alias ispositive_definite is_positive_definite -@alias ispositive_entry is_positive_entry @alias ispower_trager is_power_trager @alias ispower_unram is_power_unram @alias isprimary is_primary diff --git a/src/Misc/Poly.jl b/src/Misc/Poly.jl index 51474796fc..ac89e6460f 100644 --- a/src/Misc/Poly.jl +++ b/src/Misc/Poly.jl @@ -6,70 +6,7 @@ export rational_reconstruction, farey_lift, div, leading_coefficient, import Nemo: fmpz_mod_ctx_struct -function polynomial_ring(R::Ring; cached::Bool = false) - return polynomial_ring(R, "x", cached = cached) -end - -################################################################################ -# -# Content -# -################################################################################ - -function content(a::PolyElem{<: FieldElem}) - return one(base_ring(a)) -end - - -function ZZRingElem(a::Generic.ResidueRingElem{Nemo.ZZRingElem}) - return a.data -end - -function ZZRingElem(a::Nemo.zzModRingElem) - return ZZRingElem(a.data) -end - -function lift(::ZZRing, a::Generic.ResidueRingElem{Nemo.ZZRingElem}) - return a.data -end - -function (::ZZRing)(a::Generic.ResidueRingElem{Nemo.ZZRingElem}) - return a.data -end - -function lift(::ZZRing, a::Nemo.zzModRingElem) - return ZZRingElem(a.data) -end - -function (::ZZRing)(a::Nemo.zzModRingElem) - return ZZRingElem(a.data) -end - -function ZZRingElem(a::Nemo.ZZModRingElem) - return a.data -end - -function lift(::ZZRing, a::Nemo.ZZModRingElem) - return a.data -end - -function (::ZZRing)(a::Nemo.ZZModRingElem) - return a.data -end - -function div(f::PolyElem, g::PolyElem) - q, r = divrem(f,g) - return q -end - -function rem(f::PolyElem, g::PolyElem) - return mod(f, g) -end -function rem!(z::T, f::T, g::T) where T <: PolyElem - z = rem(f, g) - return z -end @doc raw""" induce_rational_reconstruction(a::ZZPolyRingElem, M::ZZRingElem) -> QQPolyRingElem @@ -91,25 +28,7 @@ function induce_rational_reconstruction(a::ZZPolyRingElem, M::ZZRingElem; parent return true, b end -function resultant(f::ZZPolyRingElem, g::ZZPolyRingElem, d::ZZRingElem, nb::Int) - z = ZZRingElem() - ccall((:fmpz_poly_resultant_modular_div, libflint), Nothing, - (Ref{ZZRingElem}, Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Ref{ZZRingElem}, Int), - z, f, g, d, nb) - return z -end - -function rem!(z::fqPolyRepPolyRingElem, x::fqPolyRepPolyRingElem, y::fqPolyRepPolyRingElem) - ccall((:fq_nmod_poly_rem, libflint), Nothing, (Ref{fqPolyRepPolyRingElem}, Ref{fqPolyRepPolyRingElem}, Ref{fqPolyRepPolyRingElem}, Ref{fqPolyRepField}), - z, x, y, base_ring(parent(x))) - return z -end -function rem!(z::FqPolyRepPolyRingElem, x::FqPolyRepPolyRingElem, y::FqPolyRepPolyRingElem) - ccall((:fq_poly_rem, libflint), Nothing, (Ref{FqPolyRepPolyRingElem}, Ref{FqPolyRepPolyRingElem}, Ref{FqPolyRepPolyRingElem}, Ref{FqPolyRepPolyRing}), - z, x, y, parent(x)) - return z -end ################################################################################ # @@ -396,111 +315,6 @@ function hensel_lift(f::ZZPolyRingElem, g::ZZPolyRingElem, p::ZZRingElem, k::Int return hensel_lift(f, g, h, p, k)[1] end -modulus(F::Generic.ResidueRing{ZZRingElem}) = F.modulus - -modulus(F::Generic.ResidueField{ZZRingElem}) = F.modulus - -function fmpq_poly_to_nmod_poly_raw!(r::zzModPolyRingElem, a::QQPolyRingElem) - ccall((:fmpq_poly_get_nmod_poly, libflint), Nothing, (Ref{zzModPolyRingElem}, Ref{QQPolyRingElem}), r, a) -end - -function fmpq_poly_to_gfp_poly_raw!(r::fpPolyRingElem, a::QQPolyRingElem) - ccall((:fmpq_poly_get_nmod_poly, libflint), Nothing, (Ref{fpPolyRingElem}, Ref{QQPolyRingElem}), r, a) -end - -function fmpq_poly_to_fq_default_poly_raw!(r::FqPolyRingElem, a::QQPolyRingElem, t1::ZZPolyRingElem = ZZPolyRingElem(), t2::ZZRingElem = ZZRingElem()) - ccall((:fmpq_poly_get_numerator, libflint), Nothing, (Ref{ZZPolyRingElem}, Ref{QQPolyRingElem}), t1, a) - ccall((:fq_default_poly_set_fmpz_poly, libflint), Nothing, (Ref{FqPolyRingElem}, Ref{ZZPolyRingElem}, Ref{FqField}), r, t1, r.parent.base_ring) - ccall((:fmpq_poly_get_denominator, libflint), Nothing, (Ref{ZZRingElem}, Ref{QQPolyRingElem}), t2, a) - if !isone(t2) - #res = ccall((:fmpz_invmod, libflint), Cint, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), t2, t2, characteristic(base_ring(r))) - #res - #@assert res != 0 - ccall((:fq_default_poly_scalar_div_fq_default, libflint), Nothing, (Ref{FqPolyRingElem}, Ref{FqPolyRingElem}, Ref{FqFieldElem}, Ref{FqField}), r, r, coefficient_ring(r)(t2), coefficient_ring(r)) - end -end - -function fmpq_poly_to_fmpz_mod_poly_raw!(r::ZZModPolyRingElem, a::QQPolyRingElem, t1::ZZPolyRingElem = ZZPolyRingElem(), t2::ZZRingElem = ZZRingElem()) - ccall((:fmpq_poly_get_numerator, libflint), Nothing, (Ref{ZZPolyRingElem}, Ref{QQPolyRingElem}), t1, a) - ccall((:fmpz_mod_poly_set_fmpz_poly, libflint), Nothing, (Ref{ZZModPolyRingElem}, Ref{ZZPolyRingElem}, Ref{fmpz_mod_ctx_struct}), r, t1, r.parent.base_ring.ninv) - ccall((:fmpq_poly_get_denominator, libflint), Nothing, (Ref{ZZRingElem}, Ref{QQPolyRingElem}), t2, a) - if !isone(t2) - res = ccall((:fmpz_invmod, libflint), Cint, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), t2, t2, modulus(base_ring(r))) - @assert res != 0 - ccall((:fmpz_mod_poly_scalar_mul_fmpz, libflint), Nothing, (Ref{ZZModPolyRingElem}, Ref{ZZModPolyRingElem}, Ref{ZZRingElem}, Ref{fmpz_mod_ctx_struct}), r, r, t2, r.parent.base_ring.ninv) - end -end - -function fmpq_poly_to_gfp_fmpz_poly_raw!(r::FpPolyRingElem, a::QQPolyRingElem, t1::ZZPolyRingElem = ZZPolyRingElem(), t2::ZZRingElem = ZZRingElem()) - ccall((:fmpq_poly_get_numerator, libflint), Nothing, (Ref{ZZPolyRingElem}, Ref{QQPolyRingElem}), t1, a) - ccall((:fmpz_mod_poly_set_fmpz_poly, libflint), Nothing, (Ref{FpPolyRingElem}, Ref{ZZPolyRingElem}, Ref{fmpz_mod_ctx_struct}), r, t1, r.parent.base_ring.ninv) - ccall((:fmpq_poly_get_denominator, libflint), Nothing, (Ref{ZZRingElem}, Ref{QQPolyRingElem}), t2, a) - if !isone(t2) - res = ccall((:fmpz_invmod, libflint), Cint, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), t2, t2, modulus(base_ring(r))) - @assert res != 0 - ccall((:fmpz_mod_poly_scalar_mul_fmpz, libflint), Nothing, (Ref{FpPolyRingElem}, Ref{FpPolyRingElem}, Ref{ZZRingElem}, Ref{fmpz_mod_ctx_struct}), r, r, t2, r.parent.base_ring.ninv) - end -end - -function fmpq_poly_to_nmod_poly(Rx::Nemo.zzModPolyRing, f::QQPolyRingElem) - g = Rx() - fmpq_poly_to_nmod_poly_raw!(g, f) - return g -end - -function fmpq_poly_to_gfp_poly(Rx::Nemo.fpPolyRing, f::QQPolyRingElem) - g = Rx() - fmpq_poly_to_gfp_poly_raw!(g, f) - return g -end - -function fmpz_poly_to_nmod_poly_raw!(r::zzModPolyRingElem, a::ZZPolyRingElem) - ccall((:fmpz_poly_get_nmod_poly, libflint), Nothing, - (Ref{zzModPolyRingElem}, Ref{ZZPolyRingElem}), r, a) - -end - -function fmpz_poly_to_gfp_poly_raw!(r::fpPolyRingElem, a::ZZPolyRingElem) - ccall((:fmpz_poly_get_nmod_poly, libflint), Nothing, - (Ref{fpPolyRingElem}, Ref{ZZPolyRingElem}), r, a) - -end - -function fmpz_poly_to_nmod_poly(Rx::Nemo.zzModPolyRing, f::ZZPolyRingElem) - g = Rx() - fmpz_poly_to_nmod_poly_raw!(g, f) - return g -end - -function fmpq_poly_to_fmpz_mod_poly(Rx::Nemo.ZZModPolyRing, f::QQPolyRingElem) - g = Rx() - fmpq_poly_to_fmpz_mod_poly_raw!(g, f) - return g -end - -function fmpq_poly_to_gfp_fmpz_poly(Rx::Nemo.FpPolyRing, f::QQPolyRingElem) - g = Rx() - fmpq_poly_to_gfp_fmpz_poly_raw!(g, f) - return g -end - -function fmpq_poly_to_fq_default_poly(Rx::Nemo.FqPolyRing, f::QQPolyRingElem) - g = Rx() - fmpq_poly_to_fq_default_poly_raw!(g, f) - return g -end - -function fmpz_poly_to_fmpz_mod_poly_raw!(r::ZZModPolyRingElem, a::ZZPolyRingElem) - ccall((:fmpz_poly_get_fmpz_mod_poly, libflint), Nothing, - (Ref{ZZModPolyRingElem}, Ref{ZZPolyRingElem}, Ref{fmpz_mod_ctx_struct}), r, a, r.parent.base_ring.ninv) - -end - -function fmpz_poly_to_fmpz_mod_poly(Rx::Nemo.ZZModPolyRing, f::ZZPolyRingElem) - g = Rx() - fmpz_poly_to_fmpz_mod_poly_raw!(g, f) - return g -end ################################################################################ # @@ -577,145 +391,6 @@ function rresx(f::ZZPolyRingElem, g::ZZPolyRingElem) return l, Zx(l*q), Zx(l*w) end - - - -################################################################################ -# -# QQPolyRingElem with denominator 1 to ZZPolyRingElem -# -################################################################################ - -function (a::ZZPolyRing)(b::QQPolyRingElem) - (!isone(denominator(b))) && error("Denominator has to be 1") - z = a() - ccall((:fmpq_poly_get_numerator, libflint), Nothing, - (Ref{ZZPolyRingElem}, Ref{QQPolyRingElem}), z, b) - return z -end - -############################################################## -# all of this should be in Nemo/AbstractAlgebra -# -#TODO: -# expand systematically for all finite fields -# and for ZZRingElem/QQFieldElem poly -# for fun: is_power(a::nf_elem) -# - -function factor(f::QQPolyRingElem, R::T) where T <: Union{Nemo.fqPolyRepField, Nemo.fpField} - Rt, t = polynomial_ring(R, "t", cached=false) - return factor(Rt(f)) -end - -function roots(f::QQPolyRingElem, R::T) where T <: Union{Nemo.fqPolyRepField, Nemo.fpField} - Rt, t = polynomial_ring(R, "t", cached=false) - fp = polynomial_ring(FlintZZ, cached = false)[1](f*denominator(f)) - fpp = Rt(fp) - return roots(fpp) -end - -function roots(f::fpPolyRingElem, K::fqPolyRepField) - @assert characteristic(K) == characteristic(base_ring(f)) - Kx = polynomial_ring(K, cached = false)[1] - coeffsff = Vector{elem_type(K)}(undef, degree(f)+1) - for i=0:degree(f) - coeffsff[i+1] = K(lift(coeff(f, i))) - end - ff = Kx(coeffsff) - return roots(ff) -end - -function roots(f::FpPolyRingElem, K::FqPolyRepField) - @assert characteristic(K) == characteristic(base_ring(f)) - Kx = polynomial_ring(K, cached = false)[1] - coeffsff = Vector{FqPolyRepFieldElem}(undef, degree(f)+1) - for i=0:degree(f) - coeffsff[i+1] = K(lift(coeff(f, i))) - end - ff = Kx(coeffsff) - return roots(ff) -end - -function is_power(a::Union{fqPolyRepFieldElem, FqPolyRepFieldElem, FqFieldElem}, m::Int) - if iszero(a) - return true, a - end - s = order(parent(a)) - if gcd(s - 1, m) == 1 - return true, a^invmod(FlintZZ(m), s-1) - end - St, t = polynomial_ring(parent(a), "t", cached=false) - f = t^m-a - rt = roots(f) - if length(rt) > 0 - return true, rt[1] - else - return false, a - end -end - -function roots(f::T) where T <: Union{fqPolyRepPolyRingElem, FqPolyRepPolyRingElem} # should be in Nemo and - # made available for all finite fields I guess. - q = size(base_ring(f)) - x = gen(parent(f)) - if degree(f) < q - x = powermod(x, q, f)-x - else - x = x^Int(q)-x - end - f = gcd(f, x) - l = factor(f).fac - return elem_type(base_ring(f))[-divexact(constant_coefficient(x), leading_coefficient(x)) for x = keys(l) if degree(x)==1] -end - - -function setcoeff!(z::fqPolyRepPolyRingElem, n::Int, x::ZZRingElem) - ccall((:fq_nmod_poly_set_coeff_fmpz, libflint), Nothing, - (Ref{fqPolyRepPolyRingElem}, Int, Ref{ZZRingElem}, Ref{fqPolyRepField}), - z, n, x, base_ring(parent(z))) - return z -end - -############################################################################### -# -# Sturm sequence -# -############################################################################### - -function _divide_by_content(f::ZZPolyRingElem) - p = primpart(f) - if sign(leading_coefficient(f))== sign(leading_coefficient(p)) - return p - else - return -p - end -end - -function sturm_sequence(f::ZZPolyRingElem) - g = f - h = _divide_by_content(derivative(g)) - seq = ZZPolyRingElem[g,h] - while true - r = _divide_by_content(pseudorem(g,h)) - # r has the same sign as pseudorem(g, h) - # To get a pseudo remainder sequence for the Sturm sequence, - # we need r to be the pseudo remainder of |lc(b)|^(a - b + 1), - # so we need some adjustment. See - # https://en.wikipedia.org/wiki/Polynomial_greatest_common_divisor#Sturm_sequence_with_pseudo-remainders - if leading_coefficient(h) < 0 && isodd(degree(g) - degree(h) + 1) - r = -r - end - if r != 0 - push!(seq, -r) - g, h = h, -r - else - break - end - end - return seq -end - function _number_of_sign_changes(a::Vector{Int}) nc = 0 filter!(x -> x != 0, a) @@ -867,103 +542,6 @@ function _n_positive_roots_sqf(f::PolyElem{nf_elem}, P::NumFieldEmb; start_prec: end end -################################################################################ -# -# Squarefree factorization in characteristic 0 -# -################################################################################ - -# TODO: Implement the things from -# "Square-Free Algorithms in Positive Characteristic" by Gianni--Trager -# This should avoid the full factorization for function fields -# (and/or finitely generated fields in general?!) - -function factor_squarefree(f::PolyElem{<:FieldElement}) - R = coefficient_ring(f) - if iszero(characteristic(R)) - return _factor_squarefree_char_0(f) - else - fac = factor(f) - es = unique!([e for (p, e) in fac]) - facs = Vector{typeof(f)}(undef, length(es)) - for i in 1:length(facs) - facs[i] = one(parent(f)) - end - for (p, e) in fac - i = findfirst(isequal(e), es) - facs[i] *= p - end - end - return Fac(unit(fac), - Dict{typeof(f), Int}(facs[i] => es[i] for i in 1:length(es))) -end - - -# This is Musser's algorithm -function _factor_squarefree_char_0(f::PolyElem) - @assert iszero(characteristic(base_ring(f))) - res = Dict{typeof(f), Int}() - if is_constant(f) - return Fac(f, res) - end - c = leading_coefficient(f) - f = divexact(f, c) - di = gcd(f, derivative(f)) - if isone(di) - res[f] = 1 - return Fac(parent(f)(c), res) - end - ei = divexact(f, di) - i = 1 - while !is_constant(ei) - eii = gcd(di, ei) - dii = divexact(di, eii) - if degree(eii) != degree(ei) - res[divexact(ei, eii)] = i - end - i = i +1 - di = dii - ei = eii - end - return Fac(parent(f)(c), res) -end - - -function factor_equal_deg(x::fpPolyRingElem, d::Int) - if degree(x) == d - return fpPolyRingElem[x] - end - fac = Nemo.gfp_poly_factor(x.mod_n) - ccall((:nmod_poly_factor_equal_deg, libflint), UInt, - (Ref{Nemo.gfp_poly_factor}, Ref{fpPolyRingElem}, Int), - fac, x, d) - res = Vector{fpPolyRingElem}(undef, fac.num) - for i in 1:fac.num - f = parent(x)() - ccall((:nmod_poly_factor_get_poly, libflint), Nothing, - (Ref{fpPolyRingElem}, Ref{Nemo.gfp_poly_factor}, Int), f, fac, i-1) - res[i] = f - end - return res -end - -function factor_equal_deg(x::FpPolyRingElem, d::Int) - if degree(x) == d - return FpPolyRingElem[x] - end - fac = Nemo.gfp_fmpz_poly_factor(base_ring(x)) - ccall((:fmpz_mod_poly_factor_equal_deg, libflint), UInt, - (Ref{Nemo.gfp_fmpz_poly_factor}, Ref{FpPolyRingElem}, Int, Ref{fmpz_mod_ctx_struct}), - fac, x, d, x.parent.base_ring.ninv) - res = Vector{FpPolyRingElem}(undef, fac.num) - for i in 1:fac.num - f = parent(x)() - ccall((:fmpz_mod_poly_factor_get_fmpz_mod_poly, libflint), Nothing, - (Ref{FpPolyRingElem}, Ref{Nemo.gfp_fmpz_poly_factor}, Int, Ref{fmpz_mod_ctx_struct}), f, fac, i-1, x.parent.base_ring.ninv) - res[i] = f - end - return res -end ################################################################################ # @@ -1095,83 +673,20 @@ end There are also bounds on the coefficients which are sometimes tight =# -function mulhigh_n(a::ZZPolyRingElem, b::ZZPolyRingElem, n::Int) - c = parent(a)() - #careful: as part of the interface, the coeffs 0 - (n-1) are random garbage - ccall((:fmpz_poly_mulhigh_n, libflint), Nothing, (Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Ref{ZZPolyRingElem}, Cint), c, a, b, n) - return c -end -function mulhigh(a::PolyElem{T}, b::PolyElem{T}, n::Int) where {T} - return mulhigh_n(a, b, degree(a) + degree(b) - n) -end - - -function roots(f::ZZPolyRingElem, ::QQField; max_roots::Int = degree(f)) - if degree(f) < 1 - return QQFieldElem[] - end - if degree(f) == 1 - return QQFieldElem[-constant_coefficient(f)//leading_coefficient(f)] - end - - g = gcd(f, derivative(f)) - if isone(g) - h = f - else - h = divexact(f, g) - end - if degree(h) == 1 - return QQFieldElem[-constant_coefficient(h)//leading_coefficient(h)] - end - h = primpart(h) - - global p_start - p = p_start - bd = leading_coefficient(h)+maximum(abs, coefficients(h)) - while true - p = next_prime(p) - k = GF(p) - hp = change_base_ring(k, h) - if !is_squarefree(hp) - continue - end - k = ceil(Int, log(bd)/log(p)) - Hp = factor_mod_pk(h, p, k) - pk = ZZRingElem(p)^k - r = QQFieldElem[mod_sym(-constant_coefficient(x)*leading_coefficient(h), pk)//leading_coefficient(h) for x = keys(Hp) if degree(x) == 1] - return [x for x = r if iszero(f(x)) ] - end -end - -function roots(f::ZZPolyRingElem; max_roots::Int = degree(f)) - r = roots(f, FlintQQ, max_roots = max_roots) - return ZZRingElem[FlintZZ(x) for x = r if denominator(x) == 1] -end - -function roots(f::QQPolyRingElem; max_roots::Int = degree(f)) - Zx, x = polynomial_ring(FlintZZ, cached = false) - g = Zx(denominator(f)*f) - return roots(g, FlintQQ) -end - -function roots(f::Union{ZZPolyRingElem, QQPolyRingElem}, R::AcbField, abs_tol::Int=R.prec, initial_prec::Int...) +function roots(f::Union{ZZPolyRingElem,QQPolyRingElem}, R::AcbField, abs_tol::Int=R.prec, initial_prec::Int...) lf = factor(f) return map(R, vcat([_roots(g, abs_tol, initial_prec...) for g = keys(lf.fac) if degree(g) > 0]...)) end -function _roots(f::QQPolyRingElem, ::PosInf; prec::Int = 64) +function _roots(f::QQPolyRingElem, ::PosInf; prec::Int=64) g = squarefree_part(f) - all_rts = _roots(g, prec) + all_rts = _roots(g, prec) rl_rts = real.(filter(isreal, all_rts)) compl_rts = filter(x -> !isreal(x) && ispositive(imag(x)), all_rts) @assert length(rl_rts) + 2 * length(compl_rts) == degree(g) return all_rts, rl_rts, compl_rts end -function (f::acb_poly)(x::acb) - return evaluate(f, x) -end - function factor(f::Union{ZZPolyRingElem, QQPolyRingElem}, R::AcbField, abs_tol::Int=R.prec, initial_prec::Int...) g = factor(f) d = Dict{acb_poly, Int}() @@ -1184,18 +699,6 @@ function factor(f::Union{ZZPolyRingElem, QQPolyRingElem}, R::AcbField, abs_tol:: return Fac(Rt(g.unit), d) end -function roots(f::Union{ZZPolyRingElem, QQPolyRingElem}, R::ArbField, abs_tol::Int=R.prec, initial_prec::Int...) - g = factor(f) - r = elem_type(R)[] - C = AcbField(precision(R)) - for k = keys(g.fac) - s, _ = signature(k) - rt = roots(k, C) - append!(r, map(real, rt[1:s])) - end - return r -end - function factor(f::Union{ZZPolyRingElem, QQPolyRingElem}, R::ArbField, abs_tol::Int=R.prec, initial_prec::Int...) g = factor(f) d = Dict{arb_poly, Int}() @@ -1243,61 +746,9 @@ function gcd_with_failure(a::Generic.Poly{T}, b::Generic.Poly{T}) where T return one(parent(d)), divexact(b, d) end -function mod(f::AbstractAlgebra.PolyElem{T}, g::AbstractAlgebra.PolyElem{T}) where {T <: RingElem} - check_parent(f, g) - if length(g) == 0 - throw(DivideError()) - end - if length(f) >= length(g) - f = deepcopy(f) - b = leading_coefficient(g) - g = inv(b)*g - c = base_ring(f)() - while length(f) >= length(g) - l = -leading_coefficient(f) - for i = 1:length(g) - c = mul!(c, coeff(g, i - 1), l) - u = coeff(f, i + length(f) - length(g) - 1) - u = addeq!(u, c) - f = setcoeff!(f, i + length(f) - length(g) - 1, u) - end - set_length!(f, normalise(f, length(f) - 1)) - end - end - return f -end -Nemo.normalise(f::ZZPolyRingElem, ::Int) = degree(f)+1 -Nemo.set_length!(f::ZZPolyRingElem, ::Int) = nothing -function Base.divrem(f::AbstractAlgebra.PolyElem{T}, g::AbstractAlgebra.PolyElem{T}) where {T <: RingElem} - check_parent(f, g) - if length(g) == 0 - throw(DivideError()) - end - if length(f) < length(g) - return zero(parent(f)), f - end - f = deepcopy(f) - binv = inv(leading_coefficient(g)) - g = divexact(g, leading_coefficient(g)) - qlen = length(f) - length(g) + 1 - q = zero(parent(f)) - fit!(q, qlen) - c = zero(base_ring(f)) - while length(f) >= length(g) - q1 = leading_coefficient(f) - l = -q1 - q = setcoeff!(q, length(f) - length(g), q1*binv) - for i = 1:length(g) - c = mul!(c, coeff(g, i - 1), l) - u = coeff(f, i + length(f) - length(g) - 1) - u = addeq!(u, c) - f = setcoeff!(f, i + length(f) - length(g) - 1, u) - end - set_length!(f, normalise(f, length(f) - 1)) - end - return q, f -end + + @doc raw""" @@ -1358,52 +809,6 @@ function prod1(a::Vector{T}; inplace::Bool = false) where T <: PolyElem return prod1(anew, inplace = true) end -################################################################################ -# -# Random polynomial -# -################################################################################ - -@doc raw""" - Base.rand(Rt::PolyRing{T}, n::Int) where T <: ResElem{ZZRingElem} -> PolyElem{T} - -Find a random polynomial of degree=$n$. -""" -function Base.rand(Rt::PolyRing{T}, n::Int) where T <: ResElem{ZZRingElem} - f = Rt() - R = base_ring(Rt) - for i = 0:n - setcoeff!(f, i, rand(R)) - end - return f -end - -################################################################################ -# -# Squarefreeness -# -################################################################################ - -function is_squarefree(f::PolyElem) - R = coefficient_ring(f) - - if iszero(f) || degree(f) == 0 - return true - end - - if !is_monic(f) - g = divexact(f, leading_coefficient(f)) - else - g = f - end - - if characteristic(R) == 0 || R isa FinField - return is_constant(gcd(g, derivative(g))) - else - fac = factor_squarefree(g) - return all(e <= 1 for (_, e) in fac) - end -end ################################################################################ # @@ -1516,4 +921,3 @@ end Base.IteratorSize(::FactorsOfSquarefree) = Base.SizeUnknown() Base.eltype(::FactorsOfSquarefree{T}) where T = T - From 84a0f0010b19b9df74c7f5db5c0d4cbb4ea02563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 29 Jun 2023 16:54:04 +0200 Subject: [PATCH 04/11] Remove obvious piracy from different places now in Nemo --- src/LinearAlgebra/FakeFmpqMat.jl | 6 ------ src/LocalField/Completions.jl | 4 +--- src/LocalField/Conjugates.jl | 5 ----- src/Map/FiniteField.jl | 16 -------------- src/Map/NfOrd.jl | 36 -------------------------------- src/Misc/Arb.jl | 30 -------------------------- src/Misc/coprime.jl | 4 ---- 7 files changed, 1 insertion(+), 100 deletions(-) diff --git a/src/LinearAlgebra/FakeFmpqMat.jl b/src/LinearAlgebra/FakeFmpqMat.jl index cb278e8e73..3d14d77776 100644 --- a/src/LinearAlgebra/FakeFmpqMat.jl +++ b/src/LinearAlgebra/FakeFmpqMat.jl @@ -224,12 +224,6 @@ function QQMatrix(x::FakeFmpqMat) return z end -function QQMatrix(x::ZZMatrix) - z = zero_matrix(FlintQQ, nrows(x), ncols(x)) - ccall((:fmpq_mat_set_fmpz_mat, libflint), Nothing, (Ref{QQMatrix}, Ref{ZZMatrix}), z, x) - return z -end - function _fmpq_mat_to_fmpz_mat_den(x::QQMatrix) z = zero_matrix(FlintZZ, nrows(x), ncols(x)) d = ZZRingElem() diff --git a/src/LocalField/Completions.jl b/src/LocalField/Completions.jl index 41038b2e75..0faa96e773 100644 --- a/src/LocalField/Completions.jl +++ b/src/LocalField/Completions.jl @@ -256,9 +256,7 @@ end return coeffs_eisenstein, xZp end -function round(::Type{Int}, a::QQFieldElem) - return round(Int, Rational{BigInt}(a)) -end + function setprecision!(f::CompletionMap{LocalField{qadic, EisensteinLocalField}, LocalFieldElem{qadic, EisensteinLocalField}}, new_prec::Int) P = prime(f) diff --git a/src/LocalField/Conjugates.jl b/src/LocalField/Conjugates.jl index 9f41b8a64c..5c2684d19e 100644 --- a/src/LocalField/Conjugates.jl +++ b/src/LocalField/Conjugates.jl @@ -389,11 +389,6 @@ function regulator_iwasawa(R::NfAbsOrd, C::qAdicConj, n::Int = 10) return regulator_iwasawa([mu(u[i]) for i=2:ngens(u)], C, n) end -function matrix(a::Vector{Vector{T}}) where {T} - return matrix(permutedims(reduce(hcat, a), (2, 1))) -end - - function eval_f_fs(f::PolyElem, x::RingElem) d = Int[] for i=1:degree(f) diff --git a/src/Map/FiniteField.jl b/src/Map/FiniteField.jl index 3727b89198..1c5e68a647 100644 --- a/src/Map/FiniteField.jl +++ b/src/Map/FiniteField.jl @@ -1,19 +1,3 @@ -function prime_field(K::NumField) - return QQField() -end - -function prime_field(F::fqPolyRepField; cached::Bool = true) - return Native.GF(Int(characteristic(F)), cached = cached) -end - -function prime_field(F::FqPolyRepField; cached::Bool = true) - return Native.GF(characteristic(F), cached = cached) -end - -function prime_field(F::T; cached::Bool = true) where T <: Union{fpField, FpField} - return F -end - function hom(F::FinField, K::FinField, a::FinFieldElem; check::Bool = true) @assert parent(a) == K diff --git a/src/Map/NfOrd.jl b/src/Map/NfOrd.jl index 2910049b11..94512afae3 100644 --- a/src/Map/NfOrd.jl +++ b/src/Map/NfOrd.jl @@ -179,42 +179,6 @@ function Mor(O::NfOrd, F::fqPolyRepField, h::fpPolyRingElem) return NfOrdToFqNmodMor(O, F, h) end - -function evaluate(f::ZZPolyRingElem, r::fqPolyRepFieldElem) - #Horner - stolen from Claus - - if length(f) == 0 - return parent(r)() - end - - l = f.length-1 - s = parent(r)(coeff(f, l)) - for i =l-1:-1:0 - s = s*r + parent(r)(coeff(f, i)) - end - return s -end - -function evaluate!(z::fqPolyRepFieldElem, f::ZZPolyRingElem, r::fqPolyRepFieldElem) - #Horner - stolen from Claus - - zero!(z) - - if length(f) == 0 - return z - end - - l = f.length-1 - set!(z, parent(r)(coeff(f, l))) - #s = parent(r)(coeff(f, l)) - for i =l-1:-1:0 - mul!(z, z, r) - add!(z, z, parent(r)(coeff(f, i))) - #s = s*r + parent(r)(coeff(f, i)) - end - return z -end - function _get_coeff_raw(x::fqPolyRepFieldElem, i::Int) u = ccall((:nmod_poly_get_coeff_ui, libflint), UInt, (Ref{fqPolyRepFieldElem}, Int), x, i) return u diff --git a/src/Misc/Arb.jl b/src/Misc/Arb.jl index 7903d8e84e..a0a47a5aa9 100644 --- a/src/Misc/Arb.jl +++ b/src/Misc/Arb.jl @@ -26,12 +26,6 @@ function muleq!(z::arb, x::arb, y::ZZRingElem) return nothing end -function addmul!(z::arb, x::arb, y::ZZRingElem) - q = max(bits(z), bits(x)) - ccall((:arb_addmul_fmpz, libarb), Nothing, (Ref{arb}, Ref{arb}, Ref{ZZRingElem}, Int), z, x, y, q) - return nothing -end - function abs!(z::arb, x::arb) ccall((:arb_abs, libarb), Nothing, (Ref{arb}, Ref{arb}, Int), z, x, parent(x).prec) return nothing @@ -155,30 +149,6 @@ function lower_bound(x::arb, ::Type{ZZRingElem}) return bound end -################################################################################ -# -# Signs for arb and acb -# -################################################################################ - -function sign(::Type{Int}, x::arb) - if is_positive(x) - return 1 - elseif is_negative(x) - return -1 - else - error("Could not determine sign") - end -end - -function sign(::Type{Int}, x::acb) - if isreal(x) - return sign(Int, real(x)) - else - error("Element is not real") - end -end - ################################################################################ # # Maximum diff --git a/src/Misc/coprime.jl b/src/Misc/coprime.jl index ff8d3abe5d..7b26ce6319 100644 --- a/src/Misc/coprime.jl +++ b/src/Misc/coprime.jl @@ -26,10 +26,6 @@ function mul_into!(a::ZZRingElem, b::ZZRingElem, c::ZZRingElem) return a end -function copy(a::ZZRingElem) - return deepcopy(a) -end - function copy_into!(a, b) return b end From a511e60c6ebdeafe7cebc2e159ca9068ea264ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 29 Jun 2023 17:12:42 +0200 Subject: [PATCH 05/11] Remove obvious piracy from `Hecke/src/misc/FiniteField.jl` now in Nemo --- src/Misc/FiniteField.jl | 299 ---------------------------------------- 1 file changed, 299 deletions(-) diff --git a/src/Misc/FiniteField.jl b/src/Misc/FiniteField.jl index 1283e96d03..f0b521efd0 100644 --- a/src/Misc/FiniteField.jl +++ b/src/Misc/FiniteField.jl @@ -1,141 +1,3 @@ -# additional constructors - -function FlintFiniteField(p::Integer; cached::Bool = true) - @assert is_prime(p) - k = GF(p, cached=cached) - return k, k(1) -end - -function FlintFiniteField(p::ZZRingElem; cached::Bool = true) - @assert is_prime(p) - k = GF(p, cached=cached) - return k, k(1) -end - -GF(p::Integer, k::Int, s::VarName=:o; cached::Bool = true) = FlintFiniteField(p, k, s, cached = cached)[1] -GF(p::ZZRingElem, k::Int, s::VarName=:o; cached::Bool = true) = FlintFiniteField(p, k, s, cached = cached)[1] - -## -## rand for Flint-Finite fields -## -## fqPolyRepFieldElem has no base ring -function rand(R::fqPolyRepField) - #gen is not a generator for the group! - r = R(0) - for i=0:degree(R) - r *= gen(R) - r += rand(1:characteristic(R)) - end - - return r -end - -function rand(R::FqPolyRepField) - #gen is not a generator for the group! - r = R(0) - for i=0:degree(R) - r *= gen(R) - r += rand(1:characteristic(R)) - end - - return r -end - -################################################################################ -# -# Iterators for finite fields -# -################################################################################ - -# fqPolyRepField - -function Base.iterate(F::fqPolyRepField) - return zero(F), zeros(UInt, degree(F)) -end - -function Base.iterate(F::fqPolyRepField, st::Vector{UInt}) - done = true - for j in 1:length(st) - if st[j] != F.n - 1 - done = false - end - end - - if done - return nothing - end - - st[1] = st[1] + 1 - for j in 1:(length(st) - 1) - if st[j] == F.n - st[j] = 0 - st[j + 1] = st[j + 1] + 1 - end - end - - d = F() - ccall((:fq_nmod_init2, libflint), Nothing, - (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), d, F) - for j in 1:length(st) - ccall((:nmod_poly_set_coeff_ui, libflint), Nothing, - (Ref{fqPolyRepFieldElem}, Int, UInt), d, j - 1, st[j]) - end - - return d, st -end - -Base.length(F::fqPolyRepField) = BigInt(F.n)^degree(F) - -Base.IteratorSize(::Type{fqPolyRepField}) = Base.HasLength() - -Base.eltype(::fqPolyRepField) = fqPolyRepFieldElem - -# FqPolyRepField - -function Base.iterate(F::FqPolyRepField) - return zero(F), zeros(FlintZZ, degree(F)) -end - -function Base.iterate(F::FqPolyRepField, st::Vector{ZZRingElem}) - done = true - for j in 1:length(st) - if st[j] != characteristic(F) - 1 - done = false - end - end - - if done - return nothing - end - - st[1] = st[1] + 1 - for j in 1:(length(st) - 1) - if st[j] == characteristic(F) - st[j] = 0 - st[j + 1] = st[j + 1] + 1 - end - end - - d = F() - ccall((:fq_init2, libflint), Nothing, - (Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}), d, F) - g = Hecke.Globals.Zx() - for j in 1:length(st) - #ccall((:fmpz_poly_set_coeff_fmpz, libflint), (Ref{ZZPolyRingElem}, Int, Ref{ZZRingElem}), g, j - 1, st[j]) - ccall((:fmpz_poly_set_coeff_fmpz, libflint), Nothing, - (Ref{FqPolyRepFieldElem}, Int, Ref{ZZRingElem}), d, j - 1, st[j]) - end - - return d, st -end - -Base.eltype(::FqPolyRepField) = FqPolyRepFieldElem - -Base.length(F::FqPolyRepField) = BigInt(characteristic(F)^degree(F)) - -Base.IteratorSize(::Type{FqPolyRepField}) = Base.HasLength() - -sub!(z::T, x::T, y::T) where {T} = x - y function _reduce(a::fqPolyRepFieldElem) A = parent(a) @@ -237,19 +99,6 @@ function _nf_to_fq!(a::FqFieldElem, b::nf_elem, K::FqField)#, a_tmp::FpPolyRingE return a end -function (A::fqPolyRepField)(x::fpFieldElem) - @assert characteristic(A) == characteristic(parent(x)) - return A(lift(x)) -end - -function (A::FqPolyRepField)(x::Generic.ResidueFieldElem{ZZRingElem}) - @assert characteristic(A) == characteristic(parent(x)) - return A(lift(x)) -end - -Nemo.promote_rule(::Type{fqPolyRepFieldElem}, ::Type{fpFieldElem}) = fqPolyRepFieldElem - -Nemo.promote_rule(::Type{FqPolyRepFieldElem}, ::Type{FpFieldElem}) = FqPolyRepFieldElem ################################################################################ # @@ -283,58 +132,6 @@ function has_primitive_root_1(K::Nemo.fqPolyRepField, m::Int) end -## Minpoly/ Charpoly - -function minpoly(a::fqPolyRepFieldElem) - return minpoly(polynomial_ring(Native.GF(Int(characteristic(parent(a))), cached = false), cached = false)[1], a) -end - -function minpoly(Rx::fpPolyRing, a::fqPolyRepFieldElem) - c = [a] - fa = frobenius(a) - while !(fa in c) - push!(c, fa) - fa = frobenius(fa) - end - St = polynomial_ring(parent(a), cached = false)[1] - f = prod([gen(St) - x for x = c], init = one(St)) - g = Rx() - for i = 0:degree(f) - setcoeff!(g, i, coeff(coeff(f, i), 0)) - end - return g -end - -function charpoly(a::fqPolyRepFieldElem) - return charpoly(polynomial_ring(Native.GF(Int(characteristic(parent(a))), cached = false), cached = false)[1], a) -end - -function charpoly(Rx::fpPolyRing, a::fqPolyRepFieldElem) - g = minpoly(Rx, a) - return g^div(degree(parent(a)), degree(g)) -end - - -function minpoly(a::FqPolyRepFieldElem) - return minpoly(polynomial_ring(Native.GF(characteristic(parent(a)), cached = false), cached = false)[1], a) -end - -function minpoly(Rx::FpPolyRing, a::FqPolyRepFieldElem) - c = [a] - fa = frobenius(a) - while !(fa in c) - push!(c, fa) - fa = frobenius(fa) - end - St = polynomial_ring(parent(a), cached = false)[1] - f = prod([gen(St) - x for x = c]) - g = Rx() - for i = 0:degree(f) - setcoeff!(g, i, coeff(coeff(f, i), 0)) - end - return g -end - ################################################################################ # # Generators multiplicative group @@ -414,46 +211,6 @@ function unit_group(F::T; n_quo::Int = -1) where T <: FinField return G, mG end -################################################################################ -# -# Missing ad hoc operations -# -################################################################################ - -function (R::FqPolyRepField)(x::Nemo.FpFieldElem) - return R(lift(x)) -end - -function *(a::Nemo.FqPolyRepFieldElem, b::Nemo.FpFieldElem) - return a * parent(a)(b) -end - -function *(a::Nemo.FpFieldElem, b::Nemo.FqPolyRepFieldElem) - return parent(b)(a) * b -end -function Hecke.preimage(phi::Nemo.FinFieldMorphism, x::FinFieldElem) - return preimage_map(phi)(x) -end - -function (R::Nemo.zzModRing)(a::Nemo.fpFieldElem) - @assert modulus(R) == characteristic(parent(a)) - return R(data(a)) -end - -function (k::Nemo.fqPolyRepField)(a::QQFieldElem) - return k(numerator(a))//k(denominator(a)) -end - -function (k::Nemo.FpField)(a::QQFieldElem) - return k(numerator(a))//k(denominator(a)) -end - -function (k::Nemo.FqPolyRepField)(a::QQFieldElem) - return k(numerator(a))//k(denominator(a)) -end - - -(F::Nemo.fqPolyRepField)(a::Nemo.zzModRingElem) = F(a.data) #to avoid using embed - which is (more me) still broken.. # it accumulates fields until the machine dies @@ -470,32 +227,6 @@ function find_morphism(k::fqPolyRepField, K::fqPolyRepField) return phi end -#TODO/ think: -# - should those be zzModMatrix of fpMatrix -# - base_ring/ coeff_field needs to be unique? -function representation_matrix(a::fpFieldElem) - return matrix(parent(a), 1, 1, [a]) -end - -function representation_matrix(a::fqPolyRepFieldElem) - F = parent(a) - k = quo(ZZ, Int(characteristic(F)))[1] - k = Native.GF(Int(characteristic(F))) - m = zero_matrix(k, degree(F), degree(F)) - ccall((:fq_nmod_embed_mul_matrix, libflint), Nothing, (Ref{fpMatrix}, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), m, a, F) - ccall((:nmod_mat_transpose, libflint), Nothing, (Ref{fpMatrix}, Ref{fpMatrix}), m, m) - return m -end - -function frobenius_matrix(F::fqPolyRepField, n::Int=1) - a = frobenius(gen(F), n) - k = quo(ZZ, Int(characteristic(F)))[1] - k = Native.GF(Int(characteristic(F))) - m = zero_matrix(k, degree(F), degree(F)) - ccall((:fq_nmod_embed_composition_matrix_sub, libflint), Nothing, (Ref{fpMatrix}, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}, Int), m, a, F, degree(F)) - ccall((:nmod_mat_transpose, libflint), Nothing, (Ref{fpMatrix}, Ref{fpMatrix}), m, m) - return m -end mutable struct VeryBad entries::Ptr{Nothing} @@ -562,36 +293,6 @@ function apply!(b::fqPolyRepFieldElem, a::fqPolyRepFieldElem, F::FrobeniusCtx) return b end -function frobenius!(a::fqPolyRepFieldElem, b::fqPolyRepFieldElem, i::Int = 1) - ccall((:fq_nmod_frobenius, libflint), Nothing, - (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepFieldElem}, Int, Ref{fqPolyRepField}), - a, b, i, a.parent) -end - -################################################################################ -# -# Defining polynomial for finite fields -# -################################################################################ - -defining_polynomial(F::FqPolyRepField) = minpoly(gen(F)) - -function defining_polynomial(Q::fqPolyRepField, P::Ring = Native.GF(Int(characteristic(Q)), cached = false)) - Pt, t = polynomial_ring(P, cached = false) - f = Pt() - for i=0:Q.len-1 - j = unsafe_load(reinterpret(Ptr{Int}, Q.j), i+1) - a = ZZRingElem() - ccall((:fmpz_set, libflint), Nothing, (Ref{ZZRingElem}, Int64), a, Q.a+i*sizeof(Ptr)) - setcoeff!(f, j, P(a)) - end - return f -end - -lift(::ZZRing, x::fpFieldElem) = lift(x) - -lift(::ZZRing, x::FpFieldElem) = lift(x) - function splitting_field(f::PolyElem{<:FinFieldElem}; do_roots::Bool = false) lf = factor(f) k = base_ring(f) From 104d3e520fbe99a33647aa02073ce7c1b96d32f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 29 Jun 2023 17:39:45 +0200 Subject: [PATCH 06/11] Remove obvious piracy from `Hecke/src/misc/Integer.jl` now in Nemo --- src/Misc/Integer.jl | 96 -------------------------------------------- test/Misc/Integer.jl | 60 --------------------------- 2 files changed, 156 deletions(-) delete mode 100644 test/Misc/Integer.jl diff --git a/src/Misc/Integer.jl b/src/Misc/Integer.jl index 50388a3044..c7c81da5a6 100644 --- a/src/Misc/Integer.jl +++ b/src/Misc/Integer.jl @@ -6,13 +6,6 @@ is_commutative(::ZZRing) = true -function Float64(a::QQFieldElem) - b = a*ZZRingElem(2)^53 - Float64(div(numerator(b), denominator(b)))/(Float64(2)^53) #CF 2^53 is bad in 32bit -end - -Integer(a::ZZRingElem) = BigInt(a) - @doc raw""" modord(a::ZZRingElem, m::ZZRingElem) -> Int modord(a::Integer, m::Integer) @@ -46,95 +39,6 @@ function neg!(a::ZZRingElem) return a end -## -## Ranges -## -# Note, we cannot get a UnitRange as this is only legal for subtypes of Real. -# So, we use an AbstractUnitRange here mostly copied from `base/range.jl`. -# `StepRange`s on the other hand work out of the box thanks to duck typing. - -struct fmpzUnitRange <: AbstractUnitRange{ZZRingElem} - start::ZZRingElem - stop::ZZRingElem - fmpzUnitRange(start, stop) = new(start, fmpz_unitrange_last(start, stop)) -end -fmpz_unitrange_last(start::ZZRingElem, stop::ZZRingElem) = - ifelse(stop >= start, stop, start - one(ZZRingElem)) - -Base.:(:)(a::ZZRingElem, b::ZZRingElem) = fmpzUnitRange(a, b) - -@inline function getindex(r::fmpzUnitRange, i::ZZRingElem) - val = r.start + (i - 1) - @boundscheck _in_unit_range(r, val) || throw_boundserror(r, i) - val -end -_in_unit_range(r::fmpzUnitRange, val::ZZRingElem) = r.start <= val <= r.stop - -show(io::IO, r::fmpzUnitRange) = print(io, repr(first(r)), ':', repr(last(r))) - -in(x::IntegerUnion, r::fmpzUnitRange) = first(r) <= x <= last(r) - -mod(i::IntegerUnion, r::fmpzUnitRange) = mod(i-first(r), length(r)) + first(r) - -Base.:(:)(a::ZZRingElem, b::Integer) = (:)(promote(a,b)...) -Base.:(:)(a::Integer, b::ZZRingElem) = (:)(promote(a,b)...) - -# Construct StepRange{ZZRingElem, T} where +(::ZZRingElem, zero(::T)) must be defined -Base.:(:)(a::ZZRingElem, s, b::Integer) = ((a_,b_)=promote(a,b); a_:s:b_) -Base.:(:)(a::Integer, s, b::ZZRingElem) = ((a_,b_)=promote(a,b); a_:s:b_) - -#TODO -# need to be mapped onto proper Flint primitives -# flints needs a proper interface to randomness - I think -# currently one simply cannot use it at all -# -# should be tied(?) to the Julia rng stuff? -# similarly, all the derived rand functions should probably also do this -# -# inspired by/copied from a former BigInt implementation from the stdlib in -# `Random/src/generation.jl` -# - -function rand(rng::AbstractRNG, a::fmpzUnitRange) - m = Base.last(a) - Base.first(a) - m < 0 && error("range empty") - nd = ndigits(m, 2) - nl, high = divrem(nd, 8*sizeof(Base.GMP.Limb)) - if high>0 - mask = m>>(nl*8*sizeof(Base.GMP.Limb)) - end - s = ZZRingElem(0) - while true - s = ZZRingElem(0) - for i=1:nl - s = s << (8*sizeof(Base.GMP.Limb)) - s += rand(rng, Base.GMP.Limb) - end - if high >0 - s = s << high - s += rand(rng, 0:Base.GMP.Limb(mask)) - end - if s <= m break; end - end - return s + first(a) -end - -struct RangeGeneratorfmpz# <: Base.Random.RangeGenerator - a::StepRange{ZZRingElem, ZZRingElem} -end - -function Random.RangeGenerator(r::StepRange{ZZRingElem,ZZRingElem}) - m = last(r) - first(r) - m < 0 && throw(ArgumentError("range must be non-empty")) - return RangeGeneratorfmpz(r) -end - -function rand(rng::AbstractRNG, g::RangeGeneratorfmpz) - return rand(rng, g.a) -end - - - ############################################################ # more unsafe function that Bill does not want to have.... ############################################################ diff --git a/test/Misc/Integer.jl b/test/Misc/Integer.jl deleted file mode 100644 index bc2d54d9ad..0000000000 --- a/test/Misc/Integer.jl +++ /dev/null @@ -1,60 +0,0 @@ -@testset "Integer" begin - @testset "range" begin - - r04 = ZZRingElem(0):ZZRingElem(4) - r026 = ZZRingElem(0):ZZRingElem(2):ZZRingElem(6) - zlarge = ZZRingElem(13)^100 - zjump = ZZRingElem(11)^100 - rlarge = -zlarge:zlarge - rlargejumps = -zlarge:zjump:zlarge - - @test ZZRingElem(0):4 == 0:ZZRingElem(4) == r04 - @test ZZRingElem(0):2:6 == 0:2:ZZRingElem(6) == 0:ZZRingElem(2):6 == r026 - @test typeof(ZZRingElem(0):4) == typeof(0:ZZRingElem(4)) == typeof(r04) - @test typeof(ZZRingElem(0):ZZRingElem(2):6) == typeof(0:ZZRingElem(2):ZZRingElem(6)) == typeof(0:ZZRingElem(2):6) == typeof(r026) - - @test collect(r04) == ZZRingElem[0, 1, 2, 3, 4] - @test collect(r026) == ZZRingElem[0, 2, 4, 6] - @test collect(ZZRingElem(-2):ZZRingElem(5):ZZRingElem(21)) == ZZRingElem[-2, 3, 8, 13, 18] - @test collect(ZZRingElem(3):ZZRingElem(-2):ZZRingElem(-4)) == ZZRingElem[3, 1, -1, -3] - - @test length(r04) == 5 - @test length(r026) == 4 - @test length(ZZRingElem(-2):ZZRingElem(5):ZZRingElem(21)) == 5 - @test length(ZZRingElem(3):ZZRingElem(-2):ZZRingElem(-4)) == 4 - - # Julia assumes `length` to return an Integer, so if we want several base - # functions to work, that should better be the case. - @test length(r04) isa Integer - @test length(r026) isa Integer - @test length(rlarge) isa Integer - @test length(rlargejumps) isa Integer - - @test r04[4] == r04[end-1] == 3 - @test r026[3] == r026[end-1] == 4 - @test rlarge[2zlarge] == rlarge[end-1] == rlarge[end-ZZRingElem(1)] == zlarge - 1 - @test rlargejumps[2] == -zlarge + zjump - @test rlargejumps[end] == zlarge - 2zlarge % zjump - - @test range(ZZRingElem(0); step=2, stop=6) == ZZRingElem(0):ZZRingElem(2):ZZRingElem(6) - @test range(0; step=ZZRingElem(2), stop=6) == ZZRingElem(0):ZZRingElem(2):ZZRingElem(6) - @test range(ZZRingElem(0); step=2, length=4) == ZZRingElem(0):ZZRingElem(2):ZZRingElem(6) - VERSION >= v"1.7" && @test range(; stop=ZZRingElem(0), length=3) == ZZRingElem(-2):ZZRingElem(0) - - @test 1 .+ r04 == ZZRingElem(1):ZZRingElem(5) - @test ZZRingElem(1) .+ r04 == ZZRingElem(1):ZZRingElem(5) - @test ZZRingElem(1) .+ (0:4) == ZZRingElem(1):ZZRingElem(5) - @test 2 .* r04 == ZZRingElem(0):ZZRingElem(2):ZZRingElem(8) - @test ZZRingElem(2) .* r04 == ZZRingElem(0):ZZRingElem(2):ZZRingElem(8) - @test 2 .* (0:4) == ZZRingElem(0):ZZRingElem(2):ZZRingElem(8) - - @test mod(ZZRingElem(7), r04) == ZZRingElem(2) - @test mod(7, r04) == 2 - @test mod(7, ZZRingElem(10):ZZRingElem(14)) == 12 - - @test all(x -> x in r04, rand(r04, 5)) - @test all(x -> x in ZZRingElem(0):ZZRingElem(10):ZZRingElem(100), rand(ZZRingElem(0):ZZRingElem(10):ZZRingElem(100), 5)) - @test all(x -> x in rlarge, rand(rlarge, 20)) - @test all(x -> x in rlargejumps, rand(rlargejumps, 20)) - end -end From ae59e39142664eab27c591387563fa9be2a5241f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 29 Jun 2023 18:23:35 +0200 Subject: [PATCH 07/11] Remove obvious piracy from different places now in Nemo --- src/Misc/ResidueRingPoly.jl | 20 ------- src/NumField/Elem.jl | 25 --------- src/NumField/Field.jl | 21 -------- src/NumField/NfAbs/Elem.jl | 69 ------------------------ src/NumField/NfAbs/NfAbs.jl | 51 +----------------- src/NumField/NfAbs/NonSimple.jl | 10 ---- src/NumField/NfAbs/Poly.jl | 10 ---- src/NumField/NfAbs/PolyFact.jl | 26 +-------- src/NumField/NfAbs/Simplify.jl | 18 ------- src/NumField/NfRel/NfRelNS.jl | 9 ---- src/NumField/NfRel/NfRelNSMor.jl | 23 -------- src/NumField/QQ.jl | 7 --- src/NumField/SimpleNumField/Field.jl | 4 -- src/NumFieldOrd/NfOrd/Clgp/Map.jl | 20 ------- src/NumFieldOrd/NfOrd/Hensel.jl | 20 ------- src/NumFieldOrd/NfOrd/Ideal/Prime.jl | 6 --- src/NumFieldOrd/NfOrd/Ideal/Valuation.jl | 4 -- src/NumFieldOrd/NfOrd/LinearAlgebra.jl | 29 ---------- src/QuadForm/CloseVectors.jl | 6 --- src/QuadForm/Enumeration.jl | 31 ----------- src/QuadForm/Misc.jl | 25 --------- src/QuadForm/Morphism.jl | 17 ------ src/QuadForm/Quad/NormalForm.jl | 26 --------- src/QuadForm/Quad/PadicLift.jl | 10 ---- src/RCF/conductor.jl | 5 -- src/RCF/rcf_stark.jl | 7 --- src/RieSrf/Theta.jl | 28 ---------- src/Sparse/Matrix.jl | 4 -- src/Sparse/ZZRow.jl | 7 --- src/conjugates.jl | 20 +------ 30 files changed, 4 insertions(+), 554 deletions(-) diff --git a/src/Misc/ResidueRingPoly.jl b/src/Misc/ResidueRingPoly.jl index da51c74539..2413e90d0b 100644 --- a/src/Misc/ResidueRingPoly.jl +++ b/src/Misc/ResidueRingPoly.jl @@ -190,23 +190,3 @@ function gcd!(f::Nemo.fpPolyRingElem, g::Nemo.fpPolyRingElem, h::Nemo.fpPolyRing ccall((:nmod_poly_gcd, libflint), Nothing, (Ref{Nemo.fpPolyRingElem}, Ref{Nemo.fpPolyRingElem}, Ref{Nemo.fpPolyRingElem}), f, g, h) return f end - -function (R::Nemo.zzModPolyRing)(g::QQPolyRingElem) - return fmpq_poly_to_nmod_poly(R, g) -end - -function (R::Nemo.fpPolyRing)(g::QQPolyRingElem) - return fmpq_poly_to_gfp_poly(R, g) -end - -function (R::Nemo.ZZModPolyRing)(g::QQPolyRingElem) - return fmpq_poly_to_fmpz_mod_poly(R, g) -end - -function (R::Nemo.FpPolyRing)(g::QQPolyRingElem) - return fmpq_poly_to_gfp_fmpz_poly(R, g) -end - -function (R::Nemo.FqPolyRing)(g::QQPolyRingElem) - return fmpq_poly_to_fq_default_poly(R, g) -end diff --git a/src/NumField/Elem.jl b/src/NumField/Elem.jl index ddef01c676..3a26c59881 100644 --- a/src/NumField/Elem.jl +++ b/src/NumField/Elem.jl @@ -7,31 +7,6 @@ export coordinates, absolute_coordinates, absolute_norm, absolute_tr, # ################################################################################ -is_unit(a::NumFieldElem) = !iszero(a) - -canonical_unit(a::NumFieldElem) = a - -################################################################################ -# -# Base case for dot products -# -################################################################################ - -dot(x::ZZRingElem, y::NumFieldElem) = x * y - -dot(x::Integer, y::NumFieldElem) = x * y - -dot(x::NumFieldElem, y::Integer) = x * y - -function dot(a::Vector{<: NumFieldElem}, b::Vector{ZZRingElem}) - d = zero(parent(a[1])) - t = zero(d) - for i=1:length(a) - mul!(t, a[i], b[i]) - add!(d, d, t) - end - return d -end function dot(a::Vector{NfAbsNSElem}, b::Vector{ZZRingElem}) Qxy = parent(a[1].data) diff --git a/src/NumField/Field.jl b/src/NumField/Field.jl index 16b6346577..a2b1517069 100644 --- a/src/NumField/Field.jl +++ b/src/NumField/Field.jl @@ -205,29 +205,8 @@ function is_normal_basis_generator(a::NumFieldElem) return rank(A) == n end -#trivia to make life easier - -gens(L::SimpleNumField{T}) where {T} = [gen(L)] - -function gen(L::SimpleNumField{T}, i::Int) where {T} - i == 1 || error("index must be 1") - return gen(L) -end - gen(L::NonSimpleNumField{T}, i::Int) where {T} = L(gen(parent(L.pol[1]), i)) -function Base.getindex(L::SimpleNumField{T}, i::Int) where {T} - if i == 0 - return one(L) - elseif i == 1 - return gen(L) - else - error("index has to be 0 or 1") - end -end - -ngens(L::SimpleNumField{T}) where {T} = 1 - function Base.getindex(L::NonSimpleNumField{T}, i::Int) where {T} i == 0 && return one(L) return gen(L, i) diff --git a/src/NumField/NfAbs/Elem.jl b/src/NumField/NfAbs/Elem.jl index 47fc195785..19362ac138 100644 --- a/src/NumField/NfAbs/Elem.jl +++ b/src/NumField/NfAbs/Elem.jl @@ -53,75 +53,6 @@ function basis_matrix(A::Vector{nf_elem}) return M end -################################################################################ -# -# Characteristic polynomial -# -################################################################################ - -function charpoly(Qx::QQPolyRing, a::nf_elem) - f = charpoly(Qx, representation_matrix(a)) - return f -end - -function charpoly(a::nf_elem) - f = charpoly(parent(parent(a).pol), a) - return f -end - -function charpoly(a::nf_elem, ::QQField) - return charpoly(a) -end - -function charpoly(Zx::ZZPolyRing, a::nf_elem) - f = charpoly(a) - if !isone(denominator(f)) - error("Element is not integral") - end - return Zx(f) -end - -function charpoly(a::nf_elem, Z::ZZRing) - return charpoly(polynomial_ring(Z, cached = false)[1], a) -end - -################################################################################ -# -# Minimal polynomial -# -################################################################################ - -@doc raw""" - minpoly(a::nf_elem) -> QQPolyRingElem - -The minimal polynomial of $a$. -""" -function minpoly(Qx::QQPolyRing, a::nf_elem) - f = minpoly(Qx, representation_matrix(a)) - return f -end - -function minpoly(a::nf_elem) - f = minpoly(parent(parent(a).pol), a) - return f -end - -function minpoly(a::nf_elem, ::QQField) - return minpoly(a) -end - -function minpoly(a::nf_elem, ZZ::ZZRing) - return minpoly(polynomial_ring(ZZ, cached = false)[1], a) -end - -function minpoly(Zx::ZZPolyRing, a::nf_elem) - f = minpoly(a) - if !isone(denominator(f)) - error("Element is not integral") - end - return Zx(f) -end - ################################################################################ # # Unsafe operations diff --git a/src/NumField/NfAbs/NfAbs.jl b/src/NumField/NfAbs/NfAbs.jl index d13b61f0b4..fb5a660819 100644 --- a/src/NumField/NfAbs/NfAbs.jl +++ b/src/NumField/NfAbs/NfAbs.jl @@ -1,14 +1,6 @@ export splitting_field, is_subfield, is_defining_polynomial_nice, quadratic_field, is_linearly_disjoint, rationals_as_number_field -################################################################################ -# -# Base field -# -################################################################################ - -base_field(K::AnticNumberField) = FlintQQ - ################################################################################ # # Order type @@ -244,24 +236,6 @@ function relative_class_number(K::AnticNumberField) return divexact(h, hp) end -################################################################################ -# -# Basis -# -################################################################################ - -function basis(K::AnticNumberField) - n = degree(K) - g = gen(K); - d = Array{typeof(g)}(undef, n) - b = K(1) - for i = 1:n-1 - d[i] = b - b *= g - end - d[n] = b - return d -end ################################################################################ # @@ -797,8 +771,6 @@ function splitting_field(fl::Vector{QQPolyRingElem}; coprime::Bool = false, do_r end end - -copy(f::QQPolyRingElem) = parent(f)(f) gcd_into!(a::QQPolyRingElem, b::QQPolyRingElem, c::QQPolyRingElem) = gcd(b, c) @doc raw""" @@ -895,14 +867,6 @@ function _splitting_field(fl::Vector{<:PolyElem{<:NumFieldElem}}; do_roots::Type end end -function Base.:(^)(a::nf_elem, e::UInt) - b = parent(a)() - ccall((:nf_elem_pow, libantic), Nothing, - (Ref{nf_elem}, Ref{nf_elem}, UInt, Ref{AnticNumberField}), - b, a, e, parent(a)) - return b -end - @doc raw""" normal_closure(K::AnticNumberField) -> AnticNumberField, NfToNfMor @@ -915,15 +879,6 @@ function normal_closure(K::AnticNumberField) return s, hom(K, s, r, check = false) end -function set_name!(K::AnticNumberField, s::String) - set_attribute!(K, :name => s) -end - -function set_name!(K::AnticNumberField) - s = find_name(K) - s === nothing || set_name!(K, string(s)) -end - ################################################################################ # # Is linearly disjoint @@ -1317,8 +1272,4 @@ function force_coerce_cyclo(a::AnticNumberField, b::nf_elem, throw_error::Type{V # now ff is a polynomial for b w.r.t. the fa-th cyclotomic field return a(ff) -end - -(::QQField)(a::nf_elem) = (is_rational(a) && return coeff(a, 0)) || error("not a rational") - -(::ZZRing)(a::nf_elem) = (is_integer(a) && return numerator(coeff(a, 0))) || error("not an integer") +end \ No newline at end of file diff --git a/src/NumField/NfAbs/NonSimple.jl b/src/NumField/NfAbs/NonSimple.jl index 3ad5b87f86..d72ddf96ce 100644 --- a/src/NumField/NfAbs/NonSimple.jl +++ b/src/NumField/NfAbs/NonSimple.jl @@ -104,12 +104,6 @@ function Nemo.one!(a::NfAbsNSElem) return a end -function Nemo.one!(a::QQMPolyRingElem) - ccall((:fmpq_mpoly_one, libflint), Nothing, - (Ref{QQMPolyRingElem}, Ref{QQMPolyRing}), a, parent(a)) - return a -end - ################################################################################ # # Random @@ -1136,10 +1130,6 @@ function degree(a::NfAbsNSElem) return degree(minpoly(a)) end -function degree(a::nf_elem) - return degree(minpoly(a)) -end - #TODO: Improve the algorithm function primitive_element(K::NfAbsNS) g = gens(K) diff --git a/src/NumField/NfAbs/Poly.jl b/src/NumField/NfAbs/Poly.jl index 9b253cfd52..755eac4802 100644 --- a/src/NumField/NfAbs/Poly.jl +++ b/src/NumField/NfAbs/Poly.jl @@ -431,16 +431,6 @@ function eq_mod(a::Generic.Poly{nf_elem}, b::Generic.Poly{nf_elem}, d::ZZRingEle return e end -################################################################################ -# -# Predicates -# -################################################################################ - -function is_squarefree(x::Generic.Poly{nf_elem}) - return isone(gcd(x, derivative(x), true)) -end - ################################################################################ # # Some weird conversion diff --git a/src/NumField/NfAbs/PolyFact.jl b/src/NumField/NfAbs/PolyFact.jl index 171998f8da..a5a1a41fc8 100644 --- a/src/NumField/NfAbs/PolyFact.jl +++ b/src/NumField/NfAbs/PolyFact.jl @@ -187,20 +187,6 @@ function Base.round(::Type{ZZRingElem}, a::ZZRingElem, b::ZZRingElem, bi::fmpz_p return r end -@doc raw""" - round(::ZZRingElem, a::ZZRingElem, b::ZZRingElem) -> ZZRingElem - -Computes `round(a//b)`. -""" -function Base.round(::Type{ZZRingElem}, a::ZZRingElem, b::ZZRingElem) - s = sign(a)*sign(b) - bs = abs(b) - as = abs(a) - r = s*div(2*as+bs, 2*bs) -# @assert r == round(ZZRingElem, a//b) - return r -end - #TODO: think about computing pM[1][1,:]//pM[2] as a "float" approximation # to save on multiplications function reco(a::ZZRingElem, M, pM::Tuple{ZZMatrix, ZZRingElem, fmpz_preinvn_struct}, O) @@ -521,16 +507,6 @@ function lll_with_removal_knapsack(x::ZZMatrix, b::ZZRingElem, ctx::lll_ctx = ll return d, z end -function tdivpow2!(B::ZZMatrix, t::Int) - ccall((:fmpz_mat_scalar_tdiv_q_2exp, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}, Cint), B, B, t) -end - -function Nemo.tdivpow2(B::ZZMatrix, t::Int) - C = similar(B) - ccall((:fmpz_mat_scalar_tdiv_q_2exp, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}, Cint), C, B, t) - return C -end - function gradual_feed_lll(M::ZZMatrix, sm::ZZRingElem, B::ZZMatrix, d::ZZRingElem, bnd::ZZRingElem) b = maximum(nbits, B) sc = max(0, b-55) @@ -767,7 +743,7 @@ function van_hoeij(f::PolyElem{nf_elem}, P::NfOrdIdl; prec_scale = 1) sz = nbits(vH.pM[2]) - div(r, 1) - prec_scale end push!(really_used, n) - tdivpow2!(B, sz+prec_scale) + Nemo.tdivpow2!(B, sz+prec_scale) d = tdivpow2(vH.pM[2], sz) bnd = r*ZZRingElem(2)^(2*prec_scale) + degree(K)*(ncols(M)-r)*div(r, 2)^2 diff --git a/src/NumField/NfAbs/Simplify.jl b/src/NumField/NfAbs/Simplify.jl index e5e8fdb4af..a8346844c0 100644 --- a/src/NumField/NfAbs/Simplify.jl +++ b/src/NumField/NfAbs/Simplify.jl @@ -207,24 +207,6 @@ function _block(el::NfAbsNSElem, rt::Vector{Vector{fqPolyRepFieldElem}}, R::fpFi return b end -function AbstractAlgebra.map_coefficients(F::fpField, f::QQMPolyRingElem; parent = polynomial_ring(F, nvars(parent(f)), cached = false)[1]) - dF = denominator(f) - d = F(dF) - if iszero(d) - error("Denominator divisible by p!") - end - m = inv(d) - ctx = MPolyBuildCtx(parent) - for x in zip(coefficients(f), exponent_vectors(f)) - el = numerator(x[1]*dF) - push_term!(ctx, F(el)*m, x[2]) - end - return finish(ctx) -end - - - - function _sieve_primitive_elements(B::Vector{nf_elem}) K = parent(B[1]) Zx = polynomial_ring(FlintZZ, "x", cached = false)[1] diff --git a/src/NumField/NfRel/NfRelNS.jl b/src/NumField/NfRel/NfRelNS.jl index a2bd83312a..6327cf278e 100644 --- a/src/NumField/NfRel/NfRelNS.jl +++ b/src/NumField/NfRel/NfRelNS.jl @@ -45,15 +45,6 @@ mS(gen(S)) mS\gens(K)[2] =# -#to make the MPoly module happy, divrem needs it... -function Base.div(a::nf_elem, b::nf_elem) - return a//b -end - -function Nemo.rem(a::nf_elem, b::nf_elem) - return parent(a)(0) -end - #non-simple fields are quotients by multivariate polynomials #this could be extended to arbitrary zero-dimensional quotients, but #I don't need this here. diff --git a/src/NumField/NfRel/NfRelNSMor.jl b/src/NumField/NfRel/NfRelNSMor.jl index c96fdd4439..2464683fb9 100644 --- a/src/NumField/NfRel/NfRelNSMor.jl +++ b/src/NumField/NfRel/NfRelNSMor.jl @@ -1,5 +1,3 @@ -@inline ngens(R::Nemo.Generic.MPolyRing) = R.num_vars - function _prod(A, b) for a in A b = b * a @@ -89,10 +87,6 @@ function _get_polys_from_auto(f::NfRelNSToNfRelNSMor_nf_elem, Qxy) return res end -function Base.hash(f::zzModMPolyRingElem, h::UInt) - return UInt(1) -end - function permutation_group1(G::Vector{NfRelNSToNfRelNSMor_nf_elem}) L = domain(G[1]) K = base_field(L) @@ -226,10 +220,6 @@ function powermod(a::S, i::Union{Int, ZZRingElem}, modu::Vector{S}) where S <:MP return b end -function mulmod(a::S, b::S, mod::Vector{S}) where S <:MPolyRingElem{T} where T <: RingElem - return divrem(a*b, mod)[2] -end - function _compose_mod(a, vars, vals, powers, modu) S = parent(a) @@ -254,19 +244,6 @@ function _compose_mod(a, vars, vals, powers, modu) end -function change_base_ring(p::MPolyRingElem{T}, g, new_polynomial_ring) where {T <: RingElement} - cvzip = zip(coefficients(p), exponent_vectors(p)) - M = MPolyBuildCtx(new_polynomial_ring) - for (c, v) in cvzip - res = g(c) - if !iszero(res) - push_term!(M, g(c), v) - end - end - return finish(M)::elem_type(new_polynomial_ring) -end - - function Base.:(*)(f::Hecke.NfToNfRel, g::Hecke.NfRelToNfRelNSMor_nf_elem) @assert codomain(f) === domain(g) return hom(domain(f), codomain(g), g(f(gen(domain(f))))) diff --git a/src/NumField/QQ.jl b/src/NumField/QQ.jl index 7dcfc730d8..b51bac9b97 100644 --- a/src/NumField/QQ.jl +++ b/src/NumField/QQ.jl @@ -164,7 +164,6 @@ fractional_ideal_type(::QQField) = ZZFracIdl elem_in_nf(x::ZZRingElem) = FlintQQ(x) nf(::ZZRing) = FlintQQ -number_field(::ZZRing) = FlintQQ # Infinite places @@ -279,10 +278,6 @@ sunit_group_fac_elem(S::Vector{ZZIdl}) = sunit_group_fac_elem([gen(i) for i in S # If this causes trouble, we need to change it to ArbField(p, cached = false)(x) evaluate(x::QQFieldElem, ::PosInf, p::Int) = x -real(x::QQFieldElem) = x - -norm(x::ZZRingElem) = abs(x) - ################################################################################ # @@ -320,5 +315,3 @@ lifted_denominator(x::ZZRingElem) = ZZRingElem(1) ################################################################################ absolute_basis(Q::QQField) = [one(Q)] - -gen(Q::QQField) = one(Q) diff --git a/src/NumField/SimpleNumField/Field.jl b/src/NumField/SimpleNumField/Field.jl index 767fc6d5ac..1db77eddad 100644 --- a/src/NumField/SimpleNumField/Field.jl +++ b/src/NumField/SimpleNumField/Field.jl @@ -165,10 +165,6 @@ function absolute_discriminant(K::NfRel) return d end -function discriminant(K::QQField) - return one(K) -end - ################################################################################ # # Is subfield diff --git a/src/NumFieldOrd/NfOrd/Clgp/Map.jl b/src/NumFieldOrd/NfOrd/Clgp/Map.jl index fa070dfe4e..da54fcd5ee 100644 --- a/src/NumFieldOrd/NfOrd/Clgp/Map.jl +++ b/src/NumFieldOrd/NfOrd/Clgp/Map.jl @@ -556,26 +556,6 @@ function unique_fmpz_mat(C::Nemo.arb_mat) return true, v end -function Base.round(::Type{ZZRingElem}, x::arb) - if radius(x) > 1e-1 - throw(InexactError(:round, ZZRingElem, x)) - end - return setprecision(BigFloat, precision(parent(x))) do - round(ZZRingElem, BigFloat(x)) - end -end - -function Base.round(::Type{ZZMatrix}, C::Nemo.arb_mat) - v = zero_matrix(FlintZZ, nrows(C), ncols(C)) - - for i=1:nrows(C) - for j=1:ncols(C) - v[i,j] = round(ZZRingElem, C[i,j]) - end - end - return v -end - function round_approx(::Type{ZZMatrix}, C::Nemo.arb_mat) v = zero_matrix(FlintZZ, nrows(C), ncols(C)) diff --git a/src/NumFieldOrd/NfOrd/Hensel.jl b/src/NumFieldOrd/NfOrd/Hensel.jl index 290d3cb582..5b112e8ffb 100644 --- a/src/NumFieldOrd/NfOrd/Hensel.jl +++ b/src/NumFieldOrd/NfOrd/Hensel.jl @@ -72,26 +72,6 @@ function lift(R::zzModPolyRing, a::fqPolyRepFieldElem) return f end -function (Zx::ZZPolyRing)(a::nf_elem) - b = Zx() - @assert denominator(a) == 1 - if degree(parent(a)) == 1 - # If the number field is linear, then a.elem_length is not properly - # initialized, that is, it could be anything. - setcoeff!(b, 0, numerator(coeff(a, 0))) - elseif degree(parent(a)) == 2 - # ... or quadratic, then a.elem_length is not properly - # initialized, that is, it could be anything. - setcoeff!(b, 0, numerator(coeff(a, 0))) - setcoeff!(b, 1, numerator(coeff(a, 1))) - else - for i=0:a.elem_length - setcoeff!(b, i, numerator(coeff(a, i))) - end - end - return b -end - ################################################################################ # # Root computation via Hensel lifting diff --git a/src/NumFieldOrd/NfOrd/Ideal/Prime.jl b/src/NumFieldOrd/NfOrd/Ideal/Prime.jl index d59eff7aa9..1da997a476 100644 --- a/src/NumFieldOrd/NfOrd/Ideal/Prime.jl +++ b/src/NumFieldOrd/NfOrd/Ideal/Prime.jl @@ -242,12 +242,6 @@ function prime_decomposition(O::NfAbsOrd{NfAbsNS, NfAbsNSElem}, p::IntegerUnion, end end -Nemo.fits(::Type{Int}, a::Int) = true -function Nemo.fits(::Type{Int}, a::Integer) - #TODO: possibly not optimal? - return a % Int == a -end - function prime_decomposition(O::NfOrd, p::IntegerUnion, degree_limit::Int = degree(O), lower_limit::Int = 0; cached::Bool = false) if typeof(p) != Int && fits(Int, p) return prime_decomposition(O, Int(p), degree_limit, lower_limit, cached = cached) diff --git a/src/NumFieldOrd/NfOrd/Ideal/Valuation.jl b/src/NumFieldOrd/NfOrd/Ideal/Valuation.jl index 28d55cbb77..284c8fab42 100644 --- a/src/NumFieldOrd/NfOrd/Ideal/Valuation.jl +++ b/src/NumFieldOrd/NfOrd/Ideal/Valuation.jl @@ -5,10 +5,6 @@ ################################################################################ -function valuation(a::UInt, b::UInt) - return ccall((:n_remove, libflint), Int, (Ref{UInt}, UInt), a, b) -end - # CF: # The idea is that valuations are mostly small, eg. in the class group # algorithm. So this version computes the completion and the embedding into it diff --git a/src/NumFieldOrd/NfOrd/LinearAlgebra.jl b/src/NumFieldOrd/NfOrd/LinearAlgebra.jl index 41952e51e2..55930a165d 100644 --- a/src/NumFieldOrd/NfOrd/LinearAlgebra.jl +++ b/src/NumFieldOrd/NfOrd/LinearAlgebra.jl @@ -962,17 +962,6 @@ function _spans_subset_of_pseudohnf(M::PMat{T, S}, P::PMat{T, S}, shape::Symbol return true end -function sub(M::Generic.Mat, rows::UnitRange{Int}, cols::UnitRange{Int}) - @assert step(rows) == 1 && step(cols) == 1 - z = zero_matrix(base_ring(M), length(rows), length(cols)) - for i in rows - for j in cols - z[i - first(rows) + 1, j - first(cols) + 1] = M[i, j] - end - end - return z -end - function sub(P::PMat, rows::UnitRange{Int}, cols::UnitRange{Int}) m = sub(P.matrix, rows, cols) return pseudo_matrix(m, P.coeffs[rows]) @@ -1589,24 +1578,6 @@ end base_ring(M::ModDed) = M.base_ring -function is_upper_triangular(A::Generic.Mat) - m = nrows(A) - n = ncols(A) - d = 0 - for r = 1:m - for c = 1:n - if !iszero(A[r, c]) - if c <= d - return false - end - d = c - break - end - end - end - return true -end - function show(io::IO, M::ModDed) print(io, "Module over $(M.base_ring) with defining pseudo-matrix") for i in 1:nrows(M.pmatrix.matrix) diff --git a/src/QuadForm/CloseVectors.jl b/src/QuadForm/CloseVectors.jl index 629d114a4b..6474346524 100644 --- a/src/QuadForm/CloseVectors.jl +++ b/src/QuadForm/CloseVectors.jl @@ -210,12 +210,6 @@ function Base.iterate(C::LatCloseEnumCtx{X, elem_type}, start = nothing) where { return (x, dist), st end -function sub!(z::Vector{QQFieldElem}, x::Vector{QQFieldElem}, y::Vector{ZZRingElem}) - for i in 1:length(z) - sub!(z[i], x[i], y[i]) - end - return z -end ################################################################################ # diff --git a/src/QuadForm/Enumeration.jl b/src/QuadForm/Enumeration.jl index 744d0c0af6..c428a60d46 100644 --- a/src/QuadForm/Enumeration.jl +++ b/src/QuadForm/Enumeration.jl @@ -758,11 +758,6 @@ function _deepcopy_cheap(x::QQFieldElem) return z end -function is_negative(x::QQFieldElem) - c = ccall((:fmpq_sgn, libflint), Cint, (Ref{QQFieldElem}, ), x) - return c < 0 -end - function is_lessorequal(x::QQFieldElem, y::UInt) c = ccall((:fmpq_cmp_ui, libflint), Cint, (Ref{QQFieldElem}, UInt), x, y) return c <= 0 @@ -1064,22 +1059,6 @@ end return z end -@inline function sub!(z::QQFieldElem, a::QQFieldElem, b::QQFieldElem) - ccall((:fmpq_sub, libflint), Cvoid, (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), z, a, b) - return z -end - -@inline function sub!(z::QQFieldElem, a::QQFieldElem, b::ZZRingElem) - ccall((:fmpq_sub_fmpz, libflint), Nothing, - (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{ZZRingElem}), z, a, b) - return z -end - -@inline function neg!(z::QQFieldElem, a::QQFieldElem) - ccall((:fmpq_neg, libflint), Cvoid, (Ref{QQFieldElem}, Ref{QQFieldElem}), z, a) - return z -end - @inline function isqrt!(z::ZZRingElem, a::ZZRingElem) ccall((:fmpz_sqrt, libflint), Cvoid, (Ref{ZZRingElem}, Ref{ZZRingElem}), z, a) return z @@ -1093,16 +1072,6 @@ isqrt!(z::Int, x::Int) = isqrt(x) add_two!(z::Int, x::Int) = x + 2 -sub!(z::Rational{Int}, x::Rational{Int}, y::Int) = x - y - -neg!(z::Rational{Int}, x::Rational{Int}) = -x - ceil!(z::Int, x::Rational{Int}, y::Int, w::Int) = Int(ceil(x)) -add!(z::Rational{Int}, x::Rational{Int}, y::Int) = x + y - -mul!(z::Rational{Int}, x::Rational{Int}, y::Int) = x * y - numerator!(z::Int, x::Rational{Int}) = numerator(x) - -is_negative(x::Rational) = x.num < 0 diff --git a/src/QuadForm/Misc.jl b/src/QuadForm/Misc.jl index f96dd2b021..2335dde1c3 100644 --- a/src/QuadForm/Misc.jl +++ b/src/QuadForm/Misc.jl @@ -786,8 +786,6 @@ end # ################################################################################ -order(::ZZRingElem) = FlintZZ - uniformizer(p::ZZRingElem) = p is_dyadic(p::ZZRingElem) = p == 2 @@ -1154,29 +1152,6 @@ end _eltseq(M::MatElem) = [M[i, j] for i in 1:nrows(M) for j in 1:ncols(M)] -################################################################################ -# -# Create a matrix from rows -# -################################################################################ - -function matrix(K::Ring, R::Vector{<:Vector}) - if length(R) == 0 - return zero_matrix(K, 0, 0) - else - n = length(R) - m = length(R[1]) - z = zero_matrix(K, n, m) - for i in 1:n - @assert length(R[i]) == m - for j in 1:m - z[i, j] = R[i][j] - end - end - return z - end -end - ################################################################################ # # Maximal subspaces diff --git a/src/QuadForm/Morphism.jl b/src/QuadForm/Morphism.jl index bcf88541a1..b6cca9f7fd 100644 --- a/src/QuadForm/Morphism.jl +++ b/src/QuadForm/Morphism.jl @@ -148,17 +148,6 @@ end dim(C::ZLatAutoCtx) = C.dim -function AbstractAlgebra.is_symmetric(M::MatElem) - for i in 1:nrows(M) - for j in i:ncols(M) - if M[i, j] != M[j, i] - return false - end - end - end - return true -end - function init(C::ZLatAutoCtx, auto::Bool = true, bound::ZZRingElem = ZZRingElem(-1), use_dict::Bool = true) # Compute the necessary short vectors @@ -1619,12 +1608,6 @@ function stabil(x1, x2, per, G::Matrix{Int}, V, C) return SS end -ZZMatrix(M::Matrix{Int}) = matrix(FlintZZ, M) - -zero_matrix(::Type{Int}, r, c) = zeros(Int, r, c) - -base_ring(::Vector{Int}) = Int - function _one(::Type{Matrix{Int}}, n::Int) z = zeros(Int, n, n) for i in 1:n diff --git a/src/QuadForm/Quad/NormalForm.jl b/src/QuadForm/Quad/NormalForm.jl index 3fb77c6461..da67460b0e 100644 --- a/src/QuadForm/Quad/NormalForm.jl +++ b/src/QuadForm/Quad/NormalForm.jl @@ -1035,32 +1035,6 @@ end # from sage.rings.all import polynomial_ring # from sage.modules.free_module_element import vector -function roots(f::ZZModPolyRingElem, p::ZZRingElem, e::Int) - F = Fac{ZZRingElem}() - F.unit = one(ZZRingElem) - F[p] = e - return roots(f, F) -end -function roots(f::ZZModPolyRingElem, fac::Fac{ZZRingElem}) - res = Nemo.fmpz_mod_poly_factor(base_ring(f)) - _fac = Nemo.fmpz_factor() - for (p, e) in fac - ccall((:_fmpz_factor_append, libflint), Cvoid, (Ref{Nemo.fmpz_factor}, Ref{ZZRingElem}, UInt), _fac, p, UInt(e)) - end - ccall((:fmpz_mod_poly_roots_factored, libflint), Cvoid, (Ref{Nemo.fmpz_mod_poly_factor}, Ref{ZZModPolyRingElem}, Cint, Ref{Nemo.fmpz_factor}, Ref{Nemo.fmpz_mod_ctx_struct}), res, f, 1, _fac, base_ring(f).ninv) - _res = Tuple{Nemo.ZZModRingElem, Int}[] - for i in 1:res.num - g = parent(f)() - ccall((:fmpz_mod_poly_factor_get_fmpz_mod_poly, libflint), Nothing, - (Ref{Nemo.ZZModPolyRingElem}, Ref{Nemo.fmpz_mod_poly_factor}, Int, - Ref{Nemo.fmpz_mod_ctx_struct}), - g, res, i - 1, base_ring(f).ninv) - e = unsafe_load(res.exp, i) - push!(_res, (-coeff(g, 0), e)) - end - return _res -end - function _normalize_twobytwo(G, p) # p = ZZRingElem(2) diff --git a/src/QuadForm/Quad/PadicLift.jl b/src/QuadForm/Quad/PadicLift.jl index c1e8b4e4c8..a947e2c203 100644 --- a/src/QuadForm/Quad/PadicLift.jl +++ b/src/QuadForm/Quad/PadicLift.jl @@ -396,13 +396,3 @@ function _crt(V::Vector{ZZMatrix},B::Vector{ZZRingElem}, prec::Vector{Int}) end return sol end - -@doc raw""" - valuation(G::QQMatrix, p) - -Return the minimum valuation of the entries of `G`. -""" -function valuation(G::QQMatrix, p) - return minimum([x==0 ? inf : valuation(x,p) for x in G]) -end - diff --git a/src/RCF/conductor.jl b/src/RCF/conductor.jl index 42d211b0fa..0140d7b6c5 100644 --- a/src/RCF/conductor.jl +++ b/src/RCF/conductor.jl @@ -1509,11 +1509,6 @@ function lcm(A::AbstractArray{<:NfAbsOrdIdl}) return a end -#TODO: should be done in Nemo/AbstractAlgebra s.w. -# needed by ^ (the generic power in Base using square and multiply) -Base.copy(f::Generic.MPoly) = deepcopy(f) -Base.copy(f::Generic.Poly) = deepcopy(f) - @doc raw""" lorenz_module(k::AnticNumberField, n::Int) -> NfOrdIdl diff --git a/src/RCF/rcf_stark.jl b/src/RCF/rcf_stark.jl index d0bb851bae..41c50d9b34 100644 --- a/src/RCF/rcf_stark.jl +++ b/src/RCF/rcf_stark.jl @@ -1,10 +1,3 @@ -function mul!(z::acb, x::acb, y::arb) - ccall((:acb_mul_arb, libarb), Nothing, (Ref{acb}, Ref{acb}, Ref{arb}, Int), - z, x, y, parent(z).prec) - return z -end - - ################################################################################ # # Small interface to characters diff --git a/src/RieSrf/Theta.jl b/src/RieSrf/Theta.jl index b099ec6091..d594ecf5e7 100644 --- a/src/RieSrf/Theta.jl +++ b/src/RieSrf/Theta.jl @@ -246,30 +246,6 @@ function siegel_reduction(tau::acb_mat) end end -*(x::acb, y::arb_mat) = x * _acb_mat(y) -*(x::arb_mat, y::acb) = y * x -*(x::arb_mat, y::acb_mat) = _acb_mat(x) * y -*(x::acb_mat, y::arb_mat) = x * _acb_mat(y) -+(x::arb_mat, y::acb_mat) = _acb_mat(x) + y -+(x::acb_mat, y::arb_mat) = y + x --(x::arb_mat, y::acb_mat) = x + (-y) --(x::acb_mat, y::arb_mat) = x + (-y) -//(x::arb_mat, y::arb) = map(t -> t//y, x) - - -function _acb_mat(A::arb_mat) - p = precision(base_ring(A)) - Cc = AcbField(p) - return change_base_ring(Cc, A) -end - -function real(tau::acb_mat) - return map(real, tau) -end - -function imag(tau::acb_mat) - return map(imag, tau) -end function cholesky_decomposition(x::arb_mat) z = similar(x, nrows(x), ncols(x)) @@ -308,7 +284,3 @@ function shortest_vectors(M::arb_mat) return map(matrix, [map(R, u)*M for u in U]) end -function norm(v::arb_mat) - return sqrt(sum([a^2 for a in v])) -end - diff --git a/src/Sparse/Matrix.jl b/src/Sparse/Matrix.jl index 276097e365..d1087f92b7 100644 --- a/src/Sparse/Matrix.jl +++ b/src/Sparse/Matrix.jl @@ -1222,10 +1222,6 @@ function identity_matrix(::Type{SMat}, R::Ring, n::Int) return A end -function identity_matrix(::Type{MatElem}, R::Ring, n::Int) - return identity_matrix(R, n) -end - @doc raw""" zero_matrix(::Type{SMat}, R::Ring, n::Int) diff --git a/src/Sparse/ZZRow.jl b/src/Sparse/ZZRow.jl index 3e9e561718..2ee5eac9f9 100644 --- a/src/Sparse/ZZRow.jl +++ b/src/Sparse/ZZRow.jl @@ -350,13 +350,6 @@ function get_tmp(A::SMat{ZZRingElem}) return SRow(ZZ, Int[], ZZRingElem_Array()) end -function mul!(a::ZZRingElem, b::ZZRingElem, c::Ptr{Int}) - ccall((:fmpz_mul, Nemo.libflint), Cvoid, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ptr{Int}), a, b, c) -end - -function add!(a::ZZRingElem, b::ZZRingElem, c::Ptr{Int}) - ccall((:fmpz_add, Nemo.libflint), Cvoid, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ptr{Int}), a, b, c) -end function transform_row(Ai::SRow{ZZRingElem}, Aj::SRow{ZZRingElem}, a::ZZRingElem, b::ZZRingElem, c::ZZRingElem, d::ZZRingElem, sr::SRow{ZZRingElem}, tr::SRow{ZZRingElem}) empty!(sr) diff --git a/src/conjugates.jl b/src/conjugates.jl index 7d89d84a1c..ca4925cee5 100644 --- a/src/conjugates.jl +++ b/src/conjugates.jl @@ -1,10 +1,7 @@ -export conjugates_init, is_constant, is_squarefree, conjugates, angle, cos, +export conjugates_init, is_squarefree, conjugates, angle, cos, sin, abs, abs2, sqrt -function is_constant(f::PolyElem) - return f.length<2 -end function conjugates_init(f_in::Union{ZZPolyRingElem, QQPolyRingElem}) local f::ZZPolyRingElem @@ -198,17 +195,4 @@ function *(a::ZZMatrix, b::Matrix{BigFloat}) return mult!(c, a, b) end -for (s,f) in ((:trunc, Base.trunc), (:round, Base.round), (:ceil, Base.ceil), (:floor, Base.floor)) - @eval begin - function ($s)(a::Matrix{BigFloat}) - s = Base.size(a) - m = zero_matrix(FlintZZ, s[1], s[2]) - for i = 1:s[1] - for j = 1:s[2] - m[i,j] = FlintZZ(BigInt(($f)(a[i,j]))) - end - end - return m - end - end -end + From 880c6e34a794dc043d5d3a8ce11b086df3ec7421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Fri, 30 Jun 2023 16:16:16 +0200 Subject: [PATCH 08/11] Remove much more obvious piracy from different places now in Nemo --- src/AlgAss/ChangeRing.jl | 2 - src/AlgAssAbsOrd/Conjugacy/Husert.jl | 2 - src/AlgAssRelOrd/Eichler.jl | 12 - src/Aliases.jl | 1 - src/EllCrv/Heights.jl | 11 - src/EllCrv/Misc.jl | 23 -- src/GenOrd/Auxiliary.jl | 27 -- src/GrpAb/stable_sub.jl | 25 -- src/Hecke.jl | 2 - src/LinearAlgebra/Solve.jl | 23 -- src/LocalField/Poly.jl | 5 - src/LocalField/map.jl | 2 +- src/LocalField/neq.jl | 41 +-- src/LocalField/pAdic.jl | 33 -- src/LocalField/qAdic.jl | 136 +-------- src/Map/Map.jl | 39 --- src/Map/MapType.jl | 5 - src/Map/NfOrd.jl | 37 +-- src/Map/NumField.jl | 2 +- src/Map/NumberField.jl | 20 -- src/Misc.jl | 5 - src/Misc/FactoredElem.jl | 12 +- src/Misc/Infinity.jl | 95 ------ src/Misc/Integer.jl | 437 +++++++-------------------- src/Misc/Localization.jl | 27 -- src/Misc/MPoly.jl | 132 -------- src/Misc/Matrix.jl | 70 ++++- src/Misc/Primes.jl | 19 -- src/Misc/PseudoPolynomial.jl | 14 - src/Misc/Residue.jl | 37 --- src/Misc/ResidueRingPoly.jl | 192 ------------ src/Misc/Series.jl | 92 +----- src/Misc/UnitsModM.jl | 59 ---- src/Misc/UnsafeRational.jl | 3 - src/Misc/acb_root_ctx.jl | 28 -- src/Misc/coprime.jl | 6 - src/Misc/nmod_poly.jl | 119 -------- src/Misc/psibound.jl | 8 - src/ModAlgAss/Lattices.jl | 9 - src/NumField/NfAbs/Elem.jl | 65 ---- src/NumField/NfAbs/MPolyAbsFact.jl | 32 -- src/NumField/NfAbs/MPolyGcd.jl | 14 - src/QuadForm/Enumeration.jl | 7 - src/QuadForm/Morphism.jl | 9 - src/conjugates.jl | 35 --- test/Misc.jl | 2 - test/Misc/Infinity.jl | 72 ----- test/Misc/Localization.jl | 275 ----------------- 48 files changed, 192 insertions(+), 2131 deletions(-) delete mode 100644 src/Misc/Infinity.jl delete mode 100644 src/Misc/Localization.jl delete mode 100644 src/Misc/MPoly.jl delete mode 100644 src/Misc/Residue.jl delete mode 100644 src/Misc/ResidueRingPoly.jl delete mode 100644 test/Misc/Infinity.jl delete mode 100644 test/Misc/Localization.jl diff --git a/src/AlgAss/ChangeRing.jl b/src/AlgAss/ChangeRing.jl index 75aecba0d8..4345c54eeb 100644 --- a/src/AlgAss/ChangeRing.jl +++ b/src/AlgAss/ChangeRing.jl @@ -74,8 +74,6 @@ mutable struct AlgAssResMor{S, T, U, V} <: Map{S, T, HeckeMap, AlgAssResMor} end end -degree(::QQField) = 1 - domain(f::AlgAssResMor) = f.domain codomain(f::AlgAssResMor) = f.codomain diff --git a/src/AlgAssAbsOrd/Conjugacy/Husert.jl b/src/AlgAssAbsOrd/Conjugacy/Husert.jl index 1fd19be0a5..8811333801 100644 --- a/src/AlgAssAbsOrd/Conjugacy/Husert.jl +++ b/src/AlgAssAbsOrd/Conjugacy/Husert.jl @@ -256,8 +256,6 @@ function _explode(x::Generic.MatSpaceElem{nf_elem}) return z end -Base.:(*)(x::QQFieldElem, y::AbstractAlgebra.Generic.MatSpaceElem{nf_elem}) = base_ring(y)(x) * y - function _to_absolute_basis(v, m, ns, Ks) w = Vector{QQFieldElem}(undef, m) k = 1 diff --git a/src/AlgAssRelOrd/Eichler.jl b/src/AlgAssRelOrd/Eichler.jl index 7ff3b9d9ef..ba98fdb5b4 100644 --- a/src/AlgAssRelOrd/Eichler.jl +++ b/src/AlgAssRelOrd/Eichler.jl @@ -206,8 +206,6 @@ function _eichler_find_transforming_unit(I::AlgAssRelOrdIdl, J::AlgAssRelOrdIdl) return t end -degree(F::Union{ fpField, Generic.ResidueField{ZZRingElem} }) = 1 - function get_coeff_fmpz!(x::fqPolyRepFieldElem, n::Int, z::ZZRingElem) ccall((:fmpz_set_ui, libflint), Nothing, (Ref{ZZRingElem}, UInt), z, ccall((:nmod_poly_get_coeff_ui, libflint), UInt, (Ref{fqPolyRepFieldElem}, Int), x, n)) return z @@ -218,16 +216,6 @@ function get_coeff_fmpz!(x::FqPolyRepFieldElem, n::Int, z::ZZRingElem) return z end -function lift!(x::fpFieldElem, z::ZZRingElem) - ccall((:fmpz_set_ui, libflint), Nothing, (Ref{ZZRingElem}, UInt), z, x.data) - return z -end - -function lift!(x::Generic.ResidueFieldElem{ZZRingElem}, z::ZZRingElem) - ccall((:fmpz_set, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}), z, x.data) - return z -end - # Let v and w be non-zero k \times 1-matrices over a finite field and let # generators be a vector of invertible k \times k-matrices over this field. # This functions finds a matrix g in the group generated by the elements in diff --git a/src/Aliases.jl b/src/Aliases.jl index 1899e95ea9..9f10c6bf9a 100644 --- a/src/Aliases.jl +++ b/src/Aliases.jl @@ -122,7 +122,6 @@ @alias ismaximal_order_known is_maximal_order_known @alias ismodular is_modular @alias isnegative_definite is_negative_definite -@alias isnilpotent is_nilpotent @alias isnorm is_norm @alias isnorm_divisible is_norm_divisible @alias isnorm_divisible_pp is_norm_divisible_pp diff --git a/src/EllCrv/Heights.jl b/src/EllCrv/Heights.jl index 0a4ce30754..6611fa2a85 100644 --- a/src/EllCrv/Heights.jl +++ b/src/EllCrv/Heights.jl @@ -960,14 +960,3 @@ function refine_beta_bound(P::PolyElem, Q::PolyElem, E, mu::arb, a::arb, b::arb return beta_bound end - -#This should probably go somewhere else. (Taking the nth derivative) -function derivative(x::acb_poly, n::Int64) - for i in (1:n) - x = derivative(x) - end - return x -end - - - diff --git a/src/EllCrv/Misc.jl b/src/EllCrv/Misc.jl index 304c49b73b..6fac560073 100644 --- a/src/EllCrv/Misc.jl +++ b/src/EllCrv/Misc.jl @@ -56,26 +56,6 @@ end # ################################################################################ -@doc raw""" - zeros(f::ZZPolyRingElem) -> Vector{ZZRingElem} - -Computes the integer zeros of a given polynomial $f$. -""" -function zeros(f::ZZPolyRingElem) - - fac = factor(f) - zeros = Nemo.ZZRingElem[] - - # check if there are monic linear factors <-> zeros - for i in fac - if degree(i[1]) == 1 && leading_coefficient(i[1]) == 1 - push!(zeros, -coeff(i[1],0)) - end - end - - return zeros -end - # @doc raw""" # quadroots(a::ZZRingElem, b::ZZRingElem, c::ZZRingElem, p::ZZRingElem) -> Bool @@ -234,9 +214,6 @@ function normal_basis(K::T, L::T) where T<:FinField end -jacobi_symbol(x::Integer, y::ZZRingElem) = jacobi_symbol(ZZRingElem(x), y) - - function mod(a::nf_elem, I::NfOrdIdl) R = order(I) k, phi = residue_field(R, I) diff --git a/src/GenOrd/Auxiliary.jl b/src/GenOrd/Auxiliary.jl index e40159397a..94c78c9c6e 100644 --- a/src/GenOrd/Auxiliary.jl +++ b/src/GenOrd/Auxiliary.jl @@ -72,24 +72,6 @@ function hnf_modular(M::MatElem{T}, d::T, is_prime::Bool = false) where {T} return H[1:ncols(M), :] end -function Base.divrem(a::ZZModRingElem, b::ZZModRingElem) - R = parent(a) - r = rem(a, b) - return divexact(a-r, b), r -end - -function Base.div(a::ZZModRingElem, b::ZZModRingElem) - R = parent(a) - r = rem(a, b) - return divexact(a-r, b) -end - -function Base.rem(a::ZZModRingElem, b::ZZModRingElem) - R = parent(a) - r = R(rem(lift(a), gcd(modulus(R), lift(b)))) - return r -end - function function_field(f::PolyElem{<:Generic.RationalFunctionFieldElem}, s::String = "_a"; check::Bool = true, cached::Bool = false) return FunctionField(f, s, cached = cached) end @@ -153,21 +135,12 @@ function Hecke.discriminant(F::Generic.FunctionField) return discriminant(defining_polynomial(F)) end -function (R::QQPolyRing)(a::Generic.RationalFunctionFieldElem{QQFieldElem}) - @assert isone(denominator(a)) - return R(numerator(a)) -end - ####################################################################### # # support for ZZ # ####################################################################### -denominator(a::QQFieldElem, ::ZZRing) = denominator(a) - -numerator(a::QQFieldElem, ::ZZRing) = numerator(a) - integral_split(a::QQFieldElem, ::ZZRing) = (numerator(a), denominator(a)) integral_split(a::Rational, R::ZZRing) = integral_split(QQFieldElem(a), R) diff --git a/src/GrpAb/stable_sub.jl b/src/GrpAb/stable_sub.jl index 738322c2b2..2a37b66b47 100644 --- a/src/GrpAb/stable_sub.jl +++ b/src/GrpAb/stable_sub.jl @@ -18,31 +18,6 @@ function show(io::IO, M::ZpnGModule) print(io, "Module over Z/", M.R.n, "Z with structure ", M.V) end -# -# Lifts a matrix from F_p to Z/p^nZ -# - -function lift(M::fqPolyRepMatrix, R::Nemo.zzModRing) - @hassert :StabSub 1 is_prime_power(modulus(R)) - N=zero_matrix(R,nrows(M),ncols(M)) - for i=1:nrows(M) - for j=1:ncols(M) - N[i,j]=FlintZZ(coeff(M[i,j],0)) - end - end - return N -end - -function lift(M::fpMatrix, R::Nemo.zzModRing) - @hassert :StabSub 1 is_prime_power(modulus(R)) - N=zero_matrix(R, nrows(M), ncols(M)) - for i=1:nrows(M) - for j=1:ncols(M) - N[i,j] = R(lift(M[i,j])) - end - end - return N -end # Action of a matrix on an element of the group function *(x::GrpAbFinGenElem, M::zzModMatrix) diff --git a/src/Hecke.jl b/src/Hecke.jl index ac0b09323f..5453836474 100644 --- a/src/Hecke.jl +++ b/src/Hecke.jl @@ -675,8 +675,6 @@ end elem_type(::Type{FacElemMon{T}}) where {T} = FacElem{elem_type(T), T} -elem_type(::Type{Generic.ResidueRing{T}}) where {T} = Generic.ResidueRingElem{T} - ################################################################################ # # Aliases diff --git a/src/LinearAlgebra/Solve.jl b/src/LinearAlgebra/Solve.jl index d36bc8f73b..e97f963a44 100644 --- a/src/LinearAlgebra/Solve.jl +++ b/src/LinearAlgebra/Solve.jl @@ -62,18 +62,7 @@ function modular_lift(ap::Vector{fqPolyRepMatrix}, me::modular_env) return A end -@doc raw""" - mod!(A::Generic.Mat{nf_elem}, m::ZZRingElem) -Inplace: reduce all entries of $A$ modulo $m$, into the positive residue system. -""" -function mod!(A::Generic.Mat{nf_elem}, m::ZZRingElem) - for i=1:nrows(A) - for j=1:ncols(A) - mod!(A[i, j], m) - end - end -end @doc raw""" mod_sym!(A::Generic.Mat{nf_elem}, m::ZZRingElem) @@ -221,18 +210,6 @@ function denominator_ideal(M::Vector{nf_elem}, den::nf_elem) return d end -@doc raw""" - divexact!(A::Generic.Mat{nf_elem}, p::ZZRingElem) - -Inplace: divide each entry of $A$ by $p$. -""" -function divexact!(A::Generic.Mat{nf_elem}, p::ZZRingElem) - for i=1:nrows(A) - for j=1:ncols(A) - A[i,j] = A[i,j]//p - end - end -end #TODO/ To experiment: # - vector reconstruction ala Storjohan diff --git a/src/LocalField/Poly.jl b/src/LocalField/Poly.jl index aa61cb9d19..28bade2375 100644 --- a/src/LocalField/Poly.jl +++ b/src/LocalField/Poly.jl @@ -719,7 +719,6 @@ function _rres(f::Generic.Poly{T}, g::Generic.Poly{T}) where T <: Union{padic, q return res*res1 end -degree(::FlintPadicField) = 1 base_field(Q::FlintQadicField) = base_ring(defining_polynomial(Q)) function norm(f::PolyElem{T}) where T <: Union{qadic, LocalFieldElem} @@ -732,10 +731,6 @@ function norm(f::PolyElem{T}) where T <: Union{qadic, LocalFieldElem} return inflate(N, i) end -function norm(f::PolyElem{padic}) - return f -end - @doc raw""" characteristic_polynomial(f::Generic.Poly{T}, g::Generic.Poly{T}) where T <: Union{padic, qadic} -> Generic.Poly{T} diff --git a/src/LocalField/map.jl b/src/LocalField/map.jl index 1c5595e01d..06de4b7f5c 100644 --- a/src/LocalField/map.jl +++ b/src/LocalField/map.jl @@ -509,7 +509,7 @@ function ^(f::LocalFieldMor, b::Int) end end -^(a::LocalFieldMor, n::IntegerUnion) = _generic_power(a, n) +^(a::LocalFieldMor, n::IntegerUnion) = Nemo._generic_power(a, n) ################################################################################ # diff --git a/src/LocalField/neq.jl b/src/LocalField/neq.jl index 7e64d8d171..543d8f9c03 100644 --- a/src/LocalField/neq.jl +++ b/src/LocalField/neq.jl @@ -26,7 +26,7 @@ end ########### any_root computes a single root in the finite field extensions#### -import Nemo:any_root +import Nemo: any_root function any_root(f::Union{fpPolyRingElem, fqPolyRepPolyRingElem}, F::Union{fqPolyRepField, Hecke.RelFinField}) g = polynomial(F, [coeff(f,i) for i = 0:degree(f) ] ) return any_root(g) @@ -81,20 +81,6 @@ function norm_equation(F::Union{FlintQadicField, Hecke.LocalField{padic, Hecke.U return A*T end -function Nemo.basis(k::Nemo.fpField) - return [k(1)] -end - -function Nemo.basis(k::Nemo.fpField, l::Nemo.fpField) - @assert k == l - return [k(1)] -end - -function Nemo.basis(K::fqPolyRepField, k::Nemo.fpField) - @assert characteristic(K) == characteristic(k) - return basis(K) -end - function Nemo.basis(K::FinField, k::FinField) b = basis(K) K = base_ring(K) @@ -145,15 +131,6 @@ function one_unit_group_gens(K::Union{FlintQadicField, Hecke.LocalField}) end end -function root(a::FinFieldElem, n::ZZRingElem) - return root(a, Int(n)) -end -function root(a::FinFieldElem, n::Integer) - k = parent(a) - kt, t = polynomial_ring(k, "t", cached = false) - r = roots(t^n-a) - return r[1] -end function _unit_group_gens_case2(K::Union{FlintQadicField, Hecke.LocalField}) p = prime(K) @@ -239,7 +216,6 @@ function coordinates(a::Union{qadic, LocalFieldElem}, k) return c end coordinates(a::padic, ::FlintPadicField) = [a] -prime_field(k::FlintPadicField) = k lift(a::Hecke.QadicRingElem{FlintPadicField, padic}) = lift(a.x) function setprecision!(A::Generic.MatSpaceElem{Hecke.QadicRingElem{FlintPadicField, padic}}, n::Int) @@ -457,10 +433,6 @@ function basis(K::RelFinField) return b end -function base_field(K::fqPolyRepField) - return Native.GF(Int(characteristic(K))) -end - absolute_frobenius_matrix(K::fqPolyRepField, d::Int = 1) = frobenius_matrix(K, d) absolute_frobenius_matrix(K::Nemo.fpField, d::Int = 1) = matrix(K, 1, 1, [1]) @@ -771,18 +743,7 @@ function local_fundamental_class_serre(L::Hecke.LocalField, K::Union{Hecke.Local return local_fundamental_class_serre(hom(K, L, L(gen(K)))) end -function gens(k::FlintPadicField, K::FlintPadicField) - return [k(1)] -end -function gen(k::Nemo.fpField) - return k(1) -end - -function defining_polynomial(k::Nemo.fpField) - kx, x = polynomial_ring(k, cached = false) - return x-k(1) -end function local_fundamental_class_serre(mKL::LocalFieldMor) K = domain(mKL) diff --git a/src/LocalField/pAdic.jl b/src/LocalField/pAdic.jl index ae74c77d5a..4ab233f560 100644 --- a/src/LocalField/pAdic.jl +++ b/src/LocalField/pAdic.jl @@ -1,20 +1,3 @@ -@doc raw""" - lift(a::padic) -> ZZRingElem - -Returns the positive canonical representative in $\mathbb{Z}$. $a$ needs -to be integral. -""" -function lift(a::padic) - b = ZZRingElem() - R = parent(a) - - if iszero(a) - return ZZ(0) - end - ccall((:padic_get_fmpz, libflint), Nothing, (Ref{ZZRingElem}, Ref{padic}, Ref{FlintPadicField}), b, a, R) - return b -end - function _lift(a::padic) R = parent(a) v = valuation(a) @@ -26,22 +9,6 @@ function _lift(a::padic) end end -function Base.setprecision(f::Generic.Poly{padic}, N::Int) - g = parent(f)() - fit!(g, length(f)) - for i=1:length(f) - g.coeffs[i] = setprecision!(f.coeffs[i], N) - end - set_length!(g, normalise(g, length(f))) - return g -end - -function setprecision!(f::Generic.Poly{padic}, N::Int) - for i=1:length(f) - f.coeffs[i] = setprecision!(f.coeffs[i], N) - end - return f -end """ The log of `1-x`, x needs to have a valuation >1 diff --git a/src/LocalField/qAdic.jl b/src/LocalField/qAdic.jl index e17b474a85..e6d21436ec 100644 --- a/src/LocalField/qAdic.jl +++ b/src/LocalField/qAdic.jl @@ -1,115 +1,7 @@ add_verbosity_scope(:qAdic) add_assertion_scope(:qAdic) -export setprecision!, defining_polynomial - -function Base.setprecision(q::qadic, N::Int) - r = parent(q)() - r.N = N - ccall((:padic_poly_set, libflint), Nothing, (Ref{qadic}, Ref{qadic}, Ref{FlintQadicField}), r, q, parent(q)) - return r -end - -function Base.setprecision(q::padic, N::Int) - r = parent(q)() - r.N = N - ccall((:padic_set, libflint), Nothing, (Ref{padic}, Ref{padic}, Ref{FlintPadicField}), r, q, parent(q)) - return r -end - -function setprecision!(q::qadic, N::Int) - if N >= q.N - q.N = N - end - q.N = N - ccall((:qadic_reduce, libflint), Nothing, (Ref{qadic}, Ref{FlintQadicField}), q, parent(q)) -# @assert N >= q.N - return q -end - -function setprecision!(Q::FlintQadicField, n::Int) - Q.prec_max = n -end - -function setprecision!(Q::FlintPadicField, n::Int) - Q.prec_max = n -end - -function Base.setprecision(f::Generic.MPoly{qadic}, N::Int) - return map_coefficients(x->setprecision(x, N), f, parent = parent(f)) -end - -function setprecision!(a::AbstractArray{qadic}, N::Int) - for x = a - setprecision!(x, N) - end -end - -function Base.setprecision(a::AbstractArray{qadic}, N::Int) - return map(x->setprecision(x, N), a) -end - -function setprecision!(a::Generic.MatSpaceElem{qadic}, N::Int) - setprecision!(a.entries, N) -end - -function Base.setprecision(a::Generic.MatSpaceElem{qadic}, N::Int) - b = deepcopy(a) - setprecision!(b, N) - return B -end - -function tr(r::qadic) - t = coefficient_ring(parent(r))() - ccall((:qadic_trace, libflint), Nothing, (Ref{padic}, Ref{qadic}, Ref{FlintQadicField}), t, r, parent(r)) - return t -end - -function norm(r::qadic) - t = coefficient_ring(parent(r))() - ccall((:qadic_norm, libflint), Nothing, (Ref{padic}, Ref{qadic}, Ref{FlintQadicField}), t, r, parent(r)) - return t -end - -function setcoeff!(x::fqPolyRepFieldElem, n::Int, u::UInt) - ccall((:nmod_poly_set_coeff_ui, libflint), Nothing, - (Ref{fqPolyRepFieldElem}, Int, UInt), x, n, u) -end - -function (Rx::Generic.PolyRing{padic})(a::qadic) - Qq = parent(a) - #@assert Rx === parent(defining_polynomial(Qq)) - R = base_ring(Rx) - coeffs = Vector{padic}(undef, degree(Qq)) - for i = 1:length(coeffs) - c = R() - ccall((:padic_poly_get_coeff_padic, libflint), Nothing, - (Ref{padic}, Ref{qadic}, Int, Ref{FlintQadicField}), c, a, i-1, parent(a)) - coeffs[i] = c - end - return Rx(coeffs) -end - - -function coeff(x::qadic, i::Int) - R = FlintPadicField(prime(parent(x)), parent(x).prec_max) - c = R() - ccall((:padic_poly_get_coeff_padic, libflint), Nothing, - (Ref{padic}, Ref{qadic}, Int, Ref{FlintQadicField}), c, x, i, parent(x)) - return c -end - -function setcoeff!(x::qadic, i::Int, y::padic) - ccall((:padic_poly_set_coeff_padic, libflint), Nothing, - (Ref{qadic}, Int, Ref{padic}, Ref{FlintQadicField}), x, i, y, parent(x)) -end - -function setcoeff!(x::qadic, i::Int, y::UInt) - R = FlintPadicField(prime(parent(x)), parent(x).prec_max) - Y = R(ZZRingElem(y)) - ccall((:padic_poly_set_coeff_padic, libflint), Nothing, - (Ref{qadic}, Int, Ref{padic}, Ref{FlintQadicField}), x, i, Y, parent(x)) -end +export defining_polynomial @attributes FlintQadicField @@ -161,17 +53,8 @@ function residue_field(Q::FlintPadicField) return k, MapFromFunc(pro, lif, Q, k) end -function coefficient_ring(Q::FlintQadicField) - return FlintPadicField(prime(Q), precision(Q)) -end coefficient_field(Q::FlintQadicField) = coefficient_ring(Q) -function prime(R::PadicField, i::Int) - p = ZZRingElem() - ccall((:padic_ctx_pow_ui, libflint), Cvoid, (Ref{ZZRingElem}, Int, Ref{PadicField}), p, i, R) - return p -end - function getUnit(a::padic) u = ZZRingElem() ccall((:fmpz_set, libflint), Cvoid, (Ref{ZZRingElem}, Ref{Int}), u, a.u) @@ -196,27 +79,10 @@ function lift_reco(::QQField, a::padic; reco::Bool = false) end end -function *(A::ZZMatrix, B::MatElem{padic}) - return matrix(base_ring(B), A) * B -end uniformizer(Q::FlintQadicField) = Q(prime(Q)) -Base.precision(Q::FlintQadicField) = Q.prec_max uniformizer(Q::FlintPadicField) = Q(prime(Q)) -Base.precision(Q::FlintPadicField) = Q.prec_max - -nrows(A::Matrix{T}) where {T} = size(A)[1] -ncols(A::Matrix{T}) where {T} = size(A)[2] - -import Base.^ -^(a::qadic, b::qadic) = exp(b*log(a)) -^(a::padic, b::padic) = exp(b*log(a)) - -import Base.// -//(a::qadic, b::qadic) = divexact(a, b) -//(a::padic, b::qadic) = divexact(a, b) -//(a::qadic, b::padic) = divexact(a, b) function defining_polynomial(Q::FlintQadicField, P::Ring = coefficient_ring(Q)) Pt, t = polynomial_ring(P, cached = false) diff --git a/src/Map/Map.jl b/src/Map/Map.jl index 2920177014..6341284f05 100644 --- a/src/Map/Map.jl +++ b/src/Map/Map.jl @@ -7,42 +7,6 @@ # return M.header.preim #end -function show(io::IO, M::Map) - @show_name(io, M) - if get(io, :compact, false) - print(io, domain(M), " --> ", codomain(M), "\n") - return - end - io = Base.IOContext(io, :compact => true) - print(io, "Map with following data\n") - print(io, "Domain:\n") - print(io, "=======\n") - print(io, domain(M)) - print(io, "\nCodomain:\n") - print(io, "=========\n") - print(io, codomain(M)) -end - -function preimage(M::Map{D, C}, a) where {D, C} - if isdefined(M.header, :preimage) - p = M.header.preimage(a)::elem_type(D) - @assert parent(p) === domain(M) - return p - end - error("No preimage function known") -end - -function image(M::Map{D, C}, a) where {D, C} - if isdefined(M, :header) - if isdefined(M.header, :image) - return M.header.image(a)::elem_type(C) - else - error("No image function known") - end - else - return M(a) - end -end function show(io::IO, M::InverseMap) @show_name(io, M) @@ -58,9 +22,6 @@ end # println(io, "Coerce: $(domain(M)) -> $(codomain(M))") #end -\(f::Map, x) = preimage(f, x) - - function change(D::Dict{K, V}, k::K, def::V, new::Function) where {K, V} i = Base.ht_keyindex2!(D, k) if i>0 diff --git a/src/Map/MapType.jl b/src/Map/MapType.jl index 96fe1788d7..8e297657d5 100644 --- a/src/Map/MapType.jl +++ b/src/Map/MapType.jl @@ -104,11 +104,6 @@ function MapHeader(domain::D, codomain::C, image, preimage) where {D, C} return MapHeader{D, C}(domain, codomain, image, preimage) end - -function preimage(f::AbstractAlgebra.Generic.CompositeMap, a) - return preimage(f.map1, preimage(f.map2, a)) -end - preimage_function(f) = a -> preimage(f, a) image_function(f) = a -> image(f, a) diff --git a/src/Map/NfOrd.jl b/src/Map/NfOrd.jl index 94512afae3..7e53664c6a 100644 --- a/src/Map/NfOrd.jl +++ b/src/Map/NfOrd.jl @@ -38,7 +38,7 @@ mutable struct NfOrdToFqNmodMor <: Map{NfOrd, fqPolyRepField, HeckeMap, NfOrdToF function _image(x::NfOrdElem) u = F() gg = parent(nf(O).pol)(elem_in_nf(x))::QQPolyRingElem - fmpq_poly_to_gfp_poly_raw!(tmp_gfp_poly, gg) + Nemo.fmpq_poly_to_gfp_poly_raw!(tmp_gfp_poly, gg) ccall((:nmod_poly_rem, libflint), Nothing, (Ref{fpPolyRingElem}, Ref{fpPolyRingElem}, Ref{fpPolyRingElem}, Ptr{Nothing}), tmp_gfp_poly, tmp_gfp_poly, g, pointer_from_objref(F)+sizeof(ZZRingElem)) @@ -328,7 +328,7 @@ function image(f::NfOrdToFqMor, x::NfOrdElem) O = domain(f) u = F() gg = parent(nf(O).pol)(elem_in_nf(x, copy = false))::QQPolyRingElem - fmpq_poly_to_gfp_fmpz_poly_raw!(f.tmp_gfp_fmpz_poly, gg, f.t_fmpz_poly, f.t_fmpz) + Nemo.fmpq_poly_to_gfp_fmpz_poly_raw!(f.tmp_gfp_fmpz_poly, gg, f.t_fmpz_poly, f.t_fmpz) ccall((:fmpz_mod_poly_rem, libflint), Nothing, (Ref{FpPolyRingElem}, Ref{FpPolyRingElem}, Ref{FpPolyRingElem}, Ref{Nemo.fmpz_mod_ctx_struct}), f.tmp_gfp_fmpz_poly, f.tmp_gfp_fmpz_poly, f.poly_of_the_field, f.tmp_gfp_fmpz_poly.parent.base_ring.ninv) ccall((:fq_set, libflint), Nothing, (Ref{FqPolyRepFieldElem}, Ref{FpPolyRingElem}, Ref{FqPolyRepField}), u, f.tmp_gfp_fmpz_poly, F) ccall((:fq_reduce, libflint), Nothing, (Ref{FqPolyRepFieldElem}, Ref{FqPolyRepField}), u, F) @@ -391,7 +391,7 @@ mutable struct NfOrdToGFMor <: Map{NfOrd, fpField, HeckeMap, NfOrdToFqNmodMor} let g = g, tmp_gfp_poly = tmp_gfp_poly, O = O, F = F function _image(x::NfOrdElem) gg = parent(nf(O).pol)(elem_in_nf(x))::QQPolyRingElem - fmpq_poly_to_gfp_poly_raw!(tmp_gfp_poly, gg) + Nemo.fmpq_poly_to_gfp_poly_raw!(tmp_gfp_poly, gg) ccall((:nmod_poly_rem, libflint), Nothing, (Ref{fpPolyRingElem}, Ref{fpPolyRingElem}, Ref{fpPolyRingElem}, Ptr{Nothing}), tmp_gfp_poly, tmp_gfp_poly, g, pointer_from_objref(F)+sizeof(ZZRingElem)) @@ -461,7 +461,7 @@ mutable struct NfOrdToGFFmpzMor <: Map{NfOrd, Nemo.FpField, HeckeMap, NfOrdToGFF let g = g, tmp_gfp_poly = tmp_gfp_poly, O = O, F = F function _image(x::NfOrdElem) gg = parent(nf(O).pol)(elem_in_nf(x))::QQPolyRingElem - fmpq_poly_to_gfp_fmpz_poly_raw!(tmp_gfp_poly, gg) + Nemo.fmpq_poly_to_gfp_fmpz_poly_raw!(tmp_gfp_poly, gg) rem!(tmp_gfp_poly, tmp_gfp_poly, g) return coeff(tmp_gfp_poly, 0) end @@ -619,7 +619,7 @@ function image(f::NfOrdToFqFieldMor, x::NfOrdElem) O = domain(f) u = F() gg = parent(nf(O).pol)(elem_in_nf(x, copy = false))::QQPolyRingElem - fmpq_poly_to_fq_default_poly_raw!(f.tmp_gfp_fmpz_poly, gg, f.t_fmpz_poly, f.t_fmpz) + Nemo.fmpq_poly_to_fq_default_poly_raw!(f.tmp_gfp_fmpz_poly, gg, f.t_fmpz_poly, f.t_fmpz) ccall((:fq_default_poly_rem, libflint), Nothing, (Ref{FqPolyRingElem}, Ref{FqPolyRingElem}, Ref{FqPolyRingElem}, Ref{Nemo.FqField}), f.tmp_gfp_fmpz_poly, f.tmp_gfp_fmpz_poly, f.poly_of_the_field, f.tmp_gfp_fmpz_poly.parent.base_ring) res = F.forwardmap(f.tmp_gfp_fmpz_poly)::FqFieldElem @assert parent(res) === F @@ -1308,14 +1308,7 @@ end # Helper -function mul!(z::fpFieldElem, x::fpFieldElem, y::ZZRingElem) - R = parent(x) - d = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), y, R.n) - r = ccall((:n_mulmod2_preinv, libflint), UInt, (UInt, UInt, UInt, UInt), - x.data, d, R.n, R.ninv) - z.data = r - return z -end + function mul_mod(x::UInt, y::ZZRingElem, R) d = ccall((:fmpz_fdiv_ui, libflint), UInt, (Ref{ZZRingElem}, UInt), y, R.n) @@ -1324,24 +1317,6 @@ function mul_mod(x::UInt, y::ZZRingElem, R) return r end -function mul!(z::Nemo.FpFieldElem, x::Nemo.FpFieldElem, y::ZZRingElem) - R = parent(x) - ccall((:fmpz_mod, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), - z.data, y, R.n) - - ccall((:fmpz_mod_mul, libflint), Nothing, - (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{Nemo.fmpz_mod_ctx_struct}), - z.data, x.data, z.data, R.ninv) - return z -end - -function rem!(z::fpPolyRingElem, a::fpPolyRingElem, b::fpPolyRingElem) - ccall((:nmod_poly_rem, libflint), Nothing, - (Ref{fpPolyRingElem}, Ref{fpPolyRingElem}, Ref{fpPolyRingElem}, Ptr{Nothing}), - z, a, b, pointer_from_objref(base_ring(z))+sizeof(ZZRingElem)) - return z -end - function evaluate_raw(x::fpPolyRingElem, y::fpFieldElem) z = ccall((:nmod_poly_evaluate_nmod, libflint), UInt, (Ref{fpPolyRingElem}, UInt), x, y.data) diff --git a/src/Map/NumField.jl b/src/Map/NumField.jl index b2d23b13fb..0a9af45464 100644 --- a/src/Map/NumField.jl +++ b/src/Map/NumField.jl @@ -978,7 +978,7 @@ function ^(f::NumFieldMor, b::Int) end end -^(a::NumFieldMor, n::IntegerUnion) = _generic_power(a, n) +^(a::NumFieldMor, n::IntegerUnion) = Nemo._generic_power(a, n) ################################################################################ # diff --git a/src/Map/NumberField.jl b/src/Map/NumberField.jl index c0455f2e1d..d888954f9d 100644 --- a/src/Map/NumberField.jl +++ b/src/Map/NumberField.jl @@ -81,26 +81,6 @@ function preimage(f::GrpGenToNfMorSet{S, T}, a::S) where {S, T} error("something wrong") end - -function evaluate(f::QQPolyRingElem, a::nf_elem) - #Base.show_backtrace(stdout, Base.stacktrace()) - R = parent(a) - if iszero(f) - return zero(R) - end - if a == gen(R) && parent(f) == parent(parent(a).pol) - return R(f) - end - l = length(f) - 1 - s = R(coeff(f, l)) - for i in l-1:-1:0 - #s = s*a + R(coeff(f, i)) - mul!(s, s, a) - add!(s, s, coeff(f, i)) - end - return s -end - Base.copy(f::NfToNfMor) = f ################################################################################ diff --git a/src/Misc.jl b/src/Misc.jl index 0fd12a574e..87af979348 100644 --- a/src/Misc.jl +++ b/src/Misc.jl @@ -1,6 +1,5 @@ include("Misc/PIDIdeal.jl") include("Misc/acb_root_ctx.jl") -include("Misc/Infinity.jl") include("Misc/MSet.jl") include("Misc/fmpr.jl") include("Misc/FactoredElem.jl") @@ -12,9 +11,6 @@ include("Misc/Matrix.jl") include("Misc/CRT.jl") # for crt_env and modular_env include("Misc/Poly.jl") include("Misc/nmod_poly.jl") -include("Misc/MPoly.jl") -include("Misc/Residue.jl") -include("Misc/ResidueRingPoly.jl") include("Misc/FiniteField.jl") include("Misc/Arb.jl") include("Misc/psibound.jl") @@ -28,7 +24,6 @@ include("Misc/serialize.jl") include("Misc/Series.jl") include("Misc/Screen.jl") #include("Misc/IJulia.jl") -include("Misc/Localization.jl") include("Misc/OrdLocalization.jl") include("Misc/jordan.jl") include("Misc/PseudoPolynomial.jl") diff --git a/src/Misc/FactoredElem.jl b/src/Misc/FactoredElem.jl index dbc4801e69..2bd77452a4 100644 --- a/src/Misc/FactoredElem.jl +++ b/src/Misc/FactoredElem.jl @@ -64,17 +64,7 @@ else end end -function cmpabs(a::Int, b::Int) - a = abs(a) - b = abs(b) - if a > b - return 1 - elseif a == b - return 0 - else - return -1 - end -end + ################################################################################ # # Multiplicative representation diff --git a/src/Misc/Infinity.jl b/src/Misc/Infinity.jl deleted file mode 100644 index f2b1b45224..0000000000 --- a/src/Misc/Infinity.jl +++ /dev/null @@ -1,95 +0,0 @@ -#$############################################################################## -# -# Misc/Infinite.jl: Infinity -# -# This file is part of Hecke. -# -# Copyright (c) 2015-2019: Claus Fieker, Tommy Hofmann -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# * Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. -# -# * Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# -# -# Copyright (C) 2019 Tommy Hofmann -# -################################################################################ - -export PosInf, inf, IntExt - -# This is a type for positive infinity for use in valuations. - -struct PosInf -end - -const inf = PosInf() - -+(::Int, ::PosInf) = inf - -+(::PosInf, ::Int) = inf - -+(::PosInf, ::PosInf) = inf - --(::PosInf, ::Int) = inf - -max(::Int, ::PosInf) = inf - -max(::PosInf, ::Int) = inf - -Base.isless(::Int, ::PosInf) = true - -Base.isless(x::Rational{Int}, ::PosInf) = denominator(x) != 0 - -Base.isless(::PosInf, ::PosInf) = false - -Base.isless(::PosInf, ::Int) = false - -Base.isless(::PosInf, ::Rational{Int}) = false - -Base.isfinite(::PosInf) = false - -Base.isinf(::PosInf) = true - -Base.isone(::PosInf) = false - -Base.iszero(::PosInf) = false - -Base.one(::PosInf) = 1 - -Base.zero(::PosInf) = 0 - -Base.isless(::PosInf, ::ZZRingElem) = false - -Base.isless(::ZZRingElem, ::PosInf) = true - -Base.isless(::PosInf, ::QQFieldElem) = false - -Base.isless(::QQFieldElem, ::PosInf) = true - -const IntExt = Union{Int, PosInf} - -is_positive(::PosInf) = true - -@doc raw""" - is_infinite(x::Any) -> Bool - -Tests whether $x$ is infinite, by returning `!isfinite(x)`. -""" -is_infinite(x::Any) = !isfinite(x) diff --git a/src/Misc/Integer.jl b/src/Misc/Integer.jl index c7c81da5a6..84b68a3266 100644 --- a/src/Misc/Integer.jl +++ b/src/Misc/Integer.jl @@ -13,62 +13,27 @@ is_commutative(::ZZRing) = true The multiplicative order of a modulo $m$ (not a good algorithm). """ function modord(a::ZZRingElem, m::ZZRingElem) - gcd(a,m)!=1 && error("1st argument not a unit") + gcd(a, m) != 1 && error("1st argument not a unit") i = 1 b = a % m while b != 1 i += 1 - b = b*a % m + b = b * a % m end return i end function modord(a::Integer, m::Integer) - gcd(a,m)!=1 && error("1st argument not a unit") + gcd(a, m) != 1 && error("1st argument not a unit") i = 1 b = a % m while b != 1 i += 1 - b = b*a % m + b = b * a % m end return i end -function neg!(a::ZZRingElem) - ccall((:fmpz_neg, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}), a, a) - return a -end - -############################################################ -# more unsafe function that Bill does not want to have.... -############################################################ - - -function mod!(z::ZZRingElem, x::ZZRingElem, y::ZZRingElem) - ccall((:fmpz_mod, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), z, x, y) - return z -end - -function divexact!(z::ZZRingElem, x::ZZRingElem, y::ZZRingElem) - iszero(y) && throw(DivideError()) - ccall((:fmpz_divexact, libflint), Nothing, - (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), z, x, y) - return z -end - -function lcm!(z::ZZRingElem, x::ZZRingElem, y::ZZRingElem) - ccall((:fmpz_lcm, libflint), Nothing, - (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), z, x, y) - return z -end - -function gcd!(z::ZZRingElem, x::ZZRingElem, y::ZZRingElem) - ccall((:fmpz_gcd, libflint), Nothing, - (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), z, x, y) - return z -end - - ################################################################################ # # Chinese remaindering modulo UInts to ZZRingElem @@ -88,14 +53,14 @@ mutable struct fmpz_comb function fmpz_comb(primes::Vector{UInt}) z = new() ccall((:fmpz_comb_init, libflint), Nothing, (Ref{fmpz_comb}, Ptr{UInt}, Int), - z, primes, length(primes)) + z, primes, length(primes)) finalizer(_fmpz_comb_clear_fn, z) return z end end function _fmpz_comb_clear_fn(z::fmpz_comb) - ccall((:fmpz_comb_clear, libflint), Nothing, (Ref{fmpz_comb}, ), z) + ccall((:fmpz_comb_clear, libflint), Nothing, (Ref{fmpz_comb},), z) end mutable struct fmpz_comb_temp @@ -107,26 +72,26 @@ mutable struct fmpz_comb_temp function fmpz_comb_temp(comb::fmpz_comb) z = new() ccall((:fmpz_comb_temp_init, libflint), Nothing, - (Ref{fmpz_comb_temp}, Ref{fmpz_comb}), z, comb) + (Ref{fmpz_comb_temp}, Ref{fmpz_comb}), z, comb) finalizer(_fmpz_comb_temp_clear_fn, z) return z end end function _fmpz_comb_temp_clear_fn(z::fmpz_comb_temp) - ccall((:fmpz_comb_temp_clear, libflint), Nothing, (Ref{fmpz_comb_temp}, ), z) + ccall((:fmpz_comb_temp_clear, libflint), Nothing, (Ref{fmpz_comb_temp},), z) end function fmpz_multi_crt_ui!(z::ZZRingElem, a::Vector{UInt}, b::fmpz_comb, c::fmpz_comb_temp) ccall((:fmpz_multi_CRT_ui, libflint), Nothing, - (Ref{ZZRingElem}, Ptr{UInt}, Ref{fmpz_comb}, Ref{fmpz_comb_temp}, Cint), - z, a, b, c, 1) + (Ref{ZZRingElem}, Ptr{UInt}, Ref{fmpz_comb}, Ref{fmpz_comb_temp}, Cint), + z, a, b, c, 1) return z end function _fmpz_preinvn_struct_clear_fn(z::fmpz_preinvn_struct) - ccall((:fmpz_preinvn_clear, libflint), Nothing, (Ref{fmpz_preinvn_struct}, ), z) + ccall((:fmpz_preinvn_clear, libflint), Nothing, (Ref{fmpz_preinvn_struct},), z) end function fdiv_qr_with_preinvn!(q::ZZRingElem, r::ZZRingElem, g::ZZRingElem, h::ZZRingElem, hinv::fmpz_preinvn_struct) @@ -145,12 +110,12 @@ end ################################################################################ function mod_sym(a::ZZRingElem, b::ZZRingElem) - c = mod(a,b) - @assert c>=0 - if b > 0 && 2*c > b - return c-b - elseif b < 0 && 2*c > -b - return c+b + c = mod(a, b) + @assert c >= 0 + if b > 0 && 2 * c > b + return c - b + elseif b < 0 && 2 * c > -b + return c + b else return c end @@ -163,8 +128,8 @@ end # ################################################################################ -mutable struct MapSUnitGrpZFacElem <: Map{GrpAbFinGen, FacElemMon{QQField}, HeckeMap, MapSUnitGrpZFacElem} - header::MapHeader{GrpAbFinGen, FacElemMon{QQField}} +mutable struct MapSUnitGrpZFacElem <: Map{GrpAbFinGen,FacElemMon{QQField},HeckeMap,MapSUnitGrpZFacElem} + header::MapHeader{GrpAbFinGen,FacElemMon{QQField}} idl::Vector{ZZRingElem} function MapSUnitGrpZFacElem() @@ -176,8 +141,8 @@ function show(io::IO, mC::MapSUnitGrpZFacElem) println(io, "SUnits (in factored form) map of $(codomain(mC)) for $(mC.idl)") end -mutable struct MapSUnitGrpZ <: Map{GrpAbFinGen, QQField, HeckeMap, MapSUnitGrpZ} - header::MapHeader{GrpAbFinGen, QQField} +mutable struct MapSUnitGrpZ <: Map{GrpAbFinGen,QQField,HeckeMap,MapSUnitGrpZ} + header::MapHeader{GrpAbFinGen,QQField} idl::Vector{ZZRingElem} function MapSUnitGrpZ() @@ -198,22 +163,22 @@ rational numbers divisible only by primes in $S$. The second return value is the map mapping group elements to rationals in factored form or rationals back to group elements. """ -function sunit_group_fac_elem(S::Vector{T}) where T <: Integer - return sunit_group_fac_elem(ZZRingElem[x for x=S]) +function sunit_group_fac_elem(S::Vector{T}) where {T<:Integer} + return sunit_group_fac_elem(ZZRingElem[x for x = S]) end function sunit_group_fac_elem(S::Vector{ZZRingElem}) S = coprime_base(S) #TODO: for S-units use factor??? - G = abelian_group(vcat(ZZRingElem[2], ZZRingElem[0 for i=S])) + G = abelian_group(vcat(ZZRingElem[2], ZZRingElem[0 for i = S])) S = vcat(ZZRingElem[-1], S) mp = MapSUnitGrpZFacElem() mp.idl = S - Sq = QQFieldElem[x for x=S] + Sq = QQFieldElem[x for x = S] function dexp(a::GrpAbFinGenElem) - return FacElem(Sq, ZZRingElem[a.coeff[1,i] for i=1:length(S)]) + return FacElem(Sq, ZZRingElem[a.coeff[1, i] for i = 1:length(S)]) end mp.header = MapHeader(G, FacElemMon(FlintQQ), dexp) @@ -222,9 +187,9 @@ function sunit_group_fac_elem(S::Vector{ZZRingElem}) end function preimage(f::MapSUnitGrpZFacElem, a::ZZRingElem) - g = Int[a>=0 ? 0 : 1] + g = Int[a >= 0 ? 0 : 1] S = f.idl - g = vcat(g, Int[valuation(a, x) for x=S[2:end]]) + g = vcat(g, Int[valuation(a, x) for x = S[2:end]]) return domain(f)(g) end @@ -241,7 +206,7 @@ function preimage(f::MapSUnitGrpZFacElem, a::QQFieldElem) end function preimage(f::MapSUnitGrpZFacElem, a::FacElem) - return sum(GrpAbFinGenElem[e*preimage(f, k) for (k,e) = a.fac]) + return sum(GrpAbFinGenElem[e * preimage(f, k) for (k, e) = a.fac]) end @doc raw""" @@ -253,8 +218,8 @@ rational numbers divisible only by primes in $S$. The second return value is the map mapping group elements to rationals or rationals back to group elements. """ -function sunit_group(S::Vector{T}) where T <: Integer - return sunit_group(ZZRingElem[x for x=S]) +function sunit_group(S::Vector{T}) where {T<:Integer} + return sunit_group(ZZRingElem[x for x = S]) end function sunit_group(S::Vector{ZZRingElem}) @@ -308,12 +273,12 @@ end function flint_rand_state() A = flint_rand_ctx_t() - A.a = ccall((:flint_rand_alloc, libflint), Ptr{Nothing}, (Int, ), 1) - ccall((:flint_randinit, libflint), Nothing, (Ptr{Nothing}, ), A.a) + A.a = ccall((:flint_rand_alloc, libflint), Ptr{Nothing}, (Int,), 1) + ccall((:flint_randinit, libflint), Nothing, (Ptr{Nothing},), A.a) function clean_rand_state(A::flint_rand_ctx_t) - ccall((:flint_randclear, libflint), Nothing, (Ptr{Nothing}, ), A.a) - ccall((:flint_rand_free, libflint), Nothing, (Ptr{Nothing}, ), A.a) + ccall((:flint_randclear, libflint), Nothing, (Ptr{Nothing},), A.a) + ccall((:flint_rand_free, libflint), Nothing, (Ptr{Nothing},), A.a) nothing end finalizer(clean_rand_state, A) @@ -322,13 +287,13 @@ end global flint_rand_ctx -function ecm(a::ZZRingElem, B1::UInt, B2::UInt, ncrv::UInt, rnd = flint_rand_ctx) +function ecm(a::ZZRingElem, B1::UInt, B2::UInt, ncrv::UInt, rnd=flint_rand_ctx) f = ZZRingElem() r = ccall((:fmpz_factor_ecm, libflint), Int32, (Ref{ZZRingElem}, UInt, UInt, UInt, Ptr{Nothing}, Ref{ZZRingElem}), f, ncrv, B1, B2, rnd.a, a) return r, f end -function ecm(a::ZZRingElem, B1::Int, B2::Int, ncrv::Int, rnd = flint_rand_ctx) +function ecm(a::ZZRingElem, B1::Int, B2::Int, ncrv::Int, rnd=flint_rand_ctx) return ecm(a, UInt(B1), UInt(B2), UInt(ncrv), rnd) end @@ -336,7 +301,7 @@ end B1 = [2, 11, 50, 250, 1000, 3000, 11000, 43000, 110000, 260000, 850000, 2900000]; nC = [25, 90, 300, 700, 1800, 5100, 10600, 19300, 49000, 124000, 210000, 340000]; -function ecm(a::ZZRingElem, max_digits::Int = div(ndigits(a), 3), rnd = flint_rand_ctx) +function ecm(a::ZZRingElem, max_digits::Int=div(ndigits(a), 3), rnd=flint_rand_ctx) n = ndigits(a, 10) B1s = 15 @@ -344,9 +309,9 @@ function ecm(a::ZZRingElem, max_digits::Int = div(ndigits(a), 3), rnd = flint_ra s = max(div(max_digits - 10, 5), 1) #i = s = max(i, s) while i <= s - e, f = ecm(a, B1[i]*1000, B1[i]*1000*100, nC[i], rnd) + e, f = ecm(a, B1[i] * 1000, B1[i] * 1000 * 100, nC[i], rnd) if e != 0 - return (e,f) + return (e, f) end i += 1 if i > length(B1) @@ -357,16 +322,16 @@ function ecm(a::ZZRingElem, max_digits::Int = div(ndigits(a), 3), rnd = flint_ra end function factor_trial_range(N::ZZRingElem, start::Int=0, np::Int=10^5) - F = Nemo.fmpz_factor() - ccall((:fmpz_factor_trial_range, libflint), Nothing, (Ref{Nemo.fmpz_factor}, Ref{ZZRingElem}, UInt, UInt), F, N, start, np) - res = Dict{ZZRingElem, Int}() - for i in 1:F.num - z = ZZRingElem() - ccall((:fmpz_factor_get_fmpz, libflint), Nothing, - (Ref{ZZRingElem}, Ref{Nemo.fmpz_factor}, Int), z, F, i - 1) - res[z] = unsafe_load(F.exp, i) - end - return res, canonical_unit(N) + F = Nemo.fmpz_factor() + ccall((:fmpz_factor_trial_range, libflint), Nothing, (Ref{Nemo.fmpz_factor}, Ref{ZZRingElem}, UInt, UInt), F, N, start, np) + res = Dict{ZZRingElem,Int}() + for i in 1:F.num + z = ZZRingElem() + ccall((:fmpz_factor_get_fmpz, libflint), Nothing, + (Ref{ZZRingElem}, Ref{Nemo.fmpz_factor}, Int), z, F, i - 1) + res[z] = unsafe_load(F.exp, i) + end + return res, canonical_unit(N) end const big_primes = ZZRingElem[] @@ -404,7 +369,7 @@ function factor(N::ZZRingElem) return Nemo.Fac(c, r) end -function factor_insert!(r::Dict{ZZRingElem, Int}, N::ZZRingElem, scale::Int = 1) +function factor_insert!(r::Dict{ZZRingElem,Int}, N::ZZRingElem, scale::Int=1) #assumes N to be positive # no small divisors # no big_primes @@ -424,9 +389,9 @@ function factor_insert!(r::Dict{ZZRingElem, Int}, N::ZZRingElem, scale::Int = 1) s = Nemo.factor(N) #MPQS for (p, k) in s if haskey(r, p) - r[p] += k*scale + r[p] += k * scale else - r[p] = k*scale + r[p] = k * scale end end return r @@ -437,16 +402,16 @@ function factor_insert!(r::Dict{ZZRingElem, Int}, N::ZZRingElem, scale::Int = 1) s = Nemo.factor(N) for (p, k) in s if haskey(r, p) - r[p] += k*scale + r[p] += k * scale else - r[p] = k*scale + r[p] = k * scale end end return r end cp = coprime_base([N, f]) for i = cp - factor_insert!(r, i, scale*valuation(N, i)) + factor_insert!(r, i, scale * valuation(N, i)) end return r end @@ -463,7 +428,7 @@ end # need ecm to find small factors # then recurse... -function _factors_trial_division(n::ZZRingElem, np::Int = 10^5) +function _factors_trial_division(n::ZZRingElem, np::Int=10^5) res, u = factor_trial_range(n, 0, np) factors = ZZRingElem[] for (p, v) in res @@ -474,12 +439,6 @@ function _factors_trial_division(n::ZZRingElem, np::Int = 10^5) end -function rand!(A::Vector{ZZRingElem}, v::StepRange{ZZRingElem, ZZRingElem}) - for i in 1:length(A) - A[i] = rand(v) - end - return A -end function (::Type{Base.Rational{BigInt}})(x::QQFieldElem) return Rational{BigInt}(BigInt(numerator(x)), BigInt(denominator(x))) @@ -508,7 +467,7 @@ mutable struct Divisors{T} s#::Iterator f::Function U::GrpAbFinGen - function Divisors(a::T; units::Bool = false, power::Int = 1) where {T} + function Divisors(a::T; units::Bool=false, power::Int=1) where {T} r = new{T}() r.n = a r.lf = MSet{T}() @@ -524,12 +483,12 @@ mutable struct Divisors{T} U, mU = unit_group(parent(a)) r.U = U g = r.f - r.f = x->mU(x[1]) * g(x[2]) + r.f = x -> mU(x[1]) * g(x[2]) r.s = Base.Iterators.ProductIterator((U, r.s)) end - return r + return r end - function Divisors(a::NfOrdIdl; units::Bool = false, power::Int = 1) + function Divisors(a::NfOrdIdl; units::Bool=false, power::Int=1) r = new{NfOrdIdl}() r.n = a r.lf = MSet{NfOrdIdl}() @@ -543,7 +502,7 @@ mutable struct Divisors{T} r.s = subsets(r.lf) return r end - function Divisors(a::FacElem{NfOrdIdl}; units::Bool = false, power::Int = 1) + function Divisors(a::FacElem{NfOrdIdl}; units::Bool=false, power::Int=1) r = new{NfOrdIdl}() r.n = evaluate(a) r.lf = MSet{NfOrdIdl}() @@ -558,7 +517,7 @@ mutable struct Divisors{T} return r end - function Divisors(a::FacElem{ZZRingElem, ZZRing}; units::Bool = false, power::Int = 1) + function Divisors(a::FacElem{ZZRingElem,ZZRing}; units::Bool=false, power::Int=1) r = new{ZZRingElem}() r.n = evaluate(a) r.lf = MSet{ZZRingElem}() @@ -574,18 +533,18 @@ mutable struct Divisors{T} U, mU = unit_group(parent(a)) r.U = U g = r.f - r.f = x->mU(x[1]) * g(x[2]) + r.f = x -> mU(x[1]) * g(x[2]) r.s = Base.Iterators.ProductIterator((U, r.s)) end - return r + return r end - function Divisors(a::Fac{ZZRingElem}; units::Bool = false, power::Int = 1) - return Divisors(FacElem(a), units = units, power = power) + function Divisors(a::Fac{ZZRingElem}; units::Bool=false, power::Int=1) + return Divisors(FacElem(a), units=units, power=power) end end Base.IteratorSize(::Divisors) = Base.HasLength() Base.length(D::Divisors) = length(D.s) -Base.eltype(::Divisors{T}) where T = T +Base.eltype(::Divisors{T}) where {T} = T function Base.iterate(D::Divisors) x = iterate(D.s) @@ -618,10 +577,10 @@ The unit group of $\mathbb{Z}$, i.e. $C_2$ and the map translating between the g """ function unit_group(::ZZRing) G = abelian_group([2]) - exp = function(z::GrpAbFinGenElem) + exp = function (z::GrpAbFinGenElem) return isodd(z[1]) ? ZZRingElem(-1) : ZZRingElem(1) end - log = function(z::ZZRingElem) + log = function (z::ZZRingElem) return z == -1 ? G[1] : G[0] end return G, MapFromFunc(exp, log, G, FlintZZ) @@ -634,10 +593,10 @@ The unit group of , i.e. $C_2$ and the map translating between the group and $\m """ function unit_group(R::AbstractAlgebra.Integers{T}) where {T} G = abelian_group([2]) - exp = function(z::GrpAbFinGenElem) + exp = function (z::GrpAbFinGenElem) return isodd(z[1]) ? T(-1) : T(1) end - log = function(z::T) + log = function (z::T) return z == -1 ? G[1] : G[0] end return G, MapFromFunc(exp, log, G, R) @@ -658,18 +617,18 @@ holds. The elements are returned in factored form. function euler_phi_inv_fac_elem(n::ZZRingElem) lp = ZZRingElem[] for d = Divisors(n) - if is_prime(d+1) - push!(lp, d+1) + if is_prime(d + 1) + push!(lp, d + 1) end end -# println("possible primes: ", lp) + # println("possible primes: ", lp) - E = Tuple{ZZRingElem, Vector{Tuple{ZZRingElem, Int}}}[] - res = FacElem{ZZRingElem, ZZRing}[] + E = Tuple{ZZRingElem,Vector{Tuple{ZZRingElem,Int}}}[] + res = FacElem{ZZRingElem,ZZRing}[] for p = lp v = valuation(n, p) - for i=0:v - push!(E, ((p-1)*p^i, [(p, i+1)])) + for i = 0:v + push!(E, ((p - 1) * p^i, [(p, i + 1)])) if E[end][1] == n push!(res, FacElem(Dict(E[end][2]))) end @@ -685,10 +644,10 @@ function euler_phi_inv_fac_elem(n::ZZRingElem) if p <= pm continue end - if nn % (p-1) == 0 + if nn % (p - 1) == 0 v = valuation(nn, p) for i = 0:v - push!(F, (e[1]*(p-1)*p^i, vcat(e[2], [(p, i+1)]))) + push!(F, (e[1] * (p - 1) * p^i, vcat(e[2], [(p, i + 1)]))) if F[end][1] == n push!(res, FacElem(Dict(F[end][2]))) end @@ -707,36 +666,36 @@ end #whowever, prod(p/(p-1) for p = PrimesSet(1, 1000000)) < 25 #so for 32-bit input, the output is also small function euler_phi_inv(n::Int) - @assert n>0 + @assert n > 0 T = Int if nbits(n) > 55 #to be safe... return T[T(x) for x = euler_phi_inv(ZZ(n))] end lp = T[] for d = Divisors(n) - if is_prime(d+1) - push!(lp, d+1) + if is_prime(d + 1) + push!(lp, d + 1) end end -# println("possible primes: ", lp) + # println("possible primes: ", lp) - E = Tuple{T, T, T}[] + E = Tuple{T,T,T}[] res = T[] for p = lp v = valuation(n, p) - push!(E, (p-1, p, p)) - if n == p-1 + push!(E, (p - 1, p, p)) + if n == p - 1 push!(res, p) end - for i=1:v - push!(E, (p*E[end][1], p*E[end][2], p)) + for i = 1:v + push!(E, (p * E[end][1], p * E[end][2], p)) if E[end][1] == n push!(res, prod(E[end][2])) end end end while true - F = Tuple{T, T, T}[] + F = Tuple{T,T,T}[] for e = E nn = divexact(n, e[1]) pm = e[3] @@ -744,14 +703,14 @@ function euler_phi_inv(n::Int) if p <= pm continue end - if nn % (p-1) == 0 + if nn % (p - 1) == 0 v = valuation(nn, p) - push!(F, (e[1]*(p-1), e[2]*p, p)) + push!(F, (e[1] * (p - 1), e[2] * p, p)) if F[end][1] == n push!(res, F[end][2]) end for i = 1:v - push!(F, (p*F[end][1], p*F[end][2], p)) + push!(F, (p * F[end][1], p * F[end][2], p)) if F[end][1] == n push!(res, F[end][2]) end @@ -767,13 +726,11 @@ function euler_phi_inv(n::Int) end -function euler_phi(x::Fac{ZZRingElem}) - return prod((p-1)*p^(v-1) for (p,v) = x.fac) -end -function euler_phi(x::FacElem{ZZRingElem, ZZRing}) + +function euler_phi(x::FacElem{ZZRingElem,ZZRing}) x = factor(x) - return prod((p-1)*p^(v-1) for (p,v) = x.fac) + return prod((p - 1) * p^(v - 1) for (p, v) = x.fac) end function carmichael_lambda(x::Fac{ZZRingElem}) @@ -785,7 +742,7 @@ function carmichael_lambda(x::Fac{ZZRingElem}) if v == 2 c = two elseif v > 2 - c = two^(v-2) + c = two^(v - 2) else c = ZZRingElem(1) end @@ -796,7 +753,7 @@ function carmichael_lambda(x::Fac{ZZRingElem}) if length(y) == 0 return c end - return lcm(c, reduce(lcm, (p-1)*p^(v-1) for (p,v) = y)) + return lcm(c, reduce(lcm, (p - 1) * p^(v - 1) for (p, v) = y)) end function carmichael_lambda(x::ZZRingElem) @@ -805,22 +762,22 @@ function carmichael_lambda(x::ZZRingElem) c = ZZRingElem(1) else x = factor(x) - c = reduce(lcm, (p-1)*p^(v-1) for (p,v) = x.fac) + c = reduce(lcm, (p - 1) * p^(v - 1) for (p, v) = x.fac) end if v == 2 c = lcm(2, c) elseif v > 2 - c = lcm(ZZRingElem(2)^(v-2), c) + c = lcm(ZZRingElem(2)^(v - 2), c) end return c end -function carmichael_lambda(x::FacElem{ZZRingElem, ZZRing}) +function carmichael_lambda(x::FacElem{ZZRingElem,ZZRing}) x = factor(x) return carmichael_lambda(x) end -function carmichael_lambda(n::T) where {T <: Integer} +function carmichael_lambda(n::T) where {T<:Integer} return T(carmichael_lambda(ZZRingElem(n))) end @@ -841,18 +798,18 @@ The inverse of the Euler totient functions: find all $x$ s.th. $phi(x) = n$ holds. """ function euler_phi_inv(n::ZZRingElem) - return [ evaluate(x) for x = euler_phi_inv_fac_elem(n)] + return [evaluate(x) for x = euler_phi_inv_fac_elem(n)] end -function factor(a::FacElem{ZZRingElem, ZZRing}) +function factor(a::FacElem{ZZRingElem,ZZRing}) b = simplify(a) - c = Dict{ZZRingElem, Int}() + c = Dict{ZZRingElem,Int}() s = ZZRingElem(1) - for (p,k) = b.fac + for (p, k) = b.fac lp = factor(p) s *= lp.unit - for (q,w) = lp.fac - c[q] = w*k + for (q, w) = lp.fac + c[q] = w * k end end l = Fac{ZZRingElem}() @@ -876,186 +833,26 @@ end =# radical(a::ZZRingElem) = prod(keys(factor(a).fac)) -function radical(a::T) where {T <: Integer} +function radical(a::T) where {T<:Integer} return T(radical(ZZRingElem(a))) end function quo(::ZZRing, a::ZZRingElem) R = residue_ring(FlintZZ, a) - f = MapFromFunc(x -> R(x), y->lift(y), FlintZZ, R) + f = MapFromFunc(x -> R(x), y -> lift(y), FlintZZ, R) return R, f end function quo(::ZZRing, a::Integer) R = residue_ring(FlintZZ, a) - f = MapFromFunc(x -> R(x), y->lift(y), FlintZZ, R) + f = MapFromFunc(x -> R(x), y -> lift(y), FlintZZ, R) return R, f end -module BitsMod - -using Hecke -import Nemo -import Base: ^, show, getindex, iterate, length -import Hecke.bits -export bits, Limbs - - -const hb = UInt(1)<<63 - -#= not used - lacks length -struct BitsUInt - a::UInt -end - -function bits(a::UInt) - l = nbits(a) - return BitsUInt(a<<(sizeof(a)*8-l)) -end - - -function Base.iterate(x::BitsUInt) - return iterate(x, x.a) -end - -@inline function Base.iterate(x::BitsUInt, u::UInt) - iszero(u) && return nothing - return (u&hb) != 0, u<<1 -end -=# - - -struct Limbs - a::ZZRingElem - len::Int - b::Ptr{UInt} - function Limbs(a::ZZRingElem; MSW::Bool = true) - if Nemo._fmpz_is_small(a) - return new(a, 0, convert(Ptr{UInt}, 0)) - end - z = convert(Ptr{Cint}, unsigned(a.d)<<2) - len = unsafe_load(z, 2) - d = convert(Ptr{Ptr{UInt}}, unsigned(a.d) << 2) + 2*sizeof(Cint) - p = unsafe_load(d) - if !MSW - new(a, -len, p) - else - new(a, len, p) - end - end -end - -function show(io::IO, L::Limbs) - print(io, "limb-access for: ", L.a) -end - -@inline function getindex(L::Limbs, i::Int) - if L.len == 0 - return UInt(abs(L.a.d)) #error??? - end - @boundscheck @assert i <= abs(L.len) - return unsafe_load(L.b, i) -end - -function iterate(L::Limbs) - L.len < 0 && return L[1], 1 - - return L[L.len], L.len -end - -function iterate(L::Limbs, i::Int) - if L.len < 0 - i > -L.len && return nothing - return L[i+1], i+1 - end - i == 0 && return nothing - return L[i-1], i-1 -end - -length(L::Limbs) = L.len+1 - -#= -#from https://github.com/JuliaLang/julia/issues/11592 -#compiles directly down to the ror/rol in assembly -for T in Base.BitInteger_types - mask = UInt8(sizeof(T) << 3 - 1) - @eval begin - ror(x::$T, k::Integer) = (x >>> ($mask & k)) | (x << ($mask & -k)) - rol(x::$T, k::Integer) = (x << ($mask & k)) | (x >>> ($mask & -k)) - end -end -=# - -struct BitsFmpz - L::Limbs - - function BitsFmpz(b::ZZRingElem) - return new(Limbs(b)) - end -end - -function iterate(B::BitsFmpz) - L = B.L - a = L[L.len] - b = UInt(1) << (nbits(a)-1) - return true, (b, L.len) -end - -@inline function iterate(B::BitsFmpz, s::Tuple{UInt, Int}) - b = s[1] >> 1 - if b == 0 - l = s[2] - 1 - if l < 1 - return nothing - end - b = hb - a = B.L[l] - return a&b != 0, (b, l) - end - return B.L[s[2]]&b != 0, (b, s[2]) -end - -function show(io::IO, B::BitsFmpz) - print(io, "bit iterator for:", B.L.a) -end - -length(B::BitsFmpz) = nbits(B.L.a) - -bits(a::ZZRingElem) = BitsFmpz(a) -#= wrong order, thus disabled - -function getindex(B::BitsFmpz, i::Int) - return ccall((:fmpz_tstbit, libflint), Int, (Ref{ZZRingElem}, Int), B.L.a, i) != 0 -end -=# - -end - -using .BitsMod -export bits, Limbs - -^(a::T, n::IntegerUnion) where {T <: RingElem} = _generic_power(a, n) - -^(a::NfAbsOrdIdl, n::IntegerUnion) = _generic_power(a, n) - -#^(a::NfRelOrdIdl, n::IntegerUnion) = _generic_power(a, n) +^(a::NfAbsOrdIdl, n::IntegerUnion) = Nemo._generic_power(a, n) -function _generic_power(a, n::IntegerUnion) - fits(Int, n) && return a^Int(n) - if is_negative(n) - a = inv(a) - n = -n - end - r = one(parent(a)) - for b = bits(n) - r = mul!(r, r, r) - if b - r = mul!(r, r, a) - end - end - return r -end +#^(a::NfRelOrdIdl, n::IntegerUnion) = Nemo._generic_power(a, n) ################################################################################ @@ -1082,8 +879,8 @@ function primes_up_to(n::Int) b = sqrt(n) while i <= b if list[i] - j = 3*i - s = 2*i + j = 3 * i + s = 2 * i while j <= n list[j] = false j += s @@ -1105,7 +902,7 @@ end Returns a vector containing all the squarefree numbers up to $n$. """ -function squarefree_up_to(n::Int; coprime_to::Vector{ZZRingElem} = ZZRingElem[], prime_base::Vector{ZZRingElem} = ZZRingElem[]) +function squarefree_up_to(n::Int; coprime_to::Vector{ZZRingElem}=ZZRingElem[], prime_base::Vector{ZZRingElem}=ZZRingElem[]) @assert isempty(coprime_to) || isempty(prime_base) if !isempty(prime_base) diff --git a/src/Misc/Localization.jl b/src/Misc/Localization.jl deleted file mode 100644 index 74c06f5291..0000000000 --- a/src/Misc/Localization.jl +++ /dev/null @@ -1,27 +0,0 @@ -############################################################################### -# -# Random Functions -# -############################################################################### - -# mainly for testing -function rand(L::Loc{T}, num_scale = (1:1000), den_scale=(1:1000)) where {T <: ZZRingElem} - num = rand(num_scale) - den = rand(den_scale) - while gcd(den,prime(L)) != 1 - den = rand(den_scale) - end - return L(num//den) -end - -function rand(L::Loc{T}, num_scale::Vector, den_scale::Integer) where {T <: ZZRingElem} - num = rand(num_scale) - den = rand(den_scale) - while gcd(den,prime(L)) != 1 - den = rand(den_scale) - end - return L(num//den) -end - -Nemo.promote_rule(::Type{LocElem{T}}, ::Type{T}) where {T} = LocElem{T} - diff --git a/src/Misc/MPoly.jl b/src/Misc/MPoly.jl deleted file mode 100644 index dfb67818b9..0000000000 --- a/src/Misc/MPoly.jl +++ /dev/null @@ -1,132 +0,0 @@ -############################################################################### -# other stuff, trivia and non-trivia -############################################################################### - -#TODO: only makes sense if f is univ (uses only one var) -function (Rx::ZZPolyRing)(f::QQMPolyRingElem) - fp = Rx() - R = base_ring(Rx) - d = denominator(f) - @assert d == 1 - for t = terms(f) - e = total_degree(t) - @assert length(t) == 1 - c = coeff(t, 1) - setcoeff!(fp, e, numerator(c*d)) - end - return fp -end - -function (Rx::QQPolyRing)(f::QQMPolyRingElem) - fp = Rx() - R = base_ring(Rx) - for t = terms(f) - e = total_degree(t) - @assert length(t) == 1 - c = coeff(t, 1) - setcoeff!(fp, e, c) - end - return fp -end - -function (Rx::fpPolyRing)(f::QQMPolyRingElem) - fp = Rx() - R = base_ring(Rx) - d = denominator(f) - for t = terms(f) - e = total_degree(t) - @assert length(t) == 1 - c = coeff(t, 1) - setcoeff!(fp, e, R(numerator(c*d))) - end - return fp * inv(R(d)) -end - -function mul!(res::QQMPolyRingElem, a::QQMPolyRingElem, c::ZZRingElem) - ccall((:fmpq_mpoly_scalar_mul_fmpz, libflint), Nothing, - (Ref{QQMPolyRingElem}, Ref{QQMPolyRingElem}, Ref{ZZRingElem}, Ref{QQMPolyRing}), res, a, c, parent(a)) - return nothing -end - -#@doc raw""" -# is_univariate(f::Generic.MPoly{T}) where T <: NumFieldElem -> Bool, PolyElem{T} -# -#Tests if $f$ involves only one variable. If so, return a corresponding univariate polynomial. -#""" -#function is_univariate(f::Generic.MPoly{T}) where T -# kx, x = polynomial_ring(base_ring(f), "x", cached = false) -# if ngens(parent(f)) == 1 -# f1 = kx() -# for i = 1:f.length -# setcoeff!(f1, Int(f.exps[1, i]), f.coeffs[i]) -# end -# return true, f1 -# end -# if f.length == 0 -# @assert iszero(f) -# return true, kx(0) -# end -# n = ngens(parent(f)) -# i = 1 -# while i <= n && iszero(f.exps[i, :]) -# i += 1 -# end -# j = n -# while j >= 1 && iszero(f.exps[j, :]) -# j -= 1 -# end -# if i != j -# return false, x -# end -# f1 = kx() -# for j = 1:f.length -# setcoeff!(f1, Int(f.exps[i, j]), f.coeffs[j]) -# end -# return true, f1 -#end - -function (R::ZZMPolyRing)(f::QQMPolyRingElem) - return map_coefficients(ZZ, f, parent = R) -end -Hecke.ngens(R::ZZMPolyRing) = length(gens(R)) - -#check with Nemo/ Dan if there are better solutions -#the block is also not used here I think -#functionality to view mpoly as upoly in variable `i`, so the -#coefficients are mpoly's without variable `i`. -function Hecke.leading_coefficient(f::MPolyRingElem, i::Int) - g = MPolyBuildCtx(parent(f)) - d = degree(f, i) - for (c, e) = zip(coefficients(f), exponent_vectors(f)) - if e[i] == d - e[i] = 0 - push_term!(g, c, e) - end - end - return finish(g) -end - -#not used here -""" -`content` as a polynomial in the variable `i`, i.e. the gcd of all the -coefficients when viewed as univariate polynomial in `i`. -""" -function Hecke.content(f::MPolyRingElem, i::Int) - return reduce(gcd, coefficients(f, i)) -end - -""" -The coefficients of `f` when viewed as a univariate polynomial in the `i`-th -variable. -""" -function Hecke.coefficients(f::MPolyRingElem, i::Int) - d = degree(f, i) - cf = [MPolyBuildCtx(parent(f)) for j=0:d] - for (c, e) = zip(coefficients(f), exponent_vectors(f)) - a = e[i] - e[i] = 0 - push_term!(cf[a+1], c, e) - end - return map(finish, cf) -end - diff --git a/src/Misc/Matrix.jl b/src/Misc/Matrix.jl index d9cdb0bc97..23e5cf67c1 100644 --- a/src/Misc/Matrix.jl +++ b/src/Misc/Matrix.jl @@ -89,11 +89,6 @@ function _hnf!(x::T, shape::Symbol = :upperright) where {T <: MatElem} return x::T end -function hnf!(x::ZZMatrix) - ccall((:fmpz_mat_hnf, libflint), Nothing, (Ref{ZZMatrix}, Ref{ZZMatrix}), x, x) - return x -end - function _hnf_modular_eldiv(x::ZZMatrix, m::ZZRingElem, shape::Symbol = :upperright) if shape == :lowerleft h = hnf_modular_eldiv!(reverse_cols(x), m) @@ -432,7 +427,70 @@ function is_positive_definite(a::ZZMatrix) return true end +#scales the i-th column of a by 2^d[1,i] +function mult_by_2pow_diag!(a::Matrix{BigFloat}, d::ZZMatrix, R=_RealRings[Threads.threadid()]) + s = size(a) + tmp_mpz::BigInt = R.z1 + for i = 1:s[1] + for j = 1:s[2] + e = ccall((:mpfr_get_z_2exp, :libmpfr), Clong, (Ref{BigInt}, Ref{BigFloat}), tmp_mpz, a[i, j]) + ccall((:mpfr_set_z_2exp, :libmpfr), Nothing, (Ref{BigFloat}, Ref{BigInt}, Clong, Int32), a[i, j], tmp_mpz, e + Clong(Int(d[1, j])), __get_rounding_mode()) + end + end +end + +#converts BigFloat -> ZZRingElem via round(a*2^l), in a clever(?) way +function round_scale(a::Matrix{BigFloat}, l::Int) + s = size(a) + b = zero_matrix(FlintZZ, s[1], s[2]) + return round_scale!(b, a, l) +end + +function round_scale!(b::ZZMatrix, a::Matrix{BigFloat}, l::Int, R=_RealRings[Threads.threadid()]) + s = size(a) + + local tmp_mpz::BigInt, tmp_fmpz::ZZRingElem + tmp_mpz = R.z1 + tmp_fmpz = R.zz1 + tmp_mpfr = deepcopy(a[1, 1]) #cannot use the R.?? tmp variable as it may/will + #have the wrong precision + + rd = __get_rounding_mode() + for i = 1:s[1] + for j = 1:s[2] + e = a[i, j].exp + a[i, j].exp += l + ccall((:mpfr_round, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}, Int32), tmp_mpfr, a[i, j], rd) + a[i, j].exp = e + f = ccall((:mpfr_get_z_2exp, :libmpfr), Clong, (Ref{BigInt}, Ref{BigFloat}), + tmp_mpz, tmp_mpfr) + ccall((:fmpz_set_mpz, libflint), Nothing, (Ref{ZZRingElem}, Ref{BigInt}), tmp_fmpz, tmp_mpz) + if f > 0 + ccall((:fmpz_mul_2exp, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, UInt), tmp_fmpz, tmp_fmpz, f) + else + ccall((:fmpz_tdiv_q_2exp, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, UInt), tmp_fmpz, tmp_fmpz, -f) + end + setindex!(b, tmp_fmpz, i, j) + end + end + return b +end + +function round_scale!(b::ZZMatrix, a::arb_mat, l::Int) + s = size(a) + R = base_ring(a) + r = R() + for i = 1:s[1] + for j = 1:s[2] + v = ccall((:arb_mat_entry_ptr, libarb), Ptr{arb}, + (Ref{arb_mat}, Int, Int), a, i - 1, j - 1) + ccall((:arb_mul_2exp_si, libarb), Nothing, (Ref{arb}, Ptr{arb}, Int), r, v, l) + b[i, j] = round(ZZRingElem, r) + end + end + return b +end ################################################################################ # @@ -1244,8 +1302,6 @@ function left_kernel_prime_power(A::zzModMatrix, p::Int, l::Int) return i - 1, Khow end - - function invmod(M::ZZMatrix, d::ZZRingElem) if fits(Int, d) RR = residue_ring(FlintZZ, Int(d), cached = false) diff --git a/src/Misc/Primes.jl b/src/Misc/Primes.jl index e4ba11f611..85949b008b 100644 --- a/src/Misc/Primes.jl +++ b/src/Misc/Primes.jl @@ -1,24 +1,5 @@ export PrimesSet -################################################################################ -# -# Missing is_prime functionality -# -################################################################################ - -# Fallback -function is_prime(x::Integer) - return is_prime(ZZRingElem(x)) -end - -function next_prime(x::BigInt, proved::Bool = true) - return BigInt(next_prime(ZZRingElem(x), proved)) -end - -function next_prime(x::T, proved::Bool = true) where {T <: Integer} - return T(next_prime(BigInt(x), proved)) -end - ################################################################################ # # Primes iterator diff --git a/src/Misc/PseudoPolynomial.jl b/src/Misc/PseudoPolynomial.jl index fdd20e7389..bc786f7afa 100644 --- a/src/Misc/PseudoPolynomial.jl +++ b/src/Misc/PseudoPolynomial.jl @@ -287,19 +287,5 @@ function divides(a::NfAbsOrdElem, b::NfAbsOrdElem) end end -function leading_monomial(f::Generic.MPoly) - R = parent(f) - l = length(f) - if l == 0 - return f - end - A = f.exps - r, c = size(A) - e = A[1:r, 1:1] - return R([one(base_ring(R))], e) -end -function leading_coefficient(f::Generic.MPoly) - return f.coeffs[1] -end diff --git a/src/Misc/Residue.jl b/src/Misc/Residue.jl deleted file mode 100644 index 7f503363f9..0000000000 --- a/src/Misc/Residue.jl +++ /dev/null @@ -1,37 +0,0 @@ -function divexact(a::ZZModRingElem, y::ZZRingElem; check::Bool = true) - return divexact(a, parent(a)(y), check = check) -end - -function lift(a::Generic.ResidueRingElem) - return a.data -end - -function lift(a::Generic.ResidueFieldElem) - return a.data -end - -function ^(a::ResElem, f::ZZRingElem) - f==0 && return one(parent(a)) - f==1 && return a - if f<0 - f=-f - a = inv(a) - end - if f<(1<<30) - return a^Int(f) - end - b = a^(div(f, 2)) - b = b^2 - if isodd(f) - b *= a - end - return b -end - -function set!(z::fqPolyRepFieldElem, x::fqPolyRepFieldElem) - ccall((:fq_nmod_set, libflint), Nothing, - (Ref{fqPolyRepFieldElem}, Ref{fqPolyRepFieldElem}, Ref{fqPolyRepField}), - z, x, parent(z)) -end - -characteristic(F::Generic.ResidueField{ZZRingElem}) = abs(F.modulus) diff --git a/src/Misc/ResidueRingPoly.jl b/src/Misc/ResidueRingPoly.jl deleted file mode 100644 index 2413e90d0b..0000000000 --- a/src/Misc/ResidueRingPoly.jl +++ /dev/null @@ -1,192 +0,0 @@ -import Nemo.characteristic, Nemo.gen, Nemo.size -export gen, characteristic, size, elem_to_mat_row!, rand - -function gen(R::Union{Generic.ResidueRing{T},Generic.ResidueField{T}}) where T<:PolyElem - return R(gen(base_ring(R))) -end - -function gen(R::Union{Generic.ResidueRing{fqPolyRepPolyRingElem},Generic.ResidueField{fqPolyRepPolyRingElem}}) ## this is not covered by above - return R(gen(base_ring(R))) ## and I don't know why -end - -function gen(R::Union{Generic.ResidueRing{zzModPolyRingElem},Generic.ResidueField{zzModPolyRingElem}}) - return R(gen(base_ring(R))) -end - -function characteristic(R::Union{Generic.ResidueRing{Nemo.ZZRingElem},Generic.ResidueField{Nemo.ZZRingElem}}) - return modulus(R) -end - -function characteristic(R::Union{Generic.ResidueRing{zzModPolyRingElem},Generic.ResidueField{zzModPolyRingElem}}) - return characteristic(base_ring(base_ring(R))) -end - -function characteristic(R::Union{Generic.ResidueRing{T},Generic.ResidueField{T}}) where T<:PolyElem - return characteristic(base_ring(base_ring(R))) -end - -# discuss: size = order? order = size? -function size(R::Nemo.Union{Generic.ResidueRing{Nemo.zzModPolyRingElem},Generic.ResidueField{Nemo.zzModPolyRingElem}}) - return characteristic(R)^degree(modulus(R)) -end - -function size(R::Nemo.Union{Generic.ResidueRing{T},Generic.ResidueField{T}}) where T <: ResElem - return size(base_ring(base_ring(R)))^degree(modulus(R)) -end - -function size(R::Nemo.Union{Generic.ResidueRing{ZZRingElem},Generic.ResidueField{ZZRingElem}}) - return modulus(R) -end - -function size(R::Nemo.Union{Generic.ResidueRing{T},Generic.ResidueField{T}}) where T<:PolyElem - return size(base_ring(base_ring(R)))^degree(R.modulus) -end - -function size(R::Nemo.Union{Generic.ResidueRing{fqPolyRepPolyRingElem},Generic.ResidueField{fqPolyRepPolyRingElem}}) - return size(base_ring(base_ring(R)))^degree(R.modulus) -end - -function size(R::FqPolyRepField) - return order(R) -end - -function size(R::fqPolyRepField) - return order(R) -end - -function size(F::fpField) - return order(F) -end - -function size(F::Nemo.FpField) - return order(F) -end - -function order(R::Nemo.zzModRing) - return ZZRingElem(R.n) -end - -################################################# -# in triplicate.... and probably cases missing... -function elem_to_mat_row!(M::MatElem, i::Int, a::ResElem{T}) where T <: PolyElem - z = zero(parent(M[1,1])) - for j=0:degree(a.data) - M[i,j+1] = coeff(a.data, j) - end - for j=degree(a.data)+2:ncols(M) - M[i,j] = z - end -end -function elem_to_mat_row!(M::MatElem, i::Int, a::ResElem{FqPolyRepPolyRingElem}) - z = zero(parent(M[1,1])) - for j=0:degree(a.data) - M[i,j+1] = coeff(a.data, j) - end - for j=degree(a.data)+2:ncols(M) - M[i,j] = z - end -end -function elem_to_mat_row!(M::MatElem, i::Int, a::ResElem{fqPolyRepPolyRingElem}) - z = zero(parent(M[1,1])) - for j=0:degree(a.data) - M[i,j+1] = coeff(a.data, j) - end - for j=degree(a.data)+2:ncols(M) - M[i,j] = z - end -end - -function rand(R::Union{Generic.ResidueRing{ZZRingElem},Generic.ResidueField{ZZRingElem}}) - return R(rand(ZZRingElem(0):(size(R)-1))) -end - -function rand(R::Generic.ResidueField{ZZRingElem}) - return R(rand(ZZRingElem(0):(order(R)-1))) -end - -function rand(R::Union{Generic.ResidueRing{T},Generic.ResidueField{T}}) where T<:PolyElem - r = rand(base_ring(base_ring(R))) - g = gen(R) - for i=1:degree(R.modulus) - r = r*g + rand(base_ring(base_ring(R))) - end - return r -end - -function rand(R::Union{Generic.ResidueRing{fqPolyRepPolyRingElem},Generic.ResidueField{fqPolyRepPolyRingElem}}) - r = rand(base_ring(base_ring(R))) - g = gen(R) - for i=1:degree(R.modulus) - r = r*g + rand(base_ring(base_ring(R))) - end - return r -end - -function rand(R::Union{Generic.ResidueRing{FqPolyRepPolyRingElem},Generic.ResidueField{FqPolyRepPolyRingElem}}) - r = rand(base_ring(base_ring(R))) - g = gen(R) - for i=1:degree(R.modulus) - r = r*g + rand(base_ring(base_ring(R))) - end - return r -end - -function rand(R::Union{Generic.ResidueRing{zzModPolyRingElem},Generic.ResidueField{zzModPolyRingElem}}) - r = rand(base_ring(base_ring(R))) - g = gen(R) - for i=1:degree(R.modulus) - r = r*g + rand(base_ring(base_ring(R))) - end - return r -end - - -####################################################### -## -## -## -####################################################### - -function gens(R::Union{Generic.ResidueRing{T},Generic.ResidueField{T}}) where T<:PolyElem ## probably needs more cases - ## as the other residue functions - g = gen(R) - r = Vector{typeof(g)}() - push!(r, one(R)) - if degree(R.modulus)==1 - return r - end - push!(r, g) - for i=2:degree(R.modulus)-1 - push!(r, r[end]*g) - end - return r -end - -function gens(R::Union{Generic.ResidueRing{zzModPolyRingElem},Generic.ResidueField{zzModPolyRingElem}}) - g = gen(R) - r = Vector{typeof(g)}() - push!(r, one(R)) - if degree(R.modulus)==1 - return r - end - push!(r, g) - for i=2:degree(R.modulus)-1 - push!(r, r[end]*g) - end - return r -end - -function rem!(f::Nemo.zzModPolyRingElem, g::Nemo.zzModPolyRingElem, h::Nemo.zzModPolyRingElem) - ccall((:nmod_poly_rem, libflint), Nothing, (Ref{Nemo.zzModPolyRingElem}, Ref{Nemo.zzModPolyRingElem}, Ref{Nemo.zzModPolyRingElem}), f, g, h) - return f -end - -function gcd!(f::Nemo.zzModPolyRingElem, g::Nemo.zzModPolyRingElem, h::Nemo.zzModPolyRingElem) - ccall((:nmod_poly_gcd, libflint), Nothing, (Ref{Nemo.zzModPolyRingElem}, Ref{Nemo.zzModPolyRingElem}, Ref{Nemo.zzModPolyRingElem}), f, g, h) - return f -end - -function gcd!(f::Nemo.fpPolyRingElem, g::Nemo.fpPolyRingElem, h::Nemo.fpPolyRingElem) - ccall((:nmod_poly_gcd, libflint), Nothing, (Ref{Nemo.fpPolyRingElem}, Ref{Nemo.fpPolyRingElem}, Ref{Nemo.fpPolyRingElem}), f, g, h) - return f -end diff --git a/src/Misc/Series.jl b/src/Misc/Series.jl index 3d8fce65f6..ed35f3e874 100644 --- a/src/Misc/Series.jl +++ b/src/Misc/Series.jl @@ -1,6 +1,3 @@ -Nemo.fit!(::QQRelPowerSeriesRingElem, Int) = nothing -Nemo.fit!(::QQAbsPowerSeriesRingElem, Int) = nothing - @doc raw""" integral(f::RelPowerSeriesRingElem{T}) -> RelPowerSeriesRingElem @@ -55,15 +52,6 @@ function *(f::RelPowerSeriesRingElem{qadic}, g::RelPowerSeriesRingElem{qadic}) end =# -function Base.minimum(::typeof(precision), a::Vector{<:SeriesElem}) - return minimum(map(precision, a)) -end - -function Base.maximum(::typeof(precision), a::Vector{<:SeriesElem}) - return maximum(map(precision, a)) -end -Base.length(a::qadic) = a.length - @inline function coeffraw(q::qadic, i::Int) @assert i < length(q) @@ -75,19 +63,6 @@ end return reinterpret(Ptr{ZZRingElem}, q.coeffs)+i*sizeof(Ptr{Int}) end -@inline function Hecke.setcoeff!(z::ZZPolyRingElem, n::Int, x::Ptr{ZZRingElem}) - ccall((:fmpz_poly_set_coeff_fmpz, Hecke.libflint), Nothing, - (Ref{ZZPolyRingElem}, Int, Ptr{ZZRingElem}), z, n, x) - return z -end - -@inline function Hecke.mul!(a::Ref{ZZRingElem}, b::Ref{ZZRingElem}, c::ZZRingElem) - ccall((:fmpz_mul, Hecke.libflint), Cvoid, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}),a, b, c) -end -@inline function Hecke.iszero(a::Ref{ZZRingElem}) - return unsafe_load(reinterpret(Ptr{Int}, a))==0 -end - #= function mul!(C::Generic.RelSeries{qadic}, f::Generic.RelSeries{qadic}, g::Generic.RelSeries{qadic}) return f*g @@ -247,31 +222,6 @@ function mymul_ks(f::PolyElem{<:SeriesElem{qadic}}, g::PolyElem{<:SeriesElem{qad return fg end - -function Nemo.canonical_unit(a::SeriesElem) - iszero(a) && return one(parent(a)) - v = valuation(a) - v == 0 && return a - v > 0 && return shift_right(a, v) - return shift_left(a, -v) -end - -#TODO: this is for rings, not for fields, maybe different types? -function Base.gcd(a::T, b::T) where {T <: SeriesElem} - iszero(a) && iszero(b) && return a - iszero(a) && return gen(parent(a))^valuation(b) - iszero(b) && return gen(parent(a))^valuation(a) - return gen(parent(a))^min(valuation(a), valuation(b)) -end - -function Base.lcm(a::T, b::T) where {T <: SeriesElem} - iszero(a) && iszero(b) && return a - iszero(a) && return a - iszero(b) && return b - return gen(parent(a))^max(valuation(a), valuation(b)) -end - - function Hecke.residue_field(S::SeriesRing{T}) where {T <: Nemo.RingElem} #darn zzModRingElem/gfp k = base_ring(S) return k, MapFromFunc(x -> coeff(x, 0), y -> set_precision(S(y), 1), S, k) @@ -285,45 +235,6 @@ function set_precision(a::SeriesElem, i::Int) return b end -# should be Nemo/AA -# TODO: symbols vs strings -# lift(PolyRing, Series) -# lift(FracField, Series) -# (to be in line with lift(ZZ, padic) and lift(QQ, padic) -#TODO: some of this would only work for Abs, not Rel, however, this should be fine here -function Hecke.map_coefficients(f, a::RelPowerSeriesRingElem; parent::SeriesRing) - c = typeof(f(coeff(a, 0)))[] - for i=0:Nemo.pol_length(a)-1 - push!(c, f(Nemo.polcoeff(a, i))) - end - b = parent(c, length(c), precision(a), valuation(a)) - return b -end - -#= -function Hecke.map_coefficients(f, a::RelPowerSeriesRingElem) - d = f(coeff(a, 0)) - T = parent(a) - if parent(d) == base_ring(T) - S = T - else - S = power_series_ring(parent(d), max_precision(T), string(var(T)), cached = false)[1] - end - c = typeof(d)[d] - for i=1:Nemo.pol_length(a)-1 - push!(c, f(Nemo.polcoeff(a, i))) - end - b = S(c, length(c), precision(a), valuation(a)) - return b -end -=# -function lift(R::PolyRing{S}, s::SeriesElem{S}) where {S} - t = R() - for x = 0:pol_length(s) - setcoeff!(t, x, polcoeff(s, x)) - end - return shift_left(t, valuation(s)) -end function rational_reconstruction(a::SeriesElem; parent::PolyRing = polynomial_ring(base_ring(a), cached = false)[1]) C = base_ring(a) @@ -342,8 +253,7 @@ function rational_reconstruction(a::padic) end Hecke.gcd_into!(a::PolyElem, b::PolyElem, c::PolyElem) = gcd(b, c) -Base.copy(a::PolyElem) = deepcopy(a) -Base.copy(a::SeriesElem) = deepcopy(a) + function Hecke.squarefree_part(a::PolyElem) return divexact(a, gcd(a, derivative(a))) diff --git a/src/Misc/UnitsModM.jl b/src/Misc/UnitsModM.jl index 94d272cc24..ed029686e7 100644 --- a/src/Misc/UnitsModM.jl +++ b/src/Misc/UnitsModM.jl @@ -1,10 +1,6 @@ export UnitGroup, solvemod, gen_mod_pk, disc_log_bs_gs, disc_log_ph, disc_log_mod -function order(x::Generic.ResidueRingElem{ZZRingElem}, fp::Dict{ZZRingElem, Int64}) - error("missing") -end - @doc raw""" is_primitive_root(x::Generic.ResidueRingElem{ZZRingElem}, M::ZZRingElem, fM::Dict{ZZRingElem, Int64}) -> Bool @@ -673,58 +669,3 @@ end unit_group(A::Nemo.ZZModRing) = UnitGroup(A) unit_group(A::Nemo.zzModRing) = UnitGroup(A) - - -## Make zzModRing iteratible - -Base.iterate(R::zzModRing) = (zero(R), zero(UInt)) - -function Base.iterate(R::zzModRing, st::UInt) - if st == R.n - 1 - return nothing - end - - return R(st + 1), st + 1 -end - -Base.IteratorEltype(::Type{zzModRing}) = Base.HasEltype() -Base.eltype(::Type{zzModRing}) = zzModRingElem - -Base.IteratorSize(::Type{zzModRing}) = Base.HasLength() -Base.length(R::zzModRing) = R.n - -## Make ZZModRing iteratible - -Base.iterate(R::Nemo.ZZModRing) = (zero(R), zero(ZZRingElem)) - -function Base.iterate(R::Nemo.ZZModRing, st::ZZRingElem) - if st == R.n - 1 - return nothing - end - - return R(st + 1), st + 1 -end - -Base.IteratorEltype(::Type{Nemo.ZZModRing}) = Base.HasEltype() -Base.eltype(::Type{Nemo.ZZModRing}) = ZZModRingElem - -Base.IteratorSize(::Type{Nemo.ZZModRing}) = Base.HasLength() -Base.length(R::Nemo.ZZModRing) = Integer(R.n) - -## Make fpField iteratible - -Base.iterate(R::fpField) = (zero(R), zero(UInt)) - -function Base.iterate(R::fpField, st::UInt) - if st == R.n - 1 - return nothing - end - - return R(st + 1), st + 1 -end - -Base.IteratorEltype(::Type{fpField}) = Base.HasEltype() -Base.eltype(::Type{fpField}) = fpFieldElem - -Base.IteratorSize(::Type{fpField}) = Base.HasLength() -Base.length(R::fpField) = R.n diff --git a/src/Misc/UnsafeRational.jl b/src/Misc/UnsafeRational.jl index 68d4296f3a..dc3296cdda 100644 --- a/src/Misc/UnsafeRational.jl +++ b/src/Misc/UnsafeRational.jl @@ -34,9 +34,6 @@ end Rational(x::UnsafeRational) = x.num//x.den -function Base.Int128(x::ZZRingElem) - return Base.Int128(BigInt(x)) -end ################################################################################ # diff --git a/src/Misc/acb_root_ctx.jl b/src/Misc/acb_root_ctx.jl index a396a5af02..3082de77a3 100644 --- a/src/Misc/acb_root_ctx.jl +++ b/src/Misc/acb_root_ctx.jl @@ -265,18 +265,6 @@ function radiuslttwopower(x::acb, e::Int) return ok end -function round(x::arb, p::Int) - z = ArbField(p, cached = false)() - ccall((:arb_set_round, libarb), Nothing, (Ref{Nemo.arb}, Ref{Nemo.arb}, Int), z, x, p) - return z -end - -function round(x::acb, p::Int) - z = AcbField(p, cached = false)() - ccall((:acb_set_round, libarb), Nothing, (Ref{Nemo.acb}, Ref{Nemo.acb}, Int), z, x, p) - return z -end - function arb_trim(x::arb) z = arb() ccall((:arb_trim, libarb), Nothing, (Ref{Nemo.arb}, Ref{Nemo.arb}), z, x) @@ -284,22 +272,6 @@ function arb_trim(x::arb) return z end -function round!(z::arb, x::arb, p::Int) - ccall((:arb_set_round, libarb), Nothing, (Ref{Nemo.arb}, Ref{Nemo.arb}, Int), z, x, p) - z.parent = ArbField(p, cached = false) - return z -end - -function round!(z::acb, x::acb, p::Int) - ccall((:acb_set_round, libarb), Nothing, (Ref{Nemo.acb}, Ref{Nemo.acb}, Int), z, x, p) - z.parent = AcbField(p, cached = false) - return z -end - -function bits(x::arb) - return ccall((:arb_bits, libarb), Int, (Ref{Nemo.arb}, ), x) -end - function rel_error_bits(x::arb) return ccall((:arb_rel_error_bits, libarb), Int, (Ref{Nemo.arb}, ), x) end diff --git a/src/Misc/coprime.jl b/src/Misc/coprime.jl index 7b26ce6319..7c38567cf3 100644 --- a/src/Misc/coprime.jl +++ b/src/Misc/coprime.jl @@ -1,12 +1,6 @@ import Nemo.isone, Nemo.divexact, Base.copy export divexact!, gcd_into!, coprime_base, coprime_base_insert -function divexact!(a::ZZRingElem, b::ZZRingElem) - ccall((:fmpz_divexact, libflint), Nothing, - (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), a, a, b) - return a -end - function gcd_into!(a::ZZRingElem, b::ZZRingElem, c::ZZRingElem) ccall((:fmpz_gcd, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), a, b, c) diff --git a/src/Misc/nmod_poly.jl b/src/Misc/nmod_poly.jl index a48a58a292..8ea52d995c 100644 --- a/src/Misc/nmod_poly.jl +++ b/src/Misc/nmod_poly.jl @@ -292,18 +292,6 @@ function prs_sircana(f::PolyElem{T}, g::PolyElem{T}) where T <: ResElem{S} where end =# -function Nemo.gcd(a::ResElem{T}, b::ResElem{T}) where T <: IntegerUnion - m = modulus(a) - return parent(a)(gcd(gcd(a.data, m), b.data)) -end - -function Nemo.gcdx(a::ResElem{T}, b::ResElem{T}) where T <: IntegerUnion - m = modulus(a) - R = parent(a) - g, u, v = gcdx(ZZRingElem(a.data), ZZRingElem(b.data)) - G, U, V = gcdx(g, ZZRingElem(m)) - return R(G), R(U)*R(u), R(U)*R(v) -end @doc raw""" annihilator(a::ResElem{ZZRingElem}) -> r @@ -318,93 +306,11 @@ function annihilator(a::ResElem{T}) where T <: IntegerUnion return R(divexact(m, gcd(m, a.data))) end -@doc raw""" - is_unit(f::Union{ZZModPolyRingElem,zzModPolyRingElem}) -> Bool - -Tests if $f$ is a unit in the polynomial ring, i.e. if -$f = u + n$ where $u$ is a unit in the coeff. ring -and $n$ is nilpotent. -""" -function Nemo.is_unit(f::T) where T <: Union{ZZModPolyRingElem,zzModPolyRingElem} - if !is_unit(constant_coefficient(f)) - return false - end - for i=1:degree(f) - if !is_nilpotent(coeff(f, i)) - return false - end - end - return true -end - -@doc raw""" - is_nilpotent(a::ResElem{ZZRingElem}) -> Bool - is_nilpotent(a::ResElem{Integer}) -> Bool - -Tests if $a$ is nilpotent. -""" -function is_nilpotent(a::ResElem{T}) where T <: IntegerUnion - #a is nilpontent if it is divisible by all primes divising the modulus - # the largest exponent a prime can divide is nbits(m) - l = nbits(modulus(a)) - return iszero(a^l) -end - function is_zerodivisor(f::T) where T <: Union{ZZModPolyRingElem,zzModPolyRingElem} c = content(f) return is_nilpotent(c) end -function Nemo.inv(f::T) where T <: Union{ZZModPolyRingElem,zzModPolyRingElem} - if !is_unit(f) - error("impossible inverse") - end - Rx = parent(f) - g = Rx(inv(constant_coefficient(f))) - #lifting: to invert a, start with an inverse b mod m, then - # then b -> b*(2-ab) is an inverse mod m^2 - # starting with this g, and using the fact that all coeffs are nilpotent - # we have an inverse modulo s.th. nilpotent. Hence it works - c = Rx() - mul!(c, f, g) - while !isone(c) - mul!(g, g, 2-c) - mul!(c, f, g) - end - return g -end - -function Nemo.invmod(f::ZZModPolyRingElem, M::ZZModPolyRingElem) - if !is_unit(f) - r = parent(f)() - i = ccall((:fmpz_mod_poly_invmod, libflint), Int, (Ref{ZZModPolyRingElem}, Ref{ZZModPolyRingElem}, Ref{ZZModPolyRingElem}, Ref{fmpz_mod_ctx_struct}), r, f, M, f.parent.base_ring.ninv) - if iszero(i) - error("not yet implemented") - else - return r - end - end - if !is_unit(leading_coefficient(M)) - error("not yet implemented") - end - g = parent(f)(inv(constant_coefficient(f))) - #lifting: to invert a, start with an inverse b mod m, then - # then b -> b*(2-ab) is an inverse mod m^2 - # starting with this g, and using the fact that all coeffs are nilpotent - # we have an inverse modulo s.th. nilpotent. Hence it works - c = f*g - rem!(c, c, M) - while !isone(c) - mul!(g, g, 2-c) - rem!(g, g, M) - mul!(c, f, g) - rem!(c, c, M) - end - return g -end - - - function rres_sircana_pp(f1::PolyElem{T}, g1::PolyElem{T}) where T <: ResElem{S} where S <: IntegerUnion Nemo.check_parent(f1, g1) @assert typeof(f1) == typeof(g1) @@ -1321,15 +1227,6 @@ function unit_group_pp(f::fqPolyRepPolyRingElem, k::Int) end =# -function basis(K::fqPolyRepField) - b = fqPolyRepFieldElem[] - for i=1:degree(K) - x = K() - setcoeff!(x, i-1, UInt(1)) - push!(b, x) - end - return b -end function unit_group_1_part(f::fqPolyRepPolyRingElem, k::Int) pr = [k] @@ -1438,16 +1335,6 @@ function taylor_shift(x::zzModPolyRingElem, c::UInt) return r end -function evaluate(f::fpPolyRingElem, v::Vector{fpFieldElem}) - F = base_ring(f) - v1 = UInt[x.data for x in v] - res = UInt[UInt(1) for x in v] - ccall((:nmod_poly_evaluate_nmod_vec, libflint), Nothing, - (Ptr{UInt}, Ref{fpPolyRingElem}, Ptr{UInt}, UInt), - res, f, v1, UInt(length(v))) - return fpFieldElem[fpFieldElem(x, F) for x in res] -end - """ is_primitive(f::zzModPolyRingElem) @@ -1637,9 +1524,3 @@ function _coprimality_test(f::T, g::T, h::T) where T <: Union{zzModPolyRingElem, return false end end - -function addmul!(z::T, x::T, y::T) where {T <: RingElement} - zz = parent(z)() - zz = mul!(zz, x, y) - return addeq!(z, zz) -end diff --git a/src/Misc/psibound.jl b/src/Misc/psibound.jl index 7f262dd1ed..48f568de0d 100644 --- a/src/Misc/psibound.jl +++ b/src/Misc/psibound.jl @@ -96,14 +96,6 @@ function _exp(a::ZZModAbsPowerSeriesRingElem) return r end -function lift(R::ZZAbsPowerSeriesRing, f::ZZModAbsPowerSeriesRingElem) - r = R() - for i=0:length(f)-1 - setcoeff!(r, i, lift(coeff(f, i))) - end - return r -end - function _psi_lower(N::ZZRingElem, pr, a::Int=776, cl = ceil) p = ZZRingElem(next_prime(2^60)) n = nbits(N) diff --git a/src/ModAlgAss/Lattices.jl b/src/ModAlgAss/Lattices.jl index d5ec95acf3..684c40da68 100644 --- a/src/ModAlgAss/Lattices.jl +++ b/src/ModAlgAss/Lattices.jl @@ -135,15 +135,6 @@ function _lift_to_Q!(z, K::fpMatrix) return z end -function addmul!(A::fpMatrix, B::fpMatrix, C::fpFieldElem, D::fpMatrix) - ccall((:nmod_mat_scalar_addmul_ui, libflint), Cvoid, (Ref{fpMatrix}, Ref{fpMatrix}, Ref{fpMatrix}, UInt), A, B, D, C.data) - return A -end - -function mul!(A::fpMatrix, B::fpFieldElem, D::fpMatrix) - ccall((:nmod_mat_scalar_mul_ui, libflint), Cvoid, (Ref{fpMatrix}, Ref{fpMatrix}, UInt), A, C, B.data) -end - function pmaximal_sublattices(L::ModAlgAssLat, p::Int; filter = nothing, composition_factors = nothing) res = typeof(L)[] if composition_factors === nothing diff --git a/src/NumField/NfAbs/Elem.jl b/src/NumField/NfAbs/Elem.jl index 19362ac138..0b529231d8 100644 --- a/src/NumField/NfAbs/Elem.jl +++ b/src/NumField/NfAbs/Elem.jl @@ -1,13 +1,5 @@ export is_integral -################################################################################ -# -# Copy -# -################################################################################ - -Base.copy(d::nf_elem) = deepcopy(d) - ################################################################################ # # Basis matrix @@ -59,12 +51,6 @@ end # ################################################################################ -function sub!(a::nf_elem, b::nf_elem, c::nf_elem) - ccall((:nf_elem_sub, libantic), Nothing, - (Ref{nf_elem}, Ref{nf_elem}, Ref{nf_elem}, Ref{AnticNumberField}), - a, b, c, a.parent) -end - function set_den!(a::nf_elem, d::ZZRingElem) ccall((:nf_elem_set_den, libantic), Nothing, (Ref{nf_elem}, Ref{ZZRingElem}, Ref{AnticNumberField}), @@ -77,13 +63,6 @@ function set_num_coeff!(a::nf_elem, i::Int, b::ZZRingElem) a, i, b, parent(a)) end -function divexact!(z::nf_elem, x::nf_elem, y::ZZRingElem) - ccall((:nf_elem_scalar_div_fmpz, libantic), Nothing, - (Ref{nf_elem}, Ref{nf_elem}, Ref{ZZRingElem}, Ref{AnticNumberField}), - z, x, y, parent(x)) - return z -end - function gen!(r::nf_elem) a = parent(r) ccall((:nf_elem_gen, libantic), Nothing, @@ -91,21 +70,6 @@ function gen!(r::nf_elem) return r end -function one!(r::nf_elem) - a = parent(r) - ccall((:nf_elem_one, libantic), Nothing, - (Ref{nf_elem}, Ref{AnticNumberField}), r, a) - return r -end - -function one(r::nf_elem) - a = parent(r) - return one(a) -end - -function zero(r::nf_elem) - return zero(parent(r)) -end ################################################################################ # @@ -219,27 +183,6 @@ function is_norm_divisible_pp(a::nf_elem, n::ZZRingElem) return iszero(el) end -################################################################################ -# -# Numerator -# -################################################################################ - -@doc raw""" - numerator(a::nf_elem) -> nf_elem - -For an element $a\in K = Q[t]/f$ write $a$ as $b/d$ with -$b\in Z[t]$, $\deg(a) = \deg(b)$ and $d>0$ minimal in $Z$. -This function returns $b$. -""" -function numerator(a::nf_elem) - _one = one(FlintZZ) - z = deepcopy(a) - ccall((:nf_elem_set_den, libantic), Nothing, - (Ref{nf_elem}, Ref{ZZRingElem}, Ref{AnticNumberField}), - z, _one, a.parent) - return z -end ################################################################################ # @@ -950,14 +893,6 @@ end mod(x::nf_elem, y::Integer) = mod(x, ZZRingElem(y)) -#Assuming that the denominator of a is one, reduces all the coefficients modulo p -# non-symmetric (positive) residue system -function mod!(a::nf_elem, b::ZZRingElem) - ccall((:nf_elem_mod_fmpz, libantic), Nothing, - (Ref{nf_elem}, Ref{nf_elem}, Ref{ZZRingElem}, Ref{AnticNumberField}), - a, a, b, parent(a)) - return a -end function rem(a::nf_elem, b::ZZRingElem) c = deepcopy(a) diff --git a/src/NumField/NfAbs/MPolyAbsFact.jl b/src/NumField/NfAbs/MPolyAbsFact.jl index 084f0761f3..a746a0fd06 100644 --- a/src/NumField/NfAbs/MPolyAbsFact.jl +++ b/src/NumField/NfAbs/MPolyAbsFact.jl @@ -55,21 +55,6 @@ end See what Dan did in this case. =# -function set_precision(f::PolyElem{T}, n::Int) where {T <: SeriesElem} - g = parent(f)() - for i=0:length(f) - setcoeff!(g, i, set_precision(coeff(f, i), n)) - end - return g -end - -function set_precision!(f::PolyElem{T}, n::Int) where {T <: SeriesElem} - for i=0:length(f) - setcoeff!(f, i, set_precision!(coeff(f, i), n)) - end - return f -end - mutable struct HenselCtxFqRelSeries{T} f :: ZZMPolyRingElem # bivariate n :: Int # number of factors @@ -276,18 +261,6 @@ function check_qadic(a::qadic) @assert all(x->abs(x) < p, coefficients(f)) end -function shift_right(a::qadic, n::Int) - b = deepcopy(a) - b.val -= n - return b -end - -function shift_left(a::qadic, n::Int) - b = deepcopy(a) - b.val += n - return b -end - function _shift_coeff_right(f::PolyElem{<:SeriesElem{qadic}}, n::Int) g = parent(f)() for i = 0:length(f) @@ -1318,11 +1291,6 @@ function example(k::AnticNumberField, d::Int, nt::Int, c::AbstractRange=-10:10) return norm(f) end -function Hecke.is_irreducible(a::QQMPolyRingElem) - af = factor(a) - return !(length(af.fac) > 1 || any(x->x>1, values(af.fac))) -end - # f is bivariate. return f(xvar, 0) where xvar is in the multivar ring R function _yzero_image(R, f, xvar::Int) z = MPolyBuildCtx(R) diff --git a/src/NumField/NfAbs/MPolyGcd.jl b/src/NumField/NfAbs/MPolyGcd.jl index 2a894fbef6..f5ebf6f90c 100644 --- a/src/NumField/NfAbs/MPolyGcd.jl +++ b/src/NumField/NfAbs/MPolyGcd.jl @@ -544,20 +544,6 @@ function Hecke.modular_lift(g::Vector{T}, me::Hecke.modular_env) where T <: MPol end -function Hecke.mod!(f::ZZPolyRingElem, p::ZZRingElem) - for i=0:degree(f) - setcoeff!(f, i, mod(coeff(f, i), p)) - end -end - -function Hecke.mod(f::ZZPolyRingElem, p::ZZRingElem) - g = parent(f)() - for i=0:degree(f) - setcoeff!(g, i, mod(coeff(f, i), p)) - end - return g -end - function Hecke.mod_sym!(f::ZZPolyRingElem, p::ZZRingElem) for i=0:degree(f) setcoeff!(f, i, Hecke.mod_sym(coeff(f, i), p)) diff --git a/src/QuadForm/Enumeration.jl b/src/QuadForm/Enumeration.jl index c428a60d46..98e8e2e56f 100644 --- a/src/QuadForm/Enumeration.jl +++ b/src/QuadForm/Enumeration.jl @@ -1049,11 +1049,6 @@ end return z end -@inline function divexact!(z::QQFieldElem, a::QQFieldElem, b::QQFieldElem) - ccall((:fmpq_div, libflint), Cvoid, (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), z, a, b) - return z -end - @inline function add_two!(z::ZZRingElem, x::ZZRingElem) ccall((:fmpz_add_ui, libflint), Cvoid, (Ref{ZZRingElem}, Ref{ZZRingElem}, Int), z, x, 2) return z @@ -1064,8 +1059,6 @@ end return z end -divexact!(z::Rational{Int}, x::Rational{Int}, y::Rational{Int}) = divexact(x, y) - floor!(z::Int, x::Rational{Int}, y::Int, w::Int) = Int(floor(x)) isqrt!(z::Int, x::Int) = isqrt(x) diff --git a/src/QuadForm/Morphism.jl b/src/QuadForm/Morphism.jl index b6cca9f7fd..cc21afd4f0 100644 --- a/src/QuadForm/Morphism.jl +++ b/src/QuadForm/Morphism.jl @@ -54,15 +54,6 @@ function is_normalized(w::Vector{Int}) end end -function neg!(w::Vector{Int}) - w .*= -1 -end - -function neg!(w::ZZMatrix) - ccall((:fmpz_mat_neg, libflint), Cvoid, (Ref{ZZMatrix}, Ref{ZZMatrix}), w, w) - return w -end - function find_point(w, V::VectorList) fl, k = has_point(w, V) @assert fl diff --git a/src/conjugates.jl b/src/conjugates.jl index ca4925cee5..4983b594cc 100644 --- a/src/conjugates.jl +++ b/src/conjugates.jl @@ -63,19 +63,6 @@ function evaluate(f::QQPolyRingElem, r::BigComplex) return s end -function evaluate(f::QQPolyRingElem, r::T) where T <: RingElem - R = parent(r) - if iszero(f) - return zero(R) - end - l = length(f) - 1 - s = R(coeff(f, l)) - for i in l-1:-1:0 - s = s*r + R(coeff(f, i)) - end - return s -end - function evaluate(f::ZZPolyRingElem, r::BigComplex) #Horner - not elegant, but workable l = f.length-1 @@ -145,17 +132,6 @@ function length(a::nf_elem, p::Int = 50) return sum([x*x for x in m]) end -function setprecision!(x::BigFloat, p::Int) - ccall((:mpfr_prec_round, :libmpfr), Nothing, (Ref{BigFloat}, Clong, Int32), x, p, Base.MPFR.ROUNDING_MODE[]) -end - -function Base.setprecision(x::BigFloat, p::Int) - setprecision(BigFloat, p) do - y = BigFloat() - ccall((:mpfr_set, :libmpfr), Nothing, (Ref{BigFloat}, Ref{BigFloat}, Int32), y, x, Base.MPFR.ROUNDING_MODE[]) - return y - end -end function minkowski_matrix(K::AnticNumberField, p::Int = 50) c = roots_ctx(K) @@ -185,14 +161,3 @@ function minkowski_matrix(K::AnticNumberField, p::Int = 50) setprecision(old) return m end - - -function *(a::ZZMatrix, b::Matrix{BigFloat}) - s = Base.size(b) - ncols(a) == s[1] || error("dimensions do not match") - - c = Array{BigFloat}(undef, nrows(a), s[2]) - return mult!(c, a, b) -end - - diff --git a/test/Misc.jl b/test/Misc.jl index d09a35c500..59ee89b5b3 100644 --- a/test/Misc.jl +++ b/test/Misc.jl @@ -1,13 +1,11 @@ @testset "Misc" begin include("Misc/psibound.jl") include("Misc/FiniteField.jl") - include("Misc/Infinity.jl") include("Misc/Primes.jl") include("Misc/meataxe.jl") include("Misc/stable_subgroups.jl") include("Misc/NumberField.jl") include("Misc/Poly.jl") - include("Misc/Localization.jl") include("Misc/OrdLocalization.jl") include("Misc/jordan_test.jl") include("Misc/RelFinField.jl") diff --git a/test/Misc/Infinity.jl b/test/Misc/Infinity.jl deleted file mode 100644 index ccd1d580fb..0000000000 --- a/test/Misc/Infinity.jl +++ /dev/null @@ -1,72 +0,0 @@ -@testset "PosInf basics" begin - - @test inf === PosInf() - - @test zero(inf) === 0 - @test one(inf) === 1 - -end - -@testset "PosInf arithmetic" begin - - @test inf + inf === inf - @test inf + 1 === inf - @test inf - 1 === inf - @test 1 + inf === inf - -end - -@testset "PosInf comparisons" begin - - @test max(inf, inf) === inf - @test max(inf, 1) === inf - @test max(1, inf) === inf - - @test 1 < inf - @test !(inf < 1) - - @test ZZ(1) < inf - @test !(inf < ZZ(1)) - - @test 1//2 < inf - @test !(inf < 1//2) - - @test QQ(1//2) < inf - @test !(inf < QQ(1//2)) - - # one positive infinity is not less than infinity (though that does - # not necessarily mean that they are equal either) - @test !(inf < inf) - - @test !(inf < 1//0) - @test !(1//0 < inf) - -end - -@testset "PosInf predicates" begin - - @test !isone(inf) - @test !isfinite(inf) - @test !iszero(inf) - - @test isinf(inf) - @test is_infinite(inf) - @test is_positive(inf) - -end - -@testset "is_infinite for other kinds of infinity" begin - - @test !is_infinite(0) - @test !is_infinite(0.0) - - @test is_infinite(Inf) - @test is_infinite(-Inf) - - @test is_infinite(1//0) - @test is_infinite(-1//0) - - @test is_infinite(big(1)//0) - @test is_infinite(big(-1)//0) - -end diff --git a/test/Misc/Localization.jl b/test/Misc/Localization.jl deleted file mode 100644 index e544f0d36e..0000000000 --- a/test/Misc/Localization.jl +++ /dev/null @@ -1,275 +0,0 @@ -@testset "Localization" begin - - R = FlintZZ - Qx,x = FlintQQ["x"] - - @testset "Constructor" begin - - @test parent_type(LocElem{ZZRingElem}) == Loc{ZZRingElem} - - L = localization(R, 17) - @test elem_type(L) == LocElem{ZZRingElem} - @test base_ring(L) == FlintZZ - @test base_ring(L()) == FlintZZ - - L = localization(R, [2,R(3)]) - @test elem_type(L) == LocElem{ZZRingElem} - @test base_ring(L) == FlintZZ - @test base_ring(L()) == FlintZZ - - @test parent_type(LocElem{QQPolyRingElem}) == Loc{QQPolyRingElem} - - L = localization(Qx, x^2+1) - @test elem_type(L) == LocElem{QQPolyRingElem} - @test base_ring(L) == Qx - @test base_ring(L()) == Qx - - L = localization(Qx, 4*x^2+x+1) - @test elem_type(L) == LocElem{QQPolyRingElem} - @test base_ring(L) == Qx - @test base_ring(L()) == Qx - - L = localization(Qx, [3*x^4+x+18, x^9+3]) - @test elem_type(L) == LocElem{QQPolyRingElem} - @test base_ring(L) == Qx - @test base_ring(L()) == Qx - - @test_throws ErrorException localization(R,Vector{Int64}()) - end - - @testset "Canonicalisation" begin - - L = localization(R, 23) - @test canonical_unit(L(23)) == one(L) - @test canonical_unit(L(230)) == L(10) - @test canonical_unit(L(230//45)) == L(10//45) - - L = localization(R, [2,7]) - @test canonical_unit(L(2)) == one(L) - @test canonical_unit(L(28)) == one(L) - @test canonical_unit(L(49//55)) == L(1//55) - @test canonical_unit(L(18//11)) == L(9//11) - @test canonical_unit(L(49//55)) == L(1//55) - - L = localization(Qx, 5x^2+3x+2) - @test canonical_unit(L(5x^2+3x+2)) == L(5) - @test canonical_unit(L(28)) == L(28) - @test canonical_unit(L(5*x^4+3*x^3+12*x^2+6*x+4)) == L(5*(x^2+2)) - - L = localization(Qx,[5x^2+3x+2, x^16+x+3]) - @test canonical_unit(L(x^16+x+3)) == one(L) - @test canonical_unit(L(89)) == L(89) - @test canonical_unit(L((5*x^4+3*x^3+12*x^2+6*x+4)//(x^3+9))) == inv(L((1//5*x^3+9//5)//(x^2+2))) - end - - @testset "Parent object call overloading" begin - - L = localization(R,5) - @test parent(L(4//7)) == L - @test_throws ErrorException L(24//50) - - L = localization(R,[2,3]) - @test_throws ErrorException L(11//2) - @test_throws ErrorException L(13//6) - @test_throws ErrorException L(14//18) - - L1 = localization(Qx, 5x^2+3x+2) - L2 = localization(Qx, 7x^3+2) - @test_throws ErrorException L1(L2(5)) - @test parent(L1(4x//7)) == L1 - - L = localization(Qx,[x^2+2,3x^3+5]) - @test parent(L(3x^2+6)) == L - @test_throws ErrorException L(15//(3x^2+6)) - @test_throws ErrorException L(17//(9*x^5+18*x^3+15*x^2+30)) - @test_throws ErrorException L(19//(9*x^6+54*x^5+18*x^4+123*x^3+90*x^2+30*x+180)) - end - - @testset "GCDX" begin - - L = localization(R,19) - for i in 1:300 - a = rand(L) - b = rand(L) - (g,u,v) = gcdx(a,b) - @test g == u*a + v*b - end - - L = localization(R,[3,23]) - for i in 1:300 - a = rand(L) - b = rand(L) - (g,u,v) = gcdx(a,b) - @test g == u*a + v*b - end - - L = localization(Qx, x^6+108) - a = L(x^10+2*x^8+14*x^7+49*x^5+42*x^3+294*x^2+588) - b = L(x^11+14*x^8+21*x^6+294*x^3+6) - (g,u,v) = gcdx(a,b) - @test g == u*a + v*b - - L = localization(Qx, [x^6+108, x^8+12x^3+3]) - a = L(x^10+2*x^8+14*x^7+49*x^5+42*x^3+294*x^2+588) - b = L(x^11+14*x^8+21*x^6+294*x^3+6) - (g,u,v) = gcdx(a,b) - @test g == u*a + v*b - end - - @testset "GCD and LCM" begin - - L = localization(R,23) - @test gcd(L(0),L(9)) == L(1) - @test gcd(L(12),L(9)) == L(1) - @test gcd(L(46//5),L(23)) == L(23) - for i in 1:300 - a = rand(L) - b = rand(L) - g = gcd(a,b) - @test divides(a,g)[1] && divides(b,g)[1] - end - - L = localization(R,[13,17]) - @test gcd(L(10),L(0)) == L(1) - @test gcd(L(26),L(0)) == L(13) - @test gcd(L(12),L(11)) == L(1) - @test gcd(L(26//5),L(221)) == L(13) - for i in 1:300 - a = rand(L) - b = rand(L) - g = gcd(a,b) - @test divides(a,g)[1] && divides(b,g)[1] - end - - L = localization(Qx, x^6+108) - a = L(x^10+2*x^8+14*x^7+49*x^5+42*x^3+294*x^2+588) - b = L(x^11+14*x^8+21*x^6+294*x^3+6) - g = gcd(a,b) - @test g == L(1) - a = L(x^16+2*x^14+14*x^13+49*x^11+108*x^10+42*x^9+510*x^8+1512*x^7+588*x^6+5292*x^5+4536*x^3+31752*x^2+63504) - b = L(x^17+14*x^14+21*x^12+108*x^11+294*x^9+1512*x^8+2274*x^6+31752*x^3+648) - g = gcd(a,b) - @test g == L(x^6+108) - - L = localization(Qx, [x^6+108, x^8+12x^3+3]) - a = L(x^10+2*x^8+14*x^7+49*x^5+42*x^3+294*x^2+588) - b = L(x^13+2*x^11+14*x^10+49*x^8+42*x^6+294*x^5+588*x^3+6*x^2+12) - g = gcd(a,b) - @test g == L(1) - - L = localization(R,R(3)) - @test lcm(L(15),L(9)) == L(9) - for i in 1:300 - a = rand(L) - b = rand(L) - l = lcm(a,b) - @test divides(l,a)[1] && divides(l,b)[1] - end - end - - @testset "Exact division" begin - L = localization(R,R(19)) - @test divides(L(15),L(3)) == (true, L(5)) - @test divides(L(15),L(30)) == (true, L(1//2)) - @test divides(L(15),L(19))[1] == false - @test_throws ErrorException divexact(L(8),L(38)) - for i in 1:300 - a = rand(L) - b = rand(L) - d = divides(a,b) - if d[1] - @test a == d[2] * b - end - end - - L = localization(R,[11,13]) - @test_throws ErrorException divexact(L(9),L(22)) - @test divides(L(15),L(3)) == (true, L(5)) - @test divides(L(15),L(30)) == (true, L(1//2)) - @test divides(L(15),L(22))[1] == false - for i in 1:300 - a = rand(L) - b = rand(L) - d = divides(a,b) - if d[1] - @test a == d[2] * b - end - end - - L = localization(Qx, x^6+108) - @test_throws ErrorException divexact(L(9),L(2x^6+216)) - a = L(x^10+2*x^8+14*x^7+49*x^5+42*x^3+294*x^2+588) - b = L(x^11+14*x^8+21*x^6+294*x^3+6) - g = divides(a,b) - @test a == b * g[2] - @test g[1] == true - a = L(x^16+2*x^14+14*x^13+49*x^11+108*x^10+42*x^9+510*x^8+1512*x^7+588*x^6+5292*x^5+4536*x^3+31752*x^2+63504) - b = L(x^17+14*x^14+21*x^12+108*x^11+294*x^9+1512*x^8+2274*x^6+31752*x^3+648) - @test a == b * g[2] - @test g[1] == true - end - - @testset "Inversion" begin - L = localization(R,R(19)) - @test inv(L(15)) == L(1//15) - @test inv(L(20)) == L(1//20) - @test_throws ErrorException inv(L(38)) - @test_throws ErrorException inv(L()) - - L = localization(R,[11,13]) - @test_throws ErrorException inv(L(11)) - @test inv(L(14)) == L(1//14) - - L = localization(Qx, x^6+108) - @test_throws ErrorException inv(L(2x^6+216)) - @test inv(L(x^2+108)) == L(1//(x^2+108)) - - L = localization(Qx, [x^6+3, x^3+5]) - @test_throws ErrorException inv(L(2x^6+6)) - @test inv(L(x^2+108)) == L(1//(x^2+108)) - end - - @testset "Binary operators" begin - L = localization(R,19) - @test L(19) + L(1) == L(20) - @test L(19) - L(1) == L(18) - @test L(3) * L(4//2) == L(6) - - L = localization(R,[11,13]) - @test L(18//2) + L(2//1) == L(11) - @test L(18//3) - L(1) == L(5) - @test L(32) * L(4) == L(128) - - L = localization(Qx, x^6+108) - @test L(18x//2) + L(2x//1) == L(11x) - @test L(18x//3) - L(1x) == L(5x) - @test L(32x) * L(4x) == L(128x^2) - - L = localization(Qx, [x^6+3, x^3+5]) - @test L(18x//2) + L(2x//1) == L(11x) - @test L(18x//3) - L(1x) == L(5x) - @test L(32x) * L(4x) == L(128x^2) - end - - @testset "Basic manipulation" begin - L = localization(R,19) - @test iszero(L(0)) == true - @test isone(L(2//3)) == false - @test is_unit(L(24)) == true - - L = localization(R,[11,13]) - @test iszero(L(4//5)) == false - @test isone(L(13//13)) == true - @test is_unit(L(26//2)) == false - - L = localization(Qx, x^6+108) - @test iszero(L(x^2 + x)) == false - @test isone(L(x^2//x^2)) == true - @test is_unit(L(x)) == true - - L = localization(Qx, [x^6+3, x^3+5]) - @test iszero(L(x-x)) == true - @test isone(L(x^2+3x)) == false - @test is_unit(L((x^3+5)//(x^3+5))) - end -end From e25e8a710b97d5c19de9d77b6b95bbbcd2eefe55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Fri, 30 Jun 2023 18:55:39 +0200 Subject: [PATCH 09/11] Some fixes --- src/LinearAlgebra/Solve.jl | 13 --------- src/Misc/Poly.jl | 60 ++++++++++++++++++++++++++++++++++++++ src/NumField/NfAbs/Elem.jl | 7 ----- src/Sparse/HNF.jl | 13 --------- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/src/LinearAlgebra/Solve.jl b/src/LinearAlgebra/Solve.jl index e97f963a44..739562b36d 100644 --- a/src/LinearAlgebra/Solve.jl +++ b/src/LinearAlgebra/Solve.jl @@ -64,19 +64,6 @@ end -@doc raw""" - mod_sym!(A::Generic.Mat{nf_elem}, m::ZZRingElem) - -Inplace: reduce all entries of $A$ modulo $m$, into the symmetric residue system. -""" -function mod_sym!(A::Generic.Mat{nf_elem}, m::ZZRingElem) - for i = 1:nrows(A) - for j = 1:ncols(A) - mod_sym!(A[i, j], m) - end - end -end - function small_coeff(a::nf_elem, B::ZZRingElem, i::Int) z = ZZRingElem() Nemo.num_coeff!(z, a, i) diff --git a/src/Misc/Poly.jl b/src/Misc/Poly.jl index ac89e6460f..8b7c8be923 100644 --- a/src/Misc/Poly.jl +++ b/src/Misc/Poly.jl @@ -673,6 +673,66 @@ end There are also bounds on the coefficients which are sometimes tight =# +function roots(f::ZZPolyRingElem, ::QQField; max_roots::Int=degree(f)) + if degree(f) < 1 + return QQFieldElem[] + end + if degree(f) == 1 + return QQFieldElem[-constant_coefficient(f)//leading_coefficient(f)] + end + + g = gcd(f, derivative(f)) + if isone(g) + h = f + else + h = divexact(f, g) + end + if degree(h) == 1 + return QQFieldElem[-constant_coefficient(h)//leading_coefficient(h)] + end + h = primpart(h) + + global p_start + p = p_start + bd = leading_coefficient(h) + maximum(abs, coefficients(h)) + while true + p = next_prime(p) + k = GF(p) + hp = change_base_ring(k, h) + if !is_squarefree(hp) + continue + end + k = ceil(Int, log(bd) / log(p)) + Hp = factor_mod_pk(h, p, k) + pk = ZZRingElem(p)^k + r = QQFieldElem[mod_sym(-constant_coefficient(x) * leading_coefficient(h), pk) // leading_coefficient(h) for x = keys(Hp) if degree(x) == 1] + return [x for x = r if iszero(f(x))] + end +end + +function roots(f::ZZPolyRingElem; max_roots::Int=degree(f)) + r = roots(f, FlintQQ, max_roots=max_roots) + return ZZRingElem[FlintZZ(x) for x = r if denominator(x) == 1] +end + +function roots(f::QQPolyRingElem; max_roots::Int=degree(f)) + Zx, x = polynomial_ring(FlintZZ, cached=false) + g = Zx(denominator(f) * f) + return roots(g, FlintQQ) +end + +function roots(f::Union{ZZPolyRingElem,QQPolyRingElem}, R::ArbField, abs_tol::Int=R.prec, initial_prec::Int...) + g = factor(f) + r = elem_type(R)[] + C = AcbField(precision(R)) + for k = keys(g.fac) + s, _ = signature(k) + rt = roots(k, C) + append!(r, map(real, rt[1:s])) + end + return r +end + function roots(f::Union{ZZPolyRingElem,QQPolyRingElem}, R::AcbField, abs_tol::Int=R.prec, initial_prec::Int...) lf = factor(f) return map(R, vcat([_roots(g, abs_tol, initial_prec...) for g = keys(lf.fac) if degree(g) > 0]...)) diff --git a/src/NumField/NfAbs/Elem.jl b/src/NumField/NfAbs/Elem.jl index 0b529231d8..b76e5ba5eb 100644 --- a/src/NumField/NfAbs/Elem.jl +++ b/src/NumField/NfAbs/Elem.jl @@ -874,13 +874,6 @@ function coprime_denominator(a::nf_elem, b::ZZRingElem) return z end -function mod_sym!(a::nf_elem, b::ZZRingElem) - ccall((:nf_elem_smod_fmpz, libantic), Nothing, - (Ref{nf_elem}, Ref{nf_elem}, Ref{ZZRingElem}, Ref{AnticNumberField}), - a, a, b, parent(a)) - return a -end - function mod(b::nf_elem, p::ZZRingElem) K = parent(b) if is_defining_polynomial_nice(parent(b)) diff --git a/src/Sparse/HNF.jl b/src/Sparse/HNF.jl index 3d3e5e0a5b..add2be1296 100644 --- a/src/Sparse/HNF.jl +++ b/src/Sparse/HNF.jl @@ -172,20 +172,7 @@ function reduce(A::SMat{T}, g::SRow{T}, m::T) where {T} return g end -function mod_sym(a::T, b::T) where {T} - return mod(a, b) -end -function mod_sym!(a::T, b::T) where {T} - return mod!(a, b) -end -function mod_sym!(a::ZZRingElem, b::ZZRingElem) - mod!(a, a, b) - if a>div(b, 2) - sub!(a, a, b) - end - return a -end ################################################################################ # From 854a1bb3439f8a932b1e9933ead09e82fb9eae74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Sat, 1 Jul 2023 19:01:40 +0200 Subject: [PATCH 10/11] Remove more obvious piracy from different places now in Nemo --- examples/Round2.jl | 2 +- src/GenOrd/Auxiliary.jl | 37 -------------- src/GenOrd/GenOrd.jl | 2 +- src/GrpAb/SubgroupEnum.jl | 6 --- src/LocalField/Elem.jl | 4 -- src/LocalField/LocalField.jl | 12 ----- src/LocalField/Poly.jl | 14 +----- src/Map/NfOrd.jl | 14 +++--- src/Misc/CRT.jl | 19 +------- src/Misc/FiniteField.jl | 19 ++------ src/Misc/Integer.jl | 24 --------- src/Misc/RelFiniteField.jl | 21 +------- src/Misc/Series.jl | 9 ---- src/Misc/acb_root_ctx.jl | 4 -- src/NumField/Elem.jl | 31 ------------ src/NumField/NfAbs/Elem.jl | 62 ------------------------ src/NumField/NfAbs/MPolyGcd.jl | 6 --- src/NumField/NfAbs/NfAbs.jl | 2 - src/NumField/NfAbs/PolyFact.jl | 9 ---- src/NumField/NfAbs/Simplify.jl | 2 +- src/NumField/NfRel/NfRelNS.jl | 23 --------- src/NumField/NfRel/Simplify.jl | 8 +-- src/NumField/QQ.jl | 4 +- src/NumField/promote_rules.jl | 12 ----- src/NumFieldOrd/NfOrd/Clgp/Saturate.jl | 2 +- src/NumFieldOrd/NfOrd/Ideal/Ideal.jl | 3 -- src/NumFieldOrd/NfOrd/Ideal/Valuation.jl | 6 +-- src/QuadForm/Morphism.jl | 1 - 28 files changed, 27 insertions(+), 331 deletions(-) diff --git a/examples/Round2.jl b/examples/Round2.jl index b95137688b..154119b281 100644 --- a/examples/Round2.jl +++ b/examples/Round2.jl @@ -292,7 +292,7 @@ end function Hecke.powermod(a::OrderElem, n::ZZRingElem, p::RingElem) c = parent(a)(1) - for i = Hecke.BitsMod.bits(n) + for i = Hecke.bits(n) c *= c if i c *= a diff --git a/src/GenOrd/Auxiliary.jl b/src/GenOrd/Auxiliary.jl index 94c78c9c6e..d35f85587b 100644 --- a/src/GenOrd/Auxiliary.jl +++ b/src/GenOrd/Auxiliary.jl @@ -208,42 +208,6 @@ end (R::Generic.RationalFunctionField{T})(x::KInftyElem{T}) where {T <: FieldElem} = x.d -base_ring_type(::Type{AbstractAlgebra.Generic.PolyRing{T}}) where {T} = parent_type(T) - -base_ring_type(::Type{AcbPolyRing}) = AcbField - -base_ring_type(::Type{ArbPolyRing}) = ArbField - -base_ring_type(::Type{QQPolyRing}) = QQField - -base_ring_type(::Type{ZZModPolyRing}) = Nemo.ZZModRing - -base_ring_type(::Type{ZZPolyRing}) = ZZRing - -base_ring_type(::Type{FqPolyRing}) = FqField - -base_ring_type(::Type{fqPolyRepPolyRing}) = fqPolyRepField - -base_ring_type(::Type{FqPolyRepPolyRing}) = FqPolyRepField - -base_ring_type(::Type{FpPolyRing}) = Nemo.FpField - -base_ring_type(::Type{fpPolyRing}) = Nemo.fpField - -base_ring_type(::Type{zzModPolyRing}) = Nemo.zzModRing - -function (R::Generic.PolyRing{T})(x::AbstractAlgebra.Generic.RationalFunctionFieldElem{T, U}) where {T <: RingElem, U} - @assert isone(denominator(x)) - @assert parent(numerator(x)) === R - return numerator(x) -end - -function (R::PolyRing{T})(x::AbstractAlgebra.Generic.RationalFunctionFieldElem{T, U}) where {T <: RingElem, U} - @assert isone(denominator(x)) - @assert parent(numerator(x)) === R - return numerator(x) -end - # RationalFunctionFieldElem{T}, PolyRing{T} function Hecke.numerator(a::Generic.RationalFunctionFieldElem{T}, S::PolyRing{T}) where {T} return numerator(a) @@ -312,7 +276,6 @@ function Hecke.integral_split(M::MatElem{<:AbstractAlgebra.FieldElem}, S::Generi return m, den end -Nemo.ngens(R::MPolyRing) = Nemo.nvars(R) #TODO: move elsewhere? function Hecke.lcm(a::Vector{<:RingElem}) if length(a) == 0 diff --git a/src/GenOrd/GenOrd.jl b/src/GenOrd/GenOrd.jl index cba7cad2ce..69c4801b7b 100644 --- a/src/GenOrd/GenOrd.jl +++ b/src/GenOrd/GenOrd.jl @@ -464,7 +464,7 @@ end function powermod(a::GenOrdElem, n::ZZRingElem, p::RingElem) c = one(parent(a)) - for i = BitsMod.bits(n) + for i = bits(n) c *= c if i c *= a diff --git a/src/GrpAb/SubgroupEnum.jl b/src/GrpAb/SubgroupEnum.jl index 0fcf229120..62e181104f 100644 --- a/src/GrpAb/SubgroupEnum.jl +++ b/src/GrpAb/SubgroupEnum.jl @@ -279,12 +279,6 @@ end return true end -#(::Colon)(x::Int, y::Nothing) = 1:0 - -Base.:(:)(x::Int, y::Nothing) = 1:0 - -Base.:(:)(x::Int, y::ZZRingElem) = ZZRingElem(x):y - function SigmaIteratorGivenY(s, x, y) t = something(findlast(!iszero, y), 0) SigmaIteratorGivenY(Iterators.filter(sigma -> _isvalid(s, t, x, y, sigma), diff --git a/src/LocalField/Elem.jl b/src/LocalField/Elem.jl index 153d4ebc92..15ff83fc05 100644 --- a/src/LocalField/Elem.jl +++ b/src/LocalField/Elem.jl @@ -64,10 +64,6 @@ function Base.setprecision(a::LocalFieldElem, n::Int) return setprecision!(b, n) end -function setprecision!(a::padic, n::Int) - return setprecision(a, n) -end - function setprecision!(a::LocalFieldElem, n::Int) K = parent(a) e = ramification_index(K) diff --git a/src/LocalField/LocalField.jl b/src/LocalField/LocalField.jl index 0caa4d0fe1..0e14bb9c29 100644 --- a/src/LocalField/LocalField.jl +++ b/src/LocalField/LocalField.jl @@ -400,18 +400,6 @@ function setprecision(f::Function, K::Union{LocalField, FlintPadicField, FlintQa return v end -function setprecision(f::Function, K::Union{FlintPadicField, FlintQadicField}, n::Int) - old = precision(K) -# @assert n>=0 - setprecision!(K, n) - v = try - f() - finally - setprecision!(K, old) - end - return v -end - ################################################################################ # diff --git a/src/LocalField/Poly.jl b/src/LocalField/Poly.jl index 28bade2375..7debf9ede5 100644 --- a/src/LocalField/Poly.jl +++ b/src/LocalField/Poly.jl @@ -6,18 +6,6 @@ add_assertion_scope(:padic_poly) # ################################################################################ -function setprecision!(f::Generic.Poly{qadic}, N::Int) - for i=1:length(f) - setprecision!(f.coeffs[i], N) - end - set_length!(f, normalise(f, length(f))) - return f -end - -function Base.setprecision(f::Generic.Poly{qadic}, N::Int) - g = map_coefficients(x->setprecision(x, N), f, parent = parent(f)) - return g -end function setprecision_fixed_precision(f::Generic.Poly{qadic}, N::Int) f = setprecision(f, N) @@ -158,7 +146,7 @@ function _content(f::Generic.Poly{T}) where T <: Union{padic, qadic, LocalFieldE return uniformizer(K)^numerator(e) end -function rem!(x::AbstractAlgebra.Generic.Poly{T}, y::AbstractAlgebra.Generic.Poly{T}, z::AbstractAlgebra.Generic.Poly{T}) where T <:Union{padic, qadic, LocalFieldElem} +function rem!(x::AbstractAlgebra.Generic.Poly{T}, y::AbstractAlgebra.Generic.Poly{T}, z::AbstractAlgebra.Generic.Poly{T}) where {T<:LocalFieldElem} x = rem(y, z) return x end diff --git a/src/Map/NfOrd.jl b/src/Map/NfOrd.jl index 7e53664c6a..8667557799 100644 --- a/src/Map/NfOrd.jl +++ b/src/Map/NfOrd.jl @@ -983,7 +983,7 @@ function image(mF::NfToFqNmodMor_easy, a::FacElem{nf_elem, AnticNumberField}, D: if !((quo != 0 && vv != 0) || !iszero(v)) if !cached - nf_elem_to_gfp_poly!(t, k) + Nemo.nf_elem_to_gfp_poly!(t, k) D[i] = zero(parent(t)) set!(D[i], t) end @@ -996,7 +996,7 @@ function image(mF::NfToFqNmodMor_easy, a::FacElem{nf_elem, AnticNumberField}, D: (Ref{fqPolyRepFieldElem}, Ref{fpPolyRingElem}, Ref{fqPolyRepField}), s, D[i], Fq) _reduce(s) else - nf_elem_to_gfp_poly!(t, k) + Nemo.nf_elem_to_gfp_poly!(t, k) #tt = deepcopy(t) if isassigned(D, i) y = D[i] @@ -1055,13 +1055,13 @@ function image(mF::NfToFqNmodMor_easy, a::nf_elem, n_quo::Int = 0) end function _nf_to_gfp_elem(b::nf_elem, a_tmp::fpPolyRingElem, def_pol::fpPolyRingElem) - nf_elem_to_gfp_poly!(a_tmp, b) + Nemo.nf_elem_to_gfp_poly!(a_tmp, b) rem!(a_tmp, a_tmp, def_pol) return coeff(a_tmp, 0) end function _nf_to_gfp_elem(b::nf_elem, a_tmp::FpPolyRingElem, def_pol::FpPolyRingElem) - nf_elem_to_gfp_fmpz_poly!(a_tmp, b) + Nemo.nf_elem_to_gfp_fmpz_poly!(a_tmp, b) rem!(a_tmp, a_tmp, def_pol) return coeff(a_tmp, 0) end @@ -1145,7 +1145,7 @@ function image(mF::NfToGFMor_easy, a::FacElem{nf_elem, AnticNumberField}, D::Vec if iszero(vv) && !cached D[i] = zero(parent(t)) - nf_elem_to_gfp_poly!(t, k) + Nemo.nf_elem_to_gfp_poly!(t, k) set!(D[i], t) end @@ -1153,7 +1153,7 @@ function image(mF::NfToGFMor_easy, a::FacElem{nf_elem, AnticNumberField}, D::Vec if cached s = evaluate_raw(D[i], evaluateat) else - nf_elem_to_gfp_poly!(t, k) + Nemo.nf_elem_to_gfp_poly!(t, k) #tt = deepcopy(t) if isassigned(D, i) y = D[i] @@ -1215,7 +1215,7 @@ function image(mF::NfToGFMor_easy, a::nf_elem, D::Vector, cached::Bool, n_quo::I #rem!(t, D[1], p) #s = coeff(t, 0) else - nf_elem_to_gfp_poly!(t, a) + Nemo.nf_elem_to_gfp_poly!(t, a) D[1] = deepcopy(t) #rem!(t, t, p) #s = coeff(t, 0) diff --git a/src/Misc/CRT.jl b/src/Misc/CRT.jl index 0e479d1377..bd3b3f9ee3 100644 --- a/src/Misc/CRT.jl +++ b/src/Misc/CRT.jl @@ -1,21 +1,6 @@ import Nemo.crt, Nemo.zero, Nemo.iszero, Nemo.isone, Nemo.sub! export crt_env, crt, crt_inv, modular_init, crt_signed -@inline function rem!(a::ZZRingElem, b::ZZRingElem, c::ZZRingElem) - ccall((:fmpz_mod, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), a, b, c) - return a -end - -function rem!(a::ZZModPolyRingElem, b::ZZModPolyRingElem, c::ZZModPolyRingElem) - ccall((:fmpz_mod_poly_rem, libflint), Nothing, (Ref{ZZModPolyRingElem}, Ref{ZZModPolyRingElem}, Ref{ZZModPolyRingElem}, Ref{fmpz_mod_ctx_struct}), a, b, c, a.parent.base_ring.ninv) - return a -end - -function rem!(a::FpPolyRingElem, b::FpPolyRingElem, c::FpPolyRingElem) - ccall((:fmpz_mod_poly_rem, libflint), Nothing, (Ref{FpPolyRingElem}, Ref{FpPolyRingElem}, Ref{FpPolyRingElem}, Ref{fmpz_mod_ctx_struct}), a, b, c, a.parent.base_ring.ninv) - return a -end - mutable struct crt_env{T} pr::Vector{T} id::Vector{T} @@ -778,7 +763,7 @@ end @inbounds function inner_eval(z::Vector{Tuple{fqPolyRepFieldElem, ZZRingElem, Int}}) sort!(z, lt = (a,b) -> isless(b[2], a[2])) t = z[1][3] #should be largest... - it = [BitsMod.bits(i[2]) for i=z] + it = [bits(i[2]) for i=z] is = map(iterate, it) u = one(z[1][1]) K = parent(u) @@ -860,7 +845,7 @@ function modular_proj(a::Generic.Poly{nf_elem}, me::modular_env) if iszero(mod(denominator(c), me.p)) throw(BadPrime(me.p)) end - nf_elem_to_nmod_poly!(r, c, true) + Nemo.nf_elem_to_nmod_poly!(r, c, true) crt_inv!(me.rp, r, me.ce) for j=1:me.ce.n u = coeff(me.Rp[j], i) diff --git a/src/Misc/FiniteField.jl b/src/Misc/FiniteField.jl index f0b521efd0..b25fc40a3b 100644 --- a/src/Misc/FiniteField.jl +++ b/src/Misc/FiniteField.jl @@ -17,19 +17,6 @@ function _reduce(a::FqPolyRepFieldElem) #end end -function (R::FqPolyRepField)(x::ZZModPolyRingElem) - z = R() - ccall((:fq_set_fmpz_mod_poly, libflint), Nothing, (Ref{Nemo.FqPolyRepFieldElem}, Ref{Nemo.ZZModPolyRingElem}, Ref{Nemo.FqPolyRepField}), z, x, R) - #ccall((:fq_reduce, libflint), Nothing, (Ref{Nemo.FqPolyRepFieldElem}, Ref{Nemo.FqPolyRepField}), z, R) - return z -end - -function (R::FqPolyRepField)(x::FpPolyRingElem) - z = R() - ccall((:fq_set_fmpz_mod_poly, libflint), Nothing, (Ref{Nemo.FqPolyRepFieldElem}, Ref{Nemo.FpPolyRingElem}, Ref{Nemo.FqPolyRepField}), z, x, R) - ccall((:fq_reduce, libflint), Nothing, (Ref{Nemo.FqPolyRepFieldElem}, Ref{Nemo.FqPolyRepField}), z, R) - return z -end #TODO: move elsewhere - and use. There are more calls to nmod_set/reduce function (A::fqPolyRepField)(x::zzModPolyRingElem) @@ -51,7 +38,7 @@ function (A::fqPolyRepField)(x::fpPolyRingElem) end function _nf_to_fq!(a::fqPolyRepFieldElem, b::nf_elem, K::fqPolyRepField, a_tmp::zzModPolyRingElem) - nf_elem_to_nmod_poly!(a_tmp, b) + Nemo.nf_elem_to_nmod_poly!(a_tmp, b) ccall((:fq_nmod_set, libflint), Nothing, (Ref{fqPolyRepFieldElem}, Ref{zzModPolyRingElem}, Ref{fqPolyRepField}), a, a_tmp, K) @@ -59,7 +46,7 @@ function _nf_to_fq!(a::fqPolyRepFieldElem, b::nf_elem, K::fqPolyRepField, a_tmp: end function _nf_to_fq!(a::fqPolyRepFieldElem, b::nf_elem, K::fqPolyRepField, a_tmp::fpPolyRingElem) - nf_elem_to_gfp_poly!(a_tmp, b) + Nemo.nf_elem_to_gfp_poly!(a_tmp, b) ccall((:fq_nmod_set, libflint), Nothing, (Ref{fqPolyRepFieldElem}, Ref{fpPolyRingElem}, Ref{fqPolyRepField}), a, a_tmp, K) @@ -67,7 +54,7 @@ function _nf_to_fq!(a::fqPolyRepFieldElem, b::nf_elem, K::fqPolyRepField, a_tmp: end function _nf_to_fq!(a::FqPolyRepFieldElem, b::nf_elem, K::FqPolyRepField, a_tmp::FpPolyRingElem) - nf_elem_to_gfp_fmpz_poly!(a_tmp, b) + Nemo.nf_elem_to_gfp_fmpz_poly!(a_tmp, b) ccall((:fq_set, libflint), Nothing, (Ref{FqPolyRepFieldElem}, Ref{FpPolyRingElem}, Ref{FqPolyRepField}), a, a_tmp, K) diff --git a/src/Misc/Integer.jl b/src/Misc/Integer.jl index 84b68a3266..e451770e03 100644 --- a/src/Misc/Integer.jl +++ b/src/Misc/Integer.jl @@ -98,30 +98,6 @@ function fdiv_qr_with_preinvn!(q::ZZRingElem, r::ZZRingElem, g::ZZRingElem, h::Z ccall((:fmpz_fdiv_qr_preinvn, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{fmpz_preinvn_struct}), q, r, g, h, hinv) end -function submul!(z::ZZRingElem, x::ZZRingElem, y::ZZRingElem) - ccall((:fmpz_submul, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), z, x, y) -end - - -################################################################################ -# -# Modular reduction with symmetric residue system -# -################################################################################ - -function mod_sym(a::ZZRingElem, b::ZZRingElem) - c = mod(a, b) - @assert c >= 0 - if b > 0 && 2 * c > b - return c - b - elseif b < 0 && 2 * c > -b - return c + b - else - return c - end -end - - ################################################################################ # # sunit group diff --git a/src/Misc/RelFiniteField.jl b/src/Misc/RelFiniteField.jl index d4833ad308..d06c37ce27 100644 --- a/src/Misc/RelFiniteField.jl +++ b/src/Misc/RelFiniteField.jl @@ -234,13 +234,6 @@ function (F::fpField)(a::RelFinFieldElem) return F(coeff(a, 0)) end -function (F::fpField)(a::fqPolyRepFieldElem) - for i = 1:degree(parent(a))-1 - @assert iszero(coeff(a, i)) - end - return F(coeff(a, 0)) -end - function (F::FpField)(a::RelFinFieldElem) for i = 1:degree(parent(a))-1 @assert iszero(coeff(a, i)) @@ -248,12 +241,6 @@ function (F::FpField)(a::RelFinFieldElem) return F(coeff(a, 0)) end -function (F::FpField)(a::FqPolyRepFieldElem) - for i = 1:degree(parent(a))-1 - @assert iszero(coeff(a, i)) - end - return F(coeff(a, 0)) -end ################################################################################ # @@ -765,13 +752,7 @@ function is_irreducible(f::PolyElem{T}) where T <: RelFinFieldElem return length(l.fac) == 1 end -function (Rx::fpPolyRing)(a::fqPolyRepFieldElem) - el = Rx() - for i = 0:degree(parent(a)) - setcoeff!(el, i, base_ring(Rx)(coeff(a, i))) - end - return el -end + function norm(f::PolyElem{fqPolyRepFieldElem}) Fx = parent(f) diff --git a/src/Misc/Series.jl b/src/Misc/Series.jl index ed35f3e874..6ed6ce2969 100644 --- a/src/Misc/Series.jl +++ b/src/Misc/Series.jl @@ -227,15 +227,6 @@ function Hecke.residue_field(S::SeriesRing{T}) where {T <: Nemo.RingElem} #darn return k, MapFromFunc(x -> coeff(x, 0), y -> set_precision(S(y), 1), S, k) end -#TODO: in Nemo, rename to setprecision -# fix/report series add for different length -function set_precision(a::SeriesElem, i::Int) - b = deepcopy(a) - set_precision!(b, i) - return b -end - - function rational_reconstruction(a::SeriesElem; parent::PolyRing = polynomial_ring(base_ring(a), cached = false)[1]) C = base_ring(a) Ct = parent diff --git a/src/Misc/acb_root_ctx.jl b/src/Misc/acb_root_ctx.jl index 3082de77a3..98c8221afd 100644 --- a/src/Misc/acb_root_ctx.jl +++ b/src/Misc/acb_root_ctx.jl @@ -285,10 +285,6 @@ function set!(z::arb, x::arb) return z end -function bits(x::acb) - return ccall((:acb_bits, libarb), Int, (Ref{Nemo.acb}, ), x) -end - function rel_error_bits(x::acb) return ccall((:acb_rel_error_bits, libarb), Int, (Ref{Nemo.acb}, ), x) end diff --git a/src/NumField/Elem.jl b/src/NumField/Elem.jl index 3a26c59881..36b65d7759 100644 --- a/src/NumField/Elem.jl +++ b/src/NumField/Elem.jl @@ -246,37 +246,6 @@ the minimal polynomial of $a$ over the rationals $\mathbf{Q}$. """ absolute_minpoly(::NumFieldElem) -################################################################################ -# -# Powering with ZZRingElem -# -################################################################################ - -function ^(x::NumFieldElem, y::ZZRingElem) - if fits(Int, y) - return x^Int(y) - end - - return _power(x, y) -end - -# We test once if it fits, otherwise we would have to check for every ^-call -function _power(x::NumFieldElem, y::ZZRingElem) - res = parent(x)() - if y < 0 - res = _power(inv(x), -y) - elseif y == 0 - res = parent(x)(1) - elseif y == 1 - res = deepcopy(x) - elseif mod(y, 2) == 0 - z = _power(x, div(y, 2)) - res = z*z - else - res = _power(x, y - 1) * x - end - return res -end ################################################################################ # diff --git a/src/NumField/NfAbs/Elem.jl b/src/NumField/NfAbs/Elem.jl index b76e5ba5eb..e81eb0985e 100644 --- a/src/NumField/NfAbs/Elem.jl +++ b/src/NumField/NfAbs/Elem.jl @@ -893,68 +893,6 @@ function rem(a::nf_elem, b::ZZRingElem) return c end -function mod_sym(a::nf_elem, b::ZZRingElem, b2::ZZRingElem) - return mod_sym(a, b) - return z -end - -function mod_sym(a::nf_elem, b::ZZRingElem) - c = deepcopy(a) - mod_sym!(c, b) - return c -end - -################################################################################ -# -# Conversion to zzModPolyRingElem and ZZModPolyRingElem -# -################################################################################ - -function nf_elem_to_nmod_poly!(r::zzModPolyRingElem, a::nf_elem, useden::Bool = true) - ccall((:nf_elem_get_nmod_poly_den, libantic), Nothing, - (Ref{zzModPolyRingElem}, Ref{nf_elem}, Ref{AnticNumberField}, Cint), - r, a, a.parent, Cint(useden)) - return nothing -end - -function nf_elem_to_gfp_poly!(r::fpPolyRingElem, a::nf_elem, useden::Bool = true) - ccall((:nf_elem_get_nmod_poly_den, libantic), Nothing, - (Ref{fpPolyRingElem}, Ref{nf_elem}, Ref{AnticNumberField}, Cint), - r, a, a.parent, Cint(useden)) - return nothing -end - -function (R::Nemo.zzModPolyRing)(a::nf_elem) - r = R() - nf_elem_to_nmod_poly!(r, a) - return r -end - -function (R::Nemo.fpPolyRing)(a::nf_elem) - r = R() - nf_elem_to_gfp_poly!(r, a) - return r -end - -function nf_elem_to_fmpz_mod_poly!(r::ZZModPolyRingElem, a::nf_elem, useden::Bool = true) - ccall((:nf_elem_get_fmpz_mod_poly_den, libantic), Nothing, - (Ref{ZZModPolyRingElem}, Ref{nf_elem}, Ref{AnticNumberField}, Cint, Ref{fmpz_mod_ctx_struct}), - r, a, a.parent, Cint(useden), r.parent.base_ring.ninv) - return nothing -end - -function nf_elem_to_gfp_fmpz_poly!(r::FpPolyRingElem, a::nf_elem, useden::Bool = true) - ccall((:nf_elem_get_fmpz_mod_poly_den, libantic), Nothing, - (Ref{FpPolyRingElem}, Ref{nf_elem}, Ref{AnticNumberField}, Cint, Ref{fmpz_mod_ctx_struct}), - r, a, a.parent, Cint(useden), r.parent.base_ring.ninv) - return nothing -end - -function (R::Nemo.ZZModPolyRing)(a::nf_elem) - r = R() - nf_elem_to_fmpz_mod_poly!(r, a) - return r -end ################################################################################ # diff --git a/src/NumField/NfAbs/MPolyGcd.jl b/src/NumField/NfAbs/MPolyGcd.jl index f5ebf6f90c..473298b898 100644 --- a/src/NumField/NfAbs/MPolyGcd.jl +++ b/src/NumField/NfAbs/MPolyGcd.jl @@ -544,12 +544,6 @@ function Hecke.modular_lift(g::Vector{T}, me::Hecke.modular_env) where T <: MPol end -function Hecke.mod_sym!(f::ZZPolyRingElem, p::ZZRingElem) - for i=0:degree(f) - setcoeff!(f, i, Hecke.mod_sym(coeff(f, i), p)) - end -end - #= import Base.//, Base.== diff --git a/src/NumField/NfAbs/NfAbs.jl b/src/NumField/NfAbs/NfAbs.jl index fb5a660819..c4ed6bc5e5 100644 --- a/src/NumField/NfAbs/NfAbs.jl +++ b/src/NumField/NfAbs/NfAbs.jl @@ -911,8 +911,6 @@ end # ################################################################################ -Nemo.is_cyclo_type(::NumField) = false - function force_coerce(a::NumField{T}, b::NumFieldElem, throw_error::Type{Val{S}} = Val{true}) where {T, S} if Nemo.is_cyclo_type(a) && Nemo.is_cyclo_type(parent(b)) return force_coerce_cyclo(a, b, throw_error)::elem_type(a) diff --git a/src/NumField/NfAbs/PolyFact.jl b/src/NumField/NfAbs/PolyFact.jl index a5a1a41fc8..dcda4b23fc 100644 --- a/src/NumField/NfAbs/PolyFact.jl +++ b/src/NumField/NfAbs/PolyFact.jl @@ -419,7 +419,6 @@ function zassenhaus(f::PolyElem{nf_elem}, P::NfOrdIdl; degset::Set{Int} = Set{In end ############################################### -Base.log2(a::ZZRingElem) = log2(BigInt(a)) # stupid: there has to be faster way #given the local factorisation in H, find the cld, the Coefficients of the Logarithmic #Derivative: a factor g of f is mapped to g'*f/g @@ -862,14 +861,6 @@ function van_hoeij(f::PolyElem{nf_elem}, P::NfOrdIdl; prec_scale = 1) end #the big while end -function Base.map!(f, M::ZZMatrix) - for i=1:nrows(M) - for j=1:ncols(M) - M[i,j] = f(M[i,j]) - end - end -end - #does not seem to be faster than the direct approach. (not modular) #Magma is faster, which seems to suggest the direct resultant is #even better (modular resultant) diff --git a/src/NumField/NfAbs/Simplify.jl b/src/NumField/NfAbs/Simplify.jl index a8346844c0..36448142df 100644 --- a/src/NumField/NfAbs/Simplify.jl +++ b/src/NumField/NfAbs/Simplify.jl @@ -253,7 +253,7 @@ function _block(a::nf_elem, R::Vector{fqPolyRepFieldElem}, ap::fqPolyRepPolyRing _R = Native.GF(Int(characteristic(base_ring(ap))), cached = false) _Ry, _ = polynomial_ring(_R, "y", cached = false) _tmp = _Ry() - nf_elem_to_gfp_poly!(_tmp, a, false) # ignore denominator + Nemo.nf_elem_to_gfp_poly!(_tmp, a, false) # ignore denominator set_length!(ap, length(_tmp)) for i in 0:(length(_tmp) - 1) setcoeff!(ap, i, base_ring(ap)(_get_coeff_raw(_tmp, i))) diff --git a/src/NumField/NfRel/NfRelNS.jl b/src/NumField/NfRel/NfRelNS.jl index 6327cf278e..68549bdd5c 100644 --- a/src/NumField/NfRel/NfRelNS.jl +++ b/src/NumField/NfRel/NfRelNS.jl @@ -380,29 +380,6 @@ function Nemo.degree(K::NfRelNS) return prod(Int[total_degree(x) for x=K.pol]) end -function (R::Generic.PolyRing{nf_elem})(f::Generic.MPoly) - if length(f)==0 - return R() - end - j = 1 - c = 0 - while j<= ngens(parent(f)) - if f.exps[j, 1] != 0 - if c==0 - c = j - else - error("poly is not univariate") - end - end - j += 1 - end - g = R() - for i=1:length(f) - setcoeff!(g, Int(f.exps[c, i]), f.coeffs[i]) - end - return g -end - function basis(K::NfRelNS) k = base_field(K) kxy = parent(K.pol[1]) diff --git a/src/NumField/NfRel/Simplify.jl b/src/NumField/NfRel/Simplify.jl index 7378b5542b..2baaa6cc71 100644 --- a/src/NumField/NfRel/Simplify.jl +++ b/src/NumField/NfRel/Simplify.jl @@ -66,7 +66,7 @@ function _is_primitive_via_block(a::NfRelElem{nf_elem}, rt::Dict{FqPolyRepFieldE for (r, vr) in rt coeffs = Vector{FqPolyRepFieldElem}(undef, degree(pol)+1) for i = 0:degree(pol) - nf_elem_to_gfp_fmpz_poly!(tmp, coeff(pol, i)) + Nemo.nf_elem_to_gfp_fmpz_poly!(tmp, coeff(pol, i)) coeffs[i+1] = evaluate(tmp, r) end g = Fx(coeffs) @@ -161,7 +161,7 @@ function _setup_block_system(Lrel::NfRel{nf_elem}) for r in rt_base_field coeff_gF = FqPolyRepFieldElem[] for i = 0:degree(g) - nf_elem_to_gfp_fmpz_poly!(tmp, coeff(g, i)) + Nemo.nf_elem_to_gfp_fmpz_poly!(tmp, coeff(g, i)) push!(coeff_gF, evaluate(tmp, r)) end gF = Fx(coeff_gF) @@ -287,7 +287,7 @@ function _setup_block_system(Lrel::NfRelNS{nf_elem}) g = to_univariate(Kx, f) coeff_gF = FqPolyRepFieldElem[] for i = 0:degree(g) - nf_elem_to_gfp_fmpz_poly!(tmp, coeff(g, i)) + Nemo.nf_elem_to_gfp_fmpz_poly!(tmp, coeff(g, i)) push!(coeff_gF, evaluate(tmp, r)) end gF = Fx(coeff_gF) @@ -354,7 +354,7 @@ function _is_primitive_via_block(a::NfRelNSElem{nf_elem}, rt::Dict{FqPolyRepFiel for (r, vr) in rt ctx = MPolyBuildCtx(Rxy) for (c, v) in zip(coefficients(pol), exponent_vectors(pol)) - nf_elem_to_gfp_fmpz_poly!(tmp, c) + Nemo.nf_elem_to_gfp_fmpz_poly!(tmp, c) push_term!(ctx, evaluate(tmp, r), v) end g = finish(ctx) diff --git a/src/NumField/QQ.jl b/src/NumField/QQ.jl index b51bac9b97..489e5cdd47 100644 --- a/src/NumField/QQ.jl +++ b/src/NumField/QQ.jl @@ -190,7 +190,7 @@ function signs(a::Union{QQFieldElem, ZZRingElem, FacElem{QQFieldElem}}, l::Vecto return Dict(inf => sign(a)) end -function is_positive(x::Union{QQFieldElem, ZZRingElem, FacElem{QQFieldElem}}, p::Union{PosInf, Vector{PosInf}}) +function is_positive(x::FacElem{QQFieldElem}, ::Union{PosInf, Vector{PosInf}}) return sign(x) == 1 end @@ -198,7 +198,7 @@ function is_totally_positive(x::Union{QQFieldElem, ZZRingElem, FacElem{QQFieldEl return sign(x) == 0 end -function is_negative(x::Union{QQFieldElem, ZZRingElem, FacElem{QQFieldElem}}, p::Union{PosInf, Vector{PosInf}}) +function is_negative(x::FacElem{QQFieldElem}, ::Union{PosInf, Vector{PosInf}}) return sign(x) == -1 end diff --git a/src/NumField/promote_rules.jl b/src/NumField/promote_rules.jl index 95042928ee..571b0318db 100644 --- a/src/NumField/promote_rules.jl +++ b/src/NumField/promote_rules.jl @@ -4,18 +4,6 @@ # ################################################################################ -AbstractAlgebra.promote_rule(::Type{S}, ::Type{ZZRingElem}) where S <: NumFieldElem = S - -AbstractAlgebra.promote_rule(::Type{ZZRingElem}, ::Type{S}) where S <: NumFieldElem = S - -AbstractAlgebra.promote_rule(::Type{S}, ::Type{QQFieldElem}) where S <: NumFieldElem = S - -AbstractAlgebra.promote_rule(::Type{QQFieldElem}, ::Type{S}) where S <: NumFieldElem = S - -AbstractAlgebra.promote_rule(::Type{T}, ::Type{S}) where {S <: NumFieldElem, T <: Integer} = S - -AbstractAlgebra.promote_rule(::Type{S}, ::Type{T}) where {S <: NumFieldElem, T <: Integer} = S - AbstractAlgebra.promote_rule(::Type{NfRelElem{nf_elem}}, ::Type{nf_elem}) = NfRelElem{nf_elem} AbstractAlgebra.promote_rule(::Type{NfRelNSElem{nf_elem}}, ::Type{nf_elem}) = NfRelNSElem{nf_elem} diff --git a/src/NumFieldOrd/NfOrd/Clgp/Saturate.jl b/src/NumFieldOrd/NfOrd/Clgp/Saturate.jl index 94079da9c5..c85bc77bce 100644 --- a/src/NumFieldOrd/NfOrd/Clgp/Saturate.jl +++ b/src/NumFieldOrd/NfOrd/Clgp/Saturate.jl @@ -259,7 +259,7 @@ function compute_candidates_for_saturate1(c::Hecke.ClassGrpCtx, p::Int, stable:: bad_prime = true break end - Hecke.nf_elem_to_gfp_poly!(t, k) + Nemo.nf_elem_to_gfp_poly!(t, k) #evaluations = evaluate(t, evaluateat[1:lfacts]) for j = 1:lfacts ev_t = evaluate(t, evaluateat[j])#evaluations[j] diff --git a/src/NumFieldOrd/NfOrd/Ideal/Ideal.jl b/src/NumFieldOrd/NfOrd/Ideal/Ideal.jl index 1164a611b3..d761b0c9e8 100644 --- a/src/NumFieldOrd/NfOrd/Ideal/Ideal.jl +++ b/src/NumFieldOrd/NfOrd/Ideal/Ideal.jl @@ -1020,9 +1020,6 @@ end # without 1st computing the complete inv # .../ enter rresx and rres! -function (A::Nemo.AnticNumberField)(a::Nemo.ZZPolyRingElem) - return A(FlintQQ["x"][1](a)) -end function _minmod(a::ZZRingElem, b::NfAbsOrdElem) return mod(denominator(inv(b.elem_in_nf), parent(b)), a) diff --git a/src/NumFieldOrd/NfOrd/Ideal/Valuation.jl b/src/NumFieldOrd/NfOrd/Ideal/Valuation.jl index 284c8fab42..07aa14d791 100644 --- a/src/NumFieldOrd/NfOrd/Ideal/Valuation.jl +++ b/src/NumFieldOrd/NfOrd/Ideal/Valuation.jl @@ -31,7 +31,7 @@ function val_func_no_index_small(p::NfOrdIdl) let h = h, g = g, P = P, uP = uP function vfunc(x::nf_elem, no::QQFieldElem = QQFieldElem(0)) d = denominator(x) - nf_elem_to_nmod_poly!(h, x, false) # ignores the denominator + Nemo.nf_elem_to_nmod_poly!(h, x, false) # ignores the denominator h = rem!(h, h, g) c = Nemo.coeff_raw(h, 0) v = c==0 ? typemax(Int) : valuation(c, uP) @@ -51,7 +51,7 @@ function val_func_no_index(p::NfOrdIdl) # TODO (GF): Change to proper GF to use nmod if possible Rx, g = polynomial_ring(Native.GF(P, cached=false), cached=false) Zx = polynomial_ring(FlintZZ, cached = false)[1] - nf_elem_to_gfp_fmpz_poly!(g, p.gen_two.elem_in_nf, false) + Nemo.nf_elem_to_gfp_fmpz_poly!(g, p.gen_two.elem_in_nf, false) f = Rx(K.pol) g = gcd(g, f) g = lift(Zx, g) @@ -64,7 +64,7 @@ function val_func_no_index(p::NfOrdIdl) let h = h, g = g, P = P function vfunc(x::nf_elem, no::QQFieldElem = QQFieldElem(0)) d = denominator(x) - nf_elem_to_fmpz_mod_poly!(h, x, false) # ignores the denominator + Nemo.nf_elem_to_fmpz_mod_poly!(h, x, false) # ignores the denominator h = rem!(h, h, g) _coeff_as_fmpz!(c, h, 0) v = iszero(c) ? 100 : valuation(c, P) diff --git a/src/QuadForm/Morphism.jl b/src/QuadForm/Morphism.jl index cc21afd4f0..b6fb20c95d 100644 --- a/src/QuadForm/Morphism.jl +++ b/src/QuadForm/Morphism.jl @@ -793,7 +793,6 @@ function _operate(point, A::ZZMatrix, V) return _operate(point, A, V, zero_matrix(FlintZZ, 1, ncols(A))) end -Base.replace!(::typeof(-), m::ZZMatrix) = -m function _operate(point, A, V, tmp) # V.v is a sorted list of length V.n of vectors From f56981d1e21704175c8f46803f2797df6da8ef23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Thu, 6 Jul 2023 12:24:10 +0200 Subject: [PATCH 11/11] Bump Nemo --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index c490489718..708d598d3d 100644 --- a/Project.toml +++ b/Project.toml @@ -22,7 +22,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" [compat] AbstractAlgebra = "^0.30.4" -Nemo = "^0.34.1" +Nemo = "^0.35.0" RandomExtensions = "0.4.3" Requires = "^0.5.2, 1.0" julia = "1.6"