python如何调用c++numpy.ndarray代码_Boost::python实现C++和Python的混合编程(numpy)

#include #include

class ArrayHandler

{

public:

ArrayHandler();

boost::python::object Generate();

boost::python::object GeneratebyTuple(boost::python::object &data);

boost::python::object GeneratebyList(boost::python::object &data);

boost::python::object Reshape(boost::python::object &data);

void ShowData(boost::python::object &data);

void DataType(boost::python::object &data);

};

ArrayHandler::ArrayHandler()

{

//Py_Initialize();boost::python::numpy::initialize();

}

boost::python::object ArrayHandler::Generate()

{

boost::python::tuple shape = boost::python::make_tuple(4, 4);

boost::python::numpy::dtype type = boost::python::numpy::dtype::get_builtin();

boost::python::numpy::ndarray newArray = boost::python::numpy::zeros(shape, type);

//boost::python::numpy::ndarray newArray = boost::python::numpy::empty(shape, type);return newArray;

}

boost::python::object ArrayHandler::GeneratebyTuple(boost::python::object &data)

{

boost::python::tuple dataList = (boost::python::tuple)data;

boost::python::numpy::ndarray newArray = boost::python::numpy::array(dataList);

return newArray;

}

boost::python::object ArrayHandler::GeneratebyList(boost::python::object &data)

{

boost::python::list dataList = (boost::python::list)data;

boost::python::numpy::ndarray newArray = boost::python::numpy::array(dataList);

return newArray;

}

boost::python::object ArrayHandler::Reshape(boost::python::object &data)

{

boost::python::numpy::ndarray dataArray = boost::python::numpy::from_object(data);

for (int i = 0; i < dataArray.get_nd(); i++)

{

std::cout << "Size of Dim" << i + 1 << ": " << dataArray.get_shape()[i] << std::endl;

}

boost::python::tuple newShape = boost::python::make_tuple(2,2,2,2);

boost::python::numpy::ndarray newArray = dataArray.reshape(newShape);

return newArray;

}

void ArrayHandler::ShowData(boost::python::object &data)

{

std::cout << "Original Array:" << boost::python::extract(boost::python::str(data)) << std::endl;

boost::python::numpy::ndarray dataArray = boost::python::numpy::from_object(data);

data = dataArray.reshape(boost::python::make_tuple(16));

std::cout << "Reshaped Array:" << boost::python::extract(boost::python::str(data)) << std::endl;

}

void ArrayHandler::DataType(boost::python::object &data)

{

boost::python::numpy::ndarray dataArray = boost::python::numpy::from_object(data);

std::cout << "Datatype is " << boost::python::extract(boost::python::str(dataArray.get_dtype())) << std::endl;

}

BOOST_PYTHON_MODULE(boost_python_array)

{

boost::python::def("SetDictValue", SetDictValue);

boost::python::class_("ArrayHandler", boost::python::init<>())

.def("Generate", &ArrayHandler::Generate)

.def("GeneratebyTuple", &ArrayHandler::GeneratebyTuple)

.def("GeneratebyList", &ArrayHandler::GeneratebyList)

.def("ShowData", &ArrayHandler::ShowData)

.def("DataType", &ArrayHandler::DataType)

.def("Reshape", &ArrayHandler::Reshape);

}

//python>>> import boost_python_array

>>> np = boost_python_array.ArrayHandler()

>>> np.Generate()

array([[ 0., 0., 0., 0.],

[ 0., 0., 0., 0.],

[ 0., 0., 0., 0.],

[ 0., 0., 0., 0.]], dtype=float32)

>>> array = np.Generate() + 1

>>> array

array([[ 1., 1., 1., 1.],

[ 1., 1., 1., 1.],

[ 1., 1., 1., 1.],

[ 1., 1., 1., 1.]], dtype=float32)

>>> np.ShowData(array)

Original Array:[[ 1. 1. 1. 1.]

[ 1. 1. 1. 1.]

[ 1. 1. 1. 1.]

[ 1. 1. 1. 1.]]

Reshaped Array:[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

>>> np.DataType(array)

Datatype isfloat32

//python>>> import boost_python_array

>>> np = boost_python_array.ArrayHandler()

>>> list = [i for i in range(1, 11)]

>>> list

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> listArray = np.GeneratebyList(list)

>>> listArray

array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

//python>>> import boost_python_array

>>> np = boost_python_array.ArrayHandler()

>>> list = [i for i in range(1, 11)]

>>> list

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> tuple = ('1','2','3')

>>> tupleArray = np.GeneratebyTuple(tuple)

>>> tupleArray

array(['1', '2', '3'],

dtype='|S1')

//python>>> import boost_python_array

>>> import numpy

>>> arrNew = numpy.array([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],[13, 14, 15, 16]])

>>> arrNew.shape

(4L, 4L)

>>> arrReshaped = np.Reshape(arrNew)

Size of Dim1: 4

Size of Dim2: 4

>>> arrReshaped

array([[[[ 1, 2],

[ 3, 4]],

[[ 5, 6],

[ 7, 8]]],

[[[ 9, 10],

[11, 12]],

[[13, 14],

[15, 16]]]])

>>> arrReshaped.shape

(2L, 2L, 2L, 2L)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值