Skip to content

Commit 080eb55

Browse files
committed
Fine-tune documentation formatting.
1 parent a3d8223 commit 080eb55

11 files changed

Lines changed: 845 additions & 160 deletions

File tree

doc/numpy/_static/boost.css

Lines changed: 701 additions & 51 deletions
Large diffs are not rendered by default.

doc/numpy/_static/style.css

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
@import url(boost.css);
22

3+
.header h1 a
4+
{
5+
color: #00507f;
6+
font-size: 200%;
7+
font-style: italic;
8+
}
9+
.header h3 { margin: 1px;}
310
#contents
411
{
512
/* border-bottom: solid thin black;*/
613
}
714

815
.highlight
916
{
10-
border: 1px solid #aaaaaa;
11-
}
17+
border: 1px solid #aaaaaa;
18+
background-color: inherit;
19+
padding: 0 1em;
20+
margin: 0 5em;
21+
}
22+
#searchbox
23+
{
24+
float: right;
25+
width: auto;
26+
margin: 0 2em;
27+
}
28+
29+
.admonition-title { font-weight: bold;}

doc/numpy/_templates/layout.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<link rel="alternate" type="{{ type|e(true) }}" title="{{ title|e(true) }}" href="{{ link|e(true) }}" />
3434
{%- endfor %}
3535
{%- else %}
36-
<link rel="stylesheet" href="{{ pathto('_static/style.css', 1) }}" type="text/css" />
3736
<link rel="stylesheet" href="{{ pathto('_static/pygments.css', 1) }}" type="text/css" />
37+
<link rel="stylesheet" href="{{ pathto('_static/style.css', 1) }}" type="text/css" />
3838

3939
{%- endif %}
4040
{%- if builder != 'htmlhelp' %}
@@ -90,8 +90,8 @@ <h3><a href="{{ pathto('index') }}"><img
9090
alt="C++ Boost" src="{{ pathto('_static/' + logo, 1) }}" border="0"></a></h3>
9191
</td>
9292

93-
<td valign="top">
94-
<!-- <h1 align="center"><a href="{{ pathto('index') }}">Boost.NumPy</a></h1>-->
93+
<td >
94+
<h1 align="center"><a href="{{ pathto('index') }}">(NumPy)</a></h1>
9595
<!-- <h2 align="center">CallPolicies Concept</h2>-->
9696
</td>
9797
<td>

doc/numpy/reference/binary_ufunc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Example(s)
9090
----------
9191

9292
::
93+
9394
namespace p = boost::python;
9495
namespace np = boost::python::numpy;
9596

doc/numpy/reference/dtype.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Example(s)
8282
----------
8383

8484
::
85+
8586
namespace p = boost::python;
8687
namespace np = boost::python::numpy;
8788

doc/numpy/reference/ndarray.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ Example(s)
353353
----------
354354

355355
::
356+
356357
namespace p = boost::python;
357358
namespace np = boost::python::numpy;
358359

doc/numpy/reference/unary_ufunc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Example(s)
8686
----------
8787

8888
::
89+
8990
namespace p = boost::python;
9091
namespace np = boost::python::numpy;
9192

doc/numpy/tutorial/dtype.rst

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@ Here is a brief tutorial to show how to create ndarrays with built-in python dat
55

66
Like before, first get the necessary headers, setup the namespaces and initialize the Python runtime and numpy module::
77

8-
#include <boost/python/numpy.hpp>
9-
#include <iostream>
8+
#include <boost/python/numpy.hpp>
9+
#include <iostream>
1010

11-
namespace p = boost::python;
12-
namespace np = boost::python::numpy;
11+
namespace p = boost::python;
12+
namespace np = boost::python::numpy;
1313

