You are definitely correct about assert versus try/except. The assert is a 
debugging-stage tool, and once the code is fully debugged, some asserts 
will disappear and others will become try/catch, for the purpose of making 
a human-readable error message.

I did not open a ticket. I wanted to wait a few days for everyone to be 
able to say what they want and for a consensus to form. I think we have a 
consensus that we should do *something* but unless I am very much mistaken, 
the suggestion from Vincent Delecroix and Nils Bruin that we make a 
symbolic function has advantages. In fact, I think Nils Bruin has posted 
code to Sage-devel. 

I have to confess that I don't understand Nils's code. Actually, I wasn't 
able to get the following to work, but I have a feeling that I am making a 
minor and stupid mistake with the syntax.

################
from sage.symbolic.function import SymbolicFunction
class nth_root(SymbolicFunction):
    def __init__(self):
        SymbolicFunction.__init__(self, 'nth_root', nargs=2)
    def _evalf_(self, n, x, parent=None):
        return parent(x).nth_root(n)

plot( nth_root( x, 3), -5, 5 )
################

Maybe someone here could explain exactly what the distinction is or what 
the pros/cons are for my approach versus Nils's? I have no idea.

I will also post this on Sage-Devel, but perhaps all future replies should 
be restricted to Sage-Devel and not Sage-Edu?
---Greg

On Tuesday, July 8, 2014 10:30:09 PM UTC-4, kcrisman wrote:
>
> Ah, I missed all this.  Did you ever open a ticket (with this or Nils' or 
> other code)?  I guess it's too late for your book (well, the 1st print 
> edition, on the internet revision means something different, as a recent 
> article I read on lit crit was pointing out...)
>
>
>  
>
>> def nth_real_root( x, n ): 
>>     """Note: n must be a positive integer. However, x is any real number. 
>>         (Otherwise this explanation will make no sense.) 
>>         For odd values of n, we return the unique real number y such 
>> that y^n = x. 
>>         For even values of n, if x<0, there is no real number y such 
>> that y^n = x and so we throw an exception. 
>>         For even values of n, if x=>0, then we return the unique real 
>> number y such that y^n = x.""" 
>>
>>     if ((n in ZZ)==false): 
>>         raise RuntimeError('nth_real_root(x,n) requires n to be a 
>> positive integer. Your n is not an integer.') 
>>
>>     if (n<=0): 
>>         raise RuntimeError('nth_real_root(x,n) requires n to be a 
>> positive integer. Your n is not positive.') 
>>
>>     assert (n in ZZ) 
>>     assert (n>0) 
>>
>>     if ((n%2) == 0): 
>>         # n is even 
>>         if (x<0): 
>>             raise RuntimeError('There is no nth real root (of a 
>> negative number x) when n is even.') 
>>         else: 
>>             return x^(1/n) 
>>
>>     assert ((n%2)==1) 
>>     # n is odd 
>>     return sign(x)*(abs(x))^(1/n) 
>>
>>
> By the way, apparently it's better to use a try/except clause than an 
> assert, so that we have a meaningful error message, or so I have heard, I'm 
> not a PEP guru. 
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-edu" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-edu.
For more options, visit https://groups.google.com/d/optout.

Reply via email to