diff --git a/libs/numpy/example/ndarray.cpp b/libs/numpy/example/ndarray.cpp index ec4dfade..1c1e7bf5 100644 --- a/libs/numpy/example/ndarray.cpp +++ b/libs/numpy/example/ndarray.cpp @@ -1,7 +1,7 @@ /** - * @brief An example to show how to create ndarrays using arbitrary Python sequences + * @brief An example to show how to create ndarrays using arbitrary Python sequences. * The Python sequence could be any object whose __array__ method returns an array, or any (nested) sequence. - * + * This example also shows how to create arrays using both unit and non-unit strides * */ @@ -29,13 +29,32 @@ int main(int argc, char **argv) // You can also create an array by supplying data.First,create an integer array int data[] = {1,2,3,4} ; // Create a shape, and strides, needed by the function - p::tuple shape = p::make_tuple(2,2) ; - p::tuple strides = p::make_tuple(strides(data)) ; + p::tuple shape = p::make_tuple(4) ; + p::tuple stride = p::make_tuple(4) ; // The function also needs an owner, to keep track of the data array passed. Passing none is dangerous - p::object owner ; + p::object own ; // The from_data function takes the data array, datatype,shape,stride and owner as arguments // and returns an ndarray - np::ndarray data_ex1 = np::from_data(data,dt, shape,strides,owner); + np::ndarray data_ex = np::from_data(data,dt,shape,stride,own); + // Print the ndarray we created + std::cout << "Single dimensional array ::" << std::endl << p::extract < char const * > (p::str(data_ex)) << std::endl ; + // Now lets make an 3x2 ndarray from a multi-dimensional array using non-unit strides + // First lets create a 3x4 array of 8-bit integers + uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}}; + // Now let's create an array of 3x2 elements, picking the first and third elements from each row + // For that, the shape will be 3x2 + shape = p::make_tuple(3,2) ; + // 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 + stride = p::make_tuple(4,2) ; + // Get the numpy dtype for the built-in 8-bit integer data type + np::dtype dt1 = np::dtype::get_builtin(); + // First lets create and print out the ndarray as is + np::ndarray mul_data_ex = np::from_data(mul_data,dt1, p::make_tuple(3,4),p::make_tuple(4,1),p::object()); + std::cout << "Original multi dimensional array :: " << std::endl << p::extract < char const * > (p::str(mul_data_ex)) << std::endl ; + // Now create the new ndarray using the shape and strides + mul_data_ex = np::from_data(mul_data,dt1, shape,stride,p::object()); + // Print out the array we created using non-unit strides + std::cout << "Selective multidimensional array :: "< (p::str(mul_data_ex)) << std::endl ; }