On Fri, Jul 14, 2023 at 08:29:37PM +0200, Ralf Hemmecke wrote:
> On 14.07.23 16:48, Waldek Hebisch wrote:
> > In my developement version I switched polynomial factorization
> > over prime fields to use new factorizer. That gives substantial
> > speedup. However, profiling showed that in my test case (which
> > was polynomial with 84 linear factors) abut 10% time was spent
> > in multiplying factors, of of which 8.4% went into comparing
> > exponents (POLYCAT-;smaller?)
>
> Just a side remark.
> Isn't the implementation of POLYCAT-;smaller? a bit weird if it is used in
> factorization? It basically, compares the leading coefficients.
> So we have...
>
> (1) -> S := SMP(Integer, Symbol)
>
> (31) -> px := x::S
>
> (31) x
> Type: SparseMultivariatePolynomial(Integer,Symbol)
> (32) -> py := y::S
>
> (32) y
> Type: SparseMultivariatePolynomial(Integer,Symbol)
> (33) -> smaller?(-py^100,px)
>
> (33) true
>
> Certainly not a problem for the definition of smaller?, as it just wants a
> linear order, but for factorization I would have expected that a polynomial
> with a smaller degree counts as smaller.
Well, SparseMultivariatePolynomial uses lexicographic order. The
same order is used for Groebner bases. Current answer to your
wish for degree order is "use domain with degree order like
HomogeneousDistributedMultivariatePolynomial". I must say that
I am not fully satisfied with this answer, but lexicographic
order as _default_ order for SparseMultivariatePolynomial
makes a lot of sense. Including coefficients is necessary to
have ordered ring. Note that over fields our univariate factors
are monic. Over integers we normalize factors so that leading
coefficient is positive. So in both cases bigger degree will
win.
> Sorry, I haven't looked into the factorization code to check why such a
> linear order makes sense. In fact, I wonder why the factorizer uses the
> linear order by smaller? at all. Doesn´t Comparable say that it is just an
> arbitrary but total order. I wouldn´t be surprised if I override smaller? in
> SMP by some other crazy order that the factorization might go wrong. Wild
> speculation without looking at the code, of course, but I understood that it
> makes sense to introduce Comparable for a printing order, but I am not sure
> about it's usefulness in other algorithms, that want a few more properties
> than just totality in order to get some complexity measure.
Well, main reason for order is to get repeatable results when
printing. But if you have ordered lists you can find duplicates
using linear scan. Without extra structure like order or hash
function elimination of duplicates is quadratic. When all
factors are prime and unitCanonical holds (so factors are
uniquely represented) gcd is equivalent to finding duplicate
factors and adding exponents, multiplication is eqivalent
to merge of sorted lists with removal of duplicates.
Unfortunatly conditions above are quite restrictive, so in many
cases we need to fall back to quadratic version.
> > It is not entirely clear to me what to do with Factored.
> > Ordering can be used to speed up computations only when
> > we have true order, otherwise we may get wrong results.
>
> In what sense does the order influence correctness?
See above: one may miss fact that to factors are equvalent
and count them as separate factors giving clearly wrong gcd
and slightly wrong result of multiplication.
> > Additionally, ordering factors gives speedup only when
> > all or most factors is prime, for other factors we need
> > to use quadratic method anyway. My conclusion is that
> > we can not have really efficient operations with
> > generality offered by Factored, so probably should
> > not care too much about speed of Factored.
>
> Oh, do you mean the sx := sort!(LispLessP, x) in makeFR $ Factored(R) ?
> Why should one care about the order in the representation of Factored?
Well, we want repeateble results, so evan crazy order is better
than no order. Unfortunately, GGREATERP which is used as
fallback may give different results on different runs.
Currently our factorizers use (pseudo)random numbers, so
without extra ordering effort order of factors will vary.
Over resonable finite fields we have Comparable and can
order. But we also use random evaluation points for
multivariate polynomials, here we need some ordering
effort. Over sufficiently complicated domains our
ordering efforts will fail, so probably we should
concentrate on resonable order for simple domains.
--
Waldek Hebisch