Ask Your Question
1

How do I Pass a tuple as an argument for a multivariate polynomial?

asked 2010-08-29 20:57:29 -0500

allmar gravatar image

updated 2011-04-28 09:48:21 -0500

Kelvin Li gravatar image

I can't find any support documentation on this, but I'm sure it must be possible. To give some context, I'm working on a module for invariant theory which allows for computing matrices acting on polynomials:

Say I define a polynomial h(x1,x2) = a*x1^2 + b*x1x2 + c*x2^2 in QQ[x1,x2], and an ordered pair (2-tuple) v = (x1,-x2). How do I pass v such that h(v) is h(x1,-x2) ? In other words, I want to assign each coordinate of the tuple v to it's corresponding coordinate of the argument of h. Actually, a generalization to a polynomial in n variables which takes an n-tuple as an argument would be the most helpful. Below is the error that I received when trying to do this:

TypeError                                 Traceback (most recent call last)

/home/martin/Sage/sage-4.5.2/<ipython console> in <module>()

/home/martin/Sage/sage-4.5.2/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.__call__ (sage/symbolic/expression.cpp:15476)()

/home/martin/Sage/sage-4.5.2/local/lib/python2.6/site-packages/sage/symbolic/callable.pyc in _call_element_(self, _the_element, *args, **kwds)

449         d = dict(zip(map(repr, self.arguments()), args))

450         d.update(kwds)

--> 451         return SR(_the_element.substitute(**d))

/home/martin/Sage/sage-4.5.2/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:14850)()

/home/martin/Sage/sage-4.5.2/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:10193)()

/home/martin/Sage/sage-4.5.2/local/lib/python2.6/site-packages/sage/structure/parent_old.so in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3288)()

/home/martin/Sage/sage-4.5.2/local/lib/python2.6/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.coerce (sage/structure/parent.c:7045)()

TypeError: no canonical coercion from Ambient free module of rank 2 over the integral domain Multivariate Polynomial Ring in x1, x2 over Rational Field to Callable function ring with arguments (x1, x2)
edit retag flag offensive close delete

Comments

you can put <pre> ... </pre> around the code block to make it more readable (prevent markdown formatting) niles ( 2010-08-30 01:58:48 -0500 )edit
the same effect has selecting a block of text and clicking the editor button with zeroes and ones. Evgeny ( 2010-08-30 07:17:31 -0500 )edit
if you select a span of text and click "code formatting" button - the selection will be "backticked" and as a result displayed with a fixed width font. Evgeny ( 2010-08-30 07:18:40 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2010-08-29 21:36:36 -0500

vdelecroix gravatar image

updated 2010-08-29 21:37:17 -0500

Hello,

Here is a possibility

sage: R.<a,b> = PolynomialRing(QQ,['a','b'])
sage: R
Multivariate Polynomial Ring in a, b over Rational Field
sage: P = a^3*b^5+3*a^2+2*b^4
sage: P
sage: P(5,6)   # standard way
974667
sage: P(a=5,b=6)  # another one
974667
sage: v = (5,6)
sage: P(*v)   # the * operator unfold a tuple as 5,6
974667
sage: d = {'a': 5, 'b': 6}
sage: P(**v)  # the ** operator unfold a dictionary as a=5,b=6
974667

But if v is a vector and not a tuple the trick won't work.

Hoping this would be useful, Vincent

edit flag offensive delete publish link more

Comments

And for the many-variable case, you can use `d = dict(zip(R.gens(),v))` and `h.subs(d)` (I think the post above forgot to mention this last part). If v really has to be a vector, you could probably convert it to a tuple or list first. niles ( 2010-08-30 01:54:55 -0500 )edit
Niles: for me P.subs(a=5,b=6) does not work for an element of a polynomial ring (but does for symbolic expressions). vdelecroix ( 2010-08-30 02:26:32 -0500 )edit
Digression: flatten() is useful in related use-cases. The max_level keyword argument is very useful when there is multiple nesting. ccanonc ( 2010-08-30 07:15:05 -0500 )edit
vdelecroix: I think you have to pass the dictionary object to `subs()` niles ( 2010-08-30 09:11:43 -0500 )edit
What about the efficiency of each/any of the above methods? In particular, is unpacking the dictionary quicker than unpacking the list? tdstephens3 ( 2011-06-14 10:16:16 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Stats

Asked: 2010-08-29 20:57:29 -0500

Seen: 276 times

Last updated: Aug 30 '10