2
$\begingroup$

Assume I have the the following list of data:

valuesOld = {5, 10, 15, 20, 25, 35, 40, 45, 55, 65, 80};

I would like to construct the following list:

valuesNew = {{Sqrt[5], Sqrt[5]}, {Sqrt[10], Sqrt[10]}, ..., {Sqrt[80], Sqrt[80]}}

Can I, without having to type everything manually, create valuesNew from valuesOld and then plot valuesNew as points?

$\endgroup$
2
  • 1
    $\begingroup$ Best to use one of the solutions that takes advantage of Sqrt being Listable $\endgroup$ Commented Sep 29, 2014 at 22:24
  • 1
    $\begingroup$ {#, #}\[Transpose] & @ Sqrt[v] $\endgroup$
    – Mr.Wizard
    Commented Sep 29, 2014 at 22:35

4 Answers 4

5
$\begingroup$
valuesNew  = Replace[valuesOld, x_ :> {Sqrt[x], Sqrt[x]}, {1, Infinity}]
{{Sqrt[5], Sqrt[5]}, {Sqrt[10], Sqrt[10]}, {Sqrt[15], Sqrt[15]}, 
 {2 Sqrt[5], 2 Sqrt[5]}, {5, 5}, {Sqrt[35], Sqrt[35]},  {2 Sqrt[10], 2 Sqrt[10]},
 {3 Sqrt[5], 3 Sqrt[5]}, {Sqrt[55], Sqrt[55]},  {Sqrt[65], Sqrt[65]}, {4 Sqrt[5], 4 Sqrt[5]}}

Then:

ListPlot[valuesNew]
$\endgroup$
0
6
$\begingroup$
valuesOld = {5, 10, 15, 20, 25, 35, 40, 45, 55, 65, 80};

valuesNew1 = Sqrt[{#, #} & /@ valuesOld];
valuesNew2 = {#, #} & /@ Sqrt[valuesOld];
valuesNew3 = Table[{i, i}, {i, Sqrt[valuesOld]}];
valuesNew4 = Transpose[Sqrt[{#, #}]]& @ valuesOld;
valuesNew5 = valuesOld /. x_Integer :> ({1, 1} Sqrt[x]);
valuesNew6 = valuesOld /. x_ :> Sqrt[{x, x}] // Transpose;

Equal @@ {valuesNew1, valuesNew2, valuesNew3, valuesNew4, valuesNew5, valuesNew6}
(* True *)
valuesNew1
{{Sqrt[5], Sqrt[5]}, {Sqrt[10], Sqrt[10]}, {Sqrt[15], Sqrt[15]},   
 {2 Sqrt[5], 2 Sqrt[5]}, {5, 5}, {Sqrt[35], Sqrt[35]}, {2 Sqrt[10], 2 Sqrt[10]}, 
 {3 Sqrt[5], 3 Sqrt[5]}, {Sqrt[55], Sqrt[55]}, {Sqrt[65], Sqrt[65]}, 
 {4 Sqrt[5], 4 Sqrt[5]}}

Timings:

vN1 = Sqrt[{#, #} & /@ #] &;
vN2 = {#, #} & /@ Sqrt[#] &;
vN3 = Table[{i, i}, {i, Sqrt[#]}] &;
vN4 = Transpose[Sqrt[{#, #}]] &@# &;
vN5 = # /. x_Integer :> ({1, 1} Sqrt[x]) &;
vN6 = (# /. x_ :> Sqrt[{x, x}] // Transpose) &;
vNR = Replace[#, x_ :> {Sqrt[x], Sqrt[x]}, {1, Infinity}] &; (* RunnyKine *)
vNM = Sqrt /@ {#, #} & /@ # &;  (* mete *)
vNA = Append[{Sqrt[#]}, Sqrt[#]] & /@ # &; (* algohi *)
vNW = {#, #}\[Transpose] &@Sqrt[#] &; (* Mr.Wizard -- suggested in comments *)

functions = {vN1, vN2, vN3, vN4, vN5, vN6, vNR, vNM, vNA, vNW};
flabels = {"vN1", "vN2", "vN3", "vN4", "vN5", "vN6", "vNR", "vNM", "vNA","vNW"};

vO1 = RandomInteger[1000, {10000}];
vO2 = RandomInteger[1000, {100000}];
vO3 = RandomInteger[1000, {1000000}];
{Equal @@ (#@vO1 & /@ functions),Equal @@ (#@vO2 & /@ functions), Equal @@ (#@vO3 & /@ functions)}
(* {True, True, True} *)

aT = AbsoluteTiming;
{#1, First[aT[#2[vO1];]],First[aT[#2[vO2];]], First[aT[#2[vO3];]]} & @@@
  Transpose[{flabels, functions }] // Grid[Prepend[#, {"function" , "vO1", "vO2", "vO3"}]] &

enter image description here

$\endgroup$
1
  • $\begingroup$ Really nice solution, too! $\endgroup$ Commented Sep 29, 2014 at 21:16
4
$\begingroup$

Similar to one already proposed, but I think it works for non listable functions as well

Sqrt /@ {#, #} & /@ valuesOld
$\endgroup$
0
2
$\begingroup$

Just something different :-)

Append[{Sqrt[#]}, Sqrt[#]] & /@ valuesOld
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.