Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/python/src/numeric.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 6.8 KB
RevLine 
[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/handle.hpp>
8#include <boost/python/cast.hpp>
9#include <boost/python/tuple.hpp>
10#include <boost/python/detail/raw_pyobject.hpp>
11#include <boost/python/extract.hpp>
12
13namespace boost { namespace python { namespace numeric {
14
15namespace
16{
17  enum state_t { failed = -1, unknown, succeeded };
18  state_t state = unknown;
19  std::string module_name;
20  std::string type_name;
21
22  handle<> array_module;
23  handle<> array_type;
24  handle<> array_function;
25
26  void throw_load_failure()
27  {
28      PyErr_Format(
29          PyExc_ImportError
30          , "No module named '%s' or its type '%s' did not follow the NumPy protocol"
31          , module_name.c_str(), type_name.c_str());
32      throw_error_already_set();
33     
34  }
35
36  bool load(bool throw_on_error)
37  {
38      if (!state)
39      {
40          if (module_name.size() == 0)
41          {
42              module_name = "numarray";
43              type_name = "NDArray";
44              if (load(false))
45                  return true;
46              module_name = "Numeric";
47              type_name = "ArrayType";
48          }
49
50          state = failed;
51          PyObject* module = ::PyImport_Import(object(module_name).ptr());
52          if (module)
53          {
54              PyObject* type = ::PyObject_GetAttrString(module, const_cast<char*>(type_name.c_str()));
55
56              if (type && PyType_Check(type))
57              {
58                  array_type = handle<>(type);
59                  PyObject* function = ::PyObject_GetAttrString(module, const_cast<char*>("array"));
60                 
61                  if (function && PyCallable_Check(function))
62                  {
63                      array_function = handle<>(function);
64                      state = succeeded;
65                  }
66              }
67          }
68      }
69     
70      if (state == succeeded)
71          return true;
72     
73      if (throw_on_error)
74          throw_load_failure();
75     
76      PyErr_Clear();
77      return false;
78  }
79
80  object demand_array_function()
81  {
82      load(true);
83      return object(array_function);
84  }
85}
86
87void array::set_module_and_type(char const* package_name, char const* type_attribute_name)
88{
89    state = unknown;
90    module_name = package_name ? package_name : "" ;
91    type_name = type_attribute_name ? type_attribute_name : "" ;
92}
93 
94
95namespace aux
96{
97  bool array_object_manager_traits::check(PyObject* obj)
98  {
99      if (!load(false))
100          return false;
101      return ::PyObject_IsInstance(obj, array_type.get());
102  }
103
104  python::detail::new_non_null_reference
105  array_object_manager_traits::adopt(PyObject* obj)
106  {
107      load(true);
108      return detail::new_non_null_reference(
109          pytype_check(downcast<PyTypeObject>(array_type.get()), obj));
110  }
111
112
113# define BOOST_PYTHON_AS_OBJECT(z, n, _) object(x##n)
114# define BOOST_PP_LOCAL_MACRO(n)                                        \
115    array_base::array_base(BOOST_PP_ENUM_PARAMS(n, object const& x))    \
116        : object(demand_array_function()(BOOST_PP_ENUM_PARAMS(n, x)))   \
117    {}
118# define BOOST_PP_LOCAL_LIMITS (1, 6)
119# include BOOST_PP_LOCAL_ITERATE()
120# undef BOOST_PYTHON_AS_OBJECT
121
122    array_base::array_base(BOOST_PP_ENUM_PARAMS(7, object const& x))
123        : object(demand_array_function()(BOOST_PP_ENUM_PARAMS(7, x)))
124    {}
125
126  object array_base::argmax(long axis)
127  {
128      return attr("argmax")(axis);
129  }
130 
131  object array_base::argmin(long axis)
132  {
133      return attr("argmin")(axis);
134  }
135 
136  object array_base::argsort(long axis)
137  {
138      return attr("argsort")(axis);
139  }
140 
141  object array_base::astype(object const& type)
142  {
143      return attr("astype")(type);
144  }
145 
146  void array_base::byteswap()
147  {
148      attr("byteswap")();
149  }
150 
151  object array_base::copy() const
152  {
153      return attr("copy")();
154  }
155 
156  object array_base::diagonal(long offset, long axis1, long axis2) const
157  {
158      return attr("diagonal")(offset, axis1, axis2);
159  }
160 
161  void array_base::info() const
162  {
163      attr("info")();
164  }
165 
166  bool array_base::is_c_array() const
167  {
168      return extract<bool>(attr("is_c_array")());
169  }
170 
171  bool array_base::isbyteswapped() const
172  {
173      return extract<bool>(attr("isbyteswapped")());
174  }
175 
176  object array_base::new_(object type) const
177  {
178      return attr("new")(type);
179  }
180 
181  void array_base::sort()
182  {
183      attr("sort")();
184  }
185 
186  object array_base::trace(long offset, long axis1, long axis2) const
187  {
188      return attr("trace")(offset, axis1, axis2);
189  }
190 
191  object array_base::type() const
192  {
193      return attr("type")();
194  }
195 
196  char array_base::typecode() const
197  {
198      return extract<char>(attr("typecode")());
199  }
200 
201  object array_base::factory(object const& buffer
202        , object const& type
203        , object const& shape
204        , bool copy
205        , bool savespace
206        , object typecode)
207  {
208      return attr("array")(buffer, type, shape, copy, savespace, typecode);
209  }
210
211  object array_base::getflat() const
212  {
213      return attr("getflat")();
214  }
215     
216  long array_base::getrank() const
217  {
218      return extract<long>(attr("getrank")());
219  }
220 
221  object array_base::getshape() const
222  {
223      return attr("getshape")();
224  }
225 
226  bool array_base::isaligned() const
227  {
228      return extract<bool>(attr("isaligned")());
229  }
230 
231  bool array_base::iscontiguous() const
232  {     
233      return extract<bool>(attr("iscontiguous")());
234  }
235 
236  long array_base::itemsize() const
237  {
238      return extract<long>(attr("itemsize")());
239  }
240 
241  long array_base::nelements() const
242  {
243      return extract<long>(attr("nelements")());
244  }
245 
246  object array_base::nonzero() const
247  {
248      return attr("nonzero")();
249  }
250   
251  void array_base::put(object const& indices, object const& values)
252  {
253      attr("put")(indices, values);
254  }
255   
256  void array_base::ravel()
257  {
258      attr("ravel")();
259  }
260   
261  object array_base::repeat(object const& repeats, long axis)
262  {
263      return attr("repeat")(repeats, axis);
264  }
265   
266  void array_base::resize(object const& shape)
267  {
268      attr("resize")(shape);
269  }
270     
271  void array_base::setflat(object const& flat)
272  {
273      attr("setflat")(flat);
274  }
275 
276  void array_base::setshape(object const& shape)
277  {
278      attr("setshape")(shape);
279  }
280   
281  void array_base::swapaxes(long axis1, long axis2)
282  {
283      attr("swapaxes")(axis1, axis2);
284  }
285   
286  object array_base::take(object const& sequence, long axis) const
287  {
288      return attr("take")(sequence, axis);
289  }
290   
291  void array_base::tofile(object const& file) const
292  {
293      attr("tofile")(file);
294  }
295   
296  str array_base::tostring() const
297  {
298      return str(attr("tostring")());
299  }
300   
301  void array_base::transpose(object const& axes)
302  {
303      attr("transpose")(axes);
304  }
305   
306  object array_base::view() const
307  {
308      return attr("view")();
309  }
310}
311
312}}} // namespace boost::python::numeric
Note: See TracBrowser for help on using the repository browser.