From 4db2ae8df9b51cd540da58a55c6fd2d99a5857b8 Mon Sep 17 00:00:00 2001 From: Sasha Demin Date: Fri, 2 Feb 2024 07:46:54 +0100 Subject: [PATCH] cleanup --- src/arithmetic/QQ.jl | 2 +- src/arithmetic/Zp.jl | 2 +- src/f4/basis.jl | 4 ++-- src/f4/hashtable.jl | 17 ++++++++--------- src/f4/learn-apply.jl | 6 +++--- src/f4/linalg/backend.jl | 2 +- src/fglm/fglm_internal.jl | 2 +- src/groebner/groebner.jl | 2 +- src/groebner/normalform.jl | 4 ++-- src/groebner/state.jl | 4 ++-- src/input-output/AbstractAlgebra.jl | 2 +- src/input-output/input-output.jl | 8 ++++---- src/interface.jl | 27 --------------------------- src/monomials/exponentvector.jl | 2 +- 14 files changed, 28 insertions(+), 56 deletions(-) diff --git a/src/arithmetic/QQ.jl b/src/arithmetic/QQ.jl index b5b930a7..81e03f58 100644 --- a/src/arithmetic/QQ.jl +++ b/src/arithmetic/QQ.jl @@ -21,6 +21,6 @@ function select_arithmetic( _, _ ) where {CoeffType1 <: CoeffQQ, CoeffType2 <: Coeff} - @assert iszero(characteristic) + @invariant iszero(characteristic) ArithmeticQQ() end diff --git a/src/arithmetic/Zp.jl b/src/arithmetic/Zp.jl index e636d233..304d51c6 100644 --- a/src/arithmetic/Zp.jl +++ b/src/arithmetic/Zp.jl @@ -378,7 +378,7 @@ function select_arithmetic( } # We guarantee that the characteristic is representable by CoeffType. # Maybe change the type of characteristic to CoeffType? - @assert characteristic <= typemax(CoeffType) + @invariant characteristic <= typemax(CoeffType) # The type that would act as an accumulator. Usually, this type should be a # bit wider than CoeffType diff --git a/src/f4/basis.jl b/src/f4/basis.jl index 82710efe..34fd2c8e 100644 --- a/src/f4/basis.jl +++ b/src/f4/basis.jl @@ -551,7 +551,7 @@ function basis_mark_redundant_elements!(basis::Basis) end end basis.nnonredundant = j - 1 - @assert basis.nprocessed == basis.nfilled + @invariant basis.nprocessed == basis.nfilled basis end @@ -630,7 +630,7 @@ function insert_lcms_in_basis_hashtable!( ps = pairset.pairs mod = MonomHash(ht.size - 1) - @assert ispow2(mod + 1) + @invariant ispow2(mod + 1) m = ifirst l = 1 diff --git a/src/f4/hashtable.jl b/src/f4/hashtable.jl index 7a2a54d0..871cb49a 100644 --- a/src/f4/hashtable.jl +++ b/src/f4/hashtable.jl @@ -115,7 +115,7 @@ function hashtable_initialize( # exponents[1:load] covers all stored exponents # , also exponents[1] is [0, 0, ..., 0] by default load = 1 - @assert initial_size > 1 + @invariant initial_size > 1 size = initial_size # exponents array starts from index offset, @@ -127,7 +127,6 @@ function hashtable_initialize( # use_divmask = true dmbits = 8 * sizeof(DivisionMask) compress_divmask = nvars > dmbits - @assert dmbits == 32 @log level = -5 "Using division masks: $use_divmask. Using $dmbits bits. Compressed: $compress_divmask" ndivbits = div(dmbits, nvars) # division mask stores at least 1 bit @@ -194,7 +193,7 @@ end @timeit function hashtable_initialize_secondary(ht::MonomialHashtable{M}) where {M <: Monom} # 2^6 seems to be the best out of 2^5, 2^6, 2^7 initial_size = 2^6 - @assert initial_size > 1 + @invariant initial_size > 1 exponents = Vector{M}(undef, initial_size) hashdata = Vector{Hashvalue}(undef, initial_size) @@ -241,7 +240,7 @@ function hashtable_reinitialize!(ht::MonomialHashtable{M}) where {M} @invariant !ht.frozen initial_size = 2^6 - @assert initial_size > 1 + @invariant initial_size > 1 # Reinitialize counters ht.load = 1 @@ -458,12 +457,12 @@ function hashtable_fill_divmasks!(ht::MonomialHashtable) vars_per_bit = div(ht.nvars, 8 * sizeof(DivisionMask)) vars_covered = vars_per_bit * 8 * sizeof(DivisionMask) if vars_covered != ht.nvars - @assert vars_covered < ht.nvars + @invariant vars_covered < ht.nvars vars_per_bit += 1 end - @assert vars_per_bit > 1 - @assert ht.ndivbits == 1 - @assert ndivvars == length(ht.divmap) + @invariant vars_per_bit > 1 + @invariant ht.ndivbits == 1 + @invariant ndivvars == length(ht.divmap) ctr = 1 @inbounds for i in 1:ndivvars if ht.nvars - ctr + 1 <= (ndivvars - i + 1) * (vars_per_bit - 1) @@ -472,7 +471,7 @@ function hashtable_fill_divmasks!(ht::MonomialHashtable) ht.divmap[i] = vars_per_bit ctr += vars_per_bit end - @assert sum(ht.divmap) == ht.nvars + @invariant sum(ht.divmap) == ht.nvars end @inbounds for vidx in (ht.offset):(ht.load) diff --git a/src/f4/learn-apply.jl b/src/f4/learn-apply.jl index 9f05b9fa..a3359caa 100644 --- a/src/f4/learn-apply.jl +++ b/src/f4/learn-apply.jl @@ -171,8 +171,8 @@ end @invariant basis_well_formed(:input_f4_learn!, ring, basis, hashtable) # @invariant pairset_well_formed(:input_f4!, pairset, basis, ht) - @assert basis == trace.gb_basis - @assert params.reduced === true + @invariant basis == trace.gb_basis + @invariant params.reduced @log level = -3 "Entering F4 Learn phase." basis_normalize!(basis, params.arithmetic) @@ -545,7 +545,7 @@ end params::AlgorithmParameters ) where {C <: Coeff} @invariant basis_well_formed(:input_f4_apply!, ring, basis, trace.hashtable) - @assert params.reduced == true + @invariant params.reduced basis_normalize!(basis, params.arithmetic) diff --git a/src/f4/linalg/backend.jl b/src/f4/linalg/backend.jl index 78cdc4c4..8f60ef13 100644 --- a/src/f4/linalg/backend.jl +++ b/src/f4/linalg/backend.jl @@ -158,7 +158,7 @@ end # lower part of matrix sparse_row_coeffs = matrix.some_coeffs[matrix.lower_to_coeffs[abs_column_idx]] end - @assert length(sparse_row_support) == length(sparse_row_coeffs) + @invariant length(sparse_row_support) == length(sparse_row_coeffs) # Load the row into a dense array linalg_load_sparse_row!(row, sparse_row_support, sparse_row_coeffs) diff --git a/src/fglm/fglm_internal.jl b/src/fglm/fglm_internal.jl index b7827eae..3cf87c1a 100644 --- a/src/fglm/fglm_internal.jl +++ b/src/fglm/fglm_internal.jl @@ -51,7 +51,7 @@ function enumerator_produce_next_monomials!( end function enumerator_next_monomial!(m::MonomialEnumerator) - @assert m.load > 0 + @invariant m.load > 0 monom = m.monoms[m.load] m.load -= 1 monom diff --git a/src/groebner/groebner.jl b/src/groebner/groebner.jl index 88d22aba..44df3a8b 100644 --- a/src/groebner/groebner.jl +++ b/src/groebner/groebner.jl @@ -556,7 +556,7 @@ function _groebner_learn_and_apply_threaded( while !correct_basis @log level = -2 "Iteration # $iters of modular Groebner, batchsize: $batchsize" - @assert iszero(batchsize % 4) + @invariant iszero(batchsize % 4) threadbuf_primes = map(_ -> Int32(next_lucky_prime!(luckyprimes)), 1:batchsize) for i in 1:nthreads() diff --git a/src/groebner/normalform.jl b/src/groebner/normalform.jl index b25dfa26..c57c80a8 100644 --- a/src/groebner/normalform.jl +++ b/src/groebner/normalform.jl @@ -31,7 +31,7 @@ function _normalform0(polynomials, to_be_reduced, kws::KeywordsHandler) coeffs_to_be_reduced_nonzero, params ) - @assert ring.nvars == ring_to_be_reduced.nvars && ring.ch == ring_to_be_reduced.ch + @invariant ring.nvars == ring_to_be_reduced.nvars && ring.ch == ring_to_be_reduced.ch if kws.check @log level = -2 "As `check=true` was provided, checking that the given input is indeed a Groebner basis" if !_isgroebner1(ring, monoms, coeffs, params) @@ -45,7 +45,7 @@ function _normalform0(polynomials, to_be_reduced, kws::KeywordsHandler) Finalized polynomial rings: Basis: $ring To be reduced: $ring_""" - @assert ring.nvars == ring_.nvars && ring.ch == ring_.ch && isequal(ring.ord, ring_.ord) + @invariant ring.nvars == ring_.nvars && ring.ch == ring_.ch && isequal(ring.ord, ring_.ord) monoms_reduced, coeffs_reduced = _normalform1( ring, monoms, diff --git a/src/groebner/state.jl b/src/groebner/state.jl index 15e24351..edc4162e 100644 --- a/src/groebner/state.jl +++ b/src/groebner/state.jl @@ -524,7 +524,7 @@ end prev_index = state.prev_index n = length(lucky.primes) - prev_index - @assert n > 0 + @invariant n > 0 if n == 1 @log level = -2 "Since there is only 1 new modulo, using incremental CRT" partial_incremental_crt_reconstruct!(state, lucky, indices_selection) @@ -533,7 +533,7 @@ end @log level = -2 "Using partial simultaneous CRT on range $(prev_index + 1)..$(length(lucky.primes))" - @assert n > 1 + @invariant n > 1 @inbounds for i in 1:length(indices_selection) Base.GMP.MPZ.set!(selected_prev_coeffs_zz[i], selected_coeffs_zz[i]) end diff --git a/src/input-output/AbstractAlgebra.jl b/src/input-output/AbstractAlgebra.jl index cb8fd77e..e8f842c5 100644 --- a/src/input-output/AbstractAlgebra.jl +++ b/src/input-output/AbstractAlgebra.jl @@ -494,7 +494,7 @@ function _io_extract_coeffs_raw_batched!( end batch = map(polys -> filter(!iszero, polys), batch) - @assert length(unique(length, batch)) == 1 + @invariant length(unique(length, batch)) == 1 @log level = -2 """ Permuting input terms: $permute_input_terms diff --git a/src/input-output/input-output.jl b/src/input-output/input-output.jl index cdd1c44b..09fff5b7 100644 --- a/src/input-output/input-output.jl +++ b/src/input-output/input-output.jl @@ -320,10 +320,10 @@ function io_extract_polys( ) where {T} coeffs = extract_coeffs(representation, ring, polynomials) reversed_order, var_to_index, monoms = extract_monoms(representation, ring, polynomials) - @assert length(coeffs) == length(monoms) + @invariant length(coeffs) == length(monoms) if reversed_order for i in 1:length(coeffs) - @assert length(coeffs[i]) == length(monoms[i]) + @invariant length(coeffs[i]) == length(monoms[i]) reverse!(coeffs[i]) reverse!(monoms[i]) end @@ -336,10 +336,10 @@ function io_remove_zeros_from_input!( monoms::Vector{Vector{M}}, coeffs::Vector{Vector{T}} ) where {M, T} - @assert length(monoms) == length(coeffs) + @invariant length(monoms) == length(coeffs) filter!(!iszero_coeffs, coeffs) filter!(!iszero_monoms, monoms) - @assert length(monoms) == length(coeffs) + @invariant length(monoms) == length(coeffs) iszerobasis = isempty(monoms) @log level = -7 "After removing zero polynomials from input:" monoms coeffs iszerobasis diff --git a/src/interface.jl b/src/interface.jl index 7ac1cd54..41487a07 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -497,30 +497,3 @@ function fglm( result end - -""" - fglm(basis, ordering_from, ordering_to; options...) - -Converts a Groebner basis from one monomial ordering to another. -""" -function fglm_residuals_in_batch( - basis::AbstractVector, - ordering_from::AbstractMonomialOrdering, - ordering_to::AbstractMonomialOrdering; - options... -) - Base.require_one_based_indexing(basis) - - keywords = KeywordsHandler(:fglm, options) - - logging_setup(keywords) - statistics_setup(keywords) - - result = - _fglm_residuals_in_batch(basis, ordering_from, ordering_to, keywords)::typeof(basis) - - performance_counters_print(keywords) - statistics_print(keywords) - - result -end diff --git a/src/monomials/exponentvector.jl b/src/monomials/exponentvector.jl index 2876710e..9aa1ccb5 100644 --- a/src/monomials/exponentvector.jl +++ b/src/monomials/exponentvector.jl @@ -54,7 +54,7 @@ end # Returns a vector of variable degrees that correspond to the monomial `pv`. function monom_to_vector!(tmp::Vector{M}, pv::ExponentVector{T}) where {M, T} - @assert length(tmp) == length(pv) - 1 + @invariant length(tmp) == length(pv) - 1 @inbounds tmp[1:end] = pv[2:end] tmp end