6

I wonder if the functionality exists to explode a sum, or product? What I mean by that is the following. Imagine, you have input data

data = a + b c - d e f ;

Is there a function ExplodeSum such that

res1 = ExplodeSum[data]
*out*
{a,b c,-d e f}

is the result (arbitrary but fixed order)? And then, is there a function, such that

res2 = ExplodeProduct[ res1[[2]] ]
*out*
{b,c}

is the result? If yes, please let me know how those functions are actually called. Or maybe one can implement these functions? Thanks for any suggestion!

Kagaratsch
  • 11,955
  • 4
  • 25
  • 72
  • Related: (2449) (see item #3). Also related: (55597) – Mr.Wizard Oct 08 '14 at 00:55
  • I found a similar question: (25338). Unless someone argues for this question being unique I shall be closing it as a duplicate of that one. – Mr.Wizard Oct 08 '14 at 01:02
  • Thanks to everyone for the solutions! My question has been answered. (@Mr.Wizard: The problem with these kind of questions is that one does not know exactly what to search for. So it is easier to ask again. - Now, I'm sure the next person searching for "explode sum" will find this question here, so it is not all in vain.) – Kagaratsch Oct 08 '14 at 15:13
  • I did not mean to suggest that you should not have asked this Question. While it is always appreciated if one searches first I am fully aware that even extensive searching may not reveal existing questions that can be answered in a similar way. You are correct that your question, even though closed now, will help people find the answer they need, here or in the linked Q&A. – Mr.Wizard Oct 08 '14 at 16:08

4 Answers4

9
res1 = data /. Plus -> List
 {a, b c, -d e f}
res1[[2]] /. Times -> List
{b, c}

As a function:

explodeOp[expr_, op_] := expr /. op -> List

Then:

res1 = explodeOp[data, Plus];
explodeOp[res1[[2]], Times]
RunnyKine
  • 33,088
  • 3
  • 109
  • 176
4

Try this:

List @@ (a + b c - d e ff)

Output is

{a, b c, -d e ff}

Likewise, to explode a product, do this:

List @@ %[[2]]

Output is

{b, c}

This works because List @@ deletes the head of (a + b c - d e ff), which is Plus, and replaces it with list, giving List[a, b c, -d e ff] (and similar for product explosion).

DumpsterDoofus
  • 11,857
  • 1
  • 29
  • 49
1
Cases[a + b c - d e f, _]
(*{a, b c, -d e f}*)

Level[a + b c - d e f, 1]
(*{a, b c, -d e f}*)
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
1

You can also set Part 0 of input list to List:

ClearAll[data, a, b, c, d, e, f];
data = a + b c - d e f;

data2=data;
data2[[0]] = List; 
data2
(* {a, b c, -d e f} *)

data2 = data[[2]];
data2[[0]] = List;
data2
(* {b,c} *)

or, define a function that Listifies the Heads:

ClearAll[lF1];
lF1 = Function[{h}, h[[0]] = List; h, HoldFirst];

data2 = data;
{lF1@data2, lF1@data2[[2]]}
(* {{a, b c, -d e f}, {b, c}}  *)

You can also use: MapAt or ReplacePart to change the Heads to List:

ClearAll[lF2];
lF2 = MapAt[List &, #, {0}] &;

data2 = data;
{lF2@data2, lF2@data2[[2]]}
(* {{a,b c,-d e f},{b,c}} *)

data2 = data;
{ReplacePart[data2, 0 -> List], ReplacePart[data2[[2]], 0 -> List]}
(* {{a, b c, -d e f}, {b, c}} *)
kglr
  • 394,356
  • 18
  • 477
  • 896