{ "cells": [ { "cell_type": "markdown", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "source": [ "# Welcome to ILNInteractive Demo\n", "\n", "### *ILNInteractive adds math support and visualizations for ILNumerics projects.*" ] }, { "cell_type": "markdown", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "source": [ "#### 1. First reference the ILNInteractive package\n", "\n", "This pulls the ILNInteractive package from nuget.org, including all required depedencies (such as ILNumerics itself, ILN2XPlot, XPlot.Plotly, etc.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "polyglot-notebook" } }, "outputs": [], "source": [ "#r \"nuget:ILNInteractive\"\n", "\n", "// Optional: reference a local package for debugging\n", "//#i \"nuget:D:\\Repos\\Thilo\\ILNInteractive\"\n", "//#r \"nuget:ILNInteractive\"" ] }, { "cell_type": "markdown", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "source": [ "#### 2. Create ILNumerics arrays and output them to the notebook\n", "\n", "All standard numeric types in ILNumerics are supported (sbyte, short, int, long, float, double, fcomplex and complex).\n", "\n", "- To write formatted output of the array to the notebook, simple return the array (```return a```) or reference the variable (```a```).\n", "- You can use standard indexers from ILNumerics to select the elements to display (```a[\":;3\"]``` or ```a[full, r(1,3)]```).\n", "- The output is truncated after ```ILNInteractiveOptions.MaxArrayElements``` number of elements (default: 50 per dimension)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "polyglot-notebook" } }, "outputs": [], "source": [ "// ILNInteractiveOptions.MaxArrayElements = 2;\n", "Array a = randn(2, 3, 4);\n", "//a[full, r(1,2), 1]\n", "return a[\":;:;2\"];" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "polyglot-notebook" } }, "outputs": [], "source": [ "Array b = ones(2, 3);\n", "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "polyglot-notebook" } }, "outputs": [], "source": [ "Array b = ccomplex(randn(4, 5), randn(4, 5));\n", "return b;" ] }, { "cell_type": "markdown", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "source": [ "#### 3. Visualize your data by utilizing ILNumerics plot types\n", "\n", "ILNInteractive support all possible plot types and arbitrary scenes. *However, at this point only LinePlot is converted to a fully interactive experience (via ILN2XPlot with XPlot plotly backend). All other scenes/plots are rendered as SVG graphics and embedded in the notebook output.*\n", "\n", "You can control the visualization output format and size by specifying\n", "- the format: ```ILNInteractiveOptions.GraphMode = ILNGraphMode.XPlotPlotly;``` or ```ILNGraphMode.Svg;``` or ```ILNGraphMode.Png;```\n", "- the size: ```ILNInteractiveOptions.GraphSize = new System.Drawing.Point(600, 400);```\n", "\n", "Also, for complex scene there is a configurable limit for the size of the rendered SVG. Above the threshold value, a bitmap (PNG) is used instead as fallback for faster rendering.\n", "The threshold value can be configured via ```ILNInteractiveOptions.GraphSvgSizeLimit = 4 * 1000 * 1000;``` (default: 4M Bytes)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "polyglot-notebook" } }, "outputs": [], "source": [ "//ILNInteractiveOptions.GraphMode = ILNGraphMode.Svg;\n", "//ILNInteractiveOptions.GraphSize = new System.Drawing.Point(600, 400);\n", "var scene = new Scene { new PlotCube { new LinePlot(tosingle(randn(1, 100))) } };\n", "return scene;" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "polyglot-notebook" } }, "outputs": [], "source": [ "// Example from: https://ilnumerics.net/surface-plots.html\n", "//ILNInteractiveOptions.GraphSvgSizeLimit = 4 * 1000 * 1000;\n", "//ILNInteractiveOptions.GraphSurfRotation = Matrix4.Rotation(new Vector3(1f, 0.23f, 1), 0.7f);\n", "var scene = new Scene {\n", " new PlotCube(twoDMode: false) {\n", " // Add a surface\n", " new Surface(SpecialData.sincf(30, 60, 2.5f)) { \n", " // Make thin transparent wireframes\n", " Wireframe = { Color = Color.FromArgb(50, Color.LightGray) },\n", " // Choose a different colormap\n", " Colormap = Colormaps.Jet, \n", " }\n", " }\n", " };\n", " // Rotate the plot cube \n", " scene.First().Rotation = Matrix4.Rotation(new Vector3(1f, 0.23f, 1), 0.7f);\n", " return scene;" ] }, { "cell_type": "markdown", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "source": [ "You may also save your graph as PNG, SVG or [PGFPlots/TIKZ](http://pgfplots.sourceforge.net/) format.\n", "\n", "- ```SaveAsPng(Scene scene, string filePath, Point? graphSize = null, int resolution = 300)```\n", "- ```SaveAsSvg(Scene scene, string filePath, Point? graphSize = null)```\n", "- ```SaveAsTikz(Scene scene, string filePath, Point? graphSize = null)```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "polyglot-notebook" } }, "outputs": [], "source": [ "var scene = new Scene { new PlotCube { new LinePlot(tosingle(randn(1, 100))) } };\n", "scene.SaveAsSvg(\"graph.svg\");" ] }, { "cell_type": "markdown", "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "source": [ "#### 4. Use _QuickPlot_ helpers in ILNInteractive\n", "\n", "ILNInteractive adds QuickPlot utilities, which help you creating simple plots easily. Supports float and double arrays.\n", "\n", "*LinePlot*\n", "- ```Plot(x, y)```\n", "- ```Plot(x, y, y2, ...)``` (individual x, y, etc. vectors)\n", "- ```Plot(xyPositions)``` (1, 2 or 3D positions as column vectors)\n", "\n", "*Surface*\n", "- ```Surf(xyzPositions)``` (matrix of size [m x n x [1|2|3]])\n", "- ```Surf(z, x, y)``` (each a vector of length n or a matrix of size [m by n])\n", "- ```Surf(zFunc, xmin, xmax, xlen, ymin, ymax, ylen)``` (zFunc is function from (x,y) => z, i.e. Func)\n", "\n", "For Surf() the orientation of the 3D plot cube can be configured via ```ILNInteractiveOptions.GraphSurfRotation```.\n", "\n", "QuickPlot also supports AxisScale and Labels via ```AxisScale [x|y|z]AxisScale``` and ```string [x|y|z]AxisLabel```, as well as labels for legend generation via ```IEnumerable labels```\n", "\n", "Examples for *LinePlot*\n", "- ```Plot(x, y, xAxisScale: AxisScale.Logarithmic, xAxisLabel: \"Log-scale X Axis\")``` for custom x axis scale and label\n", "- ```Plot(x, y, y2, labels: new[]{ \"line Y\", \"line Y2\" })``` for legend labels on line plot\n", "\n", "(*see code completion for all available parameters*)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "polyglot-notebook" } }, "outputs": [], "source": [ "Array x = arange(0.0, 99.0);\n", "Array y = randn(100, 1);\n", "Array y2 = randn(100, 1) + 2;\n", "//Plot(x, y, y2)\n", "//Plot(x, y, y2, labels: new[]{ \"line Y\", \"line Y2\" })\n", "Plot(x, y, y2, xAxisScale: AxisScale.Logarithmic, xAxisLabel: \"Log-scale X Axis\", labels: new[]{ \"line Y\", \"line Y2\" })\n", "//Plot(arange(0.0, 99.0), randn(100, 1), randn(100, 1) + 2, randn(100, 1) + 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "polyglot-notebook" } }, "outputs": [], "source": [ "Array xyz = SpecialData.sincf(30, 40, 2.5f);\n", "Surf(xyz)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "vscode": { "languageId": "polyglot-notebook" } }, "outputs": [], "source": [ "// Example from: https://ilnumerics.net/surface-plots.html\n", "Surf((x, y) => (float)(Math.Sin(x) * Math.Cos(y) * Math.Exp(-(x * x + y * y) / 46)))" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "polyglot_notebook": { "kernelInfo": { "defaultKernelName": "csharp", "items": [ { "aliases": [ "c#", "C#" ], "languageName": "C#", "name": "csharp" }, { "aliases": [], "name": ".NET" }, { "aliases": [ "f#", "F#" ], "languageName": "F#", "name": "fsharp" }, { "aliases": [], "languageName": "HTML", "name": "html" }, { "aliases": [], "languageName": "KQL", "name": "kql" }, { "aliases": [], "languageName": "Mermaid", "name": "mermaid" }, { "aliases": [ "powershell" ], "languageName": "PowerShell", "name": "pwsh" }, { "aliases": [], "languageName": "SQL", "name": "sql" }, { "aliases": [], "name": "value" }, { "aliases": [ "frontend" ], "name": "vscode" } ] } } }, "nbformat": 4, "nbformat_minor": 2 }