Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. v = Function[x, Evaluate[x^#]] & /@ Range[0, 5];
  2. u = v;
  3. For[i = 2, i < 7, i++,
  4. u[[i]] = Function[x,
  5. v[[i]][x] -
  6. Sum[Integrate[v[[i]][x]*u[[j]][x], {x, -Pi, Pi}]/
  7. Integrate[u[[j]][x]^2, {x, -Pi, Pi}]*u[[j]][x], {j, 1, i - 1}]
  8. ]
  9. ]
  10.  
  11. Orthogonalize[x^Range[5], Integrate[#1*#2, {x, -[Pi], [Pi]}] &]
  12.  
  13. inn = Integrate[#1*#2, {x, -[Pi], [Pi]}] &;
  14. v = x^Range[5];
  15. u = ConstantArray[0, Length@v];
  16. u[[1]] = v[[1]];
  17. Do[
  18. u[[i]] = v[[i]] - Total@Table[
  19. u[[j]]*inn[v[[i]], u[[j]]]/inn[u[[j]], u[[j]]],
  20. {j, 1, i - 1}],
  21. {i, 2, Length@v}
  22. ]
  23.  
  24. u
  25.  
  26. (*{x, x^2, -((3 [Pi]^2 x)/5) + x^3, -(5/7) [Pi]^2 x^2 +
  27. x^4, -((3 [Pi]^4 x)/7) + x^5 -
  28. 10/9 [Pi]^2 (-((3 [Pi]^2 x)/5) + x^3)}*)
  29.  
  30. Outer[inn, u, u] // MatrixForm
  31.  
  32. oneStepOrtogonalizeGen[vec_, {}, _, _, _] := vec;
  33.  
  34. oneStepOrtogonalizeGen[vec_, vecmat_List, dotF_, plusF_, timesF_] :=
  35. Fold[plusF[#1, timesF[-dotF[vec, #2]/dotF[#2, #2], #2]] &, vec, vecmat];
  36.  
  37. GSOrthogonalizeGen[startvecs_List, dotF_, plusF_, timesF_] :=
  38. Fold[Append[#1,oneStepOrtogonalizeGen[#2, #1, dotF, plusF, timesF]] &, {}, startvecs];
  39.  
  40. normalizeGen[vec_, dotF_, timesF_] := timesF[1/Sqrt[dotF[vec, vec]], vec];
  41.  
  42. GSOrthoNormalizeGen[startvecs_List, dotF_, plusF_, timesF_] :=
  43. Map[normalizeGen[#, dotF, timesF] &, GSOrthogonalizeGen[startvecs, dotF, plusF, timesF]];
  44.  
  45. hermiteDot[f_Function, g_Function] :=
  46. Module[{x}, Integrate[f[x]*g[x]*Exp[-x^2], {x, -Infinity, Infinity}]];
  47.  
  48. SetAttributes[functionPlus, {Flat, Orderless, OneIdentity}];
  49. functionPlus[f__Function] := With[{expr = Plus @@ Through[{f}[#]]}, expr &];
  50.  
  51. SetAttributes[functionTimes, {Flat, Orderless, OneIdentity}];
  52. functionTimes[a___, f_Function] /; FreeQ[{a}, # | Function] :=
  53. With[{expr = Times[a, f[#]]}, expr &];
  54.  
  55. In[114]:= hermiteDot[#^2 &, #^4 &]
  56. Out[114]= (15 Sqrt[[Pi]])/8
  57.  
  58. In[107]:= functionPlus[# &, #^2 &, Sin[#] &]
  59. Out[107]= Sin[#1] + #1 + #1^2 &
  60.  
  61. In[111]:= functionTimes[z, #^2 &, x, 5]
  62. Out[111]= 5 x z #1^2 &
  63.  
  64. In[115]:=
  65. results =
  66. GSOrthoNormalizeGen[{1 &, # &, #^2 &, #^3 &, #^4 &}, hermiteDot,
  67. functionPlus, functionTimes]
  68.  
  69. Out[115]= {1/[Pi]^(1/4) &, (Sqrt[2] #1)/[Pi]^(1/4) &, (
  70. Sqrt[2] (-(1/2) + #1^2))/[Pi]^(1/4) &, (2 (-((3 #1)/2) + #1^3))/(
  71. Sqrt[3] [Pi]^(1/4)) &, (Sqrt[2/3] (-(3/4) + #1^4 -
  72. 3 (-(1/2) + #1^2)))/[Pi]^(1/4) &}
  73.  
  74. In[116]:= Through[results[x]]
  75. Out[116]= {1/[Pi]^(1/4),(Sqrt[2] x)/[Pi]^(1/4),(Sqrt[2] (-(1/2)+x^2))/[Pi]^(1/4),
  76. (2 (-((3 x)/2)+x^3))/(Sqrt[3] [Pi]^(1/4)),(Sqrt[2/3] (-(3/4)+x^4-3 (-(1/2)+x^2)))/[Pi]^(1/4)}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement