On Friday 1 March 2024 at 12:15:36 UTC+1 John Cremona wrote:

On Fri, 1 Mar 2024 at 11:03, Dima Pasechnik <dim...@gmail.com> wrote:



On Fri, Mar 1, 2024 at 10:24 AM John Cremona <john.c...@gmail.com> wrote:



On Fri, 1 Mar 2024 at 10:04, Dima Pasechnik <dim...@gmail.com> wrote:



On 1 March 2024 09:07:26 GMT, 'Martin R' via sage-devel <
sage-...@googlegroups.com> wrote:
>I'd be OK with raising an exception or with -oo, but it should be uniform, 
>and I think it should be the same for polynomials, Laurent polynomials and 
>in the same spirit for degree and valuation.
>
>It might be best to raise an exception, because this ensures that the zero 
>polynomial gets special treatment.

Exceptions are expensive, performance-wise, and using them as a regular 
means of controlling the flow of the algorithm execution is not a good idea.
A simple  if/then/else  is much cheaper.


Isn't this suggestion to have f.degree() raise an exception when f is zero, 
but also then that any code which needs the degree to treat 0 as a special 
case (where that makes sense)?   To it would be the caller's responsibility 
to do that with a test of f.is_zero() or whatever, rather than by seeing if 
an exception is triggered.


Exactly, sorry for being unclear.  try - except would certainly be out of 
place in this case.

Letting degree(0) throw an exception means that every place where you want 
to test whether the degree of fg satisfies something needs a testing 
whether f or g is 0, in order to avoid an exception.


Yes indeed - unless you already know that the polynomial is non-zero.
 

Fair enough.  I had been assuming that for the types we are talking about 
testing for equality with 0 would be fast, but perhaps it is not.


I just found that Polynomial.is_zero checks whether degree is negative, and 
degree is `return smallInteger(len(self._coeffs) - 1)`, for flint 
polynomials it is `return smallInteger(fmpq_poly_degree(self._poly))`.  So, 
indeed, either change would come with a cost.

OTOH, setting the degree of 0 to be -oo has an obvious advantage: it 
automaticlly gives mathematically correct degree of fg, by using 
degree(fg)=degree(f)+degree(g), regardless of f or g being 0. And checking 
the degree is (or at least ought to be) faster than comparing for equality 
to 0.


It's a little dangerous to talk of -oo being "mathematically correct", but 
I have given this definition myself in undergraduate course (and for the 
reason you give) so that's ok, especially as in Sage we do have -oo as a 
possible return value and no requiremt for the value to always be of the 
same type (e.g. Integer).


I would rather say that "-1" is in some cases "mathematically incorrect", 
in particular for Laurent polynomials :-)

To make it clear, I am not against -oo, I find -1 quite problematic (for 
user facing code, and because I really want uniformity for polynomials and 
Laurent polynomials and their series variants), and I think that raising an 
exception should be best performance-wise, but probably most work.

Best wishes,

Martin

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/18554706-6f6e-4946-b853-f83faa10076en%40googlegroups.com.

Reply via email to