Xeus-Cling

Repository: https://github.com/QuantStack/xeus-cling

Installation

conda install -c conda-forge ipyleaflet

A Jupyter kernel for C++ based on the cling C++ interpreter and the xeus native implementation of the Jupyter protocol, xeus.

Output and error streams

std::cout and std::cerr are redirected to the notebook frontend.


In [ ]:
#include <iostream>

std::cout << "some output" << std::endl;

In [ ]:
std::cerr << "some error" << std::endl;

In [ ]:
#include <stdexcept>

In [ ]:
throw std::runtime_error("Unknown exception");

Documentation and completion


In [ ]:
?xt::xtensor

Using the display_data mechanism

For a user-defined type T, the rich rendering in the notebook and JupyterLab can be enabled by by implementing the function xeus::xjson mime_bundle_repr(const T& im), which returns the JSON mime bundle for that type.

More documentation on the rich display system of Jupyter and Xeus-cling is available at https://xeus-cling.readthedocs.io/en/latest/rich_display.html

Image example


In [ ]:
#include <string>
#include <fstream>

#include "xtl/xbase64.hpp"
#include "xeus/xjson.hpp"

namespace im
{
    struct image
    {   
        inline image(const std::string& filename)
        {
            std::ifstream fin(filename, std::ios::binary);   
            m_buffer << fin.rdbuf();
        }
        
        std::stringstream m_buffer;
    };
    
    xeus::xjson mime_bundle_repr(const image& i)
    {
        auto bundle = xeus::xjson::object();
        bundle["image/png"] = xtl::base64encode(i.m_buffer.str());
        return bundle;
    }
}

In [ ]:
im::image marie("src/marie.png");
marie

Magics

Magics are special commands for the kernel that are not part of the C++ language.

They are defined with the symbol % for a line magic and %% for a cell magic.

More documentation for magics is available at https://xeus-cling.readthedocs.io/en/latest/magics.html.


In [ ]:
#include <algorithm>
#include <vector>

In [ ]:
std::vector<double> to_shuffle = {1, 2, 3, 4};

In [ ]:
%timeit std::random_shuffle(to_shuffle.begin(), to_shuffle.end());

xtensor: N-D arrays in C++

  • NumPy-style API
  • Idiomatic STL-style C++
  • Lazily evaluated

In [ ]:
#include <iostream>

#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"

In [ ]:
xt::xarray<int> arr
  {1, 2, 3, 4, 5, 6, 7, 8, 9};

arr.reshape({3, 3});

arr

Symengine: Symbolic Computing in C++

  • by the creators of Sympy
  • now a possible engine of sympy

In [ ]:
#include <symengine/expression.h>

using SymEngine::Expression;

Rich rendering of mathematical expressions.


In [ ]:
Expression x("x");

auto ex = pow(x + sqrt(Expression(2)), 10);
ex

In [ ]:
expand(ex)

Combining xtensor and Symengine


In [ ]:
#include <xtensor/xarray.hpp>
#include <xtensor/xbuilder.hpp>
#include <xtensor/xio.hpp>
#include <xtensor/xview.hpp>

In [ ]:
Expression y("z");

In [ ]:
xt::xarray<int> e = xt::linspace(0.0, 100.0, 6);

In [ ]:
e + y

In [ ]:
#include <xtensor/xreducer.hpp>

In [ ]:
auto s = (e + y) * xt::view(e + y, xt::all(), xt::newaxis());
s

In [ ]:
xt::sum(s)()

Widgets


In [ ]:
#include <xwidgets/xslider.hpp>
#include <xwidgets/xbutton.hpp>

In [ ]:
xw::slider<double> slider;
slider

In [ ]:
auto button = xw::button_generator()
    .description("Click me!")
    .finalize();
button