{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ![xtensor](images/xtensor.png)\n", "\n", "
Multi-dimensional arrays with broadcasting and lazy computing.
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction\n", "\n", "`xtensor` is a C++ library meant for numerical analysis with multi-dimensional array expressions.\n", "\n", "`xtensor` provides\n", "\n", " - an extensible expression system enabling **lazy broadcasting**.\n", " - an API following the idioms of the **C++ standard library**.\n", " - tools to manipulate array expressions and build upon `xtensor`.\n", "\n", "The implementation of the containers of `xtensor` is inspired by [NumPy](http://www.numpy.org), the Python array programming library. **Adaptors** for existing data structures to be plugged into our expression system can easily be written. In fact, `xtensor` can be used to **process `numpy` data structures inplace** using Python's [buffer protocol](https://docs.python.org/3/c-api/buffer.html).\n", "\n", "`xtensor` requires a modern C++ compiler supporting C++14. The following C+ compilers are supported:\n", "\n", " - On Windows platforms, Visual C++ 2015 Update 2, or more recent\n", " - On Unix platforms, gcc 4.9 or a recent version of Clang" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Usage\n", "\n", "
\n", " \n", " \n", "
\n", " To run the selected code cell, hit
Shift + Enter
\n", "
\n", "
\n", "\n", "\n", "### Initialize a 2-D array and compute the sum of one of its rows and a 1-D array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "#include \"xtensor/xarray.hpp\"\n", "#include \"xtensor/xio.hpp\"\n", "#include \"xtensor/xview.hpp\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt::xarray arr1\n", " {{1.0, 2.0, 3.0},\n", " {2.0, 5.0, 7.0},\n", " {2.0, 5.0, 7.0}};\n", "\n", "xt::xarray arr2\n", " {5.0, 6.0, 7.0};\n", "\n", "xt::view(arr1, 1) + arr2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initialize a 1-D array and reshape it inplace" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "#include \"xtensor/xarray.hpp\"\n", "#include \"xtensor/xio.hpp\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt::xarray arr\n", " {1, 2, 3, 4, 5, 6, 7, 8, 9};\n", "\n", "arr.reshape({3, 3});\n", "\n", "arr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Broadcasting the ``xt::pow`` universal functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "#include \"xtensor/xarray.hpp\"\n", "#include \"xtensor/xmath.hpp\"\n", "#include \"xtensor/xio.hpp\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt::xarray arr3\n", " {1.0, 2.0, 3.0};\n", "\n", "xt::xarray arr4\n", " {4, 5, 6, 7};\n", "\n", "arr4.reshape({4, 1});\n", "\n", "xt::pow(arr3, arr4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Random arrays with the random module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "#include \"xtensor/xrandom.hpp\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt::xarray arr5 = xt::random::randn({4, 3});\n", "arr5" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "xt::random::randn({5, 3, 8, 10}) > 0.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using `linspace`, `arange`, `ones`, `zeros`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \"xtensor/xbuilder.hpp\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt::xarray ar = xt::linspace(0.0, 10.0, 12);\n", "ar.reshape({4, 3});\n", "ar" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt::xarray fones = xt::ones({2, 2});\n", "fones" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt::arange(1569325055)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using `xt::broadcast`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "#include \"xtensor/xbroadcast.hpp\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt::broadcast(xt::linspace(0.0, 10.0, 4),\n", " std::vector({3, 4}))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using standard algorithms with xexpressions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt::xarray frand = xt::random::randn({2, 2});\n", "\n", "// begin() and end() provide and iterator pair iterating over the xexpression in a row-major fashion\n", "std::cout << std::accumulate(frand.begin(), frand.end(), 0.0);\n", "\n", "frand" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iterating over a prescribed broadcasted shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// begin(shape) and end(shape) provide and iterator pair iterating\n", "// over the xexpression broadcasted to the prescrived shape in a row-major fashion\n", "std::vector shape = {3, 2, 2};\n", "std::cout << std::accumulate(frand.begin(shape), frand.end(shape), 0.0);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ![xtensor-blas](images/xtensor-blas.png)\n", "\n", "
Blas bindings for xtensor.
\n", "\n", "`xtensor-blas` is an extension to the xtensor library, offering bindings to BLAS and LAPACK libraries \n", "through cxxblas and cxxlapack from the [FLENS](https://github.com/michael-lehn/FLENS) project.\n", "\n", "`xtensor-blas` currently provides non-broadcasting `dot`, `norm` (1- and 2-norm for vectors), `inverse`, `solve`,\n", "`eig`, `cross`, `det`, `slogdet`, `matrix_rank`, `inv`, `cholesky`, `qr`, `svd` in the `xt::linalg` namespace (check the corresponding `xlinalg.hpp` header for the function signatures). The functions, and signatures, are trying to be 1-to-1 equivalent to NumPy.\n", "Low-level functions to interface with BLAS or LAPACK with xtensor containers are also offered \n", "in the `blas` and `lapack` namespace.\n", "\n", "`xtensor` and `xtensor-blas` require a modern C++ compiler supporting C++14. The following C++ compilers are supported:\n", "\n", " - On Windows platforms, Visual C++ 2015 Update 2, or more recent\n", " - On Unix platforms, gcc 4.9 or a recent version of Clang" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \"xtensor-blas/xlinalg.hpp\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt::xtensor m = {{1.5, 0.5}, {0.7, 1.0}};" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std::cout << \"Matrix rank: \" << std::endl << xt::linalg::matrix_rank(m);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std::cout << \"Matrix inverse: \" << std::endl << xt::linalg::inv(m);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std::cout << \"Eigen values: \" << std::endl << xt::linalg::eigvals(m);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xt::xarray arg1 = xt::arange(9);\n", "xt::xarray arg2 = xt::arange(18);\n", "arg1.reshape({3, 3});\n", "arg2.reshape({2, 3, 3});\n", "std::cout << xt::linalg::dot(arg1, arg2) << std::endl;" ] } ], "metadata": { "kernelspec": { "display_name": "C++14", "language": "C++14", "name": "xcpp14" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "c++", "version": "-std=c++14" } }, "nbformat": 4, "nbformat_minor": 4 }