Skip to content

Commit 1787995

Browse files
committed
NumPy (Numeric and numarray) support
[SVN r15521]
1 parent 31a8be0 commit 1787995

File tree

12 files changed

+839
-12
lines changed

12 files changed

+839
-12
lines changed

Jamfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ if $(UNIX) && ( $(OS) = AIX )
1313

1414
dll bpl
1515
:
16+
src/numeric.cpp
17+
1618
src/list.cpp
1719
src/long.cpp
1820
src/dict.cpp

include/boost/python/dict.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class dict : public object
105105
BOOST_PYTHON_DECL list values() const;
106106

107107
public: // implementation detail -- for internal use only
108-
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dict)
108+
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dict, object)
109109

110110
private:
111111
static BOOST_PYTHON_DECL detail::new_reference call(object const&);

include/boost/python/list.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class list : public object
9494
}
9595

9696
public: // implementation detail -- for internal use only
97-
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(list)
97+
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(list, object)
9898

9999
private:
100100
static BOOST_PYTHON_DECL detail::new_non_null_reference call(object const&);

include/boost/python/long.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class long_ : public object
3131
{
3232
}
3333
public: // implementation detail -- for internal use only
34-
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(long_)
34+
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(long_, object)
3535

3636
private:
3737
static BOOST_PYTHON_DECL detail::new_non_null_reference call(object const&);

include/boost/python/numeric.hpp

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#ifndef NUMARRAY_DWA2002922_HPP
7+
# define NUMARRAY_DWA2002922_HPP
8+
9+
# include <boost/python/tuple.hpp>
10+
# include <boost/python/str.hpp>
11+
# include <boost/preprocessor/iteration/local.hpp>
12+
# include <boost/preprocessor/cat.hpp>
13+
# include <boost/preprocessor/repetition/enum_params.hpp>
14+
# include <boost/preprocessor/repetition/enum_binary_params.hpp>
15+
16+
namespace boost { namespace python { namespace numeric {
17+
18+
namespace aux
19+
{
20+
struct BOOST_PYTHON_DECL array_base : object
21+
{
22+
# define BOOST_PP_LOCAL_MACRO(n) \
23+
array_base(BOOST_PP_ENUM_PARAMS(n, object const& x));
24+
# define BOOST_PP_LOCAL_LIMITS (1, 7)
25+
# include BOOST_PP_LOCAL_ITERATE()
26+
27+
object argmax(long axis=-1);
28+
object argmin(long axis=-1);
29+
object argsort(long axis=-1);
30+
object astype(object const& type = object());
31+
void byteswap();
32+
object copy() const;
33+
object diagonal(long offset = 0, long axis1 = 0, long axis2 = 1) const;
34+
void info() const;
35+
bool is_c_array() const;
36+
bool isbyteswapped() const;
37+
object new_(object type) const;
38+
void sort();
39+
object trace(long offset = 0, long axis1 = 0, long axis2 = 1) const;
40+
object type() const;
41+
char typecode() const;
42+
43+
object factory(object const& buffer=object()
44+
, object const& type=object()
45+
, object const& shape=object()
46+
, bool copy = true
47+
, bool savespace = false
48+
, object typecode = object());
49+
50+
object getflat() const;
51+
long getrank() const;
52+
object getshape() const;
53+
bool isaligned() const;
54+
bool iscontiguous() const;
55+
long itemsize() const;
56+
long nelements() const;
57+
object nonzero() const;
58+
59+
void put(object const& indices, object const& values);
60+
61+
void ravel();
62+
63+
object repeat(object const& repeats, long axis=0);
64+
65+
void resize(object const& shape);
66+
67+
void setflat(object const& flat);
68+
void setshape(object const& shape);
69+
70+
void swapaxes(long axis1, long axis2);
71+
72+
object take(object const& sequence, long axis = 0) const;
73+
74+
void tofile(object const& file) const;
75+
76+
str tostring() const;
77+
78+
void transpose(object const& axes = object());
79+
80+
object view() const;
81+
82+
public: // implementation detail - do not touch.
83+
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array_base, object);
84+
};
85+
86+
struct BOOST_PYTHON_DECL array_object_manager_traits
87+
{
88+
static bool check(PyObject* obj);
89+
static detail::new_non_null_reference adopt(PyObject* obj);
90+
};
91+
} // namespace aux
92+
93+
class array : public aux::array_base
94+
{
95+
typedef aux::array_base base;
96+
public:
97+
98+
object astype() { return base::astype(); }
99+
100+
template <class Type>
101+
object astype(Type const& type_)
102+
{
103+
return base::astype(object(type_));
104+
}
105+
106+
template <class Type>
107+
object new_(Type const& type_) const
108+
{
109+
return base::new_(object(type_));
110+
}
111+
112+
template <class Sequence>
113+
void resize(Sequence const& x)
114+
{
115+
base::resize(object(x));
116+
}
117+
118+
# define BOOST_PP_LOCAL_MACRO(n) \
119+
void resize(BOOST_PP_ENUM_PARAMS(n, long x)) \
120+
{ \
121+
resize(make_tuple(BOOST_PP_ENUM_PARAMS(n, x))); \
122+
}
123+
# define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY)
124+
# include BOOST_PP_LOCAL_ITERATE()
125+
126+
template <class Sequence>
127+
void setshape(Sequence const& x)
128+
{
129+
base::setshape(object(x));
130+
}
131+
132+
# define BOOST_PP_LOCAL_MACRO(n) \
133+
void setshape(BOOST_PP_ENUM_PARAMS(n, long x)) \
134+
{ \
135+
setshape(make_tuple(BOOST_PP_ENUM_PARAMS(n, x))); \
136+
}
137+
# define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY)
138+
# include BOOST_PP_LOCAL_ITERATE()
139+
140+
template <class Indices, class Values>
141+
void put(Indices const& indices, Values const& values)
142+
{
143+
base::put(object(indices), object(values));
144+
}
145+
146+
template <class Sequence>
147+
object take(Sequence const& sequence, long axis = 0)
148+
{
149+
return base::take(object(sequence), axis);
150+
}
151+
152+
template <class File>
153+
void tofile(File const& f) const
154+
{
155+
base::tofile(object(f));
156+
}
157+
158+
object factory()
159+
{
160+
return base::factory();
161+
}
162+
163+
template <class Buffer>
164+
object factory(Buffer const& buffer)
165+
{
166+
return base::factory(object(buffer));
167+
}
168+
169+
template <class Buffer, class Type>
170+
object factory(
171+
Buffer const& buffer
172+
, Type const& type_)
173+
{
174+
return base::factory(object(buffer), object(type_));
175+
}
176+
177+
template <class Buffer, class Type, class Shape>
178+
object factory(
179+
Buffer const& buffer
180+
, Type const& type_
181+
, Shape const& shape
182+
, bool copy = true
183+
, bool savespace = false)
184+
{
185+
return base::factory(object(buffer), object(type_), object(shape), copy, savespace);
186+
}
187+
188+
template <class Buffer, class Type, class Shape>
189+
object factory(
190+
Buffer const& buffer
191+
, Type const& type_
192+
, Shape const& shape
193+
, bool copy
194+
, bool savespace
195+
, char typecode)
196+
{
197+
return base::factory(object(buffer), object(type_), object(shape), copy, savespace, object(typecode));
198+
}
199+
200+
# define BOOST_PYTHON_ENUM_AS_OBJECT(z, n, x) object(BOOST_PP_CAT(x,n))
201+
# define BOOST_PP_LOCAL_MACRO(n) \
202+
template <BOOST_PP_ENUM_PARAMS(n, class T)> \
203+
explicit array(BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& x)) \
204+
: array_base(BOOST_PP_ENUM(n, BOOST_PYTHON_ENUM_AS_OBJECT, x)) \
205+
{}
206+
# define BOOST_PP_LOCAL_LIMITS (1, 7)
207+
# include BOOST_PP_LOCAL_ITERATE()
208+
# undef BOOST_PYTHON_AS_OBJECT
209+
210+
static BOOST_PYTHON_DECL void set_module_and_type(char const* package_name = 0, char const* type_attribute_name = 0);
211+
212+
public: // implementation detail -- for internal use only
213+
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array, array_base);
214+
};
215+
216+
} // namespace boost::python::numeric
217+
218+
namespace converter
219+
{
220+
template <>
221+
struct object_manager_traits< numeric::array >
222+
: numeric::aux::array_object_manager_traits
223+
{
224+
BOOST_STATIC_CONSTANT(bool, is_specialized = true);
225+
};
226+
}
227+
228+
}} // namespace boost::python
229+
230+
#endif // NUMARRAY_DWA2002922_HPP

