[sage-support] Strange (non-)substitution of variables whose name is not equal to their representation

2010-07-07 Thread David Sanders
Hi,

I have finally managed to try out Sage seriously after a long time
wanting to (and with intermediate-level Python experience). In general
it's really rather amazing, thanks to all involved!

I have come across what -- to me -- seems at least incongruous, when
substituting variables.
I am using 'Sage Version 4.4.4, Release Date: 2010-06-23', downloaded
a couple of days ago as the Ubuntu 10.04 binary package.

I want to have a variable called eps, but which appears as an epsilon
in the notebook interface, so I do

eps = var(epsilon)

Now suppose I have

a = 3 * eps

I now want to substitute eps=1, so I do

a.subs(eps = 1)

but the response is still 3*epsilon !

If I do

a.subs(epsilon = 1)

then I get 3.

But also, if I do

a.subs({eps:1})

with a dictionary instead, then I get what I expect, namely 3.

This seems to me strange, and possibly a bug, but maybe I'm just
misunderstanding.
Of course, I have found the solution -- just use a dictionary -- but I
would like to understand what's going on.

I see that the examples in the documentation seem always to use
variables defined simply as

var('x')

etc.,  whose names are equal to their representations, so this problem
seems not to arise.

Thanks and best wishes,
David.

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


Re: [sage-support] Strange (non-)substitution of variables whose name is not equal to their representation

2010-07-07 Thread Mike Hansen
On Wed, Jul 7, 2010 at 1:49 PM, David Sanders dpsand...@gmail.com wrote:
 I now want to substitute eps=1, so I do

 a.subs(eps = 1)

 but the response is still 3*epsilon !

This is due to the way Python functions work.  Basically, doing

a.subs(eps=1)

is the same as doing

a.subs(**{'eps': 1})

When you use keyword arguments, the function gets the string 'eps'
rather than the var eps.  So, it tries to look for a variable named
'eps' which it doesn't find.  When you pass in the dictionary
explicitly:

a.subs({eps: 1})

the key is the actual variable object rather than a string so it knows
to do the right thing.

--Mike

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