{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Demo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can write Macaulay2 code as is.\n", "This includes comments, whitespaces, semicolons (or lack thereof), multiple-line statements, loading packages, printing to stdout, handling exceptions, etc.\n", "By default, all stdout for a cell and only the last output value are shown.\n", "This parallels IPython but the behaviour can be changed using cell magic." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Syntax highlighting is available on client-side only, and code completion will be available soon. Use `9,0` to quickly restart and clear output." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "o1 = {1, 9, 25, 49, 81, 121}\n", "\n", "o1 : List\n" ] } ], "source": [ "for i from 1 to random(20) list -- some auto-indentation is available\n", " if i%2==1 then i*i else continue" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can write text and mathematics directly along your code.\n", "Jupyter supports Markdown fully and $\\TeX$ to a degree.\n", "For example:\n", "\n", "The cell below defines the twisted cubic $C$ over the rationals in two ways —\n", "as $\\text{Proj}~\\mathbb{Q}[s^3, s^2t, st^2, t^3]$, and as the ideal of maximal minors of\n", "$\n", "g=\n", "\\begin{bmatrix}\n", "x_0& x_1& x_2\\\\\n", "x_1& x_2& x_3\n", "\\end{bmatrix}\n", "$.\n", "It then checks that the two definitions agree using Macaulay2's `assert`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "S = QQ[x_0..x_3] -- the embedding space\n", "R = QQ[s,t] -- PP^1 \n", "f = map(R,S,{s^3, s^2*t, s*t^2, t^3})\n", "g = matrix({{x_0,x_1,x_2},{x_1,x_2,x_3}})\n", "C = ker f\n", "assert(C == minors(2, g))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that failed assertions, do propagate to stdout." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "stdio:14:1:(3): error: assertion failed\n", "\n", "\n", "o9 = - 2*P + 3*P\n", " 0 1\n", "\n", "o9 : ProjectiveHilbertPolynomial\n" ] } ], "source": [ "assert(false)\n", "hilbertPolynomial C" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Long input is OK. " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " 501 501\n", "o10 = 20s t\n", "\n", "o10 : R\n" ] } ], "source": [ "q = poly \"s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Output can be suppressed as usual." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "degree C;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "M2JK implements a limited set of *magic* commands. The name is borrowed from IPython.\n", "It can be added as `--% = `.\n", "Because it's just a comment in the Macaulay2 language,\n", "you can safely export your Jupyter notebooks to `.m2` even if they cointain magic.\n", "You can add it anywhere in the cell — it executes before the actual code.\n", "Note, however, that magic is global, that is, it carries thru other cells.\n", "A final note is that a magic call use up a single Macaulay2 input. This is delibarate and might change soon." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, M2JK implements timeout guards using `--%timeout=`.\n", "A timeout exception fires if an individual Macaulay2 statement timeouts,\n", "but then the remainder of the cell is interrupted." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[magic succeeded] timeout = 1\n", "before the timeout fires\n", "\n", "\r\n", "o14 = [KERNEL ENFORCED TIMEOUT]" ] } ], "source": [ "--%timeout=1\n", "print(\"before the timeout fires\")\n", "sleep 5\n", "print(\"after the trigger\")\n", "sleep 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are several display modes accessible thru the `mode` magic.\n", "Apart from the default mode, `default`, you can use `texmacs`, `pretty` and `original`." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[magic succeeded] mode = default\n", "\n", "o16 = | x_0 x_1 x_2 |\n", " | x_1 x_2 x_3 |\n", "\n", " 2 3\n", "o16 : Matrix S <--- S\n" ] } ], "source": [ "--the default mode is called `default`\n", "--%mode=default\n", "g" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[magic succeeded] mode = texmacs" ] }, { "data": { "text/html": [ "\n", "o18\n", "=\n", "(x0x1x2x1x2x3)\n", "\n", "\n", "o18\n", ":\n", "Matrix\n", "\n", "\n", "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "--%mode=texmacs\n", "g" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[magic succeeded] mode = pretty" ] }, { "data": { "text/html": [ "
\n",
       "| x_0 x_1 x_2 |\n",
       "| x_1 x_2 x_3 |
\n",
       "        2       3\n",
       "Matrix S  <--- S\n",
       "
" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "--%mode=pretty\n", "--the output number is the number of the Jupyter cell\n", "g" ] }, { "cell_type": "code", "execution_count": 105, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "i21 : mode(false); << \"[magic succeeded] mode = original\";\n", "[magic succeeded] mode = original\n", "i22 : res C\n", "\n", " 1 3 2\n", "o22 = S <-- S <-- S <-- 0\n", " \n", " 0 1 2 3\n", "\n", "o22 : ChainComplex\n", "\n", "i23 : g\n", "\n", "o23 = | x_0 x_1 x_2 |\n", " | x_1 x_2 x_3 |\n", "\n", " 2 3\n", "o23 : Matrix S <--- S\n" ] } ], "source": [ "--%mode=original\n", "--display is the as in default, but does not limit to last output\n", "--this is useful when sharing code in text-only medium\n", "res C\n", "g" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[magic succeeded] mode = default" ] } ], "source": [ "--%mode=default" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can preset `timeout` and `mode`, as well as the M2-executable path.\n", "This is done by creating an INI file like so:\n", "```\n", "[magic]\n", "execpath = .../usr-dist/x86_64-Darwin-MacOS-10.14.1/bin/M2-binary\n", "timeout = 4\n", "mode = default\n", "```\n", "You can then tell M2JK to use it by setting the environment variable `$M2JK_CONFIG`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, more complex language tools are available in the same way they are in an interactive session.\n", "Note, however, that there are limits to the number of character you can transmit.\n", "Additionally, printing to file (including STDOUT, STDERR) is an extremely costly operation,\n", "so timeouts may occur." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", "o30 = /Applications/Macaulay2-1.12/share/Macaulay2/Core/option.m2:13:20-17:34: --source code:\n", " (opts,f) -> args -> (\n", " -- Common code for functions created with >> to process options and arguments.\n", " uncurry(f, override (opts,args))\n", " )\n", " )\n", " | symbol class value location of symbol\n", " | ------ ----- ----- ------------------ \n", " | f : FunctionClosure -- -*Function[/Applications/Macaulay2-1.12/share/Macaulay. /Applications/Macaulay2-1.12/share/Macaulay2/Core/option.m2:13:9-13:10\n", " | opts : OptionTable -- OptionTable{EHPVariables => (e, h, p)} /Applications/Macaulay2-1.12/share/Macaulay2/Core/option.m2:13:4-13:8 \n", " | GroupActing => GL\n", " | SVariable => s \n", " | -- function f:\n", " | /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:119:70-119:102: --source code:\n", " | methodFunction := new MethodFunctionWithOptions from (opts >> o -> arg -> innerMethodFunction(o,arg));\n", " | | symbol class value location of symbol\n", " | | ------ ----- ----- ------------------ \n", " | | opts : OptionTable -- OptionTable{EHPVariables => (e, h, p)} /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:116:38-116:42\n", " | | GroupActing => GL\n", " | | SVariable => s \n", " | | innerMethodFunction : CompiledFunctionClosure -- -*Function*- /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:118:12-118:31\n", " | | outputs : List -- {false, false, false, false} /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:116:43-116:50\n", " | | methopts : OptionTable -- OptionTable{Binary => false . /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:116:29-116:37\n", " | | Dispatch => {Thing, Thing, Thing, Thing} .\n", " | | Options => {EHPVariables => (e, h, p), SVa.\n", " | | TypicalValue => Thing .\n", " | | methodFunction : MethodFunctionWithOptions -- schurRing /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:119:6-119:20 \n", " | -- option table opts:\n", " | OptionTable{EHPVariables => (e, h, p)}\n", " | GroupActing => GL\n", " | SVariable => s\n" ] } ], "source": [ "loadPackage(\"SchurRings\")\n", "S = schurRing(QQ,s,4)\n", "p = S_(2,1,1)\n", "debug Core\n", "assert(rawCompare(raw p, raw p)==0)\n", "code schurRing" ] } ], "metadata": { "kernelspec": { "display_name": "M2", "language": "text/x-macaulay2", "name": "m2" }, "language_info": { "codemirror_mode": "macaulay2", "file_extension": ".m2", "mimetype": "text/x-macaulay2", "name": "Macaulay2" } }, "nbformat": 4, "nbformat_minor": 2 }