Fortran


In [1]:
%install_ext https://raw.github.com/mgaitan/fortran_magic/master/fortranmagic.py


Installed fortranmagic.py. To use it, type:
  %load_ext fortranmagic

In [2]:
%load_ext fortranmagic



In [3]:
%%fortran --link lapack -vv

subroutine solve(A, b, x, n)
    ! solve the matrix equation A*x=b using LAPACK
    implicit none

    real*8, dimension(n,n), intent(in) :: A
    real*8, dimension(n), intent(in) :: b
    real*8, dimension(n), intent(out) :: x

    integer :: i, j, pivot(n), ok

    integer, intent(in) :: n
    x = b

    ! find the solution using the LAPACK routine SGESV
    call DGESV(n, 1, A, n, pivot, x, n, ok)
    
end subroutine


Running...
   f2py --link-lapack -m _fortran_magic_6fbda2548a60234ffecba09853d1bb43 -c /Users/tom/.ipython/fortran/_fortran_magic_6fbda2548a60234ffecba09853d1bb43.f90
	Building module "_fortran_magic_6fbda2548a60234ffecba09853d1bb43"...
		Constructing wrapper function "solve"...
		  x = solve(a,b,[n])
	Wrote C/API module "_fortran_magic_6fbda2548a60234ffecba09853d1bb43" to file "/var/folders/7d/9vt3zc650d7ghg740jbg7v2w0000gn/T/tmp17gybr8f/src.macosx-10.5-x86_64-3.4/_fortran_magic_6fbda2548a60234ffecba09853d1bb43module.c"

Ok. The following fortran objects are ready to use: solve

In [4]:
A = np.array([[1, 2.5], [-3, 4]])
b = np.array([1, 2.5])
solve(A, b)


Out[4]:
array([-0.19565217,  0.47826087])

Cython


In [5]:
%load_ext cythonmagic

In [6]:
%%cython
cimport cython
from libc.math cimport exp, sqrt, pow, log, erf

@cython.cdivision(True)
cdef double std_norm_cdf(double x) nogil:
    return 0.5*(1+erf(x/sqrt(2.0)))

@cython.cdivision(True)
def black_scholes(double s, double k, double t, double v,
                 double rf, double div, double cp):
    """Price an option using the Black-Scholes model.
    
    s : initial stock price
    k : strike price
    t : expiration time
    v : volatility
    rf : risk-free rate
    div : dividend
    cp : +1/-1 for call/put
    """
    cdef double d1, d2, optprice
    with nogil:
        d1 = (log(s/k)+(rf-div+0.5*pow(v,2))*t)/(v*sqrt(t))
        d2 = d1 - v*sqrt(t)
        optprice = cp*s*exp(-div*t)*std_norm_cdf(cp*d1) - \
            cp*k*exp(-rf*t)*std_norm_cdf(cp*d2)
    return optprice


building '_cython_magic_46744c44e28b805b81708ed1b0f857c9' extension
C compiler: gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/tom/miniconda3/envs/py3/include -arch x86_64

compile options: '-I/Users/tom/miniconda3/envs/py3/include/python3.4m -c'
gcc: /Users/tom/.ipython/cython/_cython_magic_46744c44e28b805b81708ed1b0f857c9.c
gcc -bundle -undefined dynamic_lookup -L/Users/tom/miniconda3/envs/py3/lib -arch x86_64 /Users/tom/.ipython/cython/Users/tom/.ipython/cython/_cython_magic_46744c44e28b805b81708ed1b0f857c9.o -L/Users/tom/miniconda3/envs/py3/lib -o /Users/tom/.ipython/cython/_cython_magic_46744c44e28b805b81708ed1b0f857c9.so

In [9]:
black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)


Out[9]:
10.327861752731728

In [ ]: