2013/10/19

C言語とgnuplotでマンデルブロ集合を表示する

gnuplotを使うと結構簡単にマンデルブロ集合を表示することができます。OSはdebianを使用しています。

リスト1 mandelblot.c
/*
  マンデルブロ集合計算プログラム

 */
#include <stdio.h>
#include <math.h>

#define C0r     -0.743  // 計算する場所の中心座標(実数部)
#define C0i      0.1145 // 計算する場所の中心座標(虚数部)
#define VS       0.003  // 計算する場所の中心座標からの範囲(±VS)

#define NMAX     20000  // 計算の繰り返し上限
#define STEP     800.0  // 計算する刻み


double mandelbrot(double a, double b){
  double x = 0.0;
  double y = 0.0;
  double x1, y1;

  int n;

  for (n = 1; n <= NMAX; n++) {
    x1 = x * x - y * y + a;
    y1 = 2.0 * x * y + b;
    if ( x1 * x1 + y1 * y1 > 4.0) return log(n); // 発散
    x = x1;
    y = y1;
  }
  return 0; // 計算の繰り返し上限到達
}


int main() {
  double a, b;

  for (a = C0r-VS; a < C0r+VS; a += 2.0*VS/STEP) {
    for (b = C0i-VS; b < C0i+VS; b += 2.0*VS/STEP) {
      printf("%1.14e %1.14e %1.14e\n", a, b, mandelbrot(a, b));
    }
    printf("\n"); // これがないとgnuplotでエラーが出る
  }
  return 0;
}

リスト2 png.gnuplot (gnuplot実行スクリプト)
set term png size 900, 900
set output "mandelbrot.png"
set grid
set pm3d map
set size square
set palette defined (0 "#000000", 2 "#c00000", 7 "#ffff00", 9 "#ffffff")
splot "data.txt"

リスト3 コンパイル&実行
# cc -o mandelbrot -lm -O4 -Wall mandelbrot.c
# ./mandelbrot > data.txt
# gnuplot png.gnuplot

図1 実行結果(mandelbrot.png)
  • 計算する場所はリスト1のdefine文の数値を変えることにより可能です。
  • 色の変化を分かりやすくするため、Logスケールで表示しています。
C言語のほかにPythonとR言語で記述した記事もあります。詳しくは マンデルブロ集合 をご覧ください。

2013/08/21

古いHDDの消去

 部屋の整理をしていたら古いハードディスクが出てきた。3.5インチ PATA HDD 10GBです。さすがに10GBのPATAは使い道があまりないので廃棄することにした。

 廃棄するにあたり、念のためHDDの中身を消去したいところですが、最近のPCはSATAなのでPATA HDDを直接接続できません。何かないかと探したところ、USB接続のHDDキットがありました。

 さっそくUSB HDDキットに古いHDDを組み込んでdebianが稼働しているPCに接続。接続したHDDのデバイス名を確認するため、"fdisk -l"を実行。

================================================================
# fdisk -l
Disk /dev/sdb: 10.0 GB, 10005037056 bytes
255 heads, 63 sectors/track, 1216 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb62b68b0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1        1215     9759456    7  HPFS/NTFS
#

================================================================

"/dev/sdb"で認識されているようだ。HDDを消去するコマンドは"shred"。HDD消去に時間がかかることが予想されるので、telnet接続で実行する場合は"nohup"コマンドを付加しておく。

====================================
# nohup shred -n 3 -z -v /dev/sdb &
====================================

■shredの実行ログ
shred: /dev/sdb: 経過 1/4 (random)...
shred: /dev/sdb: pass 1/4 (random)...795MiB/9.4GiB 8%
shred: /dev/sdb: pass 1/4 (random)...796MiB/9.4GiB 8%
shred: /dev/sdb: pass 1/4 (random)...1.5GiB/9.4GiB 16%
shred: /dev/sdb: pass 1/4 (random)...1.6GiB/9.4GiB 17%
~中略~
shred: /dev/sdb: pass 1/4 (random)...9.3GiB/9.4GiB 99%
shred: /dev/sdb: pass 1/4 (random)...9.4GiB/9.4GiB 100%
shred: /dev/sdb: 経過 2/4 (random)...
shred: /dev/sdb: pass 2/4 (random)...735MiB/9.4GiB 7%
shred: /dev/sdb: pass 2/4 (random)...736MiB/9.4GiB 7%
~中略~
shred: /dev/sdb: pass 2/4 (random)...9.3GiB/9.4GiB 99%
shred: /dev/sdb: pass 2/4 (random)...9.4GiB/9.4GiB 100%
shred: /dev/sdb: 経過 3/4 (random)...
shred: /dev/sdb: pass 3/4 (random)...730MiB/9.4GiB 7%
shred: /dev/sdb: pass 3/4 (random)...731MiB/9.4GiB 7%
shred: /dev/sdb: pass 3/4 (random)...1.3GiB/9.4GiB 14%
~中略~
shred: /dev/sdb: pass 3/4 (random)...9.2GiB/9.4GiB 98%
shred: /dev/sdb: pass 3/4 (random)...9.3GiB/9.4GiB 99%
shred: /dev/sdb: pass 3/4 (random)...9.4GiB/9.4GiB 100%
shred: /dev/sdb: 経過 4/4 (000000)...
shred: /dev/sdb: pass 4/4 (000000)...747MiB/9.4GiB 7%
shred: /dev/sdb: pass 4/4 (000000)...748MiB/9.4GiB 7%
shred: /dev/sdb: pass 4/4 (000000)...1.4GiB/9.4GiB 15%
shred: /dev/sdb: pass 4/4 (000000)...1.5GiB/9.4GiB 16%
~中略~
shred: /dev/sdb: pass 4/4 (000000)...9.1GiB/9.4GiB 97%
shred: /dev/sdb: pass 4/4 (000000)...9.2GiB/9.4GiB 98%
shred: /dev/sdb: pass 4/4 (000000)...9.3GiB/9.4GiB 99%
shred: /dev/sdb: pass 4/4 (000000)...9.4GiB/9.4GiB 100%

 

【備忘録】時系列データの編集方法(R言語, tidyverse)

TimeSeries.knit 1 サンプルデータ作成 2 日付単位に集計する 2.1 月毎集計 2.2 四半期毎集計 ...