1
$\begingroup$
Clear[Hmp$, "*Hmp*", m, n, p, Nmax, "ψ*"]
Nmax = 10.; (*want 80*)
Hmp = Table[0, {p, Nmax}, {m, Nmax}];

Chop[ReplacePart[Hmp, {m_, p_} :> (p^2 π^2)/2 KroneckerDelta[m, p] + 
  2 Integrate[Sin[m π χ] Sin[p π χ] V[χ], {χ, 0, 1}]]]
$\endgroup$
4
  • 1
    $\begingroup$ Is V a defined function? $\endgroup$
    – Karsten7
    Commented Mar 14, 2015 at 3:38
  • $\begingroup$ Is there a reason why you use ReplacePart and not just Table or Array? $\endgroup$
    – Karsten7
    Commented Mar 14, 2015 at 3:46
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$
    – bbgodfrey
    Commented Mar 14, 2015 at 4:16
  • $\begingroup$ Chop has no effect on non-approximate results. I'll venture your use implies you don't need exact/symbolic results. If so, something like hmp = Table[(p^2 \[Pi]^2)/2 KroneckerDelta[m, p] + 2 NIntegrate[ Sin[m \[Pi] \[Chi]] Sin[p \[Pi] \[Chi]] v[\[Chi]], {\[Chi], 0, 1}, Method -> "DoubleExponential"], {m, nmax}, {p, nmax}] should be much faster. Depends on what V is, as noted you've not supplied sufficient information. Also, bad idea in general to use uppercase initials on user-defined symbols/functions/etc. $\endgroup$
    – ciao
    Commented Mar 14, 2015 at 6:34

1 Answer 1

6
$\begingroup$

By taking advantage of the trigonometric identity,

2 Sin[m π χ] Sin[p π χ] == Cos[(m - p) π χ] - Cos[(m + p) π χ]

the number of integrals to be performed can be reduced from Nmax^2 to 2*Nmax+1, as savings of nearly a factor of 40 for Nmax = 80.

Nmax = 80;
dct = Table[Integrate[Cos[i π χ] V[χ], {χ, 0, 1}], {i, 0, 2 Nmax}];
Hmp = Table[(p^2 π^2)/2 KroneckerDelta[m, p] + 
        dct[[Abs[m - p] + 1]] - dct[[m + p + 1]], {p, Nmax}, {m, Nmax}]

Additionally, if V[χ] is known at the time that dct is calculated, then

dct = FourierCosSeries[V[χ], χ, 2 Nmax, FourierParameters -> {1, π}] 

can be used instead.

$\endgroup$

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.