Skip to content

Instantly share code, notes, and snippets.

@CyclingNinja
Last active October 28, 2016 14:46
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 CyclingNinja/974d43582a16ccc747e54ee038114121 to your computer and use it in GitHub Desktop.
Save CyclingNinja/974d43582a16ccc747e54ee038114121 to your computer and use it in GitHub Desktop.
Cython, C++ and Structures. Trying to return the structure from the function that generates it
#include "header.h"
#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::getStuctVec(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 = PySquare.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 = PySquare.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 x;
double getSqr(double x);
double getCube(double x);
Powers();
~Powers();
OutVectors getStructVec(std::vector<double> v);
};
}
# distutils: language = c++
# distutils: sources = squaremod.cpp
cimport numpy as np
import numpy as np
cdef extern from "squaremod.h" namespace "ops":
cdef cppclass OutVectors:
vector[double] A, B
OutVectors(vector[double], vector[double])
cdef cppclass Powers:
Powers() except +
double x
double getSqr(double)
double getCube(double)
OutVectors getStuctVec(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);
s = PySquare()
self.thisptr = s.get_struct_vecs(v)
return self.thisptr.A, self.thisptr.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)
def get_struct_vecs(self, v):
return self.c_powers.getStuctVec(v)
from distutils.core import setup
from Cython.Build import cythonize
import numpy
ext_modules = cythonize("cyRoadDensity.pyx",
sources=['RoadDensityPyMod.cpp'],
language="c++"
)
setup(
name="RoadDensityPyMod",
ext_modules=ext_modules,
include_dirs=[numpy.get_include()]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment