Last active
August 29, 2015 14:00
-
-
Save ww24/11142386 to your computer and use it in GitHub Desktop.
ロジスティック写像の分岐図
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ロジスティック写像 | |
* 周期倍加分岐図 | |
*/ | |
var Canvas = require("canvas"), | |
fs = require("fs"); | |
// 設定 | |
/*==========================*/ | |
var a = 2.9; | |
// a の増加間隔 | |
var da = 0.0001; | |
// a の最大値 | |
var a_max = 4.0; | |
// 初期値 | |
var x0 = 0.1; | |
// グラフにプロットする t の範囲 | |
var plot_t_range = [1900, 2000]; | |
// グラフの表示倍率 (逆数) | |
var scale = da * 10; | |
/*==========================*/ | |
var size_x = (a_max - a) / scale, | |
size_y = (a_max - a) / scale; | |
var canvas = new Canvas(size_x, size_y), | |
ctx = canvas.getContext("2d"); | |
// plot | |
for (var xa = a; xa <= a_max; xa += da) { | |
logistic(xa, x0, plot_t_range[1], function (x, y, t) { | |
if (t >= plot_t_range[0]) { | |
plot(x / scale, y / scale, "rgba(0, 0, 255, 0.5)"); | |
} | |
}); | |
} | |
// write out | |
var out_stream = fs.createWriteStream("logistic.png"); | |
canvas.pngStream().pipe(out_stream); | |
function logistic(a, x0, t, p) { | |
xt = t ? logistic(a, x0, t - 1, p) : x0; | |
p && p(a, xt, t); | |
return a * xt * (1 - xt); | |
} | |
function plot(x, y, style) { | |
y = size_y - y; | |
x = x - a / scale; | |
ctx.beginPath(); | |
ctx.arc(x, y, 1, 0, Math.PI * 2, false); | |
ctx.fillStyle = style; | |
ctx.fill(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ロジスティック写像 | |
# 周期倍加分岐図 | |
require(compiler) | |
# 設定 | |
x0 <- 0.1 | |
aR <- 29000:40000 / 10000 | |
tR <- 1900:2000 | |
logistic <- function (a, tR) { | |
xt <- x0 | |
res <- c() | |
min_tR <- min(tR) | |
for (t in 1:max(tR)) { | |
xt <- a * xt * (1 - xt) | |
if (t > min_tR) { | |
res <- append(res, xt) | |
} | |
} | |
return(res) | |
} | |
logistic <- cmpfun(logistic) | |
png('logistic_map.png', pointsize=20, width=1000, height=1000) | |
plot(NA, xlim=c(min(aR), max(aR)), ylim=0:1, xlab="a", ylab="x") | |
len <- length(tR) - 1 | |
for (a in aR) { | |
points(rep(a, len), logistic(a, tR), cex=0.01) | |
} | |
dev.off() |
ループ内で呼び出す関数 logistic
の無駄な処理を省くことで高速化。
R --vanilla --slave < logistic_map.R 20.50s user 0.11s system 99% cpu 20.684 total
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
速度比較
ループと再帰で実装が異なるので単純には比較できないが、明らかに R は遅い。
※ R で再帰的な実装にするとエラーを吐いて止まる。
出力結果 (R)