On Sat, Feb 7, 2009 at 2:53 PM, Florent Hivert
<florent.hiv...@univ-rouen.fr> wrote:
>
>> For trying to invert a non-square matrix, I think the error should be
>> a ValueError, since you're inputing an improper value so that the
>> operation is meaningless.
>>
>> >From http://docs.python.org/library/exceptions.html
>>
>> exception ValueError
>>     Raised when a built-in operation or function receives an argument
>> that has the right type but an inappropriate value, and the situation
>> is not described by a more precise exception such as IndexError.
>
> I was specifically speaking about non square matrices. Note that I'm pretty
> sure that for some kind of square matrices, you'll get an error different that
> DivisionbyZero. Here is an example:
>
> sage: x = Mat(CDF,1,1)(0)
> sage: x
> [0]
> sage: x.__class__
> <type 'sage.matrix.matrix_complex_double_dense.Matrix_complex_double_dense'>
> sage: M.inverse()
> ---------------------------------------------------------------------------
> LinAlgError                               Traceback (most recent call last)
> [...]
> LinAlgError: singular matrix
>
> This makes also sense, and seems even better to me.

Maybe.  I'm not convinced it's better.  It's usually good when people
write code, they do

try:
    ...
except (Specific, Tuple, Of, Exceptions):
    code

and *never*

try:
    ...
except:
    ...

If it is tricky to figure out what sort of exceptions might be raised
-- or even to remember what they are, or to import them -- then that
increases the chances people will do the latter.

Another problem with

> LinAlgError                               Traceback (most recent call last)
> [...]
> LinAlgError: singular matrix

is that it suggests there is exactly one exception for all types of
errors involving linear algebra.   But I can at least see situations
where ValueError and ZeroDivisionError at least both make sense, so
doing the above would presumably merge them.

So I'm definitely not a priori convinced that LinAlgError is
necessarily better.

Note that LinAlgError is not something defined by Sage, and there is
no code in the Sage library that throws that exception.  It's
something from the Numpy library.   It appears four places in the sage
code:

-------------------------
D-128-208-135-199:matrix wstein$ sage -grep linalgerror
matrix/matrix_double_dense.pyx:#            LinAlgError: singular matrix
matrix/matrix_double_dense.pyx:#            LinAlgError: singular matrix
rings/polynomial/polynomial_element.pyx:                from
numpy.linalg.linalg import LinAlgError
rings/polynomial/polynomial_element.pyx:                except
(ValueError, LinAlgError):
-------------------------

I might like something like LinAlgError, if it at least derived from
ArithmeticError, but evidently it doesn't:

sage: from numpy.linalg.linalg import LinAlgError
sage: isinstance(LinAlgError, ArithmeticError)
False

 -- William

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

Reply via email to