creating variables in Polynomial Ring

200 views
Skip to first unread message

tvn

unread,
Sep 7, 2010, 1:47:19 PM9/7/10
to sage-support
Hi, given a list of variable names as strings (e.g., l =
['a','b','c','d']) I try to make those become variables in a
Polynomial Ring. One way is to do something like
R.<a,b,c,d>=CC['a','b','c','d'] , after this the type of a or b or c
or d is <class
'sage.rings.polynomial.multi_polynomial_element.MPolynomial_polydict'> .

How can I do this in a more automatic way that can take in the list l
and do the same thing as above (e.g., make a,b,c,d of type <class
'sage.rings.polynomial.multi_polynomial_element.MPolynomial_polydict'>)
without having to manually type R.<a,b,c,d> ?

Thanks in advance

John H Palmieri

unread,
Sep 7, 2010, 2:03:09 PM9/7/10
to sage-support
Does this do what you want? (I'm not exactly sure what you're looking
for.)

sage: l = ['a','b','c','d']
sage: R = PolynomialRing(CC, l)
sage: R.inject_variables()
sage: type(a)
<class
'sage.rings.polynomial.multi_polynomial_element.MPolynomial_polydict'>

--
John

Jason Grout

unread,
Sep 7, 2010, 2:03:40 PM9/7/10
to sage-s...@googlegroups.com

Something like this?

sage: R=CC[','.join('x{0}'.format(i) for i in range(20))]
sage: R.inject_variables()
Defining x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13,
x14, x15, x16, x17, x18, x19
sage: type(x0)
<class
'sage.rings.polynomial.multi_polynomial_element.MPolynomial_polydict'>


Jason

tvn

unread,
Sep 7, 2010, 4:34:10 PM9/7/10
to sage-support
Hi John and Jason, thanks -- the inject_variables() did what I
want.

I have another question below and hope you can help

what if I already have create a function call f = x - y as below


sage: vs = var('x y')
sage: f = x - y
sage: type(f)
<type 'sage.symbolic.expression.Expression'>


now I want to convert f to
sage.rings.polynomial.multi_polynomial_element.MPolynomial_polydict ,
is there any convenient way to do so ? One way is to define
variables x,y in PolynomialRing QQ and then redefine f = x-y but it's
quite inconvenient.


sage: vs = var('x y')
sage: R = PolynomialRing(QQ,vs)
sage: R.inject_variables()
Defining x, y
sage: f = x -y
sage: type(f)
<type
'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
sage: type(x)


Thanks again for the quick helps

Jason Grout

unread,
Sep 7, 2010, 4:39:24 PM9/7/10
to sage-s...@googlegroups.com
On 9/7/10 3:34 PM, tvn wrote:
> Hi John and Jason, thanks -- the inject_variables() did what I
> want.
>
> I have another question below and hope you can help
>
> what if I already have create a function call f = x - y as below
>
>
> sage: vs = var('x y')
> sage: f = x - y
> sage: type(f)
> <type 'sage.symbolic.expression.Expression'>
>
>
> now I want to convert f to
> sage.rings.polynomial.multi_polynomial_element.MPolynomial_polydict ,
> is there any convenient way to do so ? One way is to define
> variables x,y in PolynomialRing QQ and then redefine f = x-y but it's
> quite inconvenient.
>
>


Try the .polynomial() method:

sage: var('x,y')
(x, y)
sage: f=x-y
sage: g=f.polynomial(QQ)
sage: g
x - y
sage: type(g)
<type
'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
sage: g.variables()
(x, y)
sage: g.parent()
Multivariate Polynomial Ring in x, y over Rational Field

Jason

tvn

unread,
Sep 7, 2010, 4:41:13 PM9/7/10
to sage-support
ah thanks much -- I also just found it by just trial and error.

tvn

unread,
Sep 7, 2010, 5:02:42 PM9/7/10
to sage-support
There's still one type error about parent mismatch in my code.
Basically I want to write a function foo like below


