I tried using SWIG. I remember using it for morphological analysis with Human before, but I haven't investigated it properly, so I made a note for myself.
manual http://www.swig.org/Doc1.3/Python.html#Python_nn4
Follow the README for installation.
$./configure
$make
$su
$make install
After the installation is complete, play around with the sample in the manual.
The contents of the .i file http://blog.livedoor.jp/yoshichi9/archives/21911908.html参考
module module name
%{
include header file name
%}
Function enumeration(Or.h file)
#define(Macro definition etc.)
It seems.
Now, let's create an extension module that returns values that follow a normal distribution.
First, prepare various c source files.
normaldist.h
double normal_distribution(double myu, double sigma, double x);
normaldist.c
#include "normaldist.h"
#include <math.h>
double normal_distribution(double myu, double sigma, double x){
double d;
double coefficient, in_exp;
coefficient = sqrt(2 * M_PI * sigma); //coefficient
in_exp = exp( -pow( x - myu, 2 ) / (2 * sigma )); //The one on the shoulder of exp
d = in_exp / coefficient;
return d;
}
Then the .i file for swig
normaldist.i
%module normaldist
%{
#define SWIG_FILE_WITH_INIT
#include "normaldist.h"
#include <math.h>
%}
double normal_distribution(double myu, double sigma, double x);
最後にsetup.py
setup.py
from distutils.core import setup, Extension
module1 = Extension('_normaldist',
sources = ['normaldist_wrap.c', 'normaldist.c'])
setup (name = 'normaldist',
version = '1.0',
description = 'This is a normaldist package',
ext_modules = [module1],
py_modules = ["normaldist"],)
All you have to do is type the commands in order.
$swig -python normaldist.i
$python setup.py build_ext --inplace
There should be _normaldist.so in the same directory.
Recommended Posts