subs() shows a behaviour that should be impossible by Python syntax

11 views
Skip to first unread message

Simon King

unread,
Oct 24, 2009, 4:42:55 AM10/24/09
to sage-devel
Hi!

At http://groups.google.com/group/sage-support/browse_thread/thread/38dfa72aff118470
David Madore asked a question about substitution.

While I tried to answer it, I found the following behaviour of subs()
that, to my knowledge, is technically impossible, given how keyword
arguments are passed in Python:

sage: R.<x0,x1,x2>=QQ[]
sage: S = QQ['x0,x1,x2,x3']
sage: p=S.random_element()
sage: p.subs(x2=x2)
2*x1^2 + 85*x0*x3 - 2*x0 - x3

That's fine. But this is not:

sage: p.subs({'x2':x2})
Traceback
...
TypeError: keys do not match self's parent

Isn't it the case that actually p.subs(x2=x2) is *equivalent* to p.subs
({'x2':x2}) in Python?

I am totally puzzled.

Best regards,
Simon

Martin Albrecht

unread,
Oct 24, 2009, 4:52:19 AM10/24/09
to sage-...@googlegroups.com
> Isn't it the case that actually p.subs(x2=x2) is *equivalent* to p.subs
> ({'x2':x2}) in Python?

No, this isn't the same in Python. You are looking for:

sage: R.<x0,x1,x2>=QQ[]
sage: S = QQ['x0,x1,x2,x3']
sage: p=S.random_element()
sage: p.subs(x2=x2)

and

sage: p.subs(**{'x2':x2})
-7/2*x1*x2 + 1/2*x2^2 - x0*x3 + 1/165*x1 + 3

Note the two stars. The reason why the subs function accepts a dictionary is
that its signature is:

p.subs(self, fixed, **kw=None)

i.e. it also accepts a dictionary 'fixed'. This dictionary expects its keys to
be variables and not strings of variable names.

Cheers,
Martin

--
name: Martin Albrecht
_pgp: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x8EF0DC99
_otr: 47F43D1A 5D68C36F 468BAEBA 640E8856 D7951CCF
_www: http://www.informatik.uni-bremen.de/~malb
_jab: martinr...@jabber.ccc.de

Simon King

unread,
Oct 24, 2009, 7:23:43 AM10/24/09
to sage-devel
Hi Martin!

On Oct 24, 9:52 am, Martin Albrecht <m...@informatik.uni-bremen.de>
wrote:
> sage: p.subs(**{'x2':x2})
> -7/2*x1*x2 + 1/2*x2^2 - x0*x3 + 1/165*x1 + 3
>
> Note the two stars. The reason why the subs function accepts a dictionary is
> that its signature is:
>
>     p.subs(self, fixed, **kw=None)
>
> i.e. it also accepts a dictionary 'fixed'. This dictionary expects its keys to
> be variables and not strings of variable names.

That explains everything. Thank you!

Cheers,
Simon
Reply all
Reply to author
Forward
0 new messages