summaryrefslogtreecommitdiff
path: root/libs/python/src/numeric.cpp
blob: c8a5f071d92404c6f7267e06fbec14c6d360a702 (plain)
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
// Copyright David Abrahams 2002.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <boost/python/numeric.hpp>
#include <boost/python/handle.hpp>
#include <boost/python/cast.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/detail/raw_pyobject.hpp>
#include <boost/python/extract.hpp>

namespace boost { namespace python { namespace numeric {

namespace
{
  enum state_t { failed = -1, unknown, succeeded };
  state_t state = unknown;
  std::string module_name;
  std::string type_name;

  handle<> array_module;
  handle<> array_type;
  handle<> array_function;

  void throw_load_failure()
  {
      PyErr_Format(
          PyExc_ImportError
          , "No module named '%s' or its type '%s' did not follow the NumPy protocol"
          , module_name.c_str(), type_name.c_str());
      throw_error_already_set();
      
  }

  bool load(bool throw_on_error)
  {
      if (!state)
      {
          if (module_name.size() == 0)
          {
              module_name = "numarray";
              type_name = "NDArray";
              if (load(false))
                  return true;
              module_name = "Numeric";
              type_name = "ArrayType";
          }

          state = failed;
          PyObject* module = ::PyImport_Import(object(module_name).ptr());
          if (module)
          {
              PyObject* type = ::PyObject_GetAttrString(module, const_cast<char*>(type_name.c_str()));

              if (type && PyType_Check(type))
              {
                  array_type = handle<>(type);
                  PyObject* function = ::PyObject_GetAttrString(module, const_cast<char*>("array"));
                  
                  if (function && PyCallable_Check(function))
                  {
                      array_function = handle<>(function);
                      state = succeeded;
                  }
              }
          }
      }
      
      if (state == succeeded)
          return true;
      
      if (throw_on_error)
          throw_load_failure();
      
      PyErr_Clear();
      return false;
  }

  object demand_array_function()
  {
      load(true);
      return object(array_function);
  }
}

void array::set_module_and_type(char const* package_name, char const* type_attribute_name)
{
    state = unknown;
    module_name = package_name ? package_name : "" ;
    type_name = type_attribute_name ? type_attribute_name : "" ;
}

std::string array::get_module_name()
{
    load(false);
    return module_name;
}

namespace aux
{
  bool array_object_manager_traits::check(PyObject* obj)
  {
      if (!load(false))
          return false;
      return ::PyObject_IsInstance(obj, array_type.get());
  }

  python::detail::new_non_null_reference
  array_object_manager_traits::adopt(PyObject* obj)
  {
      load(true);
      return detail::new_non_null_reference(
          pytype_check(downcast<PyTypeObject>(array_type.get()), obj));
  }

  PyTypeObject const* array_object_manager_traits::get_pytype()
  {
      load(false);
      if(!array_type) return 0;
      return downcast<PyTypeObject>(array_type.get());
  }

# define BOOST_PYTHON_AS_OBJECT(z, n, _) object(x##n)
# define BOOST_PP_LOCAL_MACRO(n)                                        \
    array_base::array_base(BOOST_PP_ENUM_PARAMS(n, object const& x))    \
        : object(demand_array_function()(BOOST_PP_ENUM_PARAMS(n, x)))   \
    {}
# define BOOST_PP_LOCAL_LIMITS (1, 6)
# include BOOST_PP_LOCAL_ITERATE()
# undef BOOST_PYTHON_AS_OBJECT

    array_base::array_base(BOOST_PP_ENUM_PARAMS(7, object const& x))
        : object(demand_array_function()(BOOST_PP_ENUM_PARAMS(7, x)))
    {}

  object array_base::argmax(long axis)
  {
      return attr("argmax")(axis);
  }
  
  object array_base::argmin(long axis)
  {
      return attr("argmin")(axis);
  }
  
  object array_base::argsort(long axis)
  {
      return attr("argsort")(axis);
  }
  
  object array_base::astype(object const& type)
  {
      return attr("astype")(type);
  }
  
  void array_base::byteswap()
  {
      attr("byteswap")();
  }
  
  object array_base::copy() const
  {
      return attr("copy")();
  }
  
  object array_base::diagonal(long offset, long axis1, long axis2) const
  {
      return attr("diagonal")(offset, axis1, axis2);
  }
  
  void array_base::info() const
  {
      attr("info")();
  }
  
  bool array_base::is_c_array() const
  {
      return extract<bool>(attr("is_c_array")());
  }
  
  bool array_base::isbyteswapped() const
  {
      return extract<bool>(attr("isbyteswapped")());
  }
  
  array array_base::new_(object type) const
  {
      return extract<array>(attr("new")(type))();
  }
  
  void array_base::sort()
  {
      attr("sort")();
  }
  
  object array_base::trace(long offset, long axis1, long axis2) const
  {
      return attr("trace")(offset, axis1, axis2);
  }
  
  object array_base::type() const
  {
      return attr("type")();
  }
  
  char array_base::typecode() const
  {
      return extract<char>(attr("typecode")());
  }

  object array_base::factory(
          object const& sequence
        , object const& typecode
        , bool copy
        , bool savespace
        , object type
        , object shape
  )
  {
      return attr("factory")(sequence, typecode, copy, savespace, type, shape);
  }

  object array_base::getflat() const
  {
      return attr("getflat")();
  }
      
  long array_base::getrank() const
  {
      return extract<long>(attr("getrank")());
  }
  
  object array_base::getshape() const
  {
      return attr("getshape")();
  }
  
  bool array_base::isaligned() const
  {
      return extract<bool>(attr("isaligned")());
  }
  
  bool array_base::iscontiguous() const
  {      
      return extract<bool>(attr("iscontiguous")());
  }
  
  long array_base::itemsize() const
  {
      return extract<long>(attr("itemsize")());
  }
  
  long array_base::nelements() const
  {
      return extract<long>(attr("nelements")());
  }
  
  object array_base::nonzero() const
  {
      return attr("nonzero")();
  }
   
  void array_base::put(object const& indices, object const& values)
  {
      attr("put")(indices, values);
  }
   
  void array_base::ravel()
  {
      attr("ravel")();
  }
   
  object array_base::repeat(object const& repeats, long axis)
  {
      return attr("repeat")(repeats, axis);
  }
   
  void array_base::resize(object const& shape)
  {
      attr("resize")(shape);
  }
      
  void array_base::setflat(object const& flat)
  {
      attr("setflat")(flat);
  }
  
  void array_base::setshape(object const& shape)
  {
      attr("setshape")(shape);
  }
   
  void array_base::swapaxes(long axis1, long axis2)
  {
      attr("swapaxes")(axis1, axis2);
  }
   
  object array_base::take(object const& sequence, long axis) const
  {
      return attr("take")(sequence, axis);
  }
   
  void array_base::tofile(object const& file) const
  {
      attr("tofile")(file);
  }
   
  str array_base::tostring() const
  {
      return str(attr("tostring")());
  }
   
  void array_base::transpose(object const& axes)
  {
      attr("transpose")(axes);
  }
   
  object array_base::view() const
  {
      return attr("view")();
  }
}

}}} // namespace boost::python::numeric