Ask Your Question

Revision history [back]

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]