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 | #include <boost/python/str.hpp> |
---|
11 | |
---|
12 | using namespace boost::python; |
---|
13 | |
---|
14 | #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580)) |
---|
15 | # define make_tuple boost::python::make_tuple |
---|
16 | #endif |
---|
17 | |
---|
18 | // See if we can invoke array() from C++ |
---|
19 | object new_array() |
---|
20 | { |
---|
21 | return numeric::array( |
---|
22 | make_tuple( |
---|
23 | make_tuple(1,2,3) |
---|
24 | , make_tuple(4,5,6) |
---|
25 | , make_tuple(7,8,9) |
---|
26 | ) |
---|
27 | ); |
---|
28 | } |
---|
29 | |
---|
30 | // test argument conversion |
---|
31 | void take_array(numeric::array /*x*/) |
---|
32 | { |
---|
33 | } |
---|
34 | |
---|
35 | // A separate function to invoke the info() member. Must happen |
---|
36 | // outside any doctests since this prints directly to stdout and the |
---|
37 | // result text includes the address of the 'self' array. |
---|
38 | void info(numeric::array const& z) |
---|
39 | { |
---|
40 | z.info(); |
---|
41 | } |
---|
42 | |
---|
43 | namespace |
---|
44 | { |
---|
45 | object handle_error() |
---|
46 | { |
---|
47 | PyObject* type, *value, *traceback; |
---|
48 | PyErr_Fetch(&type, &value, &traceback); |
---|
49 | handle<> ty(type), v(value), tr(traceback); |
---|
50 | return object("exception"); |
---|
51 | str format("exception type: %sn"); |
---|
52 | format += "exception value: %sn"; |
---|
53 | format += "traceback:n%s" ; |
---|
54 | object ret = format % boost::python::make_tuple(ty, v, tr); |
---|
55 | return ret; |
---|
56 | } |
---|
57 | } |
---|
58 | #define CHECK(expr) \ |
---|
59 | { \ |
---|
60 | object result; \ |
---|
61 | try { result = object(expr); } \ |
---|
62 | catch(error_already_set) \ |
---|
63 | { \ |
---|
64 | result = handle_error(); \ |
---|
65 | } \ |
---|
66 | check(result); \ |
---|
67 | } |
---|
68 | |
---|
69 | // Tests which work on both Numeric and numarray array objects. Of |
---|
70 | // course all of the operators "just work" since numeric::array |
---|
71 | // inherits that behavior from object. |
---|
72 | void exercise(numeric::array& y, object check) |
---|
73 | { |
---|
74 | y[make_tuple(2,1)] = 3; |
---|
75 | CHECK(y); |
---|
76 | CHECK(y.astype('D')); |
---|
77 | CHECK(y.copy()); |
---|
78 | CHECK(y.typecode()); |
---|
79 | } |
---|
80 | |
---|
81 | // numarray-specific tests. check is a callable object which we can |
---|
82 | // use to record intermediate results, which are later compared with |
---|
83 | // the results of corresponding python operations. |
---|
84 | void exercise_numarray(numeric::array& y, object check) |
---|
85 | { |
---|
86 | CHECK(str(y)); |
---|
87 | |
---|
88 | CHECK(y.argmax()); |
---|
89 | CHECK(y.argmax(0)); |
---|
90 | |
---|
91 | CHECK(y.argmin()); |
---|
92 | CHECK(y.argmin(0)); |
---|
93 | |
---|
94 | CHECK(y.argsort()); |
---|
95 | CHECK(y.argsort(1)); |
---|
96 | |
---|
97 | y.byteswap(); |
---|
98 | CHECK(y); |
---|
99 | |
---|
100 | CHECK(y.diagonal()); |
---|
101 | CHECK(y.diagonal(1)); |
---|
102 | CHECK(y.diagonal(0, 0)); |
---|
103 | CHECK(y.diagonal(0, 1, 0)); |
---|
104 | |
---|
105 | CHECK(y.is_c_array()); |
---|
106 | CHECK(y.isbyteswapped()); |
---|
107 | |
---|
108 | CHECK(y.trace()); |
---|
109 | CHECK(y.trace(1)); |
---|
110 | CHECK(y.trace(0, 0)); |
---|
111 | CHECK(y.trace(0, 1, 0)); |
---|
112 | |
---|
113 | CHECK(y.new_("D").getshape()); |
---|
114 | CHECK(y.new_("D").type()); |
---|
115 | y.sort(); |
---|
116 | CHECK(y); |
---|
117 | CHECK(y.type()); |
---|
118 | |
---|
119 | CHECK(y.factory(make_tuple(1.2, 3.4))); |
---|
120 | CHECK(y.factory(make_tuple(1.2, 3.4), "f8")); |
---|
121 | CHECK(y.factory(make_tuple(1.2, 3.4), "f8", true)); |
---|
122 | CHECK(y.factory(make_tuple(1.2, 3.4), "f8", true, false)); |
---|
123 | CHECK(y.factory(make_tuple(1.2, 3.4), "f8", true, false, object())); |
---|
124 | CHECK (y.factory(make_tuple(1.2, 3.4), "f8", true, false, object(), make_tuple(1,2,1))); |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | BOOST_PYTHON_MODULE(numpy_ext) |
---|
129 | { |
---|
130 | def("new_array", new_array); |
---|
131 | def("take_array", take_array); |
---|
132 | def("exercise", exercise); |
---|
133 | def("exercise_numarray", exercise_numarray); |
---|
134 | def("set_module_and_type", &numeric::array::set_module_and_type); |
---|
135 | def("get_module_name", &numeric::array::get_module_name); |
---|
136 | def("info", info); |
---|
137 | } |
---|
138 | |
---|
139 | #include "module_tail.cpp" |
---|