Possible to improve "inconsistency" between notations for exponentiation?

20 views
Skip to first unread message

sebe...@spawar.navy.mil

unread,
Nov 2, 2008, 1:58:45 AM11/2/08
to sage-devel
Python uses "**" for exponentiation.
Sage syntax uses the much nicer "^".
Won't this lead to problems/confusion since Sage uses python code to
define functions but not to *invoke* those functions?

Chris

Justin C. Walker

unread,
Nov 2, 2008, 2:30:11 AM11/2/08
to sage-...@googlegroups.com

Changing "^" to "**" would improve the consistency, not the
inconsistency :-}

I know what you mean, but I think we're wedded to the difference. It
does admittedly cause some issues, but in the long run, I believe it
is more consistent with the aims that the Sage developers have set for
Sage.

Justin

--
Justin C. Walker, Curmudgeon at Large
Institute for the Absorption of Federal Funds
-----------
My wife 'n kids 'n dogs are gone,
I can't get Jesus on the phone,
But Ol' Milwaukee's Best is my best friend.
-----------


sebe...@spawar.navy.mil

unread,
Nov 2, 2008, 3:08:09 PM11/2/08
to sage-devel
On Nov 1, 11:30 pm, "Justin C. Walker" <jus...@mac.com> wrote:
> Changing "^" to "**" would improve the consistency, not the  
> inconsistency  :-}
>
> I know what you mean, but I think we're wedded to the difference.

What about if Sage modified Python syntax slightly?
For example, what if Sage added pre-processing code to replace "^"
with "**" before feeding the Sage/Python code to the Python
interpreter?

This would mean that Sage coders could use "^" everywhere including in
Python code.
Then Sage could allow both "^" and "**" everywhere.

e.g.

def my_sage_square_function(x):
return x^2

One potential problem I can forsee with this is if someone really
wanted to do XOR in Sage/Python code. But even in that case they
could use alternatives.

Chris

mabshoff

unread,
Nov 2, 2008, 3:14:23 PM11/2/08
to sage-devel


On Nov 2, 12:08 pm, "seber...@spawar.navy.mil"
<seber...@spawar.navy.mil> wrote:
> On Nov 1, 11:30 pm, "Justin C. Walker" <jus...@mac.com> wrote:
>
> > Changing "^" to "**" would improve the consistency, not the  
> > inconsistency  :-}
>
> > I know what you mean, but I think we're wedded to the difference.
>
> What about if Sage modified Python syntax slightly?
> For example, what if Sage added pre-processing code to replace "^"
> with "**" before feeding the Sage/Python code to the Python
> interpreter?

Sage already does that via its preparser:

sage: x^2
x^2
sage: x**2
x^2
sage: preparse("x^2")
'x**Integer(2)'

I.e.. any time you there is a "^" in input it is replaced by "**". You
can turn the preparser off and then use "^" to signify XOR.

> This would mean that Sage coders could use "^" everywhere including in
> Python code.
> Then Sage could allow both "^" and "**" everywhere.
>
> e.g.
>
> def my_sage_square_function(x):
>             return x^2
>
> One potential problem I can forsee with this is if someone really
> wanted to do XOR in Sage/Python code.  But even in that case they
> could use alternatives.
>
> Chris

Cheers,

Michael

sebe...@spawar.navy.mil

unread,
Nov 2, 2008, 7:41:36 PM11/2/08
to sage-devel


On Nov 2, 12:14 pm, mabshoff <mabsh...@googlemail.com> wrote:
> Sage already does that via its preparser:
>
> sage: x^2
> x^2
> sage: x**2
> x^2
> sage: preparse("x^2")
> 'x**Integer(2)'

Yes but I don't think you are claiming that Python code can use ^ for
exponentials are you?
Actually I'm moving towards opinion that I should just use "**"
everywhere.

Alternatively, I could avoid Python code everywhere now that Stein
pointed out to me how to define functions w/o Python code...

my_function(t) = 4*t -9

cs

William Stein

unread,
Nov 2, 2008, 7:50:55 PM11/2/08
to sage-...@googlegroups.com
On Sun, Nov 2, 2008 at 4:41 PM, sebe...@spawar.navy.mil
<sebe...@spawar.navy.mil> wrote:
>
>
>
> On Nov 2, 12:14 pm, mabshoff <mabsh...@googlemail.com> wrote:
>> Sage already does that via its preparser:
>>
>> sage: x^2
>> x^2
>> sage: x**2
>> x^2
>> sage: preparse("x^2")
>> 'x**Integer(2)'
>
> Yes but I don't think you are claiming that Python code can use ^ for
> exponentials are you?
> Actually I'm moving towards opinion that I should just use "**"
> everywhere.

That is a consistent way to go. Plus it will work in Fortran :-)

> Alternatively, I could avoid Python code everywhere now that Stein
> pointed out to me how to define functions w/o Python code...
>
> my_function(t) = 4*t -9

There might be some confusion. If you're writing code in the notebook
or the command line, you can do this and it will work fine:

sage: def f(x):
....: return x^2
....:
sage: f(10)
100

I.e., even "python functions" get their body's preparsed, so ^ works. The
only problem is if you put code in an external .py file.

Keep persisting -- I think there's some failure to communicate here there
will get clarified with persistence. And the trail of this conversation will be
useful to other people who find it later.

William

Reply all
Reply to author
Forward
0 new messages