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}]]]
1 Answer
$\begingroup$
$\endgroup$
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.
V
a defined function? $\endgroup$ReplacePart
and not justTable
orArray
? $\endgroup$Chop
has no effect on non-approximate results. I'll venture your use implies you don't need exact/symbolic results. If so, something likehmp = 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$