1
$\begingroup$

Suppose I have a function say $f=x^2$. What I would like is a list (or even better, a list with a grid) which resembles {{1,1},{2,4},{3,9}, ...}. Ideally one should be able to specify the starting point, end point and step interval between values.

This seem like such a simple idea I don't know why I am struggling with it...

$\endgroup$
4
  • $\begingroup$ Please have a look at Table. $\endgroup$ Commented Jul 20, 2013 at 17:38
  • $\begingroup$ Yes, I am aware of table; and have managed to do almost what I desire, but I only obtain one set of values. I.e: {1,4,9,...} as opposed to {{1,1},{2,4},{3,9}} $\endgroup$
    – Ronan
    Commented Jul 20, 2013 at 17:39
  • $\begingroup$ @Ronan Table[{x, x^2}, {x, 5}] ? $\endgroup$
    – rm -rf
    Commented Jul 20, 2013 at 17:44
  • $\begingroup$ ... Ok, yes I am that oblivious. Thank you. $\endgroup$
    – Ronan
    Commented Jul 20, 2013 at 17:48

3 Answers 3

3
$\begingroup$

Using Mathematica's Map function, you can do it this way:

f[x_] := x^2

xList = Range[10]

(* ==> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} *)

values = Map[f, xList]

(* ==> {1, 4, 9, 16, 25, 36, 49, 64, 81, 100} *)

grid = Transpose[{xList, values}]

(*
==> {{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 
  49}, {8, 64}, {9, 81}, {10, 100}}
*)
$\endgroup$
1
  • $\begingroup$ Perfect! I had not even thought of mapping onto the xaxis. This is exactly what I wanted. Many Thanks $\endgroup$
    – Ronan
    Commented Jul 20, 2013 at 17:42
6
$\begingroup$

Many functions, such as the one you've defined

f[x_]:=x^2

are automatically listable. This means that you can get a list of the values directly using

f[Range[10]]

to get the f applied to the first 10 integer values. To get the form you desire, a listing of {x_value, f[x_value]} pairs can be constructed straightforwardly as

Transpose[{Range[10], f[Range[10]]}]

or somewhat more concisely as:

{#, f[#]} & /@ Range[10]
$\endgroup$
2
$\begingroup$

With f already defined:

f = #^2 &;

The most direct way is perhaps Array:

Array[{#, f@#} &, 3]

Of course Table may be used as well, as R.M proposed:

Table[{i, f@i}, {i, 3}]

Better for performance is the method using Transpose, but with the given function there is an additional optimization that may be made. The function Power, of which f is composed, is a Listable function and has optimizations for application to vectors and arrays. We could therefore write:

{#, f@#}\[Transpose] & @ Range@3
{{1, 1}, {2, 4}, {3, 9}}

Note that there is no Map here; that is handled by Power itself. See: Alternatives to procedural loops and iterating over lists in Mathematica for some additional examples.

For formatting "a list with a grid" you should look at Grid, TableForm, and MatrixForm. Be aware that these are formatting wrappers(1),(2) that affect output generation but remain part of the expression.

For the sake of formatting alone you may not even need Transpose:

TableForm[{#, f@#} &@Range@3]

TableForm[{{#, f@#} &@Range@3}]  (* note the extra set of {} *)

enter image description here

enter image description here


p.s. Another user posted, then deleted, an answer using MapIndexed, but the form didn't quite make sense. While is it not needed here I suspect that something like this was the intent:

MapIndexed[{First @ #2, #} &, Array[f, 3]]
$\endgroup$
5
  • $\begingroup$ You may want to add another way {x,x^2}/.{x->#}&/@Range@3. $\endgroup$
    – Kuba
    Commented Jul 21, 2013 at 9:24
  • 1
    $\begingroup$ @Kuba That doesn't use f however. Shorter would be merely {#, #^2}& ~Array~ 3 if we allow such changes. $\endgroup$
    – Mr.Wizard
    Commented Jul 21, 2013 at 9:40
  • $\begingroup$ So put there f :) f = #^2 &; {x, f[x]} /. {x -> #} & /@ Range@3 My point was to use ReplaceAll in that way it create an array. I know it is not useful but another method just for fun since it is not closed :) $\endgroup$
    – Kuba
    Commented Jul 21, 2013 at 9:42
  • 1
    $\begingroup$ How about MapIndexed[{Tr@#2, #} &, Array[f, 3]]? $\endgroup$
    – chyanog
    Commented Jul 21, 2013 at 9:54
  • $\begingroup$ @chyanog That's clever! :-) I usually use e.g. #2[[1]] which looks a bit better and is equally terse (but longer to write) with the special characters in a Notebook. In MapIndexed I usually try to preserve extensibility to deeper levels and Tr would change behavior there whereas First would not. $\endgroup$
    – Mr.Wizard
    Commented Jul 21, 2013 at 10:48

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.