{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Jupyter\n", "\n", "
A Jupyter kernel for C++ based on the C++ interpreter cling and the native implementation of the Jupyter protocol xeus.
\n", "\n", "
\n", " This live demo is powered by\n", "
\n", "
\"Binder
\n", "
\"Jupyter
\n", "
\"Cling
\n", "
\n", "
\n", "
\n", " \n", " \n", "
\n", " This live demo may not be runnable from behind certain corporate proxies that block the websocket protocol.\n", "
\n", "
" ] }, { "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", "
" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Output and error streams\n", "\n", "`std::cout` and `std::cerr` are correctly redirected" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#include \n", "\n", "std::cout << \"some output\" << std::endl;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "std::cerr << \"some error\" << std::endl;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#include " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "throw std::runtime_error(\"BAAAD\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Omitting the `;` in the last statement of a cell gives an output" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "int i = 4" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "int j = 5;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "j" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Interpreting the C++ programming language\n", "\n", "`cling` has a broad support of the features of C++. You can define functions, classes, templates, etc ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "double sqr(double a)\n", "{\n", " return a * a;\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "double a = 2.5;\n", "double asqr = sqr(a);\n", "asqr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Classes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Foo\n", "{\n", "public:\n", "\n", " virtual ~Foo() {}\n", " \n", " virtual void print(double value) const\n", " {\n", " std::cout << \"Foo value = \" << value << std::endl;\n", " }\n", "};" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Foo bar;\n", "bar.print(1.2);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Polymorphism" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Bar : public Foo\n", "{\n", "public:\n", "\n", " virtual ~Bar() {}\n", " \n", " virtual void print(double value) const\n", " {\n", " std::cout << \"Bar value = \" << 2 * value << std::endl;\n", " }\n", "};" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Foo* bar2 = new Bar;\n", "bar2->print(1.2);\n", "delete bar2;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Templates" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#include \n", "\n", "template \n", "class FooT\n", "{\n", "public:\n", " \n", " explicit FooT(const T& t) : m_t(t) {}\n", " \n", " void print() const\n", " {\n", " std::cout << typeid(T).name() << \" m_t = \" << m_t << std::endl;\n", " }\n", " \n", "private:\n", " \n", " T m_t;\n", "};\n", "\n", "template <>\n", "class FooT\n", "{\n", "public:\n", " \n", " explicit FooT(const int& t) : m_t(t) {}\n", " \n", " void print() const\n", " {\n", " std::cout << \"m_t = \" << m_t << std::endl;\n", " }\n", " \n", "private:\n", " \n", " int m_t;\n", "};" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "FooT foot1(1.2);\n", "foot1.print();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "FooT foot2(4);\n", "foot2.print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## C++11 / C++14 support" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Foo11\n", "{\n", "public:\n", " \n", " Foo11() { std::cout << \"Foo11 default constructor\" << std::endl; }\n", " Foo11(const Foo11&) { std::cout << \"Foo11 copy constructor\" << std::endl; }\n", " Foo11(Foo11&&) { std::cout << \"Foo11 move constructor\" << std::endl; }\n", "};" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Foo11 f1;\n", "Foo11 f2(f1);\n", "Foo11 f3(std::move(f1));" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#include \n", "\n", "std::vector v = { 1, 2, 3};\n", "auto iter = ++v.begin();\n", "v" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "*iter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... and also lambda, universal references, `decltype`, etc ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Documentation and completion" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "?std::vector" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Using the `display_data` mechanism" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#include \n", "#include \n", "\n", "#include \"xtl/xbase64.hpp\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "std::ifstream fin(\"images/marie.png\", std::ios::binary);\n", "std::stringstream buffer;\n", "buffer << fin.rdbuf();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "xeus::xjson mime;\n", "mime[\"image/png\"] = xtl::base64encode(buffer.str());" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xeus::get_interpreter().display_data(\n", " std::move(mime),\n", " xeus::xjson::object(),\n", " xeus::xjson::object());" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "std::ifstream fwav(\"audio/audio.wav\", std::ios::binary);\n", "std::stringstream wav_buffer;\n", "wav_buffer << fwav.rdbuf();\n", "xeus::xjson audio_mime;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "audio_mime[\"text/html\"] =\n", " std::string(\"\";" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xeus::get_interpreter().display_data(\n", " std::move(audio_mime),\n", " xeus::xjson::object(),\n", " xeus::xjson::object());" ] } ], "metadata": { "kernelspec": { "display_name": "xeus C++14", "language": "", "name": "xeus-cling-cpp14" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "c++", "version": "" } }, "nbformat": 4, "nbformat_minor": 2 }