2
$\begingroup$

I am trying to separate/split an expression (consisting of polynomials) to a list. However I want to keep the exponents intact. Here is an example:

k1=((x+y)^(2+e) (a+b)^(-1-e) (k - z) )//Expand
k2=k1 /. Times | Plus  | Power -> List

output:

(a + b)^(-1 - e) k (x + y)^(2 + e) - (a + b)^(-1 - e) (x + y)^(2 + e) z
{{{{a, b}, {-1, {-1, e}}}, k, {{x, y}, {2, e}}}, {-1, {{a, b}, {-1, {-1, e}}}, {{x, y}, {2, e}}, z}}

The problem in this approach is it makes list in which it is not apparant to distinguish -e and -1+e .

 -e /. Times | Plus | Power -> List
 -1+e /. Times | Plus | Power -> List

leads to same {-1, e}. For each - sign it creates a list which is unwanted.

In this example,

How do I prevent applying Plus -> List to the exponent?

The reason is I want to finally get back to the following form from each term of the list at the end, i.e.

k2[[1]], k2[[2]] etc.

where

 k2[[1]][[1]] = (a + b)^(-1 - e) 
 k2[[1]][[2]] = (x + y)^(2 + e)
 k2[[1]][[3]] = k

 k2[[2]][[1]] = (a + b)^(-1 - e) 
 k2[[2]][[2]] = (x + y)^(2 + e) 
 k2[[2]][[3]] = -z

EDIT::

The expected final output

 k2={
 {{(a + b),(-1 - e)},{ k },{(x + y),(2 + e)}},
 {{(a + b),(-1 - e)},{-z},{(x + y),(2 + e)}}
    }

or

  k2={
  {{{a , b},{-1, - e}},{ k },{{x , y},{2 , e}}},
  {{{a , b},{-1, - e}},{-z},{{x , y},{2 , e}}}
     }

Such that finally I get each of the two terms ( which are separated by +/-)

  (1.)   (a + b)^(-1 - e) k (x + y)^(2 + e) 
  (2.) - (a + b)^(-1 - e) (x + y)^(2 + e) z

Also from (1.) and (2.) I will get each terms which are multiplied i.e.

  (1.)  (a + b)^(-1 - e),  k,  (x + y)^(2 + e)
  (2.)  (a + b)^(-1 - e), -z, (x + y)^(2 + e)

I find the difficult part is to handle this -ve sign.

$\endgroup$
5
  • 2
    $\begingroup$ does k1 /. {Power[a_, b_] :> Power[a /. Plus | Times -> List, b] , Plus | Times -> List} give what you need? $\endgroup$ – kglr Sep 1 '20 at 23:02
  • $\begingroup$ partially,(I can work on this) since my final goal is to first separate out each terms first by +- and then for each of them I want to find out the terms which are multiplied. Like in this example first k2 = k2[[1]] + k2[[2]]. Then for each k2[[i]] I want to take out each term which is multipled. Like from k2[[1]] I want (a + b)^(-1 - e), (x + y)^(2 + e), k. $\endgroup$ – Boogeyman Sep 1 '20 at 23:08
  • $\begingroup$ can you post the desired output for k2? and for -e /. rule1 and -1+e /. rule2? $\endgroup$ – kglr Sep 1 '20 at 23:13
  • $\begingroup$ @kglr, pls see update. I can infact take the above comment of yours and make it work to get the desired output by just simple final substitution: k2[[1]][[1]] /. {{x_^y_, a_^b_} -> (x+a)^y}. $\endgroup$ – Boogeyman Sep 1 '20 at 23:32
  • $\begingroup$ The problem comes in handling - ve sign in a general case. In your scond list k2[[2]], it contains -1 as one of the list element, which I like to keep with any of the terms. I can probably do a workaround. Basically the top level lists will have same Length. $\endgroup$ – Boogeyman Sep 1 '20 at 23:39
2
$\begingroup$
ReplaceAll[Power -> List] @ Replace[SortBy[Length] /@ (List @@@ List @@ k1), 
   {a_, b_, c___} :> If[a === -1, {{-b}, c}, {{a}, b, c}], 2] 
{{{k}, {a + b, -1 - e}, {x + y, 2 + e}}, 
 {{-z}, {a + b, -1 - e}, {x + y, 2 + e}}}

Use ReplaceAll[Power | Plus -> List] to get

{{{k}, {{a, b}, {-1, -e}}, {{x, y}, {2, e}}},
 {{-z}, {{a, b}, {-1, -e}}, {{x, y}, {2, e}}}}
$\endgroup$

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.