2016年11月18日金曜日

Use CPAN Install a module in to Perl モジュールのインストール

Perlに必要なモジュールをインストールには、CPANを使います。

Fedora 24であれば、以下のコマンドを使います。
 $dnf install perl-CPAN

ただし、なぜか以下のようにperl-Test-Simpleをインストールしておかないと、Perlの基本モジュールとされるTest::Moreがないことになってしまいます。Test::MoreはまたStorableに依存しているようで、Cirucular Dependancyになって、CPANをインストールするだけでは、どんなモジュールでもインストールできません。
 $dnf install perl-Test-Simple

これで、さらにcpanminusというcpanmをインストールすれば、完璧です。
  $cpan cpanminus

2016年8月4日木曜日

How to install perl in a local directory

1. Go to http://www.cpan.org/src/README.html to get the source code of desired version

2.  ./Configure -des -Dprefix=$HOME/localperl

3.  make test

4.   make install

2014年6月20日金曜日

Automatic Generation of Strange Attractorsストレンジアトラクタの自動生成

 以下の式で、XとYの初期値を0.5とし、12個のaをそれぞれ-12から12の範囲内で乱数で発生します。
 ある回数(例えば10000」まで繰り返して、XとYは発散しなければ(例えば<1000)、ストレンジアトラクタです。
Xn+1=a1+a2*Xn+a3*Xn*Xn+a4*Xn*Yn+a5*Yn+a6*Yn*YnYn+1=a7+a8*Xn+a9*Xn*Xn+a10*Xn*Yn+a11*Yn+a12*Yn*Yn
下図は、複数のストレンジアトラクタをオーバーレイして、さらに鏡像で合成したものです。前の記事と同じで、PerlとGDで作成しました。

 因みに、3次元ストレンジアトラクタの一例はこのページにあります。以下の式で計算するそうです。
Rampe1 Attractor
newx=z*sin(a*x)+cos(b*y)
newy=x*sin(c*y)+cos(d*z)
newz=y*sin(e*z)+cos(f*x)
Rampe2 Attractor
newx=z*sin(a*x)+arccos(b*y)
newy=x*sin(c*y)+arccos(d*z)
newz=y*sin(e*z)+arccos(f*x)
Rampe3 Attractor
newx=x*z*sin(a*x)-cos(b*y)
newy=y*x*sin(c*y)-cos(d*z)
newz=z*y*sin(e*z)-cos(f*x)
参考文献
Automatic Generation of Strange Attractors

2014年6月18日水曜日

Chaotic 2D map Tinkerbell カオス・マップ ティンカー・ベル

Xn+1=Xn*Xn-Yn*Yn+a*Xn+b*Yn
Yn+1=2*Xn*Yn+c*Xn+d*Yn
a=0.9, b=-0.6013, c=2.0, d=0.50
xo=-0.72, y0=-0.64
a=0.3, b=0.6000, c=2.0, d=0.27
xo=-0.17, y0=-0.17

 パラメータは以下のものを使うらしいです。
a=0.9, b=-0.6013, c=2.0, d=0.50
a=0.3, b=0.6000, c=2.0, d=0.27
Perlのコードはとても単純だから、Gingerbreadmanのものを少し直せばいいです。

 しかし、上の画像を見れば分かるように、2番目のパラメータでは全然違う振る舞いをし始めます。初期値によって収束しないです。

Chaotic 2D map Gingerbreadman カオス・マップ ジンジャーブレッドマン

範囲 (-38, -38) - (80, 80)をプロットした
ブレッドマンの中心部を切り出し。背景は黒にした

 Gingerbreadman map(ジンジャーブレッドマン) は以下の式で計算されるカオスマップです。
Xn = 1-Yn-1+|Xn-1|
Yn=Xn
初期値は-20から20の間です。
 色付けは初期値ごとに適当に決めました。
 FractMus’ countersで決める方法もあるようです。 つまり、計算した座標のベクトル(sqrt(x*x+y*y))を計算し、それで色を決める方法らしいです。

 効率や恰好などを全然考えていませんが、一応Perlのソースを以下にコピーしました。
use strict;
use GD::Simple;
my @init = (7.5, -7.2, -39.6679279211404,-39.6679279211404,81.6679279211404,81.6679279211404);
my $x0 = $init[0];
my $y0 = $init[1];
my $max=5000;
my $maxTry = 1000;
my $i = 0;
my $minx = $init[2];
my $miny = $init[3];
my $maxx = $init[4];
my $maxy = $init[5];
sub transform {
 my @ret = ();
 my ($x, $y, $minx, $miny, $maxx, $maxy, $w, $h) = @_;
 $ret[0] = int (($x-$minx)*$w/($maxx-$minx));
 $ret[1] = int (($y-$miny)*$h/($maxy-$miny));
 return @ret;
}
my $imgW = 2048;
my $imgH = 2048;
my $imgWOut = $imgW;
my $imgHOut = $imgH;
# create a new image
my $img = GD::Simple->new($imgWOut,$imgHOut);
# draw a red rectangle with blue borders
$img->bgcolor('black');
$img->fgcolor('green');
my $black = $img->colorAllocate(0,0,0);
my $green = $img->colorAllocate(0,255,0);
$img->rectangle(0,0,$imgWOut,$imgHOut,$black);
$img->fill(50,50,$black);
my @colors = ();
for ($i=1; $i<=4; $i++) {
  my $val = $i*64%256;
  $colors[$i*6+0] = $img->colorAllocate($val,    0,    0);
  $colors[$i*6+1] = $img->colorAllocate(   0, $val,    0);
  $colors[$i*6+2] = $img->colorAllocate(   0,    0, $val);
  $colors[$i*6+3] = $img->colorAllocate($val, $val,    0);
  $colors[$i*6+4] = $img->colorAllocate($val,    0, $val);
  $colors[$i*6+5] = $img->colorAllocate(   0, $val, $val);
}
$img->transparent($black);
my $i = 0;
my $j = 0;
for ($j=0; $j<$maxTry; $j++) {
  for ($i=0; $i<$max; $i++) {
    my $x1 = 1-$y0+abs($x0);
    my $y1 = $x0;
    my ($xt, $yt) = transform($x1, $y1, $minx, $miny, $maxx, $maxy, $imgW, $imgH);
    $img->setPixel($xt, $yt, $colors[$j%25]);
    $x0 = $x1;
    $y0 = $y1;
  }
  $x0 = rand 40;
  $x0 -= 20;
  $y0 = rand 40;
  $y0 -= 20;
}
#output the image
open( OUT, "> gingerBreadMan.png") or die( "Cannot open file: graph.jpg" );
binmode OUT;
print OUT $img->png;

参考リンク
List of chaotic maps
Part 2. Chaotic Maps

2014年6月17日火曜日

Barnsley fern in Perl and GD フラクタル図形 バーンスレイのシダ



これは、Barnsley fernというフラクタルの画像です。名前は、イギリスの数学家であるMichael Barnsleyから来たそうです。

PerlとGD(GD::Simple)を使えば簡単に作成できます。

use GD::Simple;
use strict;
sub transform {
 my @ret = ();
 my ($x, $y, $w, $h) = @_;
 $ret[0] = int (($x+2.2)*$w/5);
 $ret[1] = int ((10-$y)*$h/10);
 return @ret;
}
#         a, b, c   d,   e, f
my @w1 = (   0,      0,     0, 0.16, 0, 0.00);
my @w2 = ( 0.85,  0.04, -0.04, 0.85, 0, 1.60);
my @w3 = ( 0.20, -0.26,  0.23, 0.22, 0, 1.60);
my @w4 = (-0.15,  0.28,  0.26, 0.24, 0, 0.44);
my $x = 0;
my $y = 0;
my @p = (0.01, 0.85, 0.07, 0.07);
my @p1 = ($p[0]*10000,($p[0]+$p[1])*10000,($p[0]+$p[1]+$p[2])*10000);
my $i = 0;
my $max = 800000;
my $x1 = 0;
my $y1 = 0;
my @wt = ();
my $j;
my @ret;
my $imgW = 960;
my $imgH = 1024;
# create a new image
my $img = GD::Simple->new($imgW,$imgH);
# draw a red rectangle with blue borders
$img->bgcolor('black');
$img->fgcolor('green');
my $black = $img->colorAllocate(0,0,0);
my $green = $img->colorAllocate(0,255,0);
$img->rectangle(0,0,$imgW,$imgH,$black);
$img->fill(50,50,$black);
$img->transparent($black);
@ret = &transform($x, $y, $imgW, $imgH);
$img->setPixel($ret[0], $ret[1], $green);
for ($i=0; $i<$max; $i++) {
  my $id=int(rand(10000));
  if ($id < $p1[0] ) {
    @wt = (@w1);
  }
  elsif ($id < $p1[1]) {
    @wt = (@w2);
  }
  elsif ($id < $p1[2]) {
    @wt = (@w3);
  }
  else {
    @wt = (@w4,0);
  }
  $x1 = $x*$wt[0] + $y*$wt[1] + $wt[4];
  $y1 = $x*$wt[2] + $y*$wt[3] + $wt[5];
  @ret = &transform($x1, $y1, $imgW, $imgH);
  $img->setPixel($ret[0], $ret[1], $green);
  $x = $x1;
  $y = $y1;
}
#output the image
open( OUT, "> fern.png") or die( "Cannot open file: graph.jpg" );
binmode OUT;
print OUT $img->png;

Install GD for perl in Fedora グラフ作成用ライブラリのインストール

いろいろ試して面倒くさいことになっていたが、結局以下のコマンドで成功した。すべてスーパーユーザで実行しました。OSは64ビットだったので、x86_64を選びました。
yum install gd-devel
yum install perl-GDTextUtil.noarch
yum install perl-GDGraph.noarch
yum install gd-devel.x86_64

perl -MCPAN -e shellで、installコマンドで試したが、途中で失敗したりしました。