|
@@ -49,6 +49,7 @@
|
|
#include "nmie-impl.hpp"
|
|
#include "nmie-impl.hpp"
|
|
#include "nmie-precision.hpp"
|
|
#include "nmie-precision.hpp"
|
|
#include <array>
|
|
#include <array>
|
|
|
|
+#include <tuple>
|
|
#include <algorithm>
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstdlib>
|
|
@@ -62,100 +63,11 @@
|
|
#include <pybind11/complex.h>
|
|
#include <pybind11/complex.h>
|
|
|
|
|
|
|
|
|
|
-// // -------------
|
|
|
|
-// // pure C++ code
|
|
|
|
-// // -------------
|
|
|
|
-
|
|
|
|
-// std::vector<int> multiply(const std::vector<double>& input)
|
|
|
|
-// {
|
|
|
|
-// std::vector<int> output(input.size());
|
|
|
|
-
|
|
|
|
-// for ( size_t i = 0 ; i < input.size() ; ++i )
|
|
|
|
-// output[i] = 10*static_cast<int>(input[i]);
|
|
|
|
-
|
|
|
|
-// return output;
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// // ----------------
|
|
|
|
-// // Python interface
|
|
|
|
-// // ----------------
|
|
|
|
-
|
|
|
|
-// namespace py = pybind11;
|
|
|
|
-
|
|
|
|
-// // wrap C++ function with NumPy array IO
|
|
|
|
-// py::array_t<int> py_multiply(py::array_t<double, py::array::c_style | py::array::forcecast> array)
|
|
|
|
-// {
|
|
|
|
-// // allocate std::vector (to pass to the C++ function)
|
|
|
|
-// std::vector<double> array_vec(array.size());
|
|
|
|
-
|
|
|
|
-// // copy py::array -> std::vector
|
|
|
|
-// std::memcpy(array_vec.data(),array.data(),array.size()*sizeof(double));
|
|
|
|
-
|
|
|
|
-// // call pure C++ function
|
|
|
|
-// std::vector<int> result_vec = multiply(array_vec);
|
|
|
|
-
|
|
|
|
-// // allocate py::array (to pass the result of the C++ function to Python)
|
|
|
|
-// auto result = py::array_t<int>(array.size());
|
|
|
|
-// auto result_buffer = result.request();
|
|
|
|
-// int *result_ptr = (int *) result_buffer.ptr;
|
|
|
|
-
|
|
|
|
-// // copy std::vector -> py::array
|
|
|
|
-// std::memcpy(result_ptr,result_vec.data(),result_vec.size()*sizeof(int));
|
|
|
|
-
|
|
|
|
-// return result;
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// // wrap as Python module
|
|
|
|
-// PYBIND11_MODULE(example,m)
|
|
|
|
-// {
|
|
|
|
-// m.doc() = "pybind11 example plugin";
|
|
|
|
-
|
|
|
|
-// m.def("multiply", &py_multiply, "Convert all entries of an 1-D NumPy-array to int and multiply by 10");
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
namespace py = pybind11;
|
|
namespace py = pybind11;
|
|
|
|
|
|
-int add(int i, int j) {
|
|
|
|
- return i + j;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-int test_stdvec(const int a, const std::vector<double> input, std::vector<double>& output) {
|
|
|
|
- for (unsigned int i = 0; i < input.size();++i){
|
|
|
|
- output[i] = input[i]*a;
|
|
|
|
- }
|
|
|
|
- return a;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// PYBIND11_MAKE_OPAQUE(std::vector<double>);
|
|
|
|
-
|
|
|
|
-py::array_t<double, py::array::c_style | py::array::forcecast>
|
|
|
|
-py_test_stdvec(const int a,
|
|
|
|
- py::array_t<double, py::array::c_style | py::array::forcecast> py_input) {
|
|
|
|
- std::vector<double> c_input(py_input.size());
|
|
|
|
- std::vector<double> c_output(py_input.size());
|
|
|
|
-
|
|
|
|
- std::memcpy(c_input.data(), py_input.data(), py_input.size()*sizeof(double));
|
|
|
|
-
|
|
|
|
- int c_a = 0;
|
|
|
|
- c_a = test_stdvec(a, c_input, c_output);
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- auto result = py::array_t<double>(c_output.size());
|
|
|
|
- auto result_buffer = result.request();
|
|
|
|
- double *result_ptr = (double *) result_buffer.ptr;
|
|
|
|
|
|
|
|
- std::memcpy(result_ptr,c_output.data(),c_output.size()*sizeof(double));
|
|
|
|
|
|
|
|
- return result;
|
|
|
|
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-PYBIND11_MODULE(example, m) {
|
|
|
|
- m.doc() = "pybind11 example plugin"; // optional module docstring
|
|
|
|
-
|
|
|
|
- m.def("add", &add, "A function which adds two numbers");
|
|
|
|
- m.def("test_stdvec", &py_test_stdvec, "Multipy vec by a const");
|
|
|
|
-}
|
|
|
|
|
|
|
|
namespace nmie {
|
|
namespace nmie {
|
|
|
|
|
|
@@ -446,3 +358,115 @@ namespace nmie {
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
} // end of namespace nmie
|
|
} // end of namespace nmie
|
|
|
|
+
|
|
|
|
+// // -------------
|
|
|
|
+// // pure C++ code
|
|
|
|
+// // -------------
|
|
|
|
+
|
|
|
|
+// std::vector<double> length(const std::vector<double>& pos)
|
|
|
|
+// {
|
|
|
|
+// size_t N = pos.size() / 2;
|
|
|
|
+
|
|
|
|
+// std::vector<double> output(N*3);
|
|
|
|
+
|
|
|
|
+// for ( size_t i = 0 ; i < N ; ++i ) {
|
|
|
|
+// output[i*3+0] = pos[i*2+0];
|
|
|
|
+// output[i*3+1] = pos[i*2+1];
|
|
|
|
+// output[i*3+2] = std::pow(pos[i*2+0]*pos[i*2+1],.5);
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// return output;
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// // ----------------
|
|
|
|
+// // Python interface
|
|
|
|
+// // ----------------
|
|
|
|
+
|
|
|
|
+// namespace py = pybind11;
|
|
|
|
+
|
|
|
|
+// // wrap C++ function with NumPy array IO
|
|
|
|
+// py::array py_length(py::array_t<double, py::array::c_style | py::array::forcecast> array)
|
|
|
|
+// {
|
|
|
|
+// // check input dimensions
|
|
|
|
+// if ( array.ndim() != 2 )
|
|
|
|
+// throw std::runtime_error("Input should be 2-D NumPy array");
|
|
|
|
+// if ( array.shape()[1] != 2 )
|
|
|
|
+// throw std::runtime_error("Input should have size [N,2]");
|
|
|
|
+
|
|
|
|
+// // allocate std::vector (to pass to the C++ function)
|
|
|
|
+// std::vector<double> pos(array.size());
|
|
|
|
+
|
|
|
|
+// // copy py::array -> std::vector
|
|
|
|
+// std::memcpy(pos.data(),array.data(),array.size()*sizeof(double));
|
|
|
|
+
|
|
|
|
+// // call pure C++ function
|
|
|
|
+// std::vector<double> result = length(pos);
|
|
|
|
+
|
|
|
|
+// ssize_t ndim = 2;
|
|
|
|
+// std::vector<ssize_t> shape = { array.shape()[0] , 3 };
|
|
|
|
+// std::vector<ssize_t> strides = { sizeof(double)*3 , sizeof(double) };
|
|
|
|
+
|
|
|
|
+// // return 2-D NumPy array
|
|
|
|
+// return py::array(py::buffer_info(
|
|
|
|
+// result.data(), /* data as contiguous array */
|
|
|
|
+// sizeof(double), /* size of one scalar */
|
|
|
|
+// py::format_descriptor<double>::format(), /* data type */
|
|
|
|
+// ndim, /* number of dimensions */
|
|
|
|
+// shape, /* shape of the matrix */
|
|
|
|
+// strides /* strides for each axis */
|
|
|
|
+// ));
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// // wrap as Python module
|
|
|
|
+// PYBIND11_MODULE(example,m)
|
|
|
|
+// {
|
|
|
|
+// m.doc() = "pybind11 example plugin";
|
|
|
|
+
|
|
|
|
+// m.def("length", &py_length, "Calculate the length of an array of vectors");
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // int ScattCoeffs(const unsigned int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m,
|
|
|
|
+ // const int nmax, std::vector<std::complex<double> >& an, std::vector<std::complex<double> >& bn) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+std::complex<double>
|
|
|
|
+test_stdvec(const std::complex<double> a, const std::vector< std::complex<double> > input, std::vector< std::complex<double> >& output) {
|
|
|
|
+ for (unsigned int i = 0; i < input.size();++i){
|
|
|
|
+ output[i] = input[i]*a;
|
|
|
|
+ }
|
|
|
|
+ return a;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+std::tuple<
|
|
|
|
+ py::array_t< std::complex<double>, py::array::c_style | py::array::forcecast>,
|
|
|
|
+ std::complex<double>
|
|
|
|
+ >
|
|
|
|
+py_test_stdvec(const std::complex<double> a,
|
|
|
|
+ py::array_t< std::complex<double>, py::array::c_style | py::array::forcecast> py_input) {
|
|
|
|
+ std::vector< std::complex<double> > c_input(py_input.size());
|
|
|
|
+ std::vector< std::complex<double> > c_output(py_input.size());
|
|
|
|
+
|
|
|
|
+ std::memcpy(c_input.data(), py_input.data(), py_input.size()*sizeof( std::complex<double>));
|
|
|
|
+
|
|
|
|
+ std::complex<double> c_a = 0;
|
|
|
|
+ c_a = test_stdvec(a, c_input, c_output);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ auto result = py::array_t< std::complex<double>>(c_output.size());
|
|
|
|
+ auto result_buffer = result.request();
|
|
|
|
+ std::complex<double> *result_ptr = ( std::complex<double> *) result_buffer.ptr;
|
|
|
|
+
|
|
|
|
+ std::memcpy(result_ptr,c_output.data(),c_output.size()*sizeof( std::complex<double>));
|
|
|
|
+
|
|
|
|
+ return std::make_tuple(result, c_a);
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+PYBIND11_MODULE(example, m) {
|
|
|
|
+ m.doc() = "pybind11 example plugin"; // optional module docstring
|
|
|
|
+
|
|
|
|
+ m.def("test_stdvec", &py_test_stdvec, "Multipy vec by a const");
|
|
|
|
+}
|