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.Wed, 04 Jul 2018 22:41:43 +0200Substitution of multiple variables, for linearizationhttps://ask.sagemath.org/question/42822/substitution-of-multiple-variables-for-linearization/ Hi
we want to substitute multiple variables, for example
x1*x3 would be z1, x2*x4 would be z2 and so on.
We save all combinations of monomials in a dictionary, then we wanted to use subs to replace the variables, but it does not work.
How could one implement this?
The purpose is for linearization of a large system of equations.
A system like this:
x[1] + x[1]*x[2]*x[3] + x[1]*x[4]*x[6] + x[1] + x[4] + x[5] + x[7],
x[0]*x[1]*x[4] + x[0]*x[1] + x[2]*x[4] + x[3]*x[5]*x[6] + x[3]*x[5] + x[7] + 1,
x[1]*x[2] + x[2]*x[3]*x[6] + x[2]*x[4] + 1,
x[0]*x[5]*x[6]*x[7] + x[1]*x[7] + x[2] + x[7],
x[1]*x[3]*x[7] + x[4] + x[5]*x[6] + x[6],
x[1]*x[3] + x[2]*x[4] + x[3]*x[4]Wed, 04 Jul 2018 11:11:49 +0200https://ask.sagemath.org/question/42822/substitution-of-multiple-variables-for-linearization/Answer by vdelecroix for <p>Hi</p>
<p>we want to substitute multiple variables, for example
x1<em>x3 would be z1, x2</em>x4 would be z2 and so on.</p>
<p>We save all combinations of monomials in a dictionary, then we wanted to use subs to replace the variables, but it does not work.
How could one implement this?</p>
<p>The purpose is for linearization of a large system of equations.
A system like this:
x[1] + x[1]<em>x[2]</em>x[3] + x[1]<em>x[4]</em>x[6] + x[1] + x[4] + x[5] + x[7],
x[0]<em>x[1]</em>x[4] + x[0]<em>x[1] + x[2]</em>x[4] + x[3]<em>x[5]</em>x[6] + x[3]<em>x[5] + x[7] + 1,
x[1]</em>x[2] + x[2]<em>x[3]</em>x[6] + x[2]<em>x[4] + 1,
x[0]</em>x[5]<em>x[6]</em>x[7] + x[1]<em>x[7] + x[2] + x[7],
x[1]</em>x[3]<em>x[7] + x[4] + x[5]</em>x[6] + x[6],
x[1]<em>x[3] + x[2]</em>x[4] + x[3]*x[4]</p>
https://ask.sagemath.org/question/42822/substitution-of-multiple-variables-for-linearization/?answer=42827#post-id-42827If you intend to work with polynomials just work with polynomial rings. The symbolic expressions in Sage are very hard to parse. A simple example can be obtained as follows
sage: K.<x,y,z> = QQ[]
sage: p = x + 2*y - 3*z + 2*x*z + 3*x*y*z
sage: R.<z1,z2,z3,z4,z5> = QQ[]
sage: d = {x: z1, y: z2, z: z3, x*z: z4, x*y*z: z5}
sage: sum(p.monomial_coefficient(m) * v for m,v in d.iteritems())
z1 + 2*z2 - 3*z3 + 2*z4 + 3*z5
Wed, 04 Jul 2018 22:41:43 +0200https://ask.sagemath.org/question/42822/substitution-of-multiple-variables-for-linearization/?answer=42827#post-id-42827