14-
int main(int argc, char **argv)
15-
{
16-
Py_Initialize();
17-
np::initialize();
14+
int main(int argc, char **argv)
15+
{
16+
Py_Initialize();
17+
np::initialize();
1818

1919
Next, we create the shape and dtype. We use the get_builtin method to get the numpy dtype corresponding to the builtin C++ dtype
2020
Here, we will create a 3x3 array passing a tuple with (3,3) for the size, and double as the data type ::
2121

22-
p::tuple shape = p::make_tuple(3, 3);
23-
np::dtype dtype = np::dtype::get_builtin<double>();
24-
np::ndarray a = np::zeros(shape, dtype);
22+
p::tuple shape = p::make_tuple(3, 3);
23+
np::dtype dtype = np::dtype::get_builtin<double>();
24+
np::ndarray a = np::zeros(shape, dtype);
2525

2626
Finally, we can print the array using the extract method in the python namespace.
2727
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 ::
2828

29-
std::cout << "Original array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
29+
std::cout << "Original array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
3030

3131
We can also print the dtypes of the data members of the ndarray by using the get_dtype method for the ndarray ::
3232

33-
std::cout << "Datatype is:\n" << p::extract<char const *>(p::str(a.get_dtype())) << std::endl ;
33+
std::cout << "Datatype is:\n" << p::extract<char const *>(p::str(a.get_dtype())) << std::endl ;
3434

3535
We can also create custom dtypes and build ndarrays with the custom dtypes
3636

@@ -40,16 +40,15 @@ The list should contain one or more tuples of the format (variable name, variabl
4040

4141
So first create a tuple with a variable name and its dtype, double, to create a custom dtype ::
4242

43-
p::tuple for_custom_dtype = p::make_tuple("ha",dtype) ;
43+
p::tuple for_custom_dtype = p::make_tuple("ha",dtype) ;
4444

4545
Next, create a list, and add this tuple to the list. Then use the list to create the custom dtype ::
4646

47-
p::list list_for_dtype ;
48-
list_for_dtype.append(for_custom_dtype) ;
49-
np::dtype custom_dtype = np::dtype(list_for_dtype) ;
47+
p::list list_for_dtype ;
48+
list_for_dtype.append(for_custom_dtype) ;
49+
np::dtype custom_dtype = np::dtype(list_for_dtype) ;
5050

5151
We are now ready to create an ndarray with dimensions specified by \*shape\* and of custom dtpye ::
5252

53-
np::ndarray new_array = np::zeros(shape,custom_dtype);
54-
55-
}
53+
np::ndarray new_array = np::zeros(shape,custom_dtype);
54+
}

doc/numpy/tutorial/fromdata.rst

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,51 @@ The from_data method makes this possible.
66

77
Like before, first get the necessary headers, setup the namespaces and initialize the Python runtime and numpy module::
88

9-
#include <boost/python/numpy.hpp>
10-
#include <iostream>
9+
#include <boost/python/numpy.hpp>
10+
#include <iostream>
1111

12-
namespace p = boost::python;
13-
namespace np = boost::python::numpy;
12+
namespace p = boost::python;
13+
namespace np = boost::python::numpy;
1414

15-
int main(int argc, char **argv)
16-
{
17-
Py_Initialize();
18-
np::initialize();
15+
int main(int argc, char **argv)
16+
{
17+
Py_Initialize();
18+
np::initialize();
1919

2020
Create an array in C++ , and pass the pointer to it to the from_data method to create an ndarray::
2121

22-
int arr[] = {1,2,3,4,5} ;
23-
np::ndarray py_array = np::from_data(arr, np::dtype::get_builtin<int>() , p::make_tuple(5), p::make_tuple(sizeof(int)), p::object());
22+
int arr[] = {1,2,3,4,5};
23+
np::ndarray py_array = np::from_data(arr, np::dtype::get_builtin<int>(),
24+
p::make_tuple(5),
25+
p::make_tuple(sizeof(int)),
26+
p::object());
2427

2528
Print the source C++ array, as well as the ndarray, to check if they are the same::
2629

27-
std::cout << "C++ array :" << std::endl ;
28-
for (int j=0;j<4;j++)
29-
{
30-
std::cout << arr[j] << ' ' ;
31-
}
32-
std::cout << std::endl << "Python ndarray :" << p::extract<char const *>(p::str(py_array)) << std::endl;
30+
std::cout << "C++ array :" << std::endl;
31+
for (int j=0;j<4;j++)
32+
{
33+
std::cout << arr[j] << ' ';
34+
}
35+
std::cout << std::endl
36+
<< "Python ndarray :" << p::extract<char const *>(p::str(py_array)) << std::endl;
3337

3438
Now, change an element in the Python ndarray, and check if the value changed correspondingly in the source C++ array::
3539

36-
py_array[1] = 5 ;
37-
std::cout << "Is the change reflected in the C++ array used to create the ndarray ? " << std::endl ;
38-
for (int j = 0; j < 5; j++)
39-
{
40-
std::cout << arr[j] << ' ' ;
41-
}
40+
py_array[1] = 5 ;
41+
std::cout << "Is the change reflected in the C++ array used to create the ndarray ? " << std::endl;
42+
for (int j = 0; j < 5; j++)
43+
{
44+
std::cout << arr[j] << ' ';
45+
}
4246

4347
Next, change an element of the source C++ array and see if it is reflected in the Python ndarray::
4448

45-
arr[2] = 8 ;
46-
std::cout << std::endl << "Is the change reflected in the Python ndarray ?" << std::endl << p::extract<char const *>(p::str(py_array)) << std::endl;
47-
48-
}
49+
arr[2] = 8;
50+
std::cout << std::endl
51+
<< "Is the change reflected in the Python ndarray ?" << std::endl
52+
<< p::extract<char const *>(p::str(py_array)) << std::endl;
53+
}
4954

5055
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.
5156

doc/numpy/tutorial/ndarray.rst

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,87 +8,92 @@ This tutorial will introduce you to some of the ways in which you can create nda
88

99
First, as before, initialise the necessary namepaces and runtimes ::
1010

11-
#include <boost/python/numpy.hpp>
12-
#include <iostream>
11+
#include <boost/python/numpy.hpp>
12+
#include <iostream>
1313

14-
namespace p = boost::python;
15-
namespace np = boost::python::numpy;
14+
namespace p = boost::python;
15+
namespace np = boost::python::numpy;
1616

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();
2121

2222
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 ::
2323

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);
2626

