Skip to content

Instantly share code, notes, and snippets.

@bjodah
Last active October 29, 2016 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjodah/5e89fd002036758e9037276fbfa2bab1 to your computer and use it in GitHub Desktop.
Save bjodah/5e89fd002036758e9037276fbfa2bab1 to your computer and use it in GitHub Desktop.
Code for cython mailing list discussion (https://groups.google.com/forum/#!topic/cython-users/SidoUF5uDKQ)
#include "header.hpp"
#include <stdlib.h>
#include <vector>
using namespace std;
namespace ops {
Powers :: Powers() {}
Powers::~Powers() {}
double Powers::getSqr(double x) {
return x*x;
}
double Powers::getCube(double x) {
return x*x*x;
}
OutVectors Powers::getStructVec(std::vector<double> v) {
// make the new vectors whe u get back sam
std::vector<double> sqs {};
std::vector<double> cbs {};
for(std::size_t i = 0; i < v.size(); ++i){
sqs.push_back(getSqr(i));
cbs.push_back(getSqr(i));
};
// so i want to return two newly created vectors from the function
// they will be generated as a result of 6 other vectors which I pass into the function from python
OutVectors s1;
s1.A = sqs;
s1.B = cbs;
return s1;
}
}
import powers_wrapper
import numpy as np
funcs = powers_wrapper.PySquare()
print("Squared {0},Cubed {1}".format(funcs.get_sqr(2), funcs.get_cube(2)))
T = np.array([1,2,3,4,5,6,7,8,9])
res = powers_wrapper.PyStruct(T)
#include <vector>
namespace ops{
struct OutVectors {
std::vector<double> A;
std::vector<double> B;
OutVectors(std::vector<double> A_={}, std::vector<double> B_={}) : A(A_), B(B_) {}
};
class Powers{
public:
double getSqr(double x);
double getCube(double x);
Powers();
~Powers();
OutVectors getStructVec(std::vector<double> v);
};
}
# distutils: language = c++
# distutils: extra_compile_args = ['-std=c++11']
# distutils: sources = calc_powers.cpp
from libcpp.vector cimport vector
cimport numpy as np
import numpy as np
cdef extern from "header.hpp" namespace "ops":
cdef cppclass OutVectors:
vector[double] A, B
OutVectors() # <--- nullary constructor
OutVectors(vector[double], vector[double])
cdef cppclass Powers:
Powers() except +
double getSqr(double)
double getCube(double)
OutVectors getStructVec(vector[double])
cdef class PyStruct:
cdef OutVectors *thisptr
def __cinit__(self, vector[double] A={1.0}, vector[double] B={1.0}):
self.thisptr = new OutVectors(A, B)
def __dealloc__(self):
del self.thisptr
# ideally, i'll just call this function from .py file and have the new vectors available to me
def GetVecPowers(self, vector[double] v):
cdef PySquare s = PySquare()
cdef OutVectors out = s.get_struct_vecs(v)
return out.A, out.B
cdef class PySquare(object):
cdef Powers c_powers
def __cinit__(self):
self.c_powers = Powers()
def get_sqr(self, double x):
return self.c_powers.getSqr(x)
def get_cube(self, double x):
return self.c_powers.getCube(x)
cdef OutVectors get_struct_vecs(self, vector[double] v):
return self.c_powers.getStructVec(v)
from distutils.core import setup
from Cython.Build import cythonize
import numpy
setup(
name="powers_wrapper",
ext_modules=cythonize("*.pyx"),
include_dirs=[numpy.get_include()]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment