Ask Your Question
1

How to define polynomial p(x_i, x_j) while x_i, x_j runs over available variables?

asked 2019-07-15 14:42:51 +0100

AnteC gravatar image

Let's say I have variables x_1, x_2, ..., x_d, where d is some integer. Let's say I have a polynomial p(a,b) defined. How could I get a list of p(x_i, x_j) where i, j runs over 1, 2, ..., d, possibly with some other qualifiers (i.e. we must have i inequal to j)? I want to define an ideal in this way, but when I get a list I know how to proceed.

Thank you in advance!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2019-07-16 20:29:49 +0100

rburing gravatar image

I suppose our multivariate polynomial ring R is defined as follows:

sage: R = PolynomialRing(QQ, 'x_', 3)
sage: x = R.gens()

Now it depends a little bit on how your bivariate polynomial is defined. Is it a Python function? A Sage callable symbolic expression? An element of a polynomial ring? An element of the symbolic ring?

If it is a Python function:

sage: p = lambda a,b: a^2 + b^2
sage: [p(x[i], x[j]) for i in range(len(x)) for j in range(len(x)) if i != j]

If it is a Sage callable expression:

sage: p(a,b) = a^2 + b^2
sage: [R(p(x[i], x[j])) for i in range(len(x)) for j in range(len(x)) if i != j]

If it is an element of the symbolic ring:

sage: var('a,b')
sage: p = a^2 + b^2
sage: [R(p(a=x[i], b=x[j])) for i in range(len(x)) for j in range(len(x)) if i != j]

If it is a polynomial in a polynomial ring:

sage: S.<a,b> = PolynomialRing(QQ)
sage: p = a^2 + b^2
sage: P = p.change_ring(R)
sage: [P(x[i],x[j]) for i in range(len(x)) for j in range(len(x)) if i != j]

All give the same result, a list of elements of R:

[x_0^2 + x_1^2,
 x_0^2 + x_2^2,
 x_0^2 + x_1^2,
 x_1^2 + x_2^2,
 x_0^2 + x_2^2,
 x_1^2 + x_2^2]

You can add other conditions in the list comprehension using and etc.

Also you can use itertools.product or itertools.combinations:

sage: p = lambda a,b: a^2 + b^2
sage: import itertools
sage: [p(x[i], x[j]) for (i,j) in itertools.product(range(len(x)),repeat=2) if i != j]

And you don't have to use indices if your condition doesn't depend on it:

sage: p = lambda a,b: a^2 + b^2
sage: [p(z,w) for (z,w) in itertools.product(x,repeat=2) if z != w]
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2019-07-15 14:42:51 +0100

Seen: 576 times

Last updated: Jul 16 '19