LoginSignup
3
2

More than 3 years have passed since last update.

Fortran 愛好家に贈る f2py で動かす matplotlib の調理法

Last updated at Posted at 2019-09-23

はじめに

世の中はpythonである。でもそれだけがいいとは思はない。一番速いコンパイラーはFortranなのだ。数値計算ならFortranの方が手っ取り早く世界最速のオブジェクトを吐く。
でも、グラフィック表示は苦手で、Pythonのmatplotlib Libralyを横目で見てため息をつく諸兄は多いはずだ。

ここに、世界一簡単だと思う方法で、Fortranの出力をMatplotlibに表示する方法を述べる。

Fortran Program

sin.f90

       subroutine plot(n,x,a)
       integer, intent(in) :: n
       real(8), intent(out) :: a(n),x(n)
       do i = 1, n
        x(i) = i
        a(i) = sin((2*3.1415926/n) * i)
       end do
       end subroutine

宣言文の類を抜くと5行のプログラムである。これが読めないのにこの記事を読んでいる人がいたら不可解である。

Python Script

plot.py
#! /usr/bin/python3
import sin
import matplotlib.pyplot as plt

x, y = sin.plot(1024)

plt.plot(x,y)
plt.show()

宣言を除くと3行。これぐらいは勘弁してほしい。しかも、x-y plotの範疇ならいじる必要はない。

Link & Run

では実行しよう。
先ず、

f2py -c sin.f90  -m sin -lm

これで、Fortran ProgramはPython ModuleにCompileされる。
で、実行。

python3 plot.py

お仕舞

Pythonアレルギーがちょっとは後退しただろうか。この先はmatplotlibのマニュアルを見て、うまくやってほしい。
Fortranの方はお任せするので活用していただければ幸いである。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2