1
$\begingroup$

I am doing work with variables $x_1$, $x_2$, $x_3$, etc. In the process I generate polynomials which are of the form $$ p = x_1 x_2 x_3 + x_2^2 x_3 + x_1^2 x_2 + \ldots $$ I would like to be able to do operations on each of these monomials as if they were independent variables.

So suppose one has a list of monomials, $$ \{x_1^2 x_2, x_3 x_2 x_1, x_1^2 x_3, \ldots\} $$ Is there a way of changing the attributes of the elements in this list so that one can treat these monomials as single variables? For example, this would allow one to differentiate with respect to a specific monomial, as opposed to a single $x_i$.

I realize this may not be possible in Mathematica but it would greatly simplify my work if this could be done. Thanks for any advice.

$\endgroup$
2
$\begingroup$

Here's a quick answer, not fully tested (as in, there might be edge cases where the generation of the monomial list fails, and perhaps the replacement rules, tool should be pretty robust, though). Let's first generate a random polynomial in the variables {a, b, c}:

SeedRandom[10]
poly = RandomInteger[{-5, 5}, Length@#].# &[Times @@@ Map[Tuples[{a, b, c}, #] &, Range[3]]~Flatten~1 // DeleteDuplicates]
(* 5 a + 2 a^2 + 5 a^3 + 5 b + 3 a b + 3 a b^2 + 2 b^3 + 4 c + a c
       + 2 a^2 c + 5 b c + a b c - b^2 c - 5 c^2 + 3 a c^2 - 5 b c^2 + 2 c^3 *)

Then, make a list or replacement rules. Note that I am using MonomialList with some post-processing to extract the monomials; this isn't necessary if you already have a list of the monomials.

rules = Thread[# -> Array[x, Length@#]] &@MonomialList@poly /. a_?NumericQ x_ :> x
(* {a^3 -> x[1], a^2 c -> x[2], a^2 -> x[3], a b^2 -> x[4], 
    a b c -> x[5], a b -> x[6], a c^2 -> x[7], a c -> x[8], a -> x[9], 
    b^3 -> x[10], b^2 c -> x[11], b c^2 -> x[12], b c -> x[13], 
    b -> x[14], c^3 -> x[15], c^2 -> x[16], c -> x[17]} *)

Finally, use the rules:

transformedpoly = poly /. rules
(* 5 x[1] + 2 x[2] + 2 x[3] + 3 x[4] + x[5] + 3 x[6] + 3 x[7] + x[8] + 
   5 x[9] + 2 x[10] - x[11] - 5 x[12] + 5 x[13] + 5 x[14] + 2 x[15] - 5 x[16] + 4 x[17] *)

We can go back to the original polynomial using Reverse /@ rules:

transformedpoly /. Reverse /@ rules
(* 5 a + 2 a^2 + 5 a^3 + 5 b + 3 a b + 3 a b^2 + 2 b^3 + 4 c + a c
       + 2 a^2 c + 5 b c + a b c - b^2 c - 5 c^2 + 3 a c^2 - 5 b c^2 + 2 c^3 *)
$\endgroup$
  • $\begingroup$ I think this is a good strategy -- I used it and it worked well enough. Thanks very much. $\endgroup$ – oscarafone Sep 25 '17 at 23:57

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.