]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright Jim Bosch & Ankit Daftery 2010-2012. |
2 | // Copyright Stefan Seefeld 2016. | |
3 | // Distributed under the Boost Software License, Version 1.0. | |
4 | // (See accompanying file LICENSE_1_0.txt or copy at | |
5 | // http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | #include <boost/python/numpy.hpp> | |
8 | ||
9 | namespace p = boost::python; | |
10 | namespace np = boost::python::numpy; | |
11 | ||
12 | np::ndarray zeros(p::tuple shape, np::dtype dt) { return np::zeros(shape, dt);} | |
13 | np::ndarray array2(p::object obj, np::dtype dt) { return np::array(obj,dt);} | |
14 | np::ndarray array1(p::object obj) { return np::array(obj);} | |
15 | np::ndarray empty1(p::tuple shape, np::dtype dt) { return np::empty(shape,dt);} | |
16 | ||
17 | np::ndarray c_empty(p::tuple shape, np::dtype dt) | |
18 | { | |
19 | // convert 'shape' to a C array so we can test the corresponding | |
20 | // version of the constructor | |
21 | unsigned len = p::len(shape); | |
22 | Py_intptr_t *c_shape = new Py_intptr_t[len]; | |
23 | for (unsigned i = 0; i != len; ++i) | |
24 | c_shape[i] = p::extract<Py_intptr_t>(shape[i]); | |
25 | np::ndarray result = np::empty(len, c_shape, dt); | |
26 | delete [] c_shape; | |
27 | return result; | |
28 | } | |
29 | ||
30 | np::ndarray transpose(np::ndarray arr) { return arr.transpose();} | |
31 | np::ndarray squeeze(np::ndarray arr) { return arr.squeeze();} | |
32 | np::ndarray reshape(np::ndarray arr,p::tuple tup) { return arr.reshape(tup);} | |
33 | ||
b32b8144 FG |
34 | Py_intptr_t shape_index(np::ndarray arr,int k) { return arr.shape(k); } |
35 | Py_intptr_t strides_index(np::ndarray arr,int k) { return arr.strides(k); } | |
36 | ||
7c673cae FG |
37 | BOOST_PYTHON_MODULE(ndarray_ext) |
38 | { | |
39 | np::initialize(); | |
40 | p::def("zeros", zeros); | |
41 | p::def("zeros_matrix", zeros, np::as_matrix<>()); | |
42 | p::def("array", array2); | |
43 | p::def("array", array1); | |
44 | p::def("empty", empty1); | |
45 | p::def("c_empty", c_empty); | |
46 | p::def("transpose", transpose); | |
47 | p::def("squeeze", squeeze); | |
48 | p::def("reshape", reshape); | |
b32b8144 FG |
49 | p::def("shape_index", shape_index); |
50 | p::def("strides_index", strides_index); | |
7c673cae | 51 | } |