def foo(f1,f2,vs):
R = PolynomialRing(QQ,vs)

f1_ = f1.polynomial(QQ)
f2_ = f2.polynomial(QQ)

I = R*[f1_]
G = I.radical().groebner_basis()
res = f2_.reduce(G) ################# error here #####
return res == 0


for example

sage:vs = var('a,b,c')
sage: f = a-b
sage: g = a^2-b^2
sage: foo(f,g,vs)


it gives a TypeError: parents do not much

/home/tnguyen/Src/Devel/sage/local/lib/python2.6/site-packages/sage/
rings/polynomial/multi_polynomial_libsingular.so in
sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular.reduce
(sage/rings/polynomial/multi_polynomial_libsingular.cpp:23945)()

TypeError: parents do not match

Nils Bruin

unread,
Sep 7, 2010, 5:04:15 PM9/7/10
to sage-support
On Sep 7, 1:34 pm, tvn <nguyenthanh...@gmail.com> wrote:
> Hi John and Jason,  thanks  --  the inject_variables() did what I
> want.
>
> I have another question below and hope you can help
>
> what if I already have create a function call f = x - y as below
>
> sage: vs = var('x y')
> sage: f = x - y
> sage: type(f)
> <type 'sage.symbolic.expression.Expression'>

The expression f is not a function, but a symbolic expression. It is
callable, though

sage: f(x=1,y=1)
0
sage: g(x,y)=x-y
sage: type(g)
<type 'sage.symbolic.expression.Expression'>

OK, so that doesn't give it away. But:

sage: g(1,1)
0

(Note that there is no need to specify the names of the variables. Try
that with f and you'll get a deprecation warning.)

Anyway, for your question:

sage: F=QQ['x','y'](f)
sage: F
x - y
sage: type(F)
<type
'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>

It's not a "polydict", but I think all dictionary functionality is
present on F as well.

I haven't succeeded in creating a "polydict" bivariate polynomial ring
over Q. You can get a a "polydict" ring by asking for a polynomial
ring over a more exotic base ring:

sage: type(PolynomialRing(FractionField(QQ['t']),['x','y']))
<class
'sage.rings.polynomial.multi_polynomial_ring.MPolynomialRing_polydict_domain'>

tvn

unread,
Sep 7, 2010, 5:17:28 PM9/7/10
to sage-support
Nils, thanks -- I think I was able to do what I want using your code
snippet


def foo(f1,f2,vs):
R = PolynomialRing(QQ,vs)
f1_ = R(f1)
f2_ = R(f2)
I = R*[f1_]
G = I.radical().groebner_basis()
res = f2_.reduce(G)
return res == 0

Jason Grout

unread,
Sep 7, 2010, 5:19:17 PM9/7/10
to sage-s...@googlegroups.com
On 9/7/10 4:04 PM, Nils Bruin wrote:
> On Sep 7, 1:34 pm, tvn<nguyenthanh...@gmail.com> wrote:
>> Hi John and Jason, thanks -- the inject_variables() did what I
>> want.
>>
>> I have another question below and hope you can help
>>
>> what if I already have create a function call f = x - y as below
>>
>> sage: vs = var('x y')
>> sage: f = x - y
>> sage: type(f)
>> <type 'sage.symbolic.expression.Expression'>
>
> The expression f is not a function, but a symbolic expression. It is
> callable, though
>
> sage: f(x=1,y=1)
> 0
> sage: g(x,y)=x-y
> sage: type(g)
> <type 'sage.symbolic.expression.Expression'>
>
> OK, so that doesn't give it away.

This does:

sage: g.parent()
Callable function ring with arguments (x, y)


But:
>
> sage: g(1,1)
> 0
>
> (Note that there is no need to specify the names of the variables. Try
> that with f and you'll get a deprecation warning.)

I'm working on removing this functionality, as the deprecation has been
in Sage for well over a year.

Thanks,

Jason

Reply all
Reply to author
Forward
0 new messages