]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/python/doc/numpy/tutorial/ndarray.rst
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / python / doc / numpy / tutorial / ndarray.rst
CommitLineData
7c673cae
FG
1Creating ndarrays
2=================
3
4The Boost.Numpy library exposes quite a few methods to create ndarrays. ndarrays can be created in a variety of ways, include empty arrays and zero filled arrays.
5ndarrays can also be created from arbitrary python sequences as well as from data and dtypes.
6
7This tutorial will introduce you to some of the ways in which you can create ndarrays. The methods covered here include creating ndarrays from arbitrary Python sequences, as well as from C++ containers, using both unit and non-unit strides
8
9First, as before, initialise the necessary namepaces and runtimes ::
10
11 #include <boost/python/numpy.hpp>
12 #include <iostream>
13
14 namespace p = boost::python;
15 namespace np = boost::python::numpy;
16
17 int main(int argc, char **argv)
18 {
19 Py_Initialize();
20 np::initialize();
21
22Let's now create an ndarray from a simple tuple. We first create a tuple object, and then pass it to the array method, to generate the necessary tuple ::
23
24 p::object tu = p::make_tuple('a','b','c');
25 np::ndarray example_tuple = np::array(tu);
26
27Let's now try the same with a list. We create an empty list, add an element using the append method, and as before, call the array method ::
28
29 p::list l;
30 l.append('a');
31 np::ndarray example_list = np::array (l);
32
33Optionally, we can also specify a dtype for the array ::
34
35 np::dtype dt = np::dtype::get_builtin<int>();
36 np::ndarray example_list1 = np::array (l,dt);
37
38We can also create an array by supplying data arrays and a few other parameters.
39
40First,create an integer array ::
41
42 int data[] = {1,2,3,4,5};
43
44Create a shape, and strides, needed by the function ::
45
46 p::tuple shape = p::make_tuple(5);
47 p::tuple stride = p::make_tuple(sizeof(int));
48
49Here, shape is (4,) , and the stride is `sizeof(int)``.
50A stride is the number of bytes that must be traveled to get to the next desired element while constructing the ndarray.
51
52The function also needs an owner, to keep track of the data array passed. Passing none is dangerous ::
53
54 p::object own;
55
56The from_data function takes the data array, datatype,shape,stride and owner as arguments and returns an ndarray ::
57
58 np::ndarray data_ex1 = np::from_data(data,dt, shape,stride,own);
59
60Now let's print the ndarray we created ::
61
62 std::cout << "Single dimensional array ::" << std::endl
63 << p::extract<char const *>(p::str(data_ex)) << std::endl;
64
65Let's make it a little more interesting. Lets make an 3x2 ndarray from a multi-dimensional array using non-unit strides
66
67First lets create a 3x4 array of 8-bit integers ::
68
69 uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}};
70
71Now let's create an array of 3x2 elements, picking the first and third elements from each row . For that, the shape will be 3x2.
72The strides will be 4x2 i.e. 4 bytes to go to the next desired row, and 2 bytes to go to the next desired column ::
73
74 shape = p::make_tuple(3,2);
75 stride = p::make_tuple(sizeof(uint8_t)*2,sizeof(uint8_t));
76
77Get the numpy dtype for the built-in 8-bit integer data type ::
78
79 np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
80
81Now lets first create and print out the ndarray as is.
82Notice how we can pass the shape and strides in the function directly, as well as the owner. The last part can be done because we don't have any use to
83manipulate the "owner" object ::
84
85 np::ndarray mul_data_ex = np::from_data(mul_data, dt1,
86 p::make_tuple(3,4),
87 p::make_tuple(4,1),
88 p::object());
89 std::cout << "Original multi dimensional array :: " << std::endl
90 << p::extract<char const *>(p::str(mul_data_ex)) << std::endl;
91
92Now create the new ndarray using the shape and strides and print out the array we created using non-unit strides ::
93
94 mul_data_ex = np::from_data(mul_data, dt1, shape, stride, p::object());
95 std::cout << "Selective multidimensional array :: "<<std::endl
96 << p::extract<char const *>(p::str(mul_data_ex)) << std::endl ;
97 }
98
99.. note:: The from_data method will throw ``error_already_set`` if the number of elements dictated by the shape and the corresponding strides don't match.