<commit_before>.. Copyright (c) 2016, <NAME>, <NAME> and <NAME>

   Distributed under the terms of the BSD 3-Clause License.

   The full license is in the file LICENSE, distributed with this software.

From numpy to xtensor
=====================

.. image:: numpy.svg
   :height: 100px
   :align: right

.. raw:: html

   <style>
   .rst-content table.docutils {
       width: 100%;
       table-layout: fixed;
       border: none;
   }

   table.docutils th {
       text-align: center;
   }

   table.docutils .line-block {
       margin-left: 0;
       margin-bottom: 0;
   }

   table.docutils code.literal {
       color: initial;
   }

   code.docutils {
       background: initial;
       border: none;
   }

   * {
       border: none;
   }

   .rst-content table.docutils thead {
       background-color: #d0e0e0;
   }

   .rst-content table.docutils td {
       border-bottom: none;
       border-left: none;
   }

   .rst-content table.docutils tr:hover {
       background-color: #d0e0e0;
   }

   .rst-content table.docutils:not(.field-list) tr:nth-child(2n-1):hover td {
       background-color: initial;
   }

   #linear-algebra table.docutils thead .row-odd {
       background: #ffdddd;
   }

   #linear-algebra tr:nth-child(2n-1) td {
       background: #f9f3f3;
   }

   #linear-algebra tr:hover {
       background: #ffdddd;
   }

   #linear-algebra tr:nth-child(2n-1):hover td {
       background-color: initial;
   }
   </style>

Containers
----------

Two container types are provided. ``xarray`` (dynamic number of dimensions) and ``xtensor``
(static number of dimensions).

+------------------------------------------------+------------------------------------------------+
|             Python 3 - numpy                   |               C++ 14 - xtensor                 |
+================================================+================================================+
| ``np.array([[3, 4], [5, 6]])``                 | | ``xt::xarray<double>({{3, 4}, {5, 6}})``     |
|                                                | | ``xt::xtensor<double, 2>({{3, 4}, {5, 6}})`` |
+------------------------------------------------+------------------------------------------------+
| ``arr.reshape([3, 4])``                        | ``arr.reshape({3, 4})``                        |
+------------------------------------------------+------------------------------------------------+
| ``arr.astype(np.float64)``                     | ``xt::cast<double>(arr)``                      |
+------------------------------------------------+------------------------------------------------+

Initializers
------------

Lazy helper functions return tensor expressions. Return types don't hold any value and are
evaluated upon access or assignment. They can be assigned to a container or directly used in
expressions.

+-----------------------------------------------+-----------------------------------------------+
|             Python 3 - numpy                  |               C++ 14 - xtensor                |
+===============================================+===============================================+
| ``np.linspace(1.0, 10.0, 100)``               | ``xt::linspace<double>(1.0, 10.0, 100)``      |
+-----------------------------------------------+-----------------------------------------------+
| ``np.logspace(2.0, 3.0, 4)``                  | ``xt::logspace<double>(2.0, 3.0, 4)``         |
+-----------------------------------------------+-----------------------------------------------+
| ``np.arange(3, 7)``                           | ``xt::arange(3, 7)``                          |
+-----------------------------------------------+-----------------------------------------------+
| ``np.eye(4)``                                 | ``xt::eye(4)``                                |
+-----------------------------------------------+-----------------------------------------------+
| ``np.zeros([3, 4])``                          | ``xt::zeros<double>({3, 4})``                 |
+-----------------------------------------------+-----------------------------------------------+
| ``np.ones([3, 4])``                           | ``xt::ones<double>({3, 4})``                  |
+-----------------------------------------------+-----------------------------------------------+
| ``np.empty([3, 4])``                          | ``xt::empty<double>({3, 4})``                 |
+-----------------------------------------------+-----------------------------------------------+
| ``np.meshgrid(x0, x1, x2, indexing='ij')``    | ``xt::meshgrid(x0, x1, x2)``                  |
+-----------------------------------------------+-----------------------------------------------+

xtensor's ``meshgrid`` implementation corresponds to numpy's ``'ij'`` indexing order.

Slicing and indexing
--------------------

+-----------------------------------------------------+-----------------------------------------------------+
|             Python 3 - numpy                        |                   C++ 14 - xtensor                  |
+=====================================================+=====================================================+
| ``a[3, 2]``                                         | ``a(3, 2)``                                         |
+-----------------------------------------------------+-----------------------------------------------------+
| ``a[:5, 1:]``                                       | ``xt::view(a, xt::range(_, 5), xt::range(1, _))``   |
+-----------------------------------------------------+-----------------------------------------------------+
| ``a[5:1:-1, :]``                                    | ``xt::view(a, xt::range(5, 1, -1), xt::all())``     |
+-----------------------------------------------------+-----------------------------------------------------+
| ``a[..., 3]``                                       | ``xt::strided_view(a, {xt::ellipsis, 3})``          |
+-----------------------------------------------------+-----------------------------------------------------+
| ``a[:, np.newaxis]``                                | ``xt::view(a, xt::all(), xt::newaxis())``           |
+-----------------------------------------------------+-----------------------------------------------------+

Broadcasting
------------<commit_msg>Added entry for view in cheatsheet<commit_after>.. Copyright (c) 2016, <NAME>, <NAME> and <NAME>

   Distributed under the terms of the BSD 3-Clause License.

   The full license is in the file LICENSE, distributed with this software.

From numpy to xtensor
=====================

.. image:: numpy.svg
   :height: 100px
   :align: right

.. raw:: html

   <style>
   .rst-content table.docutils {
       width: 100%;
       table-layout: fixed;
       border: none;
   }

   table.docutils th {
       text-align: center;
   }

   table.docutils .line-block {
       margin-left: 0;
       margin-bottom: 0;
   }

   table.docutils code.literal {
       color: initial;
   }

   code.docutils {
       background: initial;
       border: none;
   }

   * {
       border: none;
   }

   .rst-content table.docutils thead {
       background-color: #d0e0e0;
   }

   .rst-content table.docutils td {
       border-bottom: none;
       border-left: none;
   }

   .rst-content table.docutils tr:hover {
       background-color: #d0e0e0;
   }

   .rst-content table.docutils:not(.field-list) tr:nth-child(2n-1):hover td {
       background-color: initial;
   }

   #linear-algebra table.docutils thead .row-odd {
       background: #ffdddd;
   }

   #linear-algebra tr:nth-child(2n-1) td {
       background: #f9f3f3;
   }

   #linear-algebra tr:hover {
       background: #ffdddd;
   }

   #linear-algebra tr:nth-child(2n-1):hover td {
       background-color: initial;
   }
   </style>

Containers
----------

Two container types are provided. ``xarray`` (dynamic number of dimensions) and ``xtensor``
(static number of dimensions).

+------------------------------------------------+------------------------------------------------+
|             Python 3 - numpy                   |               C++ 14 - xtensor                 |
+================================================+================================================+
| ``np.array([[3, 4], [5, 6]])``                 | | ``xt::xarray<double>({{3, 4}, {5, 6}})``     |
|                                                | | ``xt::xtensor<double, 2>({{3, 4}, {5, 6}})`` |
+------------------------------------------------+------------------------------------------------+
| ``arr.reshape([3, 4])``                        | ``arr.reshape({3, 4})``                        |
+------------------------------------------------+------------------------------------------------+
| ``arr.astype(np.float64)``                     | ``xt::cast<double>(arr)``                      |
+------------------------------------------------+------------------------------------------------+

Initializers
------------

Lazy helper functions return tensor expressions. Return types don't hold any value and are
evaluated upon access or assignment. They can be assigned to a container or directly used in
expressions.

+-----------------------------------------------+-----------------------------------------------+
|             Python 3 - numpy                  |               C++ 14 - xtensor                |
+===============================================+===============================================+
| ``np.linspace(1.0, 10.0, 100)``               | ``xt::linspace<double>(1.0, 10.0, 100)``      |
+-----------------------------------------------+-----------------------------------------------+
| ``np.logspace(2.0, 3.0, 4)``                  | ``xt::logspace<double>(2.0, 3.0, 4)``         |
+-----------------------------------------------+-----------------------------------------------+
| ``np.arange(3, 7)``                           | ``xt::arange(3, 7)``                          |
+-----------------------------------------------+-----------------------------------------------+
| ``np.eye(4)``                                 | ``xt::eye(4)``                                |
+-----------------------------------------------+-----------------------------------------------+
| ``np.zeros([3, 4])``                          | ``xt::zeros<double>({3, 4})``                 |
+-----------------------------------------------+-----------------------------------------------+
| ``np.ones([3, 4])``                           | ``xt::ones<double>({3, 4})``                  |
+-----------------------------------------------+-----------------------------------------------+
| ``np.empty([3, 4])``                          | ``xt::empty<double>({3, 4})``                 |
+-----------------------------------------------+-----------------------------------------------+
| ``np.meshgrid(x0, x1, x2, indexing='ij')``    | ``xt::meshgrid(x0, x1, x2)``                  |
+-----------------------------------------------+-----------------------------------------------+

xtensor's ``meshgrid`` implementation corresponds to numpy's ``'ij'`` indexing order.

Slicing and indexing
--------------------

+-----------------------------------------------------+-----------------------------------------------------+
|             Python 3 - numpy                        |                   C++ 14 - xtensor                  |
+=====================================================+=====================================================+
| ``a[3, 2]``                                         | ``a(3, 2)``                                         |
+-----------------------------------------------------+-----------------------------------------------------+
| ``a[3]``                                            || ``xt::view(a, 3, xt::all())``                      |
|                                                     || ``xt::row(a, 3)``                                  |
+-----------------------------------------------------+-----------------------------------------------------+
| ``a[:, 2]``                                         || ``xt::view(a, xt::all(), 2)``                      |
|                                                     || ``xt::col(a, 2)``                                  |
+-----------------------------------------------------+-----------------------------------------------------+
| ``a[:5, 1:]``                                       | ``xt::view(a, xt::range(_, 5), xt::range(1, _))``   |
+-----------------------------------------------------+-----------------------------------------------------+
| ``a[5:1:-1, :]``                                    | ``xt::view(a, xt::range(5, 1, -1), xt::all())``     |
+-----------------------------------------------------+-----------------------------------------------------+
| ``a[..., 3]``                                       | ``xt::strided_view(a, {xt::ellipsis, 3})``          |
+-----------------------------------------------------+-----------------------------------------------------+
| ``a[:, np.newaxis]``                                | ``xt::view(a, xt::all(), xt::newaxis())``           |
+-----------------------------------------------------+-----------------------------------------------------+

Broadcasting
------------
{'fraction_non_alphanumeric': 0.47152018106374954, 'fraction_numerical': 0.01886080724254998, 'mean_word_length': 0.7229009617884066, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6096978', 'n_tokens_mistral': 3057, 'n_tokens_neox': 2752, 'n_words': 572}