include/boost/python/object_core.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,13 @@ namespace api
234234
// Macros for forwarding constructors in classes derived from
235235
// object. Derived classes will usually want these as an
236236
// implementation detail
237-
# define BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS_(derived) \
237+
# define BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS_(derived, base) \
238238
inline explicit derived(python::detail::borrowed_reference p) \
239-
: object(p) {} \
239+
: base(p) {} \
240240
inline explicit derived(python::detail::new_reference p) \
241-
: object(p) {} \
241+
: base(p) {} \
242242
inline explicit derived(python::detail::new_non_null_reference p) \
243-
: object(p) {}
243+
: base(p) {}
244244

245245
# if !defined(BOOST_MSVC) || BOOST_MSVC > 1200
246246
# define BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS_
@@ -253,9 +253,9 @@ namespace api
253253
// runtime failure into an ambiguity error at compile-time due to
254254
// the lack of partial ordering, or at least a link-time error if no
255255
// generalized template constructor is declared.
256-
# define BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(derived) \
257-
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS_(derived) \
258-
template <class T> \
256+
# define BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(derived, base) \
257+
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS_(derived, base) \
258+
template <class T> \
259259
explicit derived(extract<T> const&);
260260
# endif
261261

include/boost/python/str.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ class str : public object
352352
BOOST_PYTHON_DECL str upper() const;
353353

354354
public: // implementation detail -- for internal use only
355-
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str)
355+
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(str, object)
356356

357357
private:
358358
static BOOST_PYTHON_DECL detail::new_reference call(object const&);

include/boost/python/tuple.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class tuple : public object
2424
}
2525

2626
public: // implementation detail -- for internal use only
27-
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(tuple)
27+
BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(tuple, object)
2828

2929
private:
3030
static BOOST_PYTHON_DECL detail::new_reference call(object const&);

0 commit comments

Comments
 (0)