7
$\begingroup$

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...

$\endgroup$
  • 4
    $\begingroup$ Sqrt[Plus @@@ (#^3)] &[list] or Sqrt[Total[Transpose[#^3]]] &[list]? $\endgroup$ – kglr Feb 4 '17 at 11:56
  • 3
    $\begingroup$ ... or ☺ = +## & @@@ (#^3)^(1/2) &; ☺ @ list:) $\endgroup$ – kglr Feb 4 '17 at 12:18
  • $\begingroup$ Sqrt[Plus @@ (#^3)] &/@ list $\endgroup$ – Αλέξανδρος Ζεγγ Dec 21 '17 at 8:58
7
$\begingroup$
☺ = √+## & @@@ (#^3) &;

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

Mathematica graphics

Also:

√Tr/@(#^3)& (* thanks: @Mr.Wizard *)
Sqrt[Total[#^3, {2}]] &
Sqrt[Total[Transpose[#^3]]] &
Sqrt[Plus @@@ (#^3)] &
$\endgroup$
  • $\begingroup$ Since you seem to have crafted this answer to please me (thanks!) you missed one of my favorites: Tr. Sqrt[Tr /@ (#^3)] & $\endgroup$ – Mr.Wizard Feb 5 '17 at 21:10
  • $\begingroup$ @Mr.Wizard, added the version with Tr. $\endgroup$ – kglr Feb 5 '17 at 21:25
6
$\begingroup$

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];
MapThread[
  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]}

$\endgroup$
5
$\begingroup$

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

Mathematica graphics

$\endgroup$
4
$\begingroup$

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:

enter image description here

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.

$\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.