[心得] Spinski Triangle with CompilationTarget->"C"

看板Mathematica作者 (養花種魚數月亮賞星星)時間13年前 (2011/06/28 16:15), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/1
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]}] -- 養花種魚數月亮賞星星 http://chungyuandye.twbbs.org -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.232.166.217

06/28 18:00, , 1F
老師來個平行運算教學吧^^...我試過但是好像沒成功
06/28 18:00, 1F
文章代碼(AID): #1E2OsSvG (Mathematica)
文章代碼(AID): #1E2OsSvG (Mathematica)