2
$\begingroup$

I'm a beginner in Mathematica, so please forgive me if the following question turns out to be completely trivial.

My situation is that I'd like to rename certain terms of polynomials. Say I have a polynomial, which I've factorized in a certain way already so that it looks something like this (just an example): $$p=(a_1+a_2+a_3)(b_1+b_2)^2-(c_1-c_2)(d_1 d_2 + d_3 - d_4)$$ Now I'd like to call $$a := a_1+a_2+a_3, \hspace{2mm} b := b_1+b_2, \hspace{2mm} c := c_1+c_2, \hspace{2mm} d := d_1 d_2 + d_3 - d_4$$ so that the polynomial $p$ is then written as $$ p= a b^2 - c d. $$

This seems like a functionality that Mathematica could already have but I don't know about...

I'm grateful for any help!

EDIT: In the above case a simple "/." replacement seems to work. However, can someone tell me why it doesn't in the following case:

z1 z2 z3 z4 (z1 z2 + z1 z3 + z2 z3 + z1 z4 + z2 z4 + z3 z4) (z1 z2 z3 + z1 z2 z4 + z1 z3 z4 + z2 z3 z4) /. {z1 z2 + z1 z3 + z2 z3 + z1 z4 + z2 z4 + z3 z4 -> a1, z1 z2 z3 + z1 z2 z4 + z1 z3 z4 + z2 z3 z4 -> a2, z1 z2 z3 z4 -> a3}

which evaluates to

a3 (z1 z2 + z1 z3 + z2 z3 + z1 z4 + z2 z4 + z3 z4) (z1 z2 z3 + z1 z2 z4 + z1 z3 z4 + z2 z3 z4)
$\endgroup$
6
$\begingroup$

PolynomialReduce is usually the best function for this type of replacement. One often does better by first "canonicalizing" the reducers, using GroebnerBasis. The idea is to order terms/variables so that the ones desired in the result are "smaller" than those being replaced. This can be done by specifying the ones to remove in the variable lists, since all others that appear will automatically be ordered lower by PolynomialReduce and GroebnerBasis.

Here is one example in question.

poly = 
  z1 z2 z3 z4 (z1 z2 + z1 z3 + z2 z3 + z1 z4 + z2 z4 + 
     z3 z4) (z1 z2 z3 + z1 z2 z4 + z1 z3 z4 + z2 z3 z4);
reducers = {z1 z2 + z1 z3 + z2 z3 + z1 z4 + z2 z4 + z3 z4 - a1, 
   z1 z2 z3 + z1 z2 z4 + z1 z3 z4 + z2 z3 z4 - a2, z1 z2 z3 z4 - a3};
gb = GroebnerBasis[reducers, Variables[poly]];
PolynomialReduce[poly, gb, Variables[poly]][[2]]

(* Out[58]= a1 a2 a3 *)

Related: 1 2 3 4

$\endgroup$
  • $\begingroup$ By the way I tried to get the community to pen a canonical question for this general class of problems, and apparently there is interest but nobody is both willing and able to do it: meta.mathematica.stackexchange.com/q/2078/121 $\endgroup$ – Mr.Wizard Feb 16 '17 at 13:35
  • 1
    $\begingroup$ @Mr.Wizard, Ironically, I find myself again traveling. Maybe if I stayed home people would stop asking about this.. $\endgroup$ – Daniel Lichtblau Feb 16 '17 at 17:04
2
$\begingroup$

Looks like you need a simple replacement:

(a1 + a2 + a3) (b1 + b2)^2 - (c1 - c2) (d1 d2 + d3 - d4) /. 
    {a1 + a2 + a3 -> a, b1 + b2 -> b, c1 - c2 -> c, d1 d2 + d3 - d4 -> d}

(*a b^2 - c d*)
$\endgroup$
  • $\begingroup$ Thanks for your answer! Now I had tried that, although not with exactly the polynomial I wrote down, I must admit. In the above case it seems to work, but can you tell me why it doesn't work in the following case (unfortunately I don't know how to write code in a comment, sorry): z1 z2 z3 z4 (z1 z2 + z1 z3 + z2 z3 + z1 z4 + z2 z4 + z3 z4) (z1 z2 z3 + z1 z2 z4 + z1 z3 z4 + z2 z3 z4) /. {z1 z2 + z1 z3 + z2 z3 + z1 z4 + z2 z4 + z3 z4 -> a1, z1 z2 z3 + z1 z2 z4 + z1 z3 z4 + z2 z3 z4 -> a2, z1 z2 z3 z4 -> a3} $\endgroup$ – Tom Bombadil Feb 15 '17 at 19:15
  • 1
    $\begingroup$ Use //. instead of /. $\endgroup$ – Carl Woll Feb 15 '17 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.