SHARE
TWEET

Untitled

a guest Oct 12th, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // python_embed.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
  7. #include "Python.h"
  8. #include "numpy/arrayobject.h"
  9. #include<iostream>
  10.  
  11. using namespace std;
  12.  
  13. int _tmain(int argc, _TCHAR* argv[])
  14. {
  15.     Py_SetProgramName(argv[0]);
  16.     Py_Initialize();
  17.     import_array()
  18.  
  19.     // Build the 2D array
  20.     PyObject *pArgs, *pReturn, *pModule, *pFunc;
  21.     PyArrayObject *np_ret, *np_arg;
  22.     const int SIZE{ 10 };
  23.     npy_intp dims[2]{SIZE, SIZE};
  24.     const int ND{ 2 };
  25.     long double(*c_arr)[SIZE]{ new long double[SIZE][SIZE] };
  26.     long double* c_out;
  27.     for (int i{}; i < SIZE; i++)
  28.         for (int j{}; j < SIZE; j++)
  29.             c_arr[i][j] = i * SIZE + j;
  30.  
  31.     np_arg = reinterpret_cast<PyArrayObject*>(PyArray_SimpleNewFromData(ND, dims, NPY_LONGDOUBLE,
  32.         reinterpret_cast<void*>(c_arr)));
  33.  
  34.     // Calling array_tutorial from mymodule
  35.     PyObject *pName = PyUnicode_FromString("mymodule");
  36.     pModule = PyImport_Import(pName);
  37.     Py_DECREF(pName);
  38.     if (!pModule){
  39.         cout << "mymodule can not be imported" << endl;
  40.         Py_DECREF(np_arg);
  41.         delete[] c_arr;
  42.         return 1;
  43.     }
  44.     pFunc = PyObject_GetAttrString(pModule, "array_tutorial");
  45.     if (!pFunc || !PyCallable_Check(pFunc)){
  46.         Py_DECREF(pModule);
  47.         Py_XDECREF(pFunc);
  48.         Py_DECREF(np_arg);
  49.         delete[] c_arr;
  50.         cout << "array_tutorial is null or not callable" << endl;
  51.         return 1;
  52.     }
  53.     pArgs = PyTuple_New(1);
  54.     PyTuple_SetItem(pArgs, 0, reinterpret_cast<PyObject*>(np_arg));
  55.     pReturn = PyObject_CallObject(pFunc, pArgs);
  56.     np_ret = reinterpret_cast<PyArrayObject*>(pReturn);
  57.     if (PyArray_NDIM(np_ret) != ND - 1){ // row[0] is returned
  58.         cout << "Function returned with wrong dimension" << endl;
  59.         Py_DECREF(pFunc);
  60.         Py_DECREF(pModule);
  61.         Py_DECREF(np_arg);
  62.         Py_DECREF(np_ret);
  63.         delete[] c_arr;
  64.         return 1;
  65.     }
  66.     int len{ PyArray_SHAPE(np_ret)[0] };
  67.     c_out = reinterpret_cast<long double*>(PyArray_DATA(np_ret));
  68.     cout << "Printing output array" << endl;
  69.     for (int i{}; i < len; i++)
  70.         cout << c_out[i] << ' ';
  71.     cout << endl;
  72.  
  73.     // Finalizing
  74.     Py_DECREF(pFunc);
  75.     Py_DECREF(pModule);
  76.     Py_DECREF(np_arg);
  77.     Py_DECREF(np_ret);
  78.     delete[] c_arr;
  79.     Py_Finalize();
  80.     return 0;
  81. }
  82.    
  83. #include <iostream>
  84. #include "xtensor/xarray.hpp"
  85. #include "xtensor/xio.hpp"
  86.  
  87. xt::xarray<double> arr1
  88.   {{1.0, 2.0, 3.0},
  89.    {2.0, 5.0, 7.0},
  90.    {2.0, 5.0, 7.0}};
  91.  
  92. xt::xarray<double> arr2
  93.   {5.0, 6.0, 7.0};
  94.  
  95. xt::xarray<double> res = xt::view(arr1, 1) + arr2;
  96.  
  97. std::cout << res;
  98.    
  99. {7, 11, 14}
  100.    
  101. #include "pybind11/pybind11.h"
  102. #include "xtensor-python/pyvectorize.hpp"
  103. #include <numeric>
  104. #include <cmath>
  105.  
  106. namespace py = pybind11;
  107.  
  108. double scalar_func(double i, double j)
  109. {
  110.     return std::sin(i) - std::cos(j);
  111. }
  112.  
  113. PYBIND11_PLUGIN(xtensor_python_test)
  114. {
  115.     py::module m("xtensor_python_test", "Test module for xtensor python bindings");
  116.  
  117.     m.def("vectorized_func", xt::pyvectorize(scalar_func), "");
  118.  
  119.     return m.ptr();
  120. }
  121.    
  122. import numpy as np
  123. import xtensor_python_test as xt
  124.  
  125. x = np.arange(15).reshape(3, 5)
  126. y = [1, 2, 3, 4, 5]
  127. z = xt.vectorized_func(x, y)
  128. z
  129.    
  130. [[-0.540302,  1.257618,  1.89929 ,  0.794764, -1.040465],
  131.  [-1.499227,  0.136731,  1.646979,  1.643002,  0.128456],
  132.  [-1.084323, -0.583843,  0.45342 ,  1.073811,  0.706945]]
  133.    
  134. #include <pybind11/embed.h> // everything needed for embedding
  135. #include <iostream>
  136. #include <Eigen/Dense>  
  137. #include<pybind11/eigen.h>
  138. using Eigen::MatrixXd;
  139.  
  140. int main()
  141. {    
  142.   try
  143.   {          
  144.         Py_SetProgramName("PYTHON");
  145.         py::scoped_interpreter guard{};
  146.  
  147.         py::module py_test = py::module::import("py_test");
  148.  
  149.         MatrixXd m(2,2);
  150.         m(0,0) = 1;
  151.         m(1,0) = 2;
  152.         m(0,1) = 3;
  153.         m(1,1) = 4;
  154.  
  155.         py::object result = py_test.attr("test_mat")(m);
  156.  
  157.         MatrixXd res = result.cast<MatrixXd>();
  158.         std::cout << "In c++ n" << m << std::endl;
  159.   }
  160.   catch (std::exception ex)
  161.   {
  162.       std::cout << "ERROR   : " << ex.what() << std::endl;
  163.   }
  164.   return 1;
  165. }
  166.    
  167. def test_mat(m):
  168.     print ("Inside python m = n ",m )
  169.     m[0,0] = 10
  170.     m[1,1] = 99
  171.     return m
  172.    
  173. Inside python m =
  174.   [[ 1.  3.]
  175.   [ 2.  4.]]
  176. In c++
  177. 10  3
  178.  2 99
RAW Paste Data
Top