You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Finally, we can print the array using the extract method in the python namespace.
27
27
Here, we first convert the variable into a string, and then extract it as a C++ character array from the python string using the <char const \* > template ::
As we can see, the changes are reflected across the ends. This happens because the from_data method passes the C++ array by reference to create the ndarray, and thus uses the same locations for storing data.
Copy file name to clipboardExpand all lines: doc/numpy/tutorial/ndarray.rst
+37-32Lines changed: 37 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,87 +8,92 @@ This tutorial will introduce you to some of the ways in which you can create nda
8
8
9
9
First, as before, initialise the necessary namepaces and runtimes ::
10
10
11
-
#include <boost/python/numpy.hpp>
12
-
#include <iostream>
11
+
#include <boost/python/numpy.hpp>
12
+
#include <iostream>
13
13
14
-
namespace p = boost::python;
15
-
namespace np = boost::python::numpy;
14
+
namespace p = boost::python;
15
+
namespace np = boost::python::numpy;
16
16
17
-
int main(int argc, char **argv)
18
-
{
19
-
Py_Initialize();
20
-
np::initialize();
17
+
int main(int argc, char **argv)
18
+
{
19
+
Py_Initialize();
20
+
np::initialize();
21
21
22
22
Let'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
23
24
-
p::object tu = p::make_tuple('a','b','c');
25
-
np::ndarray example_tuple = np::array(tu) ;
24
+
p::object tu = p::make_tuple('a','b','c');
25
+
np::ndarray example_tuple = np::array(tu);
26
26
27
27
Let'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
28
29
-
p::list l;
30
-
l.append('a');
31
-
np::ndarray example_list = np::array (l);
29
+
p::list l;
30
+
l.append('a');
31
+
np::ndarray example_list = np::array (l);
32
32
33
33
Optionally, we can also specify a dtype for the array ::
34
34
35
-
np::dtype dt = np::dtype::get_builtin<int>();
36
-
np::ndarray example_list1 = np::array (l,dt);
35
+
np::dtype dt = np::dtype::get_builtin<int>();
36
+
np::ndarray example_list1 = np::array (l,dt);
37
37
38
38
We can also create an array by supplying data arrays and a few other parameters.
39
39
40
40
First,create an integer array ::
41
41
42
-
int data[] = {1,2,3,4,5};
42
+
int data[] = {1,2,3,4,5};
43
43
44
44
Create a shape, and strides, needed by the function ::
45
45
46
-
p::tuple shape = p::make_tuple(5);
47
-
p::tuple stride = p::make_tuple(sizeof(int));
46
+
p::tuple shape = p::make_tuple(5);
47
+
p::tuple stride = p::make_tuple(sizeof(int));
48
48
49
49
Here, shape is (4,) , and the stride is `sizeof(int)``.
50
50
A stride is the number of bytes that must be traveled to get to the next desired element while constructing the ndarray.
51
51
52
52
The function also needs an owner, to keep track of the data array passed. Passing none is dangerous ::
53
53
54
-
p::object own;
54
+
p::object own;
55
55
56
56
The from_data function takes the data array, datatype,shape,stride and owner as arguments and returns an ndarray ::
Now lets first create and print out the ndarray as is.
81
82
Notice 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
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
93
-
94
-
}
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.
0 commit comments