# Simple question about list manipulation

Let

 list=Table[RandomChoice[{a, b, c, d}, 2], {4}]


{{b, c}, {c, d}, {b, b}, {c, d}}

I want to get the following list from the given list

$$\lbrace\sqrt{b^3+c^3},\sqrt{c^3+d^3}, \sqrt{b^3+b^3} , \sqrt{c^3+d^3} \rbrace$$

How can I use pure function #&?

I tried something like this

 Sqrt[Plus @@ Map[#^3 &, list]]


but it doesn't work...

• Sqrt[Plus @@@ (#^3)] &[list] or Sqrt[Total[Transpose[#^3]]] &[list]?
– kglr
Feb 4, 2017 at 11:56
• ... or ☺ = +## & @@@ (#^3)^(1/2) &; ☺ @ list:)
– kglr
Feb 4, 2017 at 12:18
• Sqrt[Plus @@ (#^3)] &/@ list Dec 21, 2017 at 8:58

☺ = √+## & @@@ (#^3) &;

☺ @ {{b, c}, {c, d}, {b, b}, {c, d}}


Also:

√Tr/@(#^3)& (* thanks: @Mr.Wizard *)
Sqrt[Total[#^3, {2}]] &
Sqrt[Total[Transpose[#^3]]] &
Sqrt[Plus @@@ (#^3)] &

• Since you seem to have crafted this answer to please me (thanks!) you missed one of my favorites: Tr. Sqrt[Tr /@ (#^3)] & Feb 5, 2017 at 21:10
• @Mr.Wizard, added the version with Tr.
– kglr
Feb 5, 2017 at 21:25

Assuming you don't to want get results like Sqrt[2] Sqrt[a^3];i.e., that you want two distinct cubic terms, I'm substituting RandomSample for RandomChoice.

SeedRandom[42];
Sqrt[#1^3 + #2^3] &,
Transpose[Table[RandomSample[{a, b, c, d}, 2], {4}]]]


{Sqrt[b^3 + c^3], Sqrt[b^3 + d^3], Sqrt[a^3 + d^3], Sqrt[a^3 + c^3]}

Starting with your list

list = Table[RandomChoice[{a, b, c, d}, 2], {4}]

(* {{b, c}, {c, d}, {b, b}, {c, d}} *)


I often find it useful to break it down in steps.

Step 1. Compute the cube

Map[#^3 &, list]

(* {{b^3, c^3}, {c^3, d^3}, {b^3, b^3}, {c^3, d^3}} *)


Step 2. Apply Plus

Here is a brute force way to use Map on the previous output. @@ is a shortcut for Apply.

Map[Plus @@ # &, Map[#^3 &, list]]

(* {b^3 + c^3, c^3 + d^3, 2 b^3, c^3 + d^3} *)


However there is a nice shortcut shown in kglr's comment. @@@ is a shortcut for Apply acting upon heads located at level 1.

Plus @@@ Map[#^3 &, list]

(* {b^3 + c^3, c^3 + d^3, 2 b^3, c^3 + d^3} *)


Step 3. Compute the square root

By now I think you get the idea. We will use Map to compute the square root of the outputs of step 2.

Map[Sqrt[#] &, Plus @@@ Map[#^3 &, list]]

(* {Sqrt[b^3 + c^3], Sqrt[c^3 + d^3], Sqrt[2] Sqrt[b^3], Sqrt[c^3 + d^3]} *)


In the notebook one sees

Pure functions are counterproductive IMHO when constituent functions (i.e. Power) are Listable:

x = {{b, c}, {c, d}, {b, b}, {c, d}};

√( x^3 .{1,1} )

{Sqrt[b^3 + c^3], Sqrt[c^3 + d^3], Sqrt[2] Sqrt[b^3], Sqrt[c^3 + d^3]}


In 2D notation:

I just realized that Mathematica's automatically copied expression was wrong. My $x^3.\{1,1\}$ became x^3.{1,1} which parses as (x^3.)*{1,1} which of course doesn't work. A space fixes this, which I added above, but I don't think the expression should have been copied this way.