On Wed, Jan 25, 2012 at 10:15 AM, javier <vengor...@gmail.com> wrote:
> Hi all!
>
> I noticed that doing
> A.is_singular()
> is the same thing as
> A.determinant() == 0
>
> I am not sure this is optimal, as we can know that A.det() != 0
> without having to compute it.
> I was wondering if it would be quicker to make A.is_singular()
> equivalent to
>
> if A.nrows() == A.ncols():
>    return A.rank() == A.ncols()
> else:
>    return False

I think is_singular should raise a ValueError if the input matrix is
not square.
To quote wikipedia: "A square matrix that is not invertible is called
singular or degenerate. A square matrix is singular if and only if its
determinant is 0. Singular matrices are rare in the sense that if you
pick a random square matrix, it will almost surely not be singular."

>
> The rationale is that we only need to know if the determinant is
> different from 0, not its precise value.
> I have done some tests and the timings suggest that computing the rank
> is a bit faster than computing the determinant:
>
> L = range(3000^2)
>
> A = Matrix(ZZ, 3000, L)
> time A.det()
> 0
> Time: CPU 4.16 s, Wall: 4.07 s
>
> A = Matrix(ZZ, 3000, L)
> time A.rank()
> 2
> Time: CPU 4.00 s, Wall: 3.92 s

That is probably a good idea over ZZ or QQ and probably many other
rings, especially since for a random matrix over ZZ or QQ, rank will
be full and Sage will quickly figure this out by reducing modulo one
small random prime.   So, even if the coefficients of A are enormous,
the rank approach will usually be very fast, whereas computing the
determinant will be slowed down by the size of the entries.

> Small improvement, but an improvement. Also, I observed that some
> matrix functions use hand-made caches. Should we change those to use
> @cahced_method instead, or is this done by some design reason?

There was no cached_method when I wrote most of the matrix code.
Also, I think decorators weren't supported in Cython then (?).

The matrix caching code is definitely more sophisticated than what
cached_method does though.  It has to behave differently for mutable
and immutable matrices.

>
> Cheers
> Javier
>
> --
> To post to this group, send an email to sage-devel@googlegroups.com
> To unsubscribe from this group, send an email to 
> sage-devel+unsubscr...@googlegroups.com
> For more options, visit this group at 
> http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org



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

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

Reply via email to