Packaging: polished
This commit is contained in:
55
python/external/boost/libs/numpy/doc/tutorial/dtype.rst
vendored
Normal file
55
python/external/boost/libs/numpy/doc/tutorial/dtype.rst
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
How to use dtypes
|
||||
=================
|
||||
|
||||
Here is a brief tutorial to show how to create ndarrays with built-in python data types, and extract the types and values of member variables
|
||||
|
||||
Like before, first get the necessary headers, setup the namespaces and initialize the Python runtime and numpy module::
|
||||
|
||||
#include <boost/numpy.hpp>
|
||||
#include <iostream>
|
||||
|
||||
namespace p = boost::python;
|
||||
namespace np = boost::numpy;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
Py_Initialize();
|
||||
np::initialize();
|
||||
|
||||
Next, we create the shape and dtype. We use the get_builtin method to get the numpy dtype corresponding to the builtin C++ dtype
|
||||
Here, we will create a 3x3 array passing a tuple with (3,3) for the size, and double as the data type ::
|
||||
|
||||
p::tuple shape = p::make_tuple(3, 3);
|
||||
np::dtype dtype = np::dtype::get_builtin<double>();
|
||||
np::ndarray a = np::zeros(shape, dtype);
|
||||
|
||||
Finally, we can print the array using the extract method in the python namespace.
|
||||
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 ::
|
||||
|
||||
std::cout << "Original array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
|
||||
|
||||
We can also print the dtypes of the data members of the ndarray by using the get_dtype method for the ndarray ::
|
||||
|
||||
std::cout << "Datatype is:\n" << p::extract<char const *>(p::str(a.get_dtype())) << std::endl ;
|
||||
|
||||
We can also create custom dtypes and build ndarrays with the custom dtypes
|
||||
|
||||
We use the dtype constructor to create a custom dtype. This constructor takes a list as an argument.
|
||||
|
||||
The list should contain one or more tuples of the format (variable name, variable type)
|
||||
|
||||
So first create a tuple with a variable name and its dtype, double, to create a custom dtype ::
|
||||
|
||||
p::tuple for_custom_dtype = p::make_tuple("ha",dtype) ;
|
||||
|
||||
Next, create a list, and add this tuple to the list. Then use the list to create the custom dtype ::
|
||||
|
||||
p::list list_for_dtype ;
|
||||
list_for_dtype.append(for_custom_dtype) ;
|
||||
np::dtype custom_dtype = np::dtype(list_for_dtype) ;
|
||||
|
||||
We are now ready to create an ndarray with dimensions specified by \*shape\* and of custom dtpye ::
|
||||
|
||||
np::ndarray new_array = np::zeros(shape,custom_dtype);
|
||||
|
||||
}
|
51
python/external/boost/libs/numpy/doc/tutorial/fromdata.rst
vendored
Normal file
51
python/external/boost/libs/numpy/doc/tutorial/fromdata.rst
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
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.
|
||||
|
||||
Like before, first get the necessary headers, setup the namespaces and initialize the Python runtime and numpy module::
|
||||
|
||||
#include <boost/numpy.hpp>
|
||||
#include <iostream>
|
||||
|
||||
namespace p = boost::python;
|
||||
namespace np = boost::numpy;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
Py_Initialize();
|
||||
np::initialize();
|
||||
|
||||
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());
|
||||
|
||||
Print the source C++ array, as well as the ndarray, to check if they are the same::
|
||||
|
||||
std::cout << "C++ array :" << std::endl ;
|
||||
for (int j=0;j<4;j++)
|
||||
{
|
||||
std::cout << arr[j] << ' ' ;
|
||||
}
|
||||
std::cout << std::endl << "Python ndarray :" << p::extract<char const *>(p::str(py_array)) << std::endl;
|
||||
|
||||
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++)
|
||||
{
|
||||
std::cout << arr[j] << ' ' ;
|
||||
}
|
||||
|
||||
Next, change an element of the source C++ array and see if it is reflected in the Python ndarray::
|
||||
|
||||
arr[2] = 8 ;
|
||||
std::cout << std::endl << "Is the change reflected in the Python ndarray ?" << std::endl << p::extract<char const *>(p::str(py_array)) << std::endl;
|
||||
|
||||
}
|
||||
|
||||
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.
|
||||
|
14
python/external/boost/libs/numpy/doc/tutorial/index.rst
vendored
Normal file
14
python/external/boost/libs/numpy/doc/tutorial/index.rst
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
Boost.NumPy Tutorial
|
||||
====================
|
||||
|
||||
Contents:
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
|
||||
simple
|
||||
dtype
|
||||
ndarray
|
||||
ufunc
|
||||
fromdata
|
||||
|
94
python/external/boost/libs/numpy/doc/tutorial/ndarray.rst
vendored
Normal file
94
python/external/boost/libs/numpy/doc/tutorial/ndarray.rst
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
Creating ndarrays
|
||||
=================
|
||||
|
||||
The 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.
|
||||
ndarrays can also be created from arbitrary python sequences as well as from data and dtypes.
|
||||
|
||||
This 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
|
||||
|
||||
First, as before, initialise the necessary namepaces and runtimes ::
|
||||
|
||||
#include <boost/numpy.hpp>
|
||||
#include <iostream>
|
||||
|
||||
namespace p = boost::python;
|
||||
namespace np = boost::numpy;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
Py_Initialize();
|
||||
np::initialize();
|
||||
|
||||
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 ::
|
||||
|
||||
p::object tu = p::make_tuple('a','b','c') ;
|
||||
np::ndarray example_tuple = np::array(tu) ;
|
||||
|
||||
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 ::
|
||||
|
||||
p::list l ;
|
||||
l.append('a') ;
|
||||
np::ndarray example_list = np::array (l) ;
|
||||
|
||||
Optionally, we can also specify a dtype for the array ::
|
||||
|
||||
np::dtype dt = np::dtype::get_builtin<int>();
|
||||
np::ndarray example_list1 = np::array (l,dt);
|
||||
|
||||
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} ;
|
||||
|
||||
Create a shape, and strides, needed by the function ::
|
||||
|
||||
p::tuple shape = p::make_tuple(4) ;
|
||||
p::tuple stride = p::make_tuple(4) ;
|
||||
|
||||
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.
|
||||
|
||||
The function also needs an owner, to keep track of the data array passed. Passing none is dangerous ::
|
||||
|
||||
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,stride,own);
|
||||
|
||||
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 ;
|
||||
|
||||
Let's make it a little more interesting. 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.
|
||||
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) ;
|
||||
|
||||
Get the numpy dtype for the built-in 8-bit integer data type ::
|
||||
|
||||
np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
|
||||
|
||||
Now lets first create and print out the ndarray as is.
|
||||
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
|
||||
manipulate the "owner" object ::
|
||||
|
||||
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 and print out the array we created using non-unit strides ::
|
||||
|
||||
mul_data_ex = np::from_data(mul_data,dt1, shape,stride,p::object());
|
||||
std::cout << "Selective multidimensional array :: "<<std::endl << p::extract < char const * > (p::str(mul_data_ex)) << std::endl ;
|
||||
|
||||
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
|
||||
|
||||
}
|
41
python/external/boost/libs/numpy/doc/tutorial/simple.rst
vendored
Normal file
41
python/external/boost/libs/numpy/doc/tutorial/simple.rst
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
A simple tutorial on Arrays
|
||||
===========================
|
||||
|
||||
Let's start with a simple tutorial to create and modify arrays.
|
||||
|
||||
Get the necessary headers for numpy components and set up necessary namespaces::
|
||||
|
||||
#include <boost/numpy.hpp>
|
||||
#include <iostream>
|
||||
|
||||
namespace p = boost::python;
|
||||
namespace np = boost::numpy;
|
||||
|
||||
Initialise the Python runtime, and the numpy module. Failure to call these results in segmentation errors::
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
Py_Initialize();
|
||||
np::initialize();
|
||||
|
||||
|
||||
Zero filled n-dimensional arrays can be created using the shape and data-type of the array as a parameter. Here, the shape is 3x3 and the datatype is the built-in float type::
|
||||
|
||||
p::tuple shape = p::make_tuple(3, 3);
|
||||
np::dtype dtype = np::dtype::get_builtin<float>();
|
||||
np::ndarray a = np::zeros(shape, dtype);
|
||||
|
||||
You can also create an empty array like this ::
|
||||
|
||||
np::ndarray b = np::empty(shape,dtype);
|
||||
|
||||
Print the original and reshaped array. The array a which is a list is first converted to a string, and each value in the list is extracted using extract< >::
|
||||
|
||||
std::cout << "Original array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
|
||||
|
||||
// Reshape the array into a 1D array
|
||||
a = a.reshape(p::make_tuple(9));
|
||||
// Print it again.
|
||||
std::cout << "Reshaped array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
|
||||
}
|
||||
|
116
python/external/boost/libs/numpy/doc/tutorial/ufunc.rst
vendored
Normal file
116
python/external/boost/libs/numpy/doc/tutorial/ufunc.rst
vendored
Normal file
@@ -0,0 +1,116 @@
|
||||
Ufuncs
|
||||
======
|
||||
|
||||
Ufuncs or universal functions operate on ndarrays element by element, and support array broadcasting, type casting, and other features.
|
||||
|
||||
Lets try and see how we can use the binary and unary ufunc methods
|
||||
|
||||
After the neccessary includes ::
|
||||
|
||||
#include <boost/numpy.hpp>
|
||||
#include <iostream>
|
||||
|
||||
namespace p = boost::python;
|
||||
namespace np = boost::numpy;
|
||||
|
||||
Now we create the structs necessary to implement the ufuncs. The typedefs *must* be made as the ufunc generators take these typedefs as inputs and return an error otherwise ::
|
||||
|
||||
struct UnarySquare
|
||||
{
|
||||
typedef double argument_type;
|
||||
typedef double result_type;
|
||||
|
||||
double operator()(double r) const { return r * r;}
|
||||
};
|
||||
|
||||
struct BinarySquare
|
||||
{
|
||||
typedef double first_argument_type;
|
||||
typedef double second_argument_type;
|
||||
typedef double result_type;
|
||||
|
||||
double operator()(double a,double b) const { return (a*a + b*b) ; }
|
||||
};
|
||||
|
||||
Initialise the Python runtime and the numpy module ::
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
Py_Initialize();
|
||||
np::initialize();
|
||||
|
||||
Now expose the struct UnarySquare to Python as a class, and let ud be the class object. ::
|
||||
|
||||
p::object ud = p::class_<UnarySquare, boost::shared_ptr<UnarySquare> >("UnarySquare")
|
||||
.def("__call__", np::unary_ufunc<UnarySquare>::make());
|
||||
|
||||
Let inst be an instance of the class ud ::
|
||||
|
||||
p::object inst = ud();
|
||||
|
||||
Use the "__call__" method to call the overloaded () operator and print the value ::
|
||||
|
||||
std::cout << "Square of unary scalar 1.0 is " << p::extract <char const * > (p::str(inst.attr("__call__")(1.0))) << std::endl ;
|
||||
|
||||
Create an array in C++ ::
|
||||
|
||||
int arr[] = {1,2,3,4} ;
|
||||
|
||||
|
||||
..and use it to create the ndarray in Python ::
|
||||
|
||||
np::ndarray demo_array = np::from_data(arr, np::dtype::get_builtin<int>() , p::make_tuple(4), p::make_tuple(4), p::object());
|
||||
|
||||
Print out the demo array ::
|
||||
|
||||
std::cout << "Demo array is " << p::extract <char const * > (p::str(demo_array)) << std::endl ;
|
||||
|
||||
Call the "__call__" method to perform the operation and assign the value to result_array ::
|
||||
|
||||
p::object result_array = inst.attr("__call__")(demo_array) ;
|
||||
|
||||
Print the resultant array ::
|
||||
|
||||
std::cout << "Square of demo array is " << p::extract <char const * > (p::str(result_array)) << std::endl ;
|
||||
|
||||
Lets try the same with a list ::
|
||||
|
||||
p::list li ;
|
||||
li.append(3);
|
||||
li.append(7);
|
||||
|
||||
Print out the demo list ::
|
||||
|
||||
std::cout << "Demo list is " << p::extract <char const * > (p::str(li)) << std::endl ;
|
||||
|
||||
Call the ufunc for the list ::
|
||||
|
||||
result_array = inst.attr("__call__")(li) ;
|
||||
|
||||
And print the list out ::
|
||||
|
||||
std::cout << "Square of demo list is " << p::extract <char const * > (p::str(result_array)) << std::endl ;
|
||||
|
||||
Now lets try Binary ufuncs. Again, expose the struct BinarySquare to Python as a class, and let ud be the class object ::
|
||||
|
||||
ud = p::class_<BinarySquare, boost::shared_ptr<BinarySquare> >("BinarySquare")
|
||||
.def("__call__", np::binary_ufunc<BinarySquare>::make());
|
||||
|
||||
And initialise ud ::
|
||||
|
||||
inst = ud();
|
||||
|
||||
Print the two input lists ::
|
||||
|
||||
std::cout << "The two input list for binary ufunc are " << std::endl << p::extract <char const * > (p::str(demo_array)) << std::endl << p::extract <char const * > (p::str(demo_array)) << std::endl ;
|
||||
|
||||
Call the binary ufunc taking demo_array as both inputs ::
|
||||
|
||||
result_array = inst.attr("__call__")(demo_array,demo_array) ;
|
||||
|
||||
And print the output ::
|
||||
|
||||
std::cout << "Square of list with binary ufunc is " << p::extract <char const * > (p::str(result_array)) << std::endl ;
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user