# Tutorial for Map, Apply, etc

I have this code:

g1[x_] := 1;
g2[x_] := x;
g3[x_] := x^2;
norm[r_, s_] := Integrate[r[x] s[x] + r'[x] s'[x], {x, 0, 1}];
gram = {
{norm[g1, g1], norm[g1, g2], norm[g1, g3]},
{norm[g2, g1], norm[g2, g2], norm[g2, g3]},
{norm[g3, g1], norm[g3, g2], norm[g3, g3]}
}


Which returns:

$$\left( \begin{array}{ccc} 1 & \frac{1}{2} & \frac{1}{3} \\ \frac{1}{2} & \frac{4}{3} & \frac{5}{4} \\ \frac{1}{3} & \frac{5}{4} & \frac{23}{15} \\ \end{array} \right)$$

I wish I could do this computation using Map with just g[x_]:= {1,x,x^2} without loops, but I don't really understand the tricky Mathematica language for such things. I don't really know if I should use Apply or Map or something else.

I have been using Mathematica for a while and I've tried to learn this kind of coding without loops but I'm still like the first day. Watching the examples I saw here is not enough for me. I just can see a bunch of # /& @ without been able to understand it. So I always ended avoiding this chapter.

Anyone knows any tutorial for this particular matter?

• With respect to the title of the question, see section 5.2 of Leonid Shifrin's book or section 5.1 of Robert Wagner's book. Mar 25 '15 at 23:00
• On this site you can find some short animations explaining a few of Mathematica's basic commands (like Map and Apply). Mar 25 '15 at 23:10
• You can read this answer of the Question Scan vs Map vs Apply
– xyz
Mar 26 '15 at 1:34

Here's one way...

g[x_] := 1;
g[x_] := x;
g[x_] := x^2;
norm[r_, s_] := Integrate[r[x]*s[x] + r'[x]*s'[x], {x, 0, 1}];

gram=Array[norm[g[#1], g[#2]] &, {3, 3}]


or...

gram=Table[norm[a, b], {a, {g1, g2, g3}}, {b, {g1, g2, g3}}];


or...

gram=Outer[norm, {g1, g2, g3}, {g1, g2, g3}]