cadabra_in_ipython.nb

# Cadabra inside IPython/Jupyter¶

This is a short and simple Jupyter notebook shows how to use Cadabra functionality from with Jupyter. The first thing to do is to import the cadabra2 module. To save typing, we will import everything into the global namespace.

In [1]:

    from cadabra2 import *
    from IPython.display import display, Math, Latex

Expressions are declared by typing them as standard LaTeX, and feeding that into the Ex object. The result is a standard Python object.

In [9]:

    ex=Ex(r"A_{m n} B^{m n}")

Printing is still a bit messy in IPython/Jupyter, but you can get the nicely typeset form of the expression using the following:

In [10]:

    display(Math(str(ex)))

$$A_{m n} B^{m n}$$

Properties get assigned to symbols or patterns by using their Cadabra name,

In [12]:

    Symmetric(Ex(r"A_{m n}"))
    AntiSymmetric(Ex(r"B_{m n"))

Out[12]:

    Property::repr: AntiSymmetric

Cadabra algorithms are ordinary python functions. They always act on the expression that you pass it, and modify it in-place. Here's an example: the contraction of an antisymmetric and a symmetric tensor is zero:

In [17]:

    display(Math(str(canonicalise(ex))))

$$C_{m}\,^{p} D_{p n} B^{m n}$$

Let's do another example, with substitution. Note how indices get relabelled automatically.

In [20]:

    Indices(Ex(r"{m,n,p,q,r,s,t}"))
    ex=Ex(r"A_{m n} B^{m p} C_{p q}")
    display(Math(str(substitute(ex, Ex(r"A_{m n} -> D_{m q} D^{q}_{n}")))))

$$D_{m r} D^{r}\,_{n} B^{m p} C_{p q}$$

If you are interested in more of this, get in touch by emailing me at info@cadabra.science !

In [ ]: