xtensor

INSTALLATION

  • Installation

USAGE

  • Basic usage
  • Expressions and lazy evaluation
  • Arrays and tensors
  • Operators and functions
  • Views
  • Expression builders
  • Missing values

API REFERENCE

  • Expressions and semantic
  • Containers and views
  • Functions and generators
  • Mathematical functions

DEVELOPER ZONE

  • Compiler workarounds
  • Build and configuration
  • Extending xtensor
  • Releasing xtensor

MISCELLANEOUS

  • From numpy to xtensor
  • Notable differences with numpy
  • Closure semantics
  • Related projects
    • Example 1: Use an algorithm of the C++ library on a numpy array inplace
    • Example 2: Create a universal function from a C++ scalar function
xtensor
  • Docs »
  • Related projects
  • View page source

Related projects¶

xtensor-python

The xtensor-python project provides the implementation of container types compatible with xtensor‘s expression system, pyarray and pytensor which effectively wrap numpy arrays, allowing operating on numpy arrays inplace.

Example 1: Use an algorithm of the C++ library on a numpy array inplace¶

C++ code

#include <numeric>                        // Standard library import for std::accumulate
#include "pybind11/pybind11.h"            // Pybind11 import to define Python bindings
#include "xtensor/xmath.hpp"              // xtensor import for the C++ universal functions
#include "xtensor-python/pyarray.hpp"     // Numpy bindings

double sum_of_sines(xt::pyarray<double> &m)
{
    auto sines = xt::sin(m);
    // sines does not actually hold any value, which are only computed upon access
    return std::accumulate(sines.begin(), sines.end(), 0.0);
}

PYBIND11_PLUGIN(xtensor_python_test)
{
    pybind11::module m("xtensor_python_test", "Test module for xtensor python bindings");

    m.def("sum_of_sines", sum_of_sines,
        "Computes the sum of the sines of the values of the input array");

    return m.ptr();
}

Python code:

Python Code

import numpy as np
import xtensor_python_test as xt

a = np.arange(15).reshape(3, 5)
s = xt.sum_of_sines(v)
s

Outputs

1.2853996391883833

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

C++ code

#include "pybind11/pybind11.h"
#include "xtensor-python/pyvectorize.hpp"
#include <numeric>
#include <cmath>

namespace py = pybind11;

double scalar_func(double i, double j)
{
    return std::sin(i) - std::cos(j);
}

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

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

    return m.ptr();
}

Python code:

import numpy as np
import xtensor_python_test as xt

x = np.arange(15).reshape(3, 5)
y = [1, 2, 3, 4, 5]
z = xt.vectorized_func(x, y)
z

Outputs

[[-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]]
xtensor-cookiecutter

The xtensor-cookiecutter project helps extension authors create Python extension modules making use of xtensor.

It takes care of the initial work of generating a project skeleton with

  • A complete setup.py compiling the extension module

A few examples included in the resulting project including

  • A universal function defined from C++
  • A function making use of an algorithm from the STL on a numpy array
  • Unit tests
  • The generation of the HTML documentation with sphinx
Previous

© Copyright 2017, Johan Mabille and Sylvain Corlay.

Built with Sphinx using a theme provided by Read the Docs.