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