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?


Here's one way...

g[1][x_] := 1;
g[2][x_] := x;
g[3][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}]


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


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

Your Answer

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.