Skip to content

Commit

Permalink
fix cxxwrap4
Browse files Browse the repository at this point in the history
  • Loading branch information
terasakisatoshi committed Jun 14, 2024
1 parent 14bf2c2 commit 217d0af
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions cxxwrap4/deps/src/hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@
#include "jlcxx/jlcxx.hpp"
#include "jlcxx/stl.hpp"
#include <Eigen/Dense>
#include <string>
#include <iostream>

auto print_address(jlcxx::ArrayRef<double, 1> jlx) {
std::cout << jlx.data() << std::endl;
static jlcxx::ArrayRef<double, 1> y = jlcxx::make_julia_array(jlx.data(), jlx.size());
auto x = jlcxx::make_julia_array(jlx.data(), jlx.size());
std::cout << x.data() << std::endl;
// do something with x and pass results to y
for (size_t i = 0; i < jlx.size(); i++) {
y[i] = 2*x[i];
}
std::cout << y.data() << std::endl;
return y;
}

auto twice(jlcxx::ArrayRef<double, 1> jlx) {
static Eigen::VectorXd y;
auto x = Eigen::Map<Eigen::VectorXd>(&jlx[0], jlx.size());
auto x = Eigen::Map<Eigen::VectorXd>(jlx.data(), jlx.size());
// do something with x and pass results to y
y = 2 * x;

return jlcxx::make_julia_array(y.data(), y.size());
}

Expand All @@ -21,21 +33,22 @@ void inplace_twice(jlcxx::ArrayRef<double, 1> jlx) {

auto triple(jlcxx::ArrayRef<double, 1> v) {
static Eigen::VectorXd y;
auto x = Eigen::Map<Eigen::VectorXd>(&v[0], v.size());
auto x = Eigen::Map<Eigen::VectorXd>(v.data(), v.size());
// Do something
y = 2 * x + x;
return jlcxx::make_julia_array(y.data(), y.size());
}

auto inplace_triple(jlcxx::ArrayRef<double, 1> jlx) {
auto x = Eigen::Map<Eigen::VectorXd>(&jlx[0], jlx.size());
auto x = Eigen::Map<Eigen::VectorXd>(jlx.data(), jlx.size());
x = 3 * x;
for (size_t i = 0; i < jlx.size(); i++) {
jlx[i] = x[i];
}
}

JLCXX_MODULE define_julia_module(jlcxx::Module &mod) {
mod.method("print_address", &print_address);
mod.method("twice", &twice);
mod.method("twice!", &inplace_twice);

Expand Down

0 comments on commit 217d0af

Please sign in to comment.