2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 04:22:16 +00:00

Fine-tune documentation formatting.

This commit is contained in:
Stefan Seefeld
2016-10-10 12:02:28 -04:00
parent a3d8223b5d
commit 080eb55be6
11 changed files with 845 additions and 160 deletions

View File

@@ -1,66 +1,716 @@
/*=============================================================================
Copyright 2002 William E. Kempf
Distributed under the Boost Software License, Version 1.0. (See accompany-
ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Copyright (c) 2004 Joel de Guzman
http://spirit.sourceforge.net/
Copyright 2013 Niall Douglas additions for colors and alignment.
Copyright 2013 Paul A. Bristow additions for more colors and alignments.
Distributed under the Boost Software License, Version 1.0. (See accompany-
ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
H1
{
FONT-SIZE: 200%;
COLOR: #00008B;
/*=============================================================================
Body defaults
=============================================================================*/
body
{
margin: 1em;
font-family: sans-serif;
}
/*=============================================================================
Paragraphs
=============================================================================*/
p
{
text-align: left;
font-size: 10pt;
line-height: 1.15;
}
/*=============================================================================
Program listings
=============================================================================*/
/* Code on paragraphs */
p tt.computeroutput
{
font-size: 9pt;
}
pre.synopsis
{
font-size: 9pt;
margin: 1pc 4% 0pc 4%;
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
}
.programlisting,
.screen
{
font-size: 9pt;
display: block;
margin: 1pc 4% 0pc 4%;
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
}
/* Program listings in tables don't get borders */
td .programlisting,
td .screen
{
margin: 0pc 0pc 0pc 0pc;
padding: 0pc 0pc 0pc 0pc;
}
/*=============================================================================
Headings
=============================================================================*/
h1, h2, h3, h4, h5, h6
{
text-align: left;
margin: 1em 0em 0.5em 0em;
font-weight: bold;
}
h1 { font-size: 140%; }
h2 { font-weight: bold; font-size: 140%; }
h3 { font-weight: bold; font-size: 130%; }
h4 { font-weight: bold; font-size: 120%; }
h5 { font-weight: normal; font-style: italic; font-size: 110%; }
h6 { font-weight: normal; font-style: italic; font-size: 100%; }
/* Top page titles */
title,
h1.title,
h2.title
h3.title,
h4.title,
h5.title,
h6.title,
.refentrytitle
{
font-weight: bold;
margin-bottom: 1pc;
}
h1.title { font-size: 140% }
h2.title { font-size: 140% }
h3.title { font-size: 130% }
h4.title { font-size: 120% }
h5.title { font-size: 110% }
h6.title { font-size: 100% }
.section h1
{
margin: 0em 0em 0.5em 0em;
font-size: 140%;
}
.section h2 { font-size: 140% }
.section h3 { font-size: 130% }
.section h4 { font-size: 120% }
.section h5 { font-size: 110% }
.section h6 { font-size: 100% }
/* Code on titles */
h1 tt.computeroutput { font-size: 140% }
h2 tt.computeroutput { font-size: 140% }
h3 tt.computeroutput { font-size: 130% }
h4 tt.computeroutput { font-size: 130% }
h5 tt.computeroutput { font-size: 130% }
h6 tt.computeroutput { font-size: 130% }
/*=============================================================================
Author
=============================================================================*/
h3.author
{
font-size: 100%
}
/*=============================================================================
Lists
=============================================================================*/
li
{
font-size: 10pt;
line-height: 1.3;
}
/* Unordered lists */
ul
{
text-align: left;
}
/* Ordered lists */
ol
{
text-align: left;
}
/*=============================================================================
Links
=============================================================================*/
a
{
text-decoration: none; /* no underline */
}
a:hover
{
text-decoration: underline;
}
/*=============================================================================
Spirit style navigation
=============================================================================*/
.spirit-nav
{
text-align: right;
}
.spirit-nav a
{
color: white;
padding-left: 0.5em;
}
.spirit-nav img
{
border-width: 0px;
}
/*=============================================================================
Copyright footer
=============================================================================*/
.copyright-footer
{
text-align: right;
font-size: 70%;
}
.copyright-footer p
{
text-align: right;
font-size: 80%;
}
/*=============================================================================
Table of contents
=============================================================================*/
div.toc
{
margin: 1pc 4% 0pc 4%;
padding: 0.1pc 1pc 0.1pc 1pc;
font-size: 80%;
line-height: 1.15;
}
.boost-toc
{
float: right;
padding: 0.5pc;
}
/* Code on toc */
.toc .computeroutput { font-size: 120% }
/* No margin on nested menus */
.toc dl dl { margin: 0; }
/*=============================================================================
Tables
=============================================================================*/
.table-title,
div.table p.title
{
margin-left: 4%;
padding-right: 0.5em;
padding-left: 0.5em;
}
.informaltable table,
.table table
{
width: 92%;
margin-left: 4%;
margin-right: 4%;
}
div.informaltable table,
div.table table
{
padding: 4px;
}
/* Table Cells */
div.informaltable table tr td,
div.table table tr td
{
padding: 0.5em;
text-align: left;
font-size: 9pt;
}
div.informaltable table tr th,
div.table table tr th
{
padding: 0.5em 0.5em 0.5em 0.5em;
border: 1pt solid white;
font-size: 80%;
}
table.simplelist
{
width: auto !important;
margin: 0em !important;
padding: 0em !important;
border: none !important;
}
table.simplelist td
{
margin: 0em !important;
padding: 0em !important;
text-align: left !important;
font-size: 9pt !important;
border: none !important;
}
/*=============================================================================
Suppress margins in tables
=============================================================================*/
table th > *:first-child,
table td > *:first-child
{
margin-top: 0;
}
table th > *:last-child,
table td > *:last-child
{
margin-bottom: 0;
}
/*=============================================================================
Blurbs
=============================================================================*/
div.note,
div.tip,
div.important,
div.caution,
div.warning,
p.blurb
{
font-size: 9pt; /* A little bit smaller than the main text */
line-height: 1.2;
display: block;
margin: 1pc 4% 0pc 4%;
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
}
p.blurb img
{
padding: 1pt;
}
/*=============================================================================
Variable Lists
=============================================================================*/
div.variablelist
{
margin: 1em 0;
}
/* Make the terms in definition lists bold */
div.variablelist dl dt,
span.term
{
font-weight: bold;
font-size: 10pt;
}
div.variablelist table tbody tr td
{
text-align: left;
vertical-align: top;
padding: 0em 2em 0em 0em;
font-size: 10pt;
margin: 0em 0em 0.5em 0em;
line-height: 1;
}
div.variablelist dl dt
{
margin-bottom: 0.2em;
}
div.variablelist dl dd
{
margin: 0em 0em 0.5em 2em;
font-size: 10pt;
}
div.variablelist table tbody tr td p,
div.variablelist dl dd p
{
margin: 0em 0em 0.5em 0em;
line-height: 1;
}
/*=============================================================================
Misc
=============================================================================*/
/* Title of books and articles in bibliographies */
span.title
{
font-style: italic;
}
span.underline
{
text-decoration: underline;
}
span.strikethrough
{
text-decoration: line-through;
}
/* Copyright, Legal Notice */
div div.legalnotice p
{
text-align: left
}
/*=============================================================================
Colors
=============================================================================*/
@media screen
{
body {
background-color: #FFFFFF;
color: #000000;
}
/* Syntax Highlighting */
.keyword { color: #0000AA; }
.identifier { color: #000000; }
.special { color: #707070; }
.preprocessor { color: #402080; }
.char { color: teal; }
.comment { color: #800000; }
.string { color: teal; }
.number { color: teal; }
.white_bkd { background-color: #FFFFFF; }
.dk_grey_bkd { background-color: #999999; }
/* Links */
a, a .keyword, a .identifier, a .special, a .preprocessor
a .char, a .comment, a .string, a .number
{
color: #005a9c;
}
a:visited, a:visited .keyword, a:visited .identifier,
a:visited .special, a:visited .preprocessor a:visited .char,
a:visited .comment, a:visited .string, a:visited .number
{
color: #9c5a9c;
}
h1 a, h2 a, h3 a, h4 a, h5 a, h6 a,
h1 a:hover, h2 a:hover, h3 a:hover, h4 a:hover, h5 a:hover, h6 a:hover,
h1 a:visited, h2 a:visited, h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited
{
text-decoration: none; /* no underline */
color: #000000;
}
/* Copyright, Legal Notice */
.copyright
{
color: #666666;
font-size: small;
}
div div.legalnotice p
{
color: #666666;
}
/* Program listing */
pre.synopsis
{
border: 1px solid #DCDCDC;
}
.programlisting,
.screen
{
border: 1px solid #DCDCDC;
}
td .programlisting,
td .screen
{
border: 0px solid #DCDCDC;
}
/* Blurbs */
div.note,
div.tip,
div.important,
div.caution,
div.warning,
p.blurb
{
border: 1px solid #DCDCDC;
}
/* Table of contents */
div.toc
{
border: 1px solid #DCDCDC;
}
/* Tables */
div.informaltable table tr td,
div.table table tr td
{
border: 1px solid #DCDCDC;
}
div.informaltable table tr th,
div.table table tr th
{
background-color: #F0F0F0;
border: 1px solid #DCDCDC;
}
.copyright-footer
{
color: #8F8F8F;
}
/* Misc */
span.highlight
{
color: #00A000;
}
}
@media print
{
/* Links */
a
{
color: black;
}
a:visited
{
color: black;
}
.spirit-nav
{
display: none;
}
/* Program listing */
pre.synopsis
{
border: 1px solid gray;
}
.programlisting,
.screen
{
border: 1px solid gray;
}
td .programlisting,
td .screen
{
border: 0px solid #DCDCDC;
}
/* Table of contents */
div.toc
{
border: 1px solid gray;
}
.informaltable table,
.table table
{
border: 1px solid gray;
border-collapse: collapse;
}
/* Tables */
div.informaltable table tr td,
div.table table tr td
{
border: 1px solid gray;
}
div.informaltable table tr th,
div.table table tr th
{
border: 1px solid gray;
}
table.simplelist tr td
{
border: none !important;
}
/* Misc */
span.highlight
{
font-weight: bold;
}
}
/*=============================================================================
Images
=============================================================================*/
span.inlinemediaobject img
{
vertical-align: middle;
}
/*==============================================================================
Super and Subscript: style so that line spacing isn't effected, see
http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=1&postId=5341
==============================================================================*/
sup,
sub {
height: 0;
line-height: 1;
vertical-align: baseline;
position: relative;
}
H2
{
FONT-SIZE: 150%;
/* For internet explorer: */
* html sup,
* html sub {
vertical-align: bottom;
}
H3
{
FONT-SIZE: 125%;
sup {
bottom: 1ex;
}
H4
{
FONT-SIZE: 108%;
sub {
top: .5ex;
}
BODY
/*==============================================================================
Indexes: pretty much the same as the TOC.
==============================================================================*/
.index
{
font-size: 80%;
padding-top: 0px;
padding-bottom: 0px;
margin-top: 0px;
margin-bottom: 0px;
margin-left: 0px;
}
.index ul
{
padding-left: 3em;
}
.index p
{
padding: 2px;
margin: 2px;
}
.index-entry-level-0
{
font-weight: bold;
}
.index em
{
font-weight: bold;
}
/*==============================================================================
Alignment and coloring use 'role' feature, available from Quickbook 1.6 up.
Added from Niall Douglas for role color and alignment.
http://article.gmane.org/gmane.comp.lib.boost.devel/243318
*/
/* Add text alignment (see http://www.w3schools.com/cssref/pr_text_text-align.asp) */
span.aligncenter
{
FONT-SIZE: 100%;
BACKGROUND-COLOR: #ffffff;
COLOR: #000000;
display: inline-block; width: 100%; text-align: center;
}
PRE
span.alignright
{
MARGIN-LEFT: 2em;
FONT-FAMILY: Courier,
monospace;
display: inline-block; width: 100%; text-align: right;
}
CODE
/* alignleft is the default. */
span.alignleft
{
FONT-FAMILY: Courier,
monospace;
display: inline-block; width: 100%; text-align: left;
}
CODE.as_pre
/* alignjustify stretches the word spacing so that each line has equal width
within a chosen fraction of page width (here arbitrarily 20%).
*Not* useful inside table items as the column width remains the total string width.
Nor very useful, except to temporarily restrict the width.
*/
span.alignjustify
{
white-space: pre;
display: inline-block; width: 20%; text-align: justify;
}
.index
{
TEXT-ALIGN: left;
}
.page-index
{
TEXT-ALIGN: left;
}
.definition
{
TEXT-ALIGN: left;
}
.footnote
{
FONT-SIZE: 66%;
VERTICAL-ALIGN: super;
TEXT-DECORATION: none;
}
.function-semantics
{
CLEAR: left;
}
/* Text colors.
Names at http://www.w3.org/TR/2002/WD-css3-color-20020219/ 4.3. X11 color keywords.
Quickbook Usage: [role red Some red text]
*/
span.red { inline-block; color: red; }
span.green { color: green; }
span.lime { color: #00FF00; }
span.blue { color: blue; }
span.navy { color: navy; }
span.yellow { color: yellow; }
span.magenta { color: magenta; }
span.indigo { color: #4B0082; }
span.cyan { color: cyan; }
span.purple { color: purple; }
span.gold { color: gold; }
span.silver { color: silver; } /* lighter gray */
span.gray { color: #808080; } /* light gray */

View File

@@ -1,5 +1,12 @@
@import url(boost.css);
.header h1 a
{
color: #00507f;
font-size: 200%;
font-style: italic;
}
.header h3 { margin: 1px;}
#contents
{
/* border-bottom: solid thin black;*/
@@ -7,5 +14,16 @@
.highlight
{
border: 1px solid #aaaaaa;
}
border: 1px solid #aaaaaa;
background-color: inherit;
padding: 0 1em;
margin: 0 5em;
}
#searchbox
{
float: right;
width: auto;
margin: 0 2em;
}
.admonition-title { font-weight: bold;}

View File

@@ -33,8 +33,8 @@
<link rel="alternate" type="{{ type|e(true) }}" title="{{ title|e(true) }}" href="{{ link|e(true) }}" />
{%- endfor %}
{%- else %}
<link rel="stylesheet" href="{{ pathto('_static/style.css', 1) }}" type="text/css" />
<link rel="stylesheet" href="{{ pathto('_static/pygments.css', 1) }}" type="text/css" />
<link rel="stylesheet" href="{{ pathto('_static/style.css', 1) }}" type="text/css" />
{%- endif %}
{%- if builder != 'htmlhelp' %}
@@ -90,8 +90,8 @@
alt="C++ Boost" src="{{ pathto('_static/' + logo, 1) }}" border="0"></a></h3>
</td>
<td valign="top">
<!-- <h1 align="center"><a href="{{ pathto('index') }}">Boost.NumPy</a></h1>-->
<td >
<h1 align="center"><a href="{{ pathto('index') }}">(NumPy)</a></h1>
<!-- <h2 align="center">CallPolicies Concept</h2>-->
</td>
<td>

View File

@@ -90,6 +90,7 @@ Example(s)
----------
::
namespace p = boost::python;
namespace np = boost::python::numpy;

View File

@@ -82,6 +82,7 @@ Example(s)
----------
::
namespace p = boost::python;
namespace np = boost::python::numpy;

View File

@@ -353,6 +353,7 @@ Example(s)
----------
::
namespace p = boost::python;
namespace np = boost::python::numpy;

View File

@@ -86,6 +86,7 @@ Example(s)
----------
::
namespace p = boost::python;
namespace np = boost::python::numpy;

View File

@@ -5,32 +5,32 @@ Here is a brief tutorial to show how to create ndarrays with built-in python dat
Like before, first get the necessary headers, setup the namespaces and initialize the Python runtime and numpy module::
#include <boost/python/numpy.hpp>
#include <iostream>
#include <boost/python/numpy.hpp>
#include <iostream>
namespace p = boost::python;
namespace np = boost::python::numpy;
namespace p = boost::python;
namespace np = boost::python::numpy;
int main(int argc, char **argv)
{
Py_Initialize();
np::initialize();
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);
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;
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 ;
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
@@ -40,16 +40,15 @@ The list should contain one or more tuples of the format (variable name, variabl
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) ;
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) ;
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);
}
np::ndarray new_array = np::zeros(shape,custom_dtype);
}

View File

@@ -6,46 +6,51 @@ 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/python/numpy.hpp>
#include <iostream>
#include <boost/python/numpy.hpp>
#include <iostream>
namespace p = boost::python;
namespace np = boost::python::numpy;
namespace p = boost::python;
namespace np = boost::python::numpy;
int main(int argc, char **argv)
{
Py_Initialize();
np::initialize();
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,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());
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::
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;
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 < 5; j++)
{
std::cout << arr[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] << ' ';
}
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;
}
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.

View File

@@ -8,87 +8,92 @@ This tutorial will introduce you to some of the ways in which you can create nda
First, as before, initialise the necessary namepaces and runtimes ::
#include <boost/python/numpy.hpp>
#include <iostream>
#include <boost/python/numpy.hpp>
#include <iostream>
namespace p = boost::python;
namespace np = boost::python::numpy;
namespace p = boost::python;
namespace np = boost::python::numpy;
int main(int argc, char **argv)
{
Py_Initialize();
np::initialize();
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) ;
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) ;
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);
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,5} ;
int data[] = {1,2,3,4,5};
Create a shape, and strides, needed by the function ::
p::tuple shape = p::make_tuple(5) ;
p::tuple stride = p::make_tuple(sizeof(int)) ;
p::tuple shape = p::make_tuple(5);
p::tuple stride = p::make_tuple(sizeof(int));
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 ::
p::object own ;
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);
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 ;
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}};
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(sizeof(uint8_t)*2,sizeof(uint8_t)) ;
shape = p::make_tuple(3,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 ::
np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
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 ;
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 ;
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
}
.. 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.

View File

@@ -35,82 +35,86 @@ Now we create the structs necessary to implement the ufuncs. The typedefs *must*
Initialise the Python runtime and the numpy module ::
int main(int argc, char **argv)
{
Py_Initialize();
np::initialize();
{
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());
p::object ud = p::class_<UnarySquare, boost::shared_ptr<UnarySquare> >("UnarySquare");
ud.def("__call__", np::unary_ufunc<UnarySquare>::make());
Let inst be an instance of the class ud ::
p::object inst = 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 ;
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} ;
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());
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 ;
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) ;
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 ;
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);
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 ;
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) ;
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 ;
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());
ud = p::class_<BinarySquare, boost::shared_ptr<BinarySquare> >("BinarySquare");
ud.def("__call__", np::binary_ufunc<BinarySquare>::make());
And initialise ud ::
inst = 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 ;
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) ;
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 ;
}
std::cout << "Square of list with binary ufunc is " << p::extract<char const *>(p::str(result_array)) << std::endl;
}