2017/07/30

pythonでマンデルブロ集合を表示する


最近、Pythonを勉強しています。

こちらの記事「My Christmas Gift: Mandelbrot Set Computation In Python」を参考にマンデルブロ集合を表示させてみたので備忘録として残します。

環境


OSはWindows、pythonはAnacondaを使用しました。
Anacondaのnotebookで実行しています。

ソースコード


ソースコードを以下に示します。殆ど参照記事のソースコードと一緒です。
import numpy as np
from numba import jit
from tqdm import tqdm

@jit
def mandelbrot(z,maxiter,horizon,log_horizon):
    c = z
    for n in range(maxiter):
        az = abs(z)
        if az > horizon:
            return n - np.log(np.log(az))/np.log(2) + log_horizon
        z = z*z + c
    return 0

@jit
def mandelbrot_set(xmin,xmax,ymin,ymax,width,height,maxiter):
    horizon = 2 ** 40
    log_horizon = np.log(np.log(horizon))/np.log(2)
    r1 = np.linspace(xmin, xmax, width)
    r2 = np.linspace(ymin, ymax, height)
    n3 = np.empty((width,height))
    for i in tqdm(range(width)):
        for j in range(height):
            n3[i,j] = mandelbrot(r1[i] + 1j*r2[j],maxiter,horizon, log_horizon)
    return (r1,r2,n3)

from matplotlib import pyplot as plt
from matplotlib import colors
%matplotlib inline
 
def mandelbrot_image(x,y,radius,width=10,height=10,maxiter=80,cmap='jet',gamma=0.3):
    dpi = 72
    img_width = dpi * width
    img_height = dpi * height
    xmin = x - radius
    xmax = x + radius
    ymin = y - radius
    ymax = y + radius
    x,y,z = mandelbrot_set(xmin,xmax,ymin,ymax,img_width,img_height,maxiter)
    
    fig, ax = plt.subplots(figsize=(width, height),dpi=72)
    ticks = np.arange(0,img_width,3*dpi)
    x_ticks = xmin + (xmax-xmin)*ticks/img_width
    plt.xticks(ticks, x_ticks)
    y_ticks = ymin + (ymax-ymin)*ticks/img_width
    plt.yticks(ticks, y_ticks)
    ax.set_title(cmap)
    
    norm = colors.PowerNorm(gamma)
    cax = ax.imshow(z.T,cmap=cmap,origin='lower',norm=norm)
    fig.colorbar(cax, shrink=0.82)
    plt.show()

実行


notebook上のセルと呼ばれるウィンドにソースコードを流し込み、Shift+Enterを押せば実行されるようです。その後、次のセルでmandelbrot_image()にパラメータを与えてShift+Enterすれば実行されます。このnotebookは、学生の頃に遊んでいたMathematicaを思い出す操作体系でとても懐かしい。

ギャラリー


mandelbrot_image(-0.75, 0, 1.5, cmap='hot', gamma=0.4)

mandelbrot_image(0.2501, 1.6e-6, 1e-8, maxiter=1e4)

mandelbrot_image(0.3007100003, 0.02006000303, 1e-10, maxiter=2048, cmap='hot', gamma=0.4)

mandelbrot_image(-0.74958245,0.0300396,1e-6, maxiter=30000, cmap='copper')

mandelbrot_image(-0.74997501, -0.00999551, 1e-8, maxiter=1e5, cmap='flag', gamma=0.98)

補足


cmap='hogehoge'で指定するhogehogeは、こちらに一覧があります。

2016/03/20

Raspberry Pi2で円周率の計算をやってみた

今度はRaspberry Pi2で計算してみます。(前回はここ

(1)実行環境


 マシン:Raspberry Pi2
 O S:Raspbian

(2)円周率計算プログラムについて


前回と同じく、GMPの「Compute billions of digits of π using GMP!」にあるgmp-chudnovsky.cを使用させて頂きました。

(3)GMPライブラリと円周率計算プログラムのダウンロード、コンパイル


作業フォルダ"pi"を作成し、そこにGMPライブラリと円周率計算プログラムをダウンロード、コンパイルします。

$ mkdir pi
$ cd pi
$ wget https://gmplib.org/download/gmp/gmp-6.1.0.tar.xz
$ tar xvf gmp-6.1.0.tar.xz
$ cd gmp-6.1.0/
$ ./configure
$ make -j4
$ make -j4 check
$ cd ..
$ wget https://gmplib.org/download/misc/gmp-chudnovsky.c
$ cc -Wall -static -O2 -o gmp-chudnovsky gmp-chudnovsky.c -Igmp-6.1.0 -Lgmp-6.1.0/.libs -lgmp -lm

(4)円周率計算プログラムの実行

・円周率1,000桁

円周率表示ありで0.01秒以下でした。

$ time -p ./gmp-chudnovsky 1000 1
#terms=70, depth=8
sieve   time =  0.000
...................................................

bs      time =  0.000
   gcd  time =  0.000
div     time =  0.000
sqrt    time =  0.000
mul     time =  0.000
total   time =  0.000
   P size=1334 digits (1.334000)
   Q size=1327 digits (1.327000)
pi(0,70)=
0.314159265358979 …省略
real 0.00
user 0.00
sys 0.01

・円周率100万桁

円周率表示なしの計算のみで6.15秒でした。

$ time -p ./gmp-chudnovsky 1000000
#terms=70513, depth=18
sieve   time =  0.070
..................................................

bs      time =  4.480
   gcd  time =  0.000
div     time =  0.790
sqrt    time =  0.420
mul     time =  0.300
total   time =  6.060
   P size=1455608 digits (1.455608)
   Q size=1455601 digits (1.455601)
real 6.15
user 6.06
sys 0.09

(5)pi2とB+の比較


    【条件】   
  • 円周率表示なし   
  • gmp-6.1.0   
  • 円周率プログラム https://gmplib.org/download/misc/gmp-chudnovsky.c   
  • コンパイルオプション $ cc -Wall -static -O2 -o gmp-chudnovsky gmp-chudnovsky.c -Igmp-6.1.0 -Lgmp-6.1.0/.libs -lgmp -lm
桁   Pi2  B+
=======
1e3  0.01 0.02
1e4  0.03 0.05
1e5  0.36 0.70
1e6  6.14 13.25
1e7 105.93 235.95
1e8 1,721.64 -(GNU MP: Cannot allocate memory)
・Pi2はB+に比べ2倍程度早いです。
・B+はメモリ不足で1億桁の計算ができませんでした。

2015/12/30

"POST MODE"からの回復(IP電話機Fanvil C56)

IP電話機 Fanvil C56 を久しぶりに使おうと思い電源を入れたところ、"POST MODE"とLCDに表示されたまま動かない。電源を入れなおしてみたり、色々なキーを押してみたが改善しなかった。 google先生に聞いてみたところ、以下の情報を発見。 試しにやってみたところ、無事に回復できました。

用意するもの

  • PC (IPアドレスを192.168.10.100に変更できるもの)
  • C56のファームウェア(ここから入手する)
  • FTPサーバ (PCで兼用可能、別に用意してもよい。私はRaspberry PiをFTPサーバとして用意した)

手順

  1. FTPサーバのIPアドレスを192.168.10.101に変更する
  2. FTPサーバにC56のファームウェアを格納する。格納場所はFTPでログインするディレクトリの直下にする
  3. PCのIPアドレスを192.168.10.100に変更する。
  4. PC、C56、およびFTPサーバをLANケーブルで接続する。(C56はLAN用RJ45を使用)
  5. PCから192.168.10.1にtelnetする
  6. telnet接続が成功するとメニューが表示される。(login, passwd不要)
  7. メニューから"3: Clear Configration"を選択
  8. メニューから"2: Update firmware via FTP"を選択
  9. "Input Server address"と表示されるのでFTPサーバのIPアドレス"192.168.10.101"を入力する
  10. "Input image name"と表示されるのでFTPサーバに格納したファームウェアのファイル名を入力する
  11. "Input user name"と表示されるのでFTPのユーザ名を入力する
  12. "Inout password"と表示されるのでFTPのパスワードを入力する
  13. ファームウェアの更新作業が行われるので終わるまで待つ。"Update success!"と表示されればOK
  14. メニューが表示されるので、"4: Exit and Reboot"を選択
  15. C56が再起動し、"Wait logon"と表示されれば成功

上記手順により工場出荷状態に戻るので後は普通に設定すればOK。ちなみにC56の基本設定はここが分かりやすい。

以上です。

2015/11/23

モニタ、キーボードなしでRaspberryPiにraspbianをインストールする

最新のraspbian Stretchでは、仕様変更があり下記方法ではインストールできません。改訂版を用意しましたのでこちらをご覧ください。

RaspbanをインストールするにはモニタとキーボードをRaspberryPiに接続する必要があります。しかし、つなぐのは面倒なのでSSH経由でraspbianの初期設定する方法を試してみましたので備忘録を兼ねて紹介します。

■用意するもの

  • Linuxマシン
  • SDカードリーダ

■OSイメージをダウンロード

Linuxマシン上での作業です。
まずはOSイメージをミラーサイトからダウンロードします。
$ wget http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian/images/raspbian-2015-09-28/2015-09-24-raspbian-jessie.zip
$ sha1sum 2015-09-24-raspbian-jessie.zip
d8d4880cd0e4f155f343984725d5bd94c1f86023  2015-09-24-raspbian-jessie.zip

■イメージファイルをマウントする

Linuxマシン上での作業です。
raspbianのIPアドレスを固定するため、イメージファイルをマウントして設定ファイルを書き換えます。
$ unzip 2015-09-24-raspbian-jessie.zip
$ sudo fdisk -l 2015-09-24-raspbian-jessie.img

Disk 2015-09-24-raspbian-jessie.img: 4325 MB, 4325376000 bytes
255 heads, 63 sectors/track, 525 cylinders, total 8448000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xba2edfb9

                         Device Boot      Start         End      Blocks   Id  System
2015-09-24-raspbian-jessie.img1            8192      122879       57344    c  W95 FAT32 (LBA)
2015-09-24-raspbian-jessie.img2          122880     8447999     4162560   83  Linux

$ sudo mount -o loop,offset=62914560 2015-09-24-raspbian-jessie.img /mnt/

オフセットの値はfdisk中の122880にセクタサイズ512を掛けた値。

■ネットワーク関係のファイルを変更

ファイルのオーナがrootなのでrootになって作業します。
("xxx"部分は自分のネットワーク環境に合わせて適宜書き換える)

# vi /mnt/etc/network/interface

・変更前
 auto eth0
 allow-hotplug eth0
 iface eth0 inet manual

・変更後
 auto eth0
 allow-hotplug eth0
 #iface eth0 inet manual
 iface eth0 inet static
 address xxx.xxx.xxx.xxx
 netmask 255.255.255.0
 network xxx.xxx.xxx.0
 broadcast xxx.xxx.xxx.255
 gateway xxx.xxx.xxx.xxx

# vi /mnt/etc/resolv.conf

・変更前
 nameserver 8.8.8.8

・変更後
 nameserver xxx.xxx.xxx.xxx

# umount /mnt

■SDカードに書き込む

LinuxマシンからSDカードにイメージファイルを書き込みむ。("/dev/sdb"は自分の環境に合わせて適宜書き換える)

# dd bs=4M if=2015-09-24-raspbian-jessie.img of=/dev/sdb

■Raspberry PiにSDカードを挿入して起動する

ssh経由でログインする。(user=pi, passwd=raspberry, ip addressは先に設定したもの)
各種設定をする。
pi@raspberrypi ~ $ sudo raspi-config

これで完了です。

バーニングシップフラクタル

bsf.knit 今回は『バーニングシップフラクタル』についての備忘録です。 バーニングシップフラクタルとは? Wikipedia ...