[12] | 1 | // Copyright David Abrahams 2002. |
---|
| 2 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
| 3 | // accompanying file LICENSE_1_0.txt or copy at |
---|
| 4 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
| 5 | |
---|
| 6 | #include <boost/python/numeric.hpp> |
---|
| 7 | #include <boost/python/tuple.hpp> |
---|
| 8 | #include <boost/python/module.hpp> |
---|
| 9 | #include <boost/python/def.hpp> |
---|
| 10 | |
---|
| 11 | using namespace boost::python; |
---|
| 12 | |
---|
| 13 | // See if we can invoke array() from C++ |
---|
| 14 | object new_array() |
---|
| 15 | { |
---|
| 16 | return numeric::array( |
---|
| 17 | make_tuple( |
---|
| 18 | make_tuple(1,2,3) |
---|
| 19 | , make_tuple(4,5,6) |
---|
| 20 | , make_tuple(7,8,9) |
---|
| 21 | ) |
---|
| 22 | ); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | // test argument conversion |
---|
| 26 | void take_array(numeric::array x) |
---|
| 27 | { |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | // A separate function to invoke the info() member. Must happen |
---|
| 31 | // outside any doctests since this prints directly to stdout and the |
---|
| 32 | // result text includes the address of the 'self' array. |
---|
| 33 | void info(numeric::array const& z) |
---|
| 34 | { |
---|
| 35 | z.info(); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | // Tests which work on both Numeric and numarray array objects. Of |
---|
| 39 | // course all of the operators "just work" since numeric::array |
---|
| 40 | // inherits that behavior from object. |
---|
| 41 | void exercise(numeric::array& y, object check) |
---|
| 42 | { |
---|
| 43 | y[make_tuple(2,1)] = 3; |
---|
| 44 | check(y); |
---|
| 45 | check(y.astype('D')); |
---|
| 46 | check(y.copy()); |
---|
| 47 | check(y.typecode()); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | // numarray-specific tests. check is a callable object which we can |
---|
| 51 | // use to record intermediate results, which are later compared with |
---|
| 52 | // the results of corresponding python operations. |
---|
| 53 | void exercise_numarray(numeric::array& y, object check) |
---|
| 54 | { |
---|
| 55 | check(y.astype()); |
---|
| 56 | |
---|
| 57 | check(y.argmax()); |
---|
| 58 | check(y.argmax(0)); |
---|
| 59 | |
---|
| 60 | check(y.argmin()); |
---|
| 61 | check(y.argmin(0)); |
---|
| 62 | |
---|
| 63 | check(y.argsort()); |
---|
| 64 | check(y.argsort(1)); |
---|
| 65 | |
---|
| 66 | y.byteswap(); |
---|
| 67 | check(y); |
---|
| 68 | |
---|
| 69 | check(y.diagonal()); |
---|
| 70 | check(y.diagonal(1)); |
---|
| 71 | check(y.diagonal(0, 1)); |
---|
| 72 | check(y.diagonal(0, 1, 0)); |
---|
| 73 | |
---|
| 74 | check(y.is_c_array()); |
---|
| 75 | check(y.isbyteswapped()); |
---|
| 76 | |
---|
| 77 | check(y.trace()); |
---|
| 78 | check(y.trace(1)); |
---|
| 79 | check(y.trace(0, 1)); |
---|
| 80 | check(y.trace(0, 1, 0)); |
---|
| 81 | |
---|
| 82 | check(y.new_('D')); |
---|
| 83 | y.sort(); |
---|
| 84 | check(y); |
---|
| 85 | check(y.type()); |
---|
| 86 | |
---|
| 87 | check(y.factory(make_tuple(1.2, 3.4))); |
---|
| 88 | check(y.factory(make_tuple(1.2, 3.4), "Double")); |
---|
| 89 | check(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(1,2,1))); |
---|
| 90 | check(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(2,1,1), false)); |
---|
| 91 | check(y.factory(make_tuple(1.2, 3.4), "Double", make_tuple(2), true, true)); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | BOOST_PYTHON_MODULE(numpy_ext) |
---|
| 95 | { |
---|
| 96 | def("new_array", new_array); |
---|
| 97 | def("take_array", take_array); |
---|
| 98 | def("exercise", exercise); |
---|
| 99 | def("exercise_numarray", exercise_numarray); |
---|
| 100 | def("set_module_and_type", &numeric::array::set_module_and_type); |
---|
| 101 | def("info", info); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | #include "module_tail.cpp" |
---|