coercing or substituting variables in symmetric polynomials

163 views
Skip to first unread message

David Madore

unread,
Oct 23, 2009, 9:16:21 AM10/23/09
to Sage Support Mailing-List
Dear List,

I'm trying to substitute various expressions for the variables of
symmetric polynomials returned by
SymmetricFunctionAlgebra_generic(...).expand() but I can't figure out
how to ask Sage to replace these variables or coerce them to some
other polynomial ring. E.g.:

----------------------------------------------------------------------
| Sage Version 4.1.2, Release Date: 2009-10-14 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: R.<x0,x1,x2> = QQ['x0','x1','x2']
sage: x3=-(x0+x1+x2)
sage: e = SFAElementary(QQ)
sage: e([1]).expand(4)
x0 + x1 + x2 + x3
sage: e([1]).expand(4,alphabet=[x0,x1,x2,x3])
<snip long error trace>
ValueError: variable names must be alphanumeric, but one is '-x0 - x1 - x2' which is not.
sage: e([1]).expand(4).subs([x0,x1,x2,x3])
<snip another long error trace>
AttributeError: 'list' object has no attribute 'iteritems'
sage: e([1]).expand(4).subs({x0:x0,x1:x1,x2:x2,x3:x3,x4:x4})
---------------------------------------------------------------------------
NameError Traceback (most recent call last)

/usr/src/local/sage-4.1.2/<ipython console> in <module>()

NameError: name 'x4' is not defined
sage: R(e([1]).expand(4))
<snip huge error trace>
TypeError: not a constant polynomial

I think it should be quite obvious what I'm trying to do there (the
result should be 0, of course). How can I make it work?

Evidently e([1]).expand(4) returns a polynomial constructed of
variables x0,x1,x2,x3 which are not my x0,x1,x2 and x3. How can I
force it to use my variables x0,x1,x2,x3?

And, more generally, given a list of elements in a ring, how can I
efficiently (in terms of number of keystrokes!) compute the
elementary symmetric functions of these elements?

Thanks in advance,

--
David A. Madore
( http://www.madore.org/~david/ )

Simon King

unread,
Oct 24, 2009, 3:48:24 AM10/24/09
to sage-support
Hi David!

I can only give you a partial answer, and at one point I became
totally puzzled, myself. But let's try:

On 23 Okt., 15:16, David Madore <david...@madore.org> wrote:
<snip>
> sage: R.<x0,x1,x2> = QQ['x0','x1','x2']
> sage: x3=-(x0+x1+x2)
> sage: e = SFAElementary(QQ)
> sage: e([1]).expand(4)
> x0 + x1 + x2 + x3
> sage: e([1]).expand(4,alphabet=[x0,x1,x2,x3])

No surprise that it does not work, because the optional argument
"alphabet" must be formed by alphanumeric data. It is fine to give
x0,x1 and x2, because these variables stand "for themselves"; but x3
is -x0-x1-x2, and this is certainly not alphanumeric. You may try to
pass it as a string, but the result is not what you want:

sage: p = e([1]).expand(4,alphabet=[x0,x1,x2,'x3'])
sage: p
x0 + x1 + x2 + x3

But at least it enables you to substitute now:

sage: p.subs(x3=x3)
0
sage: p(x0,x1,x2,x3)
0

> sage: e([1]).expand(4).subs([x0,x1,x2,x3])
> <snip another long error trace>
> AttributeError: 'list' object has no attribute 'iteritems'

This is because the argument of subs() must be a dictionary -- you can
read the documentation by typing "p.subs?". Note that p.subs(x3=x3)
should be as much as giving a dictionary, but see below.

> sage: e([1]).expand(4).subs({x0:x0,x1:x1,x2:x2,x3:x3,x4:x4})
> ---------------------------------------------------------------------------
> NameError                                 Traceback (most recent call last)
>
> /usr/src/local/sage-4.1.2/<ipython console> in <module>()
>
> NameError: name 'x4' is not defined

Well, the problem is, as the traceback says, that x4 is not defined.
But even omitting it, you get an error:

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

Indeed:
sage: x0.parent()
Multivariate Polynomial Ring in x0, x1, x2 over Rational Field
sage: p.parent()
Multivariate Polynomial Ring in x0, x1, x2, x3 over Rational Field


Here comes the point where I am puzzled. Can please someone explain
what happens here?

I thought that p.subs(x3=x3) is equivalent to p.subs({'x3':x3}). But
in contrast to any other example that I met so far, one gets:

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

How is it possible that p.subs(x3=x3) works while p.subs({'x3':x3})
doesn't? I thought this is how Python works?

Cheers
Simon

Simon King

unread,
Oct 24, 2009, 7:40:22 AM10/24/09
to sage-support
Hi David!

Here is a follow-up.

On Oct 24, 8:48 am, Simon King <simon.k...@nuigalway.ie> wrote:
[...]
> Here comes the point where I am puzzled. Can please someone explain
> what happens here?
>
> I thought that p.subs(x3=x3) is equivalent to p.subs({'x3':x3}). But
> in contrast to any other example that I met so far, one gets:
>
>   sage: p.subs({'x3':x3})
>   Traceback
>   ...
>   TypeError: keys do not match self's parent
>
> How is it possible that p.subs(x3=x3) works while p.subs({'x3':x3})
> doesn't? I thought this is how Python works?

At http://groups.google.com/group/sage-devel/browse_thread/thread/e8cd8d6f4e334141
I raised that question as well, and Martin Albrecht explained it:

* We have the signature subs(self, fixed, **kw=None)
* Doing p.subs({'x3':x3}), the dictionary {'x3':x3} is assigned to the
argument "fixed". But if "fixed" gets a dictionary, then the dict keys
must be variables of the polynomial. Here, the key is a string,
therefore: error!
* If you want to work with a dictionary, it should be p.subs(**
{'x3':x3}). Then, the dictionary is assigned to the argument "kw", and
here string keys are fine:
sage: R.<x0,x1,x2> = QQ['x0','x1','x2']
sage: x3=-(x0+x1+x2)
sage: e = SFAElementary(QQ)
sage: p = e([1]).expand(4)
sage: p.subs(**{'x3':x3})
0

Pretty tricky! :(

One more remark.
When you do p.subs({x0:1}), there will also be an error, namely
sage: p.subs({x0:1})
Traceback
...
TypeError: keys do not match self's parent

The reason is that x0 belongs to R, but the variable x0 from p belongs
to a different polynomial ring:
sage: p.lm()
x0
sage: p.lm().parent()
Multivariate Polynomial Ring in x0, x1, x2, x3 over Rational Field
sage: x0.parent()
Multivariate Polynomial Ring in x0, x1, x2 over Rational Field

The two x0's are equal, but not identical:
sage: p.lm() == x0
True
sage: p.lm() is x0
False

So: p.subs({x0:1}) tries to replace x0. But x0 is an element of R by
your definition and is *not* the same as the variable x0 of p.
Therefore the error.

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