Fwd: sage bug report

11 views
Skip to first unread message

William Stein

unread,
Dec 17, 2009, 3:53:25 AM12/17/09
to sage-support, Sebastian Pancratz
---------- Forwarded message ----------
From: Christian Szegedy <christia...@gmail.com>
Date: Thu, Dec 17, 2009 at 12:26 AM
Subject: sage bug report
To: William Stein <wst...@gmail.com>


Simple 8X8 matrix determinant computation makes sage hang:

load x.py
test()

On the other hand if m.det() is replaced m.inverse(), it runs through
in no time.

The determinant of the matrix is a sum of two monomials:
x1*x4*x5*x6 + x0*x2*x3*x7, but even the most primitive implementation
(summing all 8! permutations,most of them zero) should run through in
much less than minute.

--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

x.py

ma...@mendelu.cz

unread,
Dec 17, 2009, 5:40:32 AM12/17/09
to sage-support
perhaps problems expanding polynomials? even determinant of submatrix
(0,0,5,5) is suprisingly slow.

workaroud is to replace polynomials in your matrix by variables.

var('x0 x1 x2 x3 x4 x5 x6 x7 a1 a2 a3 a4 a5 a6 a7 b1 b2 b3 b4 b5 b6
b7')
m=matrix([[ 0, a1, a2, a3, a4, a5, a6, a7],
[b1, 0, x0, 0, 0, 0, 0, 0],
[b2, x0, 0, x6, 0, 0, 0, 0],
[b3, 0, x6, 0, x3, 0, 0, 0],
[b4, 0, 0, x3, 0, x4, 0, 0],
[b5, 0, 0, 0, x4, 0, x2, 0],
[b6, 0, 0, 0, 0, x2, 0, x1],
[b7, 0, 0, 0, 0, 0, x1, 0]])
m

m.det() gives answer immediatelly and you can substitute back for a's
and b's.

Do not know where the bug is, but hope that this helps.

Robert

> x.py
> < 1KZobrazitStáhnout

ma...@mendelu.cz

unread,
Dec 17, 2009, 5:48:29 AM12/17/09
to sage-support
And another observation:

maxima returns answer immediatelly (with a lag necessary to start
maxima)
m is the original matrix from x.py

sage: m._maxima_().determinant().expand().sage()
x0^2*x2^2*x3^2*x7^2 - 2*x0*x1*x2*x3*x4*x5*x6*x7 + x1^2*x4^2*x5^2*x6^2


Anyway, the answer is different from expected one. I do not konw which
one is correct.

Robert

luisfe

unread,
Dec 17, 2009, 10:01:54 AM12/17/09
to sage-support

On 17 dic, 11:48, "ma...@mendelu.cz" <ma...@mendelu.cz> wrote:
> And another observation:
>
> maxima returns answer immediatelly (with a lag necessary to start
> maxima)
> m is the original matrix from x.py
>
> sage: m._maxima_().determinant().expand().sage()
> x0^2*x2^2*x3^2*x7^2 - 2*x0*x1*x2*x3*x4*x5*x6*x7 + x1^2*x4^2*x5^2*x6^2
>
> Anyway, the answer is different from expected one. I do not konw which
> one is correct.
>
> Robert

If you perform the computations over the polynomial ring, sage gives
an answer

sage: R=FractionField(PolynomialRing(GF(2),",".join(map(genVar,range
(0,10)))))
sage: n=matrix(R.base(),m)
sage: n-m

[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]

sage: det(n)
x1^2*x4^2*x5^2*x6^2 + x0^2*x2^2*x3^2*x7^2

However, the result differs from the one in maxima and maple

About the result, the matrix is mod 2, so the expected anwer in maxima
equals the one computed above

sage: factor(det(n))
(x1*x4*x5*x6 + x0*x2*x3*x7)^2

Christian Szegedy

unread,
Dec 17, 2009, 2:39:49 PM12/17/09
to sage-s...@googlegroups.com
You evaluate it over ZZ[x1,...,xn] rather than GF(2)[x1,...,x4].

Anyways, it simply can't be *that* slow in any case: even: the
(theoretically ) maximum number of monoms that can be in any
expansion is less than a few thousands, so the upper limit
for a naively implemented Gaussian elimination is in the
order of seconds. However the matrix is even much simpler than
that and even the inverse is computed immediately.

> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org
>

Robert Bradshaw

unread,
Dec 17, 2009, 7:35:04 PM12/17/09
to sage-s...@googlegroups.com
The speed could be do to the inefficiency of fraction field arithmetic
over the polynomial ring. Ideally, we should have fraction-free
gaussian elimination. Also, easily invertable/small determinant may
actually be worse--as it could be creating a lot of large intermediate
values with non-trivial gcds that are all canceling out.

It would be interesting to profile and see what exactly is taking all
the time.

- Robert

Christian Szegedy

unread,
Dec 17, 2009, 8:58:39 PM12/17/09
to sage-s...@googlegroups.com
It is impossible to come up with any reasonable explanation for this
kind of slowdown. Even if you do extremely stupid things like
summing all permutations and simplifying the expression at the end, you
can't get that slow.

Additionally, you cansee that the inverse is computed readily. If you
look at the
entries of the inverse, you can see the determinant in almost every
nonzero entry as denominator (naturally...) It is completely implausible
that the inverse() function which essentially computes the determinant
(and a lot more) is magnitudes faster than computing the determinant
alone.

I have not seen a ticket opened for this issue, but I would strongly suggest
to open one (I don't have any account for the Trac, but would be happy to
get one.)

William Stein

unread,
Dec 17, 2009, 9:05:51 PM12/17/09
to sage-support
On Thu, Dec 17, 2009 at 5:58 PM, Christian Szegedy
<christia...@gmail.com> wrote:
> It is impossible to come up with any reasonable explanation for this
> kind of slowdown. Even if you do extremely stupid things like
> summing all permutations and simplifying the expression at the end, you
> can't get that slow.
>
> Additionally, you  cansee that the inverse is computed readily. If you
> look at the
> entries of the inverse, you can see the determinant in almost every
> nonzero entry as denominator (naturally...) It is completely implausible
> that the inverse() function which essentially computes the determinant
> (and a lot more) is magnitudes faster than computing the determinant
> alone.
>
> I have not seen a ticket opened for this issue, but I would strongly suggest
> to open one (I don't have any account for the Trac, but would be happy to
> get one.)

Wonderful. Please read http://wiki.sagemath.org/TracGuidelines and
email me offlist for your account. Thanks again for improving the
quality of Sage through your very valuable bug reports.

William

Robert Bradshaw

unread,
Dec 17, 2009, 9:35:36 PM12/17/09
to sage-s...@googlegroups.com
On Dec 17, 2009, at 5:58 PM, Christian Szegedy wrote:

> It is impossible to come up with any reasonable explanation for this
> kind of slowdown. Even if you do extremely stupid things like
> summing all permutations and simplifying the expression at the end,
> you
> can't get that slow.

No, but if someone tried to be clever, and messed up, you can.

> Additionally, you cansee that the inverse is computed readily. If you
> look at the
> entries of the inverse, you can see the determinant in almost every
> nonzero entry as denominator (naturally...) It is completely
> implausible
> that the inverse() function which essentially computes the determinant
> (and a lot more) is magnitudes faster than computing the determinant
> alone.
>
> I have not seen a ticket opened for this issue, but I would strongly
> suggest
> to open one (I don't have any account for the Trac, but would be
> happy to
> get one.)

That would be great. This intrigued me, so I hunted down where it's
hanging. It is trying to take a gcd in fraction field reduction. See http://sage.math.washington.edu/home/robertwb/det.sage
for the hanging example.

William Stein

unread,
Dec 17, 2009, 10:28:53 PM12/17/09
to Sebastian Pancratz, sage-support, christian.szegedy
On Thu, Dec 17, 2009 at 7:16 PM, Sebastian Pancratz
<sebastian...@hertford.ox.ac.uk> wrote:
> Dear William,
>
> This is just a very brief reply (before going to bed, it's past 3am here).  I've added the bug report as ticket #7730.

http://trac.sagemath.org/sage_trac/ticket/7730

> After I've had a quick look at the problem, it seems it's caused by the computation of the Hessenberg form.  In fact, supplying the choice "algorithm='df'" would allow the person who submitted the bug report to carry out the computation quickly.  Just in case that person is interested (I am not quite sure what the arrangement for replying to him is, in any case, I think for me it's more appropriate to reply to you):
>
>  Computation of det by minors: 2.15s
>  Computation of det using the division-free algorithm: 3.87microseconds
>  Computation of inverse: 71.6ms
>
> Oh, and the output in either case for the determinant is x1^2*x4^2*x5^2*x6^2 + x0^2*x2^2*x3^2*x7^2, which is different from the bug report.  (It looks like the powers of two were omitted in the bug report.)
>
> While I am not all that familiar with the Hessenberg form computation, I am happy to look at it in the next few days --- it seems something that is used that often shouldn't be broken.
>

Thanks! I think I implemented Hessenberg form long, long ago
following H. Cohen's book.... The generic algorithm is fine, so
something must go wrong/weird in the special case under consideration.

William

Reply all
Reply to author
Forward
0 new messages