fortran66のブログ

fortran について書きます。

Interactive IPython

IPython が 2.0 になって新機能が増えたようです。スライダーがほぼ何もせずに付きます。これは楽だわ。

f:id:fortran66:20140517013538p:plain

    subroutine cmandel(c, m)
        integer, parameter :: maxiter = 255
        integer, parameter :: kd = kind(1.0d0)
        complex(kd), intent(in ) :: c
        integer    , intent(out) :: m
        complex(kd) :: z
        z = c
        do m = maxiter, 1, -1
          if (abs(z) > 2.0_kd) exit
          z = z**2 + c
        end do 
        return
    end subroutine cmandel 
!f2py -c --fcompiler=intelvem -m mandel mandelf2py.f90
pylab inline
from IPython.html.widgets import interact
%matplotlib inline
@interact
def main(x0=(-2.0, 0.0), x1=(0.0, 2.0), y0 = (-2.0, 0.0), y1 =(0.0, 2.0)):
    from PIL import Image
    import mandel
  #  x0 = -2.0 
  #  x1 =  2.0 
  #  y0 = -2.0 
  #  y1 =  2.0 
    nx = 512
    ny = 512
    img = Image.new("RGB", (nx, ny))
    
    for iy in range(ny):
        y = iy * (y1 - y0) / (ny - 1)  + y0
        for ix in range(nx):
            x = ix * (x1 - x0) / (nx - 1)  + x0
            z = complex(x, y)
            m = mandel.cmandel(z)
            img.putpixel((ix, iy), ((5 * m) % 256, m, (10 * m) % 256))

    imshow(img)

参考ページ
http://notebooks-boom.tumblr.com/post/83941596076/wow-fortran-is-fast-would-you-like-to-use
タンブラーの Fortran 関連は、たまにマジキチ入っていていいです。

http://nbviewer.ipython.org/gist/Carreau/11315215/Interactive%20Fortran.ipynb