Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise an overflow error if the exponent of a multivariate polynomial flows over #11856

Closed
simon-king-jena opened this issue Sep 27, 2011 · 37 comments

Comments

@simon-king-jena
Copy link
Member

The following happens at least since sage-4.6.2 and was detected in #4539 by a new doctest:

sage: P.<x,y> = QQ[]
sage: y^2^30
y^1073741824
sage: P.<x,y,z> = QQ[]
sage: y^2^30
0

According to Hans, the maximal exponent of a variable in a monomial does depend on the number of variables in the ring. There is no function that returns that maximal exponent, but it is stored in the bitmask attribute of a ring. The Singular interpreter actually only tests whether the total degree is below what is provided by bitmask; in theory, any exponent (not only the totall degree) can go up to that bound.

Here is the corresponding code in Singular's iparith.cc:

static BOOLEAN jjTIMES_P(leftv res, leftv u, leftv v)
{
  poly a;
  poly b;
  int dummy;
  if (v->next==NULL)
  {
    a=(poly)u->CopyD(POLY_CMD); // works also for VECTOR_CMD
    if (u->next==NULL)
    {
      b=(poly)v->CopyD(POLY_CMD); // works also for VECTOR_CMD
      if ((a!=NULL) && (b!=NULL)
      && (pTotaldegree(a)+pTotaldegree(b)>si_max((long)rVar(currRing),(long)currRing->bitmask)))
      {
        Werror("OVERFLOW in mult(d=%ld, d=%ld, max=%ld)",
          pTotaldegree(a),pTotaldegree(b),currRing->bitmask);
        pDelete(&a);
        pDelete(&b);
        return TRUE;
...

Apply

Depends on #10903

CC: @malb @burcin @alexanderdreyer @sagetrac-jakobkroeker

Component: commutative algebra

Keywords: exponent overflow

Author: Simon King

Reviewer: Martin Albrecht, Alexander Dreyer

Merged: sage-4.7.2.alpha4

Issue created by migration from https://trac.sagemath.org/ticket/11856

@simon-king-jena
Copy link
Member Author

Author: Simon King

@simon-king-jena
Copy link
Member Author

comment:1

The attached patch seems to solve the problem. It wraps the attribute bitmask of Singular's rings. Moreover, it detects exponent overflow in the same way as Singular does (see ticket description). rVar is in fact a macro that simply returns the ring->N attribute.

With the patch, I get (as a new doctest):

sage: P.<x,y> = QQ[]
sage: y^2^30
y^1073741824
sage: P.<x,y,z> = QQ[]
sage: y^2^30
Traceback (most recent call last):
...
OverflowError: Exponent overflow (1073741824).

I did not run the doc tests yet, but I think a reviewer can already have a look on it.

I wonder, though, whether the speed will be fine: I use the max function with the arguments being an unsigned long and a long. I can only hope that it is fast enough in Cython.

@malb
Copy link
Member

malb commented Sep 27, 2011

comment:2

It seems max_exponent_size should be removed since it's not needed any more?

@simon-king-jena
Copy link
Member Author

comment:3

I did some tests with Cython functions either testing whether (for cdefined a,b,c) we have a>max(b,c) or a>b and a>c. The execution time was about the same. So, the patch should be fine as it is (modulo doctests, of course).

I don't know whether max_exponent_size is used somewhere else.

@simon-king-jena
Copy link
Member Author

comment:4

Very tricky. Singular correctly computes x<sup>2</sup>30*x<sup>2</sup>30, but it does not print it correctly, because when printing the exponent then it is converted in a different format:

sage: P.<x,y> = QQ[]
sage: (x^2^30*x^2^30)
x^-2147483648
sage: (x^2^30*x^2^30).degree()
2147483648

So, internally the degree is correct.

Even more tricky: In the case of x<sup>2</sup>31, Singular believes that the result is zero. The degree() method first tests whether it is (believed to be) zero, and acts accordingly:

sage: (x^2^31).degree()
-1
sage: (x^2^31)
0

That happens when I remove the max_exponent_size. I suppose I shouldd revert that removal...

@simon-king-jena
Copy link
Member Author

comment:5

The latest patch version checks both whether the exponent exceeds max_exponent_size (which avoids some bugs that occur in Singular) and whether it is illegal in Singular (which avoids the bugs in Sage that led to the creation of this ticket).

We now have

        sage: x^2^30*x^2^30
        Traceback (most recent call last):
        ...
        OverflowError: Exponent overflow (2147483648).

I wonder whether the test, that became more expensive, led to an inacceptable speed regression. That should be investigated.

The tests in sage/rings/polynomial pass for me. I don't know about the rest. Needs review!

@malb
Copy link
Member

malb commented Sep 27, 2011

comment:6

Code looks okay. So modulo the possible speed regression and doctests passing this should get a positive review.

@simon-king-jena
Copy link
Member Author

comment:7

Here are some timings.

With the patch:

sage: P.<x,y,z> = QQ[]
sage: p = x+y
sage: %timeit q=p^20
625 loops, best of 3: 6.93 µs per loop
sage: p = x+y+z
sage: %timeit q=p^25
625 loops, best of 3: 418 µs per loop
sage: %timeit q=x^2^10
625 loops, best of 3: 2.1 µs per loop

Without the patch:

sage: P.<x,y,z> = QQ[]
sage: p = x+y
sage: %timeit q=p^20
625 loops, best of 3: 7.08 µs per loop
sage: p = x+y+z
sage: %timeit q=p^25
625 loops, best of 3: 419 µs per loop
sage: %timeit q=x^2^10
625 loops, best of 3: 2.14 µs per loop

These are only few data points, but they seem to show that the overhead is sufficiently small.

@loefflerd
Copy link
Mannequin

loefflerd mannequin commented Sep 28, 2011

Reviewer: Martin Albrecht

@simon-king-jena
Copy link
Member Author

comment:10

Alexander found (while reviewing #4539) that the patch from here results in two doctest errors on 32-bit machines (reasonable, since an overflow error will occur earlier on 32 bit than on 64 bit).

He provided a fix at #4539, but actually I think it should be included here. What do people think?

@simon-king-jena
Copy link
Member Author

Work Issues: 32bit doctests

@alexanderdreyer
Copy link
Mannequin

alexanderdreyer mannequin commented Oct 1, 2011

comment:12

Replying to @simon-king-jena:

He provided a fix at #4539, but actually I think it should be included here. What do people think?

Indeed, I also had to apply https://github.com/sagemath/sage-prod/files/10642668/trac4539_fix_docs_32bit.patch.gz here for 32-bit systems. So, I think, it is necessary.

BTW: it seems that this ticket depends on #11115.

@simon-king-jena
Copy link
Member Author

comment:13

Replying to @alexanderdreyer:

Replying to @simon-king-jena:
BTW: it seems that this ticket depends on #11115.

What ticket are you referring to by "this"? If "this" is #4539: #4539 depends on #11068, which depends on #11115. But if "this" is #11856 then I don't see a dependency. What do you mean? Is there fuzz when applying my or your patch? Does it not work without #11115?

@simon-king-jena
Copy link
Member Author

comment:14

I just tested: attachment: trac11856_exponent_overflow.patch followed by trac4539_fix_docs_32bit.patch cleanly applies to sage-4.7.2.alpha3-prerelease.

Thus, if Alexander finds that trac4539_fix_docs_32bit.patch fixes the problem on 32-bit (I can not test it, myself), then I suggest that Alexander's patch should be moved from #4539 to here and turned into a reviewer patch; and if Alexander says that it is fixed, then we should return to a positive review.

@alexanderdreyer
Copy link
Mannequin

alexanderdreyer mannequin commented Oct 2, 2011

comment:15

Indeed, I thought that #11856 would depend on #11115. But it turned out, that my Sage becomes corrupted when popping the patches of. #11115. So starting with a brand new clone of devel/sage-main does the trick.

So I can confirm, https://github.com/sagemath/sage-prod/files/10642668/trac4539_fix_docs_32bit.patch.gz fixes the problem on 23 Bit platforms.

@alexanderdreyer
Copy link
Mannequin

alexanderdreyer mannequin commented Oct 2, 2011

Changed reviewer from Martin Albrecht to Martin Albrecht, Alexander Dreyer

@simon-king-jena

This comment has been minimized.

@simon-king-jena
Copy link
Member Author

comment:17

Let's try to sort things out:

The patch that Alexander posted at #4539 should better be a reviewer patch here. Thus, I copied his patch, added a commit message, and posted it here under a new name reflecting the ticket number. The positive review can be kept (I hope we agree on that), and:

Apply trac11856_exponent_overflow.patch trac11856_fix_docs_32bit.patch

@jdemeyer
Copy link

jdemeyer commented Oct 4, 2011

Changed work issues from 32bit doctests to none

@jdemeyer

This comment has been minimized.

@jdemeyer
Copy link

jdemeyer commented Oct 4, 2011

comment:19

This seems to conflict with #11339.

@jdemeyer
Copy link

jdemeyer commented Oct 4, 2011

Work Issues: Conflict with #11339

@simon-king-jena
Copy link
Member Author

comment:20

The first patch has been rebased on top of #11339. The patch from #11339 did some cosmetic changes, such as replacing p_Delete(&p,r) by p_Delete(&p, r). Therefore, one of the hunks from the first patch here did not apply.

Since it is a very simple editorial change, without changing the code, I am directly reinstating the positive review.

Apply trac11856_exponent_overflow.patch trac11856_fix_docs_32bit.patch

@simon-king-jena
Copy link
Member Author

Dependencies: #11339

@simon-king-jena
Copy link
Member Author

Changed work issues from Conflict with #11339 to none

@simon-king-jena
Copy link
Member Author

comment:23

I had rebased my patch against #11339, but it turns out that #11339 needs work -- it creates some doctest errors with sage-4.7.2.alpha3-prerelease. So, I guess it would be better to return to the original patch version, which unfortunately is now lost.

@simon-king-jena
Copy link
Member Author

Changed dependencies from #11339 to none

@simon-king-jena
Copy link
Member Author

Work Issues: Rebase without #11339

@simon-king-jena
Copy link
Member Author

Changed work issues from Rebase without #11339 to none

@simon-king-jena
Copy link
Member Author

comment:24

Since this ticket fixes a critical bug, I think it would be good to merge it in sage-4.7.2, rather than waiting for 4.7.3. Since #11339 seems to need work, I returned to the original first patch and hope that it is ok to renew Martin's and Alexander's positive review.

Apply trac11856_exponent_overflow.patch trac11856_fix_docs_32bit.patch

@simon-king-jena
Copy link
Member Author

Mimmick Singular's exponent overflow check but still avoids some bugs that Singular provides

@simon-king-jena
Copy link
Member Author

Dependencies: #10903

@simon-king-jena
Copy link
Member Author

comment:25

Attachment: trac11856_exponent_overflow.patch.gz

It seems that I should not have rebased my ticket wrt #11339, but wrt #10903 (which in turn has #11339 as a dependency).

Namely, #11339 only works together with #10903.

Solution: Both #11339 and #10903 have a positive review. Hence, it is fine to make #10903 (and thus #11339 by transitivity) a dependency.

Running doctests now, again.

Apply trac11856_exponent_overflow.patch trac11856_fix_docs_32bit.patch

@simon-king-jena
Copy link
Member Author

comment:26

FWIW: The doctests are fine. So, it is justified to keep the positive review of Martin and Alexander.

@jdemeyer
Copy link

jdemeyer commented Oct 7, 2011

Merged: sage-4.7.2.alpha4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants