時序實驗
你沒有指定時間限制,所以我做了一個小實驗,一個不錯的集成包。
沒有優化,如果立方體密度是直接函數,那麼在標準筆記本電腦中,每個球面座標積分可以在0.005秒內評估。
只是作爲參考,這是在數學程序:
[email protected];
(* Define a cuboid as density function *)
iP = IntegerPart;
f[{x_, y_, z_}, {lx_, ly_, lz_}] := iP[x - lx] + iP[y - ly] + iP[z - lz] /;
lx <= x <= lx + 3 && ly <= y <= ly + 3 && lz <= z <= lz + 3;
f[{x_, y_, z_}, {lx_, ly_, lz_}] := Break[] /; True;
Timing[Table[s = RandomReal[{0, 3}, 3]; (*sphere center random*)
sphereRadius = Min[Union[s, 3 - s]]; (*max radius inside cuboid *)
NIntegrate[(f[{x, y, z} - s, -s] /. (*integrate in spherical coords *)
{x -> r [email protected] [email protected],
y -> r [email protected] [email protected],
z -> r [email protected]}) r^2 [email protected],
{r, 0, sphereRadius}, {th, 0, 2 Pi}, {phi, 0, Pi}],
{10000}]][[1]]
結果是52秒爲10^4次迭代。
因此,也許你並不需要優化了很多...
「然後小半徑」應該是「比半徑」。比較用「than」,然後用「時間順序」。 – 2010-12-10 19:03:23
如果立方體是原生的......只需加上立方體的質量即可。如果立方體不均勻,讓我們看看密度公式。您不需要CoM來計算質量。 – 2010-12-10 19:08:16
我認爲這不是一個真正的問題,除非指定了立方體的密度。 – 2010-12-10 19:17:58