I'm learning Mathematica and watched a tutorial containing this line:

Array[#1 + 2 #2 &, {4, 4}]

which returns this:

{{3, 5, 7, 9}, {4, 6, 8, 10}, {5, 7, 9, 11}, {6, 8, 10, 12}}

What does Mathematica do here? What numbers does it take for #1 and #2? The tutorial said it takes #1= 1 and #2=2, which returns the number 3, but what does it take for the others?

I hope somebody can help.

New contributor
Martha is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 2
    $\begingroup$ Try Array[#&,3] $\endgroup$ – AccidentalFourierTransform Nov 8 at 22:48
  • 1
    $\begingroup$ Note that 3 corresponds to when #1 and #2 are each filled with 1. $\endgroup$ – Brett Champion Nov 8 at 22:56
  • 1
    $\begingroup$ Framing challenge (regarding the title): #1 is bound in Array[]. The first argument of Array[] is a function or the name of a function, taking as many arguments as the second and subsequent arguments specify nesting. It is not the case that "nothing is defined". $\endgroup$ – Eric Towers 2 days ago

If you want to see what #1 and #2 are, you can use a symbolic function:

Array[f, {4, 4}] // Grid

Or you can list them:

Array[{#1, #2} &, {4, 4}] // Grid

When nothing is defined, what does Mathematica take for #1?

In the example Array[#1 + 2 #2 &, {4, 4}], the function #1 + 2 #2 & is not being called. Rather, it is being passed itself as an argument to Array. That is why there are no arguments within square brackets following it.

The documentation of Array tells us that a function passed in this way will be called once for every element in the result array. So ultimately the function will be called many times, each call receiving a set of array indices and expected to return a value for the corresponding array element.

What numbers does it take for #1 and #2?

The Array expression is creating a two-dimensional matrix so the arguments to the function will be the row and column indices into that matrix. That is, #1 is the row number and #2 is the column number.

We can see this more directly with an expression like this:

Array[a[#1, #2] &, {4, 4}] // MatrixForm

matrixform image

If we compare this matrix to the result shown below for the original expression, then we will see that each element in the lower picture is equal to the sum of the row index plus two times the column index from the upper picture:

Array[#1 + 2 #2 &, {4, 4}] // MatrixForm

matrixform image

You will sometimes see expressions like a[#1, #2] written as a[#, #2] or a[##]. In the former case, # is a short form of #1. In the latter case, ## can be read as "all arguments".

When nothing is defined, what does Mathematica take for #1? (Redux)

If a function is actually called with too few arguments, then error messages will be issued. For example:

f = #1 + #2&


error message screenshot

If too many arguments are specified, the excess arguments are quietly ignored:

f[1, 3, 5, 7]
(* 4 *)

I do not really understand the question. But maybe you get an idea by inspecting the following code and by observing that each line returns the same ouput.

Array[#1 + 2 #2 &, {4, 4}]
Table[#1 + 2 #2 &[i, j], {i, 1, 4}, {j, 1, 4}]
Table[i + 2 j, {i, 1, 4}, {j, 1, 4}]

Your Answer

Martha is a new contributor. Be nice, and check out our Code of Conduct.

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.