Write python modules in fortran using f2py

python Easy to write, but speedy ~ → Bottleneck modularized fortran !! !! !! fast! !! !!

Let's modularize the prime number display program I wrote earlier for the purpose of practicing Fortran.

environment

f2py (included with scipy) gfortran4.7.2

script

Prepare a file that describes the subroutine you want to modularize. The argument of the subroutine declares in with Cf2py. The return value of the subroutine (that is, the return value received on the python side) declares out with Cf2py. If you do not write the Cf2py declaration from the beginning of the line and open 6 spaces in the script body, a compile error will occur. Apparently, I have to write it indented after Cf2py (it's annoying !!)

primepy.f90


      subroutine prime_number(max_num, ret)
         implicit none
         integer max_num
         integer ret(max_num)
         integer i, j, list(max_num)
Cf2py intent(in) max_num
Cf2py intent(out) ret
         
         !set l
         do i=1, max_num, 1
            list(i) = i
         enddo
         list(1) = 0

         !Set a prime number
         do i=1, max_num, 1
            if (.not. list(i) == 0) then
               do j = i*2, max_num, i
                  list(j) = 0
               end do
            endif
         enddo

         !Returns a list of prime numbers
         do i=1, max_num, 1
            ret(i) = list(i)
         enddo
      end subroutine prime_number

compile

f2py -c --fcompiler=gfortran -m prime primepy.f90 Compile with -c Specify the compiler with --fcompiler Output file after -m Finally the Fortran program files Then check help with -h

Success if prime.so file is created.

Run

With a python interpreter

>>> import prime
>>> a = prime.prime_number(100)
>>> a
array([ 0,  2,  3,  0,  5,  0,  7,  0,  0,  0, 11,  0, 13,  0,  0,  0, 17,
        0, 19,  0,  0,  0, 23,  0,  0,  0,  0,  0, 29,  0, 31,  0,  0,  0,
        0,  0, 37,  0,  0,  0, 41,  0, 43,  0,  0,  0, 47,  0,  0,  0,  0,
        0, 53,  0,  0,  0,  0,  0, 59,  0, 61,  0,  0,  0,  0,  0, 67,  0,
        0,  0, 71,  0, 73,  0,  0,  0,  0,  0, 79,  0,  0,  0, 83,  0,  0,
        0,  0,  0, 89,  0,  0,  0,  0,  0,  0,  0, 97,  0,  0,  0], dtype=int32)

Water

I couldn't think of an easy way to return only non-zero, so this is ʻa.take (a.nonzero () [0])` on the python side.

Conclusion

Happy to eliminate bottlenecks If you don't understand with fortran, handle it on the python side! !! The best list of python! !! !!

Recommended Posts

Write python modules in fortran using f2py
Write Python in MySQL
Write Pandoc filters in Python
Write beta distribution in Python
Read and write NFC tags in python using PaSoRi
Translate using googletrans in Python
Using Python mode in Processing
Read Fortran output in python
Write a binary search in Python
GUI programming in Python using Appjar
Precautions when using pit in Python
Write JSON Schema in Python DSL
Write an HTTP / 2 server in Python
Write AWS Lambda function in Python
Using global variables in python functions
Write selenium test code in python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Handwriting recognition using KNN in Python
Write C unit tests in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
When using regular expressions in Python
GUI creation in python using tkinter 2
Write various forms of phylogenetic tree in Python using ETE Toolkit
Write documentation in Sphinx with Python Livereload
Mouse operation using Windows API in Python
Notes using cChardet and python3-chardet in Python 3.3.1.
Modules and packages in Python are "namespaces"
Try using the Wunderlist API in Python
GUI creation in python using tkinter part 1
Get Suica balance in Python (using libpafe)
Slowly hash passwords using bcrypt in Python
Try using the Kraken API in Python
Using venv in Windows + Docker environment [Python]
Write the test in a python docstring
Tweet using the Twitter API in Python
[Python] [Windows] Serial communication in Python using DLL
Write a short property definition in Python
Log in to Slack using requests in Python
Write O_SYNC file in C and Python
Get Youtube data in Python using Youtube Data API
Write a Caesar cipher program in Python
Using physical constants in Python scipy.constants ~ constants e ~
Scraping a website using JavaScript in Python
Develop slack bot in python using chat.postMessage
Read and write JSON files in Python
Write a simple greedy algorithm in Python
Write a simple Vim Plugin in Python 3
Draw a tree in Python 3 using graphviz
Notes for using python (pydev) in eclipse
Disease classification in Random Forest using Python
Download files in any format using Python
Parallel task execution using concurrent.futures in Python
Notes on using code formatter in Python
How to write Ruby to_s in Python
Meaning of using DI framework in Python