mirror of
https://github.com/boostorg/python.git
synced 2026-01-19 04:22:16 +00:00
Fix some confusing magic numbers in docs for stride (#50).
Documentation here needs a bigger cleanup than I can give it right now, but this at least removes the ambiguity as to whether "4" means the shape or sizeof(int).
This commit is contained in:
@@ -2,7 +2,7 @@ How to access data using raw pointers
|
||||
=====================================
|
||||
|
||||
One of the advantages of the ndarray wrapper is that the same data can be used in both Python and C++ and changes can be made to reflect at both ends.
|
||||
The from_data method makes this possible.
|
||||
The from_data method makes this possible.
|
||||
|
||||
Like before, first get the necessary headers, setup the namespaces and initialize the Python runtime and numpy module::
|
||||
|
||||
@@ -19,8 +19,8 @@ Like before, first get the necessary headers, setup the namespaces and initializ
|
||||
|
||||
Create an array in C++ , and pass the pointer to it to the from_data method to create an ndarray::
|
||||
|
||||
int arr[] = {1,2,3,4} ;
|
||||
np::ndarray py_array = np::from_data(arr, np::dtype::get_builtin<int>() , p::make_tuple(4), p::make_tuple(4), p::object());
|
||||
int arr[] = {1,2,3,4,5} ;
|
||||
np::ndarray py_array = np::from_data(arr, np::dtype::get_builtin<int>() , p::make_tuple(5), p::make_tuple(sizeof(int)), p::object());
|
||||
|
||||
Print the source C++ array, as well as the ndarray, to check if they are the same::
|
||||
|
||||
@@ -33,9 +33,9 @@ Print the source C++ array, as well as the ndarray, to check if they are the sam
|
||||
|
||||
Now, change an element in the Python ndarray, and check if the value changed correspondingly in the source C++ array::
|
||||
|
||||
py_array[1] = 5 ;
|
||||
std::cout << "Is the change reflected in the C++ array used to create the ndarray ? " << std::endl ;
|
||||
for (int j = 0;j<4 ; j++)
|
||||
py_array[1] = 5 ;
|
||||
std::cout << "Is the change reflected in the C++ array used to create the ndarray ? " << std::endl ;
|
||||
for (int j = 0; j < 5; j++)
|
||||
{
|
||||
std::cout << arr[j] << ' ' ;
|
||||
}
|
||||
|
||||
@@ -39,15 +39,15 @@ We can also create an array by supplying data arrays and a few other parameters.
|
||||
|
||||
First,create an integer array ::
|
||||
|
||||
int data[] = {1,2,3,4} ;
|
||||
int data[] = {1,2,3,4,5} ;
|
||||
|
||||
Create a shape, and strides, needed by the function ::
|
||||
|
||||
p::tuple shape = p::make_tuple(4) ;
|
||||
p::tuple stride = p::make_tuple(4) ;
|
||||
p::tuple shape = p::make_tuple(5) ;
|
||||
p::tuple stride = p::make_tuple(sizeof(int)) ;
|
||||
|
||||
Here, shape is 1x4 , and the stride is also 4.
|
||||
A stride is the number of bytes that must be travelled to get to the next desired element while constructing the ndarray. Here, the size of the "int" is 32 bits and hence, the stride is 4 to access each element.
|
||||
Here, shape is (4,) , and the stride is `sizeof(int)``.
|
||||
A stride is the number of bytes that must be traveled to get to the next desired element while constructing the ndarray.
|
||||
|
||||
The function also needs an owner, to keep track of the data array passed. Passing none is dangerous ::
|
||||
|
||||
@@ -59,7 +59,7 @@ The from_data function takes the data array, datatype,shape,stride and owner as
|
||||
|
||||
Now let's print the ndarray we created ::
|
||||
|
||||
std::cout << "Single dimensional array ::" << std::endl << p::extract < char const * > (p::str(data_ex)) << std::endl ;
|
||||
std::cout << "Single dimensional array ::" << std::endl << p::extract < char const * > (p::str(data_ex)) << std::endl ;
|
||||
|
||||
Let's make it a little more interesting. Lets make an 3x2 ndarray from a multi-dimensional array using non-unit strides
|
||||
|
||||
@@ -71,7 +71,7 @@ Now let's create an array of 3x2 elements, picking the first and third elements
|
||||
The 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 ::
|
||||
|
||||
shape = p::make_tuple(3,2) ;
|
||||
stride = p::make_tuple(4,2) ;
|
||||
stride = p::make_tuple(sizeof(uint8_t)*2,sizeof(uint8_t)) ;
|
||||
|
||||
Get the numpy dtype for the built-in 8-bit integer data type ::
|
||||
|
||||
|
||||
Reference in New Issue
Block a user