Created
May 28, 2017 13:19
-
-
Save nbecker/59054a8a7e3c94aea19fbd1af3eef611 to your computer and use it in GitHub Desktop.
summer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "jlcxx/jlcxx.hpp" | |
#include "xtensor-julia/jltensor.hpp" // Import the jltensor container definition | |
#include "xtensor-julia/jlarray.hpp" // Import the jltensor container definition | |
#include "xtensor/xmath.hpp" // xtensor import for the C++ universal functions | |
#include <complex> | |
typedef std::complex<double> complex_t; | |
template<typename el_t> | |
struct summer { | |
el_t sum; | |
summer() { sum = 0; } | |
void operator()(el_t e) { sum += e; } | |
}; | |
template<typename el_t> | |
void expose_sum (const char* name, jlcxx::Module& mod) { | |
typedef summer<el_t> accum_t; | |
mod.add_type<accum_t>(name); | |
mod.method ("call", [](accum_t & a, el_t s) { a(s); return a;}); | |
mod.method ("sum", [](accum_t const& a) { return a.sum; }); | |
mod.method ("call", [](accum_t& a, xt::jlarray<el_t> const& s) { | |
for (auto e : s) a(e); | |
return a; | |
}); | |
} | |
JULIA_CPP_MODULE_BEGIN(registry) | |
jlcxx::Module& acc = registry.create_module("summer"); | |
expose_sum<double> ("sum_double", acc); | |
expose_sum<complex_t> ("sum_complex", acc); | |
// py::object stat1st_double_obj = expose_1st<double> ("stat1st_double", m); | |
// py::object stat1st_complex_obj = expose_1st<complex_t> ("stat1st_complex", m); | |
// py::object stat2nd_float_obj = expose_2nd<float> ("stat2nd_float", m); | |
// py::object stat2nd_complex64_obj = expose_2nd<complex64_t> ("stat2nd_complex64", m); | |
// py::object stat1st_float_obj = expose_1st<float> ("stat1st_float", m); | |
// py::object stat1st_complex64_obj = expose_1st<complex64_t> ("stat1st_complex64", m); | |
// // aliases | |
// m.attr ("accumulator_double") = stat2nd_double_obj; | |
// m.attr ("accumulator_complex") = stat2nd_complex_obj; | |
// expose_extrema<float> ("extrema_float", m); | |
// expose_extrema<double> ("extrema_double", m); | |
// expose_extrema<int32_t> ("extrema_int32", m); | |
// expose_extrema<int64_t> ("extrema_int64", m); | |
// return m.ptr(); | |
JULIA_CPP_MODULE_END | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CxxWrap | |
using Xtensor | |
wrap_modules(Xtensor._l_tensors) | |
wrap_modules("./libsummer") | |
using summer: sum_double, call, sum, sum_complex | |
s = sum_double() | |
call(s, 2.0) | |
#s2 = stat2nd_complex() | |
#call(s2, complex(2.0)) | |
call(s, ones(10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment