GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — xtensor-python/test_python/main.cpp at master · DerThorsten/xtensor-python Expand file tree/ Copy pathmain.cppMore file actions 402 lines (334 loc) · 12 KB / Copy pathmain.cppFile metadata and controls402 lines (334 loc) · 12 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 /*************************************************************************** * Copyright (c) Wolf Vollprecht, Johan Mabille and Sylvain Corlay * * Copyright (c) QuantStack * * * * Distributed under the terms of the BSD 3-Clause License. * * * * The full license is in the file LICENSE, distributed with this software. * ****************************************************************************/ #include <numeric> #include "xtensor/xmath.hpp" #include "xtensor/xarray.hpp" #include "xtensor/xfixed.hpp" #define FORCE_IMPORT_ARRAY #include "xtensor-python/pyarray.hpp" #include "xtensor-python/pytensor.hpp" #include "xtensor-python/pyvectorize.hpp" #include "xtensor/xadapt.hpp" #include "xtensor/xstrided_view.hpp" namespace py = pybind11; using complex_t = std::complex<double>; // Examples double example1(xt::pyarray<double>& m) { return m(0); } xt::pyarray<double> example2(xt::pyarray<double>& m) { return m + 2; } xt::xarray<int> example3_xarray(const xt::xarray<int>& m) { return xt::transpose(m) + 2; } xt::xarray<int, xt::layout_type::column_major> example3_xarray_colmajor( const xt::xarray<int, xt::layout_type::column_major>& m) { return xt::transpose(m) + 2; } xt::xtensor<int, 3> example3_xtensor3(const xt::xtensor<int, 3>& m) { return xt::transpose(m) + 2; } xt::xtensor<int, 2> example3_xtensor2(const xt::xtensor<int, 2>& m) { return xt::transpose(m) + 2; } xt::xtensor<int, 2, xt::layout_type::column_major> example3_xtensor2_colmajor( const xt::xtensor<int, 2, xt::layout_type::column_major>& m) { return xt::transpose(m) + 2; } xt::xtensor_fixed<int, xt::xshape<4, 3, 2>> example3_xfixed3(const xt::xtensor_fixed<int, xt::xshape<2, 3, 4>>& m) { return xt::transpose(m) + 2; } xt::xtensor_fixed<int, xt::xshape<3, 2>> example3_xfixed2(const xt::xtensor_fixed<int, xt::xshape<2, 3>>& m) { return xt::transpose(m) + 2; } xt::xtensor_fixed<int, xt::xshape<3, 2>, xt::layout_type::column_major> example3_xfixed2_colmajor( const xt::xtensor_fixed<int, xt::xshape<2, 3>, xt::layout_type::column_major>& m) { return xt::transpose(m) + 2; } // Readme Examples double readme_example1(xt::pyarray<double>& m) { auto sines = xt::sin(m); return std::accumulate(sines.cbegin(), sines.cend(), 0.0); } double readme_example2(double i, double j) { return std::sin(i) - std::cos(j); } auto complex_overload(const xt::pyarray<std::complex<double>>& a) { return a; } auto no_complex_overload(const xt::pyarray<double>& a) { return a; } auto complex_overload_reg(const std::complex<double>& a) { return a; } auto no_complex_overload_reg(const double& a) { return a; } // // Operator examples // xt::pyarray<double> array_addition(const xt::pyarray<double>& m, const xt::pyarray<double>& n) { return m + n; } xt::pyarray<double> array_subtraction(xt::pyarray<double>& m, xt::pyarray<double>& n) { return m - n; } xt::pyarray<double> array_multiplication(xt::pyarray<double>& m, xt::pyarray<double>& n) { return m * n; } xt::pyarray<double> array_division(xt::pyarray<double>& m, xt::pyarray<double>& n) { return m / n; } // Vectorize Examples int add(int i, int j) { return i + j; } template <class T> std::string typestring() { return "Unknown"; } template <> std::string typestring<uint8_t>() { return "uint8"; } template <> std::string typestring<int8_t>() { return "int8"; } template <> std::string typestring<uint16_t>() { return "uint16"; } template <> std::string typestring<int16_t>() { return "int16"; } template <> std::string typestring<uint32_t>() { return "uint32"; } template <> std::string typestring<int32_t>() { return "int32"; } template <> std::string typestring<uint64_t>() { return "uint64"; } template <> std::string typestring<int64_t>() { return "int64"; } template <class T> inline std::string int_overload(xt::pyarray<T>& m) { return typestring<T>(); } void dump_numpy_constant() { std::cout << "NPY_BOOL = " << NPY_BOOL << std::endl; std::cout << "NPY_BYTE = " << NPY_BYTE << std::endl; std::cout << "NPY_UBYTE = " << NPY_UBYTE << std::endl; std::cout << "NPY_INT8 = " << NPY_INT8 << std::endl; std::cout << "NPY_UINT8 = " << NPY_UINT8 << std::endl; std::cout << "NPY_SHORT = " << NPY_SHORT << std::endl; std::cout << "NPY_USHORT = " << NPY_USHORT << std::endl; std::cout << "NPY_INT16 = " << NPY_INT16 << std::endl; std::cout << "NPY_UINT16 = " << NPY_UINT16 << std::endl; std::cout << "NPY_INT = " << NPY_INT << std::endl; std::cout << "NPY_UINT = " << NPY_UINT << std::endl; std::cout << "NPY_INT32 = " << NPY_INT32 << std::endl; std::cout << "NPY_UINT32 = " << NPY_UINT32 << std::endl; std::cout << "NPY_LONG = " << NPY_LONG << std::endl; std::cout << "NPY_ULONG = " << NPY_ULONG << std::endl; std::cout << "NPY_LONGLONG = " << NPY_LONGLONG << std::endl; std::cout << "NPY_ULONGLONG = " << NPY_ULONGLONG << std::endl; std::cout << "NPY_INT64 = " << NPY_INT64 << std::endl; std::cout << "NPY_UINT64 = " << NPY_UINT64 << std::endl; } struct A { double a; int b; char c; std::array<double, 3> x; }; struct B { double a; int b; }; class C { public: using array_type = xt::xarray<double, xt::layout_type::row_major>; C() : m_array{0, 0, 0, 0} {} array_type & array() { return m_array; } private: array_type m_array; }; struct test_native_casters { using array_type = xt::xarray<double>; array_type a = xt::ones<double>({50, 50}); const auto & get_array() { return a; } auto get_strided_view() { return xt::strided_view(a, {xt::range(0, 1), xt::range(0, 3, 2)}); } auto get_array_adapter() { using shape_type = std::vector<size_t>; shape_type shape = {2, 2}; shape_type stride = {3, 2}; return xt::adapt(a.data(), 4, xt::no_ownership(), shape, stride); } auto get_tensor_adapter() { using shape_type = std::array<size_t, 2>; shape_type shape = {2, 2}; shape_type stride = {3, 2}; return xt::adapt(a.data(), 4, xt::no_ownership(), shape, stride); } auto get_owning_array_adapter() { size_t size = 100; int * data = new int[size]; std::fill(data, data + size, 1); using shape_type = std::vector<size_t>; shape_type shape = {size}; return xt::adapt(std::move(data), size, xt::acquire_ownership(), shape); } }; xt::pyarray<A> dtype_to_python() { A a1{123, 321, 'a', {1, 2, 3}}; A a2{111, 222, 'x', {5, 5, 5}}; return xt::pyarray<A>({a1, a2}); } xt::pyarray<B> dtype_from_python(xt::pyarray<B>& b) { if (b(0).a != 1 || b(0).b != 'p' || b(1).a != 123 || b(1).b != 'c') { throw std::runtime_error("FAIL"); } b(0).a = 123.; b(0).b = 'w'; return b; } void char_array(xt::pyarray<char[20]>& carr) { if (strcmp(carr(2), "python")) { throw std::runtime_error("TEST FAILED!"); } std::fill(&carr(2)[0], &carr(2)[0] + 20, 0); carr(2)[0] = 'c'; carr(2)[1] = '+'; carr(2)[2] = '+'; carr(2)[3] = '\0'; } void row_major_tensor(xt::pytensor<double, 3, xt::layout_type::row_major>& arg) { if (!std::is_same<decltype(arg.begin()), double*>::value) { throw std::runtime_error("TEST FAILED"); } } void col_major_array(xt::pyarray<double, xt::layout_type::column_major>& arg) { if (!std::is_same<decltype(arg.template begin<xt::layout_type::column_major>()), double*>::value) { throw std::runtime_error("TEST FAILED"); } } xt::pytensor<int, 0> xscalar(const xt::pytensor<int, 1>& arg) { return xt::sum(arg); } template <class T> using ndarray = xt::pyarray<T, xt::layout_type::row_major>; void test_rm(ndarray<int>const& x) { ndarray<int> y = x; ndarray<int> z = xt::zeros<int>({10}); } PYBIND11_MODULE(xtensor_python_test, m) { xt::import_numpy(); m.doc() = "Test module for xtensor python bindings"; m.def("example1", example1); m.def("example2", example2); m.def("example3_xarray", example3_xarray); m.def("example3_xarray_colmajor", example3_xarray_colmajor); m.def("example3_xtensor3", example3_xtensor3); m.def("example3_xtensor2", example3_xtensor2); m.def("example3_xtensor2_colmajor", example3_xtensor2_colmajor); m.def("example3_xfixed3", example3_xfixed3); m.def("example3_xfixed2", example3_xfixed2); m.def("example3_xfixed2_colmajor", example3_xfixed2_colmajor); m.def("complex_overload", no_complex_overload); m.def("complex_overload", complex_overload); m.def("complex_overload_reg", no_complex_overload_reg); m.def("complex_overload_reg", complex_overload_reg); m.def("readme_example1", readme_example1); m.def("readme_example2", xt::pyvectorize(readme_example2)); m.def("array_addition", array_addition); m.def("array_subtraction", array_subtraction); m.def("array_multiplication", array_multiplication); m.def("array_division", array_division); m.def("vectorize_example1", xt::pyvectorize(add)); m.def("rect_to_polar", xt::pyvectorize([](complex_t x) { return std::abs(x); })); m.def("compare_shapes", [](const xt::pyarray<double>& a, const xt::pyarray<double>& b) { return a.shape() == b.shape(); }); m.def("test_rm", test_rm); m.def("int_overload", int_overload<uint8_t>); m.def("int_overload", int_overload<int8_t>); m.def("int_overload", int_overload<uint16_t>); m.def("int_overload", int_overload<int16_t>); m.def("int_overload", int_overload<uint32_t>); m.def("int_overload", int_overload<int32_t>); m.def("int_overload", int_overload<uint64_t>); m.def("int_overload", int_overload<int64_t>); m.def("dump_numpy_constant", dump_numpy_constant); // Register additional dtypes PYBIND11_NUMPY_DTYPE(A, a, b, c, x); PYBIND11_NUMPY_DTYPE(B, a, b); m.def("dtype_to_python", dtype_to_python); m.def("dtype_from_python", dtype_from_python); m.def("char_array", char_array); m.def("col_major_array", col_major_array); m.def("row_major_tensor", row_major_tensor); m.def("xscalar", xscalar); py::class_<C>(m, "C") .def(py::init<>()) .def_property_readonly( "copy", [](C & self) { return self.array(); } ) .def_property_readonly( "ref", [](C & self) -> C::array_type & { return self.array(); } ) ; m.def("simple_array", [](xt::pyarray<int>) { return 1; } ); m.def("simple_tensor", [](xt::pytensor<int, 1>) { return 2; } ); m.def("diff_shape_overload", [](xt::pytensor<int, 1> a) { return 1; }); m.def("diff_shape_overload", [](xt::pytensor<int, 2> a) { return 2; }); py::class_<test_native_casters>(m, "test_native_casters") .def(py::init<>()) .def("get_array", &test_native_casters::get_array, py::return_value_policy::reference_internal) // memory managed by the class instance .def("get_strided_view", &test_native_casters::get_strided_view, py::keep_alive<0, 1>()) // keep_alive<0, 1>() => do not free "self" before the returned view .def("get_array_adapter", &test_native_casters::get_array_adapter, py::keep_alive<0, 1>()) // keep_alive<0, 1>() => do not free "self" before the returned adapter .def("get_tensor_adapter", &test_native_casters::get_tensor_adapter, py::keep_alive<0, 1>()) // keep_alive<0, 1>() => do not free "self" before the returned adapter .def("get_owning_array_adapter", &test_native_casters::get_owning_array_adapter) // auto memory management as the adapter owns its memory .def("view_keep_alive_member_function", [](test_native_casters & self, xt::pyarray<double> & a) // keep_alive<0, 2>() => do not free second parameter before the returned view {return xt::reshape_view(a, {a.size(), });}, py::keep_alive<0, 2>()); } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |