Learning SciPy for Numerical and Scientific Computing

Content under Creative Commons Attribution license CC-BY 4.0, code under MIT license (c)2014 Sergio Rojas (srojas@usb.ve) and Erik A Christensen (erikcny@aol.com).

NOTE: This IPython notebook should be read alonside the corresponding chapter in the book, where each piece of code is fully explained.

Chapter 8. Interaction with Other Languages

Fortran

Please, check the book to get a full explanation of what is done by the following code and what its output represents


In [13]:
%%file primefactors.f90
! File  primefactors.f90

SUBROUTINE PRIMEFACTORS(num, factors, f)
  IMPLICIT NONE
  INTEGER, INTENT(IN) :: num  !input number
  INTEGER,INTENT(OUT), DIMENSION((num/2))::factors 
  INTEGER, INTENT(INOUT) :: f
  INTEGER :: i, n
  i = 2  
  f = 1  
  n = num
  DO
    IF (MOD(n,i) == 0) THEN 
      factors(f) = i
      f = f+1
      n = n/i
    ELSE
      i = i+1
    END IF
    IF (n == 1) THEN    
      f = f-1    
      EXIT
    END IF
  END DO
END SUBROUTINE PRIMEFACTORS


Overwriting primefactors.f90

Generate a python module using f2py:


In [14]:
!f2py -c -m primefactors primefactors.f90


running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "primefactors" sources
f2py options: []
f2py:> /tmp/tmpUiqBcO/src.linux-x86_64-2.7/primefactorsmodule.c
creating /tmp/tmpUiqBcO/src.linux-x86_64-2.7
Reading fortran codes...
	Reading file 'primefactors.f90' (format:free)
Post-processing...
	Block: primefactors
			Block: primefactors
Post-processing (stage 2)...
Building modules...
	Building module "primefactors"...
		Constructing wrapper function "primefactors"...
		  factors = primefactors(num,f)
	Wrote C/API module "primefactors" to file "/tmp/tmpUiqBcO/src.linux-x86_64-2.7/primefactorsmodule.c"
  adding '/tmp/tmpUiqBcO/src.linux-x86_64-2.7/fortranobject.c' to sources.
  adding '/tmp/tmpUiqBcO/src.linux-x86_64-2.7' to include_dirs.
copying /home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmpUiqBcO/src.linux-x86_64-2.7
copying /home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmpUiqBcO/src.linux-x86_64-2.7
build_src: building npy-pkg config files
running build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
customize Gnu95FCompiler
Found executable /usr/bin/gfortran
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'primefactors' extension
compiling C sources
C compiler: gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC

creating /tmp/tmpUiqBcO/tmp
creating /tmp/tmpUiqBcO/tmp/tmpUiqBcO
creating /tmp/tmpUiqBcO/tmp/tmpUiqBcO/src.linux-x86_64-2.7
compile options: '-I/tmp/tmpUiqBcO/src.linux-x86_64-2.7 -I/home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/core/include -I/home/srojas/myProg/Anaconda/include/python2.7 -c'
gcc: /tmp/tmpUiqBcO/src.linux-x86_64-2.7/fortranobject.c
In file included from /home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/core/include/numpy/ndarraytypes.h:1804:0,
                 from /home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:17,
                 from /home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/core/include/numpy/arrayobject.h:4,
                 from /tmp/tmpUiqBcO/src.linux-x86_64-2.7/fortranobject.h:13,
                 from /tmp/tmpUiqBcO/src.linux-x86_64-2.7/fortranobject.c:2:
/home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
gcc: /tmp/tmpUiqBcO/src.linux-x86_64-2.7/primefactorsmodule.c
In file included from /home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/core/include/numpy/ndarraytypes.h:1804:0,
                 from /home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:17,
                 from /home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/core/include/numpy/arrayobject.h:4,
                 from /tmp/tmpUiqBcO/src.linux-x86_64-2.7/fortranobject.h:13,
                 from /tmp/tmpUiqBcO/src.linux-x86_64-2.7/primefactorsmodule.c:18:
/home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
/tmp/tmpUiqBcO/src.linux-x86_64-2.7/primefactorsmodule.c:144:12: warning: ‘f2py_size’ defined but not used [-Wunused-function]
compiling Fortran sources
Fortran f77 compiler: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran f90 compiler: /usr/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran fix compiler: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
compile options: '-I/tmp/tmpUiqBcO/src.linux-x86_64-2.7 -I/home/srojas/myProg/Anaconda/lib/python2.7/site-packages/numpy/core/include -I/home/srojas/myProg/Anaconda/include/python2.7 -c'
gfortran:f90: primefactors.f90
/usr/bin/gfortran -Wall -g -Wall -g -shared /tmp/tmpUiqBcO/tmp/tmpUiqBcO/src.linux-x86_64-2.7/primefactorsmodule.o /tmp/tmpUiqBcO/tmp/tmpUiqBcO/src.linux-x86_64-2.7/fortranobject.o /tmp/tmpUiqBcO/primefactors.o -L/home/srojas/myProg/Anaconda/lib -lpython2.7 -lgfortran -o ./primefactors.so
Removing build directory /tmp/tmpUiqBcO

In [15]:
import primefactors

In [16]:
primefactors.primefactors(6,1)


Out[16]:
array([2, 3, 0], dtype=int32)

Example of a python script that use the module:


In [17]:
%%file primefactors.py
import primefactors

print(primefactors.primefactors(6,1))


Overwriting primefactors.py

In [18]:
# run the script
!python primefactors.py


[2 3 0]

C/C++

Please, check the book to get a full explanation of what is done by the following code and what its output represents


In [19]:
import scipy.weave
name = 'Francisco'
pin = 1234
code = 'std::cout << name << "---PIN: " '
code+= '<< std::hex << pin << std::endl;'

In [20]:
print(code)


std::cout << name << "---PIN: " << std::hex << pin << std::endl;

In [21]:
arg_names = ['name','pin']

In [23]:
scipy.weave.inline(code, arg_names)

The output might be seen at the terminal, from where the notebook was started

This is the end of the working codes shown and thoroughly discussed in Chapter 8 of the book Learning SciPy for Numerical and Scientific Computing

Content under Creative Commons Attribution license CC-BY 4.0, code under MIT license (c)2014 Sergio Rojas (srojas@usb.ve) and Erik A Christensen (erikcny@aol.com).