I'm new in Mathematica, so can anyone translate the following code into a simpler form where uses more familiar syntax? my mean is #,&and /@ signs. I read about every sign individually in Mathematica documentation but this code has mixed all of them and is not clear to me. Thanks in advance.

f[n_] := Orthogonalize[r^# & /@ Range[0, n], Integrate[g*#1*#2, {r, -∞, ∞}] &]
  • 1
    $\begingroup$ Why don't you ask the author of the code? $\endgroup$
    – yarchik
    Commented Jul 11, 2020 at 15:17
  • 1
    $\begingroup$ r^# & /@ Range[0, n] = r^Range[0, n]. In this case, you can replace g*#1*#2 by g * ##, but my guess is that it is not more familiar (to you). $\endgroup$
    – Michael E2
    Commented Jul 11, 2020 at 17:27

1 Answer 1


I think it might be helpful to evaluate the arguments on their own and see what happens.

The first argument

r^# & /@ Range[0, n]

outputs (for a specific choice of n)

With[{n = 3}, r^# & /@ Range[0, n]]
(* {1, r, r^2, r^3} *)

which generates a list that you could also get by the following

Table[r^i, {i, 0, n}]

The FullForm of the first argument would be something like

Map[Function[{x}, r^x], Range[0, n]]

note that f /@ list is a shorthand for Map[f, list] and, for the sake of a simple unrelated example, f /@ {1, 2, 3} gives {f[1], f[2], f[3]}. Now back to the problem at hand: The syntax r^# & generates a pure (anonymous) function; you can check that r^# & [exponent] == r^exponent where on the left-hand side the function r^# & is called with the argument exponent. Note that the ampersand & simply tells Mathematica when the pure function definition is complete.

The second argument of Orthogonalize is the inner product with respect to which the vectors should be orthogonalized. It is a (pure) function that takes two arguments. When you have more than one argument in a pure function, the slots are numbered and denoted as #1, #2, etc. A silly example

`Sqrt[#1 + #2] &[a, b] == Sqrt[a + b]`

Now the second argument is written as

`Integrate[g*#1*#2, {r, -∞, ∞}] &`

If you like you can define the function explicitly

myInnerProd[v1_, v2_] := Integrate[g*v1*v2, {r, -∞, ∞}]

and then define

f[n_] := Orthogonalize[Table[r^i, {i,0, n}], myInnerProd]

If you want to understand better the syntax of pure functions read the documentation entry tutorial/FunctionalOperations#17469.

  • $\begingroup$ Thanks, and how #1*#2 works? what does it mean? $\endgroup$
    – Wisdom
    Commented Jul 11, 2020 at 14:35
  • 2
    $\begingroup$ #1 and #2 are slots - anonymous arguments. When you orthogonalize, the process passes the vectors into #1, #2, just like v1*v2 above. One of the simpler ways to orthogonalize is using Gram-Schmidt and even though it's not used here, it explains it quite well. When you see $\langle \mathbf{v},\mathbf{u} \rangle$, you're doing an inner (dot) product. But since our inner product is on functions, not vectors, we do an integral of a product of polynomials over -∞, ∞ weighted by g. I hope that makes sense. $\endgroup$
    – flinty
    Commented Jul 11, 2020 at 14:55
  • 2
    $\begingroup$ These are legitimate questions. Why not properly extend the answer to explain every point? $\endgroup$
    – yarchik
    Commented Jul 11, 2020 at 15:16
  • 1
    $\begingroup$ @flinty Thanks a lot. Now tell me how Mathematica understands where to get #1 and #2? They refer to first and second terms of first argument (r^# & /@ Range[0, n])? and what is the single & at the end? $\endgroup$
    – Wisdom
    Commented Jul 12, 2020 at 2:38
  • 1
    $\begingroup$ & ends the definition of a pure function. So if I do f = # + 1 & that means define a function with an anonymous argument. I can then do f[4] and I get result 5. You should read the tutorial Natas referenced in this answer. On the other point, Mathematica doesn't get #1 and #2 from there exactly. In this case it generates vectors during the orthogonalization process which is kind of complicated to explain here. Read the article about Gram-Schmidt in my earlier comment. It provides #1 and #2 - slots for vectors - for you to use in your own custom inner product, like the Integrate above.. $\endgroup$
    – flinty
    Commented Jul 12, 2020 at 11:54

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.