Demo

You can write Macaulay2 code as is. This includes comments, whitespaces, semicolons (or lack thereof), multiple-line statements, loading packages, printing to stdout, handling exceptions, etc. By default, all stdout for a cell and only the last output value are shown. This parallels IPython but the behaviour can be changed using cell magic.

Syntax highlighting is available on client-side only, and code completion will be available soon. Use 9,0 to quickly restart and clear output.

In [1]:
for i from 1 to random(20) list -- some auto-indentation is available
    if i%2==1 then i*i else continue
o1 = {1, 9, 25, 49, 81, 121}

o1 : List

You can write text and mathematics directly along your code. Jupyter supports Markdown fully and $\TeX$ to a degree. For example:

The cell below defines the twisted cubic $C$ over the rationals in two ways — as $\text{Proj}~\mathbb{Q}[s^3, s^2t, st^2, t^3]$, and as the ideal of maximal minors of $ g= \begin{bmatrix} x_0& x_1& x_2\\ x_1& x_2& x_3 \end{bmatrix} $. It then checks that the two definitions agree using Macaulay2's assert.

In [7]:
S = QQ[x_0..x_3] -- the embedding space
R = QQ[s,t]      -- PP^1   
f = map(R,S,{s^3, s^2*t, s*t^2, t^3})
g = matrix({{x_0,x_1,x_2},{x_1,x_2,x_3}})
C = ker f
assert(C == minors(2, g))

Note that failed assertions, do propagate to stdout.

In [9]:
assert(false)
hilbertPolynomial C
stdio:14:1:(3): error: assertion failed


o9 = - 2*P  + 3*P
          0      1

o9 : ProjectiveHilbertPolynomial

Long input is OK.

In [10]:
q = poly "s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501+s501t501"
         501 501
o10 = 20s   t

o10 : R

Output can be suppressed as usual.

In [11]:
degree C;

M2JK implements a limited set of magic commands. The name is borrowed from IPython. It can be added as --%<key> = <value>. Because it's just a comment in the Macaulay2 language, you can safely export your Jupyter notebooks to .m2 even if they cointain magic. You can add it anywhere in the cell — it executes before the actual code. Note, however, that magic is global, that is, it carries thru other cells. A final note is that a magic call use up a single Macaulay2 input. This is delibarate and might change soon.

For example, M2JK implements timeout guards using --%timeout=<duration in sec>. A timeout exception fires if an individual Macaulay2 statement timeouts, but then the remainder of the cell is interrupted.

In [14]:
--%timeout=1
print("before the timeout fires")
sleep 5
print("after the trigger")
sleep 10
[magic succeeded] timeout = 1
before the timeout fires


o14 = [KERNEL ENFORCED TIMEOUT]

There are several display modes accessible thru the mode magic. Apart from the default mode, default, you can use texmacs, pretty and original.

In [16]:
--the default mode is called `default`
--%mode=default
g
[magic succeeded] mode = default

o16 = | x_0 x_1 x_2 |
      | x_1 x_2 x_3 |

              2       3
o16 : Matrix S  <--- S
In [18]:
--%mode=texmacs
g
[magic succeeded] mode = texmacs
Out[18]:
o18 = (x0x1x2x1x2x3) o18 : Matrix
In [20]:
--%mode=pretty
--the output number is the number of the Jupyter cell
g
[magic succeeded] mode = pretty
Out[20]:
| x_0 x_1 x_2 |
| x_1 x_2 x_3 |
        2       3
Matrix S  <--- S
In [105]:
--%mode=original
--display is the as in default, but does not limit to last output
--this is useful when sharing code in text-only medium
res C
g
i21 : mode(false); << "[magic succeeded] mode = original";
[magic succeeded] mode = original
i22 : res C

       1      3      2
o22 = S  <-- S  <-- S  <-- 0
                            
      0      1      2      3

o22 : ChainComplex

i23 : g

o23 = | x_0 x_1 x_2 |
      | x_1 x_2 x_3 |

              2       3
o23 : Matrix S  <--- S
In [24]:
--%mode=default
[magic succeeded] mode = default

You can preset timeout and mode, as well as the M2-executable path. This is done by creating an INI file like so:

[magic]
execpath = .../usr-dist/x86_64-Darwin-MacOS-10.14.1/bin/M2-binary
timeout = 4
mode = default

You can then tell M2JK to use it by setting the environment variable $M2JK_CONFIG.

Finally, more complex language tools are available in the same way they are in an interactive session. Note, however, that there are limits to the number of character you can transmit. Additionally, printing to file (including STDOUT, STDERR) is an extremely costly operation, so timeouts may occur.

In [30]:
loadPackage("SchurRings")
S = schurRing(QQ,s,4)
p = S_(2,1,1)
debug Core
assert(rawCompare(raw p, raw p)==0)
code schurRing

o30 = /Applications/Macaulay2-1.12/share/Macaulay2/Core/option.m2:13:20-17:34: --source code:
        (opts,f) -> args -> (
             -- Common code for functions created with >> to process options and arguments.
             uncurry(f, override (opts,args))
             )
        )
      | symbol   class              value                                                   location of symbol
      | ------   -----              -----                                                   ------------------                                                    
      | f      : FunctionClosure -- -*Function[/Applications/Macaulay2-1.12/share/Macaulay. /Applications/Macaulay2-1.12/share/Macaulay2/Core/option.m2:13:9-13:10
      | opts   : OptionTable     -- OptionTable{EHPVariables => (e, h, p)}                  /Applications/Macaulay2-1.12/share/Macaulay2/Core/option.m2:13:4-13:8 
      |                                         GroupActing => GL
      |                                         SVariable => s                             
      | -- function f:
      | /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:119:70-119:102: --source code:
      |      methodFunction := new MethodFunctionWithOptions from (opts >> o -> arg -> innerMethodFunction(o,arg));
      | | symbol                class                        value                                                   location of symbol
      | | ------                -----                        -----                                                   ------------------                                                        
      | | opts                : OptionTable               -- OptionTable{EHPVariables => (e, h, p)}                  /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:116:38-116:42
      | |                                                                GroupActing => GL
      | |                                                                SVariable => s                             
      | | innerMethodFunction : CompiledFunctionClosure   -- -*Function*-                                            /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:118:12-118:31
      | | outputs             : List                      -- {false, false, false, false}                            /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:116:43-116:50
      | | methopts            : OptionTable               -- OptionTable{Binary => false                           . /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:116:29-116:37
      | |                                                                Dispatch => {Thing, Thing, Thing, Thing}  .
      | |                                                                Options => {EHPVariables => (e, h, p), SVa.
      | |                                                                TypicalValue => Thing                     .
      | | methodFunction      : MethodFunctionWithOptions -- schurRing                                               /Applications/Macaulay2-1.12/share/Macaulay2/Core/methods.m2:119:6-119:20 
      | -- option table opts:
      | OptionTable{EHPVariables => (e, h, p)}
      |             GroupActing => GL
      |             SVariable => s