halfIntegerQ[j_] := IntegerQ[2 j] && j >= 1/2;
jplus[jmax_?halfIntegerQ] :=
SparseArray[
Band[{1, 2}] ->
Table[N@Sqrt[jmax (jmax + 1) - m (m + 1)], {m,
jmax - 1, -jmax, -1}], {2 jmax + 1, 2 jmax + 1}];
jminus[jmax_?halfIntegerQ] := Transpose@jplus[jmax];
jx[jmax_?halfIntegerQ] := (jplus[jmax] + jminus[jmax])/2;
jy[jmax_?halfIntegerQ] := (jplus[jmax] - jminus[jmax])/(2 I);
jz[jmax_?halfIntegerQ] :=
SparseArray[
Band[{1, 1}] -> N@Range[jmax, -jmax, -1], {2 jmax + 1,
2 jmax + 1}];
jid[jmax_?halfIntegerQ] := IdentityMatrix[2 jmax + 1, SparseArray];
hamiltonian[h_, spinNumber_?IntegerQ] := -h*jx[spinNumber/2] -
MatrixPower[jx[spinNumber/2], 2]/spinNumber;
initialState[spinNumber_?IntegerQ] :=
Block[{\[CapitalPi], e, v, psi01, psi02}, \[CapitalPi] =
MatrixExp[
I*Pi*(spinNumber/2*jid[spinNumber/2] + jz[spinNumber/2])];
{e, v} = Eigensystem[jx[spinNumber/2], 2];
{psi01, psi02} = {Normalize[v[[1]] + v[[2]]],
Normalize[v[[1]] - v[[2]]]};
If[Total@Abs[\[CapitalPi] . psi01 - psi01] < 10.^(-5), psi01,
psi02]];
ketToRho[psi_] := Outer[Times, psi, Conjugate@psi];
problem[rho_?MatrixQ] :=
Block[{jmax, opj, \[CapitalDelta], points, grid, data, ifun, w0,
w1}, jmax = (First@Dimensions@rho - 1)/2;
opj[\[Phi]_, \[Theta]_] :=
Sin[\[Theta]]*Cos[\[Phi]]*jx[jmax] +
Sin[\[Theta]]*Sin[\[Phi]]*jy[jmax] + Cos[\[Theta]]*jz[jmax];
\[CapitalDelta][index_] := \[CapitalDelta][index] =
Block[{m = index - 1 - jmax}, (1./(2.*jmax + 1.))*
Sum[(2 l + 1)*ClebschGordan[{jmax, m}, {l, 0}, {jmax, m}], {l,
0, 2*jmax, 1}]];
points[\[Phi]_, \[Theta]_] :=
Block[{e, v, kernel}, {e, v} =
Transpose@
SortBy[Transpose@Eigensystem[opj[\[Phi], \[Theta]]], First];
kernel =
Total@MapIndexed[\[CapitalDelta][First[#2]]*
Outer[Times, #1, Conjugate@#1] &, v];
Re@Tr[rho . kernel]];
grid =
Flatten[CoordinateBoundsArray[{{0., 2.*Pi}, {0, 1.*Pi}}, {Into[
401], Into[201]}], 1];
data = Parallelize@MapApply[{##, Quiet@points[##]} &, grid];
ifun = Interpolation[data];
w0 = (2.*jmax + 1.)/(4*Pi)*
Quiet@NIntegrate[
ifun[\[Phi], \[Theta]]*Sin[\[Theta]], {\[Phi], 0,
2 Pi}, {\[Theta], 0, Pi}];
w1 = 0.5*(2.*jmax + 1.)/(4*Pi)*(Quiet@
NIntegrate[
Abs[ifun[\[Phi], \[Theta]]]*Sin[\[Theta]], {\[Phi], 0,
2 Pi}, {\[Theta], 0, Pi}]) - 0.5;
{w0, w1}];
test[n_?IntegerQ] := problem@ketToRho@initialState[n];
我的问题: 第一个积分w0正确的结果是1, 以这个结果为准绳的话, 上面的代码只有在首次运算时才能返回正确的结果, 我想不明白错误是如何产生的