Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasha Demin committed Feb 2, 2024
1 parent 2918781 commit 4db2ae8
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/arithmetic/QQ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ function select_arithmetic(
_,
_
) where {CoeffType1 <: CoeffQQ, CoeffType2 <: Coeff}
@assert iszero(characteristic)
@invariant iszero(characteristic)
ArithmeticQQ()
end
2 changes: 1 addition & 1 deletion src/arithmetic/Zp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/f4/basis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions src/f4/hashtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/f4/learn-apply.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/f4/linalg/backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/fglm/fglm_internal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/groebner/groebner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/groebner/normalform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/groebner/state.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/input-output/AbstractAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/input-output/input-output.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
27 changes: 0 additions & 27 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/monomials/exponentvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4db2ae8

Please sign in to comment.