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);

}}}

コンストラクタ

ndarray view(dtype const &dt) const
戻り値

古い ndarray を与えられたデータ型でキャストした新しい ndarray。

ndarray astype(dtype const &dt) const
戻り値

古い ndarray を与えられたデータ型へ変換した新しい ndarray。

ndarray copy() const
戻り値

呼び出した ndarray のコピー。

ndarray transpose() const
戻り値

行と列を入れ替えた ndarray

ndarray squeeze() const
戻り値

大きさが 1 の次元を全て削除した ndarray

ndarray reshape(tuple const &shape) const
要件

ndarray の新しい shape はタプルとして与えなければならない

戻り値

データが同じで、与えられた shape に変形した ndarray

object scalarize() const
戻り値

ndarray の要素がただ 1 つの場合はそのスカラー。それ以外の場合は配列全体。

アクセッサ

int const shape(int n) const
戻り値

ndarray の n 次元目のサイズ。

int const strides(int n) const
戻り値

n 次元目の飛び幅。

char *get_data() const
戻り値

配列の生データ(char 型)のポインタ。

注意

char を返すため、飛び幅の算出が有効である。ユーザは reinterpret_cast を使わなければならない。

object get_base() const
戻り値

配列のデータを所有するオブジェクト、または配列が自身のデータを所有する場合は None

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
戻り値

配列のフラグ。

inline ndarray::bitflag operator|(ndarray::bitflag a, ndarray::bitflag b)
戻り値

ビットフラグの論理和(a | b)。

inline ndarray::bitflag operator&(ndarray::bitflag a, ndarray::bitflag b)
戻り値

ビットフラグの論理積(a & b)。

ndarray zeros(tuple const &shape, dtype const &dt)
ndarray zeros(int nd, Py_intptr_t const *shape, dtype const &dt)
要件

以下の引数を必ず与えなければならない。

  • shape か全次元の数(タプル)

  • データの dtype

  • 正方行列に対する nd サイズ

  • Py_intptr_t 型の shape

戻り値

与えた形状、データ型の ndarray。データは 0 で初期化される。

ndarray empty(tuple const &shape, dtype const &dt)
ndarray empty(int nd, Py_intptr_t const *shape, dtype const &dt)
要件

以下の引数を与えなければならない。

  • shape か全次元の数(タプル)

  • データの dtype

  • Py_intptr_t 型の shape

戻り値

与えた形状、データ型の新しい ndarray。データは未初期化のままとなる。

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)
要件

以下の引数を与えなければならない。

  • 汎用的な C++ データコンテナ data

  • データの dtype を表す dt

  • ndarray の shape(形状)を表す Python オブジェクト。

  • 各次元の飛び幅を表す Python オブジェクト strides

  • (所有者が ndarray 自身でない場合)データの所有者 owner

戻り値

与えられた属性とデータをもつ ndarray

注意

typename Containerstd::vector か Python のオブジェクト型のいずれかに変換可能でなければならない。

ndarray from_object(object const &obj, dtype const &dt, int nd_min, int nd_max, ndarray::bitflag flags = ndarray::NONE)
要件

以下の引数を与えなければならない。

  • ndarray に変換する Python オブジェクト obj

  • データの dtype を表す dt

  • ndarray の最小次元数を表す Python オブジェクト nd_min

  • ndarray の最大次元数を表す Python オブジェクト nd_max

  • 省略可能なビットフラグ flags

戻り値

引数で与えられた次元とデータで構築した ndarray

注意

typename Containerstd::vector か Python のオブジェクト型のいずれかに変換可能でなければならない。

inline ndarray from_object(object const &obj, dtype const &dt, int nd, ndarray::bitflag flags = ndarray::NONE)
要件

以下の引数を与えなければならない。

  • ndarray に変換する Python オブジェクト obj

  • データの dtype を表す dt

  • ndarray の次元数を表す Python オブジェクト nd

  • 省略可能なビットフラグ flags

戻り値

引数で与えた属性をもつ nd × nd 次元の ndarray

inline ndarray from_object(object const &obj, dtype const &dt, ndarray::bitflag flags = ndarray::NONE)
要件

以下の引数を与えなければならない。

  • ndarray に変換する Python オブジェクト obj

  • データの dtype を表す dt

  • 省略可能なビットフラグ flags

戻り値

与えた Python オブジェクトの ndarray

ndarray from_object(object const &obj, int nd_min, int nd_max, ndarray::bitflag flags = ndarray::NONE)
要件

以下の引数を与えなければならない。

  • ndarray に変換する Python オブジェクト obj

  • ndarray の最小次元数を表す Python オブジェクト nd_min

  • ndarray の最大次元数を表す Python オブジェクト nd_max

  • 省略可能なビットフラグ flags

戻り値

引数で与えられた次元数制限と属性をもつ ndarray

inline ndarray from_object(object const &obj, int nd, ndarray::bitflag flags = ndarray::NONE)
要件

以下の引数を与えなければならない。

  • ndarray に変換する Python オブジェクト obj

  • ndarray の次元数を表す Python オブジェクト nd

  • 省略可能なビットフラグ flags

戻り値

与えたオブジェクトから構築した nd × nd 次元の ndarray

inline ndarray from_object(object const &obj, ndarray::bitflag flags = ndarray::NONE)
要件

以下の引数を与えなければならない。

  • ndarray に変換する Python オブジェクト obj

  • 省略可能なビットフラグ flags

戻り値

与えた Python オブジェクトと同じ次元と dtype の ndarray

使用例

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());