588 lines
17 KiB
Plaintext
588 lines
17 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "472d1671-a068-4ce5-afae-076586f0b1b3",
|
||
"metadata": {},
|
||
"source": [
|
||
"<center>\n",
|
||
" <h1>Interactive Demos of C++ Libraries in the Browser</h1>\n",
|
||
" <h3>Powered by Xeus-Cpp-Lite and WebAssembly</h3>\n",
|
||
"</center>\n",
|
||
"\n",
|
||
"This notebook demonstrates several powerful C++ libraries running entirely in the browser using **xeus-cpp-lite**. The examples span symbolic computation, numerical analysis, interactive visualization, and more — all compiled to WebAssembly and ready to run in a JupyterLite environment.\n",
|
||
"\n",
|
||
"**Featured libraries:**\n",
|
||
"\n",
|
||
"1. **Scientific Computation (Symbolic & Numeric)**: `boost`, `symengine`, `openblas`\n",
|
||
"2. **Array based Computation through Xtensor-stack**: `xtl`, `xtensor`, `xsimd`, `xtensor-blas`\n",
|
||
"3. **Utilities & Miscellaneous**: `nlohmann_json`, and others"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "c5f00d53-7cc9-43fb-82c7-c69b631e7b92",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Scientific Computation (Symbolic & Numeric)\n",
|
||
"\n",
|
||
"Let’s begin with powerful C++ libraries for symbolic math, high-precision arithmetic, and efficient numerical computation using BLAS and LAPACK routines."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "7ef2ba09-bcc8-41e2-b503-3b21e60fdd3e",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Product of 98745636214564698 * 7459874565236544789 = \n",
|
||
"736630060025131838840151335215258722"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"#include <boost/multiprecision/cpp_int.hpp>\n",
|
||
"using namespace boost::multiprecision;\n",
|
||
"using namespace std;\n",
|
||
"\n",
|
||
"int128_t boost_product(long long A, long long B)\n",
|
||
"{\n",
|
||
" int128_t ans = (int128_t)A * B;\n",
|
||
" return ans;\n",
|
||
"}\n",
|
||
"\n",
|
||
"long long first = 98745636214564698;\n",
|
||
"long long second = 7459874565236544789;\n",
|
||
"cout << \"Product of \" << first << \" * \" << second << \" = \\n\"\n",
|
||
" << boost_product(first, second);"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"id": "d8e235a1-5d41-43f8-8908-9f76b4c6c1d7",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"#include <symengine/expression.h>\n",
|
||
"\n",
|
||
"using namespace SymEngine;\n",
|
||
"\n",
|
||
"Expression x(\"x\");\n",
|
||
"auto ex = pow(x + sqrt(Expression(2)), 6);"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"id": "82912670-8abf-4364-8bfb-b55e206fb153",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/latex": [
|
||
"$\\left(x + \\sqrt{2}\\right)^6$"
|
||
],
|
||
"text/plain": [
|
||
"(x + sqrt(2))**6"
|
||
]
|
||
},
|
||
"metadata": {},
|
||
"output_type": "display_data"
|
||
}
|
||
],
|
||
"source": [
|
||
"#include <xcpp/xdisplay.hpp>\n",
|
||
"xcpp::display(ex);"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"id": "f738315b-f1ed-48b8-ae71-250b3667c694",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/latex": [
|
||
"$8 + 24 \\sqrt{2} x + 40 \\sqrt{2} x^3 + 6 \\sqrt{2} x^5 + 60 x^2 + 30 x^4 + x^6$"
|
||
],
|
||
"text/plain": [
|
||
"8 + 24*sqrt(2)*x + 40*sqrt(2)*x**3 + 6*sqrt(2)*x**5 + 60*x**2 + 30*x**4 + x**6"
|
||
]
|
||
},
|
||
"metadata": {},
|
||
"output_type": "display_data"
|
||
}
|
||
],
|
||
"source": [
|
||
"auto expanded_ex = expand(ex);\n",
|
||
"xcpp::display(expanded_ex);"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"id": "f25ec9b7-951e-4494-b932-64d71a601127",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"LU Decomposition (OpenBLAS dgetrf_)\n",
|
||
"info: 0\n",
|
||
"\n",
|
||
"Factorized Matrix (A after dgetrf_):\n",
|
||
" 3.000000 6.000000 10.000000\n",
|
||
" 0.333333 2.000000 3.666667\n",
|
||
" 0.666667 0.500000 -0.500000\n",
|
||
"\n",
|
||
"Pivot Indices:\n",
|
||
"3 3 3 \n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"#include <iostream>\n",
|
||
"#include <iomanip>\n",
|
||
"\n",
|
||
"typedef int INTEGER;\n",
|
||
"typedef double DOUBLE;\n",
|
||
"\n",
|
||
"extern \"C\" {\n",
|
||
" int dgetrf_(const INTEGER* m, const INTEGER* n, DOUBLE* a,\n",
|
||
" const INTEGER* lda, INTEGER* ipiv, INTEGER* info);\n",
|
||
"}\n",
|
||
"\n",
|
||
"const INTEGER m = 3, n = 3, lda = 3;\n",
|
||
"DOUBLE A[9] = {\n",
|
||
" 1.0, 2.0, 3.0,\n",
|
||
" 4.0, 5.0, 6.0,\n",
|
||
" 7.0, 8.0, 10.0\n",
|
||
"}; // Column-major layout\n",
|
||
"\n",
|
||
"INTEGER ipiv[3];\n",
|
||
"INTEGER info;\n",
|
||
"\n",
|
||
"dgetrf_(&m, &n, A, &lda, ipiv, &info);\n",
|
||
"\n",
|
||
"// Output\n",
|
||
"std::cout << std::fixed << std::setprecision(6);\n",
|
||
"std::cout << \"LU Decomposition (OpenBLAS dgetrf_)\\n\";\n",
|
||
"std::cout << \"info: \" << info << \"\\n\\n\";\n",
|
||
"\n",
|
||
"std::cout << \"Factorized Matrix (A after dgetrf_):\\n\";\n",
|
||
"for (int i = 0; i < m; ++i) {\n",
|
||
" for (int j = 0; j < n; ++j)\n",
|
||
" std::cout << std::setw(10) << A[i + j * lda];\n",
|
||
" std::cout << \"\\n\";\n",
|
||
"}\n",
|
||
"\n",
|
||
"std::cout << \"\\nPivot Indices:\\n\";\n",
|
||
"for (int i = 0; i < std::min(m, n); ++i)\n",
|
||
" std::cout << ipiv[i] << \" \";\n",
|
||
"std::cout << \"\\n\";\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "6b7e64a2-546c-47a7-b4ed-95e98febd785",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Array based Computation through Xtensor-stack\n",
|
||
"\n",
|
||
"Explore NumPy-style array computing in C++ with Xtensor, Xtensor-BLAS, and Xsimd — enabling high-performance numerical and SIMD-accelerated workflows."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"id": "6496d5e7-d1e5-4437-be87-bb26b48ce735",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/html": [
|
||
"<table style='border-style:solid;border-width:1px;'><tbody><tr><td style='font-family:monospace;' title='0'><pre> 7.</pre></td></tr><tr><td style='font-family:monospace;' title='1'><pre> 11.</pre></td></tr><tr><td style='font-family:monospace;' title='2'><pre> 14.</pre></td></tr></tbody></table>"
|
||
]
|
||
},
|
||
"metadata": {},
|
||
"output_type": "display_data"
|
||
}
|
||
],
|
||
"source": [
|
||
"#include \"xtensor/containers/xarray.hpp\"\n",
|
||
"#include \"xtensor/io/xio.hpp\"\n",
|
||
"#include \"xtensor/views/xview.hpp\"\n",
|
||
"\n",
|
||
"xt::xarray<double> arr1 = {{1.0, 2.0, 3.0}, {2.0, 5.0, 7.0}, {2.0, 5.0, 7.0}};\n",
|
||
"xt::xarray<double> arr2{5.0, 6.0, 7.0};\n",
|
||
"\n",
|
||
"xcpp::display(xt::view(arr1, 1) + arr2);"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"id": "c748227d-4083-4c19-8f07-a449d41e67fe",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/html": [
|
||
"<table style='border-style:solid;border-width:1px;'><tbody><tr><td style='font-family:monospace;' title='(0, 0)'><pre>1</pre></td><td style='font-family:monospace;' title='(0, 1)'><pre>2</pre></td><td style='font-family:monospace;' title='(0, 2)'><pre>3</pre></td></tr><tr><td style='font-family:monospace;' title='(1, 0)'><pre>4</pre></td><td style='font-family:monospace;' title='(1, 1)'><pre>5</pre></td><td style='font-family:monospace;' title='(1, 2)'><pre>6</pre></td></tr><tr><td style='font-family:monospace;' title='(2, 0)'><pre>7</pre></td><td style='font-family:monospace;' title='(2, 1)'><pre>8</pre></td><td style='font-family:monospace;' title='(2, 2)'><pre>9</pre></td></tr></tbody></table>"
|
||
]
|
||
},
|
||
"metadata": {},
|
||
"output_type": "display_data"
|
||
}
|
||
],
|
||
"source": [
|
||
"xt::xarray<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};\n",
|
||
"arr.reshape({3, 3});\n",
|
||
"\n",
|
||
"xcpp::display(arr);"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"id": "74298554-9964-4633-bff3-e67d03a1797d",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"#include <iostream>\n",
|
||
"#include \"xtensor-blas/xlinalg.hpp\"\n",
|
||
"\n",
|
||
"xt::xtensor<double, 2> M = {{1.5, 0.5}, {0.7, 1.0}};"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"id": "22dadb84-d39e-4028-a919-febd64f4a60d",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Matrix rank: 2\n",
|
||
"Matrix inverse: \n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/html": [
|
||
"<table style='border-style:solid;border-width:1px;'><tbody><tr><td style='font-family:monospace;' title='(0, 0)'><pre> 0.869565</pre></td><td style='font-family:monospace;' title='(0, 1)'><pre>-0.434783</pre></td></tr><tr><td style='font-family:monospace;' title='(1, 0)'><pre>-0.608696</pre></td><td style='font-family:monospace;' title='(1, 1)'><pre> 1.304348</pre></td></tr></tbody></table>"
|
||
]
|
||
},
|
||
"metadata": {},
|
||
"output_type": "display_data"
|
||
},
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Eigen values: \n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/html": [
|
||
"<table style='border-style:solid;border-width:1px;'><tbody><tr><td style='font-family:monospace;' title='0'><pre> 1.892262+0.i</pre></td></tr><tr><td style='font-family:monospace;' title='1'><pre> 0.607738+0.i</pre></td></tr></tbody></table>"
|
||
]
|
||
},
|
||
"metadata": {},
|
||
"output_type": "display_data"
|
||
}
|
||
],
|
||
"source": [
|
||
"std::cout << \"Matrix rank: \" << xt::linalg::matrix_rank(M) << std::endl;\n",
|
||
"std::cout << \"Matrix inverse: \" << std::endl;\n",
|
||
"xcpp::display(xt::linalg::inv(M));\n",
|
||
"std::cout << \"Eigen values: \" << std::endl;\n",
|
||
"xcpp::display(xt::linalg::eigvals(M));"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 10,
|
||
"id": "d8e9e9e7-5def-4d77-b459-afb8002282ae",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"(3.300000, 7.700000, 12.100000, 16.500000)\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"#include <xsimd/xsimd.hpp>\n",
|
||
"\n",
|
||
"namespace xs = xsimd;\n",
|
||
"\n",
|
||
"// Define two SIMD float vectors using the wasm backend\n",
|
||
"xs::batch<float, xs::wasm> X = {1.2f, 3.4f, 5.6f, 7.8f};\n",
|
||
"xs::batch<float, xs::wasm> Y = {2.1f, 4.3f, 6.5f, 8.7f};\n",
|
||
"\n",
|
||
"// Perform SIMD addition\n",
|
||
"xs::batch<float, xs::wasm> Z = X + Y;\n",
|
||
"\n",
|
||
"// Print the result\n",
|
||
"std::cout << Z << std::endl;"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "0f0978c4-7ec4-4615-894a-c6558503d851",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Miscallaneous\n",
|
||
"\n",
|
||
"Feel free to explore other useful C++ libraries in the browser, like nlohmann_json, pugixml, xtl etc"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 11,
|
||
"id": "2ce27d4d-43f7-464c-a611-5400c3cdb735",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": [
|
||
"#include \"nlohmann/json.hpp\"\n",
|
||
"\n",
|
||
"nlohmann::json jsonObject;\n",
|
||
"\n",
|
||
"jsonObject[\"name\"] = \"John Doe\";\n",
|
||
"jsonObject[\"age\"] = 30;\n",
|
||
"jsonObject[\"is_student\"] = false;\n",
|
||
"jsonObject[\"skills\"] = {\"C++\", \"Python\", \"JavaScript\"};\n",
|
||
"\n",
|
||
"std::cout << jsonObject.dump(4) << std::endl;"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 12,
|
||
"id": "36b1b0f7-00d9-47bf-8543-985e27088d07",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 13,
|
||
"id": "b2647bb5-0c2e-4f12-b0ed-0c0f5c7ed65c",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"application/vnd.jupyter.widget-view+json": {
|
||
"model_id": "07f19b34aac74f97947e4e9b068ebe5f",
|
||
"version_major": 2,
|
||
"version_minor": 1
|
||
},
|
||
"text/plain": [
|
||
"A Jupyter widget with unique id: 07f19b34aac74f97947e4e9b068ebe5f"
|
||
]
|
||
},
|
||
"metadata": {},
|
||
"output_type": "display_data"
|
||
}
|
||
],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 14,
|
||
"id": "0f3e4dcb-5d9f-479b-b568-8d7625683946",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 15,
|
||
"id": "1eed2b39-b632-440c-9d3d-c8d377bc504c",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 16,
|
||
"id": "4d1797e5-3ce1-4737-9561-a87a7e82cce1",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"application/vnd.jupyter.widget-view+json": {
|
||
"model_id": "e34c45cc453240cebac33426cd7c448d",
|
||
"version_major": 2,
|
||
"version_minor": 1
|
||
},
|
||
"text/plain": [
|
||
"A Jupyter widget with unique id: e34c45cc453240cebac33426cd7c448d"
|
||
]
|
||
},
|
||
"metadata": {},
|
||
"output_type": "display_data"
|
||
}
|
||
],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "d26a70d2-9dc6-4ca0-b974-2cfd4249a8a7",
|
||
"metadata": {},
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 17,
|
||
"id": "e1fdb4b5-384a-42a1-80c1-ba2bcd515e01",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"{\n",
|
||
" \"age\": 30,\n",
|
||
" \"is_student\": false,\n",
|
||
" \"name\": \"John Doe\",\n",
|
||
" \"skills\": [\n",
|
||
" \"C++\",\n",
|
||
" \"Python\",\n",
|
||
" \"JavaScript\"\n",
|
||
" ]\n",
|
||
"}\n"
|
||
]
|
||
}
|
||
],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "ed69c0cd-c65a-47d7-9ee2-13502953e876",
|
||
"metadata": {
|
||
"trusted": true,
|
||
"vscode": {
|
||
"languageId": "c++"
|
||
}
|
||
},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "C++23",
|
||
"language": "cpp",
|
||
"name": "xcpp23"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": "text/x-c++src",
|
||
"file_extension": ".cpp",
|
||
"mimetype": "text/x-c++src",
|
||
"name": "C++",
|
||
"version": "23"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
} |