Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Python bindings for the [xtensor](https://github.com/QuantStack/xtensor) C++ mul
- `xtensor` is a C++ library for multi-dimensional arrays enabling numpy-style broadcasting and lazy computing.
- `xtensor-python` enables inplace use of numpy arrays in C++ with all the benefits from `xtensor`

- C++ universal function and broadcasting
- C++ universal function and broadcasting
- STL - compliant APIs.
- A broad coverage of numpy APIs (see [the numpy to xtensor cheat sheet](http://xtensor.readthedocs.io/en/latest/numpy.html)).

Expand Down Expand Up @@ -54,14 +54,12 @@ double sum_of_sines(xt::pyarray<double>& m)
return std::accumulate(sines.begin(), sines.end(), 0.0);
}

PYBIND11_PLUGIN(xtensor_python_test)
PYBIND11_MODULE(xtensor_python_test, m)
{
xt::import_numpy();
pybind11::module m("xtensor_python_test", "Test module for xtensor python bindings");
m.doc() = "Test module for xtensor python bindings";

m.def("sum_of_sines", sum_of_sines, "Sum the sines of the input values");

return m.ptr();
}
```

Expand All @@ -80,7 +78,7 @@ s

```
1.2853996391883833
```
```

### Example 2: Create a universal function from a C++ scalar function

Expand All @@ -99,13 +97,11 @@ double scalar_func(double i, double j)
return std::sin(i) - std::cos(j);
}

PYBIND11_PLUGIN(xtensor_python_test)
PYBIND11_MODULE(xtensor_python_test, m)
{
py::module m("xtensor_python_test", "Test module for xtensor python bindings");
m.doc() = "Test module for xtensor python bindings";

m.def("vectorized_func", xt::pyvectorize(scalar_func), "");

return m.ptr();
}
```

Expand All @@ -127,7 +123,7 @@ z
[[-0.540302, 1.257618, 1.89929 , 0.794764, -1.040465],
[-1.499227, 0.136731, 1.646979, 1.643002, 0.128456],
[-1.084323, -0.583843, 0.45342 , 1.073811, 0.706945]]
```
```

## Installation

Expand Down Expand Up @@ -155,7 +151,7 @@ Testing `xtensor-python` requires `pytest`
py.test .
```

To pick up changes in `xtensor-python` while rebuilding, delete the `build/` directory.
To pick up changes in `xtensor-python` while rebuilding, delete the `build/` directory.

## Building the HTML Documentation

Expand All @@ -169,7 +165,7 @@ While doxygen must be installed separately, you can install breathe by typing

```bash
pip install breathe
```
```

Breathe can also be installed with `conda`

Expand Down
6 changes: 2 additions & 4 deletions benchmark/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ using complex_t = std::complex<double>;

namespace py = pybind11;

PYBIND11_PLUGIN(benchmark_xtensor_python)
PYBIND11_MODULE(benchmark_xtensor_python, m)
{
if(_import_array() < 0)
{
PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
return nullptr;
}

py::module m("benchmark_xtensor_python", "Benchmark module for xtensor python bindings");
m.doc() = "Benchmark module for xtensor python bindings";

m.def("sum_array", [](xt::pyarray<double> const& x) {
double sum = 0;
Expand Down Expand Up @@ -55,6 +55,4 @@ PYBIND11_PLUGIN(benchmark_xtensor_python)
else
throw py::type_error("rect_to_polar unhandled type");
});

return m.ptr();
}
12 changes: 4 additions & 8 deletions docs/source/basic_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ Example 1: Use an algorithm of the C++ library on a numpy array inplace
return std::accumulate(sines.cbegin(), sines.cend(), 0.0);
}

PYBIND11_PLUGIN(xtensor_python_test)
PYBIND11_MODULE(xtensor_python_test, m)
{
xt::import_numpy();
pybind11::module m("xtensor_python_test", "Test module for xtensor python bindings");
m.doc() = "Test module for xtensor python bindings";

m.def("sum_of_sines", sum_of_sines, "Sum the sines of the input values");

return m.ptr();
}

**Python code:**
Expand Down Expand Up @@ -74,14 +72,12 @@ Example 2: Create a numpy-style universal function from a C++ scalar function
return std::sin(i) - std::cos(j);
}

PYBIND11_PLUGIN(xtensor_python_test)
PYBIND11_MODULE(xtensor_python_test, m)
{
xt::import_numpy();
py::module m("xtensor_python_test", "Test module for xtensor python bindings");
m.doc() = "Test module for xtensor python bindings";

m.def("vectorized_func", xt::pyvectorize(scalar_func), "");

return m.ptr();
}

**Python code:**
Expand Down
4 changes: 1 addition & 3 deletions docs/source/numpy_capi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ Thus the basic skeleton of the module looks like:
#define FORCE_IMPORT_ARRAY
#include "xtensor-python/pyarray.hpp"

PYBIND11_PLUGIN(plugin_name)
PYBIND11_MODULE(plugin_name, m)
{
xt::import_numpy();
pybind11::module m(//...
//...
return m.ptr();
}


Expand Down
6 changes: 2 additions & 4 deletions test_python/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ void dump_numpy_constant()
std::cout << "NPY_UINT64 = " << NPY_UINT64 << std::endl;
}

PYBIND11_PLUGIN(xtensor_python_test)
PYBIND11_MODULE(xtensor_python_test, m)
{
xt::import_numpy();

py::module m("xtensor_python_test", "Test module for xtensor python bindings");
m.doc() = "Test module for xtensor python bindings";

m.def("example1", example1);
m.def("example2", example2);
Expand Down Expand Up @@ -142,6 +142,4 @@ PYBIND11_PLUGIN(xtensor_python_test)
m.def("int_overload", int_overload<int64_t>);

m.def("dump_numpy_constant", dump_numpy_constant);

return m.ptr();
}