def sxrd(): """ Function to build C++ extension modules used for sxrd calculations """ mod = ext_tools.ext_module('sxrd_ext') # Defs to set the types of the input arrays h = array([0,1,2], dtype = float64) k, l, x, y, z, u, oc, dinv = [h]*8 f = (x[:,newaxis]*x).astype(complex128) Pt = array([c_[array([[1,0],[0,1]]), array([0,0])], c_[array([[1,0],[0,1]]), array([0,0])]], dtype = float64) code = ''' double pi = 3.14159265358979311599796346854418516159057617187500; int ij = 0; int offset = 0; std::complex<double> im(0.0, 1.0), tmp; PyObject* out_array = PyArray_SimpleNew(1, &Nh[0], NPY_COMPLEX128); std::complex<double>* fs = (std::complex<double>*) ((PyArrayObject*) out_array)->data; //printf("Atoms: %d, Points: %d, Symmetries: %d\\n", Noc[0], Nh[0], NPt[0]); // Loop over all data points for(int i = 0; i < Nh[0]; i++){ fs[i] = 0; // Loop over all atoms //printf("l = %f\\n", l[i]); for(int j = 0; j < Noc[0]; j++){ ij = i + j*Nh[0]; //printf(" x = %f, y = %f, z = %f, u = %f, oc = %f \\n", x[j], y[j], z[j], u[j], oc[j]); // Loop over symmetry operations tmp = 0; for(int m = 0; m < NPt[0]; m++){ offset = m*6; tmp += exp(2.0*pi*im*(h[i]*( Pt[0 + offset]*x[j] + Pt[1 + offset]*y[j] + Pt[2 + offset])+ k[i]*(Pt[3+offset]*x[j] + Pt[4+offset]*y[j]+ Pt[5 + offset]) + l[i]*z[j])); if(i == 0 && j == 0 && false){ printf("P = [%f, %f] [%f, %f]", Pt[0 + offset], Pt[1 + offset], Pt[3 + offset], Pt[4 + offset]); printf(", t = [%f, %f]\\n", Pt[2 + offset], Pt[5+offset]); } // End if statement } // End symmetry loop index m fs[i] += oc[j]*f[ij]*exp(-2.0*pow(pi*dinv[i],2.0)*u[j])*tmp; } // End atom loop index j } // End data point (h,k,l) loop return_val = out_array; Py_XDECREF(out_array); ''' ext = ext_tools.ext_function('surface_lattice_sum',code, ['x', 'y', 'z', 'h', 'k', 'l', 'u', 'oc', 'f', 'Pt', 'dinv']) mod.add_function(ext) mod.compile()
def build_math(): """ Builds an extension module with fibonacci calculators. """ mod = ext_tools.ext_module('math_ext') time = 0.435 # this is effectively a type declaration e_code = """ #include "math.h" #include "spike_prop.h" #define DECAY 7 inline double e(double time){ double asrf=0; if (time > 0){ //spike response function asrf = (time * pow(M_E, (1 - time * 0.142857143))) * 0.142857143; } return asrf; } """ ext_code = """ return_val = e(time); """ e_ = ext_tools.ext_function('e', ext_code, ['time']) e_.customize.add_support_code(e_code) mod.add_function(e_) mod.compile()
def test_speed(self): mod_name = 'list_speed'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = range(1000000); code = """ int v, sum = 0; for(int i = 0; i < a.len(); i++) { v = a[i]; if (v % 2) sum += v; else sum -= v; } return_val = sum; """ with_cxx = ext_tools.ext_function('with_cxx',code,['a']) mod.add_function(with_cxx) code = """ int vv, sum = 0; PyObject *v; for(int i = 0; i < a.len(); i++) { v = PyList_GetItem(py_a,i); //didn't set error here -- just speed test vv = py_to_int(v,"list item"); if (vv % 2) sum += vv; else sum -= vv; } return_val = sum; """ no_checking = ext_tools.ext_function('no_checking',code,['a']) mod.add_function(no_checking) mod.compile(location = test_dir, compiler = self.compiler) exec 'from ' + mod_name + ' import with_cxx, no_checking' import time t1 = time.time() sum1 = with_cxx(a) t2 = time.time() print 'speed test for list access' print 'compiler:', self.compiler print 'scxx:', t2 - t1 t1 = time.time() sum2 = no_checking(a) t2 = time.time() print 'C, no checking:', t2 - t1 sum3 = 0 t1 = time.time() for i in a: if i % 2: sum3 += i else: sum3 -= i t2 = time.time() print 'python:', t2 - t1 assert_( sum1 == sum2 and sum1 == sum3)
def test_speed(self): mod_name = 'list_speed' + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = range(1000000) code = """ int v, sum = 0; for(int i = 0; i < a.len(); i++) { v = a[i]; if (v % 2) sum += v; else sum -= v; } return_val = sum; """ with_cxx = ext_tools.ext_function('with_cxx', code, ['a']) mod.add_function(with_cxx) code = """ int vv, sum = 0; PyObject *v; for(int i = 0; i < a.len(); i++) { v = PyList_GetItem(py_a,i); //didn't set error here -- just speed test vv = py_to_int(v,"list item"); if (vv % 2) sum += vv; else sum -= vv; } return_val = sum; """ no_checking = ext_tools.ext_function('no_checking', code, ['a']) mod.add_function(no_checking) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import with_cxx, no_checking') import time t1 = time.time() sum1 = with_cxx(a) t2 = time.time() print('speed test for list access') print('compiler:', self.compiler) print('scxx:', t2 - t1) t1 = time.time() sum2 = no_checking(a) t2 = time.time() print('C, no checking:', t2 - t1) sum3 = 0 t1 = time.time() for i in a: if i % 2: sum3 += i else: sum3 -= i t2 = time.time() print('python:', t2 - t1) assert_(sum1 == sum2 and sum1 == sum3)
def test_simple(self): """ Simplest possible function """ mod = ext_tools.ext_module('simple_ext_function') var_specs = [] code = "" test = ext_tools.ext_function_from_specs('test',code,var_specs) mod.add_function(test) mod.compile(location = build_dir) import simple_ext_function simple_ext_function.test()
def test_simple(self): """ Simplest possible function """ mod = ext_tools.ext_module('simple_ext_function') var_specs = [] code = "" test = ext_tools.ext_function_from_specs('test',code,var_specs) mod.add_function(test) mod.compile(location=build_dir) import simple_ext_function simple_ext_function.test()
def test_multi_functions(self): mod = ext_tools.ext_module('module_multi_function') var_specs = [] code = "" test = ext_tools.ext_function_from_specs('test',code,var_specs) mod.add_function(test) test2 = ext_tools.ext_function_from_specs('test2',code,var_specs) mod.add_function(test2) mod.compile(location=build_dir) import module_multi_function module_multi_function.test() module_multi_function.test2()
def test_multi_functions(self): mod = ext_tools.ext_module('module_multi_function') var_specs = [] code = "" test = ext_tools.ext_function_from_specs('test',code,var_specs) mod.add_function(test) test2 = ext_tools.ext_function_from_specs('test2',code,var_specs) mod.add_function(test2) mod.compile(location = build_dir) import module_multi_function module_multi_function.test() module_multi_function.test2()
def test_string_and_int(self): # decalaring variables a = 2;b = 'string' # declare module mod = ext_tools.ext_module('ext_string_and_int') code = """ a=b.length(); return_val = PyInt_FromLong(a); """ test = ext_tools.ext_function('test',code,['a','b']) mod.add_function(test) mod.compile(location = build_dir) import ext_string_and_int c = ext_string_and_int.test(a,b) assert_(c == len(b))
def test_string_and_int(self): # decalaring variables a = 2;b = 'string' # declare module mod = ext_tools.ext_module('ext_string_and_int') code = """ a=b.length(); return_val = PyInt_FromLong(a); """ test = ext_tools.ext_function('test',code,['a','b']) mod.add_function(test) mod.compile(location = build_dir) import ext_string_and_int c = ext_string_and_int.test(a,b) assert(c == len(b))
def test_float_return(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1. code = """ a=a+2.; return_val = PyFloat_FromDouble(a); """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location = test_dir, compiler = self.compiler) exec 'from ' + mod_name + ' import test' b=1. c = test(b) assert_( c == 3.)
def test_complex_return(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = 1. + 1j code = """ a= a + std::complex<double>(2.,2.); return_val = PyComplex_FromDoubles(a.real(),a.imag()); """ test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1. + 1j c = test(b) assert_(c == 3. + 3j)
def test_return(self): mod_name = 'string_return' + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = 'string' code = """ a= std::string("hello"); return_val = PyString_FromString(a.c_str()); """ test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 'bub' c = test(b) assert_(c == 'hello')
def test_float_return(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = 1. code = """ a=a+2.; return_val = PyFloat_FromDouble(a); """ test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1. c = test(b) assert_(c == 3.)
def test_complex_return(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1.+1j code = """ a= a + std::complex<double>(2.,2.); return_val = PyComplex_FromDoubles(a.real(),a.imag()); """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1.+1j c = test(b) assert_(c == 3.+3j)
def test_return(self): mod_name = 'string_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 'string' code = """ a= std::string("hello"); return_val = PyString_FromString(a.c_str()); """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location = test_dir, compiler = self.compiler) exec 'from ' + mod_name + ' import test' b='bub' c = test(b) assert_( c == 'hello')
def __compile_weave_func(self): """ Fast version of the integrand function f() in moment(). We write all functions called in f() in C-code and compile it once during runtime. See scipy.weave for info. NOTE: remove TEM_wqslit_weave.so if C-code is changed """ from scipy.weave import ext_tools mod = ext_tools.ext_module('TEM_wqslit_weave') # create module TEM_wqslit_weave r = a = b = x1 = x2 = y1 = y2 = q = qE = q0 = dqx = dqy = n = 1. # declaration of variable type # translated C-code for arc_segment() and arc_in_box() code = """ double arc_segment(double r, double a, double b) { if (r<a) return 0; else if (r*r>a*a+b*b) return r*asin(b/r); else return r*acos(a/r); }; double arc_in_box(double r, double x1, double y1, double x2, double y2) { double x, y; if (x1<0 && x2<0) { x1=-x1; x2=-x2;}; if (y1<0 && y2<0) { y1=-y1; y2=-y2;}; if (x1>x2) {x=x1; x1=x2; x2=x;}; if (y1>y2) {y=y1; y1=y2; y2=y;}; if (x1*x2<0) return arc_in_box(r, 0, y1, -x1, y2) + arc_in_box( r, 0, y1, x2, y2); if (y1<0) return arc_in_box(r, x1, 0, x2, y2) - arc_in_box( r, x1, 0, x2,-y1); if (y1>0) return arc_in_box(r, x1, 0, x2, y2) + arc_in_box( r, x1, 0, x2, y1); return arc_segment(r,x1,y2) - arc_segment(r,x2,y2); }; """ # translated C-code for integrand in moment(), skips p(), weight_q() main = "return_val = pow(q,n) / (q*q + qE*qE) * 2 \ * arc_in_box(q, q0-dqx/2., 0, q0+dqx/2., dqy/2.);" # compile module func = ext_tools.ext_function('f', main, ['q', 'qE', 'n', 'q0', 'dqx', 'dqy']) func.customize.add_support_code(code) mod.add_function(func) mod.compile()
def test_return(self): mod_name = 'list_return' + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = [1] code = """ a=py::list(); a.append("hello"); return_val = a; """ test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = [1, 2] c = test(b) assert_(c == ['hello'])
def test_return(self): mod_name = 'dict_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = {'z':1} code = """ a=py::dict(); a["hello"] = 5; return_val = a; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location = test_dir, compiler = self.compiler) exec 'from ' + mod_name + ' import test' b = {'z':2} c = test(b) assert_( c['hello'] == 5)
def test_int_return(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = 1 code = """ a=a+2; return_val = PyInt_FromLong(a); """ test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec 'from ' + mod_name + ' import test' b = 1 c = test(b) assert (c == 3)
def test_return(self): mod_name = 'list_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = [1] code = """ a=py::list(); a.append("hello"); return_val = a; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location = test_dir, compiler = self.compiler) exec 'from ' + mod_name + ' import test' b=[1,2] c = test(b) assert_( c == ['hello'])
def test_return(self): mod_name = 'dict_return' + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = {'z': 1} code = """ a=py::dict(); a["hello"] = 5; return_val = a; """ test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = {'z': 2} c = test(b) assert_(c['hello'] == 5)
def test_string_and_int(self): # decalaring variables a = 2 b = "string" # declare module mod = ext_tools.ext_module("ext_string_and_int") code = """ a=b.length(); return_val = PyInt_FromLong(a); """ test = ext_tools.ext_function("test", code, ["a", "b"]) mod.add_function(test) mod.compile(location=build_dir) import ext_string_and_int c = ext_string_and_int.test(a, b) assert_(c == len(b))
def build_ex1(): ext = ext_tools.ext_module('_ex1') # Type declarations - define a sequence and a function seq = [] func = string.upper code = """ py::tuple args(1); py::list result(seq.length()); for(int i = 0; i < seq.length();i++) { args[0] = seq[i]; result[i] = func.call(args); } return_val = result; """ func = ext_tools.ext_function('my_map', code, ['func', 'seq']) ext.add_function(func) ext.compile()
def _compile_cpp(modname, cppcode, lst_extra_link, lst_extra_compile): # Compile filters # The included files are found in the .cpp file mod = ext_tools.ext_module(modname) for line in cppcode.split('\n'): if line.startswith('#include'): mod.customize.add_header(line.replace('#include ', '')) mod.customize.add_header("<Python.h>") mod.customize.add_extra_link_arg("`pkg-config --cflags --libs opencv`") for extra_link in lst_extra_link: mod.customize.add_extra_link_arg(extra_link) for extra_compile in lst_extra_compile: mod.customize.add_extra_compile_arg(extra_compile) # add debug symbol mod.customize.add_extra_compile_arg("-g") mod.customize.add_extra_compile_arg("-pipe") return mod
def test_return(self): mod_name = 'tuple_return'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = (1,) code = """ a=py::tuple(2); a[0] = "hello"; a.set_item(1,py::None); return_val = a; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location = test_dir, compiler = self.compiler) exec 'from ' + mod_name + ' import test' b=(1,2) c = test(b) assert_( c == ('hello',None))
def test_return(self): mod_name = 'tuple_return' + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = (1, ) code = """ a=py::tuple(2); a[0] = "hello"; a.set_item(1,py::None); return_val = a; """ test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = (1, 2) c = test(b) assert_(c == ('hello', None))
def __compile_weave_func(self): """ Fast version of the integrand function f() in moment(). We write all functions called in f() in C-code and compile it once during runtime. See scipy.weave for info. NOTE: remove TEM_wqslit_weave.so if C-code is changed """ from scipy.weave import ext_tools; mod = ext_tools.ext_module('TEM_wqslit_weave'); # create module TEM_wqslit_weave r=a=b=x1=x2=y1=y2=q=qE=q0=dqx=dqy=n=1.; # declaration of variable type # translated C-code for arc_segment() and arc_in_box() code=""" double arc_segment(double r, double a, double b) { if (r<a) return 0; else if (r*r>a*a+b*b) return r*asin(b/r); else return r*acos(a/r); }; double arc_in_box(double r, double x1, double y1, double x2, double y2) { double x, y; if (x1<0 && x2<0) { x1=-x1; x2=-x2;}; if (y1<0 && y2<0) { y1=-y1; y2=-y2;}; if (x1>x2) {x=x1; x1=x2; x2=x;}; if (y1>y2) {y=y1; y1=y2; y2=y;}; if (x1*x2<0) return arc_in_box(r, 0, y1, -x1, y2) + arc_in_box( r, 0, y1, x2, y2); if (y1<0) return arc_in_box(r, x1, 0, x2, y2) - arc_in_box( r, x1, 0, x2,-y1); if (y1>0) return arc_in_box(r, x1, 0, x2, y2) + arc_in_box( r, x1, 0, x2, y1); return arc_segment(r,x1,y2) - arc_segment(r,x2,y2); }; """ # translated C-code for integrand in moment(), skips p(), weight_q() main = "return_val = pow(q,n) / (q*q + qE*qE) * 2 \ * arc_in_box(q, q0-dqx/2., 0, q0+dqx/2., dqy/2.);" # compile module func = ext_tools.ext_function('f',main,['q','qE','n','q0','dqx','dqy']); func.customize.add_support_code(code); mod.add_function(func); mod.compile();
def build_ramp_ext(): mod = ext_tools.ext_module('ramp_ext') # type declarations result = array([0],float64) start,end = 0.,0. code = """ const int size = Nresult[0]; const double step = (end-start)/(size-1); double val = start; for (int i = 0; i < size; i++) { result[i] = val; val += step; } """ func = ext_tools.ext_function('Ramp',code,['result','start','end']) mod.add_function(func) mod.compile(compiler='gcc')
def test_with_include(self): # decalaring variables a = 2. # declare module mod = ext_tools.ext_module('ext_module_with_include') mod.customize.add_header('<iostream>') # function 2 --> a little more complex expression var_specs = ext_tools.assign_variable_types(['a'], locals(), globals()) code = """ std::cout << std::endl; std::cout << "test printing a value:" << a << std::endl; """ test = ext_tools.ext_function_from_specs('test', code, var_specs) mod.add_function(test) # build module mod.compile(location=build_dir) import ext_module_with_include ext_module_with_include.test(a)
def test_return_tuple(self): # decalaring variables a = 2 # declare module mod = ext_tools.ext_module('ext_return_tuple') var_specs = ext_tools.assign_variable_types(['a'],locals()) code = """ int b; b = a + 1; py::tuple returned(2); returned[0] = a; returned[1] = b; return_val = returned; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=build_dir) import ext_return_tuple c,d = ext_return_tuple.test(a) assert_(c == a and d == a+1)
def test_with_include(self): # decalaring variables a = 2.; # declare module mod = ext_tools.ext_module('ext_module_with_include') mod.customize.add_header('<iostream>') # function 2 --> a little more complex expression var_specs = ext_tools.assign_variable_types(['a'],locals(),globals()) code = """ std::cout << std::endl; std::cout << "test printing a value:" << a << std::endl; """ test = ext_tools.ext_function_from_specs('test',code,var_specs) mod.add_function(test) # build module mod.compile(location = build_dir) import ext_module_with_include ext_module_with_include.test(a)
def test_return_tuple(self): # decalaring variables a = 2 # declare module mod = ext_tools.ext_module('ext_return_tuple') var_specs = ext_tools.assign_variable_types(['a'],locals()) code = """ int b; b = a + 1; py::tuple returned(2); returned[0] = a; returned[1] = b; return_val = returned; """ test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location = build_dir) import ext_return_tuple c,d = ext_return_tuple.test(a) assert_(c==a and d == a+1)
def build(): mod = et.ext_module('filtered_ext') mod.customize.add_support_code(matrix_code) # single-tensor add_model_1tensor_f(mod) add_model_1tensor_h(mod) add_state2tensor1(mod) # two-tensor add_model_2tensor_f(mod) add_model_2tensor_h(mod) add_state2tensor2(mod) # utility add_interp3signal(mod) add_interp3scalar(mod) add_s2ga(mod) mod.compile()
def test_var_in(self): mod_name = 'int_var_in' + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1 code = "a=2;" test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location = test_dir, compiler = self.compiler) exec 'from ' + mod_name + ' import test' b=1 test(b) try: b = 1. test(b) except TypeError: pass try: b = 'abc' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'dict_var_in' + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = {'z': 1} code = 'a=py::dict();' # This just checks to make sure the type is correct test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = {'y': 2} test(b) try: b = 1. test(b) except TypeError: pass try: b = 'string' test(b) except TypeError: pass
def test_float_var_in(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 1. code = "a=2.;" test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1. test(b) try: b = 1. test(b) except TypeError: pass try: b = 'abc' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'tuple_var_in' + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = (1, ) code = 'a=py::tuple();' test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = (1, 2) test(b) try: b = 1. test(b) except TypeError: pass try: b = 'string' test(b) except TypeError: pass
def test_float_var_in(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = 1. code = "a=2.;" test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec 'from ' + mod_name + ' import test' b = 1. test(b) try: b = 1. test(b) except TypeError: pass try: b = 'abc' test(b) except TypeError: pass
def __init__(self, suppress_warnings = True, force_name = None): if type(force_name) != type(""): call_frame = sys._getframe().f_back name = call_frame.f_globals['__name__'] else: name = force_name self.module = sys.modules[name] self.dest_dir = os.path.dirname(self.module.__file__) self._module_name = name.split(".")[-1] + "_c" # check to see if rebuild needed self.extension = ext_tools.ext_module(self._module_name) self.customize = self.extension.customize self.customize.add_include_dir(self.dest_dir) self.customize.add_support_code(struct_support_code) if suppress_warnings: self.customize.add_extra_compile_arg('-Wno-unused-variable') self.customize.add_extra_compile_arg('-Wno-write-strings')
def test_complex_var_in(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = numpy.complex(1.+1j) code = "a=std::complex<double>(2.,2.);" test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = numpy.complex128(1.+1j) test(b) try: b = 1. test(b) except TypeError: pass try: b = 'abc' test(b) except TypeError: pass
def test_complex_var_in(self): mod_name = sys._getframe().f_code.co_name + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = 1. + 1j code = "a=std::complex<double>(2.,2.);" test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1. + 1j test(b) try: b = 1. test(b) except TypeError: pass try: b = 'abc' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'dict_var_in'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = {'z':1} code = 'a=py::dict();' # This just checks to make sure the type is correct test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location = test_dir, compiler = self.compiler) exec 'from ' + mod_name + ' import test' b={'y':2} test(b) try: b = 1. test(b) except TypeError: pass try: b = 'string' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'int_var_in' + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = 1 code = "a=2;" test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 1 test(b) try: b = 1. test(b) except TypeError: pass try: b = 'abc' test(b) except TypeError: pass
def test_var_in(self): mod_name = 'tuple_var_in'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = (1,) code = 'a=py::tuple();' test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location = test_dir, compiler = self.compiler) exec 'from ' + mod_name + ' import test' b=(1,2) test(b) try: b = 1. test(b) except TypeError: pass try: b = 'string' test(b) except TypeError: pass
def __init__(self, suppress_warnings = True, force_name = None): if type(force_name) != type(""): call_frame = sys._getframe().f_back name = call_frame.f_globals['__name__'] else: name = force_name self.module = sys.modules[name] self.dest_dir = os.path.dirname(self.module.__file__) self._module_name = split(name,".")[-1]+"_c" # check to see if rebuild needed self.extension = ext_tools.ext_module(self._module_name) self.customize = self.extension.customize self.customize.add_include_dir(self.dest_dir) self.customize.add_support_code(struct_support_code) if suppress_warnings: self.customize.add_extra_compile_arg('-Wno-unused-variable') self.customize.add_extra_compile_arg('-Wno-write-strings')
def build_beamformer(): mod = ext_tools.ext_module('beamformer') faverage(mod) r_beamfuncs(mod) r_beamfuncs_os(mod) r_beam_psf(mod) r_beam_psf1(mod) r_beam_psf2(mod) r_beam_psf3(mod) r_beam_psf4(mod) transfer(mod) gseidel(mod) # --- compiler arguments --- # -O3 : maximum optimization # -ffast-math : fast floating point ops, violates IEEE standards # -msse3 : enable usage of SSE3 instruction set # -Wno-write-strings : suppress warnings concerning strings conversions # -fopenmp : flag to enable code parallelization, linux only # -lgomp : library needed for code parallelization, linux only extra_compile_args = ['-O3','-ffast-math','-msse3', \ '-Wno-write-strings','-fopenmp'] extra_link_args = ['-lgomp'] if sys.platform[:5] == 'linux': compiler = 'unix' mod.compile(extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, verbose=4, compiler=compiler) else: #uncomment for non-openmp version extra_compile_args.pop() extra_link_args.pop() mod.compile(extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, verbose=4)
def test_var_in(self): mod_name = 'string_var_in' + self.compiler mod_name = unique_mod(test_dir, mod_name) mod = ext_tools.ext_module(mod_name) a = 'string' code = 'a=std::string("hello");' test = ext_tools.ext_function('test', code, ['a']) mod.add_function(test) mod.compile(location=test_dir, compiler=self.compiler) exec('from ' + mod_name + ' import test') b = 'bub' test(b) try: b = 1. test(b) except TypeError: pass try: b = 1 test(b) except TypeError: pass
def test_with_include(self): # decalaring variables a = 2.0 # declare module mod = ext_tools.ext_module("ext_module_with_include") mod.customize.add_header("<iostream>") # function 2 --> a little more complex expression var_specs = ext_tools.assign_variable_types(["a"], locals(), globals()) code = """ std::cout.clear(std::ios_base::badbit); std::cout << std::endl; std::cout << "test printing a value:" << a << std::endl; std::cout.clear(std::ios_base::goodbit); """ test = ext_tools.ext_function_from_specs("test", code, var_specs) mod.add_function(test) # build module mod.compile(location=build_dir) import ext_module_with_include ext_module_with_include.test(a)
def test_var_in(self): mod_name = 'string_var_in'+self.compiler mod_name = unique_mod(test_dir,mod_name) mod = ext_tools.ext_module(mod_name) a = 'string' code = 'a=std::string("hello");' test = ext_tools.ext_function('test',code,['a']) mod.add_function(test) mod.compile(location = test_dir, compiler = self.compiler) exec 'from ' + mod_name + ' import test' b='bub' test(b) try: b = 1. test(b) except TypeError: pass try: b = 1 test(b) except TypeError: pass
def compile_cpp_filters(): """ This method finds and compile every c++ filters If a c++ file changed, the file must be recompiled in a new .so file """ BUILD_DIR = 'build' RELOAD_DIR = os.path.join('build', 'reload') if not os.path.exists(BUILD_DIR): os.mkdir(BUILD_DIR) if not os.path.exists(RELOAD_DIR): os.mkdir(RELOAD_DIR) if len(g.cppfiles) == 0: for f in os.listdir(RELOAD_DIR): os.remove(os.path.join(RELOAD_DIR, f)) image = np.zeros((1,1), dtype=np.uint8) params = {} def ext_code(): """ Return the code that calls a c++ filter """ return """ cv::Mat mat(Nimage[0], Nimage[1], CV_8UC(3), image); cv::Mat ret = execute(mat); if (mat.data != ret.data) { ret.copyTo(mat); } """ def init_code(): """ This method returns the code to initialize the parameters """ return """ params = p; py_init_param = pip; init(); """ def params_code(): """ Support code to declare parameters in the C++ filters It assumes a method py_init_param received as a parameter from the python side to create the parameters in the python object. """ return """ py::dict params; py::object py_init_param; void init_param(const char* name, int min, int max, int def_val) { py::tuple args(4); args[0] = name; args[1] = min; args[2] = max; args[3] = def_val; py_init_param.call(args); } long ParameterAsInt(const char* name, int min, int max, int def_val) { if(!params.has_key(name)) { init_param(name, min, max, def_val); } py::object o = params.get(name); return PyInt_AsLong(o.mcall("get_current_value")); } bool ParameterAsBool(const char* name, int min, int max, int def_val) { if(!params.has_key(name)) { init_param(name, min, max, def_val); } return PyInt_AsLong(params.get(name).mcall("get_current_value")); } """ def help_code(): """ Return the code that returns the help string from a c++ file """ return """ #ifdef DOCSTRING return_val = DOCSTRING; #else return_val = ""; #endif """ def config_code(): """ Code to reconfigure the filter """ return """ configure(); """ def create_execute(cppfunc): """ Create and return an "execute" method for the dynamically created class that wraps the c++ filters """ def execute(self, image): cppfunc(image) return image return execute def create_configure(cppfunc): """ """ def configure(self): cppfunc() return configure def create_init(cppfunc, params): """ """ def __init__(self): self.params = {} cppfunc(self.params, self.py_init_param) return __init__ def py_init_param(self, name, min, max, def_val): param = Parameter(name, min, max, def_val) self.params[name] = param setattr(self, name, param) dirname = os.path.dirname(__file__) for f in os.listdir(dirname): if not f.endswith(".cpp"): continue filename, _ = os.path.splitext(f) cppcode = open(os.path.join(dirname, f)).read() #Verify if there are changes in the c++ code file. If there are #changes, add a timestamp to the filter .so file name to force a #reimportation of the new filter. if g.cppfiles.has_key(filename): if cppcode != g.cppfiles[filename]: g.cpptimestamps[filename] = str(int(time.time())) g.cppfiles[filename] = cppcode if g.cpptimestamps.has_key(filename): modname = filename + g.cpptimestamps[filename] else: modname = filename #Compile filters #The included files are found in the .cpp file mod = ext_tools.ext_module(modname) [mod.customize.add_header(line.replace('#include ', '')) for line in cppcode.split('\n') if line.startswith('#include')] mod.customize.add_header("<Python.h>") mod.customize.add_extra_link_arg("`pkg-config --cflags --libs opencv python`") mod.customize.add_extra_link_arg("-L/usr/local/cuda/lib64") func = ext_tools.ext_function('exec_' + filename, ext_code(),['image']) func.customize.add_support_code(params_code()) func.customize.add_support_code(cppcode) mod.add_function(func) helpfunc = ext_tools.ext_function('help_' + filename, help_code(), []) mod.add_function(helpfunc) p = params pip = py_init_param initfunc = ext_tools.ext_function('init_' + filename, init_code(), ['p', 'pip']) initfunc.customize.add_support_code(params_code()) #initfunc.customize.add_support_code(cppcode) mod.add_function(initfunc) configfunc = ext_tools.ext_function('config_' + filename, config_code(), []) configfunc.customize.add_support_code(params_code()) configfunc.customize.add_support_code(cppcode) mod.add_function(configfunc) try: if g.cpptimestamps.has_key(filename): #Reloaded modules are saved in the reload folder for easy cleanup mod.compile(RELOAD_DIR) else: mod.compile(BUILD_DIR) cppmodule = __import__(modname) params = {} clazz = type(filename, (object,), {'__init__' : create_init( getattr(cppmodule, 'init_' + filename), params), 'configure' : create_configure( getattr(cppmodule, 'config_' + filename)), 'execute' : create_execute( getattr(cppmodule, 'exec_' + filename)), 'py_init_param' : py_init_param, '__doc__' : getattr(cppmodule, 'help_' + filename)()}) setattr(sys.modules[__name__], filename, clazz) del clazz except Exception as e: sys.stderr.write(str(e) + '\n') sys.stderr.write(traceback.format_exc() + "\n")
def test_simple(self): """ Simplest possible module """ mod = ext_tools.ext_module('simple_ext_module') mod.compile(location=build_dir) import simple_ext_module
def build_extension(module_name,location='.',**kw): """ Build an extension module for this package. """ mod = ext_tools.ext_module(module_name) #---------------------------------------------------------- # cg_slide(iparam, dparam, x, y) #---------------------------------------------------------- # slide the view along its x- and y- axes #---------------------------------------------------------- # this is effectively a type declaration for function arguments iparam = array((3,), 'i') dparam = array((4,), 'd') x = 1.0 y = 1.0 # code code = """ double cA, sA; cA = cos(dparam[0]) * dparam[1]; sA = sin(dparam[0]) * dparam[1]; // slide via x-axis dparam[2] += cA * x; dparam[3] += sA * x; // slide via y-axis if(iparam[2]) { dparam[2] += sA * y; dparam[3] -= cA * y; } else { dparam[2] -= sA * y; dparam[3] += cA * y; } return_val = 1; """ func = ext_tools.ext_function('cg_slide',code, ['iparam', 'dparam', 'x', 'y']) # func.customize.add_support_code(fib_code) mod.add_function(func) #---------------------------------------------------------- # cg_transform_pairs(iparam, dparam, src, dst) #---------------------------------------------------------- # get 3 tranformation coordinate pairs #---------------------------------------------------------- # this is effectively a type declaration for function arguments iparam = array((3,), 'i') dparam = array((4,), 'd') src = array((6,), 'd') dst = array((6,), 'd') # code code = """ double cA, sA, x0, y0, x1, y1; cA = cos(dparam[0]) * dparam[1]; sA = sin(dparam[0]) * dparam[1]; x0 = dparam[2]; y0 = dparam[3]; x1 = 0.5 * iparam[0]; y1 = 0.5 * iparam[1]; src[0] = x0; src[1] = y0; src[2] = x0 + cA; src[3] = y0 + sA; if(iparam[2]) { src[4] = x0 + sA; src[5] = y0 - cA; } else { src[4] = x0 - sA; src[5] = y0 + cA; } dst[0] = x1; dst[1] = y1; dst[2] = x1+1; dst[3] = y1; dst[4] = x1; dst[5] = y1+1; return_val = 1; """ func = ext_tools.ext_function('cg_transform_pairs',code, ['iparam', 'dparam', 'src', 'dst']) mod.add_function(func) #---------------------------------------------------------- # return the module #---------------------------------------------------------- return mod.setup_extension(location,**kw)
def buildBoundedThinPlateLibrary(): from scipy.weave import ext_tools thinPlate2D_code = r""" #line 8 "_bounded_thinPlate.py" //void thinPlate2D_mat(double* C,double R, int cmin=0, int cmax=-1, int symm=0) if (cmax==-1) cmax=Nc[1]; if (symm) { for(int j=cmin;j<cmax;j++) { C2(j,j)=1.; for(int i=0;i<j-1;i++) { C2(i,j)=thinPlate2D(C2(i,j),R); } } } else { for(int j=cmin;j<cmax;j++) { C2(j,j)=1.; for(int i=0;i<Nc[0];i++) { C2(i,j)=thinPlate2D(C2(i,j),R); } } } """ thinPlate3D_code = thinPlate2D_code.replace('2D', '3D') thinPlate_support_code = r""" #line 39 "_bounded_thinPlate.py" double thinPlate2D(double d, double R) { double t; if (d == 0.0) t = 1.0; else { if (d < R){ const double d2 = d*d; const double R2 = R*R; t = (2*d2*log(d)-(1+2*log(R))*d2+R2)/R2; }else{ t = 0.0e0 ; } } return t; } double thinPlate3D(double d,double R) { double t; if (d == 0.) t = 1.0e0; else { if (d < R) { const double d2 = d*d; const double d3 = fabs(d)*d2; const double R2 = R*R; const double R3 = fabs(R)*R2; t = (2*d3-3*R*d2+R3)/R3; }else{ t = 0.0e0; } } return t; } """ innerProduct_thinPlate3D_normalized_code = r""" #line 83 "_bounded_thinPlate.py" //innerProduct_thinPlate3D_normalized(C,Q,R,symm) if (R < Q) { const double a = Q; Q = R; R = a; } double Rs[10]; double Qs[10]; Rs[0] = R; Qs[0] = Q; int i; for( i=0;i<9;i++ ){ Rs[i+1] = Rs[i] * R; Qs[i+1] = Qs[i] * Q; } if (symm) { for (int j=0;j<Nc[1];i++) { int i; for (i=0; i<j; i++){ C2(i,j) = covIntegralThinPlateR3Normalized( C2(i,j), Q, R, Qs, Rs); C2(j,i) = C2(i,j); } } }else{ for(int j=0; j<Nc[1]; j++) { int i; for(i=0;i<Nc[0]; i++) { C2(i,j) = covIntegralThinPlateR3Normalized( C2(i,j), Q, R, Qs, Rs); } } } """ innerProduct_thinPlate3D_normalized_support_code = r""" #line 115 "_bounded_thinPlate.py" double covIntTPR3NwLTRminusQ (double w, double Q, double R, const double* Qs, const double* Rs); double covIntTP3NRminusQLTw ( double w, double Q, double R, const double* Qs, const double* Rs); double covIntTP3NwGTR ( double w, double Q, double R, const double* Qs, const double* Rs); double covIntegralThinPlateR3Normalized( double w, double Q, double R, const double* Qs, const double* Rs) { double aux; if ( Q > R ) { aux = R; R = Q; Q = aux; } if ( w == 0 ) { return acos( -1.0e0 ) * double(Qs[3]) * double(84 * Rs[3] - 81 *R * Qs[2] + 35 * Qs[3]) / double(Rs[3]) / 0.315e3; }else if ( w <= (R-Q) ) { return covIntTPR3NwLTRminusQ(w,Q,R,Qs,Rs); }else if ( ( (R-Q) < w ) && ( w <= R) ) { return covIntTP3NRminusQLTw(w,Q,R,Qs,Rs); }else if ( w > R ) { return covIntTP3NwGTR(w,Q,R,Qs,Rs); } return 0; } double covIntTPR3NwLTRminusQ (double w, double Q, double R, const double* Qs, const double* Rs) { if ( w <= Q ) { return acos( -1.0e0 ) * double(-405 * Qs[8] * R - 1260 * Qs[6] * R * pow(w,2) + 420 * Qs[6] * Rs[3] + 15 * Q * pow(w,8) + 175 * Qs[9] + 900 * Qs[7] * pow(w,2) + 378* Qs[5] * pow(w,4) - 60 * Qs[3] * pow(w,6) - 4 * pow(w,9)) / double(Qs[3]) / double(Rs[3]) / 0.1575e4; }else{ return acos( -1.0e0 ) * double(Qs[3]) * double(-135 * Qs[2] * w * R - 420 * pow(w,3) * R + 140 * w * Rs[3] + 180 * Qs[2] * pow(w,2) + 280 * pow(w,4) + 8 * Qs[4]) / double(w) / double(Rs[3]) / 0.525e3; } } double covIntTP3NRminusQLTw ( double w, double Q, double R, const double* Qs, const double* Rs) { if ( w <= Q ) { return -0.40e1 / 0.525e3 * M_PI *(pow(w,10) / 0.2e1 + (-0.5e1 / 0.8e1 * R - 0.5e1 / 0.8e1 * Q) * pow(w,9) - 0.135e3 / 0.64e2 * pow(w,8) * R * Q + (0.5e1 / 0.2e1 * Rs[3] + 0.5e1 / 0.2e1 * Qs[3]) * pow(w,7) + 0.105e3 / 0.16e2 * Q * R * (Rs[2] + Qs[2]) * pow(w,6) + (-0.63e2 / 0.4e1 * Qs[5] - 0.63e2 / 0.4e1 * Rs[5]) * pow(w,5) + (-0.945e3 / 0.32e2 * Q * Rs[5] - 0.175e3/ 0.16e2 * Qs[3] * Rs[3] - 0.945e3 / 0.32e2 * Qs[5] * R + 0.35e2 * Rs[6] + 0.35e2 * Qs[6]) * pow(w,4) + (0.105e3 / 0.2e1 * Rs[6] * Q - 0.75e2 / 0.2e1 * Rs[7] + 0.105e3 / 0.2e1 * Qs[6] * R - 0.75e2 / 0.2e1 * Qs[7]) * pow(w,3) + 0.45e2 / 0.2e1 * pow(( Q - R ),4)* (Qs[4] + 0.17e2 / 0.8e1 * Qs[3] * R + 0.5e1 / 0.2e1 * Qs[2] *Rs[2] + 0.17e2 / 0.8e1 * Q * Rs[3] + Rs[4]) * pow(w,2) + (0.135e3 / 0.8e1 * Rs[8] * Q - 0.175e3 / 0.24e2 * Qs[9] - 0.35e2 /0.2e1* Qs[6] * Rs[3] - 0.175e3 / 0.24e2 * Rs[9] - 0.35e2 / 0.2e1 *Rs[6] * Qs[3] + 0.135e3 / 0.8e1 * Qs[8] * R)*w + pow(( Q - R ),6) * (Qs[4] + 0.209e3 / 0.64e2 * Qs[3] * R + 0.147e3 / 0.32e2 * Qs[2] * Rs[2] + 0.209e3 / 0.64e2 * Q * Rs[3] + Rs[4])) / Qs[3] / w / Rs[3] ; }else{ return M_PI * (0.192e3 * pow(Q,10) - 0.192e3 * pow(R,10) - 0.32e2 * pow(w,10) + 0.8100e4 * pow(w,2) * Qs[7] * R - 0.10080e5 * pow(w,3) * Qs[6] * R + 0.8100e4 * Q * pow(w,2) * Rs[7] - 0.3780e4 * pow(w,2) * Qs[5] * Rs[3] + 0.3360e4 * Rs[6] * Qs[3] * w - 0.3240e4 * w * Qs[8] * R - 0.900e3 * Qs[7] * Rs[3] + 0.480e3 * pow(w,7) * Qs[3] + 0.405e3 * pow(w,8) * R * Q - 0.3780e4 * Qs[3] * pow(w,2) * Rs[5] - 0.1260e4 * pow(w,6) * Qs[3] * R + 0.5670e4 * Q * pow(w,4) * Rs[5] + 0.5670e4 * pow(w,4) * Qs[5] * R - 0.3240e4 * Rs[8] * Q * w + 0.3360e4 * w * Qs[6] * Rs[3] + 0.2100e4 * pow(w,4) * Qs[3] * Rs[3] - 0.1260e4 * pow(w,6) * Q * Rs[3] - 0.10080e5 * pow(w,3) * Rs[6] * Q + 0.525e3 * Qs[9] * R - 0.480e3 * pow(w,7) * Rs[3] + 0.120e3 * pow(w,9) * R - 0.120e3 * pow(w,9) * Q - 0.1400e4 * w * Qs[9] + 0.4320e4 * pow(w,2) * Qs[8] - 0.7200e4 * pow(w,3) * Qs[7] + 0.6720e4 * pow(w,4) * Qs[6] + 0.525e3 * Q * Rs[9] + 0.1134e4 * Qs[5] * Rs[5] - 0.900e3 * Qs[3] * Rs[7] + 0.7200e4 * pow(w,3) * Rs[7] + 0.1400e4 * w * Rs[9] - 0.6720e4 * pow(w,4) * Rs[6] + 0.3024e4 * pow(w,5) * Rs[5] - 0.4320e4 * pow(w,2) * Rs[8] - 0.3024e4 * pow(w,5) * Qs[5]) / Qs[3] / w / Rs[3] / 0.25200e5; } } double covIntTP3NwGTR ( double w, double Q, double R, const double* Qs, const double* Rs) { if ( w <= R+Q ) { return 0.4e1 / 0.525e3 * acos( -1.0e0 ) * pow((R + Q - w),6) * (pow(w,4) / 0.6e1 + (0.3e1 / 0.8e1 * Q + 0.3e1 / 0.8e1 * R) * pow(w,3) + (0.103e3 / 0.64e2 * Q * R - Qs[2] / 0.4e1 - Rs[2] / 0.4e1) * pow(w,2) - 0.31e2 / 0.24e2 * (Rs[2] - 0.247e3 / 0.124e3 * Q * R + Qs[2]) * (R + Q) * w + Rs[4] - 0.209e3 / 0.64e2 * Qs[3] * R + 0.147e3 / 0.32e2 * Qs[2] * Rs[2] + Qs[4] -0.209e3 / 0.64e2 * Q * Rs[3]) / Qs[3] / w / Rs[3]; }else{ return 0; } } """ import numpy c = numpy.eye(5, dtype=float) R = 1. Q = 1. cmin = 1 cmax = 1 symm = 1 thinPlateArguments = ['c', 'R', 'cmin', 'cmax', 'symm'] innerProductArguments = ['c', 'R', 'Q', 'symm'] mod = ext_tools.ext_module('_bounded_thinPlate_lib') mod.add_function( ext_tools.ext_function('thinPlate2D_mat', thinPlate2D_code, thinPlateArguments)) mod.add_function( ext_tools.ext_function('thinPlate3D_mat', thinPlate3D_code, thinPlateArguments)) mod.add_function( ext_tools.ext_function('innerProduct_thinPlate3D_normalized', innerProduct_thinPlate3D_normalized_code, innerProductArguments)) mod.customize.add_support_code(thinPlate_support_code) mod.customize.add_support_code( innerProduct_thinPlate3D_normalized_support_code) # mod.customize.add_extra_compile_arg('-O3') # mod.customize.add_extra_link_arg('-O3') mod.compile()