Notebook
from libc.math cimport exp
from math import exp import numpy as np def rbf_network(double[:, :] X, double[:] beta, double theta): cdef int N = X.shape[0] cdef int D = X.shape[1] cdef double[:] Y = np.zeros(N) cdef int i, j, d cdef double r = 0 for i in range(N): for j in range(N): r = 0 for d in range(D): r += (X[j, d] - X[i, d]) ** 2 r = r**0.5 Y[i] += beta[j] * exp(-(r * theta)**2) return Y
from distutils.core import setup from Cython.Build import cythonize setup(name="fastloop", ext_modules=cythonize('fastloop.pyx'),)
python setup.py build_ext --inplace
cython fastloop.pyx -a
from libc.math cimport exp
#cython: boundscheck=False, wraparound=False, nonecheck=False
from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext ext_modules=[ Extension("fastloop", ["fastloop.pyx"], libraries=["m"], extra_compile_args = ["-ffast-math"])] setup( name = "fastloop", cmdclass = {"build_ext": build_ext}, ext_modules = ext_modules)
#include <math.h> static union { double d; struct { #ifdef LITTLE_ENDIAN int j, i; #else int i, j; #endif } n; } _eco; #define EXP_A (1048576/M_LN2) /* use 1512775 for integer version */ #define EXP_C 60801 /* see text for choice of c values */ #define EXP(y) (_eco.n.i = EXP_A*(y) + (1072693248 - EXP_C), _eco.d)
cdef extern from "vfastexp.h": double exp_approx "EXP" (double)