@@ -567,6 +567,9 @@ Base.copy(s::SignChange) = s
567567Base. iszero(:: SignChange ) = false
568568MA. scaling_convert(:: Type , s:: SignChange ) = s
569569Base.:* (s:: SignChange , α:: Real ) = SignChange(s. sign * α, s. Δ)
570+ Base.:* (α:: Real , s:: SignChange ) = SignChange(α * s. sign, s. Δ)
571+ # Base.convert(::Type{SignChange{T}}, s::SignChange) where {T} = SignChange{T}(s.sign, s.Δ)
572+ # Base.:+(a::SignChange, b::SignChange) = convert(SignCount, a) + b
570573
571574struct SignCount
572575 unknown:: Int
@@ -586,6 +589,9 @@ function _sign(c::SignCount)
586589 return missing
587590 end
588591end
592+ function Base. show(io:: IO , c:: SignCount )
593+ return print(io, " [$(c. unknown) |$(c. positive) |$(c. negative) ]" )
594+ end
589595
590596function Base.:* (α, a:: SignCount )
591597 if α > 0
@@ -630,26 +636,16 @@ end
630636
631637Base. convert(:: Type{SignCount} , Δ:: SignChange ) = SignCount() + Δ
632638
633- function increase(cache, counter, generator_sign, monos, mult)
634- for a in monos
635- for b in monos
636- MA. operate_to!(
637- cache,
638- * ,
639- MB. algebra_element(mult),
640- MB. algebra_element(a),
641- MB. algebra_element(b),
642- )
643- MA. operate!(
644- SA. UnsafeAddMul(* ),
645- counter,
646- _term_constant_monomial(
647- SignChange((a != b) ? missing : generator_sign, 1 ),
648- mult,
649- ),
650- cache,
651- )
652- end
639+ struct SignGram{T,B}
640+ sign:: T
641+ basis:: B
642+ end
643+ SA. basis(g:: SignGram ) = g. basis
644+ function Base. getindex(g:: SignGram , i, j)
645+ if i == j
646+ return SignChange(g. sign, 1 )
647+ else
648+ return SignChange(missing , 1 )
653649 end
654650end
655651
@@ -715,38 +711,33 @@ function post_filter(
715711 _DictCoefficients(Dict{MP. monomial_type(typeof(poly)),SignCount}()),
716712 MB. implicit_basis(SA. basis(poly)),
717713 )
718- algebra = MB. algebra(MB. implicit_basis(SA. basis(poly)))
719- cache = zero(Float64, algebra)
720- cache3 = zero(SignCount, algebra)
721- cache4 = zero(SignCount, algebra)
714+ cache = zero(SignCount, MB. algebra(MB. implicit_basis(SA. basis(poly))))
715+ cache2 = zero(SignCount, MB. algebra(MB. implicit_basis(SA. basis(poly))))
722716 for (mono, v) in SA. nonzero_pairs(SA. coeffs(poly))
723717 MA. operate!(
724- SA. UnsafeAddMul( * ),
718+ SA. UnsafeAdd( ),
725719 counter,
726720 _term(SignChange(_sign(v), 1 ), SA. basis(poly)[mono]),
727721 )
728722 end
729723 for (mult, gram_monos) in zip(generators, multipliers_gram_monos)
730- for (mono, v) in SA. nonzero_pairs(SA. coeffs(mult))
731- increase(
732- cache,
733- counter,
734- - _sign(v),
735- gram_monos,
736- SA. basis(mult)[mono],
737- )
738- end
724+ MA. operate_to!(
725+ cache,
726+ + ,
727+ SA. QuadraticForm{SA. star}(SignGram(- 1 , gram_monos)),
728+ )
729+ MA. operate!(SA. UnsafeAddMul(* ), counter, mult, cache)
739730 end
740731 function decrease(sign, a, b, generator)
741732 MA. operate_to!(
742- cache3 ,
733+ cache ,
743734 * ,
744735 _term(SignChange(sign, - 1 ), a),
745736 MB. algebra_element(b),
746737 )
747- MA. operate_to!(cache4 , * , cache3 , generator)
748- MA. operate!(SA. UnsafeAddMul( * ), counter, cache4 )
749- for mono in SA. supp(cache4 )
738+ MA. operate_to!(cache2 , * , cache , generator)
739+ MA. operate!(SA. UnsafeAdd( ), counter, cache2 )
740+ for mono in SA. supp(cache2 )
750741 count = SA. coeffs(counter)[SA. basis(counter)[mono]]
751742 count_sign = _sign(count)
752743 # This means the `counter` has a sign and it didn't have a sign before
@@ -781,16 +772,16 @@ function post_filter(
781772 for i in eachindex(generators)
782773 for (j, mono) in enumerate(multipliers_gram_monos[i])
783774 MA. operate_to!(
784- cache3 ,
775+ cache ,
785776 * ,
786777 # Dummy coef to help convert to `SignCount` which is the `eltype` of `cache`
787778 _term(SignChange(1 , 1 ), mono),
788779 MB. algebra_element(mono),
789780 )
790781 # The `eltype` of `cache` is `SignCount`
791782 # so there is no risk of term cancellation
792- MA. operate_to!(cache4 , * , cache3 , generators[i])
793- for w in SA. supp(cache4 )
783+ MA. operate_to!(cache2 , * , cache , generators[i])
784+ for w in SA. supp(cache2 )
794785 if ismissing(_sign(SA. coeffs(counter)[SA. basis(counter)[w]]))
795786 push!(get!(back, w, Tuple{Int,Int}[]), (i, j))
796787 else
0 commit comments