標題: [心得] Spinski Triangle with CompilationTarget->"C"
時間: Tue Jun 28 16:15:18 2011
Mathematica 較為人詬病的就是計算速度及不可編譯為執行檔。
但這在7.0及8.0之後的Compile及平行運算大致已獲得解決。
$Version
newpt[x_List] := {{x[[1]], Mean@{x[[1]], x[[2]]},
Mean@{x[[1]], x[[3]]}}, {x[[2]], Mean@{x[[2]], x[[1]]},
Mean@{x[[2]], x[[3]]}}, {x[[3]], Mean@{x[[3]], x[[1]]},
Mean@{x[[3]], x[[2]]}}};
(*不編譯程式*)
Stgraph1[pt_List, n_Integer] :=
Nest[Flatten[Map[newpt, #], 1] &, {pt}, n];
(* 編譯程式 *)
Stgraph2 =
Compile[{{pt, _Real, 2}, {n, _Integer}},
Nest[Flatten[
Map[{{#[[1]], Mean@{#[[1]], #[[2]]},
Mean@{#[[1]], #[[3]]}}, {#[[2]], Mean@{#[[2]], #[[1]]},
Mean@{#[[2]], #[[3]]}}, {#[[3]], Mean@{#[[3]], #[[1]]},
Mean@{#[[3]], #[[2]]}}} &, #], 1] &, {pt}, n]];
(*由 C Compiler 編譯程式*)
Stgraph3 =
Compile[{{pt, _Real, 2}, {n, _Integer}},
Nest[Flatten[
Map[{{#[[1]], Mean@{#[[1]], #[[2]]},
Mean@{#[[1]], #[[3]]}}, {#[[2]], Mean@{#[[2]], #[[1]]},
Mean@{#[[2]], #[[3]]}}, {#[[3]], Mean@{#[[3]], #[[1]]},
Mean@{#[[3]], #[[2]]}}} &, #], 1] &, {pt}, n],
CompilationTarget -> "C"];
pt = {{0, 0}, {2, 0}, {1, Sqrt[3]}};
First@AbsoluteTiming@Stgraph1[pt, 8]
First@AbsoluteTiming@Stgraph2[pt, 8]
First@AbsoluteTiming@Stgraph3[pt, 8]
Graphics[{Red, Polygon /@ Stgraph3[pt, 8]}]
--
養花種魚數月亮賞星星
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.232.166.217
沒有留言:
張貼留言