Skip to content

Instantly share code, notes, and snippets.

@ww24
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ww24/11142386 to your computer and use it in GitHub Desktop.
Save ww24/11142386 to your computer and use it in GitHub Desktop.
ロジスティック写像の分岐図
/**
* ロジスティック写像
* 周期倍加分岐図
*/
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();
}
# ロジスティック写像
# 周期倍加分岐図
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()
@ww24
Copy link
Author

ww24 commented Apr 21, 2014

速度比較

R --vanilla --slave < logistic_map.R  58.93s user 0.13s system 99% cpu 59.181 total
node logistic_map.js  10.32s user 0.47s system 100% cpu 10.728 total

ループと再帰で実装が異なるので単純には比較できないが、明らかに R は遅い。
※ R で再帰的な実装にするとエラーを吐いて止まる。

出力結果 (R)

分岐図

@ww24
Copy link
Author

ww24 commented Apr 21, 2014

ループ内で呼び出す関数 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