How to define variables over integer

75 views
Skip to first unread message

Santanu Sarkar

unread,
May 8, 2019, 9:15:06 AM5/8/19
to sage-support
I know how to define variables over BooleanPolynomialRing.
This is as follows.

n=4
V=BooleanPolynomialRing(n+1,['z%d'%(i) for i in range(n+1)] )
V.inject_variables()

Can we define similar code over integers (ZZ) or rationals (QQ)?
Also I want to store variables in an array like Z=[z0,z1,z2,z3]
but it should be automatic. I will change only n.  

darwin doppelganger

unread,
May 8, 2019, 9:29:20 PM5/8/19
to sage-support
Doesn't PolynomialRing() do what you want? For example,

n=4
V
=PolynomialRing(QQ,n+1,['z%d'%(i) for i in range(n+1)] )
V.inject_variables()

darwin doppelganger

unread,
May 8, 2019, 9:29:20 PM5/8/19
to sage-support
I forgot to add that you can automatically put the variables in a list with:

Z = list(V.gens())


Simon King

unread,
May 9, 2019, 5:49:53 AM5/9/19
to sage-support
Hi Santanu,


Am Mittwoch, 8. Mai 2019 15:15:06 UTC+2 schrieb Santanu:
I know how to define variables over BooleanPolynomialRing.
This is as follows.

n=4
V=BooleanPolynomialRing(n+1,['z%d'%(i) for i in range(n+1)] )
V.inject_variables()

The above is what you could do in an interactive session in the case that the number of variables isn't known in advance. If it is known that you have exactly four variables, simply do
   sage: V.<z0,z1,z2,z3> = BooleanPolynomialRing()
which would automatically define z0,...,z3 in the global namespace.

Similarly, you can do
   sage: V.<z0,z1,z2,z3> = ZZ[]
to create a polynomial ring over the integers with generators z0,...,z3

But the above is not what you could do in a python module and in a module it is also a bad idea to inject variables.

So, simply put the variables in a list or access them by methods of V.


Can we define similar code over integers (ZZ) or rationals (QQ)?

Actually I wonder if we mean the same when we say "variables over ZZ". I mean "generators of a polynomial ring with integer coefficients". When the number of generators isn't known in advance, but the generators are named z0,z1,z2,..., such ring can be created, e.g., by
    sage: P = PolynomialRing(ZZ, 'z', 5)
    sage: P
    Multivariate Polynomial Ring in z0, z1, z2, z3, z4 over Integer Ring

However, I could imagine that you wanted to ask how to create a symbolic variable that is assumed to take values in ZZ --- and that's totally different from a generator of a polynomial ring over ZZ. So, if that's what you mean, you could do (in an interactive session)

    sage: var('z0 z1 z2 z3', domain='integer')
(which would inject the variables into the global namespace) or
    Z = var('z0 z1 z2 z3', domain='integer')
(which would also work in a python module and puts the variables into a tuple).

Also I want to store variables in an array like Z=[z0,z1,z2,z3]
but it should be automatic. I will change only n.

If you really want to work with symbolic variables, you could do
    sage: n = 5
    sage: Z = var(['z{}'.format(i) for i in range(n)], domain='integer')
    sage: Z
    (z0, z1, z2, z3, z4)
    sage: z0
    z0
(thus, the variables are both put in a tuple and injected into the global name space.

However, I believe that very many Sage users work with symbolic variables when they should better use generators of polynomial rings. So, perhaps code such as the following
    sage: P = PolynomialRing(ZZ, 'z', n)
    sage: Z = P.gens()
    sage: Z
    (z0, z1, z2, z3, z4)
    sage: P.gen(0)
    z0
    sage: P.inject_variables()
    Defining z0, z1, z2, z3, z4
(the latter only in an interactive session) suites your needs better.

Best regards,
Simon

Santanu Sarkar

unread,
May 9, 2019, 7:50:17 AM5/9/19
to sage-support
Dear friends,
   Thank you so much for your help. It is working now.

Regards,
Santanu

--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/d7d49151-5dc8-445c-b0dc-e494eb558965%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin C. Walker

unread,
May 23, 2019, 5:38:33 AM5/23/19
to SAGE Support
Check out PolynomialRing. Typing
PolynomialRing?
will give you some documentation.

HTH

Justin

--
Justin C. Walker
Curmudgeon at Large
Director
Institute for the Enhancement of the Director's Income
--
Build a man a fire and he'll be warm
for a night.
Set a man on fire and he'll be warm
for the rest of his life.



Reply all
Reply to author
Forward
0 new messages