ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 16 Jul 2019 20:29:49 +0200How to define polynomial p(x_i, x_j) while x_i, x_j runs over available variables?https://ask.sagemath.org/question/47167/how-to-define-polynomial-px_i-x_j-while-x_i-x_j-runs-over-available-variables/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! Mon, 15 Jul 2019 14:42:51 +0200https://ask.sagemath.org/question/47167/how-to-define-polynomial-px_i-x_j-while-x_i-x_j-runs-over-available-variables/Answer by rburing for <p>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.</p>
<p>Thank you in advance! </p>
https://ask.sagemath.org/question/47167/how-to-define-polynomial-px_i-x_j-while-x_i-x_j-runs-over-available-variables/?answer=47188#post-id-47188I 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]Tue, 16 Jul 2019 20:29:49 +0200https://ask.sagemath.org/question/47167/how-to-define-polynomial-px_i-x_j-while-x_i-x_j-runs-over-available-variables/?answer=47188#post-id-47188