ndarray
-
class ndarray : public boost::python::object
ndarray は同一の型とサイズをもつ要素の N 次元配列である。N は次元数であり、タプル
shape
の形式で指定する。内容のオブジェクトについて numpy の dtype を指定することもできる。<boost/python/numpy/ndarray.hpp>
は、生のポインタを C++ と Python 間で移動し、データから ndarray を作成するのに必要な構造体とメソッドを含む。
概要
namespace boost
{
namespace python
{
namespace numpy
{
class ndarray : public object
{
public:
enum bitflag
{
NONE=0x0, C_CONTIGUOUS=0x1, F_CONTIGUOUS=0x2, V_CONTIGUOUS=0x1|0x2,
ALIGNED=0x4, WRITEABLE=0x8, BEHAVED=0x4|0x8,
CARRAY_RO=0x1|0x4, CARRAY=0x1|0x4|0x8, CARRAY_MIS=0x1|0x8,
FARRAY_RO=0x2|0x4, FARRAY=0x2|0x4|0x8, FARRAY_MIS=0x2|0x8,
UPDATE_ALL=0x1|0x2|0x4, VARRAY=0x1|0x2|0x8, ALL=0x1|0x2|0x4|0x8
};
ndarray view(dtype const & dt) const;
ndarray astype(dtype const & dt) const;
ndarray copy() const;
int const shape(int n) const;
int const strides(int n) const;
char * get_data() const;
dtype get_dtype() const;
python::object get_base() const;
void set_base(object const & base);
Py_intptr_t const * get_shape() const;
Py_intptr_t const * get_strides() const;
int const get_nd() const;
bitflag const get_flags() const;
ndarray transpose() const;
ndarray squeeze() const;
ndarray reshape(tuple const & shape) const;
object scalarize() const;
};
ndarray zeros(tuple const & shape, dtype const & dt);
ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt);
ndarray empty(tuple const & shape, dtype const & dt);
ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt);
ndarray array(object const & obj);
ndarray array(object const & obj, dtype const & dt);
template <typename Container>
ndarray from_data(void * data,dtype const & dt,Container shape,Container strides,python::object const & owner);
template <typename Container>
ndarray from_data(void const * data, dtype const & dt, Container shape, Container strides, object const & owner);
ndarray from_object(object const & obj, dtype const & dt,int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE);
ndarray from_object(object const & obj, dtype const & dt,int nd, ndarray::bitflag flags=ndarray::NONE);
ndarray from_object(object const & obj, dtype const & dt, ndarray::bitflag flags=ndarray::NONE);
ndarray from_object(object const & obj, int nd_min, int nd_max,ndarray::bitflag flags=ndarray::NONE);
ndarray from_object(object const & obj, int nd, ndarray::bitflag flags=ndarray::NONE);
ndarray from_object(object const & obj, ndarray::bitflag flags=ndarray::NONE)
ndarray::bitflag operator|(ndarray::bitflag a, ndarray::bitflag b) ;
ndarray::bitflag operator&(ndarray::bitflag a, ndarray::bitflag b);
}}}
コンストラクタ
アクセッサ
-
char *get_data() const
- 戻り値
配列の生データ(
char
型)のポインタ。- 注意
char
を返すため、飛び幅の算出が有効である。ユーザは reinterpret_cast を使わなければならない。
-
Py_intptr_t const *get_shape() const
- 戻り値
配列の形状を表す整数配列。
-
Py_intptr_t const *get_strides() const
- 戻り値
配列の飛び幅を表す整数配列。
-
int const get_nd() const
- 戻り値
配列の次元数。
-
bitflag const get_flags() const
- 戻り値
配列のフラグ。
-
ndarray zeros(tuple const &shape, dtype const &dt)
-
ndarray zeros(int nd, Py_intptr_t const *shape, dtype const &dt)
-
ndarray empty(tuple const &shape, dtype const &dt)
-
ndarray empty(int nd, Py_intptr_t const *shape, dtype const &dt)
-
ndarray array(object const &obj)
-
ndarray array(object const &obj, dtype const &dt)
- 戻り値
任意の Python シーケンスから得た新しい
ndarray
。各要素の型はオプション引数で指定した dtype となる。
-
template<typename Container>
inline ndarray from_data(void *data, dtype const &dt, Container shape, Container strides, python::object const &owner)
-
ndarray from_object(object const &obj, dtype const &dt, int nd_min, int nd_max, ndarray::bitflag flags = ndarray::NONE)
-
inline ndarray from_object(object const &obj, dtype const &dt, int nd, ndarray::bitflag flags = ndarray::NONE)
-
inline ndarray from_object(object const &obj, dtype const &dt, ndarray::bitflag flags = ndarray::NONE)
-
ndarray from_object(object const &obj, int nd_min, int nd_max, ndarray::bitflag flags = ndarray::NONE)
使用例
namespace p = boost::python;
namespace np = boost::python::numpy;
p::object tu = p::make_tuple('a','b','c') ;
np::ndarray example_tuple = np::array (tu) ;
p::list l ;
np::ndarray example_list = np::array (l) ;
np::dtype dt = np::dtype::get_builtin<int>();
np::ndarray example_list1 = np::array (l,dt);
int data[] = {1,2,3,4} ;
p::tuple shape = p::make_tuple(4) ;
p::tuple stride = p::make_tuple(4) ;
p::object own ;
np::ndarray data_ex = np::from_data(data,dt,shape,stride,own);
uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}};
shape = p::make_tuple(3,2) ;
stride = p::make_tuple(4,2) ;
np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
np::ndarray mul_data_ex = np::from_data(mul_data,dt1, p::make_tuple(3,4),p::make_tuple(4,1),p::object());
mul_data_ex = np::from_data(mul_data,dt1, shape,stride,p::object());