2727
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 ::
2828

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);
3232

3333
Optionally, we can also specify a dtype for the array ::
3434

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);
3737

3838
We can also create an array by supplying data arrays and a few other parameters.
3939

4040
First,create an integer array ::
4141

42-
int data[] = {1,2,3,4,5} ;
42+
int data[] = {1,2,3,4,5};
4343

4444
Create a shape, and strides, needed by the function ::
4545

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));
4848

4949
Here, shape is (4,) , and the stride is `sizeof(int)``.
5050
A stride is the number of bytes that must be traveled to get to the next desired element while constructing the ndarray.
5151

5252
The function also needs an owner, to keep track of the data array passed. Passing none is dangerous ::
5353

54-
p::object own ;
54+
p::object own;
5555

5656
The from_data function takes the data array, datatype,shape,stride and owner as arguments and returns an ndarray ::
5757

58-
np::ndarray data_ex1 = np::from_data(data,dt, shape,stride,own);
58+
np::ndarray data_ex1 = np::from_data(data,dt, shape,stride,own);
5959

6060
Now let's print the ndarray we created ::
6161

62-
std::cout << "Single dimensional array ::" << std::endl << p::extract < char const * > (p::str(data_ex)) << std::endl ;
62+
std::cout << "Single dimensional array ::" << std::endl
63+
<< p::extract<char const *>(p::str(data_ex)) << std::endl;
6364

6465
Let's make it a little more interesting. Lets make an 3x2 ndarray from a multi-dimensional array using non-unit strides
6566

6667
First lets create a 3x4 array of 8-bit integers ::
6768

68-
uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}};
69+
uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}};
6970

7071
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.
7172
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 ::
7273

73-
shape = p::make_tuple(3,2) ;
74-
stride = p::make_tuple(sizeof(uint8_t)*2,sizeof(uint8_t)) ;
74+
shape = p::make_tuple(3,2);
75+
stride = p::make_tuple(sizeof(uint8_t)*2,sizeof(uint8_t));
7576
7677
Get the numpy dtype for the built-in 8-bit integer data type ::
7778

78-
np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
79+
np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
7980

8081
Now lets first create and print out the ndarray as is.
8182
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
8283
manipulate the "owner" object ::
8384

84-
np::ndarray mul_data_ex = np::from_data(mul_data,dt1, p::make_tuple(3,4),p::make_tuple(4,1),p::object());
85-
std::cout << "Original multi dimensional array :: " << std::endl << p::extract < char const * > (p::str(mul_data_ex)) << std::endl ;
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;
8691

8792
Now create the new ndarray using the shape and strides and print out the array we created using non-unit strides ::
8893

89-
mul_data_ex = np::from_data(mul_data,dt1, shape,stride,p::object());
90-
std::cout << "Selective multidimensional array :: "<<std::endl << p::extract < char const * > (p::str(mul_data_ex)) << std::endl ;
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+
}
9198

92-
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

Comments
 (0)