如何使用Dynamic改进此Mathematica 9代码以加快计算速度。 在我的电脑上,这段代码与" n"少于13否则结果是" $ Aborted",但我需要n = 20。 例如,您可以使用rho = 0.64和phi = 1.107 我想在打开的html文件中输入参数时自动计算,甚至更好,这是一个按钮"计算"。
在浏览器中查看
起初我需要解决
Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) && (yy ==
Sin[\[Rho]] Sin[\[Phi]]) && (zz == Cos[\[Rho]]), {xx, yy, zz}]
然后做"替换"每个变量的三倍
" 1"
Replace[xx,
First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) && (yy ==
Sin[\[Rho]] Sin[\[Phi]]) && (zz == Cos[\[Rho]]), {xx, yy,
zz}]][[1]]]
" 2"
Replace[yy,
First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) && (yy ==
Sin[\[Rho]] Sin[\[Phi]]) && (zz == Cos[\[Rho]]), {xx, yy,
zz}]][[2]]]
" 3"
Replace[zz,
First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) && (yy ==
Sin[\[Rho]] Sin[\[Phi]]) && (zz == Cos[\[Rho]]), {xx, yy,
zz}]][[3]]]
事实上,我把它写在一个"动态"因为我不知道如何使它正确 - 所以在html文件中自动计算输入变量值后的所有
" 1"" 2"" 3"在代码中是上述
的首字母缩略词Dynamic[
Cases[
Drop[Tuples[Range[-n, n], 3], {(
Length[Tuples[Range[-n, n], 3]] + 1)/2}], {h_, k_,
l_} /;
("1" -"1"/10) <= h/(
GCD[h, k, l] Sqrt[
h^2 + k^2 +
l^2]) <= ("1" +"1"/10) \[And] ("2" -"2"/10) <= k/(
GCD[h, k, l] Sqrt[
h^2 + k^2 +
l^2]) <= ("2" +"2"/10) \[And] ("3" -"3"/10) <= l/(
GCD[h, k, l] Sqrt[
h^2 + k^2 +
l^2]) <= ("3" +"3"/10)]]
如果表示
&#34; 11&#34; -
h/(GCD[h, k, l] Sqrt[h^2 + k^2 + l^2])
&#34; 22&#34; -
k/(GCD[h, k, l] Sqrt[h^2 + k^2 + l^2])
&#34; 33&#34; -
l/(GCD[h, k, l] Sqrt[h^2 + k^2 +l^2])
然后代码是:
Dynamic[
Cases[
Drop[
Tuples[
Range[-n, n], 3], {(Length[Tuples[Range[-n, n], 3]] + 1)/2}], {h_, k_, l_} /;
("1" -"1"/10) <= "11" <= ("1" +"1"/10)
\[And]
("2" -"2"/10) <= "22" <= ("2" +"2"/10)
\[And]
("3" -"3"/10) <= "33" <= ("3" +"3"/10)]]
此代码可以根据需要使用。谢谢你的兴趣!
Column[{Style["Определить hkl", Bold, 16],
Labeled[InputField[Dynamic[\[Rho]]], "\[Rho]", Left,
LabelStyle -> Directive[Bold, FontSize -> 18]],
Labeled[InputField[Dynamic[\[Phi]]], "\[Phi]", Left,
LabelStyle -> Directive[Bold, FontSize -> 18]],
Labeled[InputField[Dynamic[n]], "n", Left,
LabelStyle -> Directive[Bold, FontSize -> 18]]}]
SetAttributes[redoButton, HoldRest]
redoButton[str_, fun_] :=
DynamicModule[{result = Null}, Column[{Button[str, result = fun]}]]
Column[{redoButton[
"x,y,z", {px =
Replace[xx,
First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) && (yy ==
Sin[\[Rho]] Sin[\[Phi]]) && (zz == Cos[\[Rho]]), {xx, yy,
zz}]][[1]]],
py = Replace[yy,
First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) && (yy ==
Sin[\[Rho]] Sin[\[Phi]]) && (zz == Cos[\[Rho]]), {xx, yy,
zz}]][[2]]],
pz = Replace[zz,
First[Solve[(xx == Sin[\[Rho]] Cos[\[Phi]]) && (yy ==
Sin[\[Rho]] Sin[\[Phi]]) && (zz == Cos[\[Rho]]), {xx, yy,
zz}]][[3]]]}], {Dynamic[px], Dynamic[py], Dynamic[pz]},
redoButton["ppp",
ppp = Part[
Nearest[ReplaceAll[
Drop[Tuples[Range[-n, n], 3], {(
Length[Tuples[Range[-n, n], 3]] + 1)/2}], {h_, k_, l_} -> {h/
Sqrt[h^2 + k^2 + l^2], k/Sqrt[h^2 + k^2 + l^2], l/Sqrt[
h^2 + k^2 + l^2]}], {px, py, pz}], 1]], Dynamic[ppp],
redoButton["hkl",
hkl = MatrixForm[
Cases[Drop[
Tuples[Range[-n, n], 3], {(
Length[Tuples[Range[-n, n], 3]] + 1)/2}], {h_, k_, l_} /;
h/(GCD[h, k, l] Sqrt[h^2 + k^2 + l^2]) == Part[ppp, 1] \[And]
k/(GCD[h, k, l] Sqrt[h^2 + k^2 + l^2]) == Part[ppp, 2] \[And]
l/(GCD[h, k, l] Sqrt[h^2 + k^2 + l^2]) == Part[ppp, 3]]]],
Style[Dynamic[hkl], Bold, 